Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [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 | */ |
| 16 | |
| 17 | package org.onosproject.provider.rest.device.impl; |
| 18 | |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 19 | import com.google.common.base.Objects; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.onlab.packet.ChassisId; |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 27 | import org.onlab.util.SharedExecutors; |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 28 | import org.onlab.util.SharedScheduledExecutorService; |
| 29 | import org.onlab.util.SharedScheduledExecutors; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
Marc De Leenheer | b0d131c | 2016-03-01 20:34:59 -0800 | [diff] [blame] | 32 | import org.onosproject.net.AnnotationKeys; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 33 | import org.onosproject.net.DefaultAnnotations; |
| 34 | import org.onosproject.net.Device; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.MastershipRole; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 37 | import org.onosproject.net.PortNumber; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 38 | import org.onosproject.net.SparseAnnotations; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 39 | import org.onosproject.net.behaviour.DevicesDiscovery; |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 40 | import org.onosproject.net.behaviour.PortAdmin; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 41 | import org.onosproject.net.config.ConfigFactory; |
| 42 | import org.onosproject.net.config.NetworkConfigEvent; |
| 43 | import org.onosproject.net.config.NetworkConfigListener; |
| 44 | import org.onosproject.net.config.NetworkConfigRegistry; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 45 | import org.onosproject.net.config.basics.SubjectFactories; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 46 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 47 | import org.onosproject.net.device.DeviceDescription; |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 48 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 49 | import org.onosproject.net.device.DeviceProvider; |
| 50 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 51 | import org.onosproject.net.device.DeviceProviderService; |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 52 | import org.onosproject.net.device.DeviceService; |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 53 | import org.onosproject.net.device.PortStatistics; |
| 54 | import org.onosproject.net.device.PortStatisticsDiscovery; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 55 | import org.onosproject.net.driver.DefaultDriverData; |
| 56 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 57 | import org.onosproject.net.driver.Driver; |
| 58 | import org.onosproject.net.driver.DriverData; |
| 59 | import org.onosproject.net.driver.DriverHandler; |
| 60 | import org.onosproject.net.driver.DriverService; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 61 | import org.onosproject.net.provider.AbstractProvider; |
| 62 | import org.onosproject.net.provider.ProviderId; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 63 | import org.onosproject.protocol.rest.DefaultRestSBDevice; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 64 | import org.onosproject.protocol.rest.RestSBController; |
| 65 | import org.onosproject.protocol.rest.RestSBDevice; |
| 66 | import org.slf4j.Logger; |
| 67 | |
Andrea Campanella | c6ecc63 | 2016-03-10 17:57:06 -0800 | [diff] [blame] | 68 | import javax.ws.rs.ProcessingException; |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 69 | import javax.ws.rs.core.MediaType; |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 70 | import java.util.Collection; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 71 | import java.util.HashSet; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 72 | import java.util.List; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 73 | import java.util.Set; |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 74 | import java.util.concurrent.Callable; |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 75 | import java.util.concurrent.CompletableFuture; |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 76 | import java.util.concurrent.ExecutionException; |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 77 | import java.util.concurrent.ExecutorService; |
| 78 | import java.util.concurrent.Executors; |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 79 | import java.util.concurrent.Future; |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 80 | import java.util.concurrent.ScheduledFuture; |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 81 | import java.util.concurrent.TimeUnit; |
| 82 | import java.util.concurrent.TimeoutException; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 83 | import java.util.stream.Collectors; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 84 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 85 | import static com.google.common.base.Preconditions.checkNotNull; |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 86 | import static org.onlab.util.Tools.groupedThreads; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 87 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED; |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 88 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REMOVED; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 89 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 90 | import static org.slf4j.LoggerFactory.getLogger; |
| 91 | |
| 92 | /** |
| 93 | * Provider for devices that use REST as means of configuration communication. |
| 94 | */ |
| 95 | @Component(immediate = true) |
| 96 | public class RestDeviceProvider extends AbstractProvider |
| 97 | implements DeviceProvider { |
| 98 | private static final String APP_NAME = "org.onosproject.restsb"; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 99 | protected static final String REST = "rest"; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 100 | private static final String PROVIDER = "org.onosproject.provider.rest.device"; |
| 101 | private static final String IPADDRESS = "ipaddress"; |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 102 | private static final String ISNOTNULL = "Rest device is not null"; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 103 | private static final String UNKNOWN = "unknown"; |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 104 | private static final int REST_TIMEOUT_SEC = 5; |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 105 | private static final int DEFAULT_POLL_FREQUENCY_SECONDS = 30; |
Georgios Katsikas | 6a4d166 | 2017-07-27 13:22:31 +0200 | [diff] [blame] | 106 | private static final int EXECUTOR_THREAD_POOL_SIZE = 8; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 107 | private final Logger log = getLogger(getClass()); |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected DeviceProviderRegistry providerRegistry; |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 113 | protected RestSBController controller; |
| 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected NetworkConfigRegistry cfgService; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 119 | protected CoreService coreService; |
| 120 | |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 122 | protected DeviceService deviceService; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 123 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | protected DriverService driverService; |
| 126 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 127 | private DeviceProviderService providerService; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 128 | private ApplicationId appId; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 129 | |
Michal Mach | 13072e2 | 2017-06-21 09:12:24 +0200 | [diff] [blame] | 130 | private ExecutorService executor; |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 131 | private final SharedScheduledExecutorService portStatisticsExecutor = |
| 132 | SharedScheduledExecutors.getPoolThreadExecutor(); |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 133 | |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 134 | private final List<ConfigFactory> factories = ImmutableList.of( |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 135 | new ConfigFactory<DeviceId, RestDeviceConfig>(SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 136 | RestDeviceConfig.class, |
| 137 | REST) { |
| 138 | @Override |
| 139 | public RestDeviceConfig createConfig() { |
| 140 | return new RestDeviceConfig(); |
| 141 | } |
| 142 | }); |
| 143 | |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 144 | private final NetworkConfigListener configListener = new InternalNetworkConfigListener(); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 145 | |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 146 | private ScheduledFuture<?> scheduledTask; |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 147 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 148 | |
| 149 | @Activate |
| 150 | public void activate() { |
| 151 | appId = coreService.registerApplication(APP_NAME); |
| 152 | providerService = providerRegistry.register(this); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 153 | factories.forEach(cfgService::registerConfigFactory); |
Georgios Katsikas | 6a4d166 | 2017-07-27 13:22:31 +0200 | [diff] [blame] | 154 | executor = Executors.newFixedThreadPool( |
| 155 | EXECUTOR_THREAD_POOL_SIZE, groupedThreads("onos/restsbprovider", "device-installer-%d", log) |
| 156 | ); |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 157 | cfgService.addListener(configListener); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 158 | executor.execute(RestDeviceProvider.this::createAndConnectDevices); |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 159 | scheduledTask = schedulePolling(); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 160 | log.info("Started"); |
| 161 | } |
| 162 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 163 | @Deactivate |
| 164 | public void deactivate() { |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 165 | cfgService.removeListener(configListener); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 166 | providerRegistry.unregister(this); |
| 167 | providerService = null; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 168 | factories.forEach(cfgService::unregisterConfigFactory); |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 169 | scheduledTask.cancel(true); |
Lukasz Ryba | 4da35c5 | 2017-06-20 09:14:11 +0200 | [diff] [blame] | 170 | executor.shutdown(); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 171 | log.info("Stopped"); |
| 172 | } |
| 173 | |
| 174 | public RestDeviceProvider() { |
| 175 | super(new ProviderId(REST, PROVIDER)); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void triggerProbe(DeviceId deviceId) { |
| 180 | // TODO: This will be implemented later. |
| 181 | log.info("Triggering probe on device {}", deviceId); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
| 186 | // TODO: This will be implemented later. |
| 187 | } |
| 188 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 189 | @Override |
| 190 | public boolean isReachable(DeviceId deviceId) { |
| 191 | RestSBDevice restDevice = controller.getDevice(deviceId); |
| 192 | if (restDevice == null) { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 193 | restDevice = controller.getProxySBDevice(deviceId); |
| 194 | if (restDevice == null) { |
| 195 | log.debug("the requested device id: " + |
| 196 | deviceId.toString() + |
| 197 | " is not associated to any REST or REST " + |
| 198 | "proxy Device"); |
| 199 | return false; |
| 200 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 201 | } |
| 202 | return restDevice.isActive(); |
| 203 | } |
| 204 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 205 | private void deviceAdded(RestSBDevice restSBDev) { |
| 206 | checkNotNull(restSBDev, ISNOTNULL); |
| 207 | |
| 208 | //check if the server is controlling a single or multiple devices |
| 209 | if (restSBDev.isProxy()) { |
| 210 | |
| 211 | Driver driver = driverService.getDriver(restSBDev.manufacturer().get(), |
| 212 | restSBDev.hwVersion().get(), |
| 213 | restSBDev.swVersion().get()); |
| 214 | |
| 215 | if (driver != null && driver.hasBehaviour(DevicesDiscovery.class)) { |
| 216 | |
| 217 | //Creates the driver to communicate with the server |
| 218 | DevicesDiscovery devicesDiscovery = |
| 219 | devicesDiscovery(restSBDev, driver); |
| 220 | Set<DeviceId> deviceIds = devicesDiscovery.deviceIds(); |
| 221 | restSBDev.setActive(true); |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 222 | deviceIds.forEach(deviceId -> { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 223 | controller.addProxiedDevice(deviceId, restSBDev); |
| 224 | DeviceDescription devDesc = |
| 225 | devicesDiscovery.deviceDetails(deviceId); |
| 226 | checkNotNull(devDesc, |
| 227 | "deviceDescription cannot be null"); |
| 228 | providerService.deviceConnected( |
| 229 | deviceId, mergeAnn(restSBDev.deviceId(), devDesc)); |
| 230 | |
| 231 | if (driver.hasBehaviour(DeviceDescriptionDiscovery.class)) { |
| 232 | DriverHandler h = driverService.createHandler(deviceId); |
| 233 | DeviceDescriptionDiscovery devDisc = |
| 234 | h.behaviour(DeviceDescriptionDiscovery.class); |
| 235 | providerService.updatePorts(deviceId, |
| 236 | devDisc.discoverPortDetails()); |
| 237 | } |
| 238 | |
| 239 | checkAndUpdateDevice(deviceId); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 240 | }); |
| 241 | } else { |
| 242 | log.warn("Driver not found for {}", restSBDev); |
| 243 | } |
| 244 | } else { |
| 245 | DeviceId deviceId = restSBDev.deviceId(); |
| 246 | ChassisId cid = new ChassisId(); |
| 247 | String ipAddress = restSBDev.ip().toString(); |
| 248 | SparseAnnotations annotations = DefaultAnnotations.builder() |
| 249 | .set(IPADDRESS, ipAddress) |
| 250 | .set(AnnotationKeys.PROTOCOL, REST.toUpperCase()) |
| 251 | .build(); |
| 252 | DeviceDescription deviceDescription = new DefaultDeviceDescription( |
| 253 | deviceId.uri(), |
| 254 | Device.Type.SWITCH, |
| 255 | UNKNOWN, UNKNOWN, |
| 256 | UNKNOWN, UNKNOWN, |
| 257 | cid, |
| 258 | annotations); |
| 259 | restSBDev.setActive(true); |
| 260 | providerService.deviceConnected(deviceId, deviceDescription); |
| 261 | checkAndUpdateDevice(deviceId); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | private DefaultDeviceDescription mergeAnn(DeviceId devId, DeviceDescription desc) { |
| 266 | return new DefaultDeviceDescription( |
| 267 | desc, |
| 268 | DefaultAnnotations.merge( |
| 269 | DefaultAnnotations.builder() |
| 270 | .set(AnnotationKeys.PROTOCOL, REST.toUpperCase()) |
| 271 | // The rest server added as annotation to the device |
| 272 | .set(AnnotationKeys.REST_SERVER, devId.toString()) |
| 273 | .build(), |
| 274 | desc.annotations())); |
| 275 | } |
| 276 | |
| 277 | private DevicesDiscovery devicesDiscovery(RestSBDevice restSBDevice, Driver driver) { |
| 278 | DriverData driverData = new DefaultDriverData(driver, restSBDevice.deviceId()); |
| 279 | DevicesDiscovery devicesDiscovery = driver.createBehaviour(driverData, |
| 280 | DevicesDiscovery.class); |
| 281 | devicesDiscovery.setHandler(new DefaultDriverHandler(driverData)); |
| 282 | return devicesDiscovery; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 285 | private void checkAndUpdateDevice(DeviceId deviceId) { |
| 286 | if (deviceService.getDevice(deviceId) == null) { |
| 287 | log.warn("Device {} has not been added to store, " + |
| 288 | "maybe due to a problem in connectivity", deviceId); |
| 289 | } else { |
| 290 | boolean isReachable = isReachable(deviceId); |
| 291 | if (isReachable && deviceService.isAvailable(deviceId)) { |
| 292 | Device device = deviceService.getDevice(deviceId); |
| 293 | if (device.is(DeviceDescriptionDiscovery.class)) { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 294 | DeviceDescriptionDiscovery deviceDescriptionDiscovery = |
| 295 | device.as(DeviceDescriptionDiscovery.class); |
| 296 | DeviceDescription updatedDeviceDescription = |
| 297 | deviceDescriptionDiscovery.discoverDeviceDetails(); |
| 298 | if (updatedDeviceDescription != null && |
| 299 | !descriptionEquals(device, updatedDeviceDescription)) { |
| 300 | providerService.deviceConnected( |
| 301 | deviceId, |
| 302 | new DefaultDeviceDescription( |
| 303 | updatedDeviceDescription, true, |
| 304 | updatedDeviceDescription.annotations())); |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 305 | //if ports are not discovered, retry the discovery |
| 306 | if (deviceService.getPorts(deviceId).isEmpty()) { |
| 307 | discoverPorts(deviceId); |
| 308 | } |
| 309 | } |
| 310 | } else { |
| 311 | log.warn("No DeviceDescriptionDiscovery behaviour for device {}", deviceId); |
| 312 | } |
| 313 | } else if (!isReachable && deviceService.isAvailable(deviceId)) { |
| 314 | providerService.deviceDisconnected(deviceId); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | private boolean descriptionEquals(Device device, DeviceDescription updatedDeviceDescription) { |
Michele Santuari | d2c8f21 | 2017-01-09 18:23:45 +0100 | [diff] [blame] | 320 | return Objects.equal(device.id().uri(), updatedDeviceDescription.deviceUri()) |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 321 | && Objects.equal(device.type(), updatedDeviceDescription.type()) |
| 322 | && Objects.equal(device.manufacturer(), updatedDeviceDescription.manufacturer()) |
| 323 | && Objects.equal(device.hwVersion(), updatedDeviceDescription.hwVersion()) |
| 324 | && Objects.equal(device.swVersion(), updatedDeviceDescription.swVersion()) |
| 325 | && Objects.equal(device.serialNumber(), updatedDeviceDescription.serialNumber()) |
| 326 | && Objects.equal(device.chassisId(), updatedDeviceDescription.chassisId()) |
| 327 | && Objects.equal(device.annotations(), updatedDeviceDescription.annotations()); |
| 328 | } |
| 329 | |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 330 | private void deviceRemoved(DeviceId deviceId) { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 331 | checkNotNull(deviceId, ISNOTNULL); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 332 | providerService.deviceDisconnected(deviceId); |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 333 | controller.getProxiedDevices(deviceId).forEach(device -> { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 334 | controller.removeProxiedDevice(device); |
| 335 | providerService.deviceDisconnected(device); |
| 336 | }); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 337 | controller.removeDevice(deviceId); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 340 | //Method to connect devices provided via net-cfg under devices/ tree |
| 341 | private void createAndConnectDevices() { |
| 342 | Set<DeviceId> deviceSubjects = |
| 343 | cfgService.getSubjects(DeviceId.class, RestDeviceConfig.class); |
| 344 | connectDevices(deviceSubjects.stream() |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 345 | .filter(deviceId -> deviceService.getDevice(deviceId) == null) |
| 346 | .map(deviceId -> { |
| 347 | RestDeviceConfig config = |
| 348 | cfgService.getConfig(deviceId, RestDeviceConfig.class); |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 349 | return toInactiveRestSBDevice(config); |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 350 | }).collect(Collectors.toSet())); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 353 | private RestSBDevice toInactiveRestSBDevice(RestDeviceConfig config) { |
| 354 | return new DefaultRestSBDevice(config.ip(), |
| 355 | config.port(), |
| 356 | config.username(), |
| 357 | config.password(), |
| 358 | config.protocol(), |
| 359 | config.url(), |
| 360 | false, |
| 361 | config.testUrl(), |
| 362 | config.manufacturer(), |
| 363 | config.hwVersion(), |
| 364 | config.swVersion(), |
| 365 | config.authenticationScheme(), |
| 366 | config.token() |
| 367 | ); |
| 368 | } |
| 369 | |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 370 | private void connectDevices(Set<RestSBDevice> devices) { |
| 371 | //Precomputing the devices to be removed |
| 372 | Set<RestSBDevice> toBeRemoved = new HashSet<>(controller.getDevices().values()); |
| 373 | toBeRemoved.removeAll(devices); |
| 374 | //Adding new devices |
| 375 | devices.stream() |
| 376 | .filter(device -> { |
| 377 | device.setActive(false); |
| 378 | controller.addDevice(device); |
| 379 | return testDeviceConnection(device); |
| 380 | }) |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 381 | .forEach(this::deviceAdded); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 382 | //Removing devices not wanted anymore |
| 383 | toBeRemoved.forEach(device -> deviceRemoved(device.deviceId())); |
| 384 | } |
| 385 | |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 386 | private void connectDevice(RestSBDevice device) { |
| 387 | // TODO borrowed from above, |
| 388 | // not sure why setting it to inactive |
| 389 | device.setActive(false); |
| 390 | controller.addDevice(device); |
| 391 | if (testDeviceConnection(device)) { |
| 392 | deviceAdded(device); |
| 393 | } |
| 394 | } |
| 395 | |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 396 | private ScheduledFuture schedulePolling() { |
| 397 | return portStatisticsExecutor.scheduleAtFixedRate(this::executePortStatisticsUpdate, |
| 398 | DEFAULT_POLL_FREQUENCY_SECONDS / 2, |
| 399 | DEFAULT_POLL_FREQUENCY_SECONDS, |
| 400 | TimeUnit.SECONDS); |
| 401 | } |
| 402 | |
| 403 | private void executePortStatisticsUpdate() { |
| 404 | controller.getDevices().keySet().forEach(this::updatePortStatistics); |
| 405 | } |
| 406 | |
| 407 | private void updatePortStatistics(DeviceId deviceId) { |
| 408 | Device device = deviceService.getDevice(deviceId); |
| 409 | checkNotNull(device, "device cannot be null"); |
| 410 | |
| 411 | if (device.is(PortStatisticsDiscovery.class)) { |
| 412 | PortStatisticsDiscovery portStatisticsDiscovery = device.as(PortStatisticsDiscovery.class); |
| 413 | Collection<PortStatistics> portStatistics = portStatisticsDiscovery.discoverPortStatistics(); |
| 414 | if (portStatistics != null && !portStatistics.isEmpty()) { |
| 415 | providerService.updatePortStatistics(deviceId, portStatistics); |
| 416 | } |
| 417 | } else { |
| 418 | log.debug("No port statistics getter behaviour for device {}", deviceId); |
| 419 | } |
| 420 | } |
| 421 | |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 422 | private void discoverPorts(DeviceId deviceId) { |
| 423 | Device device = deviceService.getDevice(deviceId); |
Ray Milkey | 640fedd | 2018-02-08 09:02:26 -0800 | [diff] [blame^] | 424 | DeviceDescriptionDiscovery deviceDescriptionDiscovery = |
| 425 | device.as(DeviceDescriptionDiscovery.class); |
| 426 | providerService.updatePorts(deviceId, deviceDescriptionDiscovery.discoverPortDetails()); |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 429 | private boolean testDeviceConnection(RestSBDevice dev) { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 430 | try { |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 431 | Callable<Boolean> connectionSuccess; |
| 432 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 433 | if (dev.testUrl().isPresent()) { |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 434 | connectionSuccess = () -> |
| 435 | controller.get(dev.deviceId(), dev.testUrl().get(), MediaType.APPLICATION_JSON_TYPE) != null; |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 436 | } else { |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 437 | connectionSuccess = () -> |
| 438 | controller.get(dev.deviceId(), "", MediaType.APPLICATION_JSON_TYPE) != null; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 439 | } |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 440 | |
| 441 | Future<Boolean> future = executor.submit(connectionSuccess); |
| 442 | try { |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 443 | return future.get(REST_TIMEOUT_SEC, TimeUnit.SECONDS); |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 444 | } catch (TimeoutException ex) { |
| 445 | log.warn("Connection to device {} timed out", dev.deviceId()); |
| 446 | return false; |
| 447 | } catch (InterruptedException ex) { |
| 448 | log.warn("Connection to device {} interrupted", dev.deviceId()); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 449 | Thread.currentThread().interrupt(); |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 450 | return false; |
| 451 | } catch (ExecutionException ex) { |
| 452 | log.warn("Connection to device {} had a execution exception", dev.deviceId()); |
| 453 | return false; |
| 454 | } |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 455 | |
Andrea Campanella | c6ecc63 | 2016-03-10 17:57:06 -0800 | [diff] [blame] | 456 | } catch (ProcessingException e) { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 457 | log.warn("Cannot connect to device {}", dev, e); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 458 | } |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 463 | @Override |
| 464 | public void event(NetworkConfigEvent event) { |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 465 | ExecutorService bg = SharedExecutors.getSingleThreadExecutor(); |
| 466 | if (event.type() == CONFIG_REMOVED) { |
| 467 | DeviceId did = (DeviceId) event.subject(); |
| 468 | bg.execute(() -> deviceRemoved(did)); |
| 469 | } else { |
| 470 | // CONFIG_ADDED or CONFIG_UPDATED |
| 471 | RestDeviceConfig cfg = (RestDeviceConfig) event.config().get(); |
| 472 | RestSBDevice restSBDevice = toInactiveRestSBDevice(cfg); |
| 473 | bg.execute(() -> connectDevice(restSBDevice)); |
| 474 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | @Override |
| 478 | public boolean isRelevant(NetworkConfigEvent event) { |
Yuta HIGUCHI | 872fdbe | 2017-12-05 15:34:28 -0800 | [diff] [blame] | 479 | return event.configClass().equals(RestDeviceConfig.class) && |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 480 | (event.type() == CONFIG_ADDED || |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 481 | event.type() == CONFIG_UPDATED || |
| 482 | event.type() == CONFIG_REMOVED); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 483 | } |
| 484 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 485 | |
| 486 | @Override |
| 487 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 488 | boolean enable) { |
fahadnaeemkhan | 482951f | 2017-08-24 16:35:17 -0700 | [diff] [blame] | 489 | Device device = deviceService.getDevice(deviceId); |
| 490 | if (device != null) { |
| 491 | if (device.is(PortAdmin.class)) { |
| 492 | PortAdmin portAdmin = device.as(PortAdmin.class); |
| 493 | CompletableFuture<Boolean> modified; |
| 494 | if (enable) { |
| 495 | modified = portAdmin.enable(portNumber); |
| 496 | } else { |
| 497 | modified = portAdmin.disable(portNumber); |
| 498 | } |
| 499 | modified.thenAcceptAsync(result -> { |
| 500 | if (!result) { |
| 501 | log.warn("Device {} port {} state can't be changed to {}", |
| 502 | deviceId, portNumber, enable); |
| 503 | } |
| 504 | }); |
| 505 | |
| 506 | } else { |
| 507 | log.warn("Device {} does not support PortAdmin behavior", deviceId); |
| 508 | } |
| 509 | } else { |
| 510 | log.warn("unable to get the device {}, port {} state can't be changed to {}", |
| 511 | deviceId, portNumber, enable); |
| 512 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 513 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 514 | } |