*/
package org.opendaylight.yangtools.yang.model.util;
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.util.Objects.requireNonNull;
-
-import com.google.common.annotations.Beta;
-import com.google.common.collect.Iterables;
import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Optional;
import java.util.Set;
-import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ContainerLike;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.ModuleLike;
-import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
-import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
-import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.api.Submodule;
import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* The Schema Context Util contains support methods for searching through Schema Context modules for specified schema
* instantiable.
*/
public final class SchemaContextUtil {
- private static final Logger LOG = LoggerFactory.getLogger(SchemaContextUtil.class);
-
-
private SchemaContextUtil() {
// Hidden on purpose
}
- /**
- * Method attempts to find DataSchemaNode in Schema Context via specified Schema Path. The returned DataSchemaNode
- * from method will be the node at the end of the SchemaPath. If the DataSchemaNode is not present in the Schema
- * Context the method will return {@code null}.
- *
- * <p>
- * In case that Schema Context or Schema Path are not specified correctly (i.e. contains {@code null} values) the
- * method will throw IllegalArgumentException.
- *
- * @param context Schema Context
- * @param schemaPath Schema Path to search for
- * @return SchemaNode from the end of the Schema Path or {@code null} if the Node is not present.
- * @throws NullPointerException if context or schemaPath is null
- */
- public static SchemaNode findDataSchemaNode(final SchemaContext context, final SchemaPath schemaPath) {
- final Iterable<QName> prefixedPath = schemaPath.getPathFromRoot();
- if (prefixedPath == null) {
- LOG.debug("Schema path {} has null path", schemaPath);
- return null;
- }
-
- LOG.trace("Looking for path {} in context {}", schemaPath, context);
- return findNodeInSchemaContext(context, prefixedPath);
- }
-
- /**
- * Returns parent Yang Module for specified Schema Context in which Schema
- * Node is declared. If the Schema Node is not present in Schema Context the
- * operation will return <code>null</code>.
- *
- * @param context Schema Context
- * @param schemaNode Schema Node
- * @return Yang Module for specified Schema Context and Schema Node, if Schema Node is NOT present, the method will
- * return <code>null</code>
- * @throws NullPointerException if any of the arguments is null
- */
- public static Module findParentModule(final SchemaContext context, final SchemaNode schemaNode) {
- return context.findModule(schemaNode.getQName().getModule()).orElse(null);
- }
-
- public static SchemaNode findNodeInSchemaContext(final SchemaContext context, final Iterable<QName> path) {
- final QName current = path.iterator().next();
-
- LOG.trace("Looking up module {} in context {}", current, path);
- final Optional<Module> module = context.findModule(current.getModule());
- if (module.isEmpty()) {
- LOG.debug("Module {} not found", current);
- return null;
- }
-
- return findNodeInModule(module.get(), path);
- }
-
- /**
- * Returns NotificationDefinition from Schema Context.
- *
- * @param schema SchemaContext in which lookup should be performed.
- * @param path Schema Path of notification
- * @return Notification schema or null, if notification is not present in schema context.
- */
- @Beta
- public static @Nullable NotificationDefinition getNotificationSchema(final @NonNull SchemaContext schema,
- final @NonNull SchemaPath path) {
- requireNonNull(schema, "Schema context must not be null.");
- requireNonNull(path, "Schema path must not be null.");
- for (final NotificationDefinition potential : schema.getNotifications()) {
- if (path.equals(potential.getPath())) {
- return potential;
- }
- }
- return null;
- }
-
- /**
- * Returns RPC Input or Output Data container from RPC definition.
- *
- * @param schema SchemaContext in which lookup should be performed.
- * @param path Schema path of RPC input/output data container
- * @return Notification schema or null, if notification is not present in schema context.
- */
- @Beta
- public static @Nullable ContainerLike getRpcDataSchema(final @NonNull SchemaContext schema,
- final @NonNull SchemaPath path) {
- requireNonNull(schema, "Schema context must not be null.");
- requireNonNull(path, "Schema path must not be null.");
- final Iterator<QName> it = path.getPathFromRoot().iterator();
- checkArgument(it.hasNext(), "Rpc must have QName.");
- final QName rpcName = it.next();
- checkArgument(it.hasNext(), "input or output must be part of path.");
- final QName inOrOut = it.next();
- for (final RpcDefinition potential : schema.getOperations()) {
- if (rpcName.equals(potential.getQName())) {
- return SchemaNodeUtils.getRpcDataSchema(potential, inOrOut);
- }
- }
- return null;
- }
-
/**
* Extract the identifiers of all modules and submodules which were used to create a particular SchemaContext.
*
* @param context SchemaContext to be examined
* @return Set of ModuleIdentifiers.
*/
+ // FIXME: rehost to yang-repo-spi (or -api?)
public static Set<SourceIdentifier> getConstituentModuleIdentifiers(final SchemaContext context) {
final Set<SourceIdentifier> ret = new HashSet<>();
private static SourceIdentifier moduleToIdentifier(final ModuleLike module) {
return RevisionSourceIdentifier.create(module.getName(), module.getRevision());
}
-
- private static SchemaNode findNodeInModule(final Module module, final Iterable<QName> path) {
- if (!path.iterator().hasNext()) {
- LOG.debug("No node matching {} found in node {}", path, module);
- return null;
- }
-
- final QName current = path.iterator().next();
- LOG.trace("Looking for node {} in module {}", current, module);
-
- SchemaNode foundNode = null;
- final Iterable<QName> nextPath = nextLevel(path);
-
- foundNode = module.dataChildByName(current);
- if (foundNode != null && nextPath.iterator().hasNext()) {
- foundNode = findNodeIn(foundNode, nextPath);
- }
-
- if (foundNode == null) {
- foundNode = getGroupingByName(module, current);
- if (foundNode != null && nextPath.iterator().hasNext()) {
- foundNode = findNodeIn(foundNode, nextPath);
- }
- }
-
- if (foundNode == null) {
- foundNode = getRpcByName(module, current);
- if (foundNode != null && nextPath.iterator().hasNext()) {
- foundNode = findNodeIn(foundNode, nextPath);
- }
- }
-
- if (foundNode == null) {
- foundNode = getNotificationByName(module, current);
- if (foundNode != null && nextPath.iterator().hasNext()) {
- foundNode = findNodeIn(foundNode, nextPath);
- }
- }
-
- if (foundNode == null) {
- LOG.debug("No node matching {} found in node {}", path, module);
- }
-
- return foundNode;
- }
-
- private static SchemaNode findNodeIn(final SchemaNode parent, final Iterable<QName> path) {
- if (!path.iterator().hasNext()) {
- LOG.debug("No node matching {} found in node {}", path, parent);
- return null;
- }
-
- final QName current = path.iterator().next();
- LOG.trace("Looking for node {} in node {}", current, parent);
-
- SchemaNode foundNode = null;
- final Iterable<QName> nextPath = nextLevel(path);
-
- if (parent instanceof DataNodeContainer) {
- final DataNodeContainer parentDataNodeContainer = (DataNodeContainer) parent;
-
- foundNode = parentDataNodeContainer.dataChildByName(current);
- if (foundNode != null && nextPath.iterator().hasNext()) {
- foundNode = findNodeIn(foundNode, nextPath);
- }
-
- if (foundNode == null) {
- foundNode = getGroupingByName(parentDataNodeContainer, current);
- if (foundNode != null && nextPath.iterator().hasNext()) {
- foundNode = findNodeIn(foundNode, nextPath);
- }
- }
- }
-
- if (foundNode == null && parent instanceof ActionNodeContainer) {
- final Optional<? extends SchemaNode> next = ((ActionNodeContainer) parent).getActions().stream()
- .filter(act -> current.equals(act.getQName())).findFirst();
- if (next.isPresent() && nextPath.iterator().hasNext()) {
- foundNode = findNodeIn(next.orElseThrow(), nextPath);
- }
- }
-
- if (foundNode == null && parent instanceof NotificationNodeContainer) {
- foundNode = ((NotificationNodeContainer) parent).getNotifications().stream()
- .filter(notif -> current.equals(notif.getQName())).findFirst().orElse(null);
- if (foundNode != null && nextPath.iterator().hasNext()) {
- foundNode = findNodeIn(foundNode, nextPath);
- }
- }
-
- if (foundNode == null && parent instanceof OperationDefinition) {
- final OperationDefinition parentRpcDefinition = (OperationDefinition) parent;
-
- if (current.getLocalName().equals("input")) {
- foundNode = parentRpcDefinition.getInput();
- if (foundNode != null && nextPath.iterator().hasNext()) {
- foundNode = findNodeIn(foundNode, nextPath);
- }
- }
-
- if (current.getLocalName().equals("output")) {
- foundNode = parentRpcDefinition.getOutput();
- if (foundNode != null && nextPath.iterator().hasNext()) {
- foundNode = findNodeIn(foundNode, nextPath);
- }
- }
-
- if (foundNode == null) {
- foundNode = getGroupingByName(parentRpcDefinition, current);
- if (foundNode != null && nextPath.iterator().hasNext()) {
- foundNode = findNodeIn(foundNode, nextPath);
- }
- }
- }
-
- if (foundNode == null && parent instanceof ChoiceSchemaNode) {
- foundNode = ((ChoiceSchemaNode) parent).findCase(current).orElse(null);
-
- if (foundNode != null && nextPath.iterator().hasNext()) {
- foundNode = findNodeIn(foundNode, nextPath);
- }
-
- if (foundNode == null) {
- // fallback that tries to map into one of the child cases
- for (final CaseSchemaNode caseNode : ((ChoiceSchemaNode) parent).getCases()) {
- final DataSchemaNode maybeChild = caseNode.dataChildByName(current);
- if (maybeChild != null) {
- foundNode = findNodeIn(maybeChild, nextPath);
- break;
- }
- }
- }
- }
-
- if (foundNode == null) {
- LOG.debug("No node matching {} found in node {}", path, parent);
- }
-
- return foundNode;
-
- }
-
- private static Iterable<QName> nextLevel(final Iterable<QName> path) {
- return Iterables.skip(path, 1);
- }
-
- private static RpcDefinition getRpcByName(final Module module, final QName name) {
- for (final RpcDefinition rpc : module.getRpcs()) {
- if (rpc.getQName().equals(name)) {
- return rpc;
- }
- }
- return null;
- }
-
- private static NotificationDefinition getNotificationByName(final Module module, final QName name) {
- for (final NotificationDefinition notification : module.getNotifications()) {
- if (notification.getQName().equals(name)) {
- return notification;
- }
- }
- return null;
- }
-
- private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final QName name) {
- for (final GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
- if (grouping.getQName().equals(name)) {
- return grouping;
- }
- }
- return null;
- }
-
- private static GroupingDefinition getGroupingByName(final OperationDefinition rpc, final QName name) {
- for (final GroupingDefinition grouping : rpc.getGroupings()) {
- if (grouping.getQName().equals(name)) {
- return grouping;
- }
- }
- return null;
- }
-
}
+++ /dev/null
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.model.util;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.Revision;
-import org.opendaylight.yangtools.yang.common.XMLNamespace;
-import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
-import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
-import org.opendaylight.yangtools.yang.model.api.PathExpression;
-import org.opendaylight.yangtools.yang.model.api.PathExpression.LocationPathSteps;
-import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath;
-import org.opendaylight.yangtools.yang.xpath.api.YangXPathAxis;
-
-public class SchemaContextUtilIntegrationTest {
- private static EffectiveModelContext context;
- private static Module myModule;
-
- @BeforeClass
- public static void beforeClass() {
- context = YangParserTestUtils.parseYangResourceDirectory("/schema-context-util");
- myModule = context.findModule(XMLNamespace.of("uri:my-module"), Revision.of("2014-10-07")).get();
- }
-
- @Test
- public void findNodeInSchemaContextTest() {
- SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
- myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-leaf-in-container"));
-
- SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
- QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
- SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
-
- assertEquals(testNode, foundNode);
-
- RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
- testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-input-leaf"));
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
- QName.create(myModule.getQNameModule(), "input"),
- QName.create(myModule.getQNameModule(), "my-input-leaf"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
-
- assertEquals(testNode, foundNode);
-
- rpc = getRpcByName(myModule, "my-rpc");
- testNode = rpc.getOutput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-output-leaf"));
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
- QName.create(myModule.getQNameModule(), "output"),
- QName.create(myModule.getQNameModule(), "my-output-leaf"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
-
- assertEquals(testNode, foundNode);
-
- final NotificationDefinition notification = myModule.getNotifications().iterator().next();
- testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "my-notification-leaf"));
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
- QName.create(myModule.getQNameModule(), "my-notification-leaf"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
-
- assertEquals(testNode, foundNode);
-
- final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
- testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-leaf-in-grouping"));
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
- QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
- QName.create(myModule.getQNameModule(), "my-leaf-in-grouping"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
-
- assertEquals(testNode, foundNode);
-
- testNode = ((ChoiceSchemaNode) myModule
- .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")))
- .findCaseNodes("one").iterator().next()
- .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
- QName.create(myModule.getQNameModule(), "one"),
- QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
-
- assertEquals(testNode, foundNode);
-
- ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
- myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-list"));
-
- testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
- QName.create(myModule.getQNameModule(), "my-list"),
- QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
-
- assertEquals(testNode, foundNode);
-
- listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
- myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-list"));
-
- testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
- QName.create(myModule.getQNameModule(), "my-list"),
- QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
-
- assertEquals(testNode, foundNode);
- }
-
- @Test
- public void findNodeInSchemaContextTest2() {
- SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
- myModule.getQNameModule(), "my-container"))).dataChildByName(QName.create(myModule.getQNameModule(),
- "my-leaf-not-in-container"));
- assertNull(testNode);
-
- SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
- QName.create(myModule.getQNameModule(), "my-leaf-not-in-container"));
- SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
-
- final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
- testNode = rpc.getInput().dataChildByName(QName.create(myModule.getQNameModule(), "no-input-leaf"));
- assertNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
- QName.create(myModule.getQNameModule(), "input"),
- QName.create(myModule.getQNameModule(), "no-input-leaf"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
-
- final NotificationDefinition notification = myModule.getNotifications().iterator().next();
- testNode = notification.dataChildByName(QName.create(myModule.getQNameModule(), "no-notification-leaf"));
- assertNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
- QName.create(myModule.getQNameModule(), "no-notification-leaf"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
-
- final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
- testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-container-in-grouping"))).dataChildByName(QName.create(myModule.getQNameModule(),
- "no-leaf-in-grouping"));
- assertNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
- QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
- QName.create(myModule.getQNameModule(), "no-leaf-in-grouping"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
-
- testNode = ((ChoiceSchemaNode) myModule
- .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")))
- .findCaseNodes("one").iterator().next()
- .dataChildByName(QName.create(myModule.getQNameModule(), "no-choice-leaf"));
- assertNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
- QName.create(myModule.getQNameModule(), "one"),
- QName.create(myModule.getQNameModule(), "no-choice-leaf"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
-
- ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
- myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-list"));
-
- testNode = listNode.dataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
- assertNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
- QName.create(myModule.getQNameModule(), "my-list"),
- QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
-
- listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
- myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-list"));
-
- testNode = listNode.dataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
- assertNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
- QName.create(myModule.getQNameModule(), "my-list"),
- QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
- }
-
- @Test
- public void findNodeInSchemaContextTest3() {
- SchemaNode testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-container"));
-
- SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
- SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- testNode = getRpcByName(myModule, "my-rpc");
- assertNotNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- testNode = myModule.getNotifications().iterator().next();
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- testNode = getGroupingByName(myModule, "my-grouping");
- assertNotNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), "my-list"));
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
- QName.create(myModule.getQNameModule(), "my-list"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
- }
-
- @Test
- public void findParentModuleTest() {
- final DataSchemaNode node = myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-container"));
-
- final Module foundModule = SchemaContextUtil.findParentModule(context, node);
-
- assertEquals(myModule, foundModule);
- }
-
- @Test
- public void findDataSchemaNodeTest() {
- final Module importedModule = context.findModule(XMLNamespace.of("uri:imported-module"),
- Revision.of("2014-10-07")).get();
-
- final QName myImportedContainer = QName.create(importedModule.getQNameModule(), "my-imported-container");
- final QName myImportedLeaf = QName.create(importedModule.getQNameModule(), "my-imported-leaf");
-
- final SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName(myImportedContainer))
- .getDataChildByName(myImportedLeaf);
-
- final PathExpression expr = mock(PathExpression.class);
- doReturn(true).when(expr).isAbsolute();
- doReturn(new LocationPathSteps(YangLocationPath.absolute(
- YangXPathAxis.CHILD.asStep(myImportedContainer), YangXPathAxis.CHILD.asStep(myImportedLeaf))))
- .when(expr).getSteps();
-
- assertEquals(testNode, SchemaInferenceStack.of(context).resolvePathExpression(expr));
- }
-
- @Test
- public void findDataSchemaNodeTest2() {
- final QName myLeafInGrouping2 = QName.create(myModule.getQNameModule(), "my-leaf-in-gouping2");
- final PathExpression expr = mock(PathExpression.class);
- doReturn(true).when(expr).isAbsolute();
- doReturn(new LocationPathSteps(YangLocationPath.relative(YangXPathAxis.CHILD.asStep(myLeafInGrouping2))))
- .when(expr).getSteps();
-
- final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
- final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
- assertSame(grouping, stack.enterGrouping(grouping.getQName()));
- assertEquals(grouping.getDataChildByName(myLeafInGrouping2), stack.resolvePathExpression(expr));
- }
-
- @Test
- public void findNodeInSchemaContextGroupingsTest() {
- // find grouping in container
- DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
- myModule.getQNameModule(), "my-container"));
- SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
- assertNotNull(testNode);
-
- SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
- SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
-
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-leaf-in-grouping-in-container"));
- path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- // find grouping in list
- dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
- myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-list"));
- testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
- assertNotNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
- QName.create(myModule.getQNameModule(), "my-list"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-leaf-in-grouping-in-list"));
- path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- // find grouping in grouping
- dataContainer = getGroupingByName(myModule, "my-grouping");
- testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
- assertNotNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-leaf-in-grouping-in-grouping"));
- path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- // find grouping in rpc
- final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
- for (final GroupingDefinition grouping : rpc.getGroupings()) {
- if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
- testNode = grouping;
- }
- }
- assertNotNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-leaf-in-grouping-in-rpc"));
- path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- // find grouping in output
- dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
- testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
- assertNotNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
- QName.create(myModule.getQNameModule(), "output"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-leaf-in-grouping-in-output"));
- path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- // find grouping in input
- dataContainer = getRpcByName(myModule, "my-rpc").getInput();
- testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
- assertNotNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
- QName.create(myModule.getQNameModule(), "input"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-leaf-in-grouping-in-input"));
- path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- // find grouping in notification
- dataContainer = getNotificationByName(myModule, "my-notification");
- testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
- assertNotNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-leaf-in-grouping-in-notification"));
- path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- // find grouping in case
- dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(
- QName.create(myModule.getQNameModule(), "my-choice")))
- .findCaseNodes("one").iterator().next()
- .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container-in-case"));
- testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
- assertNotNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
- QName.create(myModule.getQNameModule(), "one"),
- QName.create(myModule.getQNameModule(), "my-container-in-case"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
-
- testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-leaf-in-grouping-in-case"));
- path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
-
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
- }
-
- @Test
- public void findNodeInSchemaContextGroupingsTest2() {
- // find grouping in container
- DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
- myModule.getQNameModule(), "my-container"));
- SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
- assertNull(testNode);
-
- SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
- SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
-
- // find grouping in list
- dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
- myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
- "my-list"));
- testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
- assertNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
- QName.create(myModule.getQNameModule(), "my-list"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
-
- // find grouping in grouping
- dataContainer = getGroupingByName(myModule, "my-grouping");
- testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
- assertNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
-
- // find grouping in rpc
- final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
- for (final GroupingDefinition grouping : rpc.getGroupings()) {
- if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
- testNode = grouping;
- }
- }
- assertNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
-
- // find grouping in output
- dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
- testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
- assertNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
- QName.create(myModule.getQNameModule(), "output"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
-
- // find grouping in input
- dataContainer = getRpcByName(myModule, "my-rpc").getInput();
- testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
- assertNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
- QName.create(myModule.getQNameModule(), "input"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
-
- // find grouping in notification
- dataContainer = getNotificationByName(myModule, "my-notification");
- testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
- assertNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
-
- // find grouping in case
- dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(
- QName.create(myModule.getQNameModule(), "my-choice")))
- .findCaseNodes("one").iterator().next()
- .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container-in-case"));
- testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
- assertNull(testNode);
-
- path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
- QName.create(myModule.getQNameModule(), "one"),
- QName.create(myModule.getQNameModule(), "my-container-in-case"),
- QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
- foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNull(foundNode);
- }
-
- private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
- for (final GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
- if (grouping.getQName().getLocalName().equals(name)) {
- return grouping;
- }
- }
- return null;
- }
-
- private static RpcDefinition getRpcByName(final Module module, final String name) {
- for (final RpcDefinition rpc : module.getRpcs()) {
- if (rpc.getQName().getLocalName().equals(name)) {
- return rpc;
- }
- }
- return null;
- }
-
- private static NotificationDefinition getNotificationByName(final Module module, final String name) {
- for (final NotificationDefinition notification : module.getNotifications()) {
- if (notification.getQName().getLocalName().equals(name)) {
- return notification;
- }
- }
- return null;
- }
-
- @Test
- public void findNodeInSchemaContextTheSameNameOfSiblingsTest() {
- final ChoiceSchemaNode choice = (ChoiceSchemaNode) getRpcByName(myModule, "my-name").getInput()
- .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
- final SchemaNode testNode = choice.findCaseNodes("case-two").iterator().next()
- .getDataChildByName(QName.create(myModule.getQNameModule(), "two"));
-
- final SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
- QName.create(myModule.getQNameModule(), "input"), QName.create(myModule.getQNameModule(), "my-choice"),
- QName.create(myModule.getQNameModule(), "case-two"), QName.create(myModule.getQNameModule(), "two"));
- final SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
- assertNotNull(foundNode);
- assertEquals(testNode, foundNode);
- }
-}
\ No newline at end of file