Handle nullable lists in elanmanager
[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 javax.annotation.Nullable;
16 import org.opendaylight.genius.utils.cache.CacheUtil;
17 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
18
19 public final class ElanL2GwCacheUtils {
20
21     private static final ConcurrentHashMap<String, L2GatewayDevice> EMPTY_MAP = new ConcurrentHashMap<>();
22     public static final String L2GATEWAY_CONN_CACHE_NAME = "L2GWCONN";
23
24     private ElanL2GwCacheUtils() {
25     }
26
27     static {
28         CacheUtil.createCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
29     }
30
31     public static void addL2GatewayDeviceToCache(String elanName, L2GatewayDevice l2GwDevice) {
32         ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
33                 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
34                         ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
35         ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
36         if (deviceMap == null) {
37             synchronized (ElanL2GwCacheUtils.class) {
38                 deviceMap = cachedMap.computeIfAbsent(elanName, k -> new ConcurrentHashMap<>());
39             }
40         }
41         deviceMap.put(l2GwDevice.getHwvtepNodeId(), l2GwDevice);
42     }
43
44     public static void removeL2GatewayDeviceFromAllElanCache(String deviceName) {
45         ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
46                 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
47                         ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
48         cachedMap.values().forEach(deviceMap -> deviceMap.remove(deviceName));
49     }
50
51     @Nullable
52     public static L2GatewayDevice removeL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
53         ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
54                 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
55                         ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
56         ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
57         if (deviceMap != null) {
58             return deviceMap.remove(l2gwDeviceNodeId);
59         } else {
60             return null;
61         }
62     }
63
64     @Nullable
65     public static L2GatewayDevice getL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) {
66         ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
67                 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
68                         ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
69         ConcurrentMap<String, L2GatewayDevice> deviceMap = cachedMap.get(elanName);
70         if (deviceMap != null) {
71             return deviceMap.get(l2gwDeviceNodeId);
72         } else {
73             return null;
74         }
75     }
76
77     public static ConcurrentMap<String, L2GatewayDevice> getInvolvedL2GwDevices(String elanName) {
78         ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
79                 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil
80                 .getCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
81         ConcurrentMap<String, L2GatewayDevice> result = cachedMap.get(elanName);
82         if (result == null) {
83             result = EMPTY_MAP;
84         }
85         return result;
86     }
87
88     @Nullable
89     public static List<L2GatewayDevice> getAllElanDevicesFromCache() {
90         ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cachedMap =
91                 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
92                         ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
93         if (cachedMap == null || cachedMap.isEmpty()) {
94             return null;
95         }
96
97         List<L2GatewayDevice> l2GwDevices = new ArrayList<>();
98         for (ConcurrentMap<String, L2GatewayDevice> l2gwDevices : cachedMap.values()) {
99             for (L2GatewayDevice l2gwDevice : l2gwDevices.values()) {
100                 l2GwDevices.add(l2gwDevice);
101             }
102         }
103         return l2GwDevices;
104     }
105 }