Bug 6410: Fixed initialization of typedefs in rpc
[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.ImmutableSet;
11 import java.util.Collection;
12 import java.util.HashSet;
13 import java.util.LinkedHashSet;
14 import java.util.Objects;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
19 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.RpcStatement;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24
25 public class RpcEffectiveStatementImpl extends AbstractEffectiveSchemaNode<RpcStatement> implements RpcDefinition {
26     private final ContainerSchemaNode input;
27     private final ContainerSchemaNode output;
28     private final Set<TypeDefinition<?>> typeDefinitions;
29     private final Set<GroupingDefinition> groupings;
30
31     public RpcEffectiveStatementImpl(final StmtContext<QName, RpcStatement, EffectiveStatement<QName, RpcStatement>> ctx) {
32         super(ctx);
33         this.input = firstEffective(InputEffectiveStatementImpl.class);
34         this.output = firstEffective(OutputEffectiveStatementImpl.class);
35
36         // initSubstatements
37         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
38         Set<GroupingDefinition> groupingsInit = new HashSet<>();
39         Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
40         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
41             if (effectiveStatement instanceof GroupingDefinition) {
42                 GroupingDefinition groupingDefinition = (GroupingDefinition) effectiveStatement;
43                 groupingsInit.add(groupingDefinition);
44             }
45             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
46                 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
47                 TypeDefinition<?> type = typeDef.getTypeDefinition();
48                 if (!mutableTypeDefinitions.contains(type)) {
49                     mutableTypeDefinitions.add(type);
50                 } else {
51                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
52                 }
53             }
54         }
55         this.groupings = ImmutableSet.copyOf(groupingsInit);
56         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
57     }
58
59     @Override
60     public ContainerSchemaNode getInput() {
61         return input;
62     }
63
64     @Override
65     public ContainerSchemaNode getOutput() {
66         return output;
67     }
68
69     @Override
70     public Set<TypeDefinition<?>> getTypeDefinitions() {
71         return typeDefinitions;
72     }
73
74     @Override
75     public Set<GroupingDefinition> getGroupings() {
76         return groupings;
77     }
78
79     @Override
80     public int hashCode() {
81         final int prime = 31;
82         int result = 1;
83         result = prime * result + Objects.hashCode(getQName());
84         result = prime * result + Objects.hashCode(getPath());
85         return result;
86     }
87
88     @Override
89     public boolean equals(final Object obj) {
90         if (this == obj) {
91             return true;
92         }
93         if (obj == null) {
94             return false;
95         }
96         if (getClass() != obj.getClass()) {
97             return false;
98         }
99         final RpcEffectiveStatementImpl other = (RpcEffectiveStatementImpl) obj;
100         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
101     }
102
103     @Override
104     public String toString() {
105         StringBuilder sb = new StringBuilder(RpcEffectiveStatementImpl.class.getSimpleName());
106         sb.append("[");
107         sb.append("qname=");
108         sb.append(getQName());
109         sb.append(", path=");
110         sb.append(getPath());
111         sb.append(", input=");
112         sb.append(input);
113         sb.append(", output=");
114         sb.append(output);
115         sb.append("]");
116         return sb.toString();
117     }
118 }