Bug 4969: NPE in JSONCodecFactory by attempt to find codec for a leafref
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / SchemaContextUtil.java
index 2d21a0ec40a2c0ded9825c76fa14527e575862a6..7ad5070f195a118faf8590d75e3671208029c578 100644 (file)
@@ -18,6 +18,7 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
+import java.util.regex.Pattern;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -221,7 +222,7 @@ public final class SchemaContextUtil {
         Preconditions.checkState(schemaNode.getPath() != null, "Schema Path for Schema Node is not "
                 + "set properly (Schema Path is NULL)");
 
-        final QName qname = Iterables.getFirst(schemaNode.getPath().getPathTowardsRoot(), null);
+        final QName qname = schemaNode.getPath().getLastComponent();
         Preconditions.checkState(qname != null,
                 "Schema Path contains invalid state of path parts. " +
                         "The Schema Path MUST contain at least ONE QName which defines namespace and Local name of path.");
@@ -639,7 +640,7 @@ public final class SchemaContextUtil {
             }
         }
 
-        Module parentModule = findParentModuleByType(schemaContext, baseSchema);
+        Module parentModule = findParentModuleOfReferencingType(schemaContext, baseSchema);
 
         final DataSchemaNode dataSchemaNode;
         if(pathStatement.isAbsolute()) {
@@ -664,7 +665,33 @@ public final class SchemaContextUtil {
         }
     }
 
+    private static Module findParentModuleOfReferencingType(final SchemaContext schemaContext,
+            final SchemaNode schemaNode) {
+        Preconditions.checkArgument(schemaContext != null, "Schema Context reference cannot be NULL!");
+        Preconditions.checkArgument(schemaNode != null, "Schema Node cannot be NULL!");
+        TypeDefinition<?> nodeType = null;
+
+        if (schemaNode instanceof LeafSchemaNode) {
+            nodeType = ((LeafSchemaNode) schemaNode).getType();
+        } else if (schemaNode instanceof LeafListSchemaNode) {
+            nodeType = ((LeafListSchemaNode) schemaNode).getType();
+        }
+
+        if (nodeType.getBaseType() != null) {
+            while (nodeType.getBaseType() != null) {
+                nodeType = nodeType.getBaseType();
+            }
+
+            final QNameModule typeDefModuleQname = nodeType.getQName().getModule();
+            return schemaContext.findModuleByNamespaceAndRevision(typeDefModuleQname.getNamespace(),
+                    typeDefModuleQname.getRevision());
+        }
+
+        return SchemaContextUtil.findParentModule(schemaContext, schemaNode);
+    }
+
     /**
+     * @deprecated due to expensive lookup
      * Returns parent Yang Module for specified Schema Context in which Schema
      * Node is declared. If Schema Node is of type 'ExtendedType' it tries to find parent module
      * in which the type was originally declared (needed for correct leafref path resolution). <br>
@@ -683,6 +710,7 @@ public final class SchemaContextUtil {
      *         Schema Node is NOT present, the method will returns
      *         <code>null</code>
      */
+    @Deprecated
     public static Module findParentModuleByType(final SchemaContext schemaContext, final SchemaNode schemaNode) {
         Preconditions.checkArgument(schemaContext != null, "Schema Context reference cannot be NULL!");
         Preconditions.checkArgument(schemaNode != null, "Schema Node cannot be NULL!");
@@ -694,8 +722,8 @@ public final class SchemaContextUtil {
             nodeType = ((LeafListSchemaNode) schemaNode).getType();
         }
 
-        if (nodeType instanceof ExtendedType) {
-            while (nodeType.getBaseType() instanceof ExtendedType) {
+        if (!BaseTypes.isYangBuildInType(nodeType) && nodeType.getBaseType() != null) {
+            while (nodeType.getBaseType() != null && !BaseTypes.isYangBuildInType(nodeType.getBaseType())) {
                 nodeType = nodeType.getBaseType();
             }
 
@@ -738,6 +766,8 @@ public final class SchemaContextUtil {
         }
     }
 
+    private static final Pattern STRIP_PATTERN = Pattern.compile("\\[.*\\]");
+
     /**
      * Removes conditions from xPath pointed to target node.
      *
@@ -747,7 +777,7 @@ public final class SchemaContextUtil {
      *
      */
     private static String stripConditionsFromXPathString(final RevisionAwareXPath pathStatement) {
-        return pathStatement.toString().replaceAll("\\[.*\\]", "");
+        return STRIP_PATTERN.matcher(pathStatement.toString()).replaceAll("");
     }
 
     /**