c9a7fd151fdd7ce2d7daadf7625f4f3827239b15
[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 com.google.common.base.Optional;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.Map;
14 import java.util.Set;
15 import java.util.concurrent.ConcurrentHashMap;
16 import javax.annotation.Nonnull;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.genius.mdsalutil.cache.InstanceIdDataObjectCache;
23 import org.opendaylight.infrautils.caches.CacheProvider;
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.absent();
57         }
58     }
59
60     @Nonnull
61     public Optional<EtreeInterface> getEtreeInterface(@Nonnull String interfaceName) {
62         Optional<ElanInterface> elanInterface = get(interfaceName);
63         return elanInterface.isPresent() ? Optional.fromNullable(
64                 elanInterface.get().getAugmentation(EtreeInterface.class)) : Optional.absent();
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         elanInstanceToInterfacesCache.computeIfAbsent(elanInterface.getElanInstanceName(),
76             key -> ConcurrentHashMap.newKeySet()).add(elanInterface.getName());
77     }
78
79     @Override
80     protected void removed(InstanceIdentifier<ElanInterface> path, ElanInterface elanInterface) {
81         String elanInstanceName = elanInterface.getElanInstanceName();
82         elanInstanceToInterfacesCache.computeIfPresent(elanInstanceName , (key, prevInterfacesSet) -> {
83             prevInterfacesSet.remove(elanInterface.getName());
84             return !prevInterfacesSet.isEmpty() ? prevInterfacesSet : null;
85         });
86     }
87 }