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