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.Collections;
13 import java.util.List;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18 import org.opendaylight.genius.utils.cache.CacheUtil;
19 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
21 public final class ElanL2GwCacheUtils {
23 private static final ConcurrentHashMap<String, L2GatewayDevice> EMPTY_MAP = new ConcurrentHashMap<>();
24 public static final String L2GATEWAY_CONN_CACHE_NAME = "L2GWCONN";
26 private ElanL2GwCacheUtils() {
30 CacheUtil.createCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
33 public static void addL2GatewayDeviceToCache(String elanName, L2GatewayDevice l2GwDevice) {
34 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
35 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
36 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
37 ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
38 if (deviceMap == null) {
39 synchronized (ElanL2GwCacheUtils.class) {
40 deviceMap = cachedMap.computeIfAbsent(elanName, k -> new ConcurrentHashMap<>());
43 deviceMap.put(l2GwDevice.getHwvtepNodeId(), l2GwDevice);
46 public static void removeL2GatewayDeviceFromAllElanCache(String deviceName) {
47 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
48 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
49 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
50 cachedMap.values().forEach(deviceMap -> deviceMap.remove(deviceName));
54 public static L2GatewayDevice removeL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
55 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
56 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
57 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
58 ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
59 if (deviceMap != null) {
60 return deviceMap.remove(l2gwDeviceNodeId);
67 public static L2GatewayDevice getL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
68 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
69 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
70 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
71 ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
72 if (deviceMap != null) {
73 return deviceMap.get(l2gwDeviceNodeId);
79 public static ConcurrentMap<String, L2GatewayDevice> getInvolvedL2GwDevices(String elanName) {
80 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
81 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil
82 .getCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
83 ConcurrentMap<String, L2GatewayDevice> result = cachedMap.get(elanName);
91 public static List<L2GatewayDevice> getAllElanDevicesFromCache() {
92 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
93 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
94 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
95 if (cachedMap == null || cachedMap.isEmpty()) {
96 return Collections.emptyList();
99 List<L2GatewayDevice> l2GwDevices = new ArrayList<>();
100 for (ConcurrentMap<String, L2GatewayDevice> l2gwDevices : cachedMap.values()) {
101 for (L2GatewayDevice l2gwDevice : l2gwDevices.values()) {
102 l2GwDevices.add(l2gwDevice);