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
9 package org.opendaylight.netvirt.elanmanager.utils;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import javax.annotation.Nullable;
16 import org.opendaylight.genius.utils.cache.CacheUtil;
17 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
19 public final class ElanL2GwCacheUtils {
21 private static final ConcurrentHashMap<String, L2GatewayDevice> EMPTY_MAP = new ConcurrentHashMap<>();
22 public static final String L2GATEWAY_CONN_CACHE_NAME = "L2GWCONN";
24 private ElanL2GwCacheUtils() {
28 CacheUtil.createCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
31 public static void addL2GatewayDeviceToCache(String elanName, L2GatewayDevice l2GwDevice) {
32 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
33 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
34 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
35 ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
36 if (deviceMap == null) {
37 synchronized (ElanL2GwCacheUtils.class) {
38 deviceMap = cachedMap.computeIfAbsent(elanName, k -> new ConcurrentHashMap<>());
41 deviceMap.put(l2GwDevice.getHwvtepNodeId(), l2GwDevice);
44 public static void removeL2GatewayDeviceFromAllElanCache(String deviceName) {
45 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
46 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
47 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
48 cachedMap.values().forEach(deviceMap -> deviceMap.remove(deviceName));
52 public static L2GatewayDevice removeL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
53 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
54 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
55 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
56 ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
57 if (deviceMap != null) {
58 return deviceMap.remove(l2gwDeviceNodeId);
65 public static L2GatewayDevice getL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
66 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
67 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
68 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
69 ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
70 if (deviceMap != null) {
71 return deviceMap.get(l2gwDeviceNodeId);
77 public static ConcurrentMap<String, L2GatewayDevice> getInvolvedL2GwDevices(String elanName) {
78 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
79 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil
80 .getCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
81 ConcurrentMap<String, L2GatewayDevice> result = cachedMap.get(elanName);
89 public static List<L2GatewayDevice> getAllElanDevicesFromCache() {
90 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
91 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
92 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
93 if (cachedMap == null || cachedMap.isEmpty()) {
97 List<L2GatewayDevice> l2GwDevices = new ArrayList<>();
98 for (ConcurrentMap<String, L2GatewayDevice> l2gwDevices : cachedMap.values()) {
99 for (L2GatewayDevice l2gwDevice : l2gwDevices.values()) {
100 l2GwDevices.add(l2gwDevice);