2 * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.cache;
10 import java.util.Collection;
11 import java.util.Collections;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
16 import javax.inject.Singleton;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
21 * Maintains a cache of elan instance name to DpnInterfaces.
23 * @author Thomas Pantelis
26 public class ElanInstanceDpnsCache {
27 private final ConcurrentMap<String, Set<DpnInterfaces>> elanInstanceToDpnsCache = new ConcurrentHashMap<>();
29 public void add(@NonNull String elanInstanceName, @NonNull DpnInterfaces dpnInterfaces) {
30 elanInstanceToDpnsCache.computeIfAbsent(elanInstanceName, key -> ConcurrentHashMap.newKeySet())
34 public void remove(@NonNull String elanInstanceName, @NonNull DpnInterfaces dpnInterfaces) {
35 elanInstanceToDpnsCache.computeIfPresent(elanInstanceName, (key, prevInterfacesSet) -> {
36 prevInterfacesSet.remove(dpnInterfaces);
37 return !prevInterfacesSet.isEmpty() ? prevInterfacesSet : null;
42 public Collection<DpnInterfaces> get(@NonNull String elanInstanceName) {
43 Set<DpnInterfaces> dpns = elanInstanceToDpnsCache.get(elanInstanceName);
44 return dpns != null ? Collections.unmodifiableCollection(dpns) : Collections.emptyList();
48 public Map<String, Set<DpnInterfaces>> getElanDpns() {
49 return elanInstanceToDpnsCache;