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