[ONOS-3894, 4071] YANG Advanced Construct Union & Grouping

Change-Id: I0f828adb5884c2d7b6e4120f9843c416608ae5e7
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangGrouping.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangGrouping.java
index 9a4e590..791b1d3 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangGrouping.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangGrouping.java
@@ -17,11 +17,12 @@
 
 import java.util.LinkedList;
 import java.util.List;
-
 import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
 import org.onosproject.yangutils.parser.Parsable;
 import org.onosproject.yangutils.utils.YangConstructType;
 
+import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
+
 /*-
  * Reference RFC 6020.
  *
@@ -76,7 +77,7 @@
  * Data model node to maintain information defined in YANG grouping.
  */
 public class YangGrouping extends YangNode
-        implements YangLeavesHolder, YangCommonInfo, Parsable {
+        implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector {
 
     /**
      * Name of the grouping.
@@ -113,6 +114,8 @@
      */
     public YangGrouping() {
         super(YangNodeType.GROUPING_NODE);
+        listOfLeaf = new LinkedList<YangLeaf>();
+        listOfLeafList = new LinkedList<YangLeafList>();
     }
 
     /**
@@ -181,10 +184,6 @@
      */
     @Override
     public void addLeaf(YangLeaf leaf) {
-        if (getListOfLeaf() == null) {
-            setListOfLeaf(new LinkedList<YangLeaf>());
-        }
-
         getListOfLeaf().add(leaf);
     }
 
@@ -214,10 +213,6 @@
      */
     @Override
     public void addLeafList(YangLeafList leafList) {
-        if (getListOfLeafList() == null) {
-            setListOfLeafList(new LinkedList<YangLeafList>());
-        }
-
         getListOfLeafList().add(leafList);
     }
 
@@ -290,4 +285,31 @@
     public void validateDataOnExit() throws DataModelException {
         // TODO auto-generated method stub, to be implemented by parser
     }
+
+    /*
+     * Reference RFC6020
+     *
+     * Once a grouping is defined, it can be referenced in a "uses"
+     * statement (see Section 7.12).  A grouping MUST NOT reference itself,
+     * neither directly nor indirectly through a chain of other groupings.
+     *
+     * If the grouping is defined at the top level of a YANG module or
+     * submodule, the grouping's identifier MUST be unique within the
+     * module.
+     */
+    @Override
+    public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
+
+        // Asks helper to detect colliding child.
+        detectCollidingChildUtil(identifierName, dataType, this);
+    }
+
+    @Override
+    public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
+        if (getName().equals(identifierName)) {
+            throw new DataModelException("YANG file error: Duplicate input identifier detected, same as grouping \"" +
+                    getName() + "\"");
+        }
+    }
+    // TODO  A grouping MUST NOT reference itself, neither directly nor indirectly through a chain of other groupings.
 }