Fixup Augmentable and Identifiable methods changing
[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         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface;
134         iface = interfaceManager.getInterfaceInfoFromConfigDataStore(added.getName());
135         if (iface == null) {
136             LOG.error("No interface with name {} available in interfaceConfig, servicing interfaceState ADD"
137                     + "for ACL failed", added.getName());
138             return;
139         }
140         InterfaceAcl aclInPort = iface.augmentation(InterfaceAcl.class);
141         if (aclInPort == null) {
142             LOG.trace("Interface {} is not an ACL Interface, ignoring ADD interfaceState event",
143                     added.getName());
144             return;
145         }
146
147         AclInterface aclInterface = aclInterfaceCache.addOrUpdate(added.getName(), (prevAclInterface, builder) -> {
148             builder.dpId(AclServiceUtils.getDpIdFromIterfaceState(added)).lPortTag(added.getIfIndex())
149                 .isMarkedForDelete(false);
150
151             if (AclServiceUtils.isOfInterest(prevAclInterface)) {
152                 if (prevAclInterface.getSubnetInfo() == null) {
153                     // For upgrades
154                     List<SubnetInfo> subnetInfo = aclServiceUtils.getSubnetInfo(added.getName());
155                     builder.subnetInfo(subnetInfo);
156                 }
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);
162             }
163         });
164
165         if (AclServiceUtils.isOfInterest(aclInterface)) {
166             List<Uuid> aclList = aclInterface.getSecurityGroups();
167             if (aclList != null) {
168                 aclDataUtil.addAclInterfaceMap(aclList, aclInterface);
169             }
170             if (aclInterface.getElanId() == null) {
171                 LOG.debug("On Add event, skip ADD since ElanId is not updated");
172                 return;
173             }
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());
179                 }
180                 aclServiceManger.notify(aclInterface, null, Action.ADD);
181             }
182         }
183     }
184
185     @Override
186     protected AclInterfaceStateListener getDataTreeChangeListener() {
187         return AclInterfaceStateListener.this;
188     }
189 }