Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cluster.impl; |
| 2 | |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 3 | import org.apache.felix.scr.annotations.Activate; |
tom | 4a5d171 | 2014-09-23 17:49:39 -0700 | [diff] [blame] | 4 | import org.apache.felix.scr.annotations.Component; |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 5 | import org.apache.felix.scr.annotations.Deactivate; |
| 6 | import org.apache.felix.scr.annotations.Reference; |
| 7 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | 4a5d171 | 2014-09-23 17:49:39 -0700 | [diff] [blame] | 8 | import org.apache.felix.scr.annotations.Service; |
| 9 | import org.onlab.onos.cluster.ClusterService; |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 10 | import org.onlab.onos.cluster.MastershipAdminService; |
| 11 | import org.onlab.onos.cluster.MastershipEvent; |
| 12 | import org.onlab.onos.cluster.MastershipListener; |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 13 | import org.onlab.onos.cluster.MastershipService; |
| 14 | import org.onlab.onos.cluster.MastershipStore; |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame^] | 15 | import org.onlab.onos.cluster.MastershipTerm; |
| 16 | import org.onlab.onos.cluster.MastershipTermService; |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 17 | import org.onlab.onos.cluster.NodeId; |
| 18 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 19 | import org.onlab.onos.event.EventDeliveryService; |
| 20 | import org.onlab.onos.net.DeviceId; |
| 21 | import org.onlab.onos.net.MastershipRole; |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 22 | import org.slf4j.Logger; |
| 23 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 24 | import java.util.Set; |
| 25 | |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 27 | import static org.slf4j.LoggerFactory.getLogger; |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 28 | |
tom | 4a5d171 | 2014-09-23 17:49:39 -0700 | [diff] [blame] | 29 | @Component(immediate = true) |
| 30 | @Service |
Ayaka Koshibe | 3eed2b0 | 2014-09-23 13:28:05 -0700 | [diff] [blame] | 31 | public class MastershipManager |
Ayaka Koshibe | 3eed2b0 | 2014-09-23 13:28:05 -0700 | [diff] [blame] | 32 | implements MastershipService, MastershipAdminService { |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 33 | |
| 34 | private static final String NODE_ID_NULL = "Node ID cannot be null"; |
| 35 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 36 | private static final String ROLE_NULL = "Mastership role cannot be null"; |
| 37 | |
| 38 | private final Logger log = getLogger(getClass()); |
| 39 | |
| 40 | protected final AbstractListenerRegistry<MastershipEvent, MastershipListener> |
| 41 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 42 | |
| 43 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 44 | protected MastershipStore store; |
| 45 | |
| 46 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 47 | protected EventDeliveryService eventDispatcher; |
| 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 4a5d171 | 2014-09-23 17:49:39 -0700 | [diff] [blame] | 50 | protected ClusterService clusterService; |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 51 | |
| 52 | @Activate |
| 53 | public void activate() { |
| 54 | eventDispatcher.addSink(MastershipEvent.class, listenerRegistry); |
| 55 | log.info("Started"); |
| 56 | } |
| 57 | |
| 58 | @Deactivate |
| 59 | public void deactivate() { |
| 60 | eventDispatcher.removeSink(MastershipEvent.class); |
| 61 | log.info("Stopped"); |
| 62 | } |
| 63 | |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 64 | @Override |
| 65 | public void setRole(NodeId nodeId, DeviceId deviceId, MastershipRole role) { |
| 66 | checkNotNull(nodeId, NODE_ID_NULL); |
| 67 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 68 | checkNotNull(role, ROLE_NULL); |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 69 | //TODO figure out appropriate action for non-MASTER roles, if we even set those |
| 70 | if (role.equals(MastershipRole.MASTER)) { |
| 71 | MastershipEvent event = store.setMaster(nodeId, deviceId); |
| 72 | if (event != null) { |
| 73 | post(event); |
| 74 | } |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 75 | } |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 79 | public MastershipRole getLocalRole(DeviceId deviceId) { |
| 80 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 81 | return store.getRole(clusterService.getLocalNode().id(), deviceId); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void relinquishMastership(DeviceId deviceId) { |
| 86 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 87 | // FIXME: add method to store to give up mastership and trigger new master selection process |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public MastershipRole requestRoleFor(DeviceId deviceId) { |
| 92 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 93 | return store.requestRole(deviceId); |
| 94 | } |
| 95 | |
| 96 | @Override |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 97 | public NodeId getMasterFor(DeviceId deviceId) { |
| 98 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 99 | return store.getMaster(deviceId); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public Set<DeviceId> getDevicesOf(NodeId nodeId) { |
| 104 | checkNotNull(nodeId, NODE_ID_NULL); |
| 105 | return store.getDevices(nodeId); |
| 106 | } |
| 107 | |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame^] | 108 | |
| 109 | @Override |
| 110 | public MastershipTermService requestTermService() { |
| 111 | return new InternalMastershipTermService(); |
| 112 | } |
| 113 | |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 114 | @Override |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 115 | public void addListener(MastershipListener listener) { |
| 116 | checkNotNull(listener); |
| 117 | listenerRegistry.addListener(listener); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void removeListener(MastershipListener listener) { |
| 122 | checkNotNull(listener); |
| 123 | listenerRegistry.removeListener(listener); |
| 124 | } |
| 125 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 126 | // FIXME: provide wiring to allow events to be triggered by changes within the store |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 127 | |
| 128 | // Posts the specified event to the local event dispatcher. |
| 129 | private void post(MastershipEvent event) { |
| 130 | if (event != null && eventDispatcher != null) { |
| 131 | eventDispatcher.post(event); |
| 132 | } |
| 133 | } |
Ayaka Koshibe | 3eed2b0 | 2014-09-23 13:28:05 -0700 | [diff] [blame] | 134 | |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame^] | 135 | private class InternalMastershipTermService implements MastershipTermService { |
| 136 | |
| 137 | @Override |
| 138 | public MastershipTerm getMastershipTerm(DeviceId deviceId) { |
| 139 | return store.getTermFor(deviceId); |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 144 | } |