Merge "Introduce RESTCONF Client API"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / 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.yangtools.yang.parser.builder.impl;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.TreeSet;
18
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.Status;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.UsesNode;
29 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
30 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
36 import org.opendaylight.yangtools.yang.parser.util.Comparators;
37 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
38 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
39
40 public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContainerBuilder implements
41         AugmentationSchemaBuilder {
42     private boolean built;
43     private final AugmentationSchemaImpl instance;
44
45     private String whenCondition;
46
47     private final String augmentTargetStr;
48     private SchemaPath targetPath;
49     private SchemaPath targetNodeSchemaPath;
50
51     private boolean resolved;
52
53     public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr) {
54         super(moduleName, line, null);
55         this.augmentTargetStr = augmentTargetStr;
56         targetPath = ParserUtils.parseXPathString(augmentTargetStr);
57         instance = new AugmentationSchemaImpl(targetPath);
58     }
59
60     @Override
61     public Set<GroupingDefinition> getGroupings() {
62         return Collections.emptySet();
63     }
64
65     @Override
66     public Set<GroupingBuilder> getGroupingBuilders() {
67         return Collections.emptySet();
68     }
69
70     @Override
71     public void addGrouping(GroupingBuilder grouping) {
72         throw new YangParseException(moduleName, line, "augment can not contains grouping statement");
73     }
74
75     @Override
76     public SchemaPath getPath() {
77         return targetNodeSchemaPath;
78     }
79
80     @Override
81     public AugmentationSchema build() {
82         if (!built) {
83             instance.setTargetPath(targetNodeSchemaPath);
84
85             RevisionAwareXPath whenStmt;
86             if (whenCondition == null) {
87                 whenStmt = null;
88             } else {
89                 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
90             }
91             instance.setWhenCondition(whenStmt);
92
93             // CHILD NODES
94             for (DataSchemaNodeBuilder node : addedChildNodes) {
95                 DataSchemaNode child = node.build();
96                 childNodes.put(child.getQName(), child);
97             }
98             instance.addChildNodes(childNodes);
99
100             // USES
101             for (UsesNodeBuilder builder : addedUsesNodes) {
102                 usesNodes.add(builder.build());
103             }
104             instance.addUses(usesNodes);
105
106             // UNKNOWN NODES
107             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
108                 unknownNodes.add(b.build());
109             }
110             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
111             instance.addUnknownSchemaNodes(unknownNodes);
112
113             built = true;
114         }
115         return instance;
116     }
117
118     @Override
119     public boolean isResolved() {
120         return resolved;
121     }
122
123     @Override
124     public void setResolved(boolean resolved) {
125         this.resolved = resolved;
126     }
127
128     public String getWhenCondition() {
129         return whenCondition;
130     }
131
132     public void addWhenCondition(String whenCondition) {
133         this.whenCondition = whenCondition;
134     }
135
136     @Override
137     public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
138         return Collections.emptySet();
139     }
140
141     @Override
142     public void addTypedef(TypeDefinitionBuilder type) {
143         throw new YangParseException(moduleName, line, "Augmentation can not contains typedef statement.");
144     }
145
146     @Override
147     public String getDescription() {
148         return instance.description;
149     }
150
151     @Override
152     public void setDescription(final String description) {
153         instance.description = description;
154     }
155
156     @Override
157     public String getReference() {
158         return instance.reference;
159     }
160
161     @Override
162     public void setReference(final String reference) {
163         instance.reference = reference;
164     }
165
166     @Override
167     public Status getStatus() {
168         return instance.status;
169     }
170
171     @Override
172     public void setStatus(Status status) {
173         if (status != null) {
174             instance.status = status;
175         }
176     }
177
178     @Override
179     public String getTargetPathAsString() {
180         return augmentTargetStr;
181     }
182
183     @Override
184     public SchemaPath getTargetPath() {
185         return targetPath;
186     }
187
188     @Override
189     public SchemaPath getTargetNodeSchemaPath() {
190         return targetNodeSchemaPath;
191     }
192
193     @Override
194     public void setTargetNodeSchemaPath(SchemaPath path) {
195         this.targetNodeSchemaPath = path;
196     }
197
198     @Override
199     public int hashCode() {
200         final int prime = 17;
201         int result = 1;
202         result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
203         result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
204         result = prime * result + ((addedChildNodes == null) ? 0 : addedChildNodes.hashCode());
205         return result;
206     }
207
208     @Override
209     public boolean equals(Object obj) {
210         if (this == obj) {
211             return true;
212         }
213         if (obj == null) {
214             return false;
215         }
216         if (getClass() != obj.getClass()) {
217             return false;
218         }
219         AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
220         if (augmentTargetStr == null) {
221             if (other.augmentTargetStr != null) {
222                 return false;
223             }
224         } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
225             return false;
226         }
227         if (whenCondition == null) {
228             if (other.whenCondition != null) {
229                 return false;
230             }
231         } else if (!whenCondition.equals(other.whenCondition)) {
232             return false;
233         }
234         if (addedChildNodes == null) {
235             if (other.addedChildNodes != null) {
236                 return false;
237             }
238         } else if (!addedChildNodes.equals(other.addedChildNodes)) {
239             return false;
240         }
241         return true;
242     }
243
244     public String toString() {
245         return "augment " + augmentTargetStr;
246     }
247
248     private final class AugmentationSchemaImpl implements AugmentationSchema {
249         private SchemaPath targetPath;
250         private RevisionAwareXPath whenCondition;
251         private final Map<QName, DataSchemaNode> childNodes = new HashMap<>();
252         private final Set<UsesNode> uses = new HashSet<>();
253         private String description;
254         private String reference;
255         private Status status;
256         private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
257
258         private AugmentationSchemaImpl(SchemaPath targetPath) {
259             this.targetPath = targetPath;
260         }
261
262         @Override
263         public SchemaPath getTargetPath() {
264             return targetPath;
265         }
266
267         private void setTargetPath(SchemaPath path) {
268             this.targetPath = path;
269         }
270
271         @Override
272         public RevisionAwareXPath getWhenCondition() {
273             return whenCondition;
274         }
275
276         private void setWhenCondition(RevisionAwareXPath whenCondition) {
277             this.whenCondition = whenCondition;
278         }
279
280         @Override
281         public Set<DataSchemaNode> getChildNodes() {
282             final Set<DataSchemaNode> result = new TreeSet<DataSchemaNode>(Comparators.SCHEMA_NODE_COMP);
283             result.addAll(childNodes.values());
284             return Collections.unmodifiableSet(result);
285         }
286
287         private void addChildNodes(Map<QName, DataSchemaNode> childNodes) {
288             if (childNodes != null) {
289                 this.childNodes.putAll(childNodes);
290             }
291         }
292
293         /**
294          * Always returns an empty set, because augment can not contains
295          * grouping statement.
296          */
297         @Override
298         public Set<GroupingDefinition> getGroupings() {
299             return Collections.emptySet();
300         }
301
302         @Override
303         public Set<UsesNode> getUses() {
304             return Collections.unmodifiableSet(uses);
305         }
306
307         private void addUses(Set<UsesNode> uses) {
308             if (uses != null) {
309                 this.uses.addAll(uses);
310             }
311         }
312
313         /**
314          * Always returns an empty set, because augment can not contains type
315          * definitions.
316          */
317         @Override
318         public Set<TypeDefinition<?>> getTypeDefinitions() {
319             return Collections.emptySet();
320         }
321
322         @Override
323         public String getDescription() {
324             return description;
325         }
326
327         @Override
328         public String getReference() {
329             return reference;
330         }
331
332         @Override
333         public Status getStatus() {
334             return status;
335         }
336
337         @Override
338         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
339             return Collections.unmodifiableList(unknownNodes);
340         }
341
342         private void addUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
343             if (unknownSchemaNodes != null) {
344                 this.unknownNodes.addAll(unknownSchemaNodes);
345             }
346         }
347
348         @Override
349         public DataSchemaNode getDataChildByName(QName name) {
350             return childNodes.get(name);
351         }
352
353         @Override
354         public DataSchemaNode getDataChildByName(String name) {
355             DataSchemaNode result = null;
356             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
357                 if (entry.getKey().getLocalName().equals(name)) {
358                     result = entry.getValue();
359                     break;
360                 }
361             }
362             return result;
363         }
364
365         @Override
366         public int hashCode() {
367             final int prime = 17;
368             int result = 1;
369             result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
370             result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
371             result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
372             return result;
373         }
374
375         @Override
376         public boolean equals(Object obj) {
377             if (this == obj) {
378                 return true;
379             }
380             if (obj == null) {
381                 return false;
382             }
383             if (getClass() != obj.getClass()) {
384                 return false;
385             }
386             AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
387             if (targetPath == null) {
388                 if (other.targetPath != null) {
389                     return false;
390                 }
391             } else if (!targetPath.equals(other.targetPath)) {
392                 return false;
393             }
394             if (whenCondition == null) {
395                 if (other.whenCondition != null) {
396                     return false;
397                 }
398             } else if (!whenCondition.equals(other.whenCondition)) {
399                 return false;
400             }
401             if (childNodes == null) {
402                 if (other.childNodes != null) {
403                     return false;
404                 }
405             } else if (!childNodes.equals(other.childNodes)) {
406                 return false;
407             }
408             return true;
409         }
410
411         @Override
412         public String toString() {
413             StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
414             sb.append("[");
415             sb.append("targetPath=" + targetPath);
416             sb.append(", when=" + whenCondition);
417             sb.append("]");
418             return sb.toString();
419         }
420     }
421
422 }