BUG-1485: move generateLengthMethod to BuilderTemplate
[yangtools.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / YangModuleInfoTemplate.xtend
index 4feee7dbc0766bf47c25a0aed3e5e5a227a15515..64693702ff05b90f4fcd6f92e0e822385ed09d96 100644 (file)
@@ -20,7 +20,6 @@ import java.util.Map
 import java.util.Set
 import java.util.TreeMap
 
-import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil
 import org.opendaylight.yangtools.binding.generator.util.Types
 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
 import org.opendaylight.yangtools.sal.binding.model.api.Type
@@ -50,7 +49,7 @@ class YangModuleInfoTemplate {
         Preconditions.checkArgument(module != null, "Module must not be null.");
         this.module = module
         this.ctx = ctx
-        _packageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
+        _packageName = getRootPackageName(module.getQNameModule());
         _modelBindingProviderName = '''«packageName».«MODEL_BINDING_PROVIDER_CLASS_NAME»''';
     }
 
@@ -65,14 +64,14 @@ class YangModuleInfoTemplate {
                 «val DateFormat df = new SimpleDateFormat("yyyy-MM-dd")»
                 private final «String.importedName» revision = "«df.format(module.revision)»";
                 private final «String.importedName» resourcePath = "«sourcePath»";
-                
+
                 private final «Set.importedName»<YangModuleInfo> importedModules;
 
                 public static «YangModuleInfo.importedName» getInstance() {
                     return INSTANCE;
                 }
 
-                «module.classBody»
+                «classBody(module, MODULE_INFO_CLASS_NAME)»
             }
         '''
         return '''
@@ -96,10 +95,12 @@ class YangModuleInfoTemplate {
 
     }
 
-    private def CharSequence classBody(Module m) '''
-        private «MODULE_INFO_CLASS_NAME»() {
-            «IF m.imports.size != 0»
+    private def CharSequence classBody(Module m, String className) '''
+        private «className»() {
+            «IF !m.imports.empty || !m.submodules.empty»
                 «Set.importedName»<«YangModuleInfo.importedName»> set = new «HashSet.importedName»<>();
+            «ENDIF»
+            «IF !m.imports.empty»
                 «FOR imp : m.imports»
                     «val name = imp.moduleName»
                     «val rev = imp.revision»
@@ -111,15 +112,23 @@ class YangModuleInfoTemplate {
                                 «sorted.put(module.revision, module)»
                             «ENDIF»
                         «ENDFOR»
-                        set.add(«BindingGeneratorUtil.moduleNamespaceToPackageName(sorted.lastEntry().value)».«MODULE_INFO_CLASS_NAME».getInstance());
+                        set.add(«getRootPackageName(sorted.lastEntry().value.QNameModule)».«MODULE_INFO_CLASS_NAME».getInstance());
                     «ELSE»
-                        set.add(«BindingGeneratorUtil.moduleNamespaceToPackageName(ctx.findModuleByName(name, rev))».«MODULE_INFO_CLASS_NAME».getInstance());
+                        set.add(«getRootPackageName((ctx.findModuleByName(name, rev).QNameModule))».«MODULE_INFO_CLASS_NAME».getInstance());
                     «ENDIF»
                 «ENDFOR»
-                importedModules = «ImmutableSet.importedName».copyOf(set);
-            «ELSE»
+            «ENDIF»
+            «IF !m.submodules.empty»
+                «FOR submodule : m.submodules»
+                    set.add(«getClassName(submodule.name)»Info.getInstance());
+                «ENDFOR»
+            «ENDIF»
+            «IF m.imports.empty && m.submodules.empty»
                 importedModules = «Collections.importedName».emptySet();
+            «ELSE»
+                importedModules = «ImmutableSet.importedName».copyOf(set);
             «ENDIF»
+
             «InputStream.importedName» stream = «MODULE_INFO_CLASS_NAME».class.getResourceAsStream(resourcePath);
             if (stream == null) {
                 throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
@@ -172,16 +181,19 @@ class YangModuleInfoTemplate {
             sb.append("]");
             return sb.toString();
         }
+
+        «generateSubInfo(m)»
+
     '''
 
     def getSourcePath() {
         return "/" + module.moduleSourcePath.replace(java.io.File.separatorChar, '/')
     }
 
-    private def imports() ''' 
+    private def imports() '''
         «IF !importMap.empty»
             «FOR entry : importMap.entrySet»
-                «IF entry.value != BindingGeneratorUtil.moduleNamespaceToPackageName(module)»
+                «IF entry.value != getRootPackageName(module.QNameModule)»
                     import «entry.value».«entry.key»;
                 «ENDIF»
             «ENDFOR»
@@ -204,8 +216,7 @@ class YangModuleInfoTemplate {
             importMap.put(typeName, typePackageName);
         }
         if (type instanceof ParameterizedType) {
-            val ParameterizedType paramType = (type as ParameterizedType)
-            val Type[] params = paramType.getActualTypeArguments()
+            val Type[] params = type.getActualTypeArguments()
             if (params != null) {
                 for (Type param : params) {
                     putTypeIntoImports(param);
@@ -246,11 +257,10 @@ class YangModuleInfoTemplate {
 
     final def StringBuilder addActualTypeParameters(StringBuilder builder, Type type) {
         if (type instanceof ParameterizedType) {
-            val ParameterizedType pType = (type as ParameterizedType)
-            val Type[] pTypes = pType.getActualTypeArguments();
-            builder.append("<");
+            val Type[] pTypes = type.getActualTypeArguments();
+            builder.append('<');
             builder.append(getParameters(pTypes));
-            builder.append(">");
+            builder.append('>');
         }
         return builder;
     }
@@ -286,4 +296,27 @@ class YangModuleInfoTemplate {
         return builder.toString();
     }
 
+    private def generateSubInfo(Module module) '''
+        «FOR submodule : module.submodules»
+            private static final class «getClassName(submodule.name)»Info implements «YangModuleInfo.importedName» {
+
+                private static final «YangModuleInfo.importedName» INSTANCE = new «getClassName(submodule.name)»Info();
+
+                private final «String.importedName» name = "«submodule.name»";
+                private final «String.importedName» namespace = "«submodule.namespace.toString»";
+                «val DateFormat df = new SimpleDateFormat("yyyy-MM-dd")»
+                private final «String.importedName» revision = "«df.format(submodule.revision)»";
+                private final «String.importedName» resourcePath = "/«submodule.moduleSourcePath.replace(java.io.File.separatorChar, '/')»";
+
+                private final «Set.importedName»<YangModuleInfo> importedModules;
+
+                public static «YangModuleInfo.importedName» getInstance() {
+                    return INSTANCE;
+                }
+
+                «classBody(submodule, getClassName(submodule.name + "Info"))»
+            }
+        «ENDFOR»
+    '''
+
 }