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 8ca88d8d6110aa6807da562ff3c2543c10faeb57..091bfc72337e8037a1d823f81f352990d00126ec 100644 (file)
@@ -7,16 +7,19 @@
  */
 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 {
-
-    private final QName qname;
+    protected final int line;
+    protected final QName qname;
+    protected Builder parent;
 
     protected Set<DataSchemaNode> childNodes;
     protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<DataSchemaNodeBuilder>();
@@ -24,40 +27,92 @@ public abstract class AbstractDataNodeContainerBuilder implements DataNodeContai
     protected Set<GroupingDefinition> groupings;
     protected final Set<GroupingBuilder> addedGroupings = new HashSet<GroupingBuilder>();
 
-    protected AbstractDataNodeContainerBuilder(QName qname) {
+    protected AbstractDataNodeContainerBuilder(final int line, final QName qname) {
+        this.line = line;
         this.qname = qname;
     }
 
     @Override
-    public QName getQName() {
-        return qname;
+    public int getLine() {
+        return line;
     }
 
     @Override
-    public Set<DataSchemaNodeBuilder> getChildNodes() {
-        return addedChildNodes;
+    public Builder getParent() {
+        return parent;
+    }
+
+    @Override
+    public void setParent(final Builder parent) {
+        this.parent = parent;
     }
 
     @Override
-    public void addChildNode(DataSchemaNodeBuilder childNode) {
-        addedChildNodes.add(childNode);
+    public QName getQName() {
+        return qname;
+    }
+
+    @Override
+    public Set<DataSchemaNode> getChildNodes() {
+        if (childNodes == null) {
+            return Collections.emptySet();
+        }
+        return childNodes;
     }
 
     public void setChildNodes(Set<DataSchemaNode> childNodes) {
         this.childNodes = childNodes;
     }
 
-    public Set<GroupingBuilder> getGroupings() {
-        return addedGroupings;
+    @Override
+    public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
+        return addedChildNodes;
+    }
+
+    @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 void addGrouping(GroupingBuilder grouping) {
-        addedGroupings.add(grouping);
+    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 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);
+    }
+
 }