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.HashSet;
11 import java.util.List;
13 import java.util.TreeSet;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
18 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.opendaylight.yangtools.yang.model.api.Status;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractSchemaNodeBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
26 import org.opendaylight.yangtools.yang.parser.util.Comparators;
28 import com.google.common.collect.ImmutableList;
29 import com.google.common.collect.ImmutableSet;
31 public final class RpcDefinitionBuilder extends AbstractSchemaNodeBuilder {
32 private RpcDefinitionImpl instance;
33 private ContainerSchemaNodeBuilder inputBuilder;
34 private ContainerSchemaNodeBuilder outputBuilder;
35 private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<>();
36 private final Set<GroupingBuilder> addedGroupings = new HashSet<>();
38 public ContainerSchemaNodeBuilder getInput() {
42 public ContainerSchemaNodeBuilder getOutput() {
46 RpcDefinitionBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
47 super(moduleName, line, qname);
48 this.schemaPath = path;
52 public RpcDefinition build() {
53 if (instance != null) {
57 instance = new RpcDefinitionImpl(qname, schemaPath);
59 final ContainerSchemaNode input = inputBuilder == null ? null : inputBuilder.build();
60 final ContainerSchemaNode output = outputBuilder == null ? null : outputBuilder.build();
61 instance.setInput(input);
62 instance.setOutput(output);
64 instance.description = description;
65 instance.reference = reference;
66 instance.status = status;
69 final Set<TypeDefinition<?>> typedefs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
70 for (TypeDefinitionBuilder entry : addedTypedefs) {
71 typedefs.add(entry.build());
73 instance.typeDefinitions = ImmutableSet.copyOf(typedefs);
76 final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
77 for (GroupingBuilder builder : addedGroupings) {
78 groupings.add(builder.build());
80 instance.groupings = ImmutableSet.copyOf(groupings);
83 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
84 unknownNodes.add(b.build());
86 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
91 void setInput(final ContainerSchemaNodeBuilder inputBuilder) {
92 this.inputBuilder = inputBuilder;
95 void setOutput(final ContainerSchemaNodeBuilder outputBuilder) {
96 this.outputBuilder = outputBuilder;
99 public Set<TypeDefinitionBuilder> getTypeDefinitions() {
100 return addedTypedefs;
103 public void addTypedef(final TypeDefinitionBuilder type) {
104 addedTypedefs.add(type);
107 public Set<GroupingBuilder> getGroupings() {
108 return addedGroupings;
111 public void addGrouping(final GroupingBuilder grouping) {
112 addedGroupings.add(grouping);
116 public int hashCode() {
117 final int prime = 31;
119 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
120 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
125 public boolean equals(final Object obj) {
129 if (!(obj instanceof RpcDefinitionBuilder)) {
132 final RpcDefinitionBuilder other = (RpcDefinitionBuilder) obj;
133 if (other.qname == null) {
134 if (this.qname != null) {
137 } else if (!other.qname.equals(this.qname)) {
140 if (other.schemaPath == null) {
141 if (this.schemaPath != null) {
144 } else if (!other.schemaPath.equals(this.schemaPath)) {
151 public String toString() {
152 return "rpc " + qname.getLocalName();
155 private static final class RpcDefinitionImpl implements RpcDefinition {
156 private final QName qname;
157 private final SchemaPath path;
158 private String description;
159 private String reference;
160 private Status status;
161 private ContainerSchemaNode input;
162 private ContainerSchemaNode output;
163 private ImmutableSet<TypeDefinition<?>> typeDefinitions;
164 private ImmutableSet<GroupingDefinition> groupings;
165 private ImmutableList<UnknownSchemaNode> unknownNodes;
167 private RpcDefinitionImpl(final QName qname, final SchemaPath path) {
173 public QName getQName() {
178 public SchemaPath getPath() {
183 public String getDescription() {
188 public String getReference() {
193 public Status getStatus() {
198 public ContainerSchemaNode getInput() {
202 private void setInput(final ContainerSchemaNode input) {
207 public ContainerSchemaNode getOutput() {
211 private void setOutput(final ContainerSchemaNode output) {
212 this.output = output;
216 public Set<TypeDefinition<?>> getTypeDefinitions() {
217 return typeDefinitions;
221 public Set<GroupingDefinition> getGroupings() {
226 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
231 public int hashCode() {
232 final int prime = 31;
234 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
235 result = prime * result + ((path == null) ? 0 : path.hashCode());
240 public boolean equals(final Object obj) {
247 if (getClass() != obj.getClass()) {
250 final RpcDefinitionImpl other = (RpcDefinitionImpl) obj;
252 if (other.qname != null) {
255 } else if (!qname.equals(other.qname)) {
259 if (other.path != null) {
262 } else if (!path.equals(other.path)) {
269 public String toString() {
270 StringBuilder sb = new StringBuilder(RpcDefinitionImpl.class.getSimpleName());
274 sb.append(", path=");
276 sb.append(", input=");
278 sb.append(", output=");
281 return sb.toString();