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