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