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.Optional;
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;
33 * Caches ElanInterface instances by name and the set of ElanInterfacenames by elen instance name.
35 * @author Thomas Pantelis
38 public class ElanInterfaceCache extends InstanceIdDataObjectCache<ElanInterface> {
40 private static final Logger LOG = LoggerFactory.getLogger(ElanInterfaceCache.class);
42 private final Map<String, Set<String>> elanInstanceToInterfacesCache = new ConcurrentHashMap<>();
45 public ElanInterfaceCache(DataBroker dataBroker, CacheProvider cacheProvider) {
46 super(ElanInterface.class, dataBroker, LogicalDatastoreType.CONFIGURATION,
47 InstanceIdentifier.create(ElanInterfaces.class).child(ElanInterface.class), cacheProvider);
51 public Optional<ElanInterface> get(@NonNull String interfaceName) {
53 return get(ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName));
54 } catch (ReadFailedException e) {
55 LOG.warn("Error reading ElanInterface {}", interfaceName, e);
56 return Optional.empty();
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();
68 public Collection<String> getInterfaceNames(@NonNull String elanInstanceName) {
69 Set<String> removed = elanInstanceToInterfacesCache.remove(elanInstanceName);
70 return removed != null ? Collections.unmodifiableCollection(removed) : Collections.emptySet();
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());
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;