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 org.opendaylight.genius.utils.cache.CacheUtil;
16 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
18 public final class ElanL2GwCacheUtils {
20 private static final ConcurrentHashMap<String, L2GatewayDevice> EMPTY_MAP = new ConcurrentHashMap<>();
21 public static final String L2GATEWAY_CONN_CACHE_NAME = "L2GWCONN";
23 private ElanL2GwCacheUtils() {
27 CacheUtil.createCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
30 public static void addL2GatewayDeviceToCache(String elanName, L2GatewayDevice l2GwDevice) {
31 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
32 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
33 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
34 ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
35 if (deviceMap == null) {
36 synchronized (ElanL2GwCacheUtils.class) {
37 deviceMap = cachedMap.computeIfAbsent(elanName, k -> new ConcurrentHashMap<>());
40 deviceMap.put(l2GwDevice.getHwvtepNodeId(), l2GwDevice);
43 public static void removeL2GatewayDeviceFromAllElanCache(String deviceName) {
44 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
45 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
46 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
47 cachedMap.values().forEach(deviceMap -> deviceMap.remove(deviceName));
51 public static L2GatewayDevice removeL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
52 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
53 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
54 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
55 ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
56 if (deviceMap != null) {
57 return deviceMap.remove(l2gwDeviceNodeId);
63 public static L2GatewayDevice getL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
64 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
65 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
66 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
67 ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
68 if (deviceMap != null) {
69 return deviceMap.get(l2gwDeviceNodeId);
75 public static ConcurrentMap<String, L2GatewayDevice> getInvolvedL2GwDevices(String elanName) {
76 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
77 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil
78 .getCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
79 ConcurrentMap<String, L2GatewayDevice> result = cachedMap.get(elanName);
86 public static List<L2GatewayDevice> getAllElanDevicesFromCache() {
87 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
88 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
89 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
90 if (cachedMap == null || cachedMap.isEmpty()) {
94 List<L2GatewayDevice> l2GwDevices = new ArrayList<>();
95 for (ConcurrentMap<String, L2GatewayDevice> l2gwDevices : cachedMap.values()) {
96 for (L2GatewayDevice l2gwDevice : l2gwDevices.values()) {
97 l2GwDevices.add(l2gwDevice);