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.rfc7950.stmt.choice;
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.CaseSchemaNode;
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.ChoiceEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBundles;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDataSchemaNode;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.anyxml.AnyxmlEffectiveStatementImpl;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.container.ContainerEffectiveStatementImpl;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.leaf.LeafEffectiveStatementImpl;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.leaf_list.LeafListEffectiveStatementImpl;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.list.ListEffectiveStatementImpl;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
38 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
40 final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement>
41 implements ChoiceEffectiveStatement, ChoiceSchemaNode, DerivableSchemaNode {
43 private final ImmutableSet<AugmentationSchemaNode> augmentations;
44 private final ImmutableSortedMap<QName, CaseSchemaNode> cases;
45 private final CaseSchemaNode defaultCase;
46 private final ChoiceSchemaNode original;
47 private final boolean mandatory;
49 ChoiceEffectiveStatementImpl(
50 final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
52 this.original = (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
54 // initSubstatementCollectionsAndFields
55 final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
56 final SortedMap<QName, CaseSchemaNode> casesInit = new TreeMap<>();
58 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
59 if (effectiveStatement instanceof AugmentationSchemaNode) {
60 final AugmentationSchemaNode augmentationSchema = (AugmentationSchemaNode) effectiveStatement;
61 augmentationsInit.add(augmentationSchema);
63 if (effectiveStatement instanceof CaseSchemaNode) {
64 final CaseSchemaNode choiceCaseNode = (CaseSchemaNode) effectiveStatement;
65 // FIXME: we may be overwriting a previous entry, is that really okay?
66 casesInit.put(choiceCaseNode.getQName(), choiceCaseNode);
68 if (YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(effectiveStatement.statementDefinition())) {
69 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
70 final CaseSchemaNode shorthandCase = new ImplicitCaseSchemaNode(dataSchemaNode);
71 // FIXME: we may be overwriting a previous entry, is that really okay?
72 casesInit.put(shorthandCase.getQName(), shorthandCase);
73 if (dataSchemaNode.isAugmenting() && !isAugmenting()) {
74 resetAugmenting(dataSchemaNode);
79 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
80 this.cases = ImmutableSortedMap.copyOfSorted(casesInit);
82 final Optional<String> defaultArg = findFirstEffectiveSubstatementArgument(DefaultEffectiveStatement.class);
83 if (defaultArg.isPresent()) {
84 final String arg = defaultArg.get();
87 qname = QName.create(getQName(), arg);
88 } catch (IllegalArgumentException e) {
89 throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
93 // FIXME: this does not work with submodules, as they are
94 defaultCase = InferenceException.throwIfNull(cases.get(qname), ctx.getStatementSourceReference(),
95 "Default statement refers to missing case %s", qname);
100 mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
104 private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
105 if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
106 final LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
107 leaf.resetAugmenting();
108 } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
109 final ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
110 container.resetAugmenting();
111 } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
112 final LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
113 leafList.resetAugmenting();
114 } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
115 final ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
116 list.resetAugmenting();
117 } else if (dataSchemaNode instanceof AnyxmlEffectiveStatementImpl) {
118 final AnyxmlEffectiveStatementImpl anyXml = (AnyxmlEffectiveStatementImpl) dataSchemaNode;
119 anyXml.resetAugmenting();
124 public Optional<ChoiceSchemaNode> getOriginal() {
125 return Optional.ofNullable(original);
129 public Set<AugmentationSchemaNode> getAvailableAugmentations() {
130 return augmentations;
134 public SortedMap<QName, CaseSchemaNode> getCases() {
139 public Optional<CaseSchemaNode> getDefaultCase() {
140 return Optional.ofNullable(defaultCase);
144 public boolean isMandatory() {
149 public int hashCode() {
150 final int prime = 31;
152 result = prime * result + Objects.hashCode(getQName());
153 result = prime * result + Objects.hashCode(getPath());
158 public boolean equals(final Object obj) {
165 if (getClass() != obj.getClass()) {
168 final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
169 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
173 public String toString() {
174 return ChoiceEffectiveStatementImpl.class.getSimpleName() + "["
175 + "qname=" + getQName()