base for mastership term implementation

Change-Id: Ie354096ad8835536839e3c1504e19a2cbff866c1
diff --git a/core/api/src/main/java/org/onlab/onos/cluster/MastershipService.java b/core/api/src/main/java/org/onlab/onos/cluster/MastershipService.java
index 31b4bcc..be91609 100644
--- a/core/api/src/main/java/org/onlab/onos/cluster/MastershipService.java
+++ b/core/api/src/main/java/org/onlab/onos/cluster/MastershipService.java
@@ -56,6 +56,13 @@
     Set<DeviceId> getDevicesOf(NodeId nodeId);
 
     /**
+     * Returns the mastership term service for getting term information.
+     *
+     * @return the MastershipTermService for this mastership manager
+     */
+    MastershipTermService requestTermService();
+
+    /**
      * Adds the specified mastership change listener.
      *
      * @param listener the mastership listener
diff --git a/core/api/src/main/java/org/onlab/onos/cluster/MastershipStore.java b/core/api/src/main/java/org/onlab/onos/cluster/MastershipStore.java
index 5c0c207..be5d873 100644
--- a/core/api/src/main/java/org/onlab/onos/cluster/MastershipStore.java
+++ b/core/api/src/main/java/org/onlab/onos/cluster/MastershipStore.java
@@ -55,4 +55,13 @@
      * @return a mastership event
      */
     MastershipEvent setMaster(NodeId nodeId, DeviceId deviceId);
+
+    /**
+     * Returns the current master and number of past mastership hand-offs
+     * (terms) for a device.
+     *
+     * @param deviceId the device identifier
+     * @return the current master's ID and the term value for device, or null
+     */
+    MastershipTerm getTermFor(DeviceId deviceId);
 }
diff --git a/core/api/src/main/java/org/onlab/onos/cluster/MastershipTerm.java b/core/api/src/main/java/org/onlab/onos/cluster/MastershipTerm.java
index 5c3c424..f11dafd 100644
--- a/core/api/src/main/java/org/onlab/onos/cluster/MastershipTerm.java
+++ b/core/api/src/main/java/org/onlab/onos/cluster/MastershipTerm.java
@@ -1,6 +1,46 @@
 package org.onlab.onos.cluster;
 
-public class MastershipTerm {
-    private final NodeId master = null;
+import java.util.Objects;
+
+public final class MastershipTerm {
+
+    private final NodeId master;
     private int termNumber;
+
+    private MastershipTerm(NodeId master, int term) {
+        this.master = master;
+        this.termNumber = term;
+    }
+
+    public static MastershipTerm of(NodeId master, int term) {
+        return new MastershipTerm(master, term);
+    }
+
+    public NodeId master() {
+        return master;
+    }
+
+    public int termNumber() {
+        return termNumber;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(master, termNumber);
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        if (other instanceof MastershipTerm) {
+            MastershipTerm that = (MastershipTerm) other;
+            if (!this.master.equals(that.master)) {
+                return false;
+            }
+            if (this.termNumber != that.termNumber) {
+                return false;
+            }
+            return true;
+        }
+        return false;
+    }
 }
diff --git a/core/api/src/test/java/org/onlab/onos/cluster/MastershipServiceAdapter.java b/core/api/src/test/java/org/onlab/onos/cluster/MastershipServiceAdapter.java
index 4b3b7dc..2e92f5b 100644
--- a/core/api/src/test/java/org/onlab/onos/cluster/MastershipServiceAdapter.java
+++ b/core/api/src/test/java/org/onlab/onos/cluster/MastershipServiceAdapter.java
@@ -40,4 +40,9 @@
     @Override
     public void removeListener(MastershipListener listener) {
     }
+
+    @Override
+    public MastershipTermService requestTermService() {
+        return null;
+    }
 }
diff --git a/core/net/src/main/java/org/onlab/onos/cluster/impl/MastershipManager.java b/core/net/src/main/java/org/onlab/onos/cluster/impl/MastershipManager.java
index 255830c..a8d2052 100644
--- a/core/net/src/main/java/org/onlab/onos/cluster/impl/MastershipManager.java
+++ b/core/net/src/main/java/org/onlab/onos/cluster/impl/MastershipManager.java
@@ -12,6 +12,8 @@
 import org.onlab.onos.cluster.MastershipListener;
 import org.onlab.onos.cluster.MastershipService;
 import org.onlab.onos.cluster.MastershipStore;
