Fix & un-@Ignore AclServiceTest
[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 import javax.annotation.PostConstruct;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
20 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
21 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
22 import org.opendaylight.netvirt.aclservice.utils.AclClusterUtil;
23 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.AccessLists;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @Singleton
34 public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEventListener> implements
35         ClusteredDataTreeChangeListener<Acl> {
36
37     private static final Logger LOG = LoggerFactory.getLogger(AclEventListener.class);
38     private final AclServiceManager aclServiceManager;
39     private final AclClusterUtil aclClusterUtil;
40     private final DataBroker dataBroker;
41
42     @Inject
43     public AclEventListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil, DataBroker dataBroker) {
44         super(Acl.class, AclEventListener.class);
45         this.aclServiceManager = aclServiceManager;
46         this.aclClusterUtil = aclClusterUtil;
47         this.dataBroker = dataBroker;
48     }
49
50     @Override
51     @PostConstruct
52     public void init() {
53         LOG.info("{} start", getClass().getSimpleName());
54         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
55     }
56
57     @Override
58     protected InstanceIdentifier<Acl> getWildCardPath() {
59         return InstanceIdentifier
60                 .create(AccessLists.class)
61                 .child(Acl.class);
62     }
63
64     @Override
65     protected void remove(InstanceIdentifier<Acl> key, Acl acl) {
66         updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.REMOVE);
67     }
68
69     @Override
70     protected void update(InstanceIdentifier<Acl> key, Acl aclBefore, Acl aclAfter) {
71         List<AclInterface> interfaceList = AclDataUtil.getInterfaceList(new Uuid(aclAfter.getAclName()));
72         // find and update added ace rules in acl
73         List<Ace> addedAceRules = getChangedAceList(aclAfter, aclBefore);
74         updateRemoteAclCache(addedAceRules, aclAfter.getAclName(), AclServiceManager.Action.ADD);
75         if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
76             updateAceRules(interfaceList, addedAceRules, AclServiceManager.Action.ADD);
77         }
78         // find and update deleted ace rules in acl
79         List<Ace> deletedAceRules = getChangedAceList(aclBefore, aclAfter);
80         if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
81             updateAceRules(interfaceList, deletedAceRules, AclServiceManager.Action.REMOVE);
82         }
83         updateRemoteAclCache(deletedAceRules, aclAfter.getAclName(), AclServiceManager.Action.REMOVE);
84
85     }
86
87     private void updateAceRules(List<AclInterface> interfaceList, List<Ace> aceList, AclServiceManager.Action action) {
88         if (null != aceList && !aceList.isEmpty()) {
89             LOG.trace("update ace rules - action: {} , ace rules: {}", action.name(), aceList);
90             for (AclInterface port : interfaceList) {
91                 for (Ace aceRule : aceList) {
92                     aclServiceManager.notifyAce(port, action, aceRule);
93                 }
94             }
95         }
96     }
97
98     @Override
99     protected void add(InstanceIdentifier<Acl> key, Acl acl) {
100         updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.ADD);
101     }
102
103     private void updateRemoteAclCache(List<Ace> aceList, String aclName, AclServiceManager.Action action) {
104         if (null == aceList) {
105             return;
106         }
107         for (Ace ace : aceList) {
108             SecurityRuleAttr aceAttributes = ace.getAugmentation(SecurityRuleAttr.class);
109             if (aceAttributes != null && aceAttributes.getRemoteGroupId() != null) {
110                 if (action == AclServiceManager.Action.ADD) {
111                     AclDataUtil.addRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
112                 } else {
113                     AclDataUtil.removeRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
114                 }
115             }
116         }
117     }
118
119     @Override
120     protected AclEventListener getDataTreeChangeListener() {
121         return this;
122     }
123
124     private List<Ace> getChangedAceList(Acl updatedAcl, Acl currentAcl) {
125         if (updatedAcl == null) {
126             return null;
127         }
128         List<Ace> updatedAceList = new ArrayList<>(updatedAcl.getAccessListEntries().getAce());
129         if (currentAcl == null) {
130             return updatedAceList;
131         }
132         List<Ace> currentAceList = new ArrayList<>(currentAcl.getAccessListEntries().getAce());
133         for (Iterator<Ace> iterator = updatedAceList.iterator(); iterator.hasNext(); ) {
134             Ace ace1 = iterator.next();
135             for (Ace ace2 : currentAceList) {
136                 if (ace1.getRuleName().equals(ace2.getRuleName())) {
137                     iterator.remove();
138                 }
139             }
140         }
141         return updatedAceList;
142     }
143 }