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