blob: 27f6e4dfb0f34727a2ec87b7288ac1d253b91c63 [file] [log] [blame]
Andrea Campanellabc112a92017-06-26 19:06:43 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Andrea Campanellabc112a92017-06-26 19:06:43 +02003 *
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
17package org.onosproject.net.pi.impl;
18
Andrea Campanella48f99fa2017-07-13 19:06:21 +020019import com.google.common.annotations.Beta;
Andrea Campanellabc112a92017-06-26 19:06:43 +020020import com.google.common.collect.ImmutableSet;
Carmelo Casconeda60a612018-08-24 00:01:34 -070021import com.google.common.collect.Sets;
22import com.google.common.util.concurrent.Striped;
Andrea Campanellabc112a92017-06-26 19:06:43 +020023import org.onlab.util.ItemNotFoundException;
Carmelo Casconeda0b5592018-09-14 12:54:15 -070024import org.onlab.util.SharedExecutors;
Andrea Campanellabc112a92017-06-26 19:06:43 +020025import org.onosproject.net.DeviceId;
26import org.onosproject.net.config.ConfigFactory;
Andrea Campanellabc112a92017-06-26 19:06:43 +020027import org.onosproject.net.config.NetworkConfigRegistry;
28import org.onosproject.net.config.basics.BasicDeviceConfig;
29import org.onosproject.net.config.basics.SubjectFactories;
30import org.onosproject.net.driver.Behaviour;
31import org.onosproject.net.driver.DefaultDriver;
32import org.onosproject.net.driver.Driver;
33import org.onosproject.net.driver.DriverAdminService;
Carmelo Casconeda60a612018-08-24 00:01:34 -070034import org.onosproject.net.driver.DriverEvent;
35import org.onosproject.net.driver.DriverListener;
Andrea Campanellabc112a92017-06-26 19:06:43 +020036import org.onosproject.net.driver.DriverProvider;
Andrea Campanellabc112a92017-06-26 19:06:43 +020037import org.onosproject.net.pi.model.PiPipeconf;
38import org.onosproject.net.pi.model.PiPipeconfId;
Carmelo Cascone39c28ca2017-11-15 13:03:57 -080039import org.onosproject.net.pi.service.PiPipeconfConfig;
40import org.onosproject.net.pi.service.PiPipeconfMappingStore;
41import org.onosproject.net.pi.service.PiPipeconfService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042import org.osgi.service.component.annotations.Activate;
43import org.osgi.service.component.annotations.Component;
44import org.osgi.service.component.annotations.Deactivate;
45import org.osgi.service.component.annotations.Reference;
46import org.osgi.service.component.annotations.ReferenceCardinality;
Andrea Campanellabc112a92017-06-26 19:06:43 +020047import org.slf4j.Logger;
48
49import java.util.HashMap;
50import java.util.Map;
Carmelo Casconeda60a612018-08-24 00:01:34 -070051import java.util.Objects;
Andrea Campanellabc112a92017-06-26 19:06:43 +020052import java.util.Optional;
53import java.util.Set;
Andrea Campanellabc112a92017-06-26 19:06:43 +020054import java.util.concurrent.ConcurrentHashMap;
Carmelo Cascone96beb6f2018-06-27 18:07:12 +020055import java.util.concurrent.ConcurrentMap;
Andrea Campanellabc112a92017-06-26 19:06:43 +020056import java.util.concurrent.ExecutorService;
57import java.util.concurrent.Executors;
Carmelo Casconeda0b5592018-09-14 12:54:15 -070058import java.util.concurrent.TimeUnit;
Carmelo Casconeda60a612018-08-24 00:01:34 -070059import java.util.concurrent.locks.Lock;
Andrea Campanellabc112a92017-06-26 19:06:43 +020060
Carmelo Cascone44daf562017-07-16 23:55:08 -040061import static java.lang.String.format;
Andrea Campanellabc112a92017-06-26 19:06:43 +020062import static org.onlab.util.Tools.groupedThreads;
63import static org.slf4j.LoggerFactory.getLogger;
64
65
66/**
67 * Implementation of the PiPipeconfService.
68 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070069@Component(immediate = true, service = PiPipeconfService.class)
Andrea Campanella48f99fa2017-07-13 19:06:21 +020070@Beta
71public class PiPipeconfManager implements PiPipeconfService {
Andrea Campanellabc112a92017-06-26 19:06:43 +020072
73 private final Logger log = getLogger(getClass());
74
Carmelo Cascone158b8c42018-07-04 19:42:37 +020075 private static final String MERGED_DRIVER_SEPARATOR = ":";
Andrea Campanellabc112a92017-06-26 19:06:43 +020076 private static final String CFG_SCHEME = "piPipeconf";
77
Carmelo Casconeda0b5592018-09-14 12:54:15 -070078 private static final int MISSING_DRIVER_WATCHDOG_INTERVAL = 5; // Seconds.
79
Ray Milkeyd84f89b2018-08-17 14:54:17 -070080 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellabc112a92017-06-26 19:06:43 +020081 protected NetworkConfigRegistry cfgService;
82
Ray Milkeyd84f89b2018-08-17 14:54:17 -070083 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellabc112a92017-06-26 19:06:43 +020084 protected DriverAdminService driverAdminService;
85
Ray Milkeyd84f89b2018-08-17 14:54:17 -070086 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Cascone96beb6f2018-06-27 18:07:12 +020087 private PiPipeconfMappingStore pipeconfMappingStore;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020088
Carmelo Cascone96beb6f2018-06-27 18:07:12 +020089 // 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 Campanellabc112a92017-06-26 19:06:43 +020093
Carmelo Casconeda60a612018-08-24 00:01:34 -070094 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 Cascone96beb6f2018-06-27 18:07:12 +020098 protected ExecutorService executor = Executors.newFixedThreadPool(
99 10, groupedThreads("onos/pipeconf-manager", "%d", log));
Andrea Campanellabc112a92017-06-26 19:06:43 +0200100
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200101 protected final ConfigFactory configFactory =
Andrea Campanellabc112a92017-06-26 19:06:43 +0200102 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 Campanellabc112a92017-06-26 19:06:43 +0200111 @Activate
112 public void activate() {
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200113 cfgService.registerConfigFactory(configFactory);
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700114 driverAdminService.addListener(driverListener);
Carmelo Casconeda60a612018-08-24 00:01:34 -0700115 checkMissingMergedDrivers();
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700116 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 Campanellabc112a92017-06-26 19:06:43 +0200123 log.info("Started");
124 }
125
126
127 @Deactivate
128 public void deactivate() {
129 executor.shutdown();
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200130 cfgService.unregisterConfigFactory(configFactory);
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700131 driverAdminService.removeListener(driverListener);
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200132 pipeconfs.clear();
Carmelo Casconeda60a612018-08-24 00:01:34 -0700133 missingMergedDrivers.clear();
Andrea Campanellabc112a92017-06-26 19:06:43 +0200134 cfgService = null;
135 driverAdminService = null;
Andrea Campanellabc112a92017-06-26 19:06:43 +0200136 log.info("Stopped");
137 }
138
139 @Override
140 public void register(PiPipeconf pipeconf) throws IllegalStateException {
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200141 if (pipeconfs.containsKey(pipeconf.id())) {
Carmelo Cascone44daf562017-07-16 23:55:08 -0400142 throw new IllegalStateException(format("Pipeconf %s is already registered", pipeconf.id()));
143 }
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200144 pipeconfs.put(pipeconf.id(), pipeconf);
Carmelo Cascone44daf562017-07-16 23:55:08 -0400145 log.info("New pipeconf registered: {}", pipeconf.id());
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700146 executor.execute(() -> attemptMergeAll(pipeconf.id()));
Andrea Campanellabc112a92017-06-26 19:06:43 +0200147 }
148
149 @Override
Andrea Campanellaa9b3c9b2017-07-21 14:03:15 +0200150 public void remove(PiPipeconfId pipeconfId) throws IllegalStateException {
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200151 // TODO add mechanism to remove from device.
152 if (!pipeconfs.containsKey(pipeconfId)) {
Andrea Campanellaa9b3c9b2017-07-21 14:03:15 +0200153 throw new IllegalStateException(format("Pipeconf %s is not registered", pipeconfId));
154 }
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200155 // TODO remove the binding from the distributed Store when the lifecycle of a pipeconf is defined.
156 // pipeconfMappingStore.removeBindings(pipeconfId);
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200157 log.info("Removing pipeconf {}", pipeconfId);
158 pipeconfs.remove(pipeconfId);
Andrea Campanellaa9b3c9b2017-07-21 14:03:15 +0200159 }
160
161 @Override
Andrea Campanellabc112a92017-06-26 19:06:43 +0200162 public Iterable<PiPipeconf> getPipeconfs() {
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200163 return pipeconfs.values();
Andrea Campanellabc112a92017-06-26 19:06:43 +0200164 }
165
166 @Override
167 public Optional<PiPipeconf> getPipeconf(PiPipeconfId id) {
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200168 return Optional.ofNullable(pipeconfs.get(id));
Andrea Campanellabc112a92017-06-26 19:06:43 +0200169 }
170
171 @Override
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800172 public Optional<PiPipeconf> getPipeconf(DeviceId deviceId) {
173 if (pipeconfMappingStore.getPipeconfId(deviceId) == null) {
174 return Optional.empty();
175 } else {
176 return Optional.ofNullable(pipeconfs.get(
177 pipeconfMappingStore.getPipeconfId(deviceId)));
178 }
179 }
180
181 @Override
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200182 public void bindToDevice(PiPipeconfId pipeconfId, DeviceId deviceId) {
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700183 PiPipeconfId existingPipeconfId = pipeconfMappingStore.getPipeconfId(deviceId);
184 if (existingPipeconfId != null && !existingPipeconfId.equals(pipeconfId)) {
185 log.error("Cannot set binding for {} to {} as one already exists ({})",
186 deviceId, pipeconfId, existingPipeconfId);
187 return;
188 }
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200189 pipeconfMappingStore.createOrUpdateBinding(deviceId, pipeconfId);
190 }
191
192 @Override
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700193 public String getMergedDriver(DeviceId deviceId, PiPipeconfId pipeconfId) {
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200194 log.debug("Starting device driver merge of {} with {}...", deviceId, pipeconfId);
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200195 final BasicDeviceConfig basicDeviceConfig = cfgService.getConfig(
196 deviceId, BasicDeviceConfig.class);
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200197 if (basicDeviceConfig == null) {
198 log.warn("Unable to get basic device config for {}, " +
Tian Jian855b1082018-10-23 16:22:47 +0800199 "aborting pipeconf driver merge", deviceId);
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200200 return null;
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200201 }
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200202 String baseDriverName = basicDeviceConfig.driver();
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700203 if (baseDriverName == null) {
204 log.warn("Missing driver from basic device config for {}, " +
205 "cannot produce merged driver", deviceId);
206 return null;
207 }
Carmelo Casconeda60a612018-08-24 00:01:34 -0700208 if (isMergedDriverName(baseDriverName)) {
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200209 // The config already has driver name that is a merged one. We still
210 // need to make sure an instance of that merged driver is present in
211 // this node.
Carmelo Casconeda60a612018-08-24 00:01:34 -0700212 log.debug("Base driver of {} ({}) is a merged one",
213 deviceId, baseDriverName);
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200214 baseDriverName = getBaseDriverNameFromMerged(baseDriverName);
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200215 }
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200216
Carmelo Casconeda60a612018-08-24 00:01:34 -0700217 return doMergeDriver(baseDriverName, pipeconfId);
218 }
219
220 @Override
221 public Optional<PiPipeconfId> ofDevice(DeviceId deviceId) {
222 return Optional.ofNullable(pipeconfMappingStore.getPipeconfId(deviceId));
223 }
224
225 private String doMergeDriver(String baseDriverName, PiPipeconfId pipeconfId) {
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200226 final String newDriverName = mergedDriverName(baseDriverName, pipeconfId);
Carmelo Casconeda60a612018-08-24 00:01:34 -0700227 // Serialize per newDriverName, avoid creating duplicates.
228 locks.get(newDriverName).lock();
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200229 try {
Carmelo Casconeda60a612018-08-24 00:01:34 -0700230 // If merged driver exists already we don't create a new one.
231 if (getDriver(newDriverName) != null) {
232 return newDriverName;
233 }
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200234 log.info("Creating merged driver {}...", newDriverName);
Carmelo Casconeda60a612018-08-24 00:01:34 -0700235 final Driver mergedDriver = buildMergedDriver(
236 pipeconfId, baseDriverName, newDriverName);
237 if (mergedDriver == null) {
238 // Error logged by buildMergedDriver
239 return null;
240 }
241 registerMergedDriver(mergedDriver);
242 if (missingMergedDrivers.remove(newDriverName)) {
243 log.info("There are still {} missing merged drivers",
244 missingMergedDrivers.size());
245 }
246 return newDriverName;
247 } finally {
248 locks.get(newDriverName).unlock();
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200249 }
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200250 }
251
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200252 private String mergedDriverSuffix(PiPipeconfId pipeconfId) {
253 return MERGED_DRIVER_SEPARATOR + pipeconfId.id();
254 }
255
256 private String mergedDriverName(String baseDriverName, PiPipeconfId pipeconfId) {
257 return baseDriverName + mergedDriverSuffix(pipeconfId);
258 }
259
260 private String getBaseDriverNameFromMerged(String mergedDriverName) {
261 final String[] pieces = mergedDriverName.split(MERGED_DRIVER_SEPARATOR);
262 if (pieces.length != 2) {
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200263 return null;
264 }
265 return pieces[0];
266 }
267
Carmelo Casconeda60a612018-08-24 00:01:34 -0700268 private PiPipeconfId getPipeconfIdFromMerged(String mergedDriverName) {
269 final String[] pieces = mergedDriverName.split(MERGED_DRIVER_SEPARATOR);
270 if (pieces.length != 2) {
271 return null;
272 }
273 return new PiPipeconfId(pieces[1]);
274 }
275
276 private boolean isMergedDriverName(String driverName) {
277 final String[] pieces = driverName.split(MERGED_DRIVER_SEPARATOR);
278 return pieces.length == 2;
279 }
280
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200281 private Driver buildMergedDriver(PiPipeconfId pipeconfId, String baseDriverName,
282 String newDriverName) {
Carmelo Casconeda60a612018-08-24 00:01:34 -0700283 final Driver baseDriver = getDriver(baseDriverName);
284 if (baseDriver == null) {
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200285 log.error("Base driver {} not found, cannot build a merged one",
286 baseDriverName);
287 return null;
288 }
289
290 final PiPipeconf pipeconf = pipeconfs.get(pipeconfId);
291 if (pipeconf == null) {
292 log.error("Pipeconf {} is not registered, cannot build a merged driver",
293 pipeconfId);
294 return null;
295 }
296
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200297 // extract the behaviours from the pipipeconf.
298 final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours =
299 new HashMap<>();
300 pipeconf.behaviours().forEach(
301 b -> behaviours.put(b, pipeconf.implementation(b).get()));
302 final Driver piPipeconfDriver = new DefaultDriver(
303 newDriverName, baseDriver.parents(),
304 baseDriver.manufacturer(), baseDriver.hwVersion(),
305 baseDriver.swVersion(), behaviours, new HashMap<>());
306 // take the base driver created with the behaviours of the PiPeconf and
307 // merge it with the base driver that was assigned to the device
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200308 return piPipeconfDriver.merge(baseDriver);
309 }
310
311 private void registerMergedDriver(Driver driver) {
312 final DriverProvider provider = new InternalDriverProvider(driver);
313 if (driverAdminService.getProviders().contains(provider)) {
314 // A provider for this driver already exist.
315 return;
316 }
Carmelo Cascone96beb6f2018-06-27 18:07:12 +0200317 driverAdminService.registerProvider(provider);
318 }
319
Carmelo Casconeda60a612018-08-24 00:01:34 -0700320 private Driver getDriver(String name) {
321 try {
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700322 return driverAdminService.getDriver(name);
Carmelo Casconeda60a612018-08-24 00:01:34 -0700323 } catch (ItemNotFoundException e) {
324 return null;
325 }
326 }
327
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700328 private boolean driverExists(String name) {
329 return getDriver(name) != null;
Carmelo Casconeda60a612018-08-24 00:01:34 -0700330 }
331
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700332 private void checkMissingMergedDriver(DeviceId deviceId) {
333 final PiPipeconfId pipeconfId = pipeconfMappingStore.getPipeconfId(deviceId);
334 final BasicDeviceConfig cfg = cfgService.getConfig(deviceId, BasicDeviceConfig.class);
335
336 if (pipeconfId == null) {
337 // No pipeconf associated.
338 return;
339 }
340
341 if (cfg == null || cfg.driver() == null) {
342 log.warn("Missing basic device config or driver key in netcfg for " +
343 "{}, which is odd since it has a " +
344 "pipeconf associated ({})",
345 deviceId, pipeconfId);
346 return;
347 }
348
349 final String baseDriverName = cfg.driver();
350 final String mergedDriverName = mergedDriverName(baseDriverName, pipeconfId);
351
352 if (driverExists(mergedDriverName) ||
353 missingMergedDrivers.contains(mergedDriverName)) {
354 // Not missing, or already aware of it missing.
355 return;
356 }
357
358 log.info("Detected missing merged driver: {}", mergedDriverName);
359 missingMergedDrivers.add(mergedDriverName);
360 // Attempt building the driver now if all pieces are present.
361 // If not, either a driver or pipeconf event will re-trigger
362 // the process.
363 attemptDriverMerge(mergedDriverName);
364 }
365
366 private void attemptDriverMerge(String mergedDriverName) {
367 final String baseDriverName = getBaseDriverNameFromMerged(mergedDriverName);
368 final PiPipeconfId pipeconfId = getPipeconfIdFromMerged(mergedDriverName);
369 if (driverExists(baseDriverName) && pipeconfs.containsKey(pipeconfId)) {
370 doMergeDriver(baseDriverName, pipeconfId);
371 }
372 }
373
374 private void missingDriversWatchdogTask() {
375 while (true) {
376 // Most probably all missing drivers will be created before the
377 // watchdog interval, so wait before starting...
378 try {
379 TimeUnit.SECONDS.sleep(MISSING_DRIVER_WATCHDOG_INTERVAL);
380 } catch (InterruptedException e) {
381 log.warn("Interrupted! There are still {} missing merged drivers",
382 missingMergedDrivers.size());
383 }
384 if (missingMergedDrivers.isEmpty()) {
385 log.info("There are no more missing merged drivers!");
386 return;
387 }
388 log.info("Detected {} missing merged drivers, attempt merge...",
389 missingMergedDrivers.size());
390 missingMergedDrivers.forEach(this::attemptDriverMerge);
391 }
392 }
393
394 private void checkMissingMergedDrivers() {
395 cfgService.getSubjects(DeviceId.class, BasicDeviceConfig.class)
396 .forEach(this::checkMissingMergedDriver);
397 }
398
399 private void attemptMergeAll(String baseDriverName) {
Carmelo Casconeda60a612018-08-24 00:01:34 -0700400 missingMergedDrivers.stream()
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700401 .filter(missingDriver -> {
402 // Filter missing merged drivers using this base driver.
403 final String xx = getBaseDriverNameFromMerged(missingDriver);
Carmelo Casconeda60a612018-08-24 00:01:34 -0700404 return xx != null && xx.equals(baseDriverName);
405 })
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700406 .forEach(this::attemptDriverMerge);
Carmelo Casconeda60a612018-08-24 00:01:34 -0700407 }
408
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700409 private void attemptMergeAll(PiPipeconfId pipeconfId) {
Carmelo Casconeda60a612018-08-24 00:01:34 -0700410 missingMergedDrivers.stream()
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700411 .filter(missingDriver -> {
412 // Filter missing merged drivers using this pipeconf.
413 final PiPipeconfId xx = getPipeconfIdFromMerged(missingDriver);
Carmelo Casconeda60a612018-08-24 00:01:34 -0700414 return xx != null && xx.equals(pipeconfId);
415 })
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700416 .forEach(this::attemptDriverMerge);
Carmelo Casconeda60a612018-08-24 00:01:34 -0700417 }
418
419 private class InternalDriverListener implements DriverListener {
420
421 @Override
422 public void event(DriverEvent event) {
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700423 executor.execute(() -> attemptMergeAll(event.subject().name()));
Carmelo Casconeda60a612018-08-24 00:01:34 -0700424 }
425
426 @Override
427 public boolean isRelevant(DriverEvent event) {
428 return event.type() == DriverEvent.Type.DRIVER_ENHANCED;
429 }
430 }
431
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200432 /**
433 * Internal driver provider used to register merged pipeconf drivers in the
434 * core.
435 */
436 private class InternalDriverProvider implements DriverProvider {
Andrea Campanellabc112a92017-06-26 19:06:43 +0200437
438 Driver driver;
439
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200440 InternalDriverProvider(Driver driver) {
Andrea Campanellabc112a92017-06-26 19:06:43 +0200441 this.driver = driver;
442 }
443
444 @Override
445 public Set<Driver> getDrivers() {
446 return ImmutableSet.of(driver);
447 }
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200448
449 @Override
450 public boolean equals(Object o) {
451 if (this == o) {
452 return true;
453 }
454 if (o == null || getClass() != o.getClass()) {
455 return false;
456 }
457 InternalDriverProvider that = (InternalDriverProvider) o;
Carmelo Casconeda60a612018-08-24 00:01:34 -0700458 return Objects.equals(driver.name(), that.driver.name());
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200459 }
460
461 @Override
462 public int hashCode() {
463 return Objects.hashCode(driver.name());
464 }
Andrea Campanellabc112a92017-06-26 19:06:43 +0200465 }
Andrea Campanellabc112a92017-06-26 19:06:43 +0200466}