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