Imported vpnservice as a subtree
[netvirt.git] / vpnservice / elanmanager / elanmanager-api / src / main / java / org / opendaylight / elanmanager / utils / ElanL2GwCacheUtils.java
1 /*
2  * Copyright (c) 2016 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.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
16 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.L2GatewayDevice;
17 import org.opendaylight.vpnservice.utils.cache.CacheUtil;
18
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";
22
23     public static void createElanL2GwDeviceCache() {
24         if (CacheUtil.getCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME) == null) {
25             CacheUtil.createCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
26         }
27     }
28
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);
40                 }
41             }
42         }
43         deviceMap.put(l2GwDevice.getHwvtepNodeId(), l2GwDevice);
44     }
45
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);
54             }
55         }
56     }
57
58
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);
66             return device;
67         } else {
68             return null;
69         }
70     }
71
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);
79         } else {
80             return null;
81         }
82     }
83
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);
88         if (result == null) {
89             result = EMPTY_MAP;
90         }
91         return result;
92     }
93
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()) {
100             return null;
101         }
102
103         List<L2GatewayDevice> l2GwDevices = new ArrayList<L2GatewayDevice>();
104         for (ConcurrentMap<String, L2GatewayDevice> l2gwDevices : cachedMap.values())
105         {
106             for (L2GatewayDevice l2gwDevice : l2gwDevices.values() ) {
107                 l2GwDevices.add(l2gwDevice);
108             }
109         }
110
111         return l2GwDevices;
112     }
113
114 }