/* * Copyright © 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved. * * This program and the accompanying materials are made available under the * 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 java.util.ArrayList; import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import javax.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 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); } public static void removeL2GatewayDeviceFromAllElanCache(String deviceName) { ConcurrentMap> cachedMap = (ConcurrentMap>) CacheUtil.getCache( ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME); cachedMap.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; } } @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; } } 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; } @Nullable public static List getAllElanDevicesFromCache() { ConcurrentMap> cachedMap = (ConcurrentMap>) CacheUtil.getCache( ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME); if (cachedMap == null || cachedMap.isEmpty()) { return null; } List l2GwDevices = new ArrayList<>(); for (ConcurrentMap l2gwDevices : cachedMap.values()) { for (L2GatewayDevice l2gwDevice : l2gwDevices.values()) { l2GwDevices.add(l2gwDevice); } } return l2GwDevices; } }