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