Fixed resolution problems in Code Generator
[yangtools.git] / code-generator / binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / binding / yang / types / TypeProviderImpl.java
index 01862296fa8bbaab40f295fd3f3766512e951a5d..8bd0c51e64ecf001b81e23efffbdf05dfea0f0ef 100644 (file)
@@ -7,8 +7,13 @@
  */
 package org.opendaylight.yangtools.sal.binding.yang.types;
 
-import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.*;
-import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.*;
+import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.moduleNamespaceToPackageName;
+import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.packageNameForGeneratedType;
+import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.parseToClassName;
+import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.parseToValidParamName;
+import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
+import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNodeForRelativeXPath;
+import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -20,6 +25,7 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.apache.commons.lang.StringEscapeUtils;
+import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
 import org.opendaylight.yangtools.binding.generator.util.TypeConstants;
 import org.opendaylight.yangtools.binding.generator.util.Types;
 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.EnumerationBuilderImpl;
@@ -31,7 +37,7 @@ import org.opendaylight.yangtools.sal.binding.model.api.Type;
 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder;
 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
-import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
+import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
@@ -40,12 +46,12 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
@@ -55,6 +61,8 @@ import org.opendaylight.yangtools.yang.model.util.StringType;
 import org.opendaylight.yangtools.yang.model.util.UnionType;
 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
 
