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