YANG Grouping Linker Support

Change-Id: I2fec0c0bb4d1584e82ffba3228106897ccad2bf5
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/utils/DataModelUtils.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/utils/DataModelUtils.java
index 76e8f36..643eda5 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/utils/DataModelUtils.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/utils/DataModelUtils.java
@@ -20,6 +20,7 @@
 
 import org.onosproject.yangutils.datamodel.CollisionDetector;
 import org.onosproject.yangutils.datamodel.HasResolutionInfo;
+import org.onosproject.yangutils.datamodel.YangImport;
 import org.onosproject.yangutils.datamodel.YangLeaf;
 import org.onosproject.yangutils.datamodel.YangLeafList;
 import org.onosproject.yangutils.datamodel.YangLeavesHolder;
@@ -28,6 +29,7 @@
 import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
 import org.onosproject.yangutils.utils.YangConstructType;
 
+
 /**
  * Represents utilities for data model tree.
  */
@@ -43,7 +45,7 @@
      * Detects the colliding identifier name in a given YANG node and its child.
      *
      * @param identifierName name for which collision detection is to be
-     *            checked
+     * checked
      * @param dataType type of YANG node asking for detecting collision
      * @param node instance of calling node
      * @throws DataModelException a violation of data model rules
@@ -77,7 +79,7 @@
      *
      * @param leavesHolder leaves node against which collision to be checked
      * @param identifierName name for which collision detection is to be
-     *            checked
+     * checked
      * @throws DataModelException a violation of data model rules
      */
     private static void detectCollidingLeaf(YangLeavesHolder leavesHolder, String identifierName)
@@ -96,7 +98,7 @@
      *
      * @param leavesHolder leaves node against which collision to be checked
      * @param identifierName name for which collision detection is to be
-     *            checked
+     * checked
      * @throws DataModelException a violation of data model rules
      */
     private static void detectCollidingLeafList(YangLeavesHolder leavesHolder, String identifierName)
@@ -114,13 +116,17 @@
      * Add a resolution information.
      *
      * @param resolutionInfo information about the YANG construct which has to
-     *                       be resolved
+     * be resolved
      * @throws DataModelException a violation of data model rules
      */
-    public static void addResolutionInfo(YangResolutionInfo resolutionInfo) throws DataModelException {
+    public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
+            throws DataModelException {
+
+
 
         /* get the module node to add maintain the list of nested reference */
-        YangNode curNode = resolutionInfo.getHolderOfEntityToResolve();
+        YangNode curNode = resolutionInfo.getEntityToResolveInfo()
+                .getHolderOfEntityToResolve();
         while (!(curNode instanceof HasResolutionInfo)) {
             curNode = curNode.getParent();
             if (curNode == null) {
@@ -128,25 +134,58 @@
             }
         }
         HasResolutionInfo resolutionNode = (HasResolutionInfo) curNode;
+
+        if (!isPrefixValid(resolutionInfo.getEntityToResolveInfo().getEntityPrefix(),
+                resolutionNode)) {
+            throw new DataModelException("The prefix used is not valid");
+        }
         resolutionNode.addToResolutionList(resolutionInfo);
     }
 
+    private static boolean isPrefixValid(String entityPrefix, HasResolutionInfo resolutionNode) {
+        if (entityPrefix == null) {
+            return true;
+        }
+
+        if (resolutionNode.getPrefix().contentEquals(entityPrefix)) {
+            return true;
+        }
+
+        if (resolutionNode.getImportList() != null) {
+            for (YangImport importedInfo : resolutionNode.getImportList()) {
+                if (importedInfo.getPrefixId().contentEquals(entityPrefix)) {
+                    return true;
+                }
+            }
+        }
+
+        if (resolutionNode.getIncludeList() != null) {
+            /**
+             * TODO: check if the prefix matches with the imported data
+
+             for (YangInclude includedInfo : resolutionNode.getIncludeList()) {
+             if (includedInfo.contentEquals(prefix)) {
+             return true;
+             }
+             }*/
+        }
+
+        return false;
+    }
+
     /**
      * Resolve linking for a resolution list.
      *
      * @param resolutionList resolution list for which linking to be done
-     * @param resolutionInfoNode module/sub-module node
+     * @param dataModelRootNode module/sub-module node
      * @throws DataModelException a violation of data model rules
      */
     public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
-            HasResolutionInfo resolutionInfoNode)
+            HasResolutionInfo dataModelRootNode)
             throws DataModelException {
 
         for (YangResolutionInfo resolutionInfo : resolutionList) {
-            if (resolutionInfo.getPrefix() == null ||
-                    resolutionInfo.getPrefix().equals(resolutionInfoNode.getPrefix())) {
-                resolutionInfo.resolveLinkingForResolutionInfo(resolutionInfoNode.getPrefix());
-            }
+            resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode.getPrefix());
         }
     }
 }