Rework LeafRefValidation
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefContextTreeBuilder.java
index 0a23636320ff0dd078c06a2844c5d1e777f83f0c..08fb284f6fd3738565d2a4d5f26042b5e8670d76 100644 (file)
@@ -7,11 +7,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;
@@ -23,6 +18,7 @@ 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.api.TypedDataSchemaNode;
@@ -36,7 +32,7 @@ final class LeafRefContextTreeBuilder {
         this.schemaContext = schemaContext;
     }
 
-    LeafRefContext buildLeafRefContextTree() throws IOException, LeafRefYangSyntaxErrorException {
+    LeafRefContext buildLeafRefContextTree() throws LeafRefYangSyntaxErrorException {
         final LeafRefContextBuilder rootBuilder = new LeafRefContextBuilder(schemaContext.getQName(),
             schemaContext.getPath(), schemaContext);
 
@@ -67,8 +63,7 @@ final class LeafRefContextTreeBuilder {
         return rootBuilder.build();
     }
 
-    private LeafRefContext buildLeafRefContextReferencingTree(final DataSchemaNode node, final Module currentModule)
-            throws IOException, LeafRefYangSyntaxErrorException {
+    private LeafRefContext buildLeafRefContextReferencingTree(final DataSchemaNode node, final Module currentModule) {
         final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(),
             node.getPath(), schemaContext);
 
@@ -91,23 +86,18 @@ final class LeafRefContextTreeBuilder {
             }
 
         } else if (node instanceof TypedDataSchemaNode) {
-            final TypeDefinition<?> type = ((TypedDataSchemaNode) node).getType();
+            final TypedDataSchemaNode typedNode = (TypedDataSchemaNode) node;
+            final TypeDefinition<?> type = typedNode.getType();
 
             // FIXME: fix case when type is e.g. typedef -> typedef -> leafref
             if (type instanceof LeafrefTypeDefinition) {
                 final LeafrefTypeDefinition leafrefType = (LeafrefTypeDefinition) type;
-                final String leafRefPathString = leafrefType.getPathStatement().toString();
+                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,
-                        checkNotNull(getBaseTypeModule(leafrefType), "Unable to find base module for leafref %s", node),
-                        node);
-
-                final LeafRefPath leafRefPath = leafRefPathParser.parseLeafRefPathSourceToSchemaPath(
-                    new ByteArrayInputStream(leafRefPathString.getBytes(StandardCharsets.UTF_8)));
-
                 currentLeafRefContextBuilder.setLeafRefTargetPath(leafRefPath);
 
                 final LeafRefContext currentLeafRefContext = currentLeafRefContextBuilder.build();
@@ -119,20 +109,8 @@ final class LeafRefContextTreeBuilder {
         return currentLeafRefContextBuilder.build();
     }
 
-    private Module getBaseTypeModule(final LeafrefTypeDefinition leafrefType) {
-        /*
-         * Find the first definition of supplied leafref type and return the
-         * module which contains this definition.
-         */
-        LeafrefTypeDefinition baseLeafRefType = leafrefType;
-        while (baseLeafRefType.getBaseType() != null) {
-            baseLeafRefType = baseLeafRefType.getBaseType();
-        }
-        return schemaContext.findModule(baseLeafRefType.getQName().getModule()).orElse(null);
-    }
-
     private LeafRefContext buildLeafRefContextReferencedByTree(final DataSchemaNode node, final Module currentModule)
-            throws IOException,LeafRefYangSyntaxErrorException {
+            throws LeafRefYangSyntaxErrorException {
         final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(),
             node.getPath(), schemaContext);
         if (node instanceof DataNodeContainer) {