-/**
+/*
* Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
*/
package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
-
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.LinkedList;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
-import java.util.List;
+import com.google.common.collect.ImmutableSortedMap;
+import java.util.LinkedHashSet;
+import java.util.Objects;
+import java.util.Optional;
import java.util.Set;
+import java.util.SortedMap;
+import java.util.TreeMap;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
-
-public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, ChoiceStatement> implements ChoiceSchemaNode, DerivableSchemaNode {
- private final QName qname;
- private final SchemaPath path;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
+import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangValidationBundles;
- boolean augmenting;
- boolean addedByUses;
- ChoiceSchemaNode original;
- boolean configuration;
- ConstraintDefinition constraints;
- String defaultCase;
+public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement> implements
+ ChoiceSchemaNode, DerivableSchemaNode {
- ImmutableSet<ChoiceCaseNode> cases;
- ImmutableSet<AugmentationSchema> augmentations;
- ImmutableList<UnknownSchemaNode> unknownNodes;
+ private final Set<AugmentationSchemaNode> augmentations;
+ private final SortedMap<QName, ChoiceCaseNode> cases;
+ private final ChoiceCaseNode defaultCase;
+ private final ChoiceSchemaNode original;
+ private final boolean mandatory;
- public ChoiceEffectiveStatementImpl(StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
+ public ChoiceEffectiveStatementImpl(
+ final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
super(ctx);
+ this.original = (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
- this.qname = ctx.getStatementArgument();
- this.path = Utils.getSchemaPath(ctx);
- //:TODO init other fields
-
- initSubstatementCollections();
- initCopyType(ctx);
- }
-
- private void initCopyType(
- StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
-
- TypeOfCopy typeOfCopy = ctx.getTypeOfCopy();
- switch (typeOfCopy) {
- case ADDED_BY_AUGMENTATION:
- augmenting = true;
- original = (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
- break;
- case ADDED_BY_USES:
- addedByUses = true;
- original = (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
- break;
- default:
- break;
- }
- }
-
- private void initSubstatementCollections() {
- Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
+ // initSubstatementCollectionsAndFields
+ final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
+ final SortedMap<QName, ChoiceCaseNode> casesInit = new TreeMap<>();
- List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
- Set<AugmentationSchema> augmentationsInit = new HashSet<>();
- Set<ChoiceCaseNode> casesInit = new HashSet<>();
-
- for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
- if (effectiveStatement instanceof UnknownSchemaNode) {
- UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
- unknownNodesInit.add(unknownNode);
- }
- if (effectiveStatement instanceof AugmentationSchema) {
- AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
+ for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
+ if (effectiveStatement instanceof AugmentationSchemaNode) {
+ final AugmentationSchemaNode augmentationSchema = (AugmentationSchemaNode) effectiveStatement;
augmentationsInit.add(augmentationSchema);
}
if (effectiveStatement instanceof ChoiceCaseNode) {
- ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
- casesInit.add(choiceCaseNode);
+ final ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
+ // FIXME: we may be overwriting a previous entry, is that really okay?
+ casesInit.put(choiceCaseNode.getQName(), choiceCaseNode);
+ }
+ if (YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(effectiveStatement.statementDefinition())) {
+ final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
+ final ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode);
+ // FIXME: we may be overwriting a previous entry, is that really okay?
+ casesInit.put(shorthandCase.getQName(), shorthandCase);
+ if (dataSchemaNode.isAugmenting() && !this.augmenting) {
+ resetAugmenting(dataSchemaNode);
+ }
}
}
- this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
this.augmentations = ImmutableSet.copyOf(augmentationsInit);
- this.cases = ImmutableSet.copyOf(casesInit);
- }
-
- @Override
- public QName getQName() {
- return qname;
- }
-
- @Override
- public SchemaPath getPath() {
- return path;
- }
+ this.cases = ImmutableSortedMap.copyOfSorted(casesInit);
+
+ final DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
+ if (defaultStmt != null) {
+ final QName qname;
+ try {
+ qname = QName.create(getQName(), defaultStmt.argument());
+ } catch (IllegalArgumentException e) {
+ throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
+ defaultStmt.argument(), e);
+ }
- @Override
- public boolean isAugmenting() {
- return augmenting;
- }
+ // FIXME: this does not work with submodules, as they are
+ defaultCase = InferenceException.throwIfNull(cases.get(qname), ctx.getStatementSourceReference(),
+ "Default statement refers to missing case %s", qname);
+ } else {
+ defaultCase = null;
+ }
- @Override
- public boolean isAddedByUses() {
- return addedByUses;
+ final MandatoryEffectiveStatement mandatoryStmt = firstEffective(MandatoryEffectiveStatement.class);
+ mandatory = mandatoryStmt == null ? false : mandatoryStmt.argument().booleanValue();
+ }
+
+ private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
+ if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
+ final LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
+ leaf.augmenting = false;
+ } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
+ final ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
+ container.augmenting = false;
+ } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
+ final LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
+ leafList.augmenting = false;
+ } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
+ final ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
+ list.augmenting = false;
+ } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
+ final AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
+ anyXml.augmenting = false;
+ }
}
@Override
public Optional<ChoiceSchemaNode> getOriginal() {
- return Optional.fromNullable(original);
- }
-
- @Override
- public boolean isConfiguration() {
- return configuration;
- }
-
- @Override
- public ConstraintDefinition getConstraints() {
- return constraints;
+ return Optional.ofNullable(original);
}
@Override
- public Set<AugmentationSchema> getAvailableAugmentations() {
+ public Set<AugmentationSchemaNode> getAvailableAugmentations() {
return augmentations;
}
@Override
- public List<UnknownSchemaNode> getUnknownSchemaNodes() {
- return unknownNodes;
- }
-
- @Override
- public Set<ChoiceCaseNode> getCases() {
+ public SortedMap<QName, ChoiceCaseNode> getCases() {
return cases;
}
@Override
- public ChoiceCaseNode getCaseNodeByName(final QName name) {
- if (name == null) {
- throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
- }
- for (final ChoiceCaseNode caseNode : cases) {
- if (caseNode != null && name.equals(caseNode.getQName())) {
- return caseNode;
- }
- }
- return null;
+ public Optional<ChoiceCaseNode> getDefaultCase() {
+ return Optional.ofNullable(defaultCase);
}
@Override
- public ChoiceCaseNode getCaseNodeByName(final String name) {
- if (name == null) {
- throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
- }
- for (final ChoiceCaseNode caseNode : cases) {
- if (caseNode != null && (caseNode.getQName() != null)
- && name.equals(caseNode.getQName().getLocalName())) {
- return caseNode;
- }
- }
- return null;
- }
-
- @Override
- public String getDefaultCase() {
- return defaultCase;
+ public boolean isMandatory() {
+ return mandatory;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + ((qname == null) ? 0 : qname.hashCode());
- result = prime * result + ((path == null) ? 0 : path.hashCode());
+ result = prime * result + Objects.hashCode(getQName());
+ result = prime * result + Objects.hashCode(getPath());
return result;
}
if (getClass() != obj.getClass()) {
return false;
}
- ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
- if (qname == null) {
- if (other.qname != null) {
- return false;
- }
- } else if (!qname.equals(other.qname)) {
- return false;
- }
- if (path == null) {
- if (other.path != null) {
- return false;
- }
- } else if (!path.equals(other.path)) {
- return false;
- }
- return true;
+ final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
+ return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
}
@Override
public String toString() {
- StringBuilder sb = new StringBuilder(ChoiceEffectiveStatementImpl.class.getSimpleName());
- sb.append("[");
- sb.append("qname=").append(qname);
- sb.append("]");
- return sb.toString();
+ return ChoiceEffectiveStatementImpl.class.getSimpleName() + "["
+ + "qname=" + getQName()
+ + "]";
}
-
-}
\ No newline at end of file
+}