73a4bf0d712dab8e83196dd48762f18303796d1e
[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> implements ActionDefinition {
32     private final ContainerSchemaNode input;
33     private final ContainerSchemaNode output;
34     private final Set<TypeDefinition<?>> typeDefinitions;
35     private final Set<GroupingDefinition> groupings;
36
37     public ActionEffectiveStatementImpl(
38             final StmtContext<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> 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 TypeDefEffectiveStatementImpl) {
52                 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) 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         return Objects.hash(getQName(), getPath());
88     }
89
90     @Override
91     public boolean equals(final Object obj) {
92         if (this == obj) {
93             return true;
94         }
95
96         if (obj == null) {
97             return false;
98         }
99
100         if (getClass() != obj.getClass()) {
101             return false;
102         }
103
104         final ActionEffectiveStatementImpl other = (ActionEffectiveStatementImpl) obj;
105         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
106     }
107
108     @Override
109     public String toString() {
110         return MoreObjects.toStringHelper(this).add("qname", getQName()).add("path", getPath()).add("input", input)
111                 .add("output", output).toString();
112     }
113 }