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; |
| 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
| 22 | import java.util.List; |
| 23 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Service; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 30 | import org.onlab.onos.cluster.ClusterService; |
Yuta HIGUCHI | 8e939d8 | 2014-10-08 08:09:18 -0700 | [diff] [blame] | 31 | import org.onlab.onos.cluster.NodeId; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 32 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 33 | import org.onlab.onos.event.EventDeliveryService; |
Yuta HIGUCHI | 80912e6 | 2014-10-12 00:15:47 -0700 | [diff] [blame] | 34 | import org.onlab.onos.mastership.MastershipEvent; |
| 35 | import org.onlab.onos.mastership.MastershipListener; |
| 36 | import org.onlab.onos.mastership.MastershipService; |
| 37 | import org.onlab.onos.mastership.MastershipTerm; |
| 38 | import org.onlab.onos.mastership.MastershipTermService; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.Device; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.DeviceId; |
| 41 | import org.onlab.onos.net.MastershipRole; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.Port; |
| 43 | import org.onlab.onos.net.PortNumber; |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 44 | import org.onlab.onos.net.device.DefaultDeviceDescription; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.device.DeviceAdminService; |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 46 | import org.onlab.onos.net.device.DeviceClockProviderService; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 47 | import org.onlab.onos.net.device.DeviceDescription; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 48 | import org.onlab.onos.net.device.DeviceEvent; |
| 49 | import org.onlab.onos.net.device.DeviceListener; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 50 | import org.onlab.onos.net.device.DeviceProvider; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 51 | import org.onlab.onos.net.device.DeviceProviderRegistry; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 52 | import org.onlab.onos.net.device.DeviceProviderService; |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 53 | import org.onlab.onos.net.device.DeviceService; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 54 | import org.onlab.onos.net.device.DeviceStore; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 55 | import org.onlab.onos.net.device.DeviceStoreDelegate; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 56 | import org.onlab.onos.net.device.PortDescription; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 57 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 58 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 59 | import org.slf4j.Logger; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 60 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 61 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 62 | * Provides implementation of the device SB & NB APIs. |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 63 | */ |
| 64 | @Component(immediate = true) |
| 65 | @Service |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 66 | public class DeviceManager |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 67 | extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService> |
| 68 | implements DeviceService, DeviceAdminService, DeviceProviderRegistry { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 69 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 70 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 71 | private static final String PORT_NUMBER_NULL = "Port number cannot be null"; |
| 72 | private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null"; |
| 73 | private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null"; |
| 74 | private static final String ROLE_NULL = "Role cannot be null"; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 75 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 76 | private final Logger log = getLogger(getClass()); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 77 | |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 78 | protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry = |
| 79 | new AbstractListenerRegistry<>(); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 80 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 81 | private final DeviceStoreDelegate delegate = new InternalStoreDelegate(); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 82 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 83 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 84 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected DeviceStore store; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 87 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 89 | protected EventDeliveryService eventDispatcher; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 90 | |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 92 | protected ClusterService clusterService; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 95 | protected MastershipService mastershipService; |
| 96 | |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 97 | protected MastershipTermService termService; |
| 98 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 100 | protected DeviceClockProviderService deviceClockProviderService; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 101 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 102 | @Activate |
| 103 | public void activate() { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 104 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 105 | eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 106 | mastershipService.addListener(mastershipListener); |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 107 | termService = mastershipService.requestTermService(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 108 | log.info("Started"); |
| 109 | } |
| 110 | |
| 111 | @Deactivate |
| 112 | public void deactivate() { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 113 | store.unsetDelegate(delegate); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 114 | mastershipService.removeListener(mastershipListener); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 115 | eventDispatcher.removeSink(DeviceEvent.class); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 116 | log.info("Stopped"); |
| 117 | } |
| 118 | |
| 119 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 120 | public int getDeviceCount() { |
| 121 | return store.getDeviceCount(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 125 | public Iterable<Device> getDevices() { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 126 | return store.getDevices(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 127 | } |
| 128 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 129 | @Override |
| 130 | public Device getDevice(DeviceId deviceId) { |
| 131 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 132 | return store.getDevice(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 136 | public MastershipRole getRole(DeviceId deviceId) { |
| 137 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 138 | return mastershipService.getLocalRole(deviceId); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 142 | public List<Port> getPorts(DeviceId deviceId) { |
| 143 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 144 | return store.getPorts(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 149 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 150 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 151 | return store.getPort(deviceId, portNumber); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 155 | public boolean isAvailable(DeviceId deviceId) { |
| 156 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 157 | return store.isAvailable(deviceId); |
| 158 | } |
| 159 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 160 | // Applies the specified role to the device; ignores NONE |
| 161 | private void applyRole(DeviceId deviceId, MastershipRole newRole) { |
Yuta HIGUCHI | d631e42 | 2014-10-19 00:18:59 -0700 | [diff] [blame] | 162 | if (!newRole.equals(MastershipRole.NONE)) { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 163 | Device device = store.getDevice(deviceId); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 164 | // FIXME: Device might not be there yet. (eventual consistent) |
| 165 | if (device == null) { |
| 166 | return; |
| 167 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 168 | DeviceProvider provider = getProvider(device.providerId()); |
| 169 | if (provider != null) { |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 170 | provider.roleChanged(device, newRole); |
Yuta HIGUCHI | 67dce88 | 2014-10-21 21:13:26 -0700 | [diff] [blame] | 171 | |
| 172 | // only trigger event when request was sent to provider |
| 173 | // TODO: consider removing this from Device event type? |
| 174 | post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device)); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 175 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 179 | // Queries a device for port information. |
| 180 | private void queryPortInfo(DeviceId deviceId) { |
| 181 | Device device = store.getDevice(deviceId); |
| 182 | // FIXME: Device might not be there yet. (eventual consistent) |
| 183 | if (device == null) { |
| 184 | return; |
| 185 | } |
| 186 | DeviceProvider provider = getProvider(device.providerId()); |
| 187 | provider.triggerProbe(device); |
| 188 | } |
| 189 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 190 | @Override |
| 191 | public void removeDevice(DeviceId deviceId) { |
| 192 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 193 | // XXX is this intended to apply to the full global topology? |
| 194 | // if so, we probably don't want the fact that we aren't |
| 195 | // MASTER to get in the way, as it would do now. |
Yuta HIGUCHI | 67dce88 | 2014-10-21 21:13:26 -0700 | [diff] [blame] | 196 | // FIXME: forward or broadcast and let the Master handler the event. |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 197 | DeviceEvent event = store.removeDevice(deviceId); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 198 | if (event != null) { |
| 199 | log.info("Device {} administratively removed", deviceId); |
| 200 | post(event); |
| 201 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 202 | } |
| 203 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 204 | @Override |
| 205 | public void addListener(DeviceListener listener) { |
| 206 | listenerRegistry.addListener(listener); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void removeListener(DeviceListener listener) { |
| 211 | listenerRegistry.removeListener(listener); |
| 212 | } |
| 213 | |
| 214 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 215 | protected DeviceProviderService createProviderService( |
| 216 | DeviceProvider provider) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 217 | return new InternalDeviceProviderService(provider); |
| 218 | } |
| 219 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 220 | // Personalized device provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 221 | private class InternalDeviceProviderService |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 222 | extends AbstractProviderService<DeviceProvider> |
| 223 | implements DeviceProviderService { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 224 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 225 | InternalDeviceProviderService(DeviceProvider provider) { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 226 | super(provider); |
| 227 | } |
| 228 | |
| 229 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 230 | public void deviceConnected(DeviceId deviceId, |
| 231 | DeviceDescription deviceDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 232 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 233 | checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 234 | checkValidity(); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 235 | |
| 236 | log.info("Device {} connected", deviceId); |
| 237 | // check my Role |
| 238 | MastershipRole role = mastershipService.requestRoleFor(deviceId); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 239 | log.info("requestedRole, became {} for {}", role, deviceId); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 240 | if (role != MastershipRole.MASTER) { |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 241 | // TODO: Do we need to explicitly tell the Provider that |
| 242 | // this instance is no longer the MASTER? probably not |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 243 | // Device device = getDevice(deviceId); |
| 244 | // if (device != null) { |
| 245 | // // FIXME roleChanged should take DeviceId instead of Device |
| 246 | // provider().roleChanged(device, role); |
| 247 | // } |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 248 | return; |
| 249 | } |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 250 | MastershipTerm term = mastershipService.requestTermService() |
| 251 | .getMastershipTerm(deviceId); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 252 | |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 253 | if (!term.master().equals(clusterService.getLocalNode().id())) { |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 254 | // lost mastership after requestRole told this instance was MASTER. |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 255 | log.info("lost mastership before getting term info."); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 256 | return; |
| 257 | } |
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(), |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 263 | deviceId, deviceDescription); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 264 | |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 265 | // If there was a change of any kind, tell the provider |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 266 | // that this instance is the master. |
| 267 | // Note: event can be null, if mastership was lost between |
| 268 | // roleRequest and store update calls. |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 269 | if (event != null) { |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 270 | // TODO: Check switch reconnected case. Is it assured that |
| 271 | // event will never be null? |
| 272 | // Could there be a situation MastershipService told this |
| 273 | // instance is the new Master, but |
| 274 | // event returned from the store is null? |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 275 | |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 276 | // FIXME: 1st argument should be deviceId, to allow setting |
| 277 | // certain roles even if the store returned null. |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 278 | log.info("event: {} {}", event.type(), event); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 279 | provider().roleChanged(event.subject(), role); |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 280 | post(event); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 281 | } else { |
| 282 | log.info("No event to publish"); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 283 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public void deviceDisconnected(DeviceId deviceId) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 288 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 289 | checkValidity(); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 290 | |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 291 | |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 292 | DeviceEvent event = null; |
| 293 | try { |
| 294 | event = store.markOffline(deviceId); |
| 295 | } catch (IllegalStateException e) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 296 | log.warn("Failed to mark {} offline", deviceId); |
| 297 | // only the MASTER should be marking off-line in normal cases, |
| 298 | // but if I was the last STANDBY connection, etc. and no one else |
| 299 | // was there to mark the device offline, this instance may need to |
| 300 | // temporarily request for Master Role and mark offline. |
| 301 | |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 302 | //there are times when this node will correctly have mastership, BUT |
| 303 | //that isn't reflected in the ClockManager before the device disconnects. |
| 304 | //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] | 305 | |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 306 | // FIXME: Store semantics leaking out as IllegalStateException. |
| 307 | // Consider revising store API to handle this scenario. |
| 308 | |
| 309 | MastershipRole role = mastershipService.requestRoleFor(deviceId); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 310 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
Yuta HIGUCHI | 0722fb2 | 2014-10-19 01:16:33 -0700 | [diff] [blame] | 311 | final NodeId myNodeId = clusterService.getLocalNode().id(); |
| 312 | // TODO: Move this type of check inside device clock manager, etc. |
| 313 | if (myNodeId.equals(term.master())) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 314 | log.info("Marking {} offline", deviceId); |
Yuta HIGUCHI | 0722fb2 | 2014-10-19 01:16:33 -0700 | [diff] [blame] | 315 | deviceClockProviderService.setMastershipTerm(deviceId, term); |
| 316 | event = store.markOffline(deviceId); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 317 | } else { |
| 318 | log.error("Failed again marking {} offline. {}", deviceId, role); |
Yuta HIGUCHI | 0722fb2 | 2014-10-19 01:16:33 -0700 | [diff] [blame] | 319 | } |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 320 | } finally { |
| 321 | //relinquish master role and ability to be backup. |
| 322 | mastershipService.relinquishMastership(deviceId); |
| 323 | |
| 324 | if (event != null) { |
| 325 | log.info("Device {} disconnected", deviceId); |
| 326 | post(event); |
| 327 | } |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 328 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 332 | public void updatePorts(DeviceId deviceId, |
| 333 | List<PortDescription> portDescriptions) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 334 | checkNotNull(deviceId, DEVICE_ID_NULL); |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 335 | checkNotNull(portDescriptions, |
| 336 | "Port descriptions list cannot be null"); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 337 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 338 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 339 | List<DeviceEvent> events = store.updatePorts(this.provider().id(), |
| 340 | deviceId, portDescriptions); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 341 | for (DeviceEvent event : events) { |
| 342 | post(event); |
| 343 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 347 | public void portStatusChanged(DeviceId deviceId, |
| 348 | PortDescription portDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 349 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 350 | checkNotNull(portDescription, PORT_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 351 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 352 | |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 353 | final DeviceEvent event = store.updatePortStatus(this.provider().id(), |
| 354 | deviceId, portDescription); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 355 | if (event != null) { |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 356 | log.info("Device {} port {} status changed", deviceId, event |
| 357 | .port().number()); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 358 | post(event); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 359 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 360 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 361 | |
| 362 | @Override |
| 363 | public void unableToAssertRole(DeviceId deviceId, MastershipRole role) { |
| 364 | // FIXME: implement response to this notification |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 365 | log.warn("Failed to assert role [{}] onto Device {}", role, |
| 366 | deviceId); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 367 | if (role == MastershipRole.MASTER) { |
| 368 | mastershipService.relinquishMastership(deviceId); |
Yuta HIGUCHI | 67dce88 | 2014-10-21 21:13:26 -0700 | [diff] [blame] | 369 | // TODO: Shouldn't we be triggering event? |
| 370 | //final Device device = getDevice(deviceId); |
| 371 | //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device)); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 372 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 373 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 374 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 375 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 376 | // Posts the specified event to the local event dispatcher. |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 377 | private void post(DeviceEvent event) { |
| 378 | if (event != null && eventDispatcher != null) { |
| 379 | eventDispatcher.post(event); |
| 380 | } |
| 381 | } |
| 382 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 383 | // Intercepts mastership events |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 384 | private class InternalMastershipListener implements MastershipListener { |
| 385 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 386 | @Override |
| 387 | public void event(MastershipEvent event) { |
Ayaka Koshibe | 4c89127 | 2014-10-08 17:14:16 -0700 | [diff] [blame] | 388 | final DeviceId did = event.subject(); |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 389 | final NodeId myNodeId = clusterService.getLocalNode().id(); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 390 | |
Yuta HIGUCHI | 67dce88 | 2014-10-21 21:13:26 -0700 | [diff] [blame] | 391 | if (myNodeId.equals(event.roleInfo().master())) { |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 392 | MastershipTerm term = termService.getMastershipTerm(did); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 393 | |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 394 | if (!myNodeId.equals(term.master())) { |
| 395 | // something went wrong in consistency, let go |
Yuta HIGUCHI | 305ec83 | 2014-10-24 22:23:17 -0700 | [diff] [blame] | 396 | log.warn("Mastership has changed after this event." |
| 397 | + "Term Service suggests {} for {}", term, did); |
| 398 | // FIXME: Is it possible to let go of MASTER role |
| 399 | // but remain on STANDBY list? |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 400 | mastershipService.relinquishMastership(did); |
| 401 | applyRole(did, MastershipRole.STANDBY); |
| 402 | return; |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 403 | } |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 404 | |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 405 | // only set the new term if I am the master |
| 406 | deviceClockProviderService.setMastershipTerm(did, term); |
| 407 | |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 408 | // FIXME: we should check that the device is connected on our end. |
| 409 | // currently, this is not straight forward as the actual switch |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 410 | // implementation is hidden from the registry. Maybe we can ask the |
| 411 | // provider. |
| 412 | // if the device is null here, we are the first master to claim the |
| 413 | // device. No worries, the DeviceManager will create one soon. |
| 414 | Device device = getDevice(did); |
| 415 | if ((device != null) && !isAvailable(did)) { |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 416 | //flag the device as online. Is there a better way to do this? |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 417 | DeviceEvent devEvent = store.createOrUpdateDevice(device.providerId(), did, |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 418 | new DefaultDeviceDescription( |
| 419 | did.uri(), device.type(), device.manufacturer(), |
| 420 | device.hwVersion(), device.swVersion(), |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 421 | device.serialNumber(), device.chassisId())); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 422 | post(devEvent); |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 423 | } |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 424 | applyRole(did, MastershipRole.MASTER); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 425 | // re-collect device information to fix potential staleness |
| 426 | queryPortInfo(did); |
Yuta HIGUCHI | 67dce88 | 2014-10-21 21:13:26 -0700 | [diff] [blame] | 427 | } else if (event.roleInfo().backups().contains(myNodeId)) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 428 | applyRole(did, MastershipRole.STANDBY); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | } |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 432 | |
| 433 | // Store delegate to re-post events emitted from the store. |
alshabib | 271a620 | 2014-09-28 22:11:21 -0700 | [diff] [blame] | 434 | private class InternalStoreDelegate |
| 435 | implements DeviceStoreDelegate { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 436 | @Override |
| 437 | public void notify(DeviceEvent event) { |
| 438 | post(event); |
| 439 | } |
| 440 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 441 | } |