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