Search in sources :

Example 21 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project yangtools by opendaylight.

the class YT1107Test method testInterleavingLists.

@Test
public void testInterleavingLists() throws Exception {
    final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResource("/yt1107/yt1107.yang");
    final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/yt1107/yt1107.xml");
    final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
    final NormalizedNodeResult result = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
    final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, Inference.ofDataTreePath(schemaContext, PARENT));
    xmlParser.parse(reader);
    assertEquals(Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(PARENT)).withChild(Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(ADMIN)).withChild(Builders.mapEntryBuilder().withNodeIdentifier(NodeIdentifierWithPredicates.of(ADMIN, NAME, "John")).withChild(ImmutableNodes.leafNode(NAME, "John")).build()).build()).withChild(Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(USER)).withChild(Builders.mapEntryBuilder().withNodeIdentifier(NodeIdentifierWithPredicates.of(USER, NAME, "Freud")).withChild(ImmutableNodes.leafNode(NAME, "Freud")).build()).withChild(Builders.mapEntryBuilder().withNodeIdentifier(NodeIdentifierWithPredicates.of(USER, NAME, "Bob")).withChild(ImmutableNodes.leafNode(NAME, "Bob")).build()).build()).build(), result.getResult());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Example 22 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project yangtools by opendaylight.

the class AbstractMountPointContextFactory method createMountPointContext.

@Override
protected final MountPointContext createMountPointContext(final EffectiveModelContext schemaContext, final ContainerNode mountData) {
    checkArgument(SCHEMA_MOUNTS.equals(mountData.getIdentifier()), "Unexpected top-level container %s", mountData);
    final DataContainerChild mountPoint = mountData.childByArg(MOUNT_POINT);
    if (mountPoint == null) {
        LOG.debug("mount-point list not present in {}", mountData);
        return new EmptyMountPointContext(schemaContext);
    }
    checkArgument(mountPoint instanceof MapNode, "mount-point list %s is not a MapNode", mountPoint);
    return new ImmutableMountPointContext(schemaContext, ((MapNode) mountPoint).body().stream().map(entry -> {
        final String moduleName = entry.findChildByArg(MODULE).map(mod -> {
            checkArgument(mod instanceof LeafNode, "Unexpected module leaf %s", mod);
            final Object value = mod.body();
            checkArgument(value instanceof String, "Unexpected module leaf value %s", value);
            return (String) value;
        }).orElseThrow(() -> new IllegalArgumentException("Mount module missing in " + entry));
        final Iterator<? extends Module> it = schemaContext.findModules(moduleName).iterator();
        checkArgument(it.hasNext(), "Failed to find a module named %s", moduleName);
        final QNameModule module = it.next().getQNameModule();
        return new MountPointDefinition(MountPointIdentifier.of(QName.create(module, entry.findChildByArg(LABEL).map(lbl -> {
            checkArgument(lbl instanceof LeafNode, "Unexpected label leaf %s", lbl);
            final Object value = lbl.body();
            checkArgument(value instanceof String, "Unexpected label leaf value %s", value);
            return (String) value;
        }).orElseThrow(() -> new IllegalArgumentException("Mount module missing in " + entry)))), entry.findChildByArg(CONFIG).map(cfg -> {
            checkArgument(cfg instanceof LeafNode, "Unexpected config leaf %s", cfg);
            final Object value = cfg.body();
            checkArgument(value instanceof Boolean, "Unexpected config leaf value %s", cfg);
            return (Boolean) value;
        }).orElse(Boolean.TRUE), getSchema(entry.findChildByArg(SCHEMA_REF).orElseThrow(() -> new IllegalArgumentException("Missing schema-ref choice in " + entry))));
    }).collect(Collectors.toList()), this::createContextFactory);
}
Also used : NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) SchemaMountConstants(org.opendaylight.yangtools.rfc8528.model.api.SchemaMountConstants) LoggerFactory(org.slf4j.LoggerFactory) ToStringHelper(com.google.common.base.MoreObjects.ToStringHelper) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) MountPointIdentifier(org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) Objects.requireNonNull(java.util.Objects.requireNonNull) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) MountPointContextFactory(org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) LeafNode(org.opendaylight.yangtools.yang.data.api.schema.LeafNode) Immutable(org.opendaylight.yangtools.concepts.Immutable) ImmutableSet(com.google.common.collect.ImmutableSet) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Module(org.opendaylight.yangtools.yang.model.api.Module) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Collectors(java.util.stream.Collectors) AbstractSimpleIdentifiable(org.opendaylight.yangtools.concepts.AbstractSimpleIdentifiable) MountPointContext(org.opendaylight.yangtools.rfc8528.data.api.MountPointContext) QName(org.opendaylight.yangtools.yang.common.QName) Beta(com.google.common.annotations.Beta) ChoiceNode(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) LeafNode(org.opendaylight.yangtools.yang.data.api.schema.LeafNode) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule)

Example 23 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project yangtools by opendaylight.

the class AbstractDynamicMountPointContextFactory method createContext.

