Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.device.impl; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 17 | |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 19 | import static com.google.common.base.Preconditions.checkState; |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 20 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 21 | import static org.onlab.util.Tools.groupedThreads; |
| 22 | import static org.onosproject.net.MastershipRole.MASTER; |
| 23 | import static org.onosproject.net.MastershipRole.NONE; |
| 24 | import static org.onosproject.net.MastershipRole.STANDBY; |
| 25 | import static org.onosproject.security.AppGuard.checkPermission; |
| 26 | import static org.slf4j.LoggerFactory.getLogger; |
| 27 | |
| 28 | import java.util.Collection; |
| 29 | import java.util.HashSet; |
| 30 | import java.util.List; |
| 31 | import java.util.Objects; |
| 32 | import java.util.Set; |
| 33 | import java.util.concurrent.CompletableFuture; |
| 34 | import java.util.concurrent.ExecutionException; |
| 35 | import java.util.concurrent.ScheduledExecutorService; |
| 36 | import java.util.concurrent.TimeUnit; |
| 37 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 38 | import org.apache.felix.scr.annotations.Activate; |
| 39 | import org.apache.felix.scr.annotations.Component; |
| 40 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 41 | import org.apache.felix.scr.annotations.Reference; |
| 42 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 43 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.cluster.ClusterService; |
| 45 | import org.onosproject.cluster.NodeId; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 46 | import org.onosproject.core.Permission; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 47 | import org.onosproject.event.EventDeliveryService; |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 48 | import org.onosproject.event.ListenerRegistry; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 49 | import org.onosproject.incubator.net.config.NetworkConfigEvent; |
| 50 | import org.onosproject.incubator.net.config.NetworkConfigListener; |
| 51 | import org.onosproject.incubator.net.config.NetworkConfigService; |
| 52 | import org.onosproject.incubator.net.config.basics.BasicDeviceConfig; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 53 | import org.onosproject.mastership.MastershipEvent; |
| 54 | import org.onosproject.mastership.MastershipListener; |
| 55 | import org.onosproject.mastership.MastershipService; |
| 56 | import org.onosproject.mastership.MastershipTerm; |
| 57 | import org.onosproject.mastership.MastershipTermService; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 58 | import org.onosproject.net.DefaultAnnotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 59 | import org.onosproject.net.Device; |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 60 | import org.onosproject.net.Device.Type; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 61 | import org.onosproject.net.DeviceId; |
| 62 | import org.onosproject.net.MastershipRole; |
| 63 | import org.onosproject.net.Port; |
| 64 | import org.onosproject.net.PortNumber; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 65 | import org.onosproject.net.SparseAnnotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 66 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 67 | import org.onosproject.net.device.DefaultPortDescription; |
| 68 | import org.onosproject.net.device.DeviceAdminService; |
| 69 | import org.onosproject.net.device.DeviceClockProviderService; |
| 70 | import org.onosproject.net.device.DeviceDescription; |
| 71 | import org.onosproject.net.device.DeviceEvent; |
| 72 | import org.onosproject.net.device.DeviceListener; |
| 73 | import org.onosproject.net.device.DeviceProvider; |
| 74 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 75 | import org.onosproject.net.device.DeviceProviderService; |
| 76 | import org.onosproject.net.device.DeviceService; |
| 77 | import org.onosproject.net.device.DeviceStore; |
| 78 | import org.onosproject.net.device.DeviceStoreDelegate; |
| 79 | import org.onosproject.net.device.PortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 80 | import org.onosproject.net.device.PortStatistics; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 81 | import org.onosproject.net.provider.AbstractProviderRegistry; |
| 82 | import org.onosproject.net.provider.AbstractProviderService; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 83 | import org.slf4j.Logger; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 84 | |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 85 | import com.google.common.collect.Lists; |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 86 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 87 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 88 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 89 | * Provides implementation of the device SB & NB APIs. |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 90 | */ |
| 91 | @Component(immediate = true) |
| 92 | @Service |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 93 | public class DeviceManager |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 94 | extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService> |
| 95 | implements DeviceService, DeviceAdminService, DeviceProviderRegistry { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 96 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 97 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 98 | private static final String PORT_NUMBER_NULL = "Port number cannot be null"; |
| 99 | private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null"; |
| 100 | private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null"; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 101 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 102 | private final Logger log = getLogger(getClass()); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 103 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 104 | protected final ListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry = |
| 105 | new ListenerRegistry<>(); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 106 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 107 | private final DeviceStoreDelegate delegate = new InternalStoreDelegate(); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 108 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 109 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 110 | private NodeId localNodeId; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 111 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 112 | private ScheduledExecutorService backgroundService; |
| 113 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 114 | private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener(); |
| 115 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 117 | protected DeviceStore store; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 118 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 120 | protected EventDeliveryService eventDispatcher; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 121 | |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 123 | protected ClusterService clusterService; |
| 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 126 | protected MastershipService mastershipService; |
| 127 | |
Yuta HIGUCHI | bcac499 | 2014-11-22 19:27:57 -0800 | [diff] [blame] | 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 129 | protected MastershipTermService termService; |
| 130 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 132 | protected DeviceClockProviderService deviceClockProviderService; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 133 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 135 | protected NetworkConfigService networkConfigService; |
| 136 | |
| 137 | |
| 138 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 139 | @Activate |
| 140 | public void activate() { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 141 | backgroundService = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "manager-background")); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 142 | localNodeId = clusterService.getLocalNode().id(); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 143 | |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 144 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 145 | eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 146 | mastershipService.addListener(mastershipListener); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 147 | networkConfigService.addListener(networkConfigListener); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 148 | |
| 149 | backgroundService.scheduleWithFixedDelay(new Runnable() { |
| 150 | |
| 151 | @Override |
| 152 | public void run() { |
| 153 | try { |
| 154 | mastershipCheck(); |
| 155 | } catch (Exception e) { |
| 156 | log.error("Exception thrown during integrity check", e); |
| 157 | } |
| 158 | } |
| 159 | }, 1, 1, TimeUnit.MINUTES); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 160 | log.info("Started"); |
| 161 | } |
| 162 | |
| 163 | @Deactivate |
| 164 | public void deactivate() { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 165 | backgroundService.shutdown(); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 166 | networkConfigService.removeListener(networkConfigListener); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 167 | store.unsetDelegate(delegate); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 168 | mastershipService.removeListener(mastershipListener); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 169 | eventDispatcher.removeSink(DeviceEvent.class); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 170 | log.info("Stopped"); |
| 171 | } |
| 172 | |
| 173 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 174 | public int getDeviceCount() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 175 | checkPermission(Permission.DEVICE_READ); |
| 176 | |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 177 | return store.getDeviceCount(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 181 | public Iterable<Device> getDevices() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 182 | checkPermission(Permission.DEVICE_READ); |
| 183 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 184 | return store.getDevices(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 185 | } |
| 186 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 187 | @Override |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 188 | public Iterable<Device> getAvailableDevices() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 189 | checkPermission(Permission.DEVICE_READ); |
| 190 | |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 191 | return store.getAvailableDevices(); |
| 192 | } |
| 193 | |
| 194 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 195 | public Device getDevice(DeviceId deviceId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 196 | checkPermission(Permission.DEVICE_READ); |
| 197 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 198 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 199 | return store.getDevice(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 203 | public MastershipRole getRole(DeviceId deviceId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 204 | checkPermission(Permission.DEVICE_READ); |
| 205 | |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 206 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 207 | return mastershipService.getLocalRole(deviceId); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 211 | public List<Port> getPorts(DeviceId deviceId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 212 | checkPermission(Permission.DEVICE_READ); |
| 213 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 214 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 215 | return store.getPorts(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Override |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 219 | public List<PortStatistics> getPortStatistics(DeviceId deviceId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 220 | checkPermission(Permission.DEVICE_READ); |
| 221 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 222 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 223 | return store.getPortStatistics(deviceId); |
| 224 | } |
| 225 | |
| 226 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 227 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 228 | checkPermission(Permission.DEVICE_READ); |
| 229 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 230 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 231 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 232 | return store.getPort(deviceId, portNumber); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 236 | public boolean isAvailable(DeviceId deviceId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 237 | checkPermission(Permission.DEVICE_READ); |
| 238 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 239 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 240 | return store.isAvailable(deviceId); |
| 241 | } |
| 242 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 243 | // Check a device for control channel connectivity. |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 244 | private boolean isReachable(DeviceId deviceId) { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 245 | if (deviceId == null) { |
| 246 | return false; |
| 247 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 248 | DeviceProvider provider = getProvider(deviceId); |
| 249 | if (provider != null) { |
| 250 | return provider.isReachable(deviceId); |
| 251 | } else { |
Yuta HIGUCHI | 72669c4 | 2014-11-13 14:48:17 -0800 | [diff] [blame] | 252 | log.debug("Provider not found for {}", deviceId); |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 253 | return false; |
| 254 | } |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 255 | } |
| 256 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 257 | @Override |
| 258 | public void removeDevice(DeviceId deviceId) { |
| 259 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 260 | DeviceEvent event = store.removeDevice(deviceId); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 261 | if (event != null) { |
| 262 | log.info("Device {} administratively removed", deviceId); |
| 263 | post(event); |
| 264 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 265 | } |
| 266 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 267 | @Override |
| 268 | public void addListener(DeviceListener listener) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 269 | checkPermission(Permission.DEVICE_EVENT); |
| 270 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 271 | listenerRegistry.addListener(listener); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public void removeListener(DeviceListener listener) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 276 | checkPermission(Permission.DEVICE_EVENT); |
| 277 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 278 | listenerRegistry.removeListener(listener); |
| 279 | } |
| 280 | |
| 281 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 282 | protected DeviceProviderService createProviderService( |
| 283 | DeviceProvider provider) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 284 | return new InternalDeviceProviderService(provider); |
| 285 | } |
| 286 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 287 | /** |
| 288 | * Checks if all the reachable devices have a valid mastership role. |
| 289 | */ |
| 290 | private void mastershipCheck() { |
| 291 | log.debug("Checking mastership"); |
| 292 | for (Device device : getDevices()) { |
| 293 | final DeviceId deviceId = device.id(); |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 294 | log.trace("Checking device {}", deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 295 | |
| 296 | if (!isReachable(deviceId)) { |
| 297 | continue; |
| 298 | } |
| 299 | |
| 300 | if (mastershipService.getLocalRole(deviceId) != NONE) { |
| 301 | continue; |
| 302 | } |
| 303 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 304 | log.info("{} is reachable but did not have a valid role, reasserting", |
| 305 | deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 306 | |
| 307 | // isReachable but was not MASTER or STANDBY, get a role and apply |
| 308 | // Note: NONE triggers request to MastershipService |
| 309 | reassertRole(deviceId, NONE); |
| 310 | } |
| 311 | } |
| 312 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 313 | // Personalized device provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 314 | private class InternalDeviceProviderService |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 315 | extends AbstractProviderService<DeviceProvider> |
| 316 | implements DeviceProviderService { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 317 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 318 | InternalDeviceProviderService(DeviceProvider provider) { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 319 | super(provider); |
| 320 | } |
| 321 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 322 | /** |
| 323 | * Apply role in reaction to provider event. |
| 324 | * |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 325 | * @param deviceId device identifier |
| 326 | * @param newRole new role to apply to the device |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 327 | * @return true if the request was sent to provider |
| 328 | */ |
| 329 | private boolean applyRole(DeviceId deviceId, MastershipRole newRole) { |
| 330 | |
| 331 | if (newRole.equals(MastershipRole.NONE)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 332 | //no-op |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 333 | return true; |
| 334 | } |
| 335 | |
| 336 | DeviceProvider provider = provider(); |
| 337 | if (provider == null) { |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 338 | log.warn("Provider for {} was not found. Cannot apply role {}", |
| 339 | deviceId, newRole); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 340 | return false; |
| 341 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 342 | provider.roleChanged(deviceId, newRole); |
| 343 | // not triggering probe when triggered by provider service event |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 344 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 345 | return true; |
| 346 | } |
| 347 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 348 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 349 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 350 | public void deviceConnected(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 351 | DeviceDescription deviceDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 352 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 353 | checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 354 | checkValidity(); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 355 | deviceDescription = validateDevice(deviceDescription, deviceId); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 356 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 357 | // check my Role |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 358 | CompletableFuture<MastershipRole> role = mastershipService.requestRoleFor(deviceId); |
HIGUCHI Yuta | 11530fb | 2015-05-27 13:10:20 -0700 | [diff] [blame] | 359 | try { |
| 360 | // Device subsystem must wait for role assignment |
| 361 | // to avoid losing Device information. |
| 362 | // (This node could be the only Node connected to the Device.) |
| 363 | role.get(); |
| 364 | } catch (InterruptedException e) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 365 | log.warn("Interrupted while waiting role-assignment for {}", deviceId); |
HIGUCHI Yuta | 11530fb | 2015-05-27 13:10:20 -0700 | [diff] [blame] | 366 | Thread.currentThread().interrupt(); |
| 367 | } catch (ExecutionException e) { |
| 368 | log.error("Exception thrown while waiting role-assignment for {}", |
| 369 | deviceId, e); |
| 370 | } |
| 371 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 372 | final MastershipTerm term = termService.getMastershipTerm(deviceId); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 373 | if (term == null || !localNodeId.equals(term.master())) { |
Yuta HIGUCHI | 2d3cd31 | 2014-10-31 11:38:04 -0700 | [diff] [blame] | 374 | log.info("Role of this node is STANDBY for {}", deviceId); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 375 | applyRole(deviceId, MastershipRole.STANDBY); |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 376 | } else { |
| 377 | log.info("Role of this node is MASTER for {}", deviceId); |
| 378 | // tell clock provider if this instance is the master |
| 379 | deviceClockProviderService.setMastershipTerm(deviceId, term); |
| 380 | applyRole(deviceId, MastershipRole.MASTER); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 381 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 382 | DeviceEvent event = store.createOrUpdateDevice(provider().id(), deviceId, |
| 383 | deviceDescription); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 384 | if (event != null) { |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 385 | log.trace("event: {} {}", event.type(), event); |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 386 | post(event); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 387 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 390 | // returns a DeviceDescription made from the union of the BasicDeviceConfig |
| 391 | // annotations if it exists |
| 392 | private DeviceDescription validateDevice(DeviceDescription deviceDescription, DeviceId deviceId) { |
| 393 | BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class); |
| 394 | checkState(cfg == null || cfg.isAllowed(), "Device " + deviceId + " is not allowed"); |
| 395 | log.info("Device {} connected", deviceId); |
| 396 | if (cfg != null) { |
| 397 | SparseAnnotations finalSparse = processAnnotations(cfg, deviceDescription, deviceId); |
| 398 | if (cfg.type() != Type.SWITCH) { |
| 399 | deviceDescription = new DefaultDeviceDescription(deviceDescription, |
| 400 | cfg.type(), finalSparse); |
| 401 | } else { |
| 402 | deviceDescription = new DefaultDeviceDescription(deviceDescription, |
| 403 | deviceDescription.type(), finalSparse); |
| 404 | } |
| 405 | } |
| 406 | return deviceDescription; |
| 407 | } |
| 408 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 409 | @Override |
| 410 | public void deviceDisconnected(DeviceId deviceId) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 411 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 412 | checkValidity(); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 413 | |
Yuta HIGUCHI | 2d3cd31 | 2014-10-31 11:38:04 -0700 | [diff] [blame] | 414 | log.info("Device {} disconnected from this node", deviceId); |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 415 | |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 416 | List<Port> ports = store.getPorts(deviceId); |
| 417 | List<PortDescription> descs = Lists.newArrayList(); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 418 | ports.forEach(port -> |
| 419 | descs.add(new DefaultPortDescription(port.number(), |
| 420 | false, port.type(), |
| 421 | port.portSpeed()))); |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 422 | store.updatePorts(this.provider().id(), deviceId, descs); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 423 | try { |
Thomas Vachuska | 5f429d6 | 2015-05-28 15:34:36 -0700 | [diff] [blame] | 424 | if (mastershipService.getLocalRole(deviceId) == MASTER) { |
| 425 | post(store.markOffline(deviceId)); |
| 426 | } |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 427 | } catch (IllegalStateException e) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 428 | log.warn("Failed to mark {} offline", deviceId); |
| 429 | // only the MASTER should be marking off-line in normal cases, |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 430 | // but if I was the last STANDBY connection, etc. and no one else |
| 431 | // was there to mark the device offline, this instance may need to |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 432 | // temporarily request for Master Role and mark offline. |
| 433 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 434 | //there are times when this node will correctly have mastership, BUT |
| 435 | //that isn't reflected in the ClockManager before the device disconnects. |
| 436 | //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] | 437 | |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 438 | // FIXME: Store semantics leaking out as IllegalStateException. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 439 | // Consider revising store API to handle this scenario. |
| 440 | CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 441 | roleFuture.whenComplete((role, error) -> { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 442 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
| 443 | // TODO: Move this type of check inside device clock manager, etc. |
| 444 | if (term != null && localNodeId.equals(term.master())) { |
| 445 | log.info("Retry marking {} offline", deviceId); |
| 446 | deviceClockProviderService.setMastershipTerm(deviceId, term); |
| 447 | post(store.markOffline(deviceId)); |
| 448 | } else { |
| 449 | log.info("Failed again marking {} offline. {}", deviceId, role); |
| 450 | } |
| 451 | }); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 452 | } finally { |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 453 | try { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 454 | //relinquish master role and ability to be backup. |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 455 | mastershipService.relinquishMastership(deviceId).get(); |
| 456 | } catch (InterruptedException e) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 457 | log.warn("Interrupted while reliquishing role for {}", deviceId); |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 458 | Thread.currentThread().interrupt(); |
| 459 | } catch (ExecutionException e) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 460 | log.error("Exception thrown while relinquishing role for {}", deviceId, e); |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 461 | } |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 462 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 466 | public void updatePorts(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 467 | List<PortDescription> portDescriptions) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 468 | checkNotNull(deviceId, DEVICE_ID_NULL); |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 469 | checkNotNull(portDescriptions, |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 470 | "Port descriptions list cannot be null"); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 471 | checkValidity(); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 472 | if (!deviceClockProviderService.isTimestampAvailable(deviceId)) { |
| 473 | // Never been a master for this device |
| 474 | // any update will be ignored. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 475 | log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 476 | return; |
| 477 | } |
| 478 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 479 | List<DeviceEvent> events = store.updatePorts(this.provider().id(), |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 480 | deviceId, portDescriptions); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 481 | for (DeviceEvent event : events) { |
| 482 | post(event); |
| 483 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 487 | public void portStatusChanged(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 488 | PortDescription portDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 489 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 490 | checkNotNull(portDescription, PORT_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 491 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 492 | |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 493 | if (!deviceClockProviderService.isTimestampAvailable(deviceId)) { |
| 494 | // Never been a master for this device |
| 495 | // any update will be ignored. |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 496 | log.trace("Ignoring {} port update on standby node. {}", deviceId, |
| 497 | portDescription); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 498 | return; |
| 499 | } |
| 500 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 501 | final DeviceEvent event = store.updatePortStatus(this.provider().id(), |
| 502 | deviceId, portDescription); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 503 | if (event != null) { |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 504 | log.info("Device {} port {} status changed", deviceId, event |
| 505 | .port().number()); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 506 | post(event); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 507 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 508 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 509 | |
| 510 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 511 | public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 512 | MastershipRole response) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 513 | // Several things can happen here: |
| 514 | // 1. request and response match |
| 515 | // 2. request and response don't match |
| 516 | // 3. MastershipRole and requested match (and 1 or 2 are true) |
| 517 | // 4. MastershipRole and requested don't match (and 1 or 2 are true) |
| 518 | // |
| 519 | // 2, 4, and 3 with case 2 are failure modes. |
| 520 | |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 521 | // FIXME: implement response to this notification |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 522 | |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 523 | log.debug("got reply to a role request for {}: asked for {}, and got {}", |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 524 | deviceId, requested, response); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 525 | |
| 526 | if (requested == null && response == null) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 527 | // something was off with DeviceProvider, maybe check channel too? |
| 528 | log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 529 | mastershipService.relinquishMastership(deviceId); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 530 | return; |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 531 | } |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 532 | |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 533 | if (Objects.equals(requested, response)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 534 | if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 535 | return; |
| 536 | } else { |
| 537 | return; |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 538 | // FIXME roleManager got the device to comply, but doesn't agree with |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 539 | // the store; use the store's view, then try to reassert. |
| 540 | } |
| 541 | } else { |
| 542 | // we didn't get back what we asked for. Reelect someone else. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 543 | log.warn("Failed to assert role [{}] onto Device {}", response, deviceId); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 544 | if (response == MastershipRole.MASTER) { |
| 545 | mastershipService.relinquishMastership(deviceId); |
| 546 | // TODO: Shouldn't we be triggering event? |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 547 | //final Device device = getDevice(deviceId); |
| 548 | //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device)); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 549 | } |
| 550 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 551 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 552 | |
| 553 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 554 | public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 555 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 556 | checkNotNull(portStatistics, "Port statistics list cannot be null"); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 557 | checkValidity(); |
| 558 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 559 | DeviceEvent event = store.updatePortStatistics(this.provider().id(), |
| 560 | deviceId, portStatistics); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 561 | post(event); |
| 562 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 563 | |
| 564 | // supplements or replaces deviceDescription annotations with |
| 565 | // BasicDeviceConfig annotations |
| 566 | private SparseAnnotations processAnnotations(BasicDeviceConfig cfg, DeviceDescription deviceDescription, |
| 567 | DeviceId deviceId) { |
| 568 | SparseAnnotations originalAnnotations = deviceDescription.annotations(); |
| 569 | DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder(); |
| 570 | if (cfg.driver() != deviceId.toString()) { |
| 571 | newBuilder.set(cfg.DRIVER, cfg.driver()); |
| 572 | } |
| 573 | if (cfg.type() != Type.SWITCH) { |
| 574 | newBuilder.set(cfg.TYPE, cfg.type().toString()); |
| 575 | } |
| 576 | if (cfg.name() != null) { |
| 577 | newBuilder.set(cfg.NAME, cfg.name()); |
| 578 | } |
| 579 | if (cfg.latitude() != -1) { |
| 580 | newBuilder.set(cfg.LATITUDE, Double.toString(cfg.latitude())); |
| 581 | } |
| 582 | if (cfg.longitude() != -1) { |
| 583 | newBuilder.set(cfg.LONGITUDE, Double.toString(cfg.longitude())); |
| 584 | } |
| 585 | if (cfg.rackAddress() != null) { |
| 586 | newBuilder.set(cfg.RACK_ADDRESS, cfg.rackAddress()); |
| 587 | } |
| 588 | if (cfg.owner() != null) { |
| 589 | newBuilder.set(cfg.OWNER, cfg.owner()); |
| 590 | } |
| 591 | DefaultAnnotations newAnnotations = newBuilder.build(); |
| 592 | return DefaultAnnotations.union(originalAnnotations, newAnnotations); |
| 593 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 594 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 595 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 596 | // Posts the specified event to the local event dispatcher. |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 597 | private void post(DeviceEvent event) { |
| 598 | if (event != null && eventDispatcher != null) { |
| 599 | eventDispatcher.post(event); |
| 600 | } |
| 601 | } |
| 602 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 603 | // Applies the specified role to the device; ignores NONE |
| 604 | /** |
| 605 | * Apply role to device and send probe if MASTER. |
| 606 | * |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 607 | * @param deviceId device identifier |
| 608 | * @param newRole new role to apply to the device |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 609 | * @return true if the request was sent to provider |
| 610 | */ |
| 611 | private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) { |
| 612 | if (newRole.equals(MastershipRole.NONE)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 613 | //no-op |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 614 | return true; |
| 615 | } |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 616 | |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 617 | DeviceProvider provider = getProvider(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 618 | if (provider == null) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 619 | log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 620 | return false; |
| 621 | } |
| 622 | provider.roleChanged(deviceId, newRole); |
| 623 | |
| 624 | if (newRole.equals(MastershipRole.MASTER)) { |
| 625 | // only trigger event when request was sent to provider |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 626 | provider.triggerProbe(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 627 | } |
| 628 | return true; |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Reaasert role for specified device connected to this node. |
| 633 | * |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 634 | * @param did device identifier |
| 635 | * @param nextRole role to apply. If NONE is specified, |
| 636 | * it will ask mastership service for a role and apply it. |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 637 | */ |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 638 | private void reassertRole(final DeviceId did, |
| 639 | final MastershipRole nextRole) { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 640 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 641 | MastershipRole myNextRole = nextRole; |
| 642 | if (myNextRole == NONE) { |
| 643 | mastershipService.requestRoleFor(did); |
| 644 | MastershipTerm term = termService.getMastershipTerm(did); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 645 | if (term != null && localNodeId.equals(term.master())) { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 646 | myNextRole = MASTER; |
| 647 | } else { |
| 648 | myNextRole = STANDBY; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | switch (myNextRole) { |
| 653 | case MASTER: |
| 654 | final Device device = getDevice(did); |
| 655 | if ((device != null) && !isAvailable(did)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 656 | //flag the device as online. Is there a better way to do this? |
| 657 | DefaultDeviceDescription deviceDescription |
| 658 | = new DefaultDeviceDescription(did.uri(), |
| 659 | device.type(), |
| 660 | device.manufacturer(), |
| 661 | device.hwVersion(), |
| 662 | device.swVersion(), |
| 663 | device.serialNumber(), |
| 664 | device.chassisId()); |
| 665 | DeviceEvent devEvent = |
| 666 | store.createOrUpdateDevice(device.providerId(), did, |
| 667 | deviceDescription); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 668 | post(devEvent); |
| 669 | } |
| 670 | // TODO: should apply role only if there is mismatch |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 671 | log.debug("Applying role {} to {}", myNextRole, did); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 672 | if (!applyRoleAndProbe(did, MASTER)) { |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 673 | log.warn("Unsuccessful applying role {} to {}", myNextRole, did); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 674 | // immediately failed to apply role |
| 675 | mastershipService.relinquishMastership(did); |
| 676 | // FIXME disconnect? |
| 677 | } |
| 678 | break; |
| 679 | case STANDBY: |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 680 | log.debug("Applying role {} to {}", myNextRole, did); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 681 | if (!applyRoleAndProbe(did, STANDBY)) { |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 682 | log.warn("Unsuccessful applying role {} to {}", myNextRole, did); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 683 | // immediately failed to apply role |
| 684 | mastershipService.relinquishMastership(did); |
| 685 | // FIXME disconnect? |
| 686 | } |
| 687 | break; |
| 688 | case NONE: |
| 689 | default: |
| 690 | // should never reach here |
| 691 | log.error("You didn't see anything. I did not exist."); |
| 692 | break; |
| 693 | } |
| 694 | } |
| 695 | |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 696 | private void handleMastershipEvent(MastershipEvent event) { |
| 697 | if (event.type() != MastershipEvent.Type.MASTER_CHANGED) { |
| 698 | // Don't care if backup list changed. |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | final DeviceId did = event.subject(); |
| 703 | |
| 704 | // myRole suggested by MastershipService |
| 705 | MastershipRole myNextRole; |
| 706 | if (localNodeId.equals(event.roleInfo().master())) { |
| 707 | // confirm latest info |
| 708 | MastershipTerm term = termService.getMastershipTerm(did); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 709 | final boolean iHaveControl = term != null && localNodeId.equals(term.master()); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 710 | if (iHaveControl) { |
| 711 | deviceClockProviderService.setMastershipTerm(did, term); |
| 712 | myNextRole = MASTER; |
| 713 | } else { |
| 714 | myNextRole = STANDBY; |
| 715 | } |
| 716 | } else if (event.roleInfo().backups().contains(localNodeId)) { |
| 717 | myNextRole = STANDBY; |
| 718 | } else { |
| 719 | myNextRole = NONE; |
| 720 | } |
| 721 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 722 | |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 723 | final boolean isReachable = isReachable(did); |
| 724 | if (!isReachable) { |
| 725 | // device is not connected to this node |
| 726 | if (myNextRole != NONE) { |
| 727 | log.warn("Node was instructed to be {} role for {}, " |
| 728 | + "but this node cannot reach the device. " |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 729 | + "Relinquishing role. ", |
| 730 | myNextRole, did); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 731 | mastershipService.relinquishMastership(did); |
| 732 | } |
| 733 | return; |
| 734 | } |
| 735 | |
| 736 | // device is connected to this node: |
| 737 | if (store.getDevice(did) != null) { |
| 738 | reassertRole(did, myNextRole); |
| 739 | } else { |
| 740 | log.debug("Device is not yet/no longer in the store: {}", did); |
| 741 | } |
| 742 | } |
| 743 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 744 | // Intercepts mastership events |
| 745 | private class InternalMastershipListener implements MastershipListener { |
| 746 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 747 | @Override |
| 748 | public void event(MastershipEvent event) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 749 | backgroundService.submit(() -> { |
| 750 | try { |
| 751 | handleMastershipEvent(event); |
| 752 | } catch (Exception e) { |
| 753 | log.warn("Failed to handle {}", event, e); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 754 | } |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 755 | }); |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 756 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 757 | } |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 758 | |
| 759 | // Store delegate to re-post events emitted from the store. |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 760 | private class InternalStoreDelegate implements DeviceStoreDelegate { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 761 | @Override |
| 762 | public void notify(DeviceEvent event) { |
| 763 | post(event); |
| 764 | } |
| 765 | } |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 766 | |
| 767 | @Override |
| 768 | public Iterable<Device> getDevices(Type type) { |
| 769 | checkPermission(Permission.DEVICE_READ); |
| 770 | Set<Device> results = new HashSet<>(); |
| 771 | Iterable<Device> devices = store.getDevices(); |
| 772 | if (devices != null) { |
| 773 | devices.forEach(d -> { |
| 774 | if (type.equals(d.type())) { |
| 775 | results.add(d); |
| 776 | } |
| 777 | }); |
| 778 | } |
| 779 | return results; |
| 780 | } |
| 781 | |
| 782 | @Override |
| 783 | public Iterable<Device> getAvailableDevices(Type type) { |
| 784 | checkPermission(Permission.DEVICE_READ); |
| 785 | Set<Device> results = new HashSet<>(); |
| 786 | Iterable<Device> availableDevices = store.getAvailableDevices(); |
| 787 | if (availableDevices != null) { |
| 788 | availableDevices.forEach(d -> { |
| 789 | if (type.equals(d.type())) { |
| 790 | results.add(d); |
| 791 | } |
| 792 | }); |
| 793 | } |
| 794 | return results; |
| 795 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame^] | 796 | |
| 797 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 798 | @Override |
| 799 | public void event(NetworkConfigEvent event) { |
| 800 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 801 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 802 | event.configClass().equals(BasicDeviceConfig.class)) { |
| 803 | log.info("Detected Device network config event {}", event.type()); |
| 804 | kickOutBadDevice(((DeviceId) event.subject())); |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | // checks if the specified device is allowed by the BasicDeviceConfig |
| 810 | // and if not, removes it |
| 811 | private void kickOutBadDevice(DeviceId deviceId) { |
| 812 | BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class); |
| 813 | if (!cfg.isAllowed()) { |
| 814 | Device badDevice = getDevice(deviceId); |
| 815 | if (badDevice != null) { |
| 816 | removeDevice(deviceId); |
| 817 | } else { |
| 818 | log.info("Failed removal: Device {} does not exist", deviceId); |
| 819 | } |
| 820 | } |
| 821 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 822 | } |