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.Comparator;
14 import java.util.LinkedHashSet;
15 import java.util.Objects;
17 import java.util.SortedSet;
18 import java.util.TreeSet;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangValidationBundles;
31 public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement> implements
32 ChoiceSchemaNode, DerivableSchemaNode {
34 * Comparator based on alphabetical order of local name of SchemaNode's
37 private static final Comparator<SchemaNode> SCHEMA_NODE_COMP = (o1, o2) -> o1.getQName().compareTo(o2.getQName());
39 private final ChoiceSchemaNode original;
40 private final String defaultCase;
42 private final Set<ChoiceCaseNode> cases;
43 private final Set<AugmentationSchema> augmentations;
45 public ChoiceEffectiveStatementImpl(
46 final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
48 this.original = ctx.getOriginalCtx() == null ? null : (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
50 final DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
51 this.defaultCase = defaultStmt == null ? null : defaultStmt.argument();
53 // initSubstatementCollectionsAndFields
54 final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
55 final SortedSet<ChoiceCaseNode> casesInit = new TreeSet<>(SCHEMA_NODE_COMP);
57 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
58 if (effectiveStatement instanceof AugmentationSchema) {
59 final AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
60 augmentationsInit.add(augmentationSchema);
62 if (effectiveStatement instanceof ChoiceCaseNode) {
63 final ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
64 casesInit.add(choiceCaseNode);
66 if (YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(effectiveStatement.statementDefinition())) {
67 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
68 final ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode);
69 casesInit.add(shorthandCase);
70 if (dataSchemaNode.isAugmenting() && !this.augmenting) {
71 resetAugmenting(dataSchemaNode);
76 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
77 this.cases = ImmutableSet.copyOf(casesInit);
80 private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
81 if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
82 final LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
83 leaf.augmenting = false;
84 } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
85 final ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
86 container.augmenting = false;
87 } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
88 final LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
89 leafList.augmenting = false;
90 } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
91 final ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
92 list.augmenting = false;
93 } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
94 final AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
95 anyXml.augmenting = false;
100 public Optional<ChoiceSchemaNode> getOriginal() {
101 return Optional.fromNullable(original);
105 public Set<AugmentationSchema> getAvailableAugmentations() {
106 return augmentations;
110 public Set<ChoiceCaseNode> getCases() {
115 public ChoiceCaseNode getCaseNodeByName(final QName name) {
116 Preconditions.checkArgument(name != null, "Choice Case QName cannot be NULL!");
118 for (final ChoiceCaseNode caseNode : cases) {
119 if (caseNode != null && name.equals(caseNode.getQName())) {
127 public ChoiceCaseNode getCaseNodeByName(final String name) {
128 Preconditions.checkArgument(name != null, "Choice Case string Name cannot be NULL!");
130 for (final ChoiceCaseNode caseNode : cases) {
131 if (caseNode != null && caseNode.getQName() != null && name.equals(caseNode.getQName().getLocalName())) {
139 public String getDefaultCase() {
144 public int hashCode() {
145 final int prime = 31;
147 result = prime * result + Objects.hashCode(getQName());
148 result = prime * result + Objects.hashCode(getPath());
153 public boolean equals(final Object obj) {
160 if (getClass() != obj.getClass()) {
163 final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
164 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
168 public String toString() {
169 return ChoiceEffectiveStatementImpl.class.getSimpleName() + "[" +
170 "qname=" + getQName() +