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