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 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 20 | import org.onlab.packet.ChassisId; |
| 21 | import org.onlab.util.ItemNotFoundException; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 22 | import org.onlab.util.Tools; |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 23 | import org.onosproject.cfg.ComponentConfigService; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.NodeId; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 26 | import org.onosproject.core.CoreService; |
Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [diff] [blame] | 27 | import org.onosproject.gnmi.api.GnmiController; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 28 | import org.onosproject.mastership.MastershipInfo; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 29 | import org.onosproject.mastership.MastershipService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 30 | import org.onosproject.net.DefaultAnnotations; |
| 31 | import org.onosproject.net.Device; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.MastershipRole; |
| 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; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 37 | import org.onosproject.net.config.NetworkConfigEvent; |
| 38 | import org.onosproject.net.config.NetworkConfigListener; |
| 39 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 40 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 41 | import org.onosproject.net.device.DefaultDeviceDescription; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 42 | import org.onosproject.net.device.DeviceAgentEvent; |
| 43 | import org.onosproject.net.device.DeviceAgentListener; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 44 | import org.onosproject.net.device.DeviceDescription; |
| 45 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 46 | import org.onosproject.net.device.DeviceHandshaker; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 47 | import org.onosproject.net.device.DeviceProvider; |
| 48 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 49 | import org.onosproject.net.device.DeviceProviderService; |
| 50 | import org.onosproject.net.device.DeviceService; |
| 51 | import org.onosproject.net.device.PortDescription; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 52 | import org.onosproject.net.driver.Behaviour; |
| 53 | import org.onosproject.net.driver.DefaultDriverData; |
| 54 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 55 | import org.onosproject.net.driver.Driver; |
| 56 | import org.onosproject.net.driver.DriverData; |
| 57 | import org.onosproject.net.driver.DriverService; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 58 | import org.onosproject.net.pi.model.PiPipeconf; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 59 | import org.onosproject.net.pi.model.PiPipeconfId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 60 | import org.onosproject.net.pi.service.PiPipeconfService; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 61 | import org.onosproject.net.pi.service.PiPipeconfWatchdogEvent; |
| 62 | import org.onosproject.net.pi.service.PiPipeconfWatchdogListener; |
| 63 | import org.onosproject.net.pi.service.PiPipeconfWatchdogService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 64 | import org.onosproject.net.provider.AbstractProvider; |
| 65 | import org.onosproject.net.provider.ProviderId; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 66 | import org.onosproject.provider.general.device.impl.DeviceTaskExecutor.DeviceTaskException; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 67 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 68 | import org.osgi.service.component.annotations.Activate; |
| 69 | import org.osgi.service.component.annotations.Component; |
| 70 | import org.osgi.service.component.annotations.Deactivate; |
| 71 | import org.osgi.service.component.annotations.Modified; |
| 72 | import org.osgi.service.component.annotations.Reference; |
| 73 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 74 | import org.slf4j.Logger; |
| 75 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 76 | import java.util.Collections; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 77 | import java.util.Dictionary; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 78 | import java.util.List; |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 79 | import java.util.Map; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 80 | import java.util.Objects; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 81 | import java.util.concurrent.CompletableFuture; |
| 82 | import java.util.concurrent.ExecutionException; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 83 | import java.util.concurrent.ExecutorService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 84 | import java.util.concurrent.ScheduledExecutorService; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 85 | import java.util.concurrent.ScheduledFuture; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 86 | import java.util.concurrent.TimeUnit; |
| 87 | import java.util.concurrent.TimeoutException; |
| 88 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 89 | import static com.google.common.base.Preconditions.checkNotNull; |
| 90 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 91 | import static java.lang.System.currentTimeMillis; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 92 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 93 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 94 | import static org.onlab.util.Tools.groupedThreads; |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 95 | import static org.onosproject.provider.general.device.impl.OsgiPropertyConstants.OP_TIMEOUT_SHORT; |
| 96 | import static org.onosproject.provider.general.device.impl.OsgiPropertyConstants.OP_TIMEOUT_SHORT_DEFAULT; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 97 | import static org.onosproject.provider.general.device.impl.OsgiPropertyConstants.PROBE_INTERVAL; |
| 98 | import static org.onosproject.provider.general.device.impl.OsgiPropertyConstants.PROBE_INTERVAL_DEFAULT; |
| 99 | import static org.onosproject.provider.general.device.impl.OsgiPropertyConstants.STATS_POLL_INTERVAL; |
| 100 | import static org.onosproject.provider.general.device.impl.OsgiPropertyConstants.STATS_POLL_INTERVAL_DEFAULT; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 101 | import static org.slf4j.LoggerFactory.getLogger; |
| 102 | |
| 103 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 104 | * Provider which uses drivers to discover devices, perform initial handshake, |
| 105 | * and notify the core of disconnection events. The implementation listens for |
| 106 | * events from netcfg or the drivers (via {@link DeviceAgentListener}) andP |
| 107 | * schedules task for each event. |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 108 | */ |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 109 | @Component(immediate = true, |
| 110 | property = { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 111 | PROBE_INTERVAL + ":Integer=" + PROBE_INTERVAL_DEFAULT, |
| 112 | STATS_POLL_INTERVAL + ":Integer=" + STATS_POLL_INTERVAL_DEFAULT, |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 113 | OP_TIMEOUT_SHORT + ":Integer=" + OP_TIMEOUT_SHORT_DEFAULT, |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 114 | }) |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 115 | public class GeneralDeviceProvider extends AbstractProvider |
| 116 | implements DeviceProvider { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 117 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 118 | private final Logger log = getLogger(getClass()); |
| 119 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 120 | private static final String APP_NAME = "org.onosproject.generaldeviceprovider"; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 121 | private static final String URI_SCHEME = "device"; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 122 | private static final String DEVICE_PROVIDER_PACKAGE = |
| 123 | "org.onosproject.general.provider.device"; |
| 124 | private static final int CORE_POOL_SIZE = 10; |
| 125 | private static final String UNKNOWN = "unknown"; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 126 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 128 | private DeviceProviderRegistry providerRegistry; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 129 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 131 | private ComponentConfigService componentConfigService; |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 132 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 134 | private NetworkConfigRegistry cfgService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 135 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 137 | private CoreService coreService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 138 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 139 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 140 | private DeviceService deviceService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 141 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 142 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 143 | private DriverService driverService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 144 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 145 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 146 | private MastershipService mastershipService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 147 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 148 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 149 | private ClusterService clusterService; |
| 150 | |
| 151 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 152 | private PiPipeconfService pipeconfService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 153 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 154 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 155 | private PiPipeconfWatchdogService pipeconfWatchdogService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 156 | |
Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [diff] [blame] | 157 | // FIXME: no longer general if we add a dependency to a protocol-specific |
| 158 | // service. Possible solutions are: rename this provider to |
| 159 | // StratumDeviceProvider, find a way to allow this provider to register for |
| 160 | // protocol specific events (e.g. port events) via drivers (similar to |
| 161 | // DeviceAgentListener). |
| 162 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 163 | private GnmiController gnmiController; |
| 164 | |
| 165 | private GnmiDeviceStateSubscriber gnmiDeviceStateSubscriber; |
| 166 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 167 | /** Configure interval for checking device availability; default is 10 sec. */ |
| 168 | private int probeInterval = PROBE_INTERVAL_DEFAULT; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 169 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 170 | /** Configure poll frequency for port status and stats; default is 10 sec. */ |
| 171 | private int statsPollInterval = STATS_POLL_INTERVAL_DEFAULT; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 172 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 173 | /** Configure timeout in seconds for device operations; default is 10 sec. */ |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 174 | private int opTimeoutShort = OP_TIMEOUT_SHORT_DEFAULT; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 175 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 176 | private final Map<DeviceId, DeviceHandshaker> handshakersWithListeners = Maps.newConcurrentMap(); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 177 | private final Map<DeviceId, Long> lastProbedAvailability = Maps.newConcurrentMap(); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 178 | private final InternalPipeconfWatchdogListener pipeconfWatchdogListener = new InternalPipeconfWatchdogListener(); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 179 | private final NetworkConfigListener cfgListener = new InternalNetworkConfigListener(); |
| 180 | private final DeviceAgentListener deviceAgentListener = new InternalDeviceAgentListener(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 181 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 182 | private ExecutorService mainExecutor; |
| 183 | private DeviceTaskExecutor<TaskType> taskExecutor; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 184 | private ScheduledExecutorService probeExecutor; |
| 185 | private ScheduledFuture<?> probeTask; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 186 | private StatsPoller statsPoller; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 187 | private DeviceProviderService providerService; |
| 188 | |
| 189 | public GeneralDeviceProvider() { |
| 190 | super(new ProviderId(URI_SCHEME, DEVICE_PROVIDER_PACKAGE)); |
| 191 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 192 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 193 | protected DeviceProviderService providerService() { |
| 194 | return providerService; |
| 195 | } |
| 196 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 197 | @Activate |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 198 | public void activate(ComponentContext context) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 199 | mainExecutor = newFixedThreadPool(CORE_POOL_SIZE, groupedThreads( |
| 200 | "onos/gdp-task", "%d", log)); |
| 201 | taskExecutor = new DeviceTaskExecutor<>(mainExecutor); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 202 | probeExecutor = newSingleThreadScheduledExecutor(groupedThreads( |
| 203 | "onos/gdp-probe", "%d", log)); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 204 | providerService = providerRegistry.register(this); |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 205 | componentConfigService.registerProperties(getClass()); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 206 | coreService.registerApplication(APP_NAME); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 207 | cfgService.addListener(cfgListener); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 208 | pipeconfWatchdogService.addListener(pipeconfWatchdogListener); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 209 | gnmiDeviceStateSubscriber = new GnmiDeviceStateSubscriber( |
| 210 | gnmiController, deviceService, mastershipService, providerService); |
Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [diff] [blame] | 211 | gnmiDeviceStateSubscriber.activate(); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 212 | startOrRescheduleProbeTask(); |
| 213 | statsPoller = new StatsPoller(deviceService, mastershipService, providerService); |
| 214 | statsPoller.activate(statsPollInterval); |
| 215 | modified(context); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 216 | log.info("Started"); |
| 217 | } |
| 218 | |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 219 | @Modified |
| 220 | public void modified(ComponentContext context) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 221 | if (context == null) { |
| 222 | return; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 225 | Dictionary<?, ?> properties = context.getProperties(); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 226 | final int oldProbeFrequency = probeInterval; |
| 227 | probeInterval = Tools.getIntegerProperty( |
| 228 | properties, PROBE_INTERVAL, PROBE_INTERVAL_DEFAULT); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 229 | log.info("Configured. {} is configured to {} seconds", |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 230 | PROBE_INTERVAL, probeInterval); |
| 231 | final int oldStatsPollFrequency = statsPollInterval; |
| 232 | statsPollInterval = Tools.getIntegerProperty( |
| 233 | properties, STATS_POLL_INTERVAL, STATS_POLL_INTERVAL_DEFAULT); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 234 | log.info("Configured. {} is configured to {} seconds", |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 235 | STATS_POLL_INTERVAL, statsPollInterval); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 236 | opTimeoutShort = Tools.getIntegerProperty( |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 237 | properties, OP_TIMEOUT_SHORT, OP_TIMEOUT_SHORT_DEFAULT); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 238 | log.info("Configured. {} is configured to {} seconds", |
| 239 | OP_TIMEOUT_SHORT, opTimeoutShort); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 240 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 241 | if (oldProbeFrequency != probeInterval) { |
| 242 | startOrRescheduleProbeTask(); |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 243 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 244 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 245 | if (oldStatsPollFrequency != statsPollInterval) { |
| 246 | statsPoller.reschedule(statsPollInterval); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 247 | } |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 250 | @Deactivate |
| 251 | public void deactivate() { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 252 | // Shutdown stats poller. |
| 253 | statsPoller.deactivate(); |
| 254 | statsPoller = null; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 255 | // Shutdown probe executor. |
| 256 | probeTask.cancel(true); |
| 257 | probeTask = null; |
| 258 | probeExecutor.shutdownNow(); |
| 259 | try { |
| 260 | probeExecutor.awaitTermination(5, TimeUnit.SECONDS); |
| 261 | } catch (InterruptedException e) { |
| 262 | log.warn("probeExecutor not terminated properly"); |
| 263 | } |
| 264 | probeExecutor = null; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 265 | // Shutdown main and task executor. |
| 266 | taskExecutor.cancel(); |
| 267 | taskExecutor = null; |
| 268 | mainExecutor.shutdownNow(); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 269 | try { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 270 | mainExecutor.awaitTermination(5, TimeUnit.SECONDS); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 271 | } catch (InterruptedException e) { |
| 272 | log.warn("connectionExecutor not terminated properly"); |
| 273 | } |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 274 | mainExecutor = null; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 275 | // Remove all device agent listeners |
| 276 | handshakersWithListeners.values().forEach(h -> h.removeDeviceAgentListener(id())); |
| 277 | handshakersWithListeners.clear(); |
| 278 | // Other cleanup. |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 279 | lastProbedAvailability.clear(); |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 280 | componentConfigService.unregisterProperties(getClass(), false); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 281 | cfgService.removeListener(cfgListener); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 282 | pipeconfWatchdogService.removeListener(pipeconfWatchdogListener); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 283 | providerRegistry.unregister(this); |
| 284 | providerService = null; |
Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [diff] [blame] | 285 | gnmiDeviceStateSubscriber.deactivate(); |
| 286 | gnmiDeviceStateSubscriber = null; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 287 | log.info("Stopped"); |
| 288 | } |
| 289 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 290 | @Override |
| 291 | public void triggerProbe(DeviceId deviceId) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 292 | checkNotNull(deviceId); |
| 293 | submitTask(deviceId, TaskType.PROBE_AVAILABILITY); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 298 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 299 | final MastershipInfo mastershipInfo = mastershipService.getMastershipFor(deviceId); |
| 300 | final NodeId localNodeId = clusterService.getLocalNode().id(); |
| 301 | |
| 302 | if (!mastershipInfo.getRole(localNodeId).equals(newRole)) { |
| 303 | log.warn("Inconsistent mastership info for {}! Requested {}, but " + |
| 304 | "mastership service reports {}, will apply the latter...", |
| 305 | deviceId, newRole, mastershipInfo.getRole(localNodeId)); |
| 306 | newRole = mastershipInfo.getRole(localNodeId); |
| 307 | } |
| 308 | |
| 309 | // Derive preference value. |
| 310 | final int preference; |
| 311 | switch (newRole) { |
| 312 | case MASTER: |
| 313 | preference = 0; |
| 314 | break; |
| 315 | case STANDBY: |
| 316 | preference = mastershipInfo.backups().indexOf(localNodeId) + 1; |
| 317 | if (preference == 0) { |
| 318 | // Not found in list. |
| 319 | log.error("Unable to derive mastership preference for {}, " + |
| 320 | "requested role {} but local node ID was " + |
| 321 | "not found among list of backup nodes " + |
| 322 | "reported by mastership service"); |
| 323 | return; |
| 324 | } |
| 325 | break; |
| 326 | case NONE: |
| 327 | // No preference for NONE, apply as is. |
| 328 | log.info("Notifying role {} to {}", newRole, deviceId); |
| 329 | roleChanged(deviceId, newRole); |
| 330 | return; |
| 331 | default: |
| 332 | log.error("Unrecognized mastership role {}", newRole); |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | log.info("Notifying role {} (preference {}) for term {} to {}", |
| 337 | newRole, preference, mastershipInfo.term(), deviceId); |
| 338 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 339 | final DeviceHandshaker handshaker = getBehaviour( |
| 340 | deviceId, DeviceHandshaker.class); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 341 | if (handshaker == null) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 342 | log.error("Null handshaker. Unable to notify role {} to {}", |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 343 | newRole, deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 344 | return; |
| 345 | } |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 346 | |
| 347 | try { |
| 348 | handshaker.roleChanged(preference, mastershipInfo.term()); |
| 349 | } catch (UnsupportedOperationException e) { |
| 350 | // Preference-based method not supported. |
| 351 | handshaker.roleChanged(newRole); |
| 352 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public boolean isReachable(DeviceId deviceId) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 357 | final DeviceHandshaker handshaker = getBehaviour( |
| 358 | deviceId, DeviceHandshaker.class); |
Andrea Campanella | c1ecdd0 | 2018-01-12 12:48:24 +0100 | [diff] [blame] | 359 | if (handshaker == null) { |
| 360 | return false; |
| 361 | } |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 362 | return handshaker.isReachable(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 365 | @Override |
| 366 | public boolean isAvailable(DeviceId deviceId) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 367 | final DeviceHandshaker handshaker = getBehaviour( |
| 368 | deviceId, DeviceHandshaker.class); |
| 369 | if (handshaker == null) { |
| 370 | return false; |
| 371 | } |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 372 | try { |
| 373 | // Try without probing the device... |
| 374 | return handshaker.isAvailable(); |
| 375 | } catch (UnsupportedOperationException e) { |
| 376 | // Driver does not support that. |
| 377 | return probeAvailability(handshaker); |
| 378 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 381 | @Override |
| 382 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 383 | boolean enable) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 384 | if (!deviceService.getDevice(deviceId).is(PortAdmin.class)) { |
| 385 | log.warn("Missing PortAdmin behaviour on {}, aborting port state change", |
| 386 | deviceId); |
| 387 | return; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 388 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 389 | final PortAdmin portAdmin = deviceService.getDevice(deviceId) |
| 390 | .as(PortAdmin.class); |
| 391 | final CompletableFuture<Boolean> modifyTask = enable |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 392 | ? portAdmin.enable(portNumber) |
| 393 | : portAdmin.disable(portNumber); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 394 | final String descr = (enable ? "enabling" : "disabling") + " port " + portNumber; |
| 395 | getFutureWithDeadline( |
| 396 | modifyTask, descr, deviceId, null, opTimeoutShort); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 399 | @Override |
| 400 | public void triggerDisconnect(DeviceId deviceId) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 401 | checkNotNull(deviceId); |
| 402 | log.info("Triggering disconnection of device {}", deviceId); |
| 403 | submitTask(deviceId, TaskType.CONNECTION_TEARDOWN); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Listener for configuration events. |
| 408 | */ |
| 409 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 410 | @Override |
| 411 | public void event(NetworkConfigEvent event) { |
| 412 | DeviceId deviceId = (DeviceId) event.subject(); |
| 413 | switch (event.type()) { |
| 414 | case CONFIG_ADDED: |
| 415 | if (configIsComplete(deviceId)) { |
| 416 | submitTask(deviceId, TaskType.CONNECTION_SETUP); |
| 417 | } |
| 418 | break; |
| 419 | case CONFIG_UPDATED: |
| 420 | if (configIsComplete(deviceId) && mgmtAddrUpdated(event)) { |
| 421 | submitTask(deviceId, TaskType.CONNECTION_UPDATE); |
| 422 | } |
| 423 | break; |
| 424 | case CONFIG_REMOVED: |
| 425 | if (event.configClass().equals(BasicDeviceConfig.class)) { |
| 426 | submitTask(deviceId, TaskType.CONNECTION_TEARDOWN); |
| 427 | } |
| 428 | break; |
| 429 | default: |
| 430 | // Ignore |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | private boolean mgmtAddrUpdated(NetworkConfigEvent event) { |
| 436 | if (!event.prevConfig().isPresent() || !event.config().isPresent()) { |
| 437 | return false; |
| 438 | } |
| 439 | final BasicDeviceConfig prev = (BasicDeviceConfig) event.prevConfig().get(); |
| 440 | final BasicDeviceConfig current = (BasicDeviceConfig) event.config().get(); |
| 441 | return !Objects.equals(prev.managementAddress(), current.managementAddress()); |
| 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public boolean isRelevant(NetworkConfigEvent event) { |
| 446 | return event.configClass().equals(BasicDeviceConfig.class) && |
| 447 | (event.subject() instanceof DeviceId) && |
| 448 | myScheme((DeviceId) event.subject()); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Listener for device agent events. |
| 454 | */ |
| 455 | private class InternalDeviceAgentListener implements DeviceAgentListener { |
| 456 | @Override |
| 457 | public void event(DeviceAgentEvent event) { |
| 458 | DeviceId deviceId = event.subject(); |
| 459 | switch (event.type()) { |
| 460 | case CHANNEL_OPEN: |
| 461 | submitTask(deviceId, TaskType.CHANNEL_OPEN); |
| 462 | break; |
| 463 | case CHANNEL_CLOSED: |
| 464 | case CHANNEL_ERROR: |
| 465 | submitTask(deviceId, TaskType.CHANNEL_CLOSED); |
| 466 | break; |
| 467 | case ROLE_MASTER: |
| 468 | submitTask(deviceId, TaskType.ROLE_MASTER); |
| 469 | break; |
| 470 | case ROLE_STANDBY: |
| 471 | submitTask(deviceId, TaskType.ROLE_STANDBY); |
| 472 | break; |
| 473 | case ROLE_NONE: |
| 474 | submitTask(deviceId, TaskType.ROLE_NONE); |
| 475 | break; |
| 476 | case NOT_MASTER: |
| 477 | submitTask(deviceId, TaskType.NOT_MASTER); |
| 478 | break; |
| 479 | default: |
| 480 | log.warn("Unrecognized device agent event {}", event.type()); |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Pipeline event listener. |
| 487 | */ |
| 488 | private class InternalPipeconfWatchdogListener implements PiPipeconfWatchdogListener { |
| 489 | @Override |
| 490 | public void event(PiPipeconfWatchdogEvent event) { |
| 491 | final DeviceId deviceId = event.subject(); |
| 492 | switch (event.type()) { |
| 493 | case PIPELINE_READY: |
| 494 | submitTask(deviceId, TaskType.PIPELINE_READY); |
| 495 | break; |
| 496 | case PIPELINE_UNKNOWN: |
| 497 | submitTask(deviceId, TaskType.PIPELINE_NOT_READY); |
| 498 | break; |
| 499 | default: |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public boolean isRelevant(PiPipeconfWatchdogEvent event) { |
| 506 | return myScheme(event.subject()); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | private void startOrRescheduleProbeTask() { |
| 511 | synchronized (this) { |
| 512 | if (probeTask != null) { |
| 513 | probeTask.cancel(false); |
| 514 | } |
| 515 | probeTask = probeExecutor.scheduleAtFixedRate( |
| 516 | this::submitProbeTasks, |
| 517 | 0, |
| 518 | probeInterval, |
| 519 | TimeUnit.SECONDS); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | private void submitProbeTasks() { |
| 524 | // Async trigger a task for all devices in the cfg. |
| 525 | log.debug("Starting probing for all devices"); |
| 526 | cfgService.getSubjects(DeviceId.class).stream() |
| 527 | .filter(GeneralDeviceProvider::myScheme) |
| 528 | .forEach(this::submitProbeTask); |
| 529 | } |
| 530 | |
| 531 | private void submitProbeTask(DeviceId deviceId) { |
| 532 | final DeviceHandshaker handshaker = handshakersWithListeners.get(deviceId); |
| 533 | |
| 534 | if (handshaker == null) { |
| 535 | if (configIsComplete(deviceId)) { |
| 536 | // Device in config but we have not initiated a connection. |
| 537 | // Perhaps we missed the config event? |
| 538 | submitTask(deviceId, TaskType.CONNECTION_SETUP); |
| 539 | } |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | if (!handshaker.isConnected()) { |
| 544 | // Device is in the core, but driver reports there is NOT a |
| 545 | // connection to it. Perhaps the netcfg changed and we didn't |
| 546 | // pick the event? |
| 547 | log.warn("Re-establishing lost connection to {}", deviceId); |
| 548 | submitTask(deviceId, TaskType.CONNECTION_TEARDOWN); |
| 549 | submitTask(deviceId, TaskType.CONNECTION_SETUP); |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | // On probing offline devices, while we expect them to signal |
| 554 | // availability via CHANNEL_OPEN or similar events, periodic probing |
| 555 | // might be needed to stimulate some channel activity. We might consider |
| 556 | // requiring active probing of closed channels in the protocol layer. |
| 557 | |
| 558 | final Long lastProbe = lastProbedAvailability.get(deviceId); |
| 559 | if (lastProbe != null && |
| 560 | (currentTimeMillis() - lastProbe) < (probeInterval * 1000 / 3)) { |
| 561 | // This avoids overload of probe tasks which might involve sending |
| 562 | // messages over the network. We require a minimum interval of 1/3 |
| 563 | // of the configured probeInterval between consecutive probe tasks. |
| 564 | if (log.isDebugEnabled()) { |
| 565 | log.debug("Dropping probe task for {} as it happened recently", |
| 566 | deviceId); |
| 567 | } |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | submitTask(deviceId, TaskType.PROBE_AVAILABILITY); |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Type of tasks performed by this provider. |
| 576 | */ |
| 577 | enum TaskType { |
| 578 | CONNECTION_SETUP, |
| 579 | CONNECTION_UPDATE, |
| 580 | CONNECTION_TEARDOWN, |
| 581 | PIPELINE_READY, |
| 582 | CHANNEL_OPEN, |
| 583 | CHANNEL_CLOSED, |
| 584 | PIPELINE_NOT_READY, |
| 585 | PROBE_AVAILABILITY, |
| 586 | ROLE_MASTER, |
| 587 | ROLE_NONE, |
| 588 | ROLE_STANDBY, |
| 589 | NOT_MASTER, |
| 590 | } |
| 591 | |
| 592 | private void submitTask(DeviceId deviceId, TaskType taskType) { |
| 593 | taskExecutor.submit(deviceId, taskType, taskRunnable(deviceId, taskType)); |
| 594 | } |
| 595 | |
| 596 | private Runnable taskRunnable(DeviceId deviceId, TaskType taskType) { |
| 597 | switch (taskType) { |
| 598 | case CONNECTION_SETUP: |
| 599 | return () -> handleConnectionSetup(deviceId); |
| 600 | case CONNECTION_UPDATE: |
| 601 | return () -> handleConnectionUpdate(deviceId); |
| 602 | case CONNECTION_TEARDOWN: |
| 603 | return () -> handleConnectionTeardown(deviceId); |
| 604 | case CHANNEL_OPEN: |
| 605 | return () -> handleProbeAvailability(deviceId); |
| 606 | case CHANNEL_CLOSED: |
| 607 | return () -> markOfflineIfNeeded(deviceId); |
| 608 | case PIPELINE_NOT_READY: |
| 609 | return () -> markOfflineIfNeeded(deviceId); |
| 610 | case PIPELINE_READY: |
| 611 | return () -> handleProbeAvailability(deviceId); |
| 612 | case PROBE_AVAILABILITY: |
| 613 | return () -> handleProbeAvailability(deviceId); |
| 614 | case ROLE_MASTER: |
| 615 | return () -> handleMastershipResponse(deviceId, MastershipRole.MASTER); |
| 616 | case ROLE_STANDBY: |
| 617 | return () -> handleMastershipResponse(deviceId, MastershipRole.STANDBY); |
| 618 | case ROLE_NONE: |
| 619 | return () -> handleMastershipResponse(deviceId, MastershipRole.NONE); |
| 620 | case NOT_MASTER: |
| 621 | return () -> handleNotMaster(deviceId); |
| 622 | default: |
| 623 | throw new IllegalArgumentException("Unrecognized task type " + taskType); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | private void handleConnectionSetup(DeviceId deviceId) { |
| 628 | assertConfig(deviceId); |
| 629 | // Bind pipeconf (if any and if device is capable). |
| 630 | bindPipeconfIfRequired(deviceId); |
| 631 | // Get handshaker. |
| 632 | final DeviceHandshaker handshaker = handshakerOrFail(deviceId); |
| 633 | if (handshaker.isConnected() || handshakersWithListeners.containsKey(deviceId)) { |
| 634 | throw new DeviceTaskException("connection already exists"); |
| 635 | } |
| 636 | // Add device agent listener. |
| 637 | handshaker.addDeviceAgentListener(id(), deviceAgentListener); |
| 638 | handshakersWithListeners.put(deviceId, handshaker); |
| 639 | // Start connection via handshaker. |
| 640 | final Boolean connectSuccess = getFutureWithDeadline( |
| 641 | handshaker.connect(), "initiating connection", |
| 642 | deviceId, false, opTimeoutShort); |
| 643 | if (!connectSuccess) { |
| 644 | // Failed! Remove listeners. |
| 645 | handshaker.removeDeviceAgentListener(id()); |
| 646 | handshakersWithListeners.remove(deviceId); |
| 647 | throw new DeviceTaskException("connection failed"); |
| 648 | } |
| 649 | createOrUpdateDevice(deviceId, false); |
| 650 | final List<PortDescription> ports = getPortDetails(deviceId); |
| 651 | providerService.updatePorts(deviceId, ports); |
| 652 | // From here we expect a CHANNEL_OPEN event to update availability. |
| 653 | } |
| 654 | |
| 655 | private void handleConnectionUpdate(DeviceId deviceId) { |
| 656 | assertConfig(deviceId); |
| 657 | final DeviceHandshaker handshaker = handshakerOrFail(deviceId); |
| 658 | if (!handshaker.isConnected()) { |
| 659 | // If driver reports that a connection still exists, perhaps the |
| 660 | // part of the netcfg that changed does not affect the connection. |
| 661 | // Otherwise, remove any previous connection state from the old |
| 662 | // netcfg and create a new one. |
| 663 | log.warn("Detected change of connection endpoints for {}, will " + |
| 664 | "tear down existing connection and set up a new one...", |
| 665 | deviceId); |
| 666 | handleConnectionTeardown(deviceId); |
| 667 | handleConnectionSetup(deviceId); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | private void createOrUpdateDevice(DeviceId deviceId, boolean available) { |
| 672 | if (deviceService.getDevice(deviceId) != null |
| 673 | && deviceService.isAvailable(deviceId) == available) { |
| 674 | // Other nodes might have advertised this device before us. |
| 675 | return; |
| 676 | } |
| 677 | assertConfig(deviceId); |
| 678 | providerService.deviceConnected(deviceId, getDeviceDescription( |
| 679 | deviceId, available)); |
| 680 | } |
| 681 | |
| 682 | private boolean probeAvailability(DeviceHandshaker handshaker) { |
| 683 | lastProbedAvailability.put(handshaker.data().deviceId(), currentTimeMillis()); |
| 684 | return getFutureWithDeadline( |
| 685 | handshaker.probeAvailability(), "probing availability", |
| 686 | handshaker.data().deviceId(), false, opTimeoutShort); |
| 687 | } |
| 688 | |
| 689 | private boolean probeReachability(DeviceHandshaker handshaker) { |
| 690 | lastProbedAvailability.put(handshaker.data().deviceId(), currentTimeMillis()); |
| 691 | return getFutureWithDeadline( |
| 692 | handshaker.probeReachability(), "probing reachability", |
| 693 | handshaker.data().deviceId(), false, opTimeoutShort); |
| 694 | } |
| 695 | |
| 696 | private void markOfflineIfNeeded(DeviceId deviceId) { |
| 697 | assertDeviceRegistered(deviceId); |
| 698 | if (deviceService.isAvailable(deviceId)) { |
| 699 | providerService.deviceDisconnected(deviceId); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | private void handleProbeAvailability(DeviceId deviceId) { |
| 704 | assertDeviceRegistered(deviceId); |
| 705 | |
| 706 | // Make device has a valid mastership role. |
| 707 | final DeviceHandshaker handshaker = handshakerOrFail(deviceId); |
| 708 | final MastershipRole deviceRole = handshaker.getRole(); |
| 709 | final MastershipRole expectedRole = mastershipService.getLocalRole(deviceId); |
| 710 | if (expectedRole == MastershipRole.NONE || expectedRole != deviceRole) { |
| 711 | // Device does NOT have a valid role... |
| 712 | if (!handshaker.isReachable() && !probeReachability(handshaker)) { |
| 713 | // ...but is not reachable. There isn't much we can do. |
| 714 | markOfflineIfNeeded(deviceId); |
| 715 | return; |
| 716 | } |
| 717 | // ...and is reachable, re-assert role. |
| 718 | roleChanged(deviceId, expectedRole == MastershipRole.NONE |
| 719 | ? mastershipService.requestRoleForSync(deviceId) |
| 720 | : expectedRole); |
| 721 | try { |
| 722 | // Wait for role to be notified and reachability state to be |
| 723 | // updated. This should be roughly equivalent to one RTT. |
| 724 | Thread.sleep(500); |
| 725 | } catch (InterruptedException e) { |
| 726 | Thread.currentThread().interrupt(); |
| 727 | return; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | // Check and update availability. |
| 732 | if (probeAvailability(handshakerOrFail(deviceId))) { |
| 733 | // Device ready to do its job. |
| 734 | createOrUpdateDevice(deviceId, true); |
| 735 | } else { |
| 736 | markOfflineIfNeeded(deviceId); |
| 737 | if (handshaker.isReachable() && isPipelineProgrammable(deviceId)) { |
| 738 | // If reachable, but not available, and pipeline programmable, there |
| 739 | // is a high chance it's because the pipeline is not READY |
| 740 | // (independently from what the pipeconf watchdog reports, as the |
| 741 | // status there might be outdated). Encourage pipeconf watchdog to |
| 742 | // perform a pipeline probe ASAP. |
| 743 | pipeconfWatchdogService.triggerProbe(deviceId); |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | private void handleMastershipResponse(DeviceId deviceId, MastershipRole response) { |
| 749 | assertDeviceRegistered(deviceId); |
| 750 | log.debug("Device {} asserted role {}", deviceId, response); |
| 751 | providerService.receivedRoleReply(deviceId, response); |
| 752 | } |
| 753 | |
| 754 | private void handleNotMaster(DeviceId deviceId) { |
| 755 | assertDeviceRegistered(deviceId); |
| 756 | if (mastershipService.isLocalMaster(deviceId)) { |
| 757 | log.warn("Device {} notified that this node is not master, " + |
| 758 | "relinquishing mastership...", deviceId); |
| 759 | mastershipService.relinquishMastership(deviceId); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | private void assertDeviceRegistered(DeviceId deviceId) { |
| 764 | if (deviceService.getDevice(deviceId) == null) { |
| 765 | throw new DeviceTaskException("device not registered in the core"); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | private void handleConnectionTeardown(DeviceId deviceId) { |
| 770 | if (deviceService.getDevice(deviceId) != null |
| 771 | && deviceService.isAvailable(deviceId)) { |
| 772 | providerService.deviceDisconnected(deviceId); |
| 773 | } |
| 774 | final DeviceHandshaker handshaker = handshakerOrFail(deviceId); |
| 775 | handshaker.removeDeviceAgentListener(id()); |
| 776 | handshakersWithListeners.remove(deviceId); |
| 777 | handshaker.disconnect(); |
| 778 | lastProbedAvailability.remove(deviceId); |
| 779 | } |
| 780 | |
| 781 | private void bindPipeconfIfRequired(DeviceId deviceId) { |
| 782 | if (pipeconfService.getPipeconf(deviceId).isPresent() |
| 783 | || !isPipelineProgrammable(deviceId)) { |
| 784 | // Nothing to do. |
| 785 | // Device has already a pipeconf or is not programmable. |
| 786 | return; |
| 787 | } |
| 788 | // Get pipeconf from netcfg or driver (default one). |
| 789 | final PiPipelineProgrammable pipelineProg = getBehaviour( |
| 790 | deviceId, PiPipelineProgrammable.class); |
| 791 | final PiPipeconfId pipeconfId = getPipeconfId(deviceId, pipelineProg); |
| 792 | if (pipeconfId == null) { |
| 793 | throw new DeviceTaskException("unable to find pipeconf"); |
| 794 | } |
| 795 | // Store binding in pipeconf service. |
| 796 | pipeconfService.bindToDevice(pipeconfId, deviceId); |
| 797 | } |
| 798 | |
| 799 | private PiPipeconfId getPipeconfId(DeviceId deviceId, PiPipelineProgrammable pipelineProg) { |
| 800 | // Places to look for a pipeconf ID (in priority order)): |
| 801 | // 1) netcfg |
| 802 | // 2) device driver (default one) |
| 803 | final PiPipeconfId pipeconfId = getPipeconfFromCfg(deviceId); |
| 804 | if (pipeconfId != null && !pipeconfId.id().isEmpty()) { |
| 805 | return pipeconfId; |
| 806 | } |
| 807 | if (pipelineProg != null |
| 808 | && pipelineProg.getDefaultPipeconf().isPresent()) { |
| 809 | final PiPipeconf defaultPipeconf = pipelineProg.getDefaultPipeconf().get(); |
| 810 | log.info("Using default pipeconf {} for {}", defaultPipeconf.id(), deviceId); |
| 811 | return defaultPipeconf.id(); |
| 812 | } |
| 813 | return null; |
| 814 | } |
| 815 | |
| 816 | private PiPipeconfId getPipeconfFromCfg(DeviceId deviceId) { |
| 817 | BasicDeviceConfig config = cfgService.getConfig( |
| 818 | deviceId, BasicDeviceConfig.class); |
| 819 | if (config == null) { |
| 820 | return null; |
| 821 | } |
| 822 | return config.pipeconf() != null |
| 823 | ? new PiPipeconfId(config.pipeconf()) : null; |
| 824 | } |
| 825 | |
| 826 | private DeviceHandshaker handshakerOrFail(DeviceId deviceId) { |
| 827 | final DeviceHandshaker handshaker = getBehaviour( |
| 828 | deviceId, DeviceHandshaker.class); |
| 829 | if (handshaker == null) { |
| 830 | throw new DeviceTaskException("missing handshaker behavior"); |
| 831 | } |
| 832 | return handshaker; |
| 833 | } |
| 834 | |
| 835 | private boolean configIsComplete(DeviceId deviceId) { |
| 836 | final BasicDeviceConfig basicDeviceCfg = cfgService.getConfig( |
| 837 | deviceId, BasicDeviceConfig.class); |
| 838 | return basicDeviceCfg != null && !isNullOrEmpty(basicDeviceCfg.driver()); |
| 839 | } |
| 840 | |
| 841 | private void assertConfig(DeviceId deviceId) { |
| 842 | if (!configIsComplete(deviceId)) { |
| 843 | throw new DeviceTaskException("configuration is not complete"); |
| 844 | } |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 847 | private Driver getDriver(DeviceId deviceId) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 848 | try { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 849 | // DriverManager checks first using basic device config. |
| 850 | return driverService.getDriver(deviceId); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 851 | } catch (ItemNotFoundException e) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 852 | log.error("Driver not found for {}", deviceId); |
| 853 | return null; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 854 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 857 | private <T extends Behaviour> T getBehaviour(DeviceId deviceId, Class<T> type) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 858 | Driver driver = getDriver(deviceId); |
| 859 | if (driver == null) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 860 | return null; |
| 861 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 862 | if (!driver.hasBehaviour(type)) { |
| 863 | return null; |
| 864 | } |
| 865 | final DriverData data = new DefaultDriverData(driver, deviceId); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 866 | final DefaultDriverHandler handler = new DefaultDriverHandler(data); |
| 867 | return driver.createBehaviour(handler, type); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 870 | private boolean hasBehaviour(DeviceId deviceId, Class<? extends Behaviour> type) { |
| 871 | Driver driver = getDriver(deviceId); |
| 872 | if (driver == null) { |
| 873 | return false; |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 874 | } |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 875 | return driver.hasBehaviour(type); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | private DeviceDescription getDeviceDescription( |
| 879 | DeviceId deviceId, boolean defaultAvailable) { |
| 880 | // Get one from driver or forge. |
| 881 | final DeviceDescriptionDiscovery deviceDiscovery = getBehaviour( |
| 882 | deviceId, DeviceDescriptionDiscovery.class); |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 883 | if (deviceDiscovery == null) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 884 | return forgeDeviceDescription(deviceId, defaultAvailable); |
| 885 | } |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 886 | |
| 887 | final DeviceDescription d = deviceDiscovery.discoverDeviceDetails(); |
| 888 | if (d == null) { |
| 889 | return forgeDeviceDescription(deviceId, defaultAvailable); |
| 890 | } |
| 891 | // Enforce defaultAvailable flag over the one obtained from driver. |
| 892 | return new DefaultDeviceDescription(d, defaultAvailable, d.annotations()); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | private List<PortDescription> getPortDetails(DeviceId deviceId) { |
| 896 | final DeviceDescriptionDiscovery deviceDiscovery = getBehaviour( |
| 897 | deviceId, DeviceDescriptionDiscovery.class); |
| 898 | if (deviceDiscovery != null) { |
| 899 | return deviceDiscovery.discoverPortDetails(); |
| 900 | } else { |
| 901 | return Collections.emptyList(); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | private DeviceDescription forgeDeviceDescription( |
| 906 | DeviceId deviceId, boolean defaultAvailable) { |
| 907 | // Uses handshaker and provider config to get driver data. |
| 908 | final DeviceHandshaker handshaker = getBehaviour( |
| 909 | deviceId, DeviceHandshaker.class); |
| 910 | final Driver driver = handshaker != null |
| 911 | ? handshaker.handler().driver() : null; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 912 | return new DefaultDeviceDescription( |
| 913 | deviceId.uri(), |
| 914 | Device.Type.SWITCH, |
| 915 | driver != null ? driver.manufacturer() : UNKNOWN, |
| 916 | driver != null ? driver.hwVersion() : UNKNOWN, |
| 917 | driver != null ? driver.swVersion() : UNKNOWN, |
| 918 | UNKNOWN, |
| 919 | new ChassisId(), |
| 920 | defaultAvailable, |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 921 | DefaultAnnotations.EMPTY); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 924 | static boolean myScheme(DeviceId deviceId) { |
| 925 | return deviceId.uri().getScheme().equals(URI_SCHEME); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 926 | } |
| 927 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 928 | private boolean isPipelineProgrammable(DeviceId deviceId) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 929 | return hasBehaviour(deviceId, PiPipelineProgrammable.class); |
Carmelo Cascone | de3b684 | 2018-09-05 17:45:10 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 932 | private <U> U getFutureWithDeadline(CompletableFuture<U> future, String opDescription, |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 933 | DeviceId deviceId, U defaultValue, int timeout) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 934 | try { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 935 | return future.get(timeout, TimeUnit.SECONDS); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 936 | } catch (InterruptedException e) { |
| 937 | log.error("Thread interrupted while {} on {}", opDescription, deviceId); |
| 938 | Thread.currentThread().interrupt(); |
| 939 | } catch (ExecutionException e) { |
| 940 | log.error("Exception while {} on {}", opDescription, deviceId, e.getCause()); |
| 941 | } catch (TimeoutException e) { |
| 942 | log.error("Operation TIMEOUT while {} on {}", opDescription, deviceId); |
| 943 | } |
| 944 | return defaultValue; |
| 945 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 946 | } |