1f30d60c266409cba2ff4f3e213b11034883f001
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / cache / ElanInstanceCache.java
1 /*
2  * Copyright (c) 2017 Inocybe Technologies and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.elan.cache;
9
10 import com.google.common.base.Optional;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.Map;
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;
30
31 /**
32  * Caches ElanInstances.
33  *
34  * @author Thomas Pantelis
35  */
36 @Singleton
37 public class ElanInstanceCache extends InstanceIdDataObjectCache<ElanInstance> {
38     private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceCache.class);
39
40     //TODO: Make it configurable
41     private static final int ELAN_CLEANUP_DELAY_IN_MINS = 30;
42
43     private final Map<InstanceIdentifier<ElanInstance>, Collection<Runnable>> waitingJobs = new HashMap<>();
44
45     private final Scheduler scheduler;
46
47     @Inject
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;
52     }
53
54     @Override
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);
59     }
60
61     public Optional<ElanInstance> get(String elanInstanceName) {
62         try {
63             return get(ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName));
64         } catch (ReadFailedException e) {
65             LOG.warn("Error reading ElanInstance {}", elanInstanceName, e);
66             return Optional.absent();
67         }
68     }
69
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);
78                 }
79             }
80         }
81
82         return possibleInstance;
83     }
84
85     @Override
86     protected void added(InstanceIdentifier<ElanInstance> path, ElanInstance elanInstance) {
87         Collection<Runnable> jobsToRun;
88         synchronized (waitingJobs) {
89             jobsToRun = waitingJobs.remove(path);
90         }
91
92         if (jobsToRun != null) {
93             jobsToRun.forEach(Runnable::run);
94         }
95     }
96 }