2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.builder.impl;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
15 import java.util.TreeSet;
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;
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<TypeDefinition<?>> typedefs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
36 private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<>();
37 private final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
38 private final Set<GroupingBuilder> addedGroupings = new HashSet<>();
40 public ContainerSchemaNodeBuilder getInput() {
44 public ContainerSchemaNodeBuilder getOutput() {
48 RpcDefinitionBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
49 super(moduleName, line, qname);
50 this.schemaPath = path;
51 this.instance = new RpcDefinitionImpl(qname, path);
55 public RpcDefinition build() {
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);
63 for (TypeDefinitionBuilder entry : addedTypedefs) {
64 typedefs.add(entry.build());
66 instance.setTypeDefinitions(typedefs);
69 for (GroupingBuilder entry : addedGroupings) {
70 groupings.add(entry.build());
72 instance.setGroupings(groupings);
75 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
76 unknownNodes.add(b.build());
78 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
79 instance.setUnknownSchemaNodes(unknownNodes);
87 public SchemaPath getPath() {
92 public String getDescription() {
93 return instance.description;
97 public void setDescription(final String description) {
98 instance.description = description;
102 public String getReference() {
103 return instance.reference;
107 public void setReference(final String reference) {
108 instance.reference = reference;
112 public Status getStatus() {
113 return instance.status;
117 public void setStatus(Status status) {
118 if (status != null) {
119 instance.status = status;
123 void setInput(final ContainerSchemaNodeBuilder inputBuilder) {
124 this.inputBuilder = inputBuilder;
127 void setOutput(final ContainerSchemaNodeBuilder outputBuilder) {
128 this.outputBuilder = outputBuilder;
131 public Set<TypeDefinitionBuilder> getTypeDefinitions() {
132 return addedTypedefs;
135 public void addTypedef(final TypeDefinitionBuilder type) {
136 addedTypedefs.add(type);
139 public Set<GroupingBuilder> getGroupings() {
140 return addedGroupings;
143 public void addGrouping(GroupingBuilder grouping) {
144 addedGroupings.add(grouping);
148 public int hashCode() {
149 final int prime = 31;
151 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
152 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
157 public boolean equals(Object obj) {
161 if (!(obj instanceof RpcDefinitionBuilder)) {
164 final RpcDefinitionBuilder other = (RpcDefinitionBuilder) obj;
165 if (other.qname == null) {
166 if (this.qname != null) {
169 } else if (!other.qname.equals(this.qname)) {
172 if (other.schemaPath == null) {
173 if (this.schemaPath != null) {
176 } else if (!other.schemaPath.equals(this.schemaPath)) {
183 public String toString() {
184 return "rpc " + qname.getLocalName();
187 private final class RpcDefinitionImpl implements RpcDefinition {
188 private final QName qname;
189 private final SchemaPath path;
190 private String description;
191 private String reference;
192 private Status status;
193 private ContainerSchemaNode input;
194 private ContainerSchemaNode output;
195 private final Set<TypeDefinition<?>> typeDefinitions = new HashSet<>();
196 private final Set<GroupingDefinition> groupings = new HashSet<>();
197 private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
199 private RpcDefinitionImpl(final QName qname, final SchemaPath path) {
205 public QName getQName() {
210 public SchemaPath getPath() {
215 public String getDescription() {
220 public String getReference() {
225 public Status getStatus() {
230 public ContainerSchemaNode getInput() {
234 private void setInput(ContainerSchemaNode input) {
239 public ContainerSchemaNode getOutput() {
243 private void setOutput(ContainerSchemaNode output) {
244 this.output = output;
248 public Set<TypeDefinition<?>> getTypeDefinitions() {
249 return Collections.unmodifiableSet(typeDefinitions);
252 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
253 this.typeDefinitions.addAll(typeDefinitions);
257 public Set<GroupingDefinition> getGroupings() {
258 return Collections.unmodifiableSet(groupings);
261 private void setGroupings(Set<GroupingDefinition> groupings) {
262 this.groupings.addAll(groupings);
266 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
267 return Collections.unmodifiableList(unknownNodes);
270 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
271 if (unknownNodes != null) {
272 this.unknownNodes.addAll(unknownNodes);
277 public int hashCode() {
278 final int prime = 31;
280 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
281 result = prime * result + ((path == null) ? 0 : path.hashCode());
286 public boolean equals(Object obj) {
293 if (getClass() != obj.getClass()) {
296 final RpcDefinitionImpl other = (RpcDefinitionImpl) obj;
298 if (other.qname != null) {
301 } else if (!qname.equals(other.qname)) {
305 if (other.path != null) {
308 } else if (!path.equals(other.path)) {
315 public String toString() {
316 StringBuilder sb = new StringBuilder(RpcDefinitionImpl.class.getSimpleName());
320 sb.append(", path=");
322 sb.append(", input=");
324 sb.append(", output=");
327 return sb.toString();