BUG 2468 - leafrefs via import statement 83/13683/3
authorJozef Gloncak <jgloncak@cisco.com>
Tue, 16 Dec 2014 15:34:09 +0000 (16:34 +0100)
committerRobert Varga <rovarga@cisco.com>
Sat, 20 Dec 2014 09:10:34 +0000 (10:10 +0100)
Adds method which search for base type of leafref in module specified via
QName value.

Change-Id: Ib1979b56604429fccac2d9955387085aa59ed01d
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java

index 98fe8694db6ae04a68dd16429e566df850134b12..7b8d42b4da3263ff93d2fa1fbe1eb17be4fdb778 100644 (file)
@@ -16,17 +16,13 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
-
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
@@ -608,6 +604,36 @@ public final class SchemaContextUtil {
         }
     }
 
+    /**
+     * Returns base type for {@code typeDefinition} which belongs to module specified via {@code qName}. This handle case
+     * when leafref type isn't specified as type substatement of leaf or leaf-list but is defined in other module as typedef
+     * which is then imported to referenced module.
+     *
+     * Because {@code typeDefinition} is definied via typedef statement, only absolute path is meaningful.
+     *
+     * @param typeDefinition
+     * @param schemaContext
+     * @param qName
+     * @return
+     */
+    public static TypeDefinition<?> getBaseTypeForLeafRef(final LeafrefTypeDefinition typeDefinition,
+            final SchemaContext schemaContext, final QName qName) {
+        final RevisionAwareXPath pathStatement = typeDefinition.getPathStatement();
+        final RevisionAwareXPath strippedPathStatement = new RevisionAwareXPathImpl(stripConditionsFromXPathString(pathStatement), pathStatement.isAbsolute());
+        if (!strippedPathStatement.isAbsolute()) {
+            return null;
+        }
+
+        final Module parentModule = schemaContext.findModuleByNamespaceAndRevision(qName.getNamespace(),qName.getRevision());
+        final DataSchemaNode dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule, strippedPathStatement);
+        final TypeDefinition<?> targetTypeDefinition = typeDefinition(dataSchemaNode);
+        if (targetTypeDefinition instanceof LeafrefTypeDefinition) {
+            return getBaseTypeForLeafRef(((LeafrefTypeDefinition) targetTypeDefinition), schemaContext, dataSchemaNode);
+        } else {
+            return targetTypeDefinition;
+        }
+    }
+
     /**
      * Removes conditions from xPath pointed to target node.
      *