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.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.LinkedList;
17 import java.util.List;
18 import java.util.Objects;
20 import java.util.SortedSet;
21 import java.util.TreeSet;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
38 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
41 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
43 public class ChoiceEffectiveStatementImpl extends
44 AbstractEffectiveDocumentedNode<QName, ChoiceStatement> implements
45 ChoiceSchemaNode, DerivableSchemaNode {
46 private final QName qname;
47 private final SchemaPath path;
49 private boolean augmenting;
50 private boolean addedByUses;
51 private ChoiceSchemaNode original;
52 private boolean configuration = true;
53 private final ConstraintDefinition constraints;
54 private String defaultCase;
56 private ImmutableSet<ChoiceCaseNode> cases;
57 private ImmutableSet<AugmentationSchema> augmentations;
58 private ImmutableList<UnknownSchemaNode> unknownNodes;
60 public ChoiceEffectiveStatementImpl(
61 final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
64 this.qname = ctx.getStatementArgument();
65 this.path = Utils.getSchemaPath(ctx);
66 this.constraints = new EffectiveConstraintDefinitionImpl(this);
69 initSubstatementCollectionsAndFields();
72 private void initCopyType(
73 final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
75 List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
77 if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
80 if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
83 if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
84 addedByUses = augmenting = true;
87 if (ctx.getOriginalCtx() != null) {
88 original = (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
92 private void initSubstatementCollectionsAndFields() {
93 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
95 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
96 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
97 SortedSet<ChoiceCaseNode> casesInit = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
99 boolean configurationInit = false;
100 boolean defaultInit = false;
101 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
102 if (effectiveStatement instanceof UnknownSchemaNode) {
103 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
104 unknownNodesInit.add(unknownNode);
106 if (effectiveStatement instanceof AugmentationSchema) {
107 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
108 augmentationsInit.add(augmentationSchema);
110 if (effectiveStatement instanceof ChoiceCaseNode) {
111 ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
112 casesInit.add(choiceCaseNode);
114 if (effectiveStatement instanceof AnyXmlSchemaNode
115 || effectiveStatement instanceof ContainerSchemaNode
116 || effectiveStatement instanceof ListSchemaNode
117 || effectiveStatement instanceof LeafListSchemaNode
118 || effectiveStatement instanceof LeafSchemaNode) {
120 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
121 ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode);
122 casesInit.add(shorthandCase);
124 if (dataSchemaNode.isAugmenting() && !this.augmenting) {
125 resetAugmenting(dataSchemaNode);
128 if (!configurationInit && effectiveStatement instanceof ConfigEffectiveStatementImpl) {
129 ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveStatement;
130 this.configuration = configStmt.argument();
131 configurationInit = true;
133 if (!defaultInit && effectiveStatement instanceof DefaultEffectiveStatementImpl) {
134 DefaultEffectiveStatementImpl defaultCaseStmt = (DefaultEffectiveStatementImpl) effectiveStatement;
135 this.defaultCase = defaultCaseStmt.argument();
140 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
141 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
142 this.cases = ImmutableSet.copyOf(casesInit);
145 private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
146 if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
147 LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
148 leaf.augmenting = false;
149 } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
150 ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
151 container.augmenting = false;
152 } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
153 LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
154 leafList.augmenting = false;
155 } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
156 ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
157 list.augmenting = false;
158 } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
159 AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
160 anyXml.augmenting = false;
165 public QName getQName() {
170 public SchemaPath getPath() {
175 public boolean isAugmenting() {
180 public boolean isAddedByUses() {
185 public Optional<ChoiceSchemaNode> getOriginal() {
186 return Optional.fromNullable(original);
190 public boolean isConfiguration() {
191 return configuration;
195 public ConstraintDefinition getConstraints() {
200 public Set<AugmentationSchema> getAvailableAugmentations() {
201 return augmentations;
205 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
210 public Set<ChoiceCaseNode> getCases() {
215 public ChoiceCaseNode getCaseNodeByName(final QName name) {
216 Preconditions.checkArgument(name != null, "Choice Case QName cannot be NULL!");
218 for (final ChoiceCaseNode caseNode : cases) {
219 if (caseNode != null && name.equals(caseNode.getQName())) {
227 public ChoiceCaseNode getCaseNodeByName(final String name) {
228 Preconditions.checkArgument(name != null, "Choice Case string Name cannot be NULL!");
230 for (final ChoiceCaseNode caseNode : cases) {
231 if (caseNode != null && (caseNode.getQName() != null)
232 && name.equals(caseNode.getQName().getLocalName())) {
240 public String getDefaultCase() {
245 public int hashCode() {
246 final int prime = 31;
248 result = prime * result + Objects.hashCode(qname);
249 result = prime * result + Objects.hashCode(path);
254 public boolean equals(final Object obj) {
261 if (getClass() != obj.getClass()) {
264 ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
265 return Objects.equals(qname, other.qname) && Objects.equals(path, other.path);
269 public String toString() {
270 StringBuilder sb = new StringBuilder(ChoiceEffectiveStatementImpl.class.getSimpleName());
272 sb.append("qname=").append(qname);
274 return sb.toString();