Merge from development repository.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / 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.model.parser.builder.impl;
9
10 import java.util.Collections;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Set;
14
15 import org.opendaylight.controller.yang.common.QName;
16 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
17 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
18 import org.opendaylight.controller.yang.model.api.RpcDefinition;
19 import org.opendaylight.controller.yang.model.api.SchemaPath;
20 import org.opendaylight.controller.yang.model.api.Status;
21 import org.opendaylight.controller.yang.model.api.TypeDefinition;
22 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.controller.yang.model.parser.builder.api.ChildNodeBuilder;
24 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
25 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
26 import org.opendaylight.controller.yang.model.parser.builder.api.SchemaNodeBuilder;
27 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionAwareBuilder;
28 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
29 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
30
31 public class RpcDefinitionBuilder implements ChildNodeBuilder,
32         SchemaNodeBuilder, TypeDefinitionAwareBuilder {
33
34     private final RpcDefinitionImpl instance;
35     private final QName qname;
36     private ContainerSchemaNodeBuilder inputBuilder;
37     private ContainerSchemaNodeBuilder outputBuilder;
38     private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
39     private final Set<GroupingBuilder> addedGroupings = new HashSet<GroupingBuilder>();
40
41     RpcDefinitionBuilder(QName qname) {
42         this.qname = qname;
43         this.instance = new RpcDefinitionImpl(qname);
44     }
45
46     @Override
47     public RpcDefinition build() {
48         final ContainerSchemaNode input = inputBuilder.build();
49         final ContainerSchemaNode output = outputBuilder.build();
50         instance.setInput(input);
51         instance.setOutput(output);
52
53         // TYPEDEFS
54         Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
55         for (TypeDefinitionBuilder entry : addedTypedefs) {
56             typedefs.add(entry.build());
57         }
58         instance.setTypeDefinitions(typedefs);
59
60         // GROUPINGS
61         final Set<GroupingDefinition> groupings = new HashSet<GroupingDefinition>();
62         for (GroupingBuilder entry : addedGroupings) {
63             groupings.add(entry.build());
64         }
65         instance.setGroupings(groupings);
66
67         return instance;
68     }
69
70     void setInput(ContainerSchemaNodeBuilder inputBuilder) {
71         this.inputBuilder = inputBuilder;
72     }
73
74     void setOutput(ContainerSchemaNodeBuilder outputBuilder) {
75         this.outputBuilder = outputBuilder;
76     }
77
78     @Override
79     public void addTypedef(TypeDefinitionBuilder type) {
80         addedTypedefs.add(type);
81     }
82
83     @Override
84     public void setPath(SchemaPath schemaPath) {
85         instance.setPath(schemaPath);
86     }
87
88     @Override
89     public void setDescription(String description) {
90         instance.setDescription(description);
91     }
92
93     @Override
94     public void setReference(String reference) {
95         instance.setReference(reference);
96     }
97
98     @Override
99     public void setStatus(Status status) {
100         instance.setStatus(status);
101     }
102
103     @Override
104     public QName getQName() {
105         return null;
106     }
107
108     @Override
109     public void addChildNode(DataSchemaNodeBuilder childNode) {
110         throw new UnsupportedOperationException(
111                 "Can not add child node to rpc definition: rpc can not contains child nodes.");
112     }
113
114     @Override
115     public void addGrouping(GroupingBuilder grouping) {
116         addedGroupings.add(grouping);
117     }
118
119     @Override
120     public void addUsesNode(UsesNodeBuilder usesBuilder) {
121         throw new UnsupportedOperationException(
122                 "Can not add uses node to rpc definition: rpc can not contains uses nodes.");
123     }
124
125     @Override
126     public int hashCode() {
127         return qname.hashCode();
128     }
129
130     @Override
131     public boolean equals(Object obj) {
132         if (obj == null) {
133             return false;
134         }
135         if (!(obj instanceof RpcDefinitionBuilder)) {
136             return false;
137         }
138         RpcDefinitionBuilder other = (RpcDefinitionBuilder) obj;
139         if (other.qname == null) {
140             if (this.qname != null) {
141                 return false;
142             }
143         } else if (!other.qname.equals(this.qname)) {
144             return false;
145         }
146         return true;
147     }
148
149     private static class RpcDefinitionImpl implements RpcDefinition {
150
151         private final QName qname;
152         private SchemaPath path;
153         private String description;
154         private String reference;
155         private Status status;
156         private ContainerSchemaNode input;
157         private ContainerSchemaNode output;
158         private Set<TypeDefinition<?>> typeDefinitions;
159         private Set<GroupingDefinition> groupings;
160         private List<UnknownSchemaNode> unknownSchemaNodes = Collections.emptyList();
161
162         private RpcDefinitionImpl(QName qname) {
163             this.qname = qname;
164         }
165
166         @Override
167         public QName getQName() {
168             return qname;
169         }
170
171         @Override
172         public SchemaPath getPath() {
173             return path;
174         }
175
176         private void setPath(SchemaPath path) {
177             this.path = path;
178         }
179
180         @Override
181         public String getDescription() {
182             return description;
183         }
184
185         private void setDescription(String description) {
186             this.description = description;
187         }
188
189         @Override
190         public String getReference() {
191             return reference;
192         }
193
194         private void setReference(String reference) {
195             this.reference = reference;
196         }
197
198         @Override
199         public Status getStatus() {
200             return status;
201         }
202
203         private void setStatus(Status status) {
204             this.status = status;
205         }
206
207         @Override
208         public ContainerSchemaNode getInput() {
209             return input;
210         }
211
212         private void setInput(ContainerSchemaNode input) {
213             this.input = input;
214         }
215
216         @Override
217         public ContainerSchemaNode getOutput() {
218             return output;
219         }
220
221         private void setOutput(ContainerSchemaNode output) {
222             this.output = output;
223         }
224
225         @Override
226         public Set<TypeDefinition<?>> getTypeDefinitions() {
227             return typeDefinitions;
228         }
229
230         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
231             this.typeDefinitions = typeDefinitions;
232         }
233
234         @Override
235         public Set<GroupingDefinition> getGroupings() {
236             return groupings;
237         }
238
239         private void setGroupings(Set<GroupingDefinition> groupings) {
240             this.groupings = groupings;
241         }
242
243         @Override
244         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
245             return unknownSchemaNodes;
246         }
247
248         @Override
249         public int hashCode() {
250             final int prime = 31;
251             int result = 1;
252             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
253             result = prime * result + ((path == null) ? 0 : path.hashCode());
254             result = prime * result
255                     + ((description == null) ? 0 : description.hashCode());
256             result = prime * result
257                     + ((reference == null) ? 0 : reference.hashCode());
258             result = prime * result
259                     + ((status == null) ? 0 : status.hashCode());
260             result = prime * result
261                     + ((input == null) ? 0 : input.hashCode());
262             result = prime * result
263                     + ((output == null) ? 0 : output.hashCode());
264             result = prime * result
265                     + ((typeDefinitions == null) ? 0 : typeDefinitions.hashCode());
266             result = prime * result
267                     + ((groupings == null) ? 0 : groupings.hashCode());
268             return result;
269         }
270
271         @Override
272         public boolean equals(Object obj) {
273             if (this == obj) {
274                 return true;
275             }
276             if (obj == null) {
277                 return false;
278             }
279             if (getClass() != obj.getClass()) {
280                 return false;
281             }
282             RpcDefinitionImpl other = (RpcDefinitionImpl) obj;
283             if (qname == null) {
284                 if (other.qname != null) {
285                     return false;
286                 }
287             } else if (!qname.equals(other.qname)) {
288                 return false;
289             }
290             if (path == null) {
291                 if (other.path != null) {
292                     return false;
293                 }
294             } else if (!path.equals(other.path)) {
295                 return false;
296             }
297             if (description == null) {
298                 if (other.description != null) {
299                     return false;
300                 }
301             } else if (!description.equals(other.description)) {
302                 return false;
303             }
304             if (reference == null) {
305                 if (other.reference != null) {
306                     return false;
307                 }
308             } else if (!reference.equals(other.reference)) {
309                 return false;
310             }
311             if (status == null) {
312                 if (other.status != null) {
313                     return false;
314                 }
315             } else if (!status.equals(other.status)) {
316                 return false;
317             }
318             if (input == null) {
319                 if (other.input != null) {
320                     return false;
321                 }
322             } else if (!input.equals(other.input)) {
323                 return false;
324             }
325             if (output == null) {
326                 if (other.output != null) {
327                     return false;
328                 }
329             } else if (!output.equals(other.output)) {
330                 return false;
331             }
332             if (typeDefinitions == null) {
333                 if (other.typeDefinitions != null) {
334                     return false;
335                 }
336             } else if (!typeDefinitions.equals(other.typeDefinitions)) {
337                 return false;
338             }
339             if (groupings == null) {
340                 if (other.groupings != null) {
341                     return false;
342                 }
343             } else if (!groupings.equals(other.groupings)) {
344                 return false;
345             }
346             return true;
347         }
348
349         @Override
350         public String toString() {
351             StringBuilder sb = new StringBuilder(
352                     RpcDefinitionImpl.class.getSimpleName() + "[");
353             sb.append("qname=" + qname);
354             sb.append(", path=" + path);
355             sb.append(", description=" + description);
356             sb.append(", reference=" + reference);
357             sb.append(", status=" + status);
358             sb.append(", input=" + input);
359             sb.append(", output=" + output + "]");
360             return sb.toString();
361         }
362     }
363
364 }