Deprecate CopyableNode at al.
[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.augmenting = true;
75             this.addedByUses = true;
76         } else {
77             this.augmenting = copyTypesFromOriginal.contains(CopyType.ADDED_BY_AUGMENTATION);
78             this.addedByUses = copyTypesFromOriginal.contains(CopyType.ADDED_BY_USES);
79         }
80     }
81
82     private static ContainerSchemaNode findAsContainer(final EffectiveStatement<?, ?> parent,
83             final Class<? extends EffectiveStatement<QName, ?>> statementType) {
84         final EffectiveStatement<?, ?> statement = parent.findFirstEffectiveSubstatement(statementType).get();
85         Verify.verify(statement instanceof ContainerSchemaNode, "Child statement %s is not a ContainerSchemaNode");
86         return (ContainerSchemaNode) statement;
87     }
88
89     @Override
90     public ContainerSchemaNode getInput() {
91         return input;
92     }
93
94     @Override
95     public ContainerSchemaNode getOutput() {
96         return output;
97     }
98
99     @Override
100     public Set<TypeDefinition<?>> getTypeDefinitions() {
101         return typeDefinitions;
102     }
103
104     @Override
105     public Set<GroupingDefinition> getGroupings() {
106         return groupings;
107     }
108
109     @Deprecated
110     @Override
111     public boolean isAugmenting() {
112         return augmenting;
113     }
114
115     @Deprecated
116     @Override
117     public boolean isAddedByUses() {
118         return addedByUses;
119     }
120
121     @Override
122     public int hashCode() {
123         return Objects.hash(getQName(), getPath());
124     }
125
126     @Override
127     public boolean equals(final Object obj) {
128         if (this == obj) {
129             return true;
130         }
131
132         if (obj == null) {
133             return false;
134         }
135
136         if (getClass() != obj.getClass()) {
137             return false;
138         }
139
140         final ActionEffectiveStatementImpl other = (ActionEffectiveStatementImpl) obj;
141         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
142     }
143
144     @Override
145     public String toString() {
146         return MoreObjects.toStringHelper(this).add("qname", getQName()).add("path", getPath()).add("input", input)
147                 .add("output", output).toString();
148     }
149 }