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