ACL: Support for non-conntrack supported traffic.
[netvirt.git] / vpnservice / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / utils / AclDataUtil.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.aclservice.utils;
10
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;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Set;
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;
27
28
29 @Singleton
30 public class AclDataUtil implements AclDataCache {
31
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<>();
35
36     public void addAclInterfaceMap(List<Uuid> aclList, AclInterface port) {
37         for (Uuid acl : aclList) {
38             addAclInterface(acl, port);
39         }
40     }
41
42     private void addAclInterface(Uuid acl, AclInterface port) {
43         aclInterfaceMap.computeIfAbsent(acl, key -> new ConcurrentHashMap<>())
44                 .putIfAbsent(port.getInterfaceId(), port);
45     }
46
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);
50         }
51     }
52
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());
58             }
59         }
60     }
61
62     @Override
63     public Collection<AclInterface> getInterfaceList(Uuid acl) {
64         final ConcurrentMap<String, AclInterface> interfaceMap = aclInterfaceMap.get(acl);
65         return interfaceMap != null ? interfaceMap.values() : null;
66     }
67
68     /**
69      * Gets the set of ACL interfaces per ACL (in a map) which has specified
70      * remote ACL ID.
71      *
72      * @param remoteAclId the remote acl id
73      * @return the set of ACL interfaces per ACL (in a map) which has specified
74      *         remote ACL ID.
75      */
76     public Map<String, Set<AclInterface>> getRemoteAclInterfaces(Uuid remoteAclId) {
77         Collection<Uuid> remoteAclList = getRemoteAcl(remoteAclId);
78         if (remoteAclList == null) {
79             return null;
80         }
81
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);
89             }
90         }
91         return mapOfAclWithInterfaces;
92     }
93
94     public void addRemoteAclId(Uuid remoteAclId, Uuid aclId) {
95         remoteAclIdMap.computeIfAbsent(remoteAclId, key -> ConcurrentHashMap.newKeySet()).add(aclId);
96     }
97
98     public void removeRemoteAclId(Uuid remoteAclId, Uuid aclId) {
99         Set<Uuid> aclList = remoteAclIdMap.get(remoteAclId);
100         if (aclList != null) {
101             aclList.remove(aclId);
102         }
103     }
104
105     @Override
106     public Collection<Uuid> getRemoteAcl(Uuid remoteAclId) {
107         return remoteAclIdMap.get(remoteAclId);
108     }
109
110     /**
111      * Gets the set of ACL interfaces per ACL (in a map) which has remote ACL.
112      *
113      * @return the set of ACL interfaces per ACL (in a map) which has remote ACL.
114      */
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);
119             if (map != null) {
120                 mapOfAclWithInterfaces.putAll(map);
121             }
122         }
123
124         return mapOfAclWithInterfaces;
125     }
126
127     /**
128      * Adds the ACL tag to the cache.
129      *
130      * @param aclName the ACL name
131      * @param aclTag the ACL tag
132      */
133     public void addAclTag(final String aclName, final Integer aclTag) {
134         this.aclTagMap.put(aclName, aclTag);
135     }
136
137     /**
138      * Removes the acl tag from the cache.
139      *
140      * @param key the key
141      * @return the previous value associated with key, or null if there was no
142      *         mapping for key.
143      */
144     public Integer removeAclTag(final String key) {
145         return this.aclTagMap.remove(key);
146     }
147
148     /**
149      * Gets the acl tag from the cache.
150      *
151      * @param aclName the acl name
152      * @return the acl tag
153      */
154     @Override
155     public Integer getAclTag(final String aclName) {
156         return this.aclTagMap.get(aclName);
157     }
158
159     /**
160      * Checks if DPN has acl interface associated with it.
161      *
162      * @param dpnId the datapath ID of DPN
163      * @return true if DPN is associated with Acl interface, else false
164      */
165     public boolean doesDpnHaveAclInterface(BigInteger dpnId) {
166         return aclInterfaceMap.values().stream().anyMatch(map -> map.values().stream()
167                 .anyMatch(aclInterface -> aclInterface.getDpId().equals(dpnId)));
168     }
169
170     @Override
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());
175         }
176
177         return builder.build();
178     }
179
180     @Override
181     public Map<Uuid, Collection<Uuid>> getRemoteAclIdMap() {
182         return ImmutableMap.copyOf(remoteAclIdMap);
183     }
184
185     @Override
186     public Map<String, Integer> getAclTagMap() {
187         return ImmutableMap.copyOf(aclTagMap);
188     }
189 }