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; |
Yuta HIGUCHI | 8e939d8 | 2014-10-08 08:09:18 -0700 | [diff] [blame] | 16 | import org.onlab.onos.cluster.NodeId; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 17 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 18 | import org.onlab.onos.event.EventDeliveryService; |
Yuta HIGUCHI | 80912e6 | 2014-10-12 00:15:47 -0700 | [diff] [blame] | 19 | import org.onlab.onos.mastership.MastershipEvent; |
| 20 | import org.onlab.onos.mastership.MastershipListener; |
| 21 | import org.onlab.onos.mastership.MastershipService; |
| 22 | import org.onlab.onos.mastership.MastershipTerm; |
| 23 | import org.onlab.onos.mastership.MastershipTermService; |
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; |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.device.DefaultDeviceDescription; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.device.DeviceAdminService; |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.device.DeviceClockProviderService; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.device.DeviceDescription; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.device.DeviceEvent; |
| 34 | import org.onlab.onos.net.device.DeviceListener; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.device.DeviceProvider; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.device.DeviceProviderRegistry; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.device.DeviceProviderService; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.device.DeviceService; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.device.DeviceStore; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.device.DeviceStoreDelegate; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.device.PortDescription; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 43 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 44 | import org.slf4j.Logger; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 45 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 46 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 47 | * Provides implementation of the device SB & NB APIs. |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 48 | */ |
| 49 | @Component(immediate = true) |
| 50 | @Service |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 51 | public class DeviceManager |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 52 | extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService> |
| 53 | implements DeviceService, DeviceAdminService, DeviceProviderRegistry { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 54 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 55 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 56 | private static final String PORT_NUMBER_NULL = "Port number cannot be null"; |
| 57 | private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null"; |
| 58 | private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null"; |
| 59 | private static final String ROLE_NULL = "Role cannot be null"; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 60 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 61 | private final Logger log = getLogger(getClass()); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 62 | |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 63 | protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry = |
| 64 | new AbstractListenerRegistry<>(); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 65 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 66 | private final DeviceStoreDelegate delegate = new InternalStoreDelegate(); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 67 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 68 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 69 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected DeviceStore store; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 72 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 74 | protected EventDeliveryService eventDispatcher; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 75 | |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 77 | protected ClusterService clusterService; |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 80 | protected MastershipService mastershipService; |
| 81 | |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 82 | protected MastershipTermService termService; |
| 83 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 85 | protected DeviceClockProviderService deviceClockProviderService; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 86 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 87 | @Activate |
| 88 | public void activate() { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 89 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 90 | eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 91 | mastershipService.addListener(mastershipListener); |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 92 | termService = mastershipService.requestTermService(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 93 | log.info("Started"); |
| 94 | } |
| 95 | |
| 96 | @Deactivate |
| 97 | public void deactivate() { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 98 | store.unsetDelegate(delegate); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 99 | mastershipService.removeListener(mastershipListener); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 100 | eventDispatcher.removeSink(DeviceEvent.class); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 101 | log.info("Stopped"); |
| 102 | } |
| 103 | |
| 104 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 105 | public int getDeviceCount() { |
| 106 | return store.getDeviceCount(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 110 | public Iterable<Device> getDevices() { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 111 | return store.getDevices(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 112 | } |
| 113 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 114 | @Override |
| 115 | public Device getDevice(DeviceId deviceId) { |
| 116 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 117 | return store.getDevice(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 121 | public MastershipRole getRole(DeviceId deviceId) { |
| 122 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 123 | return mastershipService.getLocalRole(deviceId); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 127 | public List<Port> getPorts(DeviceId deviceId) { |
| 128 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 129 | return store.getPorts(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 134 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 135 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 136 | return store.getPort(deviceId, portNumber); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 140 | public boolean isAvailable(DeviceId deviceId) { |
| 141 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 142 | return store.isAvailable(deviceId); |
| 143 | } |
| 144 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 145 | // Applies the specified role to the device; ignores NONE |
| 146 | private void applyRole(DeviceId deviceId, MastershipRole newRole) { |
Yuta HIGUCHI | d631e42 | 2014-10-19 00:18:59 -0700 | [diff] [blame] | 147 | if (!newRole.equals(MastershipRole.NONE)) { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 148 | Device device = store.getDevice(deviceId); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 149 | // FIXME: Device might not be there yet. (eventual consistent) |
| 150 | if (device == null) { |
| 151 | return; |
| 152 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 153 | DeviceProvider provider = getProvider(device.providerId()); |
| 154 | if (provider != null) { |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 155 | provider.roleChanged(device, newRole); |
Yuta HIGUCHI | 67dce88 | 2014-10-21 21:13:26 -0700 | [diff] [blame] | 156 | |
| 157 | // only trigger event when request was sent to provider |
| 158 | // TODO: consider removing this from Device event type? |
| 159 | post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device)); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 160 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 164 | // Queries a device for port information. |
| 165 | private void queryPortInfo(DeviceId deviceId) { |
| 166 | Device device = store.getDevice(deviceId); |
| 167 | // FIXME: Device might not be there yet. (eventual consistent) |
| 168 | if (device == null) { |
| 169 | return; |
| 170 | } |
| 171 | DeviceProvider provider = getProvider(device.providerId()); |
| 172 | provider.triggerProbe(device); |
| 173 | } |
| 174 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 175 | @Override |
| 176 | public void removeDevice(DeviceId deviceId) { |
| 177 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 178 | // XXX is this intended to apply to the full global topology? |
| 179 | // if so, we probably don't want the fact that we aren't |
| 180 | // MASTER to get in the way, as it would do now. |
Yuta HIGUCHI | 67dce88 | 2014-10-21 21:13:26 -0700 | [diff] [blame] | 181 | // FIXME: forward or broadcast and let the Master handler the event. |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 182 | DeviceEvent event = store.removeDevice(deviceId); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 183 | if (event != null) { |
| 184 | log.info("Device {} administratively removed", deviceId); |
| 185 | post(event); |
| 186 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 187 | } |
| 188 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 189 | @Override |
| 190 | public void addListener(DeviceListener listener) { |
| 191 | listenerRegistry.addListener(listener); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void removeListener(DeviceListener listener) { |
| 196 | listenerRegistry.removeListener(listener); |
| 197 | } |
| 198 | |
| 199 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 200 | protected DeviceProviderService createProviderService( |
| 201 | DeviceProvider provider) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 202 | return new InternalDeviceProviderService(provider); |
| 203 | } |
| 204 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 205 | // Personalized device provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 206 | private class InternalDeviceProviderService |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 207 | extends AbstractProviderService<DeviceProvider> |
| 208 | implements DeviceProviderService { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 209 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 210 | InternalDeviceProviderService(DeviceProvider provider) { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 211 | super(provider); |
| 212 | } |
| 213 | |
| 214 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 215 | public void deviceConnected(DeviceId deviceId, |
| 216 | DeviceDescription deviceDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 217 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 218 | checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 219 | checkValidity(); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 220 | |
| 221 | log.info("Device {} connected", deviceId); |
| 222 | // check my Role |
| 223 | MastershipRole role = mastershipService.requestRoleFor(deviceId); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame^] | 224 | log.info("requestedRole, became {} for {}", role, deviceId); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 225 | if (role != MastershipRole.MASTER) { |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 226 | // TODO: Do we need to explicitly tell the Provider that |
| 227 | // this instance is no longer the MASTER? probably not |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame^] | 228 | // Device device = getDevice(deviceId); |
| 229 | // if (device != null) { |
| 230 | // // FIXME roleChanged should take DeviceId instead of Device |
| 231 | // provider().roleChanged(device, role); |
| 232 | // } |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 233 | return; |
| 234 | } |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 235 | MastershipTerm term = mastershipService.requestTermService() |
| 236 | .getMastershipTerm(deviceId); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 237 | |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 238 | if (!term.master().equals(clusterService.getLocalNode().id())) { |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 239 | // lost mastership after requestRole told this instance was MASTER. |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame^] | 240 | log.info("lost mastership before getting term info."); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 241 | return; |
| 242 | } |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 243 | |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 244 | // tell clock provider if this instance is the master |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 245 | deviceClockProviderService.setMastershipTerm(deviceId, term); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 246 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 247 | DeviceEvent event = store.createOrUpdateDevice(provider().id(), |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 248 | deviceId, deviceDescription); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 249 | |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 250 | // If there was a change of any kind, tell the provider |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 251 | // that this instance is the master. |
| 252 | // Note: event can be null, if mastership was lost between |
| 253 | // roleRequest and store update calls. |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 254 | if (event != null) { |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 255 | // TODO: Check switch reconnected case. Is it assured that |
| 256 | // event will never be null? |
| 257 | // Could there be a situation MastershipService told this |
| 258 | // instance is the new Master, but |
| 259 | // event returned from the store is null? |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 260 | |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 261 | // FIXME: 1st argument should be deviceId, to allow setting |
| 262 | // certain roles even if the store returned null. |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame^] | 263 | log.info("event: {} {}", event.type(), event); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 264 | provider().roleChanged(event.subject(), role); |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 265 | post(event); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame^] | 266 | } else { |
| 267 | log.info("No event to publish"); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 268 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void deviceDisconnected(DeviceId deviceId) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 273 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 274 | checkValidity(); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 275 | |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 276 | |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 277 | DeviceEvent event = null; |
| 278 | try { |
| 279 | event = store.markOffline(deviceId); |
| 280 | } catch (IllegalStateException e) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame^] | 281 | log.warn("Failed to mark {} offline", deviceId); |
| 282 | // only the MASTER should be marking off-line in normal cases, |
| 283 | // but if I was the last STANDBY connection, etc. and no one else |
| 284 | // was there to mark the device offline, this instance may need to |
| 285 | // temporarily request for Master Role and mark offline. |
| 286 | |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 287 | //there are times when this node will correctly have mastership, BUT |
| 288 | //that isn't reflected in the ClockManager before the device disconnects. |
| 289 | //we want to let go of the device anyways, so make sure this happens. |
Yuta HIGUCHI | 0722fb2 | 2014-10-19 01:16:33 -0700 | [diff] [blame] | 290 | |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame^] | 291 | // FIXME: Store semantics leaking out as IllegalStateException. |
| 292 | // Consider revising store API to handle this scenario. |
| 293 | |
| 294 | MastershipRole role = mastershipService.requestRoleFor(deviceId); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 295 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
Yuta HIGUCHI | 0722fb2 | 2014-10-19 01:16:33 -0700 | [diff] [blame] | 296 | final NodeId myNodeId = clusterService.getLocalNode().id(); |
| 297 | // TODO: Move this type of check inside device clock manager, etc. |
| 298 | if (myNodeId.equals(term.master())) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame^] | 299 | log.info("Marking {} offline", deviceId); |
Yuta HIGUCHI | 0722fb2 | 2014-10-19 01:16:33 -0700 | [diff] [blame] | 300 | deviceClockProviderService.setMastershipTerm(deviceId, term); |
| 301 | event = store.markOffline(deviceId); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame^] | 302 | } else { |
| 303 | log.error("Failed again marking {} offline. {}", deviceId, role); |
Yuta HIGUCHI | 0722fb2 | 2014-10-19 01:16:33 -0700 | [diff] [blame] | 304 | } |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 305 | } finally { |
| 306 | //relinquish master role and ability to be backup. |
| 307 | mastershipService.relinquishMastership(deviceId); |
| 308 | |
| 309 | if (event != null) { |
| 310 | log.info("Device {} disconnected", deviceId); |
| 311 | post(event); |
| 312 | } |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 313 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 317 | public void updatePorts(DeviceId deviceId, |
| 318 | List<PortDescription> portDescriptions) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 319 | checkNotNull(deviceId, DEVICE_ID_NULL); |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 320 | checkNotNull(portDescriptions, |
| 321 | "Port descriptions list cannot be null"); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 322 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 323 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 324 | List<DeviceEvent> events = store.updatePorts(this.provider().id(), |
| 325 | deviceId, portDescriptions); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 326 | for (DeviceEvent event : events) { |
| 327 | post(event); |
| 328 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 332 | public void portStatusChanged(DeviceId deviceId, |
| 333 | PortDescription portDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 334 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 335 | checkNotNull(portDescription, PORT_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 336 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 337 | |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame^] | 338 | final DeviceEvent event = store.updatePortStatus(this.provider().id(), |
| 339 | deviceId, portDescription); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 340 | if (event != null) { |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 341 | log.info("Device {} port {} status changed", deviceId, event |
| 342 | .port().number()); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 343 | post(event); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 344 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 345 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 346 | |
| 347 | @Override |
| 348 | public void unableToAssertRole(DeviceId deviceId, MastershipRole role) { |
| 349 | // FIXME: implement response to this notification |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 350 | log.warn("Failed to assert role [{}] onto Device {}", role, |
| 351 | deviceId); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 352 | if (role == MastershipRole.MASTER) { |
| 353 | mastershipService.relinquishMastership(deviceId); |
Yuta HIGUCHI | 67dce88 | 2014-10-21 21:13:26 -0700 | [diff] [blame] | 354 | // TODO: Shouldn't we be triggering event? |
| 355 | //final Device device = getDevice(deviceId); |
| 356 | //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device)); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 357 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 358 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 359 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 360 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 361 | // Posts the specified event to the local event dispatcher. |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 362 | private void post(DeviceEvent event) { |
| 363 | if (event != null && eventDispatcher != null) { |
| 364 | eventDispatcher.post(event); |
| 365 | } |
| 366 | } |
| 367 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 368 | // Intercepts mastership events |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 369 | private class InternalMastershipListener implements MastershipListener { |
| 370 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 371 | @Override |
| 372 | public void event(MastershipEvent event) { |
Ayaka Koshibe | 4c89127 | 2014-10-08 17:14:16 -0700 | [diff] [blame] | 373 | final DeviceId did = event.subject(); |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 374 | final NodeId myNodeId = clusterService.getLocalNode().id(); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 375 | |
Yuta HIGUCHI | 67dce88 | 2014-10-21 21:13:26 -0700 | [diff] [blame] | 376 | if (myNodeId.equals(event.roleInfo().master())) { |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 377 | MastershipTerm term = termService.getMastershipTerm(did); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 378 | |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 379 | if (!myNodeId.equals(term.master())) { |
| 380 | // something went wrong in consistency, let go |
Yuta HIGUCHI | 305ec83 | 2014-10-24 22:23:17 -0700 | [diff] [blame] | 381 | log.warn("Mastership has changed after this event." |
| 382 | + "Term Service suggests {} for {}", term, did); |
| 383 | // FIXME: Is it possible to let go of MASTER role |
| 384 | // but remain on STANDBY list? |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 385 | mastershipService.relinquishMastership(did); |
| 386 | applyRole(did, MastershipRole.STANDBY); |
| 387 | return; |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 388 | } |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 389 | |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 390 | // only set the new term if I am the master |
| 391 | deviceClockProviderService.setMastershipTerm(did, term); |
| 392 | |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 393 | // FIXME: we should check that the device is connected on our end. |
| 394 | // currently, this is not straight forward as the actual switch |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 395 | // implementation is hidden from the registry. Maybe we can ask the |
| 396 | // provider. |
| 397 | // if the device is null here, we are the first master to claim the |
| 398 | // device. No worries, the DeviceManager will create one soon. |
| 399 | Device device = getDevice(did); |
| 400 | if ((device != null) && !isAvailable(did)) { |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 401 | //flag the device as online. Is there a better way to do this? |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame^] | 402 | DeviceEvent devEvent = store.createOrUpdateDevice(device.providerId(), did, |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 403 | new DefaultDeviceDescription( |
| 404 | did.uri(), device.type(), device.manufacturer(), |
| 405 | device.hwVersion(), device.swVersion(), |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 406 | device.serialNumber(), device.chassisId())); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame^] | 407 | post(devEvent); |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 408 | } |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 409 | applyRole(did, MastershipRole.MASTER); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame^] | 410 | // re-collect device information to fix potential staleness |
| 411 | queryPortInfo(did); |
Yuta HIGUCHI | 67dce88 | 2014-10-21 21:13:26 -0700 | [diff] [blame] | 412 | } else if (event.roleInfo().backups().contains(myNodeId)) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 413 | applyRole(did, MastershipRole.STANDBY); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 417 | |
| 418 | // Store delegate to re-post events emitted from the store. |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 419 | private class InternalStoreDelegate |
| 420 | implements DeviceStoreDelegate { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 421 | @Override |
| 422 | public void notify(DeviceEvent event) { |
| 423 | post(event); |
| 424 | } |
| 425 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 426 | } |