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 LoadingCache<String, ConcurrentMap<String, L2GatewayDevice>> CACHES = CacheBuilder.newBuilder()
26 .build(new CacheLoader<String, ConcurrentMap<String, L2GatewayDevice>>() {
28 public ConcurrentMap<String, L2GatewayDevice> load(String key) {
29 return new ConcurrentHashMap<>();
33 private ElanL2GwCacheUtils() {
36 public static void addL2GatewayDeviceToCache(String elanName, L2GatewayDevice l2GwDevice) {
37 CACHES.getUnchecked(elanName).put(l2GwDevice.getHwvtepNodeId(), l2GwDevice);
40 public static void removeL2GatewayDeviceFromAllElanCache(String deviceName) {
41 CACHES.asMap().values().forEach(deviceMap -> deviceMap.remove(deviceName));
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);
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);
56 public static ConcurrentMap<String, L2GatewayDevice> getInvolvedL2GwDevices(String elanName) {
57 ConcurrentMap<String, L2GatewayDevice> result = CACHES.getIfPresent(elanName);
58 return result == null ? new ConcurrentHashMap<>() : result;
61 public static Set<Entry<String, ConcurrentMap<String, L2GatewayDevice>>> getCaches() {
62 return CACHES.asMap().entrySet();
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());