Merge from development repository.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / parser / builder / impl / ContainerSchemaNodeBuilder.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.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 import org.opendaylight.controller.yang.common.QName;
18 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
19 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
20 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
22 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
23 import org.opendaylight.controller.yang.model.api.SchemaPath;
24 import org.opendaylight.controller.yang.model.api.Status;
25 import org.opendaylight.controller.yang.model.api.TypeDefinition;
26 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
27 import org.opendaylight.controller.yang.model.api.UsesNode;
28 import org.opendaylight.controller.yang.model.parser.builder.api.AbstractChildNodeBuilder;
29 import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationTargetBuilder;
30 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
31 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
32 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionAwareBuilder;
33 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
34 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
35
36 public class ContainerSchemaNodeBuilder extends AbstractChildNodeBuilder
37         implements TypeDefinitionAwareBuilder, AugmentationTargetBuilder,
38         DataSchemaNodeBuilder {
39
40     private final ContainerSchemaNodeImpl instance;
41     private final ConstraintsBuilder constraintsBuilder;
42
43     private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
44     private final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
45     private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
46
47     ContainerSchemaNodeBuilder(QName qname) {
48         super(qname);
49         instance = new ContainerSchemaNodeImpl(qname);
50         constraintsBuilder = new ConstraintsBuilder();
51     }
52
53     @Override
54     public ContainerSchemaNode build() {
55         // CHILD NODES
56         Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
57         for (DataSchemaNodeBuilder node : childNodes) {
58             childs.put(node.getQName(), node.build());
59         }
60         instance.setChildNodes(childs);
61
62         // GROUPINGS
63         Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
64         for (GroupingBuilder builder : groupings) {
65             groupingDefinitions.add(builder.build());
66         }
67         instance.setGroupings(groupingDefinitions);
68
69         // TYPEDEFS
70         Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
71         for (TypeDefinitionBuilder entry : addedTypedefs) {
72             typedefs.add(entry.build());
73         }
74         instance.setTypeDefinitions(typedefs);
75
76         // USES
77         Set<UsesNode> uses = new HashSet<UsesNode>();
78         for (UsesNodeBuilder builder : addedUsesNodes) {
79             uses.add(builder.build());
80         }
81         instance.setUses(uses);
82
83         instance.setConstraints(constraintsBuilder.build());
84         instance.setAvailableAugmentations(augmentations);
85
86         return instance;
87     }
88
89     @Override
90     public void addTypedef(TypeDefinitionBuilder type) {
91         addedTypedefs.add(type);
92     }
93
94     @Override
95     public void addAugmentation(AugmentationSchema augment) {
96         augmentations.add(augment);
97     }
98
99     @Override
100     public void setPath(SchemaPath schemaPath) {
101         instance.setPath(schemaPath);
102     }
103
104     @Override
105     public void setDescription(String description) {
106         instance.setDescription(description);
107     }
108
109     @Override
110     public void setReference(String reference) {
111         instance.setReference(reference);
112     }
113
114     @Override
115     public void setStatus(Status status) {
116         instance.setStatus(status);
117     }
118
119     @Override
120     public void setAugmenting(boolean augmenting) {
121         instance.setAugmenting(augmenting);
122     }
123
124     @Override
125     public void setConfiguration(boolean configuration) {
126         instance.setConfiguration(configuration);
127     }
128
129     @Override
130     public ConstraintsBuilder getConstraintsBuilder() {
131         return constraintsBuilder;
132     }
133
134     @Override
135     public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
136         addedUsesNodes.add(usesNodeBuilder);
137     }
138
139     public void setPresenceContainer(boolean presence) {
140         instance.setPresenceContainer(presence);
141     }
142
143     private class ContainerSchemaNodeImpl implements ContainerSchemaNode {
144
145         private final QName qname;
146         private SchemaPath path;
147         private String description;
148         private String reference;
149         private Status status = Status.CURRENT;
150         private boolean augmenting;
151         private boolean configuration;
152         private ConstraintDefinition constraints;
153         private Set<AugmentationSchema> augmentations = Collections.emptySet();
154         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
155         private Set<GroupingDefinition> groupings = Collections.emptySet();
156         private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
157         private Set<UsesNode> uses = Collections.emptySet();
158         private boolean presence;
159
160         private ContainerSchemaNodeImpl(QName qname) {
161             this.qname = qname;
162         }
163
164         @Override
165         public QName getQName() {
166             return qname;
167         }
168
169         @Override
170         public SchemaPath getPath() {
171             return path;
172         }
173
174         private void setPath(SchemaPath path) {
175             this.path = path;
176         }
177
178         @Override
179         public String getDescription() {
180             return description;
181         }
182
183         private void setDescription(String description) {
184             this.description = description;
185         }
186
187         @Override
188         public String getReference() {
189             return reference;
190         }
191
192         private void setReference(String reference) {
193             this.reference = reference;
194         }
195
196         @Override
197         public Status getStatus() {
198             return status;
199         }
200
201         private void setStatus(Status status) {
202             if(status != null) {
203                 this.status = status;
204             }
205         }
206
207         @Override
208         public boolean isAugmenting() {
209             return augmenting;
210         }
211
212         private void setAugmenting(boolean augmenting) {
213             this.augmenting = augmenting;
214         }
215
216         @Override
217         public boolean isConfiguration() {
218             return configuration;
219         }
220
221         private void setConfiguration(boolean configuration) {
222             this.configuration = configuration;
223         }
224
225         @Override
226         public ConstraintDefinition getConstraints() {
227             return constraints;
228         }
229
230         private void setConstraints(ConstraintDefinition constraints) {
231             this.constraints = constraints;
232         }
233
234         @Override
235         public Set<AugmentationSchema> getAvailableAugmentations() {
236             return augmentations;
237         }
238
239         private void setAvailableAugmentations(
240                 Set<AugmentationSchema> augmentations) {
241             if (augmentations != null) {
242                 this.augmentations = augmentations;
243             }
244         }
245
246         @Override
247         public Set<DataSchemaNode> getChildNodes() {
248             return new HashSet<DataSchemaNode>(childNodes.values());
249         }
250
251         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
252             if (childNodes != null) {
253                 this.childNodes = childNodes;
254             }
255         }
256
257         @Override
258         public Set<GroupingDefinition> getGroupings() {
259             return groupings;
260         }
261
262         private void setGroupings(Set<GroupingDefinition> groupings) {
263             if (groupings != null) {
264                 this.groupings = groupings;
265             }
266         }
267
268         @Override
269         public DataSchemaNode getDataChildByName(QName name) {
270             return childNodes.get(name);
271         }
272
273         @Override
274         public DataSchemaNode getDataChildByName(String name) {
275             DataSchemaNode result = null;
276             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
277                 if (entry.getKey().getLocalName().equals(name)) {
278                     result = entry.getValue();
279                     break;
280                 }
281             }
282             return result;
283         }
284
285         @Override
286         public Set<UsesNode> getUses() {
287             return uses;
288         }
289
290         private void setUses(Set<UsesNode> uses) {
291             if (uses != null) {
292                 this.uses = uses;
293             }
294         }
295
296         @Override
297         public boolean isPresenceContainer() {
298             return presence;
299         }
300
301         private void setPresenceContainer(boolean presence) {
302             this.presence = presence;
303         }
304
305         @Override
306         public Set<TypeDefinition<?>> getTypeDefinitions() {
307             return typeDefinitions;
308         }
309
310         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
311             if (typeDefinitions != null) {
312                 this.typeDefinitions = typeDefinitions;
313             }
314         }
315
316         @Override
317         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
318             return Collections.emptyList();
319         }
320
321         @Override
322         public int hashCode() {
323             final int prime = 31;
324             int result = 1;
325             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
326             return result;
327         }
328
329         @Override
330         public boolean equals(Object obj) {
331             if (this == obj) {
332                 return true;
333             }
334             if (obj == null) {
335                 return false;
336             }
337             if (getClass() != obj.getClass()) {
338                 return false;
339             }
340             ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) obj;
341             if (qname == null) {
342                 if (other.qname != null) {
343                     return false;
344                 }
345             } else if (!qname.equals(other.qname)) {
346                 return false;
347             }
348             return true;
349         }
350
351         @Override
352         public String toString() {
353             StringBuilder sb = new StringBuilder(
354                     ContainerSchemaNodeImpl.class.getSimpleName());
355             sb.append("[");
356             sb.append("qname=" + qname);
357             sb.append("]");
358             return sb.toString();
359         }
360     }
361
362 }