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