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.onlab.packet.ChassisId; |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 22 | import org.onlab.util.SharedExecutors; |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 23 | import org.onlab.util.SharedScheduledExecutorService; |
| 24 | import org.onlab.util.SharedScheduledExecutors; |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 25 | import org.onlab.util.Tools; |
| 26 | import org.onosproject.cfg.ComponentConfigService; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
Marc De Leenheer | b0d131c | 2016-03-01 20:34:59 -0800 | [diff] [blame] | 29 | import org.onosproject.net.AnnotationKeys; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DefaultAnnotations; |
| 31 | import org.onosproject.net.Device; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.MastershipRole; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 34 | import org.onosproject.net.PortNumber; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 35 | import org.onosproject.net.SparseAnnotations; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 36 | import org.onosproject.net.behaviour.DevicesDiscovery; |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 37 | import org.onosproject.net.behaviour.PortAdmin; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 38 | import org.onosproject.net.config.ConfigFactory; |
| 39 | import org.onosproject.net.config.NetworkConfigEvent; |
| 40 | import org.onosproject.net.config.NetworkConfigListener; |
| 41 | import org.onosproject.net.config.NetworkConfigRegistry; |
Andrea Campanella | b38023b | 2019-02-11 17:21:35 +0100 | [diff] [blame] | 42 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 43 | import org.onosproject.net.config.basics.SubjectFactories; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 44 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 45 | import org.onosproject.net.device.DeviceDescription; |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 46 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 47 | import org.onosproject.net.device.DeviceProvider; |
| 48 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 49 | import org.onosproject.net.device.DeviceProviderService; |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 50 | import org.onosproject.net.device.DeviceService; |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 51 | import org.onosproject.net.device.PortStatistics; |
| 52 | import org.onosproject.net.device.PortStatisticsDiscovery; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 53 | import org.onosproject.net.driver.DefaultDriverData; |
| 54 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 55 | import org.onosproject.net.driver.Driver; |
| 56 | import org.onosproject.net.driver.DriverData; |
| 57 | import org.onosproject.net.driver.DriverHandler; |
| 58 | import org.onosproject.net.driver.DriverService; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 59 | import org.onosproject.net.provider.AbstractProvider; |
| 60 | import org.onosproject.net.provider.ProviderId; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 61 | import org.onosproject.protocol.rest.DefaultRestSBDevice; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 62 | import org.onosproject.protocol.rest.RestSBController; |
| 63 | import org.onosproject.protocol.rest.RestSBDevice; |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 64 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 65 | import org.osgi.service.component.annotations.Activate; |
| 66 | import org.osgi.service.component.annotations.Component; |
| 67 | import org.osgi.service.component.annotations.Deactivate; |
| 68 | import org.osgi.service.component.annotations.Modified; |
| 69 | import org.osgi.service.component.annotations.Reference; |
| 70 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 71 | import org.slf4j.Logger; |
| 72 | |
Andrea Campanella | c6ecc63 | 2016-03-10 17:57:06 -0800 | [diff] [blame] | 73 | import javax.ws.rs.ProcessingException; |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 74 | import javax.ws.rs.core.MediaType; |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 75 | import java.util.Collection; |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 76 | import java.util.Dictionary; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 77 | import java.util.HashSet; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 78 | import java.util.List; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 79 | import java.util.Set; |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 80 | import java.util.concurrent.Callable; |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 81 | import java.util.concurrent.CompletableFuture; |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 82 | import java.util.concurrent.ExecutionException; |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 83 | import java.util.concurrent.ExecutorService; |
| 84 | import java.util.concurrent.Executors; |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 85 | import java.util.concurrent.Future; |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 86 | import java.util.concurrent.ScheduledFuture; |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 87 | import java.util.concurrent.TimeUnit; |
| 88 | import java.util.concurrent.TimeoutException; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 89 | import java.util.stream.Collectors; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 90 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 91 | import static com.google.common.base.Preconditions.checkNotNull; |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 92 | import static org.onlab.util.Tools.groupedThreads; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 93 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED; |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 94 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REMOVED; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 95 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED; |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 96 | import static org.onosproject.provider.rest.device.impl.OsgiPropertyConstants.POLL_FREQUENCY; |
| 97 | import static org.onosproject.provider.rest.device.impl.OsgiPropertyConstants.POLL_FREQUENCY_DEFAULT; |
Andrea Campanella | 02f86c8 | 2019-03-18 16:57:27 -0700 | [diff] [blame] | 98 | import static org.onosproject.provider.rest.device.impl.OsgiPropertyConstants.TIMEOUT; |
| 99 | import static org.onosproject.provider.rest.device.impl.OsgiPropertyConstants.TIMEOUT_DEFAULT; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 100 | import static org.slf4j.LoggerFactory.getLogger; |
| 101 | |
| 102 | /** |
| 103 | * Provider for devices that use REST as means of configuration communication. |
| 104 | */ |
Andrea Campanella | 02f86c8 | 2019-03-18 16:57:27 -0700 | [diff] [blame] | 105 | @Component(immediate = true, |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 106 | property = { |
| 107 | POLL_FREQUENCY + ":Integer=" + POLL_FREQUENCY_DEFAULT, |
Andrea Campanella | 02f86c8 | 2019-03-18 16:57:27 -0700 | [diff] [blame] | 108 | TIMEOUT + ":Integer=" + TIMEOUT_DEFAULT, |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 109 | }) |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 110 | public class RestDeviceProvider extends AbstractProvider |
| 111 | implements DeviceProvider { |
| 112 | private static final String APP_NAME = "org.onosproject.restsb"; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 113 | protected static final String REST = "rest"; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 114 | private static final String PROVIDER = "org.onosproject.provider.rest.device"; |
| 115 | private static final String IPADDRESS = "ipaddress"; |
Andrea Campanella | b38023b | 2019-02-11 17:21:35 +0100 | [diff] [blame] | 116 | private static final String DEVICENULL = "Rest device is null"; |
| 117 | private static final String DRIVERNULL = "Driver is null"; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 118 | private static final String UNKNOWN = "unknown"; |
Georgios Katsikas | 6a4d166 | 2017-07-27 13:22:31 +0200 | [diff] [blame] | 119 | private static final int EXECUTOR_THREAD_POOL_SIZE = 8; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 120 | private final Logger log = getLogger(getClass()); |
| 121 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 123 | protected DeviceProviderRegistry providerRegistry; |
| 124 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 126 | protected RestSBController controller; |
| 127 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 129 | protected NetworkConfigRegistry netCfgService; |
| 130 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 132 | protected ComponentConfigService compCfgService; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 133 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 135 | protected CoreService coreService; |
| 136 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 138 | protected DeviceService deviceService; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 139 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 141 | protected DriverService driverService; |
| 142 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 143 | /** Configure poll frequency for port status and statistics; default is 30 seconds. */ |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 144 | private int pollFrequency = POLL_FREQUENCY_DEFAULT; |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 145 | |
Andrea Campanella | 02f86c8 | 2019-03-18 16:57:27 -0700 | [diff] [blame] | 146 | /** Configure timeout for a device reply; default is 5 seconds. */ |
| 147 | private int replyTimeout = TIMEOUT_DEFAULT; |
| 148 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 149 | private DeviceProviderService providerService; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 150 | private ApplicationId appId; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 151 | |
Michal Mach | 13072e2 | 2017-06-21 09:12:24 +0200 | [diff] [blame] | 152 | private ExecutorService executor; |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 153 | private final SharedScheduledExecutorService portStatisticsExecutor = |
| 154 | SharedScheduledExecutors.getPoolThreadExecutor(); |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 155 | |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 156 | private final SharedScheduledExecutorService deviceConnectionExecutor = |
| 157 | SharedScheduledExecutors.getPoolThreadExecutor(); |
| 158 | private ScheduledFuture<?> devicePollTask; |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 159 | private final List<ConfigFactory> factories = ImmutableList.of( |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 160 | new ConfigFactory<DeviceId, RestDeviceConfig>(SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 161 | RestDeviceConfig.class, |
| 162 | REST) { |
| 163 | @Override |
| 164 | public RestDeviceConfig createConfig() { |
| 165 | return new RestDeviceConfig(); |
| 166 | } |
| 167 | }); |
| 168 | |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 169 | private final NetworkConfigListener configListener = new InternalNetworkConfigListener(); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 170 | |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 171 | private ScheduledFuture<?> scheduledTask; |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 172 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 173 | |
| 174 | @Activate |
| 175 | public void activate() { |
| 176 | appId = coreService.registerApplication(APP_NAME); |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 177 | compCfgService.registerProperties(getClass()); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 178 | providerService = providerRegistry.register(this); |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 179 | factories.forEach(netCfgService::registerConfigFactory); |
Georgios Katsikas | 6a4d166 | 2017-07-27 13:22:31 +0200 | [diff] [blame] | 180 | executor = Executors.newFixedThreadPool( |
| 181 | EXECUTOR_THREAD_POOL_SIZE, groupedThreads("onos/restsbprovider", "device-installer-%d", log) |
| 182 | ); |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 183 | netCfgService.addListener(configListener); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 184 | executor.execute(RestDeviceProvider.this::createAndConnectDevices); |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 185 | scheduledTask = schedulePolling(); |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 186 | devicePollTask = scheduleDevicePolling(); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 187 | log.info("Started"); |
| 188 | } |
| 189 | |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 190 | @Modified |
| 191 | public void modified(ComponentContext context) { |
| 192 | int previousPollFrequency = pollFrequency; |
| 193 | |
| 194 | if (context != null) { |
| 195 | Dictionary<?, ?> properties = context.getProperties(); |
| 196 | pollFrequency = Tools.getIntegerProperty(properties, POLL_FREQUENCY, |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 197 | POLL_FREQUENCY_DEFAULT); |
Andrea Campanella | 02f86c8 | 2019-03-18 16:57:27 -0700 | [diff] [blame] | 198 | replyTimeout = Tools.getIntegerProperty(properties, TIMEOUT, |
| 199 | TIMEOUT_DEFAULT); |
| 200 | log.info("Configured. Poll frequency = {} seconds, reply timeout = {} seconds ", |
| 201 | pollFrequency, replyTimeout); |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // Re-schedule only if frequency has changed |
| 205 | if (!scheduledTask.isCancelled() && (previousPollFrequency != pollFrequency)) { |
| 206 | log.info("Re-scheduling port statistics task with frequency {} seconds", pollFrequency); |
| 207 | scheduledTask.cancel(true); |
| 208 | scheduledTask = schedulePolling(); |
| 209 | } |
| 210 | } |
| 211 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 212 | @Deactivate |
| 213 | public void deactivate() { |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 214 | compCfgService.unregisterProperties(getClass(), false); |
| 215 | netCfgService.removeListener(configListener); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 216 | providerRegistry.unregister(this); |
| 217 | providerService = null; |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 218 | factories.forEach(netCfgService::unregisterConfigFactory); |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 219 | scheduledTask.cancel(true); |
Lukasz Ryba | 4da35c5 | 2017-06-20 09:14:11 +0200 | [diff] [blame] | 220 | executor.shutdown(); |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 221 | devicePollTask.cancel(true); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 222 | log.info("Stopped"); |
| 223 | } |
| 224 | |
| 225 | public RestDeviceProvider() { |
| 226 | super(new ProviderId(REST, PROVIDER)); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void triggerProbe(DeviceId deviceId) { |
| 231 | // TODO: This will be implemented later. |
| 232 | log.info("Triggering probe on device {}", deviceId); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 237 | log.debug("Received role {} request for device {}", newRole, deviceId); |
| 238 | RestSBDevice device = controller.getDevice(deviceId); |
| 239 | if (device != null && testDeviceConnection(device)) { |
| 240 | providerService.receivedRoleReply(deviceId, newRole, newRole); |
| 241 | } else { |
| 242 | log.warn("Device not present or available {}", deviceId); |
| 243 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.NONE); |
| 244 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 247 | @Override |
| 248 | public boolean isReachable(DeviceId deviceId) { |
| 249 | RestSBDevice restDevice = controller.getDevice(deviceId); |
Andrea Campanella | b38023b | 2019-02-11 17:21:35 +0100 | [diff] [blame] | 250 | return restDevice != null && restDevice.isActive(); |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | private ScheduledFuture scheduleDevicePolling() { |
| 254 | return deviceConnectionExecutor.scheduleWithFixedDelay(() -> { |
| 255 | try { |
| 256 | controller.getDevices().values().stream().forEach(restSBDevice -> { |
| 257 | DeviceId deviceId = restSBDevice.deviceId(); |
| 258 | if (deviceService.getDevice(deviceId) != null) { |
| 259 | boolean connected = testDeviceConnection(restSBDevice); |
| 260 | restSBDevice.setActive(connected); |
| 261 | if (deviceService.isAvailable(deviceId) && (!connected)) { |
| 262 | providerService.deviceDisconnected(deviceId); |
| 263 | } else if (!deviceService.isAvailable(deviceId) && connected) { |
| 264 | DeviceDescription devDesc = getDesc(restSBDevice); |
| 265 | checkNotNull(devDesc, "deviceDescription cannot be null"); |
| 266 | providerService.deviceConnected( |
| 267 | deviceId, mergeAnn(deviceId, devDesc)); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | ); |
| 272 | } catch (Exception e) { |
| 273 | log.error("Exception at schedule Device polling", e); |
| 274 | } |
Andrea Campanella | 02f86c8 | 2019-03-18 16:57:27 -0700 | [diff] [blame] | 275 | }, 1, pollFrequency, TimeUnit.SECONDS); |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | private DeviceDescription getDesc(RestSBDevice restSBDev) { |
| 279 | DeviceId deviceId = restSBDev.deviceId(); |
Andrea Campanella | b38023b | 2019-02-11 17:21:35 +0100 | [diff] [blame] | 280 | |
| 281 | Driver driver = getDriver(restSBDev); |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 282 | |
Andrea Campanella | 60804c0 | 2018-11-06 15:06:36 +0100 | [diff] [blame] | 283 | if (restSBDev.isProxy()) { |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 284 | if (driver != null && driver.hasBehaviour(DevicesDiscovery.class)) { |
| 285 | |
| 286 | //Creates the driver to communicate with the server |
| 287 | DevicesDiscovery devicesDiscovery = |
| 288 | devicesDiscovery(restSBDev, driver); |
| 289 | return devicesDiscovery.deviceDetails(deviceId); |
| 290 | } else { |
| 291 | log.warn("Driver not found for {}", restSBDev); |
| 292 | return null; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 293 | } |
Andrea Campanella | 60804c0 | 2018-11-06 15:06:36 +0100 | [diff] [blame] | 294 | } else if (driver != null && driver.hasBehaviour(DeviceDescriptionDiscovery.class)) { |
| 295 | DriverHandler h = driverService.createHandler(deviceId); |
| 296 | DeviceDescriptionDiscovery deviceDiscovery = h.behaviour(DeviceDescriptionDiscovery.class); |
| 297 | return deviceDiscovery.discoverDeviceDetails(); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 298 | } |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 299 | ChassisId cid = new ChassisId(); |
| 300 | String ipAddress = restSBDev.ip().toString(); |
| 301 | SparseAnnotations annotations = DefaultAnnotations.builder() |
| 302 | .set(IPADDRESS, ipAddress) |
| 303 | .set(AnnotationKeys.PROTOCOL, REST.toUpperCase()) |
| 304 | .build(); |
| 305 | String manufacturer = UNKNOWN; |
| 306 | String hwVersion = UNKNOWN; |
| 307 | String swVersion = UNKNOWN; |
| 308 | String serialNumber = UNKNOWN; |
| 309 | |
| 310 | Device device = deviceService.getDevice(deviceId); |
| 311 | if (device != null) { |
| 312 | manufacturer = device.manufacturer(); |
| 313 | hwVersion = device.hwVersion(); |
| 314 | swVersion = device.swVersion(); |
| 315 | serialNumber = device.serialNumber(); |
| 316 | } |
| 317 | |
| 318 | return new DefaultDeviceDescription( |
| 319 | deviceId.uri(), |
| 320 | Device.Type.SWITCH, |
| 321 | manufacturer, hwVersion, |
| 322 | swVersion, serialNumber, |
| 323 | cid, |
| 324 | annotations); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 327 | private void deviceAdded(RestSBDevice restSBDev) { |
Andrea Campanella | b38023b | 2019-02-11 17:21:35 +0100 | [diff] [blame] | 328 | checkNotNull(restSBDev, DEVICENULL); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 329 | |
Andrea Campanella | b38023b | 2019-02-11 17:21:35 +0100 | [diff] [blame] | 330 | Driver driver = getDriver(restSBDev); |
Georgios Katsikas | 15841e2 | 2018-07-28 14:27:28 +0200 | [diff] [blame] | 331 | |
| 332 | // Check if the server is controlling a single or multiple devices |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 333 | if (restSBDev.isProxy()) { |
Andrea Campanella | b38023b | 2019-02-11 17:21:35 +0100 | [diff] [blame] | 334 | if (driver.hasBehaviour(DevicesDiscovery.class)) { |
Georgios Katsikas | 15841e2 | 2018-07-28 14:27:28 +0200 | [diff] [blame] | 335 | DevicesDiscovery devicesDiscovery = devicesDiscovery(restSBDev, driver); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 336 | Set<DeviceId> deviceIds = devicesDiscovery.deviceIds(); |
| 337 | restSBDev.setActive(true); |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 338 | deviceIds.forEach(deviceId -> { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 339 | controller.addProxiedDevice(deviceId, restSBDev); |
Georgios Katsikas | 15841e2 | 2018-07-28 14:27:28 +0200 | [diff] [blame] | 340 | DeviceDescription devDesc = devicesDiscovery.deviceDetails(deviceId); |
| 341 | checkNotNull(devDesc, "DeviceDescription cannot be null"); |
| 342 | providerService.deviceConnected(deviceId, mergeAnn(restSBDev.deviceId(), devDesc)); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 343 | |
| 344 | if (driver.hasBehaviour(DeviceDescriptionDiscovery.class)) { |
| 345 | DriverHandler h = driverService.createHandler(deviceId); |
Georgios Katsikas | 15841e2 | 2018-07-28 14:27:28 +0200 | [diff] [blame] | 346 | DeviceDescriptionDiscovery devDisc = h.behaviour(DeviceDescriptionDiscovery.class); |
| 347 | providerService.updatePorts(deviceId, devDisc.discoverPortDetails()); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | checkAndUpdateDevice(deviceId); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 351 | }); |
| 352 | } else { |
Andrea Campanella | b38023b | 2019-02-11 17:21:35 +0100 | [diff] [blame] | 353 | log.warn("Device is proxy but driver does not have proxy discovery behaviour {}", restSBDev); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 354 | } |
| 355 | } else { |
| 356 | DeviceId deviceId = restSBDev.deviceId(); |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 357 | |
Georgios Katsikas | 15841e2 | 2018-07-28 14:27:28 +0200 | [diff] [blame] | 358 | if (driver != null && driver.hasBehaviour(DevicesDiscovery.class)) { |
| 359 | restSBDev.setActive(true); |
| 360 | DevicesDiscovery devicesDiscovery = devicesDiscovery(restSBDev, driver); |
| 361 | DeviceDescription deviceDescription = devicesDiscovery.deviceDetails(deviceId); |
| 362 | checkNotNull(deviceDescription, "DeviceDescription cannot be null"); |
| 363 | providerService.deviceConnected(deviceId, deviceDescription); |
| 364 | |
| 365 | if (driver.hasBehaviour(DeviceDescriptionDiscovery.class)) { |
| 366 | DriverHandler h = driverService.createHandler(deviceId); |
| 367 | DeviceDescriptionDiscovery deviceDiscovery = h.behaviour(DeviceDescriptionDiscovery.class); |
| 368 | providerService.updatePorts(deviceId, deviceDiscovery.discoverPortDetails()); |
| 369 | } |
| 370 | } else { |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 371 | DeviceDescription deviceDescription = getDesc(restSBDev); |
Georgios Katsikas | 15841e2 | 2018-07-28 14:27:28 +0200 | [diff] [blame] | 372 | restSBDev.setActive(true); |
| 373 | providerService.deviceConnected(deviceId, deviceDescription); |
| 374 | } |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 375 | checkAndUpdateDevice(deviceId); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Andrea Campanella | b38023b | 2019-02-11 17:21:35 +0100 | [diff] [blame] | 379 | private Driver getDriver(RestSBDevice restSBDev) { |
| 380 | String driverName = netCfgService.getConfig(restSBDev.deviceId(), BasicDeviceConfig.class).driver(); |
| 381 | |
| 382 | Driver driver = driverService.getDriver(driverName); |
| 383 | |
| 384 | if (driver == null) { |
| 385 | driver = driverService.getDriver(restSBDev.manufacturer().get(), |
| 386 | restSBDev.hwVersion().get(), |
| 387 | restSBDev.swVersion().get()); |
| 388 | } |
| 389 | |
| 390 | checkNotNull(driver, DRIVERNULL); |
| 391 | return driver; |
| 392 | } |
| 393 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 394 | private DefaultDeviceDescription mergeAnn(DeviceId devId, DeviceDescription desc) { |
| 395 | return new DefaultDeviceDescription( |
| 396 | desc, |
| 397 | DefaultAnnotations.merge( |
| 398 | DefaultAnnotations.builder() |
| 399 | .set(AnnotationKeys.PROTOCOL, REST.toUpperCase()) |
| 400 | // The rest server added as annotation to the device |
| 401 | .set(AnnotationKeys.REST_SERVER, devId.toString()) |
| 402 | .build(), |
| 403 | desc.annotations())); |
| 404 | } |
| 405 | |
| 406 | private DevicesDiscovery devicesDiscovery(RestSBDevice restSBDevice, Driver driver) { |
| 407 | DriverData driverData = new DefaultDriverData(driver, restSBDevice.deviceId()); |
Georgios Katsikas | 15841e2 | 2018-07-28 14:27:28 +0200 | [diff] [blame] | 408 | DevicesDiscovery devicesDiscovery = driver.createBehaviour(driverData, DevicesDiscovery.class); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 409 | devicesDiscovery.setHandler(new DefaultDriverHandler(driverData)); |
| 410 | return devicesDiscovery; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 413 | private void checkAndUpdateDevice(DeviceId deviceId) { |
| 414 | if (deviceService.getDevice(deviceId) == null) { |
Georgios Katsikas | 15841e2 | 2018-07-28 14:27:28 +0200 | [diff] [blame] | 415 | log.warn("Device {} has not been added to store, maybe due to a problem in connectivity", deviceId); |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 416 | } else { |
| 417 | boolean isReachable = isReachable(deviceId); |
| 418 | if (isReachable && deviceService.isAvailable(deviceId)) { |
| 419 | Device device = deviceService.getDevice(deviceId); |
| 420 | if (device.is(DeviceDescriptionDiscovery.class)) { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 421 | DeviceDescriptionDiscovery deviceDescriptionDiscovery = |
| 422 | device.as(DeviceDescriptionDiscovery.class); |
| 423 | DeviceDescription updatedDeviceDescription = |
| 424 | deviceDescriptionDiscovery.discoverDeviceDetails(); |
| 425 | if (updatedDeviceDescription != null && |
| 426 | !descriptionEquals(device, updatedDeviceDescription)) { |
| 427 | providerService.deviceConnected( |
| 428 | deviceId, |
| 429 | new DefaultDeviceDescription( |
| 430 | updatedDeviceDescription, true, |
| 431 | updatedDeviceDescription.annotations())); |
Andrea Campanella | e288e5b | 2019-04-05 09:54:12 +0200 | [diff] [blame] | 432 | } |
| 433 | //if ports are not discovered, retry the discovery |
| 434 | if (deviceService.getPorts(deviceId).isEmpty()) { |
| 435 | discoverPorts(deviceId); |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 436 | } |
| 437 | } else { |
| 438 | log.warn("No DeviceDescriptionDiscovery behaviour for device {}", deviceId); |
| 439 | } |
| 440 | } else if (!isReachable && deviceService.isAvailable(deviceId)) { |
| 441 | providerService.deviceDisconnected(deviceId); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | private boolean descriptionEquals(Device device, DeviceDescription updatedDeviceDescription) { |
Michele Santuari | d2c8f21 | 2017-01-09 18:23:45 +0100 | [diff] [blame] | 447 | return Objects.equal(device.id().uri(), updatedDeviceDescription.deviceUri()) |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 448 | && Objects.equal(device.type(), updatedDeviceDescription.type()) |
| 449 | && Objects.equal(device.manufacturer(), updatedDeviceDescription.manufacturer()) |
| 450 | && Objects.equal(device.hwVersion(), updatedDeviceDescription.hwVersion()) |
| 451 | && Objects.equal(device.swVersion(), updatedDeviceDescription.swVersion()) |
| 452 | && Objects.equal(device.serialNumber(), updatedDeviceDescription.serialNumber()) |
| 453 | && Objects.equal(device.chassisId(), updatedDeviceDescription.chassisId()) |
| 454 | && Objects.equal(device.annotations(), updatedDeviceDescription.annotations()); |
| 455 | } |
| 456 | |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 457 | private void deviceRemoved(DeviceId deviceId) { |
Andrea Campanella | b38023b | 2019-02-11 17:21:35 +0100 | [diff] [blame] | 458 | checkNotNull(deviceId, DEVICENULL); |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 459 | log.debug("Device removed called for {}", deviceId); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 460 | providerService.deviceDisconnected(deviceId); |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 461 | controller.getProxiedDevices(deviceId).forEach(device -> { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 462 | controller.removeProxiedDevice(device); |
| 463 | providerService.deviceDisconnected(device); |
| 464 | }); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 465 | controller.removeDevice(deviceId); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 468 | //Method to connect devices provided via net-cfg under devices/ tree |
| 469 | private void createAndConnectDevices() { |
| 470 | Set<DeviceId> deviceSubjects = |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 471 | netCfgService.getSubjects(DeviceId.class, RestDeviceConfig.class); |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 472 | log.debug("Connecting and configuring devices with received configuration:{}", |
| 473 | deviceSubjects); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 474 | connectDevices(deviceSubjects.stream() |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 475 | .filter(deviceId -> deviceService.getDevice(deviceId) == null) |
| 476 | .map(deviceId -> { |
| 477 | RestDeviceConfig config = |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 478 | netCfgService.getConfig(deviceId, RestDeviceConfig.class); |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 479 | return toInactiveRestSBDevice(config); |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 480 | }).collect(Collectors.toSet())); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 483 | private RestSBDevice toInactiveRestSBDevice(RestDeviceConfig config) { |
| 484 | return new DefaultRestSBDevice(config.ip(), |
| 485 | config.port(), |
| 486 | config.username(), |
| 487 | config.password(), |
| 488 | config.protocol(), |
| 489 | config.url(), |
| 490 | false, |
| 491 | config.testUrl(), |
| 492 | config.manufacturer(), |
| 493 | config.hwVersion(), |
| 494 | config.swVersion(), |
Georgios Katsikas | 15841e2 | 2018-07-28 14:27:28 +0200 | [diff] [blame] | 495 | config.isProxy(), |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 496 | config.authenticationScheme(), |
| 497 | config.token() |
| 498 | ); |
| 499 | } |
| 500 | |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 501 | private void connectDevices(Set<RestSBDevice> devices) { |
| 502 | //Precomputing the devices to be removed |
| 503 | Set<RestSBDevice> toBeRemoved = new HashSet<>(controller.getDevices().values()); |
| 504 | toBeRemoved.removeAll(devices); |
| 505 | //Adding new devices |
| 506 | devices.stream() |
| 507 | .filter(device -> { |
| 508 | device.setActive(false); |
| 509 | controller.addDevice(device); |
| 510 | return testDeviceConnection(device); |
| 511 | }) |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 512 | .forEach(this::deviceAdded); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 513 | //Removing devices not wanted anymore |
| 514 | toBeRemoved.forEach(device -> deviceRemoved(device.deviceId())); |
| 515 | } |
| 516 | |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 517 | private void connectDevice(RestSBDevice device) { |
| 518 | // TODO borrowed from above, |
| 519 | // not sure why setting it to inactive |
| 520 | device.setActive(false); |
| 521 | controller.addDevice(device); |
| 522 | if (testDeviceConnection(device)) { |
| 523 | deviceAdded(device); |
| 524 | } |
| 525 | } |
| 526 | |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 527 | private ScheduledFuture schedulePolling() { |
| 528 | return portStatisticsExecutor.scheduleAtFixedRate(this::executePortStatisticsUpdate, |
Georgios Katsikas | 0f12bc6 | 2018-07-06 09:06:42 +0200 | [diff] [blame] | 529 | pollFrequency / 2, pollFrequency, |
Michal Mach | d809974 | 2017-06-19 14:32:35 +0200 | [diff] [blame] | 530 | TimeUnit.SECONDS); |
| 531 | } |
| 532 | |
| 533 | private void executePortStatisticsUpdate() { |
| 534 | controller.getDevices().keySet().forEach(this::updatePortStatistics); |
| 535 | } |
| 536 | |
| 537 | private void updatePortStatistics(DeviceId deviceId) { |
| 538 | Device device = deviceService.getDevice(deviceId); |
| 539 | checkNotNull(device, "device cannot be null"); |
| 540 | |
| 541 | if (device.is(PortStatisticsDiscovery.class)) { |
| 542 | PortStatisticsDiscovery portStatisticsDiscovery = device.as(PortStatisticsDiscovery.class); |
| 543 | Collection<PortStatistics> portStatistics = portStatisticsDiscovery.discoverPortStatistics(); |
| 544 | if (portStatistics != null && !portStatistics.isEmpty()) { |
| 545 | providerService.updatePortStatistics(deviceId, portStatistics); |
| 546 | } |
| 547 | } else { |
| 548 | log.debug("No port statistics getter behaviour for device {}", deviceId); |
| 549 | } |
| 550 | } |
| 551 | |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 552 | private void discoverPorts(DeviceId deviceId) { |
| 553 | Device device = deviceService.getDevice(deviceId); |
Georgios Katsikas | 15841e2 | 2018-07-28 14:27:28 +0200 | [diff] [blame] | 554 | DeviceDescriptionDiscovery deviceDescriptionDiscovery = device.as(DeviceDescriptionDiscovery.class); |
Ray Milkey | 640fedd | 2018-02-08 09:02:26 -0800 | [diff] [blame] | 555 | providerService.updatePorts(deviceId, deviceDescriptionDiscovery.discoverPortDetails()); |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 558 | private boolean testDeviceConnection(RestSBDevice dev) { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 559 | try { |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 560 | Callable<Boolean> connectionSuccess; |
| 561 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 562 | if (dev.testUrl().isPresent()) { |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 563 | connectionSuccess = () -> |
| 564 | controller.get(dev.deviceId(), dev.testUrl().get(), MediaType.APPLICATION_JSON_TYPE) != null; |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 565 | } else { |
Michal Mach | 67acb69 | 2017-06-21 12:05:36 +0200 | [diff] [blame] | 566 | connectionSuccess = () -> |
| 567 | controller.get(dev.deviceId(), "", MediaType.APPLICATION_JSON_TYPE) != null; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 568 | } |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 569 | |
| 570 | Future<Boolean> future = executor.submit(connectionSuccess); |
| 571 | try { |
Andrea Campanella | 02f86c8 | 2019-03-18 16:57:27 -0700 | [diff] [blame] | 572 | return future.get(replyTimeout, TimeUnit.SECONDS); |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 573 | } catch (TimeoutException ex) { |
Sean Condon | e45e12c | 2018-05-04 16:27:07 +0100 | [diff] [blame] | 574 | log.warn("Connection to device {} timed out: {}", dev.deviceId(), ex.getMessage()); |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 575 | return false; |
| 576 | } catch (InterruptedException ex) { |
Sean Condon | e45e12c | 2018-05-04 16:27:07 +0100 | [diff] [blame] | 577 | log.warn("Connection to device {} interrupted: {}", dev.deviceId(), ex.getMessage()); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 578 | Thread.currentThread().interrupt(); |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 579 | return false; |
| 580 | } catch (ExecutionException ex) { |
Sean Condon | e45e12c | 2018-05-04 16:27:07 +0100 | [diff] [blame] | 581 | log.warn("Connection to device {} had an execution exception.", dev.deviceId(), ex); |
Palash Kala | 4c71ee7 | 2017-05-24 17:43:59 +0900 | [diff] [blame] | 582 | return false; |
| 583 | } |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 584 | |
Andrea Campanella | c6ecc63 | 2016-03-10 17:57:06 -0800 | [diff] [blame] | 585 | } catch (ProcessingException e) { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 586 | log.warn("Cannot connect to device {}", dev, e); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 587 | } |
| 588 | return false; |
| 589 | } |
| 590 | |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 591 | private Runnable exceptionSafe(Runnable runnable) { |
| 592 | return () -> { |
| 593 | try { |
| 594 | runnable.run(); |
| 595 | } catch (Exception e) { |
| 596 | log.error("Unhandled Exception", e); |
| 597 | } |
| 598 | }; |
| 599 | } |
| 600 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 601 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 602 | @Override |
| 603 | public void event(NetworkConfigEvent event) { |
Georgios Katsikas | 15841e2 | 2018-07-28 14:27:28 +0200 | [diff] [blame] | 604 | if (!isRelevant(event)) { |
| 605 | log.warn("Irrelevant network configuration event: {}", event); |
| 606 | return; |
| 607 | } |
| 608 | |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 609 | ExecutorService bg = SharedExecutors.getSingleThreadExecutor(); |
| 610 | if (event.type() == CONFIG_REMOVED) { |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 611 | log.debug("Config {} event for rest device provider for {}", |
| 612 | event.type(), event.prevConfig().get().subject()); |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 613 | DeviceId did = (DeviceId) event.subject(); |
| 614 | bg.execute(() -> deviceRemoved(did)); |
| 615 | } else { |
Saritha | bd0b1f1 | 2018-05-11 11:35:27 +0530 | [diff] [blame] | 616 | //CONFIG_ADDED or CONFIG_UPDATED |
| 617 | log.debug("Config {} event for rest device provider for {}", |
| 618 | event.type(), event.config().get().subject()); |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 619 | RestDeviceConfig cfg = (RestDeviceConfig) event.config().get(); |
| 620 | RestSBDevice restSBDevice = toInactiveRestSBDevice(cfg); |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 621 | bg.execute(exceptionSafe(() -> connectDevice(restSBDevice))); |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 622 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | @Override |
| 626 | public boolean isRelevant(NetworkConfigEvent event) { |
Yuta HIGUCHI | 872fdbe | 2017-12-05 15:34:28 -0800 | [diff] [blame] | 627 | return event.configClass().equals(RestDeviceConfig.class) && |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 628 | (event.type() == CONFIG_ADDED || |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 629 | event.type() == CONFIG_UPDATED || |
| 630 | event.type() == CONFIG_REMOVED); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 631 | } |
| 632 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 633 | |
| 634 | @Override |
| 635 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 636 | boolean enable) { |
fahadnaeemkhan | 482951f | 2017-08-24 16:35:17 -0700 | [diff] [blame] | 637 | Device device = deviceService.getDevice(deviceId); |
| 638 | if (device != null) { |
| 639 | if (device.is(PortAdmin.class)) { |
| 640 | PortAdmin portAdmin = device.as(PortAdmin.class); |
| 641 | CompletableFuture<Boolean> modified; |
| 642 | if (enable) { |
| 643 | modified = portAdmin.enable(portNumber); |
| 644 | } else { |
| 645 | modified = portAdmin.disable(portNumber); |
| 646 | } |
| 647 | modified.thenAcceptAsync(result -> { |
| 648 | if (!result) { |
| 649 | log.warn("Device {} port {} state can't be changed to {}", |
| 650 | deviceId, portNumber, enable); |
| 651 | } |
| 652 | }); |
| 653 | |
| 654 | } else { |
| 655 | log.warn("Device {} does not support PortAdmin behavior", deviceId); |
| 656 | } |
| 657 | } else { |
| 658 | log.warn("unable to get the device {}, port {} state can't be changed to {}", |
| 659 | deviceId, portNumber, enable); |
| 660 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 661 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 662 | } |