@Override
public final MountPointContext createContext(final Map<ContainerName, MountPointChild> libraryContainers, final MountPointChild schemaMounts) throws YangParserException {
    for (Entry<ContainerName, MountPointChild> entry : libraryContainers.entrySet()) {
        // Context for the specific code word
        final Optional<EffectiveModelContext> optLibContext = findSchemaForLibrary(entry.getKey());
        if (optLibContext.isEmpty()) {
            LOG.debug("YANG Library context for mount point {} container {} not found", getIdentifier(), entry.getKey());
            continue;
        }
        final NormalizedNode libData;
        try {
            libData = entry.getValue().normalizeTo(optLibContext.get());
        } catch (IOException e) {
            throw new YangParserException("Failed to interpret yang-library data", e);
        }
        if (!(libData instanceof ContainerNode)) {
            throw new YangParserException("Invalid yang-library non-container " + libData);
        }
        final EffectiveModelContext schemaContext = bindLibrary(entry.getKey(), (ContainerNode) libData);
        if (schemaMounts == null) {
            return new EmptyMountPointContext(schemaContext);
        }
        final NormalizedNode mountData;
        try {
            mountData = schemaMounts.normalizeTo(schemaContext);
        } catch (IOException e) {
            throw new YangParserException("Failed to interpret schema-mount data", e);
        }
        if (!(mountData instanceof ContainerNode)) {
            throw new YangParserException("Invalid schema-mount non-container " + mountData);
        }
        return createMountPointContext(schemaContext, (ContainerNode) mountData);
    }
    throw new YangParserException("Failed to interpret " + libraryContainers);
}
Also used : ContainerName(org.opendaylight.yangtools.rfc8528.data.api.YangLibraryConstants.ContainerName) MountPointChild(org.opendaylight.yangtools.rfc8528.data.api.MountPointChild) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) IOException(java.io.IOException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) YangParserException(org.opendaylight.yangtools.yang.parser.api.YangParserException) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 24 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project yangtools by opendaylight.

the class YT892Test method setup.

@Before
public void setup() {
    final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResourceDirectory("/yt892");
    leafRefContext = LeafRefContext.create(schemaContext);
    dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
}
Also used : EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) InMemoryDataTreeFactory(org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory) Before(org.junit.Before)

Example 25 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project yangtools by opendaylight.

the class Bug7844Test method test.

@Test
public void test() {
    final EffectiveModelContext context = YangParserTestUtils.parseYangResourceDirectory("/bug7844");
    assertNotNull(context);
    final LeafRefContext leafRefContext = LeafRefContext.create(context);
    assertNotNull(leafRefContext);
    final Map<QName, LeafRefContext> referencingChilds = leafRefContext.getReferencingChilds();
    assertEquals(7, referencingChilds.size());
    final QNameModule bazQNameModule = QNameModule.create(XMLNamespace.of(BAZ_NS));
    final LeafRefPath expectedPathToBazTarget = LeafRefPath.create(true, new QNameWithPredicateImpl(bazQNameModule, "root", ImmutableList.of()), new QNameWithPredicateImpl(bazQNameModule, "target", ImmutableList.of()));
    final LeafRefContext myLeafCtx = referencingChilds.get(foo("my-leaf"));
    assertLeafRef(myLeafCtx, expectedPathToBazTarget);
    assertLeafRef(referencingChilds.get(foo("my-leaf-2")), expectedPathToBazTarget);
    assertLeafRef(referencingChilds.get(foo("bar-base-leafref")), expectedPathToBazTarget);
    assertLeafRef(referencingChilds.get(foo("bar-base-leafref-2")), expectedPathToBazTarget);
    assertLeafRef(referencingChilds.get(bar("my-leafref-in-bar")), expectedPathToBazTarget);
    assertLeafRef(referencingChilds.get(bar("my-leafref-in-bar-2")), expectedPathToBazTarget);
    final QNameModule barQNameModule = QNameModule.create(XMLNamespace.of(BAR_NS));
    final LeafRefPath expectedPathToBarTarget = LeafRefPath.create(true, new QNameWithPredicateImpl(barQNameModule, "bar-target", ImmutableList.of()));
    assertLeafRef(referencingChilds.get(foo("direct-leafref")), expectedPathToBarTarget);
    final Map<QName, LeafRefContext> referencedByChilds = leafRefContext.getReferencedByChilds();
    assertEquals(2, referencedByChilds.size());
    final LeafRefContext rootCtx = referencedByChilds.get(baz("root"));
    assertEquals(1, rootCtx.getReferencedByChilds().size());
    assertTrue(rootCtx.getReferencingChilds().isEmpty());
    assertFalse(rootCtx.isReferencing());
    assertFalse(rootCtx.isReferenced());
    final LeafRefContext targetCtx = rootCtx.getReferencedChildByName(baz("target"));
    assertTrue(targetCtx.getReferencedByChilds().isEmpty());
    assertTrue(targetCtx.getReferencingChilds().isEmpty());
    assertTrue(targetCtx.isReferenced());
    assertFalse(targetCtx.isReferencing());
    final Map<QName, LeafRefContext> allReferencedByLeafRefCtxs = targetCtx.getAllReferencedByLeafRefCtxs();
    assertEquals(6, allReferencedByLeafRefCtxs.size());
    assertTrue(myLeafCtx == targetCtx.getReferencedByLeafRefCtxByName(foo("my-leaf")));
}
Also used : QName(org.opendaylight.yangtools.yang.common.QName) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Aggregations

EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)182 Test (org.junit.Test)99 Module (org.opendaylight.yangtools.yang.model.api.Module)37 QName (org.opendaylight.yangtools.yang.common.QName)29 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)28 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)26 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)24 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)24 ContainerSchemaNode (org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode)21 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)18 QNameModule (org.opendaylight.yangtools.yang.common.QNameModule)16 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)16 NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)16 IRSchemaSource (org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource)14 DOMMountPoint (org.opendaylight.mdsal.dom.api.DOMMountPoint)13 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)13 RpcDefinition (org.opendaylight.yangtools.yang.model.api.RpcDefinition)13 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)11 IOException (java.io.IOException)9 Collection (java.util.Collection)9