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