ACL: VM IP address failures and ID Pool issues
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / listeners / AclEventListener.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 package org.opendaylight.netvirt.aclservice.listeners;
9
10 import com.google.common.collect.ImmutableSet;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Objects;
18 import java.util.Set;
19 import java.util.SortedSet;
20 import javax.annotation.PostConstruct;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
29 import org.opendaylight.netvirt.aclservice.api.AclInterfaceCache;
30 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
31 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
32 import org.opendaylight.netvirt.aclservice.utils.AclClusterUtil;
33 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
34 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
35 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
36 import org.opendaylight.serviceutils.srm.RecoverableListener;
37 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.AccessLists;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionBase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionIngress;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 @Singleton
51 public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEventListener> implements
52         ClusteredDataTreeChangeListener<Acl>, RecoverableListener {
53
54     private static final Logger LOG = LoggerFactory.getLogger(AclEventListener.class);
55
56     private final AclServiceManager aclServiceManager;
57     private final AclClusterUtil aclClusterUtil;
58     private final DataBroker dataBroker;
59     private final AclDataUtil aclDataUtil;
60     private final AclServiceUtils aclServiceUtils;
61     private final AclInterfaceCache aclInterfaceCache;
62
63     @Inject
64     public AclEventListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil, DataBroker dataBroker,
65             AclDataUtil aclDataUtil, AclServiceUtils aclServicUtils, AclInterfaceCache aclInterfaceCache,
66             ServiceRecoveryRegistry serviceRecoveryRegistry) {
67         super(Acl.class, AclEventListener.class);
68         this.aclServiceManager = aclServiceManager;
69         this.aclClusterUtil = aclClusterUtil;
70         this.dataBroker = dataBroker;
71         this.aclDataUtil = aclDataUtil;
72         this.aclServiceUtils = aclServicUtils;
73         this.aclInterfaceCache = aclInterfaceCache;
74         serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
75     }
76
77     @Override
78     @PostConstruct
79     public void init() {
80         LOG.info("{} start", getClass().getSimpleName());
81         registerListener();
82     }
83
84     @Override
85     public void registerListener() {
86         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
87     }
88
89     @Override
90     protected InstanceIdentifier<Acl> getWildCardPath() {
91         return InstanceIdentifier.create(AccessLists.class).child(Acl.class);
92     }
93
94     @Override
95     protected void remove(InstanceIdentifier<Acl> key, Acl acl) {
96         LOG.trace("On remove event, remove ACL: {}", acl);
97         String aclName = acl.getAclName();
98         this.aclDataUtil.removeAcl(aclName);
99         Integer aclTag = this.aclDataUtil.getAclTag(aclName);
100         if (aclTag != null) {
101             this.aclDataUtil.removeAclTag(aclName);
102         }
103         updateRemoteAclCache(acl.getAccessListEntries().getAce(), aclName, AclServiceManager.Action.REMOVE);
104         if (aclClusterUtil.isEntityOwner()) {
105             if (aclTag != null) {
106                 this.aclServiceUtils.releaseAclTag(aclName);
107             }
108             // Handle Rule deletion If SG Remove event is received before SG Rule delete event
109             List<Ace> aceList = acl.getAccessListEntries().getAce();
110             Collection<AclInterface> aclInterfaces =
111                     ImmutableSet.copyOf(aclDataUtil.getInterfaceList(new Uuid(aclName)));
112             updateAceRules(aclInterfaces, aclName, aceList, AclServiceManager.Action.REMOVE);
113         }
114     }
115
116     @Override
117     protected void update(InstanceIdentifier<Acl> key, Acl aclBefore, Acl aclAfter) {
118         String aclName = aclAfter.getAclName();
119         Collection<AclInterface> interfacesBefore =
120                 ImmutableSet.copyOf(aclDataUtil.getInterfaceList(new Uuid(aclName)));
121         // Find and update added ace rules in acl
122         List<Ace> addedAceRules = getChangedAceList(aclAfter, aclBefore);
123
124         // Find and update deleted ace rules in acl
125         List<Ace> deletedAceRules = getDeletedAceList(aclAfter);
126
127         if (aclClusterUtil.isEntityOwner()) {
128             LOG.debug("On update event, remove Ace rules: {} for ACL: {}", deletedAceRules, aclName);
129             updateAceRules(interfacesBefore, aclName, deletedAceRules, AclServiceManager.Action.REMOVE);
130             if (null != deletedAceRules && !deletedAceRules.isEmpty()) {
131                 aclServiceUtils.deleteAcesFromConfigDS(aclName, deletedAceRules);
132             }
133         }
134         updateAclCaches(aclBefore, aclAfter, interfacesBefore);
135
136         if (aclClusterUtil.isEntityOwner()) {
137             LOG.debug("On update event, add Ace rules: {} for ACL: {}", addedAceRules, aclName);
138             updateAceRules(interfacesBefore, aclName, addedAceRules, AclServiceManager.Action.ADD);
139
140             aclServiceManager.notifyAcl(aclBefore, aclAfter, interfacesBefore, AclServiceManager.Action.UPDATE);
141         }
142     }
143
144     private void updateAceRules(Collection<AclInterface> interfaceList, String aclName, List<Ace> aceList,
145             AclServiceManager.Action action) {
146         if (null != aceList && !aceList.isEmpty()) {
147             LOG.trace("update ace rules - action: {} , ace rules: {}", action.name(), aceList);
148             for (AclInterface port : interfaceList) {
149                 for (Ace aceRule : aceList) {
150                     aclServiceManager.notifyAce(port, action, aclName, aceRule);
151                 }
152             }
153         }
154     }
155
156     @Override
157     protected void add(InstanceIdentifier<Acl> key, Acl acl) {
158         LOG.trace("On add event, add ACL: {}", acl);
159         this.aclDataUtil.addAcl(acl);
160
161         String aclName = acl.getAclName();
162         Integer aclTag = this.aclServiceUtils.allocateAclTag(aclName);
163         if (aclTag != null && aclTag != AclConstants.INVALID_ACL_TAG) {
164             this.aclDataUtil.addAclTag(aclName, aclTag);
165         }
166
167         updateRemoteAclCache(acl.getAccessListEntries().getAce(), aclName, AclServiceManager.Action.ADD);
168     }
169
170     /**
171      * Update remote acl cache.
172      *
173      * @param aceList the ace list
174      * @param aclName the acl name
175      * @param action the action
176      */
177     private void updateRemoteAclCache(@Nullable List<Ace> aceList, String aclName, AclServiceManager.Action action) {
178         if (null == aceList) {
179             return;
180         }
181         for (Ace ace : aceList) {
182             SecurityRuleAttr aceAttributes = ace.augmentation(SecurityRuleAttr.class);
183             if (AclServiceUtils.doesAceHaveRemoteGroupId(aceAttributes)) {
184                 if (action == AclServiceManager.Action.ADD) {
185                     aclDataUtil.addRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName),
186                             aceAttributes.getDirection());
187                 } else {
188                     aclDataUtil.removeRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName),
189                             aceAttributes.getDirection());
190                 }
191             }
192         }
193     }
194
195     private void updateAclCaches(Acl aclBefore, Acl aclAfter, Collection<AclInterface> aclInterfaces) {
196         String aclName = aclAfter.getAclName();
197         Integer aclTag = this.aclDataUtil.getAclTag(aclName);
198         if (aclTag == null) {
199             aclTag = this.aclServiceUtils.allocateAclTag(aclName);
200             if (aclTag != null && aclTag != AclConstants.INVALID_ACL_TAG) {
201                 this.aclDataUtil.addAclTag(aclName, aclTag);
202             }
203         }
204         this.aclDataUtil.addAcl(aclAfter);
205
206         updateAclCaches(aclBefore, aclAfter, aclInterfaces, DirectionEgress.class);
207         updateAclCaches(aclBefore, aclAfter, aclInterfaces, DirectionIngress.class);
208     }
209
210     private void updateAclCaches(Acl aclBefore, Acl aclAfter, Collection<AclInterface> aclInterfaces,
211             Class<? extends DirectionBase> direction) {
212         Uuid aclId = new Uuid(aclAfter.getAclName());
213         Set<Uuid> remoteAclsBefore = AclServiceUtils.getRemoteAclIdsByDirection(aclBefore, direction);
214         Set<Uuid> remoteAclsAfter = AclServiceUtils.getRemoteAclIdsByDirection(aclAfter, direction);
215
216         Set<Uuid> remoteAclsDeleted = new HashSet<>(remoteAclsBefore);
217         remoteAclsDeleted.removeAll(remoteAclsAfter);
218         for (Uuid remoteAcl : remoteAclsDeleted) {
219             aclDataUtil.removeRemoteAclId(remoteAcl, aclId, direction);
220         }
221
222         Set<Uuid> remoteAclsAdded = new HashSet<>(remoteAclsAfter);
223         remoteAclsAdded.removeAll(remoteAclsBefore);
224         for (Uuid remoteAcl : remoteAclsAdded) {
225             aclDataUtil.addRemoteAclId(remoteAcl, aclId, direction);
226         }
227
228         if (remoteAclsDeleted.isEmpty() && remoteAclsAdded.isEmpty()) {
229             return;
230         }
231
232         if (aclInterfaces != null) {
233             for (AclInterface aclInterface : aclInterfaces) {
234                 AclInterface aclInterfaceInCache =
235                         aclInterfaceCache.addOrUpdate(aclInterface.getInterfaceId(), (prevAclInterface, builder) -> {
236                             SortedSet<Integer> remoteAclTags =
237                                     aclServiceUtils.getRemoteAclTags(aclInterface.getSecurityGroups(), direction);
238                             if (DirectionEgress.class.equals(direction)) {
239                                 builder.egressRemoteAclTags(remoteAclTags);
240                             } else {
241                                 builder.ingressRemoteAclTags(remoteAclTags);
242                             }
243                         });
244
245                 aclDataUtil.addOrUpdateAclInterfaceMap(aclInterface.getSecurityGroups(), aclInterfaceInCache);
246             }
247         }
248     }
249
250     @Override
251     protected AclEventListener getDataTreeChangeListener() {
252         return this;
253     }
254
255     @NonNull
256     private List<Ace> getChangedAceList(Acl updatedAcl, Acl currentAcl) {
257         if (updatedAcl == null) {
258             return Collections.emptyList();
259         }
260         List<Ace> updatedAceList =
261             updatedAcl.getAccessListEntries() == null || updatedAcl.getAccessListEntries().getAce() == null
262                 ? new ArrayList<>()
263                 : new ArrayList<>(updatedAcl.getAccessListEntries().getAce());
264         if (currentAcl == null) {
265             return updatedAceList;
266         }
267         List<Ace> currentAceList =
268             currentAcl.getAccessListEntries() == null || currentAcl.getAccessListEntries().getAce() == null
269                 ? new ArrayList<>()
270                 : new ArrayList<>(currentAcl.getAccessListEntries().getAce());
271         for (Iterator<Ace> iterator = updatedAceList.iterator(); iterator.hasNext();) {
272             Ace ace1 = iterator.next();
273             for (Ace ace2 : currentAceList) {
274                 if (Objects.equals(ace1.getRuleName(), ace2.getRuleName())) {
275                     iterator.remove();
276                 }
277             }
278         }
279         return updatedAceList;
280     }
281
282     private List<Ace> getDeletedAceList(Acl acl) {
283         if (acl == null || acl.getAccessListEntries() == null || acl.getAccessListEntries().getAce() == null) {
284             return null;
285         }
286         List<Ace> aceList = acl.getAccessListEntries().getAce();
287         List<Ace> deletedAceList = new ArrayList<>();
288         for (Ace ace: aceList) {
289             if (ace.augmentation(SecurityRuleAttr.class).isDeleted()) {
290                 deletedAceList.add(ace);
291             }
292         }
293         return deletedAceList;
294     }
295 }