org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode Java Examples

The following examples show how to use org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CompositeNodeDataWithSchema.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
AbstractNodeDataWithSchema<?> addCompositeChild(final DataSchemaNode schema, final ChildReusePolicy policy) {
    final CompositeNodeDataWithSchema<?> newChild;

    if (schema instanceof ListSchemaNode) {
        newChild = new ListNodeDataWithSchema((ListSchemaNode) schema);
    } else if (schema instanceof LeafListSchemaNode) {
        newChild = new LeafListNodeDataWithSchema((LeafListSchemaNode) schema);
    } else if (schema instanceof ContainerSchemaNode) {
        newChild = new ContainerNodeDataWithSchema((ContainerSchemaNode) schema);
    } else if (schema instanceof YangModeledAnyxmlSchemaNode) {
        newChild = new YangModeledAnyXmlNodeDataWithSchema((YangModeledAnyxmlSchemaNode)schema);
    } else {
        newChild = new CompositeNodeDataWithSchema<>(schema);
    }

    return addCompositeChild(newChild, policy);
}
 
Example #2
Source File: DataSchemaContextNode.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
public static @Nullable DataSchemaContextNode<?> fromDataSchemaNode(final DataSchemaNode potential) {
    if (potential instanceof ContainerSchemaNode) {
        return new ContainerContextNode((ContainerSchemaNode) potential);
    } else if (potential instanceof ListSchemaNode) {
        return fromListSchemaNode((ListSchemaNode) potential);
    } else if (potential instanceof LeafSchemaNode) {
        return new LeafContextNode((LeafSchemaNode) potential);
    } else if (potential instanceof ChoiceSchemaNode) {
        return new ChoiceNodeContextNode((ChoiceSchemaNode) potential);
    } else if (potential instanceof LeafListSchemaNode) {
        return fromLeafListSchemaNode((LeafListSchemaNode) potential);
    } else if (potential instanceof AnydataSchemaNode) {
        return new AnydataContextNode((AnydataSchemaNode) potential);
    } else if (potential instanceof AnyxmlSchemaNode) {
        return new AnyXmlContextNode((AnyxmlSchemaNode) potential);
    }
    return null;
}
 
Example #3
Source File: Bug9244Test.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testDeviateReplaceOfImplicitSubstatements() throws Exception {
    final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/bugs/bug9244/");
    assertNotNull(schemaContext);

    final Module barModule = schemaContext.findModule("bar", Revision.of("2017-10-13")).get();
    final ContainerSchemaNode barCont = (ContainerSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "bar-cont"));
    assertNotNull(barCont);
    assertFalse(barCont.isConfiguration());

    final LeafListSchemaNode barLeafList = (LeafListSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "bar-leaf-list"));
    assertNotNull(barLeafList);
    final ElementCountConstraint constraint = barLeafList.getElementCountConstraint().get();
    assertEquals(5, constraint.getMinElements().intValue());
    assertEquals(10, constraint.getMaxElements().intValue());

    final LeafSchemaNode barLeaf = (LeafSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "bar-leaf"));
    assertNotNull(barLeaf);
    assertTrue(barLeaf.isMandatory());
}
 
Example #4
Source File: SchemaAwareApplyOperation.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
public static ModificationApplyOperation from(final DataSchemaNode schemaNode,
        final DataTreeConfiguration treeConfig) {
    if (treeConfig.getTreeType() == TreeType.CONFIGURATION) {
        checkArgument(schemaNode.isConfiguration(), "Supplied %s does not belongs to configuration tree.",
            schemaNode);
    }
    if (schemaNode instanceof ContainerSchemaNode) {
        return ContainerModificationStrategy.of((ContainerSchemaNode) schemaNode, treeConfig);
    } else if (schemaNode instanceof ListSchemaNode) {
        return fromListSchemaNode((ListSchemaNode) schemaNode, treeConfig);
    } else if (schemaNode instanceof ChoiceSchemaNode) {
        return new ChoiceModificationStrategy((ChoiceSchemaNode) schemaNode, treeConfig);
    } else if (schemaNode instanceof LeafListSchemaNode) {
        return MinMaxElementsValidation.from(new LeafSetModificationStrategy((LeafListSchemaNode) schemaNode,
            treeConfig));
    } else if (schemaNode instanceof LeafSchemaNode) {
        return new ValueNodeModificationStrategy<>(LeafNode.class, (LeafSchemaNode) schemaNode);
    } else if (schemaNode instanceof AnydataSchemaNode) {
        return new ValueNodeModificationStrategy<>(AnydataNode.class, (AnydataSchemaNode) schemaNode);
    } else if (schemaNode instanceof AnyxmlSchemaNode) {
        return new ValueNodeModificationStrategy<>(AnyxmlNode.class, (AnyxmlSchemaNode) schemaNode);
    }
    throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
}
 
