-/**
+/*
* 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 com.google.common.collect.ImmutableSet;
import java.util.Collection;
-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.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.model.api.stmt.LeafListStatement;
-import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableList;
-import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.MustDefinition;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.LeafListStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypeBuilder;
+import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypes;
+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.TypeUtils;
-public class LeafListEffectiveStatementImpl extends
- AbstractEffectiveDocumentedNode<QName, LeafListStatement> implements
- LeafListSchemaNode, DerivableSchemaNode {
- private final QName qname;
- private final SchemaPath path;
+public final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafListStatement>
+ implements LeafListEffectiveStatement, LeafListSchemaNode, DerivableSchemaNode {
- boolean augmenting;
- boolean addedByUses;
- LeafListSchemaNode original;
- boolean configuration;
- ConstraintDefinition constraintsDef;
- TypeDefinition<?> type;
- boolean userOrdered;
+ private static final String ORDER_BY_USER_KEYWORD = "user";
- private ImmutableList<UnknownSchemaNode> unknownNodes;
+ private final TypeDefinition<?> type;
+ private final LeafListSchemaNode original;
+ private final boolean userOrdered;
+ private final Set<String> defaultValues;
+ private final Collection<MustDefinition> mustConstraints;
public LeafListEffectiveStatementImpl(
- StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
+ final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
super(ctx);
- this.qname = ctx.getStatementArgument();
- this.path = Utils.getSchemaPath(ctx);
- // :TODO init other fields
-
- initSubstatementCollections();
- initCopyType(ctx);
- }
-
- private void initCopyType(
- StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
-
- TypeOfCopy typeOfCopy = ctx.getTypeOfCopy();
- switch (typeOfCopy) {
- case ADDED_BY_AUGMENTATION:
- augmenting = true;
- original = (LeafListSchemaNode) ctx.getOriginalCtx()
- .buildEffective();
- break;
- case ADDED_BY_USES:
- addedByUses = true;
- original = (LeafListSchemaNode) ctx.getOriginalCtx()
- .buildEffective();
- break;
- default:
- break;
- }
- }
-
- private void initSubstatementCollections() {
- Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
-
- List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
+ this.original = (LeafListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
+
+ final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
+ firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
+ "Leaf-list is missing a 'type' statement");
+
+ final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
+ ctx.getSchemaPath().get());
+ final ImmutableSet.Builder<String> defaultValuesBuilder = ImmutableSet.builder();
+ boolean isUserOrdered = false;
+ for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
+ if (stmt instanceof OrderedByEffectiveStatement) {
+ isUserOrdered = ORDER_BY_USER_KEYWORD.equals(stmt.argument());
+ }
- for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
- if (effectiveStatement instanceof UnknownSchemaNode) {
- UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
- unknownNodesInit.add(unknownNode);
+ if (stmt instanceof DefaultEffectiveStatement) {
+ defaultValuesBuilder.add(((DefaultEffectiveStatement) stmt).argument());
+ } else if (stmt instanceof DescriptionEffectiveStatement) {
+ builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
+ } else if (stmt instanceof ReferenceEffectiveStatement) {
+ builder.setReference(((ReferenceEffectiveStatement)stmt).argument());
+ } else if (stmt instanceof StatusEffectiveStatement) {
+ builder.setStatus(((StatusEffectiveStatement)stmt).argument());
+ } else if (stmt instanceof UnitsEffectiveStatement) {
+ builder.setUnits(((UnitsEffectiveStatement)stmt).argument());
}
}
- this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
- }
-
- @Override
- public QName getQName() {
- return qname;
- }
+ // FIXME: We need to interpret the default value in terms of supplied element type
+ defaultValues = defaultValuesBuilder.build();
+ SourceException.throwIf(
+ TypeUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, defaultValues),
+ ctx.getStatementSourceReference(),
+ "Leaf-list '%s' has one of its default values '%s' marked with an if-feature statement.",
+ ctx.getStatementArgument(), defaultValues);
- @Override
- public SchemaPath getPath() {
- return path;
- }
+ // FIXME: RFC7950 section 7.7.4: we need to check for min-elements and defaultValues conflict
- @Override
- public boolean isAugmenting() {
- return augmenting;
+ type = builder.build();
+ userOrdered = isUserOrdered;
+ mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
}
@Override
- public boolean isAddedByUses() {
- return addedByUses;
+ public Collection<String> getDefaults() {
+ return defaultValues;
}
@Override
public Optional<LeafListSchemaNode> getOriginal() {
- return Optional.fromNullable(original);
- }
-
- @Override
- public boolean isConfiguration() {
- return configuration;
- }
-
- @Override
- public ConstraintDefinition getConstraints() {
- return constraintsDef;
+ return Optional.ofNullable(original);
}
@Override
}
@Override
- public List<UnknownSchemaNode> getUnknownSchemaNodes() {
- return unknownNodes;
+ public Collection<MustDefinition> getMustConstraints() {
+ return mustConstraints;
}
@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;
}
- LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) 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 LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
+ return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
}
@Override
public String toString() {
- StringBuilder sb = new StringBuilder(
- LeafListEffectiveStatementImpl.class.getSimpleName());
- sb.append("[");
- sb.append(qname);
- sb.append("]");
- return sb.toString();
+ return LeafListEffectiveStatementImpl.class.getSimpleName() + "[" + getQName() + "]";
}
-}
\ No newline at end of file
+}