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 com.google.common.base.Optional;
11 import java.util.Collection;
12 import java.util.Collections;
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;
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.absent();
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();
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 elanInstanceToInterfacesCache.computeIfAbsent(elanInterface.getElanInstanceName(),
76 key -> ConcurrentHashMap.newKeySet()).add(elanInterface.getName());
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;