1a4ee61bed80081203116dd7cd2a940fdb0206f1
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / effective / ActionEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950.effective;
10
11 import com.google.common.base.MoreObjects;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.HashSet;
14 import java.util.LinkedHashSet;
15 import java.util.Objects;
16 import java.util.Set;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ActionStatement;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AbstractEffectiveSchemaNode;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStmtUtils;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.InputEffectiveStatementImpl;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.OutputEffectiveStatementImpl;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.TypeDefEffectiveStatementImpl;
30
31 public class ActionEffectiveStatementImpl extends AbstractEffectiveSchemaNode<ActionStatement>
32         implements ActionDefinition {
33     private final ContainerSchemaNode input;
34     private final ContainerSchemaNode output;
35     private final Set<TypeDefinition<?>> typeDefinitions;
36     private final Set<GroupingDefinition> groupings;
37
38     public ActionEffectiveStatementImpl(
39             final StmtContext<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> ctx) {
40         super(ctx);
41         this.input = firstEffective(InputEffectiveStatementImpl.class);
42         this.output = firstEffective(OutputEffectiveStatementImpl.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 TypeDefEffectiveStatementImpl) {
53                 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) 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     @Override
67     public ContainerSchemaNode getInput() {
68         return input;
69     }
70
71     @Override
72     public ContainerSchemaNode getOutput() {
73         return output;
74     }
75
76     @Override
77     public Set<TypeDefinition<?>> getTypeDefinitions() {
78         return typeDefinitions;
79     }
80
81     @Override
82     public Set<GroupingDefinition> getGroupings() {
83         return groupings;
84     }
85
86     @Override
87     public int hashCode() {
88         return Objects.hash(getQName(), getPath());
89     }
90
91     @Override
92     public boolean equals(final Object obj) {
93         if (this == obj) {
94             return true;
95         }
96
97         if (obj == null) {
98             return false;
99         }
100
101         if (getClass() != obj.getClass()) {
102             return false;
103         }
104
105         final ActionEffectiveStatementImpl other = (ActionEffectiveStatementImpl) obj;
106         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
107     }
108
109     @Override
110     public String toString() {
111         return MoreObjects.toStringHelper(this).add("qname", getQName()).add("path", getPath()).add("input", input)
112                 .add("output", output).toString();
113     }
114 }