Rework LeafRefValidation
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefContextTreeBuilder.java
index 38dfb7550daad06abffbedb2255378c5a1403a48..08fb284f6fd3738565d2a4d5f26042b5e8670d76 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -7,49 +7,41 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.leafref;
 
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.nio.charset.Charset;
 import java.util.Collection;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Set;
-import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 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.PathExpression;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.util.Leafref;
+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;
-    private final LinkedList<LeafRefContext> leafRefs;
 
-    public LeafRefContextTreeBuilder(final SchemaContext schemaContext) {
+    LeafRefContextTreeBuilder(final SchemaContext schemaContext) {
         this.schemaContext = schemaContext;
-        this.leafRefs = new LinkedList<LeafRefContext>();
     }
 
-    public LeafRefContext buildLeafRefContextTree() throws IOException,
-            LeafRefYangSyntaxErrorException {
-        final LeafRefContextBuilder rootBuilder = new LeafRefContextBuilder(
-                schemaContext.getQName(), schemaContext.getPath(),
-                schemaContext);
+    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());
                 }
             }
         }
@@ -57,13 +49,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());
                 }
             }
         }
@@ -74,80 +63,44 @@ 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) {
+        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;
-            final Set<ChoiceCaseNode> cases = choice.getCases();
             // :FIXME choice without case
-
-            for (final ChoiceCaseNode caseNode : cases) {
-                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());
                 }
             }
 
-        } else if (node instanceof LeafSchemaNode
-                || node instanceof LeafListSchemaNode) {
-
-            TypeDefinition<?> type = null;
-
-            if (node instanceof LeafSchemaNode) {
-                type = ((LeafSchemaNode) node).getType();
-            } else {
-                type = ((LeafListSchemaNode) node).getType();
-            }
+        } else if (node instanceof TypedDataSchemaNode) {
+            final TypedDataSchemaNode typedNode = (TypedDataSchemaNode) node;
+            final TypeDefinition<?> type = typedNode.getType();
 
-            // FIXME: fix case when type is e.g. typdef -> typedef -> leafref
-            if (type instanceof Leafref) {
-                final Leafref leafrefType = (Leafref) type;
-                final String leafRefPathString = leafrefType.getPathStatement()
-                        .toString();
+            // FIXME: fix case when type is e.g. typedef -> typedef -> leafref
+            if (type instanceof LeafrefTypeDefinition) {
+                final LeafrefTypeDefinition leafrefType = (LeafrefTypeDefinition) type;
+                final PathExpression path = leafrefType.getPathStatement();
+                final LeafRefPathParserImpl leafRefPathParser = new LeafRefPathParserImpl(leafrefType, typedNode);
+                final LeafRefPath leafRefPath = leafRefPathParser.parseLeafRefPath(path);
 
-                currentLeafRefContextBuilder
-                        .setLeafRefTargetPathString(leafRefPathString);
+                currentLeafRefContextBuilder.setLeafRefTargetPathString(path.getOriginalString());
                 currentLeafRefContextBuilder.setReferencing(true);
-
-                final LeafRefPathParserImpl leafRefPathParser = new LeafRefPathParserImpl(
-                        schemaContext, currentModule, node);
-
-                final ByteArrayInputStream leafRefPathInputStream = new ByteArrayInputStream(
-                        leafRefPathString.getBytes(Charset.forName("UTF-8")));
-                final LeafRefPath leafRefPath = leafRefPathParser
-                        .parseLeafRefPathSourceToSchemaPath(leafRefPathInputStream);
-
                 currentLeafRefContextBuilder.setLeafRefTargetPath(leafRefPath);
 
-                final LeafRefContext currentLeafRefContext = currentLeafRefContextBuilder
-                        .build();
+                final LeafRefContext currentLeafRefContext = currentLeafRefContextBuilder.build();
                 leafRefs.add(currentLeafRefContext);
                 return currentLeafRefContext;
             }
@@ -156,56 +109,33 @@ class LeafRefContextTreeBuilder {
         return currentLeafRefContextBuilder.build();
     }
 
-    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) {
-
-            final ChoiceSchemaNode choice = (ChoiceSchemaNode) node;
-            final Set<ChoiceCaseNode> cases = choice.getCases();
-
-            for (final ChoiceCaseNode caseNode : cases) {
-                final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(
-                        caseNode, currentModule);
-
-                if (childLeafRefContext.hasReferencedChild()
-                        || childLeafRefContext.isReferenced()) {
-                    currentLeafRefContextBuilder.addReferencedByChild(
-                            childLeafRefContext,
-                            childLeafRefContext.getNodeName());
+            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 LinkedList<LeafRefContext> foundLeafRefs = getLeafRefsFor(node,
-                    currentModule);
+        } else if (node instanceof LeafSchemaNode || node instanceof LeafListSchemaNode) {
+            final List<LeafRefContext> foundLeafRefs = getLeafRefsFor(node, currentModule);
             if (!foundLeafRefs.isEmpty()) {
                 currentLeafRefContextBuilder.setReferencedBy(true);
                 for (final LeafRefContext leafRef : foundLeafRefs) {
-                    currentLeafRefContextBuilder.addReferencedByLeafRefCtx(
-                            leafRef.getNodeName(), leafRef);
+                    currentLeafRefContextBuilder.addReferencedByLeafRefCtx(leafRef.getNodeName(), leafRef);
                 }
             }
         }
@@ -213,16 +143,11 @@ class LeafRefContextTreeBuilder {
         return currentLeafRefContextBuilder.build();
     }
 
-    private LinkedList<LeafRefContext> getLeafRefsFor(final DataSchemaNode node,
-            final Module module) {
-        final LeafRefPath nodeXPath = LeafRefUtils.schemaPathToLeafRefPath(
-                node.getPath(), module);
-
-        final LinkedList<LeafRefContext> foundLeafRefs = new LinkedList<LeafRefContext>();
-
+    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);
             }
@@ -230,28 +155,4 @@ class LeafRefContextTreeBuilder {
 
         return foundLeafRefs;
     }
-
-    // private LeafRefContext buildLeafRefContextTreeFor(LeafRefContext parent,
-    // Module module) {
-    //
-    // Collection<DataSchemaNode> childNodes = module.getChildNodes();
-    // for (DataSchemaNode childNode : childNodes) {
-    // LeafRefContext childLeafRefContext = buildLeafRefContextTreeFor(parent,
-    // childNode);
-    //
-    // if(childLeafRefContext.hasReferencedByChild() ||
-    // childLeafRefContext.isReferencedBy()) {
-    // parent.addReferencedByChild(childLeafRefContext,
-    // childLeafRefContext.getCurrentNodeQName());
-    // }
-    // if(childLeafRefContext.hasReferencingChild() ||
-    // childLeafRefContext.isReferencing()) {
-    // parent.addReferencingChild(childLeafRefContext,
-    // childLeafRefContext.getCurrentNodeQName());
-    // }
-    // }
-    //
-    // return node;
-    // }
-
 }