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