Update ChoiceSchemaNode design
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / 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.stmt.rfc6020.effective;
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.ChoiceStatement;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangValidationBundles;
30
31 public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement> implements
32         ChoiceSchemaNode, DerivableSchemaNode {
33
34     private final Set<AugmentationSchemaNode> augmentations;
35     private final SortedMap<QName, ChoiceCaseNode> cases;
36     private final ChoiceCaseNode defaultCase;
37     private final ChoiceSchemaNode original;
38
39     public ChoiceEffectiveStatementImpl(
40             final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
41         super(ctx);
42         this.original = (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
43
44         // initSubstatementCollectionsAndFields
45         final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
46         final SortedMap<QName, ChoiceCaseNode> casesInit = new TreeMap<>();
47
48         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
49             if (effectiveStatement instanceof AugmentationSchemaNode) {
50                 final AugmentationSchemaNode augmentationSchema = (AugmentationSchemaNode) effectiveStatement;
51                 augmentationsInit.add(augmentationSchema);
52             }
53             if (effectiveStatement instanceof ChoiceCaseNode) {
54                 final ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
55                 // FIXME: we may be overwriting a previous entry, is that really okay?
56                 casesInit.put(choiceCaseNode.getQName(), choiceCaseNode);
57             }
58             if (YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(effectiveStatement.statementDefinition())) {
59                 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
60                 final ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode);
61                 // FIXME: we may be overwriting a previous entry, is that really okay?
62                 casesInit.put(shorthandCase.getQName(), shorthandCase);
63                 if (dataSchemaNode.isAugmenting() && !this.augmenting) {
64                     resetAugmenting(dataSchemaNode);
65                 }
66             }
67         }
68
69         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
70         this.cases = ImmutableSortedMap.copyOfSorted(casesInit);
71
72         final DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
73         if (defaultStmt != null) {
74             final QName qname;
75             try {
76                 qname = QName.create(getQName(), defaultStmt.argument());
77             } catch (IllegalArgumentException e) {
78                 throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
79                     defaultStmt.argument(), e);
80             }
81
82             // FIXME: this does not work with submodules, as they are
83             defaultCase = InferenceException.throwIfNull(cases.get(qname), ctx.getStatementSourceReference(),
84                 "Default statement refers to missing case %s", qname);
85         } else {
86             defaultCase = null;
87         }
88     }
89
90     private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
91         if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
92             final LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
93             leaf.augmenting = false;
94         } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
95             final ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
96             container.augmenting = false;
97         } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
98             final LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
99             leafList.augmenting = false;
100         } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
101             final ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
102             list.augmenting = false;
103         } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
104             final AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
105             anyXml.augmenting = false;
106         }
107     }
108
109     @Override
110     public Optional<ChoiceSchemaNode> getOriginal() {
111         return Optional.ofNullable(original);
112     }
113
114     @Override
115     public Set<AugmentationSchemaNode> getAvailableAugmentations() {
116         return augmentations;
117     }
118
119     @Override
120     public SortedMap<QName, ChoiceCaseNode> getCases() {
121         return cases;
122     }
123
124     @Override
125     public Optional<ChoiceCaseNode> getDefaultCase() {
126         return Optional.ofNullable(defaultCase);
127     }
128
129     @Override
130     public int hashCode() {
131         final int prime = 31;
132         int result = 1;
133         result = prime * result + Objects.hashCode(getQName());
134         result = prime * result + Objects.hashCode(getPath());
135         return result;
136     }
137
138     @Override
139     public boolean equals(final Object obj) {
140         if (this == obj) {
141             return true;
142         }
143         if (obj == null) {
144             return false;
145         }
146         if (getClass() != obj.getClass()) {
147             return false;
148         }
149         final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
150         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
151     }
152
153     @Override
154     public String toString() {
155         return ChoiceEffectiveStatementImpl.class.getSimpleName() + "["
156                 + "qname=" + getQName()
157                 + "]";
158     }
159 }