Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [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.general.device.impl; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Maps; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 22 | import com.google.common.util.concurrent.Striped; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 23 | import org.onlab.packet.ChassisId; |
| 24 | import org.onlab.util.ItemNotFoundException; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 25 | import org.onlab.util.Tools; |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 26 | import org.onosproject.cfg.ComponentConfigService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 27 | import org.onosproject.core.CoreService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 28 | import org.onosproject.mastership.MastershipService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 29 | import org.onosproject.net.AnnotationKeys; |
| 30 | import org.onosproject.net.DefaultAnnotations; |
| 31 | import org.onosproject.net.Device; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.MastershipRole; |
| 34 | import org.onosproject.net.PortNumber; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 35 | import org.onosproject.net.behaviour.PiPipelineProgrammable; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 36 | import org.onosproject.net.behaviour.PortAdmin; |
| 37 | import org.onosproject.net.config.ConfigFactory; |
| 38 | import org.onosproject.net.config.NetworkConfigEvent; |
| 39 | import org.onosproject.net.config.NetworkConfigListener; |
| 40 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 41 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 42 | import org.onosproject.net.config.basics.SubjectFactories; |
| 43 | import org.onosproject.net.device.DefaultDeviceDescription; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 44 | import org.onosproject.net.device.DeviceAgentEvent; |
| 45 | import org.onosproject.net.device.DeviceAgentListener; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 46 | import org.onosproject.net.device.DeviceDescription; |
| 47 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 48 | import org.onosproject.net.device.DeviceEvent; |
| 49 | import org.onosproject.net.device.DeviceHandshaker; |
| 50 | import org.onosproject.net.device.DeviceListener; |
| 51 | import org.onosproject.net.device.DeviceProvider; |
| 52 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 53 | import org.onosproject.net.device.DeviceProviderService; |
| 54 | import org.onosproject.net.device.DeviceService; |
| 55 | import org.onosproject.net.device.PortDescription; |
| 56 | import org.onosproject.net.device.PortStatistics; |
| 57 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 58 | import org.onosproject.net.driver.Behaviour; |
| 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.DriverService; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 64 | import org.onosproject.net.pi.model.PiPipeconf; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 65 | import org.onosproject.net.pi.model.PiPipeconfId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 66 | import org.onosproject.net.pi.service.PiPipeconfConfig; |
| 67 | import org.onosproject.net.pi.service.PiPipeconfService; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 68 | import org.onosproject.net.pi.service.PiPipeconfWatchdogEvent; |
| 69 | import org.onosproject.net.pi.service.PiPipeconfWatchdogListener; |
| 70 | import org.onosproject.net.pi.service.PiPipeconfWatchdogService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 71 | import org.onosproject.net.provider.AbstractProvider; |
| 72 | import org.onosproject.net.provider.ProviderId; |
| 73 | import org.onosproject.provider.general.device.api.GeneralProviderDeviceConfig; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 74 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 75 | import org.osgi.service.component.annotations.Activate; |
| 76 | import org.osgi.service.component.annotations.Component; |
| 77 | import org.osgi.service.component.annotations.Deactivate; |
| 78 | import org.osgi.service.component.annotations.Modified; |
| 79 | import org.osgi.service.component.annotations.Reference; |
| 80 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 81 | import org.slf4j.Logger; |
| 82 | |
Ray Milkey | b68bbbc | 2017-12-18 10:05:49 -0800 | [diff] [blame] | 83 | import java.security.SecureRandom; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 84 | import java.util.Collection; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 85 | import java.util.Collections; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 86 | import java.util.Dictionary; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 87 | import java.util.List; |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 88 | import java.util.Map; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 89 | import java.util.Set; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 90 | import java.util.StringJoiner; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 91 | import java.util.concurrent.CompletableFuture; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 92 | import java.util.concurrent.ConcurrentHashMap; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 93 | import java.util.concurrent.ConcurrentMap; |
| 94 | import java.util.concurrent.CopyOnWriteArraySet; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 95 | import java.util.concurrent.ExecutionException; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 96 | import java.util.concurrent.ExecutorService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 97 | import java.util.concurrent.ScheduledExecutorService; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 98 | import java.util.concurrent.ScheduledFuture; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 99 | import java.util.concurrent.TimeUnit; |
| 100 | import java.util.concurrent.TimeoutException; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 101 | import java.util.concurrent.locks.Lock; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 102 | import java.util.function.Supplier; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 103 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 104 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 105 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 106 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 107 | import static org.onlab.util.Tools.groupedThreads; |
| 108 | import static org.onosproject.net.device.DeviceEvent.Type; |
| 109 | import static org.slf4j.LoggerFactory.getLogger; |
| 110 | |
| 111 | /** |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 112 | * Provider which uses drivers to detect device and do initial handshake and |
| 113 | * channel establishment with devices. Any other provider specific operation is |
| 114 | * also delegated to the DeviceHandshaker driver. |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 115 | */ |
| 116 | @Beta |
| 117 | @Component(immediate = true) |
| 118 | public class GeneralDeviceProvider extends AbstractProvider |
| 119 | implements DeviceProvider { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 120 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 121 | private final Logger log = getLogger(getClass()); |
| 122 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 123 | private static final String APP_NAME = "org.onosproject.gdp"; |
| 124 | private static final String URI_SCHEME = "device"; |
| 125 | private static final String CFG_SCHEME = "generalprovider"; |
| 126 | private static final String DEVICE_PROVIDER_PACKAGE = |
| 127 | "org.onosproject.general.provider.device"; |
| 128 | private static final int CORE_POOL_SIZE = 10; |
| 129 | private static final String UNKNOWN = "unknown"; |
| 130 | private static final String DRIVER = "driver"; |
| 131 | private static final Set<String> PIPELINE_CONFIGURABLE_PROTOCOLS = |
| 132 | ImmutableSet.of("p4runtime"); |
| 133 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 135 | private DeviceProviderRegistry providerRegistry; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 136 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 138 | private ComponentConfigService componentConfigService; |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 139 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 141 | private NetworkConfigRegistry cfgService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 142 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 143 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 144 | private CoreService coreService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 145 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 146 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 147 | private DeviceService deviceService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 148 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 149 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 150 | private DriverService driverService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 151 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 152 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 153 | private MastershipService mastershipService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 154 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 155 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 156 | private PiPipeconfService pipeconfService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 157 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 158 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 159 | private PiPipeconfWatchdogService pipeconfWatchdogService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 160 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 161 | private static final String STATS_POLL_FREQUENCY = "deviceStatsPollFrequency"; |
| 162 | private static final int DEFAULT_STATS_POLL_FREQUENCY = 10; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 163 | //@Property(name = STATS_POLL_FREQUENCY, intValue = DEFAULT_STATS_POLL_FREQUENCY, |
| 164 | // label = "Configure poll frequency for port status and statistics; " + |
| 165 | // "default is 10 sec") |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 166 | private int statsPollFrequency = DEFAULT_STATS_POLL_FREQUENCY; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 167 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 168 | private static final String PROBE_FREQUENCY = "deviceProbeFrequency"; |
| 169 | private static final int DEFAULT_PROBE_FREQUENCY = 10; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 170 | //@Property(name = PROBE_FREQUENCY, intValue = DEFAULT_PROBE_FREQUENCY, |
| 171 | // label = "Configure probe frequency for checking device availability; " + |
| 172 | // "default is 10 sec") |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 173 | private int probeFrequency = DEFAULT_PROBE_FREQUENCY; |
| 174 | |
| 175 | private static final String OP_TIMEOUT_SHORT = "deviceOperationTimeoutShort"; |
| 176 | private static final int DEFAULT_OP_TIMEOUT_SHORT = 10; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 177 | //@Property(name = OP_TIMEOUT_SHORT, intValue = DEFAULT_OP_TIMEOUT_SHORT, |
| 178 | // label = "Configure timeout in seconds for device operations " + |
| 179 | // "that are supposed to take a short time " + |
| 180 | // "(e.g. checking device reachability); default is 10 seconds") |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 181 | private int opTimeoutShort = DEFAULT_OP_TIMEOUT_SHORT; |
| 182 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 183 | //FIXME to be removed when netcfg will issue device events in a bundle or |
| 184 | //ensures all configuration needed is present |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 185 | private final Set<DeviceId> deviceConfigured = new CopyOnWriteArraySet<>(); |
| 186 | private final Set<DeviceId> driverConfigured = new CopyOnWriteArraySet<>(); |
| 187 | private final Set<DeviceId> pipelineConfigured = new CopyOnWriteArraySet<>(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 188 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 189 | private final Map<DeviceId, MastershipRole> requestedRoles = Maps.newConcurrentMap(); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 190 | private final ConcurrentMap<DeviceId, ScheduledFuture<?>> statsPollingTasks = new ConcurrentHashMap<>(); |
| 191 | private final Map<DeviceId, DeviceHandshaker> handshakersWithListeners = Maps.newConcurrentMap(); |
| 192 | private final InternalDeviceListener deviceListener = new InternalDeviceListener(); |
| 193 | private final InternalPipeconfWatchdogListener pipeconfWatchdogListener = new InternalPipeconfWatchdogListener(); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 194 | private final NetworkConfigListener cfgListener = new InternalNetworkConfigListener(); |
| 195 | private final DeviceAgentListener deviceAgentListener = new InternalDeviceAgentListener(); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 196 | private final ConfigFactory factory = new InternalConfigFactory(); |
| 197 | private final Striped<Lock> deviceLocks = Striped.lock(30); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 198 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 199 | private ExecutorService connectionExecutor; |
| 200 | private ScheduledExecutorService statsExecutor; |
| 201 | private ScheduledExecutorService probeExecutor; |
| 202 | private ScheduledFuture<?> probeTask; |
| 203 | private DeviceProviderService providerService; |
| 204 | |
| 205 | public GeneralDeviceProvider() { |
| 206 | super(new ProviderId(URI_SCHEME, DEVICE_PROVIDER_PACKAGE)); |
| 207 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 208 | |
| 209 | @Activate |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 210 | public void activate(ComponentContext context) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 211 | connectionExecutor = newFixedThreadPool(CORE_POOL_SIZE, groupedThreads( |
| 212 | "onos/gdp-connect", "%d", log)); |
| 213 | statsExecutor = newScheduledThreadPool(CORE_POOL_SIZE, groupedThreads( |
| 214 | "onos/gdp-stats", "%d", log)); |
| 215 | probeExecutor = newSingleThreadScheduledExecutor(groupedThreads( |
| 216 | "onos/gdp-probe", "%d", log)); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 217 | providerService = providerRegistry.register(this); |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 218 | componentConfigService.registerProperties(getClass()); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 219 | coreService.registerApplication(APP_NAME); |
| 220 | cfgService.registerConfigFactory(factory); |
| 221 | cfgService.addListener(cfgListener); |
| 222 | deviceService.addListener(deviceListener); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 223 | pipeconfWatchdogService.addListener(pipeconfWatchdogListener); |
| 224 | rescheduleProbeTask(false); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 225 | modified(context); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 226 | log.info("Started"); |
| 227 | } |
| 228 | |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 229 | @Modified |
| 230 | public void modified(ComponentContext context) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 231 | if (context == null) { |
| 232 | return; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 235 | Dictionary<?, ?> properties = context.getProperties(); |
| 236 | final int oldStatsPollFrequency = statsPollFrequency; |
| 237 | statsPollFrequency = Tools.getIntegerProperty( |
| 238 | properties, STATS_POLL_FREQUENCY, DEFAULT_STATS_POLL_FREQUENCY); |
| 239 | log.info("Configured. {} is configured to {} seconds", |
| 240 | STATS_POLL_FREQUENCY, statsPollFrequency); |
| 241 | final int oldProbeFrequency = probeFrequency; |
| 242 | probeFrequency = Tools.getIntegerProperty( |
| 243 | properties, PROBE_FREQUENCY, DEFAULT_PROBE_FREQUENCY); |
| 244 | log.info("Configured. {} is configured to {} seconds", |
| 245 | PROBE_FREQUENCY, probeFrequency); |
| 246 | opTimeoutShort = Tools.getIntegerProperty( |
| 247 | properties, OP_TIMEOUT_SHORT, DEFAULT_OP_TIMEOUT_SHORT); |
| 248 | log.info("Configured. {} is configured to {} seconds", |
| 249 | OP_TIMEOUT_SHORT, opTimeoutShort); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 250 | |
| 251 | if (oldStatsPollFrequency != statsPollFrequency) { |
| 252 | rescheduleStatsPollingTasks(); |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 253 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 254 | |
| 255 | if (oldProbeFrequency != probeFrequency) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 256 | rescheduleProbeTask(true); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 260 | private void rescheduleProbeTask(boolean deelay) { |
| 261 | synchronized (this) { |
| 262 | if (probeTask != null) { |
| 263 | probeTask.cancel(false); |
| 264 | } |
| 265 | probeTask = probeExecutor.scheduleAtFixedRate( |
| 266 | this::triggerProbeAllDevices, |
| 267 | deelay ? probeFrequency : 0, |
| 268 | probeFrequency, |
| 269 | TimeUnit.SECONDS); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 270 | } |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 273 | @Deactivate |
| 274 | public void deactivate() { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 275 | // Shutdown stats polling tasks. |
| 276 | statsPollingTasks.keySet().forEach(this::cancelStatsPolling); |
| 277 | statsPollingTasks.clear(); |
| 278 | statsExecutor.shutdownNow(); |
| 279 | try { |
| 280 | statsExecutor.awaitTermination(5, TimeUnit.SECONDS); |
| 281 | } catch (InterruptedException e) { |
| 282 | log.warn("statsExecutor not terminated properly"); |
| 283 | } |
| 284 | statsExecutor = null; |
| 285 | // Shutdown probe executor. |
| 286 | probeTask.cancel(true); |
| 287 | probeTask = null; |
| 288 | probeExecutor.shutdownNow(); |
| 289 | try { |
| 290 | probeExecutor.awaitTermination(5, TimeUnit.SECONDS); |
| 291 | } catch (InterruptedException e) { |
| 292 | log.warn("probeExecutor not terminated properly"); |
| 293 | } |
| 294 | probeExecutor = null; |
| 295 | // Shutdown connection executor. |
| 296 | connectionExecutor.shutdownNow(); |
| 297 | try { |
| 298 | connectionExecutor.awaitTermination(5, TimeUnit.SECONDS); |
| 299 | } catch (InterruptedException e) { |
| 300 | log.warn("connectionExecutor not terminated properly"); |
| 301 | } |
| 302 | connectionExecutor = null; |
| 303 | // Remove all device agent listeners |
| 304 | handshakersWithListeners.values().forEach(h -> h.removeDeviceAgentListener(id())); |
| 305 | handshakersWithListeners.clear(); |
| 306 | // Other cleanup. |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 307 | componentConfigService.unregisterProperties(getClass(), false); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 308 | cfgService.removeListener(cfgListener); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 309 | deviceService.removeListener(deviceListener); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 310 | pipeconfWatchdogService.removeListener(pipeconfWatchdogListener); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 311 | providerRegistry.unregister(this); |
| 312 | providerService = null; |
| 313 | cfgService.unregisterConfigFactory(factory); |
| 314 | log.info("Stopped"); |
| 315 | } |
| 316 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 317 | |
| 318 | @Override |
| 319 | public void triggerProbe(DeviceId deviceId) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 320 | connectionExecutor.execute(withDeviceLock( |
| 321 | () -> doDeviceProbe(deviceId), deviceId)); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 326 | log.info("Notifying role {} to device {}", newRole, deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 327 | requestedRoles.put(deviceId, newRole); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 328 | connectionExecutor.execute(() -> doRoleChanged(deviceId, newRole)); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | private void doRoleChanged(DeviceId deviceId, MastershipRole newRole) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 332 | final DeviceHandshaker handshaker = getBehaviour( |
| 333 | deviceId, DeviceHandshaker.class); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 334 | if (handshaker == null) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 335 | log.error("Null handshaker. Unable to notify new role {} to {}", |
| 336 | newRole, deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 337 | return; |
| 338 | } |
| 339 | handshaker.roleChanged(newRole); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public boolean isReachable(DeviceId deviceId) { |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 344 | log.debug("Testing reachability for device {}", deviceId); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 345 | final DeviceHandshaker handshaker = getBehaviour( |
| 346 | deviceId, DeviceHandshaker.class); |
Andrea Campanella | c1ecdd0 | 2018-01-12 12:48:24 +0100 | [diff] [blame] | 347 | if (handshaker == null) { |
| 348 | return false; |
| 349 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 350 | return getFutureWithDeadline( |
| 351 | handshaker.isReachable(), "checking reachability", |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 352 | deviceId, false, opTimeoutShort); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 355 | private boolean isConnected(DeviceId deviceId) { |
| 356 | log.debug("Testing connection to device {}", deviceId); |
| 357 | final DeviceHandshaker handshaker = getBehaviour( |
| 358 | deviceId, DeviceHandshaker.class); |
| 359 | if (handshaker == null) { |
| 360 | return false; |
| 361 | } |
| 362 | return handshaker.isConnected(); |
| 363 | } |
| 364 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 365 | @Override |
| 366 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 367 | boolean enable) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 368 | connectionExecutor.execute( |
| 369 | () -> doChangePortState(deviceId, portNumber, enable)); |
| 370 | } |
| 371 | |
| 372 | private void doChangePortState(DeviceId deviceId, PortNumber portNumber, |
| 373 | boolean enable) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 374 | if (!deviceService.getDevice(deviceId).is(PortAdmin.class)) { |
| 375 | log.warn("Missing PortAdmin behaviour on {}, aborting port state change", |
| 376 | deviceId); |
| 377 | return; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 378 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 379 | final PortAdmin portAdmin = deviceService.getDevice(deviceId) |
| 380 | .as(PortAdmin.class); |
| 381 | final CompletableFuture<Boolean> modifyTask = enable |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 382 | ? portAdmin.enable(portNumber) |
| 383 | : portAdmin.disable(portNumber); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 384 | final String descr = (enable ? "enabling" : "disabling") + " port " + portNumber; |
| 385 | getFutureWithDeadline( |
| 386 | modifyTask, descr, deviceId, null, opTimeoutShort); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 389 | @Override |
| 390 | public void triggerDisconnect(DeviceId deviceId) { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 391 | log.debug("Triggering disconnection of device {}", deviceId); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 392 | connectionExecutor.execute(withDeviceLock( |
| 393 | () -> doDisconnectDevice(deviceId), deviceId)); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 396 | private Driver getDriver(DeviceId deviceId) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 397 | try { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 398 | // DriverManager checks first using basic device config. |
| 399 | return driverService.getDriver(deviceId); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 400 | } catch (ItemNotFoundException e) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 401 | log.error("Driver not found for {}", deviceId); |
| 402 | return null; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 403 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 406 | private <T extends Behaviour> T getBehaviour(DeviceId deviceId, Class<T> type) { |
| 407 | // Get handshaker. |
| 408 | |
| 409 | Driver driver = getDriver(deviceId); |
| 410 | if (driver == null) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 411 | return null; |
| 412 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 413 | if (!driver.hasBehaviour(type)) { |
| 414 | return null; |
| 415 | } |
| 416 | final DriverData data = new DefaultDriverData(driver, deviceId); |
| 417 | // Storing deviceKeyId and all other config values as data in the driver |
| 418 | // with protocol_<info> name as the key. e.g protocol_ip. |
| 419 | final GeneralProviderDeviceConfig providerConfig = cfgService.getConfig( |
| 420 | deviceId, GeneralProviderDeviceConfig.class); |
| 421 | if (providerConfig != null) { |
| 422 | providerConfig.protocolsInfo().forEach((protocol, info) -> { |
| 423 | info.configValues().forEach( |
| 424 | (k, v) -> data.set(protocol + "_" + k, v)); |
| 425 | data.set(protocol + "_key", info.deviceKeyId()); |
| 426 | }); |
| 427 | } |
| 428 | final DefaultDriverHandler handler = new DefaultDriverHandler(data); |
| 429 | return driver.createBehaviour(handler, type); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 432 | private void doConnectDevice(DeviceId deviceId) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 433 | log.debug("Initiating connection to device {}...", deviceId); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 434 | // Retrieve config |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 435 | if (configIsMissing(deviceId)) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 436 | return; |
| 437 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 438 | // Bind pipeconf (if any and if device is capable). |
| 439 | if (!bindPipeconfIfRequired(deviceId)) { |
| 440 | // We already logged the error. |
| 441 | return; |
| 442 | } |
| 443 | // Get handshaker. |
| 444 | final DeviceHandshaker handshaker = getBehaviour( |
| 445 | deviceId, DeviceHandshaker.class); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 446 | if (handshaker == null) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 447 | log.error("Missing handshaker behavior for {}, aborting connection", |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 448 | deviceId); |
| 449 | return; |
| 450 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 451 | // Add device agent listener. |
| 452 | handshaker.addDeviceAgentListener(id(), deviceAgentListener); |
| 453 | handshakersWithListeners.put(deviceId, handshaker); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 454 | // Start connection via handshaker. |
| 455 | final Boolean connectSuccess = getFutureWithDeadline( |
| 456 | handshaker.connect(), "initiating connection", |
Carmelo Cascone | de3b684 | 2018-09-05 17:45:10 -0700 | [diff] [blame] | 457 | deviceId, false, opTimeoutShort); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 458 | if (!connectSuccess) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 459 | log.warn("Unable to connect to {}", deviceId); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | private void triggerAdvertiseDevice(DeviceId deviceId) { |
| 464 | connectionExecutor.execute(withDeviceLock( |
| 465 | () -> doAdvertiseDevice(deviceId), deviceId)); |
| 466 | } |
| 467 | |
| 468 | private void doAdvertiseDevice(DeviceId deviceId) { |
| 469 | // Retrieve config |
| 470 | if (configIsMissing(deviceId)) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 471 | return; |
| 472 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 473 | // Obtain device and port description. |
| 474 | final boolean isPipelineReady = isPipelineReady(deviceId); |
| 475 | final DeviceDescription description = getDeviceDescription( |
| 476 | deviceId, isPipelineReady); |
| 477 | final List<PortDescription> ports = getPortDetails(deviceId); |
| 478 | // Advertise to core. |
| 479 | if (deviceService.getDevice(deviceId) == null || |
| 480 | (description.isDefaultAvailable() && |
| 481 | !deviceService.isAvailable(deviceId))) { |
| 482 | if (!isPipelineReady) { |
| 483 | log.info("Advertising device to core with available={} as " + |
| 484 | "device pipeline is not ready yet", |
| 485 | description.isDefaultAvailable()); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 486 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 487 | providerService.deviceConnected(deviceId, description); |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 488 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 489 | providerService.updatePorts(deviceId, ports); |
| 490 | // If pipeline is not ready, encourage watchdog to perform probe ASAP. |
| 491 | if (!isPipelineReady) { |
| 492 | pipeconfWatchdogService.triggerProbe(deviceId); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | private DeviceDescription getDeviceDescription( |
| 497 | DeviceId deviceId, boolean defaultAvailable) { |
| 498 | // Get one from driver or forge. |
| 499 | final DeviceDescriptionDiscovery deviceDiscovery = getBehaviour( |
| 500 | deviceId, DeviceDescriptionDiscovery.class); |
| 501 | if (deviceDiscovery != null) { |
| 502 | // Enforce defaultAvailable flag over the one obtained from driver. |
| 503 | final DeviceDescription d = deviceDiscovery.discoverDeviceDetails(); |
| 504 | return new DefaultDeviceDescription(d, defaultAvailable, d.annotations()); |
| 505 | } else { |
| 506 | return forgeDeviceDescription(deviceId, defaultAvailable); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | private List<PortDescription> getPortDetails(DeviceId deviceId) { |
| 511 | final DeviceDescriptionDiscovery deviceDiscovery = getBehaviour( |
| 512 | deviceId, DeviceDescriptionDiscovery.class); |
| 513 | if (deviceDiscovery != null) { |
| 514 | return deviceDiscovery.discoverPortDetails(); |
| 515 | } else { |
| 516 | return Collections.emptyList(); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | private DeviceDescription forgeDeviceDescription( |
| 521 | DeviceId deviceId, boolean defaultAvailable) { |
| 522 | // Uses handshaker and provider config to get driver data. |
| 523 | final DeviceHandshaker handshaker = getBehaviour( |
| 524 | deviceId, DeviceHandshaker.class); |
| 525 | final Driver driver = handshaker != null |
| 526 | ? handshaker.handler().driver() : null; |
| 527 | final GeneralProviderDeviceConfig cfg = cfgService.getConfig( |
| 528 | deviceId, GeneralProviderDeviceConfig.class); |
| 529 | final DefaultAnnotations.Builder annBuilder = DefaultAnnotations.builder(); |
| 530 | // If device is pipeline programmable, let this provider decide when the |
| 531 | // device can be marked online. |
| 532 | annBuilder.set(AnnotationKeys.PROVIDER_MARK_ONLINE, |
| 533 | String.valueOf(isPipelineProgrammable(deviceId))); |
| 534 | if (cfg != null) { |
| 535 | StringJoiner protoStringBuilder = new StringJoiner(", "); |
| 536 | cfg.protocolsInfo().keySet().forEach(protoStringBuilder::add); |
| 537 | annBuilder.set(AnnotationKeys.PROTOCOL, protoStringBuilder.toString()); |
| 538 | } |
| 539 | return new DefaultDeviceDescription( |
| 540 | deviceId.uri(), |
| 541 | Device.Type.SWITCH, |
| 542 | driver != null ? driver.manufacturer() : UNKNOWN, |
| 543 | driver != null ? driver.hwVersion() : UNKNOWN, |
| 544 | driver != null ? driver.swVersion() : UNKNOWN, |
| 545 | UNKNOWN, |
| 546 | new ChassisId(), |
| 547 | defaultAvailable, |
| 548 | annBuilder.build()); |
| 549 | } |
| 550 | |
| 551 | private void triggerMarkAvailable(DeviceId deviceId) { |
| 552 | connectionExecutor.execute(withDeviceLock( |
| 553 | () -> doMarkAvailable(deviceId), deviceId)); |
| 554 | } |
| 555 | |
| 556 | private void doMarkAvailable(DeviceId deviceId) { |
| 557 | if (deviceService.isAvailable(deviceId)) { |
| 558 | return; |
| 559 | } |
| 560 | final DeviceDescription descr = getDeviceDescription(deviceId, true); |
| 561 | // It has been observed that devices that were marked offline (e.g. |
| 562 | // after device disconnection) might end up with no master. Here we |
| 563 | // trigger a new master election (if device has no master). |
| 564 | mastershipService.requestRoleForSync(deviceId); |
| 565 | providerService.deviceConnected(deviceId, descr); |
| 566 | } |
| 567 | |
| 568 | private boolean bindPipeconfIfRequired(DeviceId deviceId) { |
| 569 | if (pipeconfService.ofDevice(deviceId).isPresent() |
| 570 | || !isPipelineProgrammable(deviceId)) { |
| 571 | // Nothing to do, all good. |
| 572 | return true; |
| 573 | } |
| 574 | // Get pipeconf from netcfg or driver (default one). |
| 575 | final PiPipelineProgrammable pipelineProg = getBehaviour( |
| 576 | deviceId, PiPipelineProgrammable.class); |
| 577 | final PiPipeconfId pipeconfId = getPipeconfId(deviceId, pipelineProg); |
| 578 | if (pipeconfId == null) { |
| 579 | return false; |
| 580 | } |
| 581 | // Store binding in pipeconf service. |
| 582 | pipeconfService.bindToDevice(pipeconfId, deviceId); |
| 583 | return true; |
| 584 | } |
| 585 | |
| 586 | private PiPipeconfId getPipeconfId(DeviceId deviceId, PiPipelineProgrammable pipelineProg) { |
| 587 | // Places to look for a pipeconf ID (in priority order)): |
| 588 | // 1) netcfg |
| 589 | // 2) device driver (default one) |
| 590 | final PiPipeconfId pipeconfId = getPipeconfFromCfg(deviceId); |
| 591 | if (pipeconfId != null && !pipeconfId.id().isEmpty()) { |
| 592 | return pipeconfId; |
| 593 | } |
| 594 | if (pipelineProg != null |
| 595 | && pipelineProg.getDefaultPipeconf().isPresent()) { |
| 596 | final PiPipeconf defaultPipeconf = pipelineProg.getDefaultPipeconf().get(); |
| 597 | log.info("Using default pipeconf {} for {}", defaultPipeconf.id(), deviceId); |
| 598 | return defaultPipeconf.id(); |
| 599 | } else { |
| 600 | log.warn("Unable to associate a pipeconf to {}", deviceId); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 601 | return null; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 602 | } |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 603 | } |
| 604 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 605 | private void doDisconnectDevice(DeviceId deviceId) { |
Carmelo Cascone | 7044efd | 2018-07-06 13:01:36 +0200 | [diff] [blame] | 606 | log.debug("Initiating disconnection from {}...", deviceId); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 607 | final DeviceHandshaker handshaker = getBehaviour( |
| 608 | deviceId, DeviceHandshaker.class); |
Carmelo Cascone | 7044efd | 2018-07-06 13:01:36 +0200 | [diff] [blame] | 609 | final boolean isAvailable = deviceService.isAvailable(deviceId); |
| 610 | // Signal disconnection to core (if master). |
| 611 | if (isAvailable && mastershipService.isLocalMaster(deviceId)) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 612 | providerService.deviceDisconnected(deviceId); |
| 613 | } |
Carmelo Cascone | 7044efd | 2018-07-06 13:01:36 +0200 | [diff] [blame] | 614 | // Cancel tasks. |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 615 | cancelStatsPolling(deviceId); |
Carmelo Cascone | 7044efd | 2018-07-06 13:01:36 +0200 | [diff] [blame] | 616 | // Disconnect device. |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 617 | if (handshaker == null) { |
Carmelo Cascone | 7044efd | 2018-07-06 13:01:36 +0200 | [diff] [blame] | 618 | if (isAvailable) { |
| 619 | // If not available don't bother logging. We are probably |
| 620 | // invoking this method multiple times for the same device. |
| 621 | log.warn("Missing DeviceHandshaker behavior for {}, " + |
| 622 | "no guarantees of complete disconnection", |
| 623 | deviceId); |
| 624 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 625 | return; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 626 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 627 | handshaker.removeDeviceAgentListener(id()); |
| 628 | handshakersWithListeners.remove(deviceId); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 629 | final boolean disconnectSuccess = getFutureWithDeadline( |
| 630 | handshaker.disconnect(), "performing disconnection", |
| 631 | deviceId, false, opTimeoutShort); |
| 632 | if (!disconnectSuccess) { |
| 633 | log.warn("Unable to disconnect from {}", deviceId); |
| 634 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 637 | // Needed to catch the exception in the executors since are not rethrown otherwise. |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 638 | private Runnable exceptionSafe(Runnable runnable) { |
| 639 | return () -> { |
| 640 | try { |
| 641 | runnable.run(); |
| 642 | } catch (Exception e) { |
| 643 | log.error("Unhandled Exception", e); |
| 644 | } |
| 645 | }; |
| 646 | } |
| 647 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 648 | private <U> U withDeviceLock(Supplier<U> task, DeviceId deviceId) { |
| 649 | final Lock lock = deviceLocks.get(deviceId); |
| 650 | lock.lock(); |
| 651 | try { |
| 652 | return task.get(); |
| 653 | } finally { |
| 654 | lock.unlock(); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | private Runnable withDeviceLock(Runnable task, DeviceId deviceId) { |
| 659 | // Wrapper of withDeviceLock(Supplier, ...) for void tasks. |
| 660 | return () -> withDeviceLock(() -> { |
| 661 | task.run(); |
| 662 | return null; |
| 663 | }, deviceId); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 664 | } |
| 665 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 666 | private void updatePortStatistics(DeviceId deviceId) { |
Andrea Campanella | ce11193 | 2017-09-18 16:59:56 +0900 | [diff] [blame] | 667 | Device device = deviceService.getDevice(deviceId); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 668 | if (device != null && deviceService.isAvailable(deviceId) && |
Andrea Campanella | ce11193 | 2017-09-18 16:59:56 +0900 | [diff] [blame] | 669 | device.is(PortStatisticsDiscovery.class)) { |
| 670 | Collection<PortStatistics> statistics = device.as(PortStatisticsDiscovery.class) |
| 671 | .discoverPortStatistics(); |
| 672 | //updating statistcs only if not empty |
| 673 | if (!statistics.isEmpty()) { |
| 674 | providerService.updatePortStatistics(deviceId, statistics); |
| 675 | } |
| 676 | } else { |
| 677 | log.debug("Can't update port statistics for device {}", deviceId); |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 681 | private boolean notMyScheme(DeviceId deviceId) { |
| 682 | return !deviceId.uri().getScheme().equals(URI_SCHEME); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 685 | private void triggerConnect(DeviceId deviceId) { |
| 686 | connectionExecutor.execute(withDeviceLock( |
| 687 | () -> doConnectDevice(deviceId), deviceId)); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 688 | } |
| 689 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 690 | private boolean isPipelineProgrammable(DeviceId deviceId) { |
| 691 | final GeneralProviderDeviceConfig providerConfig = cfgService.getConfig( |
| 692 | deviceId, GeneralProviderDeviceConfig.class); |
| 693 | if (providerConfig == null) { |
| 694 | return false; |
| 695 | } |
| 696 | return !Collections.disjoint( |
| 697 | ImmutableSet.copyOf(providerConfig.node().fieldNames()), |
| 698 | PIPELINE_CONFIGURABLE_PROTOCOLS); |
| 699 | } |
| 700 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 701 | /** |
| 702 | * Listener for configuration events. |
| 703 | */ |
| 704 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 705 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 706 | @Override |
| 707 | public void event(NetworkConfigEvent event) { |
Carmelo Cascone | 7044efd | 2018-07-06 13:01:36 +0200 | [diff] [blame] | 708 | connectionExecutor.execute(() -> consumeConfigEvent(event)); |
| 709 | } |
| 710 | |
| 711 | @Override |
| 712 | public boolean isRelevant(NetworkConfigEvent event) { |
| 713 | return (event.configClass().equals(GeneralProviderDeviceConfig.class) || |
| 714 | event.configClass().equals(BasicDeviceConfig.class) || |
| 715 | event.configClass().equals(PiPipeconfConfig.class)) && |
| 716 | (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 717 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED); |
| 718 | } |
| 719 | |
| 720 | private void consumeConfigEvent(NetworkConfigEvent event) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 721 | DeviceId deviceId = (DeviceId) event.subject(); |
| 722 | //Assuming that the deviceId comes with uri 'device:' |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 723 | if (notMyScheme(deviceId)) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 724 | // not under my scheme, skipping |
| 725 | log.debug("{} is not my scheme, skipping", deviceId); |
| 726 | return; |
| 727 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 728 | final boolean configComplete = withDeviceLock( |
| 729 | () -> isDeviceConfigComplete(event, deviceId), deviceId); |
| 730 | if (!configComplete) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 731 | // Still waiting for some configuration. |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 732 | return; |
| 733 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 734 | // Good to go. |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 735 | triggerConnect(deviceId); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 736 | cleanUpConfigInfo(deviceId); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 737 | } |
| 738 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 739 | private boolean isDeviceConfigComplete(NetworkConfigEvent event, DeviceId deviceId) { |
| 740 | // FIXME to be removed when netcfg will issue device events in a bundle or |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 741 | // ensure all configuration needed is present |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 742 | if (event.configClass().equals(GeneralProviderDeviceConfig.class)) { |
| 743 | //FIXME we currently assume that p4runtime devices are pipeline configurable. |
| 744 | //If we want to connect a p4runtime device with no pipeline |
| 745 | if (event.config().isPresent()) { |
| 746 | deviceConfigured.add(deviceId); |
| 747 | final boolean isNotPipelineConfigurable = Collections.disjoint( |
| 748 | ImmutableSet.copyOf(event.config().get().node().fieldNames()), |
| 749 | PIPELINE_CONFIGURABLE_PROTOCOLS); |
| 750 | if (isNotPipelineConfigurable) { |
| 751 | // Skip waiting for a pipeline if we can't support it. |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 752 | pipelineConfigured.add(deviceId); |
| 753 | } |
| 754 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 755 | } else if (event.configClass().equals(BasicDeviceConfig.class)) { |
| 756 | if (event.config().isPresent() && event.config().get().node().has(DRIVER)) { |
| 757 | driverConfigured.add(deviceId); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 758 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 759 | } else if (event.configClass().equals(PiPipeconfConfig.class)) { |
| 760 | if (event.config().isPresent() |
| 761 | && event.config().get().node().has(PiPipeconfConfig.PIPIPECONFID)) { |
| 762 | pipelineConfigured.add(deviceId); |
| 763 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 764 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 765 | |
| 766 | if (deviceConfigured.contains(deviceId) |
| 767 | && driverConfigured.contains(deviceId) |
| 768 | && pipelineConfigured.contains(deviceId)) { |
| 769 | return true; |
| 770 | } else { |
| 771 | if (deviceConfigured.contains(deviceId) && driverConfigured.contains(deviceId)) { |
| 772 | log.debug("Waiting for pipeline configuration for device {}", deviceId); |
| 773 | } else if (pipelineConfigured.contains(deviceId) && driverConfigured.contains(deviceId)) { |
| 774 | log.debug("Waiting for device configuration for device {}", deviceId); |
| 775 | } else if (pipelineConfigured.contains(deviceId) && deviceConfigured.contains(deviceId)) { |
| 776 | log.debug("Waiting for driver configuration for device {}", deviceId); |
| 777 | } else if (driverConfigured.contains(deviceId)) { |
| 778 | log.debug("Only driver configuration for device {}", deviceId); |
| 779 | } else if (deviceConfigured.contains(deviceId)) { |
| 780 | log.debug("Only device configuration for device {}", deviceId); |
| 781 | } |
| 782 | } |
| 783 | return false; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 784 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 787 | private boolean isPipelineReady(DeviceId deviceId) { |
| 788 | final boolean isPipelineProg = isPipelineProgrammable(deviceId); |
| 789 | final boolean isPipeconfReady = pipeconfWatchdogService |
| 790 | .getStatus(deviceId) |
| 791 | .equals(PiPipeconfWatchdogService.PipelineStatus.READY); |
| 792 | return !isPipelineProg || isPipeconfReady; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | private void cleanUpConfigInfo(DeviceId deviceId) { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 796 | deviceConfigured.remove(deviceId); |
| 797 | driverConfigured.remove(deviceId); |
| 798 | pipelineConfigured.remove(deviceId); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 799 | } |
| 800 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 801 | private void startStatsPolling(DeviceId deviceId, boolean withRandomDelay) { |
| 802 | statsPollingTasks.compute(deviceId, (did, oldTask) -> { |
| 803 | if (oldTask != null) { |
| 804 | oldTask.cancel(false); |
| 805 | } |
| 806 | final int delay = withRandomDelay |
| 807 | ? new SecureRandom().nextInt(10) : 0; |
| 808 | return statsExecutor.scheduleAtFixedRate( |
| 809 | exceptionSafe(() -> updatePortStatistics(deviceId)), |
| 810 | delay, statsPollFrequency, TimeUnit.SECONDS); |
| 811 | }); |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 814 | private void cancelStatsPolling(DeviceId deviceId) { |
| 815 | statsPollingTasks.computeIfPresent(deviceId, (did, task) -> { |
| 816 | task.cancel(false); |
| 817 | return null; |
| 818 | }); |
| 819 | } |
| 820 | |
| 821 | private void rescheduleStatsPollingTasks() { |
| 822 | statsPollingTasks.keySet().forEach(deviceId -> { |
| 823 | // startStatsPolling cancels old one if present. |
| 824 | startStatsPolling(deviceId, true); |
| 825 | }); |
| 826 | } |
| 827 | |
| 828 | private void triggerProbeAllDevices() { |
| 829 | // Async trigger a task for all devices in the cfg. |
| 830 | cfgService.getSubjects(DeviceId.class, GeneralProviderDeviceConfig.class) |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 831 | .forEach(this::triggerDeviceProbe); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 832 | } |
| 833 | |
Carmelo Cascone | 9204452 | 2018-06-29 19:00:59 +0200 | [diff] [blame] | 834 | private PiPipeconfId getPipeconfFromCfg(DeviceId deviceId) { |
| 835 | PiPipeconfConfig config = cfgService.getConfig( |
| 836 | deviceId, PiPipeconfConfig.class); |
| 837 | if (config == null) { |
| 838 | return null; |
| 839 | } |
| 840 | return config.piPipeconfId(); |
| 841 | } |
| 842 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 843 | private void triggerDeviceProbe(DeviceId deviceId) { |
| 844 | connectionExecutor.execute(withDeviceLock( |
| 845 | () -> doDeviceProbe(deviceId), deviceId)); |
| 846 | } |
| 847 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 848 | private void doDeviceProbe(DeviceId deviceId) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 849 | log.debug("Probing device {}...", deviceId); |
| 850 | if (configIsMissing(deviceId)) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 851 | return; |
| 852 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 853 | if (!isConnected(deviceId)) { |
| 854 | if (deviceService.isAvailable(deviceId)) { |
| 855 | providerService.deviceDisconnected(deviceId); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 856 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 857 | triggerConnect(deviceId); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 861 | private boolean configIsMissing(DeviceId deviceId) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 862 | final boolean present = |
| 863 | cfgService.getConfig(deviceId, GeneralProviderDeviceConfig.class) != null |
| 864 | && cfgService.getConfig(deviceId, BasicDeviceConfig.class) != null; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 865 | if (!present) { |
| 866 | log.warn("Configuration for device {} is not complete", deviceId); |
| 867 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 868 | return !present; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | private void handleMastershipResponse(DeviceId deviceId, MastershipRole response) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 872 | // Notify core about mastership response. |
| 873 | final MastershipRole request = requestedRoles.get(deviceId); |
| 874 | final boolean isAvailable = deviceService.isAvailable(deviceId); |
| 875 | if (request == null || !isAvailable) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 876 | return; |
| 877 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 878 | log.debug("Device {} asserted role {} (requested was {})", |
| 879 | deviceId, response, request); |
| 880 | providerService.receivedRoleReply(deviceId, request, response); |
| 881 | // FIXME: this should be based on assigned mastership, not what returned by device |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 882 | if (response.equals(MastershipRole.MASTER)) { |
| 883 | startStatsPolling(deviceId, false); |
| 884 | } else { |
| 885 | cancelStatsPolling(deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
Carmelo Cascone | de3b684 | 2018-09-05 17:45:10 -0700 | [diff] [blame] | 889 | private void handleNotMaster(DeviceId deviceId) { |
| 890 | log.warn("Device {} notified that this node is not master, " + |
| 891 | "relinquishing mastership...", deviceId); |
| 892 | mastershipService.relinquishMastership(deviceId); |
| 893 | } |
| 894 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 895 | private <U> U getFutureWithDeadline(CompletableFuture<U> future, String opDescription, |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 896 | DeviceId deviceId, U defaultValue, int timeout) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 897 | try { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 898 | return future.get(timeout, TimeUnit.SECONDS); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 899 | } catch (InterruptedException e) { |
| 900 | log.error("Thread interrupted while {} on {}", opDescription, deviceId); |
| 901 | Thread.currentThread().interrupt(); |
| 902 | } catch (ExecutionException e) { |
| 903 | log.error("Exception while {} on {}", opDescription, deviceId, e.getCause()); |
| 904 | } catch (TimeoutException e) { |
| 905 | log.error("Operation TIMEOUT while {} on {}", opDescription, deviceId); |
| 906 | } |
| 907 | return defaultValue; |
| 908 | } |
| 909 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 910 | /** |
| 911 | * Listener for core device events. |
| 912 | */ |
| 913 | private class InternalDeviceListener implements DeviceListener { |
| 914 | @Override |
| 915 | public void event(DeviceEvent event) { |
Andrea Campanella | ce11193 | 2017-09-18 16:59:56 +0900 | [diff] [blame] | 916 | DeviceId deviceId = event.subject().id(); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 917 | // For now this is scheduled periodically, when streaming API will |
| 918 | // be available we check and base it on the streaming API (e.g. gNMI) |
| 919 | if (mastershipService.isLocalMaster(deviceId)) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 920 | startStatsPolling(deviceId, true); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
| 924 | @Override |
| 925 | public boolean isRelevant(DeviceEvent event) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 926 | return event.type() == Type.DEVICE_ADDED && |
| 927 | event.subject().id().toString().startsWith(URI_SCHEME.toLowerCase()); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 928 | } |
| 929 | } |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 930 | |
| 931 | /** |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 932 | * Listener for device agent events. |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 933 | */ |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 934 | private class InternalDeviceAgentListener implements DeviceAgentListener { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 935 | |
| 936 | @Override |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 937 | public void event(DeviceAgentEvent event) { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 938 | DeviceId deviceId = event.subject(); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 939 | switch (event.type()) { |
| 940 | case CHANNEL_OPEN: |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 941 | triggerAdvertiseDevice(deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 942 | break; |
| 943 | case CHANNEL_CLOSED: |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 944 | case CHANNEL_ERROR: |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 945 | triggerDeviceProbe(deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 946 | break; |
| 947 | case ROLE_MASTER: |
| 948 | handleMastershipResponse(deviceId, MastershipRole.MASTER); |
| 949 | break; |
| 950 | case ROLE_STANDBY: |
| 951 | handleMastershipResponse(deviceId, MastershipRole.STANDBY); |
| 952 | break; |
| 953 | case ROLE_NONE: |
| 954 | handleMastershipResponse(deviceId, MastershipRole.NONE); |
| 955 | break; |
Carmelo Cascone | de3b684 | 2018-09-05 17:45:10 -0700 | [diff] [blame] | 956 | case NOT_MASTER: |
| 957 | handleNotMaster(deviceId); |
| 958 | break; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 959 | default: |
| 960 | log.warn("Unrecognized device agent event {}", event.type()); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 961 | } |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 965 | |
| 966 | private class InternalPipeconfWatchdogListener implements PiPipeconfWatchdogListener { |
| 967 | @Override |
| 968 | public void event(PiPipeconfWatchdogEvent event) { |
| 969 | triggerMarkAvailable(event.subject()); |
| 970 | } |
| 971 | |
| 972 | @Override |
| 973 | public boolean isRelevant(PiPipeconfWatchdogEvent event) { |
| 974 | return event.type().equals(PiPipeconfWatchdogEvent.Type.PIPELINE_READY); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | private class InternalConfigFactory |
| 979 | extends ConfigFactory<DeviceId, GeneralProviderDeviceConfig> { |
| 980 | |
| 981 | InternalConfigFactory() { |
| 982 | super(SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 983 | GeneralProviderDeviceConfig.class, CFG_SCHEME); |
| 984 | } |
| 985 | |
| 986 | @Override |
| 987 | public GeneralProviderDeviceConfig createConfig() { |
| 988 | return new GeneralProviderDeviceConfig(); |
| 989 | } |
| 990 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 991 | } |