288307fc404186664d3e0fa9dc66803b2d52acfa
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / listeners / AclInterfaceStateListener.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.aclservice.listeners;
9
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;
40
41 @Singleton
42 public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<Interface,
43         AclInterfaceStateListener> implements ClusteredDataTreeChangeListener<Interface>, RecoverableListener {
44
45     private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceStateListener.class);
46
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;
55
56     @Inject
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);
70     }
71
72     @Override
73     @PostConstruct
74     public void init() {
75         LOG.info("{} start", getClass().getSimpleName());
76         registerListener();
77     }
78
79     @Override
80     public void registerListener() {
81         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
82     }
83
84     @Override
85     protected InstanceIdentifier<Interface> getWildCardPath() {
86         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
87     }
88
89     @Override
90     protected void remove(InstanceIdentifier<Interface> key, Interface deleted) {
91         if (!L2vlan.class.equals(deleted.getType())) {
92             return;
93         }
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);
102
103                 if (aclList != null) {
104                     aclServiceUtils.deleteAclPortsLookup(aclInterface, aclList, aclInterface.getAllowedAddressPairs());
105                 }
106             }
107             if (aclList != null) {
108                 aclDataUtil.removeAclInterfaceMap(aclList, aclInterface);
109             }
110         }
111     }
112
113     @Override
114     protected void update(InstanceIdentifier<Interface> key, Interface before, Interface after) {
115         /*
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.
118          *
119          * We're only interested in update in cases where IfType got filled after creation.
120          */
121         if (before.getType() == null && L2vlan.class.equals(after.getType())) {
122             add(key, after);
123         } else {
124             LOG.trace("Update event for AclInterfaceStateListener is not of interest.");
125         }
126     }
127
128     @Override
129     protected void add(InstanceIdentifier<Interface> key, Interface added) {
130         if (!L2vlan.class.equals(added.getType())) {
131             return;
132         }
133
134         AclInterface aclInterface = aclInterfaceCache.addOrUpdate(added.getName(), (prevAclInterface, builder) -> {
135             builder.dpId(AclServiceUtils.getDpIdFromIterfaceState(added)).lPortTag(added.getIfIndex())
136                 .isMarkedForDelete(false);
137
138             if (AclServiceUtils.isOfInterest(prevAclInterface)) {
139                 if (prevAclInterface.getSubnetInfo() == null) {
140                     // For upgrades
141                     List<SubnetInfo> subnetInfo = aclServiceUtils.getSubnetInfo(added.getName());
142                     builder.subnetInfo(subnetInfo);
143                 }
144                 SortedSet<Integer> ingressRemoteAclTags =
145                         aclServiceUtils.getRemoteAclTags(prevAclInterface.getSecurityGroups(), DirectionIngress.class);
146                 SortedSet<Integer> egressRemoteAclTags =
147                         aclServiceUtils.getRemoteAclTags(prevAclInterface.getSecurityGroups(), DirectionEgress.class);
148                 builder.ingressRemoteAclTags(ingressRemoteAclTags).egressRemoteAclTags(egressRemoteAclTags);
149             }
150         });
151
152         List<Uuid> aclList = aclInterface.getSecurityGroups();
153         if (aclList == null) {
154             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
155                     .Interface iface = interfaceManager.getInterfaceInfoFromConfigDataStore(added.getName());
156             if (iface == null) {
157                 LOG.error("No interface with name {} available in interfaceConfig, servicing interfaceState ADD"
158                         + "for ACL failed", added.getName());
159                 return;
160             }
161             InterfaceAcl aclInPort = iface.augmentation(InterfaceAcl.class);
162             if (aclInPort == null) {
163                 LOG.trace("Interface {} is not an ACL Interface, ignoring ADD interfaceState event",
164                         added.getName());
165                 return;
166             }
167         }
168
169         if (AclServiceUtils.isOfInterest(aclInterface)) {
170             if (aclList != null) {
171                 aclDataUtil.addAclInterfaceMap(aclList, aclInterface);
172             }
173             if (aclInterface.getElanId() == null) {
174                 LOG.debug("On Add event, skip ADD since ElanId is not updated");
175                 return;
176             }
177             if (aclClusterUtil.isEntityOwner()) {
178                 LOG.debug("On add event, notify ACL service manager to add ACL for interface: {}", aclInterface);
179                 aclServiceManger.notify(aclInterface, null, Action.BIND);
180                 if (aclList != null) {
181                     aclServiceUtils.addAclPortsLookup(aclInterface, aclList, aclInterface.getAllowedAddressPairs());
182                 }
183                 aclServiceManger.notify(aclInterface, null, Action.ADD);
184             }
185         }
186     }
187
188     @Override
189     protected AclInterfaceStateListener getDataTreeChangeListener() {
190         return AclInterfaceStateListener.this;
191     }
192 }