Example #5
Source File: InstanceIdToNodes.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
static InstanceIdToNodes<?> fromDataSchemaNode(final DataSchemaNode potential) {
    if (potential instanceof ContainerSchemaNode) {
        return new InstanceIdToCompositeNodes.ContainerTransformation((ContainerSchemaNode) potential);
    } else if (potential instanceof ListSchemaNode) {
        return fromListSchemaNode((ListSchemaNode) potential);
    } else if (potential instanceof LeafSchemaNode) {
        return new InstanceIdToSimpleNodes.LeafNormalization((LeafSchemaNode) potential);
    } else if (potential instanceof ChoiceSchemaNode) {
        return new InstanceIdToCompositeNodes.ChoiceNodeNormalization((ChoiceSchemaNode) potential);
    } else if (potential instanceof LeafListSchemaNode) {
        return fromLeafListSchemaNode((LeafListSchemaNode) potential);
    } else if (potential instanceof AnydataSchemaNode) {
        return new AnydataNormalization((AnydataSchemaNode) potential);
    } else if (potential instanceof AnyxmlSchemaNode) {
        return new AnyXmlNormalization((AnyxmlSchemaNode) potential);
    }
    return null;
}
 
Example #6
Source File: Bug8675Test.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testLeafListAsRootElement() throws Exception {
    final LeafListSchemaNode topLevelLeafList = (LeafListSchemaNode) fooModule.findDataChildByName(
            QName.create(fooModule.getQNameModule(), "top-level-leaf-list")).get();

    final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/bug8675/foo-6.xml");

    final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);

    final NormalizedNodeResult result = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);

    final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, topLevelLeafList);
    xmlParser.parse(reader);

    final NormalizedNode<?, ?> transformedInput = result.getResult();
    assertNotNull(transformedInput);
}
 
Example #7
Source File: AbstractNonEmptyLeafListEffectiveStatement.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
AbstractNonEmptyLeafListEffectiveStatement(final LeafListStatement declared, final SchemaPath path, final int flags,
        final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
        final LeafListSchemaNode original, final ElementCountConstraint elementCountConstraint) {
    super(declared, path, flags, substatements);
    this.original = original;
    this.elementCountConstraint = elementCountConstraint;
}
 
Example #8
Source File: ImmutableOrderedLeafSetNodeSchemaAwareBuilder.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafListSchemaNode schema,
        final LeafSetNode<T> node) {
    if (!(node instanceof ImmutableOrderedLeafSetNode<?>)) {
        throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
    }

    return new ImmutableOrderedLeafSetNodeSchemaAwareBuilder<>(schema, (ImmutableOrderedLeafSetNode<T>) node);
}
 
Example #9
Source File: ImmutableLeafSetNodeSchemaAwareBuilder.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafListSchemaNode schema,
        final LeafSetNode<T> node) {
    if (!(node instanceof ImmutableLeafSetNode<?>)) {
        throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
    }

    return new ImmutableLeafSetNodeSchemaAwareBuilder<>(schema, (ImmutableLeafSetNode<T>) node);
}
 
Example #10
Source File: BuilderTest.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setup() throws URISyntaxException {
    final File leafRefTestYang = new File(getClass().getResource("/builder-test/immutable-ordered-map-node.yang")
            .toURI());
    final SchemaContext schema = YangParserTestUtils.parseYangFiles(leafRefTestYang);
    final Module module = schema.getModules().iterator().next();
    final DataSchemaNode root = module.findDataChildByName(ROOT_CONTAINER).get();
    list = (ListSchemaNode)((ContainerSchemaNode) root).findDataChildByName(LIST_MAIN).get();
    leafList = (LeafListSchemaNode)((ContainerSchemaNode) root).findDataChildByName(LEAF_LIST_MAIN).get();
}
 
