2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
10 import com.google.common.base.Verify;
11 import java.util.Collection;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
21 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
34 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
35 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
45 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
46 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.TypeDefEffectiveStatementImpl;
47 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BinaryTypeEffectiveStatementImpl;
48 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BitsTypeEffectiveStatementImpl;
49 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BooleanTypeEffectiveStatementImpl;
50 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BuiltinEffectiveStatements;
51 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.DecimalTypeEffectiveStatementImpl;
52 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EmptyTypeEffectiveStatementImpl;
53 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EnumTypeEffectiveStatementImpl;
54 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IdentityrefTypeEffectiveStatementImpl;
55 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.InstanceIdentifierTypeEffectiveStatementImpl;
56 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IntegerTypeEffectiveStatementImpl;
57 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LeafrefTypeEffectiveStatementImpl;
58 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.StringTypeEffectiveStatementImpl;
59 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnionTypeEffectiveStatementImpl;
60 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnsignedIntegerTypeEffectiveStatementImpl;
62 public class TypeStatementImpl extends AbstractDeclaredStatement<String>
63 implements TypeStatement {
64 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
66 .addOptional(YangStmtMapping.BASE)
67 .addAny(YangStmtMapping.BIT)
68 .addAny(YangStmtMapping.ENUM)
69 .addOptional(YangStmtMapping.FRACTION_DIGITS)
70 .addOptional(YangStmtMapping.LENGTH)
71 .addOptional(YangStmtMapping.PATH)
72 .addAny(YangStmtMapping.PATTERN)
73 .addOptional(YangStmtMapping.RANGE)
74 .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
75 .addAny(YangStmtMapping.TYPE)
78 protected TypeStatementImpl(final StmtContext<String, TypeStatement, ?> context) {
82 public static class Definition
84 AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
87 super(YangStmtMapping.TYPE);
91 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
96 public TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
97 return BuiltinTypeStatement.maybeReplace(new TypeStatementImpl(ctx));
101 public TypeEffectiveStatement<TypeStatement> createEffective(
102 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
104 // First look up the proper base type
105 final TypeEffectiveStatement<TypeStatement> typeStmt;
106 switch (ctx.getStatementArgument()) {
107 case TypeUtils.BINARY:
108 typeStmt = BuiltinEffectiveStatements.BINARY;
110 case TypeUtils.BOOLEAN:
111 typeStmt = BuiltinEffectiveStatements.BOOLEAN;
113 case TypeUtils.EMPTY:
114 typeStmt = BuiltinEffectiveStatements.EMPTY;
116 case TypeUtils.INSTANCE_IDENTIFIER:
117 typeStmt = BuiltinEffectiveStatements.INSTANCE_IDENTIFIER;
120 typeStmt = BuiltinEffectiveStatements.INT8;
122 case TypeUtils.INT16:
123 typeStmt = BuiltinEffectiveStatements.INT16;
125 case TypeUtils.INT32:
126 typeStmt = BuiltinEffectiveStatements.INT32;
128 case TypeUtils.INT64:
129 typeStmt = BuiltinEffectiveStatements.INT64;
131 case TypeUtils.STRING:
132 typeStmt = BuiltinEffectiveStatements.STRING;
134 case TypeUtils.UINT8:
135 typeStmt = BuiltinEffectiveStatements.UINT8;
137 case TypeUtils.UINT16:
138 typeStmt = BuiltinEffectiveStatements.UINT16;
140 case TypeUtils.UINT32:
141 typeStmt = BuiltinEffectiveStatements.UINT32;
143 case TypeUtils.UINT64:
144 typeStmt = BuiltinEffectiveStatements.UINT64;
147 final QName qname = Utils.qNameFromArgument(ctx, ctx.getStatementArgument());
148 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
149 ctx.getFromNamespace(TypeNamespace.class, qname);
150 SourceException.throwIfNull(typedef, ctx.getStatementSourceReference(), "Type '%s' not found", qname);
152 final TypedefEffectiveStatement effectiveTypedef = typedef.buildEffective();
153 Verify.verify(effectiveTypedef instanceof TypeDefEffectiveStatementImpl);
154 typeStmt = ((TypeDefEffectiveStatementImpl) effectiveTypedef).asTypeEffectiveStatement();
157 if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
161 // Now instantiate the proper effective statement for that type
162 final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
163 if (baseType instanceof BinaryTypeDefinition) {
164 return new BinaryTypeEffectiveStatementImpl(ctx, (BinaryTypeDefinition) baseType);
165 } else if (baseType instanceof BitsTypeDefinition) {
166 return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType);
167 } else if (baseType instanceof BooleanTypeDefinition) {
168 return new BooleanTypeEffectiveStatementImpl(ctx, (BooleanTypeDefinition) baseType);
169 } else if (baseType instanceof DecimalTypeDefinition) {
170 return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType);
171 } else if (baseType instanceof EmptyTypeDefinition) {
172 return new EmptyTypeEffectiveStatementImpl(ctx, (EmptyTypeDefinition) baseType);
173 } else if (baseType instanceof EnumTypeDefinition) {
174 return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType);
175 } else if (baseType instanceof IdentityrefTypeDefinition) {
176 return new IdentityrefTypeEffectiveStatementImpl(ctx, (IdentityrefTypeDefinition) baseType);
177 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
178 return new InstanceIdentifierTypeEffectiveStatementImpl(ctx, (InstanceIdentifierTypeDefinition) baseType);
179 } else if (baseType instanceof IntegerTypeDefinition) {
180 return new IntegerTypeEffectiveStatementImpl(ctx, (IntegerTypeDefinition) baseType);
181 } else if (baseType instanceof LeafrefTypeDefinition) {
182 return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
183 } else if (baseType instanceof StringTypeDefinition) {
184 return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
185 } else if (baseType instanceof UnionTypeDefinition) {
186 return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType);
187 } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
188 return new UnsignedIntegerTypeEffectiveStatementImpl(ctx, (UnsignedIntegerTypeDefinition) baseType);
190 throw new IllegalStateException("Unhandled base type " + baseType);
195 public void onFullDefinitionDeclared(
196 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt){
197 SUBSTATEMENT_VALIDATOR.validate(stmt);
199 // if it is yang built-in type, no prerequisite is needed, so simply return
200 if (TypeUtils.isYangBuiltInTypeString(stmt.getStatementArgument())) {
204 final QName typeQName = Utils.qNameFromArgument(stmt, stmt.getStatementArgument());
205 final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
206 final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
207 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
208 typeAction.mutatesEffectiveCtx(stmt.getParentContext());
211 * If the type does not exist, throw new InferenceException.
212 * Otherwise perform no operation.
214 typeAction.apply(new InferenceAction() {
216 public void apply() {
221 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
222 InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
223 "Type [%s] was not found.", typeQName);
231 public String getName() {