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