Example #11
Source File: BuilderTest.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test(expected = NullPointerException.class)
public void immutableLeafSetNodeBuilderExceptionTest() {
    final LeafSetNode<?> leafSetNode = ImmutableLeafSetNodeBuilder.create(1)
            .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST).build();
    assertNotNull(leafSetNode);
    ImmutableLeafSetNodeSchemaAwareBuilder.create(mock(LeafListSchemaNode.class), leafSetNode).build();
}
 
Example #12
Source File: SchemaAwareXMLStreamNormalizedNodeStreamWriter.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void endNode() throws IOException {
    final Object schema = tracker.endNode();
    if (schema instanceof ListSchemaNode || schema instanceof LeafListSchemaNode) {
        // For lists, we only emit end element on the inner frame
        final Object parent = tracker.getParent();
        if (parent == schema) {
            endElement();
        }
    } else if (schema instanceof ContainerSchemaNode || schema instanceof LeafSchemaNode
            || schema instanceof AnydataSchemaNode || schema instanceof AnyxmlSchemaNode) {
        endElement();
    }
}
 
Example #13
Source File: XmlParserStream.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * This method parses the XML source and emits node events into a NormalizedNodeStreamWriter based on the
 * YANG-modeled data contained in the XML source.
 *
 * @param reader
 *              StAX reader which is to used to walk through the XML source
 * @return
 *              instance of XmlParserStream
 * @throws XMLStreamException
 *              if a well-formedness error or an unexpected processing condition occurs while parsing the XML
 * @throws URISyntaxException
 *              if the namespace URI of an XML element contains a syntax error
 * @throws IOException
 *              if an error occurs while parsing the value of an anyxml node
 * @throws SAXException
 *              if an error occurs while parsing the value of an anyxml node
 */
public XmlParserStream parse(final XMLStreamReader reader) throws XMLStreamException, URISyntaxException,
        IOException, SAXException {
    if (reader.hasNext()) {
        reader.nextTag();
        final AbstractNodeDataWithSchema<?> nodeDataWithSchema;
        if (parentNode instanceof ContainerSchemaNode) {
            nodeDataWithSchema = new ContainerNodeDataWithSchema((ContainerSchemaNode) parentNode);
        } else if (parentNode instanceof ListSchemaNode) {
            nodeDataWithSchema = new ListNodeDataWithSchema((ListSchemaNode) parentNode);
        } else if (parentNode instanceof YangModeledAnyxmlSchemaNode) {
            nodeDataWithSchema = new YangModeledAnyXmlNodeDataWithSchema((YangModeledAnyxmlSchemaNode) parentNode);
        } else if (parentNode instanceof AnyxmlSchemaNode) {
            nodeDataWithSchema = new AnyXmlNodeDataWithSchema((AnyxmlSchemaNode) parentNode);
        } else if (parentNode instanceof LeafSchemaNode) {
            nodeDataWithSchema = new LeafNodeDataWithSchema((LeafSchemaNode) parentNode);
        } else if (parentNode instanceof LeafListSchemaNode) {
            nodeDataWithSchema = new LeafListNodeDataWithSchema((LeafListSchemaNode) parentNode);
        } else if (parentNode instanceof AnydataSchemaNode) {
            nodeDataWithSchema = new AnydataNodeDataWithSchema((AnydataSchemaNode) parentNode);
        } else {
            throw new IllegalStateException("Unsupported schema node type " + parentNode.getClass() + ".");
        }

        read(reader, nodeDataWithSchema, reader.getLocalName());
        nodeDataWithSchema.write(writer);
    }

    return this;
}
 
