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