Action redesign: use the new ActionInfo Genius classes
[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.AclConstants;
24 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
25 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.AccessLists;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 @Singleton
37 public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEventListener> implements
38         ClusteredDataTreeChangeListener<Acl> {
39
40     private static final Logger LOG = LoggerFactory.getLogger(AclEventListener.class);
41
42     private final AclServiceManager aclServiceManager;
43     private final AclClusterUtil aclClusterUtil;
44     private final DataBroker dataBroker;
45     private final AclDataUtil aclDataUtil;
46     private final IdManagerService idManager;
47
48     @Inject
49     public AclEventListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil, DataBroker dataBroker,
50             AclDataUtil aclDataUtil, IdManagerService idManager) {
51         super(Acl.class, AclEventListener.class);
52         this.aclServiceManager = aclServiceManager;
53         this.aclClusterUtil = aclClusterUtil;
54         this.dataBroker = dataBroker;
55         this.aclDataUtil = aclDataUtil;
56         this.idManager = idManager;
57     }
58
59     @Override
60     @PostConstruct
61     public void init() {
62         LOG.info("{} start", getClass().getSimpleName());
63         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
64     }
65
66     @Override
67     protected InstanceIdentifier<Acl> getWildCardPath() {
68         return InstanceIdentifier
69                 .create(AccessLists.class)
70                 .child(Acl.class);
71     }
72
73     @Override
74     protected void remove(InstanceIdentifier<Acl> key, Acl acl) {
75         updateAclFlowPriorityCache(acl.getAclName(), AclServiceManager.Action.REMOVE);
76         updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.REMOVE);
77     }
78
79     @Override
80     protected void update(InstanceIdentifier<Acl> key, Acl aclBefore, Acl aclAfter) {
81         String aclName = aclAfter.getAclName();
82         List<AclInterface> interfaceList = aclDataUtil.getInterfaceList(new Uuid(aclName));
83         // find and update added ace rules in acl
84         List<Ace> addedAceRules = getChangedAceList(aclAfter, aclBefore);
85         updateRemoteAclCache(addedAceRules, aclName, AclServiceManager.Action.ADD);
86         if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
87             updateAceRules(interfaceList, aclName, addedAceRules, AclServiceManager.Action.ADD);
88         }
89         // find and update deleted ace rules in acl
90         List<Ace> deletedAceRules = getChangedAceList(aclBefore, aclAfter);
91         if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
92             updateAceRules(interfaceList, aclName, deletedAceRules, AclServiceManager.Action.REMOVE);
93         }
94         updateRemoteAclCache(deletedAceRules, aclName, AclServiceManager.Action.REMOVE);
95
96     }
97
98     private void updateAceRules(List<AclInterface> interfaceList, String aclName, List<Ace> aceList,
99             AclServiceManager.Action action) {
100         if (null != aceList && !aceList.isEmpty()) {
101             LOG.trace("update ace rules - action: {} , ace rules: {}", action.name(), aceList);
102             for (AclInterface port : interfaceList) {
103                 for (Ace aceRule : aceList) {
104                     aclServiceManager.notifyAce(port, action, aclName, aceRule);
105                 }
106             }
107         }
108     }
109
110     @Override
111     protected void add(InstanceIdentifier<Acl> key, Acl acl) {
112         updateAclFlowPriorityCache(acl.getAclName(), AclServiceManager.Action.ADD);
113         updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.ADD);
114     }
115
116     /**
117      * Update remote acl cache.
118      *
119      * @param aceList the ace list
120      * @param aclName the acl name
121      * @param action the action
122      */
123     private void updateRemoteAclCache(List<Ace> aceList, String aclName, AclServiceManager.Action action) {
124         if (null == aceList) {
125             return;
126         }
127         for (Ace ace : aceList) {
128             SecurityRuleAttr aceAttributes = ace.getAugmentation(SecurityRuleAttr.class);
129             if (aceAttributes != null && aceAttributes.getRemoteGroupId() != null) {
130                 if (action == AclServiceManager.Action.ADD) {
131                     aclDataUtil.addRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
132                 } else {
133                     aclDataUtil.removeRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
134                 }
135             }
136         }
137     }
138
139     /**
140      * Update acl flow priority cache.
141      *
142      * @param aclName the acl name
143      * @param action the action
144      */
145     private void updateAclFlowPriorityCache(String aclName, AclServiceManager.Action action) {
146         if (action == AclServiceManager.Action.ADD) {
147             Integer flowPriority =
148                     AclServiceUtils.allocateId(this.idManager, AclConstants.ACL_FLOW_PRIORITY_POOL_NAME, aclName);
149             aclDataUtil.addAclFlowPriority(aclName, flowPriority);
150         } else {
151             AclServiceUtils.releaseId(this.idManager, AclConstants.ACL_FLOW_PRIORITY_POOL_NAME, aclName);
152             aclDataUtil.removeAclFlowPriority(aclName);
153         }
154     }
155
156     @Override
157     protected AclEventListener getDataTreeChangeListener() {
158         return this;
159     }
160
161     private List<Ace> getChangedAceList(Acl updatedAcl, Acl currentAcl) {
162         if (updatedAcl == null) {
163             return null;
164         }
165         List<Ace> updatedAceList = new ArrayList<>(updatedAcl.getAccessListEntries().getAce());
166         if (currentAcl == null) {
167             return updatedAceList;
168         }
169         List<Ace> currentAceList = new ArrayList<>(currentAcl.getAccessListEntries().getAce());
170         for (Iterator<Ace> iterator = updatedAceList.iterator(); iterator.hasNext(); ) {
171             Ace ace1 = iterator.next();
172             for (Ace ace2 : currentAceList) {
173                 if (ace1.getRuleName().equals(ace2.getRuleName())) {
174                     iterator.remove();
175                 }
176             }
177         }
178         return updatedAceList;
179     }
180 }