2 * Copyright (c) 2016 Red Hat, Inc. 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.Collections;
11 import java.util.List;
12 import java.util.SortedSet;
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.interfacemanager.interfaces.IInterfaceManager;
21 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
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.serviceutils.srm.RecoverableListener;
30 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionIngress;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.InterfaceAcl;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.port.subnets.port.subnet.SubnetInfo;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<Interface,
45 AclInterfaceStateListener> implements ClusteredDataTreeChangeListener<Interface>, RecoverableListener {
47 private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceStateListener.class);
49 /** Our registration. */
50 private final AclServiceManager aclServiceManger;
51 private final AclClusterUtil aclClusterUtil;
52 private final DataBroker dataBroker;
53 private final AclDataUtil aclDataUtil;
54 private final IInterfaceManager interfaceManager;
55 private final AclInterfaceCache aclInterfaceCache;
56 private final AclServiceUtils aclServiceUtils;
57 protected final JobCoordinator jobCoordinator;
60 public AclInterfaceStateListener(AclServiceManager aclServiceManger, AclClusterUtil aclClusterUtil,
61 DataBroker dataBroker, AclDataUtil aclDataUtil, IInterfaceManager interfaceManager,
62 AclInterfaceCache aclInterfaceCache, AclServiceUtils aclServicUtils, JobCoordinator jobCoordinator,
63 ServiceRecoveryRegistry serviceRecoveryRegistry) {
64 super(Interface.class, AclInterfaceStateListener.class);
65 this.aclServiceManger = aclServiceManger;
66 this.aclClusterUtil = aclClusterUtil;
67 this.dataBroker = dataBroker;
68 this.aclDataUtil = aclDataUtil;
69 this.interfaceManager = interfaceManager;
70 this.aclInterfaceCache = aclInterfaceCache;
71 this.aclServiceUtils = aclServicUtils;
72 this.jobCoordinator = jobCoordinator;
73 serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
79 LOG.info("{} start", getClass().getSimpleName());
84 public void registerListener() {
85 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
89 protected InstanceIdentifier<Interface> getWildCardPath() {
90 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
94 protected void remove(InstanceIdentifier<Interface> key, Interface deleted) {
95 if (!L2vlan.class.equals(deleted.getType())) {
98 String interfaceId = deleted.getName();
99 AclInterface aclInterface = aclInterfaceCache.remove(interfaceId);
100 if (AclServiceUtils.isOfInterest(aclInterface)) {
101 List<Uuid> aclList = aclInterface.getSecurityGroups();
102 if (aclClusterUtil.isEntityOwner()) {
103 LOG.debug("On remove event, notify ACL service manager to remove ACL from interface: {}", aclInterface);
104 aclServiceManger.notify(aclInterface, null, Action.UNBIND);
105 aclServiceManger.notify(aclInterface, null, Action.REMOVE);
107 if (aclList != null) {
108 aclServiceUtils.deleteAclPortsLookup(aclInterface, aclList, aclInterface.getAllowedAddressPairs());
111 if (aclList != null) {
112 for (Uuid acl : aclList) {
113 jobCoordinator.enqueueJob(acl.getValue().intern(), () -> {
114 aclDataUtil.removeAclInterfaceMap(acl, aclInterface);
115 return Collections.emptyList();
123 protected void update(InstanceIdentifier<Interface> key, Interface before, Interface after) {
125 * The update is not of interest as the attributes populated from this listener will not change.
126 * The northbound updates are handled in AclInterfaceListener.
128 * We're only interested in update in cases where IfType got filled after creation.
130 if (before.getType() == null && L2vlan.class.equals(after.getType())) {
133 LOG.trace("Update event for AclInterfaceStateListener is not of interest.");
138 protected void add(InstanceIdentifier<Interface> key, Interface added) {
139 if (!L2vlan.class.equals(added.getType())) {
143 AclInterface aclInterface = aclInterfaceCache.addOrUpdate(added.getName(), (prevAclInterface, builder) -> {
144 builder.dpId(AclServiceUtils.getDpIdFromIterfaceState(added)).lPortTag(added.getIfIndex())
145 .isMarkedForDelete(false);
147 if (AclServiceUtils.isOfInterest(prevAclInterface)) {
148 if (prevAclInterface.getSubnetInfo() == null) {
150 List<SubnetInfo> subnetInfo = aclServiceUtils.getSubnetInfo(added.getName());
151 builder.subnetInfo(subnetInfo);
153 SortedSet<Integer> ingressRemoteAclTags =
154 aclServiceUtils.getRemoteAclTags(prevAclInterface.getSecurityGroups(), DirectionIngress.class);
155 SortedSet<Integer> egressRemoteAclTags =
156 aclServiceUtils.getRemoteAclTags(prevAclInterface.getSecurityGroups(), DirectionEgress.class);
157 builder.ingressRemoteAclTags(ingressRemoteAclTags).egressRemoteAclTags(egressRemoteAclTags);
161 List<Uuid> aclList = aclInterface.getSecurityGroups();
162 if (aclList == null) {
163 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
164 .Interface iface = interfaceManager.getInterfaceInfoFromConfigDataStore(added.getName());
166 LOG.error("No interface with name {} available in interfaceConfig, servicing interfaceState ADD"
167 + "for ACL failed", added.getName());
170 InterfaceAcl aclInPort = iface.augmentation(InterfaceAcl.class);
171 if (aclInPort == null) {
172 LOG.trace("Interface {} is not an ACL Interface, ignoring ADD interfaceState event",
178 if (AclServiceUtils.isOfInterest(aclInterface)) {
179 if (aclList != null) {
180 aclDataUtil.addAclInterfaceMap(aclList, aclInterface);
182 if (aclInterface.getElanId() == null) {
183 LOG.debug("On Add event, skip ADD since ElanId is not updated");
186 if (aclClusterUtil.isEntityOwner()) {
187 LOG.debug("On add event, notify ACL service manager to add ACL for interface: {}", aclInterface);
188 aclServiceManger.notify(aclInterface, null, Action.BIND);
189 if (aclList != null) {
190 aclServiceUtils.addAclPortsLookup(aclInterface, aclList, aclInterface.getAllowedAddressPairs());
192 aclServiceManger.notify(aclInterface, null, Action.ADD);
198 protected AclInterfaceStateListener getDataTreeChangeListener() {
199 return AclInterfaceStateListener.this;