2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.rfc7950.stmt.type;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import java.util.Collection;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
20 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
26 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
45 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
46 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
47 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
52 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
53 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
54 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
55 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
56 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
57 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
58 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
59 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
61 abstract class AbstractTypeStatementSupport
62 extends BaseStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
63 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
65 .addOptional(YangStmtMapping.BASE)
66 .addAny(YangStmtMapping.BIT)
67 .addAny(YangStmtMapping.ENUM)
68 .addOptional(YangStmtMapping.FRACTION_DIGITS)
69 .addOptional(YangStmtMapping.LENGTH)
70 .addOptional(YangStmtMapping.PATH)
71 .addAny(YangStmtMapping.PATTERN)
72 .addOptional(YangStmtMapping.RANGE)
73 .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
74 .addAny(YangStmtMapping.TYPE)
77 static final String BINARY = "binary";
78 static final String BITS = "bits";
79 static final String BOOLEAN = "boolean";
80 static final String DECIMAL64 = "decimal64";
81 static final String EMPTY = "empty";
82 static final String ENUMERATION = "enumeration";
83 static final String IDENTITY_REF = "identityref";
84 static final String INSTANCE_IDENTIFIER = "instance-identifier";
85 static final String INT8 = "int8";
86 static final String INT16 = "int16";
87 static final String INT32 = "int32";
88 static final String INT64 = "int64";
89 static final String LEAF_REF = "leafref";
90 static final String STRING = "string";
91 static final String UINT8 = "uint8";
92 static final String UINT16 = "uint16";
93 static final String UINT32 = "uint32";
94 static final String UINT64 = "uint64";
95 static final String UNION = "union";
97 private static final ImmutableMap<String, String> BUILT_IN_TYPES = ImmutableMap.<String, String>builder()
100 .put(BOOLEAN, BOOLEAN)
101 .put(DECIMAL64, DECIMAL64)
103 .put(ENUMERATION, ENUMERATION)
104 .put(IDENTITY_REF,IDENTITY_REF)
105 .put(INSTANCE_IDENTIFIER, INSTANCE_IDENTIFIER)
110 .put(LEAF_REF, LEAF_REF)
119 private static final ImmutableMap<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS =
120 ImmutableMap.<String, StatementSupport<?, ?, ?>>builder()
121 .put(BITS, new BitsSpecificationSupport())
122 .put(DECIMAL64, new Decimal64SpecificationSupport())
123 .put(ENUMERATION, new EnumSpecificationSupport())
124 .put(IDENTITY_REF, new IdentityRefSpecificationRFC6020Support())
125 .put(INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationSupport())
126 .put(LEAF_REF, new LeafrefSpecificationRFC6020Support())
127 .put(UNION, new UnionSpecificationSupport())
130 AbstractTypeStatementSupport() {
131 super(YangStmtMapping.TYPE);
135 public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
140 public final void onFullDefinitionDeclared(
141 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
142 super.onFullDefinitionDeclared(stmt);
144 // if it is yang built-in type, no prerequisite is needed, so simply return
145 if (BUILT_IN_TYPES.containsKey(stmt.getStatementArgument())) {
149 final QName typeQName = StmtContextUtils.parseNodeIdentifier(stmt, stmt.getStatementArgument());
150 final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
151 final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
152 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
153 typeAction.mutatesEffectiveCtx(stmt.getParentContext());
156 * If the type does not exist, throw new InferenceException.
157 * Otherwise perform no operation.
159 typeAction.apply(new InferenceAction() {
161 public void apply(final InferenceContext ctx) {
166 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
167 InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
168 "Type [%s] was not found.", typeQName);
174 public final String internArgument(final String rawArgument) {
176 return (found = BUILT_IN_TYPES.get(rawArgument)) != null ? found : rawArgument;
180 public boolean hasArgumentSpecificSupports() {
181 return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
185 public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
186 return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
190 protected final SubstatementValidator getSubstatementValidator() {
191 return SUBSTATEMENT_VALIDATOR;
195 protected final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx,
196 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
197 return new RegularTypeStatement(ctx, substatements);
201 protected final TypeStatement createEmptyDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
202 final TypeStatement builtin;
203 return (builtin = BuiltinTypeStatement.lookup(ctx)) != null ? builtin : new EmptyTypeStatement(ctx);
207 protected final EffectiveStatement<String, TypeStatement> createEffective(
208 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
209 final TypeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
210 // First look up the proper base type
211 final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(ctx);
212 // Now instantiate the proper effective statement for that type
213 final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
214 if (baseType instanceof BinaryTypeDefinition) {
215 return new BinaryTypeEffectiveStatementImpl(ctx, (BinaryTypeDefinition) baseType);
216 } else if (baseType instanceof BitsTypeDefinition) {
217 return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType);
218 } else if (baseType instanceof BooleanTypeDefinition) {
219 return new BooleanTypeEffectiveStatementImpl(ctx, (BooleanTypeDefinition) baseType);
220 } else if (baseType instanceof DecimalTypeDefinition) {
221 return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType);
222 } else if (baseType instanceof EmptyTypeDefinition) {
223 return new EmptyTypeEffectiveStatementImpl(ctx, (EmptyTypeDefinition) baseType);
224 } else if (baseType instanceof EnumTypeDefinition) {
225 return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType);
226 } else if (baseType instanceof IdentityrefTypeDefinition) {
227 return new IdentityrefTypeEffectiveStatementImpl(ctx, (IdentityrefTypeDefinition) baseType);
228 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
229 return new InstanceIdentifierTypeEffectiveStatementImpl(ctx,
230 (InstanceIdentifierTypeDefinition) baseType);
231 } else if (baseType instanceof Int8TypeDefinition) {
232 return new IntegralTypeEffectiveStatementImpl<>(ctx,
233 RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
234 } else if (baseType instanceof Int16TypeDefinition) {
235 return new IntegralTypeEffectiveStatementImpl<>(ctx,
236 RestrictedTypes.newInt16Builder((Int16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
237 } else if (baseType instanceof Int32TypeDefinition) {
238 return new IntegralTypeEffectiveStatementImpl<>(ctx,
239 RestrictedTypes.newInt32Builder((Int32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
240 } else if (baseType instanceof Int64TypeDefinition) {
241 return new IntegralTypeEffectiveStatementImpl<>(ctx,
242 RestrictedTypes.newInt64Builder((Int64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
243 } else if (baseType instanceof LeafrefTypeDefinition) {
244 return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
245 } else if (baseType instanceof StringTypeDefinition) {
246 return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
247 } else if (baseType instanceof Uint8TypeDefinition) {
248 return new IntegralTypeEffectiveStatementImpl<>(ctx,
249 RestrictedTypes.newUint8Builder((Uint8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
250 } else if (baseType instanceof Uint16TypeDefinition) {
251 return new IntegralTypeEffectiveStatementImpl<>(ctx,
252 RestrictedTypes.newUint16Builder((Uint16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
253 } else if (baseType instanceof Uint32TypeDefinition) {
254 return new IntegralTypeEffectiveStatementImpl<>(ctx,
255 RestrictedTypes.newUint32Builder((Uint32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
256 } else if (baseType instanceof Uint64TypeDefinition) {
257 return new IntegralTypeEffectiveStatementImpl<>(ctx,
258 RestrictedTypes.newUint64Builder((Uint64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
259 } else if (baseType instanceof UnionTypeDefinition) {
260 return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType);
262 throw new IllegalStateException("Unhandled base type " + baseType);
267 protected final EffectiveStatement<String, TypeStatement> createEmptyEffective(
268 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
269 final TypeStatement declared) {
270 return resolveType(ctx);
273 static final SchemaPath typeEffectiveSchemaPath(final StmtContext<?, ?, ?> stmtCtx) {
274 final SchemaPath path = stmtCtx.getSchemaPath().get();
275 final SchemaPath parent = path.getParent();
276 final QName parentQName = parent.getLastComponent();
277 checkArgument(parentQName != null, "Path %s has an empty parent", path);
279 final QName qname = path.getLastComponent().bindTo(parentQName.getModule()).intern();
280 return parent.createChild(qname);
284 * Resolve type reference, as pointed to by the context's argument.
286 * @param ctx Statement context
287 * @return Resolved type
288 * @throws SourceException if the target type cannot be found
290 private static @NonNull TypeEffectiveStatement<TypeStatement> resolveType(final StmtContext<String, ?, ?> ctx) {
291 final String argument = ctx.coerceStatementArgument();
294 return BuiltinEffectiveStatement.BINARY;
296 return BuiltinEffectiveStatement.BOOLEAN;
298 return BuiltinEffectiveStatement.EMPTY;
299 case INSTANCE_IDENTIFIER:
300 return BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
302 return BuiltinEffectiveStatement.INT8;
304 return BuiltinEffectiveStatement.INT16;
306 return BuiltinEffectiveStatement.INT32;
308 return BuiltinEffectiveStatement.INT64;
310 return BuiltinEffectiveStatement.STRING;
312 return BuiltinEffectiveStatement.UINT8;
314 return BuiltinEffectiveStatement.UINT16;
316 return BuiltinEffectiveStatement.UINT32;
318 return BuiltinEffectiveStatement.UINT64;
320 final QName qname = StmtContextUtils.parseNodeIdentifier(ctx, argument);
321 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
322 SourceException.throwIfNull(ctx.getFromNamespace(TypeNamespace.class, qname),
323 ctx.getStatementSourceReference(), "Type '%s' not found", qname);
324 return typedef.buildEffective().asTypeEffectiveStatement();