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