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 java.util.ArrayList;
11 import java.util.List;
12 import javax.annotation.Nullable;
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.AclInterfaceCache;
21 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
22 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
23 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
24 import org.opendaylight.netvirt.aclservice.utils.AclClusterUtil;
25 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
26 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
27 import org.opendaylight.serviceutils.srm.RecoverableListener;
28 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
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.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interface, AclInterfaceListener>
43 implements ClusteredDataTreeChangeListener<Interface>, RecoverableListener {
44 private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceListener.class);
46 private final AclServiceManager aclServiceManager;
47 private final AclClusterUtil aclClusterUtil;
48 private final DataBroker dataBroker;
49 private final AclDataUtil aclDataUtil;
50 private final AclInterfaceCache aclInterfaceCache;
51 private final AclServiceUtils aclServiceUtils;
54 public AclInterfaceListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil,
55 DataBroker dataBroker, AclDataUtil aclDataUtil, AclInterfaceCache aclInterfaceCache,
56 AclServiceUtils aclServicUtils, ServiceRecoveryRegistry serviceRecoveryRegistry) {
57 super(Interface.class, AclInterfaceListener.class);
58 this.aclServiceManager = aclServiceManager;
59 this.aclClusterUtil = aclClusterUtil;
60 this.dataBroker = dataBroker;
61 this.aclDataUtil = aclDataUtil;
62 this.aclInterfaceCache = aclInterfaceCache;
63 this.aclServiceUtils = aclServicUtils;
64 serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
70 LOG.info("{} start", getClass().getSimpleName());
75 public void registerListener() {
76 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
80 protected InstanceIdentifier<Interface> getWildCardPath() {
81 return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
85 public void remove(InstanceIdentifier<Interface> key, Interface port) {
86 LOG.trace("Received AclInterface remove event, port={}", port);
87 String interfaceId = port.getName();
88 AclInterface aclInterface = aclInterfaceCache.remove(interfaceId);
89 if (AclServiceUtils.isOfInterest(aclInterface)) {
90 if (aclClusterUtil.isEntityOwner()) {
91 LOG.debug("On remove event, notify ACL unbind/remove for interface: {}", interfaceId);
92 aclServiceManager.notify(aclInterface, null, Action.UNBIND);
93 if (aclInterface.getDpId() != null) {
94 aclServiceManager.notify(aclInterface, null, Action.REMOVE);
96 aclServiceUtils.deleteSubnetInfo(interfaceId);
102 public void update(@Nullable InstanceIdentifier<Interface> key, Interface portBefore, Interface portAfter) {
103 if (portBefore.augmentation(ParentRefs.class) == null
104 && portAfter.augmentation(ParentRefs.class) != null) {
105 LOG.trace("Ignoring event for update in ParentRefs for {} ", portAfter.getName());
108 LOG.trace("Received AclInterface update event, portBefore={}, portAfter={}", portBefore, portAfter);
109 InterfaceAcl aclInPortAfter = portAfter.augmentation(InterfaceAcl.class);
110 InterfaceAcl aclInPortBefore = portBefore.augmentation(InterfaceAcl.class);
112 String interfaceId = portAfter.getName();
113 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
114 .Interface interfaceState = AclServiceUtils.getInterfaceStateFromOperDS(dataBroker, interfaceId);
116 AclInterface aclInterfaceBefore = aclInterfaceCache.get(interfaceId);
117 if (aclInterfaceBefore == null || isPortSecurityEnabledNow(aclInPortBefore, aclInPortAfter)) {
118 // Updating cache now as it might have not updated when
119 // port-security-enable=false
120 aclInterfaceBefore = addOrUpdateAclInterfaceCache(interfaceId, aclInPortBefore, true, interfaceState);
122 if (aclInPortAfter != null && aclInPortAfter.isPortSecurityEnabled()
123 || aclInPortBefore != null && aclInPortBefore.isPortSecurityEnabled()) {
124 List<Uuid> sgsBefore = null;
125 if (aclInPortBefore != null) {
126 sgsBefore = aclInPortBefore.getSecurityGroups();
128 boolean isSgChanged = isSecurityGroupsChanged(sgsBefore, aclInPortAfter.getSecurityGroups());
129 AclInterface aclInterfaceAfter =
130 addOrUpdateAclInterfaceCache(interfaceId, aclInPortAfter, isSgChanged, interfaceState);
132 if (aclClusterUtil.isEntityOwner()) {
133 // Handle bind/unbind service irrespective of interface state (up/down)
134 boolean isPortSecurityEnable = aclInterfaceAfter.isPortSecurityEnabled();
135 boolean isPortSecurityEnableBefore = aclInterfaceBefore.isPortSecurityEnabled();
136 // if port security enable is changed and is disabled, unbind ACL service
137 if (isPortSecurityEnableBefore != isPortSecurityEnable && !isPortSecurityEnable) {
138 LOG.debug("Notify unbind ACL service for interface={}, isPortSecurityEnable={}", interfaceId,
139 isPortSecurityEnable);
140 aclServiceManager.notify(aclInterfaceAfter, null, Action.UNBIND);
142 if (interfaceState != null && org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces
143 .rev140508.interfaces.state.Interface.OperStatus.Up.equals(interfaceState.getOperStatus())) {
144 // if port security enable is changed and is enabled, bind ACL service
145 if (isPortSecurityEnableBefore != isPortSecurityEnable && isPortSecurityEnable) {
146 LOG.debug("Notify bind ACL service for interface={}, isPortSecurityEnable={}", interfaceId,
147 isPortSecurityEnable);
148 aclServiceManager.notify(aclInterfaceAfter, null, Action.BIND);
150 LOG.debug("On update event, notify ACL service manager to update ACL for interface: {}",
152 // handle add for AclPortsLookup before processing update
153 aclServiceUtils.addAclPortsLookupForInterfaceUpdate(aclInterfaceBefore, aclInterfaceAfter);
155 aclServiceManager.notify(aclInterfaceAfter, aclInterfaceBefore, AclServiceManager.Action.UPDATE);
156 // handle delete for AclPortsLookup after processing update
157 aclServiceUtils.deleteAclPortsLookupForInterfaceUpdate(aclInterfaceBefore, aclInterfaceAfter);
160 updateCacheWithAclChange(aclInterfaceBefore, aclInterfaceAfter);
164 private void updateCacheWithAclChange(AclInterface aclInterfaceBefore, AclInterface aclInterfaceAfter) {
165 List<Uuid> addedAcls = AclServiceUtils.getUpdatedAclList(aclInterfaceAfter.getSecurityGroups(),
166 aclInterfaceBefore.getSecurityGroups());
167 List<Uuid> deletedAcls = AclServiceUtils.getUpdatedAclList(aclInterfaceBefore.getSecurityGroups(),
168 aclInterfaceAfter.getSecurityGroups());
169 if (deletedAcls != null && !deletedAcls.isEmpty()) {
170 aclDataUtil.removeAclInterfaceMap(deletedAcls, aclInterfaceAfter);
172 if (addedAcls != null && !addedAcls.isEmpty()) {
173 aclDataUtil.addOrUpdateAclInterfaceMap(addedAcls, aclInterfaceAfter);
175 List<AllowedAddressPairs> addedAap = AclServiceUtils.getUpdatedAllowedAddressPairs(aclInterfaceAfter
176 .getAllowedAddressPairs(), aclInterfaceBefore.getAllowedAddressPairs());
177 List<AllowedAddressPairs> deletedAap = AclServiceUtils.getUpdatedAllowedAddressPairs(aclInterfaceBefore
178 .getAllowedAddressPairs(), aclInterfaceAfter.getAllowedAddressPairs());
179 if (deletedAap != null && !deletedAap.isEmpty() || addedAap != null && !addedAap.isEmpty()) {
180 LOG.debug("Update cache with new AAP = {}", aclInterfaceAfter.getInterfaceId());
181 aclDataUtil.addOrUpdateAclInterfaceMap(aclInterfaceAfter.getSecurityGroups(), aclInterfaceAfter);
185 private boolean isPortSecurityEnabledNow(InterfaceAcl aclInPortBefore, InterfaceAcl aclInPortAfter) {
186 return aclInPortBefore != null && !aclInPortBefore.isPortSecurityEnabled() && aclInPortAfter != null
187 && aclInPortAfter.isPortSecurityEnabled();
190 private boolean isSecurityGroupsChanged(@Nullable List<Uuid> sgsBefore, @Nullable List<Uuid> sgsAfter) {
191 if (sgsBefore == null && sgsAfter == null) {
194 if (sgsBefore == null || sgsAfter == null) {
197 return !(sgsBefore.containsAll(sgsAfter) && sgsAfter.containsAll(sgsBefore));
200 private AclInterface addOrUpdateAclInterfaceCache(String interfaceId, InterfaceAcl aclInPort) {
201 return addOrUpdateAclInterfaceCache(interfaceId, aclInPort, true, null);
204 private AclInterface addOrUpdateAclInterfaceCache(String interfaceId, InterfaceAcl aclInPort, boolean isSgChanged,
205 @Nullable org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
206 .state.Interface interfaceState) {
207 AclInterface aclInterface = aclInterfaceCache.addOrUpdate(interfaceId, (prevAclInterface, builder) -> {
208 List<Uuid> sgs = new ArrayList<>();
209 if (aclInPort != null) {
210 sgs = aclInPort.getSecurityGroups();
211 builder.portSecurityEnabled(aclInPort.isPortSecurityEnabled()).securityGroups(sgs)
212 .allowedAddressPairs(aclInPort.getAllowedAddressPairs());
215 if ((prevAclInterface == null || prevAclInterface.getLPortTag() == null) && interfaceState != null) {
216 builder.dpId(AclServiceUtils.getDpIdFromIterfaceState(interfaceState))
217 .lPortTag(interfaceState.getIfIndex()).isMarkedForDelete(false);
220 builder.subnetInfo(aclServiceUtils.getSubnetInfo(interfaceId));
221 if (prevAclInterface == null || prevAclInterface.getElanId() == null) {
222 builder.elanId(AclServiceUtils.getElanIdFromInterface(interfaceId, dataBroker));
224 if (prevAclInterface == null || isSgChanged) {
225 builder.ingressRemoteAclTags(aclServiceUtils.getRemoteAclTags(sgs, DirectionIngress.class))
226 .egressRemoteAclTags(aclServiceUtils.getRemoteAclTags(sgs, DirectionEgress.class));
229 // Clone and return the ACL interface object
230 return AclInterface.builder(aclInterface).build();
234 public void add(InstanceIdentifier<Interface> key, Interface port) {
235 LOG.trace("Received AclInterface add event, port={}", port);
236 InterfaceAcl aclInPort = port.augmentation(InterfaceAcl.class);
237 if (aclInPort != null && aclInPort.isPortSecurityEnabled()) {
238 String interfaceId = port.getName();
239 AclInterface aclInterface = addOrUpdateAclInterfaceCache(interfaceId, aclInPort);
241 // if interface state event comes first followed by interface config event.
242 if (aclInterface.getDpId() != null && aclInterface.getElanId() != null && aclClusterUtil.isEntityOwner()) {
243 LOG.debug("On add event, notify ACL bind/add for interface: {}", interfaceId);
244 aclServiceManager.notify(aclInterface, null, Action.BIND);
245 aclServiceManager.notify(aclInterface, null, Action.ADD);
251 protected AclInterfaceListener getDataTreeChangeListener() {