[ONOS-6691][ONOS-6693] fix for when type-def name is same as identity-ref with small or caps latter combilation.

Change-Id: I0945cb6e6a222f64b2cf71a9c415c64c93b37ba6
diff --git a/compiler/base/datamodel/pom.xml b/compiler/base/datamodel/pom.xml
index 93e0923..244c3d4 100644
--- a/compiler/base/datamodel/pom.xml
+++ b/compiler/base/datamodel/pom.xml
@@ -17,6 +17,13 @@
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-yang-compiler-utils</artifactId>
+            <version>2.02-SNAPSHOT</version>
+        </dependency>
+    </dependencies>
 
     <parent>
         <groupId>org.onosproject</groupId>
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/ConflictResolveNode.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/ConflictResolveNode.java
new file mode 100644
index 0000000..d80f7c4
--- /dev/null
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/ConflictResolveNode.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yang.compiler.datamodel;
+
+/**
+ * Abstraction to unify the YANG identity and typedef name collision detection
+ * functionality.
+ */
+public interface ConflictResolveNode extends YangSchemaNode {
+
+    /**
+     * Returns the suffix for the given conflicted type.
+     *
+     * @return suffix
+     */
+    String getSuffix();
+
+    /**
+     * Returns the flag to identify is there YANG identity and typedef name
+     * conflict.
+     *
+     * @return true, if identity and typedef name conflicts otherwise false
+     */
+    boolean isNameConflict();
+
+    /**
+     * Sets the conflict flag for requested identity or typedef node.
+     */
+    void setConflictFlag();
+}
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/RpcNotificationContainer.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/RpcNotificationContainer.java
index 2e3d67f..2c8da5d 100644
--- a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/RpcNotificationContainer.java
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/RpcNotificationContainer.java
@@ -16,6 +16,8 @@
 
 package org.onosproject.yang.compiler.datamodel;
 
+import org.onosproject.yang.compiler.datamodel.exceptions.DataModelException;
+
 import java.util.List;
 
 /**
@@ -72,4 +74,15 @@
      * @return list of notification nodes
      */
     List<YangNode> getNotificationNodes();
+
+    /**
+     * Adds typedef or identity nodes in module/sub-module.
+     *
+     * @param name node name
+     * @param node YANG node
+     * @throws DataModelException if name conflict occurs for typedef or
+     *                            identity node.
+     */
+    void addToIdentityTypedefMap(String name, YangNode node)
+            throws DataModelException;
 }
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangIdentity.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangIdentity.java
index 24bfeab..608c1ea 100644
--- a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangIdentity.java
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangIdentity.java
@@ -23,6 +23,8 @@
 import java.util.LinkedList;
 import java.util.List;
 
+import static org.onosproject.yang.compiler.utils.UtilConstants.IDENTITY;
+
 /*-
  * Reference RFC 6020.
  *
@@ -51,7 +53,8 @@
  */
 public abstract class YangIdentity
         extends YangNode
-        implements YangCommonInfo, Parsable, Serializable, YangTranslatorOperatorNode {
+        implements YangCommonInfo, Parsable, Serializable,
+        YangTranslatorOperatorNode, ConflictResolveNode {
 
     private static final long serialVersionUID = 806201691L;
 
@@ -73,6 +76,11 @@
      */
     private List<YangIdentity> extendList;
 
+    /**
+     * Flag to distinguish name conflict between typedef and identity.
+     */
+    private boolean nameConflict;
+
     //Creates a identity type of node.
     public YangIdentity() {
         super(YangNodeType.IDENTITY_NODE, null);
@@ -190,4 +198,19 @@
     public void addToExtendList(YangIdentity identity) {
         extendList.add(identity);
     }
+
+    @Override
+    public String getSuffix() {
+        return IDENTITY;
+    }
+
+    @Override
+    public boolean isNameConflict() {
+        return nameConflict;
+    }
+
+    @Override
+    public void setConflictFlag() {
+        nameConflict = true;
+    }
 }
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangModule.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangModule.java
index 59ec805..17ac363 100644
--- a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangModule.java
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangModule.java
@@ -40,6 +40,7 @@
 import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
 import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.linkInterFileReferences;
 import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.resolveLinkingForResolutionList;
+import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.updateMap;
 import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.validateUniqueInList;
 import static org.onosproject.yang.compiler.datamodel.utils.YangConstructType.MODULE_DATA;
 
@@ -278,6 +279,12 @@
     private final List<YangAugment> augments;
 
     /**
+     * Map of typedef or identity with the list of corresponding schema
+     * node to find the name conflict.
+     */
+    private final Map<String, LinkedList<YangNode>> identityTypedefMap;
+
+    /**
      * List of unique identifiers List.
      */
     private List<YangUniqueHolder> uniqueHolderList;
@@ -324,6 +331,7 @@
         augments = new LinkedList<>();
         uniqueHolderList = new LinkedList<>();
         usesAugmentList = new LinkedList<>();
+        identityTypedefMap = new HashMap<>();
     }
 
     @Override
@@ -949,6 +957,14 @@
         isModuleForDeviation = moduleForDeviation;
     }
 
