2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
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;
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;
32 public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement> implements
33 ChoiceSchemaNode, DerivableSchemaNode {
35 * Comparator based on alphabetical order of local name of SchemaNode's
38 private static final Comparator<SchemaNode> SCHEMA_NODE_COMP = (o1, o2) -> o1.getQName().compareTo(o2.getQName());
40 private final ChoiceSchemaNode original;
41 private final String defaultCase;
43 private final Set<ChoiceCaseNode> cases;
44 private final Set<AugmentationSchema> augmentations;
46 public ChoiceEffectiveStatementImpl(
47 final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
49 this.original = ctx.getOriginalCtx() == null ? null : (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
51 final DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
52 this.defaultCase = (defaultStmt == null) ? null : defaultStmt.argument();
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);
59 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
60 if (effectiveStatement instanceof AugmentationSchema) {
61 final AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
62 augmentationsInit.add(augmentationSchema);
64 if (effectiveStatement instanceof ChoiceCaseNode) {
65 final ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
66 casesInit.add(choiceCaseNode);
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);
78 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
79 this.cases = ImmutableSet.copyOf(casesInit);
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;
102 public Optional<ChoiceSchemaNode> getOriginal() {
103 return Optional.fromNullable(original);
107 public Set<AugmentationSchema> getAvailableAugmentations() {
108 return augmentations;
112 public Set<ChoiceCaseNode> getCases() {
117 public ChoiceCaseNode getCaseNodeByName(final QName name) {
118 Preconditions.checkArgument(name != null, "Choice Case QName cannot be NULL!");
120 for (final ChoiceCaseNode caseNode : cases) {
121 if (caseNode != null && name.equals(caseNode.getQName())) {
129 public ChoiceCaseNode getCaseNodeByName(final String name) {
130 Preconditions.checkArgument(name != null, "Choice Case string Name cannot be NULL!");
132 for (final ChoiceCaseNode caseNode : cases) {
133 if (caseNode != null && (caseNode.getQName() != null) && name.equals(caseNode.getQName().getLocalName())) {
141 public String getDefaultCase() {
146 public int hashCode() {
147 final int prime = 31;
149 result = prime * result + Objects.hashCode(getQName());
150 result = prime * result + Objects.hashCode(getPath());
155 public boolean equals(final Object obj) {
162 if (getClass() != obj.getClass()) {
165 final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
166 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
170 public String toString() {
171 return ChoiceEffectiveStatementImpl.class.getSimpleName() + "[" +
172 "qname=" + getQName() +