Introduce ElementCountConstraintAware interface
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / choice / ChoiceEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.choice;
9
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;
15 import java.util.Set;
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;
39
40 final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement>
41         implements ChoiceEffectiveStatement, ChoiceSchemaNode, DerivableSchemaNode {
42
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;
48
49     ChoiceEffectiveStatementImpl(
50             final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
51         super(ctx);
52         this.original = (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
53
54         // initSubstatementCollectionsAndFields
55         final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
56         final SortedMap<QName, ChoiceCaseNode> casesInit = new TreeMap<>();
57
58         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
59             if (effectiveStatement instanceof AugmentationSchemaNode) {
60                 final AugmentationSchemaNode augmentationSchema = (AugmentationSchemaNode) effectiveStatement;
61                 augmentationsInit.add(augmentationSchema);
62             }
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);
67             }
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);
75                 }
76             }
77         }
78
79         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
80         this.cases = ImmutableSortedMap.copyOfSorted(casesInit);
81
82         final DefaultEffectiveStatement defaultStmt = firstEffective(DefaultEffectiveStatement.class);
83         if (defaultStmt != null) {
84             final QName qname;
85             try {
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);
90             }
91
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);
95         } else {
96             defaultCase = null;
97         }
98
99         final MandatoryEffectiveStatement mandatoryStmt = firstEffective(MandatoryEffectiveStatement.class);
100         mandatory = mandatoryStmt == null ? false : mandatoryStmt.argument().booleanValue();
101     }
102
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();
119         }
120     }
121
122     @Override
123     public Optional<ChoiceSchemaNode> getOriginal() {
124         return Optional.ofNullable(original);
125     }
126
127     @Override
128     public Set<AugmentationSchemaNode> getAvailableAugmentations() {
129         return augmentations;
130     }
131
132     @Override
133     public SortedMap<QName, ChoiceCaseNode> getCases() {
134         return cases;
135     }
136
137     @Override
138     public Optional<ChoiceCaseNode> getDefaultCase() {
139         return Optional.ofNullable(defaultCase);
140     }
141
142     @Override
143     public boolean isMandatory() {
144         return mandatory;
145     }
146
147     @Override
148     public int hashCode() {
149         final int prime = 31;
150         int result = 1;
151         result = prime * result + Objects.hashCode(getQName());
152         result = prime * result + Objects.hashCode(getPath());
153         return result;
154     }
155
156     @Override
157     public boolean equals(final Object obj) {
158         if (this == obj) {
159             return true;
160         }
161         if (obj == null) {
162             return false;
163         }
164         if (getClass() != obj.getClass()) {
165             return false;
166         }
167         final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
168         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
169     }
170
171     @Override
172     public String toString() {
173         return ChoiceEffectiveStatementImpl.class.getSimpleName() + "["
174                 + "qname=" + getQName()
175                 + "]";
176     }
177 }