+    @Override
+    public void addToIdentityTypedefMap(String name, YangNode node)
+            throws DataModelException {
+        if (node instanceof ConflictResolveNode) {
+            updateMap(name, node, identityTypedefMap);
+        }
+    }
+
     /**
      * Sets true if rpc is present.
      *
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangNode.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangNode.java
index 8130e01..a37d262 100644
--- a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangNode.java
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangNode.java
@@ -17,6 +17,7 @@
 
 import org.onosproject.yang.compiler.datamodel.exceptions.DataModelException;
 import org.onosproject.yang.compiler.datamodel.utils.Parsable;
+import org.onosproject.yang.compiler.utils.io.impl.YangIoUtils;
 import org.onosproject.yang.model.DataNode;
 import org.onosproject.yang.model.SchemaContext;
 import org.onosproject.yang.model.SchemaId;
@@ -317,6 +318,10 @@
 
         if (newChild.getParent() == null) {
             newChild.setParent(this);
+            if (this instanceof RpcNotificationContainer) {
+                ((RpcNotificationContainer) this).addToIdentityTypedefMap(
+                        YangIoUtils.getCamelCase(newChild.getName(), null), newChild);
+            }
         } else if (newChild.getParent() != this) {
             throw new DataModelException("Node is already part of a tree " +
                                                  getName() + " in " +
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangSubModule.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangSubModule.java
index 61ad3f4..42173f4 100644
--- a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangSubModule.java
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangSubModule.java
@@ -20,6 +20,7 @@
 import org.onosproject.yang.compiler.datamodel.utils.YangConstructType;
 
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -41,6 +42,7 @@
 import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
 import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.linkInterFileReferences;
 import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.resolveLinkingForResolutionList;
+import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.updateMap;
 import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.validateUniqueInList;
 import static org.onosproject.yang.compiler.datamodel.utils.YangConstructType.SUB_MODULE_DATA;
 
@@ -304,6 +306,12 @@
     private boolean isRpcPresent;
 
     /**
+     * Map of typedef or identity with the list of corresponding schema
+     * node to find the name conflict.
+     */
+    private final Map<String, LinkedList<YangNode>> identityTypedefMap;
+
+    /**
      * Creates a sub module node.
      */
     public YangSubModule() {
@@ -327,6 +335,7 @@
         augments = new LinkedList<>();
         uniqueHolderList = new LinkedList<>();
         usesAugmentList = new LinkedList<>();
+        identityTypedefMap = new LinkedHashMap<>();
     }
 
     @Override
@@ -955,6 +964,14 @@
         }
     }
 
+    @Override
+    public void addToIdentityTypedefMap(String name, YangNode node)
+            throws DataModelException {
+        if (node instanceof ConflictResolveNode) {
+            updateMap(name, node, identityTypedefMap);
+        }
+    }
+
     /**
      * Returns true if rpc is present.
      *
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangTypeDef.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangTypeDef.java
index ed7f3c2..401a982 100644
--- a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangTypeDef.java
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangTypeDef.java
@@ -18,6 +18,7 @@
 import org.onosproject.yang.compiler.datamodel.exceptions.DataModelException;
 import org.onosproject.yang.compiler.datamodel.utils.Parsable;
 import org.onosproject.yang.compiler.datamodel.utils.YangConstructType;
+import org.onosproject.yang.compiler.utils.UtilConstants;
 
 import java.util.LinkedList;
 import java.util.List;
@@ -67,7 +68,7 @@
 public abstract class YangTypeDef
         extends YangNode
         implements YangCommonInfo, Parsable, YangTypeHolder, CollisionDetector,
-        YangTranslatorOperatorNode, YangUnits, YangDefault {
+        YangTranslatorOperatorNode, YangUnits, YangDefault, ConflictResolveNode {
 
     private static final long serialVersionUID = 806201615L;
 
@@ -104,6 +105,11 @@
     private final List<YangType<?>> typeList;
 
     /**
+     * Flag to distinguish name conflict between typedef and identity.
+     */
+    private boolean nameConflict = false;
+
+    /**
      * Creates a typedef node.
      */
     public YangTypeDef() {
@@ -314,4 +320,19 @@
                                          TYPEDEF, getFileName()));
         }
     }
+
+    @Override
+    public String getSuffix() {
+        return UtilConstants.TYPEDEF;
+    }
+
+    @Override
+    public boolean isNameConflict() {
+        return nameConflict;
+    }
+
+    @Override
+    public void setConflictFlag() {
+        nameConflict = true;
+    }
 }
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java
index 390ca67..e1caeb0 100644
--- a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java
@@ -17,6 +17,7 @@
 package org.onosproject.yang.compiler.datamodel.utils;
 
 import org.onosproject.yang.compiler.datamodel.CollisionDetector;
