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