0eef258d300ccd4e457ddc059451625562667101
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / RpcEffectiveStatementImpl.java
1 /**
2  * Copyright (c) 2015 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.stmt.rfc6020.effective;
9
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
13 import java.util.HashSet;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Objects;
17 import java.util.Set;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.model.api.stmt.RpcStatement;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
29
30 public class RpcEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, RpcStatement> implements RpcDefinition {
31     private final QName qname;
32     private final SchemaPath path;
33
34     private ContainerSchemaNode input;
35     private ContainerSchemaNode output;
36
37     ImmutableSet<TypeDefinition<?>> typeDefinitions;
38     ImmutableSet<GroupingDefinition> groupings;
39     ImmutableList<UnknownSchemaNode> unknownNodes;
40
41     public RpcEffectiveStatementImpl(StmtContext<QName, RpcStatement, EffectiveStatement<QName, RpcStatement>> ctx) {
42         super(ctx);
43         this.qname = ctx.getStatementArgument();
44         this.path = Utils.getSchemaPath(ctx);
45
46         initSubstatements();
47
48     }
49
50     private void initSubstatements() {
51         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
52
53         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
54         Set<GroupingDefinition> groupingsInit = new HashSet<>();
55         Set<TypeDefinition<?>> typeDefinitionsInit = new HashSet<>();
56
57         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
58             if (effectiveStatement instanceof UnknownSchemaNode) {
59                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
60                 unknownNodesInit.add(unknownNode);
61             }
62             if (effectiveStatement instanceof GroupingDefinition) {
63                 GroupingDefinition groupingDefinition = (GroupingDefinition) effectiveStatement;
64                 groupingsInit.add(groupingDefinition);
65             }
66             if (effectiveStatement instanceof TypeDefinition) {
67                 TypeDefinition<?> typeDefinition = (TypeDefinition<?>) effectiveStatement;
68                 typeDefinitionsInit.add(typeDefinition);
69             }
70             if (this.input == null && effectiveStatement instanceof InputEffectiveStatementImpl) {
71                 this.input = (InputEffectiveStatementImpl) effectiveStatement;
72             }
73             if (this.output == null && effectiveStatement instanceof OutputEffectiveStatementImpl) {
74                 this.output = (OutputEffectiveStatementImpl) effectiveStatement;
75             }
76         }
77
78         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
79         this.groupings = ImmutableSet.copyOf(groupingsInit);
80         this.typeDefinitions = ImmutableSet.copyOf(typeDefinitionsInit);
81     }
82
83     @Override
84     public QName getQName() {
85         return qname;
86     }
87
88     @Override
89     public SchemaPath getPath() {
90         return path;
91     }
92
93     @Override
94     public ContainerSchemaNode getInput() {
95         return input;
96     }
97
98     void setInput(final ContainerSchemaNode input) {
99         this.input = input;
100     }
101
102     @Override
103     public ContainerSchemaNode getOutput() {
104         return output;
105     }
106
107     void setOutput(final ContainerSchemaNode output) {
108         this.output = output;
109     }
110
111     @Override
112     public Set<TypeDefinition<?>> getTypeDefinitions() {
113         return typeDefinitions;
114     }
115
116     @Override
117     public Set<GroupingDefinition> getGroupings() {
118         return groupings;
119     }
120
121     @Override
122     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
123         return unknownNodes;
124     }
125
126     @Override
127     public int hashCode() {
128         final int prime = 31;
129         int result = 1;
130         result = prime * result + Objects.hashCode(qname);
131         result = prime * result + Objects.hashCode(path);
132         return result;
133     }
134
135     @Override
136     public boolean equals(final Object obj) {
137         if (this == obj) {
138             return true;
139         }
140         if (obj == null) {
141             return false;
142         }
143         if (getClass() != obj.getClass()) {
144             return false;
145         }
146         final RpcEffectiveStatementImpl other = (RpcEffectiveStatementImpl) obj;
147         if (qname == null) {
148             if (other.qname != null) {
149                 return false;
150             }
151         } else if (!qname.equals(other.qname)) {
152             return false;
153         }
154         if (path == null) {
155             if (other.path != null) {
156                 return false;
157             }
158         } else if (!path.equals(other.path)) {
159             return false;
160         }
161         return true;
162     }
163
164     @Override
165     public String toString() {
166         StringBuilder sb = new StringBuilder(RpcEffectiveStatementImpl.class.getSimpleName());
167         sb.append("[");
168         sb.append("qname=");
169         sb.append(qname);
170         sb.append(", path=");
171         sb.append(path);
172         sb.append(", input=");
173         sb.append(input);
174         sb.append(", output=");
175         sb.append(output);
176         sb.append("]");
177         return sb.toString();
178     }
179 }