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; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Modified; |
| 26 | import org.apache.felix.scr.annotations.Property; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.onlab.packet.ChassisId; |
| 30 | import org.onlab.util.ItemNotFoundException; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 31 | import org.onlab.util.Tools; |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 32 | import org.onosproject.cfg.ComponentConfigService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 33 | import org.onosproject.cluster.ClusterService; |
| 34 | import org.onosproject.cluster.LeadershipService; |
| 35 | import org.onosproject.cluster.NodeId; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 36 | import org.onosproject.core.CoreService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 37 | import org.onosproject.mastership.MastershipService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 38 | import org.onosproject.net.AnnotationKeys; |
| 39 | import org.onosproject.net.DefaultAnnotations; |
| 40 | import org.onosproject.net.Device; |
| 41 | import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.net.MastershipRole; |
| 43 | import org.onosproject.net.PortNumber; |
| 44 | import org.onosproject.net.SparseAnnotations; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 45 | import org.onosproject.net.behaviour.PiPipelineProgrammable; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 46 | import org.onosproject.net.behaviour.PortAdmin; |
| 47 | import org.onosproject.net.config.ConfigFactory; |
| 48 | import org.onosproject.net.config.NetworkConfigEvent; |
| 49 | import org.onosproject.net.config.NetworkConfigListener; |
| 50 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 51 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 52 | import org.onosproject.net.config.basics.SubjectFactories; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 53 | import org.onosproject.net.device.ChannelEvent; |
| 54 | import org.onosproject.net.device.ChannelListener; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 55 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 56 | import org.onosproject.net.device.DeviceDescription; |
| 57 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 58 | import org.onosproject.net.device.DeviceEvent; |
| 59 | import org.onosproject.net.device.DeviceHandshaker; |
| 60 | import org.onosproject.net.device.DeviceListener; |
| 61 | import org.onosproject.net.device.DeviceProvider; |
| 62 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 63 | import org.onosproject.net.device.DeviceProviderService; |
| 64 | import org.onosproject.net.device.DeviceService; |
| 65 | import org.onosproject.net.device.PortDescription; |
| 66 | import org.onosproject.net.device.PortStatistics; |
| 67 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 68 | import org.onosproject.net.driver.Behaviour; |
| 69 | import org.onosproject.net.driver.DefaultDriverData; |
| 70 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 71 | import org.onosproject.net.driver.Driver; |
| 72 | import org.onosproject.net.driver.DriverData; |
| 73 | import org.onosproject.net.driver.DriverService; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 74 | import org.onosproject.net.pi.model.PiPipeconf; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 75 | import org.onosproject.net.pi.model.PiPipeconfId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 76 | import org.onosproject.net.pi.service.PiPipeconfConfig; |
| 77 | import org.onosproject.net.pi.service.PiPipeconfService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 78 | import org.onosproject.net.provider.AbstractProvider; |
| 79 | import org.onosproject.net.provider.ProviderId; |
| 80 | import org.onosproject.provider.general.device.api.GeneralProviderDeviceConfig; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 81 | import org.osgi.service.component.ComponentContext; |
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.ArrayList; |
| 86 | import java.util.Collection; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 87 | import java.util.Collections; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 88 | import java.util.Dictionary; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 89 | import java.util.List; |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 90 | import java.util.Map; |
Andrea Campanella | ce11193 | 2017-09-18 16:59:56 +0900 | [diff] [blame] | 91 | import java.util.Objects; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 92 | import java.util.Set; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 93 | import java.util.concurrent.CompletableFuture; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 94 | import java.util.concurrent.ConcurrentHashMap; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 95 | import java.util.concurrent.ConcurrentMap; |
| 96 | import java.util.concurrent.CopyOnWriteArraySet; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 97 | import java.util.concurrent.ExecutionException; |
| 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; |
| 103 | import java.util.concurrent.locks.ReentrantLock; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 104 | |
| 105 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 106 | import static org.onlab.util.Tools.groupedThreads; |
| 107 | import static org.onosproject.net.device.DeviceEvent.Type; |
| 108 | import static org.slf4j.LoggerFactory.getLogger; |
| 109 | |
| 110 | /** |
| 111 | * Provider which uses drivers to detect device and do initial handshake |
| 112 | * and channel establishment with devices. Any other provider specific operation |
| 113 | * is also delegated to the DeviceHandshaker driver. |
| 114 | */ |
| 115 | @Beta |
| 116 | @Component(immediate = true) |
| 117 | public class GeneralDeviceProvider extends AbstractProvider |
| 118 | implements DeviceProvider { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 119 | public static final String DRIVER = "driver"; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 120 | public static final int REACHABILITY_TIMEOUT = 10; |
| 121 | public static final String DEPLOY = "deploy-"; |
| 122 | public static final String PIPECONF_TOPIC = "-pipeconf"; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 123 | public static final String CHECK = "check-"; |
| 124 | public static final String CONNECTION = "-connection"; |
| 125 | private static final String POLL_FREQUENCY = "pollFrequency"; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 126 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 127 | private final Logger log = getLogger(getClass()); |
| 128 | |
| 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 130 | protected DeviceProviderRegistry providerRegistry; |
| 131 | |
| 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 133 | protected ComponentConfigService componentConfigService; |
| 134 | |
| 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 136 | protected NetworkConfigRegistry cfgService; |
| 137 | |
| 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 139 | protected CoreService coreService; |
| 140 | |
| 141 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 142 | protected DeviceService deviceService; |
| 143 | |
| 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 145 | protected DriverService driverService; |
| 146 | |
| 147 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 148 | protected MastershipService mastershipService; |
| 149 | |
| 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 151 | protected PiPipeconfService piPipeconfService; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 152 | |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 153 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 154 | protected ClusterService clusterService; |
| 155 | |
| 156 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 157 | protected LeadershipService leadershipService; |
| 158 | |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 159 | private static final int DEFAULT_POLL_FREQUENCY_SECONDS = 10; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 160 | @Property(name = POLL_FREQUENCY, intValue = DEFAULT_POLL_FREQUENCY_SECONDS, |
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") |
| 163 | private int pollFrequency = DEFAULT_POLL_FREQUENCY_SECONDS; |
| 164 | |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 165 | private static final int DEVICE_AVAILABILITY_POLL_FREQUENCY_SECONDS = 10; |
| 166 | @Property(name = "deviceAvailabilityPollFrequency", intValue = DEVICE_AVAILABILITY_POLL_FREQUENCY_SECONDS, |
| 167 | label = "Configure poll frequency for checking device availability; " + |
| 168 | "default is 10 sec") |
| 169 | private int deviceAvailabilityPollFrequency = DEVICE_AVAILABILITY_POLL_FREQUENCY_SECONDS; |
| 170 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 171 | protected static final String APP_NAME = "org.onosproject.generaldeviceprovider"; |
| 172 | protected static final String URI_SCHEME = "device"; |
| 173 | protected static final String CFG_SCHEME = "generalprovider"; |
| 174 | private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.general.provider.device"; |
| 175 | private static final int CORE_POOL_SIZE = 10; |
| 176 | private static final String UNKNOWN = "unknown"; |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 177 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 178 | //FIXME this will be removed when the configuration is synced at the source. |
| 179 | private static final Set<String> PIPELINE_CONFIGURABLE_PROTOCOLS = ImmutableSet.of("p4runtime"); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 180 | |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 181 | private static final ConcurrentMap<DeviceId, Lock> ENTRY_LOCKS = Maps.newConcurrentMap(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 182 | //FIXME to be removed when netcfg will issue device events in a bundle or |
| 183 | //ensures all configuration needed is present |
| 184 | private Set<DeviceId> deviceConfigured = new CopyOnWriteArraySet<>(); |
| 185 | private Set<DeviceId> driverConfigured = new CopyOnWriteArraySet<>(); |
| 186 | private Set<DeviceId> pipelineConfigured = new CopyOnWriteArraySet<>(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 187 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 188 | private final Map<DeviceId, DeviceHandshaker> handshakers = Maps.newConcurrentMap(); |
| 189 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 190 | |
| 191 | protected ScheduledExecutorService connectionExecutor |
| 192 | = newScheduledThreadPool(CORE_POOL_SIZE, |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 193 | groupedThreads("onos/generaldeviceprovider-device", |
| 194 | "connection-executor-%d", log)); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 195 | protected ScheduledExecutorService portStatsExecutor |
| 196 | = newScheduledThreadPool(CORE_POOL_SIZE, |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 197 | groupedThreads("onos/generaldeviceprovider-port-stats", |
| 198 | "port-stats-executor-%d", log)); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 199 | protected ScheduledExecutorService availabilityCheckExecutor |
| 200 | = newScheduledThreadPool(CORE_POOL_SIZE, |
| 201 | groupedThreads("onos/generaldeviceprovider-availability-check", |
| 202 | "availability-check-executor-%d", log)); |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 203 | protected ConcurrentMap<DeviceId, ScheduledFuture<?>> scheduledTasks = new ConcurrentHashMap<>(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 204 | |
| 205 | protected DeviceProviderService providerService; |
| 206 | private InternalDeviceListener deviceListener = new InternalDeviceListener(); |
| 207 | |
| 208 | protected final ConfigFactory factory = |
| 209 | new ConfigFactory<DeviceId, GeneralProviderDeviceConfig>( |
| 210 | SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 211 | GeneralProviderDeviceConfig.class, CFG_SCHEME) { |
| 212 | @Override |
| 213 | public GeneralProviderDeviceConfig createConfig() { |
| 214 | return new GeneralProviderDeviceConfig(); |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | protected final NetworkConfigListener cfgListener = new InternalNetworkConfigListener(); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 219 | private ChannelListener channelListener = new InternalChannelListener(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 220 | |
| 221 | |
| 222 | @Activate |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 223 | public void activate(ComponentContext context) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 224 | providerService = providerRegistry.register(this); |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 225 | componentConfigService.registerProperties(getClass()); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 226 | coreService.registerApplication(APP_NAME); |
| 227 | cfgService.registerConfigFactory(factory); |
| 228 | cfgService.addListener(cfgListener); |
| 229 | deviceService.addListener(deviceListener); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 230 | handshakers.clear(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 231 | //This will fail if ONOS has CFG and drivers which depend on this provider |
| 232 | // are activated, failing due to not finding the driver. |
| 233 | cfgService.getSubjects(DeviceId.class, GeneralProviderDeviceConfig.class) |
| 234 | .forEach(did -> connectionExecutor.execute(() -> connectDevice(did))); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 235 | //Initiating a periodic check to see if any device is available again and reconnect it. |
| 236 | availabilityCheckExecutor.scheduleAtFixedRate(this::scheduleDevicePolling, deviceAvailabilityPollFrequency, |
| 237 | deviceAvailabilityPollFrequency, TimeUnit.SECONDS); |
| 238 | modified(context); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 239 | log.info("Started"); |
| 240 | } |
| 241 | |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 242 | @Modified |
| 243 | public void modified(ComponentContext context) { |
| 244 | if (context != null) { |
| 245 | Dictionary<?, ?> properties = context.getProperties(); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 246 | pollFrequency = Tools.getIntegerProperty(properties, POLL_FREQUENCY, |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 247 | DEFAULT_POLL_FREQUENCY_SECONDS); |
| 248 | log.info("Configured. Poll frequency is configured to {} seconds", pollFrequency); |
| 249 | } |
| 250 | |
| 251 | if (!scheduledTasks.isEmpty()) { |
| 252 | //cancel all previous tasks |
| 253 | scheduledTasks.values().forEach(task -> task.cancel(false)); |
| 254 | //resubmit task with new timeout. |
| 255 | Set<DeviceId> deviceSubjects = |
| 256 | cfgService.getSubjects(DeviceId.class, GeneralProviderDeviceConfig.class); |
| 257 | deviceSubjects.forEach(deviceId -> { |
| 258 | if (!compareScheme(deviceId)) { |
| 259 | // not under my scheme, skipping |
| 260 | log.debug("{} is not my scheme, skipping", deviceId); |
| 261 | return; |
| 262 | } |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 263 | scheduledTasks.put(deviceId, scheduleStatistcsPolling(deviceId, true)); |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 264 | }); |
| 265 | } |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 268 | @Deactivate |
| 269 | public void deactivate() { |
| 270 | portStatsExecutor.shutdown(); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 271 | availabilityCheckExecutor.shutdown(); |
Andrea Campanella | 4929a81 | 2017-10-09 18:38:23 +0200 | [diff] [blame] | 272 | componentConfigService.unregisterProperties(getClass(), false); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 273 | cfgService.removeListener(cfgListener); |
| 274 | //Not Removing the device so they can still be used from other driver providers |
| 275 | //cfgService.getSubjects(DeviceId.class, GeneralProviderDeviceConfig.class) |
| 276 | // .forEach(did -> connectionExecutor.execute(() -> disconnectDevice(did))); |
| 277 | connectionExecutor.shutdown(); |
| 278 | deviceService.removeListener(deviceListener); |
| 279 | providerRegistry.unregister(this); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 280 | handshakers.clear(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 281 | providerService = null; |
| 282 | cfgService.unregisterConfigFactory(factory); |
| 283 | log.info("Stopped"); |
| 284 | } |
| 285 | |
| 286 | public GeneralDeviceProvider() { |
| 287 | super(new ProviderId(URI_SCHEME, DEVICE_PROVIDER_PACKAGE)); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | @Override |
| 292 | public void triggerProbe(DeviceId deviceId) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 293 | // TODO Really don't see the point of this in non OF Context, |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 294 | // for now testing reachability, can be moved to no-op |
| 295 | log.debug("Triggering probe equals testing reachability on device {}", deviceId); |
| 296 | isReachable(deviceId); |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 301 | log.info("Received role {} for device {}", newRole, deviceId); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 302 | CompletableFuture<MastershipRole> roleReply = getHandshaker(deviceId).roleChanged(newRole); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 303 | roleReply.thenAcceptAsync(mastership -> { |
| 304 | providerService.receivedRoleReply(deviceId, newRole, mastership); |
| 305 | if (!mastership.equals(MastershipRole.MASTER) && scheduledTasks.get(deviceId) != null) { |
| 306 | scheduledTasks.get(deviceId).cancel(false); |
| 307 | scheduledTasks.remove(deviceId); |
| 308 | } else if (mastership.equals(MastershipRole.MASTER) && scheduledTasks.get(deviceId) == null) { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 309 | scheduledTasks.put(deviceId, scheduleStatistcsPolling(deviceId, false)); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 310 | updatePortStatistics(deviceId); |
| 311 | } |
| 312 | }); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public boolean isReachable(DeviceId deviceId) { |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 317 | log.debug("Testing reachability for device {}", deviceId); |
Andrea Campanella | c1ecdd0 | 2018-01-12 12:48:24 +0100 | [diff] [blame] | 318 | |
| 319 | DeviceHandshaker handshaker = getHandshaker(deviceId); |
| 320 | if (handshaker == null) { |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | CompletableFuture<Boolean> reachable = handshaker.isReachable(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 325 | try { |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 326 | return reachable.get(REACHABILITY_TIMEOUT, TimeUnit.SECONDS); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 327 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 328 | log.warn("Device {} is not reachable {}", deviceId, e.getMessage()); |
| 329 | log.debug("Exception", e); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 336 | boolean enable) { |
| 337 | if (deviceService.getDevice(deviceId).is(PortAdmin.class)) { |
| 338 | |
| 339 | PortAdmin portAdmin = getPortAdmin(deviceId); |
| 340 | CompletableFuture<Boolean> modified; |
| 341 | if (enable) { |
| 342 | modified = portAdmin.enable(portNumber); |
| 343 | } else { |
| 344 | modified = portAdmin.disable(portNumber); |
| 345 | } |
| 346 | modified.thenAcceptAsync(result -> { |
| 347 | if (!result) { |
| 348 | log.warn("Your device {} port {} status can't be changed to {}", |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 349 | deviceId, portNumber, enable); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 350 | } |
| 351 | }); |
| 352 | |
| 353 | } else { |
| 354 | log.warn("Device {} does not support PortAdmin behaviour", deviceId); |
| 355 | } |
| 356 | } |
| 357 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 358 | @Override |
| 359 | public void triggerDisconnect(DeviceId deviceId) { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 360 | log.debug("Triggering disconnection of device {}", deviceId); |
| 361 | connectionExecutor.execute(() -> { |
| 362 | disconnectDevice(deviceId).whenComplete((success, ex) -> { |
| 363 | checkAndConnect(deviceId); |
| 364 | }); |
| 365 | }); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 368 | private DeviceHandshaker getHandshaker(DeviceId deviceId) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 369 | return handshakers.computeIfAbsent(deviceId, id -> { |
| 370 | Driver driver = getDriver(deviceId); |
| 371 | return driver == null ? null : |
| 372 | getBehaviour(driver, DeviceHandshaker.class, |
| 373 | new DefaultDriverData(driver, deviceId)); |
| 374 | }); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | private PortAdmin getPortAdmin(DeviceId deviceId) { |
| 378 | Driver driver = getDriver(deviceId); |
| 379 | return getBehaviour(driver, PortAdmin.class, |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 380 | new DefaultDriverData(driver, deviceId)); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 381 | |
| 382 | } |
| 383 | |
| 384 | private Driver getDriver(DeviceId deviceId) { |
Andrea Campanella | c1ecdd0 | 2018-01-12 12:48:24 +0100 | [diff] [blame] | 385 | Driver driver = null; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 386 | try { |
| 387 | driver = driverService.getDriver(deviceId); |
| 388 | } catch (ItemNotFoundException e) { |
| 389 | log.debug("Falling back to configuration to fetch driver " + |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 390 | "for device {}", deviceId); |
Andrea Campanella | c1ecdd0 | 2018-01-12 12:48:24 +0100 | [diff] [blame] | 391 | BasicDeviceConfig cfg = cfgService.getConfig(deviceId, BasicDeviceConfig.class); |
| 392 | if (cfg != null) { |
| 393 | driver = driverService.getDriver(cfg.driver()); |
| 394 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 395 | } |
| 396 | return driver; |
| 397 | } |
| 398 | |
| 399 | //needed since the device manager will not return the driver through implementation() |
| 400 | // method since the device is not pushed to the core so for the connectDevice |
| 401 | // we need to work around that in order to test before calling |
| 402 | // store.createOrUpdateDevice |
| 403 | private <T extends Behaviour> T getBehaviour(Driver driver, Class<T> type, |
| 404 | DriverData data) { |
Andrea Campanella | c1ecdd0 | 2018-01-12 12:48:24 +0100 | [diff] [blame] | 405 | if (driver != null && driver.hasBehaviour(type)) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 406 | DefaultDriverHandler handler = new DefaultDriverHandler(data); |
| 407 | return driver.createBehaviour(handler, type); |
| 408 | } else { |
| 409 | return null; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | //Connects a general device |
| 414 | private void connectDevice(DeviceId deviceId) { |
| 415 | //retrieve the configuration |
| 416 | GeneralProviderDeviceConfig providerConfig = |
| 417 | cfgService.getConfig(deviceId, GeneralProviderDeviceConfig.class); |
| 418 | BasicDeviceConfig basicDeviceConfig = |
| 419 | cfgService.getConfig(deviceId, BasicDeviceConfig.class); |
| 420 | |
| 421 | if (providerConfig == null || basicDeviceConfig == null) { |
| 422 | log.error("Configuration is NULL: basic config {}, general provider " + |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 423 | "config {}", basicDeviceConfig, providerConfig); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 424 | } else { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 425 | log.info("Connecting to device {} with driver {}", deviceId, basicDeviceConfig.driver()); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 426 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 427 | DeviceHandshaker handshaker = getHandshaker(deviceId); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 428 | if (handshaker == null) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 429 | log.error("Device {} does not support DeviceHandshaker behaviour", deviceId); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 430 | return; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 431 | } |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 432 | Driver driver = handshaker.handler().driver(); |
Frank Wang | 554ce97 | 2017-09-06 09:56:43 +0800 | [diff] [blame] | 433 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 434 | addConfigData(providerConfig, handshaker.data()); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 435 | |
| 436 | //Connecting to the device |
| 437 | CompletableFuture<Boolean> connected = handshaker.connect(); |
| 438 | |
| 439 | connected.thenAcceptAsync(result -> { |
| 440 | if (result) { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 441 | handshaker.addChannelListener(channelListener); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 442 | //Populated with the default values obtained by the driver |
| 443 | ChassisId cid = new ChassisId(); |
| 444 | SparseAnnotations annotations = DefaultAnnotations.builder() |
| 445 | .set(AnnotationKeys.PROTOCOL, |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 446 | providerConfig.protocolsInfo().keySet().toString()) |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 447 | .build(); |
| 448 | DeviceDescription description = |
| 449 | new DefaultDeviceDescription(deviceId.uri(), Device.Type.SWITCH, |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 450 | driver.manufacturer(), driver.hwVersion(), |
| 451 | driver.swVersion(), UNKNOWN, |
Yi Tseng | 92494fb | 2017-12-05 15:14:53 -0800 | [diff] [blame] | 452 | cid, true, annotations); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 453 | //Empty list of ports |
| 454 | List<PortDescription> ports = new ArrayList<>(); |
| 455 | |
Andrea Campanella | bf9e5ce | 2017-12-06 14:26:36 +0100 | [diff] [blame] | 456 | DeviceDescriptionDiscovery deviceDiscovery = getBehaviour(driver, |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 457 | DeviceDescriptionDiscovery.class, handshaker.data()); |
Andrea Campanella | bf9e5ce | 2017-12-06 14:26:36 +0100 | [diff] [blame] | 458 | if (deviceDiscovery != null) { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 459 | DeviceDescription newdescription = deviceDiscovery.discoverDeviceDetails(); |
| 460 | if (newdescription != null) { |
| 461 | description = newdescription; |
| 462 | } |
| 463 | ports = deviceDiscovery.discoverPortDetails(); |
Andrea Campanella | bf9e5ce | 2017-12-06 14:26:36 +0100 | [diff] [blame] | 464 | } else { |
| 465 | log.info("No Device Description Discovery for device {}, no update for " + |
| 466 | "description or ports.", deviceId); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 467 | } |
| 468 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 469 | if (!handlePipeconf(deviceId, driver, handshaker.data(), true)) { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 470 | // Something went wrong during handling of pipeconf. |
| 471 | // We already logged the error. |
| 472 | handshaker.disconnect(); |
| 473 | return; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 474 | } |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 475 | advertiseDevice(deviceId, description, ports); |
| 476 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 477 | } else { |
| 478 | log.warn("Can't connect to device {}", deviceId); |
| 479 | } |
| 480 | }); |
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 | private void connectStandbyDevice(DeviceId deviceId) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 485 | // if device is pipeline programmable we merge pipeconf + base driver for every other role |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 486 | GeneralProviderDeviceConfig providerConfig = |
| 487 | cfgService.getConfig(deviceId, GeneralProviderDeviceConfig.class); |
| 488 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 489 | DeviceHandshaker handshaker = getHandshaker(deviceId); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 490 | if (handshaker == null) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 491 | log.error("Device {} does not support DeviceHandshaker behaviour", deviceId); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 492 | return; |
| 493 | } |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 494 | addConfigData(providerConfig, handshaker.data()); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 495 | |
| 496 | //Connecting to the device |
| 497 | handshaker.connect().thenAcceptAsync(result -> { |
| 498 | if (result) { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 499 | handshaker.addChannelListener(channelListener); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 500 | handlePipeconf(deviceId, handshaker.handler().driver(), handshaker.data(), false); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 501 | } |
| 502 | }); |
| 503 | } |
| 504 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 505 | /** |
| 506 | * Handles the case of a device that is pipeline programmable. Returns true if the operation wa successful and the |
| 507 | * device can be registered to the core, false otherwise. |
| 508 | */ |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 509 | private boolean handlePipeconf(DeviceId deviceId, Driver driver, DriverData driverData, boolean deployPipeconf) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 510 | PiPipelineProgrammable pipelineProg = |
| 511 | getBehaviour(driver, PiPipelineProgrammable.class, driverData); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 512 | |
| 513 | if (pipelineProg == null) { |
| 514 | // Device is not pipeline programmable. |
| 515 | return true; |
| 516 | } |
| 517 | |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 518 | PiPipeconf pipeconf = getPipeconf(deviceId, pipelineProg); |
| 519 | |
| 520 | if (pipeconf != null) { |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 521 | PiPipeconfId pipeconfId = pipeconf.id(); |
| 522 | |
| 523 | try { |
| 524 | if (deployPipeconf) { |
| 525 | if (!pipelineProg.deployPipeconf(pipeconf).get()) { |
| 526 | log.error("Unable to deploy pipeconf {} to {}, aborting device discovery", |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 527 | pipeconfId, deviceId); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 528 | return false; |
| 529 | } |
| 530 | } |
| 531 | } catch (InterruptedException | ExecutionException e) { |
| 532 | log.warn("Exception occurred while deploying pipeconf {} to device {}", pipeconf.id(), deviceId, e); |
| 533 | return false; |
| 534 | } |
| 535 | try { |
| 536 | if (!piPipeconfService.bindToDevice(pipeconfId, deviceId).get()) { |
| 537 | log.error("Unable to merge driver {} for device {} with pipeconf {}, aborting device discovery", |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 538 | driver.name(), deviceId, pipeconfId); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 539 | return false; |
| 540 | } |
| 541 | } catch (InterruptedException | ExecutionException e) { |
| 542 | log.warn("Exception occurred while binding pipeconf {} to device {}", pipeconf.id(), deviceId, e); |
| 543 | return false; |
| 544 | } |
| 545 | } else { |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | return true; |
| 550 | } |
| 551 | |
| 552 | private PiPipeconf getPipeconf(DeviceId deviceId, PiPipelineProgrammable pipelineProg) { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 553 | PiPipeconfId pipeconfId = piPipeconfService.ofDevice(deviceId).orElseGet(() -> { |
| 554 | // No pipeconf has been associated with this device. |
| 555 | // Check if device driver provides a default one. |
| 556 | if (pipelineProg.getDefaultPipeconf().isPresent()) { |
| 557 | PiPipeconf defaultPipeconf = pipelineProg.getDefaultPipeconf().get(); |
| 558 | log.info("Using default pipeconf {} for {}", defaultPipeconf.id(), deviceId); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 559 | return defaultPipeconf.id(); |
| 560 | } else { |
| 561 | return null; |
| 562 | } |
| 563 | }); |
| 564 | |
| 565 | if (pipeconfId == null) { |
| 566 | log.warn("Device {} is pipeline programmable but no pipeconf can be associated to it.", deviceId); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 567 | return null; |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | if (!piPipeconfService.getPipeconf(pipeconfId).isPresent()) { |
| 571 | log.warn("Pipeconf {} is not registered", pipeconfId); |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 572 | return null; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 573 | } |
| 574 | |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 575 | return piPipeconfService.getPipeconf(pipeconfId).get(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 576 | } |
| 577 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 578 | private void advertiseDevice(DeviceId deviceId, DeviceDescription description, List<PortDescription> ports) { |
| 579 | providerService.deviceConnected(deviceId, description); |
| 580 | providerService.updatePorts(deviceId, ports); |
| 581 | } |
| 582 | |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 583 | private CompletableFuture<Boolean> disconnectDevice(DeviceId deviceId) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 584 | log.info("Disconnecting for device {}", deviceId); |
Andrea Campanella | c1ecdd0 | 2018-01-12 12:48:24 +0100 | [diff] [blame] | 585 | |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 586 | CompletableFuture<Boolean> disconnectError = new CompletableFuture<>(); |
| 587 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 588 | DeviceHandshaker handshaker = handshakers.remove(deviceId); |
| 589 | if (handshaker != null) { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 590 | handshaker.disconnect().thenAcceptAsync(result -> { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 591 | if (result) { |
| 592 | log.info("Disconnected device {}", deviceId); |
| 593 | providerService.deviceDisconnected(deviceId); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 594 | disconnectError.complete(true); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 595 | } else { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 596 | disconnectError.complete(false); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 597 | log.warn("Device {} was unable to disconnect", deviceId); |
| 598 | } |
| 599 | }); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 600 | } else { |
| 601 | //gracefully ignoring. |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 602 | log.warn("No DeviceHandshaker for device {}, no guarantees of complete " + |
| 603 | "shutdown of communication", deviceId); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 604 | disconnectError.complete(false); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 605 | } |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 606 | ScheduledFuture<?> pollingStatisticsTask = scheduledTasks.get(deviceId); |
| 607 | if (pollingStatisticsTask != null) { |
| 608 | pollingStatisticsTask.cancel(true); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 609 | scheduledTasks.remove(deviceId); |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 610 | } |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 611 | return disconnectError; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | //Needed to catch the exception in the executors since are not rethrown otherwise. |
| 615 | private Runnable exceptionSafe(Runnable runnable) { |
| 616 | return () -> { |
| 617 | try { |
| 618 | runnable.run(); |
| 619 | } catch (Exception e) { |
| 620 | log.error("Unhandled Exception", e); |
| 621 | } |
| 622 | }; |
| 623 | } |
| 624 | |
| 625 | private void updatePortStatistics(DeviceId deviceId) { |
Andrea Campanella | ce11193 | 2017-09-18 16:59:56 +0900 | [diff] [blame] | 626 | Device device = deviceService.getDevice(deviceId); |
| 627 | if (!Objects.isNull(device) && deviceService.isAvailable(deviceId) && |
| 628 | device.is(PortStatisticsDiscovery.class)) { |
| 629 | Collection<PortStatistics> statistics = device.as(PortStatisticsDiscovery.class) |
| 630 | .discoverPortStatistics(); |
| 631 | //updating statistcs only if not empty |
| 632 | if (!statistics.isEmpty()) { |
| 633 | providerService.updatePortStatistics(deviceId, statistics); |
| 634 | } |
| 635 | } else { |
| 636 | log.debug("Can't update port statistics for device {}", deviceId); |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
| 640 | private boolean compareScheme(DeviceId deviceId) { |
| 641 | return deviceId.uri().getScheme().equals(URI_SCHEME); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Listener for configuration events. |
| 646 | */ |
| 647 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 648 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 649 | @Override |
| 650 | public void event(NetworkConfigEvent event) { |
| 651 | DeviceId deviceId = (DeviceId) event.subject(); |
| 652 | //Assuming that the deviceId comes with uri 'device:' |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 653 | if (!compareScheme(deviceId)) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 654 | // not under my scheme, skipping |
| 655 | log.debug("{} is not my scheme, skipping", deviceId); |
| 656 | return; |
| 657 | } |
Andrea Campanella | ce11193 | 2017-09-18 16:59:56 +0900 | [diff] [blame] | 658 | if (deviceService.getDevice(deviceId) != null && deviceService.isAvailable(deviceId)) { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 659 | log.info("Device {} is already connected to ONOS and is available", deviceId); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 660 | return; |
| 661 | } |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 662 | NodeId leaderNodeId = leadershipService.runForLeadership(DEPLOY + deviceId.toString() + PIPECONF_TOPIC) |
| 663 | .leader().nodeId(); |
| 664 | NodeId localNodeId = clusterService.getLocalNode().id(); |
| 665 | if (localNodeId.equals(leaderNodeId)) { |
| 666 | if (processEvent(event, deviceId)) { |
| 667 | log.debug("{} is leader for {}, initiating the connection and deploying pipeline", leaderNodeId, |
| 668 | deviceId); |
| 669 | checkAndSubmitDeviceTask(deviceId); |
| 670 | } |
| 671 | } else { |
| 672 | if (processEvent(event, deviceId)) { |
| 673 | log.debug("{} is not leader for {}, initiating connection but not deploying pipeline, {} is LEADER", |
| 674 | localNodeId, deviceId, leaderNodeId); |
| 675 | connectionExecutor.submit(exceptionSafe(() -> connectStandbyDevice(deviceId))); |
| 676 | //FIXME this will be removed when config is synced |
| 677 | cleanUpConfigInfo(deviceId); |
| 678 | } |
| 679 | } |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | private boolean processEvent(NetworkConfigEvent event, DeviceId deviceId) { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 683 | //FIXME to be removed when netcfg will issue device events in a bundle or |
| 684 | // ensure all configuration needed is present |
| 685 | Lock lock = ENTRY_LOCKS.computeIfAbsent(deviceId, key -> new ReentrantLock()); |
| 686 | lock.lock(); |
| 687 | try { |
| 688 | if (event.configClass().equals(GeneralProviderDeviceConfig.class)) { |
| 689 | //FIXME we currently assume that p4runtime devices are pipeline configurable. |
| 690 | //If we want to connect a p4runtime device with no pipeline |
| 691 | if (event.config().isPresent() && |
| 692 | Collections.disjoint(ImmutableSet.copyOf(event.config().get().node().fieldNames()), |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 693 | PIPELINE_CONFIGURABLE_PROTOCOLS)) { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 694 | pipelineConfigured.add(deviceId); |
| 695 | } |
| 696 | deviceConfigured.add(deviceId); |
| 697 | } else if (event.configClass().equals(BasicDeviceConfig.class)) { |
| 698 | if (event.config().isPresent() && event.config().get().node().has(DRIVER)) { |
| 699 | //TODO add check for pipeline and add it to the pipeline list if no |
| 700 | // p4runtime is present. |
| 701 | driverConfigured.add(deviceId); |
| 702 | } |
| 703 | } else if (event.configClass().equals(PiPipeconfConfig.class)) { |
| 704 | if (event.config().isPresent() |
| 705 | && event.config().get().node().has(PiPipeconfConfig.PIPIPECONFID)) { |
| 706 | pipelineConfigured.add(deviceId); |
| 707 | } |
| 708 | } |
| 709 | //if the device has no "pipeline configurable protocol it will be present |
| 710 | // in the pipelineConfigured |
| 711 | if (deviceConfigured.contains(deviceId) && driverConfigured.contains(deviceId) |
| 712 | && pipelineConfigured.contains(deviceId)) { |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 713 | return true; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 714 | } else { |
| 715 | if (deviceConfigured.contains(deviceId) && driverConfigured.contains(deviceId)) { |
| 716 | log.debug("Waiting for pipeline configuration for device {}", deviceId); |
| 717 | } else if (pipelineConfigured.contains(deviceId) && driverConfigured.contains(deviceId)) { |
| 718 | log.debug("Waiting for device configuration for device {}", deviceId); |
| 719 | } else if (pipelineConfigured.contains(deviceId) && deviceConfigured.contains(deviceId)) { |
| 720 | log.debug("Waiting for driver configuration for device {}", deviceId); |
| 721 | } else if (driverConfigured.contains(deviceId)) { |
| 722 | log.debug("Only driver configuration for device {}", deviceId); |
| 723 | } else if (deviceConfigured.contains(deviceId)) { |
| 724 | log.debug("Only device configuration for device {}", deviceId); |
| 725 | } |
| 726 | } |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 727 | return false; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 728 | } finally { |
| 729 | lock.unlock(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
| 733 | @Override |
| 734 | public boolean isRelevant(NetworkConfigEvent event) { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 735 | return (event.configClass().equals(GeneralProviderDeviceConfig.class) || |
| 736 | event.configClass().equals(BasicDeviceConfig.class) || |
| 737 | event.configClass().equals(PiPipeconfConfig.class)) && |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 738 | (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 739 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED); |
| 740 | } |
| 741 | } |
| 742 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 743 | private void checkAndSubmitDeviceTask(DeviceId deviceId) { |
| 744 | connectionExecutor.submit(exceptionSafe(() -> connectDevice(deviceId))); |
| 745 | //FIXME this will be removed when configuration is synced. |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 746 | cleanUpConfigInfo(deviceId); |
| 747 | |
| 748 | } |
| 749 | |
| 750 | private void addConfigData(GeneralProviderDeviceConfig providerConfig, DriverData driverData) { |
| 751 | //Storing deviceKeyId and all other config values |
| 752 | // as data in the driver with protocol_<info> |
| 753 | // name as the key. e.g protocol_ip |
| 754 | providerConfig.protocolsInfo() |
| 755 | .forEach((protocol, deviceInfoConfig) -> { |
| 756 | deviceInfoConfig.configValues() |
| 757 | .forEach((k, v) -> driverData.set(protocol + "_" + k, v)); |
| 758 | driverData.set(protocol + "_key", deviceInfoConfig.deviceKeyId()); |
| 759 | }); |
| 760 | } |
| 761 | |
| 762 | private void cleanUpConfigInfo(DeviceId deviceId) { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 763 | deviceConfigured.remove(deviceId); |
| 764 | driverConfigured.remove(deviceId); |
| 765 | pipelineConfigured.remove(deviceId); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 768 | private ScheduledFuture<?> scheduleStatistcsPolling(DeviceId deviceId, boolean randomize) { |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 769 | int delay = 0; |
| 770 | if (randomize) { |
Ray Milkey | b68bbbc | 2017-12-18 10:05:49 -0800 | [diff] [blame] | 771 | delay = new SecureRandom().nextInt(10); |
Andrea Campanella | 1909032 | 2017-08-22 10:31:37 +0200 | [diff] [blame] | 772 | } |
| 773 | return portStatsExecutor.scheduleAtFixedRate( |
| 774 | exceptionSafe(() -> updatePortStatistics(deviceId)), |
| 775 | delay, pollFrequency, TimeUnit.SECONDS); |
| 776 | } |
| 777 | |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 778 | private void scheduleDevicePolling() { |
| 779 | cfgService.getSubjects(DeviceId.class, GeneralProviderDeviceConfig.class).forEach(this::checkAndConnect); |
| 780 | } |
| 781 | |
| 782 | private void checkAndConnect(DeviceId deviceId) { |
| 783 | // Let's try and reconnect to a device which is stored in the net-cfg. |
| 784 | // One of the following conditions must be satisfied: |
| 785 | // 1) device is null in the store meaning that is was never connected or it was administratively removed |
| 786 | // 2) the device is not available and there is no MASTER instance, meaning the device lost |
| 787 | // it's connection to ONOS at some point in the past. |
| 788 | // We also check that the general device provider config and the driver config are present. |
| 789 | // We do not check for reachability using isReachable(deviceId) since the behaviour of this method |
| 790 | // can vary depending on protocol nuances. We leave this check to the device handshaker |
| 791 | // at later stages of the connection process. |
| 792 | // IF the conditions are not met but instead the device is present in the store, available and this instance is |
| 793 | // MASTER but is not reachable we remove it from the store. |
| 794 | |
| 795 | if ((deviceService.getDevice(deviceId) == null || (!deviceService.isAvailable(deviceId) && |
| 796 | mastershipService.getMasterFor(deviceId) == null)) && configIsPresent(deviceId)) { |
| 797 | log.debug("Trying to re-connect to device {}", deviceId); |
| 798 | NodeId leaderNodeId = leadershipService.runForLeadership(CHECK + deviceId.toString() + CONNECTION) |
| 799 | .leader().nodeId(); |
| 800 | NodeId localNodeId = clusterService.getLocalNode().id(); |
| 801 | if (localNodeId.equals(leaderNodeId)) { |
| 802 | log.debug("{} is leader for {}, initiating the connection", leaderNodeId, |
| 803 | deviceId); |
| 804 | checkAndSubmitDeviceTask(deviceId); |
| 805 | } else { |
| 806 | log.debug("{} is not leader for {}, initiating connection, {} is LEADER", |
| 807 | localNodeId, deviceId, leaderNodeId); |
| 808 | connectionExecutor.submit(exceptionSafe(() -> connectStandbyDevice(deviceId))); |
| 809 | //FIXME this will be removed when config is synced |
| 810 | cleanUpConfigInfo(deviceId); |
| 811 | } |
| 812 | } else if ((deviceService.getDevice(deviceId) != null && deviceService.isAvailable(deviceId) |
| 813 | && mastershipService.isLocalMaster(deviceId) && !isReachable(deviceId) && configIsPresent(deviceId))) { |
| 814 | log.info("Removing available but unreachable device {}", deviceId); |
| 815 | disconnectDevice(deviceId); |
| 816 | providerService.deviceDisconnected(deviceId); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | private boolean configIsPresent(DeviceId deviceId) { |
| 821 | boolean present = cfgService.getConfig(deviceId, GeneralProviderDeviceConfig.class) != null |
| 822 | && cfgService.getConfig(deviceId, BasicDeviceConfig.class) != null; |
| 823 | if (!present) { |
| 824 | log.warn("Configuration for device {} is not complete", deviceId); |
| 825 | } |
| 826 | return present; |
| 827 | } |
| 828 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 829 | /** |
| 830 | * Listener for core device events. |
| 831 | */ |
| 832 | private class InternalDeviceListener implements DeviceListener { |
| 833 | @Override |
| 834 | public void event(DeviceEvent event) { |
Andrea Campanella | ce11193 | 2017-09-18 16:59:56 +0900 | [diff] [blame] | 835 | DeviceId deviceId = event.subject().id(); |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 836 | // FIXME handling for mastership change scenario missing? |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 837 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 838 | // For now this is scheduled periodically, when streaming API will |
| 839 | // be available we check and base it on the streaming API (e.g. gNMI) |
| 840 | if (mastershipService.isLocalMaster(deviceId)) { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 841 | scheduledTasks.put(deviceId, scheduleStatistcsPolling(deviceId, false)); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
| 845 | @Override |
| 846 | public boolean isRelevant(DeviceEvent event) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 847 | return event.type() == Type.DEVICE_ADDED && |
| 848 | event.subject().id().toString().startsWith(URI_SCHEME.toLowerCase()); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 851 | |
| 852 | /** |
| 853 | * Listener for device channel events. |
| 854 | */ |
| 855 | private class InternalChannelListener implements ChannelListener { |
| 856 | |
| 857 | @Override |
| 858 | public void event(ChannelEvent event) { |
| 859 | DeviceId deviceId = event.subject(); |
| 860 | if (event.type().equals(ChannelEvent.Type.CHANNEL_DISCONNECTED)) { |
| 861 | //let's properly handle device disconnection |
| 862 | CompletableFuture<Boolean> disconnection = disconnectDevice(deviceId); |
| 863 | disconnection.thenAcceptAsync(result -> { |
| 864 | //If master notifying of disconnection to the core. |
| 865 | if (mastershipService.isLocalMaster(deviceId)) { |
| 866 | log.info("Disconnecting unreachable device {}, due to error on channel", deviceId); |
| 867 | providerService.deviceDisconnected(deviceId); |
| 868 | } |
| 869 | }); |
| 870 | |
| 871 | } |
| 872 | //TODO evaluate other type of reactions. |
| 873 | } |
| 874 | |
| 875 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 876 | } |