Fix checkstyle in mdsal-binding-generator-impl
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / impl / AbstractTypeGenerator.java
index 947191e53af93d9956a5b8e34832e94e663c7f72..f0da9644c9d79c17ff9cb368e2d1f943e8561d1b 100644 (file)
@@ -9,19 +9,31 @@ package org.opendaylight.mdsal.binding.generator.impl;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.computeDefaultSUID;
 import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.packageNameForAugmentedGeneratedType;
 import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.packageNameForGeneratedType;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.BASE_IDENTITY;
 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.DATA_OBJECT;
 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.DATA_ROOT;
-import static org.opendaylight.mdsal.binding.model.util.BindingTypes.IDENTIFIABLE;
-import static org.opendaylight.mdsal.binding.model.util.BindingTypes.IDENTIFIER;
 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.NOTIFICATION;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.NOTIFICATION_LISTENER;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.ROUTING_CONTEXT;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.RPC_INPUT;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.RPC_OUTPUT;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.RPC_SERVICE;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.action;
 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.augmentable;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.childOf;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.choiceIn;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.identifiable;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.identifier;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.keyedListAction;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.rpcResult;
 import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN;
-import static org.opendaylight.mdsal.binding.model.util.Types.FUTURE;
-import static org.opendaylight.mdsal.binding.model.util.Types.VOID;
+import static org.opendaylight.mdsal.binding.model.util.Types.listTypeFor;
+import static org.opendaylight.mdsal.binding.model.util.Types.listenableFutureTypeFor;
 import static org.opendaylight.mdsal.binding.model.util.Types.typeForClass;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findNodeInSchemaContext;
@@ -30,17 +42,17 @@ import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findP
 import com.google.common.base.Splitter;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Sets;
-import java.util.ArrayDeque;
+import java.util.AbstractMap.SimpleImmutableEntry;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Comparator;
-import java.util.Deque;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
 import org.opendaylight.mdsal.binding.model.api.Constant;
 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
@@ -49,6 +61,7 @@ import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
 import org.opendaylight.mdsal.binding.model.api.Restrictions;
 import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotableTypeBuilder;
 import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotationTypeBuilder;
 import org.opendaylight.mdsal.binding.model.api.type.builder.EnumBuilder;
 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder;
@@ -58,22 +71,18 @@ import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilde
 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
 import org.opendaylight.mdsal.binding.model.api.type.builder.TypeMemberBuilder;
 import org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil;
-import org.opendaylight.mdsal.binding.model.util.BindingTypes;
 import org.opendaylight.mdsal.binding.model.util.ReferencedTypeImpl;
 import org.opendaylight.mdsal.binding.model.util.TypeConstants;
 import org.opendaylight.mdsal.binding.model.util.Types;
 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.GeneratedPropertyBuilderImpl;
+import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
 import org.opendaylight.mdsal.binding.yang.types.AbstractTypeProvider;
 import org.opendaylight.mdsal.binding.yang.types.BaseYangTypes;
 import org.opendaylight.mdsal.binding.yang.types.GroupingDefinitionDependencySort;
