Added support to generate types from Choices and Cases added by augmentation.
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-util / src / main / java / org / opendaylight / controller / yang / model / util / SchemaContextUtil.java
index 2d6a78988752de136af601dd70ab40713cfa9a9e..44ef804e2c0c9dd9bb10223d9bf6fafaaf5b49c3 100644 (file)
@@ -14,26 +14,17 @@ import java.util.Queue;
 import java.util.Set;
 
 import org.opendaylight.controller.yang.common.QName;
-import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.controller.yang.model.api.DataNodeContainer;
-import org.opendaylight.controller.yang.model.api.DataSchemaNode;
-import org.opendaylight.controller.yang.model.api.ListSchemaNode;
-import org.opendaylight.controller.yang.model.api.Module;
-import org.opendaylight.controller.yang.model.api.ModuleImport;
-import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
-import org.opendaylight.controller.yang.model.api.SchemaContext;
-import org.opendaylight.controller.yang.model.api.SchemaNode;
-import org.opendaylight.controller.yang.model.api.SchemaPath;
-import org.opendaylight.controller.yang.model.api.TypeDefinition;
+import org.opendaylight.controller.yang.model.api.*;
 
 public final class SchemaContextUtil {
 
-    private SchemaContextUtil() {}
+    private SchemaContextUtil() {
+    }
 
     public static DataSchemaNode findDataSchemaNode(final SchemaContext context, final SchemaPath schemaPath) {
         if (schemaPath != null) {
             final Module module = resolveModuleFromSchemaPath(context, schemaPath);
-            final Queue<QName> prefixedPath = new LinkedList<QName>(schemaPath.getPath());
+            final Queue<QName> prefixedPath = new LinkedList<>(schemaPath.getPath());
 
             if ((module != null) && (prefixedPath != null)) {
                 return findSchemaNodeForGivenPath(context, module, prefixedPath);
@@ -52,11 +43,9 @@ public final class SchemaContextUtil {
                     // TODO: function to escape conditions in path
                 }
                 if (nonCondXPath.isAbsolute()) {
-                    final Queue<QName> qnamedPath = xpathToQNamePath(context, module,
-                            strXPath);
+                    final Queue<QName> qnamedPath = xpathToQNamePath(context, module, strXPath);
                     if (qnamedPath != null) {
-                        final DataSchemaNode dataNode = findSchemaNodeForGivenPath(context,
-                                module, qnamedPath);
+                        final DataSchemaNode dataNode = findSchemaNodeForGivenPath(context, module, qnamedPath);
                         return dataNode;
                     }
                 }
@@ -65,20 +54,16 @@ public final class SchemaContextUtil {
         return null;
     }
 
-    public static DataSchemaNode findDataSchemaNodeForRelativeXPath(final SchemaContext context,
-            final Module module, final SchemaNode actualSchemaNode,
-            final RevisionAwareXPath relativeXPath) {
-        if ((actualSchemaNode != null) && (relativeXPath != null)
-                && !relativeXPath.isAbsolute()) {
+    public static DataSchemaNode findDataSchemaNodeForRelativeXPath(final SchemaContext context, final Module module,
+            final SchemaNode actualSchemaNode, final RevisionAwareXPath relativeXPath) {
+        if ((actualSchemaNode != null) && (relativeXPath != null) && !relativeXPath.isAbsolute()) {
 
             final SchemaPath actualNodePath = actualSchemaNode.getPath();
             if (actualNodePath != null) {
-                final Queue<QName> qnamePath = resolveRelativeXPath(context, module,
-                        relativeXPath, actualNodePath);
+                final Queue<QName> qnamePath = resolveRelativeXPath(context, module, relativeXPath, actualNodePath);
 
                 if (qnamePath != null) {
-                    final DataSchemaNode dataNode = findSchemaNodeForGivenPath(context,
-                            module, qnamePath);
+                    final DataSchemaNode dataNode = findSchemaNodeForGivenPath(context, module, qnamePath);
                     return dataNode;
                 }
             }
@@ -87,32 +72,33 @@ public final class SchemaContextUtil {
         return null;
     }
 
-    public static Module resolveModuleFromSchemaPath(final SchemaContext context, final SchemaPath schemaPath) {
+    private static Module resolveModuleFromSchemaPath(final SchemaContext context, final SchemaPath schemaPath) {
         if ((schemaPath != null) && (schemaPath.getPath() != null)) {
-            List<QName> path = schemaPath.getPath();
-            final QName qname = path.get(path.size()-1);
+            final List<QName> path = schemaPath.getPath();
+            if (!path.isEmpty()) {
+                final QName qname = path.get(path.size() - 1);
 
-            if ((qname != null) && (qname.getNamespace() != null)) {
-                return context.findModuleByNamespace(qname.getNamespace());
+                if ((qname != null) && (qname.getNamespace() != null)) {
+                    return context.findModuleByNamespace(qname.getNamespace());
+                }
             }
         }
         return null;
     }
 
-    public static Module resolveModuleFromTypePath(final SchemaContext context, final TypeDefinition<?> type) {
+    public static Module findParentModuleForTypeDefinition(final SchemaContext context, final TypeDefinition<?> type) {
         final SchemaPath schemaPath = type.getPath();
         if ((schemaPath != null) && (schemaPath.getPath() != null)) {
-            if(type instanceof ExtendedType) {
+            if (type instanceof ExtendedType) {
                 List<QName> path = schemaPath.getPath();
-                final QName qname = path.get(path.size()-1);
+                final QName qname = path.get(path.size() - 1);
 
                 if ((qname != null) && (qname.getNamespace() != null)) {
                     return context.findModuleByNamespace(qname.getNamespace());
                 }
             } else {
-                LinkedList<QName> path = new LinkedList<QName>(schemaPath.getPath());
-                path.removeLast();
-                final QName qname = path.get(path.size()-1);
+                List<QName> path = schemaPath.getPath();
+                final QName qname = path.get(path.size() - 2);
 
                 if ((qname != null) && (qname.getNamespace() != null)) {
                     return context.findModuleByNamespace(qname.getNamespace());
@@ -133,27 +119,26 @@ public final class SchemaContextUtil {
 
         final SchemaPath schemaPath = schemaNode.getPath();
         if (schemaPath == null) {
-            throw new IllegalStateException("Schema Path for Schema Node is not " +
-                    "set properly (Schema Path is NULL)");
+            throw new IllegalStateException("Schema Path for Schema Node is not "
+                    "set properly (Schema Path is NULL)");
         }
         final List<QName> qnamedPath = schemaPath.getPath();
         if (qnamedPath == null || qnamedPath.isEmpty()) {
-            throw new IllegalStateException("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.");
+            throw new IllegalStateException("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.");
         }
-        final QName qname = qnamedPath.get(0);
+        final QName qname = qnamedPath.get(qnamedPath.size() - 1);
         return context.findModuleByNamespace(qname.getNamespace());
     }
 
     private static DataSchemaNode findSchemaNodeForGivenPath(final SchemaContext context, final Module module,
             final Queue<QName> qnamedPath) {
-        if ((module != null) && (module.getNamespace() != null)
-                && (qnamedPath != null)) {
+        if ((module != null) && (module.getNamespace() != null) && (qnamedPath != null)) {
             DataNodeContainer nextNode = module;
             final URI moduleNamespace = module.getNamespace();
 
-            QName childNodeQName = null;
+            QName childNodeQName;
             DataSchemaNode schemaNode = null;
             while ((nextNode != null) && !qnamedPath.isEmpty()) {
                 childNodeQName = qnamedPath.peek();
@@ -166,14 +151,20 @@ public final class SchemaContextUtil {
                             nextNode = (ContainerSchemaNode) schemaNode;
                         } else if (schemaNode instanceof ListSchemaNode) {
                             nextNode = (ListSchemaNode) schemaNode;
+                        } else if (schemaNode instanceof ChoiceNode) {
+                            final ChoiceNode choice = (ChoiceNode) schemaNode;
+                            qnamedPath.poll();
+                            if (!qnamedPath.isEmpty()) {
+                                childNodeQName = qnamedPath.peek();
+                                nextNode = choice.getCaseNodeByName(childNodeQName);
+                                schemaNode = (DataSchemaNode)nextNode;
+                            }
                         } else {
                             nextNode = null;
                         }
                     } else if (!childNodeNamespace.equals(moduleNamespace)) {
-                        final Module nextModule = context
-                                .findModuleByNamespace(childNodeNamespace);
-                        schemaNode = findSchemaNodeForGivenPath(context, nextModule,
-                                qnamedPath);
+                        final Module nextModule = context.findModuleByNamespace(childNodeNamespace);
+                        schemaNode = findSchemaNodeForGivenPath(context, nextModule, qnamedPath);
                         return schemaNode;
                     }
                     qnamedPath.poll();
@@ -186,7 +177,7 @@ public final class SchemaContextUtil {
 
     private static Queue<QName> xpathToQNamePath(final SchemaContext context, final Module parentModule,
             final String xpath) {
-        final Queue<QName> path = new LinkedList<QName>();
+        final Queue<QName> path = new LinkedList<>();
         if (xpath != null) {
             final String[] prefixedPath = xpath.split("/");
 
@@ -204,22 +195,18 @@ public final class SchemaContextUtil {
         if (parentModule != null && prefixedPathPart != null) {
             if (prefixedPathPart.contains(":")) {
                 final String[] prefixedName = prefixedPathPart.split(":");
-                final Module module = resolveModuleForPrefix(context, parentModule,
-                        prefixedName[0]);
+                final Module module = resolveModuleForPrefix(context, parentModule, prefixedName[0]);
                 if (module != null) {
-                    return new QName(module.getNamespace(), module
-                            .getRevision(), prefixedName[1]);
+                    return new QName(module.getNamespace(), module.getRevision(), prefixedName[1]);
                 }
             } else {
-                return new QName(parentModule.getNamespace(),
-                        parentModule.getRevision(), prefixedPathPart);
+                return new QName(parentModule.getNamespace(), parentModule.getRevision(), prefixedPathPart);
             }
         }
         return null;
     }
 
-    private static Module resolveModuleForPrefix(final SchemaContext context, final Module module,
-            final String prefix) {
+    private static Module resolveModuleForPrefix(final SchemaContext context, final Module module, final String prefix) {
         if ((module != null) && (prefix != null)) {
             if (prefix.equals(module.getPrefix())) {
                 return module;
@@ -229,8 +216,7 @@ public final class SchemaContextUtil {
 
             for (final ModuleImport mi : imports) {
                 if (prefix.equals(mi.getPrefix())) {
-                    return context.findModuleByName(mi.getModuleName(),
-                            mi.getRevision());
+                    return context.findModuleByName(mi.getModuleName(), mi.getRevision());
                 }
             }
         }
@@ -238,12 +224,10 @@ public final class SchemaContextUtil {
     }
 
     private static Queue<QName> resolveRelativeXPath(final SchemaContext context, final Module module,
-            final RevisionAwareXPath relativeXPath,
-            final SchemaPath leafrefSchemaPath) {
-        final Queue<QName> absolutePath = new LinkedList<QName>();
+            final RevisionAwareXPath relativeXPath, final SchemaPath leafrefSchemaPath) {
+        final Queue<QName> absolutePath = new LinkedList<>();
 
-        if ((module != null) && (relativeXPath != null) && !relativeXPath.isAbsolute()
-                && (leafrefSchemaPath != null)) {
+        if ((module != null) && (relativeXPath != null) && !relativeXPath.isAbsolute() && (leafrefSchemaPath != null)) {
             final String strXPath = relativeXPath.toString();
             if (strXPath != null) {
                 final String[] xpaths = strXPath.split("/");