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