Bulk merge of l2gw changes
[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 LoadingCache<String, ConcurrentMap<String, L2GatewayDevice>> CACHES = CacheBuilder.newBuilder()
26             .build(new CacheLoader<String, ConcurrentMap<String, L2GatewayDevice>>() {
27                 @Override
28                 public ConcurrentMap<String, L2GatewayDevice> load(String key) {
29                     return new ConcurrentHashMap<>();
30                 }
31             });
32
33     private ElanL2GwCacheUtils() {
34     }
35
36     public static void addL2GatewayDeviceToCache(String elanName, L2GatewayDevice l2GwDevice) {
37         CACHES.getUnchecked(elanName).put(l2GwDevice.getHwvtepNodeId(), l2GwDevice);
38     }
39
40     public static void removeL2GatewayDeviceFromAllElanCache(String deviceName) {
41         CACHES.asMap().values().forEach(deviceMap -> deviceMap.remove(deviceName));
42     }
43
44     @Nullable
45     public static L2GatewayDevice removeL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
46         ConcurrentMap<String, L2GatewayDevice> deviceMap = CACHES.getIfPresent(elanName);
47         return deviceMap == null ? null : deviceMap.remove(l2gwDeviceNodeId);
48     }
49
50     @Nullable
51     public static L2GatewayDevice getL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
52         ConcurrentMap<String, L2GatewayDevice> deviceMap = CACHES.getIfPresent(elanName);
53         return deviceMap == null ? null : deviceMap.get(l2gwDeviceNodeId);
54     }
55
56     public static ConcurrentMap<String, L2GatewayDevice> getInvolvedL2GwDevices(String elanName) {
57         ConcurrentMap<String, L2GatewayDevice> result = CACHES.getIfPresent(elanName);
58         return result == null ? new ConcurrentHashMap<>() : result;
59     }
60
61     public static Set<Entry<String, ConcurrentMap<String, L2GatewayDevice>>> getCaches() {
62         return CACHES.asMap().entrySet();
63     }
64
65     @NonNull
66     public static List<L2GatewayDevice> getAllElanDevicesFromCache() {
67         List<L2GatewayDevice> l2GwDevices = new ArrayList<>();
68         for (ConcurrentMap<String, L2GatewayDevice> cache : CACHES.asMap().values()) {
69             l2GwDevices.addAll(cache.values());
70         }
71         return l2GwDevices;
72     }
73 }