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