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