+import org.onosproject.yang.compiler.datamodel.ConflictResolveNode;
 import org.onosproject.yang.compiler.datamodel.DefaultYangNamespace;
 import org.onosproject.yang.compiler.datamodel.SchemaDataNode;
 import org.onosproject.yang.compiler.datamodel.YangAtomicPath;
@@ -116,6 +117,8 @@
 public final class DataModelUtils {
     public static final String TRUE = "true";
     public static final String FALSE = "false";
+    public static final String TYPEDEF = "Typedef";
+    public static final String IDENTITY = "Identity";
     private static final String SLASH = File.separator;
     public static final String FMT_NOT_EXIST =
             "Requested %s is not child in %s.";
@@ -130,6 +133,9 @@
             " node in unique reference path is invalid";
     private static final String E_DATATREE = "Internal datamodel error: Datam" +
             "odel tree is not correct";
+    public static final String E_NOT_ALLOWED =
+            "%s with the name %s in file %s at line %s is not allowed. Please" +
+                    " avoid the %s extension in the name.";
 
     /**
      * Creates a new data model tree utility.
@@ -1462,4 +1468,52 @@
         }
         return null;
     }
+
+    /**
+     * Updates the identity and typedef info in module/sub-module map.
+     *
+     * @param name java name of the node
+     * @param node YANG node
+     * @param map  typedef and identity map
+     * @throws DataModelException exception when two identity or
+     *                            typedef present with same name
+     */
+    public static void updateMap(String name, YangNode node, Map<String,
+            LinkedList<YangNode>> map) throws DataModelException {
+        YangNode oldNode;
+        if (!map.containsKey(name)) {
+            LinkedList<YangNode> list = new LinkedList<>();
+            list.push(node);
+            map.put(name, list);
+        } else {
+            LinkedList<YangNode> value = map.get(name);
+            oldNode = value.get(0);
+            if (value.size() >= 2) {
+                if (!node.getNodeType().equals(oldNode.getNodeType())) {
+                    oldNode = value.get(1);
+                }
+                throw new DataModelException(composeErrorMsg(node, oldNode));
+            }
+            if (oldNode.getNodeType().equals(node.getNodeType())) {
+                throw new DataModelException(composeErrorMsg(node, oldNode));
+            }
+            ((ConflictResolveNode) oldNode).setConflictFlag();
+            ((ConflictResolveNode) node).setConflictFlag();
+            value.push(node);
+        }
+    }
+
+    /**
+     * Composes the error message for given new and existing YANG node
+     * name conflict.
+     *
+     * @param node    newly added YANG node
+     * @param oldNode existing YANG node.
+     */
+    private static String composeErrorMsg(YangNode node, YangNode oldNode) {
+        return "Node with name " + node.getName() + " in file " +
+                node.getFileName() + " at line " + node.getLineNumber() +
+                " is already present " + "in file " + oldNode.getFileName() +
+                " at " + "line " + oldNode.getLineNumber() + "" + ".";
+    }
 }
diff --git a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/YangJavaModelUtils.java b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/YangJavaModelUtils.java
index a8e27d0..c99e0ff 100644
--- a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/YangJavaModelUtils.java
+++ b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/YangJavaModelUtils.java
@@ -38,6 +38,7 @@
 import org.onosproject.yang.compiler.datamodel.YangTranslatorOperatorNode;
 import org.onosproject.yang.compiler.datamodel.YangType;
 import org.onosproject.yang.compiler.datamodel.YangTypeHolder;
+import org.onosproject.yang.compiler.datamodel.ConflictResolveNode;
 import org.onosproject.yang.compiler.datamodel.exceptions.DataModelException;
 import org.onosproject.yang.compiler.translator.exception.TranslatorException;
 import org.onosproject.yang.compiler.translator.tojava.javamodel.JavaLeafInfoContainer;
@@ -137,6 +138,10 @@
         } else {
             javaGenName = ((YangNode) info).getName();
         }
+        if (info instanceof ConflictResolveNode && ((ConflictResolveNode)
+                info).isNameConflict()) {
+            javaGenName = javaGenName + ((ConflictResolveNode) info).getSuffix();
+        }
         info.getJavaFileInfo().setJavaName(getCamelCase(
                 javaGenName, config.getConflictResolver()));
     }
