move vpnservice and cleanup poms
[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.mdsalutil.NwConstants;
21 import org.opendaylight.netvirt.aclservice.api.AclInterfaceCache;
22 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
23 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
24 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
25 import org.opendaylight.netvirt.aclservice.utils.AclClusterUtil;
26 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
27 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionIngress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.InterfaceAcl;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.IpPrefixOrAddress;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 @Singleton
41 public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<Interface,
42         AclInterfaceStateListener> implements ClusteredDataTreeChangeListener<Interface> {
43
44     private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceStateListener.class);
45
46     /** Our registration. */
47     private final AclServiceManager aclServiceManger;
48     private final AclClusterUtil aclClusterUtil;
49     private final DataBroker dataBroker;
50     private final AclDataUtil aclDataUtil;
51     private final IInterfaceManager interfaceManager;
52     private final AclInterfaceCache aclInterfaceCache;
53     private final AclServiceUtils aclServiceUtils;
54
55     @Inject
56     public AclInterfaceStateListener(AclServiceManager aclServiceManger, AclClusterUtil aclClusterUtil,
57             DataBroker dataBroker, AclDataUtil aclDataUtil, IInterfaceManager interfaceManager,
58             AclInterfaceCache aclInterfaceCache, AclServiceUtils aclServicUtils) {
59         super(Interface.class, AclInterfaceStateListener.class);
60         this.aclServiceManger = aclServiceManger;
61         this.aclClusterUtil = aclClusterUtil;
62         this.dataBroker = dataBroker;
63         this.aclDataUtil = aclDataUtil;
64         this.interfaceManager = interfaceManager;
65         this.aclInterfaceCache = aclInterfaceCache;
66         this.aclServiceUtils = aclServicUtils;
67     }
68
69     @Override
70     @PostConstruct
71     public void init() {
72         LOG.info("{} start", getClass().getSimpleName());
73         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
74     }
75
76     @Override
77     protected InstanceIdentifier<Interface> getWildCardPath() {
78         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
79     }
80
81     @Override
82     protected void remove(InstanceIdentifier<Interface> key, Interface deleted) {
83         if (!L2vlan.class.equals(deleted.getType())) {
84             return;
85         }
86         String interfaceId = deleted.getName();
87         AclInterface aclInterface = aclInterfaceCache.remove(interfaceId);
88         if (AclServiceUtils.isOfInterest(aclInterface)) {
89             List<Uuid> aclList = aclInterface.getSecurityGroups();
90             if (aclClusterUtil.isEntityOwner()) {
91                 LOG.debug("On remove event, notify ACL service manager to remove ACL from interface: {}", aclInterface);
92                 aclServiceManger.notify(aclInterface, null, Action.UNBIND);
93                 aclServiceManger.notify(aclInterface, null, Action.REMOVE);
94
95                 if (aclList != null) {
96                     AclServiceUtils.updateAclPortsLookup(aclInterface, aclList, aclInterface.getAllowedAddressPairs(),
97                             NwConstants.DEL_FLOW, this.dataBroker);
98                 }
99             }
100             if (aclList != null) {
101                 aclDataUtil.removeAclInterfaceMap(aclList, aclInterface);
102             }
103         }
104     }
105
106     @Override
107     protected void update(InstanceIdentifier<Interface> key, Interface before, Interface after) {
108         /*
109          * The update is not of interest as the attributes populated from this listener will not change.
110          * The northbound updates are handled in AclInterfaceListener.
111          *
112          * We're only interested in update in cases where IfType got filled after creation.
113          */
114         if (before.getType() == null && L2vlan.class.equals(after.getType())) {
115             add(key, after);
116         } else {
117             LOG.trace("Update event for AclInterfaceStateListener is not of interest.");
118         }
119     }
120
121     @Override
122     protected void add(InstanceIdentifier<Interface> key, Interface added) {
123         if (!L2vlan.class.equals(added.getType())) {
124             return;
125         }
126         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface;
127         iface = interfaceManager.getInterfaceInfoFromConfigDataStore(added.getName());
128         if (iface == null) {
129             LOG.error("No interface with name {} available in interfaceConfig, servicing interfaceState ADD"
130                     + "for ACL failed", added.getName());
131             return;
132         }
133         InterfaceAcl aclInPort = iface.getAugmentation(InterfaceAcl.class);
134         if (aclInPort == null) {
135             LOG.trace("Interface {} is not an ACL Interface, ignoring ADD interfaceState event",
136                     added.getName());
137             return;
138         }
139
140         AclInterface aclInterface = aclInterfaceCache.addOrUpdate(added.getName(), (prevAclInterface, builder) -> {
141             builder.dpId(AclServiceUtils.getDpIdFromIterfaceState(added)).lPortTag(added.getIfIndex())
142                 .isMarkedForDelete(false);
143
144             if (AclServiceUtils.isOfInterest(prevAclInterface)) {
145                 if (prevAclInterface.getSubnetIpPrefixes() == null) {
146                     // For upgrades
147                     List<IpPrefixOrAddress> subnetIpPrefixes = AclServiceUtils.getSubnetIpPrefixes(dataBroker,
148                             added.getName());
149                     builder.subnetIpPrefixes(subnetIpPrefixes);
150                 }
151                 SortedSet<Integer> ingressRemoteAclTags =
152                         aclServiceUtils.getRemoteAclTags(aclInPort.getSecurityGroups(), DirectionIngress.class);
153                 SortedSet<Integer> egressRemoteAclTags =
154                         aclServiceUtils.getRemoteAclTags(aclInPort.getSecurityGroups(), DirectionEgress.class);
155                 builder.ingressRemoteAclTags(ingressRemoteAclTags).egressRemoteAclTags(egressRemoteAclTags);
156             }
157         });
158
159         if (AclServiceUtils.isOfInterest(aclInterface)) {
160             List<Uuid> aclList = aclInterface.getSecurityGroups();
161             if (aclList != null) {
162                 aclDataUtil.addAclInterfaceMap(aclList, aclInterface);
163             }
164             if (aclInterface.getElanId() == null) {
165                 LOG.debug("On Add event, skip ADD since ElanId is not updated");
166                 return;
167             }
168             if (aclClusterUtil.isEntityOwner()) {
169                 LOG.debug("On add event, notify ACL service manager to add ACL for interface: {}", aclInterface);
170                 aclServiceManger.notify(aclInterface, null, Action.BIND);
171                 if (aclList != null) {
172                     AclServiceUtils.updateAclPortsLookup(aclInterface, aclList, aclInterface.getAllowedAddressPairs(),
173                             NwConstants.ADD_FLOW, this.dataBroker);
174                 }
175                 aclServiceManger.notify(aclInterface, null, Action.ADD);
176             }
177         }
178     }
179
180     @Override
181     protected AclInterfaceStateListener getDataTreeChangeListener() {
182         return AclInterfaceStateListener.this;
183     }
184 }