NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / cache / ElanInterfaceCache.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.Optional;
14 import java.util.Set;
15 import java.util.concurrent.ConcurrentHashMap;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.genius.mdsalutil.cache.InstanceIdDataObjectCache;
20 import org.opendaylight.infrautils.caches.CacheProvider;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.common.api.ReadFailedException;
24 import org.opendaylight.netvirt.elan.utils.ElanUtils;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Caches ElanInterface instances by name and the set of ElanInterfacenames by elen instance name.
34  *
35  * @author Thomas Pantelis
36  */
37 @Singleton
38 public class ElanInterfaceCache extends InstanceIdDataObjectCache<ElanInterface> {
39
40     private static final Logger LOG = LoggerFactory.getLogger(ElanInterfaceCache.class);
41
42     private final Map<String, Set<String>> elanInstanceToInterfacesCache = new ConcurrentHashMap<>();
43
44     @Inject
45     public ElanInterfaceCache(DataBroker dataBroker, CacheProvider cacheProvider) {
46         super(ElanInterface.class, dataBroker, LogicalDatastoreType.CONFIGURATION,
47                 InstanceIdentifier.create(ElanInterfaces.class).child(ElanInterface.class), cacheProvider);
48     }
49
50     @NonNull
51     public Optional<ElanInterface> get(@NonNull String interfaceName) {
52         try {
53             return get(ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName));
54         } catch (ReadFailedException e) {
55             LOG.warn("Error reading ElanInterface {}", interfaceName, e);
56             return Optional.empty();
57         }
58     }
59
60     @NonNull
61     public Optional<EtreeInterface> getEtreeInterface(@NonNull String interfaceName) {
62         Optional<ElanInterface> elanInterface = get(interfaceName);
63         return elanInterface.isPresent() ? Optional.ofNullable(
64                 elanInterface.get().augmentation(EtreeInterface.class)) : Optional.empty();
65     }
66
67     @NonNull
68     public Collection<String> getInterfaceNames(@NonNull String elanInstanceName) {
69         Set<String> removed = elanInstanceToInterfacesCache.remove(elanInstanceName);
70         return removed != null ? Collections.unmodifiableCollection(removed) : Collections.emptySet();
71     }
72
73     @Override
74     protected void added(InstanceIdentifier<ElanInterface> path, ElanInterface elanInterface) {
75         if (null != elanInterface.getElanInstanceName() && null != elanInterface.getName()) {
76             elanInstanceToInterfacesCache.computeIfAbsent(elanInterface.getElanInstanceName(),
77                 key -> ConcurrentHashMap.newKeySet()).add(elanInterface.getName());
78         }
79     }
80
81     @Override
82     protected void removed(InstanceIdentifier<ElanInterface> path, ElanInterface elanInterface) {
83         String elanInstanceName = elanInterface.getElanInstanceName();
84         elanInstanceToInterfacesCache.computeIfPresent(elanInstanceName , (key, prevInterfacesSet) -> {
85             prevInterfacesSet.remove(elanInterface.getName());
86             return !prevInterfacesSet.isEmpty() ? prevInterfacesSet : null;
87         });
88     }
89 }