Mass migrate fields to use Immutable collections
[yangtools.git] / yang / yang-parser-rfc7950 / 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.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;
39
40 final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement>
41         implements ChoiceEffectiveStatement, ChoiceSchemaNode, DerivableSchemaNode {
42
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;
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, CaseSchemaNode> 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 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);
67             }
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);
75                 }
76             }
77         }
78
79         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
80         this.cases = ImmutableSortedMap.copyOfSorted(casesInit);
81
82         final Optional<String> defaultArg = findFirstEffectiveSubstatementArgument(DefaultEffectiveStatement.class);
83         if (defaultArg.isPresent()) {
84             final String arg = defaultArg.get();
85             final QName qname;
86             try {
87                 qname = QName.create(getQName(), arg);
88             } catch (IllegalArgumentException e) {
89                 throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
90                     arg, e);
91             }
92
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);
96         } else {
97             defaultCase = null;
98         }
99
100         mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
101                 .booleanValue();
102     }
103
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();
120         }
121     }
122
123     @Override
124     public Optional<ChoiceSchemaNode> getOriginal() {
125         return Optional.ofNullable(original);
126     }
127
128     @Override
129     public Set<AugmentationSchemaNode> getAvailableAugmentations() {
130         return augmentations;
131     }
132
133     @Override
134     public SortedMap<QName, CaseSchemaNode> getCases() {
135         return cases;
136     }
137
138     @Override
139     public Optional<CaseSchemaNode> getDefaultCase() {
140         return Optional.ofNullable(defaultCase);
141     }
142
143     @Override
144     public boolean isMandatory() {
145         return mandatory;
146     }
147
148     @Override
149     public int hashCode() {
150         final int prime = 31;
151         int result = 1;
152         result = prime * result + Objects.hashCode(getQName());
153         result = prime * result + Objects.hashCode(getPath());
154         return result;
155     }
156
157     @Override
158     public boolean equals(final Object obj) {
159         if (this == obj) {
160             return true;
161         }
162         if (obj == null) {
163             return false;
164         }
165         if (getClass() != obj.getClass()) {
166             return false;
167         }
168         final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
169         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
170     }
171
172     @Override
173     public String toString() {
174         return ChoiceEffectiveStatementImpl.class.getSimpleName() + "["
175                 + "qname=" + getQName()
176                 + "]";
177     }
178 }