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