Added validation of range and length constraints.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / RpcDefinitionBuilder.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.HashSet;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.opendaylight.controller.yang.common.QName;
17 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
19 import org.opendaylight.controller.yang.model.api.RpcDefinition;
20 import org.opendaylight.controller.yang.model.api.SchemaPath;
21 import org.opendaylight.controller.yang.model.api.Status;
22 import org.opendaylight.controller.yang.model.api.TypeDefinition;
23 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
24 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
25 import org.opendaylight.controller.yang.parser.builder.api.SchemaNodeBuilder;
26 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionAwareBuilder;
27 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
28
29 public final class RpcDefinitionBuilder implements SchemaNodeBuilder, TypeDefinitionAwareBuilder {
30     private boolean isBuilt;
31     private final RpcDefinitionImpl instance;
32     private final int line;
33     private final QName qname;
34     private SchemaPath schemaPath;
35     private ContainerSchemaNodeBuilder inputBuilder;
36     private ContainerSchemaNodeBuilder outputBuilder;
37     private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
38     private final Set<GroupingBuilder> addedGroupings = new HashSet<GroupingBuilder>();
39     private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
40
41     RpcDefinitionBuilder(final QName qname, final int line) {
42         this.qname = qname;
43         this.line = line;
44         this.instance = new RpcDefinitionImpl(qname);
45     }
46
47     @Override
48     public RpcDefinition build() {
49         if (!isBuilt) {
50             final ContainerSchemaNode input = inputBuilder == null ? null : inputBuilder.build();
51             final ContainerSchemaNode output = outputBuilder == null ? null : outputBuilder.build();
52             instance.setInput(input);
53             instance.setOutput(output);
54
55             instance.setPath(schemaPath);
56
57             // TYPEDEFS
58             final Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
59             for (TypeDefinitionBuilder entry : addedTypedefs) {
60                 typedefs.add(entry.build());
61             }
62             instance.setTypeDefinitions(typedefs);
63
64             // GROUPINGS
65             final Set<GroupingDefinition> groupings = new HashSet<GroupingDefinition>();
66             for (GroupingBuilder entry : addedGroupings) {
67                 groupings.add(entry.build());
68             }
69             instance.setGroupings(groupings);
70
71             // UNKNOWN NODES
72             final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
73             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
74                 unknownNodes.add(b.build());
75             }
76             instance.setUnknownSchemaNodes(unknownNodes);
77
78             isBuilt = true;
79         }
80         return instance;
81     }
82
83     @Override
84     public int getLine() {
85         return line;
86     }
87
88     void setInput(final ContainerSchemaNodeBuilder inputBuilder) {
89         this.inputBuilder = inputBuilder;
90     }
91
92     void setOutput(final ContainerSchemaNodeBuilder outputBuilder) {
93         this.outputBuilder = outputBuilder;
94     }
95
96     public Set<TypeDefinitionBuilder> getTypeDefinitions() {
97         return addedTypedefs;
98     }
99
100     @Override
101     public void addTypedef(final TypeDefinitionBuilder type) {
102         addedTypedefs.add(type);
103     }
104
105     public Set<GroupingBuilder> getGroupings() {
106         return addedGroupings;
107     }
108
109     public void addGrouping(GroupingBuilder grouping) {
110         addedGroupings.add(grouping);
111     }
112
113     @Override
114     public SchemaPath getPath() {
115         return schemaPath;
116     }
117
118     @Override
119     public void setPath(SchemaPath schemaPath) {
120         this.schemaPath = schemaPath;
121     }
122
123     @Override
124     public void setDescription(final String description) {
125         instance.setDescription(description);
126     }
127
128     @Override
129     public void setReference(final String reference) {
130         instance.setReference(reference);
131     }
132
133     @Override
134     public void setStatus(final Status status) {
135         instance.setStatus(status);
136     }
137
138     @Override
139     public QName getQName() {
140         return null;
141     }
142
143     @Override
144     public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
145         addedUnknownNodes.add(unknownNode);
146     }
147
148     @Override
149     public int hashCode() {
150         return qname.hashCode();
151     }
152
153     @Override
154     public boolean equals(Object obj) {
155         if (obj == null) {
156             return false;
157         }
158         if (!(obj instanceof RpcDefinitionBuilder)) {
159             return false;
160         }
161         final RpcDefinitionBuilder other = (RpcDefinitionBuilder) obj;
162         if (other.qname == null) {
163             if (this.qname != null) {
164                 return false;
165             }
166         } else if (!other.qname.equals(this.qname)) {
167             return false;
168         }
169         return true;
170     }
171
172     private final class RpcDefinitionImpl implements RpcDefinition {
173         private final QName qname;
174         private SchemaPath path;
175         private String description;
176         private String reference;
177         private Status status;
178         private ContainerSchemaNode input;
179         private ContainerSchemaNode output;
180         private Set<TypeDefinition<?>> typeDefinitions;
181         private Set<GroupingDefinition> groupings;
182         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
183
184         private RpcDefinitionImpl(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(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             this.status = status;
227         }
228
229         @Override
230         public ContainerSchemaNode getInput() {
231             return input;
232         }
233
234         private void setInput(ContainerSchemaNode input) {
235             this.input = input;
236         }
237
238         @Override
239         public ContainerSchemaNode getOutput() {
240             return output;
241         }
242
243         private void setOutput(ContainerSchemaNode output) {
244             this.output = output;
245         }
246
247         @Override
248         public Set<TypeDefinition<?>> getTypeDefinitions() {
249             return typeDefinitions;
250         }
251
252         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
253             this.typeDefinitions = typeDefinitions;
254         }
255
256         @Override
257         public Set<GroupingDefinition> getGroupings() {
258             return groupings;
259         }
260
261         private void setGroupings(Set<GroupingDefinition> groupings) {
262             this.groupings = groupings;
263         }
264
265         @Override
266         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
267             return unknownNodes;
268         }
269
270         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
271             if (unknownNodes != null) {
272                 this.unknownNodes = unknownNodes;
273             }
274         }
275
276         @Override
277         public int hashCode() {
278             final int prime = 31;
279             int result = 1;
280             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
281             result = prime * result + ((path == null) ? 0 : path.hashCode());
282             return result;
283         }
284
285         @Override
286         public boolean equals(Object obj) {
287             if (this == obj) {
288                 return true;
289             }
290             if (obj == null) {
291                 return false;
292             }
293             if (getClass() != obj.getClass()) {
294                 return false;
295             }
296             final RpcDefinitionImpl other = (RpcDefinitionImpl) obj;
297             if (qname == null) {
298                 if (other.qname != null) {
299                     return false;
300                 }
301             } else if (!qname.equals(other.qname)) {
302                 return false;
303             }
304             if (path == null) {
305                 if (other.path != null) {
306                     return false;
307                 }
308             } else if (!path.equals(other.path)) {
309                 return false;
310             }
311             return true;
312         }
313
314         @Override
315         public String toString() {
316             StringBuilder sb = new StringBuilder(RpcDefinitionImpl.class.getSimpleName() + "[");
317             sb.append("qname=" + qname);
318             sb.append(", path=" + path);
319             sb.append(", input=" + input);
320             sb.append(", output=" + output + "]");
321             return sb.toString();
322         }
323     }
324
325 }