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