Bug 5437: Issue accessing mounted device supporting OpenConfig BGP.
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / SchemaContextUtil.java
index 10594211833df3fb15a86fad6ba32dd091bf4112..2352eedc8a32fe076d9e345e6ddc028d12c9ea44 100644 (file)
@@ -630,23 +630,25 @@ public final class SchemaContextUtil {
         RevisionAwareXPath pathStatement = typeDefinition.getPathStatement();
         pathStatement = new RevisionAwareXPathImpl(stripConditionsFromXPathString(pathStatement), pathStatement.isAbsolute());
 
-        SchemaNode baseSchema = schema;
-        while (baseSchema instanceof DerivableSchemaNode) {
-            final Optional<? extends SchemaNode> basePotential = ((DerivableSchemaNode) baseSchema).getOriginal();
-            if (basePotential.isPresent()) {
-                baseSchema = basePotential.get();
-            } else {
-                break;
+        final DataSchemaNode dataSchemaNode;
+        if (pathStatement.isAbsolute()) {
+            SchemaNode baseSchema = schema;
+            while (baseSchema instanceof DerivableSchemaNode) {
+                final Optional<? extends SchemaNode> basePotential = ((DerivableSchemaNode) baseSchema).getOriginal();
+                if (basePotential.isPresent()) {
+                    baseSchema = basePotential.get();
+                } else {
+                    break;
+                }
             }
-        }
-
-        Module parentModule = findParentModuleByType(schemaContext, baseSchema);
 
-        final DataSchemaNode dataSchemaNode;
-        if(pathStatement.isAbsolute()) {
-            dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule, pathStatement);
+            Module parentModule = findParentModuleOfReferencingType(schemaContext, baseSchema);
+            dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule,
+                    pathStatement);
         } else {
-            dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, parentModule, baseSchema, pathStatement);
+            Module parentModule = findParentModule(schemaContext, schema);
+            dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext,
+                    parentModule, schema, pathStatement);
         }
 
         // FIXME this is just to preserve backwards compatibility since yangtools do not mind wrong leafref xpaths
@@ -665,7 +667,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>
@@ -684,6 +712,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!");
@@ -695,8 +724,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();
             }