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