2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.netvirt.aclservice.utils;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableMap.Builder;
13 import java.math.BigInteger;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
19 import java.util.Map.Entry;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.ConcurrentMap;
23 import javax.inject.Singleton;
24 import org.opendaylight.netvirt.aclservice.api.utils.AclDataCache;
25 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
30 public class AclDataUtil implements AclDataCache {
32 private final ConcurrentMap<Uuid, ConcurrentMap<String, AclInterface>> aclInterfaceMap = new ConcurrentHashMap<>();
33 private final Map<Uuid, Set<Uuid>> remoteAclIdMap = new ConcurrentHashMap<>();
34 private final Map<String, Integer> aclTagMap = new ConcurrentHashMap<>();
36 public void addAclInterfaceMap(List<Uuid> aclList, AclInterface port) {
37 for (Uuid acl : aclList) {
38 addAclInterface(acl, port);
42 private void addAclInterface(Uuid acl, AclInterface port) {
43 aclInterfaceMap.computeIfAbsent(acl, key -> new ConcurrentHashMap<>())
44 .putIfAbsent(port.getInterfaceId(), port);
47 public void addOrUpdateAclInterfaceMap(List<Uuid> aclList, AclInterface port) {
48 for (Uuid acl : aclList) {
49 aclInterfaceMap.computeIfAbsent(acl, key -> new ConcurrentHashMap<>()).put(port.getInterfaceId(), port);
53 public void removeAclInterfaceMap(List<Uuid> aclList, AclInterface port) {
54 for (Uuid acl : aclList) {
55 ConcurrentMap<String, AclInterface> interfaceMap = aclInterfaceMap.get(acl);
56 if (interfaceMap != null) {
57 interfaceMap.remove(port.getInterfaceId());
63 public Collection<AclInterface> getInterfaceList(Uuid acl) {
64 final ConcurrentMap<String, AclInterface> interfaceMap = aclInterfaceMap.get(acl);
65 return interfaceMap != null ? interfaceMap.values() : null;
69 * Gets the set of ACL interfaces per ACL (in a map) which has specified
72 * @param remoteAclId the remote acl id
73 * @return the set of ACL interfaces per ACL (in a map) which has specified
76 public Map<String, Set<AclInterface>> getRemoteAclInterfaces(Uuid remoteAclId) {
77 Collection<Uuid> remoteAclList = getRemoteAcl(remoteAclId);
78 if (remoteAclList == null) {
82 Map<String, Set<AclInterface>> mapOfAclWithInterfaces = new HashMap<>();
83 for (Uuid acl : remoteAclList) {
84 Set<AclInterface> interfaceSet = new HashSet<>();
85 Collection<AclInterface> interfaces = getInterfaceList(acl);
86 if (interfaces != null && !interfaces.isEmpty()) {
87 interfaceSet.addAll(interfaces);
88 mapOfAclWithInterfaces.put(acl.getValue(), interfaceSet);
91 return mapOfAclWithInterfaces;
94 public void addRemoteAclId(Uuid remoteAclId, Uuid aclId) {
95 remoteAclIdMap.computeIfAbsent(remoteAclId, key -> ConcurrentHashMap.newKeySet()).add(aclId);
98 public void removeRemoteAclId(Uuid remoteAclId, Uuid aclId) {
99 Set<Uuid> aclList = remoteAclIdMap.get(remoteAclId);
100 if (aclList != null) {
101 aclList.remove(aclId);
106 public Collection<Uuid> getRemoteAcl(Uuid remoteAclId) {
107 return remoteAclIdMap.get(remoteAclId);
111 * Gets the set of ACL interfaces per ACL (in a map) which has remote ACL.
113 * @return the set of ACL interfaces per ACL (in a map) which has remote ACL.
115 public Map<String, Set<AclInterface>> getAllRemoteAclInterfaces() {
116 Map<String, Set<AclInterface>> mapOfAclWithInterfaces = new HashMap<>();
117 for (Uuid remoteAcl : remoteAclIdMap.keySet()) {
118 Map<String, Set<AclInterface>> map = getRemoteAclInterfaces(remoteAcl);
120 mapOfAclWithInterfaces.putAll(map);
124 return mapOfAclWithInterfaces;
128 * Adds the ACL tag to the cache.
130 * @param aclName the ACL name
131 * @param aclTag the ACL tag
133 public void addAclTag(final String aclName, final Integer aclTag) {
134 this.aclTagMap.put(aclName, aclTag);
138 * Removes the acl tag from the cache.
141 * @return the previous value associated with key, or null if there was no
144 public Integer removeAclTag(final String key) {
145 return this.aclTagMap.remove(key);
149 * Gets the acl tag from the cache.
151 * @param aclName the acl name
152 * @return the acl tag
155 public Integer getAclTag(final String aclName) {
156 return this.aclTagMap.get(aclName);
160 * Checks if DPN has acl interface associated with it.
162 * @param dpnId the datapath ID of DPN
163 * @return true if DPN is associated with Acl interface, else false
165 public boolean doesDpnHaveAclInterface(BigInteger dpnId) {
166 return aclInterfaceMap.values().stream().anyMatch(map -> map.values().stream()
167 .anyMatch(aclInterface -> aclInterface.getDpId().equals(dpnId)));
171 public Map<Uuid, Collection<AclInterface>> getAclInterfaceMap() {
172 Builder<Uuid, Collection<AclInterface>> builder = ImmutableMap.builder();
173 for (Entry<Uuid, ConcurrentMap<String, AclInterface>> entry: aclInterfaceMap.entrySet()) {
174 builder.put(entry.getKey(), entry.getValue().values());
177 return builder.build();
181 public Map<Uuid, Collection<Uuid>> getRemoteAclIdMap() {
182 return ImmutableMap.copyOf(remoteAclIdMap);
186 public Map<String, Integer> getAclTagMap() {
187 return ImmutableMap.copyOf(aclTagMap);