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.HashSet;
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.genius.srm.RecoverableListener;
21 import org.opendaylight.genius.srm.ServiceRecoveryRegistry;
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>, RecoverableListener {
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, ServiceRecoveryRegistry serviceRecoveryRegistry) {
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 serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
69 LOG.info("{} start", getClass().getSimpleName());
74 public void registerListener() {
75 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
79 protected InstanceIdentifier<Interface> getWildCardPath() {
80 return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
84 public void remove(InstanceIdentifier<Interface> key, Interface port) {
85 LOG.trace("Received AclInterface remove event, port={}", port);
86 String interfaceId = port.getName();
87 AclInterface aclInterface = aclInterfaceCache.remove(interfaceId);
88 if (AclServiceUtils.isOfInterest(aclInterface)) {
89 if (aclClusterUtil.isEntityOwner()) {
90 LOG.debug("On remove event, notify ACL unbind/remove for interface: {}", interfaceId);
91 aclServiceManager.notify(aclInterface, null, Action.UNBIND);
92 if (aclInterface.getDpId() != null) {
93 aclServiceManager.notify(aclInterface, null, Action.REMOVE);
95 aclServiceUtils.deleteSubnetInfo(interfaceId);
101 public void update(InstanceIdentifier<Interface> key, Interface portBefore, Interface portAfter) {
102 if (portBefore.augmentation(ParentRefs.class) == null
103 && portAfter.augmentation(ParentRefs.class) != null) {
104 LOG.trace("Ignoring event for update in ParentRefs for {} ", portAfter.getName());
107 LOG.trace("Received AclInterface update event, portBefore={}, portAfter={}", portBefore, portAfter);
108 InterfaceAcl aclInPortAfter = portAfter.augmentation(InterfaceAcl.class);
109 InterfaceAcl aclInPortBefore = portBefore.augmentation(InterfaceAcl.class);
111 String interfaceId = portAfter.getName();
112 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
113 .Interface interfaceState = AclServiceUtils.getInterfaceStateFromOperDS(dataBroker, interfaceId);
115 AclInterface aclInterfaceBefore = aclInterfaceCache.get(interfaceId);
116 if (aclInterfaceBefore == null || isPortSecurityEnabledNow(aclInPortBefore, aclInPortAfter)) {
117 // Updating cache now as it might have not updated when
118 // port-security-enable=false
119 aclInterfaceBefore = addOrUpdateAclInterfaceCache(interfaceId, aclInPortBefore, true, interfaceState);
121 if (aclInPortAfter != null && aclInPortAfter.isPortSecurityEnabled()
122 || aclInPortBefore != null && aclInPortBefore.isPortSecurityEnabled()) {
123 List<Uuid> sgsBefore = null;
124 if (aclInPortBefore != null) {
125 sgsBefore = aclInPortBefore.getSecurityGroups();
127 boolean isSgChanged = isSecurityGroupsChanged(sgsBefore, aclInPortAfter.getSecurityGroups());
128 AclInterface aclInterfaceAfter =
129 addOrUpdateAclInterfaceCache(interfaceId, aclInPortAfter, isSgChanged, interfaceState);
131 if (aclClusterUtil.isEntityOwner()) {
132 // Handle bind/unbind service irrespective of interface state (up/down)
133 boolean isPortSecurityEnable = aclInterfaceAfter.isPortSecurityEnabled();
134 boolean isPortSecurityEnableBefore = aclInterfaceBefore.isPortSecurityEnabled();
135 // if port security enable is changed and is disabled, unbind ACL service
136 if (isPortSecurityEnableBefore != isPortSecurityEnable && !isPortSecurityEnable) {
137 LOG.debug("Notify unbind ACL service for interface={}, isPortSecurityEnable={}", interfaceId,
138 isPortSecurityEnable);
139 aclServiceManager.notify(aclInterfaceAfter, null, Action.UNBIND);
141 if (interfaceState != null && interfaceState.getOperStatus().equals(
142 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
143 .state.Interface.OperStatus.Up)) {
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);
177 private boolean isPortSecurityEnabledNow(InterfaceAcl aclInPortBefore, InterfaceAcl aclInPortAfter) {
178 return aclInPortBefore != null && !aclInPortBefore.isPortSecurityEnabled() && aclInPortAfter != null
179 && aclInPortAfter.isPortSecurityEnabled();
182 private boolean isSecurityGroupsChanged(List<Uuid> sgsBefore, List<Uuid> sgsAfter) {
183 if (sgsBefore == null && sgsAfter == null) {
186 if ((sgsBefore == null && sgsAfter != null) || (sgsBefore != null && sgsAfter == null)) {
189 if (sgsBefore != null && sgsAfter != null) {
190 return !(new HashSet<>(sgsBefore)).equals(new HashSet<>(sgsAfter));
195 private AclInterface addOrUpdateAclInterfaceCache(String interfaceId, InterfaceAcl aclInPort) {
196 return addOrUpdateAclInterfaceCache(interfaceId, aclInPort, true, null);
199 private AclInterface addOrUpdateAclInterfaceCache(String interfaceId, InterfaceAcl aclInPort, boolean isSgChanged,
200 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
201 .Interface interfaceState) {
202 AclInterface aclInterface = aclInterfaceCache.addOrUpdate(interfaceId, (prevAclInterface, builder) -> {
203 List<Uuid> sgs = new ArrayList<>();
204 if (aclInPort != null) {
205 sgs = aclInPort.getSecurityGroups();
206 builder.portSecurityEnabled(aclInPort.isPortSecurityEnabled()).securityGroups(sgs)
207 .allowedAddressPairs(aclInPort.getAllowedAddressPairs());
210 if ((prevAclInterface == null || prevAclInterface.getLPortTag() == null) && interfaceState != null) {
211 builder.dpId(AclServiceUtils.getDpIdFromIterfaceState(interfaceState))
212 .lPortTag(interfaceState.getIfIndex()).isMarkedForDelete(false);
215 builder.subnetInfo(aclServiceUtils.getSubnetInfo(interfaceId));
216 if (prevAclInterface == null || prevAclInterface.getElanId() == null) {
217 builder.elanId(AclServiceUtils.getElanIdFromInterface(interfaceId, dataBroker));
219 if (prevAclInterface == null || isSgChanged) {
220 builder.ingressRemoteAclTags(aclServiceUtils.getRemoteAclTags(sgs, DirectionIngress.class))
221 .egressRemoteAclTags(aclServiceUtils.getRemoteAclTags(sgs, DirectionEgress.class));
224 // Clone and return the ACL interface object
225 return AclInterface.builder(aclInterface).build();
229 public void add(InstanceIdentifier<Interface> key, Interface port) {
230 LOG.trace("Received AclInterface add event, port={}", port);
231 InterfaceAcl aclInPort = port.augmentation(InterfaceAcl.class);
232 if (aclInPort != null && aclInPort.isPortSecurityEnabled()) {
233 String interfaceId = port.getName();
234 AclInterface aclInterface = addOrUpdateAclInterfaceCache(interfaceId, aclInPort);
236 // if interface state event comes first followed by interface config event.
237 if (aclInterface.getDpId() != null && aclInterface.getElanId() != null && aclClusterUtil.isEntityOwner()) {
238 LOG.debug("On add event, notify ACL bind/add for interface: {}", interfaceId);
239 aclServiceManager.notify(aclInterface, null, Action.BIND);
240 aclServiceManager.notify(aclInterface, null, Action.ADD);
246 protected AclInterfaceListener getDataTreeChangeListener() {