Bug 8449 - BindingToNormalizedNodeCodec fails to deserialize union of leafrefs 19/57619/10
authorJakub Toth <jakub.toth@pantheon.tech>
Fri, 19 May 2017 16:00:50 +0000 (18:00 +0200)
committerJakub Toth <jakub.toth@pantheon.tech>
Tue, 6 Jun 2017 11:59:11 +0000 (13:59 +0200)
Fix problem of leafref in typedef called from union
 *generated part
   *generating of new property of GTO for leaf's union type of typedef
     according to return type of referenced leaf via leafref from typedef
 *codec part
   *getting codec of leaf type according to new generator part of leafref
    in typedef
 *tests

Change-Id: I156c3efe1c614c8e6ed2be086edd08b08ace1e76
Signed-off-by: Jakub Toth <jakub.toth@pantheon.tech>
binding/binding-parent/src/main/yang/test-import.yang [new file with mode: 0644]
binding/binding-parent/src/main/yang/test-typedef-with-import.yang [new file with mode: 0644]
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/yangtools/binding/data/codec/impl/UnionTypeCodec.java
binding/mdsal-binding-dom-codec/src/test/java/org/opendaylight/yangtools/binding/data/codec/test/LeafrefSerializeDeserializeTest.java [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java
binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/UnionTypeDefTest.java
binding/mdsal-binding-generator-impl/src/test/resources/leafref_typedef_union/bug8449.yang [new file with mode: 0644]
binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/generated/type/builder/AbstractTypeMemberBuilder.java
binding/mdsal-binding-test-model/src/main/yang/bug8449.yang [new file with mode: 0644]

diff --git a/binding/binding-parent/src/main/yang/test-import.yang b/binding/binding-parent/src/main/yang/test-import.yang
new file mode 100644 (file)
index 0000000..4111504
--- /dev/null
@@ -0,0 +1,11 @@
+module test-import {
+
+    yang-version 1;
+    namespace "urn:test:simple:test:import";
+    prefix "test-import";
+
+    revision 2017-04-21;
+
+    container my-import-cont {
+    }
+}
diff --git a/binding/binding-parent/src/main/yang/test-typedef-with-import.yang b/binding/binding-parent/src/main/yang/test-typedef-with-import.yang
new file mode 100644 (file)
index 0000000..f0eb73a
--- /dev/null
@@ -0,0 +1,17 @@
+module test {
+
+    yang-version 1;
+    namespace "urn:test:simple:test";
+    prefix "test";
+
+    import test-import { prefix "imported-test"; revision-date 2017-04-21; }
+
+    revision 2017-02-06;
+
+    typedef my-type {
+        type int8;
+    }
+
+    container my-cont {
+    }
+}
index ee161a9b537a9cb1288e540ec621da799cd10523..13696a9bfd77d0d665824675fc879b6100d09294 100644 (file)
@@ -13,35 +13,98 @@ import java.util.LinkedHashSet;
 import java.util.Set;
 import java.util.concurrent.Callable;
 import org.opendaylight.yangtools.concepts.Codec;
+import org.opendaylight.yangtools.sal.binding.yang.types.BaseYangTypes;
 import org.opendaylight.yangtools.yang.binding.BindingMapping;
+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.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
+import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
 
 final class UnionTypeCodec extends ReflectionBasedCodec {
     private final ImmutableSet<UnionValueOptionContext> typeCodecs;
 
     private UnionTypeCodec(final Class<?> unionCls,final Set<UnionValueOptionContext> codecs) {
         super(unionCls);
-        typeCodecs = ImmutableSet.copyOf(codecs);
+        this.typeCodecs = ImmutableSet.copyOf(codecs);
     }
 
     static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType,
                                            final BindingCodecContext bindingCodecContext) {
         return () -> {
             final Set<UnionValueOptionContext> values = new LinkedHashSet<>();
-            for (TypeDefinition<?> subtype : unionType.getTypes()) {
-                Method valueGetter = unionCls.getMethod("get" + BindingMapping.getClassName(subtype.getQName()));
-                Class<?> valueType = valueGetter.getReturnType();
-                Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(valueType, subtype);
-                values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, valueCodec));
+            for (final TypeDefinition<?> subtype : unionType.getTypes()) {
+                if (subtype instanceof LeafrefTypeDefinition) {
+                    addLeafrefValueCodec(unionCls, unionType, bindingCodecContext, values, subtype);
+                } else {
+                    final Method valueGetter =
+                            unionCls.getMethod("get" + BindingMapping.getClassName(subtype.getQName()));
+                    final Class<?> valueType = valueGetter.getReturnType();
+                    final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(valueType, subtype);
+
+                    values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, valueCodec));
+                }
             }
             return new UnionTypeCodec(unionCls, values);
         };
     }
 
