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