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 javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.genius.mdsalutil.cache.DataObjectCache;
21 import org.opendaylight.infrautils.caches.CacheProvider;
22 import org.opendaylight.netvirt.elanmanager.api.ElanHelper;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Caches ElanInstances.
32 * @author Thomas Pantelis
35 public class ElanInstanceCache extends DataObjectCache<ElanInstance> {
36 private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceCache.class);
38 private final Map<InstanceIdentifier<ElanInstance>, Collection<Runnable>> waitingJobs = new HashMap<>();
41 public ElanInstanceCache(DataBroker dataBroker, CacheProvider cacheProvider) {
42 super(ElanInstance.class, dataBroker, LogicalDatastoreType.CONFIGURATION,
43 InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class), cacheProvider);
46 public Optional<ElanInstance> get(String elanInstanceName) {
48 return get(ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName));
49 } catch (ReadFailedException e) {
50 LOG.warn("Error reading ElanInstance {}", elanInstanceName, e);
51 return Optional.absent();
55 public Optional<ElanInstance> get(String elanInstanceName, Runnable runAfterElanIsAvailable) {
56 Optional<ElanInstance> possibleInstance = get(elanInstanceName);
57 if (!possibleInstance.isPresent()) {
58 synchronized (waitingJobs) {
59 possibleInstance = get(elanInstanceName);
60 if (!possibleInstance.isPresent()) {
61 waitingJobs.computeIfAbsent(ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName),
62 key -> new ArrayList<>()).add(runAfterElanIsAvailable);
67 return possibleInstance;
71 protected void added(InstanceIdentifier<ElanInstance> path, ElanInstance elanInstance) {
72 Collection<Runnable> jobsToRun;
73 synchronized (waitingJobs) {
74 jobsToRun = waitingJobs.remove(path);
77 if (jobsToRun != null) {
78 jobsToRun.forEach(Runnable::run);