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