Remove use of org.opendaylight.genius.utils.cache.CacheUtil
[netvirt.git] / elanmanager / api / src / main / java / org / opendaylight / netvirt / elanmanager / utils / ElanL2GwCacheUtils.java
1 /*
2  * Copyright © 2016, 2017 Ericsson India Global Services Pvt Ltd. 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.elanmanager.utils;
9
10 import com.google.common.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Map.Entry;
16 import java.util.Set;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
22
23 public final class ElanL2GwCacheUtils {
24
25     private static final ConcurrentHashMap<String, L2GatewayDevice> EMPTY_MAP = new ConcurrentHashMap<>();
26     private static final LoadingCache<String, ConcurrentMap<String, L2GatewayDevice>> CACHES = CacheBuilder.newBuilder()
27             .build(new CacheLoader<String, ConcurrentMap<String, L2GatewayDevice>>() {
28                 @Override
29                 public ConcurrentMap<String, L2GatewayDevice> load(String key) {
30                     return new ConcurrentHashMap<>();
31                 }
32             });
33
34     private ElanL2GwCacheUtils() {
35     }
36
37     public static void addL2GatewayDeviceToCache(String elanName, L2GatewayDevice l2GwDevice) {
38         CACHES.getUnchecked(elanName).put(l2GwDevice.getHwvtepNodeId(), l2GwDevice);
39     }
40
41     public static void removeL2GatewayDeviceFromAllElanCache(String deviceName) {
42         CACHES.asMap().values().forEach(deviceMap -> deviceMap.remove(deviceName));
43     }
44
45     @Nullable
46     public static L2GatewayDevice removeL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
47         ConcurrentMap<String, L2GatewayDevice> deviceMap = CACHES.getIfPresent(elanName);
48         return deviceMap == null ? null : deviceMap.remove(l2gwDeviceNodeId);
49     }
50
51     @Nullable
52     public static L2GatewayDevice getL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
53         ConcurrentMap<String, L2GatewayDevice> deviceMap = CACHES.getIfPresent(elanName);
54         return deviceMap == null ? null : deviceMap.get(l2gwDeviceNodeId);
55     }
56
57     public static ConcurrentMap<String, L2GatewayDevice> getInvolvedL2GwDevices(String elanName) {
58         ConcurrentMap<String, L2GatewayDevice> result = CACHES.getIfPresent(elanName);
59         return result == null ? EMPTY_MAP : result;
60     }
61
62     public static Set<Entry<String, ConcurrentMap<String, L2GatewayDevice>>> getCaches() {
63         return CACHES.asMap().entrySet();
64     }
65
66     @NonNull
67     public static List<L2GatewayDevice> getAllElanDevicesFromCache() {
68         List<L2GatewayDevice> l2GwDevices = new ArrayList<>();
69         for (ConcurrentMap<String, L2GatewayDevice> cache : CACHES.asMap().values()) {
70             l2GwDevices.addAll(cache.values());
71         }
72         return l2GwDevices;
73     }
74 }