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