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