-import org.opendaylight.yangtools.yang.binding.BaseIdentity;
-import org.opendaylight.yangtools.yang.binding.BindingMapping;
-import org.opendaylight.yangtools.yang.binding.DataContainer;
-import org.opendaylight.yangtools.yang.binding.RpcService;
-import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
-import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
+import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
@@ -98,8 +107,6 @@ import org.opendaylight.yangtools.yang.model.api.Status;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.UsesNode;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
@@ -114,6 +121,9 @@ import org.slf4j.LoggerFactory;
 abstract class AbstractTypeGenerator {
     private static final Logger LOG = LoggerFactory.getLogger(BindingGeneratorImpl.class);
     private static final Splitter COLON_SPLITTER = Splitter.on(':');
+    private static final JavaTypeName DEPRECATED_ANNOTATION = JavaTypeName.create(Deprecated.class);
+    private static final JavaTypeName OVERRIDE_ANNOTATION = JavaTypeName.create(Override.class);
+    private static final Type LIST_STRING_TYPE = listTypeFor(BaseYangTypes.STRING_TYPE);
 
     /**
      * Comparator based on augment target path.
@@ -149,10 +159,9 @@ abstract class AbstractTypeGenerator {
     private final Map<QNameModule, ModuleContext> genCtx = new HashMap<>();
 
     /**
-     * Outer key represents the package name. Outer value represents map of all
-     * builders in the same package. Inner key represents the schema node name
-     * (in JAVA class/interface name format). Inner value represents instance of
-     * builder for schema node specified in key part.
+     * Outer key represents the package name. Outer value represents map of all builders in the same package. Inner key
+     * represents the schema node name (in JAVA class/interface name format). Inner value represents instance of builder
+     * for schema node specified in key part.
      */
     private final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
 
@@ -162,14 +171,20 @@ abstract class AbstractTypeGenerator {
     private final AbstractTypeProvider typeProvider;
 
     /**
-     * Holds reference to schema context to resolve data of augmented element
-     * when creating augmentation builder
+     * Holds reference to schema context to resolve data of augmented element when creating augmentation builder.
      */
     private final SchemaContext schemaContext;
 
-    AbstractTypeGenerator(final SchemaContext context, final AbstractTypeProvider typeProvider) {
+    /**
+     * Holds renamed elements.
+     */
+    private final Map<SchemaNode, JavaTypeName> renames;
+
+    AbstractTypeGenerator(final SchemaContext context, final AbstractTypeProvider typeProvider,
+            final Map<SchemaNode, JavaTypeName> renames) {
         this.schemaContext = requireNonNull(context);
         this.typeProvider = requireNonNull(typeProvider);
+        this.renames = requireNonNull(renames);
 
         final List<Module> contextModules = ModuleDependencySort.sort(schemaContext.getModules());
         final List<ModuleContext> contexts = new ArrayList<>(contextModules.size());
@@ -243,19 +258,19 @@ abstract class AbstractTypeGenerator {
             if (typedef != null) {
                 final Type type = typeProvider.generatedTypeForExtendedDefinitionType(typedef,  typedef);
                 if (type != null) {
-                    context.addTypedefType(typedef.getPath(), type);
+                    context.addTypedefType(typedef, type);
                     context.addTypeToSchema(type,typedef);
                 }
             }
         }
     }
 
-    private GeneratedTypeBuilder processDataSchemaNode(final ModuleContext context, final GeneratedTypeBuilder childOf,
+    private GeneratedTypeBuilder processDataSchemaNode(final ModuleContext context, final Type baseInterface,
             final DataSchemaNode node) {
         if (node.isAugmenting() || node.isAddedByUses()) {
             return null;
         }
-        final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(context, node, childOf);
+        final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(context, node, baseInterface);
         annotateDeprecatedIfNecessary(node.getStatus(), genType);
 
         final Module module = context.module();
@@ -271,28 +286,31 @@ abstract class AbstractTypeGenerator {
     }
 
     private void containerToGenType(final ModuleContext context, final GeneratedTypeBuilder parent,
-            final GeneratedTypeBuilder childOf, final ContainerSchemaNode node) {
-        final GeneratedTypeBuilder genType = processDataSchemaNode(context, childOf, node);
+            final Type baseInterface, final ContainerSchemaNode node) {
+        final GeneratedTypeBuilder genType = processDataSchemaNode(context, baseInterface, node);
         if (genType != null) {
             constructGetter(parent, genType, node);
             resolveDataSchemaNodes(context, genType, genType, node.getChildNodes());
+            actionsToGenType(context, genType, node, null);
         }
     }
 
-    private void listToGenType(final ModuleContext context,
-            final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final ListSchemaNode node) {
-        final GeneratedTypeBuilder genType = processDataSchemaNode(context, childOf, node);
+    private void listToGenType(final ModuleContext context, final GeneratedTypeBuilder parent,
+            final Type baseInterface, final ListSchemaNode node) {
+        final GeneratedTypeBuilder genType = processDataSchemaNode(context, baseInterface, node);
         if (genType != null) {
-            constructGetter(parent, Types.listTypeFor(genType), node);
+            constructGetter(parent, listTypeFor(genType), node);
 
             final List<String> listKeys = listKeys(node);
             final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(context, node);
             if (genTOBuilder != null) {
-                final Type identifierMarker = Types.parameterizedTypeFor(IDENTIFIER, genType);
-                final Type identifiableMarker = Types.parameterizedTypeFor(IDENTIFIABLE, genTOBuilder);
+                final Type identifierMarker = identifier(genType);
+                final Type identifiableMarker = identifiable(genTOBuilder);
                 genTOBuilder.addImplementsType(identifierMarker);
                 genType.addImplementsType(identifiableMarker);
+
             }
+            actionsToGenType(context, genType, node, genTOBuilder);
 
             for (final DataSchemaNode schemaNode : node.getChildNodes()) {
                 if (!schemaNode.isAugmenting()) {
@@ -391,6 +409,62 @@ abstract class AbstractTypeGenerator {
         return moduleDataTypeBuilder;
     }
 
+    private <T extends DataNodeContainer & ActionNodeContainer> void actionsToGenType(final ModuleContext context,
+            final Type parent, final T parentSchema, final Type keyType) {
+        for (final ActionDefinition action : parentSchema.getActions()) {
+            final GeneratedType input;
+            final GeneratedType output;
+            if (action.isAddedByUses()) {
+                final ActionDefinition orig = findOrigAction(parentSchema, action).get();
+                input = context.getChildNode(orig.getInput().getPath()).build();
+                output = context.getChildNode(orig.getOutput().getPath()).build();
+            } else {
+                input = actionContainer(context, RPC_INPUT, action.getInput());
+                output = actionContainer(context, RPC_OUTPUT, action.getOutput());
+            }
+
+            if (!(parentSchema instanceof GroupingDefinition)) {
+                // Parent is a non-grouping, hence we need to establish an Action instance, which can be completely
+                // identified by an InstanceIdentifier. We do not generate Actions for groupings as they are inexact,
+                // and do not capture an actual instantiation.
+                final QName qname = action.getQName();
+                final GeneratedTypeBuilder builder = typeProvider.newGeneratedTypeBuilder(JavaTypeName.create(
+                    packageNameForGeneratedType(context.modulePackageName(), action.getPath()),
+                    BindingMapping.getClassName(qname)));
+                qnameConstant(builder, JavaTypeName.create(context.modulePackageName(),
+                    BindingMapping.MODULE_INFO_CLASS_NAME), qname.getLocalName());
+
+                annotateDeprecatedIfNecessary(action.getStatus(), builder);
+                builder.addImplementsType(keyType != null ? keyedListAction(parent, keyType, input, output)
+                        : action(parent, input, output));
+
+                addCodegenInformation(builder, context.module(), action);
+                context.addChildNodeType(action, builder);
+            }
+        }
+    }
+
+    private Optional<ActionDefinition> findOrigAction(final DataNodeContainer parent, final ActionDefinition action) {
+        for (UsesNode uses : parent.getUses()) {
+            final GroupingDefinition grp = findUsedGrouping(uses);
+            final Optional<ActionDefinition> found = grp.getActions().stream()
+                    .filter(act -> action.getQName().equals(act.getQName())).findFirst();
+            if (found.isPresent()) {
+                final ActionDefinition result = found.get();
+                return result.isAddedByUses() ? findOrigAction(grp, result) : found;
+            }
+        }
+
+        return Optional.empty();
+    }
+
+    private GeneratedType actionContainer(final ModuleContext context, final Type baseInterface,
+            final ContainerSchemaNode schema) {
+        final GeneratedTypeBuilder genType = processDataSchemaNode(context, baseInterface, schema);
+        resolveDataSchemaNodes(context, genType, genType, schema.getChildNodes());
+        return genType.build();
+    }
+
     /**
      * Converts all <b>RPCs</b> input and output substatements of the module
      * to the list of <code>Type</code> objects. In addition are to containers
@@ -417,7 +491,7 @@ abstract class AbstractTypeGenerator {
         }
 
         final GeneratedTypeBuilder interfaceBuilder = moduleTypeBuilder(context, "Service");
-        interfaceBuilder.addImplementsType(Types.typeForClass(RpcService.class));
+        interfaceBuilder.addImplementsType(RPC_SERVICE);
 
         addCodegenInformation(interfaceBuilder, module, "RPCs", rpcDefinitions);
 
@@ -426,50 +500,28 @@ abstract class AbstractTypeGenerator {
                 final String rpcName = BindingMapping.getClassName(rpc.getQName());
                 final String rpcMethodName = BindingMapping.getPropertyName(rpcName);
                 final MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName);
-                final ContainerSchemaNode input = rpc.getInput();
-                final ContainerSchemaNode output = rpc.getOutput();
 
                 // Do not refer to annotation class, as it may not be available at runtime
                 method.addAnnotation("javax.annotation", "CheckReturnValue");
-
-                //in case of implicit RPC input (StatementSource.CONTEXT),
-                // stay compatible (no input argument generated)
-                if (input != null && isExplicitStatement(input)) {
-                    final Type inTypeInstance = createRpcContainer(context, rpcName, rpc, input);
-                    method.addParameter(inTypeInstance, "input");
-                }
-
-                final Type outTypeInstance;
-                //in case of implicit RPC output (StatementSource.CONTEXT),
-                //stay compatible (Future<RpcResult<Void>> return type generated)
-                if (output != null && isExplicitStatement(output)) {
-                    outTypeInstance = createRpcContainer(context, rpcName, rpc, output);
-                } else {
-                    outTypeInstance = VOID;
-                }
-
-                final Type rpcRes = Types.parameterizedTypeFor(Types.typeForClass(RpcResult.class), outTypeInstance);
                 addComment(method, rpc);
-                method.setReturnType(Types.parameterizedTypeFor(FUTURE, rpcRes));
+                method.addParameter(
+                    createRpcContainer(context, rpcName, rpc, verifyNotNull(rpc.getInput()), RPC_INPUT), "input");
+                method.setReturnType(listenableFutureTypeFor(
+                    rpcResult(createRpcContainer(context, rpcName, rpc, verifyNotNull(rpc.getOutput()), RPC_OUTPUT))));
             }
         }
 
         context.addTopLevelNodeType(interfaceBuilder);
     }
 
-    private static boolean isExplicitStatement(final ContainerSchemaNode node) {
-        return node instanceof EffectiveStatement
-                && ((EffectiveStatement<?, ?>) node).getDeclared().getStatementSource() == StatementSource.DECLARATION;
-    }
-
     private Type createRpcContainer(final ModuleContext context, final String rpcName, final RpcDefinition rpc,
-            final ContainerSchemaNode schema) {
+            final ContainerSchemaNode schema, final Type type) {
         processUsesAugments(schema, context);
         final GeneratedTypeBuilder outType = addRawInterfaceDefinition(
             JavaTypeName.create(context.modulePackageName(), rpcName + BindingMapping.getClassName(schema.getQName())),
             schema);
         addImplementedInterfaceFromUses(schema, outType);
-        outType.addImplementsType(DATA_OBJECT);
+        outType.addImplementsType(type);
         outType.addImplementsType(augmentable(outType));
         annotateDeprecatedIfNecessary(rpc.getStatus(), outType);
         resolveDataSchemaNodes(context, outType, outType, schema.getChildNodes());
@@ -502,14 +554,14 @@ abstract class AbstractTypeGenerator {
         }
 
         final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(context, "Listener");
-        listenerInterface.addImplementsType(BindingTypes.NOTIFICATION_LISTENER);
+        listenerInterface.addImplementsType(NOTIFICATION_LISTENER);
 
         for (final NotificationDefinition notification : notifications) {
             if (notification != null) {
                 processUsesAugments(notification, context);
 
                 final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition(
-                    context.modulePackageName(), notification, null, context);
+                    context.modulePackageName(), notification, DATA_OBJECT, context);
                 annotateDeprecatedIfNecessary(notification.getStatus(), notificationInterface);
                 notificationInterface.addImplementsType(NOTIFICATION);
                 context.addChildNodeType(notification, notificationInterface);
@@ -569,36 +621,46 @@ abstract class AbstractTypeGenerator {
         if (identity == null) {
             return;
         }
-        final GeneratedTOBuilder newType = typeProvider.newGeneratedTOBuilder(JavaTypeName.create(
-            packageNameForGeneratedType(context.modulePackageName(), identity.getPath()),
-            BindingMapping.getClassName(identity.getQName())));
+
+        JavaTypeName name = renames.get(identity);
+        if (name == null) {
+            name = JavaTypeName.create(packageNameForGeneratedType(context.modulePackageName(), identity.getPath()),
+                BindingMapping.getClassName(identity.getQName()));
+        }
+
+        final GeneratedTypeBuilder newType = typeProvider.newGeneratedTypeBuilder(name);
         final Set<IdentitySchemaNode> baseIdentities = identity.getBaseIdentities();
-        if (baseIdentities.isEmpty()) {
-            final GeneratedTOBuilder gto = typeProvider.newGeneratedTOBuilder(JavaTypeName.create(BaseIdentity.class));
-            newType.setExtendsType(gto.build());
+        if (!baseIdentities.isEmpty()) {
+            for (IdentitySchemaNode baseIdentity : baseIdentities) {
+                JavaTypeName base = renames.get(baseIdentity);
+                if (base == null) {
+                    final QName qname = baseIdentity.getQName();
+                    base = JavaTypeName.create(BindingMapping.getRootPackageName(qname.getModule()),
+                        BindingMapping.getClassName(qname));
+                }
+
+                final GeneratedTransferObject gto = typeProvider.newGeneratedTOBuilder(base).build();
+                newType.addImplementsType(gto);
+            }
         } else {
-            final IdentitySchemaNode baseIdentity = baseIdentities.iterator().next();
-            final QName qname = baseIdentity.getQName();
-            final GeneratedTransferObject gto = typeProvider.newGeneratedTOBuilder(JavaTypeName.create(
-                BindingMapping.getRootPackageName(qname.getModule()), BindingMapping.getClassName(qname))).build();
-            newType.setExtendsType(gto);
+            newType.addImplementsType(BASE_IDENTITY);
         }
-        newType.setAbstract(true);
 
         final Module module = context.module();
         addCodegenInformation(newType, module, identity);
         newType.setModuleName(module.getName());
         newType.setSchemaPath(identity.getPath());
 
-        qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, identity.getQName());
+        qnameConstant(newType, JavaTypeName.create(context.modulePackageName(), BindingMapping.MODULE_INFO_CLASS_NAME),
+            identity.getQName().getLocalName());
 
-        context.addIdentityType(identity.getQName(), newType);
+        context.addIdentityType(identity, newType);
     }
 
-
-    private static Constant qnameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName,
-            final QName name) {
-        return toBuilder.addConstant(typeForClass(QName.class), constantName, name);
+    private static Constant qnameConstant(final GeneratedTypeBuilderBase<?> toBuilder,
+            final JavaTypeName yangModuleInfo, final String localName) {
+        return toBuilder.addConstant(typeForClass(QName.class), BindingMapping.QNAME_STATIC_FIELD_NAME,
+            new SimpleImmutableEntry<>(yangModuleInfo, localName));
     }
 
     /**
@@ -620,31 +682,23 @@ abstract class AbstractTypeGenerator {
             // node of grouping is resolved to the method.
             final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(context, grouping);
             annotateDeprecatedIfNecessary(grouping.getStatus(), genType);
-            context.addGroupingType(grouping.getPath(), genType);
+            context.addGroupingType(grouping, genType);
             resolveDataSchemaNodes(context, genType, genType, grouping.getChildNodes());
             groupingsToGenTypes(context, grouping.getGroupings());
             processUsesAugments(grouping, context);
+            actionsToGenType(context, genType, grouping, null);
         }
     }
 
     /**
-     * Adds enumeration builder created from <code>enumTypeDef</code> to
-     * <code>typeBuilder</code>.
-     *
-     * Each <code>enumTypeDef</code> item is added to builder with its name and
-     * value.
+     * Adds enumeration builder created from <code>enumTypeDef</code> to <code>typeBuilder</code>. Each
+     * <code>enumTypeDef</code> item is added to builder with its name and value.
      *
-     * @param enumTypeDef
-     *            EnumTypeDefinition contains enum data
-     * @param enumName
-     *            string contains name which will be assigned to enumeration
-     *            builder
-     * @param typeBuilder
-     *            GeneratedTypeBuilder to which will be enum builder assigned
-     * @param module
-     *            Module in which type should be generated
-     * @return enumeration builder which contains data from
-     *         <code>enumTypeDef</code>
+     * @param enumTypeDef EnumTypeDefinition contains enum data
+     * @param enumName string contains name which will be assigned to enumeration builder
+     * @param typeBuilder GeneratedTypeBuilder to which will be enum builder assigned
+     * @param module Module in which type should be generated
+     * @return enumeration builder which contains data from <code>enumTypeDef</code>
      */
     private EnumBuilder resolveInnerEnumFromTypeDefinition(final EnumTypeDefinition enumTypeDef, final QName enumName,
             final GeneratedTypeBuilder typeBuilder, final ModuleContext context) {
@@ -661,16 +715,10 @@ abstract class AbstractTypeGenerator {
     /**
      * Generates type builder for <code>module</code>.
      *
-     * @param module
-     *            Module which is source of package name for generated type
-     *            builder
-     * @param postfix
-     *            string which is added to the module class name representation
-     *            as suffix
-     * @return instance of GeneratedTypeBuilder which represents
-     *         <code>module</code>.
-     * @throws IllegalArgumentException
-     *             if <code>module</code> is null
+     * @param module Module which is source of package name for generated type builder
+     * @param postfix string which is added to the module class name representation as suffix
+     * @return instance of GeneratedTypeBuilder which represents <code>module</code>.
+     * @throws IllegalArgumentException if <code>module</code> is null
      */
     private GeneratedTypeBuilder moduleTypeBuilder(final ModuleContext context, final String postfix) {
         final Module module = context.module();
@@ -684,20 +732,13 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Converts <code>augSchema</code> to list of <code>Type</code> which
-     * contains generated type for augmentation. In addition there are also
-     * generated types for all containers, list and choices which are child of
-     * <code>augSchema</code> node or a generated types for cases are added if
-     * augmented node is choice.
+     * Converts <code>augSchema</code> to list of <code>Type</code> which contains generated type for augmentation.
+     * In addition there are also generated types for all containers, list and choices which are child of
+     * <code>augSchema</code> node or a generated types for cases are added if augmented node is choice.
      *
-     * @param augmentPackageName
-     *            string with the name of the package to which the augmentation
-     *            belongs
-     * @param augSchema
-     *            AugmentationSchema which is contains data about augmentation
-     *            (target path, childs...)
-     * @param module
-     *            current module
+     * @param augmentPackageName string with the name of the package to which the augmentation belongs
+     * @param augSchema AugmentationSchema which is contains data about augmentation (target path, childs...)
+     * @param module current module
      * @throws IllegalArgumentException
      *             <ul>
      *             <li>if <code>augmentPackageName</code> equals null</li>
@@ -783,26 +824,28 @@ abstract class AbstractTypeGenerator {
         }
     }
 
+    private GroupingDefinition findUsedGrouping(final UsesNode uses) {
+        final SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, uses.getGroupingPath()
+            .getPathFromRoot());
+        if (targetGrouping instanceof GroupingDefinition) {
+            return (GroupingDefinition) targetGrouping;
+        }
+
+        throw new IllegalArgumentException("Failed to resolve used grouping for " + uses);
+    }
+
     /**
      * Convenient method to find node added by uses statement.
      *
-     * @param targetPath
-     *            node path
-     * @param parentUsesNode
-     *            parent of uses node
+     * @param targetPath node path
+     * @param parentUsesNode parent of uses node
      * @return node from its original location in grouping
      */
     private DataSchemaNode findOriginalTargetFromGrouping(final SchemaPath targetPath, final UsesNode parentUsesNode) {
-        final SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, parentUsesNode.getGroupingPath()
-                .getPathFromRoot());
-        if (!(targetGrouping instanceof GroupingDefinition)) {
-            throw new IllegalArgumentException("Failed to generate code for augment in " + parentUsesNode);
-        }
-
-        SchemaNode result = targetGrouping;
+        SchemaNode result = findUsedGrouping(parentUsesNode);
         for (final QName node : targetPath.getPathFromRoot()) {
             if (result instanceof DataNodeContainer) {
-                final QName resultNode = QName.create(result.getQName().getModule(), node.getLocalName());
+                final QName resultNode = node.withModule(result.getQName().getModule());
                 result = ((DataNodeContainer) result).getDataChildByName(resultNode);
             } else if (result instanceof ChoiceSchemaNode) {
                 result = findNamedCase((ChoiceSchemaNode) result, node.getLocalName());
@@ -838,34 +881,21 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Returns a generated type builder for an augmentation.
-     *
-     * The name of the type builder is equal to the name of augmented node with
-     * serial number as suffix.
+     * Returns a generated type builder for an augmentation. The name of the type builder is equal to the name
+     * of augmented node with serial number as suffix.
      *
-     * @param context
-     *            current module
-     * @param augmentPackageName
-     *            string with contains the package name to which the augment
-     *            belongs
-     * @param basePackageName
-     *            string with the package name to which the augmented node
-     *            belongs
-     * @param targetTypeRef
-     *            target type
-     * @param augSchema
-     *            augmentation schema which contains data about the child nodes
-     *            and uses of augment
+     * @param context current module
+     * @param augmentPackageName string with contains the package name to which the augment belongs
+     * @param basePackageName string with the package name to which the augmented node belongs
+     * @param targetTypeRef target type
+     * @param augSchema augmentation schema which contains data about the child nodes and uses of augment
      * @return generated type builder for augment
      */
     private GeneratedTypeBuilder addRawAugmentGenTypeDefinition(final ModuleContext context,
             final String augmentPackageName, final Type targetTypeRef,
             final AugmentationSchemaNode augSchema) {
-        Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.get(augmentPackageName);
-        if (augmentBuilders == null) {
-            augmentBuilders = new HashMap<>();
-            genTypeBuilders.put(augmentPackageName, augmentBuilders);
-        }
+        Map<String, GeneratedTypeBuilder> augmentBuilders =
+            genTypeBuilders.computeIfAbsent(augmentPackageName, k -> new HashMap<>());
         final String augIdentifier = getAugmentIdentifier(augSchema.getUnknownSchemaNodes());
 
         String augTypeName;
@@ -883,7 +913,7 @@ abstract class AbstractTypeGenerator {
         annotateDeprecatedIfNecessary(augSchema.getStatus(), augTypeBuilder);
         addImplementedInterfaceFromUses(augSchema, augTypeBuilder);
 
-        augSchemaNodeToMethods(context,augTypeBuilder, augTypeBuilder, augSchema.getChildNodes());
+        augSchemaNodeToMethods(context, augTypeBuilder, augSchema.getChildNodes());
         augmentBuilders.put(augTypeName, augTypeBuilder);
 
         if (!augSchema.getChildNodes().isEmpty()) {
@@ -899,11 +929,6 @@ abstract class AbstractTypeGenerator {
         return addRawAugmentGenTypeDefinition(context, context.modulePackageName(), targetTypeRef, augSchema);
     }
 
-    /**
-     *
-     * @param unknownSchemaNodes
-     * @return nodeParameter of UnknownSchemaNode
-     */
     private static String getAugmentIdentifier(final List<UnknownSchemaNode> unknownSchemaNodes) {
         for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
             final QName nodeType = unknownSchemaNode.getNodeType();
@@ -916,15 +941,11 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Returns first unique name for the augment generated type builder. The
-     * generated type builder name for augment consists from name of augmented
-     * node and serial number of its augmentation.
+     * Returns first unique name for the augment generated type builder. The generated type builder name for augment
+     * consists from name of augmented node and serial number of its augmentation.
      *
-     * @param builders
-     *            map of builders which were created in the package to which the
-     *            augmentation belongs
-     * @param genTypeName
-     *            string with name of augmented node
+     * @param builders map of builders which were created in the package to which the augmentation belongs
+     * @param genTypeName string with name of augmented node
      * @return string with unique name for augmentation builder
      */
     private static String augGenTypeName(final Map<String, GeneratedTypeBuilder> builders, final String genTypeName) {
@@ -938,34 +959,27 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Adds the methods to <code>typeBuilder</code> which represent subnodes of
-     * node for which <code>typeBuilder</code> was created.
+     * Adds the methods to <code>typeBuilder</code> which represent subnodes of node for which <code>typeBuilder</code>
+     * was created. The subnodes aren't mapped to the methods if they are part of grouping or augment (in this case are
+     * already part of them).
      *
-     * The subnodes aren't mapped to the methods if they are part of grouping or
-     * augment (in this case are already part of them).
-     *
-     * @param module
-     *            current module
-     * @param parent
-     *            generated type builder which represents any node. The subnodes
-     *            of this node are added to the <code>typeBuilder</code> as
-     *            methods. The subnode can be of type leaf, leaf-list, list,
-     *            container, choice.
-     * @param childOf
-     *            parent type
-     * @param schemaNodes
-     *            set of data schema nodes which are the children of the node
-     *            for which <code>typeBuilder</code> was created
-     * @return generated type builder which is the same builder as input
-     *         parameter. The getter methods (representing child nodes) could be
-     *         added to it.
+     * @param module current module
+     * @param parent generated type builder which represents any node. The subnodes of this node are added
+     *               to the <code>typeBuilder</code> as methods. The subnode can be of type leaf, leaf-list, list,
+     *               container, choice.
+     * @param childOf parent type
+     * @param schemaNodes set of data schema nodes which are the children of the node for which
+     *                    <code>typeBuilder</code> was created
+     * @return generated type builder which is the same builder as input parameter. The getter methods (representing
+     *         child nodes) could be added to it.
      */
     private GeneratedTypeBuilder resolveDataSchemaNodes(final ModuleContext context, final GeneratedTypeBuilder parent,
-            final GeneratedTypeBuilder childOf, final Iterable<DataSchemaNode> schemaNodes) {
+            final @Nullable Type childOf, final Iterable<DataSchemaNode> schemaNodes) {
         if (schemaNodes != null && parent != null) {
+            final Type baseInterface = childOf == null ? DATA_OBJECT : childOf(childOf);
             for (final DataSchemaNode schemaNode : schemaNodes) {
                 if (!schemaNode.isAugmenting() && !schemaNode.isAddedByUses()) {
-                    addSchemaNodeToBuilderAsMethod(context, schemaNode, parent, childOf);
+                    addSchemaNodeToBuilderAsMethod(context, schemaNode, parent, baseInterface);
                 }
             }
         }
@@ -973,32 +987,26 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Adds the methods to <code>typeBuilder</code> what represents subnodes of
-     * node for which <code>typeBuilder</code> was created.
+     * Adds the methods to <code>typeBuilder</code> what represents subnodes of node for which <code>typeBuilder</code>
+     * was created.
      *
-     * @param module
-     *            current module
-     * @param typeBuilder
-     *            generated type builder which represents any node. The subnodes
-     *            of this node are added to the <code>typeBuilder</code> as
-     *            methods. The subnode can be of type leaf, leaf-list, list,
-     *            container, choice.
-     * @param childOf
-     *            parent type
-     * @param schemaNodes
-     *            set of data schema nodes which are the children of the node
-     *            for which <code>typeBuilder</code> was created
-     * @return generated type builder which is the same object as the input
-     *         parameter <code>typeBuilder</code>. The getter method could be
-     *         added to it.
+     * @param module current module
+     * @param typeBuilder generated type builder which represents any node. The subnodes of this node are added
+     *                    to the <code>typeBuilder</code> as methods. The subnode can be of type leaf, leaf-list, list,
+     *                    container, choice.
+     * @param childOf parent type
+     * @param schemaNodes set of data schema nodes which are the children of the node for which <code>typeBuilder</code>
+     *                    was created
+     * @return generated type builder which is the same object as the input parameter <code>typeBuilder</code>.
+     *         The getter method could be added to it.
      */
     private GeneratedTypeBuilder augSchemaNodeToMethods(final ModuleContext context,
-            final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf,
-            final Iterable<DataSchemaNode> schemaNodes) {
+            final GeneratedTypeBuilder typeBuilder, final Iterable<DataSchemaNode> schemaNodes) {
         if (schemaNodes != null && typeBuilder != null) {
+            final Type baseInterface = childOf(typeBuilder);
             for (final DataSchemaNode schemaNode : schemaNodes) {
                 if (!schemaNode.isAugmenting()) {
-                    addSchemaNodeToBuilderAsMethod(context, schemaNode, typeBuilder, childOf);
+                    addSchemaNodeToBuilderAsMethod(context, schemaNode, typeBuilder, baseInterface);
                 }
             }
         }
@@ -1006,31 +1014,24 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Adds to <code>typeBuilder</code> a method which is derived from
-     * <code>schemaNode</code>.
+     * Adds to <code>typeBuilder</code> a method which is derived from <code>schemaNode</code>.
      *
-     * @param node
-     *            data schema node which is added to <code>typeBuilder</code> as
-     *            a method
-     * @param typeBuilder
-     *            generated type builder to which is <code>schemaNode</code>
-     *            added as a method.
-     * @param childOf
-     *            parent type
-     * @param module
-     *            current module
+     * @param node data schema node which is added to <code>typeBuilder</code> as a method
+     * @param typeBuilder generated type builder to which is <code>schemaNode</code> added as a method.
+     * @param childOf parent type
+     * @param module current module
      */
     private void addSchemaNodeToBuilderAsMethod(final ModuleContext context, final DataSchemaNode node,
-            final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf) {
+            final GeneratedTypeBuilder typeBuilder, final Type baseInterface) {
         if (node != null && typeBuilder != null) {
             if (node instanceof LeafSchemaNode) {
                 resolveLeafSchemaNodeAsMethod(typeBuilder, (LeafSchemaNode) node, context);
             } else if (node instanceof LeafListSchemaNode) {
                 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) node, context);
             } else if (node instanceof ContainerSchemaNode) {
-                containerToGenType(context, typeBuilder, childOf, (ContainerSchemaNode) node);
+                containerToGenType(context, typeBuilder, baseInterface, (ContainerSchemaNode) node);
             } else if (node instanceof ListSchemaNode) {
-                listToGenType(context, typeBuilder, childOf, (ListSchemaNode) node);
+                listToGenType(context, typeBuilder, baseInterface, (ListSchemaNode) node);
             } else if (node instanceof ChoiceSchemaNode) {
                 choiceToGeneratedType(context, typeBuilder, (ChoiceSchemaNode) node);
             } else {
@@ -1042,22 +1043,15 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Converts <code>choiceNode</code> to the list of generated types for
-     * choice and its cases.
-     *
-     * The package names for choice and for its cases are created as
-     * concatenation of the module package (<code>basePackageName</code>) and
-     * names of all parents node.
+     * Converts <code>choiceNode</code> to the list of generated types for choice and its cases. The package names
+     * for choice and for its cases are created as concatenation of the module package (<code>basePackageName</code>)
+     * and names of all parents node.
      *
-     * @param context
-     *            current module
-     * @param basePackageName
-     *            string with the module package name
-     * @param parent
-     *            parent type
-     * @param choiceNode
-     *            choice node which is mapped to generated type. Also child
-     *            nodes - cases are mapped to generated types.
+     * @param context current module
+     * @param basePackageName string with the module package name
+     * @param parent parent type
+     * @param choiceNode choice node which is mapped to generated type. Also child nodes - cases are mapped to generated
+     *                   types.
      * @throws IllegalArgumentException
      *             <ul>
      *             <li>if <code>basePackageName</code> is null</li>
@@ -1072,28 +1066,26 @@ abstract class AbstractTypeGenerator {
             final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(
                 JavaTypeName.create(packageNameForGeneratedType(context.modulePackageName(), choiceNode.getPath()),
                 BindingMapping.getClassName(choiceNode.getQName())), choiceNode);
-            constructGetter(parent, choiceTypeBuilder, choiceNode);
-            choiceTypeBuilder.addImplementsType(typeForClass(DataContainer.class));
+            choiceTypeBuilder.addImplementsType(choiceIn(parent));
             annotateDeprecatedIfNecessary(choiceNode.getStatus(), choiceTypeBuilder);
             context.addChildNodeType(choiceNode, choiceTypeBuilder);
-            generateTypesFromChoiceCases(context, choiceTypeBuilder.build(), choiceNode);
+
+            final GeneratedType choiceType = choiceTypeBuilder.build();
+            generateTypesFromChoiceCases(context, choiceType, choiceNode);
+
+            constructGetter(parent, choiceType, choiceNode);
         }
     }
 
     /**
-     * Converts <code>caseNodes</code> set to list of corresponding generated types.
+     * Converts <code>caseNodes</code> set to list of corresponding generated types. For every <i>case</i> which is not
+     * added through augment or <i>uses</i> is created generated type builder. The package names for the builder is
+     * created as concatenation of the module package and names of all parents nodes of the concrete <i>case</i>. There
+     * is also relation "<i>implements type</i>" between every case builder and <i>choice</i> type
      *
-     * For every <i>case</i> which isn't added through augment or <i>uses</i> is created generated type builder.
-     * The package names for the builder is created as concatenation of the module package and names of all parents
-     * nodes of the concrete <i>case</i>. There is also relation "<i>implements type</i>" between every case builder
-     * and <i>choice</i> type
-     *
-     * @param context
-     *            current module context
-     * @param refChoiceType
-     *            type which represents superior <i>case</i>
-     * @param choiceNode
-     *            choice case node which is mapped to generated type
+     * @param context current module context
+     * @param refChoiceType type which represents superior <i>case</i>
+     * @param choiceNode choice case node which is mapped to generated type
      * @throws IllegalArgumentException
      *             <ul>
      *             <li>if <code>refChoiceType</code> equals null</li>
@@ -1139,7 +1131,7 @@ abstract class AbstractTypeGenerator {
                         }
 
                         checkState(parent != null, "Could not find Choice node parent %s", choiceNodeParentPath);
-                        GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath());
+                        Type childOfType = findChildNodeByPath(parent.getPath());
                         if (childOfType == null) {
                             childOfType = findGroupingByPath(parent.getPath());
                         }
@@ -1147,30 +1139,24 @@ abstract class AbstractTypeGenerator {
                     } else {
                         resolveDataSchemaNodes(context, caseTypeBuilder, moduleToDataType(context), caseChildNodes);
                     }
-               }
+                }
             }
             processUsesAugments(caseNode, context);
         }
     }
 
     /**
-     * Generates list of generated types for all the cases of a choice which are
-     * added to the choice through the augment.
+     * Generates list of generated types for all the cases of a choice which are added to the choice through
+     * the augment.
      *
-     * @param module
-     *            current module
-     * @param basePackageName
-     *            string contains name of package to which augment belongs. If
-     *            an augmented choice is from an other package (pcg1) than an
-     *            augmenting choice (pcg2) then case's of the augmenting choice
-     *            will belong to pcg2.
-     * @param targetType
-     *            Type which represents target choice
-     * @param targetNode
-     *            node which represents target choice
-     * @param augmentedNodes
-     *            set of choice case nodes for which is checked if are/aren't
-     *            added to choice through augmentation
+     * @param module current module
+     * @param basePackageName string contains name of package to which augment belongs. If an augmented choice is
+     *                        from an other package (pcg1) than an augmenting choice (pcg2) then case's
+     *                        of the augmenting choice will belong to pcg2.
+     * @param targetType Type which represents target choice
+     * @param targetNode node which represents target choice
+     * @param augmentedNodes set of choice case nodes for which is checked if are/are not added to choice through
+     *                       augmentation
      * @throws IllegalArgumentException
      *             <ul>
      *             <li>if <code>basePackageName</code> is null</li>
@@ -1189,25 +1175,6 @@ abstract class AbstractTypeGenerator {
                 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(context, caseNode);
                 caseTypeBuilder.addImplementsType(targetType);
 
-                SchemaNode parent;
-                final SchemaPath nodeSp = targetNode.getPath();
-                parent = findDataSchemaNode(schemaContext, nodeSp.getParent());
-
-                GeneratedTypeBuilder childOfType = null;
-                if (parent instanceof Module) {
-                    childOfType = moduleContext(((Module) parent).getQNameModule()).getModuleNode();
-                } else if (parent instanceof CaseSchemaNode) {
-                    childOfType = findCaseByPath(parent.getPath());
-                } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
-                    childOfType = findChildNodeByPath(parent.getPath());
-                } else if (parent instanceof GroupingDefinition) {
-                    childOfType = findGroupingByPath(parent.getPath());
-                }
-
-                if (childOfType == null) {
-                    throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode);
-                }
-
                 CaseSchemaNode node = null;
                 final String caseLocalName = caseNode.getQName().getLocalName();
                 if (caseNode instanceof CaseSchemaNode) {
@@ -1226,7 +1193,7 @@ abstract class AbstractTypeGenerator {
                 }
                 final Iterable<DataSchemaNode> childNodes = node.getChildNodes();
                 if (childNodes != null) {
-                    resolveDataSchemaNodes(context, caseTypeBuilder, childOfType, childNodes);
+                    resolveDataSchemaNodes(context, caseTypeBuilder, findChildOfType(targetNode), childNodes);
                 }
                 context.addCaseType(caseNode.getPath(), caseTypeBuilder);
                 context.addChoiceToCaseMapping(targetType, caseTypeBuilder, node);
@@ -1234,6 +1201,30 @@ abstract class AbstractTypeGenerator {
         }
     }
 
+    private GeneratedTypeBuilder findChildOfType(final ChoiceSchemaNode targetNode) {
+        final SchemaPath nodePath = targetNode.getPath();
+        final SchemaPath parentSp = nodePath.getParent();
+        if (parentSp.getParent() == null) {
+            return moduleContext(nodePath.getLastComponent().getModule()).getModuleNode();
+        }
+
+        final SchemaNode parent = findDataSchemaNode(schemaContext, parentSp);
+        GeneratedTypeBuilder childOfType = null;
+        if (parent instanceof CaseSchemaNode) {
+            childOfType = findCaseByPath(parent.getPath());
+        } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
+            childOfType = findChildNodeByPath(parent.getPath());
+        } else if (parent instanceof GroupingDefinition) {
+            childOfType = findGroupingByPath(parent.getPath());
+        }
+
+        if (childOfType == null) {
+            throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode);
+        }
+
+        return childOfType;
+    }
+
     private static CaseSchemaNode findNamedCase(final ChoiceSchemaNode choice, final String caseName) {
         final List<CaseSchemaNode> cases = choice.findCaseNodes(caseName);
         return cases.isEmpty() ? null : cases.get(0);
@@ -1258,23 +1249,17 @@ abstract class AbstractTypeGenerator {
         if (!patternConstraints.isEmpty()) {
             final StringBuilder field = new StringBuilder().append(TypeConstants.PATTERN_CONSTANT_NAME).append("_")
                 .append(BindingMapping.getPropertyName(leafName));
-            typeBuilder.addConstant(Types.listTypeFor(BaseYangTypes.STRING_TYPE), field.toString(),
+            typeBuilder.addConstant(LIST_STRING_TYPE, field.toString(),
                 typeProvider.resolveRegExpressions(patternConstraints));
         }
     }
 
     /**
-     * Converts <code>leaf</code> to the getter method which is added to
-     * <code>typeBuilder</code>.
+     * Converts <code>leaf</code> to the getter method which is added to <code>typeBuilder</code>.
      *
-     * @param typeBuilder
-     *            generated type builder to which is added getter method as
-     *            <code>leaf</code> mapping
-     * @param leaf
-     *            leaf schema node which is mapped as getter method which is
-     *            added to <code>typeBuilder</code>
-     * @param module
-     *            Module in which type was defined
+     * @param typeBuilder generated type builder to which is added getter method as <code>leaf</code> mapping
+     * @param leaf leaf schema node which is mapped as getter method which is added to <code>typeBuilder</code>
+     * @param module Module in which type was defined
      * @return boolean value
      *         <ul>
      *         <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
@@ -1303,13 +1288,10 @@ abstract class AbstractTypeGenerator {
                 }
                 typeProvider.putReferencedType(leaf.getPath(), returnType);
             } else if (typeDef instanceof UnionTypeDefinition) {
-                GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder((UnionTypeDefinition) typeDef, typeBuilder, leaf,
-                    parentModule);
-                if (genTOBuilder != null) {
-                    returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
-                    // Store the inner type within the union so that we can find the reference for it
-                    context.addInnerTypedefType(typeDef.getPath(), returnType);
-                }
+                final UnionTypeDefinition unionDef = (UnionTypeDefinition)typeDef;
+                returnType = addTOToTypeBuilder(unionDef, typeBuilder, leaf, parentModule);
+                // Store the inner type within the union so that we can find the reference for it
+                context.addInnerTypedefType(typeDef.getPath(), returnType);
             } else if (typeDef instanceof BitsTypeDefinition) {
                 GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder((BitsTypeDefinition) typeDef, typeBuilder, leaf,
                     parentModule);
@@ -1383,7 +1365,7 @@ abstract class AbstractTypeGenerator {
                             + nodeParam);
                 }
 
-                final AnnotationTypeBuilder rc = getter.addAnnotation(JavaTypeName.create(RoutingContext.class));
+                final AnnotationTypeBuilder rc = getter.addAnnotation(ROUTING_CONTEXT);
                 final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath());
                 final String genTypeName = BindingMapping.getClassName(identity.getQName().getLocalName());
                 rc.addParameter("value", packageName + "." + genTypeName + ".class");
@@ -1436,19 +1418,12 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Converts <code>leaf</code> schema node to property of generated TO
-     * builder.
+     * Converts <code>leaf</code> schema node to property of generated TO builder.
      *
-     * @param toBuilder
-     *            generated TO builder to which is <code>leaf</code> added as
-     *            property
-     * @param leaf
-     *            leaf schema node which is added to <code>toBuilder</code> as
-     *            property
-     * @param returnType
-     *            property type
-     * @param isReadOnly
-     *            boolean value which says if leaf property is|isn't read only
+     * @param toBuilder generated TO builder to which is <code>leaf</code> added as property
+     * @param leaf leaf schema node which is added to <code>toBuilder</code> as property
+     * @param returnType property type
+     * @param isReadOnly boolean value which says if leaf property is|isn't read only
      * @return boolean value
      *         <ul>
      *         <li>false - if <code>leaf</code>, <code>toBuilder</code> or leaf
@@ -1474,15 +1449,10 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Converts <code>node</code> leaf list schema node to getter method of
-     * <code>typeBuilder</code>.
+     * Converts <code>node</code> leaf list schema node to getter method of <code>typeBuilder</code>.
      *
-     * @param typeBuilder
-     *            generated type builder to which is <code>node</code> added as
-     *            getter method
-     * @param node
-     *            leaf list schema node which is added to
-     *            <code>typeBuilder</code> as getter method
+     * @param typeBuilder generated type builder to which is <code>node</code> added as getter method
+     * @param node leaf list schema node which is added to <code>typeBuilder</code> as getter method
      * @param module module
      * @return boolean value
      *         <ul>
@@ -1512,11 +1482,8 @@ abstract class AbstractTypeGenerator {
                 returnType = new ReferencedTypeImpl(enumBuilder.getIdentifier());
                 typeProvider.putReferencedType(node.getPath(), returnType);
             } else if (typeDef instanceof UnionTypeDefinition) {
-                final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder((UnionTypeDefinition)typeDef, typeBuilder,
-                    node, parentModule);
-                if (genTOBuilder != null) {
-                    returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
-                }
+                final UnionTypeDefinition unionDef = (UnionTypeDefinition)typeDef;
+                returnType = addTOToTypeBuilder(unionDef, typeBuilder, node, parentModule);
             } else if (typeDef instanceof BitsTypeDefinition) {
                 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder((BitsTypeDefinition)typeDef, typeBuilder,
                     node, parentModule);
@@ -1532,27 +1499,36 @@ abstract class AbstractTypeGenerator {
             addPatternConstant(typeBuilder, node.getQName().getLocalName(), restrictions.getPatternConstraints());
         }
 
-        final ParameterizedType listType = Types.listTypeFor(returnType);
+        final ParameterizedType listType = listTypeFor(returnType);
         constructGetter(typeBuilder, listType, node);
         return true;
     }
 
-    private Type createReturnTypeForUnion(final GeneratedTOBuilder genTOBuilder, final TypeDefinition<?> typeDef,
+    private Type createReturnTypeForUnion(final GeneratedTOBuilder genTOBuilder, final UnionTypeDefinition typeDef,
             final GeneratedTypeBuilder typeBuilder, final Module parentModule) {
-        final GeneratedTOBuilder returnType = typeProvider.newGeneratedTOBuilder(genTOBuilder.getIdentifier());
-
-        addCodegenInformation(returnType, parentModule, typeDef);
-        returnType.setSchemaPath(typeDef.getPath());
-        returnType.setModuleName(parentModule.getName());
+        final GeneratedTOBuilder returnTypeBuilder = typeProvider.newGeneratedTOBuilder(genTOBuilder.getIdentifier());
+        returnTypeBuilder.setIsUnion(true);
+        addCodegenInformation(returnTypeBuilder, parentModule, typeDef);
+        returnTypeBuilder.setSchemaPath(typeDef.getPath());
+        returnTypeBuilder.setModuleName(parentModule.getName());
+        final GeneratedTransferObject returnType = returnTypeBuilder.build();
 
         genTOBuilder.setTypedef(true);
         genTOBuilder.setIsUnion(true);
         AbstractTypeProvider.addUnitsToGenTO(genTOBuilder, typeDef.getUnits().orElse(null));
 
+        createUnionBuilder(genTOBuilder, typeBuilder, returnType, parentModule);
+        return returnType;
+    }
 
-
-        final GeneratedTOBuilder unionBuilder = createUnionBuilder(genTOBuilder, typeBuilder);
-
+    private void createUnionBuilder(final GeneratedTOBuilder genTOBuilder, final GeneratedTypeBuilder typeBuilder,
+            final GeneratedTransferObject returnType, final Module parentModule) {
+        // Append enclosing path hierarchy without dots
+        final StringBuilder sb = new StringBuilder();
+        genTOBuilder.getIdentifier().localNameComponents().forEach(sb::append);
+        final GeneratedTOBuilder unionBuilder = typeProvider.newGeneratedTOBuilder(
+            JavaTypeName.create(typeBuilder.getPackageName(), sb.append("Builder").toString()));
+        unionBuilder.setIsUnionBuilder(true);
 
         final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
         method.setReturnType(returnType);
@@ -1560,95 +1536,57 @@ abstract class AbstractTypeGenerator {
         method.setAccessModifier(AccessModifier.PUBLIC);
         method.setStatic(true);
 
+        final GeneratedTransferObject unionBuilderType = unionBuilder.build();
         final Set<Type> types = typeProvider.getAdditionalTypes().get(parentModule);
         if (types == null) {
-            typeProvider.getAdditionalTypes().put(parentModule,
-                    Sets.newHashSet(unionBuilder.build()));
+            typeProvider.getAdditionalTypes().put(parentModule, Sets.newHashSet(unionBuilderType));
         } else {
-            types.add(unionBuilder.build());
+            types.add(unionBuilderType);
         }
-        return returnType.build();
-    }
-
-    private GeneratedTOBuilder createUnionBuilder(final GeneratedTOBuilder genTOBuilder,
-            final GeneratedTypeBuilder typeBuilder) {
-        final StringBuilder sb = new StringBuilder();
-
-        // Append enclosing path hierarchy without dots
-        // TODO: JavaTypeName could be giving us a Stream<String> or similar, but there is no sense in generalizing
-        //       unless there is another caller.
-        Optional<JavaTypeName> outerClass = genTOBuilder.getIdentifier().immediatelyEnclosingClass();
-        if (outerClass.isPresent()) {
-            final Deque<String> enclosingPath = new ArrayDeque<>();
-            do {
-                final JavaTypeName outerName = outerClass.get();
-                enclosingPath.push(outerName.simpleName());
-                outerClass = outerName.immediatelyEnclosingClass();
-            } while (outerClass.isPresent());
-
-            while (!enclosingPath.isEmpty()) {
-                sb.append(enclosingPath.pop());
-            }
-        }
-
-        sb.append(genTOBuilder.getName()).append("Builder");
-        final GeneratedTOBuilder unionBuilder = typeProvider.newGeneratedTOBuilder(
-            JavaTypeName.create(typeBuilder.getPackageName(), sb.toString()));
-        unionBuilder.setIsUnionBuilder(true);
-        return unionBuilder;
     }
 
     private GeneratedTypeBuilder addDefaultInterfaceDefinition(final ModuleContext context,
             final SchemaNode schemaNode) {
-        return addDefaultInterfaceDefinition(context, schemaNode, null);
+        return addDefaultInterfaceDefinition(context, schemaNode, DATA_OBJECT);
     }
 
     private GeneratedTypeBuilder addDefaultInterfaceDefinition(final ModuleContext context,
-            final SchemaNode schemaNode, final GeneratedTypeBuilder childOf) {
+            final SchemaNode schemaNode, final Type baseInterface) {
         final String packageName = packageNameForGeneratedType(context.modulePackageName(), schemaNode.getPath());
-        return addDefaultInterfaceDefinition(packageName, schemaNode, childOf, context);
+        return addDefaultInterfaceDefinition(packageName, schemaNode, baseInterface, context);
     }
 
-
     /**
-     * Instantiates generated type builder with <code>packageName</code> and
-     * <code>schemaNode</code>.
-     *
-     * The new builder always implements
-     * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.<br>
-     * If <code>schemaNode</code> is instance of GroupingDefinition it also
-     * implements {@link org.opendaylight.yangtools.yang.binding.Augmentable
-     * Augmentable}.<br>
+     * Instantiates generated type builder with <code>packageName</code> and <code>schemaNode</code>. The new builder
+     * always implements {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.<br>
+     * If <code>schemaNode</code> is instance of GroupingDefinition it also implements
+     * {@link org.opendaylight.yangtools.yang.binding.Augmentable Augmentable}.<br>
      * If <code>schemaNode</code> is instance of
-     * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
-     * DataNodeContainer} it can also implement nodes which are specified in
-     * <i>uses</i>.
+     * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer DataNodeContainer} it can also implement nodes
+     * which are specified in <i>uses</i>.
      *
-     * @param packageName
-     *            string with the name of the package to which
-     *            <code>schemaNode</code> belongs.
-     * @param schemaNode
-     *            schema node for which is created generated type builder
-     * @param parent
-     *            parent type (can be null)
+     * @param packageName string with the name of the package to which <code>schemaNode</code> belongs.
+     * @param schemaNode schema node for which is created generated type builder
+     * @param parent parent type (can be null)
      * @return generated type builder <code>schemaNode</code>
      */
     private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode,
-            final Type parent, final ModuleContext context) {
-        final GeneratedTypeBuilder it = addRawInterfaceDefinition(
-            JavaTypeName.create(packageName, BindingMapping.getClassName(schemaNode.getQName())), schemaNode);
-        if (parent == null) {
-            it.addImplementsType(DATA_OBJECT);
-        } else {
-            it.addImplementsType(BindingTypes.childOf(parent));
+            final Type baseInterface, final ModuleContext context) {
+        JavaTypeName name = renames.get(schemaNode);
+        if (name == null) {
+            name = JavaTypeName.create(packageName, BindingMapping.getClassName(schemaNode.getQName()));
         }
+
+        final GeneratedTypeBuilder it = addRawInterfaceDefinition(name, schemaNode);
+
+        it.addImplementsType(baseInterface);
         if (!(schemaNode instanceof GroupingDefinition)) {
             it.addImplementsType(augmentable(it));
         }
-
         if (schemaNode instanceof DataNodeContainer) {
-            groupingsToGenTypes(context, ((DataNodeContainer) schemaNode).getGroupings());
-            addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it);
+            final DataNodeContainer containerSchema = (DataNodeContainer) schemaNode;
+            groupingsToGenTypes(context, containerSchema.getGroupings());
+            addImplementedInterfaceFromUses(containerSchema, it);
         }
 
         return it;
@@ -1657,11 +1595,8 @@ abstract class AbstractTypeGenerator {
     /**
      * Wraps the calling of the same overloaded method.
      *
-     * @param packageName
-     *            string with the package name to which returning generated type
-     *            builder belongs
-     * @param schemaNode
-     *            schema node which provide data about the schema node name
+     * @param packageName string with the package name to which returning generated type builder belongs
+     * @param schemaNode schema node which provide data about the schema node name
      * @return generated type builder for <code>schemaNode</code>
      */
     private GeneratedTypeBuilder addRawInterfaceDefinition(final ModuleContext context, final SchemaNode schemaNode,
@@ -1672,20 +1607,13 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Returns reference to generated type builder for specified
-     * <code>schemaNode</code> with <code>packageName</code>.
-     *
-     * Firstly the generated type builder is searched in
-     * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
-     * found it is created and added to <code>genTypeBuilders</code>.
+     * Returns reference to generated type builder for specified <code>schemaNode</code> with <code>packageName</code>.
+     * Firstly the generated type builder is searched in {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}.
+     * If it is not found it is created and added to <code>genTypeBuilders</code>.
      *
-     * @param packageName
-     *            string with the package name to which returning generated type
-     *            builder belongs
-     * @param schemaNode
-     *            schema node which provide data about the schema node name
-     * @param prefix
-     *            return type name prefix
+     * @param packageName string with the package name to which returning generated type builder belongs
+     * @param schemaNode schema node which provide data about the schema node name
+     * @param prefix return type name prefix
      * @return generated type builder for <code>schemaNode</code>
      * @throws IllegalArgumentException
      *             <ul>
@@ -1694,7 +1622,6 @@ abstract class AbstractTypeGenerator {
      *             <li>if QName of schema node is null</li>
      *             <li>if schemaNode name is null</li>
      *             </ul>
-     *
      */
     private GeneratedTypeBuilder addRawInterfaceDefinition(final JavaTypeName identifier, final SchemaNode schemaNode) {
         checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
@@ -1705,7 +1632,8 @@ abstract class AbstractTypeGenerator {
         // FIXME: Validation of name conflict
         final GeneratedTypeBuilder newType = typeProvider.newGeneratedTypeBuilder(identifier);
         final Module module = findParentModule(schemaContext, schemaNode);
-        qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
+        qnameConstant(newType, JavaTypeName.create(BindingMapping.getRootPackageName(module.getQNameModule()),
+            BindingMapping.MODULE_INFO_CLASS_NAME), schemaNode.getQName().getLocalName());
 
         addCodegenInformation(newType, module, schemaNode);
         newType.setSchemaPath(schemaNode.getPath());
@@ -1729,12 +1657,9 @@ abstract class AbstractTypeGenerator {
     /**
      * Creates the name of the getter method name from <code>localName</code>.
      *
-     * @param localName
-     *            string with the name of the getter method
-     * @param returnType
-     *            return type
-     * @return string with the name of the getter method for
-     *         <code>methodName</code> in JAVA method format
+     * @param localName string with the name of the getter method
+     * @param returnType return type
+     * @return string with the name of the getter method for <code>methodName</code> in JAVA method format
      */
     public static String getterMethodName(final String localName, final Type returnType) {
         final StringBuilder method = new StringBuilder();
@@ -1749,10 +1674,9 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Created a method signature builder as part of <code>interfaceBuilder</code>.
-     *
-     * The method signature builder is created for the getter method of <code>schemaNodeName</code>.
-     * Also <code>comment</code> and <code>returnType</code> information are added to the builder.
+     * Created a method signature builder as part of <code>interfaceBuilder</code>. The method signature builder is
+     * created for the getter method of <code>schemaNodeName</code>. Also <code>comment</code>
+     * and <code>returnType</code> information are added to the builder.
      *
      * @param interfaceBuilder generated type builder for which the getter method should be created
      * @param returnType type which represents the return type of the getter method
@@ -1767,8 +1691,10 @@ abstract class AbstractTypeGenerator {
             getterMethodName(node.getQName().getLocalName(), returnType));
         getMethod.setReturnType(returnType);
 
-        if (node.getStatus() == Status.DEPRECATED) {
-            getMethod.addAnnotation("java.lang", "Deprecated");
+        annotateDeprecatedIfNecessary(node.getStatus(), getMethod);
+        if (!returnType.getPackageName().isEmpty()) {
+            // The return type has a package, so it's not a primitive type
+            getMethod.addAnnotation("javax.annotation", "Nullable");
         }
         addComment(getMethod, node);
 
@@ -1776,23 +1702,16 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
-     * or to <code>genTOBuilder</code> as property.
+     * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method or to <code>genTOBuilder</code>
+     * as a property.
      *
-     * @param basePackageName
-     *            string contains the module package name
-     * @param schemaNode
-     *            data schema node which should be added as getter method to
-     *            <code>typeBuilder</code> or as a property to
-     *            <code>genTOBuilder</code> if is part of the list key
-     * @param typeBuilder
-     *            generated type builder for the list schema node
-     * @param genTOBuilder
-     *            generated TO builder for the list keys
-     * @param listKeys
-     *            list of string which contains names of the list keys
-     * @param module
-     *            current module
+     * @param basePackageName string contains the module package name
+     * @param schemaNode data schema node which should be added as getter method to <code>typeBuilder</code>
+     *                   or as a property to <code>genTOBuilder</code> if is part of the list key
+     * @param typeBuilder generated type builder for the list schema node
+     * @param genTOBuilder generated TO builder for the list keys
+     * @param listKeys list of string which contains names of the list keys
+     * @param module current module
      * @throws IllegalArgumentException
      *             <ul>
      *             <li>if <code>schemaNode</code> equals null</li>
@@ -1820,11 +1739,12 @@ abstract class AbstractTypeGenerator {
             if (schemaNode instanceof LeafListSchemaNode) {
                 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode, context);
             } else if (schemaNode instanceof ContainerSchemaNode) {
-                containerToGenType(context, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode);
+                containerToGenType(context, typeBuilder, childOf(typeBuilder),
+                    (ContainerSchemaNode) schemaNode);
             } else if (schemaNode instanceof ChoiceSchemaNode) {
                 choiceToGeneratedType(context, typeBuilder, (ChoiceSchemaNode) schemaNode);
             } else if (schemaNode instanceof ListSchemaNode) {
-                listToGenType(context, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode);
+                listToGenType(context, typeBuilder, childOf(typeBuilder), (ListSchemaNode) schemaNode);
             }
         }
     }
@@ -1835,24 +1755,19 @@ abstract class AbstractTypeGenerator {
 
         if (genTOBuilder != null) {
             final GeneratedTransferObject genTO = genTOBuilder.build();
-
-            // Fake the 'getKey()' for items, this is equivalent to constructGetter()
-            final MethodSignatureBuilder getMethod = typeBuilder.addMethod(getterMethodName("key", genTO));
-            getMethod.setReturnType(genTO);
-            getMethod.setComment("Returns Primary Key of Yang List Type");
+            // Add Identifiable.getKey() for items
+            typeBuilder.addMethod(BindingMapping.IDENTIFIABLE_KEY_NAME).setReturnType(genTO)
+                .addAnnotation(OVERRIDE_ANNOTATION);
             context.addGeneratedTOBuilder(genTOBuilder);
         }
     }
 
     /**
-     * Selects the names of the list keys from <code>list</code> and returns
-     * them as the list of the strings
+     * Selects the names of the list keys from <code>list</code> and returns them as the list of the strings.
      *
-     * @param list
-     *            of string with names of the list keys
-     * @return list of string which represents names of the list keys. If the
-     *         <code>list</code> contains no keys then the empty list is
-     *         returned.
+     * @param list of string with names of the list keys
+     * @return list of string which represents names of the list keys. If the <code>list</code> contains no keys then
+     *         an empty list is returned.
      */
     private static List<String> listKeys(final ListSchemaNode list) {
         final List<String> listKeys = new ArrayList<>();
@@ -1867,16 +1782,12 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Generates for the <code>list</code> which contains any list keys special
-     * generated TO builder.
+     * Generates for the <code>list</code> which contains any list keys special generated TO builder.
      *
-     * @param packageName
-     *            string with package name to which the list belongs
-     * @param list
-     *            list schema node which is source of data about the list name
-     * @return generated TO builder which represents the keys of the
-     *         <code>list</code> or null if <code>list</code> is null or list of
-     *         key definitions is null or empty.
+     * @param packageName string with package name to which the list belongs
+     * @param list list schema node which is source of data about the list name
+     * @return generated TO builder which represents the keys of the <code>list</code> or null if <code>list</code> is
+     *         null or list of key definitions is null or empty.
      */
     private GeneratedTOBuilder resolveListKeyTOBuilder(final ModuleContext context, final ListSchemaNode list) {
         if (list.getKeyDefinition() != null && !list.getKeyDefinition().isEmpty()) {
@@ -1888,25 +1799,17 @@ abstract class AbstractTypeGenerator {
     }
 
     /**
-     * Builds a GeneratedTOBuilder for a UnionType {@link UnionTypeDefinition}.
-     *
-     * If more then one generated TO builder is created for enclosing then all
-     * of the generated TO builders are added to <code>typeBuilder</code> as
+     * Builds a GeneratedTOBuilder for a UnionType {@link UnionTypeDefinition}. If more then one generated TO builder
+     * is created for enclosing then all of the generated TO builders are added to <code>typeBuilder</code> as
      * enclosing transfer objects.
      *
-     * @param typeDef
-     *            type definition which can be of type <code>UnionType</code> or
-     *            <code>BitsTypeDefinition</code>
-     * @param typeBuilder
-     *            generated type builder to which is added generated TO created
-     *            from <code>typeDef</code>
-     * @param leaf
-     *            string with name for generated TO builder
-     * @param parentModule
-     *            parent module
+     * @param typeDef type definition which can be of type <code>UnionType</code> or <code>BitsTypeDefinition</code>
+     * @param typeBuilder generated type builder to which is added generated TO created from <code>typeDef</code>
+     * @param leaf string with name for generated TO builder
+     * @param parentModule parent module
      * @return generated TO builder for <code>typeDef</code>
      */
-    private GeneratedTOBuilder addTOToTypeBuilder(final UnionTypeDefinition typeDef,
+    private Type addTOToTypeBuilder(final UnionTypeDefinition typeDef,
             final GeneratedTypeBuilder typeBuilder, final DataSchemaNode leaf, final Module parentModule) {
         final List<GeneratedTOBuilder> types = typeProvider.provideGeneratedTOBuildersForUnionTypeDef(
             typeBuilder.getIdentifier().createEnclosed(BindingMapping.getClassName(leaf.getQName())),
@@ -1915,37 +1818,29 @@ abstract class AbstractTypeGenerator {
         checkState(!types.isEmpty(), "No GeneratedTOBuilder objects generated from union %s", typeDef);
         final List<GeneratedTOBuilder> genTOBuilders = new ArrayList<>(types);
         final GeneratedTOBuilder resultTOBuilder = types.remove(0);
-        for (final GeneratedTOBuilder genTOBuilder : types) {
-            resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
+        types.forEach(resultTOBuilder::addEnclosingTransferObject);
+        genTOBuilders.forEach(typeBuilder::addEnclosingTransferObject);
+
+        for (GeneratedTOBuilder builder : types) {
+            if (builder.isUnion()) {
+                final GeneratedTransferObject type = builder.build();
+                createUnionBuilder(builder, typeBuilder, type, parentModule);
+            }
         }
 
-        final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
-        genPropBuilder.setReturnType(Types.CHAR_ARRAY);
-        resultTOBuilder.addEqualsIdentity(genPropBuilder);
-        resultTOBuilder.addHashIdentity(genPropBuilder);
-        resultTOBuilder.addToStringProperty(genPropBuilder);
-        processEnclosedTOBuilderes(typeBuilder, genTOBuilders);
-        return resultTOBuilder;
+        return createReturnTypeForUnion(resultTOBuilder, typeDef, typeBuilder, parentModule);
     }
 
     /**
-     * Builds generated TO builders for <code>typeDef</code> of type {@link BitsTypeDefinition} which are
-     * also added to <code>typeBuilder</code> as enclosing transfer object.
-     *
-     * If more then one generated TO builder is created for enclosing then all
-     * of the generated TO builders are added to <code>typeBuilder</code> as
-     * enclosing transfer objects.
+     * Builds generated TO builders for <code>typeDef</code> of type {@link BitsTypeDefinition} which are also added
+     * to <code>typeBuilder</code> as enclosing transfer object. If more then one generated TO builder is created
+     * for enclosing then all of the generated TO builders are added to <code>typeBuilder</code> as enclosing transfer
+     * objects.
      *
-     * @param typeDef
-     *            type definition which can be of type <code>UnionType</code> or
-     *            <code>BitsTypeDefinition</code>
-     * @param typeBuilder
-     *            generated type builder to which is added generated TO created
-     *            from <code>typeDef</code>
-     * @param leaf
-     *            string with name for generated TO builder
-     * @param parentModule
-     *            parent module
+     * @param typeDef type definition which can be of type <code>UnionType</code> or <code>BitsTypeDefinition</code>
+     * @param typeBuilder generated type builder to which is added generated TO created from <code>typeDef</code>
+     * @param leaf string with name for generated TO builder
+     * @param parentModule parent module
      * @return generated TO builder for <code>typeDef</code>
      */
     private GeneratedTOBuilder addTOToTypeBuilder(final BitsTypeDefinition typeDef,
@@ -1958,28 +1853,14 @@ abstract class AbstractTypeGenerator {
 
     }
 
-    private static GeneratedTOBuilder processEnclosedTOBuilderes(final GeneratedTypeBuilder typeBuilder,
-            final List<GeneratedTOBuilder> genTOBuilders) {
-        for (final GeneratedTOBuilder genTOBuilder : genTOBuilders) {
-            typeBuilder.addEnclosingTransferObject(genTOBuilder);
-        }
-        return genTOBuilders.get(0);
-    }
-
     /**
-     * Adds the implemented types to type builder.
-     *
-     * The method passes through the list of <i>uses</i> in
-     * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding
-     * generated type from {@link ModuleContext#groupings
-     * allGroupings} which is added as <i>implements type</i> to
-     * <code>builder</code>
+     * Adds the implemented types to type builder. The method passes through the list of <i>uses</i> in
+     * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding generated type
+     * from {@link ModuleContext#groupings allGroupings} which is added as <i>implements type</i>
+     * to <code>builder</code>
      *
-     * @param dataNodeContainer
-     *            element which contains the list of used YANG groupings
-     * @param builder
-     *            builder to which are added implemented types according to
-     *            <code>dataNodeContainer</code>
+     * @param dataNodeContainer element which contains the list of used YANG groupings
+     * @param builder builder to which are added implemented types according to <code>dataNodeContainer</code>
      * @return generated type builder with all implemented types
      */
     private GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer,
@@ -2026,9 +1907,9 @@ abstract class AbstractTypeGenerator {
         return null;
     }
 
-    private static void annotateDeprecatedIfNecessary(final Status status, final GeneratedTypeBuilder builder) {
+    private static void annotateDeprecatedIfNecessary(final Status status, final AnnotableTypeBuilder builder) {
         if (status == Status.DEPRECATED) {
-            builder.addAnnotation("java.lang", "Deprecated");
+            builder.addAnnotation(DEPRECATED_ANNOTATION);
         }
     }
 }