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