f6266f170378f888b57fed11b27509c4500b33f9
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / AbstractTypeStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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.RangeEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.RequireInstanceEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.ValueEffectiveStatement;
37 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
40 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
45 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
47 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
48 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
49 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
50 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
51 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
52 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
53 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
54 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
55 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
56 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
57 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
58 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
59 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
60 import org.opendaylight.yangtools.yang.model.util.type.InstanceIdentifierTypeBuilder;
61 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
62 import org.opendaylight.yangtools.yang.model.util.type.LengthRestrictedTypeBuilder;
63 import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder;
64 import org.opendaylight.yangtools.yang.model.util.type.RequireInstanceRestrictedTypeBuilder;
65 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
66 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
67 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
68 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
69 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
70 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
71 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
72 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
73 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
74 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
75 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
76 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
77 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
78 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
79 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
80
81 abstract class AbstractTypeStatementSupport
82         extends BaseStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
83     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
84         YangStmtMapping.TYPE)
85         .addOptional(YangStmtMapping.BASE)
86         .addAny(YangStmtMapping.BIT)
87         .addAny(YangStmtMapping.ENUM)
88         .addOptional(YangStmtMapping.FRACTION_DIGITS)
89         .addOptional(YangStmtMapping.LENGTH)
90         .addOptional(YangStmtMapping.PATH)
91         .addAny(YangStmtMapping.PATTERN)
92         .addOptional(YangStmtMapping.RANGE)
93         .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
94         .addAny(YangStmtMapping.TYPE)
95         .build();
96
97     static final String BINARY = "binary";
98     static final String BITS = "bits";
99     static final String BOOLEAN = "boolean";
100     static final String DECIMAL64 = "decimal64";
101     static final String EMPTY = "empty";
102     static final String ENUMERATION = "enumeration";
103     static final String IDENTITY_REF = "identityref";
104     static final String INSTANCE_IDENTIFIER = "instance-identifier";
105     static final String INT8 = "int8";
106     static final String INT16 = "int16";
107     static final String INT32 = "int32";
108     static final String INT64 = "int64";
109     static final String LEAF_REF = "leafref";
110     static final String STRING = "string";
111     static final String UINT8 = "uint8";
112     static final String UINT16 = "uint16";
113     static final String UINT32 = "uint32";
114     static final String UINT64 = "uint64";
115     static final String UNION = "union";
116
117     private static final ImmutableMap<String, String> BUILT_IN_TYPES = ImmutableMap.<String, String>builder()
118         .put(BINARY, BINARY)
119         .put(BITS, BITS)
120         .put(BOOLEAN, BOOLEAN)
121         .put(DECIMAL64, DECIMAL64)
122         .put(EMPTY, EMPTY)
123         .put(ENUMERATION, ENUMERATION)
124         .put(IDENTITY_REF,IDENTITY_REF)
125         .put(INSTANCE_IDENTIFIER, INSTANCE_IDENTIFIER)
126         .put(INT8, INT8)
127         .put(INT16, INT16)
128         .put(INT32, INT32)
129         .put(INT64, INT64)
130         .put(LEAF_REF, LEAF_REF)
131         .put(STRING, STRING)
132         .put(UINT8, UINT8)
133         .put(UINT16, UINT16)
134         .put(UINT32, UINT32)
135         .put(UINT64, UINT64)
136         .put(UNION, UNION)
137         .build();
138
139     private static final ImmutableMap<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS =
140             ImmutableMap.<String, StatementSupport<?, ?, ?>>builder()
141             .put(BITS, new BitsSpecificationSupport())
142             .put(DECIMAL64, new Decimal64SpecificationSupport())
143             .put(ENUMERATION, new EnumSpecificationSupport())
144             .put(IDENTITY_REF, new IdentityRefSpecificationRFC6020Support())
145             .put(INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationSupport())
146             .put(LEAF_REF, new LeafrefSpecificationRFC6020Support())
147             .put(UNION, new UnionSpecificationSupport())
148             .build();
149
150     AbstractTypeStatementSupport() {
151         super(YangStmtMapping.TYPE);
152     }
153
154     @Override
155     public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
156         return value;
157     }
158
159     @Override
160     public final void onFullDefinitionDeclared(
161             final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
162         super.onFullDefinitionDeclared(stmt);
163
164         // if it is yang built-in type, no prerequisite is needed, so simply return
165         if (BUILT_IN_TYPES.containsKey(stmt.getStatementArgument())) {
166             return;
167         }
168
169         final QName typeQName = StmtContextUtils.parseNodeIdentifier(stmt, stmt.getStatementArgument());
170         final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
171         final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
172                 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
173         typeAction.mutatesEffectiveCtx(stmt.getParentContext());
174
175         /*
176          * If the type does not exist, throw new InferenceException.
177          * Otherwise perform no operation.
178          */
179         typeAction.apply(new InferenceAction() {
180             @Override
181             public void apply(final InferenceContext ctx) {
182                 // Intentional NOOP
183             }
184
185             @Override
186             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
187                 InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
188                     "Type [%s] was not found.", typeQName);
189             }
190         });
191     }
192
193     @Override
194     public final String internArgument(final String rawArgument) {
195         final String found;
196         return (found = BUILT_IN_TYPES.get(rawArgument)) != null ? found : rawArgument;
197     }
198
199     @Override
200     public boolean hasArgumentSpecificSupports() {
201         return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
202     }
203
204     @Override
205     public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
206         return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
207     }
208
209     @Override
210     protected final SubstatementValidator getSubstatementValidator() {
211         return SUBSTATEMENT_VALIDATOR;
212     }
213
214     @Override
215     protected final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx,
216             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
217         return new RegularTypeStatement(ctx, substatements);
218     }
219
220     @Override
221     protected final TypeStatement createEmptyDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
222         final TypeStatement builtin;
223         return (builtin = BuiltinTypeStatement.lookup(ctx)) != null ? builtin : new EmptyTypeStatement(ctx);
224     }
225
226     @Override
227     protected final TypeEffectiveStatement<TypeStatement> createEffective(
228             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
229             final TypeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
230         // First look up the proper base type
231         final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(ctx);
232         // Now instantiate the proper effective statement for that type
233         final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
234         if (baseType instanceof BinaryTypeDefinition) {
235             return createBinary(ctx, (BinaryTypeDefinition) baseType, declared, substatements);
236         } else if (baseType instanceof BitsTypeDefinition) {
237             return createBits(ctx, (BitsTypeDefinition) baseType, declared, substatements);
238         } else if (baseType instanceof BooleanTypeDefinition) {
239             return createBoolean(ctx, (BooleanTypeDefinition) baseType, declared, substatements);
240         } else if (baseType instanceof DecimalTypeDefinition) {
241             return createDecimal(ctx, (DecimalTypeDefinition) baseType, declared, substatements);
242         } else if (baseType instanceof EmptyTypeDefinition) {
243             return createEmpty(ctx, (EmptyTypeDefinition) baseType, declared, substatements);
244         } else if (baseType instanceof EnumTypeDefinition) {
245             return createEnum(ctx, (EnumTypeDefinition) baseType, declared, substatements);
246         } else if (baseType instanceof IdentityrefTypeDefinition) {
247             return createIdentityref(ctx, (IdentityrefTypeDefinition) baseType, declared, substatements);
248         } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
249             return createInstanceIdentifier(ctx, (InstanceIdentifierTypeDefinition) baseType, declared, substatements);
250         } else if (baseType instanceof Int8TypeDefinition) {
251             return new IntegralTypeEffectiveStatementImpl<>(ctx,
252                     RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
253         } else if (baseType instanceof Int16TypeDefinition) {
254             return new IntegralTypeEffectiveStatementImpl<>(ctx,
255                     RestrictedTypes.newInt16Builder((Int16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
256         } else if (baseType instanceof Int32TypeDefinition) {
257             return new IntegralTypeEffectiveStatementImpl<>(ctx,
258                     RestrictedTypes.newInt32Builder((Int32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
259         } else if (baseType instanceof Int64TypeDefinition) {
260             return new IntegralTypeEffectiveStatementImpl<>(ctx,
261                     RestrictedTypes.newInt64Builder((Int64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
262         } else if (baseType instanceof LeafrefTypeDefinition) {
263             return createLeafref(ctx, (LeafrefTypeDefinition) baseType, declared, substatements);
264         } else if (baseType instanceof StringTypeDefinition) {
265             return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
266         } else if (baseType instanceof Uint8TypeDefinition) {
267             return new IntegralTypeEffectiveStatementImpl<>(ctx,
268                     RestrictedTypes.newUint8Builder((Uint8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
269         } else if (baseType instanceof Uint16TypeDefinition) {
270             return new IntegralTypeEffectiveStatementImpl<>(ctx,
271                     RestrictedTypes.newUint16Builder((Uint16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
272         } else if (baseType instanceof Uint32TypeDefinition) {
273             return new IntegralTypeEffectiveStatementImpl<>(ctx,
274                     RestrictedTypes.newUint32Builder((Uint32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
275         } else if (baseType instanceof Uint64TypeDefinition) {
276             return new IntegralTypeEffectiveStatementImpl<>(ctx,
277                     RestrictedTypes.newUint64Builder((Uint64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
278         } else if (baseType instanceof UnionTypeDefinition) {
279             return createUnion(ctx, (UnionTypeDefinition) baseType, declared, substatements);
280         } else {
281             throw new IllegalStateException("Unhandled base type " + baseType);
282         }
283     }
284
285     @Override
286     protected final EffectiveStatement<String, TypeStatement> createEmptyEffective(
287             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
288             final TypeStatement declared) {
289         return resolveType(ctx);
290     }
291
292     static final SchemaPath typeEffectiveSchemaPath(final StmtContext<?, ?, ?> stmtCtx) {
293         final SchemaPath path = stmtCtx.getSchemaPath().get();
294         final SchemaPath parent = path.getParent();
295         final QName parentQName = parent.getLastComponent();
296         checkArgument(parentQName != null, "Path %s has an empty parent", path);
297
298         final QName qname = path.getLastComponent().bindTo(parentQName.getModule()).intern();
299         return parent.createChild(qname);
300     }
301
302     /**
303      * Resolve type reference, as pointed to by the context's argument.
304      *
305      * @param ctx Statement context
306      * @return Resolved type
307      * @throws SourceException if the target type cannot be found
308      */
309     private static @NonNull TypeEffectiveStatement<TypeStatement> resolveType(final StmtContext<String, ?, ?> ctx) {
310         final String argument = ctx.coerceStatementArgument();
311         switch (argument) {
312             case BINARY:
313                 return BuiltinEffectiveStatement.BINARY;
314             case BOOLEAN:
315                 return BuiltinEffectiveStatement.BOOLEAN;
316             case EMPTY:
317                 return BuiltinEffectiveStatement.EMPTY;
318             case INSTANCE_IDENTIFIER:
319                 return BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
320             case INT8:
321                 return BuiltinEffectiveStatement.INT8;
322             case INT16:
323                 return BuiltinEffectiveStatement.INT16;
324             case INT32:
325                 return BuiltinEffectiveStatement.INT32;
326             case INT64:
327                 return BuiltinEffectiveStatement.INT64;
328             case STRING:
329                 return BuiltinEffectiveStatement.STRING;
330             case UINT8:
331                 return BuiltinEffectiveStatement.UINT8;
332             case UINT16:
333                 return BuiltinEffectiveStatement.UINT16;
334             case UINT32:
335                 return BuiltinEffectiveStatement.UINT32;
336             case UINT64:
337                 return BuiltinEffectiveStatement.UINT64;
338             default:
339                 final QName qname = StmtContextUtils.parseNodeIdentifier(ctx, argument);
340                 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
341                         SourceException.throwIfNull(ctx.getFromNamespace(TypeNamespace.class, qname),
342                             ctx.getStatementSourceReference(), "Type '%s' not found", qname);
343                 return typedef.buildEffective().asTypeEffectiveStatement();
344         }
345     }
346
347     private static @NonNull TypeEffectiveStatement<TypeStatement> createBinary(final StmtContext<?, ?, ?> ctx,
348             final BinaryTypeDefinition baseType, final TypeStatement declared,
349             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
350         final LengthRestrictedTypeBuilder<BinaryTypeDefinition> builder =
351                 RestrictedTypes.newBinaryBuilder(baseType, typeEffectiveSchemaPath(ctx));
352
353         for (EffectiveStatement<?, ?> stmt : substatements) {
354             if (stmt instanceof LengthEffectiveStatement) {
355                 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
356
357                 try {
358                     builder.setLengthConstraint(length, length.argument());
359                 } catch (IllegalStateException e) {
360                     throw new SourceException(ctx.getStatementSourceReference(), e,
361                         "Multiple length constraints encountered");
362                 } catch (InvalidLengthConstraintException e) {
363                     throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid length constraint %s",
364                         length.argument());
365                 }
366             }
367         }
368
369         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
370     }
371
372     private static @NonNull TypeEffectiveStatement<TypeStatement> createBits(final StmtContext<?, ?, ?> ctx,
373             final BitsTypeDefinition baseType, final TypeStatement declared,
374             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
375         final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath().get());
376
377         final YangVersion yangVersion = ctx.getRootVersion();
378         for (final EffectiveStatement<?, ?> stmt : substatements) {
379             if (stmt instanceof BitEffectiveStatement) {
380                 SourceException.throwIf(yangVersion != YangVersion.VERSION_1_1, ctx.getStatementSourceReference(),
381                         "Restricted bits type is allowed only in YANG 1.1 version.");
382                 final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) stmt;
383
384                 // FIXME: this looks like a duplicate of BitsSpecificationEffectiveStatement
385                 final Optional<Uint32> declaredPosition = bitSubStmt.getDeclaredPosition();
386                 final Uint32 effectivePos;
387                 if (declaredPosition.isEmpty()) {
388                     effectivePos = getBaseTypeBitPosition(bitSubStmt.argument(), baseType, ctx);
389                 } else {
390                     effectivePos = declaredPosition.get();
391                 }
392
393                 builder.addBit(EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos));
394             }
395         }
396
397         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
398     }
399
400     private static @NonNull TypeEffectiveStatement<TypeStatement> createBoolean(final StmtContext<?, ?, ?> ctx,
401             final BooleanTypeDefinition baseType, final TypeStatement declared,
402             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
403         return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newBooleanBuilder(baseType,
404             typeEffectiveSchemaPath(ctx)));
405     }
406
407     private static @NonNull TypeEffectiveStatement<TypeStatement> createDecimal(final StmtContext<?, ?, ?> ctx,
408             final DecimalTypeDefinition baseType, final TypeStatement declared,
409             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
410         final RangeRestrictedTypeBuilder<DecimalTypeDefinition, BigDecimal> builder =
411                 RestrictedTypes.newDecima64Builder(baseType, typeEffectiveSchemaPath(ctx));
412
413         for (EffectiveStatement<?, ?> stmt : substatements) {
414             if (stmt instanceof RangeEffectiveStatement) {
415                 final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
416                 builder.setRangeConstraint(range, range.argument());
417             }
418             if (stmt instanceof FractionDigitsEffectiveStatement) {
419                 final Integer digits = ((FractionDigitsEffectiveStatement)stmt).argument();
420                 SourceException.throwIf(baseType.getFractionDigits() != digits, ctx.getStatementSourceReference(),
421                     "Cannot override fraction-digits from base type %s to %s", baseType, digits);
422             }
423         }
424
425         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
426     }
427
428     private static @NonNull TypeEffectiveStatement<TypeStatement> createEmpty(final StmtContext<?, ?, ?> ctx,
429             final EmptyTypeDefinition baseType, final TypeStatement declared,
430             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
431         return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newEmptyBuilder(baseType,
432             typeEffectiveSchemaPath(ctx)));
433     }
434
435     private static @NonNull TypeEffectiveStatement<TypeStatement> createEnum(final StmtContext<?, ?, ?> ctx,
436             final EnumTypeDefinition baseType, final TypeStatement declared,
437             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
438         final EnumerationTypeBuilder builder = RestrictedTypes.newEnumerationBuilder(baseType,
439             ctx.getSchemaPath().get());
440
441         final YangVersion yangVersion = ctx.getRootVersion();
442         for (final EffectiveStatement<?, ?> stmt : substatements) {
443             if (stmt instanceof EnumEffectiveStatement) {
444                 SourceException.throwIf(yangVersion != YangVersion.VERSION_1_1, ctx.getStatementSourceReference(),
445                         "Restricted enumeration type is allowed only in YANG 1.1 version.");
446
447                 final EnumEffectiveStatement enumSubStmt = (EnumEffectiveStatement) stmt;
448                 final Optional<Integer> declaredValue =
449                         enumSubStmt.findFirstEffectiveSubstatementArgument(ValueEffectiveStatement.class);
450                 final int effectiveValue;
451                 if (declaredValue.isEmpty()) {
452                     effectiveValue = getBaseTypeEnumValue(enumSubStmt.getDeclared().rawArgument(), baseType, ctx);
453                 } else {
454                     effectiveValue = declaredValue.orElseThrow();
455                 }
456
457                 builder.addEnum(EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue));
458             }
459         }
460
461         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
462     }
463
464     private static @NonNull TypeEffectiveStatement<TypeStatement> createIdentityref(final StmtContext<?, ?, ?> ctx,
465             final IdentityrefTypeDefinition baseType, final TypeStatement declared,
466             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
467         return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newIdentityrefBuilder(baseType,
468             typeEffectiveSchemaPath(ctx)));
469     }
470
471     private static @NonNull TypeEffectiveStatement<TypeStatement> createInstanceIdentifier(
472             final StmtContext<?, ?, ?> ctx, final InstanceIdentifierTypeDefinition baseType,
473             final TypeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
474         final InstanceIdentifierTypeBuilder builder = RestrictedTypes.newInstanceIdentifierBuilder(baseType,
475                     typeEffectiveSchemaPath(ctx));
476
477         for (EffectiveStatement<?, ?> stmt : substatements) {
478             if (stmt instanceof RequireInstanceEffectiveStatement) {
479                 builder.setRequireInstance(((RequireInstanceEffectiveStatement)stmt).argument());
480             }
481         }
482
483         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
484     }
485
486     private static @NonNull TypeEffectiveStatement<TypeStatement> createLeafref(final StmtContext<?, ?, ?> ctx,
487             final LeafrefTypeDefinition baseType, final TypeStatement declared,
488             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
489         final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> builder =
490                 RestrictedTypes.newLeafrefBuilder(baseType, AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
491
492         for (final EffectiveStatement<?, ?> stmt : substatements) {
493             if (stmt instanceof RequireInstanceEffectiveStatement) {
494                 builder.setRequireInstance(((RequireInstanceEffectiveStatement) stmt).argument());
495             }
496         }
497
498         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
499     }
500
501     private static @NonNull TypeEffectiveStatement<TypeStatement> createUnion(final StmtContext<?, ?, ?> ctx,
502             final UnionTypeDefinition baseType, final TypeStatement declared,
503             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
504         return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newUnionBuilder(baseType,
505             typeEffectiveSchemaPath(ctx)));
506     }
507
508     private static Uint32 getBaseTypeBitPosition(final String bitName, final BitsTypeDefinition baseType,
509             final StmtContext<?, ?, ?> ctx) {
510         for (Bit baseTypeBit : baseType.getBits()) {
511             if (bitName.equals(baseTypeBit.getName())) {
512                 return baseTypeBit.getPosition();
513             }
514         }
515
516         throw new SourceException(ctx.getStatementSourceReference(),
517                 "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());
518     }
519
520     private static int getBaseTypeEnumValue(final String enumName, final EnumTypeDefinition baseType,
521             final StmtContext<?, ?, ?> ctx) {
522         for (EnumPair baseTypeEnumPair : baseType.getValues()) {
523             if (enumName.equals(baseTypeEnumPair.getName())) {
524                 return baseTypeEnumPair.getValue();
525             }
526         }
527
528         throw new SourceException(ctx.getStatementSourceReference(),
529                 "Enum '%s' is not a subset of its base enumeration type %s.", enumName, baseType.getQName());
530     }
531 }