Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.provider.rest.device.impl; |
| 18 | |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 19 | import com.google.common.base.Objects; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.onlab.packet.ChassisId; |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 27 | import org.onlab.util.SharedExecutors; |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 28 | import org.onlab.util.SharedScheduledExecutorService; |
| 29 | import org.onlab.util.SharedScheduledExecutors; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
Marc De Leenheer | b0d131c | 2016-03-01 20:34:59 -0800 | [diff] [blame] | 32 | import org.onosproject.net.AnnotationKeys; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 33 | import org.onosproject.net.DefaultAnnotations; |
| 34 | import org.onosproject.net.Device; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.MastershipRole; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 37 | import org.onosproject.net.PortNumber; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 38 | import org.onosproject.net.SparseAnnotations; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 39 | import org.onosproject.net.behaviour.DevicesDiscovery; |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 40 | import org.onosproject.net.behaviour.PortAdmin; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 41 | import org.onosproject.net.behaviour.PortDiscovery; |
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; |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 89 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REMOVED; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 90 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 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 | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 136 | new ConfigFactory<DeviceId, RestDeviceConfig>(SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 137 | RestDeviceConfig.class, |
| 138 | REST) { |
| 139 | @Override |
| 140 | public RestDeviceConfig createConfig() { |
| 141 | return new RestDeviceConfig(); |
| 142 | } |
| 143 | }); |
| 144 | |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 145 | private final NetworkConfigListener configListener = new InternalNetworkConfigListener(); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 146 | |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 147 | private ScheduledFuture<?> scheduledTask; |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 148 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 149 | |
| 150 | @Activate |
| 151 | public void activate() { |
| 152 | appId = coreService.registerApplication(APP_NAME); |
| 153 | providerService = providerRegistry.register(this); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 154 | factories.forEach(cfgService::registerConfigFactory); |
Georgios Katsikas | 6a4d166 | 2017-07-27 13:22:31 +0200 | [diff] [blame] | 155 | executor = Executors.newFixedThreadPool( |
| 156 | EXECUTOR_THREAD_POOL_SIZE, groupedThreads("onos/restsbprovider", "device-installer-%d", log) |
| 157 | ); |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 158 | cfgService.addListener(configListener); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 159 | executor.execute(RestDeviceProvider.this::createAndConnectDevices); |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 160 | scheduledTask = schedulePolling(); |
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() { |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 166 | cfgService.removeListener(configListener); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 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); |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 171 | scheduledTask.cancel(true); |
Lukasz Ryba | 4da35c5 | 2017-06-20 09:14:11 +0200 | [diff] [blame] | 172 | executor.shutdown(); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 173 | log.info("Stopped"); |
| 174 | } |
| 175 | |
| 176 | public RestDeviceProvider() { |
| 177 | super(new ProviderId(REST, PROVIDER)); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void triggerProbe(DeviceId deviceId) { |
| 182 | // TODO: This will be implemented later. |
| 183 | log.info("Triggering probe on device {}", deviceId); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
| 188 | // TODO: This will be implemented later. |
| 189 | } |
| 190 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 191 | @Override |
| 192 | public boolean isReachable(DeviceId deviceId) { |
| 193 | RestSBDevice restDevice = controller.getDevice(deviceId); |
| 194 | if (restDevice == null) { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 195 | restDevice = controller.getProxySBDevice(deviceId); |
| 196 | if (restDevice == null) { |
| 197 | log.debug("the requested device id: " + |
| 198 | deviceId.toString() + |
| 199 | " is not associated to any REST or REST " + |
| 200 | "proxy Device"); |
| 201 | return false; |
| 202 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 203 | } |
| 204 | return restDevice.isActive(); |
| 205 | } |
| 206 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 207 | private void deviceAdded(RestSBDevice restSBDev) { |
| 208 | checkNotNull(restSBDev, ISNOTNULL); |
| 209 | |
| 210 | //check if the server is controlling a single or multiple devices |
| 211 | if (restSBDev.isProxy()) { |
| 212 | |
| 213 | Driver driver = driverService.getDriver(restSBDev.manufacturer().get(), |
| 214 | restSBDev.hwVersion().get(), |
| 215 | restSBDev.swVersion().get()); |
| 216 | |
| 217 | if (driver != null && driver.hasBehaviour(DevicesDiscovery.class)) { |
| 218 | |
| 219 | //Creates the driver to communicate with the server |
| 220 | DevicesDiscovery devicesDiscovery = |
| 221 | devicesDiscovery(restSBDev, driver); |
| 222 | Set<DeviceId> deviceIds = devicesDiscovery.deviceIds(); |
| 223 | restSBDev.setActive(true); |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 224 | deviceIds.forEach(deviceId -> { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 225 | controller.addProxiedDevice(deviceId, restSBDev); |
| 226 | DeviceDescription devDesc = |
| 227 | devicesDiscovery.deviceDetails(deviceId); |
| 228 | checkNotNull(devDesc, |
| 229 | "deviceDescription cannot be null"); |
| 230 | providerService.deviceConnected( |
| 231 | deviceId, mergeAnn(restSBDev.deviceId(), devDesc)); |
| 232 | |
| 233 | if (driver.hasBehaviour(DeviceDescriptionDiscovery.class)) { |
| 234 | DriverHandler h = driverService.createHandler(deviceId); |
| 235 | DeviceDescriptionDiscovery devDisc = |
| 236 | h.behaviour(DeviceDescriptionDiscovery.class); |
| 237 | providerService.updatePorts(deviceId, |
| 238 | devDisc.discoverPortDetails()); |
| 239 | } |
| 240 | |
| 241 | checkAndUpdateDevice(deviceId); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 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); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
| 267 | private DefaultDeviceDescription mergeAnn(DeviceId devId, DeviceDescription desc) { |
| 268 | return new DefaultDeviceDescription( |
| 269 | desc, |
| 270 | DefaultAnnotations.merge( |
| 271 | DefaultAnnotations.builder() |
| 272 | .set(AnnotationKeys.PROTOCOL, REST.toUpperCase()) |
| 273 | // The rest server added as annotation to the device |
| 274 | .set(AnnotationKeys.REST_SERVER, devId.toString()) |
| 275 | .build(), |
| 276 | desc.annotations())); |
| 277 | } |
| 278 | |
| 279 | private DevicesDiscovery devicesDiscovery(RestSBDevice restSBDevice, Driver driver) { |
| 280 | DriverData driverData = new DefaultDriverData(driver, restSBDevice.deviceId()); |
| 281 | DevicesDiscovery devicesDiscovery = driver.createBehaviour(driverData, |
| 282 | DevicesDiscovery.class); |
| 283 | devicesDiscovery.setHandler(new DefaultDriverHandler(driverData)); |
| 284 | return devicesDiscovery; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 287 | private void checkAndUpdateDevice(DeviceId deviceId) { |
| 288 | if (deviceService.getDevice(deviceId) == null) { |
| 289 | log.warn("Device {} has not been added to store, " + |
| 290 | "maybe due to a problem in connectivity", deviceId); |
| 291 | } else { |
| 292 | boolean isReachable = isReachable(deviceId); |
| 293 | if (isReachable && deviceService.isAvailable(deviceId)) { |
| 294 | Device device = deviceService.getDevice(deviceId); |
| 295 | if (device.is(DeviceDescriptionDiscovery.class)) { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 296 | DeviceDescriptionDiscovery deviceDescriptionDiscovery = |
| 297 | device.as(DeviceDescriptionDiscovery.class); |
| 298 | DeviceDescription updatedDeviceDescription = |
| 299 | deviceDescriptionDiscovery.discoverDeviceDetails(); |
| 300 | if (updatedDeviceDescription != null && |
| 301 | !descriptionEquals(device, updatedDeviceDescription)) { |
| 302 | providerService.deviceConnected( |
| 303 | deviceId, |
| 304 | new DefaultDeviceDescription( |
| 305 | updatedDeviceDescription, true, |
| 306 | updatedDeviceDescription.annotations())); |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 307 | //if ports are not discovered, retry the discovery |
| 308 | if (deviceService.getPorts(deviceId).isEmpty()) { |
| 309 | discoverPorts(deviceId); |
| 310 | } |
| 311 | } |
| 312 | } else { |
| 313 | log.warn("No DeviceDescriptionDiscovery behaviour for device {}", deviceId); |
| 314 | } |
| 315 | } else if (!isReachable && deviceService.isAvailable(deviceId)) { |
| 316 | providerService.deviceDisconnected(deviceId); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | private boolean descriptionEquals(Device device, DeviceDescription updatedDeviceDescription) { |
Michele Santuari | d2c8f21 | 2017-01-09 18:23:45 +0100 | [diff] [blame] | 322 | return Objects.equal(device.id().uri(), updatedDeviceDescription.deviceUri()) |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 323 | && Objects.equal(device.type(), updatedDeviceDescription.type()) |
| 324 | && Objects.equal(device.manufacturer(), updatedDeviceDescription.manufacturer()) |
| 325 | && Objects.equal(device.hwVersion(), updatedDeviceDescription.hwVersion()) |
| 326 | && Objects.equal(device.swVersion(), updatedDeviceDescription.swVersion()) |
| 327 | && Objects.equal(device.serialNumber(), updatedDeviceDescription.serialNumber()) |
| 328 | && Objects.equal(device.chassisId(), updatedDeviceDescription.chassisId()) |
| 329 | && Objects.equal(device.annotations(), updatedDeviceDescription.annotations()); |
| 330 | } |
| 331 | |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 332 | private void deviceRemoved(DeviceId deviceId) { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 333 | checkNotNull(deviceId, ISNOTNULL); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 334 | providerService.deviceDisconnected(deviceId); |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 335 | controller.getProxiedDevices(deviceId).forEach(device -> { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 336 | controller.removeProxiedDevice(device); |
| 337 | providerService.deviceDisconnected(device); |
| 338 | }); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 339 | controller.removeDevice(deviceId); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 342 | //Method to connect devices provided via net-cfg under devices/ tree |
| 343 | private void createAndConnectDevices() { |
| 344 | Set<DeviceId> deviceSubjects = |
| 345 | cfgService.getSubjects(DeviceId.class, RestDeviceConfig.class); |
| 346 | connectDevices(deviceSubjects.stream() |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 347 | .filter(deviceId -> deviceService.getDevice(deviceId) == null) |
| 348 | .map(deviceId -> { |
| 349 | RestDeviceConfig config = |
| 350 | cfgService.getConfig(deviceId, RestDeviceConfig.class); |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 351 | return toInactiveRestSBDevice(config); |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 352 | }).collect(Collectors.toSet())); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 353 | } |
| 354 | |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 355 | private RestSBDevice toInactiveRestSBDevice(RestDeviceConfig config) { |
| 356 | return new DefaultRestSBDevice(config.ip(), |
| 357 | config.port(), |
| 358 | config.username(), |
| 359 | config.password(), |
| 360 | config.protocol(), |
| 361 | config.url(), |
| 362 | false, |
| 363 | config.testUrl(), |
| 364 | config.manufacturer(), |
| 365 | config.hwVersion(), |
| 366 | config.swVersion(), |
| 367 | config.authenticationScheme(), |
| 368 | config.token() |
| 369 | ); |
| 370 | } |
| 371 | |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 372 | private void connectDevices(Set<RestSBDevice> devices) { |
| 373 | //Precomputing the devices to be removed |
| 374 | Set<RestSBDevice> toBeRemoved = new HashSet<>(controller.getDevices().values()); |
| 375 | toBeRemoved.removeAll(devices); |
| 376 | //Adding new devices |
| 377 | devices.stream() |
| 378 | .filter(device -> { |
| 379 | device.setActive(false); |
| 380 | controller.addDevice(device); |
| 381 | return testDeviceConnection(device); |
| 382 | }) |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 383 | .forEach(this::deviceAdded); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 384 | //Removing devices not wanted anymore |
| 385 | toBeRemoved.forEach(device -> deviceRemoved(device.deviceId())); |
| 386 | } |
| 387 | |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 388 | private void connectDevice(RestSBDevice device) { |
| 389 | // TODO borrowed from above, |
| 390 | // not sure why setting it to inactive |
| 391 | device.setActive(false); |
| 392 | controller.addDevice(device); |
| 393 | if (testDeviceConnection(device)) { |
| 394 | deviceAdded(device); |
| 395 | } |
| 396 | } |
| 397 | |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 398 | private ScheduledFuture schedulePolling() { |
| 399 | return portStatisticsExecutor.scheduleAtFixedRate(this::executePortStatisticsUpdate, |
| 400 | DEFAULT_POLL_FREQUENCY_SECONDS / 2, |
| 401 | DEFAULT_POLL_FREQUENCY_SECONDS, |
| 402 | TimeUnit.SECONDS); |
| 403 | } |
| 404 | |
| 405 | private void executePortStatisticsUpdate() { |
| 406 | controller.getDevices().keySet().forEach(this::updatePortStatistics); |
| 407 | } |
| 408 | |
| 409 | private void updatePortStatistics(DeviceId deviceId) { |
| 410 | Device device = deviceService.getDevice(deviceId); |
| 411 | checkNotNull(device, "device cannot be null"); |
| 412 | |
| 413 | if (device.is(PortStatisticsDiscovery.class)) { |
| 414 | PortStatisticsDiscovery portStatisticsDiscovery = device.as(PortStatisticsDiscovery.class); |
| 415 | Collection<PortStatistics> portStatistics = portStatisticsDiscovery.discoverPortStatistics(); |
| 416 | if (portStatistics != null && !portStatistics.isEmpty()) { |
| 417 | providerService.updatePortStatistics(deviceId, portStatistics); |
| 418 | } |
| 419 | } else { |
| 420 | log.debug("No port statistics getter behaviour for device {}", deviceId); |
| 421 | } |
| 422 | } |
| 423 | |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 424 | private void discoverPorts(DeviceId deviceId) { |
| 425 | Device device = deviceService.getDevice(deviceId); |
| 426 | //TODO remove when PortDiscovery is removed from master |
| 427 | if (device.is(PortDiscovery.class)) { |
| 428 | PortDiscovery portConfig = device.as(PortDiscovery.class); |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 429 | providerService.updatePorts(deviceId, portConfig.getPorts()); |
| 430 | } else { |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 431 | DeviceDescriptionDiscovery deviceDescriptionDiscovery = |
| 432 | device.as(DeviceDescriptionDiscovery.class); |
| 433 | providerService.updatePorts(deviceId, deviceDescriptionDiscovery.discoverPortDetails()); |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 437 | private boolean testDeviceConnection(RestSBDevice dev) { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 438 | try { |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 439 | Callable<Boolean> connectionSuccess; |
| 440 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 441 | if (dev.testUrl().isPresent()) { |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 442 | connectionSuccess = () -> |
| 443 | controller.get(dev.deviceId(), dev.testUrl().get(), MediaType.APPLICATION_JSON_TYPE) != null; |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 444 | } else { |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 445 | connectionSuccess = () -> |
| 446 | controller.get(dev.deviceId(), "", MediaType.APPLICATION_JSON_TYPE) != null; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 447 | } |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 448 | |
| 449 | Future<Boolean> future = executor.submit(connectionSuccess); |
| 450 | try { |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 451 | return future.get(REST_TIMEOUT_SEC, TimeUnit.SECONDS); |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 452 | } catch (TimeoutException ex) { |
| 453 | log.warn("Connection to device {} timed out", dev.deviceId()); |
| 454 | return false; |
| 455 | } catch (InterruptedException ex) { |
| 456 | log.warn("Connection to device {} interrupted", dev.deviceId()); |
| 457 | return false; |
| 458 | } catch (ExecutionException ex) { |
| 459 | log.warn("Connection to device {} had a execution exception", dev.deviceId()); |
| 460 | return false; |
| 461 | } |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 462 | |
Andrea Campanella | c6ecc63 | 2016-03-10 17:57:06 -0800 | [diff] [blame] | 463 | } catch (ProcessingException e) { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 464 | log.warn("Cannot connect to device {}", dev, e); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 465 | } |
| 466 | return false; |
| 467 | } |
| 468 | |
| 469 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 470 | @Override |
| 471 | public void event(NetworkConfigEvent event) { |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 472 | ExecutorService bg = SharedExecutors.getSingleThreadExecutor(); |
| 473 | if (event.type() == CONFIG_REMOVED) { |
| 474 | DeviceId did = (DeviceId) event.subject(); |
| 475 | bg.execute(() -> deviceRemoved(did)); |
| 476 | } else { |
| 477 | // CONFIG_ADDED or CONFIG_UPDATED |
| 478 | RestDeviceConfig cfg = (RestDeviceConfig) event.config().get(); |
| 479 | RestSBDevice restSBDevice = toInactiveRestSBDevice(cfg); |
| 480 | bg.execute(() -> connectDevice(restSBDevice)); |
| 481 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public boolean isRelevant(NetworkConfigEvent event) { |
Yuta HIGUCHI | 872fdbe | 2017-12-05 15:34:28 -0800 | [diff] [blame] | 486 | return event.configClass().equals(RestDeviceConfig.class) && |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 487 | (event.type() == CONFIG_ADDED || |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 488 | event.type() == CONFIG_UPDATED || |
| 489 | event.type() == CONFIG_REMOVED); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 490 | } |
| 491 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 492 | |
| 493 | @Override |
| 494 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 495 | boolean enable) { |
fahadnaeemkhan | 482951f | 2017-08-24 16:35:17 -0700 | [diff] [blame] | 496 | Device device = deviceService.getDevice(deviceId); |
| 497 | if (device != null) { |
| 498 | if (device.is(PortAdmin.class)) { |
| 499 | PortAdmin portAdmin = device.as(PortAdmin.class); |
| 500 | CompletableFuture<Boolean> modified; |
| 501 | if (enable) { |
| 502 | modified = portAdmin.enable(portNumber); |
| 503 | } else { |
| 504 | modified = portAdmin.disable(portNumber); |
| 505 | } |
| 506 | modified.thenAcceptAsync(result -> { |
| 507 | if (!result) { |
| 508 | log.warn("Device {} port {} state can't be changed to {}", |
| 509 | deviceId, portNumber, enable); |
| 510 | } |
| 511 | }); |
| 512 | |
| 513 | } else { |
| 514 | log.warn("Device {} does not support PortAdmin behavior", deviceId); |
| 515 | } |
| 516 | } else { |
| 517 | log.warn("unable to get the device {}, port {} state can't be changed to {}", |
| 518 | deviceId, portNumber, enable); |
| 519 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 520 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 521 | } |