Merge "Add IfNewHostNotify to DeviceManager"
[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,
30         TypeDefinitionAwareBuilder {
31     private boolean isBuilt;
32     private final RpcDefinitionImpl instance;
33     private final int line;
34     private final QName qname;
35     private SchemaPath schemaPath;
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     private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
41
42     RpcDefinitionBuilder(final QName qname, final int line) {
43         this.qname = qname;
44         this.line = line;
45         this.instance = new RpcDefinitionImpl(qname);
46     }
47
48     @Override
49     public RpcDefinition build() {
50         if (!isBuilt) {
51             final ContainerSchemaNode input = inputBuilder.build();
52             final ContainerSchemaNode output = outputBuilder.build();
53             instance.setInput(input);
54             instance.setOutput(output);
55
56             instance.setPath(schemaPath);
57
58             // TYPEDEFS
59             final Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
60             for (TypeDefinitionBuilder entry : addedTypedefs) {
61                 typedefs.add(entry.build());
62             }
63             instance.setTypeDefinitions(typedefs);
64
65             // GROUPINGS
66             final Set<GroupingDefinition> groupings = new HashSet<GroupingDefinition>();
67             for (GroupingBuilder entry : addedGroupings) {
68                 groupings.add(entry.build());
69             }
70             instance.setGroupings(groupings);
71
72             // UNKNOWN NODES
73             final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
74             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
75                 unknownNodes.add(b.build());
76             }
77             instance.setUnknownSchemaNodes(unknownNodes);
78
79             isBuilt = true;
80         }
81         return instance;
82     }
83
84     @Override
85     public int getLine() {
86         return line;
87     }
88
89     void setInput(final ContainerSchemaNodeBuilder inputBuilder) {
90         this.inputBuilder = inputBuilder;
91     }
92
93     void setOutput(final ContainerSchemaNodeBuilder outputBuilder) {
94         this.outputBuilder = outputBuilder;
95     }
96
97     public Set<TypeDefinitionBuilder> getTypeDefinitions() {
98         return addedTypedefs;
99     }
100
101     @Override
102     public void addTypedef(final TypeDefinitionBuilder type) {
103         addedTypedefs.add(type);
104     }
105
106     public Set<GroupingBuilder> getGroupings() {
107         return addedGroupings;
108     }
109
110     public void addGrouping(GroupingBuilder grouping) {
111         addedGroupings.add(grouping);
112     }
113
114     @Override
115     public SchemaPath getPath() {
116         return schemaPath;
117     }
118
119     @Override
120     public void setPath(SchemaPath schemaPath) {
121         this.schemaPath = schemaPath;
122     }
123
124     @Override
125     public void setDescription(final String description) {
126         instance.setDescription(description);
127     }
128
129     @Override
130     public void setReference(final String reference) {
131         instance.setReference(reference);
132     }
133
134     @Override
135     public void setStatus(final Status status) {
136         instance.setStatus(status);
137     }
138
139     @Override
140     public QName getQName() {
141         return null;
142     }
143
144     @Override
145     public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
146         addedUnknownNodes.add(unknownNode);
147     }
148
149     @Override
150     public int hashCode() {
151         return qname.hashCode();
152     }
153
154     @Override
155     public boolean equals(Object obj) {
156         if (obj == null) {
157             return false;
158         }
159         if (!(obj instanceof RpcDefinitionBuilder)) {
160             return false;
161         }
162         final RpcDefinitionBuilder other = (RpcDefinitionBuilder) obj;
163         if (other.qname == null) {
164             if (this.qname != null) {
165                 return false;
166             }
167         } else if (!other.qname.equals(this.qname)) {
168             return false;
169         }
170         return true;
171     }
172
173     private final class RpcDefinitionImpl implements RpcDefinition {
174         private final QName qname;
175         private SchemaPath path;
176         private String description;
177         private String reference;
178         private Status status;
179         private ContainerSchemaNode input;
180         private ContainerSchemaNode output;
181         private Set<TypeDefinition<?>> typeDefinitions;
182         private Set<GroupingDefinition> groupings;
183         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
184
185         private RpcDefinitionImpl(final QName qname) {
186             this.qname = qname;
187         }
188
189         @Override
190         public QName getQName() {
191             return qname;
192         }
193
194         @Override
195         public SchemaPath getPath() {
196             return path;
197         }
198
199         private void setPath(SchemaPath path) {
200             this.path = path;
201         }
202
203         @Override
204         public String getDescription() {
205             return description;
206         }
207
208         private void setDescription(String description) {
209             this.description = description;
210         }
211
212         @Override
213         public String getReference() {
214             return reference;
215         }
216
217         private void setReference(String reference) {
218             this.reference = reference;
219         }
220
221         @Override
222         public Status getStatus() {
223             return status;
224         }
225
226         private void setStatus(Status status) {
227             this.status = status;
228         }
229
230         @Override
231         public ContainerSchemaNode getInput() {
232             return input;
233         }
234
235         private void setInput(ContainerSchemaNode input) {
236             this.input = input;
237         }
238
239         @Override
240         public ContainerSchemaNode getOutput() {
241             return output;
242         }
243
244         private void setOutput(ContainerSchemaNode output) {
245             this.output = output;
246         }
247
248         @Override
249         public Set<TypeDefinition<?>> getTypeDefinitions() {
250             return typeDefinitions;
251         }
252
253         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
254             this.typeDefinitions = typeDefinitions;
255         }
256
257         @Override
258         public Set<GroupingDefinition> getGroupings() {
259             return groupings;
260         }
261
262         private void setGroupings(Set<GroupingDefinition> groupings) {
263             this.groupings = groupings;
264         }
265
266         @Override
267         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
268             return unknownNodes;
269         }
270
271         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
272             if (unknownNodes != null) {
273                 this.unknownNodes = unknownNodes;
274             }
275         }
276
277         @Override
278         public int hashCode() {
279             final int prime = 31;
280             int result = 1;
281             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
282             result = prime * result + ((path == null) ? 0 : path.hashCode());
283             return result;
284         }
285
286         @Override
287         public boolean equals(Object obj) {
288             if (this == obj) {
289                 return true;
290             }
291             if (obj == null) {
292                 return false;
293             }
294             if (getClass() != obj.getClass()) {
295                 return false;
296             }
297             final RpcDefinitionImpl other = (RpcDefinitionImpl) obj;
298             if (qname == null) {
299                 if (other.qname != null) {
300                     return false;
301                 }
302             } else if (!qname.equals(other.qname)) {
303                 return false;
304             }
305             if (path == null) {
306                 if (other.path != null) {
307                     return false;
308                 }
309             } else if (!path.equals(other.path)) {
310                 return false;
311             }
312             return true;
313         }
314
315         @Override
316         public String toString() {
317             StringBuilder sb = new StringBuilder(
318                     RpcDefinitionImpl.class.getSimpleName() + "[");
319             sb.append("qname=" + qname);
320             sb.append(", path=" + path);
321             sb.append(", input=" + input);
322             sb.append(", output=" + output + "]");
323             return sb.toString();
324         }
325     }
326
327 }