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;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import javax.inject.Singleton;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
20 * Maintains a cache of elan instance name to DpnInterfaces.
22 * @author Thomas Pantelis
25 public class ElanInstanceDpnsCache {
26 private final ConcurrentMap<String, Set<DpnInterfaces>> elanInstanceToDpnsCache = new ConcurrentHashMap<>();
28 public void add(@NonNull String elanInstanceName, @NonNull DpnInterfaces dpnInterfaces) {
29 elanInstanceToDpnsCache.computeIfAbsent(elanInstanceName, key -> ConcurrentHashMap.newKeySet())
33 public void remove(@NonNull String elanInstanceName, @NonNull DpnInterfaces dpnInterfaces) {
34 elanInstanceToDpnsCache.computeIfPresent(elanInstanceName, (key, prevInterfacesSet) -> {
35 prevInterfacesSet.remove(dpnInterfaces);
36 return !prevInterfacesSet.isEmpty() ? prevInterfacesSet : null;
41 public Collection<DpnInterfaces> get(@NonNull String elanInstanceName) {
42 Set<DpnInterfaces> dpns = elanInstanceToDpnsCache.get(elanInstanceName);
43 return dpns != null ? Collections.unmodifiableCollection(dpns) : Collections.emptyList();