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.IntegralTypeEffectiveStatementImpl;
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;
72 public class TypeStatementImpl extends AbstractDeclaredStatement<String>
73 implements TypeStatement {
74 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
76 .addOptional(YangStmtMapping.BASE)
77 .addAny(YangStmtMapping.BIT)
78 .addAny(YangStmtMapping.ENUM)
79 .addOptional(YangStmtMapping.FRACTION_DIGITS)
80 .addOptional(YangStmtMapping.LENGTH)
81 .addOptional(YangStmtMapping.PATH)
82 .addAny(YangStmtMapping.PATTERN)
83 .addOptional(YangStmtMapping.RANGE)
84 .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
85 .addAny(YangStmtMapping.TYPE)
88 protected TypeStatementImpl(final StmtContext<String, TypeStatement, ?> context) {
92 public static class Definition
93 extends AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
95 private static final Map<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS =
96 ImmutableMap.<String, StatementSupport<?, ?, ?>>builder()
97 .put(TypeUtils.DECIMAL64, new Decimal64SpecificationImpl.Definition())
98 .put(TypeUtils.UNION, new UnionSpecificationImpl.Definition())
99 .put(TypeUtils.ENUMERATION, new EnumSpecificationImpl.Definition())
100 .put(TypeUtils.LEAF_REF, new LeafrefSpecificationImpl.Definition())
101 .put(TypeUtils.BITS, new BitsSpecificationImpl.Definition())
102 .put(TypeUtils.IDENTITY_REF, new IdentityRefSpecificationImpl.Definition())
103 .put(TypeUtils.INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationImpl.Definition())
106 public Definition() {
107 super(YangStmtMapping.TYPE);
111 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
116 public TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
117 return BuiltinTypeStatement.maybeReplace(new TypeStatementImpl(ctx));
121 public TypeEffectiveStatement<TypeStatement> createEffective(
122 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
124 // First look up the proper base type
125 final TypeEffectiveStatement<TypeStatement> typeStmt;
126 switch (ctx.getStatementArgument()) {
127 case TypeUtils.BINARY:
128 typeStmt = BuiltinEffectiveStatement.BINARY;
130 case TypeUtils.BOOLEAN:
131 typeStmt = BuiltinEffectiveStatement.BOOLEAN;
133 case TypeUtils.EMPTY:
134 typeStmt = BuiltinEffectiveStatement.EMPTY;
136 case TypeUtils.INSTANCE_IDENTIFIER:
137 typeStmt = BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
140 typeStmt = BuiltinEffectiveStatement.INT8;
142 case TypeUtils.INT16:
143 typeStmt = BuiltinEffectiveStatement.INT16;
145 case TypeUtils.INT32:
146 typeStmt = BuiltinEffectiveStatement.INT32;
148 case TypeUtils.INT64:
149 typeStmt = BuiltinEffectiveStatement.INT64;
151 case TypeUtils.STRING:
152 typeStmt = BuiltinEffectiveStatement.STRING;
154 case TypeUtils.UINT8:
155 typeStmt = BuiltinEffectiveStatement.UINT8;
157 case TypeUtils.UINT16:
158 typeStmt = BuiltinEffectiveStatement.UINT16;
160 case TypeUtils.UINT32:
161 typeStmt = BuiltinEffectiveStatement.UINT32;
163 case TypeUtils.UINT64:
164 typeStmt = BuiltinEffectiveStatement.UINT64;
167 final QName qname = StmtContextUtils.qnameFromArgument(ctx, ctx.getStatementArgument());
168 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
169 SourceException.throwIfNull(ctx.getFromNamespace(TypeNamespace.class, qname),
170 ctx.getStatementSourceReference(), "Type '%s' not found", qname);
172 final TypedefEffectiveStatement effectiveTypedef = typedef.buildEffective();
173 Verify.verify(effectiveTypedef instanceof TypeDefEffectiveStatementImpl);
174 typeStmt = ((TypeDefEffectiveStatementImpl) effectiveTypedef).asTypeEffectiveStatement();
177 if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
181 // Now instantiate the proper effective statement for that type
182 final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
183 if (baseType instanceof BinaryTypeDefinition) {
184 return new BinaryTypeEffectiveStatementImpl(ctx, (BinaryTypeDefinition) baseType);
185 } else if (baseType instanceof BitsTypeDefinition) {
186 return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType);
187 } else if (baseType instanceof BooleanTypeDefinition) {
188 return new BooleanTypeEffectiveStatementImpl(ctx, (BooleanTypeDefinition) baseType);
189 } else if (baseType instanceof DecimalTypeDefinition) {
190 return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType);
191 } else if (baseType instanceof EmptyTypeDefinition) {
192 return new EmptyTypeEffectiveStatementImpl(ctx, (EmptyTypeDefinition) baseType);
193 } else if (baseType instanceof EnumTypeDefinition) {
194 return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType);
195 } else if (baseType instanceof IdentityrefTypeDefinition) {
196 return new IdentityrefTypeEffectiveStatementImpl(ctx, (IdentityrefTypeDefinition) baseType);
197 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
198 return new InstanceIdentifierTypeEffectiveStatementImpl(ctx,
199 (InstanceIdentifierTypeDefinition) baseType);
200 } else if (baseType instanceof Int8TypeDefinition) {
201 return IntegralTypeEffectiveStatementImpl.create(ctx, (Int8TypeDefinition) baseType);
202 } else if (baseType instanceof Int16TypeDefinition) {
203 return IntegralTypeEffectiveStatementImpl.create(ctx, (Int16TypeDefinition) baseType);
204 } else if (baseType instanceof Int32TypeDefinition) {
205 return IntegralTypeEffectiveStatementImpl.create(ctx, (Int32TypeDefinition) baseType);
206 } else if (baseType instanceof Int64TypeDefinition) {
207 return IntegralTypeEffectiveStatementImpl.create(ctx, (Int64TypeDefinition) baseType);
208 } else if (baseType instanceof LeafrefTypeDefinition) {
209 return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
210 } else if (baseType instanceof StringTypeDefinition) {
211 return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
212 } else if (baseType instanceof Uint8TypeDefinition) {
213 return IntegralTypeEffectiveStatementImpl.create(ctx, (Uint8TypeDefinition) baseType);
214 } else if (baseType instanceof Uint16TypeDefinition) {
215 return IntegralTypeEffectiveStatementImpl.create(ctx, (Uint16TypeDefinition) baseType);
216 } else if (baseType instanceof Uint32TypeDefinition) {
217 return IntegralTypeEffectiveStatementImpl.create(ctx, (Uint32TypeDefinition) baseType);
218 } else if (baseType instanceof Uint64TypeDefinition) {
219 return IntegralTypeEffectiveStatementImpl.create(ctx, (Uint64TypeDefinition) baseType);
220 } else if (baseType instanceof UnionTypeDefinition) {
221 return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType);
223 throw new IllegalStateException("Unhandled base type " + baseType);
228 public void onFullDefinitionDeclared(
229 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
230 super.onFullDefinitionDeclared(stmt);
232 // if it is yang built-in type, no prerequisite is needed, so simply return
233 if (TypeUtils.isYangBuiltInTypeString(stmt.getStatementArgument())) {
237 final QName typeQName = StmtContextUtils.qnameFromArgument(stmt, stmt.getStatementArgument());
238 final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
239 final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
240 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
241 typeAction.mutatesEffectiveCtx(stmt.getParentContext());
244 * If the type does not exist, throw new InferenceException.
245 * Otherwise perform no operation.
247 typeAction.apply(new InferenceAction() {
249 public void apply(final InferenceContext ctx) {
254 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
255 InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
256 "Type [%s] was not found.", typeQName);
262 protected SubstatementValidator getSubstatementValidator() {
263 return SUBSTATEMENT_VALIDATOR;
267 public boolean hasArgumentSpecificSupports() {
268 return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
272 public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
273 return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
277 public String internArgument(final String rawArgument) {
278 final String found = TypeUtils.findBuiltinString(rawArgument);
279 return found != null ? found : rawArgument;
285 public String getName() {