@@ -190,9 +195,13 @@
     private static void updatePackageInfo(JavaCodeGeneratorInfo info,
                                           YangPluginConfig config,
                                           String pkg) {
-
+        String name = ((YangNode) info).getName();
         JavaFileInfoTranslator translator = info.getJavaFileInfo();
-        translator.setJavaName(getCamelCase(((YangNode) info).getName(),
+        if (info instanceof ConflictResolveNode && ((ConflictResolveNode)
+                info).isNameConflict()) {
+            name = name + ((ConflictResolveNode) info).getSuffix();
+        }
+        translator.setJavaName(getCamelCase(name,
                                             config.getConflictResolver()));
         translator.setPackage(pkg);
         updateCommonPackageInfo(translator, info, config);
diff --git a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/javamodel/AttributesJavaDataType.java b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/javamodel/AttributesJavaDataType.java
index 04a5321..3d1004c 100644
--- a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/javamodel/AttributesJavaDataType.java
+++ b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/javamodel/AttributesJavaDataType.java
@@ -23,12 +23,15 @@
 import org.onosproject.yang.compiler.datamodel.YangLeafRef;
 import org.onosproject.yang.compiler.datamodel.YangNode;
 import org.onosproject.yang.compiler.datamodel.YangType;
+import org.onosproject.yang.compiler.datamodel.YangTypeDef;
 import org.onosproject.yang.compiler.datamodel.YangUnion;
+import org.onosproject.yang.compiler.datamodel.ConflictResolveNode;
 import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.YangDataTypes;
 import org.onosproject.yang.compiler.translator.exception.TranslatorException;
 import org.onosproject.yang.compiler.translator.tojava.JavaCodeGeneratorInfo;
 import org.onosproject.yang.compiler.translator.tojava.JavaFileInfoContainer;
 import org.onosproject.yang.compiler.translator.tojava.JavaFileInfoTranslator;
+import org.onosproject.yang.compiler.utils.UtilConstants;
 import org.onosproject.yang.compiler.utils.io.YangToJavaNamingConflictUtil;
 
 import java.util.Stack;
@@ -181,8 +184,7 @@
                 case INSTANCE_IDENTIFIER:
                     return STRING_DATA_TYPE;
                 case DERIVED:
-                    return getCapitalCase(
-                            getCamelCase(yangType.getDataTypeName(), pluginConfig));
+                    return getDerivedImplClass(yangType, pluginConfig);
                 default:
                     throw new TranslatorException("given data type is not supported ." +
                                                           yangType.getDataTypeName() + " in " +
@@ -216,8 +218,7 @@
                 case INSTANCE_IDENTIFIER:
                     return STRING_DATA_TYPE;
                 case DERIVED:
-                    return getCapitalCase(
-                            getCamelCase(yangType.getDataTypeName(), pluginConfig));
+                    return getDerivedImplClass(yangType, pluginConfig);
                 default:
                     return null;
             }
@@ -225,6 +226,25 @@
     }
 
     /**
+     * Returns the derived type import class.
+     *
+     * @param yangType     yang type
+     * @param pluginConfig YANG to java naming conflict util
+     * @return import class
+     */
+    private static String getDerivedImplClass(
+            YangType<?> yangType, YangToJavaNamingConflictUtil pluginConfig) {
+        String name = yangType.getDataTypeName();
+        YangDerivedInfo derivedInfo = (YangDerivedInfo) yangType
+                .getDataTypeExtendedInfo();
+        YangTypeDef typeDef = derivedInfo.getReferredTypeDef();
+        if (typeDef.isNameConflict()) {
+            name = name + UtilConstants.TYPEDEF;
+        }
+        return getCapitalCase(getCamelCase(name, pluginConfig));
+    }
+
+    /**
      * Returns java import package.
      *
      * @param yangType         YANG type
@@ -451,11 +471,11 @@
      * Update the referred data model nodes java file info, this will be called,
      * when the linked node is yet to translate. Then resolve until the parent hierarchy.
      *
-     * @param yangNode         node whose java info needs to be updated
-     * @param conflictResolver yang plugin config
+     * @param yangNode node whose java info needs to be updated
+     * @param conf     yang plugin config
      */
     public static void updateJavaFileInfo(YangNode yangNode,
-                                          YangToJavaNamingConflictUtil conflictResolver) {
+                                          YangToJavaNamingConflictUtil conf) {
         Stack<YangNode> nodesToUpdatePackage = new Stack<>();
 
         /*
@@ -478,12 +498,12 @@
             if (yangNode instanceof YangJavaModuleTranslator) {
                 YangJavaModuleTranslator module = (YangJavaModuleTranslator) yangNode;
                 pkg = getRootPackage(module.getVersion(), module.getModuleName(),
-                                     module.getRevision(), conflictResolver);
+                                     module.getRevision(), conf);
             } else if (yangNode instanceof YangJavaSubModuleTranslator) {
                 YangJavaSubModuleTranslator submodule = (YangJavaSubModuleTranslator) yangNode;
                 pkg = getRootPackage(submodule.getVersion(),
                                      submodule.getModuleName(),
-                                     submodule.getRevision(), conflictResolver);
+                                     submodule.getRevision(), conf);
             } else {
                 throw new TranslatorException("Invalid root node of data model tree " +
                                                       yangNode.getName() + " in " +
@@ -491,15 +511,7 @@
                                                       yangNode.getCharPosition()
                                                       + " in " + yangNode.getFileName());
             }
-
-            ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo()
-                    .setJavaName(getCamelCase(yangNode.getName(), conflictResolver));
-            ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo()
-                    .setPackage(pkg);
-            ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo()
-                    .setPackageFilePath(getPackageDirPathFromJavaJPackage(
-                            ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo()
-                                    .getPackage()));
+            setJavaCodeGenInfo(yangNode, conf, pkg);
         }
 
         /*
@@ -508,18 +520,32 @@
          */
         while (nodesToUpdatePackage.size() != 0) {
             yangNode = nodesToUpdatePackage.pop();
-            ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo()
-                    .setJavaName(getCamelCase(yangNode.getName(), conflictResolver));
-            ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo()
-                    .setPackage(getCurNodePackage(yangNode));
-            ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo()
-                    .setPackageFilePath(getPackageDirPathFromJavaJPackage(
-                            ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo()
-                                    .getPackage()));
+            setJavaCodeGenInfo(yangNode, conf, getCurNodePackage(yangNode));
         }
     }
 
     /**
+     * Sets the java code generator info for given YANG node.
+     *
+     * @param node YANG node
+     * @param conf conflict util
+     * @param pkg  current node package
+     */
+    private static void setJavaCodeGenInfo(
+            YangNode node, YangToJavaNamingConflictUtil conf, String pkg) {
+        String name = node.getName();
+        JavaFileInfoTranslator info = ((JavaCodeGeneratorInfo) node)
+                .getJavaFileInfo();
+        if (node instanceof ConflictResolveNode && (
+                (ConflictResolveNode) node).isNameConflict()) {
+            name = name + ((ConflictResolveNode) node).getSuffix();
+        }
+        info.setJavaName(getCamelCase(name, conf));
+        info.setPackage(pkg);
+        info.setPackageFilePath(getPackageDirPathFromJavaJPackage(pkg));
+    }
+
+    /**
      * Returns the referred type from leaf/leaf-list.
      *
      * @param type current type in leaf
@@ -559,7 +585,11 @@
             return OBJECT_STRING;
         }
         YangIdentity identity = ir.getReferredIdentity();
-        return getCapitalCase(getCamelCase(identity.getName(), cnfg));
+        String name = identity.getName();
+        if (identity.isNameConflict()) {
+            name = name + UtilConstants.IDENTITY;
+        }
+        return getCapitalCase(getCamelCase(name, cnfg));
     }
 
     /**
diff --git a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/javamodel/YangJavaIdentityTranslator.java b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/javamodel/YangJavaIdentityTranslator.java
index 265107e..3b7fa5e 100644
--- a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/javamodel/YangJavaIdentityTranslator.java
+++ b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/javamodel/YangJavaIdentityTranslator.java
@@ -48,6 +48,7 @@
 import static org.onosproject.yang.compiler.translator.tojava.utils.TranslatorUtils.getErrorMsg;
 import static org.onosproject.yang.compiler.utils.UtilConstants.CLOSE_CURLY_BRACKET;
 import static org.onosproject.yang.compiler.utils.UtilConstants.EMPTY_STRING;
+import static org.onosproject.yang.compiler.utils.UtilConstants.IDENTITY;
 import static org.onosproject.yang.compiler.utils.UtilConstants.JAVA_FILE_EXTENSION;
 import static org.onosproject.yang.compiler.utils.UtilConstants.PERIOD;
 import static org.onosproject.yang.compiler.utils.io.impl.FileSystemUtil.closeFile;
@@ -190,10 +191,16 @@
      */
     private List<String> getImportOfDerId(List<YangIdentity> idList,
                                           List<String> imports, String className) {
+        String derClassName;
         if (idList != null) {
             for (YangIdentity id : idList) {
-                String derClassName = getCapitalCase(
-                        getCamelCase(id.getName(), null));
+                if (id.isNameConflict()) {
+                    derClassName = getCapitalCase(
+                            getCamelCase(id.getName() + IDENTITY, null));
+                } else {
+                    derClassName = getCapitalCase(
+                            getCamelCase(id.getName(), null));
+                }
                 JavaFileInfoTranslator info = ((
                         YangJavaIdentityTranslator) id).getJavaFileInfo();
                 String derPkg = info.getPackage();
diff --git a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/MethodsGenerator.java b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/MethodsGenerator.java
index c7630fa..ed46a96 100644
--- a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/MethodsGenerator.java
+++ b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/MethodsGenerator.java
@@ -94,6 +94,7 @@
 import static org.onosproject.yang.compiler.translator.tojava.utils.StringGenerator.multiAttrMethodSignature;
 import static org.onosproject.yang.compiler.translator.tojava.utils.StringGenerator.signatureClose;
 import static org.onosproject.yang.compiler.translator.tojava.utils.StringGenerator.valueAssign;
+import static org.onosproject.yang.compiler.translator.tojava.utils.TranslatorUtils.getIdentityRefName;
 import static org.onosproject.yang.compiler.utils.UtilConstants.ADD;
 import static org.onosproject.yang.compiler.utils.UtilConstants.ADD_AUGMENTATION;
 import static org.onosproject.yang.compiler.utils.UtilConstants.ADD_STRING;
@@ -140,6 +141,7 @@
 import static org.onosproject.yang.compiler.utils.UtilConstants.GET_METHOD_PREFIX;
 import static org.onosproject.yang.compiler.utils.UtilConstants.HASH;
 import static org.onosproject.yang.compiler.utils.UtilConstants.HASH_CODE_STRING;
+import static org.onosproject.yang.compiler.utils.UtilConstants.IDENTITY;
 import static org.onosproject.yang.compiler.utils.UtilConstants.IF;
 import static org.onosproject.yang.compiler.utils.UtilConstants.IMPLEMENTS;
 import static org.onosproject.yang.compiler.utils.UtilConstants.INSTANCE_OF;
@@ -1728,8 +1730,9 @@
                 YangIdentityRef ir = (YangIdentityRef) type
                         .getDataTypeExtendedInfo();
                 YangIdentity identity = ir.getReferredIdentity();
-                String idName = getCamelCase(identity.getName(), null);
-                return getCapitalCase(idName) + PERIOD + idName + TO_CAPS +
+                String idName = getCamelCase(getIdentityRefName(type), null);
+                return getCapitalCase(idName) + PERIOD +
+                        getCamelCase(identity.getName(), null) + TO_CAPS +
                         STRING_DATA_TYPE + OPEN_CLOSE_BRACKET_STRING;
             case ENUMERATION:
             case INSTANCE_IDENTIFIER:
@@ -1883,7 +1886,11 @@
             for (YangIdentity id : idList) {
                 cond = getTwoParaEqualsString(FROM_STRING_PARAM_NAME,
                                               getQuotedString(id.getName()));
-                name = getCamelCase(id.getName(), null);
+                if (id.isNameConflict()) {
+                    name = getCamelCase(id.getName() + IDENTITY, null);
+                } else {
+                    name = getCamelCase(id.getName(), null);
+                }
                 caps = getCapitalCase(name);
                 returnVal = caps + PERIOD + CLASS;
                 builder.append(getElseIfConditionBegin(
@@ -1947,7 +1954,7 @@
      *
      * @param name         class name
      * @param modifierType modifier type
-     * @param params       parameters for constrcutor
+     * @param params       parameters for constructor
      * @param space        indentation for constructor
      * @return parameterisied constructor method string
      */
diff --git a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/StringGenerator.java b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/StringGenerator.java
index 421899a..ff8c231 100644
--- a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/StringGenerator.java
+++ b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/StringGenerator.java
@@ -18,8 +18,6 @@
 
 import org.onosproject.yang.compiler.datamodel.YangCompilerAnnotation;
 import org.onosproject.yang.compiler.datamodel.YangDataStructure;
-import org.onosproject.yang.compiler.datamodel.YangIdentity;
-import org.onosproject.yang.compiler.datamodel.YangIdentityRef;
 import org.onosproject.yang.compiler.datamodel.YangType;
 import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.YangDataTypes;
 import org.onosproject.yang.compiler.translator.exception.TranslatorException;
@@ -47,6 +45,7 @@
 import static org.onosproject.yang.compiler.translator.tojava.utils.BracketType.OPEN_CLOSE_BRACKET_WITH_VALUE_AND_RETURN_TYPE;
 import static org.onosproject.yang.compiler.translator.tojava.utils.MethodClassTypes.CLASS_TYPE;
 import static org.onosproject.yang.compiler.translator.tojava.utils.MethodsGenerator.getYangDataStructure;
+import static org.onosproject.yang.compiler.translator.tojava.utils.TranslatorUtils.getIdentityRefName;
 import static org.onosproject.yang.compiler.utils.UtilConstants.ABSTRACT;
 import static org.onosproject.yang.compiler.utils.UtilConstants.ADD;
 import static org.onosproject.yang.compiler.utils.UtilConstants.ADD_STRING;
@@ -752,10 +751,8 @@
                 return targetDataType + PERIOD + OF;
 
             case IDENTITYREF:
-                YangIdentityRef ir = (YangIdentityRef) yangType
-                        .getDataTypeExtendedInfo();
-                YangIdentity identity = ir.getReferredIdentity();
-                return getCapitalCase(getCamelCase(identity.getName(), null))
+                return getCapitalCase(getCamelCase(
+                        getIdentityRefName(yangType), null))
                         + PERIOD + FROM_STRING_METHOD_NAME;
             case DERIVED:
             case UNION:
@@ -1051,7 +1048,6 @@
                     classDef = classDef + COMMA + SPACE;
                 }
             }
-
         }
         return classDef + defCloseString();
     }
diff --git a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/TranslatorUtils.java b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/TranslatorUtils.java
index b1105cf..8e0a149 100644
--- a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/TranslatorUtils.java
+++ b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/TranslatorUtils.java
@@ -17,12 +17,16 @@
 package org.onosproject.yang.compiler.translator.tojava.utils;
 
 import org.onosproject.yang.compiler.datamodel.LocationInfo;
+import org.onosproject.yang.compiler.datamodel.YangIdentity;
+import org.onosproject.yang.compiler.datamodel.YangIdentityRef;
 import org.onosproject.yang.compiler.datamodel.YangNode;
 import org.onosproject.yang.compiler.datamodel.YangSchemaNode;
+import org.onosproject.yang.compiler.datamodel.YangType;
 import org.onosproject.yang.compiler.translator.tojava.JavaCodeGeneratorInfo;
 import org.onosproject.yang.compiler.translator.tojava.TempJavaBeanFragmentFiles;
 import org.onosproject.yang.compiler.translator.tojava.TempJavaCodeFragmentFilesContainer;
 import org.onosproject.yang.compiler.translator.tojava.TempJavaTypeFragmentFiles;
+import org.onosproject.yang.compiler.utils.UtilConstants;
 
 import java.io.IOException;
 
@@ -143,4 +147,21 @@
          * input.
          */
     }
+
+    /**
+     * Returns the identity ref name with name conflict checking.
+     *
+     * @param yangType YANG type
+     * @return identity ref name
+     */
+    static String getIdentityRefName(YangType<?> yangType) {
+        YangIdentityRef ir = (YangIdentityRef) yangType
+                .getDataTypeExtendedInfo();
+        YangIdentity identity = ir.getReferredIdentity();
+        String name = identity.getName();
+        if (identity.isNameConflict()) {
+            name = name + UtilConstants.IDENTITY;
+        }
+        return name;
+    }
 }
diff --git a/compiler/base/translator/src/test/java/org/onosproject/yang/compiler/translator/tojava/javamodel/AttributesJavaDataTypeTest.java b/compiler/base/translator/src/test/java/org/onosproject/yang/compiler/translator/tojava/javamodel/AttributesJavaDataTypeTest.java
index 41c17cf..c9cf85e 100644
--- a/compiler/base/translator/src/test/java/org/onosproject/yang/compiler/translator/tojava/javamodel/AttributesJavaDataTypeTest.java
+++ b/compiler/base/translator/src/test/java/org/onosproject/yang/compiler/translator/tojava/javamodel/AttributesJavaDataTypeTest.java
@@ -168,6 +168,7 @@
     @SuppressWarnings("unchecked")
     private YangType<?> getStubExtendedInfo(YangType<?> type) throws DataModelException {
         YangJavaTypeDefTranslator typedef = new YangJavaTypeDefTranslator();
+        typedef.setName("xyz");
         getStubParent().addChild(typedef);
         YangDerivedInfo<?> derInfo = new YangDerivedInfo<>();
         derInfo.setReferredTypeDef(typedef);
diff --git a/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/UtilConstants.java b/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/UtilConstants.java
index dfc3bf0..ec09187 100644
--- a/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/UtilConstants.java
+++ b/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/UtilConstants.java
@@ -1955,6 +1955,8 @@
     public static final String AUGMENTABLE = "Augmentable";
     public static final String RPC_SERVICE = "RpcService";
 
+    public static final String IDENTITY = "_identity";
+    public static final String TYPEDEF = "_typedef";
     // No instantiation.
     private UtilConstants() {
     }
diff --git a/compiler/plugin/maven/src/test/java/org/onosproject/yang/compiler/plugin/maven/IdentityTypedefSameNameTest.java b/compiler/plugin/maven/src/test/java/org/onosproject/yang/compiler/plugin/maven/IdentityTypedefSameNameTest.java
new file mode 100644
index 0000000..1551f04
--- /dev/null
+++ b/compiler/plugin/maven/src/test/java/org/onosproject/yang/compiler/plugin/maven/IdentityTypedefSameNameTest.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yang.compiler.plugin.maven;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yang.compiler.parser.exceptions.ParserException;
+import org.onosproject.yang.compiler.tool.YangCompilerManager;
+import org.onosproject.yang.compiler.utils.io.YangPluginConfig;
+import org.onosproject.yang.compiler.utils.io.impl.YangIoUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.onosproject.yang.compiler.utils.io.impl.YangFileScanner.getYangFiles;
+
+/**
+ * Unit tests for identity and typedef with same name with different letter
+ * combination translator.
+ */
+public final class IdentityTypedefSameNameTest {
+    @Rule
+    public ExpectedException thrown = ExpectedException.none();
+    private static final String DIR = "target/IdentityTypedefTestGenFile/";
+    private static final String COMP = System.getProperty("user.dir") + File
+            .separator + DIR;
+    private final YangCompilerManager utilManager = new YangCompilerManager();
+
+    /**
+     * Checks identity and typedef with same name with different letter
+     * combination translation should not result in any exception.
+     *
+     * @throws MojoExecutionException
+     */
+    @Test
+    public void processtest() throws IOException,
+            ParserException, MojoExecutionException {
+        YangIoUtils.deleteDirectory(DIR);
+        String searchDir = "src/test/resources/identityTypedefSameName";
+
+        Set<Path> paths = new HashSet<>();
+        for (String file : getYangFiles(searchDir)) {
+            paths.add(Paths.get(file));
+        }
+
+        utilManager.createYangFileInfoSet(paths);
+        utilManager.parseYangFileInfoSet();
+        utilManager.createYangNodeSet();
+        utilManager.resolveDependenciesUsingLinker();
+
+        YangPluginConfig yangPluginConfig = new YangPluginConfig();
+        yangPluginConfig.setCodeGenDir(DIR);
+        utilManager.translateToJava(yangPluginConfig);
+        YangPluginConfig.compileCode(COMP);
+        YangIoUtils.deleteDirectory(DIR);
+    }
+
+    /**
+     * Checks the negative scenario when an identity already present with
+     * name conflict with typedef and one more identity with same name
+     * with different small and caps letter combination.
+     *
+     * @throws MojoExecutionException
+     */
+    @Test
+    public void processNegativetest() throws IOException,
+            ParserException, MojoExecutionException {
+        thrown.expect(ParserException.class);
+        thrown.expectMessage("Node with name BAsE in file");
+        YangIoUtils.deleteDirectory(DIR);
+        String searchDir = "src/test/resources/identityTypedefSameNameNeg";
+
+        Set<Path> paths = new HashSet<>();
+        for (String file : getYangFiles(searchDir)) {
+            paths.add(Paths.get(file));
+        }
+
+        utilManager.createYangFileInfoSet(paths);
+        utilManager.parseYangFileInfoSet();
+        utilManager.createYangNodeSet();
+        utilManager.resolveDependenciesUsingLinker();
+
+        YangPluginConfig yangPluginConfig = new YangPluginConfig();
+        yangPluginConfig.setCodeGenDir(DIR);
+        utilManager.translateToJava(yangPluginConfig);
+        YangPluginConfig.compileCode(COMP);
+        YangIoUtils.deleteDirectory(DIR);
+    }
+}
diff --git a/compiler/plugin/maven/src/test/resources/identityTypedefSameName/module-a.yang b/compiler/plugin/maven/src/test/resources/identityTypedefSameName/module-a.yang
new file mode 100644
index 0000000..96177d2
--- /dev/null
+++ b/compiler/plugin/maven/src/test/resources/identityTypedefSameName/module-a.yang
@@ -0,0 +1,36 @@
+module module-a {
+
+    namespace "urn:ietf:params:xml:ns:aug:module:a";
+
+    prefix mod-a;
+
+    import module-b {
+      prefix b;
+    }
+
+    import module-c {
+      prefix c;
+    }
+
+    container abc {
+        leaf arg {
+            type "b:base";
+        }
+        leaf arg2 {
+            type identityref {
+                base b:BASE;
+            }
+        }
+        leaf arg3 {
+            type "c:base";
+        }
+        leaf arg4 {
+            type identityref {
+                base c:BASE;
+            }
+        }
+        leaf arg5 {
+            type c:base1;
+        }
+    }
+}
diff --git a/compiler/plugin/maven/src/test/resources/identityTypedefSameName/module-b.yang b/compiler/plugin/maven/src/test/resources/identityTypedefSameName/module-b.yang
new file mode 100644
index 0000000..8f0dff4
--- /dev/null
+++ b/compiler/plugin/maven/src/test/resources/identityTypedefSameName/module-b.yang
@@ -0,0 +1,32 @@
+module module-b {
+
+    namespace "urn:ietf:params:xml:ns:aug:module:b";
+
+    prefix mod-a;
+
+    import module-c {
+        prefix c;
+    }
+
+    identity BASE;
+
+    identity DER1 {
+        base c:BASE;
+    }
+
+    identity DER2 {
+        base c:BASE;
+    }
+
+    typedef base {
+        type identityref {
+            base BASE;
+        }
+    }
+
+    typedef der1 {
+        type identityref {
+            base c:BASE;
+        }
+    }
+}
diff --git a/compiler/plugin/maven/src/test/resources/identityTypedefSameName/module-c.yang b/compiler/plugin/maven/src/test/resources/identityTypedefSameName/module-c.yang
new file mode 100644
index 0000000..250e537
--- /dev/null
+++ b/compiler/plugin/maven/src/test/resources/identityTypedefSameName/module-c.yang
@@ -0,0 +1,30 @@
+module module-c {
+
+    namespace "urn:ietf:params:xml:ns:aug:module:c";
+
+    prefix mod-a;
+
+    identity BASE;
+
+    typedef ip-address {
+       type string;
+    }
+
+    typedef base {
+        type union {
+            type ip-address;
+            type identityref {
+                base BASE;
+            }
+        }
+    }
+
+    typedef base1 {
+        type union {
+            type base;
+            type identityref {
+                base BASE;
+            }
+        }
+    }
+}
diff --git a/compiler/plugin/maven/src/test/resources/identityTypedefSameNameNeg/module-b.yang b/compiler/plugin/maven/src/test/resources/identityTypedefSameNameNeg/module-b.yang
new file mode 100644
index 0000000..fb3f427
--- /dev/null
+++ b/compiler/plugin/maven/src/test/resources/identityTypedefSameNameNeg/module-b.yang
@@ -0,0 +1,16 @@
+module module-b {
+
+    namespace "urn:ietf:params:xml:ns:aug:module:b";
+
+    prefix mod-a;
+
+    identity BASE;
+
+    typedef base {
+        type identityref {
+            base BASE;
+        }
+    }
+    identity BAsE;
+
+}