Refactored YANG types resolving.
[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 5f89e864559fd9f8d74dadc691150b367b16168f..86adcdd71d45ec704d1673d25abd0b975372d7ba 100644 (file)
@@ -24,6 +24,7 @@ import org.opendaylight.controller.yang.model.api.Deviation;
 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
 import org.opendaylight.controller.yang.model.api.FeatureDefinition;
 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
+import org.opendaylight.controller.yang.model.api.IdentitySchemaNode;
 import org.opendaylight.controller.yang.model.api.Module;
 import org.opendaylight.controller.yang.model.api.ModuleImport;
 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
@@ -39,6 +40,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.YangParseException;
 
 /**
  * This builder builds Module object. If this module is dependent on external
@@ -71,6 +73,7 @@ public class ModuleBuilder implements Builder {
     private final Map<List<String>, UsesNodeBuilder> addedUsesNodes = new HashMap<List<String>, UsesNodeBuilder>();
     private final Map<List<String>, RpcDefinitionBuilder> addedRpcs = new HashMap<List<String>, RpcDefinitionBuilder>();
     private final Set<NotificationBuilder> addedNotifications = new HashSet<NotificationBuilder>();
+    private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<IdentitySchemaNodeBuilder>();
     private final Map<List<String>, FeatureBuilder> addedFeatures = new HashMap<List<String>, FeatureBuilder>();
     private final Map<String, DeviationBuilder> addedDeviations = new HashMap<String, DeviationBuilder>();
     private final Map<List<String>, TypeDefinitionBuilder> addedTypedefs = new HashMap<List<String>, TypeDefinitionBuilder>();
@@ -84,7 +87,6 @@ public class ModuleBuilder implements Builder {
     }
 
 
-
     /**
      * Build new Module object based on this builder.
      */
@@ -105,8 +107,8 @@ public class ModuleBuilder implements Builder {
         instance.setGroupings(groupings);
 
         // USES
-        final Set<UsesNode> usesNodeDefinitions = buildUsesNodes(addedUsesNodes);
-        instance.setUses(usesNodeDefinitions);
+        final Set<UsesNode> usesDefinitions = buildUsesNodes(addedUsesNodes);
+        instance.setUses(usesDefinitions);
 
         // FEATURES
         final Set<FeatureDefinition> features = buildModuleFeatures(addedFeatures);
@@ -141,6 +143,13 @@ public class ModuleBuilder implements Builder {
         }
         instance.setExtensionSchemaNodes(extensions);
 
+        // IDENTITIES
+        final Set<IdentitySchemaNode> identities = new HashSet<IdentitySchemaNode>();
+        for(IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
+            identities.add(idBuilder.build());
+        }
+        instance.setIdentities(identities);
+
         return instance;
     }
 
@@ -152,6 +161,14 @@ public class ModuleBuilder implements Builder {
         return dirtyNodes;
     }
 
+    public Set<AugmentationSchemaBuilder> getAddedAugments() {
+        return addedAugments;
+    }
+
+    public Set<IdentitySchemaNodeBuilder> getAddedIdentities() {
+        return addedIdentities;
+    }
+
     public String getName() {
         return name;
     }
@@ -164,10 +181,6 @@ public class ModuleBuilder implements Builder {
         return revision;
     }
 
-    public Set<AugmentationSchemaBuilder> getAddedAugments() {
-        return addedAugments;
-    }
-
     public void addDirtyNode(List<String> path) {
         List<String> dirtyNodePath = new ArrayList<String>(path);
         TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) moduleNodes
@@ -225,8 +238,7 @@ public class ModuleBuilder implements Builder {
     }
 
     public ExtensionBuilder addExtension(QName qname) {
-        ExtensionBuilder builder = new ExtensionBuilder(qname);
-        return builder;
+        return new ExtensionBuilder(qname);
     }
 
     public ContainerSchemaNodeBuilder addContainerNode(QName containerName,
@@ -359,9 +371,6 @@ public class ModuleBuilder implements Builder {
 
         ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes.get(pathToUses);
         if (parent != null) {
-            if(parent instanceof AugmentationSchemaBuilder) {
-                usesBuilder.setAugmenting(true);
-            }
             parent.addUsesNode(usesBuilder);
         }
 
@@ -402,14 +411,14 @@ public class ModuleBuilder implements Builder {
             List<String> parentPath) {
         List<String> pathToNotification = new ArrayList<String>(parentPath);
 
-        NotificationBuilder notificationBuilder = new NotificationBuilder(
+        NotificationBuilder builder = new NotificationBuilder(
                 notificationName);
 
         pathToNotification.add(notificationName.getLocalName());
-        moduleNodes.put(pathToNotification, notificationBuilder);
-        addedNotifications.add(notificationBuilder);
+        moduleNodes.put(pathToNotification, builder);
+        addedNotifications.add(builder);
 
-        return notificationBuilder;
+        return builder;
     }
 
     public FeatureBuilder addFeature(QName featureName, List<String> parentPath) {
@@ -446,15 +455,35 @@ public class ModuleBuilder implements Builder {
 
     public void setType(TypeDefinition<?> type, List<String> parentPath) {
         TypeAwareBuilder parent = (TypeAwareBuilder) moduleNodes.get(parentPath);
+        if(parent == null) {
+            throw new YangParseException("Failed to set type '"+ type.getQName().getLocalName() +"'. Parent node not found.");
+        }
         parent.setType(type);
     }
 
+    public void addUnionType(List<String> parentPath) {
+        TypeAwareBuilder parent = (TypeAwareBuilder) moduleNodes.get(parentPath);
+        UnionTypeBuilder union = new UnionTypeBuilder();
+        parent.setType(union);
+
+        List<String> path = new ArrayList<String>(parentPath);
+        path.add("union");
+
+        moduleNodes.put(path, union);
+    }
+
     public DeviationBuilder addDeviation(String targetPath) {
         DeviationBuilder builder = new DeviationBuilder(targetPath);
         addedDeviations.put(targetPath, builder);
         return builder;
     }
 
+    public IdentitySchemaNodeBuilder addIdentity(QName qname) {
+        IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(qname);
+        addedIdentities.add(builder);
+        return builder;
+    }
+
     public void addConfiguration(boolean configuration, List<String> parentPath) {
         Builder builder = moduleNodes.get(parentPath);
         if (builder instanceof DeviationBuilder) {
@@ -467,8 +496,7 @@ public class ModuleBuilder implements Builder {
     }
 
     public UnknownSchemaNodeBuilder addUnknownSchemaNode(QName qname, List<String> parentPath) {
-        UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(qname);
-        return builder;
+        return new UnknownSchemaNodeBuilder(qname);
     }
 
 
@@ -492,7 +520,8 @@ public class ModuleBuilder implements Builder {
         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
         private Set<GroupingDefinition> groupings = Collections.emptySet();
         private Set<UsesNode> uses = Collections.emptySet();
-        private List<ExtensionDefinition> extensionSchemaNodes = Collections.emptyList();
+        private List<ExtensionDefinition> extensionNodes = Collections.emptyList();
+        private Set<IdentitySchemaNode> identities = Collections.emptySet();
 
         private ModuleImpl(String name) {
             this.name = name;
@@ -581,7 +610,9 @@ public class ModuleBuilder implements Builder {
         }
 
         private void setImports(Set<ModuleImport> imports) {
-            this.imports = imports;
+            if(imports != null) {
+                this.imports = imports;
+            }
         }
 
         @Override
@@ -590,7 +621,9 @@ public class ModuleBuilder implements Builder {
         }
 
         private void setFeatures(Set<FeatureDefinition> features) {
-            this.features = features;
+            if(features != null) {
+                this.features = features;
+            }
         }
 
         @Override
@@ -599,7 +632,9 @@ public class ModuleBuilder implements Builder {
         }
 
         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
-            this.typeDefinitions = typeDefinitions;
+            if(typeDefinitions != null) {
+                this.typeDefinitions = typeDefinitions;
+            }
         }
 
         @Override
@@ -608,7 +643,9 @@ public class ModuleBuilder implements Builder {
         }
 
         private void setNotifications(Set<NotificationDefinition> notifications) {
-            this.notifications = notifications;
+            if(notifications != null) {
+                this.notifications = notifications;
+            }
         }
 
         @Override
@@ -617,7 +654,9 @@ public class ModuleBuilder implements Builder {
         }
 
         private void setAugmentations(Set<AugmentationSchema> augmentations) {
-            this.augmentations = augmentations;
+            if(augmentations != null) {
+                this.augmentations = augmentations;
+            }
         }
 
         @Override
@@ -626,7 +665,9 @@ public class ModuleBuilder implements Builder {
         }
 
         private void setRpcs(Set<RpcDefinition> rpcs) {
-            this.rpcs = rpcs;
+            if(rpcs != null) {
+                this.rpcs = rpcs;
+            }
         }
 
         @Override
@@ -635,7 +676,9 @@ public class ModuleBuilder implements Builder {
         }
 
         private void setDeviations(Set<Deviation> deviations) {
-            this.deviations = deviations;
+            if(deviations != null) {
+                this.deviations = deviations;
+            }
         }
 
         @Override
@@ -644,7 +687,9 @@ public class ModuleBuilder implements Builder {
         }
 
         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
-            this.childNodes = childNodes;
+            if(childNodes != null) {
+                this.childNodes = childNodes;
+            }
         }
 
         @Override
@@ -653,7 +698,9 @@ public class ModuleBuilder implements Builder {
         }
 
         private void setGroupings(Set<GroupingDefinition> groupings) {
-            this.groupings = groupings;
+            if(groupings != null) {
+                this.groupings = groupings;
+            }
         }
 
         @Override
@@ -662,17 +709,30 @@ public class ModuleBuilder implements Builder {
         }
 
         private void setUses(Set<UsesNode> uses) {
-            this.uses = uses;
+            if(uses != null) {
+                this.uses = uses;
+            }
         }
 
         @Override
         public List<ExtensionDefinition> getExtensionSchemaNodes() {
-            return extensionSchemaNodes;
+            return extensionNodes;
         }
 
         private void setExtensionSchemaNodes(List<ExtensionDefinition> extensionSchemaNodes) {
             if(extensionSchemaNodes != null) {
-                this.extensionSchemaNodes = extensionSchemaNodes;
+                this.extensionNodes = extensionSchemaNodes;
+            }
+        }
+
+        @Override
+        public Set<IdentitySchemaNode> getIdentities() {
+            return identities;
+        }
+
+        private void setIdentities(Set<IdentitySchemaNode> identities) {
+            if(identities != null) {
+                this.identities = identities;
             }
         }
 
@@ -702,21 +762,6 @@ public class ModuleBuilder implements Builder {
             result = prime * result + ((revision == null) ? 0 : revision.hashCode());
             result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
             result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
-            result = prime * result + ((description == null) ? 0 : description.hashCode());
-            result = prime * result + ((reference == null) ? 0 : reference.hashCode());
-
-            result = prime * result + ((organization == null) ? 0 : organization.hashCode());
-            result = prime * result + ((contact == null) ? 0 : contact.hashCode());
-            result = prime * result + ((imports == null) ? 0 : imports.hashCode());
-            result = prime * result + ((features == null) ? 0 : features.hashCode());
-            result = prime * result + ((typeDefinitions == null) ? 0 : typeDefinitions.hashCode());
-            result = prime * result + ((notifications == null) ? 0 : notifications.hashCode());
-            result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());
-            result = prime * result + ((rpcs == null) ? 0 : rpcs.hashCode());
-            result = prime * result + ((deviations == null) ? 0 : deviations.hashCode());
-            result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
-            result = prime * result + ((groupings == null) ? 0 : groupings.hashCode());
-            result = prime * result + ((uses == null) ? 0 : uses.hashCode());
             return result;
         }
 
@@ -767,104 +812,6 @@ public class ModuleBuilder implements Builder {
             } else if (!yangVersion.equals(other.yangVersion)) {
                 return false;
             }
-            if (description == null) {
-                if (other.description != null) {
-                    return false;
-                }
-            } else if (!description.equals(other.description)) {
-                return false;
-            }
-            if (reference == null) {
-                if (other.reference != null) {
-                    return false;
-                }
-            } else if (!reference.equals(other.reference)) {
-                return false;
-            }
-            if (organization == null) {
-                if (other.organization != null) {
-                    return false;
-                }
-            } else if (!organization.equals(other.organization)) {
-                return false;
-            }
-            if (contact == null) {
-                if (other.contact != null) {
-                    return false;
-                }
-            } else if (!contact.equals(other.contact)) {
-                return false;
-            }
-            if (imports == null) {
-                if (other.imports != null) {
-                    return false;
-                }
-            } else if (!imports.equals(other.imports)) {
-                return false;
-            }
-            if (features == null) {
-                if (other.features != null) {
-                    return false;
-                }
-            } else if (!features.equals(other.features)) {
-                return false;
-            }
-            if (typeDefinitions == null) {
-                if (other.typeDefinitions != null) {
-                    return false;
-                }
-            } else if (!typeDefinitions.equals(other.typeDefinitions)) {
-                return false;
-            }
-            if (notifications == null) {
-                if (other.notifications != null) {
-                    return false;
-                }
-            } else if (!notifications.equals(other.notifications)) {
-                return false;
-            }
-            if (augmentations == null) {
-                if (other.augmentations != null) {
-                    return false;
-                }
-            } else if (!augmentations.equals(other.augmentations)) {
-                return false;
-            }
-            if (rpcs == null) {
-                if (other.rpcs != null) {
-                    return false;
-                }
-            } else if (!rpcs.equals(other.rpcs)) {
-                return false;
-            }
-            if (deviations == null) {
-                if (other.deviations != null) {
-                    return false;
-                }
-            } else if (!deviations.equals(other.deviations)) {
-                return false;
-            }
-            if (childNodes == null) {
-                if (other.childNodes != null) {
-                    return false;
-                }
-            } else if (!childNodes.equals(other.childNodes)) {
-                return false;
-            }
-            if (groupings == null) {
-                if (other.groupings != null) {
-                    return false;
-                }
-            } else if (!groupings.equals(other.groupings)) {
-                return false;
-            }
-            if (uses == null) {
-                if (other.uses != null) {
-                    return false;
-                }
-            } else if (!uses.equals(other.uses)) {
-                return false;
-            }
             return true;
         }
 
@@ -984,11 +931,12 @@ public class ModuleBuilder implements Builder {
     private Map<QName, DataSchemaNode> buildModuleChildNodes(
             Map<List<String>, DataSchemaNodeBuilder> addedChilds) {
         final Map<QName, DataSchemaNode> childNodes = new HashMap<QName, DataSchemaNode>();
-        for (Map.Entry<List<String>, DataSchemaNodeBuilder> entry : addedChilds
-                .entrySet()) {
-            if (entry.getKey().size() == 2) {
-                DataSchemaNode node = entry.getValue().build();
-                QName qname = entry.getValue().getQName();
+        for (Map.Entry<List<String>, DataSchemaNodeBuilder> entry : addedChilds.entrySet()) {
+            List<String> path = entry.getKey();
+            DataSchemaNodeBuilder child = entry.getValue();
+            if (path.size() == 2) {
+                DataSchemaNode node = child.build();
+                QName qname = node.getQName();
                 childNodes.put(qname, node);
             }
         }
@@ -1045,11 +993,11 @@ public class ModuleBuilder implements Builder {
     private Set<TypeDefinition<?>> buildModuleTypedefs(
             Map<List<String>, TypeDefinitionBuilder> addedTypedefs) {
         Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
-        for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs
-                .entrySet()) {
-            if (entry.getKey().size() == 2) {
-                TypeDefinition<? extends TypeDefinition<?>> node = entry
-                        .getValue().build();
+        for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs.entrySet()) {
+            List<String> key = entry.getKey();
+            TypeDefinitionBuilder typedefBuilder = entry.getValue();
+            if (key.size() == 2) {
+                TypeDefinition<? extends TypeDefinition<?>> node = typedefBuilder.build();
                 typedefs.add(node);
             }
         }