MRI version bumpup for Aluminium
[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.Map;
13 import java.util.Set;
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;
19
20 /**
21  * Maintains a cache of elan instance name to DpnInterfaces.
22  *
23  * @author Thomas Pantelis
24  */
25 @Singleton
26 public class ElanInstanceDpnsCache {
27     private final ConcurrentMap<String, Set<DpnInterfaces>> elanInstanceToDpnsCache = new ConcurrentHashMap<>();
28
29     public void add(@NonNull String elanInstanceName, @NonNull DpnInterfaces dpnInterfaces) {
30         elanInstanceToDpnsCache.computeIfAbsent(elanInstanceName, key -> ConcurrentHashMap.newKeySet())
31                 .add(dpnInterfaces);
32     }
33
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;
38         });
39     }
40
41     @NonNull
42     public Collection<DpnInterfaces> get(@NonNull String elanInstanceName) {
43         Set<DpnInterfaces> dpns = elanInstanceToDpnsCache.get(elanInstanceName);
44         return dpns != null ? Collections.unmodifiableCollection(dpns) : Collections.emptyList();
45     }
46
47     @NonNull
48     public Map<String, Set<DpnInterfaces>> getElanDpns() {
49         return elanInstanceToDpnsCache;
50     }
51 }