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.collect.ImmutableList;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.LinkedList;
16 import java.util.List;
18 import java.util.SortedSet;
19 import java.util.TreeSet;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
36 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
39 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
41 public class ChoiceEffectiveStatementImpl extends
42 AbstractEffectiveDocumentedNode<QName, ChoiceStatement> implements
43 ChoiceSchemaNode, DerivableSchemaNode {
44 private final QName qname;
45 private final SchemaPath path;
49 ChoiceSchemaNode original;
50 boolean configuration = true;
51 ConstraintDefinition constraints;
54 ImmutableSet<ChoiceCaseNode> cases;
55 ImmutableSet<AugmentationSchema> augmentations;
56 ImmutableList<UnknownSchemaNode> unknownNodes;
58 public ChoiceEffectiveStatementImpl(
59 final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
62 this.qname = ctx.getStatementArgument();
63 this.path = Utils.getSchemaPath(ctx);
64 this.constraints = new EffectiveConstraintDefinitionImpl(this);
67 initSubstatementCollectionsAndFields();
70 private void initCopyType(
71 final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
73 List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
75 if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
78 if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
81 if (copyTypesFromOriginal
82 .contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
83 addedByUses = augmenting = true;
86 if (ctx.getOriginalCtx() != null) {
87 original = (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
91 private void initSubstatementCollectionsAndFields() {
92 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
94 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
95 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
96 SortedSet<ChoiceCaseNode> casesInit = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
98 boolean configurationInit = false;
99 boolean defaultInit = false;
100 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
101 if (effectiveStatement instanceof UnknownSchemaNode) {
102 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
103 unknownNodesInit.add(unknownNode);
105 if (effectiveStatement instanceof AugmentationSchema) {
106 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
107 augmentationsInit.add(augmentationSchema);
109 if (effectiveStatement instanceof ChoiceCaseNode) {
110 ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
111 casesInit.add(choiceCaseNode);
113 if (effectiveStatement instanceof AnyXmlSchemaNode
114 || effectiveStatement instanceof ContainerSchemaNode
115 || effectiveStatement instanceof ListSchemaNode
116 || effectiveStatement instanceof LeafListSchemaNode
117 || effectiveStatement instanceof LeafSchemaNode) {
119 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
120 ChoiceCaseNode shorthandCase = new CaseShorthandImpl(
122 casesInit.add(shorthandCase);
124 if (dataSchemaNode.isAugmenting() == true
125 && this.augmenting == false) {
126 resetAugmenting(dataSchemaNode);
129 if (!configurationInit
130 && effectiveStatement instanceof ConfigEffectiveStatementImpl) {
131 ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveStatement;
132 this.configuration = configStmt.argument();
133 configurationInit = true;
136 && effectiveStatement instanceof DefaultEffectiveStatementImpl) {
137 DefaultEffectiveStatementImpl defaultCaseStmt = (DefaultEffectiveStatementImpl) effectiveStatement;
138 this.defaultCase = defaultCaseStmt.argument();
143 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
144 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
145 this.cases = ImmutableSet.copyOf(casesInit);
148 private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
149 if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
150 LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
151 leaf.augmenting = false;
152 } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
153 ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
154 container.augmenting = false;
155 } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
156 LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
157 leafList.augmenting = false;
158 } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
159 ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
160 list.augmenting = false;
161 } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
162 AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
163 anyXml.augmenting = false;
168 public QName getQName() {
173 public SchemaPath getPath() {
178 public boolean isAugmenting() {
183 public boolean isAddedByUses() {
188 public Optional<ChoiceSchemaNode> getOriginal() {
189 return Optional.fromNullable(original);
193 public boolean isConfiguration() {
194 return configuration;
198 public ConstraintDefinition getConstraints() {
203 public Set<AugmentationSchema> getAvailableAugmentations() {
204 return augmentations;
208 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
213 public Set<ChoiceCaseNode> getCases() {
218 public ChoiceCaseNode getCaseNodeByName(final QName name) {
220 throw new IllegalArgumentException(
221 "Choice Case QName cannot be NULL!");
223 for (final ChoiceCaseNode caseNode : cases) {
224 if (caseNode != null && name.equals(caseNode.getQName())) {
232 public ChoiceCaseNode getCaseNodeByName(final String name) {
234 throw new IllegalArgumentException(
235 "Choice Case string Name cannot be NULL!");
237 for (final ChoiceCaseNode caseNode : cases) {
238 if (caseNode != null && (caseNode.getQName() != null)
239 && name.equals(caseNode.getQName().getLocalName())) {
247 public String getDefaultCase() {
252 public int hashCode() {
253 final int prime = 31;
255 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
256 result = prime * result + ((path == null) ? 0 : path.hashCode());
261 public boolean equals(final Object obj) {
268 if (getClass() != obj.getClass()) {
271 ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
273 if (other.qname != null) {
276 } else if (!qname.equals(other.qname)) {
280 if (other.path != null) {
283 } else if (!path.equals(other.path)) {
290 public String toString() {
291 StringBuilder sb = new StringBuilder(
292 ChoiceEffectiveStatementImpl.class.getSimpleName());
294 sb.append("qname=").append(qname);
296 return sb.toString();