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