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