BUG-6497: Do not lose augmentation statement order
[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.base.Preconditions;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Collection;
14 import java.util.LinkedHashSet;
15 import java.util.Objects;
16 import java.util.Set;
17 import java.util.SortedSet;
18 import java.util.TreeSet;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
32 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
34
35 public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement> implements
36         ChoiceSchemaNode, DerivableSchemaNode {
37
38     private final ChoiceSchemaNode original;
39     private final String defaultCase;
40
41     private final Set<ChoiceCaseNode> cases;
42     private final Set<AugmentationSchema> augmentations;
43
44     public ChoiceEffectiveStatementImpl(
45             final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
46         super(ctx);
47         this.original = ctx.getOriginalCtx() == null ? null : (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
48
49         DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
50         this.defaultCase = (defaultStmt == null) ? null : defaultStmt.argument();
51
52         // initSubstatementCollectionsAndFields
53         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
54         Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
55         SortedSet<ChoiceCaseNode> casesInit = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
56
57         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
58             if (effectiveStatement instanceof AugmentationSchema) {
59                 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
60                 augmentationsInit.add(augmentationSchema);
61             }
62             if (effectiveStatement instanceof ChoiceCaseNode) {
63                 ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
64                 casesInit.add(choiceCaseNode);
65             }
66             if (effectiveStatement instanceof AnyXmlSchemaNode || effectiveStatement instanceof ContainerSchemaNode
67                     || effectiveStatement instanceof ListSchemaNode || effectiveStatement instanceof LeafListSchemaNode
68                     || effectiveStatement instanceof LeafSchemaNode) {
69
70                 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
71                 ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode);
72                 casesInit.add(shorthandCase);
73
74                 if (dataSchemaNode.isAugmenting() && !this.augmenting) {
75                     resetAugmenting(dataSchemaNode);
76                 }
77             }
78         }
79
80         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
81         this.cases = ImmutableSet.copyOf(casesInit);
82     }
83
84     private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
85         if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
86             LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
87             leaf.augmenting = false;
88         } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
89             ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
90             container.augmenting = false;
91         } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
92             LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
93             leafList.augmenting = false;
94         } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
95             ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
96             list.augmenting = false;
97         } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
98             AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
99             anyXml.augmenting = false;
100         }
101     }
102
103     @Override
104     public Optional<ChoiceSchemaNode> getOriginal() {
105         return Optional.fromNullable(original);
106     }
107
108     @Override
109     public Set<AugmentationSchema> getAvailableAugmentations() {
110         return augmentations;
111     }
112
113     @Override
114     public Set<ChoiceCaseNode> getCases() {
115         return cases;
116     }
117
118     @Override
119     public ChoiceCaseNode getCaseNodeByName(final QName name) {
120         Preconditions.checkArgument(name != null, "Choice Case QName cannot be NULL!");
121
122         for (final ChoiceCaseNode caseNode : cases) {
123             if (caseNode != null && name.equals(caseNode.getQName())) {
124                 return caseNode;
125             }
126         }
127         return null;
128     }
129
130     @Override
131     public ChoiceCaseNode getCaseNodeByName(final String name) {
132         Preconditions.checkArgument(name != null, "Choice Case string Name cannot be NULL!");
133
134         for (final ChoiceCaseNode caseNode : cases) {
135             if (caseNode != null && (caseNode.getQName() != null) && name.equals(caseNode.getQName().getLocalName())) {
136                 return caseNode;
137             }
138         }
139         return null;
140     }
141
142     @Override
143     public String getDefaultCase() {
144         return defaultCase;
145     }
146
147     @Override
148     public int hashCode() {
149         final int prime = 31;
150         int result = 1;
151         result = prime * result + Objects.hashCode(getQName());
152         result = prime * result + Objects.hashCode(getPath());
153         return result;
154     }
155
156     @Override
157     public boolean equals(final Object obj) {
158         if (this == obj) {
159             return true;
160         }
161         if (obj == null) {
162             return false;
163         }
164         if (getClass() != obj.getClass()) {
165             return false;
166         }
167         ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
168         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
169     }
170
171     @Override
172     public String toString() {
173         StringBuilder sb = new StringBuilder(ChoiceEffectiveStatementImpl.class.getSimpleName());
174         sb.append("[");
175         sb.append("qname=").append(getQName());
176         sb.append("]");
177         return sb.toString();
178     }
179 }