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 java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
14 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
15 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
16 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
17 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
18 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.AccessLists;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEventListener> implements
28 ClusteredDataTreeChangeListener<Acl> {
30 private static final Logger LOG = LoggerFactory.getLogger(AclEventListener.class);
31 private AclServiceManager aclServiceManager;
33 public AclEventListener(final AclServiceManager aclServiceManager) {
34 super(Acl.class, AclEventListener.class);
35 this.aclServiceManager = aclServiceManager;
39 protected InstanceIdentifier<Acl> getWildCardPath() {
40 return InstanceIdentifier
41 .create(AccessLists.class)
46 protected void remove(InstanceIdentifier<Acl> key, Acl accessListEntry) {
47 // no need to handle here as Acl will be removed from AclInterfaceListener
51 protected void update(InstanceIdentifier<Acl> key, Acl aclBefore, Acl aclAfter) {
52 List<AclInterface> interfaceList = AclDataUtil.getInterfaceList(new Uuid(aclAfter.getAclName()));
53 if (interfaceList == null || interfaceList.isEmpty()) {
54 LOG.debug("acl {} is not associated with any interface.", aclAfter.getAclName());
57 // find and update added ace rules in acl
58 List<Ace> addedAceRules = getChangedAceList(aclAfter, aclBefore);
59 updateAceRules(interfaceList, addedAceRules, AclServiceManager.Action.ADD);
60 // find and update deleted ace rules in acl
61 List<Ace> deletedAceRules = getChangedAceList(aclBefore, aclAfter);
62 updateAceRules(interfaceList, deletedAceRules, AclServiceManager.Action.REMOVE);
66 private void updateAceRules(List<AclInterface> interfaceList, List<Ace> aceList, AclServiceManager.Action action) {
67 if (null != aceList && !aceList.isEmpty()) {
68 LOG.trace("update ace rules - action: {} , ace rules: {}", action.name(), aceList);
69 for (AclInterface port : interfaceList) {
70 for (Ace aceRule : aceList) {
71 aclServiceManager.notifyAce(port, action, aceRule);
78 protected void add(InstanceIdentifier<Acl> key, Acl dataObjectModification) {
79 // no need to handle here as Acl will be added from AclInterfaceListener
83 protected AclEventListener getDataTreeChangeListener() {
87 private List<Ace> getChangedAceList(Acl updatedAcl, Acl currentAcl) {
88 if (updatedAcl == null) {
91 List<Ace> updatedAceList = new ArrayList<>(updatedAcl.getAccessListEntries().getAce());
92 if (currentAcl == null) {
93 return updatedAceList;
95 List<Ace> currentAceList = new ArrayList<>(currentAcl.getAccessListEntries().getAce());
96 for (Iterator<Ace> iterator = updatedAceList.iterator(); iterator.hasNext(); ) {
97 Ace ace1 = iterator.next();
98 for (Ace ace2 : currentAceList) {
99 if (ace1.getRuleName().equals(ace2.getRuleName())) {
104 return updatedAceList;