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.Collections;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
20 import java.util.Map.Entry;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24 import javax.inject.Singleton;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.opendaylight.netvirt.aclservice.api.utils.AclDataCache;
28 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionBase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
35 public class AclDataUtil implements AclDataCache {
37 private final ConcurrentMap<String, Acl> aclMap = new ConcurrentHashMap<>();
38 private final ConcurrentMap<Uuid, ConcurrentMap<String, AclInterface>> aclInterfaceMap = new ConcurrentHashMap<>();
39 private final ConcurrentMap<Uuid, Set<Uuid>> ingressRemoteAclIdMap = new ConcurrentHashMap<>();
40 private final ConcurrentMap<Uuid, Set<Uuid>> egressRemoteAclIdMap = new ConcurrentHashMap<>();
41 private final ConcurrentMap<String, Integer> aclTagMap = new ConcurrentHashMap<>();
48 public void addAcl(Acl acl) {
49 this.aclMap.put(acl.getAclName(), acl);
55 * @param aclName the acl name
58 public Acl removeAcl(String aclName) {
59 return this.aclMap.remove(aclName);
65 * @param aclName the acl name
69 public Acl getAcl(String aclName) {
70 return this.aclMap.get(aclName);
73 public void addOrUpdateAclInterfaceMap(List<Uuid> aclList, AclInterface port) {
74 for (Uuid acl : aclList) {
75 aclInterfaceMap.computeIfAbsent(acl, key -> new ConcurrentHashMap<>()).put(port.getInterfaceId(), port);
79 public void removeAclInterfaceMap(List<Uuid> aclList, AclInterface port) {
80 for (Uuid acl : aclList) {
81 removeAclInterfaceMap(acl, port);
85 public void removeAclInterfaceMap(Uuid acl, AclInterface port) {
86 ConcurrentMap<String, AclInterface> interfaceMap = aclInterfaceMap.get(acl);
87 if (interfaceMap != null) {
88 interfaceMap.remove(port.getInterfaceId());
94 public Collection<AclInterface> getInterfaceList(Uuid acl) {
95 final ConcurrentMap<String, AclInterface> interfaceMap = aclInterfaceMap.get(acl);
96 return interfaceMap != null ? interfaceMap.values() : Collections.emptySet();
100 * Gets the set of ACL interfaces per ACL (in a map) which has specified
103 * @param remoteAclId the remote acl id
104 * @param direction the direction
105 * @return the set of ACL interfaces per ACL (in a map) which has specified
109 public Map<String, Set<AclInterface>> getRemoteAclInterfaces(Uuid remoteAclId,
110 Class<? extends DirectionBase> direction) {
111 Collection<Uuid> remoteAclList = getRemoteAcl(remoteAclId, direction);
112 if (remoteAclList == null) {
116 Map<String, Set<AclInterface>> mapOfAclWithInterfaces = new HashMap<>();
117 for (Uuid acl : remoteAclList) {
118 Collection<AclInterface> interfaces = getInterfaceList(acl);
119 if (!interfaces.isEmpty()) {
120 Set<AclInterface> interfaceSet = new HashSet<>(interfaces);
121 mapOfAclWithInterfaces.put(acl.getValue(), interfaceSet);
124 return mapOfAclWithInterfaces;
127 public void addRemoteAclId(Uuid remoteAclId, Uuid aclId, Class<? extends DirectionBase> direction) {
128 getRemoteAclIdMap(direction).computeIfAbsent(remoteAclId, key -> ConcurrentHashMap.newKeySet()).add(aclId);
131 public void removeRemoteAclId(Uuid remoteAclId, Uuid aclId, Class<? extends DirectionBase> direction) {
132 Set<Uuid> aclList = getRemoteAclIdMap(direction).get(remoteAclId);
133 if (aclList != null) {
134 aclList.remove(aclId);
139 public Collection<Uuid> getRemoteAcl(Uuid remoteAclId, Class<? extends DirectionBase> direction) {
140 return getRemoteAclIdMap(direction).get(remoteAclId);
144 * Gets the set of ACL interfaces per ACL (in a map) which has remote ACL.
146 * @param direction the direction
147 * @return the set of ACL interfaces per ACL (in a map) which has remote ACL.
149 public Map<String, Set<AclInterface>> getAllRemoteAclInterfaces(Class<? extends DirectionBase> direction) {
150 Map<String, Set<AclInterface>> mapOfAclWithInterfaces = new HashMap<>();
151 for (Uuid remoteAcl : getRemoteAclIdMap(direction).keySet()) {
152 Map<String, Set<AclInterface>> map = getRemoteAclInterfaces(remoteAcl, direction);
154 mapOfAclWithInterfaces.putAll(map);
158 return mapOfAclWithInterfaces;
162 * Adds the ACL tag to the cache.
164 * @param aclName the ACL name
165 * @param aclTag the ACL tag
167 public void addAclTag(final String aclName, final Integer aclTag) {
168 this.aclTagMap.put(aclName, aclTag);
172 * Removes the acl tag from the cache.
174 * @param aclName the acl name
175 * @return the previous value associated with key, or null if there was no
178 public Integer removeAclTag(final String aclName) {
179 return this.aclTagMap.remove(aclName);
183 * Gets the acl tag from the cache.
185 * @param aclName the acl name
186 * @return the acl tag
189 public Integer getAclTag(final String aclName) {
190 return this.aclTagMap.get(aclName);
194 * Checks if DPN has acl interface associated with it.
196 * @param dpnId the datapath ID of DPN
197 * @return true if DPN is associated with Acl interface, else false
199 public boolean doesDpnHaveAclInterface(BigInteger dpnId) {
200 return aclInterfaceMap.values().stream().anyMatch(map -> map.values().stream()
201 .anyMatch(aclInterface -> aclInterface.getDpId().equals(dpnId)));
205 public Map<Uuid, Collection<AclInterface>> getAclInterfaceMap() {
206 Builder<Uuid, Collection<AclInterface>> builder = ImmutableMap.builder();
207 for (Entry<Uuid, ConcurrentMap<String, AclInterface>> entry: aclInterfaceMap.entrySet()) {
208 builder.put(entry.getKey(), entry.getValue().values());
211 return builder.build();
214 private ConcurrentMap<Uuid, Set<Uuid>> getRemoteAclIdMap(Class<? extends DirectionBase> direction) {
215 return DirectionEgress.class.equals(direction) ? egressRemoteAclIdMap : ingressRemoteAclIdMap;
219 public Map<Uuid, Collection<Uuid>> getEgressRemoteAclIdMap() {
220 return ImmutableMap.copyOf(egressRemoteAclIdMap);
224 public Map<Uuid, Collection<Uuid>> getIngressRemoteAclIdMap() {
225 return ImmutableMap.copyOf(ingressRemoteAclIdMap);
229 public Map<String, Integer> getAclTagMap() {
230 return ImmutableMap.copyOf(aclTagMap);
234 public Map<String, Acl> getAclMap() {
235 return ImmutableMap.copyOf(aclMap);