2 * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netvirt.elan.cache;
10 import com.google.common.base.Optional;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.HashMap;
15 import java.util.concurrent.TimeUnit;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.genius.mdsalutil.cache.InstanceIdDataObjectCache;
22 import org.opendaylight.infrautils.caches.CacheProvider;
23 import org.opendaylight.netvirt.elan.utils.Scheduler;
24 import org.opendaylight.netvirt.elanmanager.api.ElanHelper;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Caches ElanInstances.
34 * @author Thomas Pantelis
37 public class ElanInstanceCache extends InstanceIdDataObjectCache<ElanInstance> {
38 private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceCache.class);
40 //TODO: Make it configurable
41 private static final int ELAN_CLEANUP_DELAY_IN_MINS = 30;
43 private final Map<InstanceIdentifier<ElanInstance>, Collection<Runnable>> waitingJobs = new HashMap<>();
45 private final Scheduler scheduler;
48 public ElanInstanceCache(DataBroker dataBroker, CacheProvider cacheProvider, Scheduler scheduler) {
49 super(ElanInstance.class, dataBroker, LogicalDatastoreType.CONFIGURATION,
50 InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class), cacheProvider);
51 this.scheduler = scheduler;
55 protected void removed(InstanceIdentifier<ElanInstance> path, ElanInstance dataObject) {
56 scheduler.getScheduledExecutorService().schedule(() -> {
57 super.removed(path, dataObject);
58 }, ELAN_CLEANUP_DELAY_IN_MINS, TimeUnit.MINUTES);
61 public Optional<ElanInstance> get(String elanInstanceName) {
63 return get(ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName));
64 } catch (ReadFailedException e) {
65 LOG.warn("Error reading ElanInstance {}", elanInstanceName, e);
66 return Optional.absent();
70 public Optional<ElanInstance> get(String elanInstanceName, Runnable runAfterElanIsAvailable) {
71 Optional<ElanInstance> possibleInstance = get(elanInstanceName);
72 if (!possibleInstance.isPresent()) {
73 synchronized (waitingJobs) {
74 possibleInstance = get(elanInstanceName);
75 if (!possibleInstance.isPresent()) {
76 waitingJobs.computeIfAbsent(ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName),
77 key -> new ArrayList<>()).add(runAfterElanIsAvailable);
82 return possibleInstance;
86 protected void added(InstanceIdentifier<ElanInstance> path, ElanInstance elanInstance) {
87 Collection<Runnable> jobsToRun;
88 synchronized (waitingJobs) {
89 jobsToRun = waitingJobs.remove(path);
92 if (jobsToRun != null) {
93 jobsToRun.forEach(Runnable::run);