tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.device.impl; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 2 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_MASTERSHIP_CHANGED; |
| 5 | import static org.slf4j.LoggerFactory.getLogger; |
| 6 | |
| 7 | import java.util.List; |
| 8 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 9 | import org.apache.felix.scr.annotations.Activate; |
| 10 | import org.apache.felix.scr.annotations.Component; |
| 11 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 12 | import org.apache.felix.scr.annotations.Reference; |
| 13 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 14 | import org.apache.felix.scr.annotations.Service; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 15 | import org.onlab.onos.cluster.ClusterService; |
| 16 | import org.onlab.onos.cluster.MastershipEvent; |
| 17 | import org.onlab.onos.cluster.MastershipListener; |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 18 | import org.onlab.onos.cluster.MastershipService; |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 19 | import org.onlab.onos.cluster.MastershipTermService; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 20 | import org.onlab.onos.cluster.MastershipTerm; |
Yuta HIGUCHI | 8e939d8 | 2014-10-08 08:09:18 -0700 | [diff] [blame] | 21 | import org.onlab.onos.cluster.NodeId; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 22 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 23 | import org.onlab.onos.event.EventDeliveryService; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.Device; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.DeviceId; |
| 26 | import org.onlab.onos.net.MastershipRole; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.Port; |
| 28 | import org.onlab.onos.net.PortNumber; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.device.DeviceAdminService; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.device.DeviceDescription; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.device.DeviceEvent; |
| 32 | import org.onlab.onos.net.device.DeviceListener; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.device.DeviceProvider; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.device.DeviceProviderRegistry; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.device.DeviceProviderService; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.device.DeviceService; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.device.DeviceStore; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.device.DeviceStoreDelegate; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.device.PortDescription; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.provider.AbstractProviderService; |
Yuta HIGUCHI | 68b3494 | 2014-10-02 23:23:21 -0700 | [diff] [blame] | 42 | import org.onlab.onos.store.ClockProviderService; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 43 | import org.slf4j.Logger; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 44 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 45 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 46 | * Provides implementation of the device SB & NB APIs. |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 47 | */ |
| 48 | @Component(immediate = true) |
| 49 | @Service |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 50 | public class DeviceManager |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 51 | extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService> |
| 52 | implements DeviceService, DeviceAdminService, DeviceProviderRegistry { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 53 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 54 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 55 | private static final String PORT_NUMBER_NULL = "Port number cannot be null"; |
| 56 | private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null"; |
| 57 | private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null"; |
| 58 | private static final String ROLE_NULL = "Role cannot be null"; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 59 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 60 | private final Logger log = getLogger(getClass()); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 61 | |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 62 | protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry = |
| 63 | new AbstractListenerRegistry<>(); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 64 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 65 | private final DeviceStoreDelegate delegate = new InternalStoreDelegate(); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 66 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 67 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 68 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected DeviceStore store; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 71 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 73 | protected EventDeliveryService eventDispatcher; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 74 | |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 76 | protected ClusterService clusterService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 79 | protected MastershipService mastershipService; |
| 80 | |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 81 | protected MastershipTermService termService; |
| 82 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 68b3494 | 2014-10-02 23:23:21 -0700 | [diff] [blame] | 84 | protected ClockProviderService clockProviderService; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 85 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 86 | @Activate |
| 87 | public void activate() { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 88 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 89 | eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 90 | mastershipService.addListener(mastershipListener); |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 91 | termService = mastershipService.requestTermService(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 92 | log.info("Started"); |
| 93 | } |
| 94 | |
| 95 | @Deactivate |
| 96 | public void deactivate() { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 97 | store.unsetDelegate(delegate); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 98 | mastershipService.removeListener(mastershipListener); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 99 | eventDispatcher.removeSink(DeviceEvent.class); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 100 | log.info("Stopped"); |
| 101 | } |
| 102 | |
| 103 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 104 | public int getDeviceCount() { |
| 105 | return store.getDeviceCount(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 109 | public Iterable<Device> getDevices() { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 110 | return store.getDevices(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 111 | } |
| 112 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 113 | @Override |
| 114 | public Device getDevice(DeviceId deviceId) { |
| 115 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 116 | return store.getDevice(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 120 | public MastershipRole getRole(DeviceId deviceId) { |
| 121 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 122 | return mastershipService.getLocalRole(deviceId); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 126 | public List<Port> getPorts(DeviceId deviceId) { |
| 127 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 128 | return store.getPorts(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 133 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 134 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 135 | return store.getPort(deviceId, portNumber); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 139 | public boolean isAvailable(DeviceId deviceId) { |
| 140 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 141 | return store.isAvailable(deviceId); |
| 142 | } |
| 143 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 144 | // Applies the specified role to the device; ignores NONE |
| 145 | private void applyRole(DeviceId deviceId, MastershipRole newRole) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 146 | if (newRole.equals(MastershipRole.NONE)) { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 147 | Device device = store.getDevice(deviceId); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 148 | // FIXME: Device might not be there yet. (eventual consistent) |
| 149 | if (device == null) { |
| 150 | return; |
| 151 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 152 | DeviceProvider provider = getProvider(device.providerId()); |
| 153 | if (provider != null) { |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 154 | provider.roleChanged(device, newRole); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 155 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 156 | post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device)); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void removeDevice(DeviceId deviceId) { |
| 162 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 163 | DeviceEvent event = store.removeDevice(deviceId); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 164 | if (event != null) { |
| 165 | log.info("Device {} administratively removed", deviceId); |
| 166 | post(event); |
| 167 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 168 | } |
| 169 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 170 | @Override |
| 171 | public void addListener(DeviceListener listener) { |
| 172 | listenerRegistry.addListener(listener); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void removeListener(DeviceListener listener) { |
| 177 | listenerRegistry.removeListener(listener); |
| 178 | } |
| 179 | |
| 180 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 181 | protected DeviceProviderService createProviderService( |
| 182 | DeviceProvider provider) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 183 | return new InternalDeviceProviderService(provider); |
| 184 | } |
| 185 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 186 | // Personalized device provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 187 | private class InternalDeviceProviderService |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 188 | extends AbstractProviderService<DeviceProvider> |
| 189 | implements DeviceProviderService { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 190 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 191 | InternalDeviceProviderService(DeviceProvider provider) { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 192 | super(provider); |
| 193 | } |
| 194 | |
| 195 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 196 | public void deviceConnected(DeviceId deviceId, |
| 197 | DeviceDescription deviceDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 198 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 199 | checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 200 | checkValidity(); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 201 | |
| 202 | log.info("Device {} connected", deviceId); |
| 203 | // check my Role |
| 204 | MastershipRole role = mastershipService.requestRoleFor(deviceId); |
| 205 | |
| 206 | if (role != MastershipRole.MASTER) { |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 207 | // TODO: Do we need to explicitly tell the Provider that |
| 208 | // this instance is no longer the MASTER? probably not |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 212 | MastershipTerm term = mastershipService.requestTermService() |
| 213 | .getMastershipTerm(deviceId); |
| 214 | if (!term.master().equals(clusterService.getLocalNode().id())) { |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 215 | // lost mastership after requestRole told this instance was MASTER. |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 216 | return; |
| 217 | } |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 218 | // tell clock provider if this instance is the master |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 219 | clockProviderService.setMastershipTerm(deviceId, term); |
| 220 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 221 | DeviceEvent event = store.createOrUpdateDevice(provider().id(), |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 222 | deviceId, deviceDescription); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 223 | |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 224 | // If there was a change of any kind, tell the provider |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 225 | // that this instance is the master. |
| 226 | // Note: event can be null, if mastership was lost between |
| 227 | // roleRequest and store update calls. |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 228 | if (event != null) { |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 229 | // TODO: Check switch reconnected case. Is it assured that |
| 230 | // event will never be null? |
| 231 | // Could there be a situation MastershipService told this |
| 232 | // instance is the new Master, but |
| 233 | // event returned from the store is null? |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 234 | |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 235 | // TODO: Confirm: Mastership could be lost after requestRole |
| 236 | // and createOrUpdateDevice call. |
| 237 | // In that case STANDBY node can |
| 238 | // claim itself to be master against the Device. |
| 239 | // Will the Node, chosen by the MastershipService, retry |
| 240 | // to get the MASTER role when that happen? |
| 241 | |
| 242 | // FIXME: 1st argument should be deviceId, to allow setting |
| 243 | // certain roles even if the store returned null. |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 244 | provider().roleChanged(event.subject(), role); |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 245 | post(event); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 246 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public void deviceDisconnected(DeviceId deviceId) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 251 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 252 | checkValidity(); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 253 | |
| 254 | // FIXME: only the MASTER should be marking off-line in normal cases, |
| 255 | // but if I was the last STANDBY connection, etc. and no one else |
| 256 | // was there to mark the device offline, this instance may need to |
| 257 | // temporarily request for Master Role and mark offline. |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 258 | if (!mastershipService.getLocalRole(deviceId).equals(MastershipRole.MASTER)) { |
| 259 | log.debug("Device {} disconnected, but I am not the master", deviceId); |
| 260 | return; |
| 261 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 262 | DeviceEvent event = store.markOffline(deviceId); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 263 | //we're no longer capable of being master or a candidate. |
| 264 | mastershipService.relinquishMastership(deviceId); |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 265 | |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 266 | if (event != null) { |
| 267 | log.info("Device {} disconnected", deviceId); |
| 268 | post(event); |
| 269 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 273 | public void updatePorts(DeviceId deviceId, |
| 274 | List<PortDescription> portDescriptions) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 275 | checkNotNull(deviceId, DEVICE_ID_NULL); |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 276 | checkNotNull(portDescriptions, |
| 277 | "Port descriptions list cannot be null"); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 278 | checkValidity(); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 279 | this.provider().id(); |
| 280 | List<DeviceEvent> events = store.updatePorts(this.provider().id(), |
| 281 | deviceId, portDescriptions); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 282 | for (DeviceEvent event : events) { |
| 283 | post(event); |
| 284 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 288 | public void portStatusChanged(DeviceId deviceId, |
| 289 | PortDescription portDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 290 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 291 | checkNotNull(portDescription, PORT_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 292 | checkValidity(); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 293 | DeviceEvent event = store.updatePortStatus(this.provider().id(), |
| 294 | deviceId, portDescription); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 295 | if (event != null) { |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 296 | log.info("Device {} port {} status changed", deviceId, event |
| 297 | .port().number()); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 298 | post(event); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 299 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 300 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 301 | |
| 302 | @Override |
| 303 | public void unableToAssertRole(DeviceId deviceId, MastershipRole role) { |
| 304 | // FIXME: implement response to this notification |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 305 | log.warn("Failed to assert role [{}] onto Device {}", role, |
| 306 | deviceId); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 307 | if (role == MastershipRole.MASTER) { |
| 308 | mastershipService.relinquishMastership(deviceId); |
| 309 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 310 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 311 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 312 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 313 | // Posts the specified event to the local event dispatcher. |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 314 | private void post(DeviceEvent event) { |
| 315 | if (event != null && eventDispatcher != null) { |
| 316 | eventDispatcher.post(event); |
| 317 | } |
| 318 | } |
| 319 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 320 | // Intercepts mastership events |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 321 | private class InternalMastershipListener implements MastershipListener { |
| 322 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 323 | @Override |
| 324 | public void event(MastershipEvent event) { |
Ayaka Koshibe | 4c89127 | 2014-10-08 17:14:16 -0700 | [diff] [blame^] | 325 | final DeviceId did = event.subject(); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 326 | if (isAvailable(did)) { |
Ayaka Koshibe | 4c89127 | 2014-10-08 17:14:16 -0700 | [diff] [blame^] | 327 | final NodeId myNodeId = clusterService.getLocalNode().id(); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 328 | |
Ayaka Koshibe | 4c89127 | 2014-10-08 17:14:16 -0700 | [diff] [blame^] | 329 | if (myNodeId.equals(event.master())) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 330 | MastershipTerm term = termService.getMastershipTerm(did); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 331 | |
Ayaka Koshibe | 4c89127 | 2014-10-08 17:14:16 -0700 | [diff] [blame^] | 332 | if (term.master().equals(myNodeId)) { |
| 333 | // only set the new term if I am the master |
| 334 | clockProviderService.setMastershipTerm(did, term); |
| 335 | } |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 336 | applyRole(did, MastershipRole.MASTER); |
| 337 | } else { |
| 338 | applyRole(did, MastershipRole.STANDBY); |
| 339 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 340 | } else { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 341 | //device dead to node, give up |
| 342 | mastershipService.relinquishMastership(did); |
| 343 | applyRole(did, MastershipRole.STANDBY); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 347 | |
| 348 | // Store delegate to re-post events emitted from the store. |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 349 | private class InternalStoreDelegate |
| 350 | implements DeviceStoreDelegate { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 351 | @Override |
| 352 | public void notify(DeviceEvent event) { |
| 353 | post(event); |
| 354 | } |
| 355 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 356 | } |