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.aclservice;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Map.Entry;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import java.util.function.BiConsumer;
17 import java.util.function.BiFunction;
18 import javax.inject.Singleton;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.netvirt.aclservice.api.AclInterfaceCache;
22 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
23 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface.Builder;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Implementation of the AclInterfaceCache interface.
30 * @author Thomas Pantelis
33 public class AclInterfaceCacheImpl implements AclInterfaceCache {
34 private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceCacheImpl.class);
36 private final ConcurrentMap<String, AclInterface> cache = new ConcurrentHashMap<>();
39 public AclInterface addOrUpdate(@NonNull String interfaceId,
40 BiConsumer<AclInterface, AclInterface.Builder> updateFunction) {
42 // First try to update the existing instance in the cache if one exists.
43 AclInterface aclInterface = cache.computeIfPresent(interfaceId,
44 (key, prevAclInterface) -> {
45 Builder builder = AclInterface.builder(prevAclInterface);
46 updateFunction.accept(prevAclInterface, builder);
47 return builder.build();
50 if (aclInterface == null) {
51 // No existing instance so try to put a new one.
52 Builder builder = AclInterface.builder();
53 builder.interfaceId(interfaceId);
54 updateFunction.accept(null, builder);
55 aclInterface = builder.build();
56 if (cache.putIfAbsent(interfaceId, aclInterface) == null) {
57 // The new instance was added.
61 // The new instance wasn't added - some one else beat us to it. Loop back up and try again.
69 public AclInterface updateIfPresent(String interfaceId,
70 BiFunction<AclInterface, AclInterface.Builder, Boolean> updateFunction) {
71 final AtomicBoolean updated = new AtomicBoolean(false);
72 AclInterface aclInterface = cache.computeIfPresent(interfaceId,
73 (key, prevAclInterface) -> {
74 Builder builder = AclInterface.builder(prevAclInterface);
75 updated.set(updateFunction.apply(prevAclInterface, builder));
76 return builder.build();
79 return updated.get() ? aclInterface : null;
84 public AclInterface remove(String interfaceId) {
85 AclInterface aclInterface = cache.get(interfaceId);
86 if (aclInterface == null) {
87 LOG.debug("AclInterface object not found in cache for interface {}", interfaceId);
91 if (aclInterface.isMarkedForDelete()) {
92 cache.remove(interfaceId);
94 aclInterface.setIsMarkedForDelete(true);
101 public AclInterface get(String interfaceId) {
102 return cache.get(interfaceId);
106 public Collection<Entry<String, AclInterface>> entries() {
107 return new ArrayList<>(cache.entrySet());