*/
package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
+import static org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator.MAX;
+import com.google.common.base.Verify;
import java.util.Collection;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
-
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ExtendedTypeEffectiveStatementImpl;
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.SubstatementValidator;
+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;
+import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
+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.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<String>
implements TypeStatement {
+ private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
+ .TYPE)
+ .add(Rfc6020Mapping.BASE, 0, 1)
+ .add(Rfc6020Mapping.BIT, 0, MAX)
+ .add(Rfc6020Mapping.ENUM, 0, MAX)
+ .add(Rfc6020Mapping.FRACTION_DIGITS, 0, 1)
+ .add(Rfc6020Mapping.LENGTH, 0, 1)
+ .add(Rfc6020Mapping.PATH, 0, 1)
+ .add(Rfc6020Mapping.PATTERN, 0, MAX)
+ .add(Rfc6020Mapping.RANGE, 0, 1)
+ .add(Rfc6020Mapping.REQUIRE_INSTANCE, 0, 1)
+ .add(Rfc6020Mapping.TYPE, 0, MAX)
+ .build();
- protected TypeStatementImpl(StmtContext<String, TypeStatement, ?> context) {
+ protected TypeStatementImpl(final StmtContext<String, TypeStatement, ?> context) {
super(context);
}
}
@Override
- public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value)
- throws SourceException {
+ public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
return value;
}
@Override
public TypeStatement createDeclared(
- StmtContext<String, TypeStatement, ?> ctx) {
+ final StmtContext<String, TypeStatement, ?> ctx) {
return new TypeStatementImpl(ctx);
}
@Override
- public EffectiveStatement<String, TypeStatement> createEffective(
- StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
-
- // :FIXME improve the test of isExtended - e.g. unknown statements,
- // etc..
- Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = ctx
- .declaredSubstatements();
- boolean isExtended = declaredSubstatements.isEmpty() ? false
- : true;
- if (isExtended)
- return new ExtendedTypeEffectiveStatementImpl(ctx, true);
+ public TypeEffectiveStatement<TypeStatement> createEffective(
+ final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
+ // First look up the proper base type
+ final TypeEffectiveStatement<TypeStatement> 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);
+ 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<?, TypedefStatement, TypedefEffectiveStatement> typedef =
+ ctx.getFromNamespace(TypeNamespace.class, qname);
+ SourceException.throwIfNull(typedef, ctx.getStatementSourceReference(), "Type '%s' not found", qname);
+
+ 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);
}
}
+
+ @Override
+ public void onFullDefinitionDeclared(
+ final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt){
+ SUBSTATEMENT_VALIDATOR.validate(stmt);
+
+ // if it is yang built-in type, no prerequisite is needed, so simply return
+ if (TypeUtils.isYangBuiltInTypeString(stmt.getStatementArgument())) {
+ return;
+ }
+
+ final QName typeQName = Utils.qNameFromArgument(stmt, stmt.getStatementArgument());
+ final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
+ final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
+ typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
+ typeAction.mutatesEffectiveCtx(stmt.getParentContext());
+
+ /*
+ * If the type does not exist, throw new InferenceException.
+ * Otherwise perform no operation.
+ */
+ typeAction.apply(new InferenceAction() {
+ @Override
+ public void apply() {
+ // Intentional NOOP
+ }
+
+ @Override
+ public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
+ InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
+ "Type [%s] was not found.", typeQName);
+ }
+ });
+ }
}
@Nonnull