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