2 * Copyright (c) 2016 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.elanmanager.utils;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
16 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.L2GatewayDevice;
17 import org.opendaylight.vpnservice.utils.cache.CacheUtil;
19 public class ElanL2GwCacheUtils {
20 private static final ConcurrentHashMap<String, L2GatewayDevice> EMPTY_MAP = new ConcurrentHashMap<String, L2GatewayDevice>();
21 public static final String L2GATEWAY_CONN_CACHE_NAME = "L2GWCONN";
23 public static void createElanL2GwDeviceCache() {
24 if (CacheUtil.getCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME) == null) {
25 CacheUtil.createCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
29 public static void addL2GatewayDeviceToCache(String elanName, L2GatewayDevice l2GwDevice) {
30 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
31 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
32 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
33 ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
34 if (deviceMap == null) {
35 synchronized(ElanL2GwCacheUtils.class) {
36 deviceMap = cachedMap.get(elanName);
37 if (deviceMap == null) {
38 deviceMap = new ConcurrentHashMap<String, L2GatewayDevice>();
39 cachedMap.put(elanName, deviceMap);
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 for (String elanName : cachedMap.keySet()) {
51 ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
52 if (deviceMap != null) {
53 deviceMap.remove(deviceName);
59 public static L2GatewayDevice removeL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
60 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
61 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
62 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
63 ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
64 if (deviceMap != null) {
65 L2GatewayDevice device = deviceMap.remove(l2gwDeviceNodeId);
72 public static L2GatewayDevice getL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
73 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
74 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
75 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
76 ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
77 if (deviceMap != null) {
78 return deviceMap.get(l2gwDeviceNodeId);
84 public static ConcurrentMap<String, L2GatewayDevice> getInvolvedL2GwDevices(String elanName) {
85 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap = (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil
86 .getCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
87 ConcurrentMap<String, L2GatewayDevice> result = cachedMap.get(elanName);
94 public static List<L2GatewayDevice> getAllElanDevicesFromCache() {
95 List<String> l2GwsList = new ArrayList<>();
96 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
97 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
98 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
99 if (cachedMap == null || cachedMap.isEmpty()) {
103 List<L2GatewayDevice> l2GwDevices = new ArrayList<L2GatewayDevice>();
104 for (ConcurrentMap<String, L2GatewayDevice> l2gwDevices : cachedMap.values())
106 for (L2GatewayDevice l2gwDevice : l2gwDevices.values() ) {
107 l2GwDevices.add(l2gwDevice);