8cfe5f34da2e71931b831bdbea625ad38559c557
[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.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;
85
86 abstract class AbstractTypeStatementSupport
87         extends BaseStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
88     private static final Logger LOG = LoggerFactory.getLogger(AbstractTypeStatementSupport.class);
89
90     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
91         YangStmtMapping.TYPE)
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)
102         .build();
103
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";
123
124     private static final ImmutableMap<String, String> BUILT_IN_TYPES = ImmutableMap.<String, String>builder()
125         .put(BINARY, BINARY)
126         .put(BITS, BITS)
127         .put(BOOLEAN, BOOLEAN)
128         .put(DECIMAL64, DECIMAL64)
129         .put(EMPTY, EMPTY)
130         .put(ENUMERATION, ENUMERATION)
131         .put(IDENTITY_REF,IDENTITY_REF)
132         .put(INSTANCE_IDENTIFIER, INSTANCE_IDENTIFIER)
133         .put(INT8, INT8)
134         .put(INT16, INT16)
135         .put(INT32, INT32)
136         .put(INT64, INT64)
137         .put(LEAF_REF, LEAF_REF)
138         .put(STRING, STRING)
139         .put(UINT8, UINT8)
140         .put(UINT16, UINT16)
141         .put(UINT32, UINT32)
142         .put(UINT64, UINT64)
143         .put(UNION, UNION)
144         .build();
145
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())
155             .build();
156
157     AbstractTypeStatementSupport() {
158         super(YangStmtMapping.TYPE);
159     }
160
161     @Override
162     public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
163         return value;
164     }
165
166     @Override
167     public final void onFullDefinitionDeclared(
168             final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
169         super.onFullDefinitionDeclared(stmt);
170
171         // if it is yang built-in type, no prerequisite is needed, so simply return
172         if (BUILT_IN_TYPES.containsKey(stmt.getStatementArgument())) {
173             return;
174         }
175
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());
181
182         /*
183          * If the type does not exist, throw new InferenceException.
184          * Otherwise perform no operation.
185          */
186         typeAction.apply(new InferenceAction() {
187             @Override
188             public void apply(final InferenceContext ctx) {
189                 // Intentional NOOP
190             }
191
192             @Override
193             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
194                 InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
195                     "Type [%s] was not found.", typeQName);
196             }
197         });
198     }
199
200     @Override
201     public final String internArgument(final String rawArgument) {
202         final String found;
203         return (found = BUILT_IN_TYPES.get(rawArgument)) != null ? found : rawArgument;
204     }
205
206     @Override
207     public boolean hasArgumentSpecificSupports() {
208         return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
209     }
210
211     @Override
212     public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
213         return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
214     }
215
216     @Override
217     protected final SubstatementValidator getSubstatementValidator() {
218         return SUBSTATEMENT_VALIDATOR;
219     }
220
221     @Override
222     protected final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx,
223             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
224         return new RegularTypeStatement(ctx, substatements);
225     }
226
227     @Override
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);
231     }
232
233     @Override
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);
287         } else {
288             throw new IllegalStateException("Unhandled base type " + baseType);
289         }
290     }
291
292     @Override
293     protected final EffectiveStatement<String, TypeStatement> createEmptyEffective(
294             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
295             final TypeStatement declared) {
296         return resolveType(ctx);
297     }
298
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);
304
305         final QName qname = path.getLastComponent().bindTo(parentQName.getModule()).intern();
306         return parent.createChild(qname);
307     }
308
309     /**
310      * Resolve type reference, as pointed to by the context's argument.
311      *
312      * @param ctx Statement context
313      * @return Resolved type
314      * @throws SourceException if the target type cannot be found
315      */
316     private static @NonNull TypeEffectiveStatement<TypeStatement> resolveType(final StmtContext<String, ?, ?> ctx) {
317         final String argument = ctx.coerceStatementArgument();
318         switch (argument) {
319             case BINARY:
320                 return BuiltinEffectiveStatement.BINARY;
321             case BOOLEAN:
322                 return BuiltinEffectiveStatement.BOOLEAN;
323             case EMPTY:
324                 return BuiltinEffectiveStatement.EMPTY;
325             case INSTANCE_IDENTIFIER:
326                 return BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
327             case INT8:
328                 return BuiltinEffectiveStatement.INT8;
329             case INT16:
330                 return BuiltinEffectiveStatement.INT16;
331             case INT32:
332                 return BuiltinEffectiveStatement.INT32;
333             case INT64:
334                 return BuiltinEffectiveStatement.INT64;
335             case STRING:
336                 return BuiltinEffectiveStatement.STRING;
337             case UINT8:
338                 return BuiltinEffectiveStatement.UINT8;
339             case UINT16:
340                 return BuiltinEffectiveStatement.UINT16;
341             case UINT32:
342                 return BuiltinEffectiveStatement.UINT32;
343             case UINT64:
344                 return BuiltinEffectiveStatement.UINT64;
345             default:
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();
351         }
352     }
353
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));
359
360         for (EffectiveStatement<?, ?> stmt : substatements) {
361             if (stmt instanceof LengthEffectiveStatement) {
362                 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
363
364                 try {
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",
371                         length.argument());
372                 }
373             }
374         }
375
376         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
377     }
378
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());
383
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;
390
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);
396                 } else {
397                     effectivePos = declaredPosition.get();
398                 }
399
400                 builder.addBit(EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos));
401             }
402         }
403
404         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
405     }
406
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)));
412     }
413
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));
419
420         for (EffectiveStatement<?, ?> stmt : substatements) {
421             if (stmt instanceof RangeEffectiveStatement) {
422                 final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
423                 builder.setRangeConstraint(range, range.argument());
424             }
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);
429             }
430         }
431
432         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
433     }
434
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)));
440     }
441
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());
447
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.");
453
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);
460                 } else {
461                     effectiveValue = declaredValue.orElseThrow();
462                 }
463
464                 builder.addEnum(EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue));
465             }
466         }
467
468         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
469     }
470
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)));
476     }
477
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));
483
484         for (EffectiveStatement<?, ?> stmt : substatements) {
485             if (stmt instanceof RequireInstanceEffectiveStatement) {
486                 builder.setRequireInstance(((RequireInstanceEffectiveStatement)stmt).argument());
487             }
488         }
489
490         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
491     }
492
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));
498
499         for (final EffectiveStatement<?, ?> stmt : substatements) {
500             if (stmt instanceof RequireInstanceEffectiveStatement) {
501                 builder.setRequireInstance(((RequireInstanceEffectiveStatement) stmt).argument());
502             }
503         }
504         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
505     }
506
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));
512
513         for (EffectiveStatement<?, ?> stmt : substatements) {
514             if (stmt instanceof LengthEffectiveStatement) {
515                 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
516
517                 try {
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",
524                         length.argument());
525                 }
526             }
527             if (stmt instanceof PatternEffectiveStatement) {
528                 final PatternConstraint pattern = ((PatternEffectiveStatement)stmt).argument();
529                 if (pattern != null) {
530                     builder.addPatternConstraint(pattern);
531                 } else {
532                     LOG.debug("Ignoring empty pattern statement {}", stmt);
533                 }
534             }
535         }
536
537         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
538     }
539
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)));
545     }
546
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();
552             }
553         }
554
555         throw new SourceException(ctx.getStatementSourceReference(),
556                 "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());
557     }
558
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();
564             }
565         }
566
567         throw new SourceException(ctx.getStatementSourceReference(),
568                 "Enum '%s' is not a subset of its base enumeration type %s.", enumName, baseType.getQName());
569     }
570 }