Fixed a bug to block the creation of a static host on an ISL port, removed the code...
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / AnyXmlBuilder.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.AnyXmlSchemaNode;
16 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
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.UnknownSchemaNode;
20 import org.opendaylight.controller.yang.parser.builder.api.AbstractSchemaNodeBuilder;
21 import org.opendaylight.controller.yang.parser.builder.api.ConfigNode;
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 AnyXmlBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder, GroupingMember,
27         ConfigNode {
28     private boolean built;
29     private final AnyXmlSchemaNodeImpl instance;
30     private final ConstraintsBuilder constraints;
31
32     private Boolean configuration;
33     private boolean augmenting;
34     private boolean addedByUses;
35
36     public AnyXmlBuilder(final int line, final QName qname, final SchemaPath schemaPath) {
37         super(line, qname);
38         this.schemaPath = schemaPath;
39         instance = new AnyXmlSchemaNodeImpl(qname);
40         constraints = new ConstraintsBuilder(line);
41     }
42
43     public AnyXmlBuilder(final AnyXmlBuilder builder) {
44         super(builder.getLine(), builder.getQName());
45         parent = builder.getParent();
46         instance = new AnyXmlSchemaNodeImpl(qname);
47         constraints = builder.getConstraints();
48         schemaPath = builder.getPath();
49         unknownNodes = builder.unknownNodes;
50         addedUnknownNodes.addAll(builder.getUnknownNodes());
51         description = builder.getDescription();
52         reference = builder.getReference();
53         status = builder.getStatus();
54         configuration = builder.isConfiguration();
55         augmenting = builder.isAugmenting();
56         addedByUses = builder.isAddedByUses();
57     }
58
59     @Override
60     public AnyXmlSchemaNode build() {
61         if (!built) {
62             instance.setPath(schemaPath);
63             instance.setConstraints(constraints.build());
64             instance.setDescription(description);
65             instance.setReference(reference);
66             instance.setStatus(status);
67             instance.setConfiguration(configuration);
68             instance.setAugmenting(augmenting);
69             instance.setAddedByUses(addedByUses);
70
71             // UNKNOWN NODES
72             if (unknownNodes == null) {
73                 unknownNodes = new ArrayList<UnknownSchemaNode>();
74                 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
75                     unknownNodes.add(b.build());
76                 }
77             }
78             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
79             instance.setUnknownSchemaNodes(unknownNodes);
80
81             built = true;
82         }
83         return instance;
84     }
85
86     @Override
87     public ConstraintsBuilder getConstraints() {
88         return constraints;
89     }
90
91     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
92         return addedUnknownNodes;
93     }
94
95     @Override
96     public boolean isAugmenting() {
97         return augmenting;
98     }
99
100     @Override
101     public void setAugmenting(final boolean augmenting) {
102         this.augmenting = augmenting;
103     }
104
105     @Override
106     public boolean isAddedByUses() {
107         return addedByUses;
108     }
109
110     @Override
111     public void setAddedByUses(final boolean addedByUses) {
112         this.addedByUses = addedByUses;
113     }
114
115     @Override
116     public Boolean isConfiguration() {
117         return configuration;
118     }
119
120     @Override
121     public void setConfiguration(final Boolean configuration) {
122         this.configuration = configuration;
123     }
124
125     @Override
126     public int hashCode() {
127         final int prime = 31;
128         int result = 1;
129         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
130         return result;
131     }
132
133     @Override
134     public boolean equals(Object obj) {
135         if (this == obj) {
136             return true;
137         }
138         if (obj == null) {
139             return false;
140         }
141         if (getClass() != obj.getClass()) {
142             return false;
143         }
144         AnyXmlBuilder other = (AnyXmlBuilder) obj;
145         if (schemaPath == null) {
146             if (other.schemaPath != null) {
147                 return false;
148             }
149         } else if (!schemaPath.equals(other.schemaPath)) {
150             return false;
151         }
152         if (parent == null) {
153             if (other.parent != null) {
154                 return false;
155             }
156         } else if (!parent.equals(other.parent)) {
157             return false;
158         }
159         return true;
160     }
161
162     @Override
163     public String toString() {
164         return "anyxml " + qname.getLocalName();
165     }
166
167     private final class AnyXmlSchemaNodeImpl implements AnyXmlSchemaNode {
168         private final QName qname;
169         private SchemaPath path;
170         private String description;
171         private String reference;
172         private Status status = Status.CURRENT;
173         private boolean configuration;
174         private ConstraintDefinition constraintsDef;
175         private boolean augmenting;
176         private boolean addedByUses;
177         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
178
179         private AnyXmlSchemaNodeImpl(final QName qname) {
180             this.qname = qname;
181         }
182
183         @Override
184         public QName getQName() {
185             return qname;
186         }
187
188         @Override
189         public SchemaPath getPath() {
190             return path;
191         }
192
193         private void setPath(final SchemaPath path) {
194             this.path = path;
195         }
196
197         @Override
198         public String getDescription() {
199             return description;
200         }
201
202         private void setDescription(String description) {
203             this.description = description;
204         }
205
206         @Override
207         public String getReference() {
208             return reference;
209         }
210
211         private void setReference(String reference) {
212             this.reference = reference;
213         }
214
215         @Override
216         public Status getStatus() {
217             return status;
218         }
219
220         private void setStatus(Status status) {
221             if (status != null) {
222                 this.status = status;
223             }
224         }
225
226         @Override
227         public boolean isAugmenting() {
228             return augmenting;
229         }
230
231         private void setAugmenting(boolean augmenting) {
232             this.augmenting = augmenting;
233         }
234
235         @Override
236         public boolean isAddedByUses() {
237             return addedByUses;
238         }
239
240         private void setAddedByUses(boolean addedByUses) {
241             this.addedByUses = addedByUses;
242         }
243
244         @Override
245         public boolean isConfiguration() {
246             return configuration;
247         }
248
249         private void setConfiguration(boolean configuration) {
250             this.configuration = configuration;
251         }
252
253         @Override
254         public ConstraintDefinition getConstraints() {
255             return constraintsDef;
256         }
257
258         private void setConstraints(ConstraintDefinition constraintsDef) {
259             this.constraintsDef = constraintsDef;
260         }
261
262         @Override
263         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
264             return unknownNodes;
265         }
266
267         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
268             if (unknownNodes != null) {
269                 this.unknownNodes = unknownNodes;
270             }
271         }
272
273         @Override
274         public int hashCode() {
275             final int prime = 31;
276             int result = 1;
277             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
278             result = prime * result + ((path == null) ? 0 : path.hashCode());
279             return result;
280         }
281
282         @Override
283         public boolean equals(Object obj) {
284             if (this == obj) {
285                 return true;
286             }
287             if (obj == null) {
288                 return false;
289             }
290             if (getClass() != obj.getClass()) {
291                 return false;
292             }
293             AnyXmlSchemaNodeImpl other = (AnyXmlSchemaNodeImpl) obj;
294             if (qname == null) {
295                 if (other.qname != null) {
296                     return false;
297                 }
298             } else if (!qname.equals(other.qname)) {
299                 return false;
300             }
301             if (path == null) {
302                 if (other.path != null) {
303                     return false;
304                 }
305             } else if (!path.equals(other.path)) {
306                 return false;
307             }
308             return true;
309         }
310
311         @Override
312         public String toString() {
313             StringBuilder sb = new StringBuilder(AnyXmlSchemaNodeImpl.class.getSimpleName());
314             sb.append("[");
315             sb.append("qname=" + qname);
316             sb.append(", path=" + path);
317             sb.append("]");
318             return sb.toString();
319         }
320     }
321
322 }