Add atomic mastership/term/backups method to MastershipService
Change-Id: I18c3aeaa5101c9ce08ff38fffd70eaec903a0f3e
diff --git a/core/api/src/main/java/org/onosproject/mastership/MastershipEvent.java b/core/api/src/main/java/org/onosproject/mastership/MastershipEvent.java
index db5ba74..e7c0169 100644
--- a/core/api/src/main/java/org/onosproject/mastership/MastershipEvent.java
+++ b/core/api/src/main/java/org/onosproject/mastership/MastershipEvent.java
@@ -29,9 +29,6 @@
*/
public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceId> {
- //Contains master and standby information.
- RoleInfo roleInfo;
-
/**
* Type of mastership events.
*/
@@ -55,45 +52,58 @@
SUSPENDED
}
+ private final MastershipInfo mastershipInfo;
+
/**
* Creates an event of a given type and for the specified device,
* role information, and the current time.
*
- * @param type mastership event type
- * @param device event device subject
- * @param info mastership role information
+ * @param type mastership event type
+ * @param device event device subject
+ * @param mastershipInfo mastership info
*/
- public MastershipEvent(Type type, DeviceId device, RoleInfo info) {
+ public MastershipEvent(Type type, DeviceId device, MastershipInfo mastershipInfo) {
super(type, device);
- this.roleInfo = info;
+ this.mastershipInfo = mastershipInfo;
}
/**
* Creates an event of a given type and for the specified device, master,
* and time.
*
- * @param type mastership event type
- * @param device event device subject
- * @param info role information
- * @param time occurrence time
+ * @param type mastership event type
+ * @param device event device subject
+ * @param mastershipInfo mastership information
+ * @param time occurrence time
*/
- public MastershipEvent(Type type, DeviceId device, RoleInfo info, long time) {
+ public MastershipEvent(Type type, DeviceId device, MastershipInfo mastershipInfo, long time) {
super(type, device, time);
- this.roleInfo = info;
+ this.mastershipInfo = mastershipInfo;
+ }
+
+ /**
+ * Returns the mastership info.
+ *
+ * @return the mastership info
+ */
+ public MastershipInfo mastershipInfo() {
+ return mastershipInfo;
}
/**
* Returns the current role state for the subject.
*
* @return RoleInfo associated with Device ID subject
+ * @deprecated since 1.14
*/
+ @Deprecated
public RoleInfo roleInfo() {
- return roleInfo;
+ return new RoleInfo(mastershipInfo.master().orElse(null), mastershipInfo.backups());
}
@Override
public int hashCode() {
- return Objects.hash(type(), subject(), roleInfo(), time());
+ return Objects.hash(type(), subject(), mastershipInfo(), time());
}
@Override
@@ -105,7 +115,7 @@
final MastershipEvent other = (MastershipEvent) obj;
return Objects.equals(this.type(), other.type()) &&
Objects.equals(this.subject(), other.subject()) &&
- Objects.equals(this.roleInfo(), other.roleInfo()) &&
+ Objects.equals(this.mastershipInfo(), other.mastershipInfo()) &&
Objects.equals(this.time(), other.time());
}
return false;
@@ -117,7 +127,7 @@
.add("time", Tools.defaultOffsetDataTime(time()))
.add("type", type())
.add("subject", subject())
- .add("roleInfo", roleInfo)
+ .add("mastershipInfo", mastershipInfo())
.toString();
}
}
diff --git a/core/api/src/main/java/org/onosproject/mastership/MastershipInfo.java b/core/api/src/main/java/org/onosproject/mastership/MastershipInfo.java
new file mode 100644
index 0000000..c9a4f0e
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/mastership/MastershipInfo.java
@@ -0,0 +1,124 @@
+/*
+ * 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.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.net.MastershipRole;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+/**
+ * Mastership info.
+ */
+public final class MastershipInfo {
+ private final long term;
+ private final Optional<NodeId> master;
+ private final ImmutableMap<NodeId, MastershipRole> roles;
+
+ public MastershipInfo() {
+ this(0, Optional.empty(), ImmutableMap.of());
+ }
+
+ public MastershipInfo(long term, Optional<NodeId> master, ImmutableMap<NodeId, MastershipRole> roles) {
+ this.term = term;
+ this.master = master;
+ this.roles = roles;
+ }
+
+ /**
+ * Returns the mastership term.
+ *
+ * @return the mastership term
+ */
+ public long term() {
+ return term;
+ }
+
+ /**
+ * Returns the current master.
+ *
+ * @return the current master
+ */
+ public Optional<NodeId> master() {
+ return master;
+ }
+
+ /**
+ * Returns a sorted list of standby nodes.
+ *
+ * @return a sorted list of standby nodes
+ */
+ public List<NodeId> backups() {
+ return getRoles(MastershipRole.STANDBY);
+ }
+
+ /**
+ * Returns the list of nodes with the given role.
+ *
+ * @param role the role by which to filter nodes
+ * @return an immutable list of nodes with the given role sorted in priority order
+ */
+ public List<NodeId> getRoles(MastershipRole role) {
+ return ImmutableList.copyOf(roles.entrySet()
+ .stream()
+ .filter(entry -> entry.getValue() == role)
+ .map(Map.Entry::getKey)
+ .collect(Collectors.toList()));
+ }
+
+ /**
+ * Returns the current role for the given node.
+ *
+ * @param nodeId the node for which to return the current role
+ * @return the current role for the given node
+ */
+ public MastershipRole getRole(NodeId nodeId) {
+ return roles.get(nodeId);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(term, master, roles);
+ }
+
+ @Override
+ public boolean equals(Object object) {
+ if (object instanceof MastershipInfo) {
+ MastershipInfo that = (MastershipInfo) object;
+ return this.term == that.term
+ && Objects.equals(this.master, that.master)
+ && Objects.equals(this.roles, that.roles);
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this)
+ .add("term", term)
+ .add("master", master)
+ .add("roles", roles)
+ .toString();
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/mastership/MastershipService.java b/core/api/src/main/java/org/onosproject/mastership/MastershipService.java
index 86042b0..a5a0c43 100644
--- a/core/api/src/main/java/org/onosproject/mastership/MastershipService.java
+++ b/core/api/src/main/java/org/onosproject/mastership/MastershipService.java
@@ -120,6 +120,14 @@
RoleInfo getNodesFor(DeviceId deviceId);
/**
+ * Returns the mastership info for the given device.
+ *
+ * @param deviceId the device for which to return the mastership info
+ * @return the mastership info for the given device
+ */
+ MastershipInfo getMastershipFor(DeviceId deviceId);
+
+ /**
* Returns the devices for which a controller is master.
* <p>
* Returned Set may contain DeviceId which no longer exist in the system.
diff --git a/core/api/src/main/java/org/onosproject/mastership/MastershipStore.java b/core/api/src/main/java/org/onosproject/mastership/MastershipStore.java
index 3650388..7f43a15 100644
--- a/core/api/src/main/java/org/onosproject/mastership/MastershipStore.java
+++ b/core/api/src/main/java/org/onosproject/mastership/MastershipStore.java
@@ -73,7 +73,6 @@
*/
Set<DeviceId> getDevices(NodeId nodeId);
-
/**
* Sets a device's role for a specified controller instance.
*
@@ -93,6 +92,14 @@
MastershipTerm getTermFor(DeviceId deviceId);
/**
+ * Returns the mastership info for the given device.
+ *
+ * @param deviceId the device for which to return the mastership info
+ * @return the mastership info for the given device
+ */
+ MastershipInfo getMastership(DeviceId deviceId);
+
+ /**
* Sets a controller instance's mastership role to STANDBY for a device.
* If the role is MASTER, another controller instance will be selected
* as a candidate master.