Separate out ModificationApplyOperation.verifyStructure()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefContextTreeBuilder.java
index f17ce4e735fef4c8adcdf6b222dd076f84750f6d..75151eb968f63f04b497446f697a534feb4265c4 100644 (file)
@@ -9,9 +9,6 @@ package org.opendaylight.yangtools.yang.data.impl.leafref;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
 import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
@@ -28,7 +25,7 @@ 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;
 
-class LeafRefContextTreeBuilder {
+final class LeafRefContextTreeBuilder {
     private final List<LeafRefContext> leafRefs = new LinkedList<>();
     private final SchemaContext schemaContext;
 
@@ -36,22 +33,16 @@ class LeafRefContextTreeBuilder {
         this.schemaContext = schemaContext;
     }
 
-    public LeafRefContext buildLeafRefContextTree() throws IOException,
-            LeafRefYangSyntaxErrorException {
+    LeafRefContext buildLeafRefContextTree() throws LeafRefYangSyntaxErrorException {
         final LeafRefContextBuilder rootBuilder = new LeafRefContextBuilder(schemaContext.getQName(),
             schemaContext.getPath(), schemaContext);
 
         final Set<Module> modules = schemaContext.getModules();
         for (final Module module : modules) {
-            final Collection<DataSchemaNode> childNodes = module.getChildNodes();
-            for (final DataSchemaNode childNode : childNodes) {
-                final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(
-                        childNode, module);
-
-                if (childLeafRefContext.hasReferencingChild()
-                        || childLeafRefContext.isReferencing()) {
-                    rootBuilder.addReferencingChild(childLeafRefContext,
-                            childLeafRefContext.getNodeName());
+            for (final DataSchemaNode childNode : module.getChildNodes()) {
+                final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(childNode, module);
+                if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
+                    rootBuilder.addReferencingChild(childLeafRefContext, childLeafRefContext.getNodeName());
                 }
             }
         }
@@ -59,13 +50,10 @@ class LeafRefContextTreeBuilder {
         for (final Module module : modules) {
             final Collection<DataSchemaNode> childNodes = module.getChildNodes();
             for (final DataSchemaNode childNode : childNodes) {
-                final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(
-                        childNode, module);
+                final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(childNode, module);
 
-                if (childLeafRefContext.hasReferencedChild()
-                        || childLeafRefContext.isReferenced()) {
-                    rootBuilder.addReferencedByChild(childLeafRefContext,
-                            childLeafRefContext.getNodeName());
+                if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
+                    rootBuilder.addReferencedByChild(childLeafRefContext, childLeafRefContext.getNodeName());
                 }
             }
         }
@@ -76,42 +64,26 @@ class LeafRefContextTreeBuilder {
         return rootBuilder.build();
     }
 
-    private LeafRefContext buildLeafRefContextReferencingTree(
-            final DataSchemaNode node, final Module currentModule) throws IOException,
-            LeafRefYangSyntaxErrorException {
-
-        final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(
-                node.getQName(), node.getPath(), schemaContext);
+    private LeafRefContext buildLeafRefContextReferencingTree(final DataSchemaNode node, final Module currentModule)
+            throws LeafRefYangSyntaxErrorException {
+        final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(),
+            node.getPath(), schemaContext);
 
         if (node instanceof DataNodeContainer) {
-            final DataNodeContainer dataNodeContainer = (DataNodeContainer) node;
-            final Collection<DataSchemaNode> childNodes = dataNodeContainer
-                    .getChildNodes();
-
-            for (final DataSchemaNode childNode : childNodes) {
-                final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(
-                        childNode, currentModule);
-
-                if (childLeafRefContext.hasReferencingChild()
-                        || childLeafRefContext.isReferencing()) {
-                    currentLeafRefContextBuilder.addReferencingChild(
-                            childLeafRefContext,
-                            childLeafRefContext.getNodeName());
+            for (final DataSchemaNode childNode : ((DataNodeContainer) node).getChildNodes()) {
+                final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(childNode, currentModule);
+                if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
+                    currentLeafRefContextBuilder.addReferencingChild(childLeafRefContext,
+                        childLeafRefContext.getNodeName());
                 }
             }
         } else if (node instanceof ChoiceSchemaNode) {
-
-            final ChoiceSchemaNode choice = (ChoiceSchemaNode) node;
             // :FIXME choice without case
-            for (final CaseSchemaNode caseNode : choice.getCases().values()) {
-                final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(
-                        caseNode, currentModule);
-
-                if (childLeafRefContext.hasReferencingChild()
-                        || childLeafRefContext.isReferencing()) {
-                    currentLeafRefContextBuilder.addReferencingChild(
-                            childLeafRefContext,
-                            childLeafRefContext.getNodeName());
+            for (final CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
+                final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(caseNode, currentModule);
+                if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
+                    currentLeafRefContextBuilder.addReferencingChild(childLeafRefContext,
+                        childLeafRefContext.getNodeName());
                 }
             }
 
@@ -122,17 +94,13 @@ class LeafRefContextTreeBuilder {
             if (type instanceof LeafrefTypeDefinition) {
                 final LeafrefTypeDefinition leafrefType = (LeafrefTypeDefinition) type;
                 final String leafRefPathString = leafrefType.getPathStatement().toString();
-
-                currentLeafRefContextBuilder.setLeafRefTargetPathString(leafRefPathString);
-                currentLeafRefContextBuilder.setReferencing(true);
-
                 final LeafRefPathParserImpl leafRefPathParser = new LeafRefPathParserImpl(schemaContext,
                         checkNotNull(getBaseTypeModule(leafrefType), "Unable to find base module for leafref %s", node),
                         node);
+                final LeafRefPath leafRefPath = leafRefPathParser.parseLeafRefPath(leafRefPathString);
 
-                final LeafRefPath leafRefPath = leafRefPathParser.parseLeafRefPathSourceToSchemaPath(
-                    new ByteArrayInputStream(leafRefPathString.getBytes(StandardCharsets.UTF_8)));
-
+                currentLeafRefContextBuilder.setLeafRefTargetPathString(leafRefPathString);
+                currentLeafRefContextBuilder.setReferencing(true);
                 currentLeafRefContextBuilder.setLeafRefTargetPath(leafRefPath);
 
                 final LeafRefContext currentLeafRefContext = currentLeafRefContextBuilder.build();
@@ -156,39 +124,27 @@ class LeafRefContextTreeBuilder {
         return schemaContext.findModule(baseLeafRefType.getQName().getModule()).orElse(null);
     }
 
-    private LeafRefContext buildLeafRefContextReferencedByTree(
-            final DataSchemaNode node, final Module currentModule) throws IOException,
-            LeafRefYangSyntaxErrorException {
-
-        final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(
-                node.getQName(), node.getPath(), schemaContext);
-
+    private LeafRefContext buildLeafRefContextReferencedByTree(final DataSchemaNode node, final Module currentModule)
+            throws LeafRefYangSyntaxErrorException {
+        final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(),
+            node.getPath(), schemaContext);
         if (node instanceof DataNodeContainer) {
-            final DataNodeContainer dataNodeContainer = (DataNodeContainer) node;
-            final Collection<DataSchemaNode> childNodes = dataNodeContainer
-                    .getChildNodes();
-
-            for (final DataSchemaNode childNode : childNodes) {
-                final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(
-                        childNode, currentModule);
-
-                if (childLeafRefContext.hasReferencedChild()
-                        || childLeafRefContext.isReferenced()) {
-                    currentLeafRefContextBuilder.addReferencedByChild(
-                            childLeafRefContext,
-                            childLeafRefContext.getNodeName());
+            for (final DataSchemaNode childNode : ((DataNodeContainer) node).getChildNodes()) {
+                final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(childNode,
+                    currentModule);
+                if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
+                    currentLeafRefContextBuilder.addReferencedByChild(childLeafRefContext,
+                        childLeafRefContext.getNodeName());
                 }
             }
         } else if (node instanceof ChoiceSchemaNode) {
             for (final CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
                 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(caseNode, currentModule);
-
                 if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
                     currentLeafRefContextBuilder.addReferencedByChild(childLeafRefContext,
                         childLeafRefContext.getNodeName());
                 }
             }
-
         } else if (node instanceof LeafSchemaNode || node instanceof LeafListSchemaNode) {
             final List<LeafRefContext> foundLeafRefs = getLeafRefsFor(node, currentModule);
             if (!foundLeafRefs.isEmpty()) {
@@ -202,16 +158,11 @@ class LeafRefContextTreeBuilder {
         return currentLeafRefContextBuilder.build();
     }
 
-    private List<LeafRefContext> getLeafRefsFor(final DataSchemaNode node,
-            final Module module) {
-        final LeafRefPath nodeXPath = LeafRefUtils.schemaPathToLeafRefPath(
-                node.getPath(), module);
-
+    private List<LeafRefContext> getLeafRefsFor(final DataSchemaNode node, final Module module) {
+        final LeafRefPath nodeXPath = LeafRefUtils.schemaPathToLeafRefPath(node.getPath(), module);
         final List<LeafRefContext> foundLeafRefs = new LinkedList<>();
-
         for (final LeafRefContext leafref : leafRefs) {
-            final LeafRefPath leafRefTargetPath = leafref
-                    .getAbsoluteLeafRefTargetPath();
+            final LeafRefPath leafRefTargetPath = leafref.getAbsoluteLeafRefTargetPath();
             if (leafRefTargetPath.equals(nodeXPath)) {
                 foundLeafRefs.add(leafref);
             }
@@ -219,5 +170,4 @@ class LeafRefContextTreeBuilder {
 
         return foundLeafRefs;
     }
-
 }