Maping of union leaf a bits leaf YANG type to JAVA inner classes.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / controller / sal / java / api / generator / InterfaceGenerator.java
index abaf4fad21bb03caa81b2b1d82677bfbd7718618..27570ca77d7ede88b8c72cb110dfdcbfced1a634 100644 (file)
@@ -12,75 +12,102 @@ import static org.opendaylight.controller.sal.java.api.generator.Constants.*;
 import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.opendaylight.controller.sal.binding.model.api.CodeGenerator;
-import org.opendaylight.controller.sal.binding.model.api.Constant;
-import org.opendaylight.controller.sal.binding.model.api.Enumeration;
-import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
-import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
-import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
-import org.opendaylight.controller.sal.binding.model.api.Type;
+import org.opendaylight.controller.sal.binding.model.api.*;
 
-public class InterfaceGenerator implements CodeGenerator {
+public final class InterfaceGenerator implements CodeGenerator {
 
-    private Map<String, LinkedHashMap<String, Integer>> imports;
+    private Map<String, String> imports;
+    private Map<String, String> innerTypeImports;
+
+    private String generateEnums(List<Enumeration> enums) {
+        String result = "";
+        if (enums != null) {
+            EnumGenerator enumGenerator = new EnumGenerator();
+            for (Enumeration en : enums) {
+                try {
+                    result = result + (enumGenerator.generateInnerEnumeration(en, TAB).toString() + NL);
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return result;
+    }
+
+    private String generateConstants(List<Constant> constants, String pkgName) {
+        String result = "";
+        if (constants != null) {
+            for (Constant c : constants) {
+                result = result + GeneratorUtil.createConstant(c, TAB, imports, pkgName) + NL;
+            }
+            result.concat(NL);
+        }
+        return result;
+    }
+
+    public String generateMethods(List<MethodSignature> methods, String pkgName) {
+        String result = "";
+
+        if (methods != null) {
+            for (MethodSignature m : methods) {
+                result = result + GeneratorUtil.createMethodDeclaration(m, TAB, imports, pkgName) + NL;
+            }
+            result = result + NL;
+        }
+        return result;
+    }
+
+    public String generateInnerClasses(final List<GeneratedType> generatedTypes) throws IOException {
+        String result = "";
+
+        if (generatedTypes != null) {
+            ClassCodeGenerator classCodeGenerator = new ClassCodeGenerator();
+            for (GeneratedType genType : generatedTypes) {
+                if (genType instanceof GeneratedTransferObject) {
+                    result = result + classCodeGenerator.generateOnlyClass(genType, imports).toString();
+                    result = result + NL + NL;
+                }
+            }
+        }
+
+        return result;
+    }
 
     public Writer generate(Type type) throws IOException {
         Writer writer = new StringWriter();
-        if (type instanceof GeneratedType
-                && !(type instanceof GeneratedTransferObject)) {
-            GeneratedType genType = (GeneratedType) type;
+        if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
+            final GeneratedType genType = (GeneratedType) type;
             imports = GeneratorUtil.createImports(genType);
+            innerTypeImports = GeneratorUtil.createChildImports(genType);
 
             final String currentPkg = genType.getPackageName();
             final List<Constant> constants = genType.getConstantDefinitions();
-            final List<MethodSignature> methods = genType
-                    .getMethodDefinitions();
-            final List<Enumeration> enums = genType.getEnumDefintions();
+            final List<MethodSignature> methods = genType.getMethodDefinitions();
+            final List<Enumeration> enums = genType.getEnumerations();
+            final List<GeneratedType> enclosedGeneratedTypes = genType.getEnclosedTypes();
 
-            writer.write(GeneratorUtil.createPackageDeclaration(genType
-                    .getPackageName()));
+            writer.write(GeneratorUtil.createPackageDeclaration(genType.getPackageName()));
             writer.write(NL);
 
-            List<String> importLines = GeneratorUtil.createImportLines(imports);
+            List<String> importLines = GeneratorUtil.createImportLines(imports, innerTypeImports);
+
             for (String line : importLines) {
                 writer.write(line + NL);
             }
             writer.write(NL);
-
-            writer.write(GeneratorUtil.createIfcDeclaration(genType, "",
-                    imports));
+            writer.write(GeneratorUtil.createIfcDeclaration(genType, "", imports));
             writer.write(NL);
 
-            if (constants != null) {
-                for (Constant c : constants) {
-                    writer.write(GeneratorUtil.createConstant(c, TAB, imports,
-                            currentPkg) + NL);
-                }
-                writer.write(NL);
-            }
-
-            if (methods != null) {
-                for (MethodSignature m : methods) {
-                    writer.write(GeneratorUtil.createMethodDeclaration(m, TAB,
-                            imports, currentPkg) + NL);
-                }
-                writer.write(NL);
-            }
-
-            if (enums != null) {
-                for (Enumeration e : enums) {
-                    writer.write(GeneratorUtil.createEnum(e, TAB) + NL);
-                }
-                writer.write(NL);
-            }
+            writer.write(generateInnerClasses(enclosedGeneratedTypes));
+            writer.write(generateEnums(enums));
+            writer.write(generateConstants(constants, currentPkg));
+            writer.write(generateMethods(methods, currentPkg));
 
             writer.write(RCB);
         }
         return writer;
     }
-
 }