e751323a869e8d41750709a2ef0e84d398f78783
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / cache / ElanInstanceDpnsCache.java
1 /*
2  * Copyright (c) 2017 Inocybe Technologies 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 package org.opendaylight.netvirt.elan.cache;
9
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.Set;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import javax.annotation.Nonnull;
16 import javax.inject.Singleton;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
18
19 /**
20  * Maintains a cache of elan instance name to DpnInterfaces.
21  *
22  * @author Thomas Pantelis
23  */
24 @Singleton
25 public class ElanInstanceDpnsCache {
26     private final ConcurrentMap<String, Set<DpnInterfaces>> elanInstanceToDpnsCache = new ConcurrentHashMap<>();
27
28     public void add(@Nonnull String elanInstanceName, @Nonnull DpnInterfaces dpnInterfaces) {
29         elanInstanceToDpnsCache.computeIfAbsent(elanInstanceName, key -> ConcurrentHashMap.newKeySet())
30                 .add(dpnInterfaces);
31     }
32
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;
37         });
38     }
39
40     @Nonnull
41     public Collection<DpnInterfaces> get(@Nonnull String elanInstanceName) {
42         Set<DpnInterfaces> dpns = elanInstanceToDpnsCache.get(elanInstanceName);
43         return dpns != null ? Collections.unmodifiableCollection(dpns) : Collections.emptyList();
44     }
45 }