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 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 22 | import com.google.common.annotations.Beta; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableSet; |
| 24 | import com.google.common.collect.Maps; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 25 | import com.google.common.util.concurrent.Striped; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Modified; |
| 30 | import org.apache.felix.scr.annotations.Property; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 31 | import org.apache.felix.scr.annotations.Reference; |
| 32 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 33 | import org.onlab.packet.ChassisId; |
| 34 | import org.onlab.util.ItemNotFoundException; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 35 | import org.onlab.util.Tools; |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 36 | import org.onosproject.cfg.ComponentConfigService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 37 | import org.onosproject.cluster.ClusterService; |
| 38 | import org.onosproject.cluster.LeadershipService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 39 | import org.onosproject.core.CoreService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 40 | import org.onosproject.mastership.MastershipService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 41 | import org.onosproject.net.AnnotationKeys; |
| 42 | import org.onosproject.net.DefaultAnnotations; |
| 43 | import org.onosproject.net.Device; |
| 44 | import org.onosproject.net.DeviceId; |
| 45 | import org.onosproject.net.MastershipRole; |
| 46 | import org.onosproject.net.PortNumber; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 47 | import org.onosproject.net.behaviour.PiPipelineProgrammable; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 48 | import org.onosproject.net.behaviour.PortAdmin; |
| 49 | import org.onosproject.net.config.ConfigFactory; |
| 50 | import org.onosproject.net.config.NetworkConfigEvent; |
| 51 | import org.onosproject.net.config.NetworkConfigListener; |
| 52 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 53 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 54 | import org.onosproject.net.config.basics.SubjectFactories; |
| 55 | import org.onosproject.net.device.DefaultDeviceDescription; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 56 | import org.onosproject.net.device.DeviceAgentEvent; |
| 57 | import org.onosproject.net.device.DeviceAgentListener; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 58 | import org.onosproject.net.device.DeviceDescription; |
| 59 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 60 | import org.onosproject.net.device.DeviceEvent; |
| 61 | import org.onosproject.net.device.DeviceHandshaker; |
| 62 | import org.onosproject.net.device.DeviceListener; |
| 63 | import org.onosproject.net.device.DeviceProvider; |
| 64 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 65 | import org.onosproject.net.device.DeviceProviderService; |
| 66 | import org.onosproject.net.device.DeviceService; |
| 67 | import org.onosproject.net.device.PortDescription; |
| 68 | import org.onosproject.net.device.PortStatistics; |
| 69 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 70 | import org.onosproject.net.driver.Behaviour; |
| 71 | import org.onosproject.net.driver.DefaultDriverData; |
| 72 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 73 | import org.onosproject.net.driver.Driver; |
| 74 | import org.onosproject.net.driver.DriverData; |
| 75 | import org.onosproject.net.driver.DriverService; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 76 | import org.onosproject.net.pi.model.PiPipeconf; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 77 | import org.onosproject.net.pi.model.PiPipeconfId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 78 | import org.onosproject.net.pi.service.PiPipeconfConfig; |
| 79 | import org.onosproject.net.pi.service.PiPipeconfService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 80 | import org.onosproject.net.provider.AbstractProvider; |
| 81 | import org.onosproject.net.provider.ProviderId; |
| 82 | import org.onosproject.provider.general.device.api.GeneralProviderDeviceConfig; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 83 | import org.osgi.service.component.ComponentContext; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 84 | import org.slf4j.Logger; |
| 85 | |
Ray Milkey | b68bbbc | 2017-12-18 10:05:49 -0800 | [diff] [blame] | 86 | import java.security.SecureRandom; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 87 | import java.util.ArrayList; |
| 88 | import java.util.Collection; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 89 | import java.util.Collections; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 90 | import java.util.Dictionary; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 91 | import java.util.List; |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 92 | import java.util.Map; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 93 | import java.util.Set; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 94 | import java.util.concurrent.CompletableFuture; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 95 | import java.util.concurrent.ConcurrentHashMap; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 96 | import java.util.concurrent.ConcurrentMap; |
| 97 | import java.util.concurrent.CopyOnWriteArraySet; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 98 | import java.util.concurrent.ExecutionException; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 99 | import java.util.concurrent.ExecutorService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 100 | import java.util.concurrent.ScheduledExecutorService; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 101 | import java.util.concurrent.ScheduledFuture; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 102 | import java.util.concurrent.TimeUnit; |
| 103 | import java.util.concurrent.TimeoutException; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 104 | import java.util.concurrent.locks.Lock; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 105 | import java.util.function.Supplier; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 106 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 107 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 108 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 109 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 110 | import static org.onlab.util.Tools.groupedThreads; |
| 111 | import static org.onosproject.net.device.DeviceEvent.Type; |
| 112 | import static org.slf4j.LoggerFactory.getLogger; |
| 113 | |
| 114 | /** |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 115 | * Provider which uses drivers to detect device and do initial handshake and |
| 116 | * channel establishment with devices. Any other provider specific operation is |
| 117 | * also delegated to the DeviceHandshaker driver. |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 118 | */ |
| 119 | @Beta |
| 120 | @Component(immediate = true) |
| 121 | public class GeneralDeviceProvider extends AbstractProvider |
| 122 | implements DeviceProvider { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 123 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 124 | private static final String DRIVER = "driver"; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 125 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 126 | private final Logger log = getLogger(getClass()); |
| 127 | |
| 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 129 | private DeviceProviderRegistry providerRegistry; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 130 | |
| 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 132 | private ComponentConfigService componentConfigService; |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 133 | |
| 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 135 | private NetworkConfigRegistry cfgService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 136 | |
| 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 138 | private CoreService coreService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 139 | |
| 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 141 | private DeviceService deviceService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 142 | |
| 143 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 144 | private DriverService driverService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 145 | |
| 146 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 147 | private MastershipService mastershipService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 148 | |
| 149 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 150 | private PiPipeconfService piPipeconfService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 151 | |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 152 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 153 | private ClusterService clusterService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 154 | |
| 155 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 156 | private LeadershipService leadershipService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 157 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 158 | private static final String STATS_POLL_FREQUENCY = "deviceStatsPollFrequency"; |
| 159 | private static final int DEFAULT_STATS_POLL_FREQUENCY = 10; |
| 160 | @Property(name = STATS_POLL_FREQUENCY, intValue = DEFAULT_STATS_POLL_FREQUENCY, |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 161 | label = "Configure poll frequency for port status and statistics; " + |
| 162 | "default is 10 sec") |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 163 | private int statsPollFrequency = DEFAULT_STATS_POLL_FREQUENCY; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 164 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 165 | private static final String PROBE_FREQUENCY = "deviceProbeFrequency"; |
| 166 | private static final int DEFAULT_PROBE_FREQUENCY = 10; |
| 167 | @Property(name = PROBE_FREQUENCY, intValue = DEFAULT_PROBE_FREQUENCY, |
| 168 | label = "Configure probe frequency for checking device availability; " + |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 169 | "default is 10 sec") |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 170 | private int probeFrequency = DEFAULT_PROBE_FREQUENCY; |
| 171 | |
| 172 | private static final String OP_TIMEOUT_SHORT = "deviceOperationTimeoutShort"; |
| 173 | private static final int DEFAULT_OP_TIMEOUT_SHORT = 10; |
| 174 | @Property(name = OP_TIMEOUT_SHORT, intValue = DEFAULT_OP_TIMEOUT_SHORT, |
| 175 | label = "Configure timeout in seconds for device operations " + |
| 176 | "that are supposed to take a short time " + |
| 177 | "(e.g. checking device reachability); default is 10 seconds") |
| 178 | private int opTimeoutShort = DEFAULT_OP_TIMEOUT_SHORT; |
| 179 | |
| 180 | private static final String OP_TIMEOUT_LONG = "deviceOperationTimeoutLong"; |
| 181 | private static final int DEFAULT_OP_TIMEOUT_LONG = 60; |
| 182 | @Property(name = OP_TIMEOUT_LONG, intValue = DEFAULT_OP_TIMEOUT_LONG, |
| 183 | label = "Configure timeout in seconds for device operations " + |
| 184 | "that are supposed to take a relatively long time " + |
| 185 | "(e.g. pushing a large pipeline configuration with slow " + |
| 186 | "network); default is 60 seconds") |
| 187 | private int opTimeoutLong = DEFAULT_OP_TIMEOUT_LONG; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 188 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 189 | private static final String APP_NAME = "org.onosproject.generaldeviceprovider"; |
| 190 | private static final String URI_SCHEME = "device"; |
| 191 | private static final String CFG_SCHEME = "generalprovider"; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 192 | private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.general.provider.device"; |
| 193 | private static final int CORE_POOL_SIZE = 10; |
| 194 | private static final String UNKNOWN = "unknown"; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 195 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 196 | //FIXME this will be removed when the configuration is synced at the source. |
| 197 | private static final Set<String> PIPELINE_CONFIGURABLE_PROTOCOLS = ImmutableSet.of("p4runtime"); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 198 | |
| 199 | //FIXME to be removed when netcfg will issue device events in a bundle or |
| 200 | //ensures all configuration needed is present |
| 201 | private Set<DeviceId> deviceConfigured = new CopyOnWriteArraySet<>(); |
| 202 | private Set<DeviceId> driverConfigured = new CopyOnWriteArraySet<>(); |
| 203 | private Set<DeviceId> pipelineConfigured = new CopyOnWriteArraySet<>(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 204 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 205 | private final Map<DeviceId, DeviceHandshaker> handshakers = Maps.newConcurrentMap(); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 206 | private final Map<DeviceId, MastershipRole> requestedRoles = Maps.newConcurrentMap(); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 207 | private final Striped<Lock> deviceLocks = Striped.lock(30); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 208 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 209 | private ExecutorService connectionExecutor |
| 210 | = newFixedThreadPool(CORE_POOL_SIZE, groupedThreads( |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 211 | "onos/generaldeviceprovider-device-connect", "%d", log)); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 212 | private ScheduledExecutorService statsExecutor |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 213 | = newScheduledThreadPool(CORE_POOL_SIZE, groupedThreads( |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 214 | "onos/generaldeviceprovider-stats-poll", "%d", log)); |
| 215 | private ConcurrentMap<DeviceId, ScheduledFuture<?>> statsPollingTasks = new ConcurrentHashMap<>(); |
| 216 | private ScheduledExecutorService probeExecutor |
| 217 | = newSingleThreadScheduledExecutor(groupedThreads( |
| 218 | "onos/generaldeviceprovider-probe-", "%d", log)); |
| 219 | private ScheduledFuture<?> probeTask = null; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 220 | |
| 221 | private DeviceProviderService providerService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 222 | private InternalDeviceListener deviceListener = new InternalDeviceListener(); |
| 223 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 224 | private final ConfigFactory factory = |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 225 | new ConfigFactory<DeviceId, GeneralProviderDeviceConfig>( |
| 226 | SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 227 | GeneralProviderDeviceConfig.class, CFG_SCHEME) { |
| 228 | @Override |
| 229 | public GeneralProviderDeviceConfig createConfig() { |
| 230 | return new GeneralProviderDeviceConfig(); |
| 231 | } |
| 232 | }; |
| 233 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 234 | private final NetworkConfigListener cfgListener = new InternalNetworkConfigListener(); |
| 235 | private final DeviceAgentListener deviceAgentListener = new InternalDeviceAgentListener(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 236 | |
| 237 | |
| 238 | @Activate |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 239 | public void activate(ComponentContext context) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 240 | providerService = providerRegistry.register(this); |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 241 | componentConfigService.registerProperties(getClass()); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 242 | coreService.registerApplication(APP_NAME); |
| 243 | cfgService.registerConfigFactory(factory); |
| 244 | cfgService.addListener(cfgListener); |
| 245 | deviceService.addListener(deviceListener); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 246 | handshakers.clear(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 247 | //This will fail if ONOS has CFG and drivers which depend on this provider |
| 248 | // are activated, failing due to not finding the driver. |
| 249 | cfgService.getSubjects(DeviceId.class, GeneralProviderDeviceConfig.class) |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 250 | .forEach(this::triggerConnect); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 251 | //Initiating a periodic check to see if any device is available again and reconnect it. |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 252 | rescheduleProbeTask(); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 253 | modified(context); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 254 | log.info("Started"); |
| 255 | } |
| 256 | |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 257 | @Modified |
| 258 | public void modified(ComponentContext context) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 259 | if (context == null) { |
| 260 | return; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 263 | Dictionary<?, ?> properties = context.getProperties(); |
| 264 | final int oldStatsPollFrequency = statsPollFrequency; |
| 265 | statsPollFrequency = Tools.getIntegerProperty( |
| 266 | properties, STATS_POLL_FREQUENCY, DEFAULT_STATS_POLL_FREQUENCY); |
| 267 | log.info("Configured. {} is configured to {} seconds", |
| 268 | STATS_POLL_FREQUENCY, statsPollFrequency); |
| 269 | final int oldProbeFrequency = probeFrequency; |
| 270 | probeFrequency = Tools.getIntegerProperty( |
| 271 | properties, PROBE_FREQUENCY, DEFAULT_PROBE_FREQUENCY); |
| 272 | log.info("Configured. {} is configured to {} seconds", |
| 273 | PROBE_FREQUENCY, probeFrequency); |
| 274 | opTimeoutShort = Tools.getIntegerProperty( |
| 275 | properties, OP_TIMEOUT_SHORT, DEFAULT_OP_TIMEOUT_SHORT); |
| 276 | log.info("Configured. {} is configured to {} seconds", |
| 277 | OP_TIMEOUT_SHORT, opTimeoutShort); |
| 278 | opTimeoutLong = Tools.getIntegerProperty( |
| 279 | properties, OP_TIMEOUT_LONG, DEFAULT_OP_TIMEOUT_LONG); |
| 280 | log.info("Configured. {} is configured to {} seconds", |
| 281 | OP_TIMEOUT_LONG, opTimeoutLong); |
| 282 | |
| 283 | if (oldStatsPollFrequency != statsPollFrequency) { |
| 284 | rescheduleStatsPollingTasks(); |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 285 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 286 | |
| 287 | if (oldProbeFrequency != probeFrequency) { |
| 288 | rescheduleProbeTask(); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | private synchronized void rescheduleProbeTask() { |
| 293 | if (probeTask != null) { |
| 294 | probeTask.cancel(false); |
| 295 | } |
| 296 | probeTask = probeExecutor.scheduleAtFixedRate( |
| 297 | this::triggerProbeAllDevices, probeFrequency, |
| 298 | probeFrequency, TimeUnit.SECONDS); |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 301 | @Deactivate |
| 302 | public void deactivate() { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 303 | statsExecutor.shutdown(); |
| 304 | probeExecutor.shutdown(); |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 305 | componentConfigService.unregisterProperties(getClass(), false); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 306 | cfgService.removeListener(cfgListener); |
| 307 | //Not Removing the device so they can still be used from other driver providers |
| 308 | //cfgService.getSubjects(DeviceId.class, GeneralProviderDeviceConfig.class) |
| 309 | // .forEach(did -> connectionExecutor.execute(() -> disconnectDevice(did))); |
| 310 | connectionExecutor.shutdown(); |
| 311 | deviceService.removeListener(deviceListener); |
| 312 | providerRegistry.unregister(this); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 313 | handshakers.clear(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 314 | providerService = null; |
| 315 | cfgService.unregisterConfigFactory(factory); |
| 316 | log.info("Stopped"); |
| 317 | } |
| 318 | |
| 319 | public GeneralDeviceProvider() { |
| 320 | super(new ProviderId(URI_SCHEME, DEVICE_PROVIDER_PACKAGE)); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | @Override |
| 325 | public void triggerProbe(DeviceId deviceId) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 326 | connectionExecutor.execute(withDeviceLock( |
| 327 | () -> doDeviceProbe(deviceId), deviceId)); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 332 | log.info("Received role {} for device {}", newRole, deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 333 | requestedRoles.put(deviceId, newRole); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 334 | connectionExecutor.execute(() -> doRoleChanged(deviceId, newRole)); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | private void doRoleChanged(DeviceId deviceId, MastershipRole newRole) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 338 | final DeviceHandshaker handshaker = getHandshaker(deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 339 | if (handshaker == null) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 340 | log.error("Null handshaker. Unable to notify new role {} to {}", |
| 341 | newRole, deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | handshaker.roleChanged(newRole); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public boolean isReachable(DeviceId deviceId) { |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 349 | log.debug("Testing reachability for device {}", deviceId); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 350 | final DeviceHandshaker handshaker = getHandshaker(deviceId); |
Andrea Campanella | c1ecdd0 | 2018-01-12 12:48:24 +0100 | [diff] [blame] | 351 | if (handshaker == null) { |
| 352 | return false; |
| 353 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 354 | return getFutureWithDeadline( |
| 355 | handshaker.isReachable(), "checking reachability", |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 356 | deviceId, false, opTimeoutShort); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | @Override |
| 360 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 361 | boolean enable) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 362 | connectionExecutor.execute( |
| 363 | () -> doChangePortState(deviceId, portNumber, enable)); |
| 364 | } |
| 365 | |
| 366 | private void doChangePortState(DeviceId deviceId, PortNumber portNumber, |
| 367 | boolean enable) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 368 | if (!deviceService.getDevice(deviceId).is(PortAdmin.class)) { |
| 369 | log.warn("Missing PortAdmin behaviour on {}, aborting port state change", |
| 370 | deviceId); |
| 371 | return; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 372 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 373 | final PortAdmin portAdmin = deviceService.getDevice(deviceId) |
| 374 | .as(PortAdmin.class); |
| 375 | final CompletableFuture<Boolean> modifyTask = enable |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 376 | ? portAdmin.enable(portNumber) |
| 377 | : portAdmin.disable(portNumber); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 378 | final String descr = (enable ? "enabling" : "disabling") + " port " + portNumber; |
| 379 | getFutureWithDeadline( |
| 380 | modifyTask, descr, deviceId, null, opTimeoutShort); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 383 | @Override |
| 384 | public void triggerDisconnect(DeviceId deviceId) { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 385 | log.debug("Triggering disconnection of device {}", deviceId); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 386 | connectionExecutor.execute(withDeviceLock( |
| 387 | () -> doDisconnectDevice(deviceId), deviceId)); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 390 | private DeviceHandshaker getHandshaker(DeviceId deviceId) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 391 | return handshakers.computeIfAbsent(deviceId, id -> { |
| 392 | Driver driver = getDriver(deviceId); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 393 | return driver == null ? null : getBehaviour( |
| 394 | driver, DeviceHandshaker.class, |
| 395 | new DefaultDriverData(driver, deviceId)); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 396 | }); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 399 | private Driver getDriver(DeviceId deviceId) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 400 | try { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 401 | // DriverManager checks first using basic device config. |
| 402 | return driverService.getDriver(deviceId); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 403 | } catch (ItemNotFoundException e) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 404 | log.error("Driver not found for {}", deviceId); |
| 405 | return null; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 406 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 409 | private <T extends Behaviour> T getBehaviour(Driver driver, Class<T> type, |
| 410 | DriverData data) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 411 | // Allows obtaining behavior implementations before the device is pushed |
| 412 | // to the core. |
Andrea Campanella | c1ecdd0 | 2018-01-12 12:48:24 +0100 | [diff] [blame] | 413 | if (driver != null && driver.hasBehaviour(type)) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 414 | DefaultDriverHandler handler = new DefaultDriverHandler(data); |
| 415 | return driver.createBehaviour(handler, type); |
| 416 | } else { |
| 417 | return null; |
| 418 | } |
| 419 | } |
| 420 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 421 | private void doConnectDevice(DeviceId deviceId) { |
| 422 | // Some operations can be performed by one node only. |
| 423 | final boolean isLocalLeader = leadershipService.runForLeadership( |
| 424 | GeneralProviderDeviceConfig.class.getName() + deviceId) |
| 425 | .leader().nodeId().equals(clusterService.getLocalNode().id()); |
| 426 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 427 | if (deviceService.getDevice(deviceId) != null |
| 428 | && deviceService.isAvailable(deviceId)) { |
| 429 | log.info("Device {} is already connected to ONOS and is available", |
| 430 | deviceId); |
| 431 | return; |
| 432 | } |
| 433 | // Retrieve config |
| 434 | final GeneralProviderDeviceConfig providerConfig = cfgService.getConfig( |
| 435 | deviceId, GeneralProviderDeviceConfig.class); |
| 436 | final BasicDeviceConfig basicDeviceConfig = cfgService.getConfig( |
| 437 | deviceId, BasicDeviceConfig.class); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 438 | if (providerConfig == null || basicDeviceConfig == null) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 439 | log.error("Configuration missing, cannot connect to {}. " + |
| 440 | "basicDeviceConfig={}, generalProvider={}", |
| 441 | deviceId, basicDeviceConfig, providerConfig); |
| 442 | return; |
| 443 | } |
| 444 | log.info("Initiating connection to device {} with driver {} ... asMaster={}", |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 445 | deviceId, basicDeviceConfig.driver(), isLocalLeader); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 446 | // Get handshaker, driver and driver data. |
| 447 | final DeviceHandshaker handshaker = getHandshaker(deviceId); |
| 448 | if (handshaker == null) { |
| 449 | log.error("Missing DeviceHandshaker behavior for {}, aborting connection", |
| 450 | deviceId); |
| 451 | return; |
| 452 | } |
| 453 | final Driver driver = handshaker.handler().driver(); |
| 454 | // Enhance driver data with info in GDP config. |
| 455 | augmentConfigData(providerConfig, handshaker.data()); |
| 456 | final DriverData driverData = handshaker.data(); |
| 457 | // Start connection via handshaker. |
| 458 | final Boolean connectSuccess = getFutureWithDeadline( |
| 459 | handshaker.connect(), "initiating connection", |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 460 | deviceId, null, opTimeoutShort); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 461 | if (connectSuccess == null) { |
| 462 | // Error logged by getFutureWithDeadline(). |
| 463 | return; |
| 464 | } else if (!connectSuccess) { |
| 465 | log.warn("Unable to connect to {}", deviceId); |
| 466 | return; |
| 467 | } |
| 468 | // Handle pipeconf (if device is capable) |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 469 | if (!handlePipeconf(deviceId, driver, driverData, isLocalLeader)) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 470 | // We already logged the error. |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 471 | getFutureWithDeadline( |
| 472 | handshaker.disconnect(), "performing disconnection", |
| 473 | deviceId, null, opTimeoutShort); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 474 | return; |
| 475 | } |
| 476 | // Add device agent listener. |
| 477 | handshaker.addDeviceAgentListener(deviceAgentListener); |
| 478 | // All good. Notify core (if master). |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 479 | if (isLocalLeader) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 480 | advertiseDevice(deviceId, driver, providerConfig, driverData); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 484 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 485 | private void advertiseDevice(DeviceId deviceId, Driver driver, |
| 486 | GeneralProviderDeviceConfig providerConfig, |
| 487 | DriverData driverData) { |
| 488 | // Obtain device and port description and advertise device to core. |
| 489 | DeviceDescription description = null; |
| 490 | final List<PortDescription> ports; |
| 491 | |
| 492 | final DeviceDescriptionDiscovery deviceDiscovery = getBehaviour( |
| 493 | driver, DeviceDescriptionDiscovery.class, driverData); |
| 494 | |
| 495 | if (deviceDiscovery != null) { |
| 496 | description = deviceDiscovery.discoverDeviceDetails(); |
| 497 | ports = deviceDiscovery.discoverPortDetails(); |
| 498 | } else { |
| 499 | log.warn("Missing DeviceDescriptionDiscovery behavior for {}, " + |
| 500 | "no update for description or ports.", deviceId); |
| 501 | ports = new ArrayList<>(); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 502 | } |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 503 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 504 | if (description == null) { |
| 505 | // Generate one here. |
| 506 | // FIXME: a behavior impl should not return a null description |
| 507 | // (e.g. as GnmiDeviceDescriptionDiscovery). This case should apply |
| 508 | // only if a the behavior is not available. |
| 509 | description = new DefaultDeviceDescription( |
| 510 | deviceId.uri(), Device.Type.SWITCH, |
| 511 | driver.manufacturer(), driver.hwVersion(), |
| 512 | driver.swVersion(), UNKNOWN, |
| 513 | new ChassisId(), true, |
| 514 | DefaultAnnotations.builder() |
| 515 | .set(AnnotationKeys.PROTOCOL, |
| 516 | providerConfig.protocolsInfo().keySet().toString()) |
| 517 | .build()); |
| 518 | } |
| 519 | |
| 520 | providerService.deviceConnected(deviceId, description); |
| 521 | providerService.updatePorts(deviceId, ports); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 524 | /** |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 525 | * Handles the case of a device that is pipeline programmable. Returns true |
| 526 | * if the operation wa successful and the device can be registered to the |
| 527 | * core, false otherwise. |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 528 | */ |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 529 | private boolean handlePipeconf(DeviceId deviceId, Driver driver, |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 530 | DriverData driverData, boolean asMaster) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 531 | final PiPipelineProgrammable pipelineProg = getBehaviour( |
| 532 | driver, PiPipelineProgrammable.class, driverData); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 533 | if (pipelineProg == null) { |
| 534 | // Device is not pipeline programmable. |
| 535 | return true; |
| 536 | } |
| 537 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 538 | final PiPipeconf pipeconf = getPipeconf(deviceId, pipelineProg); |
| 539 | if (pipeconf == null) { |
| 540 | return false; |
| 541 | } |
| 542 | final PiPipeconfId pipeconfId = pipeconf.id(); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 543 | |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame^] | 544 | // To be removed in change #19606 |
| 545 | // final String mergedDriverName = piPipeconfService.mergeDriver( |
| 546 | // deviceId, pipeconfId); |
| 547 | // if (mergedDriverName == null) { |
| 548 | // log.error("Unable to get merged driver for {} and {}, aborting device discovery", |
| 549 | // deviceId, pipeconfId); |
| 550 | // return false; |
| 551 | // } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 552 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 553 | if (!asMaster) { |
| 554 | // From now one only the master. |
| 555 | return true; |
| 556 | } |
| 557 | |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame^] | 558 | // if (!setDriverViaCfg(deviceId, mergedDriverName)) { |
| 559 | // return false; |
| 560 | // } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 561 | |
| 562 | // FIXME: we just introduced a race condition as it might happen that a |
| 563 | // node does not receive the new cfg (with the merged driver) before the |
| 564 | // device is advertised to the core. Perhaps we should be waiting for a |
| 565 | // NetworkConfig event signaling that the driver has been updated on all |
| 566 | // nodes? The effect is mitigated by deploying the pipeconf (slow |
| 567 | // operation), after calling setDriverViaCfg(). |
| 568 | |
| 569 | piPipeconfService.bindToDevice(pipeconfId, deviceId); |
| 570 | |
| 571 | final Boolean deploySuccess = getFutureWithDeadline( |
| 572 | pipelineProg.deployPipeconf(pipeconf), |
| 573 | "deploying pipeconf", deviceId, null, |
| 574 | opTimeoutLong); |
| 575 | if (deploySuccess == null) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 576 | // Error logged by getFutureWithDeadline(). |
| 577 | return false; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 578 | } else if (!deploySuccess) { |
| 579 | log.error("Unable to deploy pipeconf {} to {}, aborting device discovery", |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 580 | pipeconfId, deviceId); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 581 | return false; |
| 582 | } |
| 583 | |
| 584 | return true; |
| 585 | } |
| 586 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 587 | private boolean setDriverViaCfg(DeviceId deviceId, String driverName) { |
| 588 | BasicDeviceConfig cfg = cfgService.getConfig(deviceId, BasicDeviceConfig.class); |
| 589 | if (cfg == null) { |
| 590 | log.error("Unable to get basic device config for {}, aborting device discovery", |
| 591 | deviceId); |
| 592 | return false; |
| 593 | } |
| 594 | ObjectNode newCfg = (ObjectNode) cfg.node(); |
| 595 | newCfg = newCfg.put(DRIVER, driverName); |
| 596 | ObjectMapper mapper = new ObjectMapper(); |
| 597 | JsonNode newCfgNode = mapper.convertValue(newCfg, JsonNode.class); |
| 598 | cfgService.applyConfig(deviceId, BasicDeviceConfig.class, newCfgNode); |
| 599 | return true; |
| 600 | } |
| 601 | |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 602 | private PiPipeconf getPipeconf(DeviceId deviceId, PiPipelineProgrammable pipelineProg) { |
Carmelo Cascone | 9204452 | 2018-06-29 19:00:59 +0200 | [diff] [blame] | 603 | PiPipeconfId pipeconfId = getPipeconfFromCfg(deviceId); |
| 604 | if (pipeconfId == null || pipeconfId.id().isEmpty()) { |
| 605 | // No pipeconf has been provided in the cfg. |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 606 | // Check if device driver provides a default one. |
| 607 | if (pipelineProg.getDefaultPipeconf().isPresent()) { |
Carmelo Cascone | 9204452 | 2018-06-29 19:00:59 +0200 | [diff] [blame] | 608 | final PiPipeconf defaultPipeconf = pipelineProg.getDefaultPipeconf().get(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 609 | log.info("Using default pipeconf {} for {}", defaultPipeconf.id(), deviceId); |
Carmelo Cascone | 9204452 | 2018-06-29 19:00:59 +0200 | [diff] [blame] | 610 | pipeconfId = defaultPipeconf.id(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 611 | } else { |
Carmelo Cascone | 9204452 | 2018-06-29 19:00:59 +0200 | [diff] [blame] | 612 | log.warn("Device {} is pipeline programmable but no pipeconf can be associated to it", deviceId); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 613 | return null; |
| 614 | } |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 615 | } |
Carmelo Cascone | 9204452 | 2018-06-29 19:00:59 +0200 | [diff] [blame] | 616 | // Check if registered |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 617 | if (!piPipeconfService.getPipeconf(pipeconfId).isPresent()) { |
| 618 | log.warn("Pipeconf {} is not registered", pipeconfId); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 619 | return null; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 620 | } |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 621 | return piPipeconfService.getPipeconf(pipeconfId).get(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 622 | } |
| 623 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 624 | private void doDisconnectDevice(DeviceId deviceId) { |
Carmelo Cascone | 7044efd | 2018-07-06 13:01:36 +0200 | [diff] [blame] | 625 | log.debug("Initiating disconnection from {}...", deviceId); |
| 626 | final DeviceHandshaker handshaker = handshakers.remove(deviceId); |
| 627 | final boolean isAvailable = deviceService.isAvailable(deviceId); |
| 628 | // Signal disconnection to core (if master). |
| 629 | if (isAvailable && mastershipService.isLocalMaster(deviceId)) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 630 | providerService.deviceDisconnected(deviceId); |
| 631 | } |
Carmelo Cascone | 7044efd | 2018-07-06 13:01:36 +0200 | [diff] [blame] | 632 | // Cancel tasks. |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 633 | cancelStatsPolling(deviceId); |
Carmelo Cascone | 7044efd | 2018-07-06 13:01:36 +0200 | [diff] [blame] | 634 | // Disconnect device. |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 635 | if (handshaker == null) { |
Carmelo Cascone | 7044efd | 2018-07-06 13:01:36 +0200 | [diff] [blame] | 636 | if (isAvailable) { |
| 637 | // If not available don't bother logging. We are probably |
| 638 | // invoking this method multiple times for the same device. |
| 639 | log.warn("Missing DeviceHandshaker behavior for {}, " + |
| 640 | "no guarantees of complete disconnection", |
| 641 | deviceId); |
| 642 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 643 | return; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 644 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 645 | handshaker.removeDeviceAgentListener(deviceAgentListener); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 646 | final boolean disconnectSuccess = getFutureWithDeadline( |
| 647 | handshaker.disconnect(), "performing disconnection", |
| 648 | deviceId, false, opTimeoutShort); |
| 649 | if (!disconnectSuccess) { |
| 650 | log.warn("Unable to disconnect from {}", deviceId); |
| 651 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 654 | // 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] | 655 | private Runnable exceptionSafe(Runnable runnable) { |
| 656 | return () -> { |
| 657 | try { |
| 658 | runnable.run(); |
| 659 | } catch (Exception e) { |
| 660 | log.error("Unhandled Exception", e); |
| 661 | } |
| 662 | }; |
| 663 | } |
| 664 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 665 | private <U> U withDeviceLock(Supplier<U> task, DeviceId deviceId) { |
| 666 | final Lock lock = deviceLocks.get(deviceId); |
| 667 | lock.lock(); |
| 668 | try { |
| 669 | return task.get(); |
| 670 | } finally { |
| 671 | lock.unlock(); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | private Runnable withDeviceLock(Runnable task, DeviceId deviceId) { |
| 676 | // Wrapper of withDeviceLock(Supplier, ...) for void tasks. |
| 677 | return () -> withDeviceLock(() -> { |
| 678 | task.run(); |
| 679 | return null; |
| 680 | }, deviceId); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 681 | } |
| 682 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 683 | private void updatePortStatistics(DeviceId deviceId) { |
Andrea Campanella | ce11193 | 2017-09-18 16:59:56 +0900 | [diff] [blame] | 684 | Device device = deviceService.getDevice(deviceId); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 685 | if (device != null && deviceService.isAvailable(deviceId) && |
Andrea Campanella | ce11193 | 2017-09-18 16:59:56 +0900 | [diff] [blame] | 686 | device.is(PortStatisticsDiscovery.class)) { |
| 687 | Collection<PortStatistics> statistics = device.as(PortStatisticsDiscovery.class) |
| 688 | .discoverPortStatistics(); |
| 689 | //updating statistcs only if not empty |
| 690 | if (!statistics.isEmpty()) { |
| 691 | providerService.updatePortStatistics(deviceId, statistics); |
| 692 | } |
| 693 | } else { |
| 694 | log.debug("Can't update port statistics for device {}", deviceId); |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 698 | private boolean notMyScheme(DeviceId deviceId) { |
| 699 | return !deviceId.uri().getScheme().equals(URI_SCHEME); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 702 | private void triggerConnect(DeviceId deviceId) { |
| 703 | connectionExecutor.execute(withDeviceLock( |
| 704 | () -> doConnectDevice(deviceId), deviceId)); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 705 | } |
| 706 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 707 | /** |
| 708 | * Listener for configuration events. |
| 709 | */ |
| 710 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 711 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 712 | @Override |
| 713 | public void event(NetworkConfigEvent event) { |
Carmelo Cascone | 7044efd | 2018-07-06 13:01:36 +0200 | [diff] [blame] | 714 | connectionExecutor.execute(() -> consumeConfigEvent(event)); |
| 715 | } |
| 716 | |
| 717 | @Override |
| 718 | public boolean isRelevant(NetworkConfigEvent event) { |
| 719 | return (event.configClass().equals(GeneralProviderDeviceConfig.class) || |
| 720 | event.configClass().equals(BasicDeviceConfig.class) || |
| 721 | event.configClass().equals(PiPipeconfConfig.class)) && |
| 722 | (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 723 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED); |
| 724 | } |
| 725 | |
| 726 | private void consumeConfigEvent(NetworkConfigEvent event) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 727 | DeviceId deviceId = (DeviceId) event.subject(); |
| 728 | //Assuming that the deviceId comes with uri 'device:' |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 729 | if (notMyScheme(deviceId)) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 730 | // not under my scheme, skipping |
| 731 | log.debug("{} is not my scheme, skipping", deviceId); |
| 732 | return; |
| 733 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 734 | final boolean configComplete = withDeviceLock( |
| 735 | () -> isDeviceConfigComplete(event, deviceId), deviceId); |
| 736 | if (!configComplete) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 737 | // Still waiting for some configuration. |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 738 | return; |
| 739 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 740 | // Good to go. |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 741 | triggerConnect(deviceId); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 742 | cleanUpConfigInfo(deviceId); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 743 | } |
| 744 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 745 | private boolean isDeviceConfigComplete(NetworkConfigEvent event, DeviceId deviceId) { |
| 746 | // 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] | 747 | // ensure all configuration needed is present |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 748 | if (event.configClass().equals(GeneralProviderDeviceConfig.class)) { |
| 749 | //FIXME we currently assume that p4runtime devices are pipeline configurable. |
| 750 | //If we want to connect a p4runtime device with no pipeline |
| 751 | if (event.config().isPresent()) { |
| 752 | deviceConfigured.add(deviceId); |
| 753 | final boolean isNotPipelineConfigurable = Collections.disjoint( |
| 754 | ImmutableSet.copyOf(event.config().get().node().fieldNames()), |
| 755 | PIPELINE_CONFIGURABLE_PROTOCOLS); |
| 756 | if (isNotPipelineConfigurable) { |
| 757 | // Skip waiting for a pipeline if we can't support it. |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 758 | pipelineConfigured.add(deviceId); |
| 759 | } |
| 760 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 761 | } else if (event.configClass().equals(BasicDeviceConfig.class)) { |
| 762 | if (event.config().isPresent() && event.config().get().node().has(DRIVER)) { |
| 763 | driverConfigured.add(deviceId); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 764 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 765 | } else if (event.configClass().equals(PiPipeconfConfig.class)) { |
| 766 | if (event.config().isPresent() |
| 767 | && event.config().get().node().has(PiPipeconfConfig.PIPIPECONFID)) { |
| 768 | pipelineConfigured.add(deviceId); |
| 769 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 770 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 771 | |
| 772 | if (deviceConfigured.contains(deviceId) |
| 773 | && driverConfigured.contains(deviceId) |
| 774 | && pipelineConfigured.contains(deviceId)) { |
| 775 | return true; |
| 776 | } else { |
| 777 | if (deviceConfigured.contains(deviceId) && driverConfigured.contains(deviceId)) { |
| 778 | log.debug("Waiting for pipeline configuration for device {}", deviceId); |
| 779 | } else if (pipelineConfigured.contains(deviceId) && driverConfigured.contains(deviceId)) { |
| 780 | log.debug("Waiting for device configuration for device {}", deviceId); |
| 781 | } else if (pipelineConfigured.contains(deviceId) && deviceConfigured.contains(deviceId)) { |
| 782 | log.debug("Waiting for driver configuration for device {}", deviceId); |
| 783 | } else if (driverConfigured.contains(deviceId)) { |
| 784 | log.debug("Only driver configuration for device {}", deviceId); |
| 785 | } else if (deviceConfigured.contains(deviceId)) { |
| 786 | log.debug("Only device configuration for device {}", deviceId); |
| 787 | } |
| 788 | } |
| 789 | return false; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 790 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 793 | private void augmentConfigData(GeneralProviderDeviceConfig providerConfig, DriverData driverData) { |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 794 | //Storing deviceKeyId and all other config values |
| 795 | // as data in the driver with protocol_<info> |
| 796 | // name as the key. e.g protocol_ip |
| 797 | providerConfig.protocolsInfo() |
| 798 | .forEach((protocol, deviceInfoConfig) -> { |
| 799 | deviceInfoConfig.configValues() |
| 800 | .forEach((k, v) -> driverData.set(protocol + "_" + k, v)); |
| 801 | driverData.set(protocol + "_key", deviceInfoConfig.deviceKeyId()); |
| 802 | }); |
| 803 | } |
| 804 | |
| 805 | private void cleanUpConfigInfo(DeviceId deviceId) { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 806 | deviceConfigured.remove(deviceId); |
| 807 | driverConfigured.remove(deviceId); |
| 808 | pipelineConfigured.remove(deviceId); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 811 | private void startStatsPolling(DeviceId deviceId, boolean withRandomDelay) { |
| 812 | statsPollingTasks.compute(deviceId, (did, oldTask) -> { |
| 813 | if (oldTask != null) { |
| 814 | oldTask.cancel(false); |
| 815 | } |
| 816 | final int delay = withRandomDelay |
| 817 | ? new SecureRandom().nextInt(10) : 0; |
| 818 | return statsExecutor.scheduleAtFixedRate( |
| 819 | exceptionSafe(() -> updatePortStatistics(deviceId)), |
| 820 | delay, statsPollFrequency, TimeUnit.SECONDS); |
| 821 | }); |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 824 | private void cancelStatsPolling(DeviceId deviceId) { |
| 825 | statsPollingTasks.computeIfPresent(deviceId, (did, task) -> { |
| 826 | task.cancel(false); |
| 827 | return null; |
| 828 | }); |
| 829 | } |
| 830 | |
| 831 | private void rescheduleStatsPollingTasks() { |
| 832 | statsPollingTasks.keySet().forEach(deviceId -> { |
| 833 | // startStatsPolling cancels old one if present. |
| 834 | startStatsPolling(deviceId, true); |
| 835 | }); |
| 836 | } |
| 837 | |
| 838 | private void triggerProbeAllDevices() { |
| 839 | // Async trigger a task for all devices in the cfg. |
| 840 | cfgService.getSubjects(DeviceId.class, GeneralProviderDeviceConfig.class) |
| 841 | .forEach(deviceId -> connectionExecutor.execute(withDeviceLock( |
| 842 | () -> doDeviceProbe(deviceId), deviceId))); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 843 | } |
| 844 | |
Carmelo Cascone | 9204452 | 2018-06-29 19:00:59 +0200 | [diff] [blame] | 845 | private PiPipeconfId getPipeconfFromCfg(DeviceId deviceId) { |
| 846 | PiPipeconfConfig config = cfgService.getConfig( |
| 847 | deviceId, PiPipeconfConfig.class); |
| 848 | if (config == null) { |
| 849 | return null; |
| 850 | } |
| 851 | return config.piPipeconfId(); |
| 852 | } |
| 853 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 854 | private void doDeviceProbe(DeviceId deviceId) { |
| 855 | if (!configIsPresent(deviceId)) { |
| 856 | return; |
| 857 | } |
| 858 | final boolean isAvailable = deviceService.getDevice(deviceId) != null |
| 859 | && deviceService.isAvailable(deviceId); |
| 860 | final boolean isLocalMaster = mastershipService.isLocalMaster(deviceId); |
| 861 | if (isAvailable) { |
| 862 | if (!isLocalMaster) { |
| 863 | return; |
| 864 | } |
| 865 | if (!isReachable(deviceId)) { |
| 866 | log.info("Disconnecting available but unreachable device {}...", |
| 867 | deviceId); |
| 868 | triggerDisconnect(deviceId); |
| 869 | } |
| 870 | } else { |
| 871 | // We do not check for reachability using isReachable() |
| 872 | // since the behaviour of this method can vary depending on protocol |
| 873 | // nuances. We leave this check to the device handshaker at later |
| 874 | // stages of the connection process. |
| 875 | triggerConnect(deviceId); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
| 879 | private boolean configIsPresent(DeviceId deviceId) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 880 | final boolean present = |
| 881 | cfgService.getConfig(deviceId, GeneralProviderDeviceConfig.class) != null |
| 882 | && cfgService.getConfig(deviceId, BasicDeviceConfig.class) != null; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 883 | if (!present) { |
| 884 | log.warn("Configuration for device {} is not complete", deviceId); |
| 885 | } |
| 886 | return present; |
| 887 | } |
| 888 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 889 | private void handleChannelClosed(DeviceId deviceId) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 890 | log.info("Disconnecting device {}, due to channel closed event", |
| 891 | deviceId); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 892 | triggerDisconnect(deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | private void handleMastershipResponse(DeviceId deviceId, MastershipRole response) { |
| 896 | //Notify core about response. |
| 897 | if (!requestedRoles.containsKey(deviceId)) { |
| 898 | return; |
| 899 | } |
| 900 | providerService.receivedRoleReply(deviceId, requestedRoles.get(deviceId), response); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 901 | if (response.equals(MastershipRole.MASTER)) { |
| 902 | startStatsPolling(deviceId, false); |
| 903 | } else { |
| 904 | cancelStatsPolling(deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 908 | private <U> U getFutureWithDeadline(CompletableFuture<U> future, String opDescription, |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 909 | DeviceId deviceId, U defaultValue, int timeout) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 910 | try { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 911 | return future.get(timeout, TimeUnit.SECONDS); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 912 | } catch (InterruptedException e) { |
| 913 | log.error("Thread interrupted while {} on {}", opDescription, deviceId); |
| 914 | Thread.currentThread().interrupt(); |
| 915 | } catch (ExecutionException e) { |
| 916 | log.error("Exception while {} on {}", opDescription, deviceId, e.getCause()); |
| 917 | } catch (TimeoutException e) { |
| 918 | log.error("Operation TIMEOUT while {} on {}", opDescription, deviceId); |
| 919 | } |
| 920 | return defaultValue; |
| 921 | } |
| 922 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 923 | /** |
| 924 | * Listener for core device events. |
| 925 | */ |
| 926 | private class InternalDeviceListener implements DeviceListener { |
| 927 | @Override |
| 928 | public void event(DeviceEvent event) { |
Andrea Campanella | ce11193 | 2017-09-18 16:59:56 +0900 | [diff] [blame] | 929 | DeviceId deviceId = event.subject().id(); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 930 | // For now this is scheduled periodically, when streaming API will |
| 931 | // be available we check and base it on the streaming API (e.g. gNMI) |
| 932 | if (mastershipService.isLocalMaster(deviceId)) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 933 | startStatsPolling(deviceId, true); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
| 937 | @Override |
| 938 | public boolean isRelevant(DeviceEvent event) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 939 | return event.type() == Type.DEVICE_ADDED && |
| 940 | event.subject().id().toString().startsWith(URI_SCHEME.toLowerCase()); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 941 | } |
| 942 | } |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 943 | |
| 944 | /** |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 945 | * Listener for device agent events. |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 946 | */ |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 947 | private class InternalDeviceAgentListener implements DeviceAgentListener { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 948 | |
| 949 | @Override |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 950 | public void event(DeviceAgentEvent event) { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 951 | DeviceId deviceId = event.subject(); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 952 | switch (event.type()) { |
| 953 | case CHANNEL_OPEN: |
| 954 | // Ignore. |
| 955 | break; |
| 956 | case CHANNEL_CLOSED: |
| 957 | handleChannelClosed(deviceId); |
| 958 | break; |
| 959 | case CHANNEL_ERROR: |
| 960 | // TODO evaluate other reaction to channel error. |
| 961 | log.warn("Received CHANNEL_ERROR from {}. Is the channel still open?", |
| 962 | deviceId); |
Carmelo Cascone | e15773f | 2018-08-21 18:31:42 -0700 | [diff] [blame] | 963 | handleChannelClosed(deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 964 | break; |
| 965 | case ROLE_MASTER: |
| 966 | handleMastershipResponse(deviceId, MastershipRole.MASTER); |
| 967 | break; |
| 968 | case ROLE_STANDBY: |
| 969 | handleMastershipResponse(deviceId, MastershipRole.STANDBY); |
| 970 | break; |
| 971 | case ROLE_NONE: |
| 972 | handleMastershipResponse(deviceId, MastershipRole.NONE); |
| 973 | break; |
| 974 | default: |
| 975 | log.warn("Unrecognized device agent event {}", event.type()); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 976 | } |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 980 | } |