Ayaka Koshibe

MasteshipService, store, and CLI commands use RoleInfo

Change-Id: Ibc569498a67d33d088e5c9f89c6bb1f45eadc26e
......@@ -7,6 +7,7 @@ import java.util.List;
import org.apache.karaf.shell.commands.Command;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.cluster.RoleInfo;
import org.onlab.onos.mastership.MastershipService;
import org.onlab.onos.net.Device;
import org.onlab.onos.net.DeviceId;
......@@ -20,8 +21,7 @@ import org.onlab.onos.net.device.DeviceService;
description = "Lists mastership roles of nodes for each device.")
public class RolesCommand extends AbstractShellCommand {
private static final String FMT_HDR = "%s: master=%s\nstandbys: %s nodes";
private static final String FMT_SB = "\t%s";
private static final String FMT_HDR = "%s: master=%s, standbys=%s";
@Override
protected void execute() {
......@@ -53,22 +53,14 @@ public class RolesCommand extends AbstractShellCommand {
* @param master the current master
*/
protected void printRoles(MastershipService service, DeviceId deviceId) {
List<NodeId> nodes = service.getNodesFor(deviceId);
NodeId first = null;
NodeId master = null;
if (!nodes.isEmpty()) {
first = nodes.get(0);
}
if (first != null &&
first.equals(service.getMasterFor(deviceId))) {
master = nodes.get(0);
nodes.remove(master);
RoleInfo nodes = service.getNodesFor(deviceId);
StringBuilder builder = new StringBuilder();
for (NodeId nid : nodes.backups()) {
builder.append(nid).append(" ");
}
print(FMT_HDR, deviceId, master == null ? "NONE" : master, nodes.size());
for (NodeId nid : nodes) {
print(FMT_SB, nid);
}
print(FMT_HDR, deviceId,
nodes.master() == null ? "NONE" : nodes.master(),
builder.toString());
}
}
......
package org.onlab.onos.store.common;
package org.onlab.onos.cluster;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import org.onlab.onos.cluster.NodeId;
import static com.google.common.base.Preconditions.checkNotNull;
/**
......
......@@ -56,7 +56,10 @@ public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceI
}
/**
* Returns the NodeID of the node responsible for triggering the event.
* Returns the NodeID of the node associated with the event.
* For MASTER_CHANGED this is the newly elected master, and for
* BACKUPS_CHANGED, this is the node that was newly added, removed, or
* whose position was changed in the list.
*
* @return node ID as a subject
*/
......
package org.onlab.onos.mastership;
import java.util.List;
import java.util.Set;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.cluster.RoleInfo;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.MastershipRole;
......@@ -57,7 +57,7 @@ public interface MastershipService {
* @param deviceId the identifier of the device
* @return a list of controller IDs
*/
List<NodeId> getNodesFor(DeviceId deviceId);
RoleInfo getNodesFor(DeviceId deviceId);
/**
* Returns the devices for which a controller is master.
......
package org.onlab.onos.mastership;
import java.util.List;
import java.util.Set;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.cluster.RoleInfo;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.MastershipRole;
import org.onlab.onos.store.Store;
......@@ -42,13 +42,12 @@ public interface MastershipStore extends Store<MastershipEvent, MastershipStoreD
NodeId getMaster(DeviceId deviceId);
/**
* Returns the controllers connected to a device, in mastership-
* preference order.
* Returns the master and backup nodes for a device.
*
* @param deviceId the device identifier
* @return an ordered list of controller IDs
* @return a RoleInfo containing controller IDs
*/
List<NodeId> getNodes(DeviceId deviceId);
RoleInfo getNodes(DeviceId deviceId);
/**
* Returns the devices that a controller instance is master of.
......
package org.onlab.onos.store.common;
package org.onlab.onos.cluster;
import java.util.List;
import org.junit.Test;
import org.onlab.onos.cluster.NodeId;
import com.google.common.collect.Lists;
......@@ -29,7 +28,6 @@ public class RoleInfoTest {
@Test
public void basics() {
assertEquals("wrong master", new NodeId("n1"), RI1.master());
System.out.println(RI1.toString());
assertEquals("wrong Backups", RI1.backups(), Lists.newArrayList(N2, N3));
assertNotEquals("equals() broken", RI1, RI2);
......
package org.onlab.onos.mastership;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.cluster.RoleInfo;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.MastershipRole;
import java.util.List;
import java.util.Set;
/**
......@@ -49,7 +49,7 @@ public class MastershipServiceAdapter implements MastershipService {
}
@Override
public List<NodeId> getNodesFor(DeviceId deviceId) {
public RoleInfo getNodesFor(DeviceId deviceId) {
return null;
}
}
......
......@@ -3,7 +3,6 @@ package org.onlab.onos.cluster.impl;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
......@@ -18,6 +17,7 @@ import org.onlab.onos.cluster.ClusterEventListener;
import org.onlab.onos.cluster.ClusterService;
import org.onlab.onos.cluster.ControllerNode;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.cluster.RoleInfo;
import org.onlab.onos.event.AbstractListenerRegistry;
import org.onlab.onos.event.EventDeliveryService;
import org.onlab.onos.mastership.MastershipAdminService;
......@@ -128,7 +128,7 @@ implements MastershipService, MastershipAdminService {
}
@Override
public List<NodeId> getNodesFor(DeviceId deviceId) {
public RoleInfo getNodesFor(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_ID_NULL);
return store.getNodes(deviceId);
}
......
......@@ -2,9 +2,6 @@ package org.onlab.onos.store.mastership.impl;
import static org.onlab.onos.mastership.MastershipEvent.Type.MASTER_CHANGED;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -16,6 +13,7 @@ import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onlab.onos.cluster.ClusterService;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.cluster.RoleInfo;
import org.onlab.onos.mastership.MastershipEvent;
import org.onlab.onos.mastership.MastershipStore;
import org.onlab.onos.mastership.MastershipStoreDelegate;
......@@ -161,20 +159,11 @@ implements MastershipStore {
@Override
public List<NodeId> getNodes(DeviceId deviceId) {
List<NodeId> nodes = new LinkedList<>();
//add current master to head - if there is one.
public RoleInfo getNodes(DeviceId deviceId) {
roleMap.lock(deviceId);
try {
RoleValue rv = getRoleValue(deviceId);
NodeId master = rv.get(MASTER);
if (master != null) {
nodes.add(master);
}
//We ignore NONE nodes.
nodes.addAll(rv.nodesOfRole(STANDBY));
return Collections.unmodifiableList(nodes);
return rv.roleInfo();
} finally {
roleMap.unlock(deviceId);
}
......
......@@ -7,8 +7,8 @@ import java.util.List;
import java.util.Map;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.cluster.RoleInfo;
import org.onlab.onos.net.MastershipRole;
import org.onlab.onos.store.common.RoleInfo;
/**
* A structure that holds node mastership roles associated with a
......
......@@ -18,6 +18,7 @@ import org.apache.felix.scr.annotations.Service;
import org.onlab.onos.cluster.ControllerNode;
import org.onlab.onos.cluster.DefaultControllerNode;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.cluster.RoleInfo;
import org.onlab.onos.mastership.MastershipEvent;
import org.onlab.onos.mastership.MastershipStore;
import org.onlab.onos.mastership.MastershipStoreDelegate;
......@@ -97,15 +98,11 @@ public class SimpleMastershipStore
}
@Override
public List<NodeId> getNodes(DeviceId deviceId) {
public RoleInfo getNodes(DeviceId deviceId) {
List<NodeId> nodes = new ArrayList<>();
nodes.addAll(backups);
if (!nodes.contains(masterMap.get(deviceId))) {
nodes.add(masterMap.get(deviceId));
}
return Collections.unmodifiableList(nodes);
return new RoleInfo(masterMap.get(deviceId), nodes);
}
@Override
......
......@@ -9,6 +9,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.cluster.RoleInfo;
import org.onlab.onos.mastership.MastershipListener;
import org.onlab.onos.mastership.MastershipService;
import org.onlab.onos.mastership.MastershipTermService;
......@@ -481,8 +482,8 @@ public class LLDPLinkProviderTest {
}
@Override
public List<NodeId> getNodesFor(DeviceId deviceId) {
return Collections.emptyList();
public RoleInfo getNodesFor(DeviceId deviceId) {
return new RoleInfo(new NodeId("foo"), Collections.<NodeId>emptyList());
}
}
......