Fixed a bug to block the creation of a static host on an ISL port, removed the code...
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / api / AbstractDataNodeContainerBuilder.java
index c56b40488c525978811566670fbe94396065d244..091bfc72337e8037a1d823f81f352990d00126ec 100644 (file)
  */
 package org.opendaylight.controller.yang.parser.builder.api;
 
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 
 import org.opendaylight.controller.yang.common.QName;
+import org.opendaylight.controller.yang.model.api.DataSchemaNode;
+import org.opendaylight.controller.yang.model.api.GroupingDefinition;
+import org.opendaylight.controller.yang.parser.util.YangParseException;
 
 public abstract class AbstractDataNodeContainerBuilder implements DataNodeContainerBuilder {
+    protected final int line;
+    protected final QName qname;
+    protected Builder parent;
 
-    private final QName qname;
-    protected final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
-    protected final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
+    protected Set<DataSchemaNode> childNodes;
+    protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<DataSchemaNodeBuilder>();
 
-    protected AbstractDataNodeContainerBuilder(QName qname) {
+    protected Set<GroupingDefinition> groupings;
+    protected final Set<GroupingBuilder> addedGroupings = new HashSet<GroupingBuilder>();
+
+    protected AbstractDataNodeContainerBuilder(final int line, final QName qname) {
+        this.line = line;
         this.qname = qname;
     }
 
+    @Override
+    public int getLine() {
+        return line;
+    }
+
+    @Override
+    public Builder getParent() {
+        return parent;
+    }
+
+    @Override
+    public void setParent(final Builder parent) {
+        this.parent = parent;
+    }
+
     @Override
     public QName getQName() {
         return qname;
     }
 
     @Override
-    public Set<DataSchemaNodeBuilder> getChildNodes() {
+    public Set<DataSchemaNode> getChildNodes() {
+        if (childNodes == null) {
+            return Collections.emptySet();
+        }
         return childNodes;
     }
 
+    public void setChildNodes(Set<DataSchemaNode> childNodes) {
+        this.childNodes = childNodes;
+    }
+
     @Override
-    public void addChildNode(DataSchemaNodeBuilder childNode) {
-        childNodes.add(childNode);
+    public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
+        return addedChildNodes;
     }
 
-    public Set<GroupingBuilder> getGroupings() {
+    @Override
+    public DataSchemaNodeBuilder getDataChildByName(final String name) {
+        for (DataSchemaNodeBuilder child : addedChildNodes) {
+            if (child.getQName().getLocalName().equals(name)) {
+                return child;
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public void addChildNode(DataSchemaNodeBuilder child) {
+        for (DataSchemaNodeBuilder childNode : addedChildNodes) {
+            if (childNode.getQName().getLocalName().equals(child.getQName().getLocalName())) {
+                throw new YangParseException(child.getLine(), "Duplicate node found at line " + childNode.getLine());
+            }
+        }
+        addedChildNodes.add(child);
+    }
+
+    @Override
+    public Set<GroupingDefinition> getGroupings() {
+        if (groupings == null) {
+            return Collections.emptySet();
+        }
         return groupings;
     }
 
+    public void setGroupings(final Set<GroupingDefinition> groupings) {
+        this.groupings = groupings;
+    }
+
+    public Set<GroupingBuilder> getGroupingBuilders() {
+        return addedGroupings;
+    }
+
     @Override
-    public void addGrouping(GroupingBuilder grouping) {
-        groupings.add(grouping);
+    public void addGrouping(GroupingBuilder groupingBuilder) {
+        for (GroupingBuilder gb : addedGroupings) {
+            if (gb.getQName().getLocalName().equals(groupingBuilder.getQName().getLocalName())) {
+                throw new YangParseException(groupingBuilder.getLine(), "Duplicate node found at line " + gb.getLine());
+            }
+        }
+        addedGroupings.add(groupingBuilder);
     }
 
 }