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