Example #14
Source File: LeafRefUtils.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
private static Deque<QNameWithPredicate> schemaPathToXPathQNames(final SchemaPath nodePath, final Module module) {
    final Deque<QNameWithPredicate> xpath = new LinkedList<>();
    final Iterator<QName> nodePathIterator = nodePath.getPathFromRoot().iterator();

    DataNodeContainer currenDataNodeContainer = module;
    while (nodePathIterator.hasNext()) {
        final QName qname = nodePathIterator.next();
        final DataSchemaNode child = currenDataNodeContainer.getDataChildByName(qname);

        if (child instanceof DataNodeContainer) {
            if (!(child instanceof CaseSchemaNode)) {
                xpath.add(new SimpleQNameWithPredicate(qname));
            }
            currenDataNodeContainer = (DataNodeContainer) child;
        } else if (child instanceof ChoiceSchemaNode) {
            if (nodePathIterator.hasNext()) {
                currenDataNodeContainer = ((ChoiceSchemaNode) child).findCase(nodePathIterator.next()).orElse(null);
            } else {
                break;
            }
        } else if (child instanceof LeafSchemaNode || child instanceof LeafListSchemaNode) {
            xpath.add(new SimpleQNameWithPredicate(qname));
            break;
        } else if (child == null) {
            throw new IllegalArgumentException("No child " + qname + " found in node container "
                    + currenDataNodeContainer + " in module " + module.getName());
        } else {
            throw new IllegalStateException("Illegal schema node type in the path: " + child.getClass());
        }
    }

    return xpath;
}
 
Example #15
Source File: Bug6880Test.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void valid10Test() throws Exception {
    final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6880/foo.yang");
    assertNotNull(schemaContext);

    final SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(schemaContext,
            SchemaPath.create(true, QName.create(FOO_NS, "my-leaf-list")));
    assertTrue(findDataSchemaNode instanceof LeafListSchemaNode);
    final LeafListSchemaNode myLeafList = (LeafListSchemaNode) findDataSchemaNode;

    final Collection<? extends Object> defaults = myLeafList.getDefaults();
    assertEquals(2, defaults.size());
    assertTrue(defaults.contains("my-default-value-1") && defaults.contains("my-default-value-2"));
}
 
Example #16
Source File: YinFileLeafListStmtTest.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testLeafList() {
    final Module testModule = TestUtils.findModule(context, "ietf-netconf-monitoring").get();

    final LeafListSchemaNode leafList = (LeafListSchemaNode) testModule.findDataChildByName(
        QName.create(testModule.getQNameModule(), "netconf-state"),
        QName.create(testModule.getQNameModule(), "capabilities"),
        QName.create(testModule.getQNameModule(), "capability")).get();
    assertNotNull(leafList);
    assertEquals("uri", leafList.getType().getQName().getLocalName());
    assertEquals(Optional.of("List of NETCONF capabilities supported by the server."), leafList.getDescription());
    assertFalse(leafList.isUserOrdered());
}
 
Example #17
Source File: LeafListNodeDataWithSchema.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void write(final NormalizedNodeStreamWriter writer, final StreamWriterMetadataExtension metaWriter)
        throws IOException {
    final LeafListSchemaNode schema = getSchema();
    writer.nextDataSchemaNode(schema);
    if (schema.isUserOrdered()) {
        writer.startOrderedLeafSet(provideNodeIdentifier(), childSizeHint());
    } else {
        writer.startLeafSet(provideNodeIdentifier(), childSizeHint());
    }
    super.write(writer, metaWriter);
    writer.endNode();
}
 
Example #18
Source File: LeafsetEntryInterner.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Return a {@link LeafsetEntryInterner} for a particular schema. Interner instances must be used only for leafset
 * entries for that particular schema, otherwise they may produce unexpected results.
 *
 * @param schema Schema of the parent leaf set
 * @return An interner instance, or null if the leafset's type should not be interned.
 */
public static @Nullable LeafsetEntryInterner forSchema(final @Nullable LeafListSchemaNode schema) {
    if (schema != null) {
        final TypeDefinition<?> type = schema.getType();
        if (type instanceof BooleanTypeDefinition || type instanceof EnumTypeDefinition
                || type instanceof IdentityrefTypeDefinition) {
            return INSTANCE;
        }
    }
    return null;
}
 
