2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.aclservice.listeners;
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;
41 public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interface, AclInterfaceListener>
42 implements ClusteredDataTreeChangeListener<Interface> {
43 private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceListener.class);
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;
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;
68 LOG.info("{} start", getClass().getSimpleName());
69 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
73 protected InstanceIdentifier<Interface> getWildCardPath() {
74 return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
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);
89 AclServiceUtils.deleteSubnetIpPrefixes(dataBroker, interfaceId);
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());
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);
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);
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);
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);
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);
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);
141 LOG.debug("On update event, notify ACL service manager to update ACL for interface: {}",
143 // handle add for AclPortsLookup before processing update
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);
151 aclServiceManager.notify(aclInterfaceAfter, aclInterfaceBefore, AclServiceManager.Action.UPDATE);
152 // handle delete for AclPortsLookup after processing update
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);
161 updateCacheWithAclChange(aclInterfaceBefore, aclInterfaceAfter);
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);
173 if (addedAcls != null && !addedAcls.isEmpty()) {
174 aclDataUtil.addOrUpdateAclInterfaceMap(addedAcls, aclInterfaceAfter);
178 private boolean isPortSecurityEnabledNow(InterfaceAcl aclInPortBefore, InterfaceAcl aclInPortAfter) {
179 return aclInPortBefore != null && !aclInPortBefore.isPortSecurityEnabled() && aclInPortAfter != null
180 && aclInPortAfter.isPortSecurityEnabled();
183 private boolean isSecurityGroupsChanged(List<Uuid> sgsBefore, List<Uuid> sgsAfter) {
184 if (sgsBefore == null && sgsAfter == null) {
187 if ((sgsBefore == null && sgsAfter != null) || (sgsBefore != null && sgsAfter == null)) {
190 if (sgsBefore != null && sgsAfter != null) {
191 return !(new HashSet<>(sgsBefore)).equals(new HashSet<>(sgsAfter));
196 private AclInterface addOrUpdateAclInterfaceCache(String interfaceId, InterfaceAcl aclInPort) {
197 return addOrUpdateAclInterfaceCache(interfaceId, aclInPort, true, null);
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());
211 if ((prevAclInterface == null || prevAclInterface.getLPortTag() == null) && interfaceState != null) {
212 builder.dpId(AclServiceUtils.getDpIdFromIterfaceState(interfaceState))
213 .lPortTag(interfaceState.getIfIndex()).isMarkedForDelete(false);
216 if (prevAclInterface == null) {
217 builder.subnetIpPrefixes(AclServiceUtils.getSubnetIpPrefixes(dataBroker, interfaceId));
219 if (prevAclInterface == null || prevAclInterface.getElanId() == null) {
220 builder.elanId(AclServiceUtils.getElanIdFromInterface(interfaceId, dataBroker));
222 if (prevAclInterface == null || isSgChanged) {
223 builder.ingressRemoteAclTags(aclServiceUtils.getRemoteAclTags(sgs, DirectionIngress.class))
224 .egressRemoteAclTags(aclServiceUtils.getRemoteAclTags(sgs, DirectionEgress.class));
227 // Clone and return the ACL interface object
228 return AclInterface.builder(aclInterface).build();
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);
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);
249 protected AclInterfaceListener getDataTreeChangeListener() {