[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.
 }
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangNodeIdentifier.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangNodeIdentifier.java
new file mode 100644
index 0000000..116ee7a
--- /dev/null
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangNodeIdentifier.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.yangutils.datamodel;
+
+/**
+ * YANG node identifier which is a combination of prefix and name.
+ */
+public class YangNodeIdentifier {
+
+    // Name of the node.
+    String name;
+
+    // Prefix of the node.
+    String prefix;
+
+    /**
+     * Creates an instance of YANG node identifier.
+     */
+    public YangNodeIdentifier() {
+    }
+
+    /**
+     * Returns name of the node identifier.
+     *
+     * @return name of the node identifier
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * Set name of the node identifier.
+     *
+     * @param name node identifier name
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * Returns prefix of the node identifier.
+     *
+     * @return prefix of the node identifier
+     */
+    public String getPrefix() {
+        return prefix;
+    }
+
+    /**
+     * Set prefix of the node identifier.
+     *
+     * @param prefix prefix of the node identifier
+     */
+    public void setPrefix(String prefix) {
+        this.prefix = prefix;
+    }
+}
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangUnion.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangUnion.java
index c61ce80..05e9c28 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangUnion.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangUnion.java
@@ -20,6 +20,7 @@
 import org.onosproject.yangutils.parser.Parsable;
 import org.onosproject.yangutils.utils.YangConstructType;
 
+import java.util.LinkedList;
 import java.util.List;
 
 /*
@@ -52,13 +53,14 @@
     // List of YANG type.
     private List<YangType<?>> typeList;
 
-    // Name of the union.
+    // Name of union.
     private String unionName;
 
     /**
      * Create a YANG union node.
      */
     public YangUnion() {
+        typeList = new LinkedList<>();
     }
 
     /**
@@ -89,6 +91,21 @@
     }
 
     /**
+     * Add YANG type to type list.
+     *
+     * @param yangType YANG type to be added to list
+     * @throws DataModelException union member type must not be one of the
+     * built-in types "empty" or "leafref"
+     */
+    public void addToTypeList(YangType<?> yangType) throws DataModelException {
+        if (yangType.getDataType() == YangDataTypes.EMPTY || yangType.getDataType() == YangDataTypes.LEAFREF) {
+            throw new DataModelException("Union member type must not be one of the built-in types \"empty\" or " +
+                    "\"leafref\"");
+        }
+        getTypeList().add(yangType);
+    }
+
+    /**
      * Set the union name.
      *
      * @param unionName name of the union.
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangUses.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangUses.java
index 22cbf1af..d979bea 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangUses.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangUses.java
@@ -55,17 +55,17 @@
 public class YangUses extends YangNode implements YangCommonInfo, Parsable {
 
     /**
-     * Name.
+     * Name of YANG uses.
      */
     private String name;
 
     /**
-     * referred group.
+     * Referred group.
      */
     private YangGrouping refGroup;
 
     /**
-     * description.
+     * Description of YANG uses.
      */
     private String description;
 
@@ -75,7 +75,7 @@
     private String reference;
 
     /**
-     * Status.
+     * Status of YANG uses.
      */
     private YangStatusType status;
 
@@ -87,7 +87,7 @@
     }
 
     /**
-     * Get the name.
+     * Returns the name.
      *
      * @return the name
      */
@@ -105,7 +105,7 @@
     }
 
     /**
-     * Get the referred group.
+     * Returns the referred group.
      *
      * @return the referred group
      */
@@ -123,7 +123,7 @@
     }
 
     /**
-     * Get the description.
+     * Returns the description.
      *
      * @return the description
      */
@@ -143,7 +143,7 @@
     }
 
     /**
-     * Get the textual reference.
+     * Returns the textual reference.
      *
      * @return the reference
      */
@@ -163,7 +163,7 @@
     }
 
     /**
-     * Get the status.
+     * Returns the status.
      *
      * @return the status
      */
@@ -212,26 +212,14 @@
         // TODO auto-generated method stub, to be implemented by parser
     }
 
-    /**
-     * Get uses name.
-     *
-     * @return uses name
-     */
     @Override
     public String getName() {
-        // TODO Auto-generated method stub
-        return null;
+        return name;
     }
 
-    /**
-     * Set uses name.
-     *
-     * @param name uses name
-     */
     @Override
     public void setName(String name) {
-        // TODO Auto-generated method stub
-
+        this.name = name;
     }
 
 }