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