Remove redundant names in paths
[netvirt.git] / elanmanager / api / src / main / java / org / opendaylight / netvirt / elanmanager / utils / ElanL2GwCacheUtils.java
1 /*
2  * Copyright © 2016, 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.elanmanager.utils;
10
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;
17
18 public final class ElanL2GwCacheUtils {
19
20     private static final ConcurrentHashMap<String, L2GatewayDevice> EMPTY_MAP = new ConcurrentHashMap<>();
21     public static final String L2GATEWAY_CONN_CACHE_NAME = "L2GWCONN";
22
23     private ElanL2GwCacheUtils() {
24     }
25
26     static {
27         CacheUtil.createCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
28     }
29
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<>());
38             }
39         }
40         deviceMap.put(l2GwDevice.getHwvtepNodeId(), l2GwDevice);
41     }
42
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));
48     }
49
50
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);
58         } else {
59             return null;
60         }
61     }
62
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);
70         } else {
71             return null;
72         }
73     }
74
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);
80         if (result == null) {
81             result = EMPTY_MAP;
82         }
83         return result;
84     }
85
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()) {
91             return null;
92         }
93
94         List<L2GatewayDevice> l2GwDevices = new ArrayList<>();
95         for (ConcurrentMap<String, L2GatewayDevice> l2gwDevices : cachedMap.values()) {
96             for (L2GatewayDevice l2gwDevice : l2gwDevices.values()) {
97                 l2GwDevices.add(l2gwDevice);
98             }
99         }
100         return l2GwDevices;
101     }
102 }