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.EnumEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.LengthEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.PatternEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.RequireInstanceEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.ValueEffectiveStatement;
38 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
41 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
46 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
47 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
48 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
49 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
50 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
51 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
52 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
53 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
54 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
55 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
56 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
57 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
58 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
59 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
60 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
61 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
62 import org.opendaylight.yangtools.yang.model.util.type.InstanceIdentifierTypeBuilder;
63 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
64 import org.opendaylight.yangtools.yang.model.util.type.LengthRestrictedTypeBuilder;
65 import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder;
66 import org.opendaylight.yangtools.yang.model.util.type.RequireInstanceRestrictedTypeBuilder;
67 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
68 import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
69 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
70 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
71 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
72 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
73 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
74 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
75 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
76 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
77 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
78 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
79 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
80 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
81 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
82 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
83 import org.slf4j.Logger;
84 import org.slf4j.LoggerFactory;
86 abstract class AbstractTypeStatementSupport
87 extends BaseStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
88 private static final Logger LOG = LoggerFactory.getLogger(AbstractTypeStatementSupport.class);
90 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
92 .addOptional(YangStmtMapping.BASE)
93 .addAny(YangStmtMapping.BIT)
94 .addAny(YangStmtMapping.ENUM)
95 .addOptional(YangStmtMapping.FRACTION_DIGITS)
96 .addOptional(YangStmtMapping.LENGTH)
97 .addOptional(YangStmtMapping.PATH)
98 .addAny(YangStmtMapping.PATTERN)
99 .addOptional(YangStmtMapping.RANGE)
100 .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
101 .addAny(YangStmtMapping.TYPE)
104 static final String BINARY = "binary";
105 static final String BITS = "bits";
106 static final String BOOLEAN = "boolean";
107 static final String DECIMAL64 = "decimal64";
108 static final String EMPTY = "empty";
109 static final String ENUMERATION = "enumeration";
110 static final String IDENTITY_REF = "identityref";
111 static final String INSTANCE_IDENTIFIER = "instance-identifier";
112 static final String INT8 = "int8";
113 static final String INT16 = "int16";
114 static final String INT32 = "int32";
115 static final String INT64 = "int64";
116 static final String LEAF_REF = "leafref";
117 static final String STRING = "string";
118 static final String UINT8 = "uint8";
119 static final String UINT16 = "uint16";
120 static final String UINT32 = "uint32";
121 static final String UINT64 = "uint64";
122 static final String UNION = "union";
124 private static final ImmutableMap<String, String> BUILT_IN_TYPES = ImmutableMap.<String, String>builder()
127 .put(BOOLEAN, BOOLEAN)
128 .put(DECIMAL64, DECIMAL64)
130 .put(ENUMERATION, ENUMERATION)
131 .put(IDENTITY_REF,IDENTITY_REF)
132 .put(INSTANCE_IDENTIFIER, INSTANCE_IDENTIFIER)
137 .put(LEAF_REF, LEAF_REF)
146 private static final ImmutableMap<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS =
147 ImmutableMap.<String, StatementSupport<?, ?, ?>>builder()
148 .put(BITS, new BitsSpecificationSupport())
149 .put(DECIMAL64, new Decimal64SpecificationSupport())
150 .put(ENUMERATION, new EnumSpecificationSupport())
151 .put(IDENTITY_REF, new IdentityRefSpecificationRFC6020Support())
152 .put(INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationSupport())
153 .put(LEAF_REF, new LeafrefSpecificationRFC6020Support())
154 .put(UNION, new UnionSpecificationSupport())
157 AbstractTypeStatementSupport() {
158 super(YangStmtMapping.TYPE);
162 public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
167 public final void onFullDefinitionDeclared(
168 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
169 super.onFullDefinitionDeclared(stmt);
171 // if it is yang built-in type, no prerequisite is needed, so simply return
172 if (BUILT_IN_TYPES.containsKey(stmt.getStatementArgument())) {
176 final QName typeQName = StmtContextUtils.parseNodeIdentifier(stmt, stmt.getStatementArgument());
177 final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
178 final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
179 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
180 typeAction.mutatesEffectiveCtx(stmt.getParentContext());
183 * If the type does not exist, throw new InferenceException.
184 * Otherwise perform no operation.
186 typeAction.apply(new InferenceAction() {
188 public void apply(final InferenceContext ctx) {
193 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
194 InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
195 "Type [%s] was not found.", typeQName);
201 public final String internArgument(final String rawArgument) {
203 return (found = BUILT_IN_TYPES.get(rawArgument)) != null ? found : rawArgument;
207 public boolean hasArgumentSpecificSupports() {
208 return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
212 public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
213 return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
217 protected final SubstatementValidator getSubstatementValidator() {
218 return SUBSTATEMENT_VALIDATOR;
222 protected final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx,
223 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
224 return new RegularTypeStatement(ctx, substatements);
228 protected final TypeStatement createEmptyDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
229 final TypeStatement builtin;
230 return (builtin = BuiltinTypeStatement.lookup(ctx)) != null ? builtin : new EmptyTypeStatement(ctx);
234 protected final TypeEffectiveStatement<TypeStatement> createEffective(
235 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
236 final TypeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
237 // First look up the proper base type
238 final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(ctx);
239 // Now instantiate the proper effective statement for that type
240 final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
241 if (baseType instanceof BinaryTypeDefinition) {
242 return createBinary(ctx, (BinaryTypeDefinition) baseType, declared, substatements);
243 } else if (baseType instanceof BitsTypeDefinition) {
244 return createBits(ctx, (BitsTypeDefinition) baseType, declared, substatements);
245 } else if (baseType instanceof BooleanTypeDefinition) {
246 return createBoolean(ctx, (BooleanTypeDefinition) baseType, declared, substatements);
247 } else if (baseType instanceof DecimalTypeDefinition) {
248 return createDecimal(ctx, (DecimalTypeDefinition) baseType, declared, substatements);
249 } else if (baseType instanceof EmptyTypeDefinition) {
250 return createEmpty(ctx, (EmptyTypeDefinition) baseType, declared, substatements);
251 } else if (baseType instanceof EnumTypeDefinition) {
252 return createEnum(ctx, (EnumTypeDefinition) baseType, declared, substatements);
253 } else if (baseType instanceof IdentityrefTypeDefinition) {
254 return createIdentityref(ctx, (IdentityrefTypeDefinition) baseType, declared, substatements);
255 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
256 return createInstanceIdentifier(ctx, (InstanceIdentifierTypeDefinition) baseType, declared, substatements);
257 } else if (baseType instanceof Int8TypeDefinition) {
258 return new IntegralTypeEffectiveStatementImpl<>(ctx,
259 RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
260 } else if (baseType instanceof Int16TypeDefinition) {
261 return new IntegralTypeEffectiveStatementImpl<>(ctx,
262 RestrictedTypes.newInt16Builder((Int16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
263 } else if (baseType instanceof Int32TypeDefinition) {
264 return new IntegralTypeEffectiveStatementImpl<>(ctx,
265 RestrictedTypes.newInt32Builder((Int32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
266 } else if (baseType instanceof Int64TypeDefinition) {
267 return new IntegralTypeEffectiveStatementImpl<>(ctx,
268 RestrictedTypes.newInt64Builder((Int64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
269 } else if (baseType instanceof LeafrefTypeDefinition) {
270 return createLeafref(ctx, (LeafrefTypeDefinition) baseType, declared, substatements);
271 } else if (baseType instanceof StringTypeDefinition) {
272 return createString(ctx, (StringTypeDefinition) baseType, declared, substatements);
273 } else if (baseType instanceof Uint8TypeDefinition) {
274 return new IntegralTypeEffectiveStatementImpl<>(ctx,
275 RestrictedTypes.newUint8Builder((Uint8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
276 } else if (baseType instanceof Uint16TypeDefinition) {
277 return new IntegralTypeEffectiveStatementImpl<>(ctx,
278 RestrictedTypes.newUint16Builder((Uint16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
279 } else if (baseType instanceof Uint32TypeDefinition) {
280 return new IntegralTypeEffectiveStatementImpl<>(ctx,
281 RestrictedTypes.newUint32Builder((Uint32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
282 } else if (baseType instanceof Uint64TypeDefinition) {
283 return new IntegralTypeEffectiveStatementImpl<>(ctx,
284 RestrictedTypes.newUint64Builder((Uint64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
285 } else if (baseType instanceof UnionTypeDefinition) {
286 return createUnion(ctx, (UnionTypeDefinition) baseType, declared, substatements);
288 throw new IllegalStateException("Unhandled base type " + baseType);
293 protected final EffectiveStatement<String, TypeStatement> createEmptyEffective(
294 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
295 final TypeStatement declared) {
296 return resolveType(ctx);
299 static final SchemaPath typeEffectiveSchemaPath(final StmtContext<?, ?, ?> stmtCtx) {
300 final SchemaPath path = stmtCtx.getSchemaPath().get();
301 final SchemaPath parent = path.getParent();
302 final QName parentQName = parent.getLastComponent();
303 checkArgument(parentQName != null, "Path %s has an empty parent", path);
305 final QName qname = path.getLastComponent().bindTo(parentQName.getModule()).intern();
306 return parent.createChild(qname);
310 * Resolve type reference, as pointed to by the context's argument.
312 * @param ctx Statement context
313 * @return Resolved type
314 * @throws SourceException if the target type cannot be found
316 private static @NonNull TypeEffectiveStatement<TypeStatement> resolveType(final StmtContext<String, ?, ?> ctx) {
317 final String argument = ctx.coerceStatementArgument();
320 return BuiltinEffectiveStatement.BINARY;
322 return BuiltinEffectiveStatement.BOOLEAN;
324 return BuiltinEffectiveStatement.EMPTY;
325 case INSTANCE_IDENTIFIER:
326 return BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
328 return BuiltinEffectiveStatement.INT8;
330 return BuiltinEffectiveStatement.INT16;
332 return BuiltinEffectiveStatement.INT32;
334 return BuiltinEffectiveStatement.INT64;
336 return BuiltinEffectiveStatement.STRING;
338 return BuiltinEffectiveStatement.UINT8;
340 return BuiltinEffectiveStatement.UINT16;
342 return BuiltinEffectiveStatement.UINT32;
344 return BuiltinEffectiveStatement.UINT64;
346 final QName qname = StmtContextUtils.parseNodeIdentifier(ctx, argument);
347 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
348 SourceException.throwIfNull(ctx.getFromNamespace(TypeNamespace.class, qname),
349 ctx.getStatementSourceReference(), "Type '%s' not found", qname);
350 return typedef.buildEffective().asTypeEffectiveStatement();
354 private static @NonNull TypeEffectiveStatement<TypeStatement> createBinary(final StmtContext<?, ?, ?> ctx,
355 final BinaryTypeDefinition baseType, final TypeStatement declared,
356 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
357 final LengthRestrictedTypeBuilder<BinaryTypeDefinition> builder =
358 RestrictedTypes.newBinaryBuilder(baseType, typeEffectiveSchemaPath(ctx));
360 for (EffectiveStatement<?, ?> stmt : substatements) {
361 if (stmt instanceof LengthEffectiveStatement) {
362 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
365 builder.setLengthConstraint(length, length.argument());
366 } catch (IllegalStateException e) {
367 throw new SourceException(ctx.getStatementSourceReference(), e,
368 "Multiple length constraints encountered");
369 } catch (InvalidLengthConstraintException e) {
370 throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid length constraint %s",
376 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
379 private static @NonNull TypeEffectiveStatement<TypeStatement> createBits(final StmtContext<?, ?, ?> ctx,
380 final BitsTypeDefinition baseType, final TypeStatement declared,
381 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
382 final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath().get());
384 final YangVersion yangVersion = ctx.getRootVersion();
385 for (final EffectiveStatement<?, ?> stmt : substatements) {
386 if (stmt instanceof BitEffectiveStatement) {
387 SourceException.throwIf(yangVersion != YangVersion.VERSION_1_1, ctx.getStatementSourceReference(),
388 "Restricted bits type is allowed only in YANG 1.1 version.");
389 final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) stmt;
391 // FIXME: this looks like a duplicate of BitsSpecificationEffectiveStatement
392 final Optional<Uint32> declaredPosition = bitSubStmt.getDeclaredPosition();
393 final Uint32 effectivePos;
394 if (declaredPosition.isEmpty()) {
395 effectivePos = getBaseTypeBitPosition(bitSubStmt.argument(), baseType, ctx);
397 effectivePos = declaredPosition.get();
400 builder.addBit(EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos));
404 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
407 private static @NonNull TypeEffectiveStatement<TypeStatement> createBoolean(final StmtContext<?, ?, ?> ctx,
408 final BooleanTypeDefinition baseType, final TypeStatement declared,
409 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
410 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newBooleanBuilder(baseType,
411 typeEffectiveSchemaPath(ctx)));
414 private static @NonNull TypeEffectiveStatement<TypeStatement> createDecimal(final StmtContext<?, ?, ?> ctx,
415 final DecimalTypeDefinition baseType, final TypeStatement declared,
416 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
417 final RangeRestrictedTypeBuilder<DecimalTypeDefinition, BigDecimal> builder =
418 RestrictedTypes.newDecima64Builder(baseType, typeEffectiveSchemaPath(ctx));
420 for (EffectiveStatement<?, ?> stmt : substatements) {
421 if (stmt instanceof RangeEffectiveStatement) {
422 final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
423 builder.setRangeConstraint(range, range.argument());
425 if (stmt instanceof FractionDigitsEffectiveStatement) {
426 final Integer digits = ((FractionDigitsEffectiveStatement)stmt).argument();
427 SourceException.throwIf(baseType.getFractionDigits() != digits, ctx.getStatementSourceReference(),
428 "Cannot override fraction-digits from base type %s to %s", baseType, digits);
432 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
435 private static @NonNull TypeEffectiveStatement<TypeStatement> createEmpty(final StmtContext<?, ?, ?> ctx,
436 final EmptyTypeDefinition baseType, final TypeStatement declared,
437 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
438 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newEmptyBuilder(baseType,
439 typeEffectiveSchemaPath(ctx)));
442 private static @NonNull TypeEffectiveStatement<TypeStatement> createEnum(final StmtContext<?, ?, ?> ctx,
443 final EnumTypeDefinition baseType, final TypeStatement declared,
444 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
445 final EnumerationTypeBuilder builder = RestrictedTypes.newEnumerationBuilder(baseType,
446 ctx.getSchemaPath().get());
448 final YangVersion yangVersion = ctx.getRootVersion();
449 for (final EffectiveStatement<?, ?> stmt : substatements) {
450 if (stmt instanceof EnumEffectiveStatement) {
451 SourceException.throwIf(yangVersion != YangVersion.VERSION_1_1, ctx.getStatementSourceReference(),
452 "Restricted enumeration type is allowed only in YANG 1.1 version.");
454 final EnumEffectiveStatement enumSubStmt = (EnumEffectiveStatement) stmt;
455 final Optional<Integer> declaredValue =
456 enumSubStmt.findFirstEffectiveSubstatementArgument(ValueEffectiveStatement.class);
457 final int effectiveValue;
458 if (declaredValue.isEmpty()) {
459 effectiveValue = getBaseTypeEnumValue(enumSubStmt.getDeclared().rawArgument(), baseType, ctx);
461 effectiveValue = declaredValue.orElseThrow();
464 builder.addEnum(EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue));
468 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
471 private static @NonNull TypeEffectiveStatement<TypeStatement> createIdentityref(final StmtContext<?, ?, ?> ctx,
472 final IdentityrefTypeDefinition baseType, final TypeStatement declared,
473 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
474 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newIdentityrefBuilder(baseType,
475 typeEffectiveSchemaPath(ctx)));
478 private static @NonNull TypeEffectiveStatement<TypeStatement> createInstanceIdentifier(
479 final StmtContext<?, ?, ?> ctx, final InstanceIdentifierTypeDefinition baseType,
480 final TypeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
481 final InstanceIdentifierTypeBuilder builder = RestrictedTypes.newInstanceIdentifierBuilder(baseType,
482 typeEffectiveSchemaPath(ctx));
484 for (EffectiveStatement<?, ?> stmt : substatements) {
485 if (stmt instanceof RequireInstanceEffectiveStatement) {
486 builder.setRequireInstance(((RequireInstanceEffectiveStatement)stmt).argument());
490 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
493 private static @NonNull TypeEffectiveStatement<TypeStatement> createLeafref(final StmtContext<?, ?, ?> ctx,
494 final LeafrefTypeDefinition baseType, final TypeStatement declared,
495 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
496 final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> builder =
497 RestrictedTypes.newLeafrefBuilder(baseType, AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
499 for (final EffectiveStatement<?, ?> stmt : substatements) {
500 if (stmt instanceof RequireInstanceEffectiveStatement) {
501 builder.setRequireInstance(((RequireInstanceEffectiveStatement) stmt).argument());
504 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
507 private static @NonNull TypeEffectiveStatement<TypeStatement> createString(final StmtContext<?, ?, ?> ctx,
508 final StringTypeDefinition baseType, final TypeStatement declared,
509 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
510 final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType,
511 AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
513 for (EffectiveStatement<?, ?> stmt : substatements) {
514 if (stmt instanceof LengthEffectiveStatement) {
515 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
518 builder.setLengthConstraint(length, length.argument());
519 } catch (IllegalStateException e) {
520 throw new SourceException(ctx.getStatementSourceReference(), e,
521 "Multiple length constraints encountered");
522 } catch (InvalidLengthConstraintException e) {
523 throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid length constraint %s",
527 if (stmt instanceof PatternEffectiveStatement) {
528 final PatternConstraint pattern = ((PatternEffectiveStatement)stmt).argument();
529 if (pattern != null) {
530 builder.addPatternConstraint(pattern);
532 LOG.debug("Ignoring empty pattern statement {}", stmt);
537 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
540 private static @NonNull TypeEffectiveStatement<TypeStatement> createUnion(final StmtContext<?, ?, ?> ctx,
541 final UnionTypeDefinition baseType, final TypeStatement declared,
542 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
543 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newUnionBuilder(baseType,
544 typeEffectiveSchemaPath(ctx)));
547 private static Uint32 getBaseTypeBitPosition(final String bitName, final BitsTypeDefinition baseType,
548 final StmtContext<?, ?, ?> ctx) {
549 for (Bit baseTypeBit : baseType.getBits()) {
550 if (bitName.equals(baseTypeBit.getName())) {
551 return baseTypeBit.getPosition();
555 throw new SourceException(ctx.getStatementSourceReference(),
556 "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());
559 private static int getBaseTypeEnumValue(final String enumName, final EnumTypeDefinition baseType,
560 final StmtContext<?, ?, ?> ctx) {
561 for (EnumPair baseTypeEnumPair : baseType.getValues()) {
562 if (enumName.equals(baseTypeEnumPair.getName())) {
563 return baseTypeEnumPair.getValue();
567 throw new SourceException(ctx.getStatementSourceReference(),
568 "Enum '%s' is not a subset of its base enumeration type %s.", enumName, baseType.getQName());