Merge "Added tests for yang.model.util"
[yangtools.git] / code-generator / maven-sal-api-gen-plugin / src / main / java / org / opendaylight / yangtools / yang / unified / doc / generator / GeneratorImpl.xtend
index 67b720511fbf142552170a55776b790d4a8c315a..f40cbcaa3dff249ad6cc8443028d380853fa8485 100644 (file)
@@ -21,8 +21,6 @@ import java.util.List
 import java.util.Map
 import java.util.Set
 import org.opendaylight.yangtools.yang.common.QName
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode
 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode
@@ -54,6 +52,9 @@ import org.slf4j.Logger
 import org.slf4j.LoggerFactory
 import org.sonatype.plexus.build.incremental.BuildContext
 import org.sonatype.plexus.build.incremental.DefaultBuildContext
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier
+import com.google.common.collect.Iterables
 
 class GeneratorImpl {
 
@@ -339,11 +340,8 @@ class GeneratorImpl {
     private def parseTargetPath(SchemaPath path) {
         val List<DataSchemaNode> nodes = new ArrayList<DataSchemaNode>();
         for (QName pathElement : path.pathFromRoot) {
-            val prefix = pathElement.prefix
-            val moduleName = imports.get(prefix)
-            if(moduleName != null) {
-                val revision = pathElement.revision
-                val module = ctx.findModuleByName(moduleName, revision)
+            val module = ctx.findModuleByNamespaceAndRevision(pathElement.namespace, pathElement.revision);
+            if (module !== null) {
                 var foundNode = module.getDataChildByName(pathElement)
                 if(foundNode == null) {
                     val child = nodes.last
@@ -367,7 +365,7 @@ class GeneratorImpl {
         return targetPathNodes
     }
     
-    private def DataSchemaNode findNodeInChildNodes(QName findingNode, Set<DataSchemaNode> childNodes) {
+    private def DataSchemaNode findNodeInChildNodes(QName findingNode, Iterable<DataSchemaNode> childNodes) {
         for(child : childNodes) {
             if (child.QName.equals(findingNode))
                 return child;
@@ -756,15 +754,15 @@ class GeneratorImpl {
 
     def CharSequence tree(Module module) '''
         «strong(module.name)»
-        «module.childNodes.treeSet(InstanceIdentifier.builder.toInstance())»
+        «module.childNodes.treeSet(YangInstanceIdentifier.builder.toInstance())»
     '''
 
-    private def dispatch CharSequence tree(ChoiceNode node,InstanceIdentifier path) '''
+    private def dispatch CharSequence tree(ChoiceNode node,YangInstanceIdentifier path) '''
         «node.nodeName» (choice)
         «casesTree(node.cases,path)»
     '''
 
-    def casesTree(Set<ChoiceCaseNode> nodes,InstanceIdentifier path) '''
+    def casesTree(Set<ChoiceCaseNode> nodes,YangInstanceIdentifier path) '''
         <ul>
         «FOR node : nodes»
             <li>
@@ -775,17 +773,17 @@ class GeneratorImpl {
         </ul>
     '''
 
-    private def dispatch CharSequence tree(DataSchemaNode node,InstanceIdentifier path) '''
+    private def dispatch CharSequence tree(DataSchemaNode node,YangInstanceIdentifier path) '''
         «node.nodeName»
     '''
 
-    private def dispatch CharSequence tree(ListSchemaNode node,InstanceIdentifier path) '''
+    private def dispatch CharSequence tree(ListSchemaNode node,YangInstanceIdentifier path) '''
         «val newPath = path.append(node)»
         «localLink(newPath,node.nodeName)»
         «node.childNodes.treeSet(newPath)»
     '''
 
-    private def dispatch CharSequence tree(ContainerSchemaNode node,InstanceIdentifier path) '''
+    private def dispatch CharSequence tree(ContainerSchemaNode node,YangInstanceIdentifier path) '''
         «val newPath = path.append(node)»
         «localLink(newPath,node.nodeName)»
         «node.childNodes.treeSet(newPath)»
@@ -796,7 +794,7 @@ class GeneratorImpl {
         «IF !childNodes.nullOrEmpty»
             <h2>Child nodes</h2>
 
-            «childNodes.printChildren(3,InstanceIdentifier.builder().toInstance())»
+            «childNodes.printChildren(3,YangInstanceIdentifier.builder().toInstance())»
         «ENDIF»
     '''
 
@@ -833,18 +831,15 @@ class GeneratorImpl {
 
     def String typeAnchorLink(SchemaPath path, CharSequence text) {
         if(path !== null) {
-            val prefix = path.path.last.prefix
-            if(prefix == this.currentModule.prefix) {
+            val lastElement = Iterables.getLast(path.pathFromRoot)
+            val ns = lastElement.namespace
+            if (ns == this.currentModule.namespace) {
                 return '''<a href="#«path.schemaPathToId»">«text»</a>'''
-            } else if(!prefix.nullOrEmpty){
-                val String module = imports.get(prefix)
-                if(!module.nullOrEmpty) {
-                    return '''«prefix»:«text»'''
-                    //to enable external (import) links
-                    //return '''<a href="«module».html#«path.schemaPathToId»">«prefix»:«text»</a>'''
-                }
+            } else {
+                return '''(«ns»)«text»'''
+                //to enable external (import) links
+                //return '''<a href="«module».html#«path.schemaPathToId»">«prefix»:«text»</a>'''
             }
-            return text.toString
         }
     }
 
@@ -950,7 +945,7 @@ class GeneratorImpl {
         '''
     }
 
-    def CharSequence printChildren(Set<DataSchemaNode> nodes, int level, InstanceIdentifier path) {
+    def CharSequence printChildren(Iterable<DataSchemaNode> nodes, int level, YangInstanceIdentifier path) {
         val anyxmlNodes = nodes.filter(AnyXmlSchemaNode)
         val leafNodes = nodes.filter(LeafSchemaNode)
         val leafListNodes = nodes.filter(LeafListSchemaNode)
@@ -980,9 +975,9 @@ class GeneratorImpl {
             </ul>
             «ENDIF»
 
-            «IF !path.path.empty»
+            «IF path.pathArguments.iterator.hasNext»
             <h3>XML example</h3>
-            «nodes.xmlExample(path.path.last.nodeType,path)»
+            «nodes.xmlExample(path.pathArguments.last.nodeType,path)»
             </h3>
             «ENDIF»
             «FOR childNode : containers»
@@ -1000,13 +995,13 @@ class GeneratorImpl {
         '''
     }
 
-    def CharSequence xmlExample(Set<DataSchemaNode> nodes, QName name,InstanceIdentifier path) '''
+    def CharSequence xmlExample(Iterable<DataSchemaNode> nodes, QName name,YangInstanceIdentifier path) '''
     <pre>
         «xmlExampleTag(name,nodes.xmplExampleTags(path))»
     </pre>
     '''
 
-    def CharSequence xmplExampleTags(Set<DataSchemaNode> nodes, InstanceIdentifier identifier) '''
+    def CharSequence xmplExampleTags(Iterable<DataSchemaNode> nodes, YangInstanceIdentifier identifier) '''
         <!-- Child nodes -->
         «FOR node : nodes»
         <!-- «node.QName.localName» -->
@@ -1015,29 +1010,29 @@ class GeneratorImpl {
 
     '''
 
-    private def dispatch CharSequence asXmlExampleTag(LeafSchemaNode node, InstanceIdentifier identifier) '''
+    private def dispatch CharSequence asXmlExampleTag(LeafSchemaNode node, YangInstanceIdentifier identifier) '''
         «node.QName.xmlExampleTag("...")»
     '''
 
-    private def dispatch CharSequence asXmlExampleTag(LeafListSchemaNode node, InstanceIdentifier identifier) '''
+    private def dispatch CharSequence asXmlExampleTag(LeafListSchemaNode node, YangInstanceIdentifier identifier) '''
         &lt!-- This node could appear multiple times --&gt
         «node.QName.xmlExampleTag("...")»
     '''
 
-    private def dispatch CharSequence asXmlExampleTag(ContainerSchemaNode node, InstanceIdentifier identifier) '''
+    private def dispatch CharSequence asXmlExampleTag(ContainerSchemaNode node, YangInstanceIdentifier identifier) '''
         &lt!-- See «localLink(identifier.append(node),"definition")» for child nodes.  --&gt
         «node.QName.xmlExampleTag("...")»
     '''
 
 
-    private def dispatch CharSequence asXmlExampleTag(ListSchemaNode node, InstanceIdentifier identifier) '''
+    private def dispatch CharSequence asXmlExampleTag(ListSchemaNode node, YangInstanceIdentifier identifier) '''
         &lt!-- See «localLink(identifier.append(node),"definition")» for child nodes.  --&gt
         &lt!-- This node could appear multiple times --&gt
         «node.QName.xmlExampleTag("...")»
     '''
 
 
-    private def dispatch CharSequence asXmlExampleTag(DataSchemaNode node, InstanceIdentifier identifier) '''
+    private def dispatch CharSequence asXmlExampleTag(DataSchemaNode node, YangInstanceIdentifier identifier) '''
         <!-- noop -->
     '''
 
@@ -1049,19 +1044,19 @@ class GeneratorImpl {
     def header(int level,QName name) '''<h«level»>«name.localName»</h«level»>'''
 
 
-    def header(int level,InstanceIdentifier name) '''
-        <h«level» id="«FOR cmp : name.path SEPARATOR "/"»«cmp.nodeType.localName»«ENDFOR»">
-            «FOR cmp : name.path SEPARATOR "/"»«cmp.nodeType.localName»«ENDFOR»
+    def header(int level,YangInstanceIdentifier name) '''
+        <h«level» id="«FOR cmp : name.pathArguments SEPARATOR "/"»«cmp.nodeType.localName»«ENDFOR»">
+            «FOR cmp : name.pathArguments SEPARATOR "/"»«cmp.nodeType.localName»«ENDFOR»
         </h«level»>
     '''
 
 
 
-    private def dispatch CharSequence printInfo(DataSchemaNode node, int level, InstanceIdentifier path) '''
+    private def dispatch CharSequence printInfo(DataSchemaNode node, int level, YangInstanceIdentifier path) '''
         «header(level+1,node.QName)»
     '''
 
-    private def dispatch CharSequence printInfo(ContainerSchemaNode node, int level, InstanceIdentifier path) '''
+    private def dispatch CharSequence printInfo(ContainerSchemaNode node, int level, YangInstanceIdentifier path) '''
         «val newPath = path.append(node)»
         «header(level,newPath)»
         <dl>
@@ -1073,7 +1068,7 @@ class GeneratorImpl {
         «node.childNodes.printChildren(level,newPath)»
     '''
 
-    private def dispatch CharSequence printInfo(ListSchemaNode node, int level, InstanceIdentifier path) '''
+    private def dispatch CharSequence printInfo(ListSchemaNode node, int level, YangInstanceIdentifier path) '''
         «val newPath = path.append(node)»
         «header(level,newPath)»
         <dl>
@@ -1085,18 +1080,18 @@ class GeneratorImpl {
         «node.childNodes.printChildren(level,newPath)»
     '''
 
-    private def dispatch CharSequence printInfo(ChoiceNode node, int level, InstanceIdentifier path) '''
+    private def dispatch CharSequence printInfo(ChoiceNode node, int level, YangInstanceIdentifier path) '''
         «val Set<DataSchemaNode> choiceCases = new HashSet(node.cases)»
         «choiceCases.printChildren(level,path)»
     '''
 
-    private def dispatch CharSequence printInfo(ChoiceCaseNode node, int level, InstanceIdentifier path) '''
+    private def dispatch CharSequence printInfo(ChoiceCaseNode node, int level, YangInstanceIdentifier path) '''
         «node.childNodes.printChildren(level,path)»
     '''
 
 
 
-    def CharSequence printShortInfo(ContainerSchemaNode node, int level, InstanceIdentifier path) {
+    def CharSequence printShortInfo(ContainerSchemaNode node, int level, YangInstanceIdentifier path) {
         val newPath = path.append(node);
         return '''
             <li>«strong(localLink(newPath,node.QName.localName))» (container)
@@ -1107,7 +1102,7 @@ class GeneratorImpl {
         '''
     }
 
-    def CharSequence printShortInfo(ListSchemaNode node, int level, InstanceIdentifier path) {
+    def CharSequence printShortInfo(ListSchemaNode node, int level, YangInstanceIdentifier path) {
         val newPath = path.append(node);
         return '''
             <li>«strong(localLink(newPath,node.QName.localName))» (list)
@@ -1118,7 +1113,7 @@ class GeneratorImpl {
         '''
     }
 
-    def CharSequence printShortInfo(AnyXmlSchemaNode node, int level, InstanceIdentifier path) {
+    def CharSequence printShortInfo(AnyXmlSchemaNode node, int level, YangInstanceIdentifier path) {
         return '''
             <li>«strong((node.QName.localName))» (anyxml)
             <ul>
@@ -1129,7 +1124,7 @@ class GeneratorImpl {
         '''
     }
 
-    def CharSequence printShortInfo(LeafSchemaNode node, int level, InstanceIdentifier path) {
+    def CharSequence printShortInfo(LeafSchemaNode node, int level, YangInstanceIdentifier path) {
         return '''
             <li>«strong((node.QName.localName))» (leaf)
             <ul>
@@ -1140,7 +1135,7 @@ class GeneratorImpl {
         '''
     }
 
-    def CharSequence printShortInfo(LeafListSchemaNode node, int level, InstanceIdentifier path) {
+    def CharSequence printShortInfo(LeafListSchemaNode node, int level, YangInstanceIdentifier path) {
         return '''
             <li>«strong((node.QName.localName))» (leaf-list)
             <ul>
@@ -1156,16 +1151,16 @@ class GeneratorImpl {
         '''
     }
 
-    def CharSequence localLink(InstanceIdentifier identifier, CharSequence text) '''
-        <a href="#«FOR cmp : identifier.path SEPARATOR "/"»«cmp.nodeType.localName»«ENDFOR»">«text»</a>
+    def CharSequence localLink(YangInstanceIdentifier identifier, CharSequence text) '''
+        <a href="#«FOR cmp : identifier.pathArguments SEPARATOR "/"»«cmp.nodeType.localName»«ENDFOR»">«text»</a>
     '''
 
 
-    private def dispatch InstanceIdentifier append(InstanceIdentifier identifier, ContainerSchemaNode node) {
+    private def dispatch YangInstanceIdentifier append(YangInstanceIdentifier identifier, ContainerSchemaNode node) {
         return identifier.node(node.QName);
     }
 
-    private def dispatch InstanceIdentifier append(InstanceIdentifier identifier, ListSchemaNode node) {
+    private def dispatch YangInstanceIdentifier append(YangInstanceIdentifier identifier, ListSchemaNode node) {
         val keyValues = new LinkedHashMap<QName,Object>();
         if(node.keyDefinition !== null) {
             for(definition : node.keyDefinition) {
@@ -1177,22 +1172,22 @@ class GeneratorImpl {
     }
 
 
-    def asXmlPath(InstanceIdentifier identifier) {
+    def asXmlPath(YangInstanceIdentifier identifier) {
         return "";
     }
 
-    def asRestconfPath(InstanceIdentifier identifier) {
+    def asRestconfPath(YangInstanceIdentifier identifier) {
         val it = new StringBuilder();
         append(currentModule.name)
         append(":")
         var previous = false;
-        for(arg : identifier.path) {
+        for(arg : identifier.pathArguments) {
             if(previous) append("/")
             append(arg.nodeType.localName);
             previous = true;
             if(arg instanceof NodeIdentifierWithPredicates) {
                 val nodeIdentifier = arg as NodeIdentifierWithPredicates;
-                for(qname : nodeIdentifier.keyValues.keySet) {
+                for(qname : nodeIdentifier.getKeyValues.keySet) {
                     append("/{");
                     append(qname.localName)
                     append("}")
@@ -1207,7 +1202,7 @@ class GeneratorImpl {
             val List<QName> path = schemaPath.path
         val StringBuilder pathString = new StringBuilder()
         if (schemaPath.absolute) {
-            pathString.append("/")
+            pathString.append('/')
         }
 
         val QName qname = path.get(0)
@@ -1233,17 +1228,12 @@ class GeneratorImpl {
                     }
                 }
 
-                var String prefix = name.prefix
-                var String moduleName
-                if (prefix == null || "".equals(prefix) || prefix.equals(module.prefix)) {
-                    moduleName = module.name
-                } else {
-                    moduleName = imports.get(prefix)
-                }
+                val pathElementModule = ctx.findModuleByNamespaceAndRevision(name.namespace, name.revision)
+                val String moduleName = pathElementModule.name
                 pathString.append(moduleName)
-                pathString.append(":")
+                pathString.append(':')
                 pathString.append(name.localName)
-                pathString.append("/")
+                pathString.append('/')
                 if(node instanceof ChoiceNode && dataNode !== null) {
                     val DataSchemaNode caseNode = dataNode.childNodes.findFirst[DataSchemaNode e | e instanceof ChoiceCaseNode];
                     if(caseNode !== null) {
@@ -1278,7 +1268,7 @@ class GeneratorImpl {
         «ENDIF»
     '''
 
-    private def CharSequence treeSet(Collection<DataSchemaNode> childNodes, InstanceIdentifier path) '''
+    private def CharSequence treeSet(Collection<DataSchemaNode> childNodes, YangInstanceIdentifier path) '''
         «IF childNodes !== null && !childNodes.empty»
             <ul>
             «FOR child : childNodes»
@@ -1301,7 +1291,7 @@ class GeneratorImpl {
         </ul>
     '''
 
-    private def dispatch CharSequence tree(Void obj, InstanceIdentifier path) '''
+    private def dispatch CharSequence tree(Void obj, YangInstanceIdentifier path) '''
     '''