*/
package org.opendaylight.yangtools.yang.data.impl.leafref;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableMap;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
import org.opendaylight.yangtools.yang.model.spi.AbstractEffectiveModelContextProvider;
public final class LeafRefContext extends AbstractEffectiveModelContextProvider {
return referencedByLeafRefCtx;
}
+ @Beta
+ public LeafRefContext getLeafRefReferencingContext(final SchemaNodeIdentifier node) {
+ final Iterator<QName> iterator = descendantIterator(node);
+ LeafRefContext leafRefCtx = null;
+ LeafRefContext current = this;
+ while (iterator.hasNext() && current != null) {
+ final QName qname = iterator.next();
+ leafRefCtx = current.getReferencingChildByName(qname);
+ if (iterator.hasNext()) {
+ current = leafRefCtx;
+ }
+ }
+
+ return leafRefCtx;
+ }
+
+ @Beta
+ public LeafRefContext getLeafRefReferencedByContext(final SchemaNodeIdentifier node) {
+ final Iterator<QName> iterator = descendantIterator(node);
+ LeafRefContext leafRefCtx = null;
+ LeafRefContext current = this;
+ while (iterator.hasNext() && current != null) {
+ final QName qname = iterator.next();
+ leafRefCtx = current.getReferencedChildByName(qname);
+ if (iterator.hasNext()) {
+ current = leafRefCtx;
+ }
+ }
+
+ return leafRefCtx;
+ }
+
+ private Iterator<QName> descendantIterator(final SchemaNodeIdentifier node) {
+ final Iterator<QName> nodeSteps = node.getNodeIdentifiers().iterator();
+ if (node instanceof SchemaNodeIdentifier.Absolute) {
+ final Iterator<QName> mySteps = currentNodePath.getPathFromRoot().iterator();
+ while (mySteps.hasNext()) {
+ final QName myNext = mySteps.next();
+ checkArgument(nodeSteps.hasNext(), "Node %s is an ancestor of %s", node, currentNodePath);
+ final QName nodeNext = nodeSteps.next();
+ checkArgument(myNext.equals(nodeNext), "Node %s is not a descendant of %s", node, currentNodePath);
+ }
+ }
+ return nodeSteps;
+ }
+
+ @Beta
+ public boolean isLeafRef(final SchemaNodeIdentifier node) {
+ final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(node);
+ return leafRefReferencingContext != null && leafRefReferencingContext.isReferencing();
+ }
+
+ @Beta
+ public boolean hasLeafRefChild(final SchemaNodeIdentifier node) {
+ final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(node);
+ return leafRefReferencingContext != null && leafRefReferencingContext.hasReferencingChild();
+ }
+
+ @Beta
+ public boolean isReferencedByLeafRef(final SchemaNodeIdentifier node) {
+ final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(node);
+ return leafRefReferencedByContext != null && leafRefReferencedByContext.isReferenced();
+ }
+
+ @Beta
+ public boolean hasChildReferencedByLeafRef(final SchemaNodeIdentifier node) {
+ final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(node);
+ return leafRefReferencedByContext != null && leafRefReferencedByContext.hasReferencedChild();
+ }
+
+ @Beta
+ public List<LeafRefContext> findAllLeafRefChilds(final SchemaNodeIdentifier node) {
+ final LeafRefContext ctx = getLeafRefReferencingContext(node);
+ return ctx == null ? List.of() : ctx.findAllLeafRefChilds();
+ }
+
+ private List<LeafRefContext> findAllLeafRefChilds() {
+ if (isReferencing()) {
+ return List.of(this);
+ }
+
+ final List<LeafRefContext> leafRefChilds = new ArrayList<>();
+ for (final Entry<QName, LeafRefContext> child : getReferencingChilds().entrySet()) {
+ leafRefChilds.addAll(child.getValue().findAllLeafRefChilds());
+ }
+ return leafRefChilds;
+ }
+
+ @Beta
+ public List<LeafRefContext> findAllChildsReferencedByLeafRef(final SchemaNodeIdentifier node) {
+ final LeafRefContext ctx = getLeafRefReferencedByContext(node);
+ return ctx == null ? List.of() : ctx.findAllChildsReferencedByLeafRef();
+ }
+
+ private List<LeafRefContext> findAllChildsReferencedByLeafRef() {
+ if (isReferenced()) {
+ return List.of(this);
+ }
+
+ final List<LeafRefContext> childsReferencedByLeafRef = new ArrayList<>();
+ for (final Entry<QName, LeafRefContext> child : getReferencedByChilds().entrySet()) {
+ childsReferencedByLeafRef.addAll(child.getValue().findAllChildsReferencedByLeafRef());
+ }
+ return childsReferencedByLeafRef;
+ }
+
+ @Beta
+ public Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(final SchemaNodeIdentifier node) {
+ final LeafRefContext referencedByContext = getLeafRefReferencedByContext(node);
+ return referencedByContext == null ? Map.of() : referencedByContext.getAllReferencedByLeafRefCtxs();
+ }
+
LeafRefPath getLeafRefNodePath() {
LeafRefPath ret = leafRefNodePath;
if (ret == null) {
final EffectiveModelContext schemaContext) {
this.currentNodeQName = requireNonNull(currentNodeQName);
this.currentNodePath = requireNonNull(currentNodePath);
- // FIXME: requireNonNull
- this.schemaContext = schemaContext;
+ this.schemaContext = requireNonNull(schemaContext);
}
@Override
+++ /dev/null
-/*
- * Copyright (c) 2015 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.data.impl.leafref;
-
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-
-public final class LeafRefContextUtils {
- private LeafRefContextUtils() {
- // Hidden on purpose
- }
-
- public static LeafRefContext getLeafRefReferencingContext(final SchemaNode node, final LeafRefContext root) {
- final SchemaPath schemaPath = node.getPath();
- return getLeafRefReferencingContext(schemaPath, root);
- }
-
- public static LeafRefContext getLeafRefReferencingContext(
- final SchemaPath schemaPath, final LeafRefContext root) {
- final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
- return getLeafRefReferencingContext(pathFromRoot, root);
- }
-
- public static LeafRefContext getLeafRefReferencingContext(final Iterable<QName> pathFromRoot, LeafRefContext root) {
- LeafRefContext leafRefCtx = null;
- final Iterator<QName> iterator = pathFromRoot.iterator();
- while (iterator.hasNext() && root != null) {
- final QName qname = iterator.next();
- leafRefCtx = root.getReferencingChildByName(qname);
- if (iterator.hasNext()) {
- root = leafRefCtx;
- }
- }
-
- return leafRefCtx;
- }
-
- public static LeafRefContext getLeafRefReferencedByContext(final SchemaNode node, final LeafRefContext root) {
- final SchemaPath schemaPath = node.getPath();
- return getLeafRefReferencedByContext(schemaPath, root);
- }
-
- public static LeafRefContext getLeafRefReferencedByContext(
- final SchemaPath schemaPath, final LeafRefContext root) {
- final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
- return getLeafRefReferencedByContext(pathFromRoot, root);
- }
-
- public static LeafRefContext getLeafRefReferencedByContext(final Iterable<QName> pathFromRoot,
- LeafRefContext root) {
-
- LeafRefContext leafRefCtx = null;
- final Iterator<QName> iterator = pathFromRoot.iterator();
- while (iterator.hasNext() && root != null) {
- final QName qname = iterator.next();
- leafRefCtx = root.getReferencedChildByName(qname);
- if (iterator.hasNext()) {
- root = leafRefCtx;
- }
- }
-
- return leafRefCtx;
- }
-
- public static boolean isLeafRef(final SchemaNode node, final LeafRefContext root) {
- if (node == null || root == null) {
- return false;
- }
-
- final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(node, root);
- if (leafRefReferencingContext == null) {
- return false;
- }
-
- return leafRefReferencingContext.isReferencing();
- }
-
- public static boolean hasLeafRefChild(final SchemaNode node, final LeafRefContext root) {
- if (node == null || root == null) {
- return false;
- }
-
- final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(node, root);
- if (leafRefReferencingContext == null) {
- return false;
- }
-
- return leafRefReferencingContext.hasReferencingChild();
- }
-
- public static boolean isReferencedByLeafRef(final SchemaNode node, final LeafRefContext root) {
- if (node == null || root == null) {
- return false;
- }
-
- final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(node, root);
- if (leafRefReferencedByContext == null) {
- return false;
- }
-
- return leafRefReferencedByContext.isReferenced();
- }
-
- public static boolean hasChildReferencedByLeafRef(final SchemaNode node, final LeafRefContext root) {
- if (node == null || root == null) {
- return false;
- }
-
- final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(node, root);
- if (leafRefReferencedByContext == null) {
- return false;
- }
-
- return leafRefReferencedByContext.hasReferencedChild();
- }
-
- public static List<LeafRefContext> findAllLeafRefChilds(final SchemaNode node, final LeafRefContext root) {
- return findAllLeafRefChilds(node.getPath(), root);
- }
-
- public static List<LeafRefContext> findAllLeafRefChilds(final SchemaPath schemaPath, final LeafRefContext root) {
- return findAllLeafRefChilds(schemaPath.getPathFromRoot(), root);
- }
-
- public static List<LeafRefContext> findAllLeafRefChilds(final Iterable<QName> pathFromRoot,
- final LeafRefContext root) {
- final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(pathFromRoot, root);
- final List<LeafRefContext> allLeafRefsChilds = findAllLeafRefChilds(leafRefReferencingContext);
-
- return allLeafRefsChilds;
- }
-
- public static List<LeafRefContext> findAllLeafRefChilds(final LeafRefContext parent) {
- final LinkedList<LeafRefContext> leafRefChilds = new LinkedList<>();
- if (parent == null) {
- return leafRefChilds;
- }
-
- if (parent.isReferencing()) {
- leafRefChilds.add(parent);
- return leafRefChilds;
- }
-
- final Set<Entry<QName, LeafRefContext>> childs = parent.getReferencingChilds().entrySet();
- for (final Entry<QName, LeafRefContext> child : childs) {
- leafRefChilds.addAll(findAllLeafRefChilds(child.getValue()));
- }
- return leafRefChilds;
- }
-
- public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final SchemaNode node,
- final LeafRefContext root) {
- return findAllChildsReferencedByLeafRef(node.getPath(), root);
- }
-
- public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final SchemaPath schemaPath,
- final LeafRefContext root) {
- return findAllChildsReferencedByLeafRef(schemaPath.getPathFromRoot(), root);
- }
-
- public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final Iterable<QName> pathFromRoot,
- final LeafRefContext root) {
-
- final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(pathFromRoot, root);
- final List<LeafRefContext> allChildsReferencedByLeafRef =
- findAllChildsReferencedByLeafRef(leafRefReferencedByContext);
-
- return allChildsReferencedByLeafRef;
- }
-
- public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final LeafRefContext parent) {
- final LinkedList<LeafRefContext> childsReferencedByLeafRef = new LinkedList<>();
- if (parent == null) {
- return childsReferencedByLeafRef;
- }
-
- if (parent.isReferenced()) {
- childsReferencedByLeafRef.add(parent);
- return childsReferencedByLeafRef;
- }
-
- final Set<Entry<QName, LeafRefContext>> childs = parent.getReferencedByChilds().entrySet();
- for (final Entry<QName, LeafRefContext> child : childs) {
- childsReferencedByLeafRef.addAll(findAllChildsReferencedByLeafRef(child.getValue()));
- }
- return childsReferencedByLeafRef;
- }
-
- public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
- final SchemaNode node, final LeafRefContext root) {
- return getAllLeafRefsReferencingThisNode(node.getPath(), root);
- }
-
- public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(final SchemaPath path,
- final LeafRefContext root) {
- return getAllLeafRefsReferencingThisNode(path.getPathFromRoot(), root);
- }
-
- public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(final Iterable<QName> pathFromRoot,
- final LeafRefContext root) {
-
- final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(pathFromRoot, root);
- if (leafRefReferencedByContext == null) {
- return new HashMap<>();
- }
-
- return leafRefReferencedByContext.getAllReferencedByLeafRefCtxs();
- }
-}
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
-import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContextUtils;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
public class LeafRefContextTest {
@Test
public void test() {
-
final QName q1 = QName.create(root, "ref1");
final QName q2 = QName.create(root, "leaf1");
final QName q3 = QName.create(root, "cont1");
final QName q5 = QName.create(root, "list1");
final QName q6 = QName.create(root, "name");
- final DataSchemaNode leafRefNode = rootMod.findDataChildByName(q1).get();
- final DataSchemaNode targetNode = rootMod.findDataChildByName(q2).get();
- final DataSchemaNode cont1Node = rootMod.findDataChildByName(q3).get();
- final DataSchemaNode cont2Node = rootMod.findDataChildByName(q4).get();
- final DataSchemaNode name1Node = rootMod.findDataChildByName(q3, q5, q6).get();
+ final Absolute leafRefNode = Absolute.of(q1);
+ final Absolute targetNode = Absolute.of(q2);
+ final Absolute cont1Node = Absolute.of(q3);
+ final Absolute cont2Node = Absolute.of(q4);
+ final Absolute name1Node = Absolute.of(q3, q5, q6);
- assertTrue(LeafRefContextUtils.isLeafRef(leafRefNode, rootLeafRefContext));
- assertFalse(LeafRefContextUtils.isLeafRef(targetNode, rootLeafRefContext));
+ assertTrue(rootLeafRefContext.isLeafRef(leafRefNode));
+ assertFalse(rootLeafRefContext.isLeafRef(targetNode));
- assertTrue(LeafRefContextUtils.hasLeafRefChild(cont1Node, rootLeafRefContext));
- assertFalse(LeafRefContextUtils.hasLeafRefChild(leafRefNode, rootLeafRefContext));
+ assertTrue(rootLeafRefContext.hasLeafRefChild(cont1Node));
+ assertFalse(rootLeafRefContext.hasLeafRefChild(leafRefNode));
- assertTrue(LeafRefContextUtils.isReferencedByLeafRef(targetNode, rootLeafRefContext));
- assertFalse(LeafRefContextUtils.isReferencedByLeafRef(leafRefNode, rootLeafRefContext));
+ assertTrue(rootLeafRefContext.isReferencedByLeafRef(targetNode));
+ assertFalse(rootLeafRefContext.isReferencedByLeafRef(leafRefNode));
- assertTrue(LeafRefContextUtils.hasChildReferencedByLeafRef(cont2Node, rootLeafRefContext));
- assertFalse(LeafRefContextUtils.hasChildReferencedByLeafRef(leafRefNode, rootLeafRefContext));
+ assertTrue(rootLeafRefContext.hasChildReferencedByLeafRef(cont2Node));
+ assertFalse(rootLeafRefContext.hasChildReferencedByLeafRef(leafRefNode));
- Map<QName, LeafRefContext> leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(name1Node,
- rootLeafRefContext);
+ Map<QName, LeafRefContext> leafRefs = rootLeafRefContext.getAllLeafRefsReferencingThisNode(name1Node);
assertEquals(4, leafRefs.size());
- leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(leafRefNode, rootLeafRefContext);
+ leafRefs = rootLeafRefContext.getAllLeafRefsReferencingThisNode(leafRefNode);
assertTrue(leafRefs.isEmpty());
}
}
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
-import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContextUtils;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
import org.opendaylight.yangtools.yang.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
final QName q2 = QName.create(tst, "contributor");
final QName q3 = QName.create(tst, "odl-project-name");
- final LeafRefContext odlContrProjNameCtx = rootLeafRefContext.getReferencingChildByName(q1)
- .getReferencingChildByName(q2).getReferencingChildByName(q3);
-
- final DataSchemaNode odlContrProjNameNode = tstMod.findDataChildByName(q1, q2, q3).get();
-
- final LeafRefContext foundOdlContrProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(
- odlContrProjNameNode, rootLeafRefContext);
-
- assertNotNull(foundOdlContrProjNameCtx);
- assertTrue(foundOdlContrProjNameCtx.isReferencing());
- assertNotNull(foundOdlContrProjNameCtx.getLeafRefTargetPath());
- assertEquals(odlContrProjNameCtx, foundOdlContrProjNameCtx);
+ final LeafRefContext found = rootLeafRefContext.getLeafRefReferencingContext(Absolute.of(q1, q2, q3));
+ assertNotNull(found);
+ assertTrue(found.isReferencing());
+ assertNotNull(found.getLeafRefTargetPath());
+ assertEquals(rootLeafRefContext
+ .getReferencingChildByName(q1).getReferencingChildByName(q2).getReferencingChildByName(q3), found);
}
@Test
final QName q2 = QName.create(tst, "project");
final QName q3 = QName.create(tst, "name");
- final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencedChildByName(q1).getReferencedChildByName(q2)
- .getReferencedChildByName(q3);
+ final Absolute node = Absolute.of(q1, q2, q3);
+ LeafRefContext found = rootLeafRefContext.getLeafRefReferencingContext(node);
+ assertNull(found);
- final DataSchemaNode odlProjNameNode = tstMod.findDataChildByName(q1, q2, q3).get();
+ found = rootLeafRefContext.getLeafRefReferencedByContext(node);
- LeafRefContext foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(odlProjNameNode,
- rootLeafRefContext);
-
- assertNull(foundOdlProjNameCtx);
-
- foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencedByContext(odlProjNameNode, rootLeafRefContext);
-
- assertNotNull(foundOdlProjNameCtx);
- assertTrue(foundOdlProjNameCtx.isReferenced());
- assertFalse(foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().isEmpty());
- assertEquals(6, foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().size());
- assertEquals(leafRefCtx, foundOdlProjNameCtx);
+ assertNotNull(found);
+ assertTrue(found.isReferenced());
+ assertFalse(found.getAllReferencedByLeafRefCtxs().isEmpty());
+ assertEquals(6, found.getAllReferencedByLeafRefCtxs().size());
+ assertEquals(rootLeafRefContext
+ .getReferencedChildByName(q1).getReferencedChildByName(q2).getReferencedChildByName(q3), found);
}
@Test
public void leafRefContextUtilsTest3() {
final QName q16 = QName.create(tst, "con1");
- final DataSchemaNode con1 = tstMod.findDataChildByName(q16).get();
- final List<LeafRefContext> allLeafRefChilds = LeafRefContextUtils.findAllLeafRefChilds(con1,
- rootLeafRefContext);
+ final Absolute con1 = Absolute.of(q16);
+
+ final List<LeafRefContext> allLeafRefChilds = rootLeafRefContext.findAllLeafRefChilds(con1);
assertNotNull(allLeafRefChilds);
assertFalse(allLeafRefChilds.isEmpty());
assertEquals(4, allLeafRefChilds.size());
- final QName q17 = QName.create(tst, "odl-contributor");
- final DataSchemaNode odlContributorNode = tstMod.findDataChildByName(q17).get();
- List<LeafRefContext> allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(
- odlContributorNode, rootLeafRefContext);
+ List<LeafRefContext> allChildsReferencedByLeafRef = rootLeafRefContext.findAllChildsReferencedByLeafRef(
+ Absolute.of(QName.create(tst, "odl-contributor")));
assertNotNull(allChildsReferencedByLeafRef);
assertFalse(allChildsReferencedByLeafRef.isEmpty());
assertEquals(1, allChildsReferencedByLeafRef.size());
- allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(con1, rootLeafRefContext);
+ allChildsReferencedByLeafRef = rootLeafRefContext.findAllChildsReferencedByLeafRef(con1);
assertNotNull(allChildsReferencedByLeafRef);
assertTrue(allChildsReferencedByLeafRef.isEmpty());