Merge "Bug 1366: Fallback scenario for IdentityCodec to work without schema."
[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 java.util.ArrayList;
11 import java.util.List;
12 import java.util.Set;
13
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
16 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
17 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
28 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
29
30 import com.google.common.base.Optional;
31 import com.google.common.base.Preconditions;
32 import com.google.common.collect.ImmutableList;
33 import com.google.common.collect.ImmutableSet;
34
35 public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements DataSchemaNodeBuilder,
36 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             instance.keyDefinition = ImmutableList.of();
96         } else {
97             keyDefinition = new ArrayList<>();
98             for (String key : keys) {
99                 keyDefinition.add(instance.getDataChildByName(key).getQName());
100             }
101             instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
102         }
103
104         // ORIGINAL NODE
105         if (originalNode == null && originalBuilder != null) {
106             originalNode = originalBuilder.build();
107         }
108         instance.original = originalNode;
109
110         // AUGMENTATIONS
111         for (AugmentationSchemaBuilder builder : augmentationBuilders) {
112             augmentations.add(builder.build());
113         }
114         instance.augmentations = ImmutableSet.copyOf(augmentations);
115
116         // UNKNOWN NODES
117         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
118             unknownNodes.add(b.build());
119         }
120         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
121
122         return instance;
123     }
124
125     @Override
126     protected String getStatementName() {
127         return "list";
128     }
129
130     @Override
131     public SchemaPath getPath() {
132         return schemaPath;
133     }
134
135     @Override
136     public void setPath(final SchemaPath path) {
137         this.schemaPath = path;
138     }
139
140     @Override
141     public void addAugmentation(final AugmentationSchemaBuilder augment) {
142         augmentationBuilders.add(augment);
143     }
144
145     public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
146         return augmentationBuilders;
147     }
148
149     public List<String> getKeys() {
150         return keys;
151     }
152
153     public void setKeys(final List<String> keys) {
154         this.keys = keys;
155     }
156
157     @Override
158     public boolean isAugmenting() {
159         return augmenting;
160     }
161
162     @Override
163     public void setAugmenting(final boolean augmenting) {
164         this.augmenting = augmenting;
165     }
166
167     @Override
168     public boolean isAddedByUses() {
169         return addedByUses;
170     }
171
172     @Override
173     public void setAddedByUses(final boolean addedByUses) {
174         this.addedByUses = addedByUses;
175     }
176
177     @Override
178     public ListSchemaNodeBuilder getOriginal() {
179         return originalBuilder;
180     }
181
182     @Override
183     public void setOriginal(final SchemaNodeBuilder builder) {
184         Preconditions.checkArgument(builder instanceof ListSchemaNodeBuilder, "Original of list cannot be " + builder);
185         this.originalBuilder = (ListSchemaNodeBuilder) builder;
186     }
187
188     @Override
189     public boolean isConfiguration() {
190         return configuration;
191     }
192
193     @Override
194     public void setConfiguration(final boolean configuration) {
195         this.configuration = configuration;
196     }
197
198     @Override
199     public ConstraintsBuilder getConstraints() {
200         return constraints;
201     }
202
203     public boolean isUserOrdered() {
204         return userOrdered;
205     }
206
207     public void setUserOrdered(final boolean userOrdered) {
208         this.userOrdered = userOrdered;
209     }
210
211     @Override
212     public int hashCode() {
213         final int prime = 31;
214         int result = 1;
215         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
216         return result;
217     }
218
219     @Override
220     public boolean equals(final Object obj) {
221         if (this == obj) {
222             return true;
223         }
224         if (obj == null) {
225             return false;
226         }
227         if (getClass() != obj.getClass()) {
228             return false;
229         }
230         ListSchemaNodeBuilder other = (ListSchemaNodeBuilder) obj;
231         if (schemaPath == null) {
232             if (other.schemaPath != null) {
233                 return false;
234             }
235         } else if (!schemaPath.equals(other.schemaPath)) {
236             return false;
237         }
238         if (getParent() == null) {
239             if (other.getParent() != null) {
240                 return false;
241             }
242         } else if (!getParent().equals(other.getParent())) {
243             return false;
244         }
245         return true;
246     }
247
248     @Override
249     public String toString() {
250         return "list " + qname.getLocalName();
251     }
252
253     private static final class ListSchemaNodeImpl extends AbstractDocumentedDataNodeContainer implements ListSchemaNode, DerivableSchemaNode {
254         private final QName qname;
255         private final SchemaPath path;
256         private ImmutableList<QName> keyDefinition;
257         private boolean augmenting;
258         private boolean addedByUses;
259         private ListSchemaNode original;
260         private boolean configuration;
261         private ConstraintDefinition constraints;
262         private ImmutableSet<AugmentationSchema> augmentations;
263         private ImmutableList<UnknownSchemaNode> unknownNodes;
264         private boolean userOrdered;
265
266         private ListSchemaNodeImpl(final QName qname, final SchemaPath path, final ListSchemaNodeBuilder builder) {
267             super(builder);
268             this.qname = qname;
269             this.path = path;
270         }
271
272         @Override
273         public QName getQName() {
274             return qname;
275         }
276
277         @Override
278         public SchemaPath getPath() {
279             return path;
280         }
281
282         @Override
283         public List<QName> getKeyDefinition() {
284             return keyDefinition;
285         }
286
287         @Override
288         public boolean isAugmenting() {
289             return augmenting;
290         }
291
292         @Override
293         public boolean isAddedByUses() {
294             return addedByUses;
295         }
296
297         @Override
298         public Optional<ListSchemaNode> getOriginal() {
299             return Optional.fromNullable(original);
300         }
301
302         @Override
303         public boolean isConfiguration() {
304             return configuration;
305         }
306
307         @Override
308         public ConstraintDefinition getConstraints() {
309             return constraints;
310         }
311
312         @Override
313         public Set<AugmentationSchema> getAvailableAugmentations() {
314             return augmentations;
315         }
316
317         @Override
318         public boolean isUserOrdered() {
319             return userOrdered;
320         }
321
322         @Override
323         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
324             return unknownNodes;
325         }
326
327         @Override
328         public int hashCode() {
329             final int prime = 31;
330             int result = 1;
331             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
332             result = prime * result + ((path == null) ? 0 : path.hashCode());
333             return result;
334         }
335
336         @Override
337         public boolean equals(final Object obj) {
338             if (this == obj) {
339                 return true;
340             }
341             if (obj == null) {
342                 return false;
343             }
344             if (getClass() != obj.getClass()) {
345                 return false;
346             }
347             final ListSchemaNodeImpl other = (ListSchemaNodeImpl) obj;
348             if (qname == null) {
349                 if (other.qname != null) {
350                     return false;
351                 }
352             } else if (!qname.equals(other.qname)) {
353                 return false;
354             }
355             if (path == null) {
356                 if (other.path != null) {
357                     return false;
358                 }
359             } else if (!path.equals(other.path)) {
360                 return false;
361             }
362             return true;
363         }
364
365         @Override
366         public String toString() {
367             return "list " + qname.getLocalName();
368         }
369     }
370
371 }