53190d0bb6df4ec6b83b57623404674c59a34060
[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 import java.util.TreeSet;
16
17 import org.opendaylight.controller.yang.common.QName;
18 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
19 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
20 import org.opendaylight.controller.yang.model.api.RpcDefinition;
21 import org.opendaylight.controller.yang.model.api.SchemaPath;
22 import org.opendaylight.controller.yang.model.api.Status;
23 import org.opendaylight.controller.yang.model.api.TypeDefinition;
24 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
25 import org.opendaylight.controller.yang.parser.builder.api.AbstractSchemaNodeBuilder;
26 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
27 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
28 import org.opendaylight.controller.yang.parser.util.Comparators;
29
30 public final class RpcDefinitionBuilder extends AbstractSchemaNodeBuilder {
31     private boolean isBuilt;
32     private final RpcDefinitionImpl instance;
33     private ContainerSchemaNodeBuilder inputBuilder;
34     private ContainerSchemaNodeBuilder outputBuilder;
35     private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
36     private final Set<GroupingBuilder> addedGroupings = new HashSet<GroupingBuilder>();
37
38     RpcDefinitionBuilder(final String moduleName, final int line, final QName qname) {
39         super(moduleName, line, qname);
40         this.instance = new RpcDefinitionImpl(qname);
41     }
42
43     @Override
44     public RpcDefinition build() {
45         if (!isBuilt) {
46             instance.setDescription(description);
47             instance.setReference(reference);
48             instance.setStatus(status);
49
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 TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
59             for (TypeDefinitionBuilder entry : addedTypedefs) {
60                 typedefs.add(entry.build());
61             }
62             instance.setTypeDefinitions(typedefs);
63
64             // GROUPINGS
65             final Set<GroupingDefinition> groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
66             for (GroupingBuilder entry : addedGroupings) {
67                 groupings.add(entry.build());
68             }
69             instance.setGroupings(groupings);
70
71             // UNKNOWN NODES
72             if (unknownNodes == null) {
73                 unknownNodes = new ArrayList<UnknownSchemaNode>();
74                 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
75                     unknownNodes.add(b.build());
76                 }
77                 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
78             }
79             instance.setUnknownSchemaNodes(unknownNodes);
80
81             isBuilt = true;
82         }
83         return instance;
84     }
85
86     void setInput(final ContainerSchemaNodeBuilder inputBuilder) {
87         this.inputBuilder = inputBuilder;
88     }
89
90     void setOutput(final ContainerSchemaNodeBuilder outputBuilder) {
91         this.outputBuilder = outputBuilder;
92     }
93
94     public Set<TypeDefinitionBuilder> getTypeDefinitions() {
95         return addedTypedefs;
96     }
97
98     public void addTypedef(final TypeDefinitionBuilder type) {
99         addedTypedefs.add(type);
100     }
101
102     public Set<GroupingBuilder> getGroupings() {
103         return addedGroupings;
104     }
105
106     public void addGrouping(GroupingBuilder grouping) {
107         addedGroupings.add(grouping);
108     }
109
110     @Override
111     public int hashCode() {
112         final int prime = 31;
113         int result = 1;
114         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
115         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
116         return result;
117     }
118
119     @Override
120     public boolean equals(Object obj) {
121         if (obj == null) {
122             return false;
123         }
124         if (!(obj instanceof RpcDefinitionBuilder)) {
125             return false;
126         }
127         final RpcDefinitionBuilder other = (RpcDefinitionBuilder) obj;
128         if (other.qname == null) {
129             if (this.qname != null) {
130                 return false;
131             }
132         } else if (!other.qname.equals(this.qname)) {
133             return false;
134         }
135         if (other.schemaPath == null) {
136             if (this.schemaPath != null) {
137                 return false;
138             }
139         } else if (!other.schemaPath.equals(this.schemaPath)) {
140             return false;
141         }
142         return true;
143     }
144
145     @Override
146     public String toString() {
147         return "rpc " + qname.getLocalName();
148     }
149
150     private final class RpcDefinitionImpl implements RpcDefinition {
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> unknownNodes = Collections.emptyList();
161
162         private RpcDefinitionImpl(final 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 unknownNodes;
246         }
247
248         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
249             if (unknownNodes != null) {
250                 this.unknownNodes = unknownNodes;
251             }
252         }
253
254         @Override
255         public int hashCode() {
256             final int prime = 31;
257             int result = 1;
258             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
259             result = prime * result + ((path == null) ? 0 : path.hashCode());
260             return result;
261         }
262
263         @Override
264         public boolean equals(Object obj) {
265             if (this == obj) {
266                 return true;
267             }
268             if (obj == null) {
269                 return false;
270             }
271             if (getClass() != obj.getClass()) {
272                 return false;
273             }
274             final RpcDefinitionImpl other = (RpcDefinitionImpl) obj;
275             if (qname == null) {
276                 if (other.qname != null) {
277                     return false;
278                 }
279             } else if (!qname.equals(other.qname)) {
280                 return false;
281             }
282             if (path == null) {
283                 if (other.path != null) {
284                     return false;
285                 }
286             } else if (!path.equals(other.path)) {
287                 return false;
288             }
289             return true;
290         }
291
292         @Override
293         public String toString() {
294             StringBuilder sb = new StringBuilder(RpcDefinitionImpl.class.getSimpleName() + "[");
295             sb.append("qname=" + qname);
296             sb.append(", path=" + path);
297             sb.append(", input=" + input);
298             sb.append(", output=" + output + "]");
299             return sb.toString();
300         }
301     }
302
303 }