Example #19
Source File: MockNodeBuilder.java    From yang2swagger with Eclipse Public License 1.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public MockNodeBuilder param(String name) {

    LeafListSchemaNode leaf = mock(LeafListSchemaNode.class);
    QName qname = QName.create(module, name);

    when(leaf.getQName()).thenReturn(qname);
    when(leaf.getType()).thenReturn((TypeDefinition)BaseTypes.stringType());
    params.add(leaf);
    names.add(qname);
    return this;
}
 
Example #20
Source File: ImmutableOrderedLeafSetNodeSchemaAwareBuilder.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
public ImmutableOrderedLeafSetNodeSchemaAwareBuilder(final LeafListSchemaNode schema,
        final ImmutableOrderedLeafSetNode<T> node) {
    super(node);
    this.schema = requireNonNull(schema);
    // FIXME: Preconditions.checkArgument(schema.getQName().equals(node.getIdentifier()));
    super.withNodeIdentifier(new NodeIdentifier(schema.getQName()));
}
 
Example #21
Source File: SchemaTracker.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
public LeafListSchemaNode leafSetEntryNode(final QName qname) {
    final Object parent = getParent();
    if (parent instanceof LeafListSchemaNode) {
        return (LeafListSchemaNode) parent;
    }

    final SchemaNode child = SchemaUtils.findDataChildSchemaByQName((SchemaNode) parent, qname);
    checkArgument(child instanceof LeafListSchemaNode,
        "Node %s is neither a leaf-list nor currently in a leaf-list", child);
    return (LeafListSchemaNode) child;
}
 
