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