Ayaka Koshibe

began implementing MastershipManager

Change-Id: I65b86dcfa062e4cfd74de588f4714b35e9d329f0
1 +package org.onlab.onos.cluster;
2 +
3 +import org.onlab.onos.net.provider.Provider;
4 +
5 +public interface MastershipProvider extends Provider {
6 +
7 +}
1 +package org.onlab.onos.cluster;
2 +
3 +import org.onlab.onos.net.DeviceId;
4 +import org.onlab.onos.net.MastershipRole;
5 +import org.onlab.onos.net.provider.ProviderService;
6 +
7 +public interface MastershipProviderService extends
8 + ProviderService<MastershipProvider> {
9 +
10 + /**
11 + * Signals the core that mastership has changed for a device.
12 + *
13 + * @param deviceId the device ID
14 + * @param role the new mastership role of this controller instance
15 + */
16 + void roleChanged(DeviceId deviceId, MastershipRole role);
17 +
18 +}
...@@ -25,7 +25,7 @@ public interface MastershipStore { ...@@ -25,7 +25,7 @@ public interface MastershipStore {
25 MastershipRole role); 25 MastershipRole role);
26 26
27 /** 27 /**
28 - * Adds or updates the mastership information for a device. 28 + * Adds or updates mastership information for a device.
29 * 29 *
30 * @param instance controller instance identifier 30 * @param instance controller instance identifier
31 * @param deviceId device identifier 31 * @param deviceId device identifier
......
1 +package org.onlab.onos.cluster.impl;
2 +
3 +import static org.slf4j.LoggerFactory.getLogger;
4 +
5 +import java.util.Set;
6 +
7 +import org.apache.felix.scr.annotations.Activate;
8 +import org.apache.felix.scr.annotations.Deactivate;
9 +import org.apache.felix.scr.annotations.Reference;
10 +import org.apache.felix.scr.annotations.ReferenceCardinality;
11 +import org.onlab.onos.cluster.MastershipAdminService;
12 +import org.onlab.onos.cluster.MastershipEvent;
13 +import org.onlab.onos.cluster.MastershipListener;
14 +import org.onlab.onos.cluster.MastershipProvider;
15 +import org.onlab.onos.cluster.MastershipProviderService;
16 +import org.onlab.onos.cluster.MastershipService;
17 +import org.onlab.onos.cluster.MastershipStore;
18 +import org.onlab.onos.cluster.NodeId;
19 +import org.onlab.onos.event.AbstractListenerRegistry;
20 +import org.onlab.onos.event.EventDeliveryService;
21 +import org.onlab.onos.net.DeviceId;
22 +import org.onlab.onos.net.MastershipRole;
23 +import org.onlab.onos.net.provider.AbstractProviderService;
24 +import org.slf4j.Logger;
25 +
26 +import static com.google.common.base.Preconditions.checkNotNull;
27 +
28 +public class MastershipManager implements MastershipService,
29 + MastershipAdminService {
30 +
31 + private static final String NODE_ID_NULL = "Node ID cannot be null";
32 + private static final String DEVICE_ID_NULL = "Device ID cannot be null";
33 + private static final String ROLE_NULL = "Mastership role cannot be null";
34 +
35 + private final Logger log = getLogger(getClass());
36 +
37 + protected final AbstractListenerRegistry<MastershipEvent, MastershipListener>
38 + listenerRegistry = new AbstractListenerRegistry<>();
39 +
40 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
41 + protected MastershipStore store;
42 +
43 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
44 + protected EventDeliveryService eventDispatcher;
45 +
46 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 + protected ClusterManager clusterManager;
48 +
49 + @Activate
50 + public void activate() {
51 + eventDispatcher.addSink(MastershipEvent.class, listenerRegistry);
52 + log.info("Started");
53 + }
54 +
55 + @Deactivate
56 + public void deactivate() {
57 + eventDispatcher.removeSink(MastershipEvent.class);
58 + log.info("Stopped");
59 + }
60 +
61 +
62 + @Override
63 + public void setRole(NodeId nodeId, DeviceId deviceId, MastershipRole role) {
64 + checkNotNull(nodeId, NODE_ID_NULL);
65 + checkNotNull(deviceId, DEVICE_ID_NULL);
66 + checkNotNull(role, ROLE_NULL);
67 + store.setRole(nodeId, deviceId, role);
68 + }
69 +
70 + @Override
71 + public NodeId getMasterFor(DeviceId deviceId) {
72 + checkNotNull(deviceId, DEVICE_ID_NULL);
73 + return store.getMaster(deviceId);
74 + }
75 +
76 + @Override
77 + public Set<DeviceId> getDevicesOf(NodeId nodeId) {
78 + checkNotNull(nodeId, NODE_ID_NULL);
79 + return store.getDevices(nodeId);
80 + }
81 +
82 + @Override
83 + public MastershipRole requestRoleFor(DeviceId deviceId) {
84 + checkNotNull(deviceId, DEVICE_ID_NULL);
85 + NodeId id = clusterManager.getLocalNode().id();
86 + return store.getRole(id, deviceId);
87 + }
88 +
89 + @Override
90 + public void addListener(MastershipListener listener) {
91 + checkNotNull(listener);
92 + listenerRegistry.addListener(listener);
93 + }
94 +
95 + @Override
96 + public void removeListener(MastershipListener listener) {
97 + checkNotNull(listener);
98 + listenerRegistry.removeListener(listener);
99 + }
100 +
101 + private class InternalMastershipProviderService
102 + extends AbstractProviderService<MastershipProvider>
103 + implements MastershipProviderService {
104 +
105 + protected InternalMastershipProviderService(MastershipProvider provider) {
106 + super(provider);
107 + }
108 +
109 + @Override
110 + public void roleChanged(DeviceId deviceId, MastershipRole role) {
111 + // TODO Auto-generated method stub
112 +
113 + }
114 + }
115 +
116 + // Posts the specified event to the local event dispatcher.
117 + private void post(MastershipEvent event) {
118 + if (event != null && eventDispatcher != null) {
119 + eventDispatcher.post(event);
120 + }
121 + }
122 +}