Merge "Bug 6609: when 2 vm belonging to the same NETWORK/SUBNET get created in differ...
[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 AclClusterUtil aclClusterUtil;
41     private final DataBroker dataBroker;
42
43     @Inject
44     public AclEventListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil, DataBroker dataBroker) {
45         super(Acl.class, AclEventListener.class);
46         this.aclServiceManager = aclServiceManager;
47         this.aclClusterUtil = aclClusterUtil;
48         this.dataBroker = dataBroker;
49     }
50
51     @PostConstruct
52     // TODO new interface Lifecyle
53     public void start() {
54         LOG.info("{} start", getClass().getSimpleName());
55         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
56     }
57
58     @Override
59     @PreDestroy
60     // TODO make AsyncDataTreeChangeListenerBase implement new interface Lifecyle
61     public void close() throws Exception {
62         super.close();
63     }
64
65     @Override
66     protected InstanceIdentifier<Acl> getWildCardPath() {
67         return InstanceIdentifier
68                 .create(AccessLists.class)
69                 .child(Acl.class);
70     }
71
72     @Override
73     protected void remove(InstanceIdentifier<Acl> key, Acl acl) {
74         updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.REMOVE);
75     }
76
77     @Override
78     protected void update(InstanceIdentifier<Acl> key, Acl aclBefore, Acl aclAfter) {
79         List<AclInterface> interfaceList = AclDataUtil.getInterfaceList(new Uuid(aclAfter.getAclName()));
80         // find and update added ace rules in acl
81         List<Ace> addedAceRules = getChangedAceList(aclAfter, aclBefore);
82         updateRemoteAclCache(addedAceRules, aclAfter.getAclName(), AclServiceManager.Action.ADD);
83         if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
84             updateAceRules(interfaceList, addedAceRules, AclServiceManager.Action.ADD);
85         }
86         // find and update deleted ace rules in acl
87         List<Ace> deletedAceRules = getChangedAceList(aclBefore, aclAfter);
88         if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
89             updateAceRules(interfaceList, deletedAceRules, AclServiceManager.Action.REMOVE);
90         }
91         updateRemoteAclCache(deletedAceRules, aclAfter.getAclName(), AclServiceManager.Action.REMOVE);
92
93     }
94
95     private void updateAceRules(List<AclInterface> interfaceList, List<Ace> aceList, AclServiceManager.Action action) {
96         if (null != aceList && !aceList.isEmpty()) {
97             LOG.trace("update ace rules - action: {} , ace rules: {}", action.name(), aceList);
98             for (AclInterface port : interfaceList) {
99                 for (Ace aceRule : aceList) {
100                     aclServiceManager.notifyAce(port, action, aceRule);
101                 }
102             }
103         }
104     }
105
106     @Override
107     protected void add(InstanceIdentifier<Acl> key, Acl acl) {
108         updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.ADD);
109     }
110
111     private void updateRemoteAclCache(List<Ace> aceList, String aclName, AclServiceManager.Action action) {
112         if (null == aceList) {
113             return;
114         }
115         for (Ace ace : aceList) {
116             SecurityRuleAttr aceAttributes = ace.getAugmentation(SecurityRuleAttr.class);
117             if (aceAttributes != null && aceAttributes.getRemoteGroupId() != null) {
118                 if (action == AclServiceManager.Action.ADD) {
119                     AclDataUtil.addRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
120                 } else {
121                     AclDataUtil.removeRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
122                 }
123             }
124         }
125     }
126
127     @Override
128     protected AclEventListener getDataTreeChangeListener() {
129         return this;
130     }
131
132     private List<Ace> getChangedAceList(Acl updatedAcl, Acl currentAcl) {
133         if (updatedAcl == null) {
134             return null;
135         }
136         List<Ace> updatedAceList = new ArrayList<>(updatedAcl.getAccessListEntries().getAce());
137         if (currentAcl == null) {
138             return updatedAceList;
139         }
140         List<Ace> currentAceList = new ArrayList<>(currentAcl.getAccessListEntries().getAce());
141         for (Iterator<Ace> iterator = updatedAceList.iterator(); iterator.hasNext(); ) {
142             Ace ace1 = iterator.next();
143             for (Ace ace2 : currentAceList) {
144                 if (ace1.getRuleName().equals(ace2.getRuleName())) {
145                     iterator.remove();
146                 }
147             }
148         }
149         return updatedAceList;
150     }
151 }