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