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.stmt.rfc6020.effective;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.LinkedHashSet;
14 import java.util.Objects;
16 import java.util.SortedSet;
17 import java.util.TreeSet;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
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.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangValidationBundles;
29 public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement> implements
30 ChoiceSchemaNode, DerivableSchemaNode {
31 private final ChoiceSchemaNode original;
32 private final String defaultCase;
34 private final Set<ChoiceCaseNode> cases;
35 private final Set<AugmentationSchema> augmentations;
37 public ChoiceEffectiveStatementImpl(
38 final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
40 this.original = (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
42 final DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
43 this.defaultCase = defaultStmt == null ? null : defaultStmt.argument();
45 // initSubstatementCollectionsAndFields
46 final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
47 final SortedSet<ChoiceCaseNode> casesInit = new TreeSet<>((o1, o2) -> o1.getQName().compareTo(o2.getQName()));
49 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
50 if (effectiveStatement instanceof AugmentationSchema) {
51 final AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
52 augmentationsInit.add(augmentationSchema);
54 if (effectiveStatement instanceof ChoiceCaseNode) {
55 final ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
56 casesInit.add(choiceCaseNode);
58 if (YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(effectiveStatement.statementDefinition())) {
59 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
60 final ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode);
61 casesInit.add(shorthandCase);
62 if (dataSchemaNode.isAugmenting() && !this.augmenting) {
63 resetAugmenting(dataSchemaNode);
68 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
69 this.cases = ImmutableSet.copyOf(casesInit);
72 private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
73 if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
74 final LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
75 leaf.augmenting = false;
76 } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
77 final ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
78 container.augmenting = false;
79 } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
80 final LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
81 leafList.augmenting = false;
82 } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
83 final ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
84 list.augmenting = false;
85 } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
86 final AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
87 anyXml.augmenting = false;
92 public Optional<ChoiceSchemaNode> getOriginal() {
93 return Optional.fromNullable(original);
97 public Set<AugmentationSchema> getAvailableAugmentations() {
102 public Set<ChoiceCaseNode> getCases() {
107 public ChoiceCaseNode getCaseNodeByName(final QName name) {
108 Preconditions.checkArgument(name != null, "Choice Case QName cannot be NULL!");
110 for (final ChoiceCaseNode caseNode : cases) {
111 if (caseNode != null && name.equals(caseNode.getQName())) {
119 public ChoiceCaseNode getCaseNodeByName(final String name) {
120 Preconditions.checkArgument(name != null, "Choice Case string Name cannot be NULL!");
122 for (final ChoiceCaseNode caseNode : cases) {
123 if (caseNode != null && caseNode.getQName() != null && name.equals(caseNode.getQName().getLocalName())) {
131 public String getDefaultCase() {
136 public int hashCode() {
137 final int prime = 31;
139 result = prime * result + Objects.hashCode(getQName());
140 result = prime * result + Objects.hashCode(getPath());
145 public boolean equals(final Object obj) {
152 if (getClass() != obj.getClass()) {
155 final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
156 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
160 public String toString() {
161 return ChoiceEffectiveStatementImpl.class.getSimpleName() + "["
162 + "qname=" + getQName()