3da39a1fd8c309a882935b896468eac231b895ff
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / yang / types / AbstractTypeProvider.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.yang.types;
9
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.mdsal.binding.model.ri.BindingTypes.TYPE_OBJECT;
12
13 import com.google.common.base.CharMatcher;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Strings;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.Iterables;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.Set;
28 import java.util.TreeMap;
29 import org.opendaylight.mdsal.binding.generator.BindingGeneratorUtil;
30 import org.opendaylight.mdsal.binding.generator.impl.reactor.SerialVersionHelper;
31 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
32 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
33 import org.opendaylight.mdsal.binding.model.api.Enumeration;
34 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
35 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
36 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
37 import org.opendaylight.mdsal.binding.model.api.Restrictions;
38 import org.opendaylight.mdsal.binding.model.api.Type;
39 import org.opendaylight.mdsal.binding.model.api.type.builder.EnumBuilder;
40 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder;
41 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
42 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
43 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
44 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
45 import org.opendaylight.mdsal.binding.model.ri.BaseYangTypes;
46 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
47 import org.opendaylight.mdsal.binding.model.ri.TypeConstants;
48 import org.opendaylight.mdsal.binding.model.ri.Types;
49 import org.opendaylight.mdsal.binding.model.ri.generated.type.builder.AbstractEnumerationBuilder;
50 import org.opendaylight.mdsal.binding.model.ri.generated.type.builder.GeneratedPropertyBuilderImpl;
51 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
52 import org.opendaylight.yangtools.yang.common.QName;
53 import org.opendaylight.yangtools.yang.common.Revision;
54 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
55 import org.opendaylight.yangtools.yang.model.api.Module;
56 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
57 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
58 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
59 import org.opendaylight.yangtools.yang.model.api.Status;
60 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
61 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
62 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
63 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
64 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
65 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
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.UnionTypeDefinition;
69 import org.opendaylight.yangtools.yang.model.spi.ModuleDependencySort;
70
71 // FIXME: remove this class
72 @Deprecated(forRemoval = true)
73 abstract class AbstractTypeProvider {
74     private static final JavaTypeName DEPRECATED_ANNOTATION = JavaTypeName.create(Deprecated.class);
75     private static final CharMatcher DASH_COLON_MATCHER = CharMatcher.anyOf("-:");
76
77     /**
78      * Contains the schema data red from YANG files.
79      */
80     private final SchemaContext schemaContext;
81
82     private final Map<String, Map<Optional<Revision>, Map<String, GeneratedType>>> genTypeDefsContextMap =
83         new HashMap<>();
84     private final Map<Module, Set<GeneratedType>> additionalTypes = new HashMap<>();
85
86     /**
87      * Creates new instance of class <code>TypeProviderImpl</code>.
88      *
89      * @param schemaContext contains the schema data red from YANG files
90      * @param renames renaming table
91      * @throws IllegalArgumentException if <code>schemaContext</code> equal null.
92      */
93     AbstractTypeProvider(final EffectiveModelContext schemaContext) {
94         this.schemaContext = requireNonNull(schemaContext);
95
96         resolveTypeDefsFromContext();
97     }
98
99     public Map<Module, Set<GeneratedType>> getAdditionalTypes() {
100         return additionalTypes;
101     }
102
103     /**
104      * Resolve of YANG Type Definition to it's java counter part. If the Type Definition contains one of YANG primitive
105      * types the method will return {@code java.lang.} counterpart. (For example if YANG type is int32 the Java
106      * counterpart is {@link Integer}). In case that Type Definition contains extended type defined via YANG typedef
107      * statement the method SHOULD return Generated Type or Generated Transfer Object if that Type is correctly
108      * referenced to resolved imported YANG module.
109      *
110      * <p>
111      * The method will return <code>null</code> value in situations that TypeDefinition can't be resolved (either due
112      * to missing YANG import or incorrectly specified type).
113      *
114      * <p>
115      * {@code leafref} resolution for relative paths has two models of operation: lenient and strict. This is needed to
116      * handle the case where a grouping leaf's path points outside of the grouping tree. In such a case we cannot
117      * completely determine the correct type and need to fallback to {@link Object}.
118      *
119      * @param type Type Definition to resolve from
120      * @param lenientRelativeLeafrefs treat relative leafrefs leniently
121      * @return Resolved Type
122      */
123     public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode) {
124         return javaTypeForSchemaDefinitionType(type, parentNode, null);
125     }
126
127     /**
128      * Converts schema definition type <code>typeDefinition</code> to JAVA <code>Type</code>.
129      *
130      * @param typeDefinition type definition which is converted to JAVA type
131      * @throws IllegalArgumentException
132      *             <ul>
133      *             <li>if <code>typeDefinition</code> equal null</li>
134      *             <li>if Qname of <code>typeDefinition</code> equal null</li>
135      *             <li>if name of <code>typeDefinition</code> equal null</li>
136      *             </ul>
137      */
138     public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
139             final Restrictions restrictions) {
140         throw new UnsupportedOperationException();
141     }
142
143     /**
144      * Converts <code>typeDefinition</code> to concrete JAVA <code>Type</code>.
145      *
146      * @param typeDefinition
147      *            type definition which should be converted to JAVA
148      *            <code>Type</code>
149      * @return JAVA <code>Type</code> which represents
150      *         <code>typeDefinition</code>
151      * @throws IllegalArgumentException
152      *             <ul>
153      *             <li>if <code>typeDefinition</code> equal null</li>
154      *             <li>if Q name of <code>typeDefinition</code></li>
155      *             <li>if name of <code>typeDefinition</code></li>
156      *             </ul>
157      */
158     public GeneratedType generatedTypeForExtendedDefinitionType(final TypeDefinition<?> typeDefinition,
159             final SchemaNode parentNode) {
160         Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
161         if (typeDefinition.getQName() == null) {
162             throw new IllegalArgumentException("Type Definition cannot have unspecified QName (QName cannot be NULL!)");
163         }
164         Preconditions.checkArgument(typeDefinition.getQName().getLocalName() != null,
165                 "Type Definitions Local Name cannot be NULL!");
166
167         final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
168         if (baseTypeDef instanceof LeafrefTypeDefinition || baseTypeDef instanceof IdentityrefTypeDefinition) {
169             /*
170              * This is backwards compatibility baggage from way back when. The problem at hand is inconsistency between
171              * the fact that identity is mapped to a Class, which is also returned from leaves which specify it like
172              * this:
173              *
174              *     identity iden;
175              *
176              *     container foo {
177              *         leaf foo {
178              *             type identityref {
179              *                 base iden;
180              *             }
181              *         }
182              *     }
183              *
184              * This results in getFoo() returning Class<? extends Iden>, which looks fine on the surface, but gets more
185              * dicey when we throw in:
186              *
187              *     typedef bar-ref {
188              *         type identityref {
189              *             base iden;
190              *         }
191              *     }
192              *
193              *     container bar {
194              *         leaf bar {
195              *             type bar-ref;
196              *         }
197              *     }
198              *
199              * Now we have competing requirements: typedef would like us to use encapsulation to capture the defined
200              * type, while getBar() wants us to retain shape with getFoo(), as it should not matter how the identityref
201              * is formed.
202              *
203              * In this particular case getFoo() won just after the Binding Spec was frozen, hence we do not generate
204              * an encapsulation for identityref typedefs.
205              *
206              * In case you are thinking we could get by having foo-ref map to a subclass of Iden, that is not a good
207              * option, as it would look as though it is the product of a different construct:
208              *
209              *     identity bar-ref {
210              *         base iden;
211              *     }
212              *
213              * Leading to a rather nice namespace clash and also slight incompatibility with unknown third-party
214              * sub-identities of iden.
215              *
216              * The story behind leafrefs is probably similar, but that needs to be ascertained.
217              */
218             return null;
219         }
220
221         final Module module = findParentModule(schemaContext, parentNode);
222         if (module != null) {
223             final Map<Optional<Revision>, Map<String, GeneratedType>> modulesByDate = genTypeDefsContextMap.get(
224                 module.getName());
225             final Map<String, GeneratedType> genTOs = modulesByDate.get(module.getRevision());
226             if (genTOs != null) {
227                 return genTOs.get(typeDefinition.getQName().getLocalName());
228             }
229         }
230         return null;
231     }
232
233     /**
234      * Gets base type definition for <code>extendTypeDef</code>. The method is
235      * recursively called until non <code>ExtendedType</code> type is found.
236      *
237      * @param extendTypeDef
238      *            type definition for which is the base type definition sought
239      * @return type definition which is base type for <code>extendTypeDef</code>
240      * @throws IllegalArgumentException
241      *             if <code>extendTypeDef</code> equal null
242      */
243     private static TypeDefinition<?> baseTypeDefForExtendedType(final TypeDefinition<?> extendTypeDef) {
244         Preconditions.checkArgument(extendTypeDef != null, "Type Definition reference cannot be NULL!");
245
246         TypeDefinition<?> ret = extendTypeDef;
247         while (ret.getBaseType() != null) {
248             ret = ret.getBaseType();
249         }
250
251         return ret;
252     }
253
254     /**
255      * Converts <code>enumTypeDef</code> to {@link Enumeration enumeration}.
256      *
257      * @param enumTypeDef enumeration type definition which is converted to enumeration
258      * @param enumName string with name which is used as the enumeration name
259      * @return enumeration type which is built with data (name, enum values) from <code>enumTypeDef</code>
260      * @throws IllegalArgumentException
261      *             <ul>
262      *             <li>if <code>enumTypeDef</code> equals null</li>
263      *             <li>if enum values of <code>enumTypeDef</code> equal null</li>
264      *             <li>if Q name of <code>enumTypeDef</code> equal null</li>
265      *             <li>if name of <code>enumTypeDef</code> equal null</li>
266      *             </ul>
267      */
268     private Enumeration provideTypeForEnum(final EnumTypeDefinition enumTypeDef, final String enumName,
269             final SchemaNode parentNode) {
270         Preconditions.checkArgument(enumTypeDef != null, "EnumTypeDefinition reference cannot be NULL!");
271         Preconditions.checkArgument(enumTypeDef.getValues() != null,
272                 "EnumTypeDefinition MUST contain at least ONE value definition!");
273         Preconditions.checkArgument(enumTypeDef.getQName() != null, "EnumTypeDefinition MUST contain NON-NULL QName!");
274         Preconditions.checkArgument(enumTypeDef.getQName().getLocalName() != null,
275                 "Local Name in EnumTypeDefinition QName cannot be NULL!");
276
277         final Module module = findParentModule(schemaContext, parentNode);
278         final AbstractEnumerationBuilder enumBuilder = newEnumerationBuilder(JavaTypeName.create(
279             BindingMapping.getRootPackageName(module.getQNameModule()), BindingMapping.getClassName(enumName)));
280         addEnumDescription(enumBuilder, enumTypeDef);
281         enumTypeDef.getReference().ifPresent(enumBuilder::setReference);
282         enumBuilder.setModuleName(module.getName());
283         enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
284         return enumBuilder.toInstance();
285     }
286
287     /**
288      * Adds enumeration to <code>typeBuilder</code>. The enumeration data are taken from <code>enumTypeDef</code>.
289      *
290      * @param enumTypeDef enumeration type definition is source of enumeration data for <code>typeBuilder</code>
291      * @param enumName string with the name of enumeration
292      * @param typeBuilder generated type builder to which is enumeration added
293      * @return enumeration type which contains enumeration data form <code>enumTypeDef</code>
294      * @throws IllegalArgumentException
295      *             <ul>
296      *             <li>if <code>enumTypeDef</code> equals null</li>
297      *             <li>if enum values of <code>enumTypeDef</code> equal null</li>
298      *             <li>if Q name of <code>enumTypeDef</code> equal null</li>
299      *             <li>if name of <code>enumTypeDef</code> equal null</li>
300      *             <li>if name of <code>typeBuilder</code> equal null</li>
301      *             </ul>
302      *
303      */
304     private Enumeration addInnerEnumerationToTypeBuilder(final EnumTypeDefinition enumTypeDef,
305             final String enumName, final GeneratedTypeBuilderBase<?> typeBuilder) {
306         Preconditions.checkArgument(enumTypeDef != null, "EnumTypeDefinition reference cannot be NULL!");
307         Preconditions.checkArgument(enumTypeDef.getValues() != null,
308                 "EnumTypeDefinition MUST contain at least ONE value definition!");
309         Preconditions.checkArgument(enumTypeDef.getQName() != null, "EnumTypeDefinition MUST contain NON-NULL QName!");
310         Preconditions.checkArgument(enumTypeDef.getQName().getLocalName() != null,
311                 "Local Name in EnumTypeDefinition QName cannot be NULL!");
312         Preconditions.checkArgument(typeBuilder != null, "Generated Type Builder reference cannot be NULL!");
313
314         final EnumBuilder enumBuilder = newEnumerationBuilder(
315             typeBuilder.getIdentifier().createEnclosed(BindingMapping.getClassName(enumName), "$"));
316         addEnumDescription(enumBuilder, enumTypeDef);
317         enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
318         final Enumeration ret = enumBuilder.toInstance();
319         typeBuilder.addEnumeration(ret);
320
321         return ret;
322     }
323
324     public abstract void addEnumDescription(EnumBuilder enumBuilder, EnumTypeDefinition enumTypeDef);
325
326     public abstract AbstractEnumerationBuilder newEnumerationBuilder(JavaTypeName identifier);
327
328     public abstract GeneratedTOBuilder newGeneratedTOBuilder(JavaTypeName identifier);
329
330     public abstract GeneratedTypeBuilder newGeneratedTypeBuilder(JavaTypeName identifier);
331
332     /**
333      * Converts the pattern constraints to the list of the strings which represents these constraints.
334      *
335      * @param patternConstraints list of pattern constraints
336      * @return list of strings which represents the constraint patterns
337      */
338     public abstract Map<String, String> resolveRegExpressions(List<PatternConstraint> patternConstraints);
339
340     abstract void addCodegenInformation(GeneratedTypeBuilderBase<?> genTOBuilder, TypeDefinition<?> typeDef);
341
342     /**
343      * Converts the pattern constraints from <code>typedef</code> to the list of the strings which represents these
344      * constraints.
345      *
346      * @param typedef extended type in which are the pattern constraints sought
347      * @return list of strings which represents the constraint patterns
348      * @throws IllegalArgumentException if <code>typedef</code> equals null
349      *
350      */
351     private Map<String, String> resolveRegExpressionsFromTypedef(final TypeDefinition<?> typedef) {
352         if (!(typedef instanceof StringTypeDefinition)) {
353             return ImmutableMap.of();
354         }
355
356         // TODO: run diff against base ?
357         return resolveRegExpressions(((StringTypeDefinition) typedef).getPatternConstraints());
358     }
359
360     /**
361      * Passes through all modules and through all its type definitions and convert it to generated types.
362      *
363      * <p>
364      * The modules are first sorted by mutual dependencies. The modules are sequentially passed. All type definitions
365      * of a module are at the beginning sorted so that type definition with less amount of references to other type
366      * definition are processed first.<br>
367      * For each module is created mapping record in the map
368      * {@link AbstractTypeProvider#genTypeDefsContextMap genTypeDefsContextMap}
369      * which map current module name to the map which maps type names to returned types (generated types).
370      */
371     private void resolveTypeDefsFromContext() {
372         final List<Module> modulesSortedByDependency = ModuleDependencySort.sort(schemaContext.getModules());
373
374         for (Module module : modulesSortedByDependency) {
375             Map<Optional<Revision>, Map<String, GeneratedType>> dateTypeMap = genTypeDefsContextMap.computeIfAbsent(
376                 module.getName(), key -> new HashMap<>());
377             dateTypeMap.put(module.getRevision(), Collections.emptyMap());
378             genTypeDefsContextMap.put(module.getName(), dateTypeMap);
379         }
380
381         for (Module module : modulesSortedByDependency) {
382             if (module != null) {
383                 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
384                 if (basePackageName != null) {
385                     final List<TypeDefinition<?>> typeDefinitions = TypedefResolver.getAllTypedefs(module);
386                     for (TypeDefinition<?> typedef : sortTypeDefinitionAccordingDepth(typeDefinitions)) {
387                         typedefToGeneratedType(basePackageName, module, typedef);
388                     }
389                 }
390             }
391         }
392     }
393
394     /**
395      * Create Type for specified type definition.
396      *
397      * @param basePackageName string with name of package to which the module belongs
398      * @param module string with the name of the module for to which the <code>typedef</code> belongs
399      * @param typedef type definition of the node for which should be created JAVA <code>Type</code>
400      *                (usually generated TO)
401      * @return JAVA <code>Type</code> representation of <code>typedef</code> or
402      *         <code>null</code> value if <code>basePackageName</code> or
403      *         <code>modulName</code> or <code>typedef</code> or Q name of
404      *         <code>typedef</code> equals <code>null</code>
405      */
406     private Type typedefToGeneratedType(final String basePackageName, final Module module,
407             final TypeDefinition<?> typedef) {
408         final TypeDefinition<?> baseTypedef = typedef.getBaseType();
409
410         // See generatedTypeForExtendedDefinitionType() above for rationale behind this special case.
411         if (baseTypedef instanceof LeafrefTypeDefinition || baseTypedef instanceof IdentityrefTypeDefinition) {
412             return null;
413         }
414
415         final String typedefName = typedef.getQName().getLocalName();
416
417         final GeneratedType returnType;
418         if (baseTypedef.getBaseType() != null) {
419             returnType = provideGeneratedTOFromExtendedType(typedef, baseTypedef, basePackageName,
420                 module.getName());
421         } else if (baseTypedef instanceof UnionTypeDefinition) {
422             final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForUnionTypeDef(
423                 JavaTypeName.create(basePackageName, BindingMapping.getClassName(typedef.getQName())),
424                 (UnionTypeDefinition) baseTypedef, typedef);
425             genTOBuilder.setTypedef(true);
426             genTOBuilder.setIsUnion(true);
427             addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
428             makeSerializable(genTOBuilder);
429             returnType = genTOBuilder.build();
430
431             // Define a corresponding union builder. Typedefs are always anchored at a Java package root,
432             // so we are placing the builder alongside the union.
433             final GeneratedTOBuilder unionBuilder = newGeneratedTOBuilder(JavaTypeName.create(
434                 genTOBuilder.getPackageName(), genTOBuilder.getName() + BindingMapping.BUILDER_SUFFIX));
435             unionBuilder.setIsUnionBuilder(true);
436             final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
437             method.setReturnType(returnType);
438             method.addParameter(Types.STRING, "defaultValue");
439             method.setAccessModifier(AccessModifier.PUBLIC);
440             method.setStatic(true);
441             additionalTypes.computeIfAbsent(module, key -> new HashSet<>()).add(unionBuilder.build());
442         } else if (baseTypedef instanceof EnumTypeDefinition) {
443             // enums are automatically Serializable
444             final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypedef;
445             // TODO units for typedef enum
446             returnType = provideTypeForEnum(enumTypeDef, typedefName, typedef);
447         } else if (baseTypedef instanceof BitsTypeDefinition) {
448             final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForBitsTypeDefinition(
449                 JavaTypeName.create(basePackageName, BindingMapping.getClassName(typedef.getQName())),
450                 (BitsTypeDefinition) baseTypedef, module.getName());
451             genTOBuilder.setTypedef(true);
452             addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
453             makeSerializable(genTOBuilder);
454             returnType = genTOBuilder.build();
455         } else {
456             final Type javaType = javaTypeForSchemaDefinitionType(baseTypedef, typedef);
457             returnType = wrapJavaTypeIntoTO(basePackageName, typedef, javaType, module.getName());
458         }
459         if (returnType != null) {
460             final Map<Optional<Revision>, Map<String, GeneratedType>> modulesByDate =
461                     genTypeDefsContextMap.get(module.getName());
462             final Optional<Revision> moduleRevision = module.getRevision();
463             Map<String, GeneratedType> typeMap = modulesByDate.get(moduleRevision);
464             if (typeMap != null) {
465                 if (typeMap.isEmpty()) {
466                     typeMap = new HashMap<>(4);
467                     modulesByDate.put(moduleRevision, typeMap);
468                 }
469                 typeMap.put(typedefName, returnType);
470             }
471             return returnType;
472         }
473         return null;
474     }
475
476     /**
477      * Wraps base YANG type to generated TO.
478      *
479      * @param basePackageName string with name of package to which the module belongs
480      * @param typedef type definition which is converted to the TO
481      * @param javaType JAVA <code>Type</code> to which is <code>typedef</code> mapped
482      * @return generated transfer object which represent<code>javaType</code>
483      */
484     private GeneratedTransferObject wrapJavaTypeIntoTO(final String basePackageName, final TypeDefinition<?> typedef,
485             final Type javaType, final String moduleName) {
486         requireNonNull(javaType, "javaType cannot be null");
487
488         final GeneratedTOBuilder genTOBuilder = typedefToTransferObject(basePackageName, typedef, moduleName);
489         genTOBuilder.setRestrictions(BindingGeneratorUtil.getRestrictions(typedef));
490         final GeneratedPropertyBuilder genPropBuilder = genTOBuilder.addProperty(TypeConstants.VALUE_PROP);
491         genPropBuilder.setReturnType(javaType);
492
493         genTOBuilder.addEqualsIdentity(genPropBuilder);
494         genTOBuilder.addHashIdentity(genPropBuilder);
495         genTOBuilder.addToStringProperty(genPropBuilder);
496         genTOBuilder.addImplementsType(BindingTypes.scalarTypeObject(javaType));
497         if (typedef.getStatus() == Status.DEPRECATED) {
498             genTOBuilder.addAnnotation(DEPRECATED_ANNOTATION);
499         }
500         if (javaType instanceof ConcreteType && "String".equals(javaType.getName()) && typedef.getBaseType() != null) {
501             addStringRegExAsConstant(genTOBuilder, resolveRegExpressionsFromTypedef(typedef));
502         }
503         addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
504         genTOBuilder.setTypedef(true);
505         makeSerializable(genTOBuilder);
506         return genTOBuilder.build();
507     }
508
509     /**
510      * Converts output list of generated TO builders to one TO builder (first
511      * from list) which contains the remaining builders as its enclosing TO.
512      *
513      * @param typeName new type identifier
514      * @param typedef type definition which should be of type {@link UnionTypeDefinition}
515      * @return generated TO builder with the list of enclosed generated TO builders
516      */
517     public GeneratedTOBuilder provideGeneratedTOBuilderForUnionTypeDef(final JavaTypeName typeName,
518             final UnionTypeDefinition typedef, final TypeDefinition<?> parentNode) {
519         final List<GeneratedTOBuilder> builders = provideGeneratedTOBuildersForUnionTypeDef(typeName, typedef,
520             parentNode);
521         Preconditions.checkState(!builders.isEmpty(), "No GeneratedTOBuilder objects generated from union %s", typedef);
522
523         final GeneratedTOBuilder resultTOBuilder = builders.remove(0);
524         builders.forEach(builder -> resultTOBuilder.addEnclosingTransferObject(builder.build()));
525         return resultTOBuilder;
526     }
527
528     /**
529      * Converts <code>typedef</code> to generated TO with <code>typeDefName</code>. Every union type from
530      * <code>typedef</code> is added to generated TO builder as property.
531      *
532      * @param typeName new type identifier
533      * @param typedef type definition which should be of type <code>UnionTypeDefinition</code>
534      * @return generated TO builder which represents <code>typedef</code>
535      * @throws NullPointerException
536      *             <ul>
537      *             <li>if <code>basePackageName</code> is null</li>
538      *             <li>if <code>typedef</code> is null</li>
539      *             <li>if Qname of <code>typedef</code> is null</li>
540      *             </ul>
541      */
542     public List<GeneratedTOBuilder> provideGeneratedTOBuildersForUnionTypeDef(final JavaTypeName typeName,
543             final UnionTypeDefinition typedef, final SchemaNode parentNode) {
544         requireNonNull(typedef, "Type Definition cannot be NULL!");
545         requireNonNull(typedef.getQName(), "Type definition QName cannot be NULL!");
546
547         final List<GeneratedTOBuilder> generatedTOBuilders = new ArrayList<>();
548         final List<TypeDefinition<?>> unionTypes = typedef.getTypes();
549         final Module module = findParentModule(schemaContext, parentNode);
550
551         final GeneratedTOBuilder unionGenTOBuilder = newGeneratedTOBuilder(typeName);
552         unionGenTOBuilder.setIsUnion(true);
553         unionGenTOBuilder.setSchemaPath(typedef.getPath());
554         unionGenTOBuilder.setModuleName(module.getName());
555         unionGenTOBuilder.addImplementsType(TYPE_OBJECT);
556         addCodegenInformation(unionGenTOBuilder, typedef);
557         generatedTOBuilders.add(unionGenTOBuilder);
558
559         // Pattern string is the key, XSD regex is the value. The reason for this choice is that the pattern carries
560         // also negation information and hence guarantees uniqueness.
561         final Map<String, String> expressions = new HashMap<>();
562         for (TypeDefinition<?> unionType : unionTypes) {
563             final String unionTypeName = unionType.getQName().getLocalName();
564
565             // If we have a base type we should follow the type definition backwards, except for identityrefs, as those
566             // do not follow type encapsulation -- we use the general case for that.
567             if (unionType.getBaseType() != null  && !(unionType instanceof IdentityrefTypeDefinition)) {
568                 resolveExtendedSubtypeAsUnion(unionGenTOBuilder, unionType, expressions, parentNode);
569             } else if (unionType instanceof UnionTypeDefinition) {
570                 generatedTOBuilders.addAll(resolveUnionSubtypeAsUnion(unionGenTOBuilder,
571                     (UnionTypeDefinition) unionType, parentNode));
572             } else if (unionType instanceof EnumTypeDefinition) {
573                 final Enumeration enumeration = addInnerEnumerationToTypeBuilder((EnumTypeDefinition) unionType,
574                         unionTypeName, unionGenTOBuilder);
575                 updateUnionTypeAsProperty(unionGenTOBuilder, enumeration, unionTypeName);
576             } else {
577                 final Type javaType = javaTypeForSchemaDefinitionType(unionType, parentNode);
578                 updateUnionTypeAsProperty(unionGenTOBuilder, javaType, unionTypeName);
579             }
580         }
581         addStringRegExAsConstant(unionGenTOBuilder, expressions);
582
583         storeGenTO(typedef, unionGenTOBuilder, parentNode);
584
585         return generatedTOBuilders;
586     }
587
588     /**
589      * Wraps code which handles the case when union subtype is also of the type <code>UnionType</code>.
590      *
591      * <p>
592      * In this case the new generated TO is created for union subtype (recursive call of method
593      * {@link #provideGeneratedTOBuildersForUnionTypeDef(String, UnionTypeDefinition, String, SchemaNode)}
594      * provideGeneratedTOBuilderForUnionTypeDef} and in parent TO builder <code>parentUnionGenTOBuilder</code> is
595      * created property which type is equal to new generated TO.
596      *
597      * @param parentUnionGenTOBuilder generated TO builder to which is the property with the child union subtype added
598      * @param basePackageName string with the name of the module package
599      * @param unionSubtype type definition which represents union subtype
600      * @return list of generated TO builders. The number of the builders can be bigger one due to recursive call of
601      *         <code>provideGeneratedTOBuildersForUnionTypeDef</code> method.
602      */
603     private List<GeneratedTOBuilder> resolveUnionSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
604             final UnionTypeDefinition unionSubtype, final SchemaNode parentNode) {
605         final JavaTypeName newTOBuilderName = parentUnionGenTOBuilder.getIdentifier().createSibling(
606             provideAvailableNameForGenTOBuilder(parentUnionGenTOBuilder.getName()));
607         final List<GeneratedTOBuilder> subUnionGenTOBUilders = provideGeneratedTOBuildersForUnionTypeDef(
608             newTOBuilderName, unionSubtype, parentNode);
609
610         final GeneratedPropertyBuilder propertyBuilder;
611         propertyBuilder = parentUnionGenTOBuilder.addProperty(BindingMapping.getPropertyName(
612             newTOBuilderName.simpleName()));
613         propertyBuilder.setReturnType(subUnionGenTOBUilders.get(0).build());
614         parentUnionGenTOBuilder.addEqualsIdentity(propertyBuilder);
615         parentUnionGenTOBuilder.addToStringProperty(propertyBuilder);
616
617         return subUnionGenTOBUilders;
618     }
619
620     /**
621      * Wraps code which handle case when union subtype is of the type <code>ExtendedType</code>. If TO for this type
622      * already exists it is used for the creation of the property in <code>parentUnionGenTOBuilder</code>. Otherwise
623      * the base type is used for the property creation.
624      *
625      * @param parentUnionGenTOBuilder generated TO builder in which new property is created
626      * @param unionSubtype type definition of the <code>ExtendedType</code> type which represents union subtype
627      * @param expressions list of strings with the regular expressions
628      * @param parentNode parent Schema Node for Extended Subtype
629      */
630     private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
631             final TypeDefinition<?> unionSubtype, final Map<String, String> expressions, final SchemaNode parentNode) {
632         final String unionTypeName = unionSubtype.getQName().getLocalName();
633         final Type genTO = findGenTO(unionTypeName, unionSubtype);
634         if (genTO != null) {
635             updateUnionTypeAsProperty(parentUnionGenTOBuilder, genTO, genTO.getName());
636             return;
637         }
638
639         final TypeDefinition<?> baseType = baseTypeDefForExtendedType(unionSubtype);
640         if (unionTypeName.equals(baseType.getQName().getLocalName())) {
641             final Type javaType = baseJavaTypeForSchema(baseType, parentNode,
642                 BindingGeneratorUtil.getRestrictions(unionSubtype));
643             if (javaType != null) {
644                 updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, unionTypeName);
645             }
646         } else if (baseType instanceof LeafrefTypeDefinition) {
647             final Type javaType = javaTypeForSchemaDefinitionType(baseType, parentNode);
648             boolean typeExist = false;
649             for (GeneratedPropertyBuilder generatedPropertyBuilder : parentUnionGenTOBuilder.getProperties()) {
650                 final Type origType = ((GeneratedPropertyBuilderImpl) generatedPropertyBuilder).getReturnType();
651                 if (origType != null && javaType != null && javaType == origType) {
652                     typeExist = true;
653                     break;
654                 }
655             }
656             if (!typeExist && javaType != null) {
657                 updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType,
658                     BindingMapping.getUnionLeafrefMemberName(parentUnionGenTOBuilder.getName(), javaType.getName()));
659             }
660         }
661         if (baseType instanceof StringTypeDefinition) {
662             expressions.putAll(resolveRegExpressionsFromTypedef(unionSubtype));
663         }
664     }
665
666     private static Type baseJavaTypeForSchema(final TypeDefinition<?> type, final SchemaNode parentNode,
667             final Restrictions restrictions) {
668         final String typeName = type.getQName().getLocalName();
669         final Type mapped = BaseYangTypes.javaTypeForYangType(typeName);
670         return mapped == null || restrictions == null ? mapped : Types.restrictedType(mapped, restrictions);
671     }
672
673     /**
674      * Searches for generated TO for <code>searchedTypeDef</code> type  definition
675      * in {@link #genTypeDefsContextMap genTypeDefsContextMap}.
676      *
677      * @param searchedTypeName string with name of <code>searchedTypeDef</code>
678      * @return generated TO for <code>searchedTypeDef</code> or <code>null</code> it it doesn't exist
679      */
680     private Type findGenTO(final String searchedTypeName, final SchemaNode parentNode) {
681         final Module typeModule = findParentModule(schemaContext, parentNode);
682         if (typeModule != null && typeModule.getName() != null) {
683             final Map<Optional<Revision>, Map<String, GeneratedType>> modulesByDate = genTypeDefsContextMap.get(
684                 typeModule.getName());
685             final Map<String, GeneratedType> genTOs = modulesByDate.get(typeModule.getRevision());
686             if (genTOs != null) {
687                 return genTOs.get(searchedTypeName);
688             }
689         }
690         return null;
691     }
692
693     /**
694      * Stores generated TO created from <code>genTOBuilder</code> for <code>newTypeDef</code>
695      * to {@link #genTypeDefsContextMap genTypeDefsContextMap} if the module for <code>newTypeDef</code> exists.
696      *
697      * @param newTypeDef type definition for which is <code>genTOBuilder</code> created
698      * @param genTOBuilder generated TO builder which is converted to generated TO and stored
699      */
700     private void storeGenTO(final TypeDefinition<?> newTypeDef, final GeneratedTOBuilder genTOBuilder,
701             final SchemaNode parentNode) {
702         if (!(newTypeDef instanceof UnionTypeDefinition)) {
703             final Module parentModule = findParentModule(schemaContext, parentNode);
704             if (parentModule != null && parentModule.getName() != null) {
705                 final Map<Optional<Revision>, Map<String, GeneratedType>> modulesByDate = genTypeDefsContextMap.get(
706                     parentModule.getName());
707                 final Map<String, GeneratedType> genTOsMap = modulesByDate.get(parentModule.getRevision());
708                 genTOsMap.put(newTypeDef.getQName().getLocalName(), genTOBuilder.build());
709             }
710         }
711     }
712
713     /**
714      * Adds a new property with the name <code>propertyName</code> and with type <code>type</code>
715      * to <code>unonGenTransObject</code>.
716      *
717      * @param unionGenTransObject generated TO to which should be property added
718      * @param type JAVA <code>type</code> of the property which should be added to <code>unionGentransObject</code>
719      * @param propertyName string with name of property which should be added to <code>unionGentransObject</code>
720      */
721     private static void updateUnionTypeAsProperty(final GeneratedTOBuilder unionGenTransObject, final Type type,
722             final String propertyName) {
723         if (unionGenTransObject != null && type != null && !unionGenTransObject.containsProperty(propertyName)) {
724             final GeneratedPropertyBuilder propBuilder = unionGenTransObject
725                     .addProperty(BindingMapping.getPropertyName(propertyName));
726             propBuilder.setReturnType(type);
727
728             unionGenTransObject.addEqualsIdentity(propBuilder);
729             unionGenTransObject.addHashIdentity(propBuilder);
730             unionGenTransObject.addToStringProperty(propBuilder);
731         }
732     }
733
734     /**
735      * Converts <code>typedef</code> to the generated TO builder.
736      *
737      * @param basePackageName string with name of package to which the module belongs
738      * @param typedef type definition from which is the generated TO builder created
739      * @return generated TO builder which contains data from <code>typedef</code> and <code>basePackageName</code>
740      */
741     private GeneratedTOBuilder typedefToTransferObject(final String basePackageName,
742             final TypeDefinition<?> typedef, final String moduleName) {
743         final JavaTypeName name = JavaTypeName.create(
744                 packageNameForGeneratedType(basePackageName, typedef.getPath()),
745                 BindingMapping.getClassName(typedef.getQName().getLocalName()));
746
747         final GeneratedTOBuilder newType = newGeneratedTOBuilder(name);
748         newType.setSchemaPath(typedef.getPath());
749         newType.setModuleName(moduleName);
750         addCodegenInformation(newType, typedef);
751         return newType;
752     }
753
754     /**
755      * Creates package name from specified <code>basePackageName</code> (package name for module)
756      * and <code>schemaPath</code>. Resulting package name is concatenation of <code>basePackageName</code>
757      * and all local names of YANG nodes which are parents of some node for which <code>schemaPath</code> is specified.
758      *
759      * @param basePackageName string with package name of the module, MUST be normalized, otherwise this method may
760      *                        return an invalid string.
761      * @param schemaPath list of names of YANG nodes which are parents of some node + name of this node
762      * @return string with valid JAVA package name
763      * @throws NullPointerException if any of the arguments are null
764      */
765     private static String packageNameForGeneratedType(final String basePackageName, final SchemaPath schemaPath) {
766         final int size = Iterables.size(schemaPath.getPathTowardsRoot()) - 1;
767         if (size <= 0) {
768             return basePackageName;
769         }
770
771         return generateNormalizedPackageName(basePackageName, schemaPath.getPathFromRoot(), size);
772     }
773
774     private static String generateNormalizedPackageName(final String base, final Iterable<QName> path, final int size) {
775         final StringBuilder builder = new StringBuilder(base);
776         final Iterator<QName> iterator = path.iterator();
777         for (int i = 0; i < size; ++i) {
778             builder.append('.');
779             final String nodeLocalName = iterator.next().getLocalName();
780             // FIXME: Collon ":" is invalid in node local name as per RFC6020, identifier statement.
781             builder.append(DASH_COLON_MATCHER.replaceFrom(nodeLocalName, '.'));
782         }
783         return BindingMapping.normalizePackageName(builder.toString());
784     }
785
786
787     /**
788      * Converts <code>typeDef</code> which should be of the type <code>BitsTypeDefinition</code>
789      * to <code>GeneratedTOBuilder</code>. All the bits of the typeDef are added to returning generated TO as
790      * properties.
791      *
792      * @param typeName new type identifier
793      * @param typeDef type definition from which is the generated TO builder created
794      * @return generated TO builder which represents <code>typeDef</code>
795      * @throws IllegalArgumentException
796      *             <ul>
797      *             <li>if <code>typeDef</code> equals null</li>
798      *             <li>if <code>basePackageName</code> equals null</li>
799      *             </ul>
800      */
801     public GeneratedTOBuilder provideGeneratedTOBuilderForBitsTypeDefinition(final JavaTypeName typeName,
802             final BitsTypeDefinition typeDef, final String moduleName) {
803         final GeneratedTOBuilder genTOBuilder = newGeneratedTOBuilder(typeName);
804         genTOBuilder.setSchemaPath(typeDef.getPath());
805         genTOBuilder.setModuleName(moduleName);
806         genTOBuilder.setBaseType(typeDef);
807         genTOBuilder.addImplementsType(TYPE_OBJECT);
808         addCodegenInformation(genTOBuilder, typeDef);
809
810         for (Bit bit : typeDef.getBits()) {
811             final String name = bit.getName();
812             GeneratedPropertyBuilder genPropertyBuilder = genTOBuilder.addProperty(
813                 BindingMapping.getPropertyName(name));
814             genPropertyBuilder.setReadOnly(true);
815             genPropertyBuilder.setReturnType(BaseYangTypes.BOOLEAN_TYPE);
816
817             genTOBuilder.addEqualsIdentity(genPropertyBuilder);
818             genTOBuilder.addHashIdentity(genPropertyBuilder);
819             genTOBuilder.addToStringProperty(genPropertyBuilder);
820         }
821
822         return genTOBuilder;
823     }
824
825     /**
826      * Adds to the <code>genTOBuilder</code> the constant which contains regular expressions from
827      * the <code>regularExpressions</code>.
828      *
829      * @param genTOBuilder generated TO builder to which are <code>regular expressions</code> added
830      * @param expressions list of string which represent regular expressions
831      */
832     private static void addStringRegExAsConstant(final GeneratedTOBuilder genTOBuilder,
833             final Map<String, String> expressions) {
834         if (!expressions.isEmpty()) {
835             genTOBuilder.addConstant(Types.listTypeFor(BaseYangTypes.STRING_TYPE), TypeConstants.PATTERN_CONSTANT_NAME,
836                 ImmutableMap.copyOf(expressions));
837         }
838     }
839
840     /**
841      * Creates generated TO with data about inner extended type <code>innerExtendedType</code>, about the package name
842      * <code>typedefName</code> and about the generated TO name <code>typedefName</code>.
843      *
844      * <p>
845      * It is assumed that <code>innerExtendedType</code> is already present in
846      * {@link AbstractTypeProvider#genTypeDefsContextMap genTypeDefsContextMap} to be possible set it as extended type
847      * for the returning generated TO.
848      *
849      * @param typedef Type Definition
850      * @param innerExtendedType extended type which is part of some other extended type
851      * @param basePackageName string with the package name of the module
852      * @param moduleName Module Name
853      * @return generated TO which extends generated TO for <code>innerExtendedType</code>
854      * @throws IllegalArgumentException
855      *             <ul>
856      *             <li>if <code>extendedType</code> equals null</li>
857      *             <li>if <code>basePackageName</code> equals null</li>
858      *             <li>if <code>typedefName</code> equals null</li>
859      *             </ul>
860      */
861     private GeneratedTransferObject provideGeneratedTOFromExtendedType(final TypeDefinition<?> typedef,
862             final TypeDefinition<?> innerExtendedType, final String basePackageName, final String moduleName) {
863         Preconditions.checkArgument(innerExtendedType != null, "Extended type cannot be NULL!");
864         Preconditions.checkArgument(basePackageName != null, "String with base package name cannot be NULL!");
865
866         final GeneratedTOBuilder genTOBuilder = newGeneratedTOBuilder(JavaTypeName.create(basePackageName,
867             BindingMapping.getClassName(typedef.getQName())));
868         genTOBuilder.setSchemaPath(typedef.getPath());
869         genTOBuilder.setModuleName(moduleName);
870         genTOBuilder.setTypedef(true);
871         addCodegenInformation(genTOBuilder, typedef);
872
873         final Restrictions r = BindingGeneratorUtil.getRestrictions(typedef);
874         genTOBuilder.setRestrictions(r);
875         addStringRegExAsConstant(genTOBuilder, resolveRegExpressionsFromTypedef(typedef));
876
877         if (typedef.getStatus() == Status.DEPRECATED) {
878             genTOBuilder.addAnnotation(DEPRECATED_ANNOTATION);
879         }
880
881         if (baseTypeDefForExtendedType(innerExtendedType) instanceof UnionTypeDefinition) {
882             genTOBuilder.setIsUnion(true);
883         }
884
885         Map<Optional<Revision>, Map<String, GeneratedType>> modulesByDate = null;
886         Map<String, GeneratedType> typeMap = null;
887         final Module parentModule = findParentModule(schemaContext, innerExtendedType);
888         if (parentModule != null) {
889             modulesByDate = genTypeDefsContextMap.get(parentModule.getName());
890             typeMap = modulesByDate.get(parentModule.getRevision());
891         }
892
893         if (typeMap != null) {
894             final String innerTypeDef = innerExtendedType.getQName().getLocalName();
895             final GeneratedType type = typeMap.get(innerTypeDef);
896             if (type instanceof GeneratedTransferObject) {
897                 genTOBuilder.setExtendsType((GeneratedTransferObject) type);
898             }
899         }
900         addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
901         makeSerializable(genTOBuilder);
902
903         return genTOBuilder.build();
904     }
905
906     /**
907      * Add {@link java.io.Serializable} to implemented interfaces of this TO. Also compute and add serialVersionUID
908      * property.
909      *
910      * @param gto transfer object which needs to be made serializable
911      */
912     private static void makeSerializable(final GeneratedTOBuilder gto) {
913         gto.addImplementsType(Types.serializableType());
914         final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
915         prop.setValue(Long.toString(SerialVersionHelper.computeDefaultSUID(gto)));
916         gto.setSUID(prop);
917     }
918
919     /**
920      * Finds out for each type definition how many immersion (depth) is necessary to get to the base type. Every type
921      * definition is inserted to the map which key is depth and value is list of type definitions with equal depth.
922      * In next step are lists from this map concatenated to one list in ascending order according to their depth. All
923      * type definitions are in the list behind all type definitions on which depends.
924      *
925      * @param unsortedTypeDefinitions list of type definitions which should be sorted by depth
926      * @return list of type definitions sorted according their each other dependencies (type definitions which are
927      *              dependent on other type definitions are in list behind them).
928      */
929     private static List<TypeDefinition<?>> sortTypeDefinitionAccordingDepth(
930             final Collection<TypeDefinition<?>> unsortedTypeDefinitions) {
931         final List<TypeDefinition<?>> sortedTypeDefinition = new ArrayList<>();
932
933         final Map<Integer, List<TypeDefinition<?>>> typeDefinitionsDepths = new TreeMap<>();
934         for (TypeDefinition<?> unsortedTypeDefinition : unsortedTypeDefinitions) {
935             final Integer depth = getTypeDefinitionDepth(unsortedTypeDefinition);
936             List<TypeDefinition<?>> typeDefinitionsConcreteDepth =
937                 typeDefinitionsDepths.computeIfAbsent(depth, k -> new ArrayList<>());
938             typeDefinitionsConcreteDepth.add(unsortedTypeDefinition);
939         }
940
941         // SortedMap guarantees order corresponding to keys in ascending order
942         for (List<TypeDefinition<?>> v : typeDefinitionsDepths.values()) {
943             sortedTypeDefinition.addAll(v);
944         }
945
946         return sortedTypeDefinition;
947     }
948
949     /**
950      * Returns how many immersion is necessary to get from the type definition to the base type.
951      *
952      * @param typeDefinition type definition for which is depth sought.
953      * @return number of immersions which are necessary to get from the type definition to the base type
954      */
955     private static int getTypeDefinitionDepth(final TypeDefinition<?> typeDefinition) {
956         // FIXME: rewrite this in a non-recursive manner
957         if (typeDefinition == null) {
958             return 1;
959         }
960         final TypeDefinition<?> baseType = typeDefinition.getBaseType();
961         if (baseType == null) {
962             return 1;
963         }
964
965         int depth = 1;
966         if (baseType.getBaseType() != null) {
967             depth = depth + getTypeDefinitionDepth(baseType);
968         } else if (baseType instanceof UnionTypeDefinition) {
969             final List<TypeDefinition<?>> childTypeDefinitions = ((UnionTypeDefinition) baseType).getTypes();
970             int maxChildDepth = 0;
971             int childDepth = 1;
972             for (TypeDefinition<?> childTypeDefinition : childTypeDefinitions) {
973                 childDepth = childDepth + getTypeDefinitionDepth(childTypeDefinition);
974                 if (childDepth > maxChildDepth) {
975                     maxChildDepth = childDepth;
976                 }
977             }
978             return maxChildDepth;
979         }
980         return depth;
981     }
982
983     /**
984      * Returns string which contains the same value as <code>name</code> but integer suffix is incremented by one. If
985      * <code>name</code> contains no number suffix, a new suffix initialized at 1 is added. A suffix is actually
986      * composed of a '$' marker, which is safe, as no YANG identifier can contain '$', and a unsigned decimal integer.
987      *
988      * @param name string with name of augmented node
989      * @return string with the number suffix incremented by one (or 1 is added)
990      */
991     private static String provideAvailableNameForGenTOBuilder(final String name) {
992         final int dollar = name.indexOf('$');
993         if (dollar == -1) {
994             return name + "$1";
995         }
996
997         final int newSuffix = Integer.parseUnsignedInt(name.substring(dollar + 1)) + 1;
998         Preconditions.checkState(newSuffix > 0, "Suffix counter overflow");
999         return name.substring(0, dollar + 1) + newSuffix;
1000     }
1001
1002     public static void addUnitsToGenTO(final GeneratedTOBuilder to, final String units) {
1003         if (!Strings.isNullOrEmpty(units)) {
1004             to.addConstant(Types.STRING, "_UNITS", "\"" + units + "\"");
1005             final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("UNITS");
1006             prop.setReturnType(Types.STRING);
1007             to.addToStringProperty(prop);
1008         }
1009     }
1010
1011     /**
1012      * Returns parent Yang Module for specified Schema Context in which Schema
1013      * Node is declared. If the Schema Node is not present in Schema Context the
1014      * operation will return <code>null</code>.
1015      *
1016      * @param context Schema Context
1017      * @param schemaNode Schema Node
1018      * @return Yang Module for specified Schema Context and Schema Node, if Schema Node is NOT present, the method will
1019      *         return <code>null</code>
1020      * @throws NullPointerException if any of the arguments is null
1021      */
1022     private static Module findParentModule(final SchemaContext context, final SchemaNode schemaNode) {
1023         return context.findModule(schemaNode.getQName().getModule()).orElse(null);
1024     }
1025 }