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