Merge "Added missing parent tag to pom files"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ListSchemaNodeBuilder.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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Set;
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.ConstraintDefinition;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
25 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
32 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
33 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
34
35 public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements
36         DataSchemaNodeBuilder, AugmentationTargetBuilder {
37     private ListSchemaNodeImpl instance;
38     private boolean userOrdered;
39     private List<String> keys;
40     private List<QName> keyDefinition;
41     // SchemaNode args
42     private SchemaPath schemaPath;
43     // DataSchemaNode args
44     private boolean augmenting;
45     private boolean addedByUses;
46     private ListSchemaNodeBuilder originalBuilder;
47     private ListSchemaNode originalNode;
48     private boolean configuration;
49     private final ConstraintsBuilder constraints;
50     // AugmentationTarget args
51     private final List<AugmentationSchema> augmentations = new ArrayList<>();
52     private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
53
54     public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
55         super(moduleName, line, qname);
56         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
57         constraints = new ConstraintsBuilderImpl(moduleName, line);
58     }
59
60     public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
61             final ListSchemaNode base) {
62         super(moduleName, line, qname, path, base);
63         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
64         constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
65
66         keyDefinition = ImmutableList.copyOf(base.getKeyDefinition());
67         userOrdered = base.isUserOrdered();
68
69         augmenting = base.isAugmenting();
70         addedByUses = base.isAddedByUses();
71         originalNode = base;
72         configuration = base.isConfiguration();
73
74         addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
75                 qname));
76         augmentations.addAll(base.getAvailableAugmentations());
77     }
78
79     @Override
80     public ListSchemaNode build() {
81         if (instance != null) {
82             return instance;
83         }
84         buildChildren();
85         instance = new ListSchemaNodeImpl(qname, schemaPath, this);
86
87         instance.augmenting = augmenting;
88         instance.addedByUses = addedByUses;
89         instance.configuration = configuration;
90         instance.constraints = constraints.toInstance();
91         instance.userOrdered = userOrdered;
92
93         // KEY
94         if (keys != null) {
95             keyDefinition = new ArrayList<>();
96             for (String key : keys) {
97                 DataSchemaNode keyPart = instance.getDataChildByName(key);
98                 if (keyPart == null) {
99                     throw new YangParseException(getModuleName(), getLine(), "Failed to resolve list key for name "
100                             + key);
101                 }
102
103                 final QName qname = keyPart.getQName();
104                 if (!keyDefinition.contains(qname)) {
105                     keyDefinition.add(qname);
106                 }
107             }
108             instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
109         } else {
110             instance.keyDefinition = ImmutableList.of();
111         }
112
113         // ORIGINAL NODE
114         if (originalNode == null && originalBuilder != null) {
115             originalNode = originalBuilder.build();
116         }
117         instance.original = originalNode;
118
119         // AUGMENTATIONS
120         for (AugmentationSchemaBuilder builder : augmentationBuilders) {
121             augmentations.add(builder.build());
122         }
123         instance.augmentations = ImmutableSet.copyOf(augmentations);
124
125         // UNKNOWN NODES
126         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
127             unknownNodes.add(b.build());
128         }
129         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
130
131         return instance;
132     }
133
134     @Override
135     protected String getStatementName() {
136         return "list";
137     }
138
139     @Override
140     public SchemaPath getPath() {
141         return schemaPath;
142     }
143
144     @Override
145     public void setPath(final SchemaPath path) {
146         this.schemaPath = path;
147     }
148
149     @Override
150     public void addAugmentation(final AugmentationSchemaBuilder augment) {
151         augmentationBuilders.add(augment);
152     }
153
154     public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
155         return augmentationBuilders;
156     }
157
158     public List<String> getKeys() {
159         return keys;
160     }
161
162     public void setKeys(final List<String> keys) {
163         this.keys = keys;
164     }
165
166     @Override
167     public boolean isAugmenting() {
168         return augmenting;
169     }
170
171     @Override
172     public void setAugmenting(final boolean augmenting) {
173         this.augmenting = augmenting;
174     }
175
176     @Override
177     public boolean isAddedByUses() {
178         return addedByUses;
179     }
180
181     @Override
182     public void setAddedByUses(final boolean addedByUses) {
183         this.addedByUses = addedByUses;
184     }
185
186     @Override
187     public ListSchemaNodeBuilder getOriginal() {
188         return originalBuilder;
189     }
190
191     @Override
192     public void setOriginal(final SchemaNodeBuilder builder) {
193         Preconditions.checkArgument(builder instanceof ListSchemaNodeBuilder, "Original of list cannot be " + builder);
194         this.originalBuilder = (ListSchemaNodeBuilder) builder;
195     }
196
197     @Override
198     public boolean isConfiguration() {
199         return configuration;
200     }
201
202     @Override
203     public void setConfiguration(final boolean configuration) {
204         this.configuration = configuration;
205     }
206
207     @Override
208     public ConstraintsBuilder getConstraints() {
209         return constraints;
210     }
211
212     public boolean isUserOrdered() {
213         return userOrdered;
214     }
215
216     public void setUserOrdered(final boolean userOrdered) {
217         this.userOrdered = userOrdered;
218     }
219
220     @Override
221     public int hashCode() {
222         final int prime = 31;
223         int result = 1;
224         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
225         return result;
226     }
227
228     @Override
229     public boolean equals(final Object obj) {
230         if (this == obj) {
231             return true;
232         }
233         if (obj == null) {
234             return false;
235         }
236         if (getClass() != obj.getClass()) {
237             return false;
238         }
239         ListSchemaNodeBuilder other = (ListSchemaNodeBuilder) obj;
240         if (schemaPath == null) {
241             if (other.schemaPath != null) {
242                 return false;
243             }
244         } else if (!schemaPath.equals(other.schemaPath)) {
245             return false;
246         }
247         if (getParent() == null) {
248             if (other.getParent() != null) {
249                 return false;
250             }
251         } else if (!getParent().equals(other.getParent())) {
252             return false;
253         }
254         return true;
255     }
256
257     @Override
258     public String toString() {
259         return "list " + qname.getLocalName();
260     }
261
262     private static final class ListSchemaNodeImpl extends AbstractDocumentedDataNodeContainer implements
263             ListSchemaNode, DerivableSchemaNode {
264         private final QName qname;
265         private final SchemaPath path;
266         private ImmutableList<QName> keyDefinition;
267         private boolean augmenting;
268         private boolean addedByUses;
269         private ListSchemaNode original;
270         private boolean configuration;
271         private ConstraintDefinition constraints;
272         private ImmutableSet<AugmentationSchema> augmentations;
273         private ImmutableList<UnknownSchemaNode> unknownNodes;
274         private boolean userOrdered;
275
276         private ListSchemaNodeImpl(final QName qname, final SchemaPath path, final ListSchemaNodeBuilder builder) {
277             super(builder);
278             this.qname = qname;
279             this.path = path;
280         }
281
282         @Override
283         public QName getQName() {
284             return qname;
285         }
286
287         @Override
288         public SchemaPath getPath() {
289             return path;
290         }
291
292         @Override
293         public List<QName> getKeyDefinition() {
294             return keyDefinition;
295         }
296
297         @Override
298         public boolean isAugmenting() {
299             return augmenting;
300         }
301
302         @Override
303         public boolean isAddedByUses() {
304             return addedByUses;
305         }
306
307         @Override
308         public Optional<ListSchemaNode> getOriginal() {
309             return Optional.fromNullable(original);
310         }
311
312         @Override
313         public boolean isConfiguration() {
314             return configuration;
315         }
316
317         @Override
318         public ConstraintDefinition getConstraints() {
319             return constraints;
320         }
321
322         @Override
323         public Set<AugmentationSchema> getAvailableAugmentations() {
324             return augmentations;
325         }
326
327         @Override
328         public boolean isUserOrdered() {
329             return userOrdered;
330         }
331
332         @Override
333         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
334             return unknownNodes;
335         }
336
337         @Override
338         public int hashCode() {
339             final int prime = 31;
340             int result = 1;
341             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
342             result = prime * result + ((path == null) ? 0 : path.hashCode());
343             return result;
344         }
345
346         @Override
347         public boolean equals(final Object obj) {
348             if (this == obj) {
349                 return true;
350             }
351             if (obj == null) {
352                 return false;
353             }
354             if (getClass() != obj.getClass()) {
355                 return false;
356             }
357             final ListSchemaNodeImpl other = (ListSchemaNodeImpl) obj;
358             if (qname == null) {
359                 if (other.qname != null) {
360                     return false;
361                 }
362             } else if (!qname.equals(other.qname)) {
363                 return false;
364             }
365             if (path == null) {
366                 if (other.path != null) {
367                     return false;
368                 }
369             } else if (!path.equals(other.path)) {
370                 return false;
371             }
372             return true;
373         }
374
375         @Override
376         public String toString() {
377             return "list " + qname.getLocalName();
378         }
379     }
380
381 }