Example #22
Source File: DeviationResolutionTest.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testDeviateDelete() throws ReactorException {
    final SchemaContext schemaContext = StmtTestUtils.parseYangSources(
            sourceForResource("/deviation-resolution-test/deviation-delete/foo.yang"),
            sourceForResource("/deviation-resolution-test/deviation-delete/bar.yang"));
    assertNotNull(schemaContext);

    final Module barModule = schemaContext.findModule("bar", Revision.of("2017-01-20")).get();
    final LeafSchemaNode myLeaf = (LeafSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-leaf"));
    assertNotNull(myLeaf);

    assertEquals(Optional.empty(), myLeaf.getType().getDefaultValue());
    assertEquals(Optional.empty(), myLeaf.getType().getUnits());
    assertEquals(0, myLeaf.getUnknownSchemaNodes().size());

    final LeafListSchemaNode myLeafList = (LeafListSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-leaf-list"));
    assertNotNull(myLeafList);

    assertEquals(0, myLeafList.getDefaults().size());
    assertEquals(0, myLeafList.getMustConstraints().size());

    final ListSchemaNode myList = (ListSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-list"));
    assertNotNull(myList);

    assertEquals(0, myList.getUniqueConstraints().size());
    assertEquals(0, myList.getUnknownSchemaNodes().size());

    final ContainerSchemaNode myCont = (ContainerSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-cont"));
    assertNotNull(myCont);

    final LeafSchemaNode myAugLeaf = (LeafSchemaNode) myCont.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-aug-leaf"));
    assertNotNull(myAugLeaf);
    assertEquals(Optional.empty(), myAugLeaf.getType().getDefaultValue());
    assertEquals(Optional.empty(), myAugLeaf.getType().getUnits());
    assertEquals(0, myAugLeaf.getMustConstraints().size());
    assertEquals(0, myAugLeaf.getUnknownSchemaNodes().size());

    final LeafSchemaNode myUsedLeaf = (LeafSchemaNode) myCont.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-used-leaf"));
    assertNotNull(myUsedLeaf);
    assertEquals(Optional.empty(), myUsedLeaf.getType().getDefaultValue());
    assertEquals(Optional.empty(), myUsedLeaf.getType().getUnits());
    assertEquals(0, myUsedLeaf.getMustConstraints().size());
    assertEquals(0, myUsedLeaf.getUnknownSchemaNodes().size());
}
 
Example #23
Source File: LeafSetModificationStrategy.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
LeafSetModificationStrategy(final LeafListSchemaNode schema, final DataTreeConfiguration treeConfig) {
    super(schema.isUserOrdered() ? ORDERED_SUPPORT : UNORDERED_SUPPORT, treeConfig,
            new ValueNodeModificationStrategy<>(LeafSetEntryNode.class, schema));
}
 
Example #24
Source File: Builders.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> leafSetBuilder(final LeafListSchemaNode schema,
        final LeafSetNode<T> node) {
    return ImmutableLeafSetNodeSchemaAwareBuilder.create(schema, node);
}
 
Example #25
Source File: ElementCountConstraintsTest.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testElementCountConstraints() throws ParseException, ReactorException, URISyntaxException, IOException,
        YangSyntaxErrorException {
    final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
            .addSource(YangStatementStreamSource.create(
                YangTextSchemaSource.forResource("/constraint-definitions-test/foo.yang")))
            .buildEffective();
    assertNotNull(schemaContext);

    final Module testModule = schemaContext.findModule("foo", Revision.of("2016-09-20")).get();
    final LeafListSchemaNode constrainedLeafList1 = (LeafListSchemaNode) testModule.getDataChildByName(
            QName.create(testModule.getQNameModule(), "constrained-leaf-list-1"));
    assertNotNull(constrainedLeafList1);
    ElementCountConstraint constraints1 = constrainedLeafList1.getElementCountConstraint().get();

    final LeafListSchemaNode constrainedLeafList2 = (LeafListSchemaNode) testModule.getDataChildByName(
            QName.create(testModule.getQNameModule(), "constrained-leaf-list-2"));
    assertNotNull(constrainedLeafList2);
    ElementCountConstraint constraints2 = constrainedLeafList2.getElementCountConstraint().get();

    assertEquals(constraints1.hashCode(), constraints2.hashCode());
    assertEquals(constraints1, constraints2);

    final LeafListSchemaNode constrainedLeafList3 = (LeafListSchemaNode) testModule.getDataChildByName(
            QName.create(testModule.getQNameModule(), "constrained-leaf-list-3"));
    assertNotNull(constrainedLeafList3);
    ElementCountConstraint constraints3 = constrainedLeafList3.getElementCountConstraint().get();

    assertNotEquals(constraints2.hashCode(), constraints3.hashCode());
    assertNotEquals(constraints2, constraints3);

    final LeafListSchemaNode constrainedLeafList4 = (LeafListSchemaNode) testModule.getDataChildByName(
            QName.create(testModule.getQNameModule(), "constrained-leaf-list-4"));
    assertNotNull(constrainedLeafList4);
    ElementCountConstraint constraints4 = constrainedLeafList4.getElementCountConstraint().get();

    assertNotEquals(constraints3.hashCode(), constraints4.hashCode());
    assertNotEquals(constraints3, constraints4);

    assertEquals("ElementCountConstraint{minElements=50, maxElements=100}", constraints4.toString());
}
 
Example #26
Source File: Builders.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> leafSetBuilder(final LeafListSchemaNode schema) {
    return ImmutableLeafSetNodeSchemaAwareBuilder.create(schema);
}
 
Example #27
Source File: Builders.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> orderedLeafSetBuilder(final LeafListSchemaNode schema) {
    return ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(schema);
}
 
Example #28
Source File: YangParserSimpleTest.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testParseContainer() {
    final ContainerSchemaNode nodes = (ContainerSchemaNode) testModule
            .getDataChildByName(QName.create(testModule.getQNameModule(), "nodes"));
    // test SchemaNode args
    assertEquals(SN_NODES, nodes.getQName());
    assertEquals(SN_NODES_PATH, nodes.getPath());
    assertEquals(Optional.of("nodes collection"), nodes.getDescription());
    assertEquals(Optional.of("nodes ref"), nodes.getReference());
    assertEquals(Status.CURRENT, nodes.getStatus());
    assertEquals(0, nodes.getUnknownSchemaNodes().size());
    // test DataSchemaNode args
    assertFalse(nodes.isAugmenting());
    assertFalse(nodes.isConfiguration());

    // constraints
    assertEquals("class != 'wheel'", nodes.getWhenCondition().get().getOriginalString());
    final Collection<? extends MustDefinition> mustConstraints = nodes.getMustConstraints();
    assertEquals(2, mustConstraints.size());

    final String must1 = "ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)";
    final String errMsg1 = "An atm MTU must be  64 .. 17966";
    final String must2 = "ifId != 0";

    boolean found1 = false;
    boolean found2 = false;
    for (final MustDefinition must : mustConstraints) {
        if (must1.equals(must.toString())) {
            found1 = true;
            assertEquals(Optional.of(errMsg1), must.getErrorMessage());
        } else if (must2.equals(must.toString())) {
            found2 = true;
            assertFalse(must.getErrorMessage().isPresent());
            assertFalse(must.getErrorAppTag().isPresent());
            assertFalse(must.getDescription().isPresent());
            assertFalse(must.getReference().isPresent());
        }
    }
    assertTrue(found1);
    assertTrue(found2);

    assertTrue(nodes.isPresenceContainer());

    // typedef
    final Collection<? extends TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
    assertEquals(1, typedefs.size());
    final TypeDefinition<?> nodesType = typedefs.iterator().next();
    final QName typedefQName = QName.create(SN, "nodes-type");
    assertEquals(typedefQName, nodesType.getQName());
    assertEquals(SN_NODES_PATH.createChild(QName.create(SN, "nodes-type")), nodesType.getPath());
    assertFalse(nodesType.getDescription().isPresent());
    assertFalse(nodesType.getReference().isPresent());
    assertEquals(Status.CURRENT, nodesType.getStatus());
    assertEquals(0, nodesType.getUnknownSchemaNodes().size());

    // child nodes
    // total size = 8: defined 6, inserted by uses 2
    assertEquals(8, nodes.getChildNodes().size());
    final LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName(QName.create(
        testModule.getQNameModule(), "added"));
    assertEquals(createPath("nodes", "added"), added.getPath());
    assertEquals(createPath("mytype"), added.getType().getPath());

    final ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName(QName.create(
        testModule.getQNameModule(), "links"));
    assertFalse(links.isUserOrdered());

    final Collection<? extends GroupingDefinition> groupings = nodes.getGroupings();
    assertEquals(1, groupings.size());
    final GroupingDefinition nodeGroup = groupings.iterator().next();
    final QName groupQName = QName.create(SN, "node-group");
    assertEquals(groupQName, nodeGroup.getQName());
    final SchemaPath nodeGroupPath = SN_NODES_PATH.createChild(groupQName);
    assertEquals(nodeGroupPath, nodeGroup.getPath());

    final Collection<? extends UsesNode> uses = nodes.getUses();
    assertEquals(1, uses.size());
    final UsesNode use = uses.iterator().next();
    assertEquals(nodeGroup, use.getSourceGrouping());
}
 
Example #29
Source File: DeviationResolutionTest.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testDeviateAdd() throws ReactorException {
    final SchemaContext schemaContext = StmtTestUtils.parseYangSources(
            sourceForResource("/deviation-resolution-test/deviation-add/foo.yang"),
            sourceForResource("/deviation-resolution-test/deviation-add/bar.yang"));
    assertNotNull(schemaContext);

    final Module barModule = schemaContext.findModule("bar", Revision.of("2017-01-20")).get();
    final LeafListSchemaNode myLeafList = (LeafListSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-leaf-list"));
    assertNotNull(myLeafList);

    assertFalse(myLeafList.isConfiguration());
    assertEquals(3, myLeafList.getDefaults().size());

    final ElementCountConstraint constraint = myLeafList.getElementCountConstraint().get();
    assertEquals(10, constraint.getMaxElements().intValue());
    assertEquals(5, constraint.getMinElements().intValue());
    assertNotNull(myLeafList.getType().getUnits());

    final ListSchemaNode myList = (ListSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-list"));
    assertNotNull(myList);
    assertEquals(2, myList.getUniqueConstraints().size());

    final ChoiceSchemaNode myChoice = (ChoiceSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-choice"));
    assertNotNull(myChoice);
    assertEquals("c2", myChoice.getDefaultCase().get().getQName().getLocalName());

    final RpcDefinition myRpc = barModule.getRpcs().iterator().next();
    final ContainerSchemaNode input = myRpc.getInput();
    assertEquals(2, input.getMustConstraints().size());
    final ContainerSchemaNode output = myRpc.getOutput();
    assertEquals(2, output.getMustConstraints().size());

    final NotificationDefinition myNotification = barModule.getNotifications().iterator().next();
    assertEquals(2, myNotification.getMustConstraints().size());

    final AnyxmlSchemaNode myAnyxml = (AnyxmlSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-anyxml"));
    assertNotNull(myAnyxml);
    assertTrue(myAnyxml.isMandatory());
    assertEquals(2, myAnyxml.getUnknownSchemaNodes().size());

    final AnydataSchemaNode myAnyData = (AnydataSchemaNode) barModule.findDataChildByName(
            QName.create(barModule.getQNameModule(), "my-anydata")).orElse(null);
    assertNotNull(myAnyData);
    assertTrue(myAnyData.isMandatory());
    assertEquals(2, myAnyData.getUnknownSchemaNodes().size());
}
 
Example #30
Source File: DeviationResolutionTest.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testDeviateReplace() throws ReactorException {
    final SchemaContext schemaContext = StmtTestUtils.parseYangSources(
            sourceForResource("/deviation-resolution-test/deviation-replace/foo.yang"),
            sourceForResource("/deviation-resolution-test/deviation-replace/bar.yang"));
    assertNotNull(schemaContext);

    final Module barModule = schemaContext.findModule("bar", Revision.of("2017-01-20")).get();
    assertNotNull(barModule);

    final LeafSchemaNode myLeaf = (LeafSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-leaf"));
    assertNotNull(myLeaf);

    assertThat(myLeaf.getType(), instanceOf(Uint32TypeDefinition.class));
    assertEquals(Optional.of("bytes"), myLeaf.getType().getUnits());
    assertEquals(Optional.of("10"), myLeaf.getType().getDefaultValue());

    final LeafListSchemaNode myLeafList = (LeafListSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-leaf-list-test"));
    assertNotNull(myLeafList);

    final ElementCountConstraint constraint = myLeafList.getElementCountConstraint().get();
    assertEquals(6, constraint.getMaxElements().intValue());
    assertEquals(3, constraint.getMinElements().intValue());
    assertTrue(myLeafList.isConfiguration());

    final ChoiceSchemaNode myChoice = (ChoiceSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-choice"));
    assertNotNull(myChoice);

    assertFalse(myChoice.isMandatory());
    assertEquals(1, myChoice.getUnknownSchemaNodes().size());
    assertEquals("new arg", myChoice.getUnknownSchemaNodes().iterator().next().getNodeParameter());

    final ContainerSchemaNode myCont = (ContainerSchemaNode) barModule.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-cont"));
    assertNotNull(myCont);

    final LeafSchemaNode myAugLeaf = (LeafSchemaNode) myCont.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-aug-leaf"));
    assertNotNull(myAugLeaf);
    assertThat(myAugLeaf.getType(), instanceOf(Uint32TypeDefinition.class));
    assertEquals(Optional.of("seconds"), myAugLeaf.getType().getUnits());
    assertEquals(Optional.of("new-def-val"), myAugLeaf.getType().getDefaultValue());
    assertEquals(1, myAugLeaf.getUnknownSchemaNodes().size());
    assertEquals("new arg", myAugLeaf.getUnknownSchemaNodes().iterator().next().getNodeParameter());

    final LeafSchemaNode myUsedLeaf = (LeafSchemaNode) myCont.getDataChildByName(
            QName.create(barModule.getQNameModule(), "my-used-leaf"));
    assertNotNull(myUsedLeaf);
    assertThat(myUsedLeaf.getType(), instanceOf(Uint32TypeDefinition.class));
    assertEquals(Optional.of("weeks"), myUsedLeaf.getType().getUnits());
    assertEquals(Optional.of("new-def-val"), myUsedLeaf.getType().getDefaultValue());
    assertEquals(1, myUsedLeaf.getUnknownSchemaNodes().size());
    assertEquals("new arg", myUsedLeaf.getUnknownSchemaNodes().iterator().next().getNodeParameter());
}