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.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.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.stmt.AbstractEffectiveDataSchemaNode;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.anyxml.AnyxmlEffectiveStatementImpl;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.container.ContainerEffectiveStatementImpl;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.leaf.LeafEffectiveStatementImpl;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.leaf_list.LeafListEffectiveStatementImpl;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.list.ListEffectiveStatementImpl;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
37 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
38 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangValidationBundles;
40 final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement>
41 implements ChoiceEffectiveStatement, ChoiceSchemaNode, DerivableSchemaNode {
43 private final Set<AugmentationSchemaNode> augmentations;
44 private final SortedMap<QName, ChoiceCaseNode> cases;
45 private final ChoiceCaseNode 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, ChoiceCaseNode> 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 ChoiceCaseNode) {
64 final ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) 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 ChoiceCaseNode shorthandCase = new ImplicitChoiceCaseNode(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 DefaultEffectiveStatement defaultStmt = firstEffective(DefaultEffectiveStatement.class);
83 if (defaultStmt != null) {
86 qname = QName.create(getQName(), defaultStmt.argument());
87 } catch (IllegalArgumentException e) {
88 throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
89 defaultStmt.argument(), e);
92 // FIXME: this does not work with submodules, as they are
93 defaultCase = InferenceException.throwIfNull(cases.get(qname), ctx.getStatementSourceReference(),
94 "Default statement refers to missing case %s", qname);
99 final MandatoryEffectiveStatement mandatoryStmt = firstEffective(MandatoryEffectiveStatement.class);
100 mandatory = mandatoryStmt == null ? false : mandatoryStmt.argument().booleanValue();
103 private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
104 if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
105 final LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
106 leaf.resetAugmenting();
107 } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
108 final ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
109 container.resetAugmenting();
110 } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
111 final LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
112 leafList.resetAugmenting();
113 } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
114 final ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
115 list.resetAugmenting();
116 } else if (dataSchemaNode instanceof AnyxmlEffectiveStatementImpl) {
117 final AnyxmlEffectiveStatementImpl anyXml = (AnyxmlEffectiveStatementImpl) dataSchemaNode;
118 anyXml.resetAugmenting();
123 public Optional<ChoiceSchemaNode> getOriginal() {
124 return Optional.ofNullable(original);
128 public Set<AugmentationSchemaNode> getAvailableAugmentations() {
129 return augmentations;
133 public SortedMap<QName, ChoiceCaseNode> getCases() {
138 public Optional<ChoiceCaseNode> getDefaultCase() {
139 return Optional.ofNullable(defaultCase);
143 public boolean isMandatory() {
148 public int hashCode() {
149 final int prime = 31;
151 result = prime * result + Objects.hashCode(getQName());
152 result = prime * result + Objects.hashCode(getPath());
157 public boolean equals(final Object obj) {
164 if (getClass() != obj.getClass()) {
167 final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
168 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
172 public String toString() {
173 return ChoiceEffectiveStatementImpl.class.getSimpleName() + "["
174 + "qname=" + getQName()