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
8 package org.opendaylight.netvirt.aclservice.listeners;
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;
19 import java.util.SortedSet;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import javax.annotation.PostConstruct;
23 import javax.inject.Inject;
24 import javax.inject.Singleton;
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;
51 public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEventListener> implements
52 ClusteredDataTreeChangeListener<Acl>, RecoverableListener {
54 private static final Logger LOG = LoggerFactory.getLogger(AclEventListener.class);
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;
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);
80 LOG.info("{} start", getClass().getSimpleName());
85 public void registerListener() {
86 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
90 protected InstanceIdentifier<Acl> getWildCardPath() {
91 return InstanceIdentifier.create(AccessLists.class).child(Acl.class);
95 protected void remove(InstanceIdentifier<Acl> key, Acl acl) {
96 String aclName = acl.getAclName();
97 if (!AclServiceUtils.isOfAclInterest(acl)) {
98 LOG.trace("{} does not have SecurityRuleAttr augmentation", aclName);
102 LOG.trace("On remove event, remove ACL: {}", acl);
103 this.aclDataUtil.removeAcl(aclName);
104 Integer aclTag = this.aclDataUtil.getAclTag(aclName);
105 if (aclTag != null) {
106 this.aclDataUtil.removeAclTag(aclName);
107 this.aclServiceUtils.releaseAclTag(aclName);
109 updateRemoteAclCache(acl.getAccessListEntries().getAce(), aclName, AclServiceManager.Action.REMOVE);
113 protected void update(InstanceIdentifier<Acl> key, Acl aclBefore, Acl aclAfter) {
114 if (!AclServiceUtils.isOfAclInterest(aclAfter) && !AclServiceUtils.isOfAclInterest(aclBefore)) {
115 LOG.trace("before {} and after {} does not have SecurityRuleAttr augmentation",
116 aclBefore.getAclName(), aclAfter.getAclName());
119 String aclName = aclAfter.getAclName();
120 Collection<AclInterface> interfacesBefore =
121 ImmutableSet.copyOf(aclDataUtil.getInterfaceList(new Uuid(aclName)));
122 // Find and update added ace rules in acl
123 List<Ace> addedAceRules = getChangedAceList(aclAfter, aclBefore);
125 // Find and update deleted ace rules in acl
126 List<Ace> deletedAceRules = getChangedAceList(aclBefore, aclAfter);
128 if (aclClusterUtil.isEntityOwner()) {
129 LOG.debug("On update event, remove Ace rules: {} for ACL: {}", deletedAceRules, aclName);
130 updateAceRules(interfacesBefore, aclName, deletedAceRules, AclServiceManager.Action.REMOVE);
132 updateAclCaches(aclBefore, aclAfter, interfacesBefore);
134 if (aclClusterUtil.isEntityOwner()) {
135 LOG.debug("On update event, add Ace rules: {} for ACL: {}", addedAceRules, aclName);
136 updateAceRules(interfacesBefore, aclName, addedAceRules, AclServiceManager.Action.ADD);
138 aclServiceManager.notifyAcl(aclBefore, aclAfter, interfacesBefore, AclServiceManager.Action.UPDATE);
142 private void updateAceRules(Collection<AclInterface> interfaceList, String aclName, List<Ace> aceList,
143 AclServiceManager.Action action) {
144 if (null != aceList && !aceList.isEmpty()) {
145 LOG.trace("update ace rules - action: {} , ace rules: {}", action.name(), aceList);
146 for (AclInterface port : interfaceList) {
147 for (Ace aceRule : aceList) {
148 aclServiceManager.notifyAce(port, action, aclName, aceRule);
155 protected void add(InstanceIdentifier<Acl> key, Acl acl) {
156 String aclName = acl.getAclName();
157 if (!AclServiceUtils.isOfAclInterest(acl)) {
158 LOG.trace("{} does not have SecurityRuleAttr augmentation", aclName);
162 LOG.trace("On add event, add ACL: {}", acl);
163 this.aclDataUtil.addAcl(acl);
165 Integer aclTag = this.aclServiceUtils.allocateAclTag(aclName);
166 if (aclTag != null && aclTag != AclConstants.INVALID_ACL_TAG) {
167 this.aclDataUtil.addAclTag(aclName, aclTag);
170 updateRemoteAclCache(acl.getAccessListEntries().getAce(), aclName, AclServiceManager.Action.ADD);
174 * Update remote acl cache.
176 * @param aceList the ace list
177 * @param aclName the acl name
178 * @param action the action
180 private void updateRemoteAclCache(@Nullable List<Ace> aceList, String aclName, AclServiceManager.Action action) {
181 if (null == aceList) {
184 for (Ace ace : aceList) {
185 SecurityRuleAttr aceAttributes = ace.augmentation(SecurityRuleAttr.class);
186 if (AclServiceUtils.doesAceHaveRemoteGroupId(aceAttributes)) {
187 if (action == AclServiceManager.Action.ADD) {
188 aclDataUtil.addRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName),
189 aceAttributes.getDirection());
191 aclDataUtil.removeRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName),
192 aceAttributes.getDirection());
198 private void updateAclCaches(Acl aclBefore, Acl aclAfter, Collection<AclInterface> aclInterfaces) {
199 String aclName = aclAfter.getAclName();
200 Integer aclTag = this.aclDataUtil.getAclTag(aclName);
201 if (aclTag == null) {
202 aclTag = this.aclServiceUtils.allocateAclTag(aclName);
203 if (aclTag != null && aclTag != AclConstants.INVALID_ACL_TAG) {
204 this.aclDataUtil.addAclTag(aclName, aclTag);
207 this.aclDataUtil.addAcl(aclAfter);
209 updateAclCaches(aclBefore, aclAfter, aclInterfaces, DirectionEgress.class);
210 updateAclCaches(aclBefore, aclAfter, aclInterfaces, DirectionIngress.class);
213 private void updateAclCaches(Acl aclBefore, Acl aclAfter, Collection<AclInterface> aclInterfaces,
214 Class<? extends DirectionBase> direction) {
215 Uuid aclId = new Uuid(aclAfter.getAclName());
216 Set<Uuid> remoteAclsBefore = AclServiceUtils.getRemoteAclIdsByDirection(aclBefore, direction);
217 Set<Uuid> remoteAclsAfter = AclServiceUtils.getRemoteAclIdsByDirection(aclAfter, direction);
219 Set<Uuid> remoteAclsDeleted = new HashSet<>(remoteAclsBefore);
220 remoteAclsDeleted.removeAll(remoteAclsAfter);
221 for (Uuid remoteAcl : remoteAclsDeleted) {
222 aclDataUtil.removeRemoteAclId(remoteAcl, aclId, direction);
225 Set<Uuid> remoteAclsAdded = new HashSet<>(remoteAclsAfter);
226 remoteAclsAdded.removeAll(remoteAclsBefore);
227 for (Uuid remoteAcl : remoteAclsAdded) {
228 aclDataUtil.addRemoteAclId(remoteAcl, aclId, direction);
231 if (remoteAclsDeleted.isEmpty() && remoteAclsAdded.isEmpty()) {
235 if (aclInterfaces != null) {
236 for (AclInterface aclInterface : aclInterfaces) {
237 AclInterface aclInterfaceInCache =
238 aclInterfaceCache.addOrUpdate(aclInterface.getInterfaceId(), (prevAclInterface, builder) -> {
239 SortedSet<Integer> remoteAclTags =
240 aclServiceUtils.getRemoteAclTags(aclInterface.getSecurityGroups(), direction);
241 if (DirectionEgress.class.equals(direction)) {
242 builder.egressRemoteAclTags(remoteAclTags);
244 builder.ingressRemoteAclTags(remoteAclTags);
248 aclDataUtil.addOrUpdateAclInterfaceMap(aclInterface.getSecurityGroups(), aclInterfaceInCache);
254 protected AclEventListener getDataTreeChangeListener() {
259 private List<Ace> getChangedAceList(Acl updatedAcl, Acl currentAcl) {
260 if (updatedAcl == null) {
261 return Collections.emptyList();
263 List<Ace> updatedAceList =
264 updatedAcl.getAccessListEntries() == null || updatedAcl.getAccessListEntries().getAce() == null
266 : new ArrayList<>(updatedAcl.getAccessListEntries().getAce());
267 if (currentAcl == null) {
268 return updatedAceList;
270 List<Ace> currentAceList =
271 currentAcl.getAccessListEntries() == null || currentAcl.getAccessListEntries().getAce() == null
273 : new ArrayList<>(currentAcl.getAccessListEntries().getAce());
274 for (Iterator<Ace> iterator = updatedAceList.iterator(); iterator.hasNext();) {
275 Ace ace1 = iterator.next();
276 for (Ace ace2 : currentAceList) {
277 if (Objects.equals(ace1.getRuleName(), ace2.getRuleName())) {
282 return updatedAceList;