From 387d6385bbd6a689372c6ac9cdde4021a7162e4d Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 14 Nov 2015 01:03:19 +0100 Subject: [PATCH] BUG-4638: Convert to using new types This patch switches the Lithium parser to use yang.model.util.type as the underlying implementation of TypeDefinitions. Change-Id: Idf18a95f268b41b7a6832db74289fa7dcd86bf06 Signed-off-by: Robert Varga --- .../yang/model/util/EnumPairImpl.java | 116 +++++++ .../stmt/rfc6020/PatternStatementImpl.java | 14 +- .../stmt/rfc6020/TypeStatementImpl.java | 156 ++++++--- .../yang/parser/stmt/rfc6020/TypeUtils.java | 165 +++------- .../effective/EffectiveStatementBase.java | 4 +- .../ExtendedTypeEffectiveStatementImpl.java | 295 ------------------ .../effective/LeafEffectiveStatementImpl.java | 42 ++- .../LeafListEffectiveStatementImpl.java | 42 ++- .../TypeDefEffectiveStatementImpl.java | 233 +++++--------- .../AbstractBuiltinEffectiveStatement.java | 58 ++++ .../type/AbstractTypeEffectiveStatement.java | 42 +++ .../type/BinaryEffectiveStatementImpl.java | 96 ------ .../BinaryTypeEffectiveStatementImpl.java | 59 ++++ ...tsSpecificationEffectiveStatementImpl.java | 182 +++-------- .../type/BitsTypeEffectiveStatementImpl.java | 24 ++ .../type/BooleanEffectiveStatementImpl.java | 97 ------ .../BooleanTypeEffectiveStatementImpl.java | 24 ++ .../type/BuiltinEffectiveStatements.java | 98 ++++++ ...64SpecificationEffectiveStatementImpl.java | 221 ++----------- .../DecimalTypeEffectiveStatementImpl.java | 60 ++++ .../type/EmptyEffectiveStatementImpl.java | 88 ------ .../type/EmptyTypeEffectiveStatementImpl.java | 24 ++ ...umSpecificationEffectiveStatementImpl.java | 193 +++--------- .../type/EnumTypeEffectiveStatementImpl.java | 24 ++ ...efSpecificationEffectiveStatementImpl.java | 115 ++----- ...IdentityrefTypeEffectiveStatementImpl.java | 26 ++ ...erSpecificationEffectiveStatementImpl.java | 120 ++----- ...eIdentifierTypeEffectiveStatementImpl.java | 51 +++ .../type/Int16EffectiveStatementImpl.java | 36 --- .../type/Int32EffectiveStatementImpl.java | 36 --- .../type/Int64EffectiveStatementImpl.java | 36 --- .../type/Int8EffectiveStatementImpl.java | 36 --- .../type/IntegerEffectiveImplBase.java | 165 ---------- .../IntegerTypeEffectiveStatementImpl.java | 60 ++++ ...efSpecificationEffectiveStatementImpl.java | 132 +------- .../LeafrefTypeEffectiveStatementImpl.java | 24 ++ .../type/StringEffectiveStatementImpl.java | 165 ---------- .../StringTypeEffectiveStatementImpl.java | 71 +++++ .../type/UInt16EffectiveStatementImpl.java | 35 --- .../type/UInt32EffectiveStatementImpl.java | 35 --- .../type/UInt64EffectiveStatementImpl.java | 36 --- .../type/UInt8EffectiveStatementImpl.java | 35 --- ...onSpecificationEffectiveStatementImpl.java | 139 +-------- .../type/UnionTypeEffectiveStatementImpl.java | 24 ++ .../UnsignedIntegerEffectiveImplBase.java | 166 ---------- ...gnedIntegerTypeEffectiveStatementImpl.java | 51 +++ .../test/EffectiveStatementTypeTest.java | 222 ++++++------- .../yang/stmt/retest/UsesAugmentTest.java | 80 +++-- .../stmt/retest/YangParserNegativeTest.java | 9 +- .../stmt/retest/YangParserSimpleTest.java | 5 +- .../yang/stmt/retest/YangParserTest.java | 263 +++++++--------- .../retest/YangParserWithContextTest.java | 26 +- .../yangtools/yang/stmt/test/ListTest.java | 3 +- .../stmt/test/TypedefConstraintsTest.java | 32 +- .../yang/stmt/test/YangTypes2StmtTest.java | 36 +-- .../stmt/test/yin/YinFileListStmtTest.java | 11 +- .../src/test/resources/type-tests/types.yang | 2 +- 57 files changed, 1629 insertions(+), 3011 deletions(-) create mode 100644 yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/EnumPairImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ExtendedTypeEffectiveStatementImpl.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/AbstractBuiltinEffectiveStatement.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/AbstractTypeEffectiveStatement.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BinaryEffectiveStatementImpl.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BinaryTypeEffectiveStatementImpl.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsTypeEffectiveStatementImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BooleanEffectiveStatementImpl.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BooleanTypeEffectiveStatementImpl.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BuiltinEffectiveStatements.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/DecimalTypeEffectiveStatementImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EmptyEffectiveStatementImpl.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EmptyTypeEffectiveStatementImpl.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EnumTypeEffectiveStatementImpl.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IdentityrefTypeEffectiveStatementImpl.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/InstanceIdentifierTypeEffectiveStatementImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int16EffectiveStatementImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int32EffectiveStatementImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int64EffectiveStatementImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int8EffectiveStatementImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IntegerEffectiveImplBase.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IntegerTypeEffectiveStatementImpl.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/LeafrefTypeEffectiveStatementImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/StringEffectiveStatementImpl.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/StringTypeEffectiveStatementImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt16EffectiveStatementImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt32EffectiveStatementImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt64EffectiveStatementImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt8EffectiveStatementImpl.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnionTypeEffectiveStatementImpl.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnsignedIntegerEffectiveImplBase.java create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnsignedIntegerTypeEffectiveStatementImpl.java diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/EnumPairImpl.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/EnumPairImpl.java new file mode 100644 index 0000000000..f93dee2a0f --- /dev/null +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/EnumPairImpl.java @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.model.util; + +import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; +import org.opendaylight.yangtools.concepts.Immutable; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.opendaylight.yangtools.yang.model.api.Status; +import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; +import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair; + +public final class EnumPairImpl implements EnumPair, Immutable { + private final List unknownSchemaNodes; + private final String description; + private final String reference; + private final SchemaPath path; + private final Status status; + private final Integer value; + private final String name; + + public EnumPairImpl(final String name, final Integer value, final SchemaPath path, final String description, + final String reference, final Status status, final Collection unknownSchemaNodes) { + this.path = Preconditions.checkNotNull(path); + this.value = Preconditions.checkNotNull(value); + this.name = Preconditions.checkNotNull(name); + this.description = description; + this.reference = reference; + this.status = Preconditions.checkNotNull(status); + this.unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodes); + } + + @Override + public QName getQName() { + return path.getLastComponent(); + } + + @Override + public SchemaPath getPath() { + return path; + } + + @Override + public List getUnknownSchemaNodes() { + return unknownSchemaNodes; + } + + @Override + public String getDescription() { + return description; + } + + @Override + public String getReference() { + return reference; + } + + @Override + public Status getStatus() { + return status; + } + + @Override + public String getName() { + return name; + } + + @Override + public Integer getValue() { + return value; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + Objects.hashCode(path.getLastComponent()); + result = prime * result + Objects.hashCode(path); + result = prime * result + Objects.hashCode(unknownSchemaNodes); + result = prime * result + Objects.hashCode(name); + result = prime * result + Objects.hashCode(value); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof EnumPair)) { + return false; + } + EnumPair other = (EnumPair) obj; + if (!Objects.equals(name, other.getName())) { + return false; + } + + return Objects.equals(value, other.getValue()) && Objects.equals(getPath(), other.getPath()) && + Objects.equals(unknownSchemaNodes, other.getUnknownSchemaNodes()); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this).add("name", name).add("value", value).toString(); + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/PatternStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/PatternStatementImpl.java index f587a0df94..7fbafff6be 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/PatternStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/PatternStatementImpl.java @@ -28,9 +28,8 @@ import org.slf4j.LoggerFactory; public class PatternStatementImpl extends AbstractDeclaredStatement implements PatternStatement { private static final Logger LOG = LoggerFactory.getLogger(PatternStatementImpl.class); - private static final Optional OPTIONAL_EMPTY = Optional.of(""); - protected PatternStatementImpl(StmtContext context) { + protected PatternStatementImpl(final StmtContext context) { super(context); } @@ -43,26 +42,27 @@ public class PatternStatementImpl extends AbstractDeclaredStatement ctx, String value) { + public PatternConstraint parseArgumentValue(final StmtContext ctx, final String value) { final String pattern = "^" + value + '$'; try { Pattern.compile(pattern); - return new PatternConstraintEffectiveImpl(pattern, OPTIONAL_EMPTY, OPTIONAL_EMPTY); } catch (PatternSyntaxException e) { - LOG.debug("Pattern {} failed to compile", pattern, e); + LOG.debug("Pattern \"{}\" failed to compile at {}", pattern, ctx.getStatementSourceReference(), e); return null; } + + return new PatternConstraintEffectiveImpl(pattern, Optional.absent(), Optional.absent()); } @Override - public PatternStatement createDeclared(StmtContext ctx) { + public PatternStatement createDeclared(final StmtContext ctx) { return new PatternStatementImpl(ctx); } @Override public EffectiveStatement createEffective( - StmtContext> ctx) { + final StmtContext> ctx) { return new PatternEffectiveStatementImpl(ctx); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeStatementImpl.java index 251bbe2365..a06463a7d5 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeStatementImpl.java @@ -7,12 +7,30 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; +import com.google.common.base.Verify; import java.util.Collection; import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement; +import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement; import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport; @@ -24,20 +42,21 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; -import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ExtendedTypeEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BinaryEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BooleanEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EmptyEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Int16EffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Int32EffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Int64EffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Int8EffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.StringEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UInt16EffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UInt32EffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UInt64EffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UInt8EffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.TypeDefEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BinaryTypeEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BitsTypeEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BooleanTypeEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BuiltinEffectiveStatements; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.DecimalTypeEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EmptyTypeEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EnumTypeEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IdentityrefTypeEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.InstanceIdentifierTypeEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IntegerTypeEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LeafrefTypeEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.StringTypeEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnionTypeEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnsignedIntegerTypeEffectiveStatementImpl; public class TypeStatementImpl extends AbstractDeclaredStatement implements TypeStatement { @@ -67,47 +86,98 @@ public class TypeStatementImpl extends AbstractDeclaredStatement } @Override - public EffectiveStatement createEffective( + public TypeEffectiveStatement createEffective( final StmtContext> ctx) { - // :FIXME improve the test of isExtended - e.g. unknown statements, - // etc.. - Collection> declaredSubstatements = ctx - .declaredSubstatements(); - boolean isExtended = !declaredSubstatements.isEmpty(); - if (isExtended) { - return new ExtendedTypeEffectiveStatementImpl(ctx, true); - } - + // First look up the proper base type + final TypeEffectiveStatement typeStmt; switch (ctx.getStatementArgument()) { + case TypeUtils.BINARY: + typeStmt = BuiltinEffectiveStatements.BINARY; + break; + case TypeUtils.BOOLEAN: + typeStmt = BuiltinEffectiveStatements.BOOLEAN; + break; + case TypeUtils.EMPTY: + typeStmt = BuiltinEffectiveStatements.EMPTY; + break; + case TypeUtils.INSTANCE_IDENTIFIER: + typeStmt = BuiltinEffectiveStatements.INSTANCE_IDENTIFIER; + break; case TypeUtils.INT8: - return new Int8EffectiveStatementImpl(ctx); + typeStmt = BuiltinEffectiveStatements.INT8; + break; case TypeUtils.INT16: - return new Int16EffectiveStatementImpl(ctx); + typeStmt = BuiltinEffectiveStatements.INT16; + break; case TypeUtils.INT32: - return new Int32EffectiveStatementImpl(ctx); + typeStmt = BuiltinEffectiveStatements.INT32; + break; case TypeUtils.INT64: - return new Int64EffectiveStatementImpl(ctx); + typeStmt = BuiltinEffectiveStatements.INT64; + break; + case TypeUtils.STRING: + typeStmt = BuiltinEffectiveStatements.STRING; + break; case TypeUtils.UINT8: - return new UInt8EffectiveStatementImpl(ctx); + typeStmt = BuiltinEffectiveStatements.UINT8; + break; case TypeUtils.UINT16: - return new UInt16EffectiveStatementImpl(ctx); + typeStmt = BuiltinEffectiveStatements.UINT16; + break; case TypeUtils.UINT32: - return new UInt32EffectiveStatementImpl(ctx); + typeStmt = BuiltinEffectiveStatements.UINT32; + break; case TypeUtils.UINT64: - return new UInt64EffectiveStatementImpl(ctx); - case TypeUtils.STRING: - return new StringEffectiveStatementImpl(ctx); - case TypeUtils.BOOLEAN: - return new BooleanEffectiveStatementImpl(ctx); - case TypeUtils.EMPTY: - return new EmptyEffectiveStatementImpl(ctx); - case TypeUtils.BINARY: - return new BinaryEffectiveStatementImpl(ctx); + typeStmt = BuiltinEffectiveStatements.UINT64; + break; default: - // :FIXME try to resolve original typedef context here and - // return buildEffective of original typedef context - return new ExtendedTypeEffectiveStatementImpl(ctx, false); + final QName qname = Utils.qNameFromArgument(ctx, ctx.getStatementArgument()); + final StmtContext typedef = + ctx.getFromNamespace(TypeNamespace.class, qname); + if (typedef == null) { + throw new SourceException("Type '" + qname + "' not found", ctx.getStatementSourceReference()); + } + + final TypedefEffectiveStatement effectiveTypedef = typedef.buildEffective(); + Verify.verify(effectiveTypedef instanceof TypeDefEffectiveStatementImpl); + typeStmt = ((TypeDefEffectiveStatementImpl) effectiveTypedef).asTypeEffectiveStatement(); + } + + if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) { + return typeStmt; + } + + // Now instantiate the proper effective statement for that type + final TypeDefinition baseType = typeStmt.getTypeDefinition(); + if (baseType instanceof BinaryTypeDefinition) { + return new BinaryTypeEffectiveStatementImpl(ctx, (BinaryTypeDefinition) baseType); + } else if (baseType instanceof BitsTypeDefinition) { + return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType); + } else if (baseType instanceof BooleanTypeDefinition) { + return new BooleanTypeEffectiveStatementImpl(ctx, (BooleanTypeDefinition) baseType); + } else if (baseType instanceof DecimalTypeDefinition) { + return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType); + } else if (baseType instanceof EmptyTypeDefinition) { + return new EmptyTypeEffectiveStatementImpl(ctx, (EmptyTypeDefinition) baseType); + } else if (baseType instanceof EnumTypeDefinition) { + return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType); + } else if (baseType instanceof IdentityrefTypeDefinition) { + return new IdentityrefTypeEffectiveStatementImpl(ctx, (IdentityrefTypeDefinition) baseType); + } else if (baseType instanceof InstanceIdentifierTypeDefinition) { + return new InstanceIdentifierTypeEffectiveStatementImpl(ctx, (InstanceIdentifierTypeDefinition) baseType); + } else if (baseType instanceof IntegerTypeDefinition) { + return new IntegerTypeEffectiveStatementImpl(ctx, (IntegerTypeDefinition) baseType); + } else if (baseType instanceof LeafrefTypeDefinition) { + return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType); + } else if (baseType instanceof StringTypeDefinition) { + return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType); + } else if (baseType instanceof UnionTypeDefinition) { + return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType); + } else if (baseType instanceof UnsignedIntegerTypeDefinition) { + return new UnsignedIntegerTypeEffectiveStatementImpl(ctx, (UnsignedIntegerTypeDefinition) baseType); + } else { + throw new IllegalStateException("Unhandled base type " + baseType); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeUtils.java index f2fb51e644..9b9683d41c 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeUtils.java @@ -10,39 +10,23 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Set; -import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypeDefinitionAware; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; -import org.opendaylight.yangtools.yang.model.util.BinaryType; -import org.opendaylight.yangtools.yang.model.util.BooleanType; -import org.opendaylight.yangtools.yang.model.util.EmptyType; -import org.opendaylight.yangtools.yang.model.util.Int16; -import org.opendaylight.yangtools.yang.model.util.Int32; -import org.opendaylight.yangtools.yang.model.util.Int64; -import org.opendaylight.yangtools.yang.model.util.Int8; -import org.opendaylight.yangtools.yang.model.util.StringType; -import org.opendaylight.yangtools.yang.model.util.Uint16; -import org.opendaylight.yangtools.yang.model.util.Uint32; -import org.opendaylight.yangtools.yang.model.util.Uint64; -import org.opendaylight.yangtools.yang.model.util.Uint8; +import org.opendaylight.yangtools.yang.model.util.UnresolvedNumber; +import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LengthConstraintEffectiveImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.RangeConstraintEffectiveImpl; -import org.opendaylight.yangtools.yang.parser.util.UnknownBoundaryNumber; /** * util class for manipulating YANG base and extended types implementation @@ -69,57 +53,30 @@ public final class TypeUtils { public static final String UINT64 = "uint64"; public static final String UNION = "union"; - private static final Set BUILT_IN_TYPES = ImmutableSet.of( - BINARY, BITS, BOOLEAN, DECIMAL64, EMPTY, ENUMERATION, IDENTITY_REF, INSTANCE_IDENTIFIER, - INT8, INT16, INT32, INT64, LEAF_REF, STRING, UINT8, UINT16, UINT32, UINT64, UNION); - - public static final Set TYPE_BODY_STMTS = ImmutableSet.of( - DECIMAL64, ENUMERATION, LEAF_REF, IDENTITY_REF, INSTANCE_IDENTIFIER, BITS, UNION); - - public static final Map> PRIMITIVE_TYPES_MAP; - static { - Builder> b = ImmutableMap.>builder(); - b.put(BINARY, BinaryType.getInstance()); - b.put(BOOLEAN, BooleanType.getInstance()); - b.put(EMPTY, EmptyType.getInstance()); - b.put(INT8, Int8.getInstance()); - b.put(INT16, Int16.getInstance()); - b.put(INT32, Int32.getInstance()); - b.put(INT64, Int64.getInstance()); - b.put(STRING, StringType.getInstance()); - b.put(UINT8, Uint8.getInstance()); - b.put(UINT16, Uint16.getInstance()); - b.put(UINT32, Uint32.getInstance()); - b.put(UINT64, Uint64.getInstance()); - PRIMITIVE_TYPES_MAP = b.build(); - } + private static final Set BUILT_IN_TYPES = + ImmutableSet.of(BINARY, BITS, BOOLEAN, DECIMAL64, EMPTY, ENUMERATION, IDENTITY_REF, INSTANCE_IDENTIFIER, + INT8, INT16, INT32, INT64, LEAF_REF, STRING, UINT8, UINT16, UINT32, UINT64, UNION); - private static final Comparator> TYPE_SORT_COMPARATOR = new Comparator>() { - @Override - public int compare(final TypeDefinition o1, final TypeDefinition o2) { - return Boolean.compare(isBuiltInType(o2), isBuiltInType(o1)); - } - }; + private static final Set TYPE_BODY_STMTS = ImmutableSet.of( + DECIMAL64, ENUMERATION, LEAF_REF, IDENTITY_REF, BITS, UNION); + private static final Splitter PIPE_SPLITTER = Splitter.on('|').trimResults(); + private static final Splitter TWO_DOTS_SPLITTER = Splitter.on("..").trimResults(); private TypeUtils() { } - private static final Splitter PIPE_SPLITTER = Splitter.on('|').trimResults(); - private static final Splitter TWO_DOTS_SPLITTER = Splitter.on("..").trimResults(); - private static BigDecimal yangConstraintToBigDecimal(final Number number) { - if (number instanceof UnknownBoundaryNumber) { - if (number.toString().equals("min")) { - return RangeStatementImpl.YANG_MIN_NUM; - } else { - return RangeStatementImpl.YANG_MAX_NUM; - } - } else { - return new BigDecimal(number.toString()); + if (UnresolvedNumber.max().equals(number)) { + return RangeStatementImpl.YANG_MAX_NUM; + } + if (UnresolvedNumber.min().equals(number)) { + return RangeStatementImpl.YANG_MIN_NUM; } + + return new BigDecimal(number.toString()); } - public static int compareNumbers(final Number n1, final Number n2) { + private static int compareNumbers(final Number n1, final Number n2) { final BigDecimal num1 = yangConstraintToBigDecimal(n1); final BigDecimal num2 = yangConstraintToBigDecimal(n2); @@ -128,41 +85,33 @@ public final class TypeUtils { } private static Number parseIntegerConstraintValue(final String value) { - Number result; - - if (isMinOrMaxString(value)) { - result = new UnknownBoundaryNumber(value); - } else { - try { - result = new BigInteger(value); - } catch (NumberFormatException e) { - throw new IllegalArgumentException(String.format("Value %s is not a valid integer", value), e); - } + if ("max".equals(value)) { + return UnresolvedNumber.max(); + } + if ("min".equals(value)) { + return UnresolvedNumber.min(); + } + + try { + return new BigInteger(value); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(String.format("Value %s is not a valid integer", value), e); } - return result; } private static Number parseDecimalConstraintValue(final String value) { - final Number result; - - if (isMinOrMaxString(value)) { - result = new UnknownBoundaryNumber(value); - } else { - try { - if (value.indexOf('.') != -1) { - result = new BigDecimal(value); - } else { - result = new BigInteger(value); - } - } catch (NumberFormatException e) { - throw new IllegalArgumentException(String.format("Value %s is not a valid decimal number", value), e); - } + if ("max".equals(value)) { + return UnresolvedNumber.max(); + } + if ("min".equals(value)) { + return UnresolvedNumber.min(); } - return result; - } - private static boolean isMinOrMaxString(final String value) { - return "min".equals(value) || "max".equals(value); + try { + return value.indexOf('.') != -1 ? new BigDecimal(value) : new BigInteger(value); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(String.format("Value %s is not a valid decimal number", value), e); + } } public static List parseRangeListFromString(final String rangeArgument) { @@ -240,37 +189,19 @@ public final class TypeUtils { return rangeConstraints; } - public static boolean isBuiltInType(final TypeDefinition o1) { - return BUILT_IN_TYPES.contains(o1.getQName().getLocalName()); - } - - public static boolean isYangBuiltInTypeString(final String typeName) { - return BUILT_IN_TYPES.contains(typeName); - } - - public static boolean isYangPrimitiveTypeString(final String typeName) { - return PRIMITIVE_TYPES_MAP.containsKey(typeName); - } - public static boolean isYangTypeBodyStmtString(final String typeName) { return TYPE_BODY_STMTS.contains(typeName); } - public static TypeDefinition getYangPrimitiveTypeFromString(final String typeName) { - return PRIMITIVE_TYPES_MAP.get(typeName); + public static boolean isYangBuiltInTypeString(final String typeName) { + return BUILT_IN_TYPES.contains(typeName); } - public static TypeDefinition getTypeFromEffectiveStatement(final EffectiveStatement effectiveStatement) { - if (effectiveStatement instanceof TypeDefinitionAware) { - TypeDefinitionAware typeDefEffectiveBuilder = (TypeDefinitionAware) effectiveStatement; - return typeDefEffectiveBuilder.getTypeDefinition(); - } else { - final String typeName = ((TypeDefinition) effectiveStatement).getQName().getLocalName(); - return PRIMITIVE_TYPES_MAP.get(typeName); - } - } - public static void sortTypes(final List> typesInit) { - Collections.sort(typesInit, TYPE_SORT_COMPARATOR); + public static SchemaPath typeEffectiveSchemaPath(final StmtContext stmtCtx) { + final SchemaPath path = stmtCtx.getSchemaPath().get(); + final QName qname = stmtCtx.getFromNamespace(QNameCacheNamespace.class, + QName.create(path.getParent().getLastComponent(), path.getLastComponent().getLocalName())); + return path.getParent().createChild(qname); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java index a381200a84..7151db18f4 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java @@ -149,8 +149,6 @@ public abstract class EffectiveStatementBase> } protected final EffectiveStatement firstEffectiveSubstatementOfType(final Class type) { - Optional> possible = Iterables.tryFind(substatements, - Predicates.instanceOf(type)); - return possible.isPresent() ? possible.get() : null; + return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull(); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ExtendedTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ExtendedTypeEffectiveStatementImpl.java deleted file mode 100644 index 912ffc99ae..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ExtendedTypeEffectiveStatementImpl.java +++ /dev/null @@ -1,295 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; - -import com.google.common.base.Optional; -import com.google.common.base.Splitter; -import com.google.common.collect.ImmutableList; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -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.TypeEffectiveStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement; -import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; -import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; -import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; -import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; -import org.opendaylight.yangtools.yang.model.util.ExtendedType.Builder; -import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LengthEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.PatternEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.RangeEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.util.TypeConstraints; - -public final class ExtendedTypeEffectiveStatementImpl extends AbstractEffectiveDocumentedNode -implements - TypeDefinition>, TypeEffectiveStatement { - - private static final Splitter COLON_SPLITTER = Splitter.on(':').trimResults(); - - private final QName qName; - private final SchemaPath path; - private final TypeDefinition baseType; - private final String defaultValue; - private final String units; - private final List ranges; - private final List lengths; - private final List patterns; - private final Integer fractionDigits; - - private ExtendedType extendedType = null; - private final boolean isExtended; - - public ExtendedTypeEffectiveStatementImpl( - final StmtContext> ctx, final boolean isExtended) { - super(ctx); - - this.isExtended = isExtended; - qName = initQName(ctx, isExtended); - - final StmtContext typeStmt = - ctx.getFromNamespace(TypeNamespace.class, qName); - if (typeStmt == null) { - path = ctx.getSchemaPath().get(); - } else { - path = ctx.getFromNamespace(TypeNamespace.class, qName).getSchemaPath().get(); - } - - UnitsEffectiveStatementImpl unitsStmt = firstEffective(UnitsEffectiveStatementImpl.class); - this.units = (unitsStmt == null) ? null : unitsStmt.argument(); - DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class); - this.defaultValue = (defaultStmt == null) ? null : defaultStmt.argument(); - - ranges = initRanges(); - lengths = initLengths(); - patterns = initPatterns(); - fractionDigits = initFractionDigits(); - - baseType = parseBaseTypeFromCtx(ctx); - validateTypeConstraints(ctx); - } - - private static QName initQName(final StmtContext> ctx, - final boolean isExtended) { - - QName qName; - - if (isExtended) { - final List nameTokens = COLON_SPLITTER.splitToList(ctx.getStatementArgument()); - - switch (nameTokens.size()) { - case 1: - qName = QName.create(Utils.getRootModuleQName(ctx), nameTokens.get(0)); - break; - case 2: - qName = QName.create(Utils.getRootModuleQName(ctx), nameTokens.get(1)); - break; - default: - throw new IllegalArgumentException(String.format( - "Bad colon separated parts number (%d) of QName '%s'.", nameTokens.size(), - ctx.getStatementArgument())); - } - } else { - qName = Utils.qNameFromArgument(ctx, ctx.getStatementArgument()); - } - return qName; - } - - private static TypeDefinition parseBaseTypeFromCtx( - final StmtContext> ctx) { - - TypeDefinition baseType; - - final QName baseTypeQName = Utils.qNameFromArgument(ctx, ctx.getStatementArgument()); - if (TypeUtils.isYangPrimitiveTypeString(baseTypeQName.getLocalName())) { - baseType = TypeUtils.getYangPrimitiveTypeFromString(baseTypeQName.getLocalName()); - } else { - StmtContext baseTypeCtx = ctx - .getParentContext().getFromNamespace(TypeNamespace.class, baseTypeQName); - - if (baseTypeCtx == null) { - throw new IllegalStateException(String.format("Type '%s' was not found in %s.", baseTypeQName, - ctx.getStatementSourceReference())); - } - - baseType = (TypeDefEffectiveStatementImpl) baseTypeCtx.buildEffective(); - } - - return baseType; - } - - private void validateTypeConstraints( - final StmtContext> ctx) { - - final List sourceParts = COLON_SPLITTER.splitToList(ctx.getStatementSourceReference().toString()); - TypeConstraints typeConstraints = new TypeConstraints(sourceParts.get(0), Integer.parseInt(sourceParts.get(1))); - - typeConstraints.addRanges(ranges); - typeConstraints.addLengths(lengths); - typeConstraints.addPatterns(patterns); - typeConstraints.addFractionDigits(fractionDigits); - - typeConstraints = addConstraintsFromBaseType(typeConstraints, baseType); - typeConstraints.validateConstraints(); - } - - private static TypeConstraints addConstraintsFromBaseType(final TypeConstraints typeConstraints, - final TypeDefinition baseType) { - - final String baseTypeName = baseType.getQName().getLocalName(); - - if (baseType instanceof IntegerTypeDefinition) { - final IntegerTypeDefinition intType = (IntegerTypeDefinition) TypeUtils - .getYangPrimitiveTypeFromString(baseTypeName); - typeConstraints.addRanges(intType.getRangeConstraints()); - } else if (baseType instanceof UnsignedIntegerTypeDefinition) { - final UnsignedIntegerTypeDefinition uintType = (UnsignedIntegerTypeDefinition) TypeUtils - .getYangPrimitiveTypeFromString(baseTypeName); - typeConstraints.addRanges(uintType.getRangeConstraints()); - } else if (baseType instanceof StringTypeDefinition) { - final StringTypeDefinition stringType = (StringTypeDefinition) TypeUtils - .getYangPrimitiveTypeFromString(baseTypeName); - typeConstraints.addLengths(stringType.getLengthConstraints()); - typeConstraints.addPatterns(stringType.getPatternConstraints()); - } else if (baseType instanceof BinaryTypeDefinition) { - final BinaryTypeDefinition binaryType = (BinaryTypeDefinition) TypeUtils - .getYangPrimitiveTypeFromString(baseTypeName); - typeConstraints.addLengths(binaryType.getLengthConstraints()); - } else if (baseType instanceof TypeDefEffectiveStatementImpl) { - typeConstraints.addRanges(((TypeDefEffectiveStatementImpl) baseType).getRangeConstraints()); - typeConstraints.addLengths(((TypeDefEffectiveStatementImpl) baseType).getLengthConstraints()); - typeConstraints.addPatterns(((TypeDefEffectiveStatementImpl) baseType).getPatternConstraints()); - typeConstraints.addFractionDigits(((TypeDefEffectiveStatementImpl) baseType).getFractionDigits()); - } - - return typeConstraints; - } - - protected final Integer initFractionDigits() { - final FractionDigitsEffectiveStatementImpl fractionDigitsEffStmt = firstEffective(FractionDigitsEffectiveStatementImpl.class); - return fractionDigitsEffStmt != null ? fractionDigitsEffStmt.argument() : null; - } - - protected final List initRanges() { - final RangeEffectiveStatementImpl rangeConstraints = firstEffective(RangeEffectiveStatementImpl.class); - return rangeConstraints != null ? rangeConstraints.argument() : Collections. emptyList(); - } - - protected final List initLengths() { - final LengthEffectiveStatementImpl lengthConstraints = firstEffective(LengthEffectiveStatementImpl.class); - return lengthConstraints != null ? lengthConstraints.argument() : Collections. emptyList(); - } - - protected final List initPatterns() { - final List patternConstraints = new ArrayList<>(); - - for (final EffectiveStatement effectiveStatement : effectiveSubstatements()) { - if (effectiveStatement instanceof PatternEffectiveStatementImpl) { - final PatternConstraint pattern = ((PatternEffectiveStatementImpl) effectiveStatement).argument(); - - if (pattern != null) { - patternConstraints.add(pattern); - } - } - } - - return !patternConstraints.isEmpty() ? ImmutableList.copyOf(patternConstraints) : Collections - . emptyList(); - } - - @Override - public TypeDefinition getBaseType() { - return baseType; - } - - @Override - public String getUnits() { - return units; - } - - @Override - public Object getDefaultValue() { - return defaultValue; - } - - @Override - public QName getQName() { - return qName; - } - - @Override - public SchemaPath getPath() { - return path; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - public List getRangeConstraints() { - return ranges; - } - - public List getLengthConstraints() { - return lengths; - } - - public List getPatternConstraints() { - return patterns; - } - - public Integer getFractionDigits() { - return fractionDigits; - } - - @Override - public TypeDefinition getTypeDefinition() { - if (extendedType != null) { - return extendedType; - } - - if (!isExtended && baseType instanceof TypeDefEffectiveStatementImpl) { - TypeDefEffectiveStatementImpl originalTypeDef = (TypeDefEffectiveStatementImpl) baseType; - return originalTypeDef.getTypeDefinition(); - } - - Builder extendedTypeBuilder; - if (baseType instanceof TypeDefEffectiveStatementImpl) { - TypeDefEffectiveStatementImpl typeDefBaseType = (TypeDefEffectiveStatementImpl) baseType; - extendedTypeBuilder = ExtendedType.builder(qName, typeDefBaseType.getTypeDefinition(), - Optional.fromNullable(getDescription()), Optional.fromNullable(getReference()), path); - } else { - extendedTypeBuilder = ExtendedType.builder(qName, baseType, Optional.fromNullable(getDescription()), - Optional.fromNullable(getReference()), path); - } - - extendedTypeBuilder.fractionDigits(fractionDigits); - extendedTypeBuilder.ranges(ranges); - extendedTypeBuilder.lengths(lengths); - extendedTypeBuilder.patterns(patterns); - - extendedType = extendedTypeBuilder.build(); - - return extendedType; - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafEffectiveStatementImpl.java index fa692d646c..70d8039469 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafEffectiveStatementImpl.java @@ -15,8 +15,11 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; +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.stmt.rfc6020.TypeUtils; +import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; public final class LeafEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode implements LeafSchemaNode, DerivableSchemaNode { @@ -29,14 +32,34 @@ public final class LeafEffectiveStatementImpl extends AbstractEffectiveDataSchem super(ctx); this.original = ctx.getOriginalCtx() == null ? null : (LeafSchemaNode) ctx.getOriginalCtx().buildEffective(); - DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class); - this.defaultStr = (defaultStmt == null) ? null : defaultStmt.argument(); + final TypeEffectiveStatement typeStmt = firstSubstatementOfType(TypeEffectiveStatement.class); + if (typeStmt == null) { + throw new SourceException("Leaf is missing a 'type' statement", ctx.getStatementSourceReference()); + } - UnitsEffectiveStatementImpl unitsStmt = firstEffective(UnitsEffectiveStatementImpl.class); - this.unitsStr = (unitsStmt == null) ? null : unitsStmt.argument(); + String dflt = null; + String units = null; + final ConcreteTypeBuilder builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(), + ctx.getSchemaPath().get()); + for (EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof DefaultEffectiveStatementImpl) { + dflt = ((DefaultEffectiveStatementImpl)stmt).argument(); + builder.setDefaultValue(stmt.argument()); + } else if (stmt instanceof DescriptionEffectiveStatementImpl) { + builder.setDescription(((DescriptionEffectiveStatementImpl)stmt).argument()); + } else if (stmt instanceof ReferenceEffectiveStatementImpl) { + builder.setReference(((ReferenceEffectiveStatementImpl)stmt).argument()); + } else if (stmt instanceof StatusEffectiveStatementImpl) { + builder.setStatus(((StatusEffectiveStatementImpl)stmt).argument()); + } else if (stmt instanceof UnitsEffectiveStatementImpl) { + units = ((UnitsEffectiveStatementImpl)stmt).argument(); + builder.setUnits(units); + } + } - EffectiveStatement typeEffectiveSubstatement = firstEffectiveSubstatementOfType(TypeDefinition.class); - this.type = TypeUtils.getTypeFromEffectiveStatement(typeEffectiveSubstatement); + defaultStr = dflt; + unitsStr = units; + type = builder.build(); } @Override @@ -73,10 +96,7 @@ public final class LeafEffectiveStatementImpl extends AbstractEffectiveDataSchem if (this == obj) { return true; } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { + if (!(obj instanceof LeafEffectiveStatementImpl)) { return false; } LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj; diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafListEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafListEffectiveStatementImpl.java index 1963d6b79a..6d9a5f6f42 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafListEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafListEffectiveStatementImpl.java @@ -15,16 +15,19 @@ import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.LeafListStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; +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.stmt.rfc6020.TypeUtils; +import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; public final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode implements LeafListSchemaNode, DerivableSchemaNode { - private final LeafListSchemaNode original; + private static final String ORDER_BY_USER_KEYWORD = "user"; private final TypeDefinition type; + private final LeafListSchemaNode original; private final boolean userOrdered; - private static final String ORDER_BY_USER_KEYWORD = "user"; public LeafListEffectiveStatementImpl( final StmtContext> ctx) { @@ -32,15 +35,34 @@ public final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataS this.original = ctx.getOriginalCtx() == null ? null : (LeafListSchemaNode) ctx.getOriginalCtx() .buildEffective(); - OrderedByEffectiveStatementImpl orderedByStmt = firstEffective(OrderedByEffectiveStatementImpl.class); - if (orderedByStmt != null && orderedByStmt.argument().equals(ORDER_BY_USER_KEYWORD)) { - this.userOrdered = true; - } else { - this.userOrdered = false; + final TypeEffectiveStatement typeStmt = firstSubstatementOfType(TypeEffectiveStatement.class); + if (typeStmt == null) { + throw new SourceException("Leaf-list is missing a 'type' statement", ctx.getStatementSourceReference()); + } + + final ConcreteTypeBuilder builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(), + ctx.getSchemaPath().get()); + boolean isUserOrdered = false; + for (EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof OrderedByEffectiveStatementImpl) { + isUserOrdered = ORDER_BY_USER_KEYWORD.equals(stmt.argument()); + } + + if (stmt instanceof DefaultEffectiveStatementImpl) { + builder.setDefaultValue(stmt.argument()); + } else if (stmt instanceof DescriptionEffectiveStatementImpl) { + builder.setDescription(((DescriptionEffectiveStatementImpl)stmt).argument()); + } else if (stmt instanceof ReferenceEffectiveStatementImpl) { + builder.setReference(((ReferenceEffectiveStatementImpl)stmt).argument()); + } else if (stmt instanceof StatusEffectiveStatementImpl) { + builder.setStatus(((StatusEffectiveStatementImpl)stmt).argument()); + } else if (stmt instanceof UnitsEffectiveStatementImpl) { + builder.setUnits(((UnitsEffectiveStatementImpl)stmt).argument()); + } } - EffectiveStatement typeEffectiveSubstatement = firstEffectiveSubstatementOfType(TypeDefinition.class); - this.type = TypeUtils.getTypeFromEffectiveStatement(typeEffectiveSubstatement); + type = builder.build(); + userOrdered = isUserOrdered; } @Override diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/TypeDefEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/TypeDefEffectiveStatementImpl.java index b0db7d407f..576441fa53 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/TypeDefEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/TypeDefEffectiveStatementImpl.java @@ -7,194 +7,121 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; -import com.google.common.base.Optional; -import com.google.common.collect.ImmutableList; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.Collection; +import java.util.Map; import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; 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.TypeDefinitionAware; +import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace; +import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition; +import org.opendaylight.yangtools.yang.model.api.meta.StatementSource; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement; -import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; -import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; -import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; -import org.opendaylight.yangtools.yang.model.util.ExtendedType.Builder; -import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace; +import org.opendaylight.yangtools.yang.model.util.type.DerivedTypeBuilder; +import org.opendaylight.yangtools.yang.model.util.type.DerivedTypes; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Decimal64SpecificationEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LengthEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.PatternEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.RangeEffectiveStatementImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public final class TypeDefEffectiveStatementImpl extends AbstractEffectiveSchemaNode implements - TypeDefinition>, TypedefEffectiveStatement { - private final TypeDefinition baseType; - private final String defaultValue; - private final String units; - private final List ranges; - private final List lengths; - private final List patterns; - private final Integer fractionDigits; - private ExtendedType extendedType = null; + TypedefEffectiveStatement { + private static final Logger LOG = LoggerFactory.getLogger(TypeDefEffectiveStatementImpl.class); + private final TypeDefinition typeDefinition; + private TypeEffectiveStatement typeStatement; public TypeDefEffectiveStatementImpl(final StmtContext ctx) { super(ctx); - baseType = parseBaseTypeFromCtx(ctx); - - UnitsEffectiveStatementImpl unitsStmt = firstEffective(UnitsEffectiveStatementImpl.class); - this.units = (unitsStmt == null) ? null : unitsStmt.argument(); - DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class); - this.defaultValue = (defaultStmt == null) ? null : defaultStmt.argument(); - - EffectiveStatementBase typeEffectiveStmt = firstSubstatementOfType(TypeDefinition.class, - EffectiveStatementBase.class); - ranges = initRanges(typeEffectiveStmt); - lengths = initLengths(typeEffectiveStmt); - patterns = initPatterns(typeEffectiveStmt); - - // due to compatibility problems with original yang parser - // :FIXME try to find out better solution - if (typeEffectiveStmt.argument().equals(TypeUtils.DECIMAL64) && ranges.isEmpty()) { - fractionDigits = null; - } else { - fractionDigits = initFractionDigits(typeEffectiveStmt); - } - } - - private TypeDefinition parseBaseTypeFromCtx(final StmtContext ctx) { - - TypeDefinition baseTypeInit; - - QName baseTypeQName = Utils.qNameFromArgument(ctx, - StmtContextUtils.firstAttributeOf(ctx.declaredSubstatements(), TypeStatement.class)); - if (TypeUtils.isYangBuiltInTypeString(baseTypeQName.getLocalName())) { - baseTypeInit = TypeUtils.getYangPrimitiveTypeFromString(baseTypeQName.getLocalName()); - if (baseTypeInit == null) { - baseTypeInit = firstSubstatementOfType(TypeDefinition.class); - - // due to compatibility problems with original yang parser - // :FIXME try to find out better solution - if (baseTypeInit instanceof Decimal64SpecificationEffectiveStatementImpl) { - Decimal64SpecificationEffectiveStatementImpl decimal64 = (Decimal64SpecificationEffectiveStatementImpl) baseTypeInit; - if (decimal64.isExtended()) { - baseTypeInit = decimal64.getBaseType(); - } + final TypeEffectiveStatement typeEffectiveStmt = firstSubstatementOfType(TypeEffectiveStatement.class); + final DerivedTypeBuilder builder = DerivedTypes.derivedTypeBuilder(typeEffectiveStmt.getTypeDefinition(), + ctx.getSchemaPath().get()); + for (EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof DefaultEffectiveStatementImpl) { + builder.setDefaultValue(stmt.argument()); + } else if (stmt instanceof DescriptionEffectiveStatementImpl) { + builder.setDescription(((DescriptionEffectiveStatementImpl)stmt).argument()); + } else if (stmt instanceof ReferenceEffectiveStatementImpl) { + builder.setReference(((ReferenceEffectiveStatementImpl)stmt).argument()); + } else if (stmt instanceof StatusEffectiveStatementImpl) { + builder.setStatus(((StatusEffectiveStatementImpl)stmt).argument()); + } else if (stmt instanceof UnitsEffectiveStatementImpl) { + builder.setUnits(((UnitsEffectiveStatementImpl)stmt).argument()); + } else if (stmt instanceof UnknownEffectiveStatementImpl) { + // FIXME: should not directly implement, I think + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); + } else { + if (!(stmt instanceof TypeEffectiveStatement)) { + LOG.debug("Ignoring statement {}", stmt); } } - } else { - StmtContext baseTypeCtx = ctx - .getParentContext().getFromNamespace(TypeNamespace.class, baseTypeQName); - baseTypeInit = (TypeDefEffectiveStatementImpl) baseTypeCtx.buildEffective(); } - return baseTypeInit; - } - - private static Integer initFractionDigits(final EffectiveStatementBase typeEffectiveStmt) { - final FractionDigitsEffectiveStatementImpl fractionDigitsEffStmt = typeEffectiveStmt - .firstEffective(FractionDigitsEffectiveStatementImpl.class); - return fractionDigitsEffStmt != null ? fractionDigitsEffStmt.argument() : null; - } - - private static List initRanges(final EffectiveStatementBase typeEffectiveStmt) { - final RangeEffectiveStatementImpl rangeConstraints = typeEffectiveStmt - .firstEffective(RangeEffectiveStatementImpl.class); - return rangeConstraints != null ? rangeConstraints.argument() : Collections. emptyList(); + typeDefinition = builder.build(); } - private static List initLengths(final EffectiveStatementBase typeEffectiveStmt) { - final LengthEffectiveStatementImpl lengthConstraints = typeEffectiveStmt - .firstEffective(LengthEffectiveStatementImpl.class); - return lengthConstraints != null ? lengthConstraints.argument() : Collections. emptyList(); + @Override + public TypeDefinition getTypeDefinition() { + return typeDefinition; } - private static List initPatterns(final EffectiveStatementBase typeEffectiveStmt) { - final List patternConstraints = new ArrayList<>(); - - for (final EffectiveStatement effectiveStatement : typeEffectiveStmt.effectiveSubstatements()) { - if (effectiveStatement instanceof PatternEffectiveStatementImpl) { - final PatternConstraint pattern = ((PatternEffectiveStatementImpl) effectiveStatement).argument(); - if (pattern != null) { - patternConstraints.add(pattern); + public TypeEffectiveStatement asTypeEffectiveStatement() { + TypeEffectiveStatement ret = typeStatement; + if (ret == null) { + synchronized (this) { + ret = typeStatement; + if (ret == null) { + ret = new ProxyTypeEffectiveStatement(); + typeStatement = ret; } } } - return ImmutableList.copyOf(patternConstraints); - } - - @Override - public TypeDefinition getBaseType() { - return baseType; + return ret; } - @Override - public String getUnits() { - return units; - } - - @Override - public Object getDefaultValue() { - return defaultValue; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - public List getRangeConstraints() { - return ranges; - } - - public List getLengthConstraints() { - return lengths; - } - - public List getPatternConstraints() { - return patterns; - } + private final class ProxyTypeEffectiveStatement implements TypeEffectiveStatement { + @Override + public TypeStatement getDeclared() { + return null; + } - public Integer getFractionDigits() { - return fractionDigits; - } + @Override + public > V get(final Class namespace, final K identifier) { + return TypeDefEffectiveStatementImpl.this.get(namespace, identifier); + } - @Override - public TypeDefinition getTypeDefinition() { - if (extendedType != null) { - return extendedType; + @Override + public > Map getAll(final Class namespace) { + return TypeDefEffectiveStatementImpl.this.getAll(namespace); } - Builder extendedTypeBuilder; - if (baseType instanceof TypeDefinitionAware) { - TypeDefinitionAware typeDefBaseType = (TypeDefinitionAware) baseType; - extendedTypeBuilder = ExtendedType.builder(getQName(), typeDefBaseType.getTypeDefinition(), - Optional.fromNullable(getDescription()), Optional.fromNullable(getReference()), getPath()); - } else { - extendedTypeBuilder = ExtendedType.builder(getQName(), baseType, Optional.fromNullable(getDescription()), - Optional.fromNullable(getReference()), getPath()); + @Override + public Collection> effectiveSubstatements() { + // FIXME: this is tricky + throw new UnsupportedOperationException("Not implemented yet"); } - extendedTypeBuilder.defaultValue(defaultValue); - extendedTypeBuilder.units(units); + @Override + public StatementDefinition statementDefinition() { + return Rfc6020Mapping.TYPE; + } - extendedTypeBuilder.fractionDigits(fractionDigits); - extendedTypeBuilder.ranges(ranges); - extendedTypeBuilder.lengths(lengths); - extendedTypeBuilder.patterns(patterns); + @Override + public String argument() { + return getQName().getLocalName(); + } - extendedType = extendedTypeBuilder.build(); + @Override + public StatementSource getStatementSource() { + return StatementSource.CONTEXT; + } - return extendedType; + @Override + public TypeDefinition getTypeDefinition() { + return TypeDefEffectiveStatementImpl.this.getTypeDefinition(); + } } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/AbstractBuiltinEffectiveStatement.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/AbstractBuiltinEffectiveStatement.java new file mode 100644 index 0000000000..2e52a40a5c --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/AbstractBuiltinEffectiveStatement.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import com.google.common.collect.ImmutableList; +import java.util.Collection; +import java.util.Map; +import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace; +import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition; +import org.opendaylight.yangtools.yang.model.api.meta.StatementSource; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; + +abstract class AbstractBuiltinEffectiveStatement implements TypeEffectiveStatement { + @Override + public final TypeStatement getDeclared() { + return null; + } + + @Override + public final > V get(final Class namespace, final K identifier) { + // TODO Auto-generated method stub + return null; + } + + @Override + public final > Map getAll(final Class namespace) { + // TODO Auto-generated method stub + return null; + } + + @Override + public final Collection> effectiveSubstatements() { + return ImmutableList.of(); + } + + @Override + public final StatementDefinition statementDefinition() { + return Rfc6020Mapping.TYPE; + } + + @Override + public final String argument() { + return getTypeDefinition().getQName().getLocalName(); + } + + @Override + public final StatementSource getStatementSource() { + return StatementSource.CONTEXT; + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/AbstractTypeEffectiveStatement.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/AbstractTypeEffectiveStatement.java new file mode 100644 index 0000000000..a980dcf652 --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/AbstractTypeEffectiveStatement.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.util.type.TypeBuilder; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; + +abstract class AbstractTypeEffectiveStatement> extends + DeclaredEffectiveStatementBase implements TypeEffectiveStatement { + private final T typeDefinition; + + protected AbstractTypeEffectiveStatement( + final StmtContext> ctx, + final TypeBuilder builder) { + super(ctx); + + for (EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); + } + } + + typeDefinition = builder.build(); + } + + @Override + public final T getTypeDefinition() { + return typeDefinition; + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BinaryEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BinaryEffectiveStatementImpl.java deleted file mode 100644 index a88dee54c9..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BinaryEffectiveStatementImpl.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import com.google.common.base.Optional; -import java.util.Collections; -import java.util.List; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.YangConstants; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -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.TypeStatement; -import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; - -public class BinaryEffectiveStatementImpl extends DeclaredEffectiveStatementBase - implements BinaryTypeDefinition { - - private static final String DESCRIPTION = "The binary built-in type represents any binary data, i.e., a sequence of octets."; - private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.8"; - private static final String UNITS = ""; - - private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, "binary"); - private static final SchemaPath PATH = SchemaPath.create(true, QNAME); - private static final Optional OPTIONAL_EMPTY = Optional.of(""); - private final List lengthConstraints; - - public BinaryEffectiveStatementImpl( - final StmtContext> ctx) { - super(ctx); - - final LengthConstraint lengthConstraint = - new LengthConstraintEffectiveImpl(0, Long.MAX_VALUE, OPTIONAL_EMPTY, OPTIONAL_EMPTY); - lengthConstraints = Collections.singletonList(lengthConstraint); - } - - @Override - public List getLengthConstraints() { - return lengthConstraints; - } - - @Override - public BinaryTypeDefinition getBaseType() { - return null; - } - - @Override - public String getUnits() { - return UNITS; - } - - @Override - public Object getDefaultValue() { - return null; - } - - @Override - public QName getQName() { - return QNAME; - } - - @Override - public SchemaPath getPath() { - return PATH; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return DESCRIPTION; - } - - @Override - public String getReference() { - return REFERENCE; - } - - @Override - public Status getStatus() { - return Status.CURRENT; - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BinaryTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BinaryTypeEffectiveStatementImpl.java new file mode 100644 index 0000000000..3c528f7a90 --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BinaryTypeEffectiveStatementImpl.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; +import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException; +import org.opendaylight.yangtools.yang.model.util.type.LengthRestrictedTypeBuilder; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +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; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; + +public final class BinaryTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase + implements TypeEffectiveStatement { + private final BinaryTypeDefinition typeDefinition; + + public BinaryTypeEffectiveStatementImpl( + final StmtContext> ctx, + final BinaryTypeDefinition baseType) { + super(ctx); + + final LengthRestrictedTypeBuilder builder = + RestrictedTypes.newBinaryBuilder(baseType, TypeUtils.typeEffectiveSchemaPath(ctx)); + + for (EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof LengthEffectiveStatementImpl) { + builder.setLengthAlternatives(((LengthEffectiveStatementImpl)stmt).argument()); + } + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); + } + } + + try { + typeDefinition = builder.build(); + } catch (InvalidLengthConstraintException e) { + final LengthConstraint c = e.getOffendingConstraint(); + throw new SourceException(String.format("Invalid length constraint: <%s, %s>", c.getMin(), c.getMax()), + ctx.getStatementSourceReference(), e); + } + } + + @Override + public BinaryTypeDefinition getTypeDefinition() { + return typeDefinition; + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java index 06726e7f21..05374ce27c 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java @@ -7,160 +7,68 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; -import com.google.common.collect.ImmutableList; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -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.TypeEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification; import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.BaseTypes; -import org.opendaylight.yangtools.yang.model.util.BitsType; +import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit; +import org.opendaylight.yangtools.yang.model.util.BitImpl; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; +import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder; 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.effective.DeclaredEffectiveStatementBase; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; -public class BitsSpecificationEffectiveStatementImpl extends - DeclaredEffectiveStatementBase - implements BitsTypeDefinition, TypeEffectiveStatement { +public final class BitsSpecificationEffectiveStatementImpl extends + DeclaredEffectiveStatementBase implements TypeEffectiveStatement { - private static final QName QNAME = BaseTypes.BITS_QNAME; - private static final String DESCRIPTION = "The bits built-in type represents a bit set. " - + "That is, a bits value is a set of flags identified by small integer position " - + "numbers starting at 0. Each bit number has an assigned name."; - - private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.7"; - private static final String UNITS = ""; - private final SchemaPath path; - private final List bits; - private BitsType bitsTypeInstance = null; + private final BitsTypeDefinition typeDefinition; public BitsSpecificationEffectiveStatementImpl(final StmtContext> ctx) { super(ctx); - List bitsInit = new ArrayList<>(); - - path = ctx.getParentContext().getSchemaPath().get().createChild(QNAME); - - for (final EffectiveStatement effectiveStatement : effectiveSubstatements()) { - if (effectiveStatement instanceof Bit) { - bitsInit.add(((Bit) effectiveStatement)); + final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(ctx.getSchemaPath().get()); + Long highestPosition = null; + for (final EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof Bit) { + Bit b = (Bit) stmt; + + if (b.getPosition() == null) { + final Long newPos; + if (highestPosition == null) { + newPos = 0L; + } else if (highestPosition != 4294967295L) { + newPos = highestPosition + 1; + } else { + throw new SourceException("Bit " + b + " must have a position statement", + ctx.getStatementSourceReference()); + } + + b = new BitImpl(newPos, b.getQName(), b.getPath(), b.getDescription(), b.getReference(), + b.getStatus(), b.getUnknownSchemaNodes()); + } + + if (b.getPosition() < 0L || b.getPosition() > 4294967295L) { + throw new SourceException("Bit " + b + " has illegal position", ctx.getStatementSourceReference()); + } + + if (highestPosition == null || highestPosition < b.getPosition()) { + highestPosition = b.getPosition(); + } + + builder.addBit(b); + } + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); } } - bits = ImmutableList.copyOf(bitsInit); - } - - @Override - public List getBits() { - return bits; - } - - @Override - public BitsTypeDefinition getBaseType() { - return null; - } - - @Override - public String getUnits() { - return UNITS; - } - - @Override - public Object getDefaultValue() { - // FIXME: Should return real value - return null; - } - - @Override - public QName getQName() { - return QNAME; - } - - @Override - public SchemaPath getPath() { - return path; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return DESCRIPTION; - } - - @Override - public String getReference() { - return REFERENCE; - } - - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + Objects.hashCode(bits); - result = prime * result + QNAME.hashCode(); - result = prime * result + path.hashCode(); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - BitsSpecificationEffectiveStatementImpl other = (BitsSpecificationEffectiveStatementImpl) obj; - return Objects.equals(bits, other.bits) && Objects.equals(path, other.path); - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(BitsSpecificationEffectiveStatementImpl.class.getSimpleName()); - builder.append(" [name="); - builder.append(QNAME); - builder.append(", path="); - builder.append(path); - builder.append(", description="); - builder.append(DESCRIPTION); - builder.append(", reference="); - builder.append(REFERENCE); - builder.append(", bits="); - builder.append(bits); - builder.append(", units="); - builder.append(UNITS); - builder.append("]"); - return builder.toString(); + typeDefinition = builder.build(); } @Override - public TypeDefinition getTypeDefinition() { - if(bitsTypeInstance != null) { - return bitsTypeInstance; - } - - bitsTypeInstance = BitsType.create(path, bits); - - return bitsTypeInstance; + public BitsTypeDefinition getTypeDefinition() { + return typeDefinition; } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsTypeEffectiveStatementImpl.java new file mode 100644 index 0000000000..a7fc6ec81f --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsTypeEffectiveStatementImpl.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; + +public final class BitsTypeEffectiveStatementImpl extends AbstractTypeEffectiveStatement { + public BitsTypeEffectiveStatementImpl( + final StmtContext> ctx, + final BitsTypeDefinition baseType) { + super(ctx, RestrictedTypes.newBitsBuilder(baseType, TypeUtils.typeEffectiveSchemaPath(ctx))); + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BooleanEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BooleanEffectiveStatementImpl.java deleted file mode 100644 index 3981beabba..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BooleanEffectiveStatementImpl.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import java.util.Collections; -import java.util.List; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.YangConstants; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -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.TypeStatement; -import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.BaseTypes; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; - -public class BooleanEffectiveStatementImpl extends DeclaredEffectiveStatementBase - implements BooleanTypeDefinition { - - public static final String LOCAL_NAME = TypeUtils.BOOLEAN; - private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, LOCAL_NAME); - private static final SchemaPath PATH = SchemaPath.create(true, QNAME); - private static final String DESCRIPTION = "The boolean built-in type represents a boolean value."; - private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.5"; - private static final String UNITS = ""; - - public BooleanEffectiveStatementImpl( - final StmtContext> ctx) { - super(ctx); - } - - @Override - public BooleanTypeDefinition getBaseType() { - return null; - } - - @Override - public String getUnits() { - return UNITS; - } - - @Override - public Object getDefaultValue() { - return null; - } - - @Override - public QName getQName() { - return QNAME; - } - - @Override - public SchemaPath getPath() { - return PATH; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return DESCRIPTION; - } - - @Override - public String getReference() { - return REFERENCE; - } - - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(getClass().getSimpleName()); - builder.append(" [name="); - builder.append(BaseTypes.BOOLEAN_QNAME); - builder.append(", path="); - builder.append(PATH); - builder.append("]"); - return builder.toString(); - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BooleanTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BooleanTypeEffectiveStatementImpl.java new file mode 100644 index 0000000000..a4fee258d8 --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BooleanTypeEffectiveStatementImpl.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; + +public final class BooleanTypeEffectiveStatementImpl extends AbstractTypeEffectiveStatement { + public BooleanTypeEffectiveStatementImpl( + final StmtContext> ctx, + final BooleanTypeDefinition baseType) { + super(ctx, RestrictedTypes.newBooleanBuilder(baseType, TypeUtils.typeEffectiveSchemaPath(ctx))); + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BuiltinEffectiveStatements.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BuiltinEffectiveStatements.java new file mode 100644 index 0000000000..c0919c959a --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BuiltinEffectiveStatements.java @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; + +public final class BuiltinEffectiveStatements { + private BuiltinEffectiveStatements() { + throw new UnsupportedOperationException(); + } + + public static final TypeEffectiveStatement BINARY = new AbstractBuiltinEffectiveStatement() { + @Override + public TypeDefinition getTypeDefinition() { + return BaseTypes.binaryType(); + } + }; + public static final TypeEffectiveStatement BOOLEAN = new AbstractBuiltinEffectiveStatement() { + @Override + public TypeDefinition getTypeDefinition() { + return BaseTypes.booleanType(); + } + }; + public static final TypeEffectiveStatement EMPTY = new AbstractBuiltinEffectiveStatement() { + @Override + public TypeDefinition getTypeDefinition() { + return BaseTypes.emptyType(); + } + }; + public static final TypeEffectiveStatement INSTANCE_IDENTIFIER = new AbstractBuiltinEffectiveStatement() { + @Override + public TypeDefinition getTypeDefinition() { + return BaseTypes.instanceIdentifierType(); + } + }; + public static final TypeEffectiveStatement INT8 = new AbstractBuiltinEffectiveStatement() { + @Override + public TypeDefinition getTypeDefinition() { + return BaseTypes.int8Type(); + } + }; + public static final TypeEffectiveStatement INT16 = new AbstractBuiltinEffectiveStatement() { + @Override + public TypeDefinition getTypeDefinition() { + return BaseTypes.int16Type(); + } + }; + public static final TypeEffectiveStatement INT32 = new AbstractBuiltinEffectiveStatement() { + @Override + public TypeDefinition getTypeDefinition() { + return BaseTypes.int32Type(); + } + }; + public static final TypeEffectiveStatement INT64 = new AbstractBuiltinEffectiveStatement() { + @Override + public TypeDefinition getTypeDefinition() { + return BaseTypes.int64Type(); + } + }; + public static final TypeEffectiveStatement STRING = new AbstractBuiltinEffectiveStatement() { + @Override + public TypeDefinition getTypeDefinition() { + return BaseTypes.stringType(); + } + }; + public static final TypeEffectiveStatement UINT8 = new AbstractBuiltinEffectiveStatement() { + @Override + public TypeDefinition getTypeDefinition() { + return BaseTypes.uint8Type(); + } + }; + public static final TypeEffectiveStatement UINT16 = new AbstractBuiltinEffectiveStatement() { + @Override + public TypeDefinition getTypeDefinition() { + return BaseTypes.uint16Type(); + } + }; + public static final TypeEffectiveStatement UINT32 = new AbstractBuiltinEffectiveStatement() { + @Override + public TypeDefinition getTypeDefinition() { + return BaseTypes.uint32Type(); + } + }; + public static final TypeEffectiveStatement UINT64 = new AbstractBuiltinEffectiveStatement() { + @Override + public TypeDefinition getTypeDefinition() { + return BaseTypes.uint64Type(); + } + }; +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Decimal64SpecificationEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Decimal64SpecificationEffectiveStatementImpl.java index e9076efdc2..24f3ba456d 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Decimal64SpecificationEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Decimal64SpecificationEffectiveStatementImpl.java @@ -7,231 +7,46 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; -import com.google.common.base.Optional; -import com.google.common.collect.ImmutableList; -import java.math.BigDecimal; -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.YangConstants; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -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.TypeEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.Decimal64Specification; import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; -import org.opendaylight.yangtools.yang.model.util.Decimal64; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; -import org.opendaylight.yangtools.yang.model.util.ExtendedType.Builder; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; +import org.opendaylight.yangtools.yang.model.util.type.DecimalTypeBuilder; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.FractionDigitsEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; -public class Decimal64SpecificationEffectiveStatementImpl extends +public final class Decimal64SpecificationEffectiveStatementImpl extends DeclaredEffectiveStatementBase - implements DecimalTypeDefinition, TypeEffectiveStatement { + implements TypeEffectiveStatement { - private static final String UNITS = ""; - private static final BigDecimal DEFAULT_VALUE = null; - private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, TypeUtils.DECIMAL64); - - private static final String DESCRIPTION = "The decimal64 type represents a subset of the real numbers, which can " - + "be represented by decimal numerals. The value space of decimal64 is the set of numbers that can " - + "be obtained by multiplying a 64-bit signed integer by a negative power of ten, i.e., expressible as " - + "'i x 10^-n' where i is an integer64 and n is an integer between 1 and 18, inclusively."; - - private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.3"; - private static final BigDecimal MIN_VALUE = new BigDecimal("-922337203685477580.8"); - private static final BigDecimal MAX_VALUE = new BigDecimal("922337203685477580.7"); - private static final List DEFAULT_RANGE_STATEMENTS; - static { - final String rangeDescription = "Integer values between " + MIN_VALUE - + " and " + MAX_VALUE + ", inclusively."; - final String rangeReference = RangeConstraintEffectiveImpl.DEFAULT_REFERENCE; - - DEFAULT_RANGE_STATEMENTS = ImmutableList.of( - new RangeConstraintEffectiveImpl(MIN_VALUE, MAX_VALUE, Optional.of(rangeDescription), - Optional.of(rangeReference))); - } - - private List rangeConstraints; - private Integer fractionDigits; - private SchemaPath path; - private QName extendedTypeQName; - - private ExtendedType extendedType; - private final boolean isExtended; - private Decimal64 decimal64Instance = null; + private final DecimalTypeDefinition typeDefinition; public Decimal64SpecificationEffectiveStatementImpl( final StmtContext> ctx) { super(ctx); - for (final EffectiveStatement effectiveStatement : effectiveSubstatements()) { - if (effectiveStatement instanceof FractionDigitsEffectiveStatementImpl) { - fractionDigits = ((FractionDigitsEffectiveStatementImpl) effectiveStatement) - .argument(); - } - } + final DecimalTypeBuilder builder = BaseTypes.decimalTypeBuilder(ctx.getSchemaPath().get()); - List initRanges = initRanges(); - - if (!initRanges.isEmpty() && validateRanges(initRanges)) { - isExtended = true; - rangeConstraints = ImmutableList.copyOf(initRanges); - SchemaPath parentPath = ctx.getParentContext().getSchemaPath().get(); - extendedTypeQName = QName.create(parentPath.getLastComponent().getModule(), QNAME.getLocalName()); - path = parentPath.createChild(extendedTypeQName); - } else { - isExtended = false; - rangeConstraints = DEFAULT_RANGE_STATEMENTS; - path = ctx.getParentContext().getSchemaPath().get().createChild(QNAME); - } - } - - private static boolean validateRanges(final List initRanges) { - for (RangeConstraint rangeConstraint : initRanges) { - - String maxValueString = rangeConstraint.getMax().toString(); - String minValueString = rangeConstraint.getMin().toString(); - - if ((!"max".equals(maxValueString) && MAX_VALUE.compareTo(new BigDecimal(maxValueString)) < 0) - || (!"min".equals(minValueString) && MIN_VALUE.compareTo(new BigDecimal(minValueString)) > 0)) { - return false; + for (final EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof FractionDigitsEffectiveStatementImpl) { + builder.setFractionDigits(((FractionDigitsEffectiveStatementImpl) stmt).argument()); } - } - return true; - } - - private List initRanges() { - final RangeEffectiveStatementImpl rangeConstraintsStmt = firstEffective(RangeEffectiveStatementImpl.class); - return rangeConstraintsStmt != null ? rangeConstraintsStmt.argument() : Collections. emptyList(); - } - - public boolean isExtended() { - return isExtended; - } - - @Override - public List getRangeConstraints() { - return rangeConstraints; - } - - @Override - public Integer getFractionDigits() { - return fractionDigits; - } - - @Override - public DecimalTypeDefinition getBaseType() { - if (isExtended) { - if (decimal64Instance == null) { - decimal64Instance = Decimal64.create(path, fractionDigits); + if (stmt instanceof RangeEffectiveStatementImpl) { + builder.setRangeAlternatives(((RangeEffectiveStatementImpl)stmt).argument()); + } + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); } - return decimal64Instance; - } else { - return null; - } - } - - @Override - public String getUnits() { - return UNITS; - } - - @Override - public Object getDefaultValue() { - return DEFAULT_VALUE; - } - - @Override - public QName getQName() { - return QNAME; - } - - @Override - public SchemaPath getPath() { - return path; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return DESCRIPTION; - } - - @Override - public String getReference() { - return REFERENCE; - } - - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + Objects.hashCode(QNAME); - result = prime * result + Objects.hashCode(path); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; } - Decimal64SpecificationEffectiveStatementImpl other = (Decimal64SpecificationEffectiveStatementImpl) obj; - return Objects.equals(path, other.path); - } - @Override - public String toString() { - return Decimal64SpecificationEffectiveStatementImpl.class.getSimpleName() - + "[qName=" + QNAME + ", fractionDigits=" + fractionDigits + "]"; + typeDefinition = builder.build(); } @Override - public TypeDefinition getTypeDefinition() { - - if (decimal64Instance == null) { - decimal64Instance = Decimal64.create(path, fractionDigits); - } - - if (!isExtended) { - return decimal64Instance; - } - - if (extendedType != null) { - return extendedType; - } - - Builder extendedTypeBuilder = ExtendedType.builder(path.getLastComponent(), decimal64Instance, - Optional.absent(), Optional.absent(), path); - - extendedTypeBuilder.fractionDigits(fractionDigits); - extendedTypeBuilder.ranges(rangeConstraints); - - extendedType = extendedTypeBuilder.build(); - - return extendedType; + public DecimalTypeDefinition getTypeDefinition() { + return typeDefinition; } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/DecimalTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/DecimalTypeEffectiveStatementImpl.java new file mode 100644 index 0000000000..e867e4c569 --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/DecimalTypeEffectiveStatementImpl.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.FractionDigitsEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; + +public final class DecimalTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase + implements TypeEffectiveStatement { + + private final DecimalTypeDefinition typeDefinition; + + public DecimalTypeEffectiveStatementImpl( + final StmtContext> ctx, + final DecimalTypeDefinition baseType) { + super(ctx); + + final RangeRestrictedTypeBuilder builder = + RestrictedTypes.newDecima64Builder(baseType, TypeUtils.typeEffectiveSchemaPath(ctx)); + + for (EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof RangeEffectiveStatementImpl) { + builder.setRangeAlternatives(((RangeEffectiveStatementImpl)stmt).argument()); + } + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); + } + if (stmt instanceof FractionDigitsEffectiveStatementImpl) { + final Integer digits = ((FractionDigitsEffectiveStatementImpl)stmt).argument(); +// FIXME: activate this and fix models +// if (!baseType.getFractionDigits().equals(digits)) { +// throw new SourceException(String.format("Cannot override fraction-digits from base type %s", +// baseType), ctx.getStatementSourceReference()); +// } + } + } + + typeDefinition = builder.build(); + } + + @Override + public DecimalTypeDefinition getTypeDefinition() { + return typeDefinition; + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EmptyEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EmptyEffectiveStatementImpl.java deleted file mode 100644 index 71a0b0fa0a..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EmptyEffectiveStatementImpl.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import java.util.Collections; -import java.util.List; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.YangConstants; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -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.TypeStatement; -import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; - -public class EmptyEffectiveStatementImpl extends DeclaredEffectiveStatementBase - implements EmptyTypeDefinition { - - public static final String LOCAL_NAME = TypeUtils.EMPTY; - private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, LOCAL_NAME); - private static final SchemaPath PATH = SchemaPath.create(true, QNAME); - private static final String DESCRIPTION = "The empty built-in type represents a leaf that does not have any value, it conveys information by its presence or absence."; - private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#page-131"; - - public EmptyEffectiveStatementImpl( - final StmtContext> ctx) { - super(ctx); - } - - @Override - public EmptyTypeDefinition getBaseType() { - return null; - } - - @Override - public String getUnits() { - return null; - } - - @Override - public Object getDefaultValue() { - return null; - } - - @Override - public QName getQName() { - return QNAME; - } - - @Override - public SchemaPath getPath() { - return PATH; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return DESCRIPTION; - } - - @Override - public String getReference() { - return REFERENCE; - } - - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public String toString() { - return "type empty " + QNAME; - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EmptyTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EmptyTypeEffectiveStatementImpl.java new file mode 100644 index 0000000000..d63cc3c6fe --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EmptyTypeEffectiveStatementImpl.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; + +public final class EmptyTypeEffectiveStatementImpl extends AbstractTypeEffectiveStatement { + public EmptyTypeEffectiveStatementImpl( + final StmtContext> ctx, + final EmptyTypeDefinition baseType) { + super(ctx, RestrictedTypes.newEmptyBuilder(baseType, TypeUtils.typeEffectiveSchemaPath(ctx))); + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EnumSpecificationEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EnumSpecificationEffectiveStatementImpl.java index 564d9296df..66fc7d0d95 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EnumSpecificationEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EnumSpecificationEffectiveStatementImpl.java @@ -7,176 +7,65 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; -import com.google.common.base.Optional; -import com.google.common.collect.ImmutableList; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.YangConstants; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -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.TypeEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification; import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.EnumerationType; +import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair; +import org.opendaylight.yangtools.yang.model.util.EnumPairImpl; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; +import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder; 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.effective.DeclaredEffectiveStatementBase; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; -public class EnumSpecificationEffectiveStatementImpl extends - DeclaredEffectiveStatementBase implements EnumTypeDefinition, +public final class EnumSpecificationEffectiveStatementImpl extends + DeclaredEffectiveStatementBase implements TypeEffectiveStatement { - private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, "enumeration"); - - private static final String DESCRIPTION = "The enumeration built-in type represents values from a set of assigned names."; - private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.6"; - private static final String UNITS = ""; - - private final SchemaPath path; - private final EnumPair defaultEnum; - private final List enums; - private EnumerationType enumerationTypeInstance = null; + private final EnumTypeDefinition typeDefinition; public EnumSpecificationEffectiveStatementImpl(final StmtContext> ctx) { super(ctx); - List enumsInit = new ArrayList<>(); - - path = ctx.getParentContext().getSchemaPath().get().createChild(QNAME); - - for (final EffectiveStatement effectiveStatement : effectiveSubstatements()) { - if (effectiveStatement instanceof EnumPair) { - enumsInit.add((EnumPair) effectiveStatement); + final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get()); + Integer highestValue = null; + for (final EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof EnumPair) { + EnumPair p = (EnumPair) stmt; + + if (p.getValue() == null) { + final Integer newValue; + if (highestValue == null) { + newValue = 0; + } else if (highestValue != 2147483647) { + newValue = highestValue + 1; + } else { + throw new SourceException("Enum " + p + " must have a value statement", + ctx.getStatementSourceReference()); + } + + p = new EnumPairImpl(p.getName(), newValue, p.getPath(), p.getDescription(), p.getReference(), + p.getStatus(), p.getUnknownSchemaNodes()); + } + + if (highestValue == null || highestValue < p.getValue()) { + highestValue = p.getValue(); + } + + builder.addEnum(p); + } + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); } } - // FIXME: get from parentContext - defaultEnum = null; - enums = ImmutableList.copyOf(enumsInit); - } - - @Override - public List getValues() { - return enums; - } - - @Override - public EnumTypeDefinition getBaseType() { - return null; - } - - @Override - public String getUnits() { - return UNITS; - } - - @Override - public Object getDefaultValue() { - return defaultEnum; - } - - @Override - public QName getQName() { - return QNAME; - } - - @Override - public SchemaPath getPath() { - return path; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return DESCRIPTION; - } - - @Override - public String getReference() { - return REFERENCE; - } - - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + Objects.hashCode(defaultEnum); - result = prime * result + Objects.hashCode(enums); - result = prime * result + QNAME.hashCode(); - result = prime * result + Objects.hashCode(path); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - EnumSpecificationEffectiveStatementImpl other = (EnumSpecificationEffectiveStatementImpl) obj; - if (!Objects.equals(defaultEnum, other.defaultEnum)) { - return false; - } - if (!Objects.equals(enums, other.enums)) { - return false; - } - if (!Objects.equals(path, other.path)) { - return false; - } - return true; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(EnumSpecificationEffectiveStatementImpl.class.getSimpleName()); - builder.append(" [name="); - builder.append(QNAME); - builder.append(", path="); - builder.append(path); - builder.append(", description="); - builder.append(DESCRIPTION); - builder.append(", reference="); - builder.append(REFERENCE); - builder.append(", defaultEnum="); - builder.append(defaultEnum); - builder.append(", enums="); - builder.append(enums); - builder.append(", units="); - builder.append(UNITS); - builder.append("]"); - return builder.toString(); + typeDefinition = builder.build(); } @Override - public TypeDefinition getTypeDefinition() { - - if (enumerationTypeInstance !=null) { - return enumerationTypeInstance; - } - - // FIXME: set defaultValue as parameter - enumerationTypeInstance = EnumerationType.create(path, enums, Optional.absent()); - - return enumerationTypeInstance; + public EnumTypeDefinition getTypeDefinition() { + return typeDefinition; } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EnumTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EnumTypeEffectiveStatementImpl.java new file mode 100644 index 0000000000..e5f53ef9b5 --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EnumTypeEffectiveStatementImpl.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; + +public final class EnumTypeEffectiveStatementImpl extends AbstractTypeEffectiveStatement { + public EnumTypeEffectiveStatementImpl( + final StmtContext> ctx, + final EnumTypeDefinition baseType) { + super(ctx, RestrictedTypes.newEnumerationBuilder(baseType, TypeUtils.typeEffectiveSchemaPath(ctx))); + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IdentityRefSpecificationEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IdentityRefSpecificationEffectiveStatementImpl.java index 7b31a3f42c..c376d7d714 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IdentityRefSpecificationEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IdentityRefSpecificationEffectiveStatementImpl.java @@ -7,123 +7,50 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; -import java.util.Collections; -import java.util.List; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.YangConstants; import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -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.IdentityStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.IdentityRefSpecification; import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.IdentityrefType; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; +import org.opendaylight.yangtools.yang.model.util.type.IdentityrefTypeBuilder; import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BaseEffectiveStatementImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; -public class IdentityRefSpecificationEffectiveStatementImpl extends - DeclaredEffectiveStatementBase implements IdentityrefTypeDefinition, +public final class IdentityRefSpecificationEffectiveStatementImpl extends + DeclaredEffectiveStatementBase implements TypeEffectiveStatement { - private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, TypeUtils.IDENTITY_REF); - - private static final String DESCRIPTION = "The identityref type is used to reference an existing identity."; - private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.10"; - - private static final String UNITS = ""; - - private final SchemaPath path; - - private final IdentitySchemaNode identity; - private IdentityrefType identityRefTypeInstance = null; + private final IdentityrefTypeDefinition typeDefinition; public IdentityRefSpecificationEffectiveStatementImpl( final StmtContext> ctx) { super(ctx); - path = ctx.getParentContext().getSchemaPath().get().createChild(QNAME); - - final BaseEffectiveStatementImpl base = firstEffective(BaseEffectiveStatementImpl.class); - if (base != null) { - QName identityQName = base.argument(); - StmtContext> identityCtx = ctx.getFromNamespace(IdentityNamespace.class, identityQName); - identity = (IdentitySchemaNode) identityCtx.buildEffective(); - } else { - identity = null; + final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(ctx.getSchemaPath().get()); + for (final EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof BaseEffectiveStatementImpl) { + final BaseEffectiveStatementImpl base = firstEffective(BaseEffectiveStatementImpl.class); + final QName identityQName = base.argument(); + final StmtContext> identityCtx = + ctx.getFromNamespace(IdentityNamespace.class, identityQName); + builder.setIdentity((IdentitySchemaNode) identityCtx.buildEffective()); + } + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); + } } - } - @Override - public IdentitySchemaNode getIdentity() { - return identity; + typeDefinition = builder.build(); } @Override - public IdentityrefTypeDefinition getBaseType() { - return null; - } - - @Override - public String getUnits() { - return UNITS; - } - - @Override - public Object getDefaultValue() { - return identity; - } - - @Override - public QName getQName() { - return QNAME; - } - - @Override - public SchemaPath getPath() { - return path; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return DESCRIPTION; - } - - @Override - public String getReference() { - return REFERENCE; - } - - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public String toString() { - return "identityref " + identity.getQName().getLocalName(); - } - - @Override - public TypeDefinition getTypeDefinition() { - - if (identityRefTypeInstance != null) { - return identityRefTypeInstance; - } - - identityRefTypeInstance = IdentityrefType.create(path, identity); - - return identityRefTypeInstance; + public IdentityrefTypeDefinition getTypeDefinition() { + return typeDefinition; } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IdentityrefTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IdentityrefTypeEffectiveStatementImpl.java new file mode 100644 index 0000000000..91a561570a --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IdentityrefTypeEffectiveStatementImpl.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; + +public final class IdentityrefTypeEffectiveStatementImpl extends + AbstractTypeEffectiveStatement { + + public IdentityrefTypeEffectiveStatementImpl( + final StmtContext> ctx, + final IdentityrefTypeDefinition baseType) { + super(ctx, RestrictedTypes.newIdentityrefBuilder(baseType, TypeUtils.typeEffectiveSchemaPath(ctx))); + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/InstanceIdentifierSpecificationEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/InstanceIdentifierSpecificationEffectiveStatementImpl.java index 708e44251a..56befe7578 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/InstanceIdentifierSpecificationEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/InstanceIdentifierSpecificationEffectiveStatementImpl.java @@ -7,125 +7,45 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; -import java.util.Collections; -import java.util.List; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.YangConstants; -import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -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.TypeEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.InstanceIdentifierSpecification; import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; +import org.opendaylight.yangtools.yang.model.util.type.InstanceIdentifierTypeBuilder; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.RequireInstanceEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; -public class InstanceIdentifierSpecificationEffectiveStatementImpl extends - DeclaredEffectiveStatementBase implements InstanceIdentifierTypeDefinition, +public final class InstanceIdentifierSpecificationEffectiveStatementImpl extends + DeclaredEffectiveStatementBase implements TypeEffectiveStatement { - private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, "instance-identifier"); - private static final SchemaPath PATH = SchemaPath.create(true, QNAME); - private static final String DESCRIPTION = "The instance-identifier built-in type is used to " - + "uniquely identify a particular instance node in the data tree."; - private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.13"; - - private static final String UNITS = ""; - private final Boolean requireInstance; + private final InstanceIdentifierTypeDefinition typeDefinition; public InstanceIdentifierSpecificationEffectiveStatementImpl( final StmtContext> ctx) { super(ctx); - RequireInstanceEffectiveStatementImpl requireInstanceStmtCtx = firstEffective(RequireInstanceEffectiveStatementImpl.class); - requireInstance = (requireInstanceStmtCtx != null) ? requireInstanceStmtCtx.argument() : false; - } - - @Override - public RevisionAwareXPath getPathStatement() { - return null; - } - - @Override - public boolean requireInstance() { - return requireInstance; - } - - @Override - public InstanceIdentifierTypeDefinition getBaseType() { - return null; - } - - @Override - public String getUnits() { - return UNITS; - } - - @Override - public Object getDefaultValue() { - return null; - } - - @Override - public QName getQName() { - return QNAME; - } - - @Override - public SchemaPath getPath() { - return PATH; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return DESCRIPTION; - } - - @Override - public String getReference() { - return REFERENCE; - } + final InstanceIdentifierTypeBuilder builder = + RestrictedTypes.newInstanceIdentifierBuilder(BaseTypes.instanceIdentifierType(), ctx.getSchemaPath().get()); - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + requireInstance.hashCode(); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; + for (EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof RequireInstanceEffectiveStatementImpl) { + builder.setRequireInstance(((RequireInstanceEffectiveStatementImpl)stmt).argument()); + } + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); + } } - if (getClass() != obj.getClass()) { - return false; - } - InstanceIdentifierSpecificationEffectiveStatementImpl other = (InstanceIdentifierSpecificationEffectiveStatementImpl) obj; - return requireInstance.equals(other.requireInstance); + + typeDefinition = builder.build(); } @Override - public TypeDefinition getTypeDefinition() { - return InstanceIdentifierType.create(requireInstance); + public InstanceIdentifierTypeDefinition getTypeDefinition() { + return typeDefinition; } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/InstanceIdentifierTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/InstanceIdentifierTypeEffectiveStatementImpl.java new file mode 100644 index 0000000000..e1976256ac --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/InstanceIdentifierTypeEffectiveStatementImpl.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.InstanceIdentifierTypeBuilder; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.RequireInstanceEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; + +public final class InstanceIdentifierTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase + implements TypeEffectiveStatement { + private final InstanceIdentifierTypeDefinition typeDefinition; + + public InstanceIdentifierTypeEffectiveStatementImpl( + final StmtContext> ctx, + final InstanceIdentifierTypeDefinition baseType) { + super(ctx); + + final InstanceIdentifierTypeBuilder builder = + RestrictedTypes.newInstanceIdentifierBuilder(baseType, TypeUtils.typeEffectiveSchemaPath(ctx)); + + for (EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof RequireInstanceEffectiveStatementImpl) { + builder.setRequireInstance(((RequireInstanceEffectiveStatementImpl)stmt).argument()); + } + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); + } + } + + typeDefinition = builder.build(); + } + + @Override + public InstanceIdentifierTypeDefinition getTypeDefinition() { + return typeDefinition; + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int16EffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int16EffectiveStatementImpl.java deleted file mode 100644 index de6993540d..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int16EffectiveStatementImpl.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; -import org.opendaylight.yangtools.yang.model.util.BaseTypes; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; - -public class Int16EffectiveStatementImpl extends IntegerEffectiveImplBase { - - public static final String LOCAL_NAME = TypeUtils.INT16; - - private static final Number MIN_RANGE = Short.MIN_VALUE; - private static final Number MAX_RANGE = Short.MAX_VALUE; - - private static final String DESCRIPTION = LOCAL_NAME + " represents integer values between " + MIN_RANGE + " and " - + MAX_RANGE + ", inclusively."; - - public Int16EffectiveStatementImpl(final StmtContext> ctx) { - - super(ctx, BaseTypes.INT16_QNAME, MIN_RANGE, MAX_RANGE, DESCRIPTION); - } - - @Override - public String toString() { - return "type " + getQName(); - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int32EffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int32EffectiveStatementImpl.java deleted file mode 100644 index 0919b8b700..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int32EffectiveStatementImpl.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; -import org.opendaylight.yangtools.yang.model.util.BaseTypes; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; - -public class Int32EffectiveStatementImpl extends IntegerEffectiveImplBase { - - public static final String LOCAL_NAME = TypeUtils.INT32; - - private static final Number MIN_RANGE = Integer.MIN_VALUE; - private static final Number MAX_RANGE = Integer.MAX_VALUE; - - private static final String DESCRIPTION = LOCAL_NAME + " represents integer values between " + MIN_RANGE + " and " - + MAX_RANGE + ", inclusively."; - - public Int32EffectiveStatementImpl(final StmtContext> ctx) { - - super(ctx, BaseTypes.INT32_QNAME, MIN_RANGE, MAX_RANGE, DESCRIPTION); - } - - @Override - public String toString() { - return "type " + getQName(); - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int64EffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int64EffectiveStatementImpl.java deleted file mode 100644 index 68d6fd68ea..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int64EffectiveStatementImpl.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; -import org.opendaylight.yangtools.yang.model.util.BaseTypes; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; - -public class Int64EffectiveStatementImpl extends IntegerEffectiveImplBase { - - public static final String LOCAL_NAME = TypeUtils.INT64; - - private static final Number MIN_RANGE = Long.MIN_VALUE; - private static final Number MAX_RANGE = Long.MAX_VALUE; - - private static final String DESCRIPTION = LOCAL_NAME + " represents integer values between " + MIN_RANGE + " and " - + MAX_RANGE + ", inclusively."; - - public Int64EffectiveStatementImpl(final StmtContext> ctx) { - - super(ctx, BaseTypes.INT64_QNAME, MIN_RANGE, MAX_RANGE, DESCRIPTION); - } - - @Override - public String toString() { - return "type " + getQName(); - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int8EffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int8EffectiveStatementImpl.java deleted file mode 100644 index 1daa1db8e7..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Int8EffectiveStatementImpl.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; -import org.opendaylight.yangtools.yang.model.util.BaseTypes; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; - -public class Int8EffectiveStatementImpl extends IntegerEffectiveImplBase { - - public static final String LOCAL_NAME = TypeUtils.INT8; - - private static final Number MIN_RANGE = Byte.MIN_VALUE; - private static final Number MAX_RANGE = Byte.MAX_VALUE; - - private static final String DESCRIPTION = LOCAL_NAME + " represents integer values between " + MIN_RANGE + " and " - + MAX_RANGE + ", inclusively."; - - public Int8EffectiveStatementImpl(final StmtContext> ctx) { - - super(ctx, BaseTypes.INT8_QNAME, MIN_RANGE, MAX_RANGE, DESCRIPTION); - } - - @Override - public String toString() { - return "type " + getQName(); - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IntegerEffectiveImplBase.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IntegerEffectiveImplBase.java deleted file mode 100644 index 9f2c67d186..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IntegerEffectiveImplBase.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -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.TypeStatement; -import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; - -abstract class IntegerEffectiveImplBase extends - DeclaredEffectiveStatementBase implements IntegerTypeDefinition { - - private static final String REFERENCE_INT = "https://tools.ietf.org/html/rfc6020#section-9.2"; - - private final QName qName; - private final SchemaPath path; - private final String units = ""; - private final String description; - private final List rangeStatements; - - protected IntegerEffectiveImplBase(final StmtContext> ctx, - final QName qname, final Number minRange, final Number maxRange, final String description) { - - super(ctx); - - this.qName = Preconditions.checkNotNull(qname); - path = ctx.getSchemaPath().get(); - - final String rangeDescription = "Integer values between " + minRange + " and " + maxRange + ", inclusively."; - final RangeConstraint defaultRange = new RangeConstraintEffectiveImpl(minRange, maxRange, - Optional.of(rangeDescription), Optional.of(RangeConstraintEffectiveImpl.DEFAULT_REFERENCE)); - rangeStatements = Collections.singletonList(defaultRange); - - this.description = description; - } - - @Override - public List getRangeConstraints() { - return rangeStatements; - } - - @Override - public IntegerTypeDefinition getBaseType() { - return null; - } - - @Override - public String getUnits() { - return units; - } - - @Override - public Object getDefaultValue() { - return null; - } - - @Override - public QName getQName() { - return qName; - } - - @Override - public SchemaPath getPath() { - return path; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return description; - } - - @Override - public String getReference() { - return REFERENCE_INT; - } - - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + Objects.hashCode(description); - result = prime * result + Objects.hashCode(qName); - result = prime * result + Objects.hashCode(path); - result = prime * result + Objects.hashCode(rangeStatements); - result = prime * result + Objects.hashCode(units); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - IntegerEffectiveImplBase other = (IntegerEffectiveImplBase) obj; - if (!Objects.equals(description, other.description)) { - return false; - } - if (!Objects.equals(qName, other.qName)) { - return false; - } - if (!Objects.equals(path, other.path)) { - return false; - } - if (!Objects.equals(rangeStatements, other.rangeStatements)) { - return false; - } - if (!Objects.equals(units, other.units)) { - return false; - } - return true; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(getClass().getSimpleName()); - builder.append(" [name="); - builder.append(qName); - builder.append(", path="); - builder.append(path); - builder.append(", description="); - builder.append(description); - builder.append(", reference="); - builder.append(REFERENCE_INT); - builder.append(", units="); - builder.append(units); - builder.append(", rangeStatements="); - builder.append(rangeStatements); - builder.append("]"); - return builder.toString(); - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IntegerTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IntegerTypeEffectiveStatementImpl.java new file mode 100644 index 0000000000..5576360b65 --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/IntegerTypeEffectiveStatementImpl.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; +import org.opendaylight.yangtools.yang.model.util.type.InvalidRangeConstraintException; +import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +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; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; + +public final class IntegerTypeEffectiveStatementImpl extends + DeclaredEffectiveStatementBase implements TypeEffectiveStatement { + + private final IntegerTypeDefinition typeDefinition; + + public IntegerTypeEffectiveStatementImpl( + final StmtContext> ctx, + final IntegerTypeDefinition baseType) { + super(ctx); + + final RangeRestrictedTypeBuilder builder = + RestrictedTypes.newIntegerBuilder(baseType, TypeUtils.typeEffectiveSchemaPath(ctx)); + + for (EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof RangeEffectiveStatementImpl) { + builder.setRangeAlternatives(((RangeEffectiveStatementImpl)stmt).argument()); + } + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); + } + } + + try { + typeDefinition = builder.build(); + } catch (InvalidRangeConstraintException e) { + final RangeConstraint c = e.getOffendingConstraint(); + throw new SourceException(String.format("Invalid range constraint: <%s, %s>", c.getMin(), c.getMax()), + ctx.getStatementSourceReference(), e); + } + } + + @Override + public IntegerTypeDefinition getTypeDefinition() { + return typeDefinition; + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/LeafrefSpecificationEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/LeafrefSpecificationEffectiveStatementImpl.java index 33447d8576..c0e5fa990b 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/LeafrefSpecificationEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/LeafrefSpecificationEffectiveStatementImpl.java @@ -7,141 +7,41 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.YangConstants; -import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -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.TypeEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.LeafrefSpecification; import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.Leafref; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; +import org.opendaylight.yangtools.yang.model.util.type.LeafrefTypeBuilder; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.PathEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; -public class LeafrefSpecificationEffectiveStatementImpl extends DeclaredEffectiveStatementBase - implements LeafrefTypeDefinition, TypeEffectiveStatement { +public final class LeafrefSpecificationEffectiveStatementImpl extends DeclaredEffectiveStatementBase + implements TypeEffectiveStatement { - public static final String LOCAL_NAME = "leafref"; - private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, LOCAL_NAME); - private static final String DESCRIPTION = "The leafref type is used to reference a particular leaf instance in the data tree."; - private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.9"; - private static final String UNITS = ""; - - private final SchemaPath path; - private RevisionAwareXPath xpath; - private Leafref leafrefInstance = null; + private final LeafrefTypeDefinition typeDefinition; public LeafrefSpecificationEffectiveStatementImpl(final StmtContext> ctx) { super(ctx); - path = ctx.getParentContext().getSchemaPath().get().createChild(QNAME); + final LeafrefTypeBuilder builder = BaseTypes.leafrefTypeBuilder(ctx.getSchemaPath().get()); - for (final EffectiveStatement effectiveStatement : effectiveSubstatements()) { - if (effectiveStatement instanceof PathEffectiveStatementImpl) { - xpath = ((PathEffectiveStatementImpl) effectiveStatement).argument(); + for (final EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof PathEffectiveStatementImpl) { + builder.setPathStatement(((PathEffectiveStatementImpl) stmt).argument()); + } + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); } } - } - - @Override - public RevisionAwareXPath getPathStatement() { - return xpath; - } - - @Override - public LeafrefTypeDefinition getBaseType() { - return null; - } - - @Override - public String getUnits() { - return UNITS; - } - - @Override - public Object getDefaultValue() { - return null; - } - - @Override - public QName getQName() { - return QNAME; - } - - @Override - public SchemaPath getPath() { - return path; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return DESCRIPTION; - } - - @Override - public String getReference() { - return REFERENCE; - } - - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + Objects.hashCode(xpath); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - LeafrefSpecificationEffectiveStatementImpl other = (LeafrefSpecificationEffectiveStatementImpl) obj; - return Objects.equals(xpath, other.xpath); - } - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("type "); - builder.append(QNAME); - builder.append(" [xpath="); - builder.append(xpath); - builder.append("]"); - return builder.toString(); + typeDefinition = builder.build(); } @Override - public TypeDefinition getTypeDefinition() { - if (leafrefInstance != null) { - return leafrefInstance; - } - leafrefInstance = Leafref.create(path, xpath); - - return leafrefInstance; + public LeafrefTypeDefinition getTypeDefinition() { + return typeDefinition; } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/LeafrefTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/LeafrefTypeEffectiveStatementImpl.java new file mode 100644 index 0000000000..5ac9768328 --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/LeafrefTypeEffectiveStatementImpl.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; + +public final class LeafrefTypeEffectiveStatementImpl extends AbstractTypeEffectiveStatement { + public LeafrefTypeEffectiveStatementImpl( + final StmtContext> ctx, + final LeafrefTypeDefinition baseType) { + super(ctx, RestrictedTypes.newLeafrefBuilder(baseType, TypeUtils.typeEffectiveSchemaPath(ctx))); + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/StringEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/StringEffectiveStatementImpl.java deleted file mode 100644 index b9a9673b79..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/StringEffectiveStatementImpl.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import com.google.common.base.Optional; -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.YangConstants; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -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.TypeStatement; -import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; -import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; -import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; - -public class StringEffectiveStatementImpl extends DeclaredEffectiveStatementBase implements - StringTypeDefinition { - - public static final String LOCAL_NAME = TypeUtils.STRING; - private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, LOCAL_NAME); - private static final SchemaPath PATH = SchemaPath.create(true, QNAME); - private static final String DESCRIPTION = "The string built-in type represents human-readable strings in YANG. " - + "Legal characters are tab, carriage return, line feed, and the legal " - + "characters of Unicode and ISO/IEC 10646"; - private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.4"; - private static final String UNITS = ""; - private static final Optional OPTIONAL_EMPTY = Optional.of(""); - - private final List lengthConstraints; - private final List patternConstraints; - - public StringEffectiveStatementImpl( - final StmtContext> ctx) { - super(ctx); - - final LengthConstraint defLength = new LengthConstraintEffectiveImpl(0, Integer.MAX_VALUE, OPTIONAL_EMPTY, - OPTIONAL_EMPTY); - - lengthConstraints = Collections.singletonList(defLength); - patternConstraints = Collections.emptyList(); - } - - @Override - public List getLengthConstraints() { - return lengthConstraints; - } - - @Override - public List getPatternConstraints() { - return patternConstraints; - } - - @Override - public StringTypeDefinition getBaseType() { - return null; - } - - @Override - public String getUnits() { - return UNITS; - } - - @Override - public Object getDefaultValue() { - return null; - } - - @Override - public QName getQName() { - return QNAME; - } - - @Override - public SchemaPath getPath() { - return PATH; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return DESCRIPTION; - } - - @Override - public String getReference() { - return REFERENCE; - } - - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + Objects.hashCode(lengthConstraints); - result = prime * result + QNAME.hashCode(); - result = prime * result + PATH.hashCode(); - result = prime * result + Objects.hashCode(patternConstraints); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - StringEffectiveStatementImpl other = (StringEffectiveStatementImpl) obj; - if (!Objects.equals(lengthConstraints, other.lengthConstraints)) { - return false; - } - if (!Objects.equals(patternConstraints, other.patternConstraints)) { - return false; - } - return true; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(getClass().getSimpleName()); - builder.append(" [name="); - builder.append(QNAME); - builder.append(", path="); - builder.append(PATH); - builder.append(", defaultValue=null"); - builder.append(", description="); - builder.append(DESCRIPTION); - builder.append(", reference="); - builder.append(REFERENCE); - builder.append(", lengthStatements="); - builder.append(lengthConstraints); - builder.append(", patterns="); - builder.append(patternConstraints); - builder.append(", units="); - builder.append(UNITS); - builder.append("]"); - return builder.toString(); - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/StringTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/StringTypeEffectiveStatementImpl.java new file mode 100644 index 0000000000..895f14dde1 --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/StringTypeEffectiveStatementImpl.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; +import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; +import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder; +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; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class StringTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase implements + TypeEffectiveStatement { + private static final Logger LOG = LoggerFactory.getLogger(StringTypeEffectiveStatementImpl.class); + private final StringTypeDefinition typeDefinition; + + public StringTypeEffectiveStatementImpl( + final StmtContext> ctx, + final StringTypeDefinition baseType) { + super(ctx); + + final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType, + TypeUtils.typeEffectiveSchemaPath(ctx)); + + for (EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof LengthEffectiveStatementImpl) { + builder.setLengthAlternatives(((LengthEffectiveStatementImpl)stmt).argument()); + } + if (stmt instanceof PatternEffectiveStatementImpl) { + final PatternConstraint pattern = ((PatternEffectiveStatementImpl)stmt).argument(); + if (pattern != null) { + builder.addPatternConstraint(pattern); + } else { + LOG.debug("Ignoring empty pattern statement {}", stmt); + } + } + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); + } + } + + try { + typeDefinition = builder.build(); + } catch (InvalidLengthConstraintException e) { + final LengthConstraint c = e.getOffendingConstraint(); + throw new SourceException(String.format("Invalid length constraint: <%s, %s>", c.getMin(), c.getMax()), + ctx.getStatementSourceReference(), e); + } + } + + @Override + public StringTypeDefinition getTypeDefinition() { + return typeDefinition; + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt16EffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt16EffectiveStatementImpl.java deleted file mode 100644 index b9d45da8cd..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt16EffectiveStatementImpl.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; -import org.opendaylight.yangtools.yang.model.util.BaseTypes; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; - -public class UInt16EffectiveStatementImpl extends UnsignedIntegerEffectiveImplBase { - - public static final String LOCAL_NAME = TypeUtils.UINT16; - - private static final Number MAX_RANGE = 65535; - - private static final String DESCRIPTION = LOCAL_NAME + " represents integer values between " + MIN_RANGE + " and " - + MAX_RANGE + ", inclusively."; - - public UInt16EffectiveStatementImpl(final StmtContext> ctx) { - - super(ctx, BaseTypes.UINT16_QNAME, MAX_RANGE, DESCRIPTION); - } - - @Override - public String toString() { - return "type " + getQName(); - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt32EffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt32EffectiveStatementImpl.java deleted file mode 100644 index 640873a814..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt32EffectiveStatementImpl.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; -import org.opendaylight.yangtools.yang.model.util.BaseTypes; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; - -public class UInt32EffectiveStatementImpl extends UnsignedIntegerEffectiveImplBase { - - public static final String LOCAL_NAME = TypeUtils.UINT32; - - private static final Number MAX_RANGE = 4294967295L; - - private static final String DESCRIPTION = LOCAL_NAME + " represents integer values between " + MIN_RANGE + " and " - + MAX_RANGE + ", inclusively."; - - public UInt32EffectiveStatementImpl(final StmtContext> ctx) { - - super(ctx, BaseTypes.UINT32_QNAME, MAX_RANGE, DESCRIPTION); - } - - @Override - public String toString() { - return "type " + getQName(); - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt64EffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt64EffectiveStatementImpl.java deleted file mode 100644 index 715f397083..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt64EffectiveStatementImpl.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import java.math.BigInteger; -import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; -import org.opendaylight.yangtools.yang.model.util.BaseTypes; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; - -public class UInt64EffectiveStatementImpl extends UnsignedIntegerEffectiveImplBase { - - public static final String LOCAL_NAME = TypeUtils.UINT64; - - private static final Number MAX_RANGE = new BigInteger("18446744073709551615"); - - private static final String DESCRIPTION = LOCAL_NAME + " represents integer values between " + MIN_RANGE + " and " - + MAX_RANGE + ", inclusively."; - - public UInt64EffectiveStatementImpl(final StmtContext> ctx) { - - super(ctx, BaseTypes.UINT64_QNAME, MAX_RANGE, DESCRIPTION); - } - - @Override - public String toString() { - return "type " + getQName(); - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt8EffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt8EffectiveStatementImpl.java deleted file mode 100644 index ed61e5171e..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UInt8EffectiveStatementImpl.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; -import org.opendaylight.yangtools.yang.model.util.BaseTypes; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; - -public class UInt8EffectiveStatementImpl extends UnsignedIntegerEffectiveImplBase { - - public static final String LOCAL_NAME = TypeUtils.UINT8; - - private static final Number MAX_RANGE = 255; - - private static final String DESCRIPTION = LOCAL_NAME + " represents integer values between " + MIN_RANGE + " and " - + MAX_RANGE + ", inclusively."; - - public UInt8EffectiveStatementImpl(final StmtContext> ctx) { - - super(ctx, BaseTypes.UINT8_QNAME, MAX_RANGE, DESCRIPTION); - } - - @Override - public String toString() { - return "type " + getQName(); - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnionSpecificationEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnionSpecificationEffectiveStatementImpl.java index 8980ec9ee4..4aae2c6c3a 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnionSpecificationEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnionSpecificationEffectiveStatementImpl.java @@ -7,149 +7,42 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; -import com.google.common.collect.ImmutableList; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.YangConstants; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -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.TypeEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.UnionSpecification; import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.UnionType; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; +import org.opendaylight.yangtools.yang.model.util.type.UnionTypeBuilder; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; -public class UnionSpecificationEffectiveStatementImpl extends - DeclaredEffectiveStatementBase implements UnionTypeDefinition, +public final class UnionSpecificationEffectiveStatementImpl extends + DeclaredEffectiveStatementBase implements TypeEffectiveStatement { - private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, "union"); - private static final SchemaPath PATH = SchemaPath.create(true, QNAME); - private static final String DESCRIPTION = "The union built-in type represents a value that corresponds to one of its member types."; - private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.12"; - - private final List> types; - private UnionType unionTypeInstance = null; + private final UnionTypeDefinition typeDefinition; public UnionSpecificationEffectiveStatementImpl( final StmtContext> ctx) { super(ctx); - List> typesInit = new ArrayList<>(); + final UnionTypeBuilder builder = BaseTypes.unionTypeBuilder(ctx.getSchemaPath().get()); - for (final EffectiveStatement effectiveStatement : effectiveSubstatements()) { - if (effectiveStatement instanceof TypeDefinition) { - typesInit.add(TypeUtils.getTypeFromEffectiveStatement(effectiveStatement)); + for (final EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof TypeEffectiveStatement) { + builder.addType(((TypeEffectiveStatement)stmt).getTypeDefinition()); + } + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); } } - TypeUtils.sortTypes(typesInit); - - types = ImmutableList.copyOf(typesInit); - } - - @Override - public List> getTypes() { - return types; - } - - @Override - public UnionTypeDefinition getBaseType() { - return null; - } - - @Override - public String getUnits() { - return null; - } - - @Override - public Object getDefaultValue() { - return null; - } - - @Override - public QName getQName() { - return QNAME; - } - - @Override - public SchemaPath getPath() { - return PATH; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return DESCRIPTION; - } - - @Override - public String getReference() { - return REFERENCE; - } - - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + types.hashCode(); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - UnionSpecificationEffectiveStatementImpl other = (UnionSpecificationEffectiveStatementImpl) obj; - return types.equals(other.types); - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("type "); - builder.append(QNAME); - builder.append(" (types=["); - for (TypeDefinition td : types) { - builder.append(", ").append(td.getQName().getLocalName()); - } - builder.append(']'); - return builder.toString(); + typeDefinition = builder.build(); } @Override - public TypeDefinition getTypeDefinition() { - - if (unionTypeInstance != null) { - return unionTypeInstance; - } - - unionTypeInstance = UnionType.create(types); - - return unionTypeInstance; + public UnionTypeDefinition getTypeDefinition() { + return typeDefinition; } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnionTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnionTypeEffectiveStatementImpl.java new file mode 100644 index 0000000000..79aec01bdf --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnionTypeEffectiveStatementImpl.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; + +public final class UnionTypeEffectiveStatementImpl extends AbstractTypeEffectiveStatement { + public UnionTypeEffectiveStatementImpl( + final StmtContext> ctx, + final UnionTypeDefinition baseType) { + super(ctx, RestrictedTypes.newUnionBuilder(baseType, TypeUtils.typeEffectiveSchemaPath(ctx))); + } +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnsignedIntegerEffectiveImplBase.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnsignedIntegerEffectiveImplBase.java deleted file mode 100644 index 45f183dad8..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnsignedIntegerEffectiveImplBase.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; - -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -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.TypeStatement; -import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; -import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; - -abstract class UnsignedIntegerEffectiveImplBase extends - DeclaredEffectiveStatementBase implements UnsignedIntegerTypeDefinition { - - private static final String REFERENCE_INT = "https://tools.ietf.org/html/rfc6020#section-9.2"; - - protected static final Number MIN_RANGE = 0; - private final QName qName; - private final SchemaPath path; - private final String units = ""; - private final String description; - private final List rangeStatements; - - protected UnsignedIntegerEffectiveImplBase(final StmtContext> ctx, - final QName qname, final Number maxRange, final String description) { - - super(ctx); - - this.qName = Preconditions.checkNotNull(qname); - path = ctx.getSchemaPath().get(); - - final String rangeDescription = "Integer values between " + MIN_RANGE + " and " + maxRange + ", inclusively."; - final RangeConstraint defaultRange = new RangeConstraintEffectiveImpl(MIN_RANGE, maxRange, - Optional.of(rangeDescription), Optional.of(RangeConstraintEffectiveImpl.DEFAULT_REFERENCE)); - rangeStatements = Collections.singletonList(defaultRange); - - this.description = description; - } - - @Override - public List getRangeConstraints() { - return rangeStatements; - } - - @Override - public UnsignedIntegerTypeDefinition getBaseType() { - return null; - } - - @Override - public String getUnits() { - return units; - } - - @Override - public Object getDefaultValue() { - return null; - } - - @Override - public QName getQName() { - return qName; - } - - @Override - public SchemaPath getPath() { - return path; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - @Override - public String getDescription() { - return description; - } - - @Override - public String getReference() { - return REFERENCE_INT; - } - - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + Objects.hashCode(description); - result = prime * result + Objects.hashCode(qName); - result = prime * result + Objects.hashCode(path); - result = prime * result + Objects.hashCode(rangeStatements); - result = prime * result + Objects.hashCode(units); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - UnsignedIntegerEffectiveImplBase other = (UnsignedIntegerEffectiveImplBase) obj; - if (!Objects.equals(description, other.description)) { - return false; - } - if (!Objects.equals(qName, other.qName)) { - return false; - } - if (!Objects.equals(path, other.path)) { - return false; - } - if (!Objects.equals(rangeStatements, other.rangeStatements)) { - return false; - } - if (!Objects.equals(units, other.units)) { - return false; - } - return true; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(getClass().getSimpleName()); - builder.append(" [name="); - builder.append(qName); - builder.append(", path="); - builder.append(path); - builder.append(", description="); - builder.append(description); - builder.append(", reference="); - builder.append(REFERENCE_INT); - builder.append(", units="); - builder.append(units); - builder.append(", rangeStatements="); - builder.append(rangeStatements); - builder.append("]"); - return builder.toString(); - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnsignedIntegerTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnsignedIntegerTypeEffectiveStatementImpl.java new file mode 100644 index 0000000000..2a99a3d89a --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/UnsignedIntegerTypeEffectiveStatementImpl.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; + +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; +import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl; + +public final class UnsignedIntegerTypeEffectiveStatementImpl extends + DeclaredEffectiveStatementBase implements TypeEffectiveStatement { + + private final UnsignedIntegerTypeDefinition typeDefinition; + + public UnsignedIntegerTypeEffectiveStatementImpl( + final StmtContext> ctx, + final UnsignedIntegerTypeDefinition baseType) { + super(ctx); + + final RangeRestrictedTypeBuilder builder = + RestrictedTypes.newUnsignedBuilder(baseType, TypeUtils.typeEffectiveSchemaPath(ctx)); + + for (EffectiveStatement stmt : effectiveSubstatements()) { + if (stmt instanceof RangeEffectiveStatementImpl) { + builder.setRangeAlternatives(((RangeEffectiveStatementImpl)stmt).argument()); + } + if (stmt instanceof UnknownEffectiveStatementImpl) { + builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt); + } + } + + typeDefinition = builder.build(); + } + + @Override + public UnsignedIntegerTypeDefinition getTypeDefinition() { + return typeDefinition; + } +} diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/effective/build/test/EffectiveStatementTypeTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/effective/build/test/EffectiveStatementTypeTest.java index 070db9e93a..fb095d2aad 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/effective/build/test/EffectiveStatementTypeTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/effective/build/test/EffectiveStatementTypeTest.java @@ -18,40 +18,33 @@ import java.util.List; import org.junit.Before; import org.junit.Test; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.BitsType; -import org.opendaylight.yangtools.yang.model.util.EnumerationType; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; +import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; +import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IdentityEffectiveStatementImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.LeafEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BinaryEffectiveStatementImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BitEffectiveStatementImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BitsSpecificationEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BooleanEffectiveStatementImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Decimal64SpecificationEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EmptyEffectiveStatementImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EnumEffectiveStatementImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EnumSpecificationEffectiveStatementImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IdentityRefSpecificationEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.InstanceIdentifierSpecificationEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Int16EffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Int32EffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Int64EffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Int8EffectiveStatementImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LeafrefSpecificationEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LengthConstraintEffectiveImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.PatternConstraintEffectiveImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.StringEffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UInt16EffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UInt32EffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UInt64EffectiveStatementImpl; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UInt8EffectiveStatementImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnionSpecificationEffectiveStatementImpl; public class EffectiveStatementTypeTest { @@ -74,19 +67,19 @@ public class EffectiveStatementTypeTest { .getDataChildByName("leaf-binary"); assertNotNull(currentLeaf.getType()); - BinaryEffectiveStatementImpl binaryEff = (BinaryEffectiveStatementImpl) - ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next(); + BinaryTypeDefinition binaryEff = (BinaryTypeDefinition) ((TypeEffectiveStatement) + ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition(); assertNull(binaryEff.getBaseType()); - assertEquals("", binaryEff.getUnits()); + assertNull(binaryEff.getUnits()); assertNull(binaryEff.getDefaultValue()); assertEquals("binary", binaryEff.getQName().getLocalName()); - assertEquals(0, binaryEff.getLengthConstraints().get(0).getMin()); + assertEquals(0, binaryEff.getLengthConstraints().size()); assertEquals("CURRENT", binaryEff.getStatus().toString()); assertEquals("binary", binaryEff.getPath().getPathFromRoot().iterator().next().getLocalName()); assertNotNull(binaryEff.getUnknownSchemaNodes()); - assertNotNull(binaryEff.getDescription()); - assertNotNull(binaryEff.getReference()); + assertNull(binaryEff.getDescription()); + assertNull(binaryEff.getReference()); } @Test @@ -95,29 +88,27 @@ public class EffectiveStatementTypeTest { .getDataChildByName("leaf-bits"); assertNotNull(currentLeaf.getType()); - List bitsEffIter = ((BitsType) currentLeaf.getType()).getBits(); + List bitsEffIter = ((BitsTypeDefinition) currentLeaf.getType()).getBits(); BitEffectiveStatementImpl bitEff = (BitEffectiveStatementImpl) bitsEffIter.get(0); BitEffectiveStatementImpl bitEffSecond = (BitEffectiveStatementImpl) bitsEffIter.get(1); - BitsSpecificationEffectiveStatementImpl bitsEff = (BitsSpecificationEffectiveStatementImpl) - ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next(); - BitsSpecificationEffectiveStatementImpl bitsEffSecond = (BitsSpecificationEffectiveStatementImpl) - ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next(); + BitsTypeDefinition bitsEff = ((BitsSpecificationEffectiveStatementImpl) + ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition(); assertNull(bitsEff.getBaseType()); assertNotNull(bitsEff.getQName()); assertEquals("bits", bitsEff.getQName().getLocalName()); assertEquals("bits", bitsEff.getPath().getLastComponent().getLocalName()); assertNotNull(bitsEff.getUnknownSchemaNodes()); - assertNotNull(bitsEff.getDescription()); - assertNotNull(bitsEff.getReference()); + assertNull(bitsEff.getDescription()); + assertNull(bitsEff.getReference()); assertEquals("CURRENT", bitsEff.getStatus().toString()); - assertEquals("", bitsEff.getUnits()); + assertNull(bitsEff.getUnits()); assertNotNull(bitsEff.toString()); assertNotNull(bitsEff.hashCode()); assertFalse(bitsEff.equals(null)); assertFalse(bitsEff.equals("test")); - assertTrue(bitsEff.equals(bitsEffSecond)); + assertTrue(bitsEff.equals(bitsEff)); assertEquals(3, bitsEff.getBits().size()); assertNull(bitsEff.getDefaultValue()); @@ -142,17 +133,17 @@ public class EffectiveStatementTypeTest { currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-boolean"); assertNotNull(currentLeaf.getType()); - BooleanEffectiveStatementImpl booleanEff = (BooleanEffectiveStatementImpl) - ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next(); + BooleanTypeDefinition booleanEff = (BooleanTypeDefinition) ((TypeEffectiveStatement) + ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition(); assertNull(booleanEff.getBaseType()); - assertEquals("", booleanEff.getUnits()); + assertNull(booleanEff.getUnits()); assertNull(booleanEff.getDefaultValue()); assertEquals("boolean", booleanEff.getQName().getLocalName()); assertNull(booleanEff.getPath().getParent().getParent()); assertNotNull(booleanEff.getUnknownSchemaNodes()); - assertNotNull(booleanEff.getDescription()); - assertNotNull(booleanEff.getReference()); + assertNull(booleanEff.getDescription()); + assertNull(booleanEff.getReference()); assertEquals("CURRENT", booleanEff.getStatus().toString()); assertNotNull(booleanEff.toString()); } @@ -162,27 +153,27 @@ public class EffectiveStatementTypeTest { currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-decimal64"); assertNotNull(currentLeaf.getType()); - Decimal64SpecificationEffectiveStatementImpl decimal64Eff = (Decimal64SpecificationEffectiveStatementImpl) - ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next(); - Decimal64SpecificationEffectiveStatementImpl decimal64EffSecond = (Decimal64SpecificationEffectiveStatementImpl) - ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next(); + DecimalTypeDefinition decimal64Eff = ((Decimal64SpecificationEffectiveStatementImpl) + ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition(); - assertEquals("decimal64", decimal64Eff.getBaseType().getQName().getLocalName()); - assertEquals("", decimal64Eff.getUnits()); + assertNull(decimal64Eff.getBaseType()); + assertNull(decimal64Eff.getUnits()); assertNull(decimal64Eff.getDefaultValue()); assertEquals("decimal64", decimal64Eff.getQName().getLocalName()); assertNotNull(decimal64Eff.getUnknownSchemaNodes()); - assertNotNull(decimal64Eff.getDescription()); - assertNotNull(decimal64Eff.getReference()); + + // FIXME: The model is wrong: description/reference/status are not allowed under 'type', how come we parse it? + assertNull(decimal64Eff.getDescription()); + assertNull(decimal64Eff.getReference()); assertEquals("CURRENT", decimal64Eff.getStatus().toString()); + assertEquals(3, decimal64Eff.getRangeConstraints().size()); assertNotNull(decimal64Eff.toString()); assertNotNull(decimal64Eff.hashCode()); assertTrue(decimal64Eff.getFractionDigits().equals(2)); - assertTrue(decimal64Eff.isExtended()); assertFalse(decimal64Eff.equals(null)); assertFalse(decimal64Eff.equals("test")); - assertTrue(decimal64Eff.equals(decimal64EffSecond)); + assertTrue(decimal64Eff.equals(decimal64Eff)); assertEquals("decimal64", decimal64Eff.getPath().getLastComponent().getLocalName()); } @@ -191,8 +182,8 @@ public class EffectiveStatementTypeTest { currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-empty"); assertNotNull(currentLeaf.getType()); - EmptyEffectiveStatementImpl emptyEff = (EmptyEffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf) - .effectiveSubstatements().iterator().next(); + EmptyTypeDefinition emptyEff = (EmptyTypeDefinition) ((TypeEffectiveStatement) + ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition(); assertNull(emptyEff.getUnits()); assertNull(emptyEff.getDefaultValue()); @@ -200,8 +191,8 @@ public class EffectiveStatementTypeTest { assertEquals("empty", emptyEff.getQName().getLocalName()); assertNull(emptyEff.getPath().getParent().getParent()); assertNotNull(emptyEff.getUnknownSchemaNodes()); - assertNotNull(emptyEff.getDescription()); - assertNotNull(emptyEff.getReference()); + assertNull(emptyEff.getDescription()); + assertNull(emptyEff.getReference()); assertEquals("CURRENT", emptyEff.getStatus().toString()); assertNotNull(emptyEff.toString()); } @@ -211,13 +202,11 @@ public class EffectiveStatementTypeTest { currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-enum"); assertNotNull(currentLeaf.getType()); - List enumEffIter = ((EnumerationType) currentLeaf.getType()).getValues(); + List enumEffIter = ((EnumTypeDefinition) currentLeaf.getType()).getValues(); EnumEffectiveStatementImpl enumEff = (EnumEffectiveStatementImpl) enumEffIter.iterator().next(); - EnumSpecificationEffectiveStatementImpl enumSpecEff = (EnumSpecificationEffectiveStatementImpl) - ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next(); - EnumSpecificationEffectiveStatementImpl enumSpecEffSecond = (EnumSpecificationEffectiveStatementImpl) - ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next(); + EnumTypeDefinition enumSpecEff = ((EnumSpecificationEffectiveStatementImpl) + ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition(); assertEquals("enumeration", enumSpecEff.getQName().getLocalName()); assertEquals("enumeration", enumSpecEff.getPath().getLastComponent().getLocalName()); @@ -226,14 +215,14 @@ public class EffectiveStatementTypeTest { assertNull(enumSpecEff.getBaseType()); assertNotNull(enumSpecEff.getUnknownSchemaNodes()); assertEquals("CURRENT", enumSpecEff.getStatus().toString()); - assertNotNull(enumSpecEff.getDescription()); - assertNotNull(enumSpecEff.getReference()); - assertEquals("", enumSpecEff.getUnits()); + assertNull(enumSpecEff.getDescription()); + assertNull(enumSpecEff.getReference()); + assertNull(enumSpecEff.getUnits()); assertNotNull(enumSpecEff.toString()); assertNotNull(enumSpecEff.hashCode()); assertFalse(enumSpecEff.equals(null)); assertFalse(enumSpecEff.equals("test")); - assertTrue(enumSpecEff.equals(enumSpecEffSecond)); + assertTrue(enumSpecEff.equals(enumSpecEff)); assertEquals("zero", enumEff.getQName().getLocalName()); assertEquals("zero", enumEff.getName()); @@ -250,20 +239,22 @@ public class EffectiveStatementTypeTest { currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-identityref"); assertNotNull(currentLeaf.getType()); - IdentityRefSpecificationEffectiveStatementImpl identityRefEff = (IdentityRefSpecificationEffectiveStatementImpl) - ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next(); + IdentityrefTypeDefinition identityRefEff = ((IdentityRefSpecificationEffectiveStatementImpl) + ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition(); - assertTrue(identityRefEff.getDefaultValue() instanceof IdentityEffectiveStatementImpl); + assertNull(identityRefEff.getDefaultValue()); assertEquals("identityref", identityRefEff.getQName().getLocalName()); assertEquals("identityref", identityRefEff.getPath().getLastComponent().getLocalName()); assertNull(identityRefEff.getBaseType()); assertNotNull(identityRefEff.getUnknownSchemaNodes()); assertEquals("CURRENT", identityRefEff.getStatus().toString()); assertEquals("test-identity", identityRefEff.getIdentity().getQName().getLocalName()); - assertNotNull(identityRefEff.getDescription()); - assertNotNull(identityRefEff.getReference()); - assertEquals("", identityRefEff.getUnits()); + assertNull(identityRefEff.getDescription()); + assertNull(identityRefEff.getReference()); assertNotNull(identityRefEff.toString()); + + // FIXME: the model is wrong, but we accept units in 'type' statement + assertNull(identityRefEff.getUnits()); } @Test @@ -271,12 +262,9 @@ public class EffectiveStatementTypeTest { currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-instance-identifier"); assertNotNull(currentLeaf.getType()); - InstanceIdentifierSpecificationEffectiveStatementImpl instanceIdentEff = - (InstanceIdentifierSpecificationEffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf) - .effectiveSubstatements().iterator().next(); - InstanceIdentifierSpecificationEffectiveStatementImpl instanceIdentEffSecond = - (InstanceIdentifierSpecificationEffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf) - .effectiveSubstatements().iterator().next(); + InstanceIdentifierTypeDefinition instanceIdentEff = (InstanceIdentifierTypeDefinition) + ((TypeEffectiveStatement) ((LeafEffectiveStatementImpl) currentLeaf) + .effectiveSubstatements().iterator().next()).getTypeDefinition(); assertNotNull(instanceIdentEff.toString()); assertFalse(instanceIdentEff.requireInstance()); @@ -284,16 +272,15 @@ public class EffectiveStatementTypeTest { assertEquals("instance-identifier", instanceIdentEff.getPath().getLastComponent().getLocalName()); assertNull(instanceIdentEff.getBaseType()); assertNull(instanceIdentEff.getDefaultValue()); - assertNull(instanceIdentEff.getPathStatement()); assertNotNull(instanceIdentEff.getUnknownSchemaNodes()); - assertNotNull(instanceIdentEff.getDescription()); - assertNotNull(instanceIdentEff.getReference()); - assertEquals("", instanceIdentEff.getUnits()); + assertNull(instanceIdentEff.getDescription()); + assertNull(instanceIdentEff.getReference()); + assertNull(instanceIdentEff.getUnits()); assertEquals("CURRENT", instanceIdentEff.getStatus().toString()); assertNotNull(instanceIdentEff.hashCode()); assertFalse(instanceIdentEff.equals(null)); assertFalse(instanceIdentEff.equals("test")); - assertTrue(instanceIdentEff.equals(instanceIdentEffSecond)); + assertTrue(instanceIdentEff.equals(instanceIdentEff)); } @Test @@ -301,27 +288,25 @@ public class EffectiveStatementTypeTest { currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-leafref"); assertNotNull(currentLeaf.getType()); - LeafrefSpecificationEffectiveStatementImpl leafrefEff = (LeafrefSpecificationEffectiveStatementImpl) - ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next(); - LeafrefSpecificationEffectiveStatementImpl leafrefEffSecond = (LeafrefSpecificationEffectiveStatementImpl) - ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next(); + LeafrefTypeDefinition leafrefEff = ((LeafrefSpecificationEffectiveStatementImpl) + ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition(); assertEquals("/container-test/leaf-test", leafrefEff.getPathStatement().toString()); assertNull(leafrefEff.getBaseType()); - assertEquals("", leafrefEff.getUnits()); - assertNull("leafref", leafrefEff.getDefaultValue()); + assertNull(leafrefEff.getUnits()); + assertNull(leafrefEff.getDefaultValue()); assertNotNull(leafrefEff.toString()); assertEquals("leafref", leafrefEff.getQName().getLocalName()); assertEquals("CURRENT", leafrefEff.getStatus().toString()); assertNotNull(leafrefEff.getUnknownSchemaNodes()); assertEquals("leafref", leafrefEff.getPath().getLastComponent().getLocalName()); - assertNotNull(leafrefEff.getDescription()); - assertNotNull(leafrefEff.getReference()); + assertNull(leafrefEff.getDescription()); + assertNull(leafrefEff.getReference()); assertNotNull(leafrefEff.hashCode()); assertFalse(leafrefEff.equals(null)); assertFalse(leafrefEff.equals("test")); - assertTrue(leafrefEff.equals(leafrefEffSecond)); + assertTrue(leafrefEff.equals(leafrefEff)); } @Test @@ -329,42 +314,42 @@ public class EffectiveStatementTypeTest { currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-int8"); assertNotNull(currentLeaf.getType()); - Int8EffectiveStatementImpl int8Eff = (Int8EffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf) + TypeEffectiveStatement int8Eff = (TypeEffectiveStatement) ((LeafEffectiveStatementImpl) currentLeaf) .effectiveSubstatements().iterator().next(); assertNotNull(int8Eff.toString()); currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-int16"); assertNotNull(currentLeaf.getType()); - Int16EffectiveStatementImpl int16Eff = (Int16EffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf) + TypeEffectiveStatement int16Eff = (TypeEffectiveStatement) ((LeafEffectiveStatementImpl) currentLeaf) .effectiveSubstatements().iterator().next(); assertNotNull(int16Eff.toString()); currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-int32"); assertNotNull(currentLeaf.getType()); - Int32EffectiveStatementImpl int32Eff = (Int32EffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf) + TypeEffectiveStatement int32Eff = (TypeEffectiveStatement) ((LeafEffectiveStatementImpl) currentLeaf) .effectiveSubstatements().iterator().next(); assertNotNull(int32Eff.toString()); currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-int64"); assertNotNull(currentLeaf.getType()); - Int64EffectiveStatementImpl int64Eff = (Int64EffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf) + TypeEffectiveStatement int64Eff = (TypeEffectiveStatement) ((LeafEffectiveStatementImpl) currentLeaf) .effectiveSubstatements().iterator().next(); assertNotNull(int64Eff.toString()); currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-uint8"); assertNotNull(currentLeaf.getType()); - UInt8EffectiveStatementImpl uint8Eff = (UInt8EffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf) + TypeEffectiveStatement uint8Eff = (TypeEffectiveStatement) ((LeafEffectiveStatementImpl) currentLeaf) .effectiveSubstatements().iterator().next(); assertNotNull(uint8Eff.toString()); currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-uint16"); assertNotNull(currentLeaf.getType()); - UInt16EffectiveStatementImpl uint16Eff = (UInt16EffectiveStatementImpl) ((LeafEffectiveStatementImpl) + TypeEffectiveStatement uint16Eff = (TypeEffectiveStatement) ((LeafEffectiveStatementImpl) currentLeaf) .effectiveSubstatements().iterator().next(); assertNotNull(uint16Eff.toString()); @@ -372,7 +357,7 @@ public class EffectiveStatementTypeTest { currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-uint32"); assertNotNull(currentLeaf.getType()); - UInt32EffectiveStatementImpl uint32Eff = (UInt32EffectiveStatementImpl) ((LeafEffectiveStatementImpl) + TypeEffectiveStatement uint32Eff = (TypeEffectiveStatement) ((LeafEffectiveStatementImpl) currentLeaf) .effectiveSubstatements().iterator().next(); assertNotNull(uint32Eff.toString()); @@ -380,7 +365,7 @@ public class EffectiveStatementTypeTest { currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-uint64"); assertNotNull(currentLeaf.getType()); - UInt64EffectiveStatementImpl uint64Eff = (UInt64EffectiveStatementImpl) ((LeafEffectiveStatementImpl) + TypeEffectiveStatement uint64Eff = (TypeEffectiveStatement) ((LeafEffectiveStatementImpl) currentLeaf) .effectiveSubstatements().iterator().next(); assertNotNull(uint64Eff.toString()); @@ -391,10 +376,8 @@ public class EffectiveStatementTypeTest { currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-union"); assertNotNull(currentLeaf.getType()); - UnionSpecificationEffectiveStatementImpl unionEff = (UnionSpecificationEffectiveStatementImpl) - ((LeafEffectiveStatementImpl)currentLeaf).effectiveSubstatements().iterator().next(); - UnionSpecificationEffectiveStatementImpl unionEffSecond = (UnionSpecificationEffectiveStatementImpl) - ((LeafEffectiveStatementImpl)currentLeaf).effectiveSubstatements().iterator().next(); + UnionTypeDefinition unionEff = ((UnionSpecificationEffectiveStatementImpl) + ((LeafEffectiveStatementImpl)currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition(); assertEquals(2, unionEff.getTypes().size()); assertEquals("union", unionEff.getQName().getLocalName()); @@ -403,13 +386,13 @@ public class EffectiveStatementTypeTest { assertNull(unionEff.getBaseType()); assertNull(unionEff.getUnits()); assertNull(unionEff.getDefaultValue()); - assertNotNull(unionEff.getDescription()); - assertNotNull(unionEff.getReference()); + assertNull(unionEff.getDescription()); + assertNull(unionEff.getReference()); assertNotNull(unionEff.toString()); assertNotNull(unionEff.hashCode()); assertFalse(unionEff.equals(null)); assertFalse(unionEff.equals("test")); - assertTrue(unionEff.equals(unionEffSecond)); + assertTrue(unionEff.equals(unionEff)); assertEquals("union", unionEff.getPath().getLastComponent().getLocalName()); } @@ -418,15 +401,12 @@ public class EffectiveStatementTypeTest { currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-length-pattern"); assertNotNull(currentLeaf.getType()); - LengthConstraintEffectiveImpl lengthConstraint = (LengthConstraintEffectiveImpl) - ((ExtendedType) (currentLeaf.getType())).getLengthConstraints().get(0); - LengthConstraintEffectiveImpl lengthConstraintThird = (LengthConstraintEffectiveImpl) - ((ExtendedType) (currentLeaf.getType())).getLengthConstraints().get(0); + LengthConstraint lengthConstraint = ((StringTypeDefinition) (currentLeaf.getType())).getLengthConstraints().get(0); + LengthConstraint lengthConstraintThird = ((StringTypeDefinition) (currentLeaf.getType())).getLengthConstraints().get(0); currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-length-pattern-second"); assertNotNull(currentLeaf.getType()); - LengthConstraintEffectiveImpl lengthConstraintSecond = (LengthConstraintEffectiveImpl) - ((ExtendedType) (currentLeaf.getType())).getLengthConstraints().get(0); + LengthConstraint lengthConstraintSecond = ((StringTypeDefinition) (currentLeaf.getType())).getLengthConstraints().get(0); assertEquals(1, lengthConstraint.getMin().intValue()); assertEquals(255, lengthConstraint.getMax().intValue()); @@ -448,18 +428,18 @@ public class EffectiveStatementTypeTest { .getDataChildByName("leaf-length-pattern"); assertNotNull(currentLeaf.getType()); PatternConstraintEffectiveImpl lengthConstraint = (PatternConstraintEffectiveImpl) - ((ExtendedType) (currentLeaf.getType())).getPatternConstraints().get(0); + ((StringTypeDefinition) (currentLeaf.getType())).getPatternConstraints().get(0); PatternConstraintEffectiveImpl lengthConstraintThird = (PatternConstraintEffectiveImpl) - ((ExtendedType) (currentLeaf.getType())).getPatternConstraints().get(0); + ((StringTypeDefinition) (currentLeaf.getType())).getPatternConstraints().get(0); currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-length-pattern-second"); assertNotNull(currentLeaf.getType()); PatternConstraintEffectiveImpl lengthConstraintSecond = (PatternConstraintEffectiveImpl) - ((ExtendedType) (currentLeaf.getType())).getPatternConstraints().get(0); + ((StringTypeDefinition) (currentLeaf.getType())).getPatternConstraints().get(0); assertEquals("^[0-9a-fA-F]*$", lengthConstraint.getRegularExpression()); - assertNotNull(lengthConstraint.getReference()); - assertNotNull(lengthConstraint.getDescription()); + assertNull(lengthConstraint.getReference()); + assertNull(lengthConstraint.getDescription()); assertEquals("String ^[0-9a-fA-F]*$ is not valid regular expression.", lengthConstraint.getErrorMessage()); assertEquals("invalid-regular-expression", lengthConstraint.getErrorAppTag()); assertNotNull(lengthConstraint.toString()); @@ -475,26 +455,24 @@ public class EffectiveStatementTypeTest { currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null) .getDataChildByName("leaf-string"); assertNotNull(currentLeaf.getType()); - StringEffectiveStatementImpl stringEff = (StringEffectiveStatementImpl) - ((LeafEffectiveStatementImpl)currentLeaf).effectiveSubstatements().iterator().next(); - StringEffectiveStatementImpl stringEffSecond = (StringEffectiveStatementImpl) - ((LeafEffectiveStatementImpl)currentLeaf).effectiveSubstatements().iterator().next(); + StringTypeDefinition stringEff = (StringTypeDefinition) ((TypeEffectiveStatement) + ((LeafEffectiveStatementImpl)currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition(); assertEquals("string", stringEff.getQName().getLocalName()); assertEquals("CURRENT", stringEff.getStatus().toString()); - assertEquals("", stringEff.getUnits()); + assertNull(stringEff.getUnits()); assertNull(stringEff.getDefaultValue()); assertNotNull(stringEff.getUnknownSchemaNodes()); assertNull(stringEff.getBaseType()); - assertNotNull(stringEff.getDescription()); - assertNotNull(stringEff.getReference()); + assertNull(stringEff.getDescription()); + assertNull(stringEff.getReference()); assertNotNull(stringEff.toString()); assertNotNull(stringEff.hashCode()); assertFalse(stringEff.equals(null)); assertFalse(stringEff.equals("test")); - assertTrue(stringEff.equals(stringEffSecond)); + assertTrue(stringEff.equals(stringEff)); assertEquals("string", stringEff.getPath().getLastComponent().getLocalName()); - assertEquals(1, stringEff.getLengthConstraints().size()); + assertEquals(0, stringEff.getLengthConstraints().size()); assertNotNull(stringEff.getPatternConstraints()); } } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/UsesAugmentTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/UsesAugmentTest.java index 69b3a1a5bd..4adb610e43 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/UsesAugmentTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/UsesAugmentTest.java @@ -11,17 +11,16 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import com.google.common.collect.Lists; import java.io.FileNotFoundException; import java.net.URI; import java.net.URISyntaxException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayDeque; import java.util.Collection; import java.util.Date; -import java.util.LinkedList; -import java.util.List; +import java.util.Deque; import java.util.Set; import org.junit.Before; import org.junit.Test; @@ -35,12 +34,9 @@ import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.opendaylight.yangtools.yang.model.util.BaseTypes; -import org.opendaylight.yangtools.yang.model.util.BooleanType; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; -import org.opendaylight.yangtools.yang.model.util.Uint32; -import org.opendaylight.yangtools.yang.model.util.Uint8; -import org.opendaylight.yangtools.yang.model.util.UnionType; +import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; @@ -118,7 +114,7 @@ public class UsesAugmentTest { modules = TestUtils.loadModules(getClass().getResource("/grouping-test").toURI()); Module testModule = TestUtils.findModule(modules, "uses-grouping"); - LinkedList path = new LinkedList<>(); + Deque path = new ArrayDeque<>(); // * notification pcreq Set notifications = testModule.getNotifications(); @@ -138,11 +134,11 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath = SchemaPath.create(path, true); assertEquals(expectedPath, version.getPath()); - expectedQName = QName.create(GD_NS, GD_REV, "protocol-version"); + expectedQName = QName.create(UG_NS, UG_REV, "version"); path.offer(expectedQName); - expectedPath = SchemaPath.create(Lists.newArrayList(expectedQName), true); + expectedPath = SchemaPath.create(true, pcreq.getQName(), expectedQName); assertEquals(expectedPath, version.getType().getPath()); - assertEquals(Uint8.getInstance(), version.getType().getBaseType()); + assertEquals(BaseTypes.uint8Type(), version.getType().getBaseType().getBaseType()); assertTrue(version.isAddedByUses()); // * |-- leaf type LeafSchemaNode type = (LeafSchemaNode) pcreq.getDataChildByName("type"); @@ -156,10 +152,10 @@ public class UsesAugmentTest { assertEquals(expectedPath, type.getPath()); expectedQName = QName.create(GD_NS, GD_REV, "int-ext"); path.offer(expectedQName); - expectedPath = SchemaPath.create(Lists.newArrayList(expectedQName), true); + expectedPath = SchemaPath.create(true, expectedQName); assertEquals(expectedPath, type.getType().getPath()); - UnionType union = (UnionType)type.getType().getBaseType(); - assertEquals(SchemaPath.create(true, BaseTypes.constructQName("union")), union.getPath()); + UnionTypeDefinition union = (UnionTypeDefinition) type.getType().getBaseType(); + assertEquals(SchemaPath.create(true, expectedQName, QName.create(expectedQName, "union")), union.getPath()); assertEquals(2, union.getTypes().size()); // * |-- list requests ListSchemaNode requests = (ListSchemaNode) pcreq.getDataChildByName("requests"); @@ -192,7 +188,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath = SchemaPath.create(path, true); assertEquals(expectedPath, processingRule.getPath()); - assertEquals(BooleanType.getInstance(), processingRule.getType()); + assertEquals(BaseTypes.booleanType(), processingRule.getType()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- |-- leaf ignore LeafSchemaNode ignore = (LeafSchemaNode) rp.getDataChildByName("ignore"); @@ -203,7 +199,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath = SchemaPath.create(path, true); assertEquals(expectedPath, ignore.getPath()); - assertEquals(BooleanType.getInstance(), ignore.getType()); + assertEquals(BaseTypes.booleanType(), ignore.getType()); assertTrue(ignore.isAddedByUses()); // * |-- |-- |-- leaf priority LeafSchemaNode priority = (LeafSchemaNode) rp.getDataChildByName("priority"); @@ -219,7 +215,7 @@ public class UsesAugmentTest { expectedPath = SchemaPath.create(path, true); // TODO //assertEquals(expectedPath, priority.getType().getPath()); - assertEquals(Uint8.getInstance(), priority.getType().getBaseType()); + assertEquals(BaseTypes.uint8Type(), priority.getType().getBaseType()); assertTrue(priority.isAddedByUses()); // * |-- |-- |-- container box ContainerSchemaNode box = (ContainerSchemaNode) rp.getDataChildByName("box"); @@ -251,7 +247,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath = SchemaPath.create(path, true); assertEquals(expectedPath, delete.getPath()); - assertEquals(Uint32.getInstance(), delete.getType()); + assertEquals(BaseTypes.uint32Type(), delete.getType()); assertTrue(delete.isAddedByUses()); // * |-- |-- |-- |-- |-- leaf setup LeafSchemaNode setup = (LeafSchemaNode) order.getDataChildByName("setup"); @@ -262,7 +258,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath = SchemaPath.create(path, true); assertEquals(expectedPath, setup.getPath()); - assertEquals(Uint32.getInstance(), setup.getType()); + assertEquals(BaseTypes.uint32Type(), setup.getType()); assertTrue(setup.isAddedByUses()); // * |-- |-- path-key-expansion ContainerSchemaNode pke = (ContainerSchemaNode) requests.getDataChildByName("path-key-expansion"); @@ -295,7 +291,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, processingRule.getPath()); - assertEquals(BooleanType.getInstance(), processingRule.getType()); + assertEquals(BaseTypes.booleanType(), processingRule.getType()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- |-- |-- leaf ignore ignore = (LeafSchemaNode) pathKey.getDataChildByName("ignore"); @@ -306,7 +302,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, ignore.getPath()); - assertEquals(BooleanType.getInstance(), ignore.getType()); + assertEquals(BaseTypes.booleanType(), ignore.getType()); assertTrue(ignore.isAddedByUses()); // * |-- |-- |-- |-- list path-keys ListSchemaNode pathKeys = (ListSchemaNode) pathKey.getDataChildByName("path-keys"); @@ -328,8 +324,8 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, version.getPath()); - assertTrue(version.getType() instanceof ExtendedType); - assertEquals(Uint8.getInstance(), version.getType().getBaseType()); + assertTrue(version.getType() instanceof UnsignedIntegerTypeDefinition); + assertEquals(BaseTypes.uint8Type(), version.getType().getBaseType().getBaseType()); assertTrue(version.isAddedByUses()); assertTrue(version.isAugmenting()); // * |-- |-- |-- |-- |-- leaf type @@ -341,7 +337,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, type.getPath()); - assertTrue(type.getType() instanceof ExtendedType); + assertTrue(type.getType() instanceof UnionTypeDefinition); assertTrue(type.isAddedByUses()); assertTrue(type.isAugmenting()); // * |-- |-- container segment-computation @@ -383,7 +379,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, processingRule.getPath()); - assertEquals(BooleanType.getInstance(), processingRule.getType()); + assertEquals(BaseTypes.booleanType(), processingRule.getType()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- |-- |-- |-- leaf ignore ignore = (LeafSchemaNode) endpoints.getDataChildByName("ignore"); @@ -394,7 +390,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, ignore.getPath()); - assertEquals(BooleanType.getInstance(), ignore.getType()); + assertEquals(BaseTypes.booleanType(), ignore.getType()); assertTrue(ignore.isAddedByUses()); // * |-- |-- |-- |-- |-- container box box = (ContainerSchemaNode) endpoints.getDataChildByName("box"); @@ -435,7 +431,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, processingRule.getPath()); - assertEquals(BooleanType.getInstance(), processingRule.getType()); + assertEquals(BaseTypes.booleanType(), processingRule.getType()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- |-- |-- |-- leaf ignore ignore = (LeafSchemaNode) reportedRoute.getDataChildByName("ignore"); @@ -446,7 +442,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, ignore.getPath()); - assertEquals(BooleanType.getInstance(), ignore.getType()); + assertEquals(BaseTypes.booleanType(), ignore.getType()); assertTrue(ignore.isAddedByUses()); // * |-- |-- |-- |-- |-- list subobjects ListSchemaNode subobjects = (ListSchemaNode) reportedRoute.getDataChildByName("subobjects"); @@ -487,7 +483,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, processingRule.getPath()); - assertEquals(BooleanType.getInstance(), processingRule.getType()); + assertEquals(BaseTypes.booleanType(), processingRule.getType()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- |-- |-- |-- leaf ignore ignore = (LeafSchemaNode) bandwidth.getDataChildByName("ignore"); @@ -498,7 +494,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, ignore.getPath()); - assertEquals(BooleanType.getInstance(), ignore.getType()); + assertEquals(BaseTypes.booleanType(), ignore.getType()); assertTrue(ignore.isAddedByUses()); // * |-- |-- |-- |-- |-- container bandwidth ContainerSchemaNode bandwidthInner = (ContainerSchemaNode) bandwidth.getDataChildByName("bandwidth"); @@ -532,7 +528,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, linkDiverse.getPath()); - assertEquals(BooleanType.getInstance(), linkDiverse.getType()); + assertEquals(BaseTypes.booleanType(), linkDiverse.getType().getBaseType()); assertTrue(linkDiverse.isAddedByUses()); // * |-- |-- leaf processing-rule processingRule = (LeafSchemaNode) svec.getDataChildByName("processing-rule"); @@ -543,7 +539,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, processingRule.getPath()); - assertEquals(BooleanType.getInstance(), processingRule.getType()); + assertEquals(BaseTypes.booleanType(), processingRule.getType()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- leaf ignore ignore = (LeafSchemaNode) svec.getDataChildByName("ignore"); @@ -554,7 +550,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, ignore.getPath()); - assertEquals(BooleanType.getInstance(), ignore.getType()); + assertEquals(BaseTypes.booleanType(), ignore.getType()); assertTrue(ignore.isAddedByUses()); // * |-- |-- list metric ListSchemaNode metric = (ListSchemaNode) svec.getDataChildByName("metric"); @@ -574,7 +570,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, metricType.getPath()); - assertEquals(Uint8.getInstance(), metricType.getType()); + assertEquals(BaseTypes.uint8Type(), metricType.getType()); assertTrue(metricType.isAddedByUses()); // * |-- |-- |-- box box = (ContainerSchemaNode) metric.getDataChildByName("box"); @@ -595,7 +591,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, processingRule.getPath()); - assertEquals(BooleanType.getInstance(), processingRule.getType()); + assertEquals(BaseTypes.booleanType(), processingRule.getType()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- |-- leaf ignore ignore = (LeafSchemaNode) metric.getDataChildByName("ignore"); @@ -606,7 +602,7 @@ public class UsesAugmentTest { path.offer(expectedQName); expectedPath= SchemaPath.create(path, true); assertEquals(expectedPath, ignore.getPath()); - assertEquals(BooleanType.getInstance(), ignore.getType()); + assertEquals(BaseTypes.booleanType(), ignore.getType()); assertTrue(ignore.isAddedByUses()); } @@ -624,11 +620,10 @@ public class UsesAugmentTest { } assertNotNull(intExt); - List path = Lists.newArrayList(QName.create(GD_NS, GD_REV, "int-ext")); - SchemaPath expectedPath = SchemaPath.create(path, true); + SchemaPath expectedPath = SchemaPath.create(true, QName.create(GD_NS, GD_REV, "int-ext")); assertEquals(expectedPath, intExt.getPath()); - UnionType union = (UnionType)intExt.getBaseType(); + UnionTypeDefinition union = (UnionTypeDefinition)intExt.getBaseType(); TypeDefinition uint8 = null; TypeDefinition pv = null; @@ -642,8 +637,7 @@ public class UsesAugmentTest { assertNotNull(uint8); assertNotNull(pv); - QName q1 = BaseTypes.constructQName("union"); - expectedPath = SchemaPath.create(Lists.newArrayList(q1), true); + expectedPath = SchemaPath.create(true, QName.create(GD_NS, GD_REV, "int-ext"), QName.create(GD_NS, GD_REV, "union")); assertEquals(expectedPath, union.getPath()); } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserNegativeTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserNegativeTest.java index 1b3c38badf..012f154c82 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserNegativeTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserNegativeTest.java @@ -10,8 +10,6 @@ package org.opendaylight.yangtools.yang.stmt.retest; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; - -import com.google.common.base.Throwables; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; @@ -26,6 +24,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedEx import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.util.YangParseException; import org.opendaylight.yangtools.yang.parser.util.YangValidationException; +import com.google.common.base.Throwables; public class YangParserNegativeTest { @@ -51,7 +50,7 @@ public class YangParserNegativeTest { try { try (InputStream stream = new FileInputStream(yang)) { TestUtils.loadModule(stream); - fail("SomeModifiersUnresolvedException should be thrown."); + fail("IllegalArgumentException should be thrown"); } } catch (SomeModifiersUnresolvedException e) { Throwable rootCause = Throwables.getRootCause(e); @@ -109,7 +108,7 @@ public class YangParserNegativeTest { TestUtils.loadModule(stream); fail("YangParseException should be thrown"); } - } catch (YangParseException e) { + } catch (SourceException e) { assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>")); } } @@ -122,7 +121,7 @@ public class YangParserNegativeTest { TestUtils.loadModule(stream); fail("Exception should be thrown"); } - } catch (YangParseException e) { + } catch (SourceException e) { assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>")); } } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserSimpleTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserSimpleTest.java index 15077ae232..5451ac7f12 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserSimpleTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserSimpleTest.java @@ -12,7 +12,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; - import java.net.URI; import java.text.DateFormat; import java.text.ParseException; @@ -159,8 +158,8 @@ public class YangParserSimpleTest { assertEquals(typedefQName, nodesType.getQName()); SchemaPath nodesTypePath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "nodes-type"); assertEquals(nodesTypePath, nodesType.getPath()); - assertTrue(nodesType.getDescription().isEmpty()); - assertTrue(nodesType.getReference().isEmpty()); + assertNull(nodesType.getDescription()); + assertNull(nodesType.getReference()); assertEquals(Status.CURRENT, nodesType.getStatus()); assertEquals(0, nodesType.getUnknownSchemaNodes().size()); diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserTest.java index ee19e26915..66e65c4381 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserTest.java @@ -15,7 +15,6 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; -import java.math.BigInteger; import java.net.URI; import java.net.URISyntaxException; import java.text.DateFormat; @@ -30,6 +29,7 @@ import java.util.Set; import org.junit.Before; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.YangConstants; import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; @@ -50,18 +50,17 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.Status; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; +import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; +import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser; import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; -import org.opendaylight.yangtools.yang.model.util.Decimal64; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; -import org.opendaylight.yangtools.yang.model.util.Int16; -import org.opendaylight.yangtools.yang.model.util.Int32; -import org.opendaylight.yangtools.yang.model.util.StringType; -import org.opendaylight.yangtools.yang.model.util.Uint32; -import org.opendaylight.yangtools.yang.model.util.UnionType; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; @@ -166,9 +165,10 @@ public class YangParserTest { LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry.getDataChildByName("ifIndex"); assertEquals(ifEntry.getKeyDefinition().get(0), ifIndex.getQName()); - assertTrue(ifIndex.getType() instanceof Uint32); + assertTrue(ifIndex.getType() instanceof UnsignedIntegerTypeDefinition); + assertEquals("minutes", ifIndex.getUnits()); LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry.getDataChildByName("ifMtu"); - assertTrue(ifMtu.getType() instanceof Int32); + assertEquals(BaseTypes.int32Type(), ifMtu.getType()); } @Test @@ -176,55 +176,52 @@ public class YangParserTest { Module foo = TestUtils.findModule(modules, "foo"); LeafSchemaNode int32Leaf = (LeafSchemaNode) foo.getDataChildByName("int32-leaf"); - ExtendedType leafType = (ExtendedType) int32Leaf.getType(); + IntegerTypeDefinition leafType = (IntegerTypeDefinition) int32Leaf.getType(); QName leafTypeQName = leafType.getQName(); assertEquals("int32-ext2", leafTypeQName.getLocalName()); assertEquals(fooNS, leafTypeQName.getNamespace()); assertEquals(fooRev, leafTypeQName.getRevision()); - assertNull(leafType.getUnits()); - assertNull(leafType.getDefaultValue()); - assertTrue(leafType.getLengthConstraints().isEmpty()); - assertTrue(leafType.getPatternConstraints().isEmpty()); + assertEquals("mile", leafType.getUnits()); + assertEquals("11", leafType.getDefaultValue()); + List ranges = leafType.getRangeConstraints(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); - assertEquals(BigInteger.valueOf(12), range.getMin()); - assertEquals(BigInteger.valueOf(20), range.getMax()); + assertEquals(12, range.getMin().intValue()); + assertEquals(20, range.getMax().intValue()); - ExtendedType baseType = (ExtendedType) leafType.getBaseType(); + IntegerTypeDefinition baseType = leafType.getBaseType(); QName baseTypeQName = baseType.getQName(); assertEquals("int32-ext2", baseTypeQName.getLocalName()); assertEquals(barNS, baseTypeQName.getNamespace()); assertEquals(barRev, baseTypeQName.getRevision()); assertEquals("mile", baseType.getUnits()); assertEquals("11", baseType.getDefaultValue()); - assertTrue(leafType.getLengthConstraints().isEmpty()); - assertTrue(leafType.getPatternConstraints().isEmpty()); + List baseTypeRanges = baseType.getRangeConstraints(); assertEquals(2, baseTypeRanges.size()); RangeConstraint baseTypeRange1 = baseTypeRanges.get(0); - assertEquals(BigInteger.valueOf(3), baseTypeRange1.getMin()); - assertEquals(BigInteger.valueOf(9), baseTypeRange1.getMax()); + assertEquals(3, baseTypeRange1.getMin().intValue()); + assertEquals(9, baseTypeRange1.getMax().intValue()); RangeConstraint baseTypeRange2 = baseTypeRanges.get(1); - assertEquals(BigInteger.valueOf(11), baseTypeRange2.getMin()); - assertEquals(BigInteger.valueOf(20), baseTypeRange2.getMax()); + assertEquals(11, baseTypeRange2.getMin().intValue()); + assertEquals(20, baseTypeRange2.getMax().intValue()); - ExtendedType base = (ExtendedType) baseType.getBaseType(); + IntegerTypeDefinition base = baseType.getBaseType(); QName baseQName = base.getQName(); assertEquals("int32-ext1", baseQName.getLocalName()); assertEquals(barNS, baseQName.getNamespace()); assertEquals(barRev, baseQName.getRevision()); assertNull(base.getUnits()); assertNull(base.getDefaultValue()); - assertTrue(leafType.getLengthConstraints().isEmpty()); - assertTrue(leafType.getPatternConstraints().isEmpty()); + List baseRanges = base.getRangeConstraints(); assertEquals(1, baseRanges.size()); RangeConstraint baseRange = baseRanges.get(0); - assertEquals(BigInteger.valueOf(2), baseRange.getMin()); - assertEquals(BigInteger.valueOf(20), baseRange.getMax()); + assertEquals(2, baseRange.getMin().intValue()); + assertEquals(20, baseRange.getMax().intValue()); - assertTrue(base.getBaseType() instanceof Int32); + assertEquals(BaseTypes.int32Type(), base.getBaseType()); } @Test @@ -232,7 +229,8 @@ public class YangParserTest { Module foo = TestUtils.findModule(modules, "foo"); LeafSchemaNode stringleaf = (LeafSchemaNode) foo.getDataChildByName("string-leaf"); - ExtendedType type = (ExtendedType) stringleaf.getType(); + assertTrue(stringleaf.getType() instanceof StringTypeDefinition); + StringTypeDefinition type = (StringTypeDefinition) stringleaf.getType(); QName typeQName = type.getQName(); assertEquals("string-ext4", typeQName.getLocalName()); assertEquals(barNS, typeQName.getNamespace()); @@ -244,9 +242,8 @@ public class YangParserTest { PatternConstraint pattern = patterns.iterator().next(); assertEquals("^[e-z]*$", pattern.getRegularExpression()); assertTrue(type.getLengthConstraints().isEmpty()); - assertTrue(type.getRangeConstraints().isEmpty()); - ExtendedType baseType1 = (ExtendedType) type.getBaseType(); + StringTypeDefinition baseType1 = type.getBaseType(); QName baseType1QName = baseType1.getQName(); assertEquals("string-ext3", baseType1QName.getLocalName()); assertEquals(barNS, baseType1QName.getNamespace()); @@ -258,9 +255,8 @@ public class YangParserTest { pattern = patterns.iterator().next(); assertEquals("^[b-u]*$", pattern.getRegularExpression()); assertTrue(baseType1.getLengthConstraints().isEmpty()); - assertTrue(baseType1.getRangeConstraints().isEmpty()); - ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType(); + StringTypeDefinition baseType2 = baseType1.getBaseType(); QName baseType2QName = baseType2.getQName(); assertEquals("string-ext2", baseType2QName.getLocalName()); assertEquals(barNS, baseType2QName.getNamespace()); @@ -271,11 +267,10 @@ public class YangParserTest { List baseType2Lengths = baseType2.getLengthConstraints(); assertEquals(1, baseType2Lengths.size()); LengthConstraint length = baseType2Lengths.get(0); - assertEquals(BigInteger.valueOf(6), length.getMin()); - assertEquals(BigInteger.TEN, length.getMax()); - assertTrue(baseType2.getRangeConstraints().isEmpty()); + assertEquals(6, length.getMin().intValue()); + assertEquals(10, length.getMax().intValue()); - ExtendedType baseType3 = (ExtendedType) baseType2.getBaseType(); + StringTypeDefinition baseType3 = baseType2.getBaseType(); QName baseType3QName = baseType3.getQName(); assertEquals("string-ext1", baseType3QName.getLocalName()); assertEquals(barNS, baseType3QName.getNamespace()); @@ -289,11 +284,10 @@ public class YangParserTest { List baseType3Lengths = baseType3.getLengthConstraints(); assertEquals(1, baseType3Lengths.size()); length = baseType3Lengths.get(0); - assertEquals(BigInteger.valueOf(5), length.getMin()); - assertEquals(BigInteger.valueOf(11), length.getMax()); - assertTrue(baseType3.getRangeConstraints().isEmpty()); + assertEquals(5, length.getMin().intValue()); + assertEquals(11, length.getMax().intValue()); - assertTrue(baseType3.getBaseType() instanceof StringType); + assertEquals(BaseTypes.stringType(), baseType3.getBaseType()); } @Test @@ -301,7 +295,7 @@ public class YangParserTest { Module foo = TestUtils.findModule(modules, "foo"); final LeafSchemaNode invalidPatternStringLeaf = (LeafSchemaNode) foo .getDataChildByName("invalid-pattern-string-leaf"); - ExtendedType type = (ExtendedType) invalidPatternStringLeaf.getType(); + StringTypeDefinition type = (StringTypeDefinition) invalidPatternStringLeaf.getType(); QName typeQName = type.getQName(); assertEquals("invalid-string-pattern", typeQName.getLocalName()); assertEquals(barNS, typeQName.getNamespace()); @@ -313,11 +307,11 @@ public class YangParserTest { final LeafSchemaNode invalidDirectStringPatternDefLeaf = (LeafSchemaNode) foo .getDataChildByName("invalid-direct-string-pattern-def-leaf"); - type = (ExtendedType) invalidDirectStringPatternDefLeaf.getType(); + type = (StringTypeDefinition) invalidDirectStringPatternDefLeaf.getType(); typeQName = type.getQName(); assertEquals("string", typeQName.getLocalName()); - assertEquals(fooNS, typeQName.getNamespace()); - assertEquals(fooRev, typeQName.getRevision()); + assertEquals(YangConstants.RFC6020_YANG_NAMESPACE, typeQName.getNamespace()); + assertNull(typeQName.getRevision()); assertNull(type.getUnits()); assertNull(type.getDefaultValue()); patterns = type.getPatternConstraints(); @@ -325,7 +319,7 @@ public class YangParserTest { final LeafSchemaNode multiplePatternStringLeaf = (LeafSchemaNode) foo .getDataChildByName("multiple-pattern-string-leaf"); - type = (ExtendedType) multiplePatternStringLeaf.getType(); + type = (StringTypeDefinition) multiplePatternStringLeaf.getType(); typeQName = type.getQName(); assertEquals("multiple-pattern-string", typeQName.getLocalName()); assertEquals(barNS, typeQName.getNamespace()); @@ -338,11 +332,10 @@ public class YangParserTest { PatternConstraint pattern = patterns.iterator().next(); assertEquals("^[e-z]*$", pattern.getRegularExpression()); assertTrue(type.getLengthConstraints().isEmpty()); - assertTrue(type.getRangeConstraints().isEmpty()); final LeafSchemaNode multiplePatternDirectStringDefLeaf = (LeafSchemaNode) foo .getDataChildByName("multiple-pattern-direct-string-def-leaf"); - type = (ExtendedType) multiplePatternDirectStringDefLeaf.getType(); + type = (StringTypeDefinition) multiplePatternDirectStringDefLeaf.getType(); typeQName = type.getQName(); assertEquals("string", typeQName.getLocalName()); assertEquals(fooNS, typeQName.getNamespace()); @@ -371,7 +364,7 @@ public class YangParserTest { Module foo = TestUtils.findModule(modules, "foo"); LeafSchemaNode lengthLeaf = (LeafSchemaNode) foo.getDataChildByName("length-leaf"); - ExtendedType type = (ExtendedType) lengthLeaf.getType(); + StringTypeDefinition type = (StringTypeDefinition) lengthLeaf.getType(); QName typeQName = type.getQName(); assertEquals("string-ext2", typeQName.getLocalName()); @@ -383,11 +376,10 @@ public class YangParserTest { List typeLengths = type.getLengthConstraints(); assertEquals(1, typeLengths.size()); LengthConstraint length = typeLengths.get(0); - assertEquals(BigInteger.valueOf(7), length.getMin()); - assertEquals(BigInteger.TEN, length.getMax()); - assertTrue(type.getRangeConstraints().isEmpty()); + assertEquals(7, length.getMin().intValue()); + assertEquals(10, length.getMax().intValue()); - ExtendedType baseType1 = (ExtendedType) type.getBaseType(); + StringTypeDefinition baseType1 = type.getBaseType(); QName baseType1QName = baseType1.getQName(); assertEquals("string-ext2", baseType1QName.getLocalName()); assertEquals(barNS, baseType1QName.getNamespace()); @@ -398,11 +390,10 @@ public class YangParserTest { List baseType2Lengths = baseType1.getLengthConstraints(); assertEquals(1, baseType2Lengths.size()); length = baseType2Lengths.get(0); - assertEquals(BigInteger.valueOf(6), length.getMin()); - assertEquals(BigInteger.TEN, length.getMax()); - assertTrue(baseType1.getRangeConstraints().isEmpty()); + assertEquals(6, length.getMin().intValue()); + assertEquals(10, length.getMax().intValue()); - ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType(); + StringTypeDefinition baseType2 = baseType1.getBaseType(); QName baseType2QName = baseType2.getQName(); assertEquals("string-ext1", baseType2QName.getLocalName()); assertEquals(barNS, baseType2QName.getNamespace()); @@ -416,11 +407,10 @@ public class YangParserTest { List baseType3Lengths = baseType2.getLengthConstraints(); assertEquals(1, baseType3Lengths.size()); length = baseType3Lengths.get(0); - assertEquals(BigInteger.valueOf(5), length.getMin()); - assertEquals(BigInteger.valueOf(11), length.getMax()); - assertTrue(baseType2.getRangeConstraints().isEmpty()); + assertEquals(5, length.getMin().intValue()); + assertEquals(11, length.getMax().intValue()); - assertTrue(baseType2.getBaseType() instanceof StringType); + assertEquals(BaseTypes.stringType(), baseType2.getBaseType()); } @Test @@ -428,32 +418,28 @@ public class YangParserTest { Module foo = TestUtils.findModule(modules, "foo"); LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("decimal-leaf"); - ExtendedType type = (ExtendedType) testleaf.getType(); + assertTrue(testleaf.getType() instanceof DecimalTypeDefinition); + DecimalTypeDefinition type = (DecimalTypeDefinition) testleaf.getType(); QName typeQName = type.getQName(); assertEquals("my-decimal-type", typeQName.getLocalName()); - assertEquals(fooNS, typeQName.getNamespace()); - assertEquals(fooRev, typeQName.getRevision()); + assertEquals(barNS, typeQName.getNamespace()); + assertEquals(barRev, typeQName.getRevision()); assertNull(type.getUnits()); assertNull(type.getDefaultValue()); - assertEquals(4, (int) type.getFractionDigits()); - assertTrue(type.getLengthConstraints().isEmpty()); - assertTrue(type.getPatternConstraints().isEmpty()); - assertTrue(type.getRangeConstraints().isEmpty()); + assertEquals(6, type.getFractionDigits().intValue()); + assertEquals(1, type.getRangeConstraints().size()); - ExtendedType typeBase = (ExtendedType) type.getBaseType(); + DecimalTypeDefinition typeBase = type.getBaseType(); QName typeBaseQName = typeBase.getQName(); - assertEquals("my-decimal-type", typeBaseQName.getLocalName()); + assertEquals("decimal64", typeBaseQName.getLocalName()); assertEquals(barNS, typeBaseQName.getNamespace()); assertEquals(barRev, typeBaseQName.getRevision()); assertNull(typeBase.getUnits()); assertNull(typeBase.getDefaultValue()); - assertNull(typeBase.getFractionDigits()); - assertTrue(typeBase.getLengthConstraints().isEmpty()); - assertTrue(typeBase.getPatternConstraints().isEmpty()); - assertTrue(typeBase.getRangeConstraints().isEmpty()); + assertEquals(6, typeBase.getFractionDigits().intValue()); + assertEquals(1, typeBase.getRangeConstraints().size()); - Decimal64 decimal = (Decimal64) typeBase.getBaseType(); - assertEquals(6, (int) decimal.getFractionDigits()); + assertNull(typeBase.getBaseType()); } @Test @@ -461,20 +447,19 @@ public class YangParserTest { Module foo = TestUtils.findModule(modules, "foo"); LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("decimal-leaf2"); - ExtendedType type = (ExtendedType) testleaf.getType(); + assertTrue(testleaf.getType() instanceof DecimalTypeDefinition); + DecimalTypeDefinition type = (DecimalTypeDefinition) testleaf.getType(); QName typeQName = type.getQName(); assertEquals("my-decimal-type", typeQName.getLocalName()); assertEquals(barNS, typeQName.getNamespace()); assertEquals(barRev, typeQName.getRevision()); assertNull(type.getUnits()); assertNull(type.getDefaultValue()); - assertNull(type.getFractionDigits()); - assertTrue(type.getLengthConstraints().isEmpty()); - assertTrue(type.getPatternConstraints().isEmpty()); - assertTrue(type.getRangeConstraints().isEmpty()); + assertEquals(6, type.getFractionDigits().intValue()); + assertEquals(1, type.getRangeConstraints().size()); - Decimal64 baseTypeDecimal = (Decimal64) type.getBaseType(); - assertEquals(6, (int) baseTypeDecimal.getFractionDigits()); + DecimalTypeDefinition baseTypeDecimal = type.getBaseType(); + assertEquals(6, baseTypeDecimal.getFractionDigits().intValue()); } @Test @@ -482,52 +467,43 @@ public class YangParserTest { Module foo = TestUtils.findModule(modules, "foo"); LeafSchemaNode unionleaf = (LeafSchemaNode) foo.getDataChildByName("union-leaf"); - ExtendedType type = (ExtendedType) unionleaf.getType(); + assertTrue(unionleaf.getType() instanceof UnionTypeDefinition); + UnionTypeDefinition type = (UnionTypeDefinition) unionleaf.getType(); QName typeQName = type.getQName(); assertEquals("my-union-ext", typeQName.getLocalName()); assertEquals(barNS, typeQName.getNamespace()); assertEquals(barRev, typeQName.getRevision()); assertNull(type.getUnits()); assertNull(type.getDefaultValue()); - assertNull(type.getFractionDigits()); - assertTrue(type.getLengthConstraints().isEmpty()); - assertTrue(type.getPatternConstraints().isEmpty()); - assertTrue(type.getRangeConstraints().isEmpty()); - ExtendedType baseType = (ExtendedType) type.getBaseType(); + UnionTypeDefinition baseType = type.getBaseType(); QName baseTypeQName = baseType.getQName(); assertEquals("my-union", baseTypeQName.getLocalName()); assertEquals(barNS, baseTypeQName.getNamespace()); assertEquals(barRev, baseTypeQName.getRevision()); assertNull(baseType.getUnits()); assertNull(baseType.getDefaultValue()); - assertNull(baseType.getFractionDigits()); - assertTrue(baseType.getLengthConstraints().isEmpty()); - assertTrue(baseType.getPatternConstraints().isEmpty()); - assertTrue(baseType.getRangeConstraints().isEmpty()); - UnionType unionType = (UnionType) baseType.getBaseType(); + UnionTypeDefinition unionType = baseType.getBaseType(); List> unionTypes = unionType.getTypes(); assertEquals(2, unionTypes.size()); - ExtendedType unionType1 = (ExtendedType) unionTypes.get(0); + IntegerTypeDefinition unionType1 = (IntegerTypeDefinition) unionTypes.get(0); QName unionType1QName = baseType.getQName(); assertEquals("my-union", unionType1QName.getLocalName()); assertEquals(barNS, unionType1QName.getNamespace()); assertEquals(barRev, unionType1QName.getRevision()); assertNull(unionType1.getUnits()); assertNull(unionType1.getDefaultValue()); - assertNull(unionType1.getFractionDigits()); - assertTrue(unionType1.getLengthConstraints().isEmpty()); - assertTrue(unionType1.getPatternConstraints().isEmpty()); + List ranges = unionType1.getRangeConstraints(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); - assertEquals(BigInteger.ONE, range.getMin()); - assertEquals(BigInteger.valueOf(100), range.getMax()); - assertTrue(unionType1.getBaseType() instanceof Int16); + assertEquals(1, range.getMin().intValue()); + assertEquals(100, range.getMax().intValue()); + assertEquals(BaseTypes.int16Type(), unionType1.getBaseType()); - assertTrue(unionTypes.get(1) instanceof Int32); + assertEquals(BaseTypes.int32Type(), unionTypes.get(1)); } @Test @@ -535,101 +511,80 @@ public class YangParserTest { Module foo = TestUtils.findModule(modules, "foo"); LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("custom-union-leaf"); - ExtendedType type = (ExtendedType) testleaf.getType(); + assertTrue(testleaf.getType() instanceof UnionTypeDefinition); + UnionTypeDefinition type = (UnionTypeDefinition) testleaf.getType(); QName testleafTypeQName = type.getQName(); assertEquals(bazNS, testleafTypeQName.getNamespace()); assertEquals(bazRev, testleafTypeQName.getRevision()); assertEquals("union1", testleafTypeQName.getLocalName()); assertNull(type.getUnits()); assertNull(type.getDefaultValue()); - assertNull(type.getFractionDigits()); - assertTrue(type.getLengthConstraints().isEmpty()); - assertTrue(type.getPatternConstraints().isEmpty()); - assertTrue(type.getRangeConstraints().isEmpty()); - ExtendedType typeBase = (ExtendedType) type.getBaseType(); + UnionTypeDefinition typeBase = type.getBaseType(); QName typeBaseQName = typeBase.getQName(); assertEquals(bazNS, typeBaseQName.getNamespace()); assertEquals(bazRev, typeBaseQName.getRevision()); assertEquals("union2", typeBaseQName.getLocalName()); assertNull(typeBase.getUnits()); assertNull(typeBase.getDefaultValue()); - assertNull(typeBase.getFractionDigits()); - assertTrue(typeBase.getLengthConstraints().isEmpty()); - assertTrue(typeBase.getPatternConstraints().isEmpty()); - assertTrue(typeBase.getRangeConstraints().isEmpty()); - UnionType union = (UnionType) typeBase.getBaseType(); + UnionTypeDefinition union = typeBase.getBaseType(); List> unionTypes = union.getTypes(); assertEquals(2, unionTypes.size()); - assertTrue(unionTypes.get(0) instanceof Int32); - assertTrue(unionTypes.get(1) instanceof ExtendedType); + assertEquals(BaseTypes.int32Type(), unionTypes.get(0)); + assertTrue(unionTypes.get(1) instanceof UnionTypeDefinition); - ExtendedType unionType1 = (ExtendedType) unionTypes.get(1); + UnionTypeDefinition unionType1 = (UnionTypeDefinition) unionTypes.get(1); QName uniontType1QName = unionType1.getQName(); assertEquals(barNS, uniontType1QName.getNamespace()); assertEquals(barRev, uniontType1QName.getRevision()); assertEquals("nested-union2", uniontType1QName.getLocalName()); assertNull(unionType1.getUnits()); assertNull(unionType1.getDefaultValue()); - assertNull(unionType1.getFractionDigits()); - assertTrue(unionType1.getLengthConstraints().isEmpty()); - assertTrue(unionType1.getPatternConstraints().isEmpty()); - assertTrue(unionType1.getRangeConstraints().isEmpty()); - UnionType nestedUnion = (UnionType) unionType1.getBaseType(); + UnionTypeDefinition nestedUnion = unionType1.getBaseType(); List> nestedUnion2Types = nestedUnion.getTypes(); assertEquals(2, nestedUnion2Types.size()); - assertTrue(nestedUnion2Types.get(0) instanceof StringType); - assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType); + assertTrue(nestedUnion2Types.get(1) instanceof StringTypeDefinition); + assertTrue(nestedUnion2Types.get(0) instanceof UnionTypeDefinition); - ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1); + UnionTypeDefinition myUnionExt = (UnionTypeDefinition) nestedUnion2Types.get(0); QName myUnionExtQName = myUnionExt.getQName(); assertEquals(barNS, myUnionExtQName.getNamespace()); assertEquals(barRev, myUnionExtQName.getRevision()); assertEquals("my-union-ext", myUnionExtQName.getLocalName()); assertNull(myUnionExt.getUnits()); assertNull(myUnionExt.getDefaultValue()); - assertNull(myUnionExt.getFractionDigits()); - assertTrue(myUnionExt.getLengthConstraints().isEmpty()); - assertTrue(myUnionExt.getPatternConstraints().isEmpty()); - assertTrue(myUnionExt.getRangeConstraints().isEmpty()); - ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType(); + + UnionTypeDefinition myUnion = myUnionExt.getBaseType(); QName myUnionQName = myUnion.getQName(); assertEquals(barNS, myUnionQName.getNamespace()); assertEquals(barRev, myUnionQName.getRevision()); assertEquals("my-union", myUnionQName.getLocalName()); assertNull(myUnion.getUnits()); assertNull(myUnion.getDefaultValue()); - assertNull(myUnion.getFractionDigits()); - assertTrue(myUnion.getLengthConstraints().isEmpty()); - assertTrue(myUnion.getPatternConstraints().isEmpty()); - assertTrue(myUnion.getRangeConstraints().isEmpty()); - UnionType myUnionBase = (UnionType) myUnion.getBaseType(); + UnionTypeDefinition myUnionBase = myUnion.getBaseType(); List> myUnionBaseTypes = myUnionBase.getTypes(); assertEquals(2, myUnionBaseTypes.size()); - assertTrue(myUnionBaseTypes.get(0) instanceof ExtendedType); - assertTrue(myUnionBaseTypes.get(1) instanceof Int32); + assertTrue(myUnionBaseTypes.get(0) instanceof IntegerTypeDefinition); + assertEquals(BaseTypes.int32Type(), myUnionBaseTypes.get(1)); - ExtendedType int16Ext = (ExtendedType) myUnionBaseTypes.get(0); + IntegerTypeDefinition int16Ext = (IntegerTypeDefinition) myUnionBaseTypes.get(0); QName int16ExtQName = int16Ext.getQName(); assertEquals(barNS, int16ExtQName.getNamespace()); assertEquals(barRev, int16ExtQName.getRevision()); assertEquals("int16", int16ExtQName.getLocalName()); assertNull(int16Ext.getUnits()); assertNull(int16Ext.getDefaultValue()); - assertNull(int16Ext.getFractionDigits()); - assertTrue(int16Ext.getLengthConstraints().isEmpty()); - assertTrue(int16Ext.getPatternConstraints().isEmpty()); List ranges = int16Ext.getRangeConstraints(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); - assertEquals(BigInteger.ONE, range.getMin()); - assertEquals(BigInteger.valueOf(100), range.getMax()); + assertEquals(1, range.getMin().intValue()); + assertEquals(100, range.getMax().intValue()); - assertTrue(int16Ext.getBaseType() instanceof Int16); + assertEquals(BaseTypes.int16Type(), int16Ext.getBaseType()); } @Test @@ -726,9 +681,9 @@ public class YangParserTest { assertEquals(0, notification.getUses().size()); LeafSchemaNode eventClass = (LeafSchemaNode) notification.getDataChildByName("event-class"); - assertTrue(eventClass.getType() instanceof StringType); + assertTrue(eventClass.getType() instanceof StringTypeDefinition); LeafSchemaNode severity = (LeafSchemaNode) notification.getDataChildByName("severity"); - assertTrue(severity.getType() instanceof StringType); + assertTrue(severity.getType() instanceof StringTypeDefinition); } @Test @@ -749,7 +704,7 @@ public class YangParserTest { Set> types = bar.getTypeDefinitions(); // int32-ext1 - ExtendedType int32ext1 = (ExtendedType) TestUtils.findTypedef(types, "int32-ext1"); + IntegerTypeDefinition int32ext1 = (IntegerTypeDefinition) TestUtils.findTypedef(types, "int32-ext1"); QName int32TypedefQName = int32ext1.getQName(); assertEquals(barNS, int32TypedefQName.getNamespace()); @@ -763,8 +718,8 @@ public class YangParserTest { assertFalse(typePathIt.hasNext()); // int32-ext1/int32 - Int32 int32 = (Int32) int32ext1.getBaseType(); - assertEquals(Int32.getInstance(), int32); + IntegerTypeDefinition int32 = int32ext1.getBaseType(); + assertEquals(BaseTypes.int32Type(), int32); } @Test @@ -773,7 +728,7 @@ public class YangParserTest { Set> types = bar.getTypeDefinitions(); // my-decimal-type - ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type"); + DecimalTypeDefinition myDecType = (DecimalTypeDefinition) TestUtils.findTypedef(types, "my-decimal-type"); QName myDecTypeQName = myDecType.getQName(); assertEquals(barNS, myDecTypeQName.getNamespace()); @@ -787,11 +742,11 @@ public class YangParserTest { assertFalse(typePathIt.hasNext()); // my-base-int32-type/int32 - Decimal64 dec64 = (Decimal64) myDecType.getBaseType(); + DecimalTypeDefinition dec64 = myDecType.getBaseType(); QName dec64QName = dec64.getQName(); - assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), dec64QName.getNamespace()); - assertNull(dec64QName.getRevision()); + assertEquals(barNS, dec64QName.getNamespace()); + assertEquals(barRev, dec64QName.getRevision()); assertEquals("decimal64", dec64QName.getLocalName()); SchemaPath dec64SchemaPath = dec64.getPath(); diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserWithContextTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserWithContextTest.java index 102b04a270..9b299b9969 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserWithContextTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserWithContextTest.java @@ -11,15 +11,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; - -import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil; - -import org.opendaylight.yangtools.yang.stmt.test.StmtTestUtils; -import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl; import java.io.File; -import java.math.BigInteger; import java.net.URI; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -32,6 +24,7 @@ import java.util.Map; import java.util.Set; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil; import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode; import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; @@ -50,8 +43,12 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.api.UsesNode; import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; +import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; +import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl; +import org.opendaylight.yangtools.yang.stmt.test.StmtTestUtils; public class YangParserWithContextTest { private final DateFormat simpleDateFormat = new SimpleDateFormat( @@ -114,26 +111,27 @@ public class YangParserWithContextTest { LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName("id"); - ExtendedType leafType = (ExtendedType) leaf.getType(); + assertTrue(leaf.getType() instanceof UnsignedIntegerTypeDefinition); + UnsignedIntegerTypeDefinition leafType = (UnsignedIntegerTypeDefinition) leaf.getType(); QName qname = leafType.getQName(); assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace()); assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision()); assertEquals("port-number", qname.getLocalName()); - ExtendedType leafBaseType = (ExtendedType) leafType.getBaseType(); + UnsignedIntegerTypeDefinition leafBaseType = leafType.getBaseType(); qname = leafBaseType.getQName(); assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace()); assertEquals(simpleDateFormat.parse("2010-09-24"), qname.getRevision()); assertEquals("port-number", qname.getLocalName()); - ExtendedType dscpExt = (ExtendedType) TestUtils.findTypedef( + UnsignedIntegerTypeDefinition dscpExt = (UnsignedIntegerTypeDefinition) TestUtils.findTypedef( module.getTypeDefinitions(), "dscp-ext"); List ranges = dscpExt.getRangeConstraints(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); - assertEquals(BigInteger.ZERO, range.getMin()); - assertEquals(BigInteger.valueOf(63), range.getMax()); + assertEquals(0, range.getMin().intValue()); + assertEquals(63, range.getMax().intValue()); } @Test diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/ListTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/ListTest.java index edb33fc023..8dae4dd336 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/ListTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/ListTest.java @@ -12,7 +12,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; - import java.util.List; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; @@ -76,7 +75,7 @@ public class ListTest { leaf = (LeafSchemaNode) list.getDataChildByName("young-leaf"); assertNotNull(leaf); assertFalse(leaf.getConstraints().isMandatory()); - assertEquals("string", leaf.getType().getQName().getLocalName()); + assertEquals("young-leaf", leaf.getType().getQName().getLocalName()); assertEquals("default-value", leaf.getDefault()); LeafListSchemaNode leafList = (LeafListSchemaNode) list.getDataChildByName("list-of-leaves"); diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/TypedefConstraintsTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/TypedefConstraintsTest.java index 263c179c55..d8b6fc29b4 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/TypedefConstraintsTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/TypedefConstraintsTest.java @@ -2,25 +2,23 @@ package org.opendaylight.yangtools.yang.stmt.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; - +import java.io.FileNotFoundException; import java.math.BigDecimal; - -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; -import org.opendaylight.yangtools.yang.model.util.Decimal64; -import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import java.net.URISyntaxException; import java.util.List; -import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; import java.util.Set; -import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.junit.Test; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import java.io.FileNotFoundException; -import java.net.URISyntaxException; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; -import org.junit.Test; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; public class TypedefConstraintsTest { @@ -39,9 +37,9 @@ public class TypedefConstraintsTest { TypeDefinition myDecimal = typeDefinitions.iterator().next(); assertNotNull(myDecimal); - assertTrue(myDecimal instanceof ExtendedType); + assertTrue(myDecimal instanceof DecimalTypeDefinition); - List rangeConstraints = ((ExtendedType) myDecimal) + List rangeConstraints = ((DecimalTypeDefinition) myDecimal) .getRangeConstraints(); assertNotNull(rangeConstraints); @@ -54,8 +52,8 @@ public class TypedefConstraintsTest { LeafSchemaNode leafDecimal = (LeafSchemaNode) dataNode; TypeDefinition type = leafDecimal.getType(); - assertTrue(type instanceof ExtendedType); - ExtendedType decType = (ExtendedType) type; + assertTrue(type instanceof DecimalTypeDefinition); + DecimalTypeDefinition decType = (DecimalTypeDefinition) type; List decRangeConstraints = decType.getRangeConstraints(); @@ -67,7 +65,7 @@ public class TypedefConstraintsTest { assertTrue(decType.getQName().getModule().equals(leafDecimal.getQName().getModule())); assertTrue(decType.getQName().getLocalName().equals(TypeUtils.DECIMAL64)); - assertTrue(decType.getBaseType() instanceof Decimal64); + assertNull(decType.getBaseType()); } } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/YangTypes2StmtTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/YangTypes2StmtTest.java index c04a49d3fc..1dcc82b131 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/YangTypes2StmtTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/YangTypes2StmtTest.java @@ -9,22 +9,15 @@ package org.opendaylight.yangtools.yang.stmt.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.net.URI; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; -import org.opendaylight.yangtools.yang.model.util.BooleanType; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; -import org.opendaylight.yangtools.yang.model.util.Int16; -import org.opendaylight.yangtools.yang.model.util.Int32; -import org.opendaylight.yangtools.yang.model.util.Int64; -import org.opendaylight.yangtools.yang.model.util.Int8; -import org.opendaylight.yangtools.yang.model.util.Uint16; -import org.opendaylight.yangtools.yang.model.util.Uint32; -import org.opendaylight.yangtools.yang.model.util.Uint64; -import org.opendaylight.yangtools.yang.model.util.Uint8; +import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource; @@ -68,45 +61,46 @@ public class YangTypes2StmtTest { final LeafSchemaNode lfDecimalNode = (LeafSchemaNode) result.getDataChildByName(lfDecimal); assertNotNull(lfDecimalNode); - final ExtendedType lfDecimalNodeType = (ExtendedType) lfDecimalNode.getType(); - assertEquals(ExtendedType.class, lfDecimalNodeType.getClass()); + + assertTrue(lfDecimalNode.getType() instanceof DecimalTypeDefinition); + final DecimalTypeDefinition lfDecimalNodeType = (DecimalTypeDefinition) lfDecimalNode.getType(); assertEquals(2, lfDecimalNodeType.getFractionDigits().intValue()); final LeafSchemaNode lfInt8Node = (LeafSchemaNode) result.getDataChildByName(lfInt8); assertNotNull(lfInt8Node); - assertEquals(Int8.class, lfInt8Node.getType().getClass()); + assertEquals(BaseTypes.int8Type().getClass(), lfInt8Node.getType().getClass()); final LeafSchemaNode lfInt16Node = (LeafSchemaNode) result.getDataChildByName(lfInt16); assertNotNull(lfInt16Node); - assertEquals(Int16.class, lfInt16Node.getType().getClass()); + assertEquals(BaseTypes.int16Type().getClass(), lfInt16Node.getType().getClass()); final LeafSchemaNode lfInt32Node = (LeafSchemaNode) result.getDataChildByName(lfInt32); assertNotNull(lfInt32Node); - assertEquals(Int32.class, lfInt32Node.getType().getClass()); + assertEquals(BaseTypes.int32Type().getClass(), lfInt32Node.getType().getClass()); final LeafSchemaNode lfInt64Node = (LeafSchemaNode) result.getDataChildByName(lfInt64); assertNotNull(lfInt64Node); - assertEquals(Int64.class, lfInt64Node.getType().getClass()); + assertEquals(BaseTypes.int64Type().getClass(), lfInt64Node.getType().getClass()); final LeafSchemaNode lfUInt8Node = (LeafSchemaNode) result.getDataChildByName(lfUInt8); assertNotNull(lfUInt8Node); - assertEquals(Uint8.class, lfUInt8Node.getType().getClass()); + assertEquals(BaseTypes.uint8Type().getClass(), lfUInt8Node.getType().getClass()); final LeafSchemaNode lfUInt16Node = (LeafSchemaNode) result.getDataChildByName(lfUInt16); assertNotNull(lfUInt16Node); - assertEquals(Uint16.class, lfUInt16Node.getType().getClass()); + assertEquals(BaseTypes.uint16Type().getClass(), lfUInt16Node.getType().getClass()); final LeafSchemaNode lfUInt32Node = (LeafSchemaNode) result.getDataChildByName(lfUInt32); assertNotNull(lfUInt32Node); - assertEquals(Uint32.class, lfUInt32Node.getType().getClass()); + assertEquals(BaseTypes.uint32Type().getClass(), lfUInt32Node.getType().getClass()); final LeafSchemaNode lfUInt64Node = (LeafSchemaNode) result.getDataChildByName(lfUInt64); assertNotNull(lfUInt64Node); - assertEquals(Uint64.class, lfUInt64Node.getType().getClass()); + assertEquals(BaseTypes.uint64Type().getClass(), lfUInt64Node.getType().getClass()); final LeafSchemaNode lfBoolNode = (LeafSchemaNode) result.getDataChildByName(lfBool); assertNotNull(lfBoolNode); - assertEquals(BooleanType.class, lfBoolNode.getType().getClass()); + assertEquals(BaseTypes.booleanType().getClass(), lfBoolNode.getType().getClass()); } private static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final StatementStreamSource... sources) { diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/yin/YinFileListStmtTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/yin/YinFileListStmtTest.java index 0abffe3e93..27a27313c3 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/yin/YinFileListStmtTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/yin/YinFileListStmtTest.java @@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.stmt.test.yin; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; - import java.net.URISyntaxException; import java.util.Collection; import java.util.Iterator; @@ -24,6 +23,9 @@ import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.stmt.retest.TestUtils; @@ -58,12 +60,15 @@ public class YinFileListStmtTest { LeafSchemaNode leaf = (LeafSchemaNode) childrenIterator.next(); assertEquals("name", leaf.getQName().getLocalName()); assertEquals("Unique module instance name", leaf.getDescription()); - assertTrue(leaf.getType().toString().startsWith("StringType")); + assertEquals(BaseTypes.stringType(), leaf.getType()); assertTrue(leaf.getConstraints().isMandatory()); leaf = (LeafSchemaNode) childrenIterator.next(); assertEquals("type", leaf.getQName().getLocalName()); - assertEquals("identityref module-type", leaf.getType().toString()); + + final TypeDefinition leafType = leaf.getType(); + assertTrue(leafType instanceof IdentityrefTypeDefinition); + assertEquals("module-type", ((IdentityrefTypeDefinition)leafType).getIdentity().getQName().getLocalName()); assertTrue(leaf.getConstraints().isMandatory()); } diff --git a/yang/yang-parser-impl/src/test/resources/type-tests/types.yang b/yang/yang-parser-impl/src/test/resources/type-tests/types.yang index 400aca9fac..eb57402a6d 100644 --- a/yang/yang-parser-impl/src/test/resources/type-tests/types.yang +++ b/yang/yang-parser-impl/src/test/resources/type-tests/types.yang @@ -106,7 +106,7 @@ module types { value 1; } bit three { - position 0; + position 2; description "test bit"; reference "test bit ref"; status current; -- 2.36.6