Rework AugmentRuntimeType and Choice/Case linkage
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / AbstractTypeObjectGenerator.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, 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.mdsal.binding.generator.impl.reactor;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static com.google.common.base.Verify.verifyNotNull;
13
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.Maps;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Optional;
21 import java.util.stream.Collectors;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.opendaylight.mdsal.binding.generator.BindingGeneratorUtil;
25 import org.opendaylight.mdsal.binding.generator.impl.reactor.TypeReference.ResolvedLeafref;
26 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
27 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
28 import org.opendaylight.mdsal.binding.model.api.Enumeration;
29 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
30 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
31 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
32 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
33 import org.opendaylight.mdsal.binding.model.api.Restrictions;
34 import org.opendaylight.mdsal.binding.model.api.Type;
35 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder;
36 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
37 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
38 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
39 import org.opendaylight.mdsal.binding.model.ri.BaseYangTypes;
40 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
41 import org.opendaylight.mdsal.binding.model.ri.TypeConstants;
42 import org.opendaylight.mdsal.binding.model.ri.Types;
43 import org.opendaylight.mdsal.binding.model.ri.generated.type.builder.AbstractEnumerationBuilder;
44 import org.opendaylight.mdsal.binding.model.ri.generated.type.builder.GeneratedPropertyBuilderImpl;
45 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
46 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
47 import org.opendaylight.yangtools.concepts.Immutable;
48 import org.opendaylight.yangtools.yang.binding.RegexPatterns;
49 import org.opendaylight.yangtools.yang.binding.TypeObject;
50 import org.opendaylight.yangtools.yang.common.QName;
51 import org.opendaylight.yangtools.yang.common.YangConstants;
52 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
53 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
54 import org.opendaylight.yangtools.yang.model.api.stmt.BaseEffectiveStatement;
55 import org.opendaylight.yangtools.yang.model.api.stmt.LengthEffectiveStatement;
56 import org.opendaylight.yangtools.yang.model.api.stmt.PathEffectiveStatement;
57 import org.opendaylight.yangtools.yang.model.api.stmt.PatternEffectiveStatement;
58 import org.opendaylight.yangtools.yang.model.api.stmt.PatternExpression;
59 import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
60 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
61 import org.opendaylight.yangtools.yang.model.api.stmt.ValueRange;
62 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
63 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
64 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
65 import org.opendaylight.yangtools.yang.model.api.type.ModifierKind;
66 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
67 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
68 import org.opendaylight.yangtools.yang.model.api.type.TypeDefinitions;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71
72 /**
73  * Common base class for {@link TypedefGenerator} and {@link AbstractTypeAwareGenerator}. This encompasses three
74  * different statements with two different semantics:
75  * <ul>
76  *   <li>{@link TypedefGenerator}s always result in a generated {@link TypeObject}, even if the semantics is exactly
77  *       the same as its base type. This aligns with {@code typedef} defining a new type.<li>
78  *   <li>{@link LeafGenerator}s and {@link LeafListGenerator}s, on the other hand, do not generate a {@link TypeObject}
79  *       unless absolutely necassary. This aligns with {@code leaf} and {@code leaf-list} being mapped onto a property
80  *       of its parent type.<li>
81  * </ul>
82  *
83  * <p>
84  * To throw a bit of confusion into the mix, there are three exceptions to those rules:
85  * <ul>
86  *   <li>
87  *     {@code identityref} definitions never result in a type definition being emitted. The reason for this has to do
88  *     with identity type mapping as well as history of our codebase.
89  *
90  *     <p>
91  *     The problem at hand is inconsistency between the fact that identity is mapped to a {@link Class}, which is also
92  *     returned from leaves which specify it like this:
93  *     <pre>
94  *       <code>
95  *         identity iden;
96  *
97  *         container foo {
98  *           leaf foo {
99  *             type identityref {
100  *               base iden;
101  *             }
102  *           }
103  *         }
104  *       </code>
105  *     </pre>
106  *     which results in fine-looking
107  *     <pre>
108  *       <code>
109  *         interface Foo {
110  *           Class&lt;? extends Iden&gt; getFoo();
111  *         }
112  *       </code>
113  *     </pre>
114  *
115  *     <p>
116  *     This gets more dicey if we decide to extend the previous snippet to also include:
117  *     <pre>
118  *       <code>
119  *         typedef bar-ref {
120  *           type identityref {
121  *             base iden;
122  *           }
123  *         }
124  *
125  *         container bar {
126  *           leaf bar {
127  *             type bar-ref;
128  *           }
129  *         }
130  *       </code>
131  *     </pre>
132  *
133  *     <p>
134  *     Now we have competing requirements: {@code typedef} would like us to use encapsulation to capture the defined
135  *     type, while {@code getBar()} wants us to retain shape with getFoo(), as it should not matter how the
136  *     {@code identityref} was formed. We need to pick between:
137  *     <ol>
138  *       <li>
139  *         <pre>
140  *           <code>
141  *             public class BarRef extends ScalarTypeObject&lt;Class&lt;? extends Iden&gt;&gt; {
142  *               Class&lt;? extends Iden&gt; getValue() {
143  *                 // ...
144  *               }
145  *             }
146  *
147  *             interface Bar {
148  *               BarRef getBar();
149  *             }
150  *           </code>
151  *         </pre>
152  *       </li>
153  *       <li>
154  *         <pre>
155  *           <code>
156  *             interface Bar {
157  *               Class&lt;? extends Iden&gt; getBar();
158  *             }
159  *           </code>
160  *         </pre>
161  *       </li>
162  *     </ol>
163  *
164  *     <p>
165  *     Here the second option is more user-friendly, as the type system works along the lines of <b>reference</b>
166  *     semantics, treating and {@code Bar.getBar()} and {@code Foo.getFoo()} as equivalent. The first option would
167  *     force users to go through explicit encapsulation, for no added benefit as the {@code typedef} cannot possibly add
168  *     anything useful to the actual type semantics.
169  *   </li>
170  *   <li>
171  *     {@code leafref} definitions never result in a type definition being emitted. The reasons for this are similar to
172  *     {@code identityref}, but have an additional twist: a {@leafref} can target a relative path, which may only be
173  *     resolved at a particular instantiation.
174  *
175  *     Take the example of the following model:
176  *     <pre>
177  *       <code>
178  *         grouping grp {
179  *           typedef ref {
180  *             type leafref {
181  *               path ../xyzzy;
182  *             }
183  *           }
184  *
185  *           leaf foo {
186  *             type ref;
187  *           }
188  *         }
189  *
190  *         container bar {
191              uses grp;
192  *
193  *           leaf xyzzy {
194  *             type string;
195  *           }
196  *         }
197  *
198  *         container baz {
199  *           uses grp;
200  *
201  *           leaf xyzzy {
202  *             type int32;
203  *           }
204  *         }
205  *       </code>
206  *     </pre>
207  *
208  *     The {@code typedef ref} points to outside of the grouping, and hence the type of {@code leaf foo} is polymorphic:
209  *     the definition in {@code grouping grp} needs to use {@code Object}, whereas the instantiations in
210  *     {@code container bar} and {@code container baz} need to use {@code String} and {@link Integer} respectively.
211  *     Expressing the resulting interface contracts requires return type specialization and run-time checks. An
212  *     intermediate class generated for the typedef would end up being a hindrance without any benefit.
213  *   <li>
214  *   <li>
215  *     {@code enumeration} definitions never result in a derived type. This is because these are mapped to Java
216  *     {@code enum}, which does not allow subclassing.
217  *   <li>
218  * </ul>
219  *
220  * <p>
221  * At the end of the day, the mechanic translation rules are giving way to correctly mapping the semantics -- which in
222  * both of the exception cases boil down to tracking type indirection. Intermediate constructs involved in tracking
223  * type indirection in YANG constructs is therefore explicitly excluded from the generated Java code, but the Binding
224  * Specification still takes them into account when determining types as outlined above.
225  */
226 abstract class AbstractTypeObjectGenerator<S extends EffectiveStatement<?, ?>, R extends RuntimeType>
227         extends AbstractDependentGenerator<S, R> {
228     private static final class UnionDependencies implements Immutable {
229         private final Map<EffectiveStatement<?, ?>, TypeReference> identityTypes = new HashMap<>();
230         private final Map<EffectiveStatement<?, ?>, TypeReference> leafTypes = new HashMap<>();
231         private final Map<QName, TypedefGenerator> baseTypes = new HashMap<>();
232
233         UnionDependencies(final TypeEffectiveStatement<?> type, final GeneratorContext context) {
234             resolveUnionDependencies(context, type);
235         }
236
237         private void resolveUnionDependencies(final GeneratorContext context, final TypeEffectiveStatement<?> union) {
238             for (EffectiveStatement<?, ?> stmt : union.effectiveSubstatements()) {
239                 if (stmt instanceof TypeEffectiveStatement) {
240                     final TypeEffectiveStatement<?> type = (TypeEffectiveStatement<?>) stmt;
241                     final QName typeName = type.argument();
242                     if (TypeDefinitions.IDENTITYREF.equals(typeName)) {
243                         if (!identityTypes.containsKey(stmt)) {
244                             identityTypes.put(stmt, TypeReference.identityRef(
245                                 type.streamEffectiveSubstatements(BaseEffectiveStatement.class)
246                                     .map(BaseEffectiveStatement::argument)
247                                     .map(context::resolveIdentity)
248                                     .collect(Collectors.toUnmodifiableList())));
249                         }
250                     } else if (TypeDefinitions.LEAFREF.equals(typeName)) {
251                         if (!leafTypes.containsKey(stmt)) {
252                             leafTypes.put(stmt, TypeReference.leafRef(context.resolveLeafref(
253                                 type.findFirstEffectiveSubstatementArgument(PathEffectiveStatement.class)
254                                 .orElseThrow())));
255                         }
256                     } else if (TypeDefinitions.UNION.equals(typeName)) {
257                         resolveUnionDependencies(context, type);
258                     } else if (!isBuiltinName(typeName) && !baseTypes.containsKey(typeName)) {
259                         baseTypes.put(typeName, context.resolveTypedef(typeName));
260                     }
261                 }
262             }
263         }
264     }
265
266     private static final Logger LOG = LoggerFactory.getLogger(AbstractTypeObjectGenerator.class);
267     static final ImmutableMap<QName, Type> SIMPLE_TYPES = ImmutableMap.<QName, Type>builder()
268         .put(TypeDefinitions.BINARY, BaseYangTypes.BINARY_TYPE)
269         .put(TypeDefinitions.BOOLEAN, BaseYangTypes.BOOLEAN_TYPE)
270         .put(TypeDefinitions.DECIMAL64, BaseYangTypes.DECIMAL64_TYPE)
271         .put(TypeDefinitions.EMPTY, BaseYangTypes.EMPTY_TYPE)
272         .put(TypeDefinitions.INSTANCE_IDENTIFIER, BaseYangTypes.INSTANCE_IDENTIFIER)
273         .put(TypeDefinitions.INT8, BaseYangTypes.INT8_TYPE)
274         .put(TypeDefinitions.INT16, BaseYangTypes.INT16_TYPE)
275         .put(TypeDefinitions.INT32, BaseYangTypes.INT32_TYPE)
276         .put(TypeDefinitions.INT64, BaseYangTypes.INT64_TYPE)
277         .put(TypeDefinitions.STRING, BaseYangTypes.STRING_TYPE)
278         .put(TypeDefinitions.UINT8, BaseYangTypes.UINT8_TYPE)
279         .put(TypeDefinitions.UINT16, BaseYangTypes.UINT16_TYPE)
280         .put(TypeDefinitions.UINT32, BaseYangTypes.UINT32_TYPE)
281         .put(TypeDefinitions.UINT64, BaseYangTypes.UINT64_TYPE)
282         .build();
283
284     private final TypeEffectiveStatement<?> type;
285
286     // FIXME: these fields should be better-controlled with explicit sequencing guards. It it currently stands, we are
287     //        expending two (or more) additional fields to express downstream linking. If we had the concept of
288     //        resolution step (an enum), we could just get by with a simple queue of Step/Callback pairs, which would
289     //        trigger as needed. See how we manage baseGen/inferred fields.
290
291     /**
292      * The generator corresponding to our YANG base type. It produces the superclass of our encapsulated type. If it is
293      * {@code null}, this generator is the root of the hierarchy.
294      */
295     private TypedefGenerator baseGen;
296     private TypeReference refType;
297     private List<GeneratedType> auxiliaryGeneratedTypes = List.of();
298     private UnionDependencies unionDependencies;
299     private List<AbstractTypeObjectGenerator<?, ?>> inferred = List.of();
300
301     /**
302      * The type of single-element return type of the getter method associated with this generator. This is retained for
303      * run-time type purposes. It may be uninitialized, in which case this object must have a generated type.
304      */
305     private Type methodReturnTypeElement;
306
307     AbstractTypeObjectGenerator(final S statement, final AbstractCompositeGenerator<?, ?> parent) {
308         super(statement, parent);
309         type = statement().findFirstEffectiveSubstatement(TypeEffectiveStatement.class).orElseThrow();
310     }
311
312     @Override
313     public final List<GeneratedType> auxiliaryGeneratedTypes() {
314         return auxiliaryGeneratedTypes;
315     }
316
317     @Override
318     final void linkDependencies(final GeneratorContext context) {
319         verify(inferred != null, "Duplicate linking of %s", this);
320
321         final QName typeName = type.argument();
322         if (isBuiltinName(typeName)) {
323             verify(inferred.isEmpty(), "Unexpected non-empty downstreams in %s", this);
324             inferred = null;
325             return;
326         }
327
328         final AbstractExplicitGenerator<S, R> prev = previous();
329         if (prev != null) {
330             verify(prev instanceof AbstractTypeObjectGenerator, "Unexpected previous %s", prev);
331             ((AbstractTypeObjectGenerator<S, R>) prev).linkInferred(this);
332         } else {
333             linkBaseGen(context.resolveTypedef(typeName));
334         }
335     }
336
337     private void linkInferred(final AbstractTypeObjectGenerator<?, ?> downstream) {
338         if (inferred == null) {
339             downstream.linkBaseGen(verifyNotNull(baseGen, "Mismatch on linking between %s and %s", this, downstream));
340             return;
341         }
342
343         if (inferred.isEmpty()) {
344             inferred = new ArrayList<>(2);
345         }
346         inferred.add(downstream);
347     }
348
349     private void linkBaseGen(final TypedefGenerator upstreamBaseGen) {
350         verify(baseGen == null, "Attempted to replace base %s with %s in %s", baseGen, upstreamBaseGen, this);
351         final List<AbstractTypeObjectGenerator<?, ?>> downstreams = verifyNotNull(inferred,
352             "Duplicated linking of %s", this);
353         baseGen = verifyNotNull(upstreamBaseGen);
354         baseGen.addDerivedGenerator(this);
355         inferred = null;
356
357         for (AbstractTypeObjectGenerator<?, ?> downstream : downstreams) {
358             downstream.linkBaseGen(upstreamBaseGen);
359         }
360     }
361
362     void bindTypeDefinition(final GeneratorContext context) {
363         if (baseGen != null) {
364             // We have registered with baseGen, it will push the type to us
365             return;
366         }
367
368         final QName arg = type.argument();
369         if (TypeDefinitions.IDENTITYREF.equals(arg)) {
370             refType = TypeReference.identityRef(type.streamEffectiveSubstatements(BaseEffectiveStatement.class)
371                 .map(BaseEffectiveStatement::argument)
372                 .map(context::resolveIdentity)
373                 .collect(Collectors.toUnmodifiableList()));
374         } else if (TypeDefinitions.LEAFREF.equals(arg)) {
375             final AbstractTypeObjectGenerator<?, ?> targetGenerator = context.resolveLeafref(
376                 type.findFirstEffectiveSubstatementArgument(PathEffectiveStatement.class).orElseThrow());
377             checkArgument(targetGenerator != this, "Effective model contains self-referencing leaf %s",
378                 statement().argument());
379             refType = TypeReference.leafRef(targetGenerator);
380         } else if (TypeDefinitions.UNION.equals(arg)) {
381             unionDependencies = new UnionDependencies(type, context);
382             LOG.trace("Resolved union {} to dependencies {}", type, unionDependencies);
383         }
384
385         LOG.trace("Resolved base {} to generator {}", type, refType);
386         bindDerivedGenerators(refType);
387     }
388
389     final void bindTypeDefinition(final @Nullable TypeReference reference) {
390         refType = reference;
391         LOG.trace("Resolved derived {} to generator {}", type, refType);
392     }
393
394     private static boolean isBuiltinName(final QName typeName) {
395         return YangConstants.RFC6020_YANG_MODULE.equals(typeName.getModule());
396     }
397
398     abstract void bindDerivedGenerators(@Nullable TypeReference reference);
399
400     @Override
401     final ClassPlacement classPlacement() {
402         if (refType != null) {
403             // Reference types never create a new type
404             return ClassPlacement.NONE;
405         }
406         if (isDerivedEnumeration()) {
407             // Types derived from an enumeration never create a new type, as that would have to be a subclass of an enum
408             // and since enums are final, that can never happen.
409             return ClassPlacement.NONE;
410         }
411         return classPlacementImpl();
412     }
413
414     @NonNull ClassPlacement classPlacementImpl() {
415         // TODO: make this a lot more accurate by comparing the effective delta between the base type and the effective
416         //       restricted type. We should not be generating a type for constructs like:
417         //
418         //         leaf foo {
419         //           type uint8 { range 0..255; }
420         //         }
421         //
422         //       or
423         //
424         //         typedef foo {
425         //           type uint8 { range 0..100; }
426         //         }
427         //
428         //         leaf foo {
429         //           type foo { range 0..100; }
430         //         }
431         //
432         //       Which is relatively easy to do for integral types, but is way more problematic for 'pattern'
433         //       restrictions. Nevertheless we can define the mapping in a way which can be implemented with relative
434         //       ease.
435         return baseGen != null || SIMPLE_TYPES.containsKey(type.argument()) || isAddedByUses() || isAugmenting()
436             ? ClassPlacement.NONE : ClassPlacement.MEMBER;
437     }
438
439     @Override
440     final GeneratedType getGeneratedType(final TypeBuilderFactory builderFactory) {
441         // For derived enumerations defer to base type
442         return isDerivedEnumeration() ? baseGen.getGeneratedType(builderFactory)
443             : super.getGeneratedType(builderFactory);
444     }
445
446     final boolean isEnumeration() {
447         return baseGen != null ? baseGen.isEnumeration() : TypeDefinitions.ENUMERATION.equals(type.argument());
448     }
449
450     final boolean isDerivedEnumeration() {
451         return baseGen != null && baseGen.isEnumeration();
452     }
453
454     @Override
455     Type methodReturnType(final TypeBuilderFactory builderFactory) {
456         return methodReturnElementType(builderFactory);
457     }
458
459     @Override
460     final Type runtimeJavaType() {
461         if (methodReturnTypeElement != null) {
462             return methodReturnTypeElement;
463         }
464         final var genType = generatedType();
465         if (genType.isPresent()) {
466             return genType.orElseThrow();
467         }
468         final var prev = verifyNotNull(previous(), "No previous generator for %s", this);
469         return prev.runtimeJavaType();
470     }
471
472     final @NonNull Type methodReturnElementType(final @NonNull TypeBuilderFactory builderFactory) {
473         var local = methodReturnTypeElement;
474         if (local == null) {
475             methodReturnTypeElement = local = createMethodReturnElementType(builderFactory);
476         }
477         return local;
478     }
479
480     private @NonNull Type createMethodReturnElementType(final @NonNull TypeBuilderFactory builderFactory) {
481         final GeneratedType generatedType = tryGeneratedType(builderFactory);
482         if (generatedType != null) {
483             // We have generated a type here, so return it. This covers 'bits', 'enumeration' and 'union'.
484             return generatedType;
485         }
486
487         if (refType != null) {
488             // This is a reference type of some kind. Defer to its judgement as to what the return type is.
489             return refType.methodReturnType(builderFactory);
490         }
491
492         final AbstractExplicitGenerator<?, ?> prev = previous();
493         if (prev != null) {
494             // We have been added through augment/uses, defer to the original definition
495             return prev.methodReturnType(builderFactory);
496         }
497
498         final Type baseType;
499         if (baseGen == null) {
500             final QName qname = type.argument();
501             baseType = verifyNotNull(SIMPLE_TYPES.get(qname), "Cannot resolve type %s in %s", qname, this);
502         } else {
503             // We are derived from a base generator. Defer to its type for return.
504             baseType = baseGen.getGeneratedType(builderFactory);
505         }
506
507         return restrictType(baseType, computeRestrictions(), builderFactory);
508     }
509
510     private static @NonNull Type restrictType(final @NonNull Type baseType, final Restrictions restrictions,
511             final TypeBuilderFactory builderFactory) {
512         if (restrictions == null || restrictions.isEmpty()) {
513             // No additional restrictions, return base type
514             return baseType;
515         }
516
517         if (!(baseType instanceof GeneratedTransferObject)) {
518             // This is a simple Java type, just wrap it with new restrictions
519             return Types.restrictedType(baseType, restrictions);
520         }
521
522         // Base type is a GTO, we need to re-adjust it with new restrictions
523         final GeneratedTransferObject gto = (GeneratedTransferObject) baseType;
524         final GeneratedTOBuilder builder = builderFactory.newGeneratedTOBuilder(gto.getIdentifier());
525         final GeneratedTransferObject parent = gto.getSuperType();
526         if (parent != null) {
527             builder.setExtendsType(parent);
528         }
529         builder.setRestrictions(restrictions);
530         for (GeneratedProperty gp : gto.getProperties()) {
531             builder.addProperty(gp.getName())
532                 .setValue(gp.getValue())
533                 .setReadOnly(gp.isReadOnly())
534                 .setAccessModifier(gp.getAccessModifier())
535                 .setReturnType(gp.getReturnType())
536                 .setFinal(gp.isFinal())
537                 .setStatic(gp.isStatic());
538         }
539         return builder.build();
540     }
541
542     @Override
543     final void addAsGetterMethodOverride(final GeneratedTypeBuilderBase<?> builder,
544             final TypeBuilderFactory builderFactory) {
545         if (!(refType instanceof ResolvedLeafref)) {
546             // We are not dealing with a leafref or have nothing to add
547             return;
548         }
549
550         final AbstractTypeObjectGenerator<?, ?> prev =
551             (AbstractTypeObjectGenerator<?, ?>) verifyNotNull(previous(), "Missing previous link in %s", this);
552         if (prev.refType instanceof ResolvedLeafref) {
553             // We should be already inheriting the correct type
554             return;
555         }
556
557         // Note: this may we wrapped for leaf-list, hence we need to deal with that
558         final Type myType = methodReturnType(builderFactory);
559         LOG.trace("Override of {} to {}", this, myType);
560         final MethodSignatureBuilder getter = constructGetter(builder, myType);
561         getter.addAnnotation(OVERRIDE_ANNOTATION);
562         annotateDeprecatedIfNecessary(getter);
563     }
564
565     final @Nullable Restrictions computeRestrictions() {
566         final List<ValueRange> length = type.findFirstEffectiveSubstatementArgument(LengthEffectiveStatement.class)
567             .orElse(List.of());
568         final List<ValueRange> range = type.findFirstEffectiveSubstatementArgument(RangeEffectiveStatement.class)
569             .orElse(List.of());
570         final List<PatternExpression> patterns = type.streamEffectiveSubstatements(PatternEffectiveStatement.class)
571             .map(PatternEffectiveStatement::argument)
572             .collect(Collectors.toUnmodifiableList());
573
574         if (length.isEmpty() && range.isEmpty() && patterns.isEmpty()) {
575             return null;
576         }
577
578         return BindingGeneratorUtil.getRestrictions(extractTypeDefinition());
579     }
580
581     @Override
582     final GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
583         if (baseGen != null) {
584             final GeneratedType baseType = baseGen.getGeneratedType(builderFactory);
585             verify(baseType instanceof GeneratedTransferObject, "Unexpected base type %s", baseType);
586             return createDerivedType(builderFactory, (GeneratedTransferObject) baseType);
587         }
588
589         // FIXME: why do we need this boolean?
590         final boolean isTypedef = this instanceof TypedefGenerator;
591         final QName arg = type.argument();
592         if (TypeDefinitions.BITS.equals(arg)) {
593             return createBits(builderFactory, typeName(), currentModule(), extractTypeDefinition(), isTypedef);
594         } else if (TypeDefinitions.ENUMERATION.equals(arg)) {
595             return createEnumeration(builderFactory, typeName(), currentModule(),
596                 (EnumTypeDefinition) extractTypeDefinition());
597         } else if (TypeDefinitions.UNION.equals(arg)) {
598             final List<GeneratedType> tmp = new ArrayList<>(1);
599             final GeneratedTransferObject ret = createUnion(tmp, builderFactory, statement(), unionDependencies,
600                 typeName(), currentModule(), type, isTypedef, extractTypeDefinition());
601             auxiliaryGeneratedTypes = List.copyOf(tmp);
602             return ret;
603         } else {
604             return createSimple(builderFactory, typeName(), currentModule(),
605                 verifyNotNull(SIMPLE_TYPES.get(arg), "Unhandled type %s", arg), extractTypeDefinition());
606         }
607     }
608
609     private static @NonNull GeneratedTransferObject createBits(final TypeBuilderFactory builderFactory,
610             final JavaTypeName typeName, final ModuleGenerator module, final TypeDefinition<?> typedef,
611             final boolean isTypedef) {
612         final GeneratedTOBuilder builder = builderFactory.newGeneratedTOBuilder(typeName);
613         builder.setTypedef(isTypedef);
614         builder.addImplementsType(BindingTypes.TYPE_OBJECT);
615         builder.setBaseType(typedef);
616
617         for (Bit bit : ((BitsTypeDefinition) typedef).getBits()) {
618             final String name = bit.getName();
619             GeneratedPropertyBuilder genPropertyBuilder = builder.addProperty(BindingMapping.getPropertyName(name));
620             genPropertyBuilder.setReadOnly(true);
621             genPropertyBuilder.setReturnType(BaseYangTypes.BOOLEAN_TYPE);
622
623             builder.addEqualsIdentity(genPropertyBuilder);
624             builder.addHashIdentity(genPropertyBuilder);
625             builder.addToStringProperty(genPropertyBuilder);
626         }
627
628         // builder.setSchemaPath(typedef.getPath());
629         builder.setModuleName(module.statement().argument().getLocalName());
630         builderFactory.addCodegenInformation(typedef, builder);
631         annotateDeprecatedIfNecessary(typedef, builder);
632         makeSerializable(builder);
633         return builder.build();
634     }
635
636     private static @NonNull Enumeration createEnumeration(final TypeBuilderFactory builderFactory,
637             final JavaTypeName typeName, final ModuleGenerator module, final EnumTypeDefinition typedef) {
638         // TODO units for typedef enum
639         final AbstractEnumerationBuilder builder = builderFactory.newEnumerationBuilder(typeName);
640
641         typedef.getDescription().map(BindingGeneratorUtil::encodeAngleBrackets)
642             .ifPresent(builder::setDescription);
643         typedef.getReference().ifPresent(builder::setReference);
644
645         builder.setModuleName(module.statement().argument().getLocalName());
646         builder.updateEnumPairsFromEnumTypeDef(typedef);
647         return builder.toInstance();
648     }
649
650     private static @NonNull GeneratedType createSimple(final TypeBuilderFactory builderFactory,
651             final JavaTypeName typeName, final ModuleGenerator module, final Type javaType,
652             final TypeDefinition<?> typedef) {
653         final String moduleName = module.statement().argument().getLocalName();
654         final GeneratedTOBuilder builder = builderFactory.newGeneratedTOBuilder(typeName);
655         builder.setTypedef(true);
656         builder.addImplementsType(BindingTypes.scalarTypeObject(javaType));
657
658         final GeneratedPropertyBuilder genPropBuilder = builder.addProperty(TypeConstants.VALUE_PROP);
659         genPropBuilder.setReturnType(javaType);
660         builder.addEqualsIdentity(genPropBuilder);
661         builder.addHashIdentity(genPropBuilder);
662         builder.addToStringProperty(genPropBuilder);
663
664         builder.setRestrictions(BindingGeneratorUtil.getRestrictions(typedef));
665
666 //        builder.setSchemaPath(typedef.getPath());
667         builder.setModuleName(moduleName);
668         builderFactory.addCodegenInformation(typedef, builder);
669
670         annotateDeprecatedIfNecessary(typedef, builder);
671
672         if (javaType instanceof ConcreteType
673             // FIXME: This looks very suspicious: we should by checking for Types.STRING
674             && "String".equals(javaType.getName()) && typedef.getBaseType() != null) {
675             addStringRegExAsConstant(builder, resolveRegExpressions(typedef));
676         }
677         addUnits(builder, typedef);
678
679         makeSerializable(builder);
680         return builder.build();
681     }
682
683     private static @NonNull GeneratedTransferObject createUnion(final List<GeneratedType> auxiliaryGeneratedTypes,
684             final TypeBuilderFactory builderFactory, final EffectiveStatement<?, ?> definingStatement,
685             final UnionDependencies dependencies, final JavaTypeName typeName, final ModuleGenerator module,
686             final TypeEffectiveStatement<?> type, final boolean isTypedef, final TypeDefinition<?> typedef) {
687         final GeneratedUnionBuilder builder = builderFactory.newGeneratedUnionBuilder(typeName);
688         builder.addImplementsType(BindingTypes.TYPE_OBJECT);
689         builder.setIsUnion(true);
690
691 //        builder.setSchemaPath(typedef.getPath());
692         builder.setModuleName(module.statement().argument().getLocalName());
693         builderFactory.addCodegenInformation(definingStatement, builder);
694
695         annotateDeprecatedIfNecessary(definingStatement, builder);
696
697         // Pattern string is the key, XSD regex is the value. The reason for this choice is that the pattern carries
698         // also negation information and hence guarantees uniqueness.
699         final Map<String, String> expressions = new HashMap<>();
700
701         // Linear list of properties generated from subtypes. We need this information for runtime types, as it allows
702         // direct mapping of type to corresponding property -- without having to resort to re-resolving the leafrefs
703         // again.
704         final List<String> typeProperties = new ArrayList<>();
705
706         for (EffectiveStatement<?, ?> stmt : type.effectiveSubstatements()) {
707             if (stmt instanceof TypeEffectiveStatement) {
708                 final TypeEffectiveStatement<?> subType = (TypeEffectiveStatement<?>) stmt;
709                 final QName subName = subType.argument();
710                 final String localName = subName.getLocalName();
711
712                 String propSource = localName;
713                 final Type generatedType;
714                 if (TypeDefinitions.UNION.equals(subName)) {
715                     final JavaTypeName subUnionName = typeName.createEnclosed(
716                         provideAvailableNameForGenTOBuilder(typeName.simpleName()));
717                     final GeneratedTransferObject subUnion = createUnion(auxiliaryGeneratedTypes, builderFactory,
718                         definingStatement, dependencies, subUnionName, module, subType, isTypedef,
719                         subType.getTypeDefinition());
720                     builder.addEnclosingTransferObject(subUnion);
721                     propSource = subUnionName.simpleName();
722                     generatedType = subUnion;
723                 } else if (TypeDefinitions.ENUMERATION.equals(subName)) {
724                     final Enumeration subEnumeration = createEnumeration(builderFactory,
725                         typeName.createEnclosed(BindingMapping.getClassName(localName), "$"), module,
726                         (EnumTypeDefinition) subType.getTypeDefinition());
727                     builder.addEnumeration(subEnumeration);
728                     generatedType = subEnumeration;
729                 } else if (TypeDefinitions.BITS.equals(subName)) {
730                     final GeneratedTransferObject subBits = createBits(builderFactory,
731                         typeName.createEnclosed(BindingMapping.getClassName(localName), "$"), module,
732                         subType.getTypeDefinition(), isTypedef);
733                     builder.addEnclosingTransferObject(subBits);
734                     generatedType = subBits;
735                 } else if (TypeDefinitions.IDENTITYREF.equals(subName)) {
736                     generatedType = verifyNotNull(dependencies.identityTypes.get(stmt),
737                         "Cannot resolve identityref %s in %s", stmt, definingStatement)
738                         .methodReturnType(builderFactory);
739                 } else if (TypeDefinitions.LEAFREF.equals(subName)) {
740                     generatedType = verifyNotNull(dependencies.leafTypes.get(stmt),
741                         "Cannot resolve leafref %s in %s", stmt, definingStatement)
742                         .methodReturnType(builderFactory);
743                 } else {
744                     Type baseType = SIMPLE_TYPES.get(subName);
745                     if (baseType == null) {
746                         // This has to be a reference to a typedef, let's lookup it up and pick up its type
747                         final AbstractTypeObjectGenerator<?, ?> baseGen = verifyNotNull(
748                             dependencies.baseTypes.get(subName), "Cannot resolve base type %s in %s", subName,
749                             definingStatement);
750                         baseType = baseGen.methodReturnType(builderFactory);
751
752                         // FIXME: This is legacy behaviour for leafrefs:
753                         if (baseGen.refType instanceof TypeReference.Leafref) {
754                             // if there already is a compatible property, do not generate a new one
755                             final Type search = baseType;
756
757                             final String matching = builder.getProperties().stream()
758                                 .filter(prop -> search == ((GeneratedPropertyBuilderImpl) prop).getReturnType())
759                                 .findFirst()
760                                 .map(GeneratedPropertyBuilder::getName)
761                                 .orElse(null);
762                             if (matching != null) {
763                                 typeProperties.add(matching);
764                                 continue;
765                             }
766
767                             // ... otherwise generate this weird property name
768                             propSource = BindingMapping.getUnionLeafrefMemberName(builder.getName(),
769                                 baseType.getName());
770                         }
771                     }
772
773                     expressions.putAll(resolveRegExpressions(subType.getTypeDefinition()));
774
775                     generatedType = restrictType(baseType,
776                         BindingGeneratorUtil.getRestrictions(type.getTypeDefinition()), builderFactory);
777                 }
778
779                 final String propName = BindingMapping.getPropertyName(propSource);
780                 typeProperties.add(propName);
781
782                 if (builder.containsProperty(propName)) {
783                     /*
784                      *  FIXME: this is not okay, as we are ignoring multiple base types. For example in the case of:
785                      *
786                      *    type union {
787                      *      type string {
788                      *        length 1..5;
789                      *      }
790                      *      type string {
791                      *        length 8..10;
792                      *      }
793                      *    }
794                      *
795                      *  We are ending up losing the information about 8..10 being an alternative. This is also the case
796                      *  for leafrefs -- we are performing property compression as well (see above). While it is alluring
797                      *  to merge these into 'length 1..5|8..10', that may not be generally feasible.
798                      *
799                      *  We should resort to a counter of conflicting names, i.e. the second string would be mapped to
800                      *  'string1' or similar.
801                      */
802                     continue;
803                 }
804
805                 final GeneratedPropertyBuilder propBuilder = builder
806                     .addProperty(propName)
807                     .setReturnType(generatedType);
808
809                 builder.addEqualsIdentity(propBuilder);
810                 builder.addHashIdentity(propBuilder);
811                 builder.addToStringProperty(propBuilder);
812             }
813         }
814
815         // Record property names if needed
816         builder.setTypePropertyNames(typeProperties);
817
818         addStringRegExAsConstant(builder, expressions);
819         addUnits(builder, typedef);
820
821         makeSerializable(builder);
822         final GeneratedTransferObject ret = builder.build();
823
824         // Define a corresponding union builder. Typedefs are always anchored at a Java package root,
825         // so we are placing the builder alongside the union.
826         final GeneratedTOBuilder unionBuilder = builderFactory.newGeneratedTOBuilder(unionBuilderName(typeName));
827         unionBuilder.setIsUnionBuilder(true);
828         unionBuilder.addMethod("getDefaultInstance")
829             .setAccessModifier(AccessModifier.PUBLIC)
830             .setStatic(true)
831             .setReturnType(ret)
832             .addParameter(Types.STRING, "defaultValue");
833         auxiliaryGeneratedTypes.add(unionBuilder.build());
834
835         return ret;
836     }
837
838     // FIXME: this can be a source of conflicts as we are not guarding against nesting
839     private static @NonNull JavaTypeName unionBuilderName(final JavaTypeName unionName) {
840         final StringBuilder sb = new StringBuilder();
841         for (String part : unionName.localNameComponents()) {
842             sb.append(part);
843         }
844         return JavaTypeName.create(unionName.packageName(), sb.append(BindingMapping.BUILDER_SUFFIX).toString());
845     }
846
847     // FIXME: we should not rely on TypeDefinition
848     abstract @NonNull TypeDefinition<?> extractTypeDefinition();
849
850     abstract @NonNull GeneratedTransferObject createDerivedType(@NonNull TypeBuilderFactory builderFactory,
851         @NonNull GeneratedTransferObject baseType);
852
853     /**
854      * Adds to the {@code genTOBuilder} the constant which contains regular expressions from the {@code expressions}.
855      *
856      * @param genTOBuilder generated TO builder to which are {@code regular expressions} added
857      * @param expressions list of string which represent regular expressions
858      */
859     static void addStringRegExAsConstant(final GeneratedTOBuilder genTOBuilder, final Map<String, String> expressions) {
860         if (!expressions.isEmpty()) {
861             genTOBuilder.addConstant(Types.listTypeFor(BaseYangTypes.STRING_TYPE), TypeConstants.PATTERN_CONSTANT_NAME,
862                 ImmutableMap.copyOf(expressions));
863         }
864     }
865
866     /**
867      * Converts the pattern constraints from {@code typedef} to the list of the strings which represents these
868      * constraints.
869      *
870      * @param typedef extended type in which are the pattern constraints sought
871      * @return list of strings which represents the constraint patterns
872      * @throws IllegalArgumentException if <code>typedef</code> equals null
873      */
874     static Map<String, String> resolveRegExpressions(final TypeDefinition<?> typedef) {
875         return typedef instanceof StringTypeDefinition
876             // TODO: run diff against base ?
877             ? resolveRegExpressions(((StringTypeDefinition) typedef).getPatternConstraints())
878                 : ImmutableMap.of();
879     }
880
881     /**
882      * Converts the pattern constraints to the list of the strings which represents these constraints.
883      *
884      * @param patternConstraints list of pattern constraints
885      * @return list of strings which represents the constraint patterns
886      */
887     private static Map<String, String> resolveRegExpressions(final List<PatternConstraint> patternConstraints) {
888         if (patternConstraints.isEmpty()) {
889             return ImmutableMap.of();
890         }
891
892         final Map<String, String> regExps = Maps.newHashMapWithExpectedSize(patternConstraints.size());
893         for (PatternConstraint patternConstraint : patternConstraints) {
894             String regEx = patternConstraint.getJavaPatternString();
895
896             // The pattern can be inverted
897             final Optional<ModifierKind> optModifier = patternConstraint.getModifier();
898             if (optModifier.isPresent()) {
899                 regEx = applyModifier(optModifier.get(), regEx);
900             }
901
902             regExps.put(regEx, patternConstraint.getRegularExpressionString());
903         }
904
905         return regExps;
906     }
907
908     /**
909      * Returns string which contains the same value as <code>name</code> but integer suffix is incremented by one. If
910      * <code>name</code> contains no number suffix, a new suffix initialized at 1 is added. A suffix is actually
911      * composed of a '$' marker, which is safe, as no YANG identifier can contain '$', and a unsigned decimal integer.
912      *
913      * @param name string with name of augmented node
914      * @return string with the number suffix incremented by one (or 1 is added)
915      */
916     private static String provideAvailableNameForGenTOBuilder(final String name) {
917         final int dollar = name.indexOf('$');
918         if (dollar == -1) {
919             return name + "$1";
920         }
921
922         final int newSuffix = Integer.parseUnsignedInt(name.substring(dollar + 1)) + 1;
923         verify(newSuffix > 0, "Suffix counter overflow");
924         return name.substring(0, dollar + 1) + newSuffix;
925     }
926
927     private static String applyModifier(final ModifierKind modifier, final String pattern) {
928         switch (modifier) {
929             case INVERT_MATCH:
930                 return RegexPatterns.negatePatternString(pattern);
931             default:
932                 LOG.warn("Ignoring unhandled modifier {}", modifier);
933                 return pattern;
934         }
935     }
936 }