[ONOS-4993] [ONOS-4956] [ONOS-4935] YANG interfile linker defect fix

Change-Id: I0037c9bd2a73b13a50bb874c7a72c6b2f5050e7e
diff --git a/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangLinkerManager.java b/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangLinkerManager.java
index 17c3238..641af4c 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangLinkerManager.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangLinkerManager.java
@@ -21,6 +21,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
+
 import org.onosproject.yangutils.datamodel.ResolvableType;
 import org.onosproject.yangutils.datamodel.YangNode;
 import org.onosproject.yangutils.datamodel.YangReferenceResolver;
@@ -115,7 +116,8 @@
      * @param yangNodeSet set of YANG files info
      * @throws LinkerException fails to find imported module
      */
-    public void addRefToYangFilesImportList(Set<YangNode> yangNodeSet) throws LinkerException {
+    public void addRefToYangFilesImportList(Set<YangNode> yangNodeSet)
+            throws LinkerException {
         for (YangNode yangNode : yangNodeSet) {
             if (yangNode instanceof YangReferenceResolver) {
                 try {
@@ -137,7 +139,8 @@
      * @param yangNodeSet set of YANG files info
      * @throws LinkerException fails to find included sub-module
      */
-    public void addRefToYangFilesIncludeList(Set<YangNode> yangNodeSet) throws LinkerException {
+    public void addRefToYangFilesIncludeList(Set<YangNode> yangNodeSet)
+            throws LinkerException {
         for (YangNode yangNode : yangNodeSet) {
             if (yangNode instanceof YangReferenceResolver) {
                 try {
diff --git a/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangLinkerUtils.java b/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangLinkerUtils.java
index c9fedd8..fb80076 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangLinkerUtils.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangLinkerUtils.java
@@ -22,12 +22,15 @@
 import java.util.Set;
 import java.util.regex.Pattern;
 
+import org.onosproject.yangutils.datamodel.TraversalType;
 import org.onosproject.yangutils.datamodel.YangAtomicPath;
 import org.onosproject.yangutils.datamodel.YangAugment;
 import org.onosproject.yangutils.datamodel.YangAugmentableNode;
 import org.onosproject.yangutils.datamodel.YangAugmentedInfo;
 import org.onosproject.yangutils.datamodel.YangCase;
 import org.onosproject.yangutils.datamodel.YangChoice;
+import org.onosproject.yangutils.datamodel.YangGrouping;
+import org.onosproject.yangutils.datamodel.YangIdentityRef;
 import org.onosproject.yangutils.datamodel.YangImport;
 import org.onosproject.yangutils.datamodel.YangInclude;
 import org.onosproject.yangutils.datamodel.YangLeaf;
@@ -37,9 +40,21 @@
 import org.onosproject.yangutils.datamodel.YangNode;
 import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
 import org.onosproject.yangutils.datamodel.YangReferenceResolver;
+import org.onosproject.yangutils.datamodel.YangType;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.datamodel.YangUses;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.datamodel.utils.ResolvableStatus;
 import org.onosproject.yangutils.datamodel.utils.YangConstructType;
 import org.onosproject.yangutils.linker.exceptions.LinkerException;
 
+import static org.onosproject.yangutils.datamodel.TraversalType.CHILD;
+import static org.onosproject.yangutils.datamodel.TraversalType.PARENT;
+import static org.onosproject.yangutils.datamodel.TraversalType.ROOT;
+import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING;
+import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo;
+import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
+import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.IDENTITYREF;
 import static org.onosproject.yangutils.utils.UtilConstants.COLON;
 import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
 
@@ -187,7 +202,8 @@
      * @return parent node which can hold data
      * @throws LinkerException a violation of linker rules
      */
-    static YangNode skipInvalidDataNodes(YangNode currentParent, YangLeafRef leafref) throws LinkerException {
+    static YangNode skipInvalidDataNodes(YangNode currentParent, YangLeafRef leafref)
+            throws LinkerException {
         while (currentParent instanceof YangChoice || currentParent instanceof YangCase) {
             if (currentParent.getParent() == null) {
                 throw new LinkerException("YANG file error: The target node, in the leafref path " +
@@ -206,7 +222,7 @@
      * @return valid node identifier
      */
     static YangNodeIdentifier getValidNodeIdentifier(String nodeIdentifierString,
-                                                     YangConstructType yangConstruct) {
+            YangConstructType yangConstruct) {
         String[] tmpData = nodeIdentifierString.split(Pattern.quote(COLON));
         if (tmpData.length == 1) {
             YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier();
@@ -294,4 +310,163 @@
             }
         }
     }
+
+    /**
+     * Add the unresolved data under the root leve grouping to be resolved, since it will be used in interfile uses.
+     *
+     * @param referenceResolver module / sub-module
+     */
+    public static void resolveGroupingInDefinationScope(YangReferenceResolver referenceResolver) {
+        YangNode potentialInterFileGrouping = ((YangNode) referenceResolver).getChild();
+
+        while (potentialInterFileGrouping != null) {
+            if (potentialInterFileGrouping instanceof YangGrouping) {
+                addGroupingResolvableEntitiesToResolutionList((YangGrouping) potentialInterFileGrouping);
+            }
+
+            potentialInterFileGrouping = potentialInterFileGrouping.getNextSibling();
+        }
+    }
+
+    /**
+     * Add the interfile grouping resolvable entities to reesolution list.
+     *
+     * @param interFileGrouping interfile grouping
+     */
+    private static void addGroupingResolvableEntitiesToResolutionList(YangGrouping interFileGrouping) {
+        YangNode curNode = interFileGrouping;
+        TraversalType curTraversal = ROOT;
+        addResolvableLeavesToResolutionList((YangLeavesHolder) curNode);
+        curTraversal = CHILD;
+        curNode = interFileGrouping.getChild();
+        if (curNode == null) {
+            return;
+        }
+        while (curNode != interFileGrouping) {
+            if (curTraversal != PARENT) {
+                if (curNode instanceof YangGrouping || curNode instanceof YangUses) {
+                    if (curNode.getNextSibling() != null) {
+                        curTraversal = SIBILING;
+                        curNode = curNode.getNextSibling();
+                    } else {
+                        curTraversal = PARENT;
+                        curNode = curNode.getParent();
+                    }
+                    continue;
+                }
+
+                if (curNode instanceof YangLeavesHolder) {
+                    addResolvableLeavesToResolutionList((YangLeavesHolder) curNode);
+                } else if (curNode instanceof YangTypeDef) {
+                    List<YangType<?>> typeList = ((YangTypeDef) curNode).getTypeList();
+                    if (!typeList.isEmpty()) {
+                        YangType<?> type = typeList.get(0);
+                        if (type.getDataType() == DERIVED) {
+                            if (type.getResolvableStatus() != ResolvableStatus.RESOLVED) {
+
+                                type.setTypeForInterFileGroupingResolution(true);
+
+                                // Add resolution information to the list
+                                YangResolutionInfoImpl resolutionInfo =
+                                        new YangResolutionInfoImpl<YangType>(type, curNode, type.getLineNumber(),
+                                                type.getCharPosition());
+                                try {
+                                    addResolutionInfo(resolutionInfo);
+                                } catch (DataModelException e) {
+                                    throw new LinkerException("Failed to add type info in grouping to resolution ");
+                                }
+                            }
+                        }
+                    }
+                }
+
+            }
+            if (curTraversal != PARENT && curNode.getChild() != null) {
+                curTraversal = CHILD;
+                curNode = curNode.getChild();
+            } else if (curNode.getNextSibling() != null) {
+
+                curTraversal = SIBILING;
+                curNode = curNode.getNextSibling();
+            } else {
+                curTraversal = PARENT;
+                curNode = curNode.getParent();
+            }
+        }
+    }
+
+    /**
+     * Add resolvable leaves type info to resolution list.
+     *
+     * @param leavesHolder leaves holder node
+     */
+    private static void addResolvableLeavesToResolutionList(YangLeavesHolder leavesHolder) {
+        if (leavesHolder.getListOfLeaf() != null && !leavesHolder.getListOfLeaf().isEmpty()) {
+            for (YangLeaf leaf : leavesHolder.getListOfLeaf()) {
+                YangType type = leaf.getDataType();
+                if (type.getDataType() == DERIVED) {
+
+                    type.setTypeForInterFileGroupingResolution(true);
+
+                    // Add resolution information to the list
+                    YangResolutionInfoImpl resolutionInfo =
+                            new YangResolutionInfoImpl<YangType>(type, (YangNode) leavesHolder,
+                                    type.getLineNumber(), type.getCharPosition());
+                    try {
+                        addResolutionInfo(resolutionInfo);
+                    } catch (DataModelException e) {
+                        throw new LinkerException("Failed to add leaf type info in grouping, to resolution ");
+                    }
+                } else if (type.getDataType() == IDENTITYREF) {
+                    YangIdentityRef identityRef = (YangIdentityRef) type.getDataTypeExtendedInfo();
+
+                    identityRef.setIdentityForInterFileGroupingResolution(true);
+
+                    // Add resolution information to the list
+                    YangResolutionInfoImpl resolutionInfo =
+                            new YangResolutionInfoImpl<YangIdentityRef>(identityRef, (YangNode) leavesHolder,
+                                    identityRef.getLineNumber(), identityRef.getCharPosition());
+                    try {
+                        addResolutionInfo(resolutionInfo);
+                    } catch (DataModelException e) {
+                        throw new LinkerException("Failed to add leaf identity ref info in grouping, to resolution ");
+                    }
+                }
+            }
+        }
+
+        if (leavesHolder.getListOfLeafList() != null && !leavesHolder.getListOfLeafList().isEmpty()) {
+            for (YangLeafList leafList : leavesHolder.getListOfLeafList()) {
+                YangType type = leafList.getDataType();
+                if (type.getDataType() == DERIVED) {
+
+                    type.setTypeForInterFileGroupingResolution(true);
+
+                    // Add resolution information to the list
+                    YangResolutionInfoImpl resolutionInfo =
+                            new YangResolutionInfoImpl<YangType>(type, (YangNode) leavesHolder,
+                                    type.getLineNumber(), type.getCharPosition());
+                    try {
+                        addResolutionInfo(resolutionInfo);
+                    } catch (DataModelException e) {
+                        throw new LinkerException("Failed to add leaf type info in grouping, to resolution ");
+                    }
+                } else if (type.getDataType() == IDENTITYREF) {
+                    YangIdentityRef identityRef = (YangIdentityRef) type.getDataTypeExtendedInfo();
+
+                    identityRef.setIdentityForInterFileGroupingResolution(true);
+                    // Add resolution information to the list
+                    YangResolutionInfoImpl resolutionInfo =
+                            new YangResolutionInfoImpl<YangIdentityRef>(identityRef, (YangNode) leavesHolder,
+                                    identityRef.getLineNumber(), identityRef.getCharPosition());
+                    try {
+                        addResolutionInfo(resolutionInfo);
+                    } catch (DataModelException e) {
+                        throw new LinkerException("Failed to add leaf identity ref info in grouping, to resolution ");
+                    }
+                }
+            }
+        }
+    }
+
 }
diff --git a/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangResolutionInfoImpl.java b/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangResolutionInfoImpl.java
index f338672..00fb699 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangResolutionInfoImpl.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/linker/impl/YangResolutionInfoImpl.java
@@ -21,6 +21,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Stack;
+
 import org.onosproject.yangutils.datamodel.Resolvable;
 import org.onosproject.yangutils.datamodel.ResolvableType;
 import org.onosproject.yangutils.datamodel.TraversalType;
@@ -286,7 +287,8 @@
      * Adds the leafref/identityref type to the type, which has derived type referring to
      * typedef with leafref/identityref type.
      */
-    private void addDerivedRefTypeToRefTypeResolutionList() throws DataModelException {
+    private void addDerivedRefTypeToRefTypeResolutionList()
+            throws DataModelException {
 
         YangNode potentialAncestorWithReferredNode = getEntityToResolveInfo().getHolderOfEntityToResolve();
 
@@ -377,7 +379,7 @@
         if (entityToResolve != null && !entityToResolve.isEmpty()) {
             Iterator<T> entityToResolveIterator = entityToResolve.listIterator();
             while (entityToResolveIterator.hasNext()) {
-                addUnresolvedEntitiesToStack(entityToResolveIterator.next());
+                addUnresolvedEntitiesToResolutionList(entityToResolveIterator.next());
             }
         }
         if (((Resolvable) getCurrentEntityToResolveFromStack()).getResolvableStatus() != INTRA_FILE_RESOLVED
@@ -393,7 +395,8 @@
      * @param entityToResolve entity to resolve
      * @throws DataModelException a violation of data model rules
      */
-    private void addUnresolvedEntitiesToStack(T entityToResolve) throws DataModelException {
+    private void addUnresolvedEntitiesToResolutionList(T entityToResolve)
+            throws DataModelException {
         if (entityToResolve instanceof YangEntityToResolveInfoImpl) {
             YangEntityToResolveInfoImpl entityToResolveInfo = (YangEntityToResolveInfoImpl) entityToResolve;
             if (entityToResolveInfo.getEntityToResolve() instanceof YangLeafRef) {
@@ -403,12 +406,13 @@
                 if (leafref.getResolvableStatus() == UNRESOLVED) {
                     leafref.setResolvableStatus(INTRA_FILE_RESOLVED);
                 }
-                // Add resolution information to the list.
-                YangResolutionInfoImpl resolutionInfoImpl = new YangResolutionInfoImpl<YangLeafRef>(leafref,
-                        parentNodeOfLeafref, entityToResolveInfo.getLineNumber(),
-                        entityToResolveInfo.getCharPosition());
-                addResolutionInfo(resolutionInfoImpl);
             }
+
+            // Add resolution information to the list.
+            YangResolutionInfoImpl resolutionInfoImpl = new YangResolutionInfoImpl<>(
+                    entityToResolveInfo.getEntityToResolve(), entityToResolveInfo.getHolderOfEntityToResolve(),
+                    entityToResolveInfo.getLineNumber(), entityToResolveInfo.getCharPosition());
+            addResolutionInfo(resolutionInfoImpl);
         }
     }
 
@@ -450,6 +454,11 @@
             return;
         } else {
 
+            YangType type = null;
+            if (getCurrentEntityToResolveFromStack() instanceof YangType) {
+                type = (YangType) getCurrentEntityToResolveFromStack();
+            }
+
             /**
              * Traverse up in the ancestor tree to check if the referred node is
              * defined
@@ -465,6 +474,12 @@
                 }
 
                 potentialAncestorWithReferredNode = potentialAncestorWithReferredNode.getParent();
+
+                if (type != null && potentialAncestorWithReferredNode != null) {
+                    if (potentialAncestorWithReferredNode.getParent() == null) {
+                        type.setTypeNotResolvedTillRootNode(true);
+                    }
+                }
             }
         }
 
@@ -719,7 +734,8 @@
      * @throws DataModelException data model errors
      */
     private YangNode isReferredNodeInSiblingProcessedForIdentity(YangNode potentialReferredNode,
-                                                                 String referredNodeName) throws DataModelException {
+            String referredNodeName)
+            throws DataModelException {
 
         while (potentialReferredNode != null) {
             if (potentialReferredNode instanceof YangIdentity) {
@@ -902,7 +918,8 @@
         return false;
     }
 
-    private boolean isFeatureDefinedInNode(YangNode node) throws DataModelException {
+    private boolean isFeatureDefinedInNode(YangNode node)
+            throws DataModelException {
         YangNodeIdentifier ifFeature = ((YangIfFeature) getCurrentEntityToResolveFromStack()).getName();
         List<YangFeature> featureList = ((YangFeatureHolder) node).getFeatureList();
         if (featureList != null && !featureList.isEmpty()) {
@@ -1209,7 +1226,7 @@
      * @param root            root node
      */
     private void processXPathLinking(T entityToResolve,
-                                     YangReferenceResolver root) {
+            YangReferenceResolver root) {
 
         YangXpathLinker<T> xPathLinker = new YangXpathLinker<T>();
 
@@ -1455,7 +1472,8 @@
      * @param resolutionInfo information about the YANG construct which has to be resolved
      * @throws DataModelException a violation of data model rules
      */
-    public void setAbsolutePathFromRelativePathInLeafref(T resolutionInfo) throws DataModelException {
+    public void setAbsolutePathFromRelativePathInLeafref(T resolutionInfo)
+            throws DataModelException {
         if (resolutionInfo instanceof YangLeafRef) {
 
             YangNode parentOfLeafref = ((YangLeafRef) resolutionInfo).getParentNodeOfLeafref();
@@ -1504,7 +1522,8 @@
      * @throws DataModelException a violation of data model rules
      */
     private void fillAbsolutePathValuesInLeafref(YangLeafRef leafref, String pathNameToBePrefixed,
-                                                 List<YangAtomicPath> atomicPathsInRelative) throws DataModelException {
+            List<YangAtomicPath> atomicPathsInRelative)
+            throws DataModelException {
 
         leafref.setPathType(YangPathArgType.ABSOLUTE_PATH);
         String[] pathName = new String[0];
diff --git a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/IdentityrefListener.java b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/IdentityrefListener.java
index 49037e2..2d04602 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/IdentityrefListener.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/IdentityrefListener.java
@@ -33,11 +33,13 @@
 import static org.onosproject.yangutils.datamodel.utils.YangConstructType.IDENTITYREF_DATA;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
-import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
-import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
-import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
+        .constructExtendedListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
+        .constructListenerErrorMessage;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
@@ -71,7 +73,7 @@
      * @param ctx      context object of the grammar rule
      */
     public static void processIdentityrefEntry(TreeWalkListener listener,
-                                        GeneratedYangParser.IdentityrefSpecificationContext ctx) {
+            GeneratedYangParser.IdentityrefSpecificationContext ctx) {
 
         // Check for stack to be non empty.
         checkStackIsNotEmpty(listener, MISSING_HOLDER, IDENTITYREF_DATA, "", ENTRY);
@@ -85,7 +87,7 @@
 
             // Validate node identifier.
             YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.baseStatement().string().getText(),
-                                                                       BASE_DATA, ctx);
+                    BASE_DATA, ctx);
             identityRef.setBaseIdentity(nodeIdentifier);
             ((YangType) typeData).setDataTypeExtendedInfo(identityRef);
 
@@ -106,16 +108,17 @@
                     // Verify parent node of leaf
                     if (!(parentNodeOfLeaf instanceof YangNode)) {
                         throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
-                                                                                IDENTITYREF_DATA, ctx.getText(), EXIT));
+                                IDENTITYREF_DATA, ctx.getText(), EXIT));
                     }
 
                     identityRef.setResolvableStatus(UNRESOLVED);
 
-                    // Add resolution information to the list
-                    resolutionInfo =  new YangResolutionInfoImpl<YangIdentityRef>(identityRef,
-                                                  (YangNode) parentNodeOfLeaf, errorLine, errorPosition);
-                    addToResolutionList(resolutionInfo, ctx);
-
+                    if (listener.getGroupingDepth() == 0) {
+                        // Add resolution information to the list
+                        resolutionInfo = new YangResolutionInfoImpl<YangIdentityRef>(identityRef,
+                                (YangNode) parentNodeOfLeaf, errorLine, errorPosition);
+                        addToResolutionList(resolutionInfo, ctx);
+                    }
                     break;
                 case LEAF_LIST_DATA:
 
@@ -129,15 +132,17 @@
                     // Verify parent node of leaf
                     if (!(parentNodeOfLeafList instanceof YangNode)) {
                         throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
-                                                                                IDENTITYREF_DATA, ctx.getText(), EXIT));
+                                IDENTITYREF_DATA, ctx.getText(), EXIT));
                     }
 
                     identityRef.setResolvableStatus(UNRESOLVED);
 
-                    // Add resolution information to the list
-                    resolutionInfo = new YangResolutionInfoImpl<YangIdentityRef>(identityRef,
-                                               (YangNode) parentNodeOfLeafList, errorLine, errorPosition);
-                    addToResolutionList(resolutionInfo, ctx);
+                    if (listener.getGroupingDepth() == 0) {
+                        // Add resolution information to the list
+                        resolutionInfo = new YangResolutionInfoImpl<YangIdentityRef>(identityRef,
+                                (YangNode) parentNodeOfLeafList, errorLine, errorPosition);
+                        addToResolutionList(resolutionInfo, ctx);
+                    }
                     break;
                 case UNION_DATA:
 
@@ -146,16 +151,17 @@
                     // Verify parent node of leaf
                     if (!(parentNodeOfUnionNode instanceof YangNode)) {
                         throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
-                                                                                IDENTITYREF_DATA, ctx.getText(), EXIT));
+                                IDENTITYREF_DATA, ctx.getText(), EXIT));
                     }
 
                     identityRef.setResolvableStatus(UNRESOLVED);
 
-                    // Add resolution information to the list
-                    resolutionInfo = new YangResolutionInfoImpl<YangIdentityRef>(identityRef,
-                                              (YangNode) parentNodeOfUnionNode, errorLine, errorPosition);
-                    addToResolutionList(resolutionInfo, ctx);
-
+                    if (listener.getGroupingDepth() == 0) {
+                        // Add resolution information to the list
+                        resolutionInfo = new YangResolutionInfoImpl<YangIdentityRef>(identityRef,
+                                (YangNode) parentNodeOfUnionNode, errorLine, errorPosition);
+                        addToResolutionList(resolutionInfo, ctx);
+                    }
                     break;
                 case TYPEDEF_DATA:
                     /**
@@ -167,11 +173,11 @@
                     break;
                 default:
                     throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITYREF_DATA,
-                                                                            ctx.getText(), EXIT));
+                            ctx.getText(), EXIT));
             }
             listener.getParsedDataStack().push(typeData);
             listener.getParsedDataStack().push(identityRef);
-         } else {
+        } else {
             throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITYREF_DATA, "", ENTRY));
         }
     }
@@ -184,7 +190,7 @@
      * @param ctx      context object of the grammar rule
      */
     public static void processIdentityrefExit(TreeWalkListener listener,
-                                       GeneratedYangParser.IdentityrefSpecificationContext ctx) {
+            GeneratedYangParser.IdentityrefSpecificationContext ctx) {
 
         // Check for stack to be non empty.
         checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, IDENTITYREF_DATA, ctx.getText(), EXIT);
@@ -192,7 +198,7 @@
         Parsable parsableType = listener.getParsedDataStack().pop();
         if (!(parsableType instanceof YangIdentityRef)) {
             throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITYREF_DATA,
-                                                                    ctx.getText(), EXIT));
+                    ctx.getText(), EXIT));
         }
     }
 
@@ -203,12 +209,12 @@
      * @param ctx            context object of the grammar rule
      */
     private static void addToResolutionList(YangResolutionInfoImpl<YangIdentityRef> resolutionInfo,
-                                            GeneratedYangParser.IdentityrefSpecificationContext ctx) {
+            GeneratedYangParser.IdentityrefSpecificationContext ctx) {
         try {
             addResolutionInfo(resolutionInfo);
         } catch (DataModelException e) {
             throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
-                                               IDENTITYREF_DATA, ctx.getText(), ENTRY, e.getMessage()));
+                    IDENTITYREF_DATA, ctx.getText(), ENTRY, e.getMessage()));
         }
     }
 }
diff --git a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListener.java b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListener.java
index 972d1ef..c77ffea 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListener.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListener.java
@@ -17,6 +17,7 @@
 package org.onosproject.yangutils.parser.impl.listeners;
 
 import java.util.Date;
+
 import org.onosproject.yangutils.datamodel.ResolvableType;
 import org.onosproject.yangutils.datamodel.YangModule;
 import org.onosproject.yangutils.datamodel.YangReferenceResolver;
diff --git a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/TypeListener.java b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/TypeListener.java
index 3c7fba9..73b62b2 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/TypeListener.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/TypeListener.java
@@ -38,8 +38,10 @@
 import static org.onosproject.yangutils.datamodel.utils.YangConstructType.TYPE_DATA;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
-import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
-import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
+        .constructExtendedListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
+        .constructListenerErrorMessage;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
@@ -82,7 +84,7 @@
      * @param ctx      context object of the grammar rule
      */
     public static void processTypeEntry(TreeWalkListener listener,
-                                        GeneratedYangParser.TypeStatementContext ctx) {
+            GeneratedYangParser.TypeStatementContext ctx) {
 
         // Check for stack to be non empty.
         checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), ENTRY);
@@ -136,10 +138,12 @@
 
                     type.setResolvableStatus(UNRESOLVED);
 
-                    // Add resolution information to the list
-                    YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangType>(type,
-                            (YangNode) parentNodeOfLeaf, errorLine, errorPosition);
-                    addToResolutionList(resolutionInfo, ctx);
+                    if (listener.getGroupingDepth() == 0) {
+                        // Add resolution information to the list
+                        YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangType>(type,
+                                (YangNode) parentNodeOfLeaf, errorLine, errorPosition);
+                        addToResolutionList(resolutionInfo, ctx);
+                    }
                 }
                 break;
             case LEAF_LIST_DATA:
@@ -166,11 +170,13 @@
                     YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
                     ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
 
-                    // Add resolution information to the list
-                    YangResolutionInfoImpl resolutionInfo =
-                            new YangResolutionInfoImpl<YangType>(type, (YangNode) parentNodeOfLeafList, errorLine,
-                                    errorPosition);
-                    addToResolutionList(resolutionInfo, ctx);
+                    if (listener.getGroupingDepth() == 0) {
+                        // Add resolution information to the list
+                        YangResolutionInfoImpl resolutionInfo =
+                                new YangResolutionInfoImpl<YangType>(type, (YangNode) parentNodeOfLeafList, errorLine,
+                                        errorPosition);
+                        addToResolutionList(resolutionInfo, ctx);
+                    }
                 }
                 break;
             case UNION_DATA:
@@ -196,10 +202,12 @@
 
                     type.setResolvableStatus(UNRESOLVED);
 
-                    // Add resolution information to the list
-                    YangResolutionInfoImpl resolutionInfo =
-                            new YangResolutionInfoImpl<YangType>(type, unionNode, errorLine, errorPosition);
-                    addToResolutionList(resolutionInfo, ctx);
+                    if (listener.getGroupingDepth() == 0) {
+                        // Add resolution information to the list
+                        YangResolutionInfoImpl resolutionInfo =
+                                new YangResolutionInfoImpl<YangType>(type, unionNode, errorLine, errorPosition);
+                        addToResolutionList(resolutionInfo, ctx);
+                    }
                 }
 
                 break;