+import org.onlab.onos.cluster.MastershipTerm;
+import org.onlab.onos.cluster.MastershipTermService;
 import org.onlab.onos.cluster.NodeId;
 import org.onlab.onos.event.AbstractListenerRegistry;
 import org.onlab.onos.event.EventDeliveryService;
@@ -103,6 +105,12 @@
         return store.getDevices(nodeId);
     }
 
+
+    @Override
+    public MastershipTermService requestTermService() {
+        return new InternalMastershipTermService();
+    }
+
     @Override
     public void addListener(MastershipListener listener) {
         checkNotNull(listener);
@@ -124,4 +132,13 @@
         }
     }
 
+    private class InternalMastershipTermService implements MastershipTermService {
+
+        @Override
+        public MastershipTerm getMastershipTerm(DeviceId deviceId) {
+            return store.getTermFor(deviceId);
+        }
+
+    }
+
 }
diff --git a/core/store/src/main/java/org/onlab/onos/store/cluster/impl/DistributedMastershipStore.java b/core/store/src/main/java/org/onlab/onos/store/cluster/impl/DistributedMastershipStore.java
index d3fcf3e..46bf6af 100644
--- a/core/store/src/main/java/org/onlab/onos/store/cluster/impl/DistributedMastershipStore.java
+++ b/core/store/src/main/java/org/onlab/onos/store/cluster/impl/DistributedMastershipStore.java
@@ -15,6 +15,7 @@
 import org.onlab.onos.cluster.MastershipEvent;
 import org.onlab.onos.cluster.MastershipStore;
 import org.onlab.onos.cluster.MastershipStoreDelegate;
+import org.onlab.onos.cluster.MastershipTerm;
 import org.onlab.onos.cluster.NodeId;
 import org.onlab.onos.net.DeviceId;
 import org.onlab.onos.net.MastershipRole;
@@ -115,4 +116,10 @@
         return nodeId.equals(master) ? MastershipRole.MASTER : MastershipRole.STANDBY;
     }
 
+    @Override
+    public MastershipTerm getTermFor(DeviceId deviceId) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
 }
diff --git a/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleMastershipStore.java b/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleMastershipStore.java
index da691fe..61dbe61 100644
--- a/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleMastershipStore.java
+++ b/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleMastershipStore.java
@@ -3,11 +3,13 @@
 import static org.slf4j.LoggerFactory.getLogger;
 
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
@@ -18,6 +20,7 @@
 import org.onlab.onos.cluster.MastershipEvent;
 import org.onlab.onos.cluster.MastershipStore;
 import org.onlab.onos.cluster.MastershipStoreDelegate;
+import org.onlab.onos.cluster.MastershipTerm;
 import org.onlab.onos.cluster.NodeId;
 import org.onlab.onos.net.DeviceId;
 import org.onlab.onos.net.MastershipRole;
@@ -47,6 +50,7 @@
     //devices mapped to their masters, to emulate multiple nodes
     protected final ConcurrentMap<DeviceId, NodeId> masterMap =
             new ConcurrentHashMap<>();
+    protected final Map<DeviceId, AtomicInteger> termMap = new HashMap<>();
 
     @Activate
     public void activate() {
@@ -63,15 +67,21 @@
 
         NodeId node = masterMap.get(deviceId);
         if (node == null) {
-            masterMap.put(deviceId, nodeId);
+            synchronized (this) {
+                masterMap.put(deviceId, nodeId);
+                termMap.put(deviceId, new AtomicInteger());
+            }
             return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId);
         }
 
         if (node.equals(nodeId)) {
             return null;
         } else {
-            masterMap.put(deviceId, nodeId);
-            return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId);
+            synchronized (this) {
+                masterMap.put(deviceId, nodeId);
+                termMap.get(deviceId).incrementAndGet();
+                return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId);
+            }
         }
     }
 
@@ -114,4 +124,13 @@
         return role;
     }
 
+    @Override
+    public MastershipTerm getTermFor(DeviceId deviceId) {
+        if (masterMap.get(deviceId) == null) {
+            return null;
+        }
+        return MastershipTerm.of(
+                masterMap.get(deviceId), termMap.get(deviceId).get());
+    }
+
 }