ebb12dbc00ddf4ea565ba733beaf21072afdc956
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / api / AbstractDataNodeContainerBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
3  * This program and the accompanying materials are made available under the
4  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/epl-v10.html
6  */
7 package org.opendaylight.yangtools.yang.parser.builder.api;
8
9 import java.util.Collections;
10 import java.util.HashSet;
11 import java.util.Map;
12 import java.util.Set;
13 import java.util.TreeMap;
14 import java.util.TreeSet;
15
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.UsesNode;
21 import org.opendaylight.yangtools.yang.parser.util.Comparators;
22 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
23
24 /**
25  * Basic implementation of DataNodeContainerBuilder.
26  */
27 public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder implements DataNodeContainerBuilder {
28     protected final QName qname;
29
30     protected final Map<QName, DataSchemaNode> childNodes = new TreeMap<>();
31     protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<>();
32
33     protected final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
34     protected final Set<GroupingBuilder> addedGroupings = new HashSet<>();
35
36     protected final Set<TypeDefinition<?>> typedefs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
37     protected final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<>();
38
39     protected final Set<UsesNode> usesNodes = new HashSet<>();
40     protected final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<>();
41
42     protected AbstractDataNodeContainerBuilder(final String moduleName, final int line, final QName qname) {
43         super(moduleName, line);
44         this.qname = qname;
45     }
46
47     @Override
48     public QName getQName() {
49         return qname;
50     }
51
52     public Map<QName, DataSchemaNode> getChildNodes() {
53         return childNodes;
54     }
55
56     @Override
57     public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
58         return addedChildNodes;
59     }
60
61     @Override
62     public DataSchemaNodeBuilder getDataChildByName(final String name) {
63         for (DataSchemaNodeBuilder child : addedChildNodes) {
64             if (child.getQName().getLocalName().equals(name)) {
65                 return child;
66             }
67         }
68         return null;
69     }
70
71     @Override
72     public void addChildNode(final DataSchemaNodeBuilder child) {
73         QName childName = child.getQName();
74         for (DataSchemaNodeBuilder addedChildNode : addedChildNodes) {
75             if (addedChildNode.getQName().equals(childName)) {
76                 throw new YangParseException(child.getModuleName(), child.getLine(), String.format(
77                         "Can not add '%s' to '%s' in module '%s': node with same name already declared at line %d",
78                         child, this, getModuleName(), addedChildNode.getLine()));
79             }
80         }
81         addedChildNodes.add(child);
82     }
83
84     @Override
85     public void addChildNodeToContext(final DataSchemaNodeBuilder child) {
86         addedChildNodes.add(child);
87     }
88
89     @Override
90     public void addChildNode(final DataSchemaNode child) {
91         QName childName = child.getQName();
92         if (childNodes.containsKey(childName)) {
93             throw new YangParseException(getModuleName(), getLine(),
94                     String.format("Can not add '%s' to '%s' in module '%s': node with same name already declared",
95                             child, this, getModuleName()));
96         }
97         childNodes.put(childName, child);
98     }
99
100     @Override
101     public Set<GroupingDefinition> getGroupings() {
102         if (groupings == null) {
103             return Collections.emptySet();
104         }
105         return groupings;
106     }
107
108     @Override
109     public Set<GroupingBuilder> getGroupingBuilders() {
110         return addedGroupings;
111     }
112
113     @Override
114     public void addGrouping(final GroupingBuilder grouping) {
115         QName groupingName = grouping.getQName();
116         for (GroupingBuilder addedGrouping : addedGroupings) {
117             if (addedGrouping.getQName().equals(groupingName)) {
118                 throw new YangParseException(grouping.getModuleName(), grouping.getLine(), String.format(
119                         "Can not add '%s': grouping with same name already declared in module '%s' at line %d",
120                         grouping, getModuleName(), addedGrouping.getLine()));
121             }
122         }
123         addedGroupings.add(grouping);
124     }
125
126     @Override
127     public Set<TypeDefinition<?>> getTypeDefinitions() {
128         return typedefs;
129     }
130
131     public Set<UsesNode> getUsesNodes() {
132         return usesNodes;
133     }
134
135     @Override
136     public Set<UsesNodeBuilder> getUsesNodeBuilders() {
137         return addedUsesNodes;
138     }
139
140     @Override
141     public void addUsesNode(final UsesNodeBuilder usesNode) {
142         addedUsesNodes.add(usesNode);
143     }
144
145     protected static DataSchemaNode getChildNode(final Set<DataSchemaNode> childNodes, final QName name) {
146         for (DataSchemaNode node : childNodes) {
147             if (node.getQName().equals(name)) {
148                 return node;
149             }
150         }
151         return null;
152     }
153
154     protected static DataSchemaNode getChildNode(final Set<DataSchemaNode> childNodes, final String name) {
155         for (DataSchemaNode node : childNodes) {
156             if (node.getQName().getLocalName().equals(name)) {
157                 return node;
158             }
159         }
160         return null;
161     }
162
163 }