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