Rework LeafRefValidation
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefContextTreeBuilder.java
index 19271b98411a76766a5c9583bb9f57e096caaec8..08fb284f6fd3738565d2a4d5f26042b5e8670d76 100644 (file)
@@ -7,8 +7,6 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.leafref;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-
 import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
@@ -20,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;
@@ -64,8 +63,7 @@ final class LeafRefContextTreeBuilder {
         return rootBuilder.build();
     }
 
-    private LeafRefContext buildLeafRefContextReferencingTree(final DataSchemaNode node, final Module currentModule)
-            throws LeafRefYangSyntaxErrorException {
+    private LeafRefContext buildLeafRefContextReferencingTree(final DataSchemaNode node, final Module currentModule) {
         final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(),
             node.getPath(), schemaContext);
 
@@ -88,18 +86,17 @@ 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().getOriginalString();
-                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 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);
                 currentLeafRefContextBuilder.setLeafRefTargetPath(leafRefPath);
 
@@ -112,18 +109,6 @@ 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 LeafRefYangSyntaxErrorException {
         final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(),