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.math.BigDecimal;
15 import java.util.Collection;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.Uint32;
20 import org.opendaylight.yangtools.yang.common.YangVersion;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
24 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.LengthEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
34 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
37 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
47 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
48 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
49 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
50 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
51 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
52 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
53 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
54 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
55 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
56 import org.opendaylight.yangtools.yang.model.util.type.LengthRestrictedTypeBuilder;
57 import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder;
58 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
59 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
60 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
61 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
62 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
63 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
64 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
65 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
66 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
67 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
68 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
69 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
70 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
71 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
72 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
74 abstract class AbstractTypeStatementSupport
75 extends BaseStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
76 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
78 .addOptional(YangStmtMapping.BASE)
79 .addAny(YangStmtMapping.BIT)
80 .addAny(YangStmtMapping.ENUM)
81 .addOptional(YangStmtMapping.FRACTION_DIGITS)
82 .addOptional(YangStmtMapping.LENGTH)
83 .addOptional(YangStmtMapping.PATH)
84 .addAny(YangStmtMapping.PATTERN)
85 .addOptional(YangStmtMapping.RANGE)
86 .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
87 .addAny(YangStmtMapping.TYPE)
90 static final String BINARY = "binary";
91 static final String BITS = "bits";
92 static final String BOOLEAN = "boolean";
93 static final String DECIMAL64 = "decimal64";
94 static final String EMPTY = "empty";
95 static final String ENUMERATION = "enumeration";
96 static final String IDENTITY_REF = "identityref";
97 static final String INSTANCE_IDENTIFIER = "instance-identifier";
98 static final String INT8 = "int8";
99 static final String INT16 = "int16";
100 static final String INT32 = "int32";
101 static final String INT64 = "int64";
102 static final String LEAF_REF = "leafref";
103 static final String STRING = "string";
104 static final String UINT8 = "uint8";
105 static final String UINT16 = "uint16";
106 static final String UINT32 = "uint32";
107 static final String UINT64 = "uint64";
108 static final String UNION = "union";
110 private static final ImmutableMap<String, String> BUILT_IN_TYPES = ImmutableMap.<String, String>builder()
113 .put(BOOLEAN, BOOLEAN)
114 .put(DECIMAL64, DECIMAL64)
116 .put(ENUMERATION, ENUMERATION)
117 .put(IDENTITY_REF,IDENTITY_REF)
118 .put(INSTANCE_IDENTIFIER, INSTANCE_IDENTIFIER)
123 .put(LEAF_REF, LEAF_REF)
132 private static final ImmutableMap<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS =
133 ImmutableMap.<String, StatementSupport<?, ?, ?>>builder()
134 .put(BITS, new BitsSpecificationSupport())
135 .put(DECIMAL64, new Decimal64SpecificationSupport())
136 .put(ENUMERATION, new EnumSpecificationSupport())
137 .put(IDENTITY_REF, new IdentityRefSpecificationRFC6020Support())
138 .put(INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationSupport())
139 .put(LEAF_REF, new LeafrefSpecificationRFC6020Support())
140 .put(UNION, new UnionSpecificationSupport())
143 AbstractTypeStatementSupport() {
144 super(YangStmtMapping.TYPE);
148 public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
153 public final void onFullDefinitionDeclared(
154 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
155 super.onFullDefinitionDeclared(stmt);
157 // if it is yang built-in type, no prerequisite is needed, so simply return
158 if (BUILT_IN_TYPES.containsKey(stmt.getStatementArgument())) {
162 final QName typeQName = StmtContextUtils.parseNodeIdentifier(stmt, stmt.getStatementArgument());
163 final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
164 final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
165 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
166 typeAction.mutatesEffectiveCtx(stmt.getParentContext());
169 * If the type does not exist, throw new InferenceException.
170 * Otherwise perform no operation.
172 typeAction.apply(new InferenceAction() {
174 public void apply(final InferenceContext ctx) {
179 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
180 InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
181 "Type [%s] was not found.", typeQName);
187 public final String internArgument(final String rawArgument) {
189 return (found = BUILT_IN_TYPES.get(rawArgument)) != null ? found : rawArgument;
193 public boolean hasArgumentSpecificSupports() {
194 return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
198 public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
199 return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
203 protected final SubstatementValidator getSubstatementValidator() {
204 return SUBSTATEMENT_VALIDATOR;
208 protected final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx,
209 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
210 return new RegularTypeStatement(ctx, substatements);
214 protected final TypeStatement createEmptyDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
215 final TypeStatement builtin;
216 return (builtin = BuiltinTypeStatement.lookup(ctx)) != null ? builtin : new EmptyTypeStatement(ctx);
220 protected final TypeEffectiveStatement<TypeStatement> createEffective(
221 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
222 final TypeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
223 // First look up the proper base type
224 final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(ctx);
225 // Now instantiate the proper effective statement for that type
226 final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
227 if (baseType instanceof BinaryTypeDefinition) {
228 return createBinary(ctx, (BinaryTypeDefinition) baseType, declared, substatements);
229 } else if (baseType instanceof BitsTypeDefinition) {
230 return createBits(ctx, (BitsTypeDefinition) baseType, declared, substatements);
231 } else if (baseType instanceof BooleanTypeDefinition) {
232 return createBoolean(ctx, (BooleanTypeDefinition) baseType, declared, substatements);
233 } else if (baseType instanceof DecimalTypeDefinition) {
234 return createDecimal(ctx, (DecimalTypeDefinition) baseType, declared, substatements);
235 } else if (baseType instanceof EmptyTypeDefinition) {
236 return createEmpty(ctx, (EmptyTypeDefinition) baseType, declared, substatements);
237 } else if (baseType instanceof EnumTypeDefinition) {
238 return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType);
239 } else if (baseType instanceof IdentityrefTypeDefinition) {
240 return createIdentityref(ctx, (IdentityrefTypeDefinition) baseType, declared, substatements);
241 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
242 return new InstanceIdentifierTypeEffectiveStatementImpl(ctx,
243 (InstanceIdentifierTypeDefinition) baseType);
244 } else if (baseType instanceof Int8TypeDefinition) {
245 return new IntegralTypeEffectiveStatementImpl<>(ctx,
246 RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
247 } else if (baseType instanceof Int16TypeDefinition) {
248 return new IntegralTypeEffectiveStatementImpl<>(ctx,
249 RestrictedTypes.newInt16Builder((Int16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
250 } else if (baseType instanceof Int32TypeDefinition) {
251 return new IntegralTypeEffectiveStatementImpl<>(ctx,
252 RestrictedTypes.newInt32Builder((Int32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
253 } else if (baseType instanceof Int64TypeDefinition) {
254 return new IntegralTypeEffectiveStatementImpl<>(ctx,
255 RestrictedTypes.newInt64Builder((Int64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
256 } else if (baseType instanceof LeafrefTypeDefinition) {
257 return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
258 } else if (baseType instanceof StringTypeDefinition) {
259 return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
260 } else if (baseType instanceof Uint8TypeDefinition) {
261 return new IntegralTypeEffectiveStatementImpl<>(ctx,
262 RestrictedTypes.newUint8Builder((Uint8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
263 } else if (baseType instanceof Uint16TypeDefinition) {
264 return new IntegralTypeEffectiveStatementImpl<>(ctx,
265 RestrictedTypes.newUint16Builder((Uint16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
266 } else if (baseType instanceof Uint32TypeDefinition) {
267 return new IntegralTypeEffectiveStatementImpl<>(ctx,
268 RestrictedTypes.newUint32Builder((Uint32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
269 } else if (baseType instanceof Uint64TypeDefinition) {
270 return new IntegralTypeEffectiveStatementImpl<>(ctx,
271 RestrictedTypes.newUint64Builder((Uint64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
272 } else if (baseType instanceof UnionTypeDefinition) {
273 return createUnion(ctx, (UnionTypeDefinition) baseType, declared, substatements);
275 throw new IllegalStateException("Unhandled base type " + baseType);
280 protected final EffectiveStatement<String, TypeStatement> createEmptyEffective(
281 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
282 final TypeStatement declared) {
283 return resolveType(ctx);
286 static final SchemaPath typeEffectiveSchemaPath(final StmtContext<?, ?, ?> stmtCtx) {
287 final SchemaPath path = stmtCtx.getSchemaPath().get();
288 final SchemaPath parent = path.getParent();
289 final QName parentQName = parent.getLastComponent();
290 checkArgument(parentQName != null, "Path %s has an empty parent", path);
292 final QName qname = path.getLastComponent().bindTo(parentQName.getModule()).intern();
293 return parent.createChild(qname);
297 * Resolve type reference, as pointed to by the context's argument.
299 * @param ctx Statement context
300 * @return Resolved type
301 * @throws SourceException if the target type cannot be found
303 private static @NonNull TypeEffectiveStatement<TypeStatement> resolveType(final StmtContext<String, ?, ?> ctx) {
304 final String argument = ctx.coerceStatementArgument();
307 return BuiltinEffectiveStatement.BINARY;
309 return BuiltinEffectiveStatement.BOOLEAN;
311 return BuiltinEffectiveStatement.EMPTY;
312 case INSTANCE_IDENTIFIER:
313 return BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
315 return BuiltinEffectiveStatement.INT8;
317 return BuiltinEffectiveStatement.INT16;
319 return BuiltinEffectiveStatement.INT32;
321 return BuiltinEffectiveStatement.INT64;
323 return BuiltinEffectiveStatement.STRING;
325 return BuiltinEffectiveStatement.UINT8;
327 return BuiltinEffectiveStatement.UINT16;
329 return BuiltinEffectiveStatement.UINT32;
331 return BuiltinEffectiveStatement.UINT64;
333 final QName qname = StmtContextUtils.parseNodeIdentifier(ctx, argument);
334 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
335 SourceException.throwIfNull(ctx.getFromNamespace(TypeNamespace.class, qname),
336 ctx.getStatementSourceReference(), "Type '%s' not found", qname);
337 return typedef.buildEffective().asTypeEffectiveStatement();
341 private static @NonNull TypeEffectiveStatement<TypeStatement> createBinary(final StmtContext<?, ?, ?> ctx,
342 final BinaryTypeDefinition baseType, final TypeStatement declared,
343 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
344 final LengthRestrictedTypeBuilder<BinaryTypeDefinition> builder =
345 RestrictedTypes.newBinaryBuilder(baseType, typeEffectiveSchemaPath(ctx));
347 for (EffectiveStatement<?, ?> stmt : substatements) {
348 if (stmt instanceof LengthEffectiveStatement) {
349 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
352 builder.setLengthConstraint(length, length.argument());
353 } catch (IllegalStateException e) {
354 throw new SourceException(ctx.getStatementSourceReference(), e,
355 "Multiple length constraints encountered");
356 } catch (InvalidLengthConstraintException e) {
357 throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid length constraint %s",
363 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
366 private static @NonNull TypeEffectiveStatement<TypeStatement> createBits(final StmtContext<?, ?, ?> ctx,
367 final BitsTypeDefinition baseType, final TypeStatement declared,
368 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
369 final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath().get());
371 final YangVersion yangVersion = ctx.getRootVersion();
372 for (final EffectiveStatement<?, ?> stmt : substatements) {
373 if (stmt instanceof BitEffectiveStatement) {
374 SourceException.throwIf(yangVersion != YangVersion.VERSION_1_1, ctx.getStatementSourceReference(),
375 "Restricted bits type is allowed only in YANG 1.1 version.");
376 final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) stmt;
378 // FIXME: this looks like a duplicate of BitsSpecificationEffectiveStatement
379 final Optional<Uint32> declaredPosition = bitSubStmt.getDeclaredPosition();
380 final Uint32 effectivePos;
381 if (declaredPosition.isEmpty()) {
382 effectivePos = getBaseTypeBitPosition(bitSubStmt.argument(), baseType, ctx);
384 effectivePos = declaredPosition.get();
387 builder.addBit(EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos));
391 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
394 private static @NonNull TypeEffectiveStatement<TypeStatement> createBoolean(final StmtContext<?, ?, ?> ctx,
395 final BooleanTypeDefinition baseType, final TypeStatement declared,
396 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
397 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newBooleanBuilder(baseType,
398 typeEffectiveSchemaPath(ctx)));
401 private static @NonNull TypeEffectiveStatement<TypeStatement> createDecimal(final StmtContext<?, ?, ?> ctx,
402 final DecimalTypeDefinition baseType, final TypeStatement declared,
403 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
404 final RangeRestrictedTypeBuilder<DecimalTypeDefinition, BigDecimal> builder =
405 RestrictedTypes.newDecima64Builder(baseType, typeEffectiveSchemaPath(ctx));
407 for (EffectiveStatement<?, ?> stmt : substatements) {
408 if (stmt instanceof RangeEffectiveStatement) {
409 final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
410 builder.setRangeConstraint(range, range.argument());
412 if (stmt instanceof FractionDigitsEffectiveStatement) {
413 final Integer digits = ((FractionDigitsEffectiveStatement)stmt).argument();
414 SourceException.throwIf(baseType.getFractionDigits() != digits, ctx.getStatementSourceReference(),
415 "Cannot override fraction-digits from base type %s to %s", baseType, digits);
419 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
422 private static @NonNull TypeEffectiveStatement<TypeStatement> createEmpty(final StmtContext<?, ?, ?> ctx,
423 final EmptyTypeDefinition baseType, final TypeStatement declared,
424 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
425 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newEmptyBuilder(baseType,
426 typeEffectiveSchemaPath(ctx)));
429 private static @NonNull TypeEffectiveStatement<TypeStatement> createIdentityref(final StmtContext<?, ?, ?> ctx,
430 final IdentityrefTypeDefinition baseType, final TypeStatement declared,
431 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
432 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newIdentityrefBuilder(baseType,
433 typeEffectiveSchemaPath(ctx)));
436 private static @NonNull TypeEffectiveStatement<TypeStatement> createUnion(final StmtContext<?, ?, ?> ctx,
437 final UnionTypeDefinition baseType, final TypeStatement declared,
438 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
439 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newUnionBuilder(baseType,
440 typeEffectiveSchemaPath(ctx)));
443 private static Uint32 getBaseTypeBitPosition(final String bitName, final BitsTypeDefinition baseType,
444 final StmtContext<?, ?, ?> ctx) {
445 for (Bit baseTypeBit : baseType.getBits()) {
446 if (bitName.equals(baseTypeBit.getName())) {
447 return baseTypeBit.getPosition();
451 throw new SourceException(ctx.getStatementSourceReference(),
452 "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());