Cleanup yang-parser-impl
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ChoiceEffectiveStatementImpl.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.base.Optional;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.LinkedList;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.SortedSet;
19 import java.util.TreeSet;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
36 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
39 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
40
41 public class ChoiceEffectiveStatementImpl extends
42         AbstractEffectiveDocumentedNode<QName, ChoiceStatement> implements
43         ChoiceSchemaNode, DerivableSchemaNode {
44     private final QName qname;
45     private final SchemaPath path;
46
47     boolean augmenting;
48     boolean addedByUses;
49     ChoiceSchemaNode original;
50     boolean configuration = true;
51     ConstraintDefinition constraints;
52     String defaultCase;
53
54     ImmutableSet<ChoiceCaseNode> cases;
55     ImmutableSet<AugmentationSchema> augmentations;
56     ImmutableList<UnknownSchemaNode> unknownNodes;
57
58     public ChoiceEffectiveStatementImpl(
59             final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
60         super(ctx);
61
62         this.qname = ctx.getStatementArgument();
63         this.path = Utils.getSchemaPath(ctx);
64         this.constraints = new EffectiveConstraintDefinitionImpl(this);
65
66         initCopyType(ctx);
67         initSubstatementCollectionsAndFields();
68     }
69
70     private void initCopyType(
71             final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
72
73         List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
74
75         if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
76             augmenting = true;
77         }
78         if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
79             addedByUses = true;
80         }
81         if (copyTypesFromOriginal
82                 .contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
83             addedByUses = augmenting = true;
84         }
85
86         if (ctx.getOriginalCtx() != null) {
87             original = (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
88         }
89     }
90
91     private void initSubstatementCollectionsAndFields() {
92         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
93
94         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
95         Set<AugmentationSchema> augmentationsInit = new HashSet<>();
96         SortedSet<ChoiceCaseNode> casesInit = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
97
98         boolean configurationInit = false;
99         boolean defaultInit = false;
100         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
101             if (effectiveStatement instanceof UnknownSchemaNode) {
102                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
103                 unknownNodesInit.add(unknownNode);
104             }
105             if (effectiveStatement instanceof AugmentationSchema) {
106                 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
107                 augmentationsInit.add(augmentationSchema);
108             }
109             if (effectiveStatement instanceof ChoiceCaseNode) {
110                 ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
111                 casesInit.add(choiceCaseNode);
112             }
113             if (effectiveStatement instanceof AnyXmlSchemaNode
114                     || effectiveStatement instanceof ContainerSchemaNode
115                     || effectiveStatement instanceof ListSchemaNode
116                     || effectiveStatement instanceof LeafListSchemaNode
117                     || effectiveStatement instanceof LeafSchemaNode) {
118
119                 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
120                 ChoiceCaseNode shorthandCase = new CaseShorthandImpl(
121                         dataSchemaNode);
122                 casesInit.add(shorthandCase);
123
124                 if (dataSchemaNode.isAugmenting() == true
125                         && this.augmenting == false) {
126                     resetAugmenting(dataSchemaNode);
127                 }
128             }
129             if (!configurationInit
130                     && effectiveStatement instanceof ConfigEffectiveStatementImpl) {
131                 ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveStatement;
132                 this.configuration = configStmt.argument();
133                 configurationInit = true;
134             }
135             if (!defaultInit
136                     && effectiveStatement instanceof DefaultEffectiveStatementImpl) {
137                 DefaultEffectiveStatementImpl defaultCaseStmt = (DefaultEffectiveStatementImpl) effectiveStatement;
138                 this.defaultCase = defaultCaseStmt.argument();
139                 defaultInit = true;
140             }
141         }
142
143         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
144         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
145         this.cases = ImmutableSet.copyOf(casesInit);
146     }
147
148     private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
149         if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
150             LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
151             leaf.augmenting = false;
152         } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
153             ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
154             container.augmenting = false;
155         } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
156             LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
157             leafList.augmenting = false;
158         } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
159             ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
160             list.augmenting = false;
161         } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
162             AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
163             anyXml.augmenting = false;
164         }
165     }
166
167     @Override
168     public QName getQName() {
169         return qname;
170     }
171
172     @Override
173     public SchemaPath getPath() {
174         return path;
175     }
176
177     @Override
178     public boolean isAugmenting() {
179         return augmenting;
180     }
181
182     @Override
183     public boolean isAddedByUses() {
184         return addedByUses;
185     }
186
187     @Override
188     public Optional<ChoiceSchemaNode> getOriginal() {
189         return Optional.fromNullable(original);
190     }
191
192     @Override
193     public boolean isConfiguration() {
194         return configuration;
195     }
196
197     @Override
198     public ConstraintDefinition getConstraints() {
199         return constraints;
200     }
201
202     @Override
203     public Set<AugmentationSchema> getAvailableAugmentations() {
204         return augmentations;
205     }
206
207     @Override
208     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
209         return unknownNodes;
210     }
211
212     @Override
213     public Set<ChoiceCaseNode> getCases() {
214         return cases;
215     }
216
217     @Override
218     public ChoiceCaseNode getCaseNodeByName(final QName name) {
219         if (name == null) {
220             throw new IllegalArgumentException(
221                     "Choice Case QName cannot be NULL!");
222         }
223         for (final ChoiceCaseNode caseNode : cases) {
224             if (caseNode != null && name.equals(caseNode.getQName())) {
225                 return caseNode;
226             }
227         }
228         return null;
229     }
230
231     @Override
232     public ChoiceCaseNode getCaseNodeByName(final String name) {
233         if (name == null) {
234             throw new IllegalArgumentException(
235                     "Choice Case string Name cannot be NULL!");
236         }
237         for (final ChoiceCaseNode caseNode : cases) {
238             if (caseNode != null && (caseNode.getQName() != null)
239                     && name.equals(caseNode.getQName().getLocalName())) {
240                 return caseNode;
241             }
242         }
243         return null;
244     }
245
246     @Override
247     public String getDefaultCase() {
248         return defaultCase;
249     }
250
251     @Override
252     public int hashCode() {
253         final int prime = 31;
254         int result = 1;
255         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
256         result = prime * result + ((path == null) ? 0 : path.hashCode());
257         return result;
258     }
259
260     @Override
261     public boolean equals(final Object obj) {
262         if (this == obj) {
263             return true;
264         }
265         if (obj == null) {
266             return false;
267         }
268         if (getClass() != obj.getClass()) {
269             return false;
270         }
271         ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
272         if (qname == null) {
273             if (other.qname != null) {
274                 return false;
275             }
276         } else if (!qname.equals(other.qname)) {
277             return false;
278         }
279         if (path == null) {
280             if (other.path != null) {
281                 return false;
282             }
283         } else if (!path.equals(other.path)) {
284             return false;
285         }
286         return true;
287     }
288
289     @Override
290     public String toString() {
291         StringBuilder sb = new StringBuilder(
292                 ChoiceEffectiveStatementImpl.class.getSimpleName());
293         sb.append("[");
294         sb.append("qname=").append(qname);
295         sb.append("]");
296         return sb.toString();
297     }
298
299 }