Switch to JDT annotations for Nullable and NonNull
[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 addAclInterfaceMap(List<Uuid> aclList, AclInterface port) {
74         for (Uuid acl : aclList) {
75             addAclInterface(acl, port);
76         }
77     }
78
79     private void addAclInterface(Uuid acl, AclInterface port) {
80         aclInterfaceMap.computeIfAbsent(acl, key -> new ConcurrentHashMap<>())
81                 .putIfAbsent(port.getInterfaceId(), port);
82     }
83
84     public void addOrUpdateAclInterfaceMap(List<Uuid> aclList, AclInterface port) {
85         for (Uuid acl : aclList) {
86             aclInterfaceMap.computeIfAbsent(acl, key -> new ConcurrentHashMap<>()).put(port.getInterfaceId(), port);
87         }
88     }
89
90     public void removeAclInterfaceMap(List<Uuid> aclList, AclInterface port) {
91         for (Uuid acl : aclList) {
92             removeAclInterfaceMap(acl, port);
93         }
94     }
95
96     public void removeAclInterfaceMap(Uuid acl, AclInterface port) {
97         ConcurrentMap<String, AclInterface> interfaceMap = aclInterfaceMap.get(acl);
98         if (interfaceMap != null) {
99             interfaceMap.remove(port.getInterfaceId());
100         }
101     }
102
103     @Override
104     @NonNull
105     public Collection<AclInterface> getInterfaceList(Uuid acl) {
106         final ConcurrentMap<String, AclInterface> interfaceMap = aclInterfaceMap.get(acl);
107         return interfaceMap != null ? interfaceMap.values() : Collections.emptySet();
108     }
109
110     /**
111      * Gets the set of ACL interfaces per ACL (in a map) which has specified
112      * remote ACL ID.
113      *
114      * @param remoteAclId the remote acl id
115      * @param direction the direction
116      * @return the set of ACL interfaces per ACL (in a map) which has specified
117      *         remote ACL ID.
118      */
119     @Nullable
120     public Map<String, Set<AclInterface>> getRemoteAclInterfaces(Uuid remoteAclId,
121             Class<? extends DirectionBase> direction) {
122         Collection<Uuid> remoteAclList = getRemoteAcl(remoteAclId, direction);
123         if (remoteAclList == null) {
124             return null;
125         }
126
127         Map<String, Set<AclInterface>> mapOfAclWithInterfaces = new HashMap<>();
128         for (Uuid acl : remoteAclList) {
129             Collection<AclInterface> interfaces = getInterfaceList(acl);
130             if (!interfaces.isEmpty()) {
131                 Set<AclInterface> interfaceSet = new HashSet<>(interfaces);
132                 mapOfAclWithInterfaces.put(acl.getValue(), interfaceSet);
133             }
134         }
135         return mapOfAclWithInterfaces;
136     }
137
138     public void addRemoteAclId(Uuid remoteAclId, Uuid aclId, Class<? extends DirectionBase> direction) {
139         getRemoteAclIdMap(direction).computeIfAbsent(remoteAclId, key -> ConcurrentHashMap.newKeySet()).add(aclId);
140     }
141
142     public void removeRemoteAclId(Uuid remoteAclId, Uuid aclId, Class<? extends DirectionBase> direction) {
143         Set<Uuid> aclList = getRemoteAclIdMap(direction).get(remoteAclId);
144         if (aclList != null) {
145             aclList.remove(aclId);
146         }
147     }
148
149     @Override
150     public Collection<Uuid> getRemoteAcl(Uuid remoteAclId, Class<? extends DirectionBase> direction) {
151         return getRemoteAclIdMap(direction).get(remoteAclId);
152     }
153
154     /**
155      * Gets the set of ACL interfaces per ACL (in a map) which has remote ACL.
156      *
157      * @param direction the direction
158      * @return the set of ACL interfaces per ACL (in a map) which has remote ACL.
159      */
160     public Map<String, Set<AclInterface>> getAllRemoteAclInterfaces(Class<? extends DirectionBase> direction) {
161         Map<String, Set<AclInterface>> mapOfAclWithInterfaces = new HashMap<>();
162         for (Uuid remoteAcl : getRemoteAclIdMap(direction).keySet()) {
163             Map<String, Set<AclInterface>> map = getRemoteAclInterfaces(remoteAcl, direction);
164             if (map != null) {
165                 mapOfAclWithInterfaces.putAll(map);
166             }
167         }
168
169         return mapOfAclWithInterfaces;
170     }
171
172     /**
173      * Adds the ACL tag to the cache.
174      *
175      * @param aclName the ACL name
176      * @param aclTag the ACL tag
177      */
178     public void addAclTag(final String aclName, final Integer aclTag) {
179         this.aclTagMap.put(aclName, aclTag);
180     }
181
182     /**
183      * Removes the acl tag from the cache.
184      *
185      * @param aclName the acl name
186      * @return the previous value associated with key, or null if there was no
187      *         mapping for key.
188      */
189     public Integer removeAclTag(final String aclName) {
190         return this.aclTagMap.remove(aclName);
191     }
192
193     /**
194      * Gets the acl tag from the cache.
195      *
196      * @param aclName the acl name
197      * @return the acl tag
198      */
199     @Override
200     public Integer getAclTag(final String aclName) {
201         return this.aclTagMap.get(aclName);
202     }
203
204     /**
205      * Checks if DPN has acl interface associated with it.
206      *
207      * @param dpnId the datapath ID of DPN
208      * @return true if DPN is associated with Acl interface, else false
209      */
210     public boolean doesDpnHaveAclInterface(BigInteger dpnId) {
211         return aclInterfaceMap.values().stream().anyMatch(map -> map.values().stream()
212                 .anyMatch(aclInterface -> aclInterface.getDpId().equals(dpnId)));
213     }
214
215     @Override
216     public Map<Uuid, Collection<AclInterface>> getAclInterfaceMap() {
217         Builder<Uuid, Collection<AclInterface>> builder = ImmutableMap.builder();
218         for (Entry<Uuid, ConcurrentMap<String, AclInterface>> entry: aclInterfaceMap.entrySet()) {
219             builder.put(entry.getKey(), entry.getValue().values());
220         }
221
222         return builder.build();
223     }
224
225     private ConcurrentMap<Uuid, Set<Uuid>> getRemoteAclIdMap(Class<? extends DirectionBase> direction) {
226         return DirectionEgress.class.equals(direction) ? egressRemoteAclIdMap : ingressRemoteAclIdMap;
227     }
228
229     @Override
230     public Map<Uuid, Collection<Uuid>> getEgressRemoteAclIdMap() {
231         return ImmutableMap.copyOf(egressRemoteAclIdMap);
232     }
233
234     @Override
235     public Map<Uuid, Collection<Uuid>> getIngressRemoteAclIdMap() {
236         return ImmutableMap.copyOf(ingressRemoteAclIdMap);
237     }
238
239     @Override
240     public Map<String, Integer> getAclTagMap() {
241         return ImmutableMap.copyOf(aclTagMap);
242     }
243
244     @Override
245     public Map<String, Acl> getAclMap() {
246         return ImmutableMap.copyOf(aclMap);
247     }
248 }