Port update with no security groups
[netvirt.git] / 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.Collections;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.Set;
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;
33
34 @Singleton
35 public class AclDataUtil implements AclDataCache {
36
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<>();
42
43     /**
44      * Adds the acl.
45      *
46      * @param acl the acl
47      */
48     public void addAcl(Acl acl) {
49         this.aclMap.put(acl.getAclName(), acl);
50     }
51
52     /**
53      * Removes the acl.
54      *
55      * @param aclName the acl name
56      * @return the acl
57      */
58     public Acl removeAcl(String aclName) {
59         return this.aclMap.remove(aclName);
60     }
61
62     /**
63      * Gets the acl.
64      *
65      * @param aclName the acl name
66      * @return the acl
67      */
68     @Override
69     public Acl getAcl(String aclName) {
70         return this.aclMap.get(aclName);
71     }
72
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);
76         }
77     }
78
79     public void removeAclInterfaceMap(List<Uuid> aclList, AclInterface port) {
80         for (Uuid acl : aclList) {
81             removeAclInterfaceMap(acl, port);
82         }
83     }
84
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());
89         }
90     }
91
92     @Override
93     @NonNull
94     public Collection<AclInterface> getInterfaceList(Uuid acl) {
95         final ConcurrentMap<String, AclInterface> interfaceMap = aclInterfaceMap.get(acl);
96         return interfaceMap != null ? interfaceMap.values() : Collections.emptySet();
97     }
98
99     @SuppressWarnings("checkstyle:JavadocParagraph")
100     /**
101      * Gets set of ACL interfaces per ACL (in a map) for the specified remote ACL IDs.
102      *
103      * @param remoteAclIdList List of remote ACL Ids
104      * @param direction the direction
105      * @return set of ACL interfaces per ACL (in a map) for the specified remote ACL IDs.
106      *         Format: ConcurrentMap<<Remote-ACL-ID>, Map<<ACL-ID>, Set<AclInterface>>>
107      */
108     public ConcurrentMap<Uuid, Map<String, Set<AclInterface>>> getRemoteAclInterfaces(List<Uuid> remoteAclIdList,
109             Class<? extends DirectionBase> direction) {
110         ConcurrentMap<Uuid, Map<String, Set<AclInterface>>> mapOfAclWithInterfacesList = new ConcurrentHashMap<>();
111         for (Uuid remoteAclId : remoteAclIdList) {
112             Map<String, Set<AclInterface>> mapOfAclWithInterfaces = getRemoteAclInterfaces(remoteAclId, direction);
113             if (mapOfAclWithInterfaces != null) {
114                 mapOfAclWithInterfacesList.put(remoteAclId, mapOfAclWithInterfaces);
115             }
116         }
117         return mapOfAclWithInterfacesList;
118     }
119
120     /**
121      * Gets the set of ACL interfaces per ACL (in a map) which has specified
122      * remote ACL ID.
123      *
124      * @param remoteAclId the remote acl id
125      * @param direction the direction
126      * @return the set of ACL interfaces per ACL (in a map) which has specified
127      *         remote ACL ID.
128      */
129     @Nullable
130     public Map<String, Set<AclInterface>> getRemoteAclInterfaces(Uuid remoteAclId,
131             Class<? extends DirectionBase> direction) {
132         Collection<Uuid> remoteAclList = getRemoteAcl(remoteAclId, direction);
133         if (remoteAclList == null) {
134             return null;
135         }
136
137         Map<String, Set<AclInterface>> mapOfAclWithInterfaces = new HashMap<>();
138         for (Uuid acl : remoteAclList) {
139             Collection<AclInterface> interfaces = getInterfaceList(acl);
140             if (!interfaces.isEmpty()) {
141                 Set<AclInterface> interfaceSet = new HashSet<>(interfaces);
142                 mapOfAclWithInterfaces.put(acl.getValue(), interfaceSet);
143             }
144         }
145         return mapOfAclWithInterfaces;
146     }
147
148     public void addRemoteAclId(Uuid remoteAclId, Uuid aclId, Class<? extends DirectionBase> direction) {
149         getRemoteAclIdMap(direction).computeIfAbsent(remoteAclId, key -> ConcurrentHashMap.newKeySet()).add(aclId);
150     }
151
152     public void removeRemoteAclId(Uuid remoteAclId, Uuid aclId, Class<? extends DirectionBase> direction) {
153         Set<Uuid> aclList = getRemoteAclIdMap(direction).get(remoteAclId);
154         if (aclList != null) {
155             aclList.remove(aclId);
156         }
157     }
158
159     @Override
160     public Collection<Uuid> getRemoteAcl(Uuid remoteAclId, Class<? extends DirectionBase> direction) {
161         return getRemoteAclIdMap(direction).get(remoteAclId);
162     }
163
164     /**
165      * Gets the set of ACL interfaces per ACL (in a map) which has remote ACL.
166      *
167      * @param direction the direction
168      * @return the set of ACL interfaces per ACL (in a map) which has remote ACL.
169      */
170     public Map<String, Set<AclInterface>> getAllRemoteAclInterfaces(Class<? extends DirectionBase> direction) {
171         Map<String, Set<AclInterface>> mapOfAclWithInterfaces = new HashMap<>();
172         for (Uuid remoteAcl : getRemoteAclIdMap(direction).keySet()) {
173             Map<String, Set<AclInterface>> map = getRemoteAclInterfaces(remoteAcl, direction);
174             if (map != null) {
175                 mapOfAclWithInterfaces.putAll(map);
176             }
177         }
178
179         return mapOfAclWithInterfaces;
180     }
181
182     /**
183      * Adds the ACL tag to the cache.
184      *
185      * @param aclName the ACL name
186      * @param aclTag the ACL tag
187      */
188     public void addAclTag(final String aclName, final Integer aclTag) {
189         this.aclTagMap.put(aclName, aclTag);
190     }
191
192     /**
193      * Removes the acl tag from the cache.
194      *
195      * @param aclName the acl name
196      * @return the previous value associated with key, or null if there was no
197      *         mapping for key.
198      */
199     public Integer removeAclTag(final String aclName) {
200         return this.aclTagMap.remove(aclName);
201     }
202
203     /**
204      * Gets the acl tag from the cache.
205      *
206      * @param aclName the acl name
207      * @return the acl tag
208      */
209     @Override
210     public Integer getAclTag(final String aclName) {
211         return this.aclTagMap.get(aclName);
212     }
213
214     /**
215      * Checks if DPN has acl interface associated with it.
216      *
217      * @param dpnId the datapath ID of DPN
218      * @return true if DPN is associated with Acl interface, else false
219      */
220     public boolean doesDpnHaveAclInterface(BigInteger dpnId) {
221         return aclInterfaceMap.values().stream().anyMatch(map -> map.values().stream()
222                 .anyMatch(aclInterface -> aclInterface.getDpId().equals(dpnId)));
223     }
224
225     @Override
226     public Map<Uuid, Collection<AclInterface>> getAclInterfaceMap() {
227         Builder<Uuid, Collection<AclInterface>> builder = ImmutableMap.builder();
228         for (Entry<Uuid, ConcurrentMap<String, AclInterface>> entry: aclInterfaceMap.entrySet()) {
229             builder.put(entry.getKey(), entry.getValue().values());
230         }
231
232         return builder.build();
233     }
234
235     private ConcurrentMap<Uuid, Set<Uuid>> getRemoteAclIdMap(Class<? extends DirectionBase> direction) {
236         return DirectionEgress.class.equals(direction) ? egressRemoteAclIdMap : ingressRemoteAclIdMap;
237     }
238
239     @Override
240     public Map<Uuid, Collection<Uuid>> getEgressRemoteAclIdMap() {
241         return ImmutableMap.copyOf(egressRemoteAclIdMap);
242     }
243
244     @Override
245     public Map<Uuid, Collection<Uuid>> getIngressRemoteAclIdMap() {
246         return ImmutableMap.copyOf(ingressRemoteAclIdMap);
247     }
248
249     @Override
250     public Map<String, Integer> getAclTagMap() {
251         return ImmutableMap.copyOf(aclTagMap);
252     }
253
254     @Override
255     public Map<String, Acl> getAclMap() {
256         return ImmutableMap.copyOf(aclMap);
257     }
258 }