Ayaka Koshibe

mastership manager listens to cluster events

Change-Id: I925967ef8ada88b9116d292d623d2f0f4cda3a8d
...@@ -56,7 +56,8 @@ public interface MastershipService { ...@@ -56,7 +56,8 @@ public interface MastershipService {
56 Set<DeviceId> getDevicesOf(NodeId nodeId); 56 Set<DeviceId> getDevicesOf(NodeId nodeId);
57 57
58 /** 58 /**
59 - * Returns the mastership term service for getting term information. 59 + * Returns the mastership term service for getting read-only
60 + * term information.
60 * 61 *
61 * @return the MastershipTermService for this mastership manager 62 * @return the MastershipTermService for this mastership manager
62 */ 63 */
......
1 +package org.onlab.onos.cluster;
2 +
3 +import static org.junit.Assert.assertEquals;
4 +
5 +import org.junit.Test;
6 +
7 +import com.google.common.testing.EqualsTester;
8 +
9 +public class MastershipTermTest {
10 +
11 + private static final NodeId N1 = new NodeId("foo");
12 + private static final NodeId N2 = new NodeId("bar");
13 +
14 + private static final MastershipTerm TERM1 = MastershipTerm.of(N1, 0);
15 + private static final MastershipTerm TERM2 = MastershipTerm.of(N2, 1);
16 + private static final MastershipTerm TERM3 = MastershipTerm.of(N2, 1);
17 + private static final MastershipTerm TERM4 = MastershipTerm.of(N1, 1);
18 +
19 + @Test
20 + public void basics() {
21 + assertEquals("incorrect term number", 0, TERM1.termNumber());
22 + assertEquals("incorrect master", new NodeId("foo"), TERM1.master());
23 + }
24 +
25 + @Test
26 + public void testEquality() {
27 + new EqualsTester().addEqualityGroup(MastershipTerm.of(N1, 0), TERM1)
28 + .addEqualityGroup(TERM2, TERM3)
29 + .addEqualityGroup(TERM4);
30 + }
31 +
32 +}
...@@ -6,6 +6,8 @@ import org.apache.felix.scr.annotations.Deactivate; ...@@ -6,6 +6,8 @@ import org.apache.felix.scr.annotations.Deactivate;
6 import org.apache.felix.scr.annotations.Reference; 6 import org.apache.felix.scr.annotations.Reference;
7 import org.apache.felix.scr.annotations.ReferenceCardinality; 7 import org.apache.felix.scr.annotations.ReferenceCardinality;
8 import org.apache.felix.scr.annotations.Service; 8 import org.apache.felix.scr.annotations.Service;
9 +import org.onlab.onos.cluster.ClusterEvent;
10 +import org.onlab.onos.cluster.ClusterEventListener;
9 import org.onlab.onos.cluster.ClusterService; 11 import org.onlab.onos.cluster.ClusterService;
10 import org.onlab.onos.cluster.MastershipAdminService; 12 import org.onlab.onos.cluster.MastershipAdminService;
11 import org.onlab.onos.cluster.MastershipEvent; 13 import org.onlab.onos.cluster.MastershipEvent;
...@@ -49,15 +51,19 @@ public class MastershipManager ...@@ -49,15 +51,19 @@ public class MastershipManager
49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected ClusterService clusterService; 52 protected ClusterService clusterService;
51 53
54 + private ClusterEventListener clusterListener = new InternalClusterEventListener();
55 +
52 @Activate 56 @Activate
53 public void activate() { 57 public void activate() {
54 eventDispatcher.addSink(MastershipEvent.class, listenerRegistry); 58 eventDispatcher.addSink(MastershipEvent.class, listenerRegistry);
59 + clusterService.addListener(clusterListener);
55 log.info("Started"); 60 log.info("Started");
56 } 61 }
57 62
58 @Deactivate 63 @Deactivate
59 public void deactivate() { 64 public void deactivate() {
60 eventDispatcher.removeSink(MastershipEvent.class); 65 eventDispatcher.removeSink(MastershipEvent.class);
66 + clusterService.removeListener(clusterListener);
61 log.info("Stopped"); 67 log.info("Stopped");
62 } 68 }
63 69
...@@ -141,4 +147,27 @@ public class MastershipManager ...@@ -141,4 +147,27 @@ public class MastershipManager
141 147
142 } 148 }
143 149
150 + //callback for reacting to cluster events
151 + private class InternalClusterEventListener implements ClusterEventListener {
152 +
153 + @Override
154 + public void event(ClusterEvent event) {
155 + switch (event.type()) {
156 + //FIXME: worry about addition when the time comes
157 + case INSTANCE_ADDED:
158 + case INSTANCE_ACTIVATED:
159 + break;
160 + case INSTANCE_REMOVED:
161 + case INSTANCE_DEACTIVATED:
162 + for (DeviceId d : getDevicesOf(event.subject().id())) {
163 + //this method should be an admin iface?
164 + relinquishMastership(d);
165 + }
166 + break;
167 + default:
168 + log.warn("unknown cluster event {}", event);
169 + }
170 + }
171 +
172 + }
144 } 173 }
......
...@@ -10,6 +10,7 @@ import org.onlab.onos.cluster.ClusterService; ...@@ -10,6 +10,7 @@ import org.onlab.onos.cluster.ClusterService;
10 import org.onlab.onos.cluster.MastershipEvent; 10 import org.onlab.onos.cluster.MastershipEvent;
11 import org.onlab.onos.cluster.MastershipListener; 11 import org.onlab.onos.cluster.MastershipListener;
12 import org.onlab.onos.cluster.MastershipService; 12 import org.onlab.onos.cluster.MastershipService;
13 +import org.onlab.onos.cluster.MastershipTermService;
13 import org.onlab.onos.event.AbstractListenerRegistry; 14 import org.onlab.onos.event.AbstractListenerRegistry;
14 import org.onlab.onos.event.EventDeliveryService; 15 import org.onlab.onos.event.EventDeliveryService;
15 import org.onlab.onos.net.Device; 16 import org.onlab.onos.net.Device;
...@@ -74,11 +75,14 @@ public class DeviceManager ...@@ -74,11 +75,14 @@ public class DeviceManager
74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected MastershipService mastershipService; 76 protected MastershipService mastershipService;
76 77
78 + protected MastershipTermService termService;
79 +
77 @Activate 80 @Activate
78 public void activate() { 81 public void activate() {
79 store.setDelegate(delegate); 82 store.setDelegate(delegate);
80 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); 83 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
81 mastershipService.addListener(mastershipListener); 84 mastershipService.addListener(mastershipListener);
85 + termService = mastershipService.requestTermService();
82 log.info("Started"); 86 log.info("Started");
83 } 87 }
84 88
......
...@@ -65,8 +65,8 @@ public class DefaultTopologyProvider extends AbstractProvider ...@@ -65,8 +65,8 @@ public class DefaultTopologyProvider extends AbstractProvider
65 private volatile boolean isStarted = false; 65 private volatile boolean isStarted = false;
66 66
67 private TopologyProviderService providerService; 67 private TopologyProviderService providerService;
68 - private DeviceListener deviceListener = new InnerDeviceListener(); 68 + private DeviceListener deviceListener = new InternalDeviceListener();
69 - private LinkListener linkListener = new InnerLinkListener(); 69 + private LinkListener linkListener = new InternalLinkListener();
70 70
71 private EventAccumulator accumulator; 71 private EventAccumulator accumulator;
72 private ExecutorService executor; 72 private ExecutorService executor;
...@@ -132,7 +132,7 @@ public class DefaultTopologyProvider extends AbstractProvider ...@@ -132,7 +132,7 @@ public class DefaultTopologyProvider extends AbstractProvider
132 } 132 }
133 133
134 // Callback for device events 134 // Callback for device events
135 - private class InnerDeviceListener implements DeviceListener { 135 + private class InternalDeviceListener implements DeviceListener {
136 @Override 136 @Override
137 public void event(DeviceEvent event) { 137 public void event(DeviceEvent event) {
138 DeviceEvent.Type type = event.type(); 138 DeviceEvent.Type type = event.type();
...@@ -144,7 +144,7 @@ public class DefaultTopologyProvider extends AbstractProvider ...@@ -144,7 +144,7 @@ public class DefaultTopologyProvider extends AbstractProvider
144 } 144 }
145 145
146 // Callback for link events 146 // Callback for link events
147 - private class InnerLinkListener implements LinkListener { 147 + private class InternalLinkListener implements LinkListener {
148 @Override 148 @Override
149 public void event(LinkEvent event) { 149 public void event(LinkEvent event) {
150 accumulator.add(event); 150 accumulator.add(event);
......
...@@ -7,8 +7,6 @@ import java.util.HashMap; ...@@ -7,8 +7,6 @@ import java.util.HashMap;
7 import java.util.HashSet; 7 import java.util.HashSet;
8 import java.util.Map; 8 import java.util.Map;
9 import java.util.Set; 9 import java.util.Set;
10 -import java.util.concurrent.ConcurrentHashMap;
11 -import java.util.concurrent.ConcurrentMap;
12 import java.util.concurrent.atomic.AtomicInteger; 10 import java.util.concurrent.atomic.AtomicInteger;
13 11
14 import org.apache.felix.scr.annotations.Activate; 12 import org.apache.felix.scr.annotations.Activate;
...@@ -48,9 +46,9 @@ public class SimpleMastershipStore ...@@ -48,9 +46,9 @@ public class SimpleMastershipStore
48 new DefaultControllerNode(new NodeId("local"), LOCALHOST); 46 new DefaultControllerNode(new NodeId("local"), LOCALHOST);
49 47
50 //devices mapped to their masters, to emulate multiple nodes 48 //devices mapped to their masters, to emulate multiple nodes
51 - protected final ConcurrentMap<DeviceId, NodeId> masterMap = 49 + protected final Map<DeviceId, NodeId> masterMap = new HashMap<>();
52 - new ConcurrentHashMap<>();
53 protected final Map<DeviceId, AtomicInteger> termMap = new HashMap<>(); 50 protected final Map<DeviceId, AtomicInteger> termMap = new HashMap<>();
51 + protected final Set<NodeId> masters = new HashSet<>();
54 52
55 @Activate 53 @Activate
56 public void activate() { 54 public void activate() {
......