Implemented refine statement parsing.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / parser / builder / impl / AugmentationSchemaBuilderImpl.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.model.parser.builder.impl;
9
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.Map;
14 import java.util.Set;
15
16 import org.opendaylight.controller.yang.common.QName;
17 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
18 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
19 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
20 import org.opendaylight.controller.yang.model.api.SchemaPath;
21 import org.opendaylight.controller.yang.model.api.Status;
22 import org.opendaylight.controller.yang.model.api.TypeDefinition;
23 import org.opendaylight.controller.yang.model.api.UsesNode;
24 import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;
25 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
26 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
27 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
28 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
29 import org.opendaylight.controller.yang.model.parser.util.YangModelBuilderUtil;
30
31 public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder {
32
33     private final AugmentationSchemaImpl instance;
34     private final String augmentTargetStr;
35     private final SchemaPath augmentTarget;
36     private final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
37     private final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
38     private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
39
40     AugmentationSchemaBuilderImpl(final String augmentTargetStr) {
41         this.augmentTargetStr = augmentTargetStr;
42         final SchemaPath targetPath = YangModelBuilderUtil.parseAugmentPath(augmentTargetStr);
43         augmentTarget = targetPath;
44         instance = new AugmentationSchemaImpl(targetPath);
45     }
46
47
48     @Override
49     public void addChildNode(DataSchemaNodeBuilder childNode) {
50         childNodes.add(childNode);
51     }
52
53     @Override
54     public Set<DataSchemaNodeBuilder> getChildNodes() {
55         return childNodes;
56     }
57
58     @Override
59     public void addGrouping(GroupingBuilder grouping) {
60         groupings.add(grouping);
61     }
62
63     @Override
64     public void addUsesNode(UsesNodeBuilder usesBuilder) {
65         usesNodes.add(usesBuilder);
66     }
67
68     /**
69      * Always returns null.
70      */
71     @Override
72     public QName getQName() {
73         return null;
74     }
75
76     @Override
77     public AugmentationSchema build() {
78         // CHILD NODES
79         final Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
80         for (DataSchemaNodeBuilder node : childNodes) {
81             childs.put(node.getQName(), node.build());
82         }
83         instance.setChildNodes(childs);
84
85         // GROUPINGS
86         final Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
87         for (GroupingBuilder builder : groupings) {
88             groupingDefinitions.add(builder.build());
89         }
90         instance.setGroupings(groupingDefinitions);
91
92         // USES
93         final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
94         for (UsesNodeBuilder builder : usesNodes) {
95             usesNodeDefinitions.add(builder.build());
96         }
97         instance.setUses(usesNodeDefinitions);
98
99         return instance;
100     }
101
102     @Override
103     public void addTypedef(TypeDefinitionBuilder type) {
104         throw new UnsupportedOperationException(
105                 "Augmentation can not contains type definitions");
106     }
107
108     @Override
109     public void setDescription(String description) {
110         instance.setDescription(description);
111     }
112
113     @Override
114     public void setReference(String reference) {
115         instance.setReference(reference);
116     }
117
118     @Override
119     public void setStatus(Status status) {
120         instance.setStatus(status);
121     }
122
123     @Override
124     public SchemaPath getTargetPath() {
125         return augmentTarget;
126     }
127
128     @Override
129     public String getTargetPathAsString() {
130         return augmentTargetStr;
131     }
132
133     private static class AugmentationSchemaImpl implements AugmentationSchema {
134         private final SchemaPath targetPath;
135         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
136         private Set<GroupingDefinition> groupings = Collections.emptySet();
137         private Set<UsesNode> uses = Collections.emptySet();
138
139         private String description;
140         private String reference;
141         private Status status;
142
143         private AugmentationSchemaImpl(SchemaPath targetPath) {
144             this.targetPath = targetPath;
145         }
146
147         @Override
148         public SchemaPath getTargetPath() {
149             return targetPath;
150         }
151
152         @Override
153         public Set<DataSchemaNode> getChildNodes() {
154             return new HashSet<DataSchemaNode>(childNodes.values());
155         }
156
157         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
158             if(childNodes != null) {
159                 this.childNodes = childNodes;
160             }
161         }
162
163         @Override
164         public Set<GroupingDefinition> getGroupings() {
165             return groupings;
166         }
167
168         private void setGroupings(Set<GroupingDefinition> groupings) {
169             if(groupings != null) {
170                 this.groupings = groupings;
171             }
172         }
173
174         @Override
175         public Set<UsesNode> getUses() {
176             return uses;
177         }
178
179         private void setUses(Set<UsesNode> uses) {
180             if(uses != null) {
181                 this.uses = uses;
182             }
183         }
184
185         /**
186          * Always returns an empty set, because augmentation can not contains
187          * type definitions.
188          */
189         @Override
190         public Set<TypeDefinition<?>> getTypeDefinitions() {
191             return Collections.emptySet();
192         }
193
194         @Override
195         public String getDescription() {
196             return description;
197         }
198
199         private void setDescription(String description) {
200             this.description = description;
201         }
202
203         @Override
204         public String getReference() {
205             return reference;
206         }
207
208         private void setReference(String reference) {
209             this.reference = reference;
210         }
211
212         @Override
213         public Status getStatus() {
214             return status;
215         }
216
217         private void setStatus(Status status) {
218             this.status = status;
219         }
220
221         @Override
222         public DataSchemaNode getDataChildByName(QName name) {
223             return childNodes.get(name);
224         }
225
226         @Override
227         public DataSchemaNode getDataChildByName(String name) {
228             DataSchemaNode result = null;
229             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
230                 if (entry.getKey().getLocalName().equals(name)) {
231                     result = entry.getValue();
232                     break;
233                 }
234             }
235             return result;
236         }
237
238         @Override
239         public int hashCode() {
240             final int prime = 17;
241             int result = 1;
242             result = prime * result
243                     + ((targetPath == null) ? 0 : targetPath.hashCode());
244             result = prime * result
245                     + ((childNodes == null) ? 0 : childNodes.hashCode());
246             result = prime * result
247                     + ((groupings == null) ? 0 : groupings.hashCode());
248             result = prime * result + ((uses == null) ? 0 : uses.hashCode());
249             return result;
250         }
251
252         @Override
253         public boolean equals(Object obj) {
254             if (this == obj) {
255                 return true;
256             }
257             if (obj == null) {
258                 return false;
259             }
260             if (getClass() != obj.getClass()) {
261                 return false;
262             }
263             AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
264             if (targetPath == null) {
265                 if (other.targetPath != null) {
266                     return false;
267                 }
268             } else if (!targetPath.equals(other.targetPath)) {
269                 return false;
270             }
271             if (childNodes == null) {
272                 if (other.childNodes != null) {
273                     return false;
274                 }
275             } else if (!childNodes.equals(other.childNodes)) {
276                 return false;
277             }
278             if (groupings == null) {
279                 if (other.groupings != null) {
280                     return false;
281                 }
282             } else if (!groupings.equals(other.groupings)) {
283                 return false;
284             }
285             if (uses == null) {
286                 if (other.uses != null) {
287                     return false;
288                 }
289             } else if (!uses.equals(other.uses)) {
290                 return false;
291             }
292             return true;
293         }
294
295         @Override
296         public String toString() {
297             StringBuilder sb = new StringBuilder(
298                     AugmentationSchemaImpl.class.getSimpleName());
299             sb.append("[");
300             sb.append("targetPath=" + targetPath);
301             sb.append(", childNodes=" + childNodes.values());
302             sb.append(", groupings=" + groupings);
303             sb.append(", uses=" + uses);
304             sb.append("]");
305             return sb.toString();
306         }
307     }
308
309 }