+    /**
+     * Prepare codec for type from leaf's return type of leafref.
+     *
+     * @param unionCls
+     *            - union class
+     * @param unionType
+     *            - union type
+     * @param bindingCodecContext
+     *            - binding codec context
+     * @param values
+     *            - union values
+     * @param subtype
+     *            - subtype of union
+     * @throws NoSuchMethodException
+     */
+    private static void addLeafrefValueCodec(final Class<?> unionCls, final UnionTypeDefinition unionType,
+            final BindingCodecContext bindingCodecContext, final Set<UnionValueOptionContext> values,
+            final TypeDefinition<?> subtype) throws NoSuchMethodException {
+        final SchemaContext schemaContext = bindingCodecContext.getRuntimeContext().getSchemaContext();
+        final Module module = schemaContext.findModuleByNamespaceAndRevision(
+                subtype.getQName().getNamespace(), subtype.getQName().getRevision());
+        final RevisionAwareXPath xpath = ((LeafrefTypeDefinition) subtype).getPathStatement();
+        // find schema node in schema context by xpath of leafref
+        final SchemaNode dataNode;
+        if (xpath.isAbsolute()) {
+            dataNode = SchemaContextUtil.findDataSchemaNode(schemaContext, module, xpath);
+        } else {
+            dataNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, module,
+                    unionType, xpath);
+        }
+        final String className = BindingMapping.getClassName(unionCls.getSimpleName());
+        final LeafSchemaNode typeNode = (LeafSchemaNode) dataNode;
+
+        // prepare name of type form return type of referenced leaf
+        final String typeName = BindingMapping.getClassName(BaseYangTypes.BASE_YANG_TYPES_PROVIDER
+                .javaTypeForSchemaDefinitionType(typeNode.getType(), typeNode).getName());
+
+        // get method via reflection from generated code according to
+        // get_TypeName_Value method
+        final Method valueGetterParent = unionCls.getMethod(
+                new StringBuilder("get").append(typeName).append(className).append("Value").toString());
+        final Class<?> returnType = valueGetterParent.getReturnType();
+
+        // prepare codec of union subtype according to return type of referenced
+        // leaf
+        final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(returnType, subtype);
+        values.add(new UnionValueOptionContext(unionCls, returnType, valueGetterParent, valueCodec));
+    }
+
     @Override
     public Object deserialize(final Object input) {
-        for (UnionValueOptionContext member : typeCodecs) {
+        for (final UnionValueOptionContext member : this.typeCodecs) {
             final Object ret = member.deserializeUnion(input);
             if (ret != null) {
                 return ret;
@@ -55,7 +118,7 @@ final class UnionTypeCodec extends ReflectionBasedCodec {
     @Override
     public Object serialize(final Object input) {
         if (input != null) {
-            for (UnionValueOptionContext valCtx : typeCodecs) {
+            for (final UnionValueOptionContext valCtx : this.typeCodecs) {
                 final Object domValue = valCtx.serialize(input);
                 if (domValue != null) {
                     return domValue;
diff --git a/binding/mdsal-binding-dom-codec/src/test/java/org/opendaylight/yangtools/binding/data/codec/test/LeafrefSerializeDeserializeTest.java b/binding/mdsal-binding-dom-codec/src/test/java/org/opendaylight/yangtools/binding/data/codec/test/LeafrefSerializeDeserializeTest.java
new file mode 100644 (file)
index 0000000..25a50ce
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.binding.data.codec.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Map.Entry;
+import javassist.ClassPool;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.bug8449.rev180405.Cont;
+import org.opendaylight.yang.gen.v1.bug8449.rev180405.Cont.Ref;
+import org.opendaylight.yang.gen.v1.bug8449.rev180405.ContBuilder;
+import org.opendaylight.yang.gen.v1.bug8449.rev180405.ContInt32;
+import org.opendaylight.yang.gen.v1.bug8449.rev180405.ContInt32.RefUnionInt32;
+import org.opendaylight.yang.gen.v1.bug8449.rev180405.ContInt32Builder;
+import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
+import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
+import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+public class LeafrefSerializeDeserializeTest extends AbstractBindingRuntimeTest {
+
+    private BindingNormalizedNodeCodecRegistry registry;
+
+    @Override
+    @Before
+    public void setup() {
+        super.setup();
+        final JavassistUtils utils = JavassistUtils.forClassPool(ClassPool.getDefault());
+        this.registry = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(utils));
+        this.registry.onBindingRuntimeContextUpdated(getRuntimeContext());
+    }
+
+    @Test
+    public void listReferenceTest() {
+        final YangInstanceIdentifier contYII = YangInstanceIdentifier.builder().node(Cont.QNAME).build();
+        final InstanceIdentifier<?> fromYangInstanceIdentifier = this.registry.fromYangInstanceIdentifier(contYII);
+        assertNotNull(fromYangInstanceIdentifier);
+
+        final InstanceIdentifier<Cont> BA_II_CONT = InstanceIdentifier.builder(Cont.class).build();
+        final Ref refVal = new Ref("myvalue");
+        final Cont data = new ContBuilder().setRef(refVal).build();
+        final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedNode =
+                this.registry.toNormalizedNode(BA_II_CONT, data);
+        assertNotNull(normalizedNode);
+
+        final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode =
+                this.registry.fromNormalizedNode(contYII, normalizedNode.getValue());
+        assertNotNull(fromNormalizedNode);
+        final Cont value = (Cont) fromNormalizedNode.getValue();
+        assertEquals(refVal, value.getRef());
+    }
+
+    @Test
+    public void uint32LeafrefTest() {
+        final YangInstanceIdentifier contYII = YangInstanceIdentifier.builder().node(ContInt32.QNAME).build();
+        final InstanceIdentifier<?> fromYangInstanceIdentifier = this.registry.fromYangInstanceIdentifier(contYII);
+        assertNotNull(fromYangInstanceIdentifier);
+
+        final InstanceIdentifier<ContInt32> BA_II_CONT = InstanceIdentifier.builder(ContInt32.class).build();
+        final RefUnionInt32 refVal = new RefUnionInt32(5l);
+        final ContInt32 data = new ContInt32Builder().setRefUnionInt32(refVal).build();
+        final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedNode =
+                this.registry.toNormalizedNode(BA_II_CONT, data);
+        assertNotNull(normalizedNode);
+
+        final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode =
+                this.registry.fromNormalizedNode(contYII, normalizedNode.getValue());
+        assertNotNull(fromNormalizedNode);
+        final ContInt32 value = (ContInt32) fromNormalizedNode.getValue();
+        assertEquals(refVal, value.getRefUnionInt32());
+    }
+}
index c485d01a87306170cc51a496354898ad4d2e1957..ed6fd66c088561fb158d6007d17dfe6d34c4b5a4 100644 (file)
@@ -210,8 +210,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
     public List<Type> generateTypes(final SchemaContext context) {
         checkArgument(context != null, "Schema Context reference cannot be NULL.");
         checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
-        schemaContext = context;
-        typeProvider = new TypeProviderImpl(context);
+        this.schemaContext = context;
+        this.typeProvider = new TypeProviderImpl(context);
         final Set<Module> modules = context.getModules();
         return generateTypes(context, modules);
     }
@@ -250,12 +250,12 @@ public class BindingGeneratorImpl implements BindingGenerator {
         checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
         checkArgument(modules != null, "Set of Modules cannot be NULL.");
 
-        schemaContext = context;
-        typeProvider = new TypeProviderImpl(context);
+        this.schemaContext = context;
+        this.typeProvider = new TypeProviderImpl(context);
         final Module[] modulesArray = new Module[context.getModules().size()];
         context.getModules().toArray(modulesArray);
         final List<Module> contextModules = ModuleDependencySort.sort(modulesArray);
-        genTypeBuilders = new HashMap<>();
+        this.genTypeBuilders = new HashMap<>();
 
         for (final Module contextModule : contextModules) {
             moduleToGenTypes(contextModule, context);
@@ -266,9 +266,9 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
         final List<Type> filteredGenTypes = new ArrayList<>();
         for (final Module m : modules) {
-            final ModuleContext ctx = checkNotNull(genCtx.get(m), "Module context not found for module %s", m);
+            final ModuleContext ctx = checkNotNull(this.genCtx.get(m), "Module context not found for module %s", m);
             filteredGenTypes.addAll(ctx.getGeneratedTypes());
-            final Set<Type> additionalTypes = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(m);
+            final Set<Type> additionalTypes = ((TypeProviderImpl) this.typeProvider).getAdditionalTypes().get(m);
             if (additionalTypes != null) {
                 filteredGenTypes.addAll(additionalTypes);
             }
@@ -278,7 +278,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
     }
 
     private void moduleToGenTypes(final Module m, final SchemaContext context) {
-        genCtx.put(m, new ModuleContext());
+        this.genCtx.put(m, new ModuleContext());
         allTypeDefinitionsToGenTypes(m);
         groupingsToGenTypes(m, m.getGroupings());
         rpcMethodsToGenType(m);
@@ -287,7 +287,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
         if (!m.getChildNodes().isEmpty()) {
             final GeneratedTypeBuilder moduleType = moduleToDataType(m);
-            genCtx.get(m).addModuleNode(moduleType);
+            this.genCtx.get(m).addModuleNode(moduleType);
             final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
             resolveDataSchemaNodes(m, basePackageName, moduleType, moduleType, m.getChildNodes());
         }
@@ -316,10 +316,10 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
         for (final TypeDefinition<?> typedef : typeDefinitions) {
             if (typedef != null) {
-                final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
+                final Type type = ((TypeProviderImpl) this.typeProvider).generatedTypeForExtendedDefinitionType(typedef,
                         typedef);
                 if (type != null) {
-                    final ModuleContext ctx = genCtx.get(module);
+                    final ModuleContext ctx = this.genCtx.get(module);
                     ctx.addTypedefType(typedef.getPath(), type);
                     ctx.addTypeToSchema(type,typedef);
                 }
@@ -341,7 +341,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         genType.setReference(node.getReference());
         genType.setSchemaPath(node.getPath().getPathFromRoot());
         if (node instanceof DataNodeContainer) {
-            genCtx.get(module).addChildNodeType(node, genType);
+            this.genCtx.get(module).addChildNodeType(node, genType);
             groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings());
             processUsesAugments((DataNodeContainer) node, module);
         }
@@ -523,7 +523,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                     inType.addImplementsType(augmentable(inType));
                     annotateDeprecatedIfNecessary(rpc.getStatus(), inType);
                     resolveDataSchemaNodes(module, basePackageName, inType, inType, input.getChildNodes());
-                    genCtx.get(module).addChildNodeType(input, inType);
+                    this.genCtx.get(module).addChildNodeType(input, inType);
                     final GeneratedType inTypeInstance = inType.toInstance();
                     method.addParameter(inTypeInstance, "input");
                 }
@@ -538,7 +538,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                     outType.addImplementsType(augmentable(outType));
                     annotateDeprecatedIfNecessary(rpc.getStatus(), outType);
                     resolveDataSchemaNodes(module, basePackageName, outType, outType, output.getChildNodes());
-                    genCtx.get(module).addChildNodeType(output, outType);
+                    this.genCtx.get(module).addChildNodeType(output, outType);
                     outTypeInstance = outType.toInstance();
                 }
 
@@ -548,10 +548,10 @@ public class BindingGeneratorImpl implements BindingGenerator {
             }
         }
 
-        genCtx.get(module).addTopLevelNodeType(interfaceBuilder);
+        this.genCtx.get(module).addTopLevelNodeType(interfaceBuilder);
     }
 
-    private static boolean isExplicitStatement(ContainerSchemaNode node) {
+    private static boolean isExplicitStatement(final ContainerSchemaNode node) {
         return node instanceof EffectiveStatement
                 && ((EffectiveStatement) node).getDeclared().getStatementSource() == StatementSource.DECLARATION;
     }
@@ -595,7 +595,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                         notification, null, module);
                 annotateDeprecatedIfNecessary(notification.getStatus(), notificationInterface);
                 notificationInterface.addImplementsType(NOTIFICATION);
-                genCtx.get(module).addChildNodeType(notification, notificationInterface);
+                this.genCtx.get(module).addChildNodeType(notification, notificationInterface);
 
                 // Notification object
                 resolveDataSchemaNodes(module, basePackageName, notificationInterface, notificationInterface,
@@ -608,7 +608,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         }
         listenerInterface.setDescription(createDescription(notifications, module.getName()));
 
-        genCtx.get(module).addTopLevelNodeType(listenerInterface);
+        this.genCtx.get(module).addTopLevelNodeType(listenerInterface);
     }
 
     /**
@@ -685,7 +685,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         final QName qname = identity.getQName();
         qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, qname);
 
-        genCtx.get(module).addIdentityType(identity.getQName(), newType);
+        this.genCtx.get(module).addIdentityType(identity.getQName(), newType);
     }
 
     private static Constant qnameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName,
@@ -733,7 +733,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath());
         final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, grouping, module);
         annotateDeprecatedIfNecessary(grouping.getStatus(), genType);
-        genCtx.get(module).addGroupingType(grouping.getPath(), genType);
+        this.genCtx.get(module).addGroupingType(grouping.getPath(), genType);
         resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes());
         groupingsToGenTypes(module, grouping.getGroupings());
         processUsesAugments(grouping, module);
@@ -767,7 +767,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             final String enumTypedefDescription = encodeAngleBrackets(enumTypeDef.getDescription());
             enumBuilder.setDescription(enumTypedefDescription);
             enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
-            ModuleContext ctx = genCtx.get(module);
+            final ModuleContext ctx = this.genCtx.get(module);
             ctx.addInnerTypedefType(enumTypeDef.getPath(), enumBuilder);
             return enumBuilder;
         }
@@ -836,7 +836,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         final SchemaPath targetPath = augSchema.getTargetPath();
         SchemaNode targetSchemaNode = null;
 
-        targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
+        targetSchemaNode = findDataSchemaNode(this.schemaContext, targetPath);
         if (targetSchemaNode instanceof DataSchemaNode && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
             if (targetSchemaNode instanceof DerivableSchemaNode) {
                 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
@@ -915,7 +915,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
      * @return node from its original location in grouping
      */
     private DataSchemaNode findOriginalTargetFromGrouping(final SchemaPath targetPath, final UsesNode parentUsesNode) {
-        final SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, parentUsesNode.getGroupingPath()
+        final SchemaNode targetGrouping = findNodeInSchemaContext(this.schemaContext, parentUsesNode.getGroupingPath()
                 .getPathFromRoot());
         if (!(targetGrouping instanceof GroupingDefinition)) {
             throw new IllegalArgumentException("Failed to generate code for augment in " + parentUsesNode);
@@ -936,8 +936,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
         }
 
         if (result instanceof DerivableSchemaNode) {
-            DerivableSchemaNode castedResult = (DerivableSchemaNode) result;
-            Optional<? extends SchemaNode> originalNode = castedResult
+            final DerivableSchemaNode castedResult = (DerivableSchemaNode) result;
+            final Optional<? extends SchemaNode> originalNode = castedResult
                     .getOriginal();
             if (castedResult.isAddedByUses() && originalNode.isPresent()) {
                 result = originalNode.get();
@@ -945,7 +945,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         }
 
         if (result instanceof DataSchemaNode) {
-            DataSchemaNode resultDataSchemaNode = (DataSchemaNode) result;
+            final DataSchemaNode resultDataSchemaNode = (DataSchemaNode) result;
             if (resultDataSchemaNode.isAddedByUses()) {
                 // The original node is required, but we have only the copy of
                 // the original node.
@@ -986,10 +986,10 @@ public class BindingGeneratorImpl implements BindingGenerator {
      */
     private GeneratedTypeBuilder addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
             final String basePackageName, final Type targetTypeRef, final AugmentationSchema augSchema) {
-        Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.get(augmentPackageName);
+        Map<String, GeneratedTypeBuilder> augmentBuilders = this.genTypeBuilders.get(augmentPackageName);
         if (augmentBuilders == null) {
             augmentBuilders = new HashMap<>();
-            genTypeBuilders.put(augmentPackageName, augmentBuilders);
+            this.genTypeBuilders.put(augmentPackageName, augmentBuilders);
         }
         final String augIdentifier = getAugmentIdentifier(augSchema.getUnknownSchemaNodes());
 
@@ -1011,10 +1011,10 @@ public class BindingGeneratorImpl implements BindingGenerator {
         augmentBuilders.put(augTypeName, augTypeBuilder);
 
         if (!augSchema.getChildNodes().isEmpty()) {
-            genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
+            this.genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
 
         }
-        genCtx.get(module).addAugmentType(augTypeBuilder);
+        this.genCtx.get(module).addAugmentType(augTypeBuilder);
         return augTypeBuilder;
     }
 
@@ -1201,7 +1201,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                     choiceNode.getDescription(), choiceTypeBuilder, choiceNode.getStatus());
             choiceTypeBuilder.addImplementsType(typeForClass(DataContainer.class));
             annotateDeprecatedIfNecessary(choiceNode.getStatus(), choiceTypeBuilder);
-            genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder);
+            this.genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder);
             generateTypesFromChoiceCases(module, basePackageName, choiceTypeBuilder.toInstance(), choiceNode);
         }
     }
@@ -1250,19 +1250,19 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode, module);
                 caseTypeBuilder.addImplementsType(refChoiceType);
                 annotateDeprecatedIfNecessary(caseNode.getStatus(), caseTypeBuilder);
-                genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
-                genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
+                this.genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
+                this.genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
                 final Iterable<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
                 if (caseChildNodes != null) {
                     final SchemaPath choiceNodeParentPath = choiceNode.getPath().getParent();
 
                     if (!Iterables.isEmpty(choiceNodeParentPath.getPathFromRoot())) {
-                        SchemaNode parent = findDataSchemaNode(schemaContext, choiceNodeParentPath);
+                        SchemaNode parent = findDataSchemaNode(this.schemaContext, choiceNodeParentPath);
 
                         if (parent instanceof AugmentationSchema) {
                             final AugmentationSchema augSchema = (AugmentationSchema) parent;
                             final SchemaPath targetPath = augSchema.getTargetPath();
-                            SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
+                            SchemaNode targetSchemaNode = findDataSchemaNode(this.schemaContext, targetPath);
                             if (targetSchemaNode instanceof DataSchemaNode
                                     && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
                                 if (targetSchemaNode instanceof DerivableSchemaNode) {
@@ -1336,11 +1336,11 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
                 SchemaNode parent = null;
                 final SchemaPath nodeSp = targetNode.getPath();
-                parent = findDataSchemaNode(schemaContext, nodeSp.getParent());
+                parent = findDataSchemaNode(this.schemaContext, nodeSp.getParent());
 
                 GeneratedTypeBuilder childOfType = null;
                 if (parent instanceof Module) {
-                    childOfType = genCtx.get(parent).getModuleNode();
+                    childOfType = this.genCtx.get(parent).getModuleNode();
                 } else if (parent instanceof ChoiceCaseNode) {
                     childOfType = findCaseByPath(parent.getPath());
                 } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
@@ -1359,7 +1359,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                     node = (ChoiceCaseNode) caseNode;
                 } else if (targetNode.getCaseNodeByName(caseLocalName) == null) {
                     final String targetNodeLocalName = targetNode.getQName().getLocalName();
-                    for (DataSchemaNode dataSchemaNode : usesNodeParent.getChildNodes()) {
+                    for (final DataSchemaNode dataSchemaNode : usesNodeParent.getChildNodes()) {
                         if (dataSchemaNode instanceof ChoiceSchemaNode && targetNodeLocalName.equals(dataSchemaNode.getQName
                                 ().getLocalName())) {
                             node = ((ChoiceSchemaNode) dataSchemaNode).getCaseNodeByName(caseLocalName);
@@ -1373,8 +1373,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 if (childNodes != null) {
                     resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, childNodes);
                 }
-                genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
-                genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node);
+                this.genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
+                this.genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node);
             }
         }
     }
@@ -1422,27 +1422,27 @@ public class BindingGeneratorImpl implements BindingGenerator {
             return null;
         }
 
-        final Module parentModule = findParentModule(schemaContext, leaf);
+        final Module parentModule = findParentModule(this.schemaContext, leaf);
         Type returnType = null;
 
         final TypeDefinition<?> typeDef = CompatUtils.compatLeafType(leaf);
         if (isInnerType(leaf, typeDef)) {
             if (typeDef instanceof EnumTypeDefinition) {
-                returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
+                returnType = this.typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
                 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
                 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
                     typeBuilder, module);
                 if (enumBuilder != null) {
                     returnType = enumBuilder.toInstance(typeBuilder);
                 }
-                ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
+                ((TypeProviderImpl) this.typeProvider).putReferencedType(leaf.getPath(), returnType);
             } else if (typeDef instanceof UnionTypeDefinition) {
-                GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
+                final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
                 if (genTOBuilder != null) {
                     returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
                 }
             } else if (typeDef instanceof BitsTypeDefinition) {
-                GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
+                final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
                 if (genTOBuilder != null) {
                     returnType = genTOBuilder.toInstance();
                 }
@@ -1452,12 +1452,12 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 // In order to get proper class we need to look up closest derived type
                 // and apply restrictions from leaf type
                 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
-                returnType = typeProvider.javaTypeForSchemaDefinitionType(getBaseOrDeclaredType(typeDef), leaf,
+                returnType = this.typeProvider.javaTypeForSchemaDefinitionType(getBaseOrDeclaredType(typeDef), leaf,
                         restrictions);
             }
         } else {
             final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
-            returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
+            returnType = this.typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
         }
 
         if (returnType == null) {
@@ -1465,7 +1465,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         }
 
         if (typeDef instanceof EnumTypeDefinition) {
-            ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
+            ((TypeProviderImpl) this.typeProvider).putReferencedType(leaf.getPath(), returnType);
         }
 
         String leafDesc = leaf.getDescription();
@@ -1538,7 +1538,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
     private Module findModuleFromImports(final Set<ModuleImport> imports, final String prefix) {
         for (final ModuleImport imp : imports) {
             if (imp.getPrefix().equals(prefix)) {
-                return schemaContext.findModuleByName(imp.getModuleName(), imp.getRevision());
+                return this.schemaContext.findModuleByName(imp.getModuleName(), imp.getRevision());
             }
         }
         return null;
@@ -1557,19 +1557,19 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 // GeneratedType for this type definition should be already
                 // created
                 final QName qname = typeDef.getQName();
-                final Module unionModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
+                final Module unionModule = this.schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
                         qname.getRevision());
-                final ModuleContext mc = genCtx.get(unionModule);
+                final ModuleContext mc = this.genCtx.get(unionModule);
                 returnType = mc.getTypedefs().get(typeDef.getPath());
             } else if (typeDef instanceof EnumTypeDefinition && typeDef.getBaseType() == null) {
                 // Annonymous enumeration (already generated, since it is inherited via uses).
-                LeafSchemaNode originalLeaf = (LeafSchemaNode) SchemaNodeUtils.getRootOriginalIfPossible(leaf);
-                QName qname = originalLeaf.getQName();
-                final Module enumModule =  schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
+                final LeafSchemaNode originalLeaf = (LeafSchemaNode) SchemaNodeUtils.getRootOriginalIfPossible(leaf);
+                final QName qname = originalLeaf.getQName();
+                final Module enumModule =  this.schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
                         qname.getRevision());
-                returnType = genCtx.get(enumModule).getInnerType(originalLeaf.getType().getPath());
+                returnType = this.genCtx.get(enumModule).getInnerType(originalLeaf.getType().getPath());
             } else {
-                returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
+                returnType = this.typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
             }
             return resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
         }
@@ -1643,17 +1643,17 @@ public class BindingGeneratorImpl implements BindingGenerator {
         }
 
         final TypeDefinition<?> typeDef = node.getType();
-        final Module parentModule = findParentModule(schemaContext, node);
+        final Module parentModule = findParentModule(this.schemaContext, node);
 
         Type returnType = null;
         if (typeDef.getBaseType() == null) {
             if (typeDef instanceof EnumTypeDefinition) {
-                returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
+                returnType = this.typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
                 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
                 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
                     typeBuilder,module);
                 returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
-                ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
+                ((TypeProviderImpl) this.typeProvider).putReferencedType(node.getPath(), returnType);
             } else if (typeDef instanceof UnionTypeDefinition) {
                 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
                 if (genTOBuilder != null) {
@@ -1664,11 +1664,11 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 returnType = genTOBuilder.toInstance();
             } else {
                 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
-                returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
+                returnType = this.typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
             }
         } else {
             final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
-            returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
+            returnType = this.typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
         }
 
         final ParameterizedType listType = Types.listTypeFor(returnType);
@@ -1702,9 +1702,9 @@ public class BindingGeneratorImpl implements BindingGenerator {
         method.setAccessModifier(AccessModifier.PUBLIC);
         method.setStatic(true);
 
-        final Set<Type> types = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(parentModule);
+        final Set<Type> types = ((TypeProviderImpl) this.typeProvider).getAdditionalTypes().get(parentModule);
         if (types == null) {
-            ((TypeProviderImpl) typeProvider).getAdditionalTypes().put(parentModule,
+            ((TypeProviderImpl) this.typeProvider).getAdditionalTypes().put(parentModule,
                     Sets.<Type> newHashSet(unionBuilder.toInstance()));
         } else {
             types.add(unionBuilder.toInstance());
@@ -1831,7 +1831,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
         // FIXME: Validation of name conflict
         final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, genTypeName);
-        final Module module = findParentModule(schemaContext, schemaNode);
+        final Module module = findParentModule(this.schemaContext, schemaNode);
         qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
         newType.addComment(schemaNode.getDescription());
         newType.setDescription(createDescription(schemaNode, newType.getFullyQualifiedName()));
@@ -1839,12 +1839,12 @@ public class BindingGeneratorImpl implements BindingGenerator {
         newType.setSchemaPath(schemaNode.getPath().getPathFromRoot());
         newType.setModuleName(module.getName());
 
-        if (!genTypeBuilders.containsKey(packageName)) {
+        if (!this.genTypeBuilders.containsKey(packageName)) {
             final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
             builders.put(genTypeName, newType);
-            genTypeBuilders.put(packageName, builders);
+            this.genTypeBuilders.put(packageName, builders);
         } else {
-            final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
+            final Map<String, GeneratedTypeBuilder> builders = this.genTypeBuilders.get(packageName);
             if (!builders.containsKey(genTypeName)) {
                 builders.put(genTypeName, newType);
             }
@@ -1970,7 +1970,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         if (genTOBuilder != null) {
             final GeneratedTransferObject genTO = genTOBuilder.toInstance();
             constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO, Status.CURRENT);
-            genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
+            this.genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
         }
     }
 
@@ -2044,7 +2044,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         final List<GeneratedTOBuilder> genTOBuilders = new ArrayList<>();
         final String packageName = typeBuilder.getFullyQualifiedName();
         if (typeDef instanceof UnionTypeDefinition) {
-            final List<GeneratedTOBuilder> types = ((TypeProviderImpl) typeProvider)
+            final List<GeneratedTOBuilder> types = ((TypeProviderImpl) this.typeProvider)
                     .provideGeneratedTOBuildersForUnionTypeDef(packageName, ((UnionTypeDefinition) typeDef),
                             classNameFromLeaf, leaf);
             genTOBuilders.addAll(types);
@@ -2065,7 +2065,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             resultTOBuilder.addToStringProperty(genPropBuilder);
 
         } else if (typeDef instanceof BitsTypeDefinition) {
-            genTOBuilders.add((((TypeProviderImpl) typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
+            genTOBuilders.add((((TypeProviderImpl) this.typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
                     packageName, typeDef, classNameFromLeaf, parentModule.getName()));
         }
         if (!genTOBuilders.isEmpty()) {
@@ -2135,7 +2135,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         }
         sb.append(NEW_LINE);
 
-        if (verboseClassComments) {
+        if (this.verboseClassComments) {
             sb.append("<pre>");
             sb.append(NEW_LINE);
             sb.append(encodeAngleBrackets(YangTemplate.generateYangSnipet(schemaNodes)));
@@ -2156,8 +2156,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
             sb.append(NEW_LINE);
         }
 
-        if (verboseClassComments) {
-            final Module module = findParentModule(schemaContext, schemaNode);
+        if (this.verboseClassComments) {
+            final Module module = findParentModule(this.schemaContext, schemaNode);
             final StringBuilder linkToBuilderClass = new StringBuilder();
             final String[] namespace = Iterables.toArray(BSDOT_SPLITTER.split(fullyQualifiedName), String.class);
             final String className = namespace[namespace.length - 1];
@@ -2224,7 +2224,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             sb.append(NEW_LINE);
         }
 
-        if (verboseClassComments) {
+        if (this.verboseClassComments) {
             sb.append("<p>");
             sb.append("This class represents the following YANG schema fragment defined in module <b>");
             sb.append(module.getName());
@@ -2240,7 +2240,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
     }
 
     private GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path) {
-        for (final ModuleContext ctx : genCtx.values()) {
+        for (final ModuleContext ctx : this.genCtx.values()) {
             final GeneratedTypeBuilder result = ctx.getChildNode(path);
             if (result != null) {
                 return result;
@@ -2250,7 +2250,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
     }
 
     private GeneratedTypeBuilder findGroupingByPath(final SchemaPath path) {
-        for (final ModuleContext ctx : genCtx.values()) {
+        for (final ModuleContext ctx : this.genCtx.values()) {
             final GeneratedTypeBuilder result = ctx.getGrouping(path);
             if (result != null) {
                 return result;
@@ -2260,7 +2260,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
     }
 
     private GeneratedTypeBuilder findCaseByPath(final SchemaPath path) {
-        for (final ModuleContext ctx : genCtx.values()) {
+        for (final ModuleContext ctx : this.genCtx.values()) {
             final GeneratedTypeBuilder result = ctx.getCase(path);
             if (result != null) {
                 return result;
@@ -2270,7 +2270,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
     }
 
     public Map<Module, ModuleContext> getModuleContexts() {
-        return genCtx;
+        return this.genCtx;
     }
 
     @VisibleForTesting
index 397557611591c7132ce56f4ab5b342f750716b5a..4c91e037f2f93576efc4745ad2cfd30dbd861817 100644 (file)
@@ -149,11 +149,11 @@ public final class TypeProviderImpl implements TypeProvider {
         Preconditions.checkArgument(refTypePath != null,
                 "Path reference of Enumeration Type Definition cannot be NULL!");
         Preconditions.checkArgument(refType != null, "Reference to Enumeration Type cannot be NULL!");
-        referencedTypes.put(refTypePath, refType);
+        this.referencedTypes.put(refTypePath, refType);
     }
 
     public Map<Module, Set<Type>> getAdditionalTypes() {
-        return additionalTypes;
+        return this.additionalTypes;
     }
 
     /**
@@ -194,7 +194,7 @@ public final class TypeProviderImpl implements TypeProvider {
         Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
         Preconditions.checkArgument(typeDefinition.getQName() != null,
                 "Type Definition cannot have non specified QName (QName cannot be NULL!)");
-        String typedefName = typeDefinition.getQName().getLocalName();
+        final String typedefName = typeDefinition.getQName().getLocalName();
         Preconditions.checkArgument(typedefName != null, "Type Definitions Local Name cannot be NULL!");
 
         // Deal with base types
@@ -226,12 +226,12 @@ public final class TypeProviderImpl implements TypeProvider {
 
         Type returnType = javaTypeForExtendedType(typeDefinition);
         if (r != null && returnType instanceof GeneratedTransferObject) {
-            GeneratedTransferObject gto = (GeneratedTransferObject) returnType;
-            Module module = findParentModule(schemaContext, parentNode);
-            String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
-            String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, typeDefinition.getPath());
-            String genTOName = BindingMapping.getClassName(typedefName);
-            String name = packageName + "." + genTOName;
+            final GeneratedTransferObject gto = (GeneratedTransferObject) returnType;
+            final Module module = findParentModule(this.schemaContext, parentNode);
+            final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
+            final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, typeDefinition.getPath());
+            final String genTOName = BindingMapping.getClassName(typedefName);
+            final String name = packageName + "." + genTOName;
             if (!(returnType.getFullyQualifiedName().equals(name))) {
                 returnType = shadedTOWithRestrictions(gto, r);
             }
@@ -240,14 +240,14 @@ public final class TypeProviderImpl implements TypeProvider {
     }
 
     private static GeneratedTransferObject shadedTOWithRestrictions(final GeneratedTransferObject gto, final Restrictions r) {
-        GeneratedTOBuilder gtob = new GeneratedTOBuilderImpl(gto.getPackageName(), gto.getName());
-        GeneratedTransferObject parent = gto.getSuperType();
+        final GeneratedTOBuilder gtob = new GeneratedTOBuilderImpl(gto.getPackageName(), gto.getName());
+        final GeneratedTransferObject parent = gto.getSuperType();
         if (parent != null) {
             gtob.setExtendsType(parent);
         }
         gtob.setRestrictions(r);
-        for (GeneratedProperty gp : gto.getProperties()) {
-            GeneratedPropertyBuilder gpb = gtob.addProperty(gp.getName());
+        for (final GeneratedProperty gp : gto.getProperties()) {
+            final GeneratedPropertyBuilder gpb = gtob.addProperty(gp.getName());
             gpb.setValue(gp.getValue());
             gpb.setReadOnly(gp.isReadOnly());
             gpb.setAccessModifier(gp.getAccessModifier());
@@ -273,7 +273,7 @@ public final class TypeProviderImpl implements TypeProvider {
         while (iterator.hasNext() && !isAugmenting) {
             final QName next = iterator.next();
             if (current == null) {
-                dataChildByName = schemaContext.getDataChildByName(next);
+                dataChildByName = this.schemaContext.getDataChildByName(next);
             } else {
                 dataChildByName = current.getDataChildByName(next);
             }
@@ -291,12 +291,12 @@ public final class TypeProviderImpl implements TypeProvider {
         }
         /////
 
-        Module parentModule = getParentModule(parentNode);
+        final Module parentModule = getParentModule(parentNode);
         if (!leafRefStrippedXPath.isAbsolute()) {
-            leafRefValueNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, parentModule,
+            leafRefValueNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(this.schemaContext, parentModule,
                     parentNode, leafRefStrippedXPath);
         } else {
-            leafRefValueNode = SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule, leafRefStrippedXPath);
+            leafRefValueNode = SchemaContextUtil.findDataSchemaNode(this.schemaContext, parentModule, leafRefStrippedXPath);
         }
         return (leafRefValueNode != null) ? leafRefValueNode.equals(parentNode) : false;
     }
@@ -343,10 +343,10 @@ public final class TypeProviderImpl implements TypeProvider {
                 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypeDef;
                 returnType = provideTypeForEnum(enumTypeDef, typedefName, typeDefinition);
             } else {
-                final Module module = findParentModule(schemaContext, typeDefinition);
-                Restrictions r = BindingGeneratorUtil.getRestrictions(typeDefinition);
+                final Module module = findParentModule(this.schemaContext, typeDefinition);
+                final Restrictions r = BindingGeneratorUtil.getRestrictions(typeDefinition);
                 if (module != null) {
-                    final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(module.getName());
+                    final Map<Date, Map<String, Type>> modulesByDate = this.genTypeDefsContextMap.get(module.getName());
                     final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
                     if (genTOs != null) {
                         returnType = genTOs.get(typedefName);
@@ -377,11 +377,11 @@ public final class TypeProviderImpl implements TypeProvider {
      *         <code>idref</code>
      */
     private Type provideTypeForIdentityref(final IdentityrefTypeDefinition idref) {
-        QName baseIdQName = idref.getIdentity().getQName();
-        Module module = schemaContext.findModuleByNamespaceAndRevision(baseIdQName.getNamespace(),
+        final QName baseIdQName = idref.getIdentity().getQName();
+        final Module module = this.schemaContext.findModuleByNamespaceAndRevision(baseIdQName.getNamespace(),
                 baseIdQName.getRevision());
         IdentitySchemaNode identity = null;
-        for (IdentitySchemaNode id : module.getIdentities()) {
+        for (final IdentitySchemaNode id : module.getIdentities()) {
             if (id.getQName().equals(baseIdQName)) {
                 identity = id;
             }
@@ -392,8 +392,8 @@ public final class TypeProviderImpl implements TypeProvider {
         final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, identity.getPath());
         final String genTypeName = BindingMapping.getClassName(identity.getQName());
 
-        Type baseType = Types.typeForClass(Class.class);
-        Type paramType = Types.wildcardTypeFor(packageName, genTypeName);
+        final Type baseType = Types.typeForClass(Class.class);
+        final Type paramType = Types.wildcardTypeFor(packageName, genTypeName);
         return Types.parameterizedTypeFor(baseType, paramType);
     }
 
@@ -423,10 +423,10 @@ public final class TypeProviderImpl implements TypeProvider {
 
         final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
         if (!(baseTypeDef instanceof LeafrefTypeDefinition) && !(baseTypeDef instanceof IdentityrefTypeDefinition)) {
-            final Module module = findParentModule(schemaContext, parentNode);
+            final Module module = findParentModule(this.schemaContext, parentNode);
 
             if (module != null) {
-                final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(module.getName());
+                final Map<Date, Map<String, Type>> modulesByDate = this.genTypeDefsContextMap.get(module.getName());
                 final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
                 if (genTOs != null) {
                     return genTOs.get(typeDefinition.getQName().getLocalName());
@@ -486,22 +486,22 @@ public final class TypeProviderImpl implements TypeProvider {
 
         if (strXPath != null) {
             if (strXPath.indexOf('[') == -1) {
-                final Module module = findParentModule(schemaContext, parentNode);
+                final Module module = findParentModule(this.schemaContext, parentNode);
                 Preconditions.checkArgument(module != null, "Failed to find module for parent %s", parentNode);
 
                 final SchemaNode dataNode;
                 if (xpath.isAbsolute()) {
-                    dataNode = findDataSchemaNode(schemaContext, module, xpath);
+                    dataNode = findDataSchemaNode(this.schemaContext, module, xpath);
                 } else {
-                    dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
+                    dataNode = findDataSchemaNodeForRelativeXPath(this.schemaContext, module, parentNode, xpath);
                 }
                 Preconditions.checkArgument(dataNode != null, "Failed to find leafref target: %s in module %s (%s)",
                         strXPath, this.getParentModule(parentNode).getName(), parentNode.getQName().getModule());
 
                 if (leafContainsEnumDefinition(dataNode)) {
-                    returnType = referencedTypes.get(dataNode.getPath());
+                    returnType = this.referencedTypes.get(dataNode.getPath());
                 } else if (leafListContainsEnumDefinition(dataNode)) {
-                    returnType = Types.listTypeFor(referencedTypes.get(dataNode.getPath()));
+                    returnType = Types.listTypeFor(this.referencedTypes.get(dataNode.getPath()));
                 } else {
                     returnType = resolveTypeFromDataSchemaNode(dataNode);
                 }
@@ -590,7 +590,7 @@ public final class TypeProviderImpl implements TypeProvider {
 
         final String enumerationName = BindingMapping.getClassName(enumName);
 
-        Module module = findParentModule(schemaContext, parentNode);
+        final Module module = findParentModule(this.schemaContext, parentNode);
         final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
 
         final EnumerationBuilderImpl enumBuilder = new EnumerationBuilderImpl(basePackageName, enumerationName);
@@ -681,23 +681,23 @@ public final class TypeProviderImpl implements TypeProvider {
      *
      */
     private void resolveTypeDefsFromContext() {
-        final Set<Module> modules = schemaContext.getModules();
+        final Set<Module> modules = this.schemaContext.getModules();
         Preconditions.checkArgument(modules != null, "Set of Modules cannot be NULL!");
         final Module[] modulesArray = new Module[modules.size()];
         int i = 0;
-        for (Module modul : modules) {
+        for (final Module modul : modules) {
             modulesArray[i++] = modul;
         }
         final List<Module> modulesSortedByDependency = org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort
                 .sort(modulesArray);
 
         for (final Module module : modulesSortedByDependency) {
-            Map<Date, Map<String, Type>> dateTypeMap = genTypeDefsContextMap.get(module.getName());
+            Map<Date, Map<String, Type>> dateTypeMap = this.genTypeDefsContextMap.get(module.getName());
             if (dateTypeMap == null) {
                 dateTypeMap = new HashMap<>();
             }
             dateTypeMap.put(module.getRevision(), Collections.<String, Type>emptyMap());
-            genTypeDefsContextMap.put(module.getName(), dateTypeMap);
+            this.genTypeDefsContextMap.put(module.getName(), dateTypeMap);
         }
 
         for (final Module module : modulesSortedByDependency) {
@@ -731,7 +731,8 @@ public final class TypeProviderImpl implements TypeProvider {
      *         <code>modulName</code> or <code>typedef</code> or Q name of
      *         <code>typedef</code> equals <code>null</code>
      */
-    private Type typedefToGeneratedType(final String basePackageName, final Module module, final TypeDefinition<?> typedef) {
+    private Type typedefToGeneratedType(final String basePackageName, final Module module,
+            final TypeDefinition<?> typedef) {
         final String moduleName = module.getName();
         final Date moduleRevision = module.getRevision();
         if ((basePackageName != null) && (moduleName != null) && (typedef != null) && (typedef.getQName() != null)) {
@@ -751,18 +752,18 @@ public final class TypeProviderImpl implements TypeProvider {
                     makeSerializable((GeneratedTOBuilderImpl) genTOBuilder);
                     returnType = genTOBuilder.toInstance();
                     // union builder
-                    GeneratedTOBuilder unionBuilder = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
+                    final GeneratedTOBuilder unionBuilder = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
                             genTOBuilder.getName() + "Builder");
                     unionBuilder.setIsUnionBuilder(true);
-                    MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
+                    final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
                     method.setReturnType(returnType);
                     method.addParameter(Types.STRING, "defaultValue");
                     method.setAccessModifier(AccessModifier.PUBLIC);
                     method.setStatic(true);
-                    Set<Type> types = additionalTypes.get(module);
+                    Set<Type> types = this.additionalTypes.get(module);
                     if (types == null) {
                         types = Sets.<Type> newHashSet(unionBuilder.toInstance());
-                        additionalTypes.put(module, types);
+                        this.additionalTypes.put(module, types);
                     } else {
                         types.add(unionBuilder.toInstance());
                     }
@@ -784,7 +785,7 @@ public final class TypeProviderImpl implements TypeProvider {
                     returnType = wrapJavaTypeIntoTO(basePackageName, typedef, javaType, module.getName());
                 }
                 if (returnType != null) {
-                    final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(moduleName);
+                    final Map<Date, Map<String, Type>> modulesByDate = this.genTypeDefsContextMap.get(moduleName);
                     Map<String, Type> typeMap = modulesByDate.get(moduleRevision);
                     if (typeMap != null) {
                         if (typeMap.isEmpty()) {
@@ -856,7 +857,7 @@ public final class TypeProviderImpl implements TypeProvider {
         Preconditions.checkState(!builders.isEmpty(), "No GeneratedTOBuilder objects generated from union %s", typedef);
 
         final GeneratedTOBuilder resultTOBuilder = builders.remove(0);
-        for (GeneratedTOBuilder genTOBuilder : builders) {
+        for (final GeneratedTOBuilder genTOBuilder : builders) {
             resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
         }
 
@@ -884,14 +885,15 @@ public final class TypeProviderImpl implements TypeProvider {
      *             <li>if Qname of <code>typedef</code> is null</li>
      *             </ul>
      */
-    public List<GeneratedTOBuilder> provideGeneratedTOBuildersForUnionTypeDef(final String basePackageName, final UnionTypeDefinition typedef, final String typeDefName, final SchemaNode parentNode) {
+    public List<GeneratedTOBuilder> provideGeneratedTOBuildersForUnionTypeDef(final String basePackageName,
+            final UnionTypeDefinition typedef, final String typeDefName, final SchemaNode parentNode) {
         Preconditions.checkNotNull(basePackageName, "Base Package Name cannot be NULL!");
         Preconditions.checkNotNull(typedef, "Type Definition cannot be NULL!");
         Preconditions.checkNotNull(typedef.getQName(), "Type definition QName cannot be NULL!");
 
         final List<GeneratedTOBuilder> generatedTOBuilders = new ArrayList<>();
         final List<TypeDefinition<?>> unionTypes = typedef.getTypes();
-        final Module module = findParentModule(schemaContext, parentNode);
+        final Module module = findParentModule(this.schemaContext, parentNode);
 
         final GeneratedTOBuilderImpl unionGenTOBuilder;
         if (typeDefName != null && !typeDefName.isEmpty()) {
@@ -941,8 +943,7 @@ public final class TypeProviderImpl implements TypeProvider {
      *
      * In this case the new generated TO is created for union subtype (recursive
      * call of method
-     * {@link #provideGeneratedTOBuildersForUnionTypeDef(String, UnionTypeDefinition,
-     * String, SchemaNode)}
+     * {@link #provideGeneratedTOBuildersForUnionTypeDef(String, UnionTypeDefinition, String, SchemaNode)}
      * provideGeneratedTOBuilderForUnionTypeDef} and in parent TO builder
      * <code>parentUnionGenTOBuilder</code> is created property which type is
      * equal to new generated TO.
@@ -958,7 +959,8 @@ public final class TypeProviderImpl implements TypeProvider {
      *         bigger one due to recursive call of
      *         <code>provideGeneratedTOBuildersForUnionTypeDef</code> method.
      */
-    private List<GeneratedTOBuilder> resolveUnionSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder, final UnionTypeDefinition unionSubtype, final String basePackageName, final SchemaNode parentNode) {
+    private List<GeneratedTOBuilder> resolveUnionSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
+            final UnionTypeDefinition unionSubtype, final String basePackageName, final SchemaNode parentNode) {
         final String newTOBuilderName = provideAvailableNameForGenTOBuilder(parentUnionGenTOBuilder.getName());
         final List<GeneratedTOBuilder> subUnionGenTOBUilders = provideGeneratedTOBuildersForUnionTypeDef(
                 basePackageName, unionSubtype, newTOBuilderName, parentNode);
@@ -991,7 +993,8 @@ public final class TypeProviderImpl implements TypeProvider {
      *            parent Schema Node for Extended Subtype
      *
      */
-    private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder, final TypeDefinition<?> unionSubtype, final List<String> regularExpressions, final SchemaNode parentNode) {
+    private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
+            final TypeDefinition<?> unionSubtype, final List<String> regularExpressions, final SchemaNode parentNode) {
         final String unionTypeName = unionSubtype.getQName().getLocalName();
         final Type genTO = findGenTO(unionTypeName, unionSubtype);
         if (genTO != null) {
@@ -999,11 +1002,26 @@ public final class TypeProviderImpl implements TypeProvider {
         } else {
             final TypeDefinition<?> baseType = baseTypeDefForExtendedType(unionSubtype);
             if (unionTypeName.equals(baseType.getQName().getLocalName())) {
-                final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(baseType,
-                        parentNode);
+                final Type javaType =
+                        BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(baseType, parentNode);
                 if (javaType != null) {
                     updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, unionTypeName);
                 }
+            } else if (baseType instanceof LeafrefTypeDefinition) {
+                final Type javaType = javaTypeForSchemaDefinitionType(baseType, parentNode);
+                boolean typeExist = false;
+                for (final GeneratedPropertyBuilder generatedPropertyBuilder : parentUnionGenTOBuilder
+                        .getProperties()) {
+                    final Type origType = ((GeneratedPropertyBuilderImpl) generatedPropertyBuilder).getReturnType();
+                    if (origType != null && javaType != null && javaType == origType) {
+                        typeExist = true;
+                        break;
+                    }
+                }
+                if (!typeExist && javaType != null) {
+                    updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, new StringBuilder(javaType.getName())
+                            .append(parentUnionGenTOBuilder.getName()).append("Value").toString());
+                }
             }
             if (baseType instanceof StringTypeDefinition) {
                 regularExpressions.addAll(resolveRegExpressionsFromTypedef(unionSubtype));
@@ -1021,9 +1039,9 @@ public final class TypeProviderImpl implements TypeProvider {
      *         <code>null</code> it it doesn't exist
      */
     private Type findGenTO(final String searchedTypeName, final SchemaNode parentNode) {
-        final Module typeModule = findParentModule(schemaContext, parentNode);
+        final Module typeModule = findParentModule(this.schemaContext, parentNode);
         if (typeModule != null && typeModule.getName() != null) {
-            final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(typeModule.getName());
+            final Map<Date, Map<String, Type>> modulesByDate = this.genTypeDefsContextMap.get(typeModule.getName());
             final Map<String, Type> genTOs = modulesByDate.get(typeModule.getRevision());
             if (genTOs != null) {
                 return genTOs.get(searchedTypeName);
@@ -1045,10 +1063,10 @@ public final class TypeProviderImpl implements TypeProvider {
      */
     private void storeGenTO(final TypeDefinition<?> newTypeDef, final GeneratedTOBuilder genTOBuilder, final SchemaNode parentNode) {
         if (!(newTypeDef instanceof UnionTypeDefinition)) {
-            final Module parentModule = findParentModule(schemaContext, parentNode);
+            final Module parentModule = findParentModule(this.schemaContext, parentNode);
             if (parentModule != null && parentModule.getName() != null) {
-                Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(parentModule.getName());
-                Map<String, Type> genTOsMap = modulesByDate.get(parentModule.getRevision());
+                final Map<Date, Map<String, Type>> modulesByDate = this.genTypeDefsContextMap.get(parentModule.getName());
+                final Map<String, Type> genTOsMap = modulesByDate.get(parentModule.getRevision());
                 genTOsMap.put(newTypeDef.getQName().getLocalName(), genTOBuilder.toInstance());
             }
         }
@@ -1056,7 +1074,7 @@ public final class TypeProviderImpl implements TypeProvider {
 
     /**
      * Adds a new property with the name <code>propertyName</code> and with type
-     * <code>type</code> to <code>unonGenTransObject</code>.
+     * <code>type</code> to <code>unionGenTransObject</code>.
      *
      * @param unionGenTransObject
      *            generated TO to which should be property added
@@ -1135,7 +1153,7 @@ public final class TypeProviderImpl implements TypeProvider {
         Preconditions.checkArgument(basePackageName != null, "Base Package Name cannot be NULL!");
 
         if (typeDef instanceof BitsTypeDefinition) {
-            BitsTypeDefinition bitsTypeDefinition = (BitsTypeDefinition) typeDef;
+            final BitsTypeDefinition bitsTypeDefinition = (BitsTypeDefinition) typeDef;
 
             final String typeName = BindingMapping.getClassName(typeDefName);
             final GeneratedTOBuilderImpl genTOBuilder = new GeneratedTOBuilderImpl(basePackageName, typeName);
@@ -1150,7 +1168,7 @@ public final class TypeProviderImpl implements TypeProvider {
             final List<Bit> bitList = bitsTypeDefinition.getBits();
             GeneratedPropertyBuilder genPropertyBuilder;
             for (final Bit bit : bitList) {
-                String name = bit.getName();
+                final String name = bit.getName();
                 genPropertyBuilder = genTOBuilder.addProperty(BindingMapping.getPropertyName(name));
                 genPropertyBuilder.setReadOnly(true);
                 genPropertyBuilder.setReturnType(BaseYangTypes.BOOLEAN_TYPE);
@@ -1188,7 +1206,7 @@ public final class TypeProviderImpl implements TypeProvider {
         }
 
         final List<String> regExps = new ArrayList<>(patternConstraints.size());
-        for (PatternConstraint patternConstraint : patternConstraints) {
+        for (final PatternConstraint patternConstraint : patternConstraints) {
             final String regEx = patternConstraint.getRegularExpression();
             final String modifiedRegEx = StringEscapeUtils.escapeJava(regEx);
             regExps.add(modifiedRegEx);
@@ -1268,7 +1286,7 @@ public final class TypeProviderImpl implements TypeProvider {
         genTOBuilder.setSchemaPath(typedef.getPath().getPathFromRoot());
         genTOBuilder.setModuleName(moduleName);
         genTOBuilder.setTypedef(true);
-        Restrictions r = BindingGeneratorUtil.getRestrictions(typedef);
+        final Restrictions r = BindingGeneratorUtil.getRestrictions(typedef);
         genTOBuilder.setRestrictions(r);
         if (typedef.getStatus() == Status.DEPRECATED) {
             genTOBuilder.addAnnotation("", "Deprecated");
@@ -1280,14 +1298,14 @@ public final class TypeProviderImpl implements TypeProvider {
 
         Map<Date, Map<String, Type>> modulesByDate = null;
         Map<String, Type> typeMap = null;
-        final Module parentModule = findParentModule(schemaContext, innerExtendedType);
+        final Module parentModule = findParentModule(this.schemaContext, innerExtendedType);
         if (parentModule != null) {
-            modulesByDate = genTypeDefsContextMap.get(parentModule.getName());
+            modulesByDate = this.genTypeDefsContextMap.get(parentModule.getName());
             typeMap = modulesByDate.get(parentModule.getRevision());
         }
 
         if (typeMap != null) {
-            Type type = typeMap.get(innerTypeDef);
+            final Type type = typeMap.get(innerTypeDef);
             if (type instanceof GeneratedTransferObject) {
                 genTOBuilder.setExtendsType((GeneratedTransferObject) type);
             }
@@ -1307,7 +1325,7 @@ public final class TypeProviderImpl implements TypeProvider {
      */
     private static void makeSerializable(final GeneratedTOBuilderImpl gto) {
         gto.addImplementsType(Types.typeForClass(Serializable.class));
-        GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
+        final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
         prop.setValue(Long.toString(BindingGeneratorUtil.computeDefaultSUID(gto)));
         gto.setSUID(prop);
     }
@@ -1328,10 +1346,10 @@ public final class TypeProviderImpl implements TypeProvider {
      */
     private static List<TypeDefinition<?>> sortTypeDefinitionAccordingDepth(
             final Collection<TypeDefinition<?>> unsortedTypeDefinitions) {
-        List<TypeDefinition<?>> sortedTypeDefinition = new ArrayList<>();
+        final List<TypeDefinition<?>> sortedTypeDefinition = new ArrayList<>();
 
-        Map<Integer, List<TypeDefinition<?>>> typeDefinitionsDepths = new TreeMap<>();
-        for (TypeDefinition<?> unsortedTypeDefinition : unsortedTypeDefinitions) {
+        final Map<Integer, List<TypeDefinition<?>>> typeDefinitionsDepths = new TreeMap<>();
+        for (final TypeDefinition<?> unsortedTypeDefinition : unsortedTypeDefinitions) {
             final int depth = getTypeDefinitionDepth(unsortedTypeDefinition);
             List<TypeDefinition<?>> typeDefinitionsConcreteDepth = typeDefinitionsDepths.get(depth);
             if (typeDefinitionsConcreteDepth == null) {
@@ -1342,7 +1360,7 @@ public final class TypeProviderImpl implements TypeProvider {
         }
 
         // SortedMap guarantees order corresponding to keys in ascending order
-        for (List<TypeDefinition<?>> v : typeDefinitionsDepths.values()) {
+        for (final List<TypeDefinition<?>> v : typeDefinitionsDepths.values()) {
             sortedTypeDefinition.addAll(v);
         }
 
@@ -1363,7 +1381,7 @@ public final class TypeProviderImpl implements TypeProvider {
         if (typeDefinition == null) {
             return 1;
         }
-        TypeDefinition<?> baseType = typeDefinition.getBaseType();
+        final TypeDefinition<?> baseType = typeDefinition.getBaseType();
         if (baseType == null) {
             return 1;
         }
@@ -1372,10 +1390,10 @@ public final class TypeProviderImpl implements TypeProvider {
         if (baseType.getBaseType() != null) {
             depth = depth + getTypeDefinitionDepth(baseType);
         } else if (baseType instanceof UnionTypeDefinition) {
-            List<TypeDefinition<?>> childTypeDefinitions = ((UnionTypeDefinition) baseType).getTypes();
+            final List<TypeDefinition<?>> childTypeDefinitions = ((UnionTypeDefinition) baseType).getTypes();
             int maxChildDepth = 0;
             int childDepth = 1;
-            for (TypeDefinition<?> childTypeDefinition : childTypeDefinitions) {
+            for (final TypeDefinition<?> childTypeDefinition : childTypeDefinitions) {
                 childDepth = childDepth + getTypeDefinitionDepth(childTypeDefinition);
                 if (childDepth > maxChildDepth) {
                     maxChildDepth = childDepth;
@@ -1396,7 +1414,7 @@ public final class TypeProviderImpl implements TypeProvider {
      * @return string with the number suffix incremented by one (or 1 is added)
      */
     private static String provideAvailableNameForGenTOBuilder(final String name) {
-        Matcher mtch = NUMBERS_PATTERN.matcher(name);
+        final Matcher mtch = NUMBERS_PATTERN.matcher(name);
         if (mtch.find()) {
             final int newSuffix = Integer.valueOf(name.substring(mtch.start())) + 1;
             return name.substring(0, mtch.start()) + newSuffix;
@@ -1408,7 +1426,7 @@ public final class TypeProviderImpl implements TypeProvider {
     public static void addUnitsToGenTO(final GeneratedTOBuilder to, final String units) {
         if (!Strings.isNullOrEmpty(units)) {
             to.addConstant(Types.STRING, "_UNITS", "\"" + units + "\"");
-            GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("UNITS");
+            final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("UNITS");
             prop.setReturnType(Types.STRING);
             to.addToStringProperty(prop);
         }
@@ -1420,30 +1438,30 @@ public final class TypeProviderImpl implements TypeProvider {
     }
 
     public String getTypeDefaultConstruction(final LeafSchemaNode node, final String defaultValue) {
-        TypeDefinition<?> type = CompatUtils.compatLeafType(node);
-        QName typeQName = type.getQName();
-        TypeDefinition<?> base = baseTypeDefForExtendedType(type);
+        final TypeDefinition<?> type = CompatUtils.compatLeafType(node);
+        final QName typeQName = type.getQName();
+        final TypeDefinition<?> base = baseTypeDefForExtendedType(type);
         Preconditions.checkNotNull(type, "Cannot provide default construction for null type of %s", node);
         Preconditions.checkNotNull(defaultValue, "Cannot provide default construction for null default statement of %s",
                 node);
 
-        StringBuilder sb = new StringBuilder();
+        final StringBuilder sb = new StringBuilder();
         String result = null;
         if (base instanceof BinaryTypeDefinition) {
             result = binaryToDef(defaultValue);
         } else if (base instanceof BitsTypeDefinition) {
             String parentName;
             String className;
-            Module parent = getParentModule(node);
-            Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
+            final Module parent = getParentModule(node);
+            final Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
             path.next();
             if (!(path.hasNext())) {
                 parentName = BindingMapping.getClassName(parent.getName()) + "Data";
-                String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
+                final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
                 className = basePackageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
             } else {
-                String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
-                String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
+                final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
+                final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
                 parentName = BindingMapping.getClassName(parent.getName());
                 className = packageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
             }
@@ -1455,20 +1473,20 @@ public final class TypeProviderImpl implements TypeProvider {
         } else if (base instanceof EmptyTypeDefinition) {
             result = typeToDef(Boolean.class, defaultValue);
         } else if (base instanceof EnumTypeDefinition) {
-            char[] defValArray = defaultValue.toCharArray();
-            char first = Character.toUpperCase(defaultValue.charAt(0));
+            final char[] defValArray = defaultValue.toCharArray();
+            final char first = Character.toUpperCase(defaultValue.charAt(0));
             defValArray[0] = first;
-            String newDefVal = new String(defValArray);
+            final String newDefVal = new String(defValArray);
             String className;
             if (type.getBaseType() != null) {
-                Module m = getParentModule(type);
-                String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
-                String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
+                final Module m = getParentModule(type);
+                final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
+                final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
                 className = packageName + "." + BindingMapping.getClassName(typeQName);
             } else {
-                Module parentModule = getParentModule(node);
-                String basePackageName = BindingMapping.getRootPackageName(parentModule.getQNameModule());
-                String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, node.getPath());
+                final Module parentModule = getParentModule(node);
+                final String basePackageName = BindingMapping.getRootPackageName(parentModule.getQNameModule());
+                final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, node.getPath());
                 className = packageName + "." + BindingMapping.getClassName(node.getQName());
             }
             result = className + "." + newDefVal;
@@ -1505,10 +1523,10 @@ public final class TypeProviderImpl implements TypeProvider {
 
         if (type.getBaseType() != null && !(base instanceof LeafrefTypeDefinition)
                 && !(base instanceof EnumTypeDefinition) && !(base instanceof UnionTypeDefinition)) {
-            Module m = getParentModule(type);
-            String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
-            String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
-            String className = packageName + "." + BindingMapping.getClassName(typeQName);
+            final Module m = getParentModule(type);
+            final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
+            final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
+            final String className = packageName + "." + BindingMapping.getClassName(typeQName);
             sb.insert(0, "new " + className + "(");
             sb.insert(sb.length(), ')');
         }
@@ -1521,9 +1539,9 @@ public final class TypeProviderImpl implements TypeProvider {
     }
 
     private static String binaryToDef(final String defaultValue) {
-        StringBuilder sb = new StringBuilder();
-        BaseEncoding en = BaseEncoding.base64();
-        byte[] encoded = en.decode(defaultValue);
+        final StringBuilder sb = new StringBuilder();
+        final BaseEncoding en = BaseEncoding.base64();
+        final byte[] encoded = en.decode(defaultValue);
         sb.append("new byte[] {");
         for (int i = 0; i < encoded.length; i++) {
             sb.append(encoded[i]);
@@ -1538,9 +1556,9 @@ public final class TypeProviderImpl implements TypeProvider {
     private static final Comparator<Bit> BIT_NAME_COMPARATOR = (o1, o2) -> o1.getName().compareTo(o2.getName());
 
     private static String bitsToDef(final BitsTypeDefinition type, final String className, final String defaultValue, final boolean isExt) {
-        List<Bit> bits = new ArrayList<>(type.getBits());
+        final List<Bit> bits = new ArrayList<>(type.getBits());
         Collections.sort(bits, BIT_NAME_COMPARATOR);
-        StringBuilder sb = new StringBuilder();
+        final StringBuilder sb = new StringBuilder();
         if (!isExt) {
             sb.append("new ");
             sb.append(className);
@@ -1563,10 +1581,10 @@ public final class TypeProviderImpl implements TypeProvider {
     }
 
     private Module getParentModule(final SchemaNode node) {
-        QName qname = node.getPath().getPathFromRoot().iterator().next();
-        URI namespace = qname.getNamespace();
-        Date revision = qname.getRevision();
-        return schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
+        final QName qname = node.getPath().getPathFromRoot().iterator().next();
+        final URI namespace = qname.getNamespace();
+        final Date revision = qname.getRevision();
+        return this.schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
     }
 
     private String leafrefToDef(final LeafSchemaNode parentNode, final LeafrefTypeDefinition leafrefType, final String defaultValue) {
@@ -1579,15 +1597,15 @@ public final class TypeProviderImpl implements TypeProvider {
 
         if (strXPath != null) {
             if (strXPath.indexOf('[') == -1) {
-                final Module module = findParentModule(schemaContext, parentNode);
+                final Module module = findParentModule(this.schemaContext, parentNode);
                 if (module != null) {
                     final SchemaNode dataNode;
                     if (xpath.isAbsolute()) {
-                        dataNode = findDataSchemaNode(schemaContext, module, xpath);
+                        dataNode = findDataSchemaNode(this.schemaContext, module, xpath);
                     } else {
-                        dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
+                        dataNode = findDataSchemaNodeForRelativeXPath(this.schemaContext, module, parentNode, xpath);
                     }
-                    String result = getTypeDefaultConstruction((LeafSchemaNode) dataNode, defaultValue);
+                    final String result = getTypeDefaultConstruction((LeafSchemaNode) dataNode, defaultValue);
                     return result;
                 }
             } else {
@@ -1604,18 +1622,18 @@ public final class TypeProviderImpl implements TypeProvider {
         String className;
 
         if (type.getBaseType() != null) {
-            QName typeQName = type.getQName();
+            final QName typeQName = type.getQName();
             Module module = null;
-            Set<Module> modules = schemaContext.findModuleByNamespace(typeQName.getNamespace());
+            final Set<Module> modules = this.schemaContext.findModuleByNamespace(typeQName.getNamespace());
             if (modules.size() > 1) {
-                for (Module m : modules) {
+                for (final Module m : modules) {
                     if (m.getRevision().equals(typeQName.getRevision())) {
                         module = m;
                         break;
                     }
                 }
                 if (module == null) {
-                    List<Module> modulesList = new ArrayList<>(modules);
+                    final List<Module> modulesList = new ArrayList<>(modules);
                     Collections.sort(modulesList, (o1, o2) -> o1.getRevision().compareTo(o2.getRevision()));
                     module = modulesList.get(0);
                 }
@@ -1623,24 +1641,24 @@ public final class TypeProviderImpl implements TypeProvider {
                 module = modules.iterator().next();
             }
 
-            String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
+            final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
             className = basePackageName + "." + BindingMapping.getClassName(typeQName);
         } else {
-            Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
-            QName first = path.next();
+            final Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
+            final QName first = path.next();
             if (!(path.hasNext())) {
-                URI namespace = first.getNamespace();
-                Date revision = first.getRevision();
-                Module parent = schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
+                final URI namespace = first.getNamespace();
+                final Date revision = first.getRevision();
+                final Module parent = this.schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
                 parentName = BindingMapping.getClassName((parent).getName()) + "Data";
-                String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
+                final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
                 className = basePackageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
             } else {
-                URI namespace = first.getNamespace();
-                Date revision = first.getRevision();
-                Module parentModule = schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
-                String basePackageName = BindingMapping.getRootPackageName(parentModule.getQNameModule());
-                String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, UNION_PATH);
+                final URI namespace = first.getNamespace();
+                final Date revision = first.getRevision();
+                final Module parentModule = this.schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
+                final String basePackageName = BindingMapping.getRootPackageName(parentModule.getQNameModule());
+                final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, UNION_PATH);
                 className = packageName + "." + BindingMapping.getClassName(node.getQName());
             }
         }
@@ -1648,7 +1666,7 @@ public final class TypeProviderImpl implements TypeProvider {
     }
 
     private static String union(final String className, final String defaultValue, final LeafSchemaNode node) {
-        StringBuilder sb = new StringBuilder();
+        final StringBuilder sb = new StringBuilder();
         sb.append("new ");
         sb.append(className);
         sb.append("(\"");
index 848a1b33fc6f2921cbc0315140a3583e00fe540b..ddcd86ab52b404d417a800cb22253aec50df9c79 100644 (file)
@@ -7,22 +7,27 @@
  */
 package org.opendaylight.yangtools.sal.binding.generator.impl;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 
 import java.io.File;
 import java.util.List;
 import org.junit.Test;
+import org.opendaylight.yangtools.binding.generator.util.Types;
 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
 import org.opendaylight.yangtools.sal.binding.model.api.Type;
+import org.opendaylight.yangtools.sal.binding.yang.types.TestUtils;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
 public class UnionTypeDefTest {
 
     @Test
     public void unionTypeResolvingTest() throws Exception {
-        File abstractTopology = new File(getClass().getResource("/union-test-models/abstract-topology.yang").toURI());
-        File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
+        final File abstractTopology = new File(getClass().getResource("/union-test-models/abstract-topology.yang").toURI());
+        final File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
         final SchemaContext context = TestUtils.parseYangSources(abstractTopology, ietfInetTypes);
 
         assertNotNull("context is null", context);
@@ -34,4 +39,30 @@ public class UnionTypeDefTest {
 
         // TODO: implement test
     }
+
+    @Test
+    public void unionTypedefLeafrefTest() throws Exception {
+        final File yang = new File(getClass().getResource("/leafref_typedef_union/bug8449.yang").toURI());
+        final SchemaContext schemaContext = TestUtils.parseYangSources(yang);
+        assertNotNull(schemaContext);
+        final List<Type> generateTypes = new BindingGeneratorImpl(false).generateTypes(schemaContext);
+        assertNotNull(generateTypes);
+        for (final Type type : generateTypes) {
+            if (type.getName().equals("Cont")) {
+                final GeneratedType gt = ((GeneratedType) type);
+                assertNotNull(gt);
+                final GeneratedType refType = gt.getEnclosedTypes().iterator().next();
+                for (final GeneratedProperty generatedProperty : refType.getProperties()) {
+                    switch (generatedProperty.getName()) {
+                        case "stringRefValue":
+                            assertEquals(Types.STRING, generatedProperty.getReturnType());
+                            break;
+                        case "value":
+                            assertEquals(Types.CHAR_ARRAY, generatedProperty.getReturnType());
+                            break;
+                    }
+                }
+            }
+        }
+    }
 }
diff --git a/binding/mdsal-binding-generator-impl/src/test/resources/leafref_typedef_union/bug8449.yang b/binding/mdsal-binding-generator-impl/src/test/resources/leafref_typedef_union/bug8449.yang
new file mode 100644 (file)
index 0000000..f4f685a
--- /dev/null
@@ -0,0 +1,49 @@
+module bug8449 {
+    yang-version 1;
+    namespace "bug8449";
+    prefix "tst";
+
+    revision "2017-16-05" {
+    }
+
+    typedef name1-ref {
+        type leafref {
+            path "/tst:top/tst:name1";
+        }
+    }
+
+    typedef name2-ref {
+        type leafref {
+            path "/tst:top/tst:name2";
+        }
+    }
+
+    typedef name3-ref {
+        type string;
+    }
+
+    container top {
+        leaf name1 {
+            type string;
+        }
+        leaf name2 {
+            type string;
+        }
+    }
+
+    container test {
+        leaf ref {
+            type name3-ref;
+        }
+    }
+
+    container cont {
+        leaf ref {
+            type union {
+                type name1-ref;
+                type name2-ref;
+            }
+            mandatory true;
+        }
+    }
+}
\ No newline at end of file
index 58451825d885a180750029eb9295823f13bb8cc7..73feb7b66e6325e21bb0140c08ff523e3961246f 100644 (file)
@@ -43,7 +43,7 @@ abstract class AbstractTypeMemberBuilder<T extends TypeMemberBuilder<T>> impleme
         return builder;
     }
 
-    protected Type getReturnType() {
+    public Type getReturnType() {
         return returnType;
     }
 
@@ -141,13 +141,13 @@ abstract class AbstractTypeMemberBuilder<T extends TypeMemberBuilder<T>> impleme
         if (getClass() != obj.getClass()) {
             return false;
         }
-        AbstractTypeMemberBuilder<?> other = (AbstractTypeMemberBuilder<?>) obj;
+        final AbstractTypeMemberBuilder<?> other = (AbstractTypeMemberBuilder<?>) obj;
         return Objects.equals(getName(), other.getName()) && Objects.equals(getReturnType(), other.getReturnType());
     }
 
     @Override
     public String toString() {
-        StringBuilder builder = new StringBuilder();
+        final StringBuilder builder = new StringBuilder();
         builder.append("GeneratedPropertyImpl [name=");
         builder.append(getName());
         builder.append(", annotations=");
diff --git a/binding/mdsal-binding-test-model/src/main/yang/bug8449.yang b/binding/mdsal-binding-test-model/src/main/yang/bug8449.yang
new file mode 100644 (file)
index 0000000..ac254df
--- /dev/null
@@ -0,0 +1,59 @@
+module bug8449 {
+    yang-version 1;
+    namespace "bug8449";
+    prefix "tst";
+
+    revision "2017-16-05" {
+    }
+
+    typedef name1-ref {
+        type leafref {
+            path "/tst:top/tst:name1";
+        }
+    }
+
+    typedef name2-ref {
+        type leafref {
+            path "/tst:top/tst:name2";
+        }
+    }
+
+    typedef int32-ref {
+        type leafref {
+            path "/tst:top-int/tst:leaf-int32";
+        }
+    }
+
+    container top-int {
+        leaf leaf-int32 {
+            type uint32;
+        }
+    }
+
+    container top {
+        leaf name1 {
+            type string;
+        }
+        leaf name2 {
+            type string;
+        }
+    }
+
+    container cont-int32 {
+        leaf ref-union-int32 {
+            type union {
+                type int32-ref;
+            }
+        }
+    }
+
+    container cont {
+        leaf ref {
+            type union {
+                type name1-ref;
+                type name2-ref;
+            }
+            mandatory true;
+        }
+    }
+}