Refactor static WAITING_JOB_LIST in ElanInstanceListener
[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 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;
28
29 /**
30  * Caches ElanInstances.
31  *
32  * @author Thomas Pantelis
33  */
34 @Singleton
35 public class ElanInstanceCache extends DataObjectCache<ElanInstance> {
36     private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceCache.class);
37
38     private final Map<InstanceIdentifier<ElanInstance>, Collection<Runnable>> waitingJobs = new HashMap<>();
39
40     @Inject
41     public ElanInstanceCache(DataBroker dataBroker, CacheProvider cacheProvider) {
42         super(ElanInstance.class, dataBroker, LogicalDatastoreType.CONFIGURATION,
43                 InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class), cacheProvider);
44     }
45
46     public Optional<ElanInstance> get(String elanInstanceName) {
47         try {
48             return get(ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName));
49         } catch (ReadFailedException e) {
50             LOG.warn("Error reading ElanInstance {}", elanInstanceName, e);
51             return Optional.absent();
52         }
53     }
54
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);
63                 }
64             }
65         }
66
67         return possibleInstance;
68     }
69
70     @Override
71     protected void added(InstanceIdentifier<ElanInstance> path, ElanInstance elanInstance) {
72         Collection<Runnable> jobsToRun;
73         synchronized (waitingJobs) {
74             jobsToRun = waitingJobs.remove(path);
75         }
76
77         if (jobsToRun != null) {
78             jobsToRun.forEach(Runnable::run);
79         }
80     }
81 }