Tolerate unresolvable leafrefs in groupings
[mdsal.git] / binding / mdsal-binding-generator-impl / 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 com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
13 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNodeForRelativeXPath;
14 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
15
16 import com.google.common.annotations.Beta;
17 import com.google.common.base.Preconditions;
18 import com.google.common.base.Strings;
19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.collect.Sets;
21 import com.google.common.io.BaseEncoding;
22 import java.io.Serializable;
23 import java.math.BigDecimal;
24 import java.math.BigInteger;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Optional;
34 import java.util.Set;
35 import java.util.TreeMap;
36 import java.util.regex.Pattern;
37 import org.opendaylight.mdsal.binding.generator.spi.TypeProvider;
38 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
39 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
40 import org.opendaylight.mdsal.binding.model.api.Enumeration;
41 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
42 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
43 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
44 import org.opendaylight.mdsal.binding.model.api.Restrictions;
45 import org.opendaylight.mdsal.binding.model.api.Type;
46 import org.opendaylight.mdsal.binding.model.api.type.builder.EnumBuilder;
47 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder;
48 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
49 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
50 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
51 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
52 import org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil;
53 import org.opendaylight.mdsal.binding.model.util.TypeConstants;
54 import org.opendaylight.mdsal.binding.model.util.Types;
55 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.AbstractEnumerationBuilder;
56 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.GeneratedPropertyBuilderImpl;
57 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
58 import org.opendaylight.yangtools.yang.common.QName;
59 import org.opendaylight.yangtools.yang.common.Revision;
60 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
61 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
62 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
63 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
64 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
65 import org.opendaylight.yangtools.yang.model.api.Module;
66 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
67 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
68 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
69 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
70 import org.opendaylight.yangtools.yang.model.api.Status;
71 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
72 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
73 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
74 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
75 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
76 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
77 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
78 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
79 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
80 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
81 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
82 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
83 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
84 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
85 import org.opendaylight.yangtools.yang.model.util.ModuleDependencySort;
86 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
87 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
88 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
89 import org.opendaylight.yangtools.yang.model.util.type.CompatUtils;
90 import org.slf4j.Logger;
91 import org.slf4j.LoggerFactory;
92
93 @Beta
94 public abstract class AbstractTypeProvider implements TypeProvider {
95     private static final Logger LOG = LoggerFactory.getLogger(AbstractTypeProvider.class);
96     private static final Pattern GROUPS_PATTERN = Pattern.compile("\\[(.*?)\\]");
97
98     // Backwards compatibility: Union types used to be instantiated in YANG namespace, which is no longer
99     // the case, as unions are emitted to their correct schema path.
100     private static final SchemaPath UNION_PATH = SchemaPath.create(true,
101         org.opendaylight.yangtools.yang.model.util.BaseTypes.UNION_QNAME);
102
103     /**
104      * Contains the schema data red from YANG files.
105      */
106     private final SchemaContext schemaContext;
107
108     /**
109      * Map<moduleName, Map<moduleDate, Map<typeName, type>>>
110      */
111     private final Map<String, Map<Optional<Revision>, Map<String, Type>>> genTypeDefsContextMap = new HashMap<>();
112
113     /**
114      * The map which maps schema paths to JAVA <code>Type</code>.
115      */
116     private final Map<SchemaPath, Type> referencedTypes = new HashMap<>();
117     private final Map<Module, Set<Type>> additionalTypes = new HashMap<>();
118     private final Map<SchemaNode, JavaTypeName> renames;
119
120     /**
121      * Creates new instance of class <code>TypeProviderImpl</code>.
122      *
123      * @param schemaContext
124      *            contains the schema data red from YANG files
125      * @param renames
126      * @throws IllegalArgumentException
127      *             if <code>schemaContext</code> equal null.
128      */
129     AbstractTypeProvider(final SchemaContext schemaContext, final Map<SchemaNode, JavaTypeName> renames) {
130         Preconditions.checkArgument(schemaContext != null, "Schema Context cannot be null!");
131
132         this.schemaContext = schemaContext;
133         this.renames = requireNonNull(renames);
134         resolveTypeDefsFromContext();
135     }
136
137     /**
138      * Puts <code>refType</code> to map with key <code>refTypePath</code>
139      *
140      * @param refTypePath
141      *            schema path used as the map key
142      * @param refType
143      *            type which represents the map value
144      * @throws IllegalArgumentException
145      *             <ul>
146      *             <li>if <code>refTypePath</code> equal null</li>
147      *             <li>if <code>refType</code> equal null</li>
148      *             </ul>
149      *
150      */
151     public void putReferencedType(final SchemaPath refTypePath, final Type refType) {
152         Preconditions.checkArgument(refTypePath != null,
153                 "Path reference of Enumeration Type Definition cannot be NULL!");
154         Preconditions.checkArgument(refType != null, "Reference to Enumeration Type cannot be NULL!");
155         referencedTypes.put(refTypePath, refType);
156     }
157
158     public Map<Module, Set<Type>> getAdditionalTypes() {
159         return additionalTypes;
160     }
161
162     /**
163      *
164      * Converts basic YANG type <code>type</code> to JAVA <code>Type</code>.
165      *
166      * @param type
167      *            string with YANG name of type
168      * @return JAVA <code>Type</code> for YANG type <code>type</code>
169      * @see TypeProvider#javaTypeForYangType(String)
170      */
171     @Override
172     @Deprecated
173     public Type javaTypeForYangType(final String type) {
174         return BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForYangType(type);
175     }
176
177     @Override
178     public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
179             final boolean lenientRelativeLeafrefs) {
180         return javaTypeForSchemaDefinitionType(typeDefinition, parentNode, null, lenientRelativeLeafrefs);
181     }
182
183     /**
184      * Converts schema definition type <code>typeDefinition</code> to JAVA
185      * <code>Type</code>
186      *
187      * @param typeDefinition
188      *            type definition which is converted to JAVA type
189      * @throws IllegalArgumentException
190      *             <ul>
191      *             <li>if <code>typeDefinition</code> equal null</li>
192      *             <li>if Qname of <code>typeDefinition</code> equal null</li>
193      *             <li>if name of <code>typeDefinition</code> equal null</li>
194      *             </ul>
195      */
196     @Override
197     public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
198             final Restrictions r, final boolean lenientRelativeLeafrefs) {
199         Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
200         Preconditions.checkArgument(typeDefinition.getQName() != null,
201                 "Type Definition cannot have non specified QName (QName cannot be NULL!)");
202         final String typedefName = typeDefinition.getQName().getLocalName();
203         Preconditions.checkArgument(typedefName != null, "Type Definitions Local Name cannot be NULL!");
204
205         // Deal with base types
206         if (typeDefinition.getBaseType() == null) {
207             // We have to deal with differing handling of decimal64. The old parser used a fixed Decimal64 type
208             // and generated an enclosing ExtendedType to hold any range constraints. The new parser instantiates
209             // a base type which holds these constraints.
210             if (typeDefinition instanceof DecimalTypeDefinition) {
211                 final Type ret = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(typeDefinition,
212                     parentNode, r, lenientRelativeLeafrefs);
213                 if (ret != null) {
214                     return ret;
215                 }
216             }
217
218             // Deal with leafrefs/identityrefs
219             Type ret = javaTypeForLeafrefOrIdentityRef(typeDefinition, parentNode, lenientRelativeLeafrefs);
220             if (ret != null) {
221                 return ret;
222             }
223
224             // FIXME: it looks as though we could be using the same codepath as above...
225             ret = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForYangType(typeDefinition.getQName().getLocalName());
226             if (ret == null) {
227                 LOG.debug("Failed to resolve Java type for {}", typeDefinition);
228             }
229
230             return ret;
231         }
232
233         Type returnType = javaTypeForExtendedType(typeDefinition, lenientRelativeLeafrefs);
234         if (r != null && returnType instanceof GeneratedTransferObject) {
235             final GeneratedTransferObject gto = (GeneratedTransferObject) returnType;
236             final Module module = findParentModule(schemaContext, parentNode);
237             final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
238             final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
239                 typeDefinition.getPath());
240             final String genTOName = BindingMapping.getClassName(typedefName);
241             final String name = packageName + "." + genTOName;
242             if (!returnType.getFullyQualifiedName().equals(name)) {
243                 returnType = shadedTOWithRestrictions(gto, r);
244             }
245         }
246         return returnType;
247     }
248
249     private GeneratedTransferObject shadedTOWithRestrictions(final GeneratedTransferObject gto, final Restrictions r) {
250         final GeneratedTOBuilder gtob = newGeneratedTOBuilder(gto.getIdentifier());
251         final GeneratedTransferObject parent = gto.getSuperType();
252         if (parent != null) {
253             gtob.setExtendsType(parent);
254         }
255         gtob.setRestrictions(r);
256         for (GeneratedProperty gp : gto.getProperties()) {
257             final GeneratedPropertyBuilder gpb = gtob.addProperty(gp.getName());
258             gpb.setValue(gp.getValue());
259             gpb.setReadOnly(gp.isReadOnly());
260             gpb.setAccessModifier(gp.getAccessModifier());
261             gpb.setReturnType(gp.getReturnType());
262             gpb.setFinal(gp.isFinal());
263             gpb.setStatic(gp.isStatic());
264         }
265         return gtob.build();
266     }
267
268     private boolean isLeafRefSelfReference(final LeafrefTypeDefinition leafref, final SchemaNode parentNode) {
269         final SchemaNode leafRefValueNode;
270         final RevisionAwareXPath leafRefXPath = leafref.getPathStatement();
271         final RevisionAwareXPath leafRefStrippedXPath = new RevisionAwareXPathImpl(
272             GROUPS_PATTERN.matcher(leafRefXPath.toString()).replaceAll(""), leafRefXPath.isAbsolute());
273
274         ///// skip leafrefs in augments - they're checked once augments are resolved
275         final Iterator<QName> iterator = parentNode.getPath().getPathFromRoot().iterator();
276         boolean isAugmenting = false;
277         DataNodeContainer current = null;
278         DataSchemaNode dataChildByName;
279
280         while (iterator.hasNext() && !isAugmenting) {
281             final QName next = iterator.next();
282             if (current == null) {
283                 dataChildByName = schemaContext.getDataChildByName(next);
284             } else {
285                 dataChildByName = current.getDataChildByName(next);
286             }
287             if (dataChildByName != null) {
288                 isAugmenting = dataChildByName.isAugmenting();
289             } else {
290                 return false;
291             }
292             if (dataChildByName instanceof DataNodeContainer) {
293                 current = (DataNodeContainer) dataChildByName;
294             }
295         }
296         if (isAugmenting) {
297             return false;
298         }
299         /////
300
301         final Module parentModule = getParentModule(parentNode);
302         if (!leafRefStrippedXPath.isAbsolute()) {
303             leafRefValueNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, parentModule,
304                     parentNode, leafRefStrippedXPath);
305         } else {
306             leafRefValueNode = SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule, leafRefStrippedXPath);
307         }
308         return leafRefValueNode != null ? leafRefValueNode.equals(parentNode) : false;
309     }
310
311     /**
312      * Returns JAVA <code>Type</code> for instances of the type
313      * <code>LeafrefTypeDefinition</code> or
314      * <code>IdentityrefTypeDefinition</code>.
315      *
316      * @param typeDefinition
317      *            type definition which is converted to JAVA <code>Type</code>
318      * @return JAVA <code>Type</code> instance for <code>typeDefinition</code>
319      */
320     private Type javaTypeForLeafrefOrIdentityRef(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
321             final boolean inGrouping) {
322         if (typeDefinition instanceof LeafrefTypeDefinition) {
323             final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) typeDefinition;
324             Preconditions.checkArgument(!isLeafRefSelfReference(leafref, parentNode),
325                 "Leafref %s is referencing itself, incoming StackOverFlowError detected.", leafref);
326             return provideTypeForLeafref(leafref, parentNode, inGrouping);
327         } else if (typeDefinition instanceof IdentityrefTypeDefinition) {
328             return provideTypeForIdentityref((IdentityrefTypeDefinition) typeDefinition);
329         }
330
331         return null;
332     }
333
334     /**
335      * Returns JAVA <code>Type</code> for instances of the type
336      * <code>ExtendedType</code>.
337      *
338      * @param typeDefinition
339      *            type definition which is converted to JAVA <code>Type</code>
340      * @return JAVA <code>Type</code> instance for <code>typeDefinition</code>
341      */
342     private Type javaTypeForExtendedType(final TypeDefinition<?> typeDefinition, final boolean lenient) {
343         final String typedefName = typeDefinition.getQName().getLocalName();
344         final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
345         Type returnType = javaTypeForLeafrefOrIdentityRef(baseTypeDef, typeDefinition, lenient);
346         if (returnType == null) {
347             if (baseTypeDef instanceof EnumTypeDefinition) {
348                 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypeDef;
349                 returnType = provideTypeForEnum(enumTypeDef, typedefName, typeDefinition);
350             } else {
351                 final Module module = findParentModule(schemaContext, typeDefinition);
352                 final Restrictions r = BindingGeneratorUtil.getRestrictions(typeDefinition);
353                 if (module != null) {
354                     final Map<Optional<Revision>, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(
355                         module.getName());
356                     final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
357                     if (genTOs != null) {
358                         returnType = genTOs.get(typedefName);
359                     }
360                     if (returnType == null) {
361                         returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(
362                                 baseTypeDef, typeDefinition, r, lenient);
363                     }
364                 }
365             }
366         }
367         return returnType;
368     }
369
370     /**
371      * Seeks for identity reference <code>idref</code> the JAVA
372      * <code>type</code>.<br />
373      * <br />
374      *
375      * <i>Example:<br />
376      * If identy which is referenced via <code>idref</code> has name <b>Idn</b>
377      * then returning type is <b>{@code Class<? extends Idn>}</b></i>
378      *
379      * @param idref
380      *            identityref type definition for which JAVA <code>Type</code>
381      *            is sought
382      * @return JAVA <code>Type</code> of the identity which is referenced through
383      *         <code>idref</code>
384      */
385     private Type provideTypeForIdentityref(final IdentityrefTypeDefinition idref) {
386         final Collection<IdentitySchemaNode> identities = idref.getIdentities();
387         if (identities.size() > 1) {
388             LOG.warn("Identity reference {} has multiple identities, using only the first one", idref);
389         }
390
391         final QName baseIdQName = identities.iterator().next().getQName();
392         final Module module = schemaContext.findModule(baseIdQName.getModule()).orElse(null);
393         IdentitySchemaNode identity = null;
394         for (IdentitySchemaNode id : module.getIdentities()) {
395             if (id.getQName().equals(baseIdQName)) {
396                 identity = id;
397             }
398         }
399         Preconditions.checkArgument(identity != null, "Target identity '" + baseIdQName + "' do not exists");
400
401         final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
402         final JavaTypeName identifier = JavaTypeName.create(BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
403             identity.getPath()), BindingMapping.getClassName(identity.getQName()));
404         return Types.classType(Types.wildcardTypeFor(identifier));
405     }
406
407     /**
408      * Converts <code>typeDefinition</code> to concrete JAVA <code>Type</code>.
409      *
410      * @param typeDefinition
411      *            type definition which should be converted to JAVA
412      *            <code>Type</code>
413      * @return JAVA <code>Type</code> which represents
414      *         <code>typeDefinition</code>
415      * @throws IllegalArgumentException
416      *             <ul>
417      *             <li>if <code>typeDefinition</code> equal null</li>
418      *             <li>if Q name of <code>typeDefinition</code></li>
419      *             <li>if name of <code>typeDefinition</code></li>
420      *             </ul>
421      */
422     public Type generatedTypeForExtendedDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode) {
423         Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
424         if (typeDefinition.getQName() == null) {
425             throw new IllegalArgumentException(
426                     "Type Definition cannot have non specified QName (QName cannot be NULL!)");
427         }
428         Preconditions.checkArgument(typeDefinition.getQName().getLocalName() != null,
429                 "Type Definitions Local Name cannot be NULL!");
430
431         final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
432         if (baseTypeDef instanceof LeafrefTypeDefinition || baseTypeDef instanceof IdentityrefTypeDefinition) {
433             /*
434              * This is backwards compatibility baggage from way back when. The problem at hand is inconsistency between
435              * the fact that identity is mapped to a Class, which is also returned from leaves which specify it like
436              * this:
437              *
438              *     identity iden;
439              *
440              *     container foo {
441              *         leaf foo {
442              *             type identityref {
443              *                 base iden;
444              *             }
445              *         }
446              *     }
447              *
448              * This results in getFoo() returning Class<? extends Iden>, which looks fine on the surface, but gets more
449              * dicey when we throw in:
450              *
451              *     typedef bar-ref {
452              *         type identityref {
453              *             base iden;
454              *         }
455              *     }
456              *
457              *     container bar {
458              *         leaf bar {
459              *             type bar-ref;
460              *         }
461              *     }
462              *
463              * Now we have competing requirements: typedef would like us to use encapsulation to capture the defined
464              * type, while getBar() wants us to retain shape with getFoo(), as it should not matter how the identityref
465              * is formed.
466              *
467              * In this particular case getFoo() won just after the Binding Spec was frozen, hence we do not generate
468              * an encapsulation for identityref typedefs.
469              *
470              * In case you are thinking we could get by having foo-ref map to a subclass of Iden, that is not a good
471              * option, as it would look as though it is the product of a different construct:
472              *
473              *     identity bar-ref {
474              *         base iden;
475              *     }
476              *
477              * Leading to a rather nice namespace clash and also slight incompatibility with unknown third-party
478              * sub-identities of iden.
479              *
480              * The story behind leafrefs is probably similar, but that needs to be ascertained.
481              */
482             return null;
483         }
484
485         final Module module = findParentModule(schemaContext, parentNode);
486         if (module != null) {
487             final Map<Optional<Revision>, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(
488                 module.getName());
489             final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
490             if (genTOs != null) {
491                 return genTOs.get(typeDefinition.getQName().getLocalName());
492             }
493         }
494         return null;
495     }
496
497     /**
498      * Gets base type definition for <code>extendTypeDef</code>. The method is
499      * recursively called until non <code>ExtendedType</code> type is found.
500      *
501      * @param extendTypeDef
502      *            type definition for which is the base type definition sought
503      * @return type definition which is base type for <code>extendTypeDef</code>
504      * @throws IllegalArgumentException
505      *             if <code>extendTypeDef</code> equal null
506      */
507     private static TypeDefinition<?> baseTypeDefForExtendedType(final TypeDefinition<?> extendTypeDef) {
508         Preconditions.checkArgument(extendTypeDef != null, "Type Definition reference cannot be NULL!");
509
510         TypeDefinition<?> ret = extendTypeDef;
511         while (ret.getBaseType() != null) {
512             ret = ret.getBaseType();
513         }
514
515         return ret;
516     }
517
518     /**
519      * Converts <code>leafrefType</code> to JAVA <code>Type</code>.
520      *
521      * The path of <code>leafrefType</code> is followed to find referenced node
522      * and its <code>Type</code> is returned.
523      *
524      * @param leafrefType leafref type definition for which is the type sought
525      * @param parentNode parent node of the leaf being resolved
526      * @param inGrouping true if we are resolving the type within a grouping.
527      * @return JAVA <code>Type</code> of data schema node which is referenced in
528      *         <code>leafrefType</code>
529      * @throws IllegalArgumentException
530      *             <ul>
531      *             <li>if <code>leafrefType</code> equal null</li>
532      *             <li>if path statement of <code>leafrefType</code> equal null</li>
533      *             </ul>
534      */
535     public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType, final SchemaNode parentNode,
536             final boolean inGrouping) {
537         Preconditions.checkArgument(leafrefType != null, "Leafref Type Definition reference cannot be NULL!");
538
539         Preconditions.checkArgument(leafrefType.getPathStatement() != null,
540                 "The Path Statement for Leafref Type Definition cannot be NULL!");
541
542         final RevisionAwareXPath xpath = leafrefType.getPathStatement();
543         final String strXPath = verifyNotNull(xpath.toString());
544         if (strXPath.indexOf('[') != -1) {
545             // XXX: why are we special-casing this?
546             return Types.objectType();
547         }
548
549         final Module module = findParentModule(schemaContext, parentNode);
550         Preconditions.checkArgument(module != null, "Failed to find module for parent %s", parentNode);
551
552         final SchemaNode dataNode;
553         if (xpath.isAbsolute()) {
554             dataNode = findDataSchemaNode(schemaContext, module, xpath);
555         } else {
556             dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
557             if (dataNode == null && inGrouping) {
558                 // Relative path within a grouping may end up being unresolvable because it may refer outside
559                 // the grouping, in which case it is polymorphic based on instantiation, for example:
560                 //
561                 // grouping foo {
562                 //     leaf foo {
563                 //         type leafref {
564                 //             path "../../bar";
565                 //         }
566                 //     }
567                 // }
568                 //
569                 // container one {
570                 //     leaf bar {
571                 //         type string;
572                 //     }
573                 //     uses foo;
574                 // }
575                 //
576                 // container two {
577                 //     leaf bar {
578                 //         type uint16;
579                 //     }
580                 //     uses foo;
581                 // }
582                 LOG.debug("Leafref type {} not found in parent {}, assuming polymorphic object", leafrefType,
583                     parentNode);
584                 return Types.objectType();
585             }
586         }
587         Preconditions.checkArgument(dataNode != null, "Failed to find leafref target: %s in module %s (%s)",
588                 strXPath, this.getParentModule(parentNode).getName(), parentNode.getQName().getModule());
589
590         // FIXME: this block seems to be some weird magic hack. Analyze and refactor it.
591         Type returnType = null;
592         if (leafContainsEnumDefinition(dataNode)) {
593             returnType = referencedTypes.get(dataNode.getPath());
594         } else if (leafListContainsEnumDefinition(dataNode)) {
595             returnType = Types.listTypeFor(referencedTypes.get(dataNode.getPath()));
596         }
597         if (returnType == null) {
598             returnType = resolveTypeFromDataSchemaNode(dataNode);
599         }
600         Preconditions.checkArgument(returnType != null, "Failed to find leafref target: %s in module %s (%s)",
601                 strXPath, this.getParentModule(parentNode).getName(), parentNode.getQName().getModule(), this);
602         return returnType;
603     }
604
605     /**
606      * Checks if <code>dataNode</code> is <code>LeafSchemaNode</code> and if it
607      * so then checks if it is of type <code>EnumTypeDefinition</code>.
608      *
609      * @param dataNode
610      *            data schema node for which is checked if it is leaf and if it
611      *            is of enum type
612      * @return boolean value
613      *         <ul>
614      *         <li>true - if <code>dataNode</code> is leaf of type enumeration</li>
615      *         <li>false - other cases</li>
616      *         </ul>
617      */
618     private static boolean leafContainsEnumDefinition(final SchemaNode dataNode) {
619         if (dataNode instanceof LeafSchemaNode) {
620             final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
621             if (CompatUtils.compatLeafType(leaf) instanceof EnumTypeDefinition) {
622                 return true;
623             }
624         }
625         return false;
626     }
627
628     /**
629      * Checks if <code>dataNode</code> is <code>LeafListSchemaNode</code> and if
630      * it so then checks if it is of type <code>EnumTypeDefinition</code>.
631      *
632      * @param dataNode
633      *            data schema node for which is checked if it is leaflist and if
634      *            it is of enum type
635      * @return boolean value
636      *         <ul>
637      *         <li>true - if <code>dataNode</code> is leaflist of type
638      *         enumeration</li>
639      *         <li>false - other cases</li>
640      *         </ul>
641      */
642     private static boolean leafListContainsEnumDefinition(final SchemaNode dataNode) {
643         if (dataNode instanceof LeafListSchemaNode) {
644             final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
645             if (leafList.getType() instanceof EnumTypeDefinition) {
646                 return true;
647             }
648         }
649         return false;
650     }
651
652     /**
653      * Converts <code>enumTypeDef</code> to
654      * {@link Enumeration
655      * enumeration}.
656      *
657      * @param enumTypeDef
658      *            enumeration type definition which is converted to enumeration
659      * @param enumName
660      *            string with name which is used as the enumeration name
661      * @return enumeration type which is built with data (name, enum values)
662      *         from <code>enumTypeDef</code>
663      * @throws IllegalArgumentException
664      *             <ul>
665      *             <li>if <code>enumTypeDef</code> equals null</li>
666      *             <li>if enum values of <code>enumTypeDef</code> equal null</li>
667      *             <li>if Q name of <code>enumTypeDef</code> equal null</li>
668      *             <li>if name of <code>enumTypeDef</code> equal null</li>
669      *             </ul>
670      */
671     private Enumeration provideTypeForEnum(final EnumTypeDefinition enumTypeDef, final String enumName,
672             final SchemaNode parentNode) {
673         Preconditions.checkArgument(enumTypeDef != null, "EnumTypeDefinition reference cannot be NULL!");
674         Preconditions.checkArgument(enumTypeDef.getValues() != null,
675                 "EnumTypeDefinition MUST contain at least ONE value definition!");
676         Preconditions.checkArgument(enumTypeDef.getQName() != null, "EnumTypeDefinition MUST contain NON-NULL QName!");
677         Preconditions.checkArgument(enumTypeDef.getQName().getLocalName() != null,
678                 "Local Name in EnumTypeDefinition QName cannot be NULL!");
679
680         final Module module = findParentModule(schemaContext, parentNode);
681         final AbstractEnumerationBuilder enumBuilder = newEnumerationBuilder(JavaTypeName.create(
682             BindingMapping.getRootPackageName(module.getQNameModule()), BindingMapping.getClassName(enumName)));
683         addEnumDescription(enumBuilder, enumTypeDef);
684         enumTypeDef.getReference().ifPresent(enumBuilder::setReference);
685         enumBuilder.setModuleName(module.getName());
686         enumBuilder.setSchemaPath(enumTypeDef.getPath());
687         enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
688         return enumBuilder.toInstance(null);
689     }
690
691     /**
692      * Adds enumeration to <code>typeBuilder</code>. The enumeration data are
693      * taken from <code>enumTypeDef</code>.
694      *
695      * @param enumTypeDef
696      *            enumeration type definition is source of enumeration data for
697      *            <code>typeBuilder</code>
698      * @param enumName
699      *            string with the name of enumeration
700      * @param typeBuilder
701      *            generated type builder to which is enumeration added
702      * @return enumeration type which contains enumeration data form
703      *         <code>enumTypeDef</code>
704      * @throws IllegalArgumentException
705      *             <ul>
706      *             <li>if <code>enumTypeDef</code> equals null</li>
707      *             <li>if enum values of <code>enumTypeDef</code> equal null</li>
708      *             <li>if Q name of <code>enumTypeDef</code> equal null</li>
709      *             <li>if name of <code>enumTypeDef</code> equal null</li>
710      *             <li>if name of <code>typeBuilder</code> equal null</li>
711      *             </ul>
712      *
713      */
714     private Enumeration addInnerEnumerationToTypeBuilder(final EnumTypeDefinition enumTypeDef,
715             final String enumName, final GeneratedTypeBuilderBase<?> typeBuilder) {
716         Preconditions.checkArgument(enumTypeDef != null, "EnumTypeDefinition reference cannot be NULL!");
717         Preconditions.checkArgument(enumTypeDef.getValues() != null,
718                 "EnumTypeDefinition MUST contain at least ONE value definition!");
719         Preconditions.checkArgument(enumTypeDef.getQName() != null, "EnumTypeDefinition MUST contain NON-NULL QName!");
720         Preconditions.checkArgument(enumTypeDef.getQName().getLocalName() != null,
721                 "Local Name in EnumTypeDefinition QName cannot be NULL!");
722         Preconditions.checkArgument(typeBuilder != null, "Generated Type Builder reference cannot be NULL!");
723
724         final EnumBuilder enumBuilder = typeBuilder.addEnumeration(BindingMapping.getClassName(enumName));
725
726         addEnumDescription(enumBuilder, enumTypeDef);
727         enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
728         return enumBuilder.toInstance(enumBuilder);
729     }
730
731     public abstract void addEnumDescription(EnumBuilder enumBuilder, EnumTypeDefinition enumTypeDef);
732
733     public abstract AbstractEnumerationBuilder newEnumerationBuilder(JavaTypeName identifier);
734
735     public abstract GeneratedTOBuilder newGeneratedTOBuilder(JavaTypeName identifier);
736
737     public abstract GeneratedTypeBuilder newGeneratedTypeBuilder(JavaTypeName identifier);
738
739     /**
740      * Converts the pattern constraints to the list of the strings which represents these constraints.
741      *
742      * @param patternConstraints list of pattern constraints
743      * @return list of strings which represents the constraint patterns
744      */
745     public abstract Map<String, String> resolveRegExpressions(List<PatternConstraint> patternConstraints);
746
747     abstract void addCodegenInformation(GeneratedTypeBuilderBase<?> genTOBuilder, TypeDefinition<?> typeDef);
748
749     /**
750      * Converts the pattern constraints from <code>typedef</code> to the list of
751      * the strings which represents these constraints.
752      *
753      * @param typedef
754      *            extended type in which are the pattern constraints sought
755      * @return list of strings which represents the constraint patterns
756      * @throws IllegalArgumentException
757      *             if <code>typedef</code> equals null
758      *
759      */
760     private Map<String, String> resolveRegExpressionsFromTypedef(final TypeDefinition<?> typedef) {
761         if (!(typedef instanceof StringTypeDefinition)) {
762             return ImmutableMap.of();
763         }
764
765         // TODO: run diff against base ?
766         return resolveRegExpressions(((StringTypeDefinition) typedef).getPatternConstraints());
767     }
768
769     /**
770      * Converts <code>dataNode</code> to JAVA <code>Type</code>.
771      *
772      * @param dataNode
773      *            contains information about YANG type
774      * @return JAVA <code>Type</code> representation of <code>dataNode</code>
775      */
776     private Type resolveTypeFromDataSchemaNode(final SchemaNode dataNode) {
777         Type returnType = null;
778         if (dataNode != null) {
779             if (dataNode instanceof LeafSchemaNode) {
780                 final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
781                 final TypeDefinition<?> type = CompatUtils.compatLeafType(leaf);
782                 returnType = javaTypeForSchemaDefinitionType(type, leaf);
783             } else if (dataNode instanceof LeafListSchemaNode) {
784                 final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
785                 returnType = javaTypeForSchemaDefinitionType(leafList.getType(), leafList);
786             }
787         }
788         return returnType;
789     }
790
791     /**
792      * Passes through all modules and through all its type definitions and
793      * convert it to generated types.
794      *
795      * The modules are firstly sorted by mutual dependencies. The modules are
796      * sequentially passed. All type definitions of a module are at the
797      * beginning sorted so that type definition with less amount of references
798      * to other type definition are processed first.<br />
799      * For each module is created mapping record in the map
800      * {@link AbstractTypeProvider#genTypeDefsContextMap genTypeDefsContextMap}
801      * which map current module name to the map which maps type names to
802      * returned types (generated types).
803      *
804      */
805     private void resolveTypeDefsFromContext() {
806         final Set<Module> modules = schemaContext.getModules();
807         Preconditions.checkArgument(modules != null, "Set of Modules cannot be NULL!");
808         final List<Module> modulesSortedByDependency = ModuleDependencySort.sort(modules);
809
810         for (Module module : modulesSortedByDependency) {
811             Map<Optional<Revision>, Map<String, Type>> dateTypeMap = genTypeDefsContextMap.computeIfAbsent(
812                 module.getName(), key -> new HashMap<>());
813             dateTypeMap.put(module.getRevision(), Collections.<String, Type>emptyMap());
814             genTypeDefsContextMap.put(module.getName(), dateTypeMap);
815         }
816
817         for (Module module : modulesSortedByDependency) {
818             if (module != null) {
819                 final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
820                 if (basePackageName != null) {
821                     final List<TypeDefinition<?>> typeDefinitions = TypedefResolver.getAllTypedefs(module);
822                     for (TypeDefinition<?> typedef : sortTypeDefinitionAccordingDepth(typeDefinitions)) {
823                         typedefToGeneratedType(basePackageName, module, typedef);
824                     }
825                 }
826             }
827         }
828     }
829
830     /**
831      *
832      * @param basePackageName
833      *            string with name of package to which the module belongs
834      * @param module
835      *            string with the name of the module for to which the
836      *            <code>typedef</code> belongs
837      * @param typedef
838      *            type definition of the node for which should be created JAVA <code>Type</code> (usually generated TO)
839      * @return JAVA <code>Type</code> representation of <code>typedef</code> or
840      *         <code>null</code> value if <code>basePackageName</code> or
841      *         <code>modulName</code> or <code>typedef</code> or Q name of
842      *         <code>typedef</code> equals <code>null</code>
843      */
844     private Type typedefToGeneratedType(final String basePackageName, final Module module,
845             final TypeDefinition<?> typedef) {
846         final TypeDefinition<?> baseTypedef = typedef.getBaseType();
847
848         // See generatedTypeForExtendedDefinitionType() above for rationale behind this special case.
849         if (baseTypedef instanceof LeafrefTypeDefinition || baseTypedef instanceof IdentityrefTypeDefinition) {
850             return null;
851         }
852
853         final String typedefName = typedef.getQName().getLocalName();
854
855         final Type returnType;
856         if (baseTypedef.getBaseType() != null) {
857             returnType = provideGeneratedTOFromExtendedType(typedef, baseTypedef, basePackageName,
858                 module.getName());
859         } else if (baseTypedef instanceof UnionTypeDefinition) {
860             final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForUnionTypeDef(
861                 JavaTypeName.create(basePackageName, BindingMapping.getClassName(typedef.getQName())),
862                 (UnionTypeDefinition) baseTypedef, typedef);
863             genTOBuilder.setTypedef(true);
864             genTOBuilder.setIsUnion(true);
865             addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
866             makeSerializable(genTOBuilder);
867             returnType = genTOBuilder.build();
868
869             // Define a corresponding union builder. Typedefs are always anchored at a Java package root,
870             // so we are placing the builder alongside the union.
871             final GeneratedTOBuilder unionBuilder = newGeneratedTOBuilder(
872                 JavaTypeName.create(genTOBuilder.getPackageName(), genTOBuilder.getName() + "Builder"));
873             unionBuilder.setIsUnionBuilder(true);
874             final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
875             method.setReturnType(returnType);
876             method.addParameter(Types.STRING, "defaultValue");
877             method.setAccessModifier(AccessModifier.PUBLIC);
878             method.setStatic(true);
879             Set<Type> types = additionalTypes.get(module);
880             if (types == null) {
881                 types = Sets.<Type> newHashSet(unionBuilder.build());
882                 additionalTypes.put(module, types);
883             } else {
884                 types.add(unionBuilder.build());
885             }
886         } else if (baseTypedef instanceof EnumTypeDefinition) {
887             // enums are automatically Serializable
888             final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypedef;
889             // TODO units for typedef enum
890             returnType = provideTypeForEnum(enumTypeDef, typedefName, typedef);
891         } else if (baseTypedef instanceof BitsTypeDefinition) {
892             final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForBitsTypeDefinition(
893                 JavaTypeName.create(basePackageName, BindingMapping.getClassName(typedef.getQName())),
894                 (BitsTypeDefinition) baseTypedef, module.getName());
895             genTOBuilder.setTypedef(true);
896             addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
897             makeSerializable(genTOBuilder);
898             returnType = genTOBuilder.build();
899         } else {
900             final Type javaType = javaTypeForSchemaDefinitionType(baseTypedef, typedef);
901             returnType = wrapJavaTypeIntoTO(basePackageName, typedef, javaType, module.getName());
902         }
903         if (returnType != null) {
904             final Map<Optional<Revision>, Map<String, Type>> modulesByDate =
905                     genTypeDefsContextMap.get(module.getName());
906             final Optional<Revision> moduleRevision = module.getRevision();
907             Map<String, Type> typeMap = modulesByDate.get(moduleRevision);
908             if (typeMap != null) {
909                 if (typeMap.isEmpty()) {
910                     typeMap = new HashMap<>(4);
911                     modulesByDate.put(moduleRevision, typeMap);
912                 }
913                 typeMap.put(typedefName, returnType);
914             }
915             return returnType;
916         }
917         return null;
918     }
919
920     /**
921      * Wraps base YANG type to generated TO.
922      *
923      * @param basePackageName
924      *            string with name of package to which the module belongs
925      * @param typedef
926      *            type definition which is converted to the TO
927      * @param javaType
928      *            JAVA <code>Type</code> to which is <code>typedef</code> mapped
929      * @return generated transfer object which represent<code>javaType</code>
930      */
931     private GeneratedTransferObject wrapJavaTypeIntoTO(final String basePackageName, final TypeDefinition<?> typedef,
932             final Type javaType, final String moduleName) {
933         Preconditions.checkNotNull(javaType, "javaType cannot be null");
934
935         final GeneratedTOBuilder genTOBuilder = typedefToTransferObject(basePackageName, typedef, moduleName);
936         genTOBuilder.setRestrictions(BindingGeneratorUtil.getRestrictions(typedef));
937         final GeneratedPropertyBuilder genPropBuilder = genTOBuilder.addProperty("value");
938         genPropBuilder.setReturnType(javaType);
939         genTOBuilder.addEqualsIdentity(genPropBuilder);
940         genTOBuilder.addHashIdentity(genPropBuilder);
941         genTOBuilder.addToStringProperty(genPropBuilder);
942         if (typedef.getStatus() == Status.DEPRECATED) {
943             genTOBuilder.addAnnotation("java.lang", "Deprecated");
944         }
945         if (javaType instanceof ConcreteType && "String".equals(javaType.getName()) && typedef.getBaseType() != null) {
946             addStringRegExAsConstant(genTOBuilder, resolveRegExpressionsFromTypedef(typedef));
947         }
948         addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
949         genTOBuilder.setTypedef(true);
950         makeSerializable(genTOBuilder);
951         return genTOBuilder.build();
952     }
953
954     /**
955      * Converts output list of generated TO builders to one TO builder (first
956      * from list) which contains the remaining builders as its enclosing TO.
957      *
958      * @param typeName new type identifier
959      * @param typedef type definition which should be of type {@link UnionTypeDefinition}
960      * @return generated TO builder with the list of enclosed generated TO builders
961      */
962     public GeneratedTOBuilder provideGeneratedTOBuilderForUnionTypeDef(final JavaTypeName typeName,
963             final UnionTypeDefinition typedef, final TypeDefinition<?> parentNode) {
964         final List<GeneratedTOBuilder> builders = provideGeneratedTOBuildersForUnionTypeDef(typeName, typedef,
965             parentNode);
966         Preconditions.checkState(!builders.isEmpty(), "No GeneratedTOBuilder objects generated from union %s", typedef);
967
968         final GeneratedTOBuilder resultTOBuilder = builders.remove(0);
969         builders.forEach(resultTOBuilder::addEnclosingTransferObject);
970         return resultTOBuilder;
971     }
972
973     /**
974      * Converts <code>typedef</code> to generated TO with
975      * <code>typeDefName</code>. Every union type from <code>typedef</code> is
976      * added to generated TO builder as property.
977      *
978      * @param typeName new type identifier
979      * @param typedef
980      *            type definition which should be of type
981      *            <code>UnionTypeDefinition</code>
982      * @return generated TO builder which represents <code>typedef</code>
983      * @throws NullPointerException
984      *             <ul>
985      *             <li>if <code>basePackageName</code> is null</li>
986      *             <li>if <code>typedef</code> is null</li>
987      *             <li>if Qname of <code>typedef</code> is null</li>
988      *             </ul>
989      */
990     public List<GeneratedTOBuilder> provideGeneratedTOBuildersForUnionTypeDef(final JavaTypeName typeName,
991             final UnionTypeDefinition typedef, final SchemaNode parentNode) {
992         Preconditions.checkNotNull(typedef, "Type Definition cannot be NULL!");
993         Preconditions.checkNotNull(typedef.getQName(), "Type definition QName cannot be NULL!");
994
995         final List<GeneratedTOBuilder> generatedTOBuilders = new ArrayList<>();
996         final List<TypeDefinition<?>> unionTypes = typedef.getTypes();
997         final Module module = findParentModule(schemaContext, parentNode);
998
999         final GeneratedTOBuilder unionGenTOBuilder = newGeneratedTOBuilder(typeName);
1000         unionGenTOBuilder.setIsUnion(true);
1001         unionGenTOBuilder.setSchemaPath(typedef.getPath());
1002         unionGenTOBuilder.setModuleName(module.getName());
1003         addCodegenInformation(unionGenTOBuilder, typedef);
1004         generatedTOBuilders.add(unionGenTOBuilder);
1005
1006         // Pattern string is the key, XSD regex is the value. The reason for this choice is that the pattern carries
1007         // also negation information and hence guarantees uniqueness.
1008         final Map<String, String> expressions = new HashMap<>();
1009         for (TypeDefinition<?> unionType : unionTypes) {
1010             final String unionTypeName = unionType.getQName().getLocalName();
1011
1012             // If we have a base type we should follow the type definition backwards, except for identityrefs, as those
1013             // do not follow type encapsulation -- we use the general case for that.
1014             if (unionType.getBaseType() != null  && !(unionType instanceof IdentityrefTypeDefinition)) {
1015                 resolveExtendedSubtypeAsUnion(unionGenTOBuilder, unionType, expressions, parentNode);
1016             } else if (unionType instanceof UnionTypeDefinition) {
1017                 generatedTOBuilders.addAll(resolveUnionSubtypeAsUnion(unionGenTOBuilder,
1018                     (UnionTypeDefinition) unionType, parentNode));
1019             } else if (unionType instanceof EnumTypeDefinition) {
1020                 final Enumeration enumeration = addInnerEnumerationToTypeBuilder((EnumTypeDefinition) unionType,
1021                         unionTypeName, unionGenTOBuilder);
1022                 updateUnionTypeAsProperty(unionGenTOBuilder, enumeration, unionTypeName);
1023             } else {
1024                 final Type javaType = javaTypeForSchemaDefinitionType(unionType, parentNode);
1025                 updateUnionTypeAsProperty(unionGenTOBuilder, javaType, unionTypeName);
1026             }
1027         }
1028         addStringRegExAsConstant(unionGenTOBuilder, expressions);
1029
1030         storeGenTO(typedef, unionGenTOBuilder, parentNode);
1031
1032         return generatedTOBuilders;
1033     }
1034
1035     /**
1036      * Wraps code which handles the case when union subtype is also of the type <code>UnionType</code>.
1037      *
1038      * In this case the new generated TO is created for union subtype (recursive call of method
1039      * {@link #provideGeneratedTOBuildersForUnionTypeDef(String, UnionTypeDefinition, String, SchemaNode)}
1040      * provideGeneratedTOBuilderForUnionTypeDef} and in parent TO builder <code>parentUnionGenTOBuilder</code> is
1041      * created property which type is equal to new generated TO.
1042      *
1043      * @param parentUnionGenTOBuilder
1044      *            generated TO builder to which is the property with the child
1045      *            union subtype added
1046      * @param basePackageName
1047      *            string with the name of the module package
1048      * @param unionSubtype
1049      *            type definition which represents union subtype
1050      * @return list of generated TO builders. The number of the builders can be
1051      *         bigger one due to recursive call of
1052      *         <code>provideGeneratedTOBuildersForUnionTypeDef</code> method.
1053      */
1054     private List<GeneratedTOBuilder> resolveUnionSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
1055             final UnionTypeDefinition unionSubtype, final SchemaNode parentNode) {
1056         final JavaTypeName newTOBuilderName = parentUnionGenTOBuilder.getIdentifier().createSibling(
1057             provideAvailableNameForGenTOBuilder(parentUnionGenTOBuilder.getName()));
1058         final List<GeneratedTOBuilder> subUnionGenTOBUilders = provideGeneratedTOBuildersForUnionTypeDef(
1059             newTOBuilderName, unionSubtype, parentNode);
1060
1061         final GeneratedPropertyBuilder propertyBuilder;
1062         propertyBuilder = parentUnionGenTOBuilder.addProperty(BindingMapping.getPropertyName(
1063             newTOBuilderName.simpleName()));
1064         propertyBuilder.setReturnType(subUnionGenTOBUilders.get(0).build());
1065         parentUnionGenTOBuilder.addEqualsIdentity(propertyBuilder);
1066         parentUnionGenTOBuilder.addToStringProperty(propertyBuilder);
1067
1068         return subUnionGenTOBUilders;
1069     }
1070
1071     /**
1072      * Wraps code which handle case when union subtype is of the type
1073      * <code>ExtendedType</code>.
1074      *
1075      * If TO for this type already exists it is used for the creation of the
1076      * property in <code>parentUnionGenTOBuilder</code>. In other case the base
1077      * type is used for the property creation.
1078      *
1079      * @param parentUnionGenTOBuilder
1080      *            generated TO builder in which new property is created
1081      * @param unionSubtype
1082      *            type definition of the <code>ExtendedType</code> type which
1083      *            represents union subtype
1084      * @param expressions
1085      *            list of strings with the regular expressions
1086      * @param parentNode
1087      *            parent Schema Node for Extended Subtype
1088      *
1089      */
1090     private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
1091             final TypeDefinition<?> unionSubtype, final Map<String, String> expressions, final SchemaNode parentNode) {
1092         final String unionTypeName = unionSubtype.getQName().getLocalName();
1093         final Type genTO = findGenTO(unionTypeName, unionSubtype);
1094         if (genTO != null) {
1095             updateUnionTypeAsProperty(parentUnionGenTOBuilder, genTO, genTO.getName());
1096             return;
1097         }
1098
1099         final TypeDefinition<?> baseType = baseTypeDefForExtendedType(unionSubtype);
1100         if (unionTypeName.equals(baseType.getQName().getLocalName())) {
1101             final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(baseType,
1102                 parentNode, BindingGeneratorUtil.getRestrictions(unionSubtype));
1103             if (javaType != null) {
1104                 updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, unionTypeName);
1105             }
1106         } else if (baseType instanceof LeafrefTypeDefinition) {
1107             final Type javaType = javaTypeForSchemaDefinitionType(baseType, parentNode);
1108             boolean typeExist = false;
1109             for (GeneratedPropertyBuilder generatedPropertyBuilder : parentUnionGenTOBuilder.getProperties()) {
1110                 final Type origType = ((GeneratedPropertyBuilderImpl) generatedPropertyBuilder).getReturnType();
1111                 if (origType != null && javaType != null && javaType == origType) {
1112                     typeExist = true;
1113                     break;
1114                 }
1115             }
1116             if (!typeExist && javaType != null) {
1117                 updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType,
1118                     javaType.getName() + parentUnionGenTOBuilder.getName() + "Value");
1119             }
1120         }
1121         if (baseType instanceof StringTypeDefinition) {
1122             expressions.putAll(resolveRegExpressionsFromTypedef(unionSubtype));
1123         }
1124     }
1125
1126     /**
1127      * Searches for generated TO for <code>searchedTypeDef</code> type
1128      * definition in {@link #genTypeDefsContextMap genTypeDefsContextMap}
1129      *
1130      * @param searchedTypeName
1131      *            string with name of <code>searchedTypeDef</code>
1132      * @return generated TO for <code>searchedTypeDef</code> or
1133      *         <code>null</code> it it doesn't exist
1134      */
1135     private Type findGenTO(final String searchedTypeName, final SchemaNode parentNode) {
1136         final Module typeModule = findParentModule(schemaContext, parentNode);
1137         if (typeModule != null && typeModule.getName() != null) {
1138             final Map<Optional<Revision>, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(typeModule.getName());
1139             final Map<String, Type> genTOs = modulesByDate.get(typeModule.getRevision());
1140             if (genTOs != null) {
1141                 return genTOs.get(searchedTypeName);
1142             }
1143         }
1144         return null;
1145     }
1146
1147     /**
1148      * Stores generated TO created from <code>genTOBuilder</code> for
1149      * <code>newTypeDef</code> to {@link #genTypeDefsContextMap
1150      * genTypeDefsContextMap} if the module for <code>newTypeDef</code> exists
1151      *
1152      * @param newTypeDef
1153      *            type definition for which is <code>genTOBuilder</code> created
1154      * @param genTOBuilder
1155      *            generated TO builder which is converted to generated TO and
1156      *            stored
1157      */
1158     private void storeGenTO(final TypeDefinition<?> newTypeDef, final GeneratedTOBuilder genTOBuilder, final SchemaNode parentNode) {
1159         if (!(newTypeDef instanceof UnionTypeDefinition)) {
1160             final Module parentModule = findParentModule(schemaContext, parentNode);
1161             if (parentModule != null && parentModule.getName() != null) {
1162                 final Map<Optional<Revision>, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(parentModule.getName());
1163                 final Map<String, Type> genTOsMap = modulesByDate.get(parentModule.getRevision());
1164                 genTOsMap.put(newTypeDef.getQName().getLocalName(), genTOBuilder.build());
1165             }
1166         }
1167     }
1168
1169     /**
1170      * Adds a new property with the name <code>propertyName</code> and with type
1171      * <code>type</code> to <code>unonGenTransObject</code>.
1172      *
1173      * @param unionGenTransObject
1174      *            generated TO to which should be property added
1175      * @param type
1176      *            JAVA <code>type</code> of the property which should be added
1177      *            to <code>unionGentransObject</code>
1178      * @param propertyName
1179      *            string with name of property which should be added to
1180      *            <code>unionGentransObject</code>
1181      */
1182     private static void updateUnionTypeAsProperty(final GeneratedTOBuilder unionGenTransObject, final Type type, final String propertyName) {
1183         if (unionGenTransObject != null && type != null && !unionGenTransObject.containsProperty(propertyName)) {
1184             final GeneratedPropertyBuilder propBuilder = unionGenTransObject
1185                     .addProperty(BindingMapping.getPropertyName(propertyName));
1186             propBuilder.setReturnType(type);
1187
1188             unionGenTransObject.addEqualsIdentity(propBuilder);
1189             unionGenTransObject.addHashIdentity(propBuilder);
1190             unionGenTransObject.addToStringProperty(propBuilder);
1191         }
1192     }
1193
1194     /**
1195      * Converts <code>typedef</code> to the generated TO builder.
1196      *
1197      * @param basePackageName
1198      *            string with name of package to which the module belongs
1199      * @param typedef
1200      *            type definition from which is the generated TO builder created
1201      * @return generated TO builder which contains data from
1202      *         <code>typedef</code> and <code>basePackageName</code>
1203      */
1204     private GeneratedTOBuilder typedefToTransferObject(final String basePackageName,
1205             final TypeDefinition<?> typedef, final String moduleName) {
1206         JavaTypeName name = renames.get(typedef);
1207         if (name == null) {
1208             name = JavaTypeName.create(
1209                 BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, typedef.getPath()),
1210                 BindingMapping.getClassName(typedef.getQName().getLocalName()));
1211         }
1212
1213         final GeneratedTOBuilder newType = newGeneratedTOBuilder(name);
1214         newType.setSchemaPath(typedef.getPath());
1215         newType.setModuleName(moduleName);
1216         addCodegenInformation(newType, typedef);
1217         return newType;
1218     }
1219
1220     /**
1221      * Converts <code>typeDef</code> which should be of the type
1222      * <code>BitsTypeDefinition</code> to <code>GeneratedTOBuilder</code>.
1223      *
1224      * All the bits of the typeDef are added to returning generated TO as
1225      * properties.
1226      *
1227      * @param typeName new type identifier
1228      * @param typeDef
1229      *            type definition from which is the generated TO builder created
1230      * @return generated TO builder which represents <code>typeDef</code>
1231      * @throws IllegalArgumentException
1232      *             <ul>
1233      *             <li>if <code>typeDef</code> equals null</li>
1234      *             <li>if <code>basePackageName</code> equals null</li>
1235      *             </ul>
1236      */
1237     public GeneratedTOBuilder provideGeneratedTOBuilderForBitsTypeDefinition(final JavaTypeName typeName,
1238             final BitsTypeDefinition typeDef, final String moduleName) {
1239         final GeneratedTOBuilder genTOBuilder = newGeneratedTOBuilder(typeName);
1240         genTOBuilder.setSchemaPath(typeDef.getPath());
1241         genTOBuilder.setModuleName(moduleName);
1242         genTOBuilder.setBaseType(typeDef);
1243         addCodegenInformation(genTOBuilder, typeDef);
1244
1245         final List<Bit> bitList = typeDef.getBits();
1246         GeneratedPropertyBuilder genPropertyBuilder;
1247         for (Bit bit : bitList) {
1248             final String name = bit.getName();
1249             genPropertyBuilder = genTOBuilder.addProperty(BindingMapping.getPropertyName(name));
1250             genPropertyBuilder.setReadOnly(true);
1251             genPropertyBuilder.setReturnType(BaseYangTypes.BOOLEAN_TYPE);
1252
1253             genTOBuilder.addEqualsIdentity(genPropertyBuilder);
1254             genTOBuilder.addHashIdentity(genPropertyBuilder);
1255             genTOBuilder.addToStringProperty(genPropertyBuilder);
1256         }
1257
1258         return genTOBuilder;
1259     }
1260
1261     /**
1262      *
1263      * Adds to the <code>genTOBuilder</code> the constant which contains regular
1264      * expressions from the <code>regularExpressions</code>
1265      *
1266      * @param genTOBuilder
1267      *            generated TO builder to which are
1268      *            <code>regular expressions</code> added
1269      * @param expressions
1270      *            list of string which represent regular expressions
1271      */
1272     private static void addStringRegExAsConstant(final GeneratedTOBuilder genTOBuilder,
1273             final Map<String, String> expressions) {
1274         if (!expressions.isEmpty()) {
1275             genTOBuilder.addConstant(Types.listTypeFor(BaseYangTypes.STRING_TYPE), TypeConstants.PATTERN_CONSTANT_NAME,
1276                 ImmutableMap.copyOf(expressions));
1277         }
1278     }
1279
1280     /**
1281      * Creates generated TO with data about inner extended type
1282      * <code>innerExtendedType</code>, about the package name
1283      * <code>typedefName</code> and about the generated TO name
1284      * <code>typedefName</code>.
1285      *
1286      * It is supposed that <code>innerExtendedType</code> is already present in
1287      * {@link AbstractTypeProvider#genTypeDefsContextMap genTypeDefsContextMap} to
1288      * be possible set it as extended type for the returning generated TO.
1289      *
1290      * @param typedef
1291      *            Type Definition
1292      * @param innerExtendedType
1293      *            extended type which is part of some other extended type
1294      * @param basePackageName
1295      *            string with the package name of the module
1296      * @param moduleName
1297      *            Module Name
1298      * @return generated TO which extends generated TO for
1299      *         <code>innerExtendedType</code>
1300      * @throws IllegalArgumentException
1301      *             <ul>
1302      *             <li>if <code>extendedType</code> equals null</li>
1303      *             <li>if <code>basePackageName</code> equals null</li>
1304      *             <li>if <code>typedefName</code> equals null</li>
1305      *             </ul>
1306      */
1307     private GeneratedTransferObject provideGeneratedTOFromExtendedType(final TypeDefinition<?> typedef,
1308             final TypeDefinition<?> innerExtendedType, final String basePackageName, final String moduleName) {
1309         Preconditions.checkArgument(innerExtendedType != null, "Extended type cannot be NULL!");
1310         Preconditions.checkArgument(basePackageName != null, "String with base package name cannot be NULL!");
1311
1312         final GeneratedTOBuilder genTOBuilder = newGeneratedTOBuilder(JavaTypeName.create(basePackageName,
1313             BindingMapping.getClassName(typedef.getQName())));
1314         genTOBuilder.setSchemaPath(typedef.getPath());
1315         genTOBuilder.setModuleName(moduleName);
1316         genTOBuilder.setTypedef(true);
1317         addCodegenInformation(genTOBuilder, typedef);
1318
1319         final Restrictions r = BindingGeneratorUtil.getRestrictions(typedef);
1320         genTOBuilder.setRestrictions(r);
1321         addStringRegExAsConstant(genTOBuilder, resolveRegExpressionsFromTypedef(typedef));
1322
1323         if (typedef.getStatus() == Status.DEPRECATED) {
1324             genTOBuilder.addAnnotation("java.lang", "Deprecated");
1325         }
1326
1327         if (baseTypeDefForExtendedType(innerExtendedType) instanceof UnionTypeDefinition) {
1328             genTOBuilder.setIsUnion(true);
1329         }
1330
1331         Map<Optional<Revision>, Map<String, Type>> modulesByDate = null;
1332         Map<String, Type> typeMap = null;
1333         final Module parentModule = findParentModule(schemaContext, innerExtendedType);
1334         if (parentModule != null) {
1335             modulesByDate = genTypeDefsContextMap.get(parentModule.getName());
1336             typeMap = modulesByDate.get(parentModule.getRevision());
1337         }
1338
1339         if (typeMap != null) {
1340             final String innerTypeDef = innerExtendedType.getQName().getLocalName();
1341             final Type type = typeMap.get(innerTypeDef);
1342             if (type instanceof GeneratedTransferObject) {
1343                 genTOBuilder.setExtendsType((GeneratedTransferObject) type);
1344             }
1345         }
1346         addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
1347         makeSerializable(genTOBuilder);
1348
1349         return genTOBuilder.build();
1350     }
1351
1352     /**
1353      * Add {@link Serializable} to implemented interfaces of this TO. Also
1354      * compute and add serialVersionUID property.
1355      *
1356      * @param gto
1357      *            transfer object which needs to be serializable
1358      */
1359     private static void makeSerializable(final GeneratedTOBuilder gto) {
1360         gto.addImplementsType(Types.serializableType());
1361         final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
1362         prop.setValue(Long.toString(BindingGeneratorUtil.computeDefaultSUID(gto)));
1363         gto.setSUID(prop);
1364     }
1365
1366     /**
1367      * Finds out for each type definition how many immersion (depth) is
1368      * necessary to get to the base type. Every type definition is inserted to
1369      * the map which key is depth and value is list of type definitions with
1370      * equal depth. In next step are lists from this map concatenated to one
1371      * list in ascending order according to their depth. All type definitions
1372      * are in the list behind all type definitions on which depends.
1373      *
1374      * @param unsortedTypeDefinitions
1375      *            list of type definitions which should be sorted by depth
1376      * @return list of type definitions sorted according their each other
1377      *         dependencies (type definitions which are depend on other type
1378      *         definitions are in list behind them).
1379      */
1380     private static List<TypeDefinition<?>> sortTypeDefinitionAccordingDepth(
1381             final Collection<TypeDefinition<?>> unsortedTypeDefinitions) {
1382         final List<TypeDefinition<?>> sortedTypeDefinition = new ArrayList<>();
1383
1384         final Map<Integer, List<TypeDefinition<?>>> typeDefinitionsDepths = new TreeMap<>();
1385         for (TypeDefinition<?> unsortedTypeDefinition : unsortedTypeDefinitions) {
1386             final Integer depth = getTypeDefinitionDepth(unsortedTypeDefinition);
1387             List<TypeDefinition<?>> typeDefinitionsConcreteDepth =
1388                 typeDefinitionsDepths.computeIfAbsent(depth, k -> new ArrayList<>());
1389             typeDefinitionsConcreteDepth.add(unsortedTypeDefinition);
1390         }
1391
1392         // SortedMap guarantees order corresponding to keys in ascending order
1393         for (List<TypeDefinition<?>> v : typeDefinitionsDepths.values()) {
1394             sortedTypeDefinition.addAll(v);
1395         }
1396
1397         return sortedTypeDefinition;
1398     }
1399
1400     /**
1401      * Returns how many immersion is necessary to get from the type definition
1402      * to the base type.
1403      *
1404      * @param typeDefinition
1405      *            type definition for which is depth sought.
1406      * @return number of immersions which are necessary to get from the type
1407      *         definition to the base type
1408      */
1409     private static int getTypeDefinitionDepth(final TypeDefinition<?> typeDefinition) {
1410         // FIXME: rewrite this in a non-recursive manner
1411         if (typeDefinition == null) {
1412             return 1;
1413         }
1414         final TypeDefinition<?> baseType = typeDefinition.getBaseType();
1415         if (baseType == null) {
1416             return 1;
1417         }
1418
1419         int depth = 1;
1420         if (baseType.getBaseType() != null) {
1421             depth = depth + getTypeDefinitionDepth(baseType);
1422         } else if (baseType instanceof UnionTypeDefinition) {
1423             final List<TypeDefinition<?>> childTypeDefinitions = ((UnionTypeDefinition) baseType).getTypes();
1424             int maxChildDepth = 0;
1425             int childDepth = 1;
1426             for (TypeDefinition<?> childTypeDefinition : childTypeDefinitions) {
1427                 childDepth = childDepth + getTypeDefinitionDepth(childTypeDefinition);
1428                 if (childDepth > maxChildDepth) {
1429                     maxChildDepth = childDepth;
1430                 }
1431             }
1432             return maxChildDepth;
1433         }
1434         return depth;
1435     }
1436
1437     /**
1438      * Returns string which contains the same value as <code>name</code> but integer suffix is incremented by one. If
1439      * <code>name</code> contains no number suffix, a new suffix initialized at 1 is added. A suffix is actually
1440      * composed of a '$' marker, which is safe, as no YANG identifier can contain '$', and a unsigned decimal integer.
1441      *
1442      * @param name string with name of augmented node
1443      * @return string with the number suffix incremented by one (or 1 is added)
1444      */
1445     private static String provideAvailableNameForGenTOBuilder(final String name) {
1446         final int dollar = name.indexOf('$');
1447         if (dollar == -1) {
1448             return name + "$1";
1449         }
1450
1451         final int newSuffix = Integer.parseUnsignedInt(name.substring(dollar + 1)) + 1;
1452         Preconditions.checkState(newSuffix > 0, "Suffix counter overflow");
1453         return name.substring(0, dollar + 1) + newSuffix;
1454     }
1455
1456     public static void addUnitsToGenTO(final GeneratedTOBuilder to, final String units) {
1457         if (!Strings.isNullOrEmpty(units)) {
1458             to.addConstant(Types.STRING, "_UNITS", "\"" + units + "\"");
1459             final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("UNITS");
1460             prop.setReturnType(Types.STRING);
1461             to.addToStringProperty(prop);
1462         }
1463     }
1464
1465     @Override
1466     public String getTypeDefaultConstruction(final LeafSchemaNode node) {
1467         return getTypeDefaultConstruction(node, (String) node.getType().getDefaultValue().orElse(null));
1468     }
1469
1470     public String getTypeDefaultConstruction(final LeafSchemaNode node, final String defaultValue) {
1471         final TypeDefinition<?> type = CompatUtils.compatLeafType(node);
1472         final QName typeQName = type.getQName();
1473         final TypeDefinition<?> base = baseTypeDefForExtendedType(type);
1474         Preconditions.checkNotNull(type, "Cannot provide default construction for null type of %s", node);
1475         Preconditions.checkNotNull(defaultValue, "Cannot provide default construction for null default statement of %s",
1476                 node);
1477
1478         final StringBuilder sb = new StringBuilder();
1479         String result = null;
1480         if (base instanceof BinaryTypeDefinition) {
1481             result = binaryToDef(defaultValue);
1482         } else if (base instanceof BitsTypeDefinition) {
1483             String parentName;
1484             String className;
1485             final Module parent = getParentModule(node);
1486             final Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
1487             path.next();
1488             if (!path.hasNext()) {
1489                 parentName = BindingMapping.getClassName(parent.getName()) + "Data";
1490                 final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
1491                 className = basePackageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
1492             } else {
1493                 final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
1494                 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
1495                 parentName = BindingMapping.getClassName(parent.getName());
1496                 className = packageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
1497             }
1498             result = bitsToDef((BitsTypeDefinition) base, className, defaultValue, type.getBaseType() != null);
1499         } else if (base instanceof BooleanTypeDefinition) {
1500             result = typeToBooleanDef(defaultValue);
1501         } else if (base instanceof DecimalTypeDefinition) {
1502             result = typeToDef(BigDecimal.class, defaultValue);
1503         } else if (base instanceof EmptyTypeDefinition) {
1504             result = typeToBooleanDef(defaultValue);
1505         } else if (base instanceof EnumTypeDefinition) {
1506             final char[] defValArray = defaultValue.toCharArray();
1507             final char first = Character.toUpperCase(defaultValue.charAt(0));
1508             defValArray[0] = first;
1509             final String newDefVal = new String(defValArray);
1510             String className;
1511             if (type.getBaseType() != null) {
1512                 final Module m = getParentModule(type);
1513                 final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
1514                 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
1515                 className = packageName + "." + BindingMapping.getClassName(typeQName);
1516             } else {
1517                 final Module parentModule = getParentModule(node);
1518                 final String basePackageName = BindingMapping.getRootPackageName(parentModule.getQNameModule());
1519                 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, node.getPath());
1520                 className = packageName + "." + BindingMapping.getClassName(node.getQName());
1521             }
1522             result = className + "." + newDefVal;
1523         } else if (base instanceof IdentityrefTypeDefinition) {
1524             throw new UnsupportedOperationException("Cannot get default construction for identityref type");
1525         } else if (base instanceof InstanceIdentifierTypeDefinition) {
1526             throw new UnsupportedOperationException("Cannot get default construction for instance-identifier type");
1527         } else if (BaseTypes.isInt8(base)) {
1528             result = typeToValueOfDef(Byte.class, defaultValue);
1529         } else if (BaseTypes.isInt16(base)) {
1530             result = typeToValueOfDef(Short.class, defaultValue);
1531         } else if (BaseTypes.isInt32(base)) {
1532             result = typeToValueOfDef(Integer.class, defaultValue);
1533         } else if (BaseTypes.isInt64(base)) {
1534             result = typeToValueOfDef(Long.class, defaultValue);
1535         } else if (base instanceof LeafrefTypeDefinition) {
1536             result = leafrefToDef(node, (LeafrefTypeDefinition) base, defaultValue);
1537         } else if (base instanceof StringTypeDefinition) {
1538             result = "\"" + defaultValue + "\"";
1539         } else if (BaseTypes.isUint8(base)) {
1540             result = typeToValueOfDef(Short.class, defaultValue);
1541         } else if (BaseTypes.isUint16(base)) {
1542             result = typeToValueOfDef(Integer.class, defaultValue);
1543         } else if (BaseTypes.isUint32(base)) {
1544             result = typeToValueOfDef(Long.class, defaultValue);
1545         } else if (BaseTypes.isUint64(base)) {
1546             switch (defaultValue) {
1547                 case "0":
1548                     result = "java.math.BigInteger.ZERO";
1549                     break;
1550                 case "1":
1551                     result = "java.math.BigInteger.ONE";
1552                     break;
1553                 case "10":
1554                     result = "java.math.BigInteger.TEN";
1555                     break;
1556                 default:
1557                     result = typeToDef(BigInteger.class, defaultValue);
1558             }
1559         } else if (base instanceof UnionTypeDefinition) {
1560             result = unionToDef(node);
1561         } else {
1562             result = "";
1563         }
1564         sb.append(result);
1565
1566         if (type.getBaseType() != null && !(base instanceof LeafrefTypeDefinition)
1567                 && !(base instanceof EnumTypeDefinition) && !(base instanceof UnionTypeDefinition)) {
1568             final Module m = getParentModule(type);
1569             final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
1570             final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
1571             final String className = packageName + "." + BindingMapping.getClassName(typeQName);
1572             sb.insert(0, "new " + className + "(");
1573             sb.insert(sb.length(), ')');
1574         }
1575
1576         return sb.toString();
1577     }
1578
1579     private static String typeToDef(final Class<?> clazz, final String defaultValue) {
1580         return "new " + clazz.getName() + "(\"" + defaultValue + "\")";
1581     }
1582
1583     private static String typeToValueOfDef(final Class<?> clazz, final String defaultValue) {
1584         return clazz.getName() + ".valueOf(\"" + defaultValue + "\")";
1585     }
1586
1587     private static String typeToBooleanDef(final String defaultValue) {
1588         switch (defaultValue) {
1589             case "false":
1590                 return "java.lang.Boolean.FALSE";
1591             case "true":
1592                 return "java.lang.Boolean.TRUE";
1593             default:
1594                 return typeToValueOfDef(Boolean.class, defaultValue);
1595         }
1596     }
1597
1598     private static String binaryToDef(final String defaultValue) {
1599         final StringBuilder sb = new StringBuilder();
1600         final BaseEncoding en = BaseEncoding.base64();
1601         final byte[] encoded = en.decode(defaultValue);
1602         sb.append("new byte[] {");
1603         for (int i = 0; i < encoded.length; i++) {
1604             sb.append(encoded[i]);
1605             if (i != encoded.length - 1) {
1606                 sb.append(", ");
1607             }
1608         }
1609         sb.append('}');
1610         return sb.toString();
1611     }
1612
1613     private static final Comparator<Bit> BIT_NAME_COMPARATOR = Comparator.comparing(Bit::getName);
1614
1615     private static String bitsToDef(final BitsTypeDefinition type, final String className, final String defaultValue, final boolean isExt) {
1616         final List<Bit> bits = new ArrayList<>(type.getBits());
1617         bits.sort(BIT_NAME_COMPARATOR);
1618         final StringBuilder sb = new StringBuilder();
1619         if (!isExt) {
1620             sb.append("new ");
1621             sb.append(className);
1622             sb.append('(');
1623         }
1624         for (int i = 0; i < bits.size(); i++) {
1625             if (bits.get(i).getName().equals(defaultValue)) {
1626                 sb.append(true);
1627             } else {
1628                 sb.append(false);
1629             }
1630             if (i != bits.size() - 1) {
1631                 sb.append(", ");
1632             }
1633         }
1634         if (!isExt) {
1635             sb.append(')');
1636         }
1637         return sb.toString();
1638     }
1639
1640     private Module getParentModule(final SchemaNode node) {
1641         final QName qname = node.getPath().getPathFromRoot().iterator().next();
1642         return schemaContext.findModule(qname.getModule()).orElse(null);
1643     }
1644
1645     private String leafrefToDef(final LeafSchemaNode parentNode, final LeafrefTypeDefinition leafrefType, final String defaultValue) {
1646         Preconditions.checkArgument(leafrefType != null, "Leafref Type Definition reference cannot be NULL!");
1647         Preconditions.checkArgument(leafrefType.getPathStatement() != null,
1648                 "The Path Statement for Leafref Type Definition cannot be NULL!");
1649
1650         final RevisionAwareXPath xpath = leafrefType.getPathStatement();
1651         final String strXPath = xpath.toString();
1652
1653         if (strXPath != null) {
1654             if (strXPath.indexOf('[') == -1) {
1655                 final Module module = findParentModule(schemaContext, parentNode);
1656                 if (module != null) {
1657                     final SchemaNode dataNode;
1658                     if (xpath.isAbsolute()) {
1659                         dataNode = findDataSchemaNode(schemaContext, module, xpath);
1660                     } else {
1661                         dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
1662                     }
1663                     final String result = getTypeDefaultConstruction((LeafSchemaNode) dataNode, defaultValue);
1664                     return result;
1665                 }
1666             } else {
1667                 return "new java.lang.Object()";
1668             }
1669         }
1670
1671         return null;
1672     }
1673
1674     private String unionToDef(final LeafSchemaNode node) {
1675         final TypeDefinition<?> type = CompatUtils.compatLeafType(node);
1676         String parentName;
1677         String className;
1678
1679         if (type.getBaseType() != null) {
1680             final QName typeQName = type.getQName();
1681             Module module = null;
1682             final Set<Module> modules = schemaContext.findModules(typeQName.getNamespace());
1683             if (modules.size() > 1) {
1684                 for (Module m : modules) {
1685                     if (m.getRevision().equals(typeQName.getRevision())) {
1686                         module = m;
1687                         break;
1688                     }
1689                 }
1690                 if (module == null) {
1691                     final List<Module> modulesList = new ArrayList<>(modules);
1692                     modulesList.sort((o1, o2) -> Revision.compare(o1.getRevision(), o2.getRevision()));
1693                     module = modulesList.get(0);
1694                 }
1695             } else {
1696                 module = modules.iterator().next();
1697             }
1698
1699             final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
1700             className = basePackageName + "." + BindingMapping.getClassName(typeQName);
1701         } else {
1702             final Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
1703             final QName first = path.next();
1704             final Module parent = schemaContext.findModule(first.getModule()).orElse(null);
1705             final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
1706             if (!path.hasNext()) {
1707                 parentName = BindingMapping.getClassName(parent.getName()) + "Data";
1708                 className = basePackageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
1709             } else {
1710                 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, UNION_PATH);
1711                 className = packageName + "." + BindingMapping.getClassName(node.getQName());
1712             }
1713         }
1714         return union(className, (String) node.getType().getDefaultValue().orElse(null), node);
1715     }
1716
1717     private static String union(final String className, final String defaultValue, final LeafSchemaNode node) {
1718         final StringBuilder sb = new StringBuilder();
1719         sb.append("new ");
1720         sb.append(className);
1721         sb.append("(\"");
1722         sb.append(defaultValue);
1723         sb.append("\".toCharArray())");
1724         return sb.toString();
1725     }
1726
1727     @Override
1728     public String getConstructorPropertyName(final SchemaNode node) {
1729         return node instanceof TypeDefinition<?> ? "value" : "";
1730     }
1731
1732     @Override
1733     public String getParamNameFromType(final TypeDefinition<?> type) {
1734         return BindingMapping.getPropertyName(type.getQName().getLocalName());
1735     }
1736 }