Add atomic mastership/term/backups method to MastershipService

Change-Id: I18c3aeaa5101c9ce08ff38fffd70eaec903a0f3e
diff --git a/core/api/src/test/java/org/onosproject/mastership/MastershipEventTest.java b/core/api/src/test/java/org/onosproject/mastership/MastershipEventTest.java
index c5f6ba9..7b791f4 100644
--- a/core/api/src/test/java/org/onosproject/mastership/MastershipEventTest.java
+++ b/core/api/src/test/java/org/onosproject/mastership/MastershipEventTest.java
@@ -15,14 +15,15 @@
  */
 package org.onosproject.mastership;
 
+import com.google.common.collect.ImmutableMap;
 import com.google.common.testing.EqualsTester;
 import org.junit.Test;
 
 import org.onosproject.cluster.NodeId;
-import org.onosproject.cluster.RoleInfo;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.MastershipRole;
 
-import java.util.Arrays;
+import java.util.Optional;
 
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThat;
@@ -36,21 +37,33 @@
     private final DeviceId deviceId2 = DeviceId.deviceId("bar:baz");
     private final NodeId node1 = new NodeId("1");
     private final NodeId node2 = new NodeId("2");
-    private final RoleInfo roleInfo1 = new RoleInfo(node1, Arrays.asList(node1, node2));
-    private final RoleInfo roleInfo2 = new RoleInfo(node2, Arrays.asList(node2, node1));
+    private final MastershipInfo mastershipInfo1 = new MastershipInfo(
+        1,
+        Optional.of(node1),
+        ImmutableMap.<NodeId, MastershipRole>builder()
+            .put(node1, MastershipRole.MASTER)
+            .put(node2, MastershipRole.STANDBY)
+            .build());
+    private final MastershipInfo mastershipInfo2 = new MastershipInfo(
+        2,
+        Optional.of(node1),
+        ImmutableMap.<NodeId, MastershipRole>builder()
+            .put(node2, MastershipRole.MASTER)
+            .put(node1, MastershipRole.STANDBY)
+            .build());
 
     private final MastershipEvent event1 =
-            new MastershipEvent(MastershipEvent.Type.BACKUPS_CHANGED, deviceId1, roleInfo1);
+            new MastershipEvent(MastershipEvent.Type.BACKUPS_CHANGED, deviceId1, mastershipInfo1);
     private final MastershipEvent event2 =
-            new MastershipEvent(MastershipEvent.Type.MASTER_CHANGED, deviceId1, roleInfo1);
+            new MastershipEvent(MastershipEvent.Type.MASTER_CHANGED, deviceId1, mastershipInfo1);
     private final MastershipEvent event3 =
-            new MastershipEvent(MastershipEvent.Type.SUSPENDED, deviceId1, roleInfo1);
+            new MastershipEvent(MastershipEvent.Type.SUSPENDED, deviceId1, mastershipInfo1);
     private final MastershipEvent event4 =
-            new MastershipEvent(MastershipEvent.Type.MASTER_CHANGED, deviceId1, roleInfo2, time);
+            new MastershipEvent(MastershipEvent.Type.MASTER_CHANGED, deviceId1, mastershipInfo2, time);
     private final MastershipEvent sameAsEvent4 =
-            new MastershipEvent(MastershipEvent.Type.MASTER_CHANGED, deviceId1, roleInfo2, time);
+            new MastershipEvent(MastershipEvent.Type.MASTER_CHANGED, deviceId1, mastershipInfo2, time);
     private final MastershipEvent event5 =
-            new MastershipEvent(MastershipEvent.Type.BACKUPS_CHANGED, deviceId2, roleInfo1);
+            new MastershipEvent(MastershipEvent.Type.BACKUPS_CHANGED, deviceId2, mastershipInfo1);
 
     /**
      * Tests for proper operation of equals(), hashCode() and toString() methods.
@@ -73,12 +86,12 @@
     public void checkConstruction() {
         assertThat(event1.type(), is(MastershipEvent.Type.BACKUPS_CHANGED));
         assertThat(event1.subject(), is(deviceId1));
-        assertThat(event1.roleInfo(), is(roleInfo1));
+        assertThat(event1.mastershipInfo(), is(mastershipInfo1));
 
         assertThat(event4.time(), is(time));
         assertThat(event4.type(), is(MastershipEvent.Type.MASTER_CHANGED));
         assertThat(event4.subject(), is(deviceId1));
-        assertThat(event4.roleInfo(), is(roleInfo2));
+        assertThat(event4.mastershipInfo(), is(mastershipInfo2));
     }
 
 }
diff --git a/core/api/src/test/java/org/onosproject/mastership/MastershipInfoTest.java b/core/api/src/test/java/org/onosproject/mastership/MastershipInfoTest.java
new file mode 100644
index 0000000..89a3350
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/mastership/MastershipInfoTest.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.mastership;
+
+import java.util.Optional;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import org.junit.Test;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.net.MastershipRole;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+/**
+ * Mastership info test.
+ */
+public class MastershipInfoTest {
+    private final NodeId node1 = new NodeId("1");
+    private final NodeId node2 = new NodeId("2");
+    private final NodeId node3 = new NodeId("3");
+    private final NodeId node4 = new NodeId("4");
+
+    private final MastershipInfo mastershipInfo = new MastershipInfo(
+        1,
+        Optional.of(node1),
+        ImmutableMap.<NodeId, MastershipRole>builder()
+            .put(node1, MastershipRole.MASTER)
+            .put(node2, MastershipRole.STANDBY)
+            .put(node3, MastershipRole.STANDBY)
+            .put(node4, MastershipRole.NONE)
+            .build());
+
+    @Test
+    public void testMastershipInfo() throws Exception {
+        assertEquals(1, mastershipInfo.term());
+        assertEquals(node1, mastershipInfo.master().get());
+        assertEquals(Lists.newArrayList(node1), mastershipInfo.getRoles(MastershipRole.MASTER));
+        assertEquals(Lists.newArrayList(node2, node3), mastershipInfo.backups());
+        assertEquals(Lists.newArrayList(node2, node3), mastershipInfo.getRoles(MastershipRole.STANDBY));
+        assertEquals(Lists.newArrayList(node4), mastershipInfo.getRoles(MastershipRole.NONE));
+    }
+
+    @Test
+    public void testEquals() throws Exception {
+        assertEquals(mastershipInfo, mastershipInfo);
+        assertNotEquals(mastershipInfo, new MastershipInfo(1, Optional.of(node1), ImmutableMap.of()));
+    }
+}
diff --git a/core/api/src/test/java/org/onosproject/mastership/MastershipServiceAdapter.java b/core/api/src/test/java/org/onosproject/mastership/MastershipServiceAdapter.java
index a0b203f..d9a2971 100644
--- a/core/api/src/test/java/org/onosproject/mastership/MastershipServiceAdapter.java
+++ b/core/api/src/test/java/org/onosproject/mastership/MastershipServiceAdapter.java
@@ -48,6 +48,11 @@
     }
 
     @Override
+    public MastershipInfo getMastershipFor(DeviceId deviceId) {
+        return null;
+    }
+
+    @Override
     public Set<DeviceId> getDevicesOf(NodeId nodeId) {
         return null;
     }