elanmanager: drop nullToEmpty and reqNonNullOrElse
[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.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;
20
21 public final class ElanL2GwCacheUtils {
22
23     private static final ConcurrentHashMap<String, L2GatewayDevice> EMPTY_MAP = new ConcurrentHashMap<>();
24     public static final String L2GATEWAY_CONN_CACHE_NAME = "L2GWCONN";
25
26     private ElanL2GwCacheUtils() {
27     }
28
29     static {
30         CacheUtil.createCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
31     }
32
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<>());
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         cachedMap.values().forEach(deviceMap -> deviceMap.remove(deviceName));
51     }
52
53     @Nullable
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);
61         } else {
62             return null;
63         }
64     }
65
66     @Nullable
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);
74         } else {
75             return null;
76         }
77     }
78
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);
84         if (result == null) {
85             result = EMPTY_MAP;
86         }
87         return result;
88     }
89
90     @Nonnull
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();
97         }
98
99         List<L2GatewayDevice> l2GwDevices = new ArrayList<>();
100         for (ConcurrentMap<String, L2GatewayDevice> l2gwDevices : cachedMap.values()) {
101             for (L2GatewayDevice l2gwDevice : l2gwDevices.values()) {
102                 l2GwDevices.add(l2gwDevice);
103             }
104         }
105         return l2GwDevices;
106     }
107 }