Bug 1411-3: MDSAL Binding2 Generator Util
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / binding / generator / impl / YangTemplate.xtend
index 6738a665ddb7a5486a99d5cfb9df067aa33a6941..322d335f6520714d4f8f6b9e5bd8736f7eed5fa3 100644 (file)
@@ -12,8 +12,7 @@ import java.util.Date
 import java.util.List
 import java.util.Map
 import java.util.Set
-import java.util.StringTokenizer
-import org.opendaylight.yangtools.yang.common.QName
+import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode
@@ -34,21 +33,18 @@ import org.opendaylight.yangtools.yang.model.api.NotificationDefinition
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition
 import org.opendaylight.yangtools.yang.model.api.SchemaNode
 import org.opendaylight.yangtools.yang.model.api.SchemaPath
+import org.opendaylight.yangtools.yang.model.api.Status
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode
 import org.opendaylight.yangtools.yang.model.api.UsesNode
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair
-import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil
-import com.google.common.base.CharMatcher
 
 class YangTemplate {
 
     // FIXME: this is not thread-safe and seems to be unused!
     private static var Module module = null
 
-    private static val CharMatcher NEWLINE_OR_TAB = CharMatcher.anyOf("\n\t")
-
     def static String generateYangSnipet(SchemaNode schemaNode) {
         if (schemaNode == null)
             return ''
@@ -86,11 +82,11 @@ class YangTemplate {
             «ENDIF»
         '''
     }
-    
+
     def static String generateYangSnipet(Set<? extends SchemaNode> nodes) {
         if (nodes.nullOrEmpty)
             return ''
-        
+
         '''
             «FOR node : nodes»
                 «IF node instanceof NotificationDefinition»
@@ -102,17 +98,15 @@ class YangTemplate {
         '''
     }
 
-    def static writeEnumPair(EnumPair pair) {
-        var boolean hasEnumPairValue = pair.value != null
+    def private static writeEnumPair(EnumPair pair) {
         '''
-            enum «pair.name»«IF !hasEnumPairValue»;«ELSE»{
+            enum «pair.name» {
                 value «pair.value»;
             }
-            «ENDIF»
         '''
     }
 
-    def static String writeModuleImports(Set<ModuleImport> moduleImports) {
+    def private static String writeModuleImports(Set<ModuleImport> moduleImports) {
         if (moduleImports.nullOrEmpty)
             return ''
 
@@ -125,12 +119,12 @@ class YangTemplate {
         '''
     }
 
-    def static writeRevision(Date moduleRevision, String moduleDescription) {
+    def private static writeRevision(Date moduleRevision, String moduleDescription) {
         val revisionIndent = 12
 
         '''
             revision «SimpleDateFormatUtil.getRevisionFormat.format(moduleRevision)» {
-                description "«formatToParagraph(moduleDescription, revisionIndent)»";
+                description "«YangTextTemplate.formatToParagraph(moduleDescription, revisionIndent)»";
             }
         '''
     }
@@ -197,7 +191,7 @@ class YangTemplate {
         '''
     }
 
-    def static writeRPCs(Set<RpcDefinition> rpcDefs) {
+    def private static writeRPCs(Set<RpcDefinition> rpcDefs) {
         '''
             «FOR rpc : rpcDefs»
                 «IF rpc != null»
@@ -207,7 +201,8 @@ class YangTemplate {
         '''
     }
 
-    def static writeRPC(RpcDefinition rpc) {
+    def private static writeRPC(RpcDefinition rpc) {
+        var boolean isStatusDeprecated = rpc.status == Status::DEPRECATED
         '''
             rpc «rpc.QName.localName» {
                 «IF !rpc.description.nullOrEmpty»
@@ -226,14 +221,14 @@ class YangTemplate {
                 reference
                     "«rpc.reference»";
                 «ENDIF»
-                «IF rpc.status != null»
+                «IF isStatusDeprecated»
                 status «rpc.status»;
                 «ENDIF»
             }
         '''
     }
 
-    def static writeRpcInput(ContainerSchemaNode input) {
+    def private static writeRpcInput(ContainerSchemaNode input) {
         if(input == null)
             return ''
 
@@ -247,7 +242,7 @@ class YangTemplate {
         '''
     }
 
-    def static writeRpcOutput(ContainerSchemaNode output) {
+    def private static writeRpcOutput(ContainerSchemaNode output) {
         if(output == null)
             return ''
 
@@ -260,7 +255,7 @@ class YangTemplate {
         '''
     }
 
-    def static writeNotifications(Set<NotificationDefinition> notifications) {
+    def private static writeNotifications(Set<NotificationDefinition> notifications) {
         '''
             «FOR notification : notifications»
                 «IF notification != null»
@@ -270,7 +265,8 @@ class YangTemplate {
         '''
     }
 
-    def static writeNotification(NotificationDefinition notification) {
+    def private static writeNotification(NotificationDefinition notification) {
+        var boolean isStatusDeprecated = notification.status == Status::DEPRECATED
         '''
             notification «notification.QName.localName» {
                 «IF !notification.description.nullOrEmpty»
@@ -293,14 +289,14 @@ class YangTemplate {
                 reference
                     "«notification.reference»";
                 «ENDIF»
-                «IF notification.status != null»
+                «IF isStatusDeprecated»
                 status «notification.status»;
                 «ENDIF»
             }
         '''
     }
 
-    def static writeUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
+    def private static writeUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
         if (unknownSchemaNodes.nullOrEmpty)
             return ''
 
@@ -311,11 +307,11 @@ class YangTemplate {
         '''
     }
 
-    def static writeUnknownSchemaNode(UnknownSchemaNode unknownSchemaNode) {
+    def private static writeUnknownSchemaNode(UnknownSchemaNode unknownSchemaNode) {
         return ''
     }
 
-    def static writeUsesNodes(Set<UsesNode> usesNodes) {
+    def private static writeUsesNodes(Set<UsesNode> usesNodes) {
         if (usesNodes == null) {
             return ''
         }
@@ -329,7 +325,7 @@ class YangTemplate {
         '''
     }
 
-    def static writeUsesNode(UsesNode usesNode) {
+    def private static writeUsesNode(UsesNode usesNode) {
         val hasRefines = !usesNode.refines.empty
 
         '''
@@ -341,7 +337,7 @@ class YangTemplate {
         '''
     }
 
-    def static writeRefines(Map<SchemaPath, SchemaNode> refines) {
+    def private static writeRefines(Map<SchemaPath, SchemaNode> refines) {
         '''
             «FOR path : refines.keySet»
             «val schemaNode = refines.get(path)»
@@ -350,7 +346,7 @@ class YangTemplate {
         '''
     }
 
-    def static writeRefine(SchemaPath path, SchemaNode schemaNode) {
+    def private static writeRefine(SchemaPath path, SchemaNode schemaNode) {
         '''
             refine «path.pathFromRoot.last» {
                 «IF schemaNode instanceof DataSchemaNode»
@@ -360,23 +356,17 @@ class YangTemplate {
         '''
     }
 
-    def static writeTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
-        '''
-            «FOR typeDefinition : typeDefinitions»
-                «IF typeDefinition != null»
-                «writeTypeDefinition(typeDefinition)»
-                «ENDIF»
-            «ENDFOR»
-        '''
-    }
-
-    def static writeTypeDefinition(TypeDefinition<?> typeDefinition) {
+    def private static writeTypeDefinition(TypeDefinition<?> typeDefinition) {
+        var boolean isStatusDeprecated = typeDefinition.status == Status::DEPRECATED
         '''
-            type «typeDefinition.QName.localName»;
+            type «typeDefinition.QName.localName»«IF !isStatusDeprecated»;«ELSE» {
+                status «typeDefinition.status»;
+            }
+            «ENDIF»
         '''
     }
 
-    def static writeIdentities(Set<IdentitySchemaNode> identities) {
+    def private static writeIdentities(Set<IdentitySchemaNode> identities) {
         if (identities.nullOrEmpty)
             return ''
         '''
@@ -386,7 +376,7 @@ class YangTemplate {
         '''
     }
 
-    def static writeIdentity(IdentitySchemaNode identity) {
+    def private static writeIdentity(IdentitySchemaNode identity) {
         if (identity == null)
             return ''
         '''
@@ -409,7 +399,7 @@ class YangTemplate {
         '''
     }
 
-    def static writeIdentityNs(IdentitySchemaNode identity) {
+    def private static writeIdentityNs(IdentitySchemaNode identity) {
         if(module == null)
             return ''
 
@@ -420,7 +410,7 @@ class YangTemplate {
         return ''
     }
 
-    def static writeFeatures(Set<FeatureDefinition> features) {
+    def private static writeFeatures(Set<FeatureDefinition> features) {
         '''
             «FOR feature : features»
                 «IF feature != null»
@@ -430,7 +420,7 @@ class YangTemplate {
         '''
     }
 
-    def static writeFeature(FeatureDefinition featureDef) {
+    def private static writeFeature(FeatureDefinition featureDef) {
         '''
             feature «featureDef.QName.localName» {
                 «IF !featureDef.description.nullOrEmpty»
@@ -448,7 +438,7 @@ class YangTemplate {
         '''
     }
 
-    def static writeExtensions(List<ExtensionDefinition> extensions) {
+    def private static writeExtensions(List<ExtensionDefinition> extensions) {
         '''
             «FOR anExtension : extensions»
                 «IF anExtension != null»
@@ -458,7 +448,7 @@ class YangTemplate {
         '''
     }
 
-    def static writeExtension(ExtensionDefinition extensionDef) {
+    def private static writeExtension(ExtensionDefinition extensionDef) {
         '''
             extension «extensionDef.QName.localName» {
                 «IF !extensionDef.description.nullOrEmpty»
@@ -479,7 +469,7 @@ class YangTemplate {
         '''
     }
 
-    def static writeDeviations(Set<Deviation> deviations) {
+    def private static writeDeviations(Set<Deviation> deviations) {
         '''
             «FOR deviation : deviations»
                 «IF deviation != null»
@@ -489,21 +479,23 @@ class YangTemplate {
         '''
     }
 
-    def static writeDeviation(Deviation deviation) {
+    def private static writeDeviation(Deviation deviation) {
         '''
             deviation «deviation.targetPath» {
                 «IF !deviation.reference.nullOrEmpty»
                     reference
                         "«deviation.reference»";
                 «ENDIF»
-                «IF deviation.deviate != null && !deviation.deviate.name.nullOrEmpty»
-                    deviation «deviation.deviate.name»;
-                «ENDIF»
+                «FOR dev : deviation.deviates»
+                    «IF dev != null && dev.deviateType != null»
+                        deviation «dev.deviateType.name»;
+                    «ENDIF»
+                «ENDFOR»
             }
         '''
     }
 
-    def static writeAugments(Set<AugmentationSchema> augments) {
+    def private static writeAugments(Set<AugmentationSchema> augments) {
         '''
             «FOR augment : augments»
                 «IF augment != null»
@@ -513,7 +505,7 @@ class YangTemplate {
         '''
     }
 
-    def static writeDataSchemaNodes(Collection<DataSchemaNode> dataSchemaNodes) {
+    def private static writeDataSchemaNodes(Collection<DataSchemaNode> dataSchemaNodes) {
         '''
             «FOR schemaNode : dataSchemaNodes»
                 «writeDataSchemaNode(schemaNode)»
@@ -521,7 +513,7 @@ class YangTemplate {
         '''
     }
 
-    def static CharSequence writeGroupingDefs(Set<GroupingDefinition> groupingDefs) {
+    def private static CharSequence writeGroupingDefs(Set<GroupingDefinition> groupingDefs) {
         '''
             «FOR groupingDef : groupingDefs»
                 «IF groupingDef != null»
@@ -531,9 +523,9 @@ class YangTemplate {
         '''
     }
 
-    def static writeAugment(AugmentationSchema augment) {
+    def private static writeAugment(AugmentationSchema augment) {
         '''
-            augment «formatToAugmentPath(augment.targetPath.pathFromRoot)» {
+            augment «YangTextTemplate.formatToAugmentPath(augment.targetPath.pathFromRoot)» {
                 «IF augment.whenCondition != null && !augment.whenCondition.toString.nullOrEmpty»
                 when "«augment.whenCondition.toString»";
                 «ENDIF»
@@ -558,7 +550,8 @@ class YangTemplate {
         '''
     }
 
-    def static writeGroupingDef(GroupingDefinition groupingDef) {
+    def private static writeGroupingDef(GroupingDefinition groupingDef) {
+        var boolean isStatusDeprecated = groupingDef.status == Status::DEPRECATED
         '''
             grouping «groupingDef.QName.localName» {
                 «IF !groupingDef.groupings.nullOrEmpty»
@@ -567,6 +560,9 @@ class YangTemplate {
                 «IF !groupingDef.childNodes.nullOrEmpty»
                     «writeDataSchemaNodes(groupingDef.childNodes)»
                 «ENDIF»
+                «IF isStatusDeprecated»
+                    status «groupingDef.status»;
+                «ENDIF»
                 «IF !groupingDef.unknownSchemaNodes.nullOrEmpty»
                     «writeUnknownSchemaNodes(groupingDef.unknownSchemaNodes)»
                 «ENDIF»
@@ -574,7 +570,8 @@ class YangTemplate {
         '''
     }
 
-    def static writeContSchemaNode(ContainerSchemaNode contSchemaNode) {
+    def private static writeContSchemaNode(ContainerSchemaNode contSchemaNode) {
+        var boolean isStatusDeprecated = contSchemaNode.status == Status::DEPRECATED
         '''
             container «contSchemaNode.getQName.localName» {
                 «IF !contSchemaNode.childNodes.nullOrEmpty»
@@ -589,6 +586,9 @@ class YangTemplate {
                 «IF !contSchemaNode.uses.nullOrEmpty»
                 «writeUsesNodes(contSchemaNode.uses)»
                 «ENDIF»
+                «IF isStatusDeprecated»
+                status «contSchemaNode.status»;
+                «ENDIF»
                 «IF !contSchemaNode.unknownSchemaNodes.nullOrEmpty»
                 «writeUnknownSchemaNodes(contSchemaNode.unknownSchemaNodes)»
                 «ENDIF»
@@ -596,49 +596,71 @@ class YangTemplate {
         '''
     }
 
-    def static writeAnyXmlSchemaNode(AnyXmlSchemaNode anyXmlSchemaNode) {
+    def private static writeAnyXmlSchemaNode(AnyXmlSchemaNode anyXmlSchemaNode) {
+        var boolean isStatusDeprecated = anyXmlSchemaNode.status == Status::DEPRECATED
         '''
-            anyxml «anyXmlSchemaNode.getQName.localName»;
+            anyxml «anyXmlSchemaNode.getQName.localName»«IF !isStatusDeprecated»;«ELSE» {
+                status «anyXmlSchemaNode.status»;
+            }
+            «ENDIF»
         '''
     }
 
-    def static writeLeafSchemaNode(LeafSchemaNode leafSchemaNode) {
+    def private static writeLeafSchemaNode(LeafSchemaNode leafSchemaNode) {
+        var boolean isStatusDeprecated = leafSchemaNode.status == Status::DEPRECATED
         '''
             leaf «leafSchemaNode.getQName.localName» {
                 type «leafSchemaNode.type.getQName.localName»;
+                «IF isStatusDeprecated»
+                    status «leafSchemaNode.status»;
+                «ENDIF»
             }
         '''
     }
 
-    def static writeLeafListSchemaNode(LeafListSchemaNode leafListSchemaNode) {
+    def private static writeLeafListSchemaNode(LeafListSchemaNode leafListSchemaNode) {
+        var boolean isStatusDeprecated = leafListSchemaNode.status == Status::DEPRECATED
         '''
             leaf-list «leafListSchemaNode.getQName.localName» {
                 type «leafListSchemaNode.type.getQName.localName»;
+                «IF isStatusDeprecated»
+                    status «leafListSchemaNode.status»;
+                «ENDIF»
             }
         '''
     }
 
-    def static writeChoiceCaseNode(ChoiceCaseNode choiceCaseNode) {
+    def private static writeChoiceCaseNode(ChoiceCaseNode choiceCaseNode) {
+        var boolean isStatusDeprecated = choiceCaseNode.status == Status::DEPRECATED
         '''
             case «choiceCaseNode.getQName.localName» {
                 «FOR childNode : choiceCaseNode.childNodes»
                     «writeDataSchemaNode(childNode)»
                 «ENDFOR»
+                «IF isStatusDeprecated»
+                    status «choiceCaseNode.status»;
+                «ENDIF»
             }
         '''
     }
 
-    def static writeChoiceNode(ChoiceSchemaNode choiceNode) {
+    def private static writeChoiceNode(ChoiceSchemaNode choiceNode) {
+        var boolean isStatusDeprecated = choiceNode.status == Status::DEPRECATED
         '''
             choice «choiceNode.getQName.localName» {
                 «FOR child : choiceNode.cases»
                     «writeDataSchemaNode(child)»
                 «ENDFOR»
+                «IF isStatusDeprecated»
+                    status «choiceNode.status»;
+                «ENDIF»
             }
         '''
     }
 
-    def static writeListSchemaNode(ListSchemaNode listSchemaNode) {
+    def private static writeListSchemaNode(ListSchemaNode listSchemaNode) {
+        var boolean isStatusDeprecated = listSchemaNode.status == Status::DEPRECATED
+
         '''
             list «listSchemaNode.getQName.localName» {
                 key «FOR listKey : listSchemaNode.keyDefinition SEPARATOR " "»"«listKey.localName»"
@@ -655,6 +677,9 @@ class YangTemplate {
                 «IF !listSchemaNode.uses.nullOrEmpty»
                     «writeUsesNodes(listSchemaNode.uses)»
                 «ENDIF»
+                «IF isStatusDeprecated»
+                    status «listSchemaNode.status»;
+                «ENDIF»
                 «IF !listSchemaNode.unknownSchemaNodes.nullOrEmpty»
                     «writeUnknownSchemaNodes(listSchemaNode.unknownSchemaNodes)»
                 «ENDIF»
@@ -662,7 +687,7 @@ class YangTemplate {
         '''
     }
 
-    def static CharSequence writeDataSchemaNode(DataSchemaNode child) {
+    def private static CharSequence writeDataSchemaNode(DataSchemaNode child) {
         '''
             «IF child instanceof ContainerSchemaNode»
                 «writeContSchemaNode(child)»
@@ -687,99 +712,4 @@ class YangTemplate {
             «ENDIF»
         '''
     }
-    
-    static def String formatSchemaPath(String moduleName, Iterable<QName> schemaPath) {
-        var currentElement = schemaPath.head
-        val StringBuilder sb = new StringBuilder()
-        sb.append(moduleName)
-
-        for(pathElement : schemaPath) {
-            if(!currentElement.namespace.equals(pathElement.namespace)) {
-                currentElement = pathElement
-                sb.append('/')
-                sb.append(pathElement)
-            }
-            else {
-                sb.append('/')
-                sb.append(pathElement.localName)
-            }
-        }
-        return sb.toString
-    }
-
-    static def String formatToParagraph(String text, int nextLineIndent) {
-        if (text == null || text.isEmpty())
-            return '';
-
-        var String formattedText = text;
-        val StringBuilder sb = new StringBuilder();
-        val StringBuilder lineBuilder = new StringBuilder();
-        var boolean isFirstElementOnNewLineEmptyChar = false;
-        val lineIndent = computeNextLineIndent(nextLineIndent);
-
-        formattedText = NEWLINE_OR_TAB.removeFrom(formattedText);
-        formattedText = formattedText.replaceAll(" +", " ");
-
-        val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true);
-
-        while (tokenizer.hasMoreElements()) {
-            val String nextElement = tokenizer.nextElement().toString();
-
-            if (lineBuilder.length() + nextElement.length() > 80) {
-                if (lineBuilder.charAt(lineBuilder.length() - 1) == ' ') {
-                    lineBuilder.setLength(0);
-                    lineBuilder.append(lineBuilder.substring(0, lineBuilder.length() - 1));
-                }
-                if (lineBuilder.charAt(0) == ' ') {
-                    lineBuilder.setLength(0);
-                    lineBuilder.append(lineBuilder.substring(1));
-                }
-
-                sb.append(lineBuilder);
-                lineBuilder.setLength(0);
-                sb.append("\n");
-
-                if (nextLineIndent > 0) {
-                    sb.append(lineIndent)
-                }
-
-                if (nextElement.toString().equals(" "))
-                    isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
-            }
-            if (isFirstElementOnNewLineEmptyChar) {
-                isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
-            } else {
-                lineBuilder.append(nextElement);
-            }
-        }
-        sb.append(lineBuilder);
-        sb.append("\n");
-
-        return sb.toString();
-    }
-
-    def private static formatToAugmentPath(Iterable<QName> schemaPath) {
-        val StringBuilder sb = new StringBuilder();
-
-        for(pathElement : schemaPath) {
-            val ns = pathElement.namespace
-            val localName = pathElement.localName
-
-            sb.append("\\(")
-            sb.append(ns)
-            sb.append(')')
-            sb.append(localName)
-        }
-        return sb.toString
-    }
-
-    private static def computeNextLineIndent(int nextLineIndent) {
-        val StringBuilder sb = new StringBuilder()
-        var i = 0
-        while (i < nextLineIndent) {
-            sb.append(' ')
-            i = i + 1
-        }
-        return sb.toString
-    }
 }