Merge "Package names for enclosed Types of TOs were changed to fully qualified. Fully...
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / api / AbstractDataNodeContainerBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.yang.parser.builder.api;
9
10 import java.util.Collections;
11 import java.util.HashSet;
12 import java.util.Set;
13
14 import org.opendaylight.controller.yang.common.QName;
15 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
16 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
17 import org.opendaylight.controller.yang.parser.util.YangParseException;
18
19 /**
20  * Basic implementation of DataNodeContainerBuilder.
21  */
22 public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder implements DataNodeContainerBuilder {
23     protected final QName qname;
24
25     protected Set<DataSchemaNode> childNodes;
26     protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<DataSchemaNodeBuilder>();
27
28     protected Set<GroupingDefinition> groupings;
29     protected final Set<GroupingBuilder> addedGroupings = new HashSet<GroupingBuilder>();
30
31     protected AbstractDataNodeContainerBuilder(final String moduleName, final int line, final QName qname) {
32         super(moduleName, line);
33         this.qname = qname;
34     }
35
36     @Override
37     public QName getQName() {
38         return qname;
39     }
40
41     @Override
42     public Set<DataSchemaNode> getChildNodes() {
43         if (childNodes == null) {
44             return Collections.emptySet();
45         }
46         return childNodes;
47     }
48
49     public void setChildNodes(Set<DataSchemaNode> childNodes) {
50         this.childNodes = childNodes;
51     }
52
53     @Override
54     public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
55         return addedChildNodes;
56     }
57
58     @Override
59     public DataSchemaNodeBuilder getDataChildByName(final String name) {
60         for (DataSchemaNodeBuilder child : addedChildNodes) {
61             if (child.getQName().getLocalName().equals(name)) {
62                 return child;
63             }
64         }
65         return null;
66     }
67
68     @Override
69     public void addChildNode(DataSchemaNodeBuilder child) {
70         String childName = child.getQName().getLocalName();
71         for (DataSchemaNodeBuilder addedChildNode : addedChildNodes) {
72             if (addedChildNode.getQName().getLocalName().equals(childName)) {
73                 throw new YangParseException(child.getModuleName(), child.getLine(), "Can not add '" + child
74                         + "' to node '" + qname.getLocalName() + "' in module '" + moduleName
75                         + "': node with same name already declared at line " + addedChildNode.getLine());
76             }
77         }
78         addedChildNodes.add(child);
79     }
80
81     @Override
82     public Set<GroupingDefinition> getGroupings() {
83         if (groupings == null) {
84             return Collections.emptySet();
85         }
86         return groupings;
87     }
88
89     public void setGroupings(final Set<GroupingDefinition> groupings) {
90         this.groupings = groupings;
91     }
92
93     public Set<GroupingBuilder> getGroupingBuilders() {
94         return addedGroupings;
95     }
96
97     @Override
98     public void addGrouping(GroupingBuilder grouping) {
99         String groupingName = grouping.getQName().getLocalName();
100         for (GroupingBuilder addedGrouping : addedGroupings) {
101             if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
102                 throw new YangParseException(grouping.getModuleName(), grouping.getLine(), "Can not add '" + grouping
103                         + "': grouping with same name already declared in module '" + moduleName + "' at line "
104                         + addedGrouping.getLine());
105             }
106         }
107         addedGroupings.add(grouping);
108     }
109
110 }