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.controller.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.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;
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>();
38 RpcDefinitionBuilder(final int line, final QName qname) {
40 this.instance = new RpcDefinitionImpl(qname);
44 public RpcDefinition build() {
46 instance.setDescription(description);
47 instance.setReference(reference);
48 instance.setStatus(status);
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);
55 instance.setPath(schemaPath);
58 final Set<TypeDefinition<?>> typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
59 for (TypeDefinitionBuilder entry : addedTypedefs) {
60 typedefs.add(entry.build());
62 instance.setTypeDefinitions(typedefs);
65 final Set<GroupingDefinition> groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
66 for (GroupingBuilder entry : addedGroupings) {
67 groupings.add(entry.build());
69 instance.setGroupings(groupings);
72 final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
73 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
74 unknownNodes.add(b.build());
76 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
77 instance.setUnknownSchemaNodes(unknownNodes);
84 void setInput(final ContainerSchemaNodeBuilder inputBuilder) {
85 this.inputBuilder = inputBuilder;
88 void setOutput(final ContainerSchemaNodeBuilder outputBuilder) {
89 this.outputBuilder = outputBuilder;
92 public Set<TypeDefinitionBuilder> getTypeDefinitions() {
96 public void addTypedef(final TypeDefinitionBuilder type) {
97 addedTypedefs.add(type);
100 public Set<GroupingBuilder> getGroupings() {
101 return addedGroupings;
104 public void addGrouping(GroupingBuilder grouping) {
105 addedGroupings.add(grouping);
109 public int hashCode() {
110 final int prime = 31;
112 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
113 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
118 public boolean equals(Object obj) {
122 if (!(obj instanceof RpcDefinitionBuilder)) {
125 final RpcDefinitionBuilder other = (RpcDefinitionBuilder) obj;
126 if (other.qname == null) {
127 if (this.qname != null) {
130 } else if (!other.qname.equals(this.qname)) {
133 if (other.schemaPath == null) {
134 if (this.schemaPath != null) {
137 } else if (!other.schemaPath.equals(this.schemaPath)) {
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();
155 private RpcDefinitionImpl(final QName qname) {
160 public QName getQName() {
165 public SchemaPath getPath() {
169 private void setPath(SchemaPath path) {
174 public String getDescription() {
178 private void setDescription(String description) {
179 this.description = description;
183 public String getReference() {
187 private void setReference(String reference) {
188 this.reference = reference;
192 public Status getStatus() {
196 private void setStatus(Status status) {
197 this.status = status;
201 public ContainerSchemaNode getInput() {
205 private void setInput(ContainerSchemaNode input) {
210 public ContainerSchemaNode getOutput() {
214 private void setOutput(ContainerSchemaNode output) {
215 this.output = output;
219 public Set<TypeDefinition<?>> getTypeDefinitions() {
220 return typeDefinitions;
223 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
224 this.typeDefinitions = typeDefinitions;
228 public Set<GroupingDefinition> getGroupings() {
232 private void setGroupings(Set<GroupingDefinition> groupings) {
233 this.groupings = groupings;
237 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
241 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
242 if (unknownNodes != null) {
243 this.unknownNodes = unknownNodes;
248 public int hashCode() {
249 final int prime = 31;
251 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
252 result = prime * result + ((path == null) ? 0 : path.hashCode());
257 public boolean equals(Object obj) {
264 if (getClass() != obj.getClass()) {
267 final RpcDefinitionImpl other = (RpcDefinitionImpl) obj;
269 if (other.qname != null) {
272 } else if (!qname.equals(other.qname)) {
276 if (other.path != null) {
279 } else if (!path.equals(other.path)) {
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();