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.collect.ImmutableSet;
11 import com.google.common.collect.ImmutableSortedMap;
12 import java.util.LinkedHashSet;
13 import java.util.Objects;
14 import java.util.Optional;
16 import java.util.SortedMap;
17 import java.util.TreeMap;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangValidationBundles;
31 public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement> implements
32 ChoiceSchemaNode, DerivableSchemaNode {
34 private final Set<AugmentationSchemaNode> augmentations;
35 private final SortedMap<QName, ChoiceCaseNode> cases;
36 private final ChoiceCaseNode defaultCase;
37 private final ChoiceSchemaNode original;
39 public ChoiceEffectiveStatementImpl(
40 final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
42 this.original = (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
44 // initSubstatementCollectionsAndFields
45 final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
46 final SortedMap<QName, ChoiceCaseNode> casesInit = new TreeMap<>();
48 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
49 if (effectiveStatement instanceof AugmentationSchemaNode) {
50 final AugmentationSchemaNode augmentationSchema = (AugmentationSchemaNode) effectiveStatement;
51 augmentationsInit.add(augmentationSchema);
53 if (effectiveStatement instanceof ChoiceCaseNode) {
54 final ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
55 // FIXME: we may be overwriting a previous entry, is that really okay?
56 casesInit.put(choiceCaseNode.getQName(), choiceCaseNode);
58 if (YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(effectiveStatement.statementDefinition())) {
59 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
60 final ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode);
61 // FIXME: we may be overwriting a previous entry, is that really okay?
62 casesInit.put(shorthandCase.getQName(), shorthandCase);
63 if (dataSchemaNode.isAugmenting() && !this.augmenting) {
64 resetAugmenting(dataSchemaNode);
69 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
70 this.cases = ImmutableSortedMap.copyOfSorted(casesInit);
72 final DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
73 if (defaultStmt != null) {
76 qname = QName.create(getQName(), defaultStmt.argument());
77 } catch (IllegalArgumentException e) {
78 throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
79 defaultStmt.argument(), e);
82 // FIXME: this does not work with submodules, as they are
83 defaultCase = InferenceException.throwIfNull(cases.get(qname), ctx.getStatementSourceReference(),
84 "Default statement refers to missing case %s", qname);
90 private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
91 if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
92 final LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
93 leaf.augmenting = false;
94 } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
95 final ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
96 container.augmenting = false;
97 } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
98 final LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
99 leafList.augmenting = false;
100 } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
101 final ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
102 list.augmenting = false;
103 } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
104 final AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
105 anyXml.augmenting = false;
110 public Optional<ChoiceSchemaNode> getOriginal() {
111 return Optional.ofNullable(original);
115 public Set<AugmentationSchemaNode> getAvailableAugmentations() {
116 return augmentations;
120 public SortedMap<QName, ChoiceCaseNode> getCases() {
125 public Optional<ChoiceCaseNode> getDefaultCase() {
126 return Optional.ofNullable(defaultCase);
130 public int hashCode() {
131 final int prime = 31;
133 result = prime * result + Objects.hashCode(getQName());
134 result = prime * result + Objects.hashCode(getPath());
139 public boolean equals(final Object obj) {
146 if (getClass() != obj.getClass()) {
149 final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
150 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
154 public String toString() {
155 return ChoiceEffectiveStatementImpl.class.getSimpleName() + "["
156 + "qname=" + getQName()