Ayaka Koshibe

MasteshipService, store, and CLI commands use RoleInfo

Change-Id: Ibc569498a67d33d088e5c9f89c6bb1f45eadc26e
...@@ -7,6 +7,7 @@ import java.util.List; ...@@ -7,6 +7,7 @@ import java.util.List;
7 7
8 import org.apache.karaf.shell.commands.Command; 8 import org.apache.karaf.shell.commands.Command;
9 import org.onlab.onos.cluster.NodeId; 9 import org.onlab.onos.cluster.NodeId;
10 +import org.onlab.onos.cluster.RoleInfo;
10 import org.onlab.onos.mastership.MastershipService; 11 import org.onlab.onos.mastership.MastershipService;
11 import org.onlab.onos.net.Device; 12 import org.onlab.onos.net.Device;
12 import org.onlab.onos.net.DeviceId; 13 import org.onlab.onos.net.DeviceId;
...@@ -20,8 +21,7 @@ import org.onlab.onos.net.device.DeviceService; ...@@ -20,8 +21,7 @@ import org.onlab.onos.net.device.DeviceService;
20 description = "Lists mastership roles of nodes for each device.") 21 description = "Lists mastership roles of nodes for each device.")
21 public class RolesCommand extends AbstractShellCommand { 22 public class RolesCommand extends AbstractShellCommand {
22 23
23 - private static final String FMT_HDR = "%s: master=%s\nstandbys: %s nodes"; 24 + private static final String FMT_HDR = "%s: master=%s, standbys=%s";
24 - private static final String FMT_SB = "\t%s";
25 25
26 @Override 26 @Override
27 protected void execute() { 27 protected void execute() {
...@@ -53,22 +53,14 @@ public class RolesCommand extends AbstractShellCommand { ...@@ -53,22 +53,14 @@ public class RolesCommand extends AbstractShellCommand {
53 * @param master the current master 53 * @param master the current master
54 */ 54 */
55 protected void printRoles(MastershipService service, DeviceId deviceId) { 55 protected void printRoles(MastershipService service, DeviceId deviceId) {
56 - List<NodeId> nodes = service.getNodesFor(deviceId); 56 + RoleInfo nodes = service.getNodesFor(deviceId);
57 - NodeId first = null; 57 + StringBuilder builder = new StringBuilder();
58 - NodeId master = null; 58 + for (NodeId nid : nodes.backups()) {
59 - 59 + builder.append(nid).append(" ");
60 - if (!nodes.isEmpty()) {
61 - first = nodes.get(0);
62 - }
63 - if (first != null &&
64 - first.equals(service.getMasterFor(deviceId))) {
65 - master = nodes.get(0);
66 - nodes.remove(master);
67 } 60 }
68 - print(FMT_HDR, deviceId, master == null ? "NONE" : master, nodes.size());
69 61
70 - for (NodeId nid : nodes) { 62 + print(FMT_HDR, deviceId,
71 - print(FMT_SB, nid); 63 + nodes.master() == null ? "NONE" : nodes.master(),
72 - } 64 + builder.toString());
73 } 65 }
74 } 66 }
......
1 -package org.onlab.onos.store.common; 1 +package org.onlab.onos.cluster;
2 2
3 import java.util.Collections; 3 import java.util.Collections;
4 import java.util.LinkedList; 4 import java.util.LinkedList;
5 import java.util.List; 5 import java.util.List;
6 import java.util.Objects; 6 import java.util.Objects;
7 7
8 -import org.onlab.onos.cluster.NodeId;
9 -
10 import static com.google.common.base.Preconditions.checkNotNull; 8 import static com.google.common.base.Preconditions.checkNotNull;
11 9
12 /** 10 /**
......
...@@ -56,7 +56,10 @@ public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceI ...@@ -56,7 +56,10 @@ public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceI
56 } 56 }
57 57
58 /** 58 /**
59 - * Returns the NodeID of the node responsible for triggering the event. 59 + * Returns the NodeID of the node associated with the event.
60 + * For MASTER_CHANGED this is the newly elected master, and for
61 + * BACKUPS_CHANGED, this is the node that was newly added, removed, or
62 + * whose position was changed in the list.
60 * 63 *
61 * @return node ID as a subject 64 * @return node ID as a subject
62 */ 65 */
......
1 package org.onlab.onos.mastership; 1 package org.onlab.onos.mastership;
2 2
3 -import java.util.List;
4 import java.util.Set; 3 import java.util.Set;
5 4
6 import org.onlab.onos.cluster.NodeId; 5 import org.onlab.onos.cluster.NodeId;
6 +import org.onlab.onos.cluster.RoleInfo;
7 import org.onlab.onos.net.DeviceId; 7 import org.onlab.onos.net.DeviceId;
8 import org.onlab.onos.net.MastershipRole; 8 import org.onlab.onos.net.MastershipRole;
9 9
...@@ -57,7 +57,7 @@ public interface MastershipService { ...@@ -57,7 +57,7 @@ public interface MastershipService {
57 * @param deviceId the identifier of the device 57 * @param deviceId the identifier of the device
58 * @return a list of controller IDs 58 * @return a list of controller IDs
59 */ 59 */
60 - List<NodeId> getNodesFor(DeviceId deviceId); 60 + RoleInfo getNodesFor(DeviceId deviceId);
61 61
62 /** 62 /**
63 * Returns the devices for which a controller is master. 63 * Returns the devices for which a controller is master.
......
1 package org.onlab.onos.mastership; 1 package org.onlab.onos.mastership;
2 2
3 -import java.util.List;
4 import java.util.Set; 3 import java.util.Set;
5 4
6 import org.onlab.onos.cluster.NodeId; 5 import org.onlab.onos.cluster.NodeId;
6 +import org.onlab.onos.cluster.RoleInfo;
7 import org.onlab.onos.net.DeviceId; 7 import org.onlab.onos.net.DeviceId;
8 import org.onlab.onos.net.MastershipRole; 8 import org.onlab.onos.net.MastershipRole;
9 import org.onlab.onos.store.Store; 9 import org.onlab.onos.store.Store;
...@@ -42,13 +42,12 @@ public interface MastershipStore extends Store<MastershipEvent, MastershipStoreD ...@@ -42,13 +42,12 @@ public interface MastershipStore extends Store<MastershipEvent, MastershipStoreD
42 NodeId getMaster(DeviceId deviceId); 42 NodeId getMaster(DeviceId deviceId);
43 43
44 /** 44 /**
45 - * Returns the controllers connected to a device, in mastership- 45 + * Returns the master and backup nodes for a device.
46 - * preference order.
47 * 46 *
48 * @param deviceId the device identifier 47 * @param deviceId the device identifier
49 - * @return an ordered list of controller IDs 48 + * @return a RoleInfo containing controller IDs
50 */ 49 */
51 - List<NodeId> getNodes(DeviceId deviceId); 50 + RoleInfo getNodes(DeviceId deviceId);
52 51
53 /** 52 /**
54 * Returns the devices that a controller instance is master of. 53 * Returns the devices that a controller instance is master of.
......
1 -package org.onlab.onos.store.common; 1 +package org.onlab.onos.cluster;
2 2
3 import java.util.List; 3 import java.util.List;
4 4
5 import org.junit.Test; 5 import org.junit.Test;
6 -import org.onlab.onos.cluster.NodeId;
7 6
8 import com.google.common.collect.Lists; 7 import com.google.common.collect.Lists;
9 8
...@@ -29,7 +28,6 @@ public class RoleInfoTest { ...@@ -29,7 +28,6 @@ public class RoleInfoTest {
29 @Test 28 @Test
30 public void basics() { 29 public void basics() {
31 assertEquals("wrong master", new NodeId("n1"), RI1.master()); 30 assertEquals("wrong master", new NodeId("n1"), RI1.master());
32 - System.out.println(RI1.toString());
33 assertEquals("wrong Backups", RI1.backups(), Lists.newArrayList(N2, N3)); 31 assertEquals("wrong Backups", RI1.backups(), Lists.newArrayList(N2, N3));
34 32
35 assertNotEquals("equals() broken", RI1, RI2); 33 assertNotEquals("equals() broken", RI1, RI2);
......
1 package org.onlab.onos.mastership; 1 package org.onlab.onos.mastership;
2 2
3 import org.onlab.onos.cluster.NodeId; 3 import org.onlab.onos.cluster.NodeId;
4 +import org.onlab.onos.cluster.RoleInfo;
4 import org.onlab.onos.net.DeviceId; 5 import org.onlab.onos.net.DeviceId;
5 import org.onlab.onos.net.MastershipRole; 6 import org.onlab.onos.net.MastershipRole;
6 7
7 -import java.util.List;
8 import java.util.Set; 8 import java.util.Set;
9 9
10 /** 10 /**
...@@ -49,7 +49,7 @@ public class MastershipServiceAdapter implements MastershipService { ...@@ -49,7 +49,7 @@ public class MastershipServiceAdapter implements MastershipService {
49 } 49 }
50 50
51 @Override 51 @Override
52 - public List<NodeId> getNodesFor(DeviceId deviceId) { 52 + public RoleInfo getNodesFor(DeviceId deviceId) {
53 return null; 53 return null;
54 } 54 }
55 } 55 }
......
...@@ -3,7 +3,6 @@ package org.onlab.onos.cluster.impl; ...@@ -3,7 +3,6 @@ package org.onlab.onos.cluster.impl;
3 import static com.google.common.base.Preconditions.checkNotNull; 3 import static com.google.common.base.Preconditions.checkNotNull;
4 import static org.slf4j.LoggerFactory.getLogger; 4 import static org.slf4j.LoggerFactory.getLogger;
5 5
6 -import java.util.List;
7 import java.util.Set; 6 import java.util.Set;
8 import java.util.concurrent.atomic.AtomicInteger; 7 import java.util.concurrent.atomic.AtomicInteger;
9 8
...@@ -18,6 +17,7 @@ import org.onlab.onos.cluster.ClusterEventListener; ...@@ -18,6 +17,7 @@ import org.onlab.onos.cluster.ClusterEventListener;
18 import org.onlab.onos.cluster.ClusterService; 17 import org.onlab.onos.cluster.ClusterService;
19 import org.onlab.onos.cluster.ControllerNode; 18 import org.onlab.onos.cluster.ControllerNode;
20 import org.onlab.onos.cluster.NodeId; 19 import org.onlab.onos.cluster.NodeId;
20 +import org.onlab.onos.cluster.RoleInfo;
21 import org.onlab.onos.event.AbstractListenerRegistry; 21 import org.onlab.onos.event.AbstractListenerRegistry;
22 import org.onlab.onos.event.EventDeliveryService; 22 import org.onlab.onos.event.EventDeliveryService;
23 import org.onlab.onos.mastership.MastershipAdminService; 23 import org.onlab.onos.mastership.MastershipAdminService;
...@@ -128,7 +128,7 @@ implements MastershipService, MastershipAdminService { ...@@ -128,7 +128,7 @@ implements MastershipService, MastershipAdminService {
128 } 128 }
129 129
130 @Override 130 @Override
131 - public List<NodeId> getNodesFor(DeviceId deviceId) { 131 + public RoleInfo getNodesFor(DeviceId deviceId) {
132 checkNotNull(deviceId, DEVICE_ID_NULL); 132 checkNotNull(deviceId, DEVICE_ID_NULL);
133 return store.getNodes(deviceId); 133 return store.getNodes(deviceId);
134 } 134 }
......
...@@ -2,9 +2,6 @@ package org.onlab.onos.store.mastership.impl; ...@@ -2,9 +2,6 @@ package org.onlab.onos.store.mastership.impl;
2 2
3 import static org.onlab.onos.mastership.MastershipEvent.Type.MASTER_CHANGED; 3 import static org.onlab.onos.mastership.MastershipEvent.Type.MASTER_CHANGED;
4 4
5 -import java.util.Collections;
6 -import java.util.LinkedList;
7 -import java.util.List;
8 import java.util.Map; 5 import java.util.Map;
9 import java.util.Set; 6 import java.util.Set;
10 7
...@@ -16,6 +13,7 @@ import org.apache.felix.scr.annotations.ReferenceCardinality; ...@@ -16,6 +13,7 @@ import org.apache.felix.scr.annotations.ReferenceCardinality;
16 import org.apache.felix.scr.annotations.Service; 13 import org.apache.felix.scr.annotations.Service;
17 import org.onlab.onos.cluster.ClusterService; 14 import org.onlab.onos.cluster.ClusterService;
18 import org.onlab.onos.cluster.NodeId; 15 import org.onlab.onos.cluster.NodeId;
16 +import org.onlab.onos.cluster.RoleInfo;
19 import org.onlab.onos.mastership.MastershipEvent; 17 import org.onlab.onos.mastership.MastershipEvent;
20 import org.onlab.onos.mastership.MastershipStore; 18 import org.onlab.onos.mastership.MastershipStore;
21 import org.onlab.onos.mastership.MastershipStoreDelegate; 19 import org.onlab.onos.mastership.MastershipStoreDelegate;
...@@ -161,20 +159,11 @@ implements MastershipStore { ...@@ -161,20 +159,11 @@ implements MastershipStore {
161 159
162 160
163 @Override 161 @Override
164 - public List<NodeId> getNodes(DeviceId deviceId) { 162 + public RoleInfo getNodes(DeviceId deviceId) {
165 - List<NodeId> nodes = new LinkedList<>();
166 -
167 - //add current master to head - if there is one.
168 roleMap.lock(deviceId); 163 roleMap.lock(deviceId);
169 try { 164 try {
170 RoleValue rv = getRoleValue(deviceId); 165 RoleValue rv = getRoleValue(deviceId);
171 - NodeId master = rv.get(MASTER); 166 + return rv.roleInfo();
172 - if (master != null) {
173 - nodes.add(master);
174 - }
175 - //We ignore NONE nodes.
176 - nodes.addAll(rv.nodesOfRole(STANDBY));
177 - return Collections.unmodifiableList(nodes);
178 } finally { 167 } finally {
179 roleMap.unlock(deviceId); 168 roleMap.unlock(deviceId);
180 } 169 }
......
...@@ -7,8 +7,8 @@ import java.util.List; ...@@ -7,8 +7,8 @@ import java.util.List;
7 import java.util.Map; 7 import java.util.Map;
8 8
9 import org.onlab.onos.cluster.NodeId; 9 import org.onlab.onos.cluster.NodeId;
10 +import org.onlab.onos.cluster.RoleInfo;
10 import org.onlab.onos.net.MastershipRole; 11 import org.onlab.onos.net.MastershipRole;
11 -import org.onlab.onos.store.common.RoleInfo;
12 12
13 /** 13 /**
14 * A structure that holds node mastership roles associated with a 14 * A structure that holds node mastership roles associated with a
......
...@@ -18,6 +18,7 @@ import org.apache.felix.scr.annotations.Service; ...@@ -18,6 +18,7 @@ import org.apache.felix.scr.annotations.Service;
18 import org.onlab.onos.cluster.ControllerNode; 18 import org.onlab.onos.cluster.ControllerNode;
19 import org.onlab.onos.cluster.DefaultControllerNode; 19 import org.onlab.onos.cluster.DefaultControllerNode;
20 import org.onlab.onos.cluster.NodeId; 20 import org.onlab.onos.cluster.NodeId;
21 +import org.onlab.onos.cluster.RoleInfo;
21 import org.onlab.onos.mastership.MastershipEvent; 22 import org.onlab.onos.mastership.MastershipEvent;
22 import org.onlab.onos.mastership.MastershipStore; 23 import org.onlab.onos.mastership.MastershipStore;
23 import org.onlab.onos.mastership.MastershipStoreDelegate; 24 import org.onlab.onos.mastership.MastershipStoreDelegate;
...@@ -97,15 +98,11 @@ public class SimpleMastershipStore ...@@ -97,15 +98,11 @@ public class SimpleMastershipStore
97 } 98 }
98 99
99 @Override 100 @Override
100 - public List<NodeId> getNodes(DeviceId deviceId) { 101 + public RoleInfo getNodes(DeviceId deviceId) {
101 List<NodeId> nodes = new ArrayList<>(); 102 List<NodeId> nodes = new ArrayList<>();
102 -
103 nodes.addAll(backups); 103 nodes.addAll(backups);
104 - if (!nodes.contains(masterMap.get(deviceId))) {
105 - nodes.add(masterMap.get(deviceId));
106 - }
107 104
108 - return Collections.unmodifiableList(nodes); 105 + return new RoleInfo(masterMap.get(deviceId), nodes);
109 } 106 }
110 107
111 @Override 108 @Override
......
...@@ -9,6 +9,7 @@ import org.junit.After; ...@@ -9,6 +9,7 @@ import org.junit.After;
9 import org.junit.Before; 9 import org.junit.Before;
10 import org.junit.Test; 10 import org.junit.Test;
11 import org.onlab.onos.cluster.NodeId; 11 import org.onlab.onos.cluster.NodeId;
12 +import org.onlab.onos.cluster.RoleInfo;
12 import org.onlab.onos.mastership.MastershipListener; 13 import org.onlab.onos.mastership.MastershipListener;
13 import org.onlab.onos.mastership.MastershipService; 14 import org.onlab.onos.mastership.MastershipService;
14 import org.onlab.onos.mastership.MastershipTermService; 15 import org.onlab.onos.mastership.MastershipTermService;
...@@ -481,8 +482,8 @@ public class LLDPLinkProviderTest { ...@@ -481,8 +482,8 @@ public class LLDPLinkProviderTest {
481 } 482 }
482 483
483 @Override 484 @Override
484 - public List<NodeId> getNodesFor(DeviceId deviceId) { 485 + public RoleInfo getNodesFor(DeviceId deviceId) {
485 - return Collections.emptyList(); 486 + return new RoleInfo(new NodeId("foo"), Collections.<NodeId>emptyList());
486 } 487 }
487 } 488 }
488 489
......