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.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.iana._if.type.rev140508.L2vlan;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
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.port.subnets.port.subnet.SubnetInfo;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<Interface,
43 AclInterfaceStateListener> implements ClusteredDataTreeChangeListener<Interface>, RecoverableListener {
45 private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceStateListener.class);
47 /** Our registration. */
48 private final AclServiceManager aclServiceManger;
49 private final AclClusterUtil aclClusterUtil;
50 private final DataBroker dataBroker;
51 private final AclDataUtil aclDataUtil;
52 private final IInterfaceManager interfaceManager;
53 private final AclInterfaceCache aclInterfaceCache;
54 private final AclServiceUtils aclServiceUtils;
57 public AclInterfaceStateListener(AclServiceManager aclServiceManger, AclClusterUtil aclClusterUtil,
58 DataBroker dataBroker, AclDataUtil aclDataUtil, IInterfaceManager interfaceManager,
59 AclInterfaceCache aclInterfaceCache, AclServiceUtils aclServicUtils,
60 ServiceRecoveryRegistry serviceRecoveryRegistry) {
61 super(Interface.class, AclInterfaceStateListener.class);
62 this.aclServiceManger = aclServiceManger;
63 this.aclClusterUtil = aclClusterUtil;
64 this.dataBroker = dataBroker;
65 this.aclDataUtil = aclDataUtil;
66 this.interfaceManager = interfaceManager;
67 this.aclInterfaceCache = aclInterfaceCache;
68 this.aclServiceUtils = aclServicUtils;
69 serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
75 LOG.info("{} start", getClass().getSimpleName());
80 public void registerListener() {
81 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
85 protected InstanceIdentifier<Interface> getWildCardPath() {
86 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
90 protected void remove(InstanceIdentifier<Interface> key, Interface deleted) {
91 if (!L2vlan.class.equals(deleted.getType())) {
94 String interfaceId = deleted.getName();
95 AclInterface aclInterface = aclInterfaceCache.remove(interfaceId);
96 if (AclServiceUtils.isOfInterest(aclInterface)) {
97 List<Uuid> aclList = aclInterface.getSecurityGroups();
98 if (aclClusterUtil.isEntityOwner()) {
99 LOG.debug("On remove event, notify ACL service manager to remove ACL from interface: {}", aclInterface);
100 aclServiceManger.notify(aclInterface, null, Action.UNBIND);
101 aclServiceManger.notify(aclInterface, null, Action.REMOVE);
103 if (aclList != null) {
104 aclServiceUtils.deleteAclPortsLookup(aclInterface, aclList, aclInterface.getAllowedAddressPairs());
107 if (aclList != null) {
108 aclDataUtil.removeAclInterfaceMap(aclList, aclInterface);
114 protected void update(InstanceIdentifier<Interface> key, Interface before, Interface after) {
116 * The update is not of interest as the attributes populated from this listener will not change.
117 * The northbound updates are handled in AclInterfaceListener.
119 * We're only interested in update in cases where IfType got filled after creation.
121 if (before.getType() == null && L2vlan.class.equals(after.getType())) {
124 LOG.trace("Update event for AclInterfaceStateListener is not of interest.");
129 protected void add(InstanceIdentifier<Interface> key, Interface added) {
130 if (!L2vlan.class.equals(added.getType())) {
133 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface;
134 iface = interfaceManager.getInterfaceInfoFromConfigDataStore(added.getName());
136 LOG.error("No interface with name {} available in interfaceConfig, servicing interfaceState ADD"
137 + "for ACL failed", added.getName());
140 InterfaceAcl aclInPort = iface.augmentation(InterfaceAcl.class);
141 if (aclInPort == null) {
142 LOG.trace("Interface {} is not an ACL Interface, ignoring ADD interfaceState event",
147 AclInterface aclInterface = aclInterfaceCache.addOrUpdate(added.getName(), (prevAclInterface, builder) -> {
148 builder.dpId(AclServiceUtils.getDpIdFromIterfaceState(added)).lPortTag(added.getIfIndex())
149 .isMarkedForDelete(false);
151 if (AclServiceUtils.isOfInterest(prevAclInterface)) {
152 if (prevAclInterface.getSubnetInfo() == null) {
154 List<SubnetInfo> subnetInfo = aclServiceUtils.getSubnetInfo(added.getName());
155 builder.subnetInfo(subnetInfo);
157 SortedSet<Integer> ingressRemoteAclTags =
158 aclServiceUtils.getRemoteAclTags(aclInPort.getSecurityGroups(), DirectionIngress.class);
159 SortedSet<Integer> egressRemoteAclTags =
160 aclServiceUtils.getRemoteAclTags(aclInPort.getSecurityGroups(), DirectionEgress.class);
161 builder.ingressRemoteAclTags(ingressRemoteAclTags).egressRemoteAclTags(egressRemoteAclTags);
165 if (AclServiceUtils.isOfInterest(aclInterface)) {
166 List<Uuid> aclList = aclInterface.getSecurityGroups();
167 if (aclList != null) {
168 aclDataUtil.addAclInterfaceMap(aclList, aclInterface);
170 if (aclInterface.getElanId() == null) {
171 LOG.debug("On Add event, skip ADD since ElanId is not updated");
174 if (aclClusterUtil.isEntityOwner()) {
175 LOG.debug("On add event, notify ACL service manager to add ACL for interface: {}", aclInterface);
176 aclServiceManger.notify(aclInterface, null, Action.BIND);
177 if (aclList != null) {
178 aclServiceUtils.addAclPortsLookup(aclInterface, aclList, aclInterface.getAllowedAddressPairs());
180 aclServiceManger.notify(aclInterface, null, Action.ADD);
186 protected AclInterfaceStateListener getDataTreeChangeListener() {
187 return AclInterfaceStateListener.this;