2 * Copyright © 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elanmanager.utils;
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;
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;
23 public final class ElanL2GwCacheUtils {
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>>() {
29 public ConcurrentMap<String, L2GatewayDevice> load(String key) {
30 return new ConcurrentHashMap<>();
34 private ElanL2GwCacheUtils() {
37 public static void addL2GatewayDeviceToCache(String elanName, L2GatewayDevice l2GwDevice) {
38 CACHES.getUnchecked(elanName).put(l2GwDevice.getHwvtepNodeId(), l2GwDevice);
41 public static void removeL2GatewayDeviceFromAllElanCache(String deviceName) {
42 CACHES.asMap().values().forEach(deviceMap -> deviceMap.remove(deviceName));
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);
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);
57 public static ConcurrentMap<String, L2GatewayDevice> getInvolvedL2GwDevices(String elanName) {
58 ConcurrentMap<String, L2GatewayDevice> result = CACHES.getIfPresent(elanName);
59 return result == null ? EMPTY_MAP : result;
62 public static Set<Entry<String, ConcurrentMap<String, L2GatewayDevice>>> getCaches() {
63 return CACHES.asMap().entrySet();
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());