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());
}
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);
}
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);
}
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);
}
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")));
}
Aggregations