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