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