Revert "Service-Recovery: Below service recovery actions are supported for ACL (a...
[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.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;
38
39 @Singleton
40 public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<Interface,
41         AclInterfaceStateListener> implements ClusteredDataTreeChangeListener<Interface> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceStateListener.class);
44
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;
53
54     @Inject
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;
66     }
67
68     @Override
69     @PostConstruct
70     public void init() {
71         LOG.info("{} start", getClass().getSimpleName());
72         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
73     }
74
75     @Override
76     protected InstanceIdentifier<Interface> getWildCardPath() {
77         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
78     }
79
80     @Override
81     protected void remove(InstanceIdentifier<Interface> key, Interface deleted) {
82         if (!L2vlan.class.equals(deleted.getType())) {
83             return;
84         }
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);
93
94                 if (aclList != null) {
95                     aclServiceUtils.deleteAclPortsLookup(aclInterface, aclList, aclInterface.getAllowedAddressPairs());
96                 }
97             }
98             if (aclList != null) {
99                 aclDataUtil.removeAclInterfaceMap(aclList, aclInterface);
100             }
101         }
102     }
103
104     @Override
105     protected void update(InstanceIdentifier<Interface> key, Interface before, Interface after) {
106         /*
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.
109          *
110          * We're only interested in update in cases where IfType got filled after creation.
111          */
112         if (before.getType() == null && L2vlan.class.equals(after.getType())) {
113             add(key, after);
114         } else {
115             LOG.trace("Update event for AclInterfaceStateListener is not of interest.");
116         }
117     }
118
119     @Override
120     protected void add(InstanceIdentifier<Interface> key, Interface added) {
121         if (!L2vlan.class.equals(added.getType())) {
122             return;
123         }
124         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface;
125         iface = interfaceManager.getInterfaceInfoFromConfigDataStore(added.getName());
126         if (iface == null) {
127             LOG.error("No interface with name {} available in interfaceConfig, servicing interfaceState ADD"
128                     + "for ACL failed", added.getName());
129             return;
130         }
131         InterfaceAcl aclInPort = iface.getAugmentation(InterfaceAcl.class);
132         if (aclInPort == null) {
133             LOG.trace("Interface {} is not an ACL Interface, ignoring ADD interfaceState event",
134                     added.getName());
135             return;
136         }
137
138         AclInterface aclInterface = aclInterfaceCache.addOrUpdate(added.getName(), (prevAclInterface, builder) -> {
139             builder.dpId(AclServiceUtils.getDpIdFromIterfaceState(added)).lPortTag(added.getIfIndex())
140                 .isMarkedForDelete(false);
141
142             if (AclServiceUtils.isOfInterest(prevAclInterface)) {
143                 if (prevAclInterface.getSubnetIpPrefixes() == null) {
144                     // For upgrades
145                     List<IpPrefixOrAddress> subnetIpPrefixes = AclServiceUtils.getSubnetIpPrefixes(dataBroker,
146                             added.getName());
147                     builder.subnetIpPrefixes(subnetIpPrefixes);
148                 }
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);
154             }
155         });
156
157         if (AclServiceUtils.isOfInterest(aclInterface)) {
158             List<Uuid> aclList = aclInterface.getSecurityGroups();
159             if (aclList != null) {
160                 aclDataUtil.addAclInterfaceMap(aclList, aclInterface);
161             }
162             if (aclInterface.getElanId() == null) {
163                 LOG.debug("On Add event, skip ADD since ElanId is not updated");
164                 return;
165             }
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());
171                 }
172                 aclServiceManger.notify(aclInterface, null, Action.ADD);
173             }
174         }
175     }
176
177     @Override
178     protected AclInterfaceStateListener getDataTreeChangeListener() {
179         return AclInterfaceStateListener.this;
180     }
181 }