Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [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 | bc112a9 | 2017-06-26 19:06:43 +0200 | [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.net.pi.impl; |
| 18 | |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 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; |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
| 22 | import com.google.common.util.concurrent.Striped; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 23 | import org.onlab.util.ItemNotFoundException; |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 24 | import org.onlab.util.SharedExecutors; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 26 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 27 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 28 | import org.onosproject.net.driver.Behaviour; |
| 29 | import org.onosproject.net.driver.DefaultDriver; |
| 30 | import org.onosproject.net.driver.Driver; |
| 31 | import org.onosproject.net.driver.DriverAdminService; |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 32 | import org.onosproject.net.driver.DriverEvent; |
| 33 | import org.onosproject.net.driver.DriverListener; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 34 | import org.onosproject.net.driver.DriverProvider; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 35 | import org.onosproject.net.pi.model.PiPipeconf; |
| 36 | import org.onosproject.net.pi.model.PiPipeconfId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 37 | import org.onosproject.net.pi.service.PiPipeconfMappingStore; |
| 38 | import org.onosproject.net.pi.service.PiPipeconfService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 39 | import org.osgi.service.component.annotations.Activate; |
| 40 | import org.osgi.service.component.annotations.Component; |
| 41 | import org.osgi.service.component.annotations.Deactivate; |
| 42 | import org.osgi.service.component.annotations.Reference; |
| 43 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 44 | import org.slf4j.Logger; |
| 45 | |
| 46 | import java.util.HashMap; |
| 47 | import java.util.Map; |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 48 | import java.util.Objects; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 49 | import java.util.Optional; |
| 50 | import java.util.Set; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 51 | import java.util.concurrent.ConcurrentHashMap; |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 52 | import java.util.concurrent.ConcurrentMap; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 53 | import java.util.concurrent.ExecutorService; |
| 54 | import java.util.concurrent.Executors; |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 55 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 56 | import java.util.concurrent.locks.Lock; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 57 | |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 58 | import static java.lang.String.format; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 59 | import static org.onlab.util.Tools.groupedThreads; |
| 60 | import static org.slf4j.LoggerFactory.getLogger; |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Implementation of the PiPipeconfService. |
| 65 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 66 | @Component(immediate = true, service = PiPipeconfService.class) |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 67 | @Beta |
| 68 | public class PiPipeconfManager implements PiPipeconfService { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 69 | |
| 70 | private final Logger log = getLogger(getClass()); |
| 71 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 72 | private static final String MERGED_DRIVER_SEPARATOR = ":"; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 73 | |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 74 | private static final int MISSING_DRIVER_WATCHDOG_INTERVAL = 5; // Seconds. |
| 75 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 77 | protected NetworkConfigRegistry cfgService; |
| 78 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 80 | protected DriverAdminService driverAdminService; |
| 81 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 83 | private PiPipeconfMappingStore pipeconfMappingStore; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 84 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 85 | // Registered pipeconf are replicated through the app subsystem and |
| 86 | // registered on app activated events. Hence, there should be no need of |
| 87 | // distributing this map. |
| 88 | protected ConcurrentMap<PiPipeconfId, PiPipeconf> pipeconfs = new ConcurrentHashMap<>(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 89 | |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 90 | private final DriverListener driverListener = new InternalDriverListener(); |
| 91 | private final Set<String> missingMergedDrivers = Sets.newCopyOnWriteArraySet(); |
| 92 | private final Striped<Lock> locks = Striped.lock(20); |
| 93 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 94 | protected ExecutorService executor = Executors.newFixedThreadPool( |
| 95 | 10, groupedThreads("onos/pipeconf-manager", "%d", log)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 96 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 97 | @Activate |
| 98 | public void activate() { |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 99 | driverAdminService.addListener(driverListener); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 100 | checkMissingMergedDrivers(); |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 101 | if (!missingMergedDrivers.isEmpty()) { |
| 102 | // Missing drivers should be created upon detecting registration |
| 103 | // events of a new pipeconf or a base driver. If, for any reason, we |
| 104 | // miss such event, here's a watchdog task. |
| 105 | SharedExecutors.getPoolThreadExecutor() |
| 106 | .execute(this::missingDriversWatchdogTask); |
| 107 | } |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 108 | log.info("Started"); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | @Deactivate |
| 113 | public void deactivate() { |
| 114 | executor.shutdown(); |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 115 | driverAdminService.removeListener(driverListener); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 116 | pipeconfs.clear(); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 117 | missingMergedDrivers.clear(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 118 | cfgService = null; |
| 119 | driverAdminService = null; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 120 | log.info("Stopped"); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void register(PiPipeconf pipeconf) throws IllegalStateException { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 125 | if (pipeconfs.containsKey(pipeconf.id())) { |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 126 | throw new IllegalStateException(format("Pipeconf %s is already registered", pipeconf.id())); |
| 127 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 128 | pipeconfs.put(pipeconf.id(), pipeconf); |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 129 | log.info("New pipeconf registered: {}", pipeconf.id()); |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 130 | executor.execute(() -> attemptMergeAll(pipeconf.id())); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 134 | public void remove(PiPipeconfId pipeconfId) throws IllegalStateException { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 135 | // TODO add mechanism to remove from device. |
| 136 | if (!pipeconfs.containsKey(pipeconfId)) { |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 137 | throw new IllegalStateException(format("Pipeconf %s is not registered", pipeconfId)); |
| 138 | } |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 139 | // TODO remove the binding from the distributed Store when the lifecycle of a pipeconf is defined. |
| 140 | // pipeconfMappingStore.removeBindings(pipeconfId); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 141 | log.info("Removing pipeconf {}", pipeconfId); |
| 142 | pipeconfs.remove(pipeconfId); |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 146 | public Iterable<PiPipeconf> getPipeconfs() { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 147 | return pipeconfs.values(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Optional<PiPipeconf> getPipeconf(PiPipeconfId id) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 152 | return Optional.ofNullable(pipeconfs.get(id)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 156 | public Optional<PiPipeconf> getPipeconf(DeviceId deviceId) { |
| 157 | if (pipeconfMappingStore.getPipeconfId(deviceId) == null) { |
| 158 | return Optional.empty(); |
| 159 | } else { |
| 160 | return Optional.ofNullable(pipeconfs.get( |
| 161 | pipeconfMappingStore.getPipeconfId(deviceId))); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @Override |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 166 | public void bindToDevice(PiPipeconfId pipeconfId, DeviceId deviceId) { |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 167 | PiPipeconfId existingPipeconfId = pipeconfMappingStore.getPipeconfId(deviceId); |
| 168 | if (existingPipeconfId != null && !existingPipeconfId.equals(pipeconfId)) { |
| 169 | log.error("Cannot set binding for {} to {} as one already exists ({})", |
| 170 | deviceId, pipeconfId, existingPipeconfId); |
| 171 | return; |
| 172 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 173 | pipeconfMappingStore.createOrUpdateBinding(deviceId, pipeconfId); |
| 174 | } |
| 175 | |
| 176 | @Override |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 177 | public String getMergedDriver(DeviceId deviceId, PiPipeconfId pipeconfId) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 178 | log.debug("Starting device driver merge of {} with {}...", deviceId, pipeconfId); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 179 | final BasicDeviceConfig basicDeviceConfig = cfgService.getConfig( |
| 180 | deviceId, BasicDeviceConfig.class); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 181 | if (basicDeviceConfig == null) { |
| 182 | log.warn("Unable to get basic device config for {}, " + |
Tian Jian | 855b108 | 2018-10-23 16:22:47 +0800 | [diff] [blame] | 183 | "aborting pipeconf driver merge", deviceId); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 184 | return null; |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 185 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 186 | String baseDriverName = basicDeviceConfig.driver(); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 187 | if (baseDriverName == null) { |
| 188 | log.warn("Missing driver from basic device config for {}, " + |
| 189 | "cannot produce merged driver", deviceId); |
| 190 | return null; |
| 191 | } |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 192 | if (isMergedDriverName(baseDriverName)) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 193 | // The config already has driver name that is a merged one. We still |
| 194 | // need to make sure an instance of that merged driver is present in |
| 195 | // this node. |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 196 | log.debug("Base driver of {} ({}) is a merged one", |
| 197 | deviceId, baseDriverName); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 198 | baseDriverName = getBaseDriverNameFromMerged(baseDriverName); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 199 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 200 | |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 201 | return doMergeDriver(baseDriverName, pipeconfId); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public Optional<PiPipeconfId> ofDevice(DeviceId deviceId) { |
| 206 | return Optional.ofNullable(pipeconfMappingStore.getPipeconfId(deviceId)); |
| 207 | } |
| 208 | |
| 209 | private String doMergeDriver(String baseDriverName, PiPipeconfId pipeconfId) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 210 | final String newDriverName = mergedDriverName(baseDriverName, pipeconfId); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 211 | // Serialize per newDriverName, avoid creating duplicates. |
| 212 | locks.get(newDriverName).lock(); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 213 | try { |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 214 | // If merged driver exists already we don't create a new one. |
| 215 | if (getDriver(newDriverName) != null) { |
| 216 | return newDriverName; |
| 217 | } |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 218 | log.debug("Creating merged driver {}...", newDriverName); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 219 | final Driver mergedDriver = buildMergedDriver( |
| 220 | pipeconfId, baseDriverName, newDriverName); |
| 221 | if (mergedDriver == null) { |
| 222 | // Error logged by buildMergedDriver |
| 223 | return null; |
| 224 | } |
| 225 | registerMergedDriver(mergedDriver); |
| 226 | if (missingMergedDrivers.remove(newDriverName)) { |
| 227 | log.info("There are still {} missing merged drivers", |
| 228 | missingMergedDrivers.size()); |
| 229 | } |
| 230 | return newDriverName; |
| 231 | } finally { |
| 232 | locks.get(newDriverName).unlock(); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 233 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 236 | private String mergedDriverSuffix(PiPipeconfId pipeconfId) { |
| 237 | return MERGED_DRIVER_SEPARATOR + pipeconfId.id(); |
| 238 | } |
| 239 | |
| 240 | private String mergedDriverName(String baseDriverName, PiPipeconfId pipeconfId) { |
| 241 | return baseDriverName + mergedDriverSuffix(pipeconfId); |
| 242 | } |
| 243 | |
| 244 | private String getBaseDriverNameFromMerged(String mergedDriverName) { |
| 245 | final String[] pieces = mergedDriverName.split(MERGED_DRIVER_SEPARATOR); |
| 246 | if (pieces.length != 2) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 247 | return null; |
| 248 | } |
| 249 | return pieces[0]; |
| 250 | } |
| 251 | |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 252 | private PiPipeconfId getPipeconfIdFromMerged(String mergedDriverName) { |
| 253 | final String[] pieces = mergedDriverName.split(MERGED_DRIVER_SEPARATOR); |
| 254 | if (pieces.length != 2) { |
| 255 | return null; |
| 256 | } |
| 257 | return new PiPipeconfId(pieces[1]); |
| 258 | } |
| 259 | |
| 260 | private boolean isMergedDriverName(String driverName) { |
| 261 | final String[] pieces = driverName.split(MERGED_DRIVER_SEPARATOR); |
| 262 | return pieces.length == 2; |
| 263 | } |
| 264 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 265 | private Driver buildMergedDriver(PiPipeconfId pipeconfId, String baseDriverName, |
| 266 | String newDriverName) { |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 267 | final Driver baseDriver = getDriver(baseDriverName); |
| 268 | if (baseDriver == null) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 269 | log.error("Base driver {} not found, cannot build a merged one", |
| 270 | baseDriverName); |
| 271 | return null; |
| 272 | } |
| 273 | |
| 274 | final PiPipeconf pipeconf = pipeconfs.get(pipeconfId); |
| 275 | if (pipeconf == null) { |
| 276 | log.error("Pipeconf {} is not registered, cannot build a merged driver", |
| 277 | pipeconfId); |
| 278 | return null; |
| 279 | } |
| 280 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 281 | // extract the behaviours from the pipipeconf. |
| 282 | final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = |
| 283 | new HashMap<>(); |
| 284 | pipeconf.behaviours().forEach( |
| 285 | b -> behaviours.put(b, pipeconf.implementation(b).get())); |
| 286 | final Driver piPipeconfDriver = new DefaultDriver( |
| 287 | newDriverName, baseDriver.parents(), |
| 288 | baseDriver.manufacturer(), baseDriver.hwVersion(), |
| 289 | baseDriver.swVersion(), behaviours, new HashMap<>()); |
| 290 | // take the base driver created with the behaviours of the PiPeconf and |
| 291 | // merge it with the base driver that was assigned to the device |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 292 | return piPipeconfDriver.merge(baseDriver); |
| 293 | } |
| 294 | |
| 295 | private void registerMergedDriver(Driver driver) { |
| 296 | final DriverProvider provider = new InternalDriverProvider(driver); |
| 297 | if (driverAdminService.getProviders().contains(provider)) { |
| 298 | // A provider for this driver already exist. |
| 299 | return; |
| 300 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 301 | driverAdminService.registerProvider(provider); |
| 302 | } |
| 303 | |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 304 | private Driver getDriver(String name) { |
| 305 | try { |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 306 | return driverAdminService.getDriver(name); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 307 | } catch (ItemNotFoundException e) { |
| 308 | return null; |
| 309 | } |
| 310 | } |
| 311 | |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 312 | private boolean driverExists(String name) { |
| 313 | return getDriver(name) != null; |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 316 | private void checkMissingMergedDriver(DeviceId deviceId) { |
| 317 | final PiPipeconfId pipeconfId = pipeconfMappingStore.getPipeconfId(deviceId); |
| 318 | final BasicDeviceConfig cfg = cfgService.getConfig(deviceId, BasicDeviceConfig.class); |
| 319 | |
| 320 | if (pipeconfId == null) { |
| 321 | // No pipeconf associated. |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | if (cfg == null || cfg.driver() == null) { |
| 326 | log.warn("Missing basic device config or driver key in netcfg for " + |
| 327 | "{}, which is odd since it has a " + |
| 328 | "pipeconf associated ({})", |
| 329 | deviceId, pipeconfId); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | final String baseDriverName = cfg.driver(); |
| 334 | final String mergedDriverName = mergedDriverName(baseDriverName, pipeconfId); |
| 335 | |
| 336 | if (driverExists(mergedDriverName) || |
| 337 | missingMergedDrivers.contains(mergedDriverName)) { |
| 338 | // Not missing, or already aware of it missing. |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | log.info("Detected missing merged driver: {}", mergedDriverName); |
| 343 | missingMergedDrivers.add(mergedDriverName); |
| 344 | // Attempt building the driver now if all pieces are present. |
| 345 | // If not, either a driver or pipeconf event will re-trigger |
| 346 | // the process. |
| 347 | attemptDriverMerge(mergedDriverName); |
| 348 | } |
| 349 | |
| 350 | private void attemptDriverMerge(String mergedDriverName) { |
| 351 | final String baseDriverName = getBaseDriverNameFromMerged(mergedDriverName); |
| 352 | final PiPipeconfId pipeconfId = getPipeconfIdFromMerged(mergedDriverName); |
| 353 | if (driverExists(baseDriverName) && pipeconfs.containsKey(pipeconfId)) { |
| 354 | doMergeDriver(baseDriverName, pipeconfId); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | private void missingDriversWatchdogTask() { |
| 359 | while (true) { |
| 360 | // Most probably all missing drivers will be created before the |
| 361 | // watchdog interval, so wait before starting... |
| 362 | try { |
| 363 | TimeUnit.SECONDS.sleep(MISSING_DRIVER_WATCHDOG_INTERVAL); |
| 364 | } catch (InterruptedException e) { |
| 365 | log.warn("Interrupted! There are still {} missing merged drivers", |
| 366 | missingMergedDrivers.size()); |
| 367 | } |
| 368 | if (missingMergedDrivers.isEmpty()) { |
| 369 | log.info("There are no more missing merged drivers!"); |
| 370 | return; |
| 371 | } |
| 372 | log.info("Detected {} missing merged drivers, attempt merge...", |
| 373 | missingMergedDrivers.size()); |
| 374 | missingMergedDrivers.forEach(this::attemptDriverMerge); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | private void checkMissingMergedDrivers() { |
| 379 | cfgService.getSubjects(DeviceId.class, BasicDeviceConfig.class) |
| 380 | .forEach(this::checkMissingMergedDriver); |
| 381 | } |
| 382 | |
| 383 | private void attemptMergeAll(String baseDriverName) { |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 384 | missingMergedDrivers.stream() |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 385 | .filter(missingDriver -> { |
| 386 | // Filter missing merged drivers using this base driver. |
| 387 | final String xx = getBaseDriverNameFromMerged(missingDriver); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 388 | return xx != null && xx.equals(baseDriverName); |
| 389 | }) |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 390 | .forEach(this::attemptDriverMerge); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 393 | private void attemptMergeAll(PiPipeconfId pipeconfId) { |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 394 | missingMergedDrivers.stream() |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 395 | .filter(missingDriver -> { |
| 396 | // Filter missing merged drivers using this pipeconf. |
| 397 | final PiPipeconfId xx = getPipeconfIdFromMerged(missingDriver); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 398 | return xx != null && xx.equals(pipeconfId); |
| 399 | }) |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 400 | .forEach(this::attemptDriverMerge); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | private class InternalDriverListener implements DriverListener { |
| 404 | |
| 405 | @Override |
| 406 | public void event(DriverEvent event) { |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 407 | executor.execute(() -> attemptMergeAll(event.subject().name())); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public boolean isRelevant(DriverEvent event) { |
| 412 | return event.type() == DriverEvent.Type.DRIVER_ENHANCED; |
| 413 | } |
| 414 | } |
| 415 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 416 | /** |
| 417 | * Internal driver provider used to register merged pipeconf drivers in the |
| 418 | * core. |
| 419 | */ |
| 420 | private class InternalDriverProvider implements DriverProvider { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 421 | |
| 422 | Driver driver; |
| 423 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 424 | InternalDriverProvider(Driver driver) { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 425 | this.driver = driver; |
| 426 | } |
| 427 | |
| 428 | @Override |
| 429 | public Set<Driver> getDrivers() { |
| 430 | return ImmutableSet.of(driver); |
| 431 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 432 | |
| 433 | @Override |
| 434 | public boolean equals(Object o) { |
| 435 | if (this == o) { |
| 436 | return true; |
| 437 | } |
| 438 | if (o == null || getClass() != o.getClass()) { |
| 439 | return false; |
| 440 | } |
| 441 | InternalDriverProvider that = (InternalDriverProvider) o; |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 442 | return Objects.equals(driver.name(), that.driver.name()); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | @Override |
| 446 | public int hashCode() { |
| 447 | return Objects.hashCode(driver.name()); |
| 448 | } |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 449 | } |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 450 | } |