@@ -218,11 +226,12 @@
                     ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
 
                     type.setResolvableStatus(UNRESOLVED);
-
-                    // Add resolution information to the list
-                    YangResolutionInfoImpl resolutionInfo =
-                            new YangResolutionInfoImpl<YangType>(type, typeDef, errorLine, errorPosition);
-                    addToResolutionList(resolutionInfo, ctx);
+                    if (listener.getGroupingDepth() == 0) {
+                        // Add resolution information to the list
+                        YangResolutionInfoImpl resolutionInfo =
+                                new YangResolutionInfoImpl<YangType>(type, typeDef, errorLine, errorPosition);
+                        addToResolutionList(resolutionInfo, ctx);
+                    }
                 }
                 break;
             //TODO: deviate replacement statement.
@@ -256,7 +265,7 @@
      * @param ctx      context object of the grammar rule
      */
     public static void processTypeExit(TreeWalkListener listener,
-                                       GeneratedYangParser.TypeStatementContext ctx) {
+            GeneratedYangParser.TypeStatementContext ctx) {
 
         // Check for stack to be non empty.
         checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
@@ -275,7 +284,7 @@
      * @param ctx            context object of the grammar rule
      */
     private static void addToResolutionList(YangResolutionInfoImpl<YangType> resolutionInfo,
-                                            GeneratedYangParser.TypeStatementContext ctx) {
+            GeneratedYangParser.TypeStatementContext ctx) {
         try {
             addResolutionInfo(resolutionInfo);
         } catch (DataModelException e) {
@@ -291,7 +300,7 @@
      * @param yangDataType yang data type
      */
     private static void validateTypeSubStatementCardinality(GeneratedYangParser.TypeStatementContext ctx,
-                                                            YangDataTypes yangDataType) {
+            YangDataTypes yangDataType) {
         if (ctx.typeBodyStatements() == null || ctx.typeBodyStatements().isEmpty()) {
             ParserException parserException;
             switch (yangDataType) {
@@ -317,7 +326,7 @@
                     break;
                 case IDENTITYREF:
                     parserException = new ParserException("YANG file error : a type identityref" +
-                                                                  " must have base statement.");
+                            " must have base statement.");
                     break;
                 default:
                     return;
diff --git a/plugin/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java b/plugin/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java
index af4deb9..9ffb424 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java
@@ -32,6 +32,8 @@
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
 import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangReferenceResolver;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
 import org.onosproject.yangutils.linker.YangLinker;
 import org.onosproject.yangutils.linker.exceptions.LinkerException;
 import org.onosproject.yangutils.linker.impl.YangLinkerManager;
@@ -45,6 +47,9 @@
 
 import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES;
 import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE;
+import static org.onosproject.yangutils.datamodel.ResolvableType.YANG_DERIVED_DATA_TYPE;
+import static org.onosproject.yangutils.datamodel.ResolvableType.YANG_IDENTITYREF;
+import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.resolveGroupingInDefinationScope;
 import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.addToCompilationRoot;
 import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.copyYangFilesToTarget;
 import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.resolveInterJarDependencies;
@@ -214,7 +219,6 @@
             if (getCurYangFileInfo() != null) {
                 fileName = getCurYangFileInfo().getYangFileName();
             }
-
             try {
                 translatorErrorHandler(getRootNode(), yangPlugin);
                 deleteDirectory(getDirectory(baseDir, classFileDir) + DEFAULT_PKG);
@@ -242,7 +246,8 @@
      *
      * @throws IOException when fails to do IO operations
      */
-    private void resolveInterJarDependency() throws IOException {
+    private void resolveInterJarDependency()
+            throws IOException {
         try {
             List<YangNode> interJarResolvedNodes = resolveInterJarDependencies(project, localRepository,
                     remoteRepository, getDirectory(baseDir, outputDirectory));
@@ -298,6 +303,13 @@
                     YangNode yangNode = yangUtilsParser.getDataModel(yangFileInfo.getYangFileName());
                     yangFileInfo.setRootNode(yangNode);
                     setRootNode(yangNode);
+                    resolveGroupingInDefinationScope((YangReferenceResolver) yangNode);
+                    try {
+                        ((YangReferenceResolver) yangNode).resolveSelfFileLinking(YANG_DERIVED_DATA_TYPE);
+                        ((YangReferenceResolver) yangNode).resolveSelfFileLinking(YANG_IDENTITYREF);
+                    } catch (DataModelException e) {
+                        //TODO: throw exception : throw e;
+                    }
                 } catch (ParserException e) {
                     String logInfo = "Error in file: " + e.getFileName();
                     if (e.getLineNumber() != 0) {