From b91a541866bb3ed3de923ef9988b85cfd8f6e455 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 29 Jun 2019 13:07:27 +0200 Subject: [PATCH] Remove use of org.opendaylight.genius.utils.cache.CacheUtil This class has been deprecated, this patch updates ElanL2GwCacheUtils to work with a LoadingCache and ConcurrentHashMap, updating CLI access to match. Change-Id: I448b0d8445ded9c466a38ba56f846413b7bd48af Signed-off-by: Robert Varga --- .../elanmanager/utils/ElanL2GwCacheUtils.java | 85 ++++++------------- .../elan/cli/l2gw/L2GwUtilsCacheCli.java | 45 ++++------ 2 files changed, 42 insertions(+), 88 deletions(-) diff --git a/elanmanager/api/src/main/java/org/opendaylight/netvirt/elanmanager/utils/ElanL2GwCacheUtils.java b/elanmanager/api/src/main/java/org/opendaylight/netvirt/elanmanager/utils/ElanL2GwCacheUtils.java index c28b794509..bcd47d8678 100644 --- a/elanmanager/api/src/main/java/org/opendaylight/netvirt/elanmanager/utils/ElanL2GwCacheUtils.java +++ b/elanmanager/api/src/main/java/org/opendaylight/netvirt/elanmanager/utils/ElanL2GwCacheUtils.java @@ -5,102 +5,69 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.netvirt.elanmanager.utils; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import java.util.ArrayList; -import java.util.Collections; import java.util.List; +import java.util.Map.Entry; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; -import org.opendaylight.genius.utils.cache.CacheUtil; import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice; public final class ElanL2GwCacheUtils { private static final ConcurrentHashMap EMPTY_MAP = new ConcurrentHashMap<>(); - public static final String L2GATEWAY_CONN_CACHE_NAME = "L2GWCONN"; + private static final LoadingCache> CACHES = CacheBuilder.newBuilder() + .build(new CacheLoader>() { + @Override + public ConcurrentMap load(String key) { + return new ConcurrentHashMap<>(); + } + }); private ElanL2GwCacheUtils() { } - static { - CacheUtil.createCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME); - } - public static void addL2GatewayDeviceToCache(String elanName, L2GatewayDevice l2GwDevice) { - ConcurrentMap> cachedMap = - (ConcurrentMap>) CacheUtil.getCache( - ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME); - ConcurrentMap deviceMap = cachedMap.get(elanName); - if (deviceMap == null) { - synchronized (ElanL2GwCacheUtils.class) { - deviceMap = cachedMap.computeIfAbsent(elanName, k -> new ConcurrentHashMap<>()); - } - } - deviceMap.put(l2GwDevice.getHwvtepNodeId(), l2GwDevice); + CACHES.getUnchecked(elanName).put(l2GwDevice.getHwvtepNodeId(), l2GwDevice); } public static void removeL2GatewayDeviceFromAllElanCache(String deviceName) { - ConcurrentMap> cachedMap = - (ConcurrentMap>) CacheUtil.getCache( - ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME); - cachedMap.values().forEach(deviceMap -> deviceMap.remove(deviceName)); + CACHES.asMap().values().forEach(deviceMap -> deviceMap.remove(deviceName)); } @Nullable public static L2GatewayDevice removeL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) { - ConcurrentMap> cachedMap = - (ConcurrentMap>) CacheUtil.getCache( - ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME); - ConcurrentMap deviceMap = cachedMap.get(elanName); - if (deviceMap != null) { - return deviceMap.remove(l2gwDeviceNodeId); - } else { - return null; - } + ConcurrentMap deviceMap = CACHES.getIfPresent(elanName); + return deviceMap == null ? null : deviceMap.remove(l2gwDeviceNodeId); } @Nullable public static L2GatewayDevice getL2GatewayDeviceFromCache(String elanName, String l2gwDeviceNodeId) { - ConcurrentMap> cachedMap = - (ConcurrentMap>) CacheUtil.getCache( - ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME); - ConcurrentMap deviceMap = cachedMap.get(elanName); - if (deviceMap != null) { - return deviceMap.get(l2gwDeviceNodeId); - } else { - return null; - } + ConcurrentMap deviceMap = CACHES.getIfPresent(elanName); + return deviceMap == null ? null : deviceMap.get(l2gwDeviceNodeId); } public static ConcurrentMap getInvolvedL2GwDevices(String elanName) { - ConcurrentMap> cachedMap = - (ConcurrentMap>) CacheUtil - .getCache(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME); - ConcurrentMap result = cachedMap.get(elanName); - if (result == null) { - result = EMPTY_MAP; - } - return result; + ConcurrentMap result = CACHES.getIfPresent(elanName); + return result == null ? EMPTY_MAP : result; + } + + public static Set>> getCaches() { + return CACHES.asMap().entrySet(); } @NonNull public static List getAllElanDevicesFromCache() { - ConcurrentMap> cachedMap = - (ConcurrentMap>) CacheUtil.getCache( - ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME); - if (cachedMap == null || cachedMap.isEmpty()) { - return Collections.emptyList(); - } - List l2GwDevices = new ArrayList<>(); - for (ConcurrentMap l2gwDevices : cachedMap.values()) { - for (L2GatewayDevice l2gwDevice : l2gwDevices.values()) { - l2GwDevices.add(l2gwDevice); - } + for (ConcurrentMap cache : CACHES.asMap().values()) { + l2GwDevices.addAll(cache.values()); } return l2GwDevices; } diff --git a/elanmanager/impl/src/main/java/org/opendaylight/netvirt/elan/cli/l2gw/L2GwUtilsCacheCli.java b/elanmanager/impl/src/main/java/org/opendaylight/netvirt/elan/cli/l2gw/L2GwUtilsCacheCli.java index 933c1cad92..8d1dce075d 100644 --- a/elanmanager/impl/src/main/java/org/opendaylight/netvirt/elan/cli/l2gw/L2GwUtilsCacheCli.java +++ b/elanmanager/impl/src/main/java/org/opendaylight/netvirt/elan/cli/l2gw/L2GwUtilsCacheCli.java @@ -5,7 +5,6 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.netvirt.elan.cli.l2gw; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -21,7 +20,6 @@ import org.apache.karaf.shell.commands.Command; import org.apache.karaf.shell.commands.Option; import org.apache.karaf.shell.console.OsgiCommandSupport; import org.eclipse.jdt.annotation.Nullable; -import org.opendaylight.genius.utils.cache.CacheUtil; import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache; import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils; import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache; @@ -32,6 +30,8 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; @Command(scope = "l2gw", name = "show-cache", description = "display l2gateways cache") public class L2GwUtilsCacheCli extends OsgiCommandSupport { private static final String L2GATEWAY_CACHE_NAME = "L2GW"; + private static final String L2GATEWAY_CONN_CACHE_NAME = "L2GWCONN"; + private static final String DEMARCATION = "================================="; @Option(name = "-cache", aliases = {"--cache"}, description = "cache name", @@ -55,15 +55,23 @@ public class L2GwUtilsCacheCli extends OsgiCommandSupport { protected Object doExecute() throws IOException { if (cacheName == null) { session.getConsole().println("Available caches"); - session.getConsole().println(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME); + session.getConsole().println(L2GATEWAY_CONN_CACHE_NAME); session.getConsole().println(L2GATEWAY_CACHE_NAME); session.getConsole().println("HA"); session.getConsole().println("HA_EVENTS"); return null; } switch (cacheName) { - case ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME: - dumpElanL2GwCache(); + case L2GATEWAY_CONN_CACHE_NAME: + if (elanName == null) { + for (Entry> entry : ElanL2GwCacheUtils.getCaches()) { + print(entry.getKey(), entry.getValue().values()); + session.getConsole().println(DEMARCATION); + session.getConsole().println(DEMARCATION); + } + } else { + print(elanName, ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).values()); + } break; case L2GATEWAY_CACHE_NAME: dumpL2GwCache(); @@ -129,31 +137,10 @@ public class L2GwUtilsCacheCli extends OsgiCommandSupport { } } - private void dumpElanL2GwCache() { - if (elanName == null) { - ConcurrentMap> cache = - (ConcurrentMap>) CacheUtil.getCache( - cacheName); - if (cache == null) { - session.getConsole().println("no devices are present in elan cache"); - } else { - for (Entry> entry : cache.entrySet()) { - print(entry.getKey(), entry.getValue()); - session.getConsole().println(DEMARCATION); - session.getConsole().println(DEMARCATION); - } - } - return; - } - ConcurrentMap elanDevices = ElanL2GwCacheUtils - .getInvolvedL2GwDevices(elanName); - print(elanName, elanDevices); - } - - private void print(String elan, ConcurrentMap devices) { + private void print(String elan, Collection devices) { session.getConsole().println("Elan name : " + elan); - session.getConsole().println("No of devices in elan " + devices.keySet().size()); - for (L2GatewayDevice device : devices.values()) { + session.getConsole().println("No of devices in elan " + devices.size()); + for (L2GatewayDevice device : devices) { session.getConsole().println("device " + device); } } -- 2.36.6