Expanded UnknownSchemaNode implementation, refactored java source code generation.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / parser / builder / impl / ModuleBuilder.java
index f364ad45b1c579f4a1534d9d600c68200cfe5a33..e0ba03adfb0013f4eb69873ee96973b9f1c2733f 100644 (file)
@@ -29,6 +29,7 @@ import org.opendaylight.controller.yang.model.api.Module;
 import org.opendaylight.controller.yang.model.api.ModuleImport;
 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
 import org.opendaylight.controller.yang.model.api.RpcDefinition;
+import org.opendaylight.controller.yang.model.api.SchemaPath;
 import org.opendaylight.controller.yang.model.api.TypeDefinition;
 import org.opendaylight.controller.yang.model.api.UsesNode;
 import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;
@@ -41,6 +42,7 @@ import org.opendaylight.controller.yang.model.parser.builder.api.TypeAwareBuilde
 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionAwareBuilder;
 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
+import org.opendaylight.controller.yang.model.parser.util.RefineHolder;
 import org.opendaylight.controller.yang.model.parser.util.YangParseException;
 
 /**
@@ -51,9 +53,12 @@ import org.opendaylight.controller.yang.model.parser.util.YangParseException;
 public class ModuleBuilder implements Builder {
     private final ModuleImpl instance;
     private final String name;
+    private URI namespace;
     private String prefix;
     private Date revision;
 
+    private int augmentsResolved;
+
     private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
 
     /**
@@ -77,6 +82,7 @@ public class ModuleBuilder implements Builder {
     private final Map<String, DeviationBuilder> addedDeviations = new HashMap<String, DeviationBuilder>();
     private final Map<List<String>, TypeDefinitionBuilder> addedTypedefs = new HashMap<List<String>, TypeDefinitionBuilder>();
     private final List<ExtensionBuilder> addedExtensions = new ArrayList<ExtensionBuilder>();
+    private final Set<UnknownSchemaNodeBuilder> addedUnknownNodes = new HashSet<UnknownSchemaNodeBuilder>();
 
     private final Map<List<String>, TypeAwareBuilder> dirtyNodes = new HashMap<List<String>, TypeAwareBuilder>();
 
@@ -91,6 +97,7 @@ public class ModuleBuilder implements Builder {
     @Override
     public Module build() {
         instance.setImports(imports);
+        instance.setNamespace(namespace);
 
         // TYPEDEFS
         final Set<TypeDefinition<?>> typedefs = buildModuleTypedefs(addedTypedefs);
@@ -120,7 +127,6 @@ public class ModuleBuilder implements Builder {
         instance.setNotifications(notifications);
 
         // AUGMENTATIONS
-        // instance.setAugmentations(augmentations);
         final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
         for (AugmentationSchemaBuilder builder : addedAugments) {
             augmentations.add(builder.build());
@@ -160,6 +166,19 @@ public class ModuleBuilder implements Builder {
         return moduleNodes.get(path);
     }
 
+    public Set<DataSchemaNodeBuilder> getChildNodes() {
+        final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
+        for (Map.Entry<List<String>, DataSchemaNodeBuilder> entry : addedChilds
+                .entrySet()) {
+            List<String> path = entry.getKey();
+            DataSchemaNodeBuilder child = entry.getValue();
+            if (path.size() == 2) {
+                childNodes.add(child);
+            }
+        }
+        return childNodes;
+    }
+
     public Map<List<String>, TypeAwareBuilder> getDirtyNodes() {
         return dirtyNodes;
     }
@@ -176,6 +195,10 @@ public class ModuleBuilder implements Builder {
         return addedUsesNodes;
     }
 
+    public Set<UnknownSchemaNodeBuilder> getAddedUnknownNodes() {
+        return addedUnknownNodes;
+    }
+
     public Set<TypeDefinitionBuilder> getModuleTypedefs() {
         Set<TypeDefinitionBuilder> typedefs = new HashSet<TypeDefinitionBuilder>();
         for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs
@@ -191,6 +214,14 @@ public class ModuleBuilder implements Builder {
         return name;
     }
 
+    public URI getNamespace() {
+        return namespace;
+    }
+
+    public void setNamespace(final URI namespace) {
+        this.namespace = namespace;
+    }
+
     public String getPrefix() {
         return prefix;
     }
@@ -199,6 +230,14 @@ public class ModuleBuilder implements Builder {
         return revision;
     }
 
+    public int getAugmentsResolved() {
+        return augmentsResolved;
+    }
+
+    public void augmentResolved() {
+        augmentsResolved++;
+    }
+
     public void addDirtyNode(final List<String> path) {
         final List<String> dirtyNodePath = new ArrayList<String>(path);
         final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) moduleNodes
@@ -206,10 +245,6 @@ public class ModuleBuilder implements Builder {
         dirtyNodes.put(dirtyNodePath, nodeBuilder);
     }
 
-    public void setNamespace(final URI namespace) {
-        instance.setNamespace(namespace);
-    }
-
     public void setRevision(final Date revision) {
         this.revision = revision;
         instance.setRevision(revision);
@@ -390,10 +425,9 @@ public class ModuleBuilder implements Builder {
     public UsesNodeBuilder addUsesNode(final String groupingPathStr,
             final List<String> parentPath) {
         final List<String> pathToUses = new ArrayList<String>(parentPath);
-
-        UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(groupingPathStr);
-
-        ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes
+        final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(
+                groupingPathStr);
+        final ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes
                 .get(pathToUses);
         if (parent != null) {
             parent.addUsesNode(usesBuilder);
@@ -401,33 +435,46 @@ public class ModuleBuilder implements Builder {
 
         pathToUses.add(groupingPathStr);
         addedUsesNodes.put(pathToUses, usesBuilder);
-
+        moduleNodes.put(pathToUses, usesBuilder);
         return usesBuilder;
     }
 
-    public RpcDefinitionBuilder addRpc(final QName qname,
+    public void addRefine(final RefineHolder refine,
             final List<String> parentPath) {
-        List<String> pathToRpc = new ArrayList<String>(parentPath);
+        final List<String> path = new ArrayList<String>(parentPath);
+        final Builder parent = moduleNodes.get(path);
+        if (!(parent instanceof UsesNodeBuilder)) {
+            throw new YangParseException("Failed to parse refine "
+                    + refine.getName());
+        }
+        UsesNodeBuilder usesBuilder = (UsesNodeBuilder) parent;
+        usesBuilder.addRefine(refine);
+        path.add(refine.getName());
+        moduleNodes.put(path, refine);
+    }
 
-        RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(qname);
+    public RpcDefinitionBuilder addRpc(final QName qname,
+            final List<String> parentPath) {
+        final List<String> pathToRpc = new ArrayList<String>(parentPath);
+        final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(qname);
 
         pathToRpc.add(qname.getLocalName());
         addedRpcs.put(pathToRpc, rpcBuilder);
 
-        QName inputQName = new QName(qname.getNamespace(), qname.getRevision(),
-                qname.getPrefix(), "input");
-        ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(
+        final QName inputQName = new QName(qname.getNamespace(),
+                qname.getRevision(), qname.getPrefix(), "input");
+        final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(
                 inputQName);
-        List<String> pathToInput = new ArrayList<String>(pathToRpc);
+        final List<String> pathToInput = new ArrayList<String>(pathToRpc);
         pathToInput.add("input");
         moduleNodes.put(pathToInput, inputBuilder);
         rpcBuilder.setInput(inputBuilder);
 
-        QName outputQName = new QName(qname.getNamespace(),
+        final QName outputQName = new QName(qname.getNamespace(),
                 qname.getRevision(), qname.getPrefix(), "output");
-        ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(
+        final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(
                 outputQName);
-        List<String> pathToOutput = new ArrayList<String>(pathToRpc);
+        final List<String> pathToOutput = new ArrayList<String>(pathToRpc);
         pathToOutput.add("output");
         moduleNodes.put(pathToOutput, outputBuilder);
         rpcBuilder.setOutput(outputBuilder);
@@ -485,7 +532,7 @@ public class ModuleBuilder implements Builder {
         List<String> pathToCase = new ArrayList<String>(parentPath);
         ChoiceCaseBuilder builder = new ChoiceCaseBuilder(caseName);
 
-        final ChoiceBuilder parent = (ChoiceBuilder) moduleNodes
+        final ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes
                 .get(pathToCase);
         if (parent != null) {
             if (parent instanceof AugmentationSchemaBuilder) {
@@ -495,7 +542,6 @@ public class ModuleBuilder implements Builder {
         }
 
         pathToCase.add(caseName.getLocalName());
-        addedChilds.put(pathToCase, builder);
         moduleNodes.put(pathToCase, builder);
 
         return builder;
@@ -549,18 +595,32 @@ public class ModuleBuilder implements Builder {
         parent.setType(type);
     }
 
-    public void addUnionType(List<String> parentPath) {
+    public void addUnionType(final List<String> actualPath,
+            final URI namespace, final Date revision) {
+        List<String> pathToUnion = new ArrayList<String>(actualPath);
         TypeAwareBuilder parent = (TypeAwareBuilder) moduleNodes
-                .get(parentPath);
-        UnionTypeBuilder union = new UnionTypeBuilder();
+                .get(pathToUnion);
+        UnionTypeBuilder union = new UnionTypeBuilder(pathToUnion, namespace,
+                revision);
         parent.setType(union);
 
-        List<String> path = new ArrayList<String>(parentPath);
+        List<String> path = new ArrayList<String>(pathToUnion);
         path.add("union");
 
         moduleNodes.put(path, union);
     }
 
+    public void addIdentityrefType(String baseString, List<String> parentPath,
+            SchemaPath schemaPath) {
+        List<String> pathToIdentityref = new ArrayList<String>(parentPath);
+        TypeAwareBuilder parent = (TypeAwareBuilder) moduleNodes
+                .get(pathToIdentityref);
+        IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(
+                baseString, schemaPath);
+        parent.setType(identityref);
+        dirtyNodes.put(pathToIdentityref, parent);
+    }
+
     public DeviationBuilder addDeviation(String targetPath,
             List<String> parentPath) {
         final List<String> pathToDeviation = new ArrayList<String>(parentPath);
@@ -571,8 +631,12 @@ public class ModuleBuilder implements Builder {
         return builder;
     }
 
-    public IdentitySchemaNodeBuilder addIdentity(QName qname) {
+    public IdentitySchemaNodeBuilder addIdentity(QName qname,
+            List<String> parentPath) {
+        List<String> pathToIdentity = new ArrayList<String>(parentPath);
         IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(qname);
+        pathToIdentity.add(qname.getLocalName());
+        moduleNodes.put(pathToIdentity, builder);
         addedIdentities.add(builder);
         return builder;
     }
@@ -592,14 +656,14 @@ public class ModuleBuilder implements Builder {
         final List<String> pathToUnknown = new ArrayList<String>(parentPath);
         final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(
                 qname);
-
-        final SchemaNodeBuilder parent = (SchemaNodeBuilder) moduleNodes
-                .get(pathToUnknown);
-        if (parent != null) {
-            parent.addUnknownSchemaNode(builder);
+        final Builder parent = moduleNodes.get(pathToUnknown);
+        if (parent instanceof RefineHolder) {
+            ((RefineHolder) parent).addUnknownSchemaNode(builder);
+        } else if (parent instanceof SchemaNodeBuilder) {
+            ((SchemaNodeBuilder) parent).addUnknownSchemaNode(builder);
         }
-
-        return new UnknownSchemaNodeBuilder(qname);
+        addedUnknownNodes.add(builder);
+        return builder;
     }
 
     private class ModuleImpl implements Module {