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