Do not use SchemaPath in LeafRefContext 36/102036/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Aug 2022 14:24:58 +0000 (16:24 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Aug 2022 14:28:20 +0000 (16:28 +0200)
Use a simple immutable list instead of SchemaPath.

JIRA: YANGTOOLS-1236
Change-Id: I430671a1ac078718e03af3417f01470567c5e744
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
data/yang-data-tree-ri/src/main/java/module-info.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContext.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContextBuilder.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContextTreeBuilder.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefUtils.java

index 27ce4d3805442cb74b31a254b4360582d64db982..ef48028197cd30b471386c3271c83b5d3c958bd2 100644 (file)
@@ -15,7 +15,9 @@ module org.opendaylight.yangtools.yang.data.tree {
     provides DataTreeFactory with InMemoryDataTreeFactory;
 
     requires transitive org.opendaylight.yangtools.yang.data.tree.api;
+    requires com.google.common;
     requires org.opendaylight.yangtools.concepts;
+    requires org.opendaylight.yangtools.yang.common;
     requires org.opendaylight.yangtools.yang.data.tree.spi;
     requires org.opendaylight.yangtools.yang.data.impl;
     requires org.opendaylight.yangtools.yang.data.spi;
index 32892364eea325eb120934a6b391e737a61ee0a6..4a147bcd308a22137ff365daae864cc8ef1106e3 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.data.tree.leafref;
 import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -19,14 +20,13 @@ 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 {
 
     private final QName currentNodeQName;
-    private final SchemaPath currentNodePath;
+    private final ImmutableList<QName> currentNodePath;
     private final Module module;
 
     private final LeafRefPath leafRefTargetPath;
@@ -47,17 +47,17 @@ public final class LeafRefContext extends AbstractEffectiveModelContextProvider
 
     LeafRefContext(final LeafRefContextBuilder leafRefContextBuilder) {
         super(leafRefContextBuilder.getSchemaContext());
-        this.currentNodeQName = leafRefContextBuilder.getCurrentNodeQName();
-        this.currentNodePath = leafRefContextBuilder.getCurrentNodePath();
-        this.leafRefTargetPath = leafRefContextBuilder.getLeafRefTargetPath();
-        this.absoluteLeafRefTargetPath = leafRefContextBuilder.getAbsoluteLeafRefTargetPath();
-        this.leafRefTargetPathString = leafRefContextBuilder.getLeafRefTargetPathString();
-        this.isReferencedBy = leafRefContextBuilder.isReferencedBy();
-        this.isReferencing = leafRefContextBuilder.isReferencing();
-        this.referencingChilds = ImmutableMap.copyOf(leafRefContextBuilder.getReferencingChilds());
-        this.referencedByChilds = ImmutableMap.copyOf(leafRefContextBuilder.getReferencedByChilds());
-        this.referencedByLeafRefCtx = ImmutableMap.copyOf(leafRefContextBuilder.getAllReferencedByLeafRefCtxs());
-        this.module = leafRefContextBuilder.getLeafRefContextModule();
+        currentNodeQName = leafRefContextBuilder.getCurrentNodeQName();
+        currentNodePath = leafRefContextBuilder.getCurrentNodePath();
+        leafRefTargetPath = leafRefContextBuilder.getLeafRefTargetPath();
+        absoluteLeafRefTargetPath = leafRefContextBuilder.getAbsoluteLeafRefTargetPath();
+        leafRefTargetPathString = leafRefContextBuilder.getLeafRefTargetPathString();
+        isReferencedBy = leafRefContextBuilder.isReferencedBy();
+        isReferencing = leafRefContextBuilder.isReferencing();
+        referencingChilds = ImmutableMap.copyOf(leafRefContextBuilder.getReferencingChilds());
+        referencedByChilds = ImmutableMap.copyOf(leafRefContextBuilder.getReferencedByChilds());
+        referencedByLeafRefCtx = ImmutableMap.copyOf(leafRefContextBuilder.getAllReferencedByLeafRefCtxs());
+        module = leafRefContextBuilder.getLeafRefContextModule();
     }
 
     public static LeafRefContext create(final EffectiveModelContext ctx) {
@@ -104,7 +104,7 @@ public final class LeafRefContext extends AbstractEffectiveModelContextProvider
         return referencedByChilds;
     }
 
-    public SchemaPath getCurrentNodePath() {
+    public ImmutableList<QName> getCurrentNodePath() {
         return currentNodePath;
     }
 
@@ -171,7 +171,7 @@ public final class LeafRefContext extends AbstractEffectiveModelContextProvider
     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();
+            final Iterator<QName> mySteps = currentNodePath.iterator();
             while (mySteps.hasNext()) {
                 final QName myNext = mySteps.next();
                 checkArgument(nodeSteps.hasNext(), "Node %s is an ancestor of %s", node, currentNodePath);
index 5b99b2b81a02edd6963d481bf4e9d04a1c76f2a4..08cee01dee820a4ce32b274c379619fd8a544571 100644 (file)
@@ -9,8 +9,8 @@ package org.opendaylight.yangtools.yang.data.tree.leafref;
 
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.collect.ImmutableList;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.Mutable;
@@ -18,7 +18,6 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 final class LeafRefContextBuilder implements Mutable {
     private final Map<QName, LeafRefContext> referencingChildren = new HashMap<>();
@@ -26,7 +25,7 @@ final class LeafRefContextBuilder implements Mutable {
     private final Map<QName, LeafRefContext> referencedByLeafRefCtx = new HashMap<>();
 
     private final QName currentNodeQName;
-    private final SchemaPath currentNodePath;
+    private final ImmutableList<QName> currentNodePath;
     private final EffectiveModelContext schemaContext;
 
     private LeafRefPath leafRefTargetPath = null;
@@ -36,7 +35,7 @@ final class LeafRefContextBuilder implements Mutable {
     private boolean isReferencedBy = false;
     private boolean isReferencing = false;
 
-    LeafRefContextBuilder(final QName currentNodeQName, final SchemaPath currentNodePath,
+    LeafRefContextBuilder(final QName currentNodeQName, final ImmutableList<QName> currentNodePath,
             final EffectiveModelContext schemaContext) {
         this.currentNodeQName = requireNonNull(currentNodeQName);
         this.currentNodePath = requireNonNull(currentNodePath);
@@ -86,7 +85,7 @@ final class LeafRefContextBuilder implements Mutable {
         return referencedByChildren;
     }
 
-    SchemaPath getCurrentNodePath() {
+    ImmutableList<QName> getCurrentNodePath() {
         return currentNodePath;
     }
 
@@ -128,8 +127,8 @@ final class LeafRefContextBuilder implements Mutable {
     }
 
     Module getLeafRefContextModule() {
-        final List<QName> path = currentNodePath.getPathFromRoot();
-        final QNameModule qnameModule = path.isEmpty() ? currentNodeQName.getModule() : path.get(0).getModule();
+        final QNameModule qnameModule = currentNodePath.isEmpty() ? currentNodeQName.getModule()
+            : currentNodePath.get(0).getModule();
         return schemaContext.findModule(qnameModule).orElse(null);
     }
 
index 98bfc9be7c5426887db01cc81a3ef772d8b68924..9b563c10b1f28888311e36f6988521d8b2380b71 100644 (file)
@@ -7,9 +7,11 @@
  */
 package org.opendaylight.yangtools.yang.data.tree.leafref;
 
+import com.google.common.collect.ImmutableList;
 import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
@@ -20,7 +22,6 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.PathExpression;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
@@ -36,7 +37,7 @@ final class LeafRefContextTreeBuilder {
 
     LeafRefContext buildLeafRefContextTree() throws LeafRefYangSyntaxErrorException {
         final SchemaInferenceStack stack = SchemaInferenceStack.of(schemaContext);
-        final LeafRefContextBuilder rootBuilder = new LeafRefContextBuilder(SchemaContext.NAME, SchemaPath.ROOT,
+        final LeafRefContextBuilder rootBuilder = new LeafRefContextBuilder(SchemaContext.NAME, ImmutableList.of(),
             schemaContext);
 
         final Collection<? extends Module> modules = schemaContext.getModules();
@@ -71,7 +72,7 @@ final class LeafRefContextTreeBuilder {
     private LeafRefContext buildLeafRefContextReferencingTree(final DataSchemaNode node,
             final SchemaInferenceStack stack) {
         final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(),
-            stack.toSchemaPath(), schemaContext);
+            extractPath(stack), schemaContext);
 
         if (node instanceof DataNodeContainer container) {
             for (final DataSchemaNode childNode : container.getChildNodes()) {
@@ -120,7 +121,7 @@ final class LeafRefContextTreeBuilder {
     private LeafRefContext buildLeafRefContextReferencedByTree(final DataSchemaNode node, final Module currentModule,
             final SchemaInferenceStack stack) {
         final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(),
-                stack.toSchemaPath(), schemaContext);
+                extractPath(stack), schemaContext);
         if (node instanceof DataNodeContainer container) {
             for (final DataSchemaNode childNode : container.getChildNodes()) {
                 stack.enterSchemaTree(childNode.getQName());
@@ -157,7 +158,7 @@ final class LeafRefContextTreeBuilder {
     }
 
     private List<LeafRefContext> getLeafRefsFor(final Module module, final SchemaInferenceStack stack) {
-        final LeafRefPath nodeXPath = LeafRefUtils.schemaPathToLeafRefPath(stack.toSchemaPath(), module);
+        final LeafRefPath nodeXPath = LeafRefUtils.schemaPathToLeafRefPath(extractPath(stack), module);
         final List<LeafRefContext> foundLeafRefs = new LinkedList<>();
         for (final LeafRefContext leafref : leafRefs) {
             final LeafRefPath leafRefTargetPath = leafref.getAbsoluteLeafRefTargetPath();
@@ -168,4 +169,9 @@ final class LeafRefContextTreeBuilder {
 
         return foundLeafRefs;
     }
+
+    private static ImmutableList<QName> extractPath(final SchemaInferenceStack stack) {
+        return stack.isEmpty() ? ImmutableList.of()
+            : ImmutableList.copyOf(stack.toSchemaNodeIdentifier().getNodeIdentifiers());
+    }
 }
index ac9fbe9708507e1167179f2316ab1e954ab52500..235e5c31ff3f59f86c3a9c9ed637c222dca628a6 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.yangtools.yang.data.tree.leafref;
 
+import com.google.common.collect.ImmutableList;
 import java.util.Deque;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -18,7 +19,6 @@ import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 public final class LeafRefUtils {
     private LeafRefUtils() {
@@ -33,9 +33,8 @@ public final class LeafRefUtils {
      * @param module module
      * @return LeafRefPath object
      */
-    public static LeafRefPath createAbsoluteLeafRefPath(
-            final LeafRefPath leafRefPath, final SchemaPath contextNodeSchemaPath,
-            final Module module) {
+    public static LeafRefPath createAbsoluteLeafRefPath(final LeafRefPath leafRefPath,
+            final ImmutableList<QName> contextNodeSchemaPath, final Module module) {
         if (leafRefPath.isAbsolute()) {
             return leafRefPath;
         }
@@ -56,9 +55,10 @@ public final class LeafRefUtils {
         return LeafRefPath.create(absoluteLeafRefTargetPathList, true);
     }
 
-    private static Deque<QNameWithPredicate> schemaPathToXPathQNames(final SchemaPath nodePath, final Module module) {
+    private static Deque<QNameWithPredicate> schemaPathToXPathQNames(final ImmutableList<QName> nodePath,
+            final Module module) {
         final Deque<QNameWithPredicate> xpath = new LinkedList<>();
-        final Iterator<QName> nodePathIterator = nodePath.getPathFromRoot().iterator();
+        final Iterator<QName> nodePathIterator = nodePath.iterator();
 
         DataNodeContainer currenDataNodeContainer = module;
         while (nodePathIterator.hasNext()) {
@@ -90,7 +90,7 @@ public final class LeafRefUtils {
         return xpath;
     }
 
-    public static LeafRefPath schemaPathToLeafRefPath(final SchemaPath nodePath, final Module module) {
+    public static LeafRefPath schemaPathToLeafRefPath(final ImmutableList<QName> nodePath, final Module module) {
         return LeafRefPath.create(schemaPathToXPathQNames(nodePath, module), true);
     }
 }