c91d84eac3fa7b441b6c1871cabc7cb63fb36053
[netvirt.git] / vpnservice / 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 java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
13
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;
26
27 public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEventListener> implements
28         ClusteredDataTreeChangeListener<Acl> {
29
30     private static final Logger LOG = LoggerFactory.getLogger(AclEventListener.class);
31     private AclServiceManager aclServiceManager;
32
33     public AclEventListener(final AclServiceManager aclServiceManager) {
34         super(Acl.class, AclEventListener.class);
35         this.aclServiceManager = aclServiceManager;
36     }
37
38     @Override
39     protected InstanceIdentifier<Acl> getWildCardPath() {
40         return InstanceIdentifier
41                 .create(AccessLists.class)
42                 .child(Acl.class);
43     }
44
45     @Override
46     protected void remove(InstanceIdentifier<Acl> key, Acl accessListEntry) {
47         // no need to handle here as Acl will be removed from AclInterfaceListener
48     }
49
50     @Override
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());
55             return;
56         }
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);
63
64     }
65
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);
72                 }
73             }
74         }
75     }
76
77     @Override
78     protected void add(InstanceIdentifier<Acl> key, Acl dataObjectModification) {
79         // no need to handle here as Acl will be added from AclInterfaceListener
80     }
81
82     @Override
83     protected AclEventListener getDataTreeChangeListener() {
84         return this;
85     }
86
87     private List<Ace> getChangedAceList(Acl updatedAcl, Acl currentAcl) {
88         if (updatedAcl == null) {
89             return null;
90         }
91         List<Ace> updatedAceList = new ArrayList<>(updatedAcl.getAccessListEntries().getAce());
92         if (currentAcl == null) {
93             return updatedAceList;
94         }
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())) {
100                     iterator.remove();
101                 }
102             }
103         }
104         return updatedAceList;
105     }
106 }