8f314e4c1615e987a74885268ede9e1ab0690d1d
[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.Collection;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Map.Entry;
18 import java.util.Set;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ConcurrentMap;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
24
25 public final class ElanL2GwCacheUtils {
26
27     private static final LoadingCache<String, ConcurrentMap<String, L2GatewayDevice>> CACHES = CacheBuilder.newBuilder()
28             .build(new CacheLoader<String, ConcurrentMap<String, L2GatewayDevice>>() {
29                 @Override
30                 public ConcurrentMap<String, L2GatewayDevice> load(String key) {
31                     return new ConcurrentHashMap<>();
32                 }
33             });
34
35     private ElanL2GwCacheUtils() {
36     }
37
38     public static void addL2GatewayDeviceToCache(String elanName, L2GatewayDevice l2GwDevice) {
39         CACHES.getUnchecked(elanName).put(l2GwDevice.getHwvtepNodeId(), l2GwDevice);
40     }
41
42     public static void removeL2GatewayDeviceFromAllElanCache(String deviceName) {
43         CACHES.asMap().values().forEach(deviceMap -> deviceMap.remove(deviceName));
44     }
45
46     @Nullable
47     public static L2GatewayDevice removeL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
48         ConcurrentMap<String, L2GatewayDevice> deviceMap = CACHES.getIfPresent(elanName);
49         return deviceMap == null ? null : deviceMap.remove(l2gwDeviceNodeId);
50     }
51
52     @Nullable
53     public static L2GatewayDevice getL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
54         ConcurrentMap<String, L2GatewayDevice> deviceMap = CACHES.getIfPresent(elanName);
55         return deviceMap == null ? null : deviceMap.get(l2gwDeviceNodeId);
56     }
57
58     public static Collection<L2GatewayDevice> getInvolvedL2GwDevices(String elanName) {
59         ConcurrentMap<String, L2GatewayDevice> result = CACHES.getIfPresent(elanName);
60         return result == null ? Collections.emptyList() : result.values();
61     }
62
63     public static Set<Entry<String, ConcurrentMap<String, L2GatewayDevice>>> getCaches() {
64         return CACHES.asMap().entrySet();
65     }
66
67     @NonNull
68     public static List<L2GatewayDevice> getAllElanDevicesFromCache() {
69         List<L2GatewayDevice> l2GwDevices = new ArrayList<>();
70         for (ConcurrentMap<String, L2GatewayDevice> cache : CACHES.asMap().values()) {
71             l2GwDevices.addAll(cache.values());
72         }
73         return l2GwDevices;
74     }
75 }