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