Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.device.impl; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 17 | |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.event.AbstractListenerRegistry; |
| 28 | import org.onosproject.event.EventDeliveryService; |
| 29 | import org.onosproject.mastership.MastershipEvent; |
| 30 | import org.onosproject.mastership.MastershipListener; |
| 31 | import org.onosproject.mastership.MastershipService; |
| 32 | import org.onosproject.mastership.MastershipTerm; |
| 33 | import org.onosproject.mastership.MastershipTermService; |
| 34 | import org.onosproject.net.Device; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.MastershipRole; |
| 37 | import org.onosproject.net.Port; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 40 | import org.onosproject.net.device.DefaultPortDescription; |
| 41 | import org.onosproject.net.device.DeviceAdminService; |
| 42 | import org.onosproject.net.device.DeviceClockProviderService; |
| 43 | import org.onosproject.net.device.DeviceDescription; |
| 44 | import org.onosproject.net.device.DeviceEvent; |
| 45 | import org.onosproject.net.device.DeviceListener; |
| 46 | import org.onosproject.net.device.DeviceProvider; |
| 47 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 48 | import org.onosproject.net.device.DeviceProviderService; |
| 49 | import org.onosproject.net.device.DeviceService; |
| 50 | import org.onosproject.net.device.DeviceStore; |
| 51 | import org.onosproject.net.device.DeviceStoreDelegate; |
| 52 | import org.onosproject.net.device.PortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 53 | import org.onosproject.net.device.PortStatistics; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | import org.onosproject.net.provider.AbstractProviderRegistry; |
| 55 | import org.onosproject.net.provider.AbstractProviderService; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 56 | import org.slf4j.Logger; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 57 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 58 | import java.util.Collection; |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 59 | import java.util.List; |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 60 | import java.util.concurrent.ScheduledExecutorService; |
| 61 | import java.util.concurrent.TimeUnit; |
| 62 | |
| 63 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 64 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 65 | import static org.onlab.util.Tools.groupedThreads; |
| 66 | import static org.onosproject.net.MastershipRole.*; |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 67 | import static org.slf4j.LoggerFactory.getLogger; |
| 68 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 69 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 70 | * Provides implementation of the device SB & NB APIs. |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 71 | */ |
| 72 | @Component(immediate = true) |
| 73 | @Service |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 74 | public class DeviceManager |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 75 | extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService> |
| 76 | implements DeviceService, DeviceAdminService, DeviceProviderRegistry { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 77 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 78 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 79 | private static final String PORT_NUMBER_NULL = "Port number cannot be null"; |
| 80 | private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null"; |
| 81 | private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null"; |
| 82 | private static final String ROLE_NULL = "Role cannot be null"; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 83 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 84 | private final Logger log = getLogger(getClass()); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 85 | |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 86 | protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry = |
| 87 | new AbstractListenerRegistry<>(); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 88 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 89 | private final DeviceStoreDelegate delegate = new InternalStoreDelegate(); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 90 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 91 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 92 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 93 | private ScheduledExecutorService backgroundService; |
| 94 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected DeviceStore store; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 97 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 99 | protected EventDeliveryService eventDispatcher; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 100 | |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 102 | protected ClusterService clusterService; |
| 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 105 | protected MastershipService mastershipService; |
| 106 | |
Yuta HIGUCHI | bcac499 | 2014-11-22 19:27:57 -0800 | [diff] [blame] | 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 108 | protected MastershipTermService termService; |
| 109 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 111 | protected DeviceClockProviderService deviceClockProviderService; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 112 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 113 | @Activate |
| 114 | public void activate() { |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 115 | backgroundService = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "manager-background")); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 116 | |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 117 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 118 | eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 119 | mastershipService.addListener(mastershipListener); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 120 | |
| 121 | backgroundService.scheduleWithFixedDelay(new Runnable() { |
| 122 | |
| 123 | @Override |
| 124 | public void run() { |
| 125 | try { |
| 126 | mastershipCheck(); |
| 127 | } catch (Exception e) { |
| 128 | log.error("Exception thrown during integrity check", e); |
| 129 | } |
| 130 | } |
| 131 | }, 1, 1, TimeUnit.MINUTES); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 132 | log.info("Started"); |
| 133 | } |
| 134 | |
| 135 | @Deactivate |
| 136 | public void deactivate() { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 137 | backgroundService.shutdown(); |
| 138 | |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 139 | store.unsetDelegate(delegate); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 140 | mastershipService.removeListener(mastershipListener); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 141 | eventDispatcher.removeSink(DeviceEvent.class); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 142 | log.info("Stopped"); |
| 143 | } |
| 144 | |
| 145 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 146 | public int getDeviceCount() { |
| 147 | return store.getDeviceCount(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 151 | public Iterable<Device> getDevices() { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 152 | return store.getDevices(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 153 | } |
| 154 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 155 | @Override |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 156 | public Iterable<Device> getAvailableDevices() { |
| 157 | return store.getAvailableDevices(); |
| 158 | } |
| 159 | |
| 160 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 161 | public Device getDevice(DeviceId deviceId) { |
| 162 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 163 | return store.getDevice(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 167 | public MastershipRole getRole(DeviceId deviceId) { |
| 168 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 169 | return mastershipService.getLocalRole(deviceId); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 173 | public List<Port> getPorts(DeviceId deviceId) { |
| 174 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 175 | return store.getPorts(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 179 | public List<PortStatistics> getPortStatistics(DeviceId deviceId) { |
| 180 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 181 | return store.getPortStatistics(deviceId); |
| 182 | } |
| 183 | |
| 184 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 185 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 186 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 187 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 188 | return store.getPort(deviceId, portNumber); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 192 | public boolean isAvailable(DeviceId deviceId) { |
| 193 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 194 | return store.isAvailable(deviceId); |
| 195 | } |
| 196 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 197 | // Check a device for control channel connectivity. |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 198 | private boolean isReachable(DeviceId deviceId) { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 199 | if (deviceId == null) { |
| 200 | return false; |
| 201 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 202 | DeviceProvider provider = getProvider(deviceId); |
| 203 | if (provider != null) { |
| 204 | return provider.isReachable(deviceId); |
| 205 | } else { |
Yuta HIGUCHI | 72669c4 | 2014-11-13 14:48:17 -0800 | [diff] [blame] | 206 | log.debug("Provider not found for {}", deviceId); |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 207 | return false; |
| 208 | } |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 209 | } |
| 210 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 211 | @Override |
| 212 | public void removeDevice(DeviceId deviceId) { |
| 213 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 214 | DeviceEvent event = store.removeDevice(deviceId); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 215 | if (event != null) { |
| 216 | log.info("Device {} administratively removed", deviceId); |
| 217 | post(event); |
| 218 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 219 | } |
| 220 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 221 | @Override |
| 222 | public void addListener(DeviceListener listener) { |
| 223 | listenerRegistry.addListener(listener); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void removeListener(DeviceListener listener) { |
| 228 | listenerRegistry.removeListener(listener); |
| 229 | } |
| 230 | |
| 231 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 232 | protected DeviceProviderService createProviderService( |
| 233 | DeviceProvider provider) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 234 | return new InternalDeviceProviderService(provider); |
| 235 | } |
| 236 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 237 | /** |
| 238 | * Checks if all the reachable devices have a valid mastership role. |
| 239 | */ |
| 240 | private void mastershipCheck() { |
| 241 | log.debug("Checking mastership"); |
| 242 | for (Device device : getDevices()) { |
| 243 | final DeviceId deviceId = device.id(); |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 244 | log.trace("Checking device {}", deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 245 | |
| 246 | if (!isReachable(deviceId)) { |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | if (mastershipService.getLocalRole(deviceId) != NONE) { |
| 251 | continue; |
| 252 | } |
| 253 | |
| 254 | log.info("{} is reachable but did not have a valid role, reasserting", deviceId); |
| 255 | |
| 256 | // isReachable but was not MASTER or STANDBY, get a role and apply |
| 257 | // Note: NONE triggers request to MastershipService |
| 258 | reassertRole(deviceId, NONE); |
| 259 | } |
| 260 | } |
| 261 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 262 | // Personalized device provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 263 | private class InternalDeviceProviderService |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 264 | extends AbstractProviderService<DeviceProvider> |
| 265 | implements DeviceProviderService { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 266 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 267 | InternalDeviceProviderService(DeviceProvider provider) { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 268 | super(provider); |
| 269 | } |
| 270 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 271 | /** |
| 272 | * Apply role in reaction to provider event. |
| 273 | * |
| 274 | * @param deviceId device identifier |
| 275 | * @param newRole new role to apply to the device |
| 276 | * @return true if the request was sent to provider |
| 277 | */ |
| 278 | private boolean applyRole(DeviceId deviceId, MastershipRole newRole) { |
| 279 | |
| 280 | if (newRole.equals(MastershipRole.NONE)) { |
| 281 | //no-op |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | DeviceProvider provider = provider(); |
| 286 | if (provider == null) { |
| 287 | log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole); |
| 288 | return false; |
| 289 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 290 | provider.roleChanged(deviceId, newRole); |
| 291 | // not triggering probe when triggered by provider service event |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 292 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 293 | return true; |
| 294 | } |
| 295 | |
| 296 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 297 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 298 | public void deviceConnected(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 299 | DeviceDescription deviceDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 300 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 301 | checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 302 | checkValidity(); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 303 | |
| 304 | log.info("Device {} connected", deviceId); |
Yuta HIGUCHI | dfe6e3b | 2014-10-30 11:31:51 -0700 | [diff] [blame] | 305 | final NodeId myNodeId = clusterService.getLocalNode().id(); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 306 | |
| 307 | // check my Role |
| 308 | mastershipService.requestRoleFor(deviceId); |
| 309 | final MastershipTerm term = termService.getMastershipTerm(deviceId); |
Madan Jampani | 2096b93 | 2015-04-30 12:55:25 -0700 | [diff] [blame] | 310 | if (term == null || !myNodeId.equals(term.master())) { |
Yuta HIGUCHI | 2d3cd31 | 2014-10-31 11:38:04 -0700 | [diff] [blame] | 311 | log.info("Role of this node is STANDBY for {}", deviceId); |
| 312 | // TODO: Do we need to explicitly tell the Provider that |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 313 | // this instance is not the MASTER |
| 314 | applyRole(deviceId, MastershipRole.STANDBY); |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 315 | } else { |
| 316 | log.info("Role of this node is MASTER for {}", deviceId); |
| 317 | // tell clock provider if this instance is the master |
| 318 | deviceClockProviderService.setMastershipTerm(deviceId, term); |
| 319 | applyRole(deviceId, MastershipRole.MASTER); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 320 | } |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 321 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 322 | DeviceEvent event = store.createOrUpdateDevice(provider().id(), |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 323 | deviceId, deviceDescription); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 324 | |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 325 | // If there was a change of any kind, tell the provider |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 326 | // that this instance is the master. |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 327 | if (event != null) { |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 328 | log.trace("event: {} {}", event.type(), event); |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 329 | post(event); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 330 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public void deviceDisconnected(DeviceId deviceId) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 335 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 336 | checkValidity(); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 337 | |
Yuta HIGUCHI | 2d3cd31 | 2014-10-31 11:38:04 -0700 | [diff] [blame] | 338 | log.info("Device {} disconnected from this node", deviceId); |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 339 | |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 340 | DeviceEvent event = null; |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 341 | List<Port> ports = store.getPorts(deviceId); |
| 342 | List<PortDescription> descs = Lists.newArrayList(); |
| 343 | ports.forEach(port -> |
| 344 | descs.add(new DefaultPortDescription(port.number(), |
| 345 | false, port.type(), |
| 346 | port.portSpeed()))); |
| 347 | store.updatePorts(this.provider().id(), deviceId, descs); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 348 | try { |
| 349 | event = store.markOffline(deviceId); |
| 350 | } catch (IllegalStateException e) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 351 | log.warn("Failed to mark {} offline", deviceId); |
| 352 | // only the MASTER should be marking off-line in normal cases, |
| 353 | // but if I was the last STANDBY connection, etc. and no one else |
| 354 | // was there to mark the device offline, this instance may need to |
| 355 | // temporarily request for Master Role and mark offline. |
| 356 | |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 357 | //there are times when this node will correctly have mastership, BUT |
| 358 | //that isn't reflected in the ClockManager before the device disconnects. |
| 359 | //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] | 360 | |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 361 | // FIXME: Store semantics leaking out as IllegalStateException. |
| 362 | // Consider revising store API to handle this scenario. |
| 363 | |
| 364 | MastershipRole role = mastershipService.requestRoleFor(deviceId); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 365 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
Yuta HIGUCHI | 0722fb2 | 2014-10-19 01:16:33 -0700 | [diff] [blame] | 366 | final NodeId myNodeId = clusterService.getLocalNode().id(); |
| 367 | // TODO: Move this type of check inside device clock manager, etc. |
| 368 | if (myNodeId.equals(term.master())) { |
Yuta HIGUCHI | 2d3cd31 | 2014-10-31 11:38:04 -0700 | [diff] [blame] | 369 | log.info("Retry marking {} offline", deviceId); |
Yuta HIGUCHI | 0722fb2 | 2014-10-19 01:16:33 -0700 | [diff] [blame] | 370 | deviceClockProviderService.setMastershipTerm(deviceId, term); |
| 371 | event = store.markOffline(deviceId); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 372 | } else { |
Yuta HIGUCHI | 2d3cd31 | 2014-10-31 11:38:04 -0700 | [diff] [blame] | 373 | log.info("Failed again marking {} offline. {}", deviceId, role); |
Yuta HIGUCHI | 0722fb2 | 2014-10-19 01:16:33 -0700 | [diff] [blame] | 374 | } |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 375 | } finally { |
| 376 | //relinquish master role and ability to be backup. |
| 377 | mastershipService.relinquishMastership(deviceId); |
| 378 | |
| 379 | if (event != null) { |
Yuta HIGUCHI | 2d3cd31 | 2014-10-31 11:38:04 -0700 | [diff] [blame] | 380 | log.info("Device {} disconnected from cluster", deviceId); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 381 | post(event); |
| 382 | } |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 383 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 387 | public void updatePorts(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 388 | List<PortDescription> portDescriptions) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 389 | checkNotNull(deviceId, DEVICE_ID_NULL); |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 390 | checkNotNull(portDescriptions, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 391 | "Port descriptions list cannot be null"); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 392 | checkValidity(); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 393 | if (!deviceClockProviderService.isTimestampAvailable(deviceId)) { |
| 394 | // Never been a master for this device |
| 395 | // any update will be ignored. |
| 396 | log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions); |
| 397 | return; |
| 398 | } |
| 399 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 400 | List<DeviceEvent> events = store.updatePorts(this.provider().id(), |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 401 | deviceId, portDescriptions); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 402 | for (DeviceEvent event : events) { |
| 403 | post(event); |
| 404 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 408 | public void portStatusChanged(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 409 | PortDescription portDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 410 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 411 | checkNotNull(portDescription, PORT_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 412 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 413 | |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 414 | if (!deviceClockProviderService.isTimestampAvailable(deviceId)) { |
| 415 | // Never been a master for this device |
| 416 | // any update will be ignored. |
| 417 | log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription); |
| 418 | return; |
| 419 | } |
| 420 | |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 421 | final DeviceEvent event = store.updatePortStatus(this.provider().id(), |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 422 | deviceId, portDescription); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 423 | if (event != null) { |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 424 | log.info("Device {} port {} status changed", deviceId, event |
| 425 | .port().number()); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 426 | post(event); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 427 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 428 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 429 | |
| 430 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 431 | public void receivedRoleReply( |
| 432 | DeviceId deviceId, MastershipRole requested, MastershipRole response) { |
| 433 | // Several things can happen here: |
| 434 | // 1. request and response match |
| 435 | // 2. request and response don't match |
| 436 | // 3. MastershipRole and requested match (and 1 or 2 are true) |
| 437 | // 4. MastershipRole and requested don't match (and 1 or 2 are true) |
| 438 | // |
| 439 | // 2, 4, and 3 with case 2 are failure modes. |
| 440 | |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 441 | // FIXME: implement response to this notification |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 442 | |
| 443 | log.info("got reply to a role request for {}: asked for {}, and got {}", |
| 444 | deviceId, requested, response); |
| 445 | |
| 446 | if (requested == null && response == null) { |
| 447 | // something was off with DeviceProvider, maybe check channel too? |
| 448 | log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 449 | mastershipService.relinquishMastership(deviceId); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 450 | return; |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 451 | } |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 452 | |
| 453 | if (requested.equals(response)) { |
| 454 | if (requested.equals(mastershipService.getLocalRole(deviceId))) { |
| 455 | |
| 456 | return; |
| 457 | } else { |
| 458 | return; |
| 459 | // FIXME roleManager got the device to comply, but doesn't agree with |
| 460 | // the store; use the store's view, then try to reassert. |
| 461 | } |
| 462 | } else { |
| 463 | // we didn't get back what we asked for. Reelect someone else. |
| 464 | log.warn("Failed to assert role [{}] onto Device {}", response, deviceId); |
| 465 | if (response == MastershipRole.MASTER) { |
| 466 | mastershipService.relinquishMastership(deviceId); |
| 467 | // TODO: Shouldn't we be triggering event? |
| 468 | //final Device device = getDevice(deviceId); |
| 469 | //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device)); |
| 470 | } |
| 471 | } |
| 472 | |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 473 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 474 | |
| 475 | @Override |
| 476 | public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) { |
| 477 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 478 | checkNotNull(portStatistics, |
| 479 | "Port statistics list cannot be null"); |
| 480 | checkValidity(); |
| 481 | |
| 482 | DeviceEvent event = store.updatePortStatistics(this.provider().id(), |
| 483 | deviceId, portStatistics); |
| 484 | |
| 485 | post(event); |
| 486 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 487 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 488 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 489 | // Posts the specified event to the local event dispatcher. |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 490 | private void post(DeviceEvent event) { |
| 491 | if (event != null && eventDispatcher != null) { |
| 492 | eventDispatcher.post(event); |
| 493 | } |
| 494 | } |
| 495 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 496 | // Applies the specified role to the device; ignores NONE |
| 497 | /** |
| 498 | * Apply role to device and send probe if MASTER. |
| 499 | * |
| 500 | * @param deviceId device identifier |
| 501 | * @param newRole new role to apply to the device |
| 502 | * @return true if the request was sent to provider |
| 503 | */ |
| 504 | private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) { |
| 505 | if (newRole.equals(MastershipRole.NONE)) { |
| 506 | //no-op |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 507 | return true; |
| 508 | } |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 509 | |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 510 | DeviceProvider provider = getProvider(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 511 | if (provider == null) { |
| 512 | log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole); |
| 513 | return false; |
| 514 | } |
| 515 | provider.roleChanged(deviceId, newRole); |
| 516 | |
| 517 | if (newRole.equals(MastershipRole.MASTER)) { |
| 518 | // only trigger event when request was sent to provider |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 519 | provider.triggerProbe(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 520 | } |
| 521 | return true; |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Reaasert role for specified device connected to this node. |
| 526 | * |
| 527 | * @param did device identifier |
| 528 | * @param nextRole role to apply. If NONE is specified, |
| 529 | * it will ask mastership service for a role and apply it. |
| 530 | */ |
| 531 | private void reassertRole(final DeviceId did, |
| 532 | final MastershipRole nextRole) { |
| 533 | |
| 534 | final NodeId myNodeId = clusterService.getLocalNode().id(); |
| 535 | MastershipRole myNextRole = nextRole; |
| 536 | if (myNextRole == NONE) { |
| 537 | mastershipService.requestRoleFor(did); |
| 538 | MastershipTerm term = termService.getMastershipTerm(did); |
| 539 | if (myNodeId.equals(term.master())) { |
| 540 | myNextRole = MASTER; |
| 541 | } else { |
| 542 | myNextRole = STANDBY; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | switch (myNextRole) { |
| 547 | case MASTER: |
| 548 | final Device device = getDevice(did); |
| 549 | if ((device != null) && !isAvailable(did)) { |
| 550 | //flag the device as online. Is there a better way to do this? |
| 551 | DefaultDeviceDescription deviceDescription |
| 552 | = new DefaultDeviceDescription(did.uri(), |
| 553 | device.type(), |
| 554 | device.manufacturer(), |
| 555 | device.hwVersion(), |
| 556 | device.swVersion(), |
| 557 | device.serialNumber(), |
| 558 | device.chassisId()); |
| 559 | DeviceEvent devEvent = |
| 560 | store.createOrUpdateDevice(device.providerId(), did, |
| 561 | deviceDescription); |
| 562 | post(devEvent); |
| 563 | } |
| 564 | // TODO: should apply role only if there is mismatch |
| 565 | log.info("Applying role {} to {}", myNextRole, did); |
| 566 | if (!applyRoleAndProbe(did, MASTER)) { |
| 567 | // immediately failed to apply role |
| 568 | mastershipService.relinquishMastership(did); |
| 569 | // FIXME disconnect? |
| 570 | } |
| 571 | break; |
| 572 | case STANDBY: |
| 573 | log.info("Applying role {} to {}", myNextRole, did); |
| 574 | if (!applyRoleAndProbe(did, STANDBY)) { |
| 575 | // immediately failed to apply role |
| 576 | mastershipService.relinquishMastership(did); |
| 577 | // FIXME disconnect? |
| 578 | } |
| 579 | break; |
| 580 | case NONE: |
| 581 | default: |
| 582 | // should never reach here |
| 583 | log.error("You didn't see anything. I did not exist."); |
| 584 | break; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | // Intercepts mastership events |
| 589 | private class InternalMastershipListener implements MastershipListener { |
| 590 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 591 | @Override |
| 592 | public void event(MastershipEvent event) { |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 593 | |
| 594 | if (event.type() != MastershipEvent.Type.MASTER_CHANGED) { |
| 595 | // Don't care if backup list changed. |
| 596 | return; |
| 597 | } |
| 598 | |
Ayaka Koshibe | 4c89127 | 2014-10-08 17:14:16 -0700 | [diff] [blame] | 599 | final DeviceId did = event.subject(); |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 600 | final NodeId myNodeId = clusterService.getLocalNode().id(); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 601 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 602 | // myRole suggested by MastershipService |
| 603 | MastershipRole myNextRole; |
Yuta HIGUCHI | 67dce88 | 2014-10-21 21:13:26 -0700 | [diff] [blame] | 604 | if (myNodeId.equals(event.roleInfo().master())) { |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 605 | // confirm latest info |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 606 | MastershipTerm term = termService.getMastershipTerm(did); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 607 | final boolean iHaveControl = myNodeId.equals(term.master()); |
| 608 | if (iHaveControl) { |
Yuta HIGUCHI | 38b4d9d | 2014-10-30 11:32:41 -0700 | [diff] [blame] | 609 | deviceClockProviderService.setMastershipTerm(did, term); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 610 | myNextRole = MASTER; |
Yuta HIGUCHI | 38b4d9d | 2014-10-30 11:32:41 -0700 | [diff] [blame] | 611 | } else { |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 612 | myNextRole = STANDBY; |
| 613 | } |
| 614 | } else if (event.roleInfo().backups().contains(myNodeId)) { |
| 615 | myNextRole = STANDBY; |
| 616 | } else { |
| 617 | myNextRole = NONE; |
| 618 | } |
| 619 | |
| 620 | |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 621 | final boolean isReachable = isReachable(did); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 622 | if (!isReachable) { |
| 623 | // device is not connected to this node |
| 624 | if (myNextRole != NONE) { |
| 625 | log.warn("Node was instructed to be {} role for {}, " |
| 626 | + "but this node cannot reach the device. " |
| 627 | + "Relinquishing role. ", |
| 628 | myNextRole, did); |
| 629 | mastershipService.relinquishMastership(did); |
| 630 | } |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | // device is connected to this node: |
alshabib | fd3cc05 | 2015-02-10 15:16:57 -0800 | [diff] [blame] | 635 | if (store.getDevice(did) != null) { |
| 636 | reassertRole(did, myNextRole); |
| 637 | } else { |
| 638 | log.warn("Device is not yet/no longer in the store: {}", did); |
| 639 | } |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 640 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 641 | } |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 642 | |
| 643 | // Store delegate to re-post events emitted from the store. |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 644 | private class InternalStoreDelegate |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 645 | implements DeviceStoreDelegate { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 646 | @Override |
| 647 | public void notify(DeviceEvent event) { |
| 648 | post(event); |
| 649 | } |
| 650 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 651 | } |