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