+import com.google.common.base.Preconditions;
+
 public final class TypeProviderImpl implements TypeProvider {
     /**
      * Contains the schema data red from YANG files.
@@ -82,9 +90,7 @@ public final class TypeProviderImpl implements TypeProvider {
      *             if <code>schemaContext</code> equal null.
      */
     public TypeProviderImpl(final SchemaContext schemaContext) {
-        if (schemaContext == null) {
-            throw new IllegalArgumentException("Schema Context cannot be null!");
-        }
+        Preconditions.checkArgument(schemaContext != null, "Schema Context cannot be null!");
 
         this.schemaContext = schemaContext;
         this.genTypeDefsContextMap = new HashMap<>();
@@ -107,13 +113,10 @@ public final class TypeProviderImpl implements TypeProvider {
      * 
      */
     public void putReferencedType(final SchemaPath refTypePath, final Type refType) {
-        if (refTypePath == null) {
-            throw new IllegalArgumentException("Path reference of " + "Enumeration Type Definition cannot be NULL!");
-        }
+        Preconditions.checkArgument(refTypePath != null,
+                "Path reference of Enumeration Type Definition cannot be NULL!");
 
-        if (refType == null) {
-            throw new IllegalArgumentException("Reference to Enumeration " + "Type cannot be NULL!");
-        }
+        Preconditions.checkArgument(refType != null, "Reference to Enumeration Type cannot be NULL!");
         referencedTypes.put(refTypePath, refType);
     }
 
@@ -146,25 +149,23 @@ public final class TypeProviderImpl implements TypeProvider {
      *             </ul>
      */
     @Override
-    public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition) {
+    public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode) {
         Type returnType = null;
-        if (typeDefinition == null) {
-            throw new IllegalArgumentException("Type Definition cannot be NULL!");
-        }
+        Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
         if (typeDefinition.getQName() == null) {
             throw new IllegalArgumentException(
                     "Type Definition cannot have non specified QName (QName cannot be NULL!)");
         }
-        if (typeDefinition.getQName().getLocalName() == null) {
-            throw new IllegalArgumentException("Type Definitions Local Name cannot be NULL!");
-        }
+        Preconditions.checkArgument(typeDefinition.getQName().getLocalName() != null,
+                "Type Definitions Local Name cannot be NULL!");
 
         if (typeDefinition instanceof ExtendedType) {
             returnType = javaTypeForExtendedType(typeDefinition);
         } else {
-            returnType = javaTypeForLeafrefOrIdentityRef(typeDefinition);
+            returnType = javaTypeForLeafrefOrIdentityRef(typeDefinition, parentNode);
             if (returnType == null) {
-                returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(typeDefinition);
+                returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(typeDefinition,
+                        parentNode);
             }
         }
         // TODO: add throw exception when we will be able to resolve ALL yang
@@ -185,10 +186,10 @@ public final class TypeProviderImpl implements TypeProvider {
      *            type definition which is converted to JAVA <code>Type</code>
      * @return JAVA <code>Type</code> instance for <code>typeDefinition</code>
      */
-    private Type javaTypeForLeafrefOrIdentityRef(TypeDefinition<?> typeDefinition) {
+    private Type javaTypeForLeafrefOrIdentityRef(TypeDefinition<?> typeDefinition, SchemaNode parentNode) {
         if (typeDefinition instanceof LeafrefTypeDefinition) {
             final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) typeDefinition;
-            return provideTypeForLeafref(leafref);
+            return provideTypeForLeafref(leafref, parentNode);
         } else if (typeDefinition instanceof IdentityrefTypeDefinition) {
             final IdentityrefTypeDefinition idref = (IdentityrefTypeDefinition) typeDefinition;
             return provideTypeForIdentityref(idref);
@@ -209,26 +210,32 @@ public final class TypeProviderImpl implements TypeProvider {
         final String typedefName = typeDefinition.getQName().getLocalName();
         final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
         Type returnType = null;
-        returnType = javaTypeForLeafrefOrIdentityRef(baseTypeDef);
+        returnType = javaTypeForLeafrefOrIdentityRef(baseTypeDef, typeDefinition);
         if (returnType == null) {
             if (baseTypeDef instanceof EnumTypeDefinition) {
                 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypeDef;
-                returnType = provideTypeForEnum(enumTypeDef, typedefName);
+                returnType = provideTypeForEnum(enumTypeDef, typedefName, typeDefinition);
             } else {
-                final Module module = findParentModuleForTypeDefinition(schemaContext, typeDefinition);
+                final Module module = findParentModule(schemaContext, typeDefinition);
                 if (module != null) {
                     final Map<String, Type> genTOs = genTypeDefsContextMap.get(module.getName());
                     if (genTOs != null) {
                         returnType = genTOs.get(typedefName);
                     }
                     if (returnType == null) {
-                        returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
-                                .javaTypeForSchemaDefinitionType(baseTypeDef);
+                        returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(
+                                baseTypeDef, typeDefinition);
                     }
                 }
             }
         }
         return returnType;
+        // TODO: add throw exception when we will be able to resolve ALL yang
+        // types!
+        // if (returnType == null) {
+        // throw new IllegalArgumentException("Type Provider can't resolve " +
+        // "type for specified Type Definition " + typedefName);
+        // }
     }
 
     /**
@@ -248,16 +255,14 @@ public final class TypeProviderImpl implements TypeProvider {
      */
     private Type provideTypeForIdentityref(IdentityrefTypeDefinition idref) {
         QName baseIdQName = idref.getIdentity();
-        Module module = schemaContext.findModuleByNamespace(baseIdQName.getNamespace());
+        Module module = schemaContext.findModuleByNamespaceAndRevision(baseIdQName.getNamespace(),baseIdQName.getRevision());
         IdentitySchemaNode identity = null;
         for (IdentitySchemaNode id : module.getIdentities()) {
             if (id.getQName().equals(baseIdQName)) {
                 identity = id;
             }
         }
-        if (identity == null) {
-            throw new IllegalArgumentException("Target identity '" + baseIdQName + "' do not exists");
-        }
+        Preconditions.checkArgument(identity != null, "Target identity '" + baseIdQName + "' do not exists");
 
         final String basePackageName = moduleNamespaceToPackageName(module);
         final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
@@ -283,25 +288,23 @@ public final class TypeProviderImpl implements TypeProvider {
      *             <li>if name of <code>typeDefinition</code></li>
      *             </ul>
      */
-    public Type generatedTypeForExtendedDefinitionType(final TypeDefinition<?> typeDefinition) {
+    public Type generatedTypeForExtendedDefinitionType(final TypeDefinition<?> typeDefinition,
+            final SchemaNode parentNode) {
         Type returnType = null;
-        if (typeDefinition == null) {
-            throw new IllegalArgumentException("Type Definition cannot be NULL!");
-        }
+        Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
         if (typeDefinition.getQName() == null) {
             throw new IllegalArgumentException(
                     "Type Definition cannot have non specified QName (QName cannot be NULL!)");
         }
-        if (typeDefinition.getQName().getLocalName() == null) {
-            throw new IllegalArgumentException("Type Definitions Local Name cannot be NULL!");
-        }
+        Preconditions.checkArgument(typeDefinition.getQName().getLocalName() != null,
+                "Type Definitions Local Name cannot be NULL!");
 
         final String typedefName = typeDefinition.getQName().getLocalName();
         if (typeDefinition instanceof ExtendedType) {
             final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
 
             if (!(baseTypeDef instanceof LeafrefTypeDefinition) && !(baseTypeDef instanceof IdentityrefTypeDefinition)) {
-                final Module module = findParentModuleForTypeDefinition(schemaContext, typeDefinition);
+                final Module module = findParentModule(schemaContext, parentNode);
 
                 if (module != null) {
                     final Map<String, Type> genTOs = genTypeDefsContextMap.get(module.getName());
@@ -325,9 +328,7 @@ public final class TypeProviderImpl implements TypeProvider {
      *             if <code>extendTypeDef</code> equal null
      */
     private TypeDefinition<?> baseTypeDefForExtendedType(final TypeDefinition<?> extendTypeDef) {
-        if (extendTypeDef == null) {
-            throw new IllegalArgumentException("Type Definiition reference cannot be NULL!");
-        }
+        Preconditions.checkArgument(extendTypeDef != null, "Type Definiition reference cannot be NULL!");
         final TypeDefinition<?> baseTypeDef = extendTypeDef.getBaseType();
         if (baseTypeDef instanceof ExtendedType) {
             return baseTypeDefForExtendedType(baseTypeDef);
@@ -354,15 +355,12 @@ public final class TypeProviderImpl implements TypeProvider {
      *             </ul>
      * 
      */
-    public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType) {
+    public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType, final SchemaNode parentNode) {
         Type returnType = null;
-        if (leafrefType == null) {
-            throw new IllegalArgumentException("Leafref Type Definition reference cannot be NULL!");
-        }
+        Preconditions.checkArgument(leafrefType != null, "Leafref Type Definition reference cannot be NULL!");
 
-        if (leafrefType.getPathStatement() == null) {
-            throw new IllegalArgumentException("The Path Statement for Leafref Type Definition cannot be NULL!");
-        }
+        Preconditions.checkArgument(leafrefType.getPathStatement() != null,
+                "The Path Statement for Leafref Type Definition cannot be NULL!");
 
         final RevisionAwareXPath xpath = leafrefType.getPathStatement();
         final String strXPath = xpath.toString();
@@ -371,13 +369,13 @@ public final class TypeProviderImpl implements TypeProvider {
             if (strXPath.contains("[")) {
                 returnType = Types.typeForClass(Object.class);
             } else {
-                final Module module = findParentModuleForTypeDefinition(schemaContext, leafrefType);
+                final Module module = findParentModule(schemaContext, parentNode);
                 if (module != null) {
-                    final DataSchemaNode dataNode;
+                    final SchemaNode dataNode;
                     if (xpath.isAbsolute()) {
                         dataNode = findDataSchemaNode(schemaContext, module, xpath);
                     } else {
-                        dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, leafrefType, xpath);
+                        dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
                     }
 
                     if (leafContainsEnumDefinition(dataNode)) {
@@ -406,7 +404,7 @@ public final class TypeProviderImpl implements TypeProvider {
      *         <li>false - other cases</li>
      *         </ul>
      */
-    private boolean leafContainsEnumDefinition(final DataSchemaNode dataNode) {
+    private boolean leafContainsEnumDefinition(final SchemaNode dataNode) {
         if (dataNode instanceof LeafSchemaNode) {
             final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
             if (leaf.getType() instanceof EnumTypeDefinition) {
@@ -430,7 +428,7 @@ public final class TypeProviderImpl implements TypeProvider {
      *         <li>false - other cases</li>
      *         </ul>
      */
-    private boolean leafListContainsEnumDefinition(final DataSchemaNode dataNode) {
+    private boolean leafListContainsEnumDefinition(final SchemaNode dataNode) {
         if (dataNode instanceof LeafListSchemaNode) {
             final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
             if (leafList.getType() instanceof EnumTypeDefinition) {
@@ -459,23 +457,18 @@ public final class TypeProviderImpl implements TypeProvider {
      *             <li>if name of <code>enumTypeDef</code> equal null</li>
      *             </ul>
      */
-    private Enumeration provideTypeForEnum(final EnumTypeDefinition enumTypeDef, final String enumName) {
-        if (enumTypeDef == null) {
-            throw new IllegalArgumentException("EnumTypeDefinition reference cannot be NULL!");
-        }
-        if (enumTypeDef.getValues() == null) {
-            throw new IllegalArgumentException("EnumTypeDefinition MUST contain at least ONE value definition!");
-        }
-        if (enumTypeDef.getQName() == null) {
-            throw new IllegalArgumentException("EnumTypeDefinition MUST contain NON-NULL QName!");
-        }
-        if (enumTypeDef.getQName().getLocalName() == null) {
-            throw new IllegalArgumentException("Local Name in EnumTypeDefinition QName cannot be NULL!");
-        }
+    private Enumeration provideTypeForEnum(final EnumTypeDefinition enumTypeDef, final String enumName,
+            final SchemaNode parentNode) {
+        Preconditions.checkArgument(enumTypeDef != null, "EnumTypeDefinition reference cannot be NULL!");
+        Preconditions.checkArgument(enumTypeDef.getValues() != null,
+                "EnumTypeDefinition MUST contain at least ONE value definition!");
+        Preconditions.checkArgument(enumTypeDef.getQName() != null, "EnumTypeDefinition MUST contain NON-NULL QName!");
+        Preconditions.checkArgument(enumTypeDef.getQName().getLocalName() != null,
+                "Local Name in EnumTypeDefinition QName cannot be NULL!");
 
         final String enumerationName = parseToClassName(enumName);
 
-        Module module = findParentModuleForTypeDefinition(schemaContext, enumTypeDef);
+        Module module = findParentModule(schemaContext, parentNode);
         final String basePackageName = moduleNamespaceToPackageName(module);
 
         final EnumBuilder enumBuilder = new EnumerationBuilderImpl(basePackageName, enumerationName);
@@ -507,22 +500,14 @@ public final class TypeProviderImpl implements TypeProvider {
      * 
      */
     private Enumeration addInnerEnumerationToTypeBuilder(final EnumTypeDefinition enumTypeDef, final String enumName,
-            final GeneratedTypeBuilder typeBuilder) {
-        if (enumTypeDef == null) {
-            throw new IllegalArgumentException("EnumTypeDefinition reference cannot be NULL!");
-        }
-        if (enumTypeDef.getValues() == null) {
-            throw new IllegalArgumentException("EnumTypeDefinition MUST contain at least ONE value definition!");
-        }
-        if (enumTypeDef.getQName() == null) {
-            throw new IllegalArgumentException("EnumTypeDefinition MUST contain NON-NULL QName!");
-        }
-        if (enumTypeDef.getQName().getLocalName() == null) {
-            throw new IllegalArgumentException("Local Name in EnumTypeDefinition QName cannot be NULL!");
-        }
-        if (typeBuilder == null) {
-            throw new IllegalArgumentException("Generated Type Builder reference cannot be NULL!");
-        }
+            final GeneratedTypeBuilderBase<?> typeBuilder) {
+        Preconditions.checkArgument(enumTypeDef != null, "EnumTypeDefinition reference cannot be NULL!");
+        Preconditions.checkArgument(enumTypeDef.getValues() != null,
+                "EnumTypeDefinition MUST contain at least ONE value definition!");
+        Preconditions.checkArgument(enumTypeDef.getQName() != null, "EnumTypeDefinition MUST contain NON-NULL QName!");
+        Preconditions.checkArgument(enumTypeDef.getQName().getLocalName() != null,
+                "Local Name in EnumTypeDefinition QName cannot be NULL!");
+        Preconditions.checkArgument(typeBuilder != null, "Generated Type Builder reference cannot be NULL!");
 
         final String enumerationName = parseToClassName(enumName);
 
@@ -538,15 +523,15 @@ public final class TypeProviderImpl implements TypeProvider {
      *            contains information about YANG type
      * @return JAVA <code>Type</code> representation of <code>dataNode</code>
      */
-    private Type resolveTypeFromDataSchemaNode(final DataSchemaNode dataNode) {
+    private Type resolveTypeFromDataSchemaNode(final SchemaNode dataNode) {
         Type returnType = null;
         if (dataNode != null) {
             if (dataNode instanceof LeafSchemaNode) {
                 final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
-                returnType = javaTypeForSchemaDefinitionType(leaf.getType());
+                returnType = javaTypeForSchemaDefinitionType(leaf.getType(), leaf);
             } else if (dataNode instanceof LeafListSchemaNode) {
                 final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
-                returnType = javaTypeForSchemaDefinitionType(leafList.getType());
+                returnType = javaTypeForSchemaDefinitionType(leafList.getType(), leafList);
             }
         }
         return returnType;
@@ -568,9 +553,7 @@ public final class TypeProviderImpl implements TypeProvider {
      */
     private void resolveTypeDefsFromContext() {
         final Set<Module> modules = schemaContext.getModules();
-        if (modules == null) {
-            throw new IllegalArgumentException("Sef of Modules cannot be NULL!");
-        }
+        Preconditions.checkArgument(modules != null, "Sef of Modules cannot be NULL!");
         final Module[] modulesArray = new Module[modules.size()];
         int i = 0;
         for (Module modul : modules) {
@@ -628,11 +611,11 @@ public final class TypeProviderImpl implements TypeProvider {
                     returnType = provideGeneratedTOFromExtendedType(innerExtendedType, basePackageName, typedefName);
                 } else if (innerTypeDefinition instanceof UnionTypeDefinition) {
                     final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForUnionTypeDef(basePackageName,
-                            typedef, typedefName);
+                            typedef, typedefName, typedef);
                     returnType = genTOBuilder.toInstance();
                 } else if (innerTypeDefinition instanceof EnumTypeDefinition) {
                     final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) innerTypeDefinition;
-                    returnType = provideTypeForEnum(enumTypeDef, typedefName);
+                    returnType = provideTypeForEnum(enumTypeDef, typedefName, typedef);
 
                 } else if (innerTypeDefinition instanceof BitsTypeDefinition) {
                     final BitsTypeDefinition bitsTypeDefinition = (BitsTypeDefinition) innerTypeDefinition;
@@ -641,8 +624,8 @@ public final class TypeProviderImpl implements TypeProvider {
                     returnType = genTOBuilder.toInstance();
 
                 } else {
-                    final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
-                            .javaTypeForSchemaDefinitionType(innerTypeDefinition);
+                    final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(
+                            innerTypeDefinition, typedef);
 
                     returnType = wrapJavaTypeIntoTO(basePackageName, typedef, javaType);
                 }
@@ -672,13 +655,10 @@ public final class TypeProviderImpl implements TypeProvider {
     private GeneratedTransferObject wrapJavaTypeIntoTO(final String basePackageName, final TypeDefinition<?> typedef,
             final Type javaType) {
         if (javaType != null) {
-            final String typedefName = typedef.getQName().getLocalName();
-            final String propertyName = parseToValidParamName(typedefName);
+            final String propertyName = "value";
 
             final GeneratedTOBuilder genTOBuilder = typedefToTransferObject(basePackageName, typedef);
-
             final GeneratedPropertyBuilder genPropBuilder = genTOBuilder.addProperty(propertyName);
-
             genPropBuilder.setReturnType(javaType);
             genTOBuilder.addEqualsIdentity(genPropBuilder);
             genTOBuilder.addHashIdentity(genPropBuilder);
@@ -707,9 +687,9 @@ public final class TypeProviderImpl implements TypeProvider {
      *         builders
      */
     public GeneratedTOBuilder provideGeneratedTOBuilderForUnionTypeDef(final String basePackageName,
-            final TypeDefinition<?> typedef, String typeDefName) {
+            final TypeDefinition<?> typedef, String typeDefName, SchemaNode parentNode) {
         final List<GeneratedTOBuilder> genTOBuilders = provideGeneratedTOBuildersForUnionTypeDef(basePackageName,
-                typedef, typeDefName);
+                typedef, typeDefName, parentNode);
         GeneratedTOBuilder resultTOBuilder = null;
         if (!genTOBuilders.isEmpty()) {
             resultTOBuilder = genTOBuilders.get(0);
@@ -742,17 +722,11 @@ public final class TypeProviderImpl implements TypeProvider {
      *             </ul>
      */
     public List<GeneratedTOBuilder> provideGeneratedTOBuildersForUnionTypeDef(final String basePackageName,
-            final TypeDefinition<?> typedef, final String typeDefName) {
-        if (basePackageName == null) {
-            throw new IllegalArgumentException("Base Package Name cannot be NULL!");
-        }
-        if (typedef == null) {
-            throw new IllegalArgumentException("Type Definition cannot be NULL!");
-        }
-        if (typedef.getQName() == null) {
-            throw new IllegalArgumentException(
-                    "Type Definition cannot have non specified QName (QName cannot be NULL!)");
-        }
+            final TypeDefinition<?> typedef, final String typeDefName, final SchemaNode parentNode) {
+        Preconditions.checkArgument(basePackageName != null, "Base Package Name cannot be NULL!");
+        Preconditions.checkArgument(typedef != null, "Type Definition cannot be NULL!");
+        Preconditions.checkArgument(typedef.getQName() != null,
+                "Type Definition cannot have non specified QName (QName cannot be NULL!)");
 
         final List<GeneratedTOBuilder> generatedTOBuilders = new ArrayList<>();
 
@@ -770,33 +744,30 @@ public final class TypeProviderImpl implements TypeProvider {
             }
             generatedTOBuilders.add(unionGenTOBuilder);
             unionGenTOBuilder.setIsUnion(true);
-
             final List<String> regularExpressions = new ArrayList<String>();
             for (final TypeDefinition<?> unionType : unionTypes) {
                 final String unionTypeName = unionType.getQName().getLocalName();
                 if (unionType instanceof UnionType) {
-                    generatedTOBuilders
-                            .addAll(resolveUnionSubtypeAsUnion(unionGenTOBuilder, unionType, basePackageName));
+                    generatedTOBuilders.addAll(resolveUnionSubtypeAsUnion(unionGenTOBuilder, unionType,
+                            basePackageName, parentNode));
                 } else if (unionType instanceof ExtendedType) {
                     resolveExtendedSubtypeAsUnion(unionGenTOBuilder, (ExtendedType) unionType, unionTypeName,
-                            regularExpressions);
+                            regularExpressions, parentNode);
                 } else if (unionType instanceof EnumTypeDefinition) {
                     final Enumeration enumeration = addInnerEnumerationToTypeBuilder((EnumTypeDefinition) unionType,
                             unionTypeName, unionGenTOBuilder);
                     updateUnionTypeAsProperty(unionGenTOBuilder, enumeration, unionTypeName);
                 } else {
-                    final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
-                            .javaTypeForSchemaDefinitionType(unionType);
-                    if (javaType != null) {
-                        updateUnionTypeAsProperty(unionGenTOBuilder, javaType, unionTypeName);
-                    }
+                    final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(
+                            unionType, parentNode);
+                    updateUnionTypeAsProperty(unionGenTOBuilder, javaType, unionTypeName);
                 }
             }
             if (!regularExpressions.isEmpty()) {
                 addStringRegExAsConstant(unionGenTOBuilder, regularExpressions);
             }
 
-            storeGenTO(typedef, unionGenTOBuilder);
+            storeGenTO(typedef, unionGenTOBuilder, parentNode);
         }
         return generatedTOBuilders;
     }
@@ -824,12 +795,14 @@ public final class TypeProviderImpl implements TypeProvider {
      *         <code>provideGeneratedTOBuildersForUnionTypeDef</code> method.
      */
     private List<GeneratedTOBuilder> resolveUnionSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
-            final TypeDefinition<?> unionSubtype, final String basePackageName) {
+            final TypeDefinition<?> unionSubtype, final String basePackageName, final SchemaNode parentNode) {
         final String newTOBuilderName = provideAvailableNameForGenTOBuilder(parentUnionGenTOBuilder.getName());
         final List<GeneratedTOBuilder> subUnionGenTOBUilders = provideGeneratedTOBuildersForUnionTypeDef(
-                basePackageName, unionSubtype, newTOBuilderName);
+                basePackageName, unionSubtype, newTOBuilderName, parentNode);
 
-        GeneratedPropertyBuilder propertyBuilder = parentUnionGenTOBuilder.addProperty(newTOBuilderName);
+        final GeneratedPropertyBuilder propertyBuilder;
+        propertyBuilder = parentUnionGenTOBuilder.addProperty(BindingGeneratorUtil
+                .parseToValidParamName(newTOBuilderName));
         propertyBuilder.setReturnType(subUnionGenTOBUilders.get(0));
         parentUnionGenTOBuilder.addEqualsIdentity(propertyBuilder);
         parentUnionGenTOBuilder.addToStringProperty(propertyBuilder);
@@ -856,14 +829,16 @@ public final class TypeProviderImpl implements TypeProvider {
      *            list of strings with the regular expressions
      */
     private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
-            final ExtendedType unionSubtype, final String unionTypeName, final List<String> regularExpressions) {
-        final Type genTO = findGenTO(unionSubtype, unionTypeName);
+            final ExtendedType unionSubtype, final String unionTypeName, final List<String> regularExpressions,
+            final SchemaNode parentNode) {
+        final Type genTO = findGenTO(unionTypeName, parentNode);
         if (genTO != null) {
             updateUnionTypeAsProperty(parentUnionGenTOBuilder, genTO, genTO.getName());
         } else {
             final TypeDefinition<?> baseType = baseTypeDefForExtendedType(unionSubtype);
             if (unionTypeName.equals(baseType.getQName().getLocalName())) {
-                final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(baseType);
+                final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(baseType,
+                        parentNode);
                 if (javaType != null) {
                     updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, unionTypeName);
                 }
@@ -878,15 +853,13 @@ public final class TypeProviderImpl implements TypeProvider {
      * Searches for generated TO for <code>searchedTypeDef</code> type
      * definition in {@link #genTypeDefsContextMap genTypeDefsContextMap}
      * 
-     * @param searchedTypeDef
-     *            type definition for which is generatet TO sought
      * @param searchedTypeName
      *            string with name of <code>searchedTypeDef</code>
      * @return generated TO for <code>searchedTypeDef</code> or
      *         <code>null</code> it it doesn't exist
      */
-    private Type findGenTO(final TypeDefinition<?> searchedTypeDef, final String searchedTypeName) {
-        final Module typeModule = findParentModuleForTypeDefinition(schemaContext, searchedTypeDef);
+    private Type findGenTO(final String searchedTypeName, final SchemaNode parentNode) {
+        final Module typeModule = findParentModule(schemaContext, parentNode);
         if (typeModule != null && typeModule.getName() != null) {
             final Map<String, Type> genTOs = genTypeDefsContextMap.get(typeModule.getName());
             if (genTOs != null) {
@@ -907,10 +880,10 @@ public final class TypeProviderImpl implements TypeProvider {
      *            generated TO builder which is converted to generated TO and
      *            stored
      */
-    private void storeGenTO(TypeDefinition<?> newTypeDef, GeneratedTOBuilder genTOBuilder) {
+    private void storeGenTO(TypeDefinition<?> newTypeDef, GeneratedTOBuilder genTOBuilder, SchemaNode parentNode) {
         if (!(newTypeDef instanceof UnionType)) {
             Map<String, Type> genTOsMap = null;
-            final Module parentModule = findParentModuleForTypeDefinition(schemaContext, newTypeDef);
+            final Module parentModule = findParentModule(schemaContext, parentNode);
             if (parentModule != null && parentModule.getName() != null) {
                 genTOsMap = genTypeDefsContextMap.get(parentModule.getName());
                 genTOsMap.put(newTypeDef.getQName().getLocalName(), genTOBuilder.toInstance());
@@ -933,16 +906,14 @@ public final class TypeProviderImpl implements TypeProvider {
      */
     private void updateUnionTypeAsProperty(final GeneratedTOBuilder unionGenTransObject, final Type type,
             final String propertyName) {
-        if (unionGenTransObject != null && type != null) {
-            if (!unionGenTransObject.containsProperty(propertyName)) {
-                final GeneratedPropertyBuilder propBuilder = unionGenTransObject
-                        .addProperty(parseToValidParamName(propertyName));
-                propBuilder.setReturnType(type);
-
-                unionGenTransObject.addEqualsIdentity(propBuilder);
-                unionGenTransObject.addHashIdentity(propBuilder);
-                unionGenTransObject.addToStringProperty(propBuilder);
-            }
+        if (unionGenTransObject != null && type != null && !unionGenTransObject.containsProperty(propertyName)) {
+            final GeneratedPropertyBuilder propBuilder = unionGenTransObject
+                    .addProperty(parseToValidParamName(propertyName));
+            propBuilder.setReturnType(type);
+
+            unionGenTransObject.addEqualsIdentity(propBuilder);
+            unionGenTransObject.addHashIdentity(propBuilder);
+            unionGenTransObject.addToStringProperty(propBuilder);
         }
     }
 
@@ -964,7 +935,7 @@ public final class TypeProviderImpl implements TypeProvider {
         if ((packageName != null) && (typedef != null) && (typeDefTOName != null)) {
             final String genTOName = parseToClassName(typeDefTOName);
             final GeneratedTOBuilder newType = new GeneratedTOBuilderImpl(packageName, genTOName);
-
+            newType.addComment(typedef.getDescription());
             return newType;
         }
         return null;
@@ -993,12 +964,8 @@ public final class TypeProviderImpl implements TypeProvider {
     public GeneratedTOBuilder provideGeneratedTOBuilderForBitsTypeDefinition(final String basePackageName,
             final TypeDefinition<?> typeDef, String typeDefName) {
 
-        if (typeDef == null) {
-            throw new IllegalArgumentException("typeDef cannot be NULL!");
-        }
-        if (basePackageName == null) {
-            throw new IllegalArgumentException("Base Package Name cannot be NULL!");
-        }
+        Preconditions.checkArgument(typeDef != null, "typeDef cannot be NULL!");
+        Preconditions.checkArgument(basePackageName != null, "Base Package Name cannot be NULL!");
 
         if (typeDef instanceof BitsTypeDefinition) {
             BitsTypeDefinition bitsTypeDefinition = (BitsTypeDefinition) typeDef;
@@ -1037,9 +1004,7 @@ public final class TypeProviderImpl implements TypeProvider {
      */
     private List<String> resolveRegExpressionsFromTypedef(ExtendedType typedef) {
         final List<String> regExps = new ArrayList<String>();
-        if (typedef == null) {
-            throw new IllegalArgumentException("typedef can't be null");
-        }
+        Preconditions.checkArgument(typedef != null, "typedef can't be null");
         final TypeDefinition<?> strTypeDef = baseTypeDefForExtendedType(typedef);
         if (strTypeDef instanceof StringType) {
             final List<PatternConstraint> patternConstraints = typedef.getPatterns();
@@ -1073,11 +1038,12 @@ public final class TypeProviderImpl implements TypeProvider {
      *             </ul>
      */
     private void addStringRegExAsConstant(GeneratedTOBuilder genTOBuilder, List<String> regularExpressions) {
-        if (genTOBuilder == null)
-            throw new IllegalArgumentException("genTOBuilder can't be null");
-        if (regularExpressions == null)
-            throw new IllegalArgumentException("regularExpressions can't be null");
-
+        if (genTOBuilder == null) {
+            throw new IllegalArgumentException("Generated transfer object builder can't be null");
+        }
+        if (regularExpressions == null) {
+            throw new IllegalArgumentException("List of regular expressions can't be null");
+        }
         if (!regularExpressions.isEmpty()) {
             genTOBuilder.addConstant(Types.listTypeFor(BaseYangTypes.STRING_TYPE), TypeConstants.PATTERN_CONSTANT_NAME,
                     regularExpressions);
@@ -1112,22 +1078,16 @@ public final class TypeProviderImpl implements TypeProvider {
     private GeneratedTransferObject provideGeneratedTOFromExtendedType(final ExtendedType innerExtendedType,
             final String basePackageName, final String typedefName) {
 
-        if (innerExtendedType == null) {
-            throw new IllegalArgumentException("Extended type cannot be NULL!");
-        }
-        if (basePackageName == null) {
-            throw new IllegalArgumentException("String with base package name cannot be NULL!");
-        }
-        if (typedefName == null) {
-            throw new IllegalArgumentException("String with type definition name cannot be NULL!");
-        }
+        Preconditions.checkArgument(innerExtendedType != null, "Extended type cannot be NULL!");
+        Preconditions.checkArgument(basePackageName != null, "String with base package name cannot be NULL!");
+        Preconditions.checkArgument(typedefName != null, "String with type definition name cannot be NULL!");
 
         final String classTypedefName = parseToClassName(typedefName);
         final String innerTypeDef = innerExtendedType.getQName().getLocalName();
         final GeneratedTOBuilder genTOBuilder = new GeneratedTOBuilderImpl(basePackageName, classTypedefName);
 
         Map<String, Type> typeMap = null;
-        final Module parentModule = findParentModuleForTypeDefinition(schemaContext, innerExtendedType);
+        final Module parentModule = findParentModule(schemaContext, innerExtendedType);
         if (parentModule != null) {
             typeMap = genTypeDefsContextMap.get(parentModule.getName());
         }
@@ -1170,9 +1130,8 @@ public final class TypeProviderImpl implements TypeProvider {
             }
             typeDefinitionsConcreteDepth.add(unsortedTypeDefinition);
         }
-
-        Set<Integer> depths = typeDefinitionsDepths.keySet(); // keys are in
-                                                              // ascending order
+        // keys are in ascending order
+        Set<Integer> depths = typeDefinitionsDepths.keySet();
         for (Integer depth : depths) {
             sortedTypeDefinition.addAll(typeDefinitionsDepths.get(depth));
         }
@@ -1190,9 +1149,7 @@ public final class TypeProviderImpl implements TypeProvider {
      *         definition to the base type
      */
     private int getTypeDefinitionDepth(final TypeDefinition<?> typeDefinition) {
-        if (typeDefinition == null) {
-            throw new IllegalArgumentException("Type definition can't be null");
-        }
+        Preconditions.checkArgument(typeDefinition != null, "Type definition can't be null");
         int depth = 1;
         TypeDefinition<?> baseType = typeDefinition.getBaseType();