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