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