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 com.google.common.collect.ImmutableMap;
12 import java.util.Collection;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
23 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
42 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
52 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
53 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
54 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
55 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
56 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
57 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.TypeDefEffectiveStatementImpl;
58 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BinaryTypeEffectiveStatementImpl;
59 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BitsTypeEffectiveStatementImpl;
60 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BooleanTypeEffectiveStatementImpl;
61 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BuiltinEffectiveStatement;
62 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.DecimalTypeEffectiveStatementImpl;
63 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EmptyTypeEffectiveStatementImpl;
64 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EnumTypeEffectiveStatementImpl;
65 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IdentityrefTypeEffectiveStatementImpl;
66 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.InstanceIdentifierTypeEffectiveStatementImpl;
67 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IntegerTypeEffectiveStatementImpl;
68 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LeafrefTypeEffectiveStatementImpl;
69 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.StringTypeEffectiveStatementImpl;
70 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnionTypeEffectiveStatementImpl;
71 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnsignedIntegerTypeEffectiveStatementImpl;
73 public class TypeStatementImpl extends AbstractDeclaredStatement<String>
74 implements TypeStatement {
75 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
77 .addOptional(YangStmtMapping.BASE)
78 .addAny(YangStmtMapping.BIT)
79 .addAny(YangStmtMapping.ENUM)
80 .addOptional(YangStmtMapping.FRACTION_DIGITS)
81 .addOptional(YangStmtMapping.LENGTH)
82 .addOptional(YangStmtMapping.PATH)
83 .addAny(YangStmtMapping.PATTERN)
84 .addOptional(YangStmtMapping.RANGE)
85 .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
86 .addAny(YangStmtMapping.TYPE)
89 protected TypeStatementImpl(final StmtContext<String, TypeStatement, ?> context) {
93 public static class Definition
94 extends AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
96 private static final Map<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS =
97 ImmutableMap.<String, StatementSupport<?, ?, ?>>builder()
98 .put(TypeUtils.DECIMAL64, new Decimal64SpecificationImpl.Definition())
99 .put(TypeUtils.UNION, new UnionSpecificationImpl.Definition())
100 .put(TypeUtils.ENUMERATION, new EnumSpecificationImpl.Definition())
101 .put(TypeUtils.LEAF_REF, new LeafrefSpecificationImpl.Definition())
102 .put(TypeUtils.BITS, new BitsSpecificationImpl.Definition())
103 .put(TypeUtils.IDENTITY_REF, new IdentityRefSpecificationImpl.Definition())
104 .put(TypeUtils.INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationImpl.Definition())
107 public Definition() {
108 super(YangStmtMapping.TYPE);
112 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
117 public TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
118 return BuiltinTypeStatement.maybeReplace(new TypeStatementImpl(ctx));
122 public TypeEffectiveStatement<TypeStatement> createEffective(
123 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
125 // First look up the proper base type
126 final TypeEffectiveStatement<TypeStatement> typeStmt;
127 switch (ctx.getStatementArgument()) {
128 case TypeUtils.BINARY:
129 typeStmt = BuiltinEffectiveStatement.BINARY;
131 case TypeUtils.BOOLEAN:
132 typeStmt = BuiltinEffectiveStatement.BOOLEAN;
134 case TypeUtils.EMPTY:
135 typeStmt = BuiltinEffectiveStatement.EMPTY;
137 case TypeUtils.INSTANCE_IDENTIFIER:
138 typeStmt = BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
141 typeStmt = BuiltinEffectiveStatement.INT8;
143 case TypeUtils.INT16:
144 typeStmt = BuiltinEffectiveStatement.INT16;
146 case TypeUtils.INT32:
147 typeStmt = BuiltinEffectiveStatement.INT32;
149 case TypeUtils.INT64:
150 typeStmt = BuiltinEffectiveStatement.INT64;
152 case TypeUtils.STRING:
153 typeStmt = BuiltinEffectiveStatement.STRING;
155 case TypeUtils.UINT8:
156 typeStmt = BuiltinEffectiveStatement.UINT8;
158 case TypeUtils.UINT16:
159 typeStmt = BuiltinEffectiveStatement.UINT16;
161 case TypeUtils.UINT32:
162 typeStmt = BuiltinEffectiveStatement.UINT32;
164 case TypeUtils.UINT64:
165 typeStmt = BuiltinEffectiveStatement.UINT64;
168 final QName qname = StmtContextUtils.qnameFromArgument(ctx, ctx.getStatementArgument());
169 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
170 SourceException.throwIfNull(ctx.getFromNamespace(TypeNamespace.class, qname),
171 ctx.getStatementSourceReference(), "Type '%s' not found", qname);
173 final TypedefEffectiveStatement effectiveTypedef = typedef.buildEffective();
174 Verify.verify(effectiveTypedef instanceof TypeDefEffectiveStatementImpl);
175 typeStmt = ((TypeDefEffectiveStatementImpl) effectiveTypedef).asTypeEffectiveStatement();
178 if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
182 // Now instantiate the proper effective statement for that type
183 final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
184 if (baseType instanceof BinaryTypeDefinition) {
185 return new BinaryTypeEffectiveStatementImpl(ctx, (BinaryTypeDefinition) baseType);
186 } else if (baseType instanceof BitsTypeDefinition) {
187 return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType);
188 } else if (baseType instanceof BooleanTypeDefinition) {
189 return new BooleanTypeEffectiveStatementImpl(ctx, (BooleanTypeDefinition) baseType);
190 } else if (baseType instanceof DecimalTypeDefinition) {
191 return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType);
192 } else if (baseType instanceof EmptyTypeDefinition) {
193 return new EmptyTypeEffectiveStatementImpl(ctx, (EmptyTypeDefinition) baseType);
194 } else if (baseType instanceof EnumTypeDefinition) {
195 return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType);
196 } else if (baseType instanceof IdentityrefTypeDefinition) {
197 return new IdentityrefTypeEffectiveStatementImpl(ctx, (IdentityrefTypeDefinition) baseType);
198 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
199 return new InstanceIdentifierTypeEffectiveStatementImpl(ctx,
200 (InstanceIdentifierTypeDefinition) baseType);
201 } else if (baseType instanceof Int8TypeDefinition) {
202 return IntegerTypeEffectiveStatementImpl.create(ctx, (Int8TypeDefinition) baseType);
203 } else if (baseType instanceof Int16TypeDefinition) {
204 return IntegerTypeEffectiveStatementImpl.create(ctx, (Int16TypeDefinition) baseType);
205 } else if (baseType instanceof Int32TypeDefinition) {
206 return IntegerTypeEffectiveStatementImpl.create(ctx, (Int32TypeDefinition) baseType);
207 } else if (baseType instanceof Int64TypeDefinition) {
208 return IntegerTypeEffectiveStatementImpl.create(ctx, (Int64TypeDefinition) baseType);
209 } else if (baseType instanceof LeafrefTypeDefinition) {
210 return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
211 } else if (baseType instanceof StringTypeDefinition) {
212 return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
213 } else if (baseType instanceof Uint8TypeDefinition) {
214 return UnsignedIntegerTypeEffectiveStatementImpl.create(ctx, (Uint8TypeDefinition) baseType);
215 } else if (baseType instanceof Uint16TypeDefinition) {
216 return UnsignedIntegerTypeEffectiveStatementImpl.create(ctx, (Uint16TypeDefinition) baseType);
217 } else if (baseType instanceof Uint32TypeDefinition) {
218 return UnsignedIntegerTypeEffectiveStatementImpl.create(ctx, (Uint32TypeDefinition) baseType);
219 } else if (baseType instanceof Uint64TypeDefinition) {
220 return UnsignedIntegerTypeEffectiveStatementImpl.create(ctx, (Uint64TypeDefinition) baseType);
221 } else if (baseType instanceof UnionTypeDefinition) {
222 return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType);
224 throw new IllegalStateException("Unhandled base type " + baseType);
229 public void onFullDefinitionDeclared(
230 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
231 super.onFullDefinitionDeclared(stmt);
233 // if it is yang built-in type, no prerequisite is needed, so simply return
234 if (TypeUtils.isYangBuiltInTypeString(stmt.getStatementArgument())) {
238 final QName typeQName = StmtContextUtils.qnameFromArgument(stmt, stmt.getStatementArgument());
239 final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
240 final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
241 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
242 typeAction.mutatesEffectiveCtx(stmt.getParentContext());
245 * If the type does not exist, throw new InferenceException.
246 * Otherwise perform no operation.
248 typeAction.apply(new InferenceAction() {
250 public void apply(final InferenceContext ctx) {
255 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
256 InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
257 "Type [%s] was not found.", typeQName);
263 protected SubstatementValidator getSubstatementValidator() {
264 return SUBSTATEMENT_VALIDATOR;
268 public boolean hasArgumentSpecificSupports() {
269 return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
273 public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
274 return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
278 public String internArgument(final String rawArgument) {
279 final String found = TypeUtils.findBuiltinString(rawArgument);
280 return found != null ? found : rawArgument;
286 public String getName() {