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.Collection;
13 import java.util.LinkedHashSet;
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.DerivableSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDataSchemaNode;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement>
34 implements ChoiceEffectiveStatement, ChoiceSchemaNode, DerivableSchemaNode {
36 private final ImmutableSet<AugmentationSchemaNode> augmentations;
37 private final ImmutableSortedMap<QName, CaseSchemaNode> cases;
38 private final CaseSchemaNode defaultCase;
39 private final ChoiceSchemaNode original;
40 private final boolean mandatory;
42 ChoiceEffectiveStatementImpl(final StmtContext<QName, ChoiceStatement, ChoiceEffectiveStatement> ctx) {
44 this.original = (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
46 // initSubstatementCollectionsAndFields
47 final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
48 final SortedMap<QName, CaseSchemaNode> casesInit = new TreeMap<>();
50 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
51 if (effectiveStatement instanceof AugmentationSchemaNode) {
52 final AugmentationSchemaNode augmentationSchema = (AugmentationSchemaNode) effectiveStatement;
53 augmentationsInit.add(augmentationSchema);
55 if (effectiveStatement instanceof CaseSchemaNode) {
56 final CaseSchemaNode choiceCaseNode = (CaseSchemaNode) effectiveStatement;
57 // FIXME: we may be overwriting a previous entry, is that really okay?
58 casesInit.put(choiceCaseNode.getQName(), choiceCaseNode);
62 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
63 this.cases = ImmutableSortedMap.copyOfSorted(casesInit);
65 final Optional<String> defaultArg = findFirstEffectiveSubstatementArgument(DefaultEffectiveStatement.class);
66 if (defaultArg.isPresent()) {
67 final String arg = defaultArg.get();
70 qname = QName.create(getQName(), arg);
71 } catch (IllegalArgumentException e) {
72 throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
76 // FIXME: this does not work with submodules, as they are
77 defaultCase = InferenceException.throwIfNull(cases.get(qname), ctx.getStatementSourceReference(),
78 "Default statement refers to missing case %s", qname);
83 mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
88 public Optional<ChoiceSchemaNode> getOriginal() {
89 return Optional.ofNullable(original);
93 public Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
98 public SortedMap<QName, CaseSchemaNode> getCases() {
103 public Optional<CaseSchemaNode> getDefaultCase() {
104 return Optional.ofNullable(defaultCase);
108 public boolean isMandatory() {
113 public String toString() {
114 return ChoiceEffectiveStatementImpl.class.getSimpleName() + "[" + "qname=" + getQName() + "]";