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 if (unknownNodes == null) {
73 unknownNodes = new ArrayList<UnknownSchemaNode>();
74 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
75 unknownNodes.add(b.build());
77 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
79 instance.setUnknownSchemaNodes(unknownNodes);
86 void setInput(final ContainerSchemaNodeBuilder inputBuilder) {
87 this.inputBuilder = inputBuilder;
90 void setOutput(final ContainerSchemaNodeBuilder outputBuilder) {
91 this.outputBuilder = outputBuilder;
94 public Set<TypeDefinitionBuilder> getTypeDefinitions() {
98 public void addTypedef(final TypeDefinitionBuilder type) {
99 addedTypedefs.add(type);
102 public Set<GroupingBuilder> getGroupings() {
103 return addedGroupings;
106 public void addGrouping(GroupingBuilder grouping) {
107 addedGroupings.add(grouping);
111 public int hashCode() {
112 final int prime = 31;
114 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
115 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
120 public boolean equals(Object obj) {
124 if (!(obj instanceof RpcDefinitionBuilder)) {
127 final RpcDefinitionBuilder other = (RpcDefinitionBuilder) obj;
128 if (other.qname == null) {
129 if (this.qname != null) {
132 } else if (!other.qname.equals(this.qname)) {
135 if (other.schemaPath == null) {
136 if (this.schemaPath != null) {
139 } else if (!other.schemaPath.equals(this.schemaPath)) {
146 public String toString() {
147 return "rpc " + qname.getLocalName();
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();
162 private RpcDefinitionImpl(final QName qname) {
167 public QName getQName() {
172 public SchemaPath getPath() {
176 private void setPath(SchemaPath path) {
181 public String getDescription() {
185 private void setDescription(String description) {
186 this.description = description;
190 public String getReference() {
194 private void setReference(String reference) {
195 this.reference = reference;
199 public Status getStatus() {
203 private void setStatus(Status status) {
204 this.status = status;
208 public ContainerSchemaNode getInput() {
212 private void setInput(ContainerSchemaNode input) {
217 public ContainerSchemaNode getOutput() {
221 private void setOutput(ContainerSchemaNode output) {
222 this.output = output;
226 public Set<TypeDefinition<?>> getTypeDefinitions() {
227 return typeDefinitions;
230 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
231 this.typeDefinitions = typeDefinitions;
235 public Set<GroupingDefinition> getGroupings() {
239 private void setGroupings(Set<GroupingDefinition> groupings) {
240 this.groupings = groupings;
244 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
248 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
249 if (unknownNodes != null) {
250 this.unknownNodes = unknownNodes;
255 public int hashCode() {
256 final int prime = 31;
258 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
259 result = prime * result + ((path == null) ? 0 : path.hashCode());
264 public boolean equals(Object obj) {
271 if (getClass() != obj.getClass()) {
274 final RpcDefinitionImpl other = (RpcDefinitionImpl) obj;
276 if (other.qname != null) {
279 } else if (!qname.equals(other.qname)) {
283 if (other.path != null) {
286 } else if (!path.equals(other.path)) {
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();