Finished implementation of GossipIntentStore based on new API and semantics.

Change-Id: I1a71d075e5d34ab7b9f7c2533d389235d6da1d9a
diff --git a/core/store/dist/src/main/java/org/onosproject/store/impl/ClockService.java b/core/store/dist/src/main/java/org/onosproject/store/impl/ClockService.java
index 4fbfc22..3ed89f8 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/impl/ClockService.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/impl/ClockService.java
@@ -18,15 +18,18 @@
 import org.onosproject.store.Timestamp;
 
 /**
- * Clock service that can generate timestamps per object.
+ * Clock service that can generate timestamps based off of two input objects.
+ * Implementations are free to only take one or none of the objects into account
+ * when generating timestamps.
  */
-public interface ClockService<T> {
+public interface ClockService<T, U> {
 
     /**
-     * Gets a new timestamp for the given object.
+     * Gets a new timestamp for the given objects.
      *
-     * @param object Object to get a timestamp for
+     * @param object1 First object to use when generating timestamps
+     * @param object2 Second object to use when generating timestamps
      * @return the new timestamp
      */
-    public Timestamp getTimestamp(T object);
+    public Timestamp getTimestamp(T object1, U object2);
 }
diff --git a/core/store/dist/src/main/java/org/onosproject/store/impl/EventuallyConsistentMap.java b/core/store/dist/src/main/java/org/onosproject/store/impl/EventuallyConsistentMap.java
index 9aaa00c..7cfcd8f 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/impl/EventuallyConsistentMap.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/impl/EventuallyConsistentMap.java
@@ -111,6 +111,27 @@
     public void remove(K key);
 
     /**
+     * Removes the given key-value mapping from the map, if it exists.
+     * <p>
+     * This actually means remove any values up to and including the timestamp
+     * given by {@link org.onosproject.store.impl.ClockService#getTimestamp(Object, Object)}.
+     * Any mappings that produce an earlier timestamp than this given key-value
+     * pair will be removed, and any mappings that produce a later timestamp
+     * will supersede this remove.
+     * </p><p>
+     * Note: this differs from the specification of {@link java.util.Map}
+     * because it does not return a boolean indication whether a value was removed.
+     * Clients are expected to register an
+     * {@link org.onosproject.store.impl.EventuallyConsistentMapListener} if
+     * they are interested in receiving notification of updates to the map.
+     * </p>
+     *
+     * @param key the key to remove the mapping for
+     * @param value the value mapped to the key
+     */
+    public void remove(K key, V value);
+
+    /**
      * Adds mappings for all key-value pairs in the specified map to this map.
      * <p>
      * This will be more efficient in communication than calling individual put
diff --git a/core/store/dist/src/main/java/org/onosproject/store/impl/EventuallyConsistentMapImpl.java b/core/store/dist/src/main/java/org/onosproject/store/impl/EventuallyConsistentMapImpl.java
index f40ca63..0e6a590 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/impl/EventuallyConsistentMapImpl.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/impl/EventuallyConsistentMapImpl.java
@@ -69,7 +69,7 @@
     private final ClusterCommunicationService clusterCommunicator;
     private final KryoSerializer serializer;
 
-    private final ClockService<K> clockService;
+    private final ClockService<K, V> clockService;
 
     private final MessageSubject updateMessageSubject;
     private final MessageSubject removeMessageSubject;
@@ -126,7 +126,7 @@
                                        ClusterService clusterService,
                                        ClusterCommunicationService clusterCommunicator,
                                        KryoNamespace.Builder serializerBuilder,
-                                       ClockService<K> clockService) {
+                                       ClockService<K, V> clockService) {
 
         this.mapName = checkNotNull(mapName);
         this.clusterService = checkNotNull(clusterService);
@@ -227,7 +227,8 @@
         checkNotNull(key, ERROR_NULL_KEY);
         checkNotNull(value, ERROR_NULL_VALUE);
 
-        Timestamp timestamp = clockService.getTimestamp(key);
+        Timestamp timestamp = clockService.getTimestamp(key, value);
+
         if (putInternal(key, value, timestamp)) {
             notifyPeers(new InternalPutEvent<>(key, value, timestamp));
             EventuallyConsistentMapEvent<K, V> externalEvent
@@ -260,7 +261,9 @@
         checkState(!destroyed, mapName + ERROR_DESTROYED);
         checkNotNull(key, ERROR_NULL_KEY);
 
-        Timestamp timestamp = clockService.getTimestamp(key);
+        // TODO prevent calls here if value is important for timestamp
+        Timestamp timestamp = clockService.getTimestamp(key, null);
+
         if (removeInternal(key, timestamp)) {
             notifyPeers(new InternalRemoveEvent<>(key, timestamp));
             EventuallyConsistentMapEvent<K, V> externalEvent
@@ -283,6 +286,23 @@
     }
 
     @Override
+    public void remove(K key, V value) {
+        checkState(!destroyed, mapName + ERROR_DESTROYED);
+        checkNotNull(key, ERROR_NULL_KEY);
+        checkNotNull(value, ERROR_NULL_VALUE);
+
+        Timestamp timestamp = clockService.getTimestamp(key, value);
+
+        if (removeInternal(key, timestamp)) {
+            notifyPeers(new InternalRemoveEvent<>(key, timestamp));
+            EventuallyConsistentMapEvent<K, V> externalEvent
+                    = new EventuallyConsistentMapEvent<>(
+                    EventuallyConsistentMapEvent.Type.REMOVE, key, value);
+            notifyListeners(externalEvent);
+        }
+    }
+
+    @Override
     public void putAll(Map<? extends K, ? extends V> m) {
         checkState(!destroyed, mapName + ERROR_DESTROYED);
 
@@ -295,7 +315,7 @@
             checkNotNull(key, ERROR_NULL_KEY);
             checkNotNull(value, ERROR_NULL_VALUE);
 
-            Timestamp timestamp = clockService.getTimestamp(entry.getKey());
+            Timestamp timestamp = clockService.getTimestamp(key, value);
 
             if (putInternal(key, value, timestamp)) {
                 updates.add(new PutEntry<>(key, value, timestamp));
@@ -306,7 +326,8 @@
             notifyPeers(new InternalPutEvent<>(updates));
 
             for (PutEntry<K, V> entry : updates) {
-                EventuallyConsistentMapEvent<K, V> externalEvent = new EventuallyConsistentMapEvent<>(
+                EventuallyConsistentMapEvent<K, V> externalEvent =
+                        new EventuallyConsistentMapEvent<>(
                         EventuallyConsistentMapEvent.Type.PUT, entry.key(),
                         entry.value());
                 notifyListeners(externalEvent);
@@ -321,7 +342,8 @@
         List<RemoveEntry<K>> removed = new ArrayList<>(items.size());
 
         for (K key : items.keySet()) {
-            Timestamp timestamp = clockService.getTimestamp(key);
+            // TODO also this is not applicable if value is important for timestamp?
+            Timestamp timestamp = clockService.getTimestamp(key, null);
 
             if (removeInternal(key, timestamp)) {
                 removed.add(new RemoveEntry<>(key, timestamp));
@@ -565,7 +587,8 @@
         // Send all updates to the peer at once
         if (!updatesToSend.isEmpty()) {
             try {
-                unicastMessage(sender, updateMessageSubject, new InternalPutEvent<>(updatesToSend));
+                unicastMessage(sender, updateMessageSubject,
+                               new InternalPutEvent<>(updatesToSend));
             } catch (IOException e) {
                 log.warn("Failed to send advertisement response", e);
             }
@@ -603,7 +626,8 @@
         // Send all removes to the peer at once
         if (!removesToSend.isEmpty()) {
             try {
-                unicastMessage(sender, removeMessageSubject, new InternalRemoveEvent<>(removesToSend));
+                unicastMessage(sender, removeMessageSubject,
+                               new InternalRemoveEvent<>(removesToSend));
             } catch (IOException e) {
                 log.warn("Failed to send advertisement response", e);
             }
diff --git a/core/store/dist/src/main/java/org/onosproject/store/impl/MultiValuedTimestamp.java b/core/store/dist/src/main/java/org/onosproject/store/impl/MultiValuedTimestamp.java
new file mode 100644
index 0000000..46c416e
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onosproject/store/impl/MultiValuedTimestamp.java
@@ -0,0 +1,109 @@
+/*
+ * 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.store.impl;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ComparisonChain;
+import org.onosproject.store.Timestamp;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * A logical timestamp that derives its value from two input values. Value1
+ * always takes precedence over value2 when comparing timestamps.
+ */
+public class MultiValuedTimestamp implements Timestamp {
+
+    private final Timestamp timestamp;
+    private final long value2;
+
+    /**
+     * Creates a new timestamp based on two values. The first value has higher
+     * precedence than the second when comparing timestamps.
+     *
+     * @param timestamp first value
+     * @param value2 second value
+     */
+    public MultiValuedTimestamp(Timestamp timestamp, long value2) {
+        this.timestamp = timestamp;
+        this.value2 = value2;
+    }
+
+    @Override
+    public int compareTo(Timestamp o) {
+        checkArgument(o instanceof MultiValuedTimestamp,
+                      "Must be MultiValuedTimestamp", o);
+        MultiValuedTimestamp that = (MultiValuedTimestamp) o;
+
+        return ComparisonChain.start()
+                .compare(this.timestamp, that.timestamp)
+                .compare(this.value2, that.value2)
+                .result();
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(timestamp, value2);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!(obj instanceof MultiValuedTimestamp)) {
+            return false;
+        }
+        MultiValuedTimestamp that = (MultiValuedTimestamp) obj;
+        return Objects.equals(this.timestamp, that.timestamp) &&
+                Objects.equals(this.value2, that.value2);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("timestamp", timestamp)
+                .add("value2", value2)
+                .toString();
+    }
+
+    /**
+     * Returns the first value.
+     *
+     * @return first value
+     */
+    public Timestamp timestamp() {
+        return timestamp;
+    }
+
+    /**
+     * Returns the second value.
+     *
+     * @return second value
+     */
+    public long sequenceNumber() {
+        return value2;
+    }
+
+    // Default constructor for serialization
+    @SuppressWarnings("unused")
+    private MultiValuedTimestamp() {
+        this.timestamp = null;
+        this.value2 = -1;
+    }
+}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/impl/WallclockClockManager.java b/core/store/dist/src/main/java/org/onosproject/store/impl/WallclockClockManager.java
index 0103ee4..ec90a5f 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/impl/WallclockClockManager.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/impl/WallclockClockManager.java
@@ -20,9 +20,9 @@
 /**
  * A clock service which hands out wallclock-based timestamps.
  */
-public class WallclockClockManager<T> implements ClockService<T> {
+public class WallclockClockManager<T, U> implements ClockService<T, U> {
     @Override
-    public Timestamp getTimestamp(T object) {
+    public Timestamp getTimestamp(T object1, U object2) {
         return new WallClockTimestamp();
     }
 }