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.List;
11 import java.util.SortedSet;
12 import javax.annotation.PostConstruct;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
19 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionIngress;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.InterfaceAcl;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.IpPrefixOrAddress;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<Interface,
41 AclInterfaceStateListener> implements ClusteredDataTreeChangeListener<Interface> {
43 private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceStateListener.class);
45 /** Our registration. */
46 private final AclServiceManager aclServiceManger;
47 private final AclClusterUtil aclClusterUtil;
48 private final DataBroker dataBroker;
49 private final AclDataUtil aclDataUtil;
50 private final IInterfaceManager interfaceManager;
51 private final AclInterfaceCache aclInterfaceCache;
52 private final AclServiceUtils aclServiceUtils;
55 public AclInterfaceStateListener(AclServiceManager aclServiceManger, AclClusterUtil aclClusterUtil,
56 DataBroker dataBroker, AclDataUtil aclDataUtil, IInterfaceManager interfaceManager,
57 AclInterfaceCache aclInterfaceCache, AclServiceUtils aclServicUtils) {
58 super(Interface.class, AclInterfaceStateListener.class);
59 this.aclServiceManger = aclServiceManger;
60 this.aclClusterUtil = aclClusterUtil;
61 this.dataBroker = dataBroker;
62 this.aclDataUtil = aclDataUtil;
63 this.interfaceManager = interfaceManager;
64 this.aclInterfaceCache = aclInterfaceCache;
65 this.aclServiceUtils = aclServicUtils;
71 LOG.info("{} start", getClass().getSimpleName());
72 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
76 protected InstanceIdentifier<Interface> getWildCardPath() {
77 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
81 protected void remove(InstanceIdentifier<Interface> key, Interface deleted) {
82 if (!L2vlan.class.equals(deleted.getType())) {
85 String interfaceId = deleted.getName();
86 AclInterface aclInterface = aclInterfaceCache.remove(interfaceId);
87 if (AclServiceUtils.isOfInterest(aclInterface)) {
88 List<Uuid> aclList = aclInterface.getSecurityGroups();
89 if (aclClusterUtil.isEntityOwner()) {
90 LOG.debug("On remove event, notify ACL service manager to remove ACL from interface: {}", aclInterface);
91 aclServiceManger.notify(aclInterface, null, Action.UNBIND);
92 aclServiceManger.notify(aclInterface, null, Action.REMOVE);
94 if (aclList != null) {
95 aclServiceUtils.deleteAclPortsLookup(aclInterface, aclList, aclInterface.getAllowedAddressPairs());
98 if (aclList != null) {
99 aclDataUtil.removeAclInterfaceMap(aclList, aclInterface);
105 protected void update(InstanceIdentifier<Interface> key, Interface before, Interface after) {
107 * The update is not of interest as the attributes populated from this listener will not change.
108 * The northbound updates are handled in AclInterfaceListener.
110 * We're only interested in update in cases where IfType got filled after creation.
112 if (before.getType() == null && L2vlan.class.equals(after.getType())) {
115 LOG.trace("Update event for AclInterfaceStateListener is not of interest.");
120 protected void add(InstanceIdentifier<Interface> key, Interface added) {
121 if (!L2vlan.class.equals(added.getType())) {
124 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface;
125 iface = interfaceManager.getInterfaceInfoFromConfigDataStore(added.getName());
127 LOG.error("No interface with name {} available in interfaceConfig, servicing interfaceState ADD"
128 + "for ACL failed", added.getName());
131 InterfaceAcl aclInPort = iface.getAugmentation(InterfaceAcl.class);
132 if (aclInPort == null) {
133 LOG.trace("Interface {} is not an ACL Interface, ignoring ADD interfaceState event",
138 AclInterface aclInterface = aclInterfaceCache.addOrUpdate(added.getName(), (prevAclInterface, builder) -> {
139 builder.dpId(AclServiceUtils.getDpIdFromIterfaceState(added)).lPortTag(added.getIfIndex())
140 .isMarkedForDelete(false);
142 if (AclServiceUtils.isOfInterest(prevAclInterface)) {
143 if (prevAclInterface.getSubnetIpPrefixes() == null) {
145 List<IpPrefixOrAddress> subnetIpPrefixes = AclServiceUtils.getSubnetIpPrefixes(dataBroker,
147 builder.subnetIpPrefixes(subnetIpPrefixes);
149 SortedSet<Integer> ingressRemoteAclTags =
150 aclServiceUtils.getRemoteAclTags(aclInPort.getSecurityGroups(), DirectionIngress.class);
151 SortedSet<Integer> egressRemoteAclTags =
152 aclServiceUtils.getRemoteAclTags(aclInPort.getSecurityGroups(), DirectionEgress.class);
153 builder.ingressRemoteAclTags(ingressRemoteAclTags).egressRemoteAclTags(egressRemoteAclTags);
157 if (AclServiceUtils.isOfInterest(aclInterface)) {
158 List<Uuid> aclList = aclInterface.getSecurityGroups();
159 if (aclList != null) {
160 aclDataUtil.addAclInterfaceMap(aclList, aclInterface);
162 if (aclInterface.getElanId() == null) {
163 LOG.debug("On Add event, skip ADD since ElanId is not updated");
166 if (aclClusterUtil.isEntityOwner()) {
167 LOG.debug("On add event, notify ACL service manager to add ACL for interface: {}", aclInterface);
168 aclServiceManger.notify(aclInterface, null, Action.BIND);
169 if (aclList != null) {
170 aclServiceUtils.addAclPortsLookup(aclInterface, aclList, aclInterface.getAllowedAddressPairs());
172 aclServiceManger.notify(aclInterface, null, Action.ADD);
178 protected AclInterfaceStateListener getDataTreeChangeListener() {
179 return AclInterfaceStateListener.this;