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