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