From 0df79514c1986d366db9a186b912a4d5d1829d64 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 10 Aug 2022 16:24:58 +0200 Subject: [PATCH] Do not use SchemaPath in LeafRefContext Use a simple immutable list instead of SchemaPath. JIRA: YANGTOOLS-1236 Change-Id: I430671a1ac078718e03af3417f01470567c5e744 Signed-off-by: Robert Varga --- .../src/main/java/module-info.java | 2 ++ .../data/tree/leafref/LeafRefContext.java | 30 +++++++++---------- .../tree/leafref/LeafRefContextBuilder.java | 13 ++++---- .../leafref/LeafRefContextTreeBuilder.java | 16 ++++++---- .../yang/data/tree/leafref/LeafRefUtils.java | 14 ++++----- 5 files changed, 41 insertions(+), 34 deletions(-) diff --git a/data/yang-data-tree-ri/src/main/java/module-info.java b/data/yang-data-tree-ri/src/main/java/module-info.java index 27ce4d3805..ef48028197 100644 --- a/data/yang-data-tree-ri/src/main/java/module-info.java +++ b/data/yang-data-tree-ri/src/main/java/module-info.java @@ -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; diff --git a/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContext.java b/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContext.java index 32892364ee..4a147bcd30 100644 --- a/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContext.java +++ b/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContext.java @@ -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 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 getCurrentNodePath() { return currentNodePath; } @@ -171,7 +171,7 @@ public final class LeafRefContext extends AbstractEffectiveModelContextProvider private Iterator descendantIterator(final SchemaNodeIdentifier node) { final Iterator nodeSteps = node.getNodeIdentifiers().iterator(); if (node instanceof SchemaNodeIdentifier.Absolute) { - final Iterator mySteps = currentNodePath.getPathFromRoot().iterator(); + final Iterator mySteps = currentNodePath.iterator(); while (mySteps.hasNext()) { final QName myNext = mySteps.next(); checkArgument(nodeSteps.hasNext(), "Node %s is an ancestor of %s", node, currentNodePath); diff --git a/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContextBuilder.java b/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContextBuilder.java index 5b99b2b81a..08cee01dee 100644 --- a/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContextBuilder.java +++ b/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContextBuilder.java @@ -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 referencingChildren = new HashMap<>(); @@ -26,7 +25,7 @@ final class LeafRefContextBuilder implements Mutable { private final Map referencedByLeafRefCtx = new HashMap<>(); private final QName currentNodeQName; - private final SchemaPath currentNodePath; + private final ImmutableList 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 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 getCurrentNodePath() { return currentNodePath; } @@ -128,8 +127,8 @@ final class LeafRefContextBuilder implements Mutable { } Module getLeafRefContextModule() { - final List 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); } diff --git a/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContextTreeBuilder.java b/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContextTreeBuilder.java index 98bfc9be7c..9b563c10b1 100644 --- a/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContextTreeBuilder.java +++ b/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefContextTreeBuilder.java @@ -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 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 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 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 extractPath(final SchemaInferenceStack stack) { + return stack.isEmpty() ? ImmutableList.of() + : ImmutableList.copyOf(stack.toSchemaNodeIdentifier().getNodeIdentifiers()); + } } diff --git a/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefUtils.java b/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefUtils.java index ac9fbe9708..235e5c31ff 100644 --- a/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefUtils.java +++ b/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/leafref/LeafRefUtils.java @@ -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 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 schemaPathToXPathQNames(final SchemaPath nodePath, final Module module) { + private static Deque schemaPathToXPathQNames(final ImmutableList nodePath, + final Module module) { final Deque xpath = new LinkedList<>(); - final Iterator nodePathIterator = nodePath.getPathFromRoot().iterator(); + final Iterator 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 nodePath, final Module module) { return LeafRefPath.create(schemaPathToXPathQNames(nodePath, module), true); } } -- 2.36.6