Distributed group store using eventual consistent map abstraction

Change-Id: I618a0f6fa80e0e25285d7a2026032f09ba90aa70
diff --git a/core/api/src/main/java/org/onosproject/net/group/DefaultGroup.java b/core/api/src/main/java/org/onosproject/net/group/DefaultGroup.java
index 9248625..c52f771 100644
--- a/core/api/src/main/java/org/onosproject/net/group/DefaultGroup.java
+++ b/core/api/src/main/java/org/onosproject/net/group/DefaultGroup.java
@@ -16,13 +16,11 @@
 package org.onosproject.net.group;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
-import static org.slf4j.LoggerFactory.getLogger;
 
 import java.util.Objects;
 
 import org.onosproject.core.GroupId;
 import org.onosproject.net.DeviceId;
-import org.slf4j.Logger;
 
 /**
  * ONOS implementation of default group that is stored in the system.
@@ -30,9 +28,8 @@
 public class DefaultGroup extends DefaultGroupDescription
     implements Group, StoredGroupEntry {
 
-    private final Logger log = getLogger(getClass());
-
     private GroupState state;
+    private boolean isGroupStateAddedFirstTime;
     private long life;
     private long packets;
     private long bytes;
@@ -215,4 +212,14 @@
                 .add("state", state)
                 .toString();
     }
+
+    @Override
+    public void setIsGroupStateAddedFirstTime(boolean isGroupStateAddedFirstTime) {
+        this.isGroupStateAddedFirstTime = isGroupStateAddedFirstTime;
+    }
+
+    @Override
+    public boolean isGroupStateAddedFirstTime() {
+        return isGroupStateAddedFirstTime;
+    }
 }
diff --git a/core/api/src/main/java/org/onosproject/net/group/DefaultGroupDescription.java b/core/api/src/main/java/org/onosproject/net/group/DefaultGroupDescription.java
index 385a5d6..3ffb2c2 100644
--- a/core/api/src/main/java/org/onosproject/net/group/DefaultGroupDescription.java
+++ b/core/api/src/main/java/org/onosproject/net/group/DefaultGroupDescription.java
@@ -41,7 +41,8 @@
      * @param deviceId device identifier
      * @param type type of the group
      * @param buckets immutable list of group bucket
-     * @param appCookie immutable application cookie to be associated with the group
+     * @param appCookie immutable application cookie of type DefaultGroupKey
+     * to be associated with the group
      * @param appId application id
      */
     public DefaultGroupDescription(DeviceId deviceId,
diff --git a/core/api/src/main/java/org/onosproject/net/group/DefaultGroupKey.java b/core/api/src/main/java/org/onosproject/net/group/DefaultGroupKey.java
new file mode 100644
index 0000000..7f00ae7
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/group/DefaultGroupKey.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2015 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.net.group;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Arrays;
+
+/**
+ * Default implementation of group key interface.
+ */
+public class DefaultGroupKey implements GroupKey {
+
+    private final byte[] key;
+
+    public DefaultGroupKey(byte[] key) {
+        this.key = checkNotNull(key);
+    }
+
+    @Override
+    public byte[] key() {
+        return key;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (!(o instanceof DefaultGroupKey)) {
+            return false;
+        }
+        DefaultGroupKey that = (DefaultGroupKey) o;
+        return (Arrays.equals(this.key, that.key));
+    }
+
+    @Override
+    public int hashCode() {
+        return Arrays.hashCode(this.key);
+    }
+
+}
\ No newline at end of file
diff --git a/core/api/src/main/java/org/onosproject/net/group/GroupKey.java b/core/api/src/main/java/org/onosproject/net/group/GroupKey.java
index da8d449..9c87b83 100644
--- a/core/api/src/main/java/org/onosproject/net/group/GroupKey.java
+++ b/core/api/src/main/java/org/onosproject/net/group/GroupKey.java
@@ -17,8 +17,15 @@
 
 /**
  * Representation of generalized Key that would be used to store
- * groups in < Key, Value > store. Implementation of this interface
- * MUST override "equals()" and "hashcode()" methods.
+ * groups in < Key, Value > store. This key uses a generic
+ * byte array so that applications can associate their groups with
+ * any of their data by translating it into a byte array.
  */
 public interface GroupKey  {
+    /**
+     * Returns the byte representation of key.
+     *
+     * @return byte array
+     */
+    public byte[] key();
 }
diff --git a/core/api/src/main/java/org/onosproject/net/group/GroupStore.java b/core/api/src/main/java/org/onosproject/net/group/GroupStore.java
index e7bf4f8..7ccab8a 100644
--- a/core/api/src/main/java/org/onosproject/net/group/GroupStore.java
+++ b/core/api/src/main/java/org/onosproject/net/group/GroupStore.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.net.group;
 
+import org.onosproject.core.GroupId;
 import org.onosproject.net.DeviceId;
 import org.onosproject.store.Store;
 
@@ -60,6 +61,15 @@
     Group getGroup(DeviceId deviceId, GroupKey appCookie);
 
     /**
+     * Returns the stored group entry for an id.
+     *
+     * @param deviceId the device ID
+     * @param groupId the group identifier
+     * @return a group associated with the key
+     */
+    Group getGroup(DeviceId deviceId, GroupId groupId);
+
+    /**
      * Stores a new group entry using the information from group description.
      *
      * @param groupDesc group description to be used to store group entry
diff --git a/core/api/src/main/java/org/onosproject/net/group/StoredGroupEntry.java b/core/api/src/main/java/org/onosproject/net/group/StoredGroupEntry.java
index 297663f..47d3612 100644
--- a/core/api/src/main/java/org/onosproject/net/group/StoredGroupEntry.java
+++ b/core/api/src/main/java/org/onosproject/net/group/StoredGroupEntry.java
@@ -29,6 +29,23 @@
     void setState(Group.GroupState newState);
 
     /**
+     * Sets if group has transitioned to ADDED state for the first time.
+     * This is to differentiate state transitions "from PENDING_ADD to ADDED"
+     * and "from PENDING_UPDATE to ADDED". For internal use only.
+     *
+     * @param isGroupAddedFirstTime true if group moves to ADDED state
+     * for the first time.
+     */
+    void setIsGroupStateAddedFirstTime(boolean isGroupAddedFirstTime);
+
+    /**
+     * Returns the isGroupStateAddedFirstTime value. For internal use only.
+     *
+     * @return isGroupStateAddedFirstTime value
+     */
+    boolean isGroupStateAddedFirstTime();
+
+    /**
      * Sets how long this entry has been entered in the system.
      *
      * @param life epoch time