Revert "Service-Recovery: Below service recovery actions are supported for ACL (a...
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / listeners / AclInterfaceListener.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 com.google.common.util.concurrent.Futures;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
15 import javax.annotation.PostConstruct;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
22 import org.opendaylight.netvirt.aclservice.api.AclInterfaceCache;
23 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
24 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
25 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
26 import org.opendaylight.netvirt.aclservice.utils.AclClusterUtil;
27 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
28 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionIngress;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.InterfaceAcl;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 @Singleton
41 public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interface, AclInterfaceListener>
42         implements ClusteredDataTreeChangeListener<Interface> {
43     private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceListener.class);
44
45     private final AclServiceManager aclServiceManager;
46     private final AclClusterUtil aclClusterUtil;
47     private final DataBroker dataBroker;
48     private final AclDataUtil aclDataUtil;
49     private final AclInterfaceCache aclInterfaceCache;
50     private final AclServiceUtils aclServiceUtils;
51
52     @Inject
53     public AclInterfaceListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil,
54             DataBroker dataBroker, AclDataUtil aclDataUtil, AclInterfaceCache aclInterfaceCache,
55             AclServiceUtils aclServicUtils) {
56         super(Interface.class, AclInterfaceListener.class);
57         this.aclServiceManager = aclServiceManager;
58         this.aclClusterUtil = aclClusterUtil;
59         this.dataBroker = dataBroker;
60         this.aclDataUtil = aclDataUtil;
61         this.aclInterfaceCache = aclInterfaceCache;
62         this.aclServiceUtils = aclServicUtils;
63     }
64
65     @Override
66     @PostConstruct
67     public void init() {
68         LOG.info("{} start", getClass().getSimpleName());
69         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
70     }
71
72     @Override
73     protected InstanceIdentifier<Interface> getWildCardPath() {
74         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
75     }
76
77     @Override
78     protected void remove(InstanceIdentifier<Interface> key, Interface port) {
79         LOG.trace("Received AclInterface remove event, port={}", port);
80         String interfaceId = port.getName();
81         AclInterface aclInterface = aclInterfaceCache.remove(interfaceId);
82         if (AclServiceUtils.isOfInterest(aclInterface)) {
83             if (aclClusterUtil.isEntityOwner()) {
84                 LOG.debug("On remove event, notify ACL unbind/remove for interface: {}", interfaceId);
85                 aclServiceManager.notify(aclInterface, null, Action.UNBIND);
86                 if (aclInterface.getDpId() != null) {
87                     aclServiceManager.notify(aclInterface, null, Action.REMOVE);
88                 }
89                 AclServiceUtils.deleteSubnetIpPrefixes(dataBroker, interfaceId);
90             }
91         }
92     }
93
94     @Override
95     protected void update(InstanceIdentifier<Interface> key, Interface portBefore, Interface portAfter) {
96         if (portBefore.getAugmentation(ParentRefs.class) == null
97                 && portAfter.getAugmentation(ParentRefs.class) != null) {
98             LOG.trace("Ignoring event for update in ParentRefs for {} ", portAfter.getName());
99             return;
100         }
101         LOG.trace("Received AclInterface update event, portBefore={}, portAfter={}", portBefore, portAfter);
102         InterfaceAcl aclInPortAfter = portAfter.getAugmentation(InterfaceAcl.class);
103         InterfaceAcl aclInPortBefore = portBefore.getAugmentation(InterfaceAcl.class);
104
105         String interfaceId = portAfter.getName();
106         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
107             .Interface interfaceState = AclServiceUtils.getInterfaceStateFromOperDS(dataBroker, interfaceId);
108
109         AclInterface aclInterfaceBefore = aclInterfaceCache.get(interfaceId);
110         if (aclInterfaceBefore == null || isPortSecurityEnabledNow(aclInPortBefore, aclInPortAfter)) {
111             // Updating cache now as it might have not updated when
112             // port-security-enable=false
113             aclInterfaceBefore = addOrUpdateAclInterfaceCache(interfaceId, aclInPortBefore, true, interfaceState);
114         }
115         if (aclInPortAfter != null && aclInPortAfter.isPortSecurityEnabled()
116                 || aclInPortBefore != null && aclInPortBefore.isPortSecurityEnabled()) {
117             boolean isSgChanged =
118                     isSecurityGroupsChanged(aclInPortBefore.getSecurityGroups(), aclInPortAfter.getSecurityGroups());
119             AclInterface aclInterfaceAfter =
120                     addOrUpdateAclInterfaceCache(interfaceId, aclInPortAfter, isSgChanged, interfaceState);
121
122             if (aclClusterUtil.isEntityOwner()) {
123                 // Handle bind/unbind service irrespective of interface state (up/down)
124                 boolean isPortSecurityEnable = aclInterfaceAfter.isPortSecurityEnabled();
125                 boolean isPortSecurityEnableBefore = aclInterfaceBefore.isPortSecurityEnabled();
126                 // if port security enable is changed and is disabled, unbind ACL service
127                 if (isPortSecurityEnableBefore != isPortSecurityEnable && !isPortSecurityEnable) {
128                     LOG.debug("Notify unbind ACL service for interface={}, isPortSecurityEnable={}", interfaceId,
129                             isPortSecurityEnable);
130                     aclServiceManager.notify(aclInterfaceAfter, null, Action.UNBIND);
131                 }
132                 if (interfaceState != null && interfaceState.getOperStatus().equals(
133                         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
134                             .state.Interface.OperStatus.Up)) {
135                     // if port security enable is changed and is enabled, bind ACL service
136                     if (isPortSecurityEnableBefore != isPortSecurityEnable && isPortSecurityEnable) {
137                         LOG.debug("Notify bind ACL service for interface={}, isPortSecurityEnable={}", interfaceId,
138                                 isPortSecurityEnable);
139                         aclServiceManager.notify(aclInterfaceAfter, null, Action.BIND);
140                     }
141                     LOG.debug("On update event, notify ACL service manager to update ACL for interface: {}",
142                             interfaceId);
143                     // handle add for AclPortsLookup before processing update
144                     try {
145                         Futures.allAsList(aclServiceUtils.addAclPortsLookupForInterfaceUpdate(aclInterfaceBefore,
146                                 aclInterfaceAfter)).get();
147                     } catch (InterruptedException | ExecutionException e) {
148                         LOG.error("Error adding ACL ports for interface update", e);
149                     }
150
151                     aclServiceManager.notify(aclInterfaceAfter, aclInterfaceBefore, AclServiceManager.Action.UPDATE);
152                     // handle delete for AclPortsLookup after processing update
153                     try {
154                         Futures.allAsList(aclServiceUtils.deleteAclPortsLookupForInterfaceUpdate(aclInterfaceBefore,
155                                 aclInterfaceAfter)).get();
156                     } catch (InterruptedException | ExecutionException e) {
157                         LOG.error("Error deleting ACL ports for interface update", e);
158                     }
159                 }
160             }
161             updateCacheWithAclChange(aclInterfaceBefore, aclInterfaceAfter);
162         }
163     }
164
165     private void updateCacheWithAclChange(AclInterface aclInterfaceBefore, AclInterface aclInterfaceAfter) {
166         List<Uuid> addedAcls = AclServiceUtils.getUpdatedAclList(aclInterfaceAfter.getSecurityGroups(),
167                 aclInterfaceBefore.getSecurityGroups());
168         List<Uuid> deletedAcls = AclServiceUtils.getUpdatedAclList(aclInterfaceBefore.getSecurityGroups(),
169                 aclInterfaceAfter.getSecurityGroups());
170         if (deletedAcls != null && !deletedAcls.isEmpty()) {
171             aclDataUtil.removeAclInterfaceMap(deletedAcls, aclInterfaceAfter);
172         }
173         if (addedAcls != null && !addedAcls.isEmpty()) {
174             aclDataUtil.addOrUpdateAclInterfaceMap(addedAcls, aclInterfaceAfter);
175         }
176     }
177
178     private boolean isPortSecurityEnabledNow(InterfaceAcl aclInPortBefore, InterfaceAcl aclInPortAfter) {
179         return aclInPortBefore != null && !aclInPortBefore.isPortSecurityEnabled() && aclInPortAfter != null
180                 && aclInPortAfter.isPortSecurityEnabled();
181     }
182
183     private boolean isSecurityGroupsChanged(List<Uuid> sgsBefore, List<Uuid> sgsAfter) {
184         if (sgsBefore == null && sgsAfter == null) {
185             return false;
186         }
187         if ((sgsBefore == null && sgsAfter != null) || (sgsBefore != null && sgsAfter == null)) {
188             return true;
189         }
190         if (sgsBefore != null && sgsAfter != null) {
191             return !(new HashSet<>(sgsBefore)).equals(new HashSet<>(sgsAfter));
192         }
193         return true;
194     }
195
196     private AclInterface addOrUpdateAclInterfaceCache(String interfaceId, InterfaceAcl aclInPort) {
197         return addOrUpdateAclInterfaceCache(interfaceId, aclInPort, true, null);
198     }
199
200     private AclInterface addOrUpdateAclInterfaceCache(String interfaceId, InterfaceAcl aclInPort, boolean isSgChanged,
201             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
202                     .Interface interfaceState) {
203         AclInterface aclInterface = aclInterfaceCache.addOrUpdate(interfaceId, (prevAclInterface, builder) -> {
204             List<Uuid> sgs = new ArrayList<>();
205             if (aclInPort != null) {
206                 sgs = aclInPort.getSecurityGroups();
207                 builder.portSecurityEnabled(aclInPort.isPortSecurityEnabled()).securityGroups(sgs)
208                         .allowedAddressPairs(aclInPort.getAllowedAddressPairs());
209             }
210
211             if ((prevAclInterface == null || prevAclInterface.getLPortTag() == null) && interfaceState != null) {
212                 builder.dpId(AclServiceUtils.getDpIdFromIterfaceState(interfaceState))
213                         .lPortTag(interfaceState.getIfIndex()).isMarkedForDelete(false);
214             }
215
216             if (prevAclInterface == null) {
217                 builder.subnetIpPrefixes(AclServiceUtils.getSubnetIpPrefixes(dataBroker, interfaceId));
218             }
219             if (prevAclInterface == null || prevAclInterface.getElanId() == null) {
220                 builder.elanId(AclServiceUtils.getElanIdFromInterface(interfaceId, dataBroker));
221             }
222             if (prevAclInterface == null || isSgChanged) {
223                 builder.ingressRemoteAclTags(aclServiceUtils.getRemoteAclTags(sgs, DirectionIngress.class))
224                         .egressRemoteAclTags(aclServiceUtils.getRemoteAclTags(sgs, DirectionEgress.class));
225             }
226         });
227         // Clone and return the ACL interface object
228         return AclInterface.builder(aclInterface).build();
229     }
230
231     @Override
232     protected void add(InstanceIdentifier<Interface> key, Interface port) {
233         LOG.trace("Received AclInterface add event, port={}", port);
234         InterfaceAcl aclInPort = port.getAugmentation(InterfaceAcl.class);
235         if (aclInPort != null && aclInPort.isPortSecurityEnabled()) {
236             String interfaceId = port.getName();
237             AclInterface aclInterface = addOrUpdateAclInterfaceCache(interfaceId, aclInPort);
238
239             // if interface state event comes first followed by interface config event.
240             if (aclInterface.getDpId() != null && aclInterface.getElanId() != null && aclClusterUtil.isEntityOwner()) {
241                 LOG.debug("On add event, notify ACL bind/add for interface: {}", interfaceId);
242                 aclServiceManager.notify(aclInterface, null, Action.BIND);
243                 aclServiceManager.notify(aclInterface, null, Action.ADD);
244             }
245         }
246     }
247
248     @Override
249     protected AclInterfaceListener getDataTreeChangeListener() {
250         return this;
251     }
252 }