Fix build faliures due to OFPlugin checktyle fixes
[netvirt.git] / vpnservice / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / netvirt / dhcpservice / DhcpInterfaceEventListener.java
1 /*
2  * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. 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.dhcpservice;
9
10 import java.math.BigInteger;
11 import java.util.List;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
15 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
16 import org.opendaylight.genius.mdsalutil.MDSALUtil;
17 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
18 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
19 import org.opendaylight.netvirt.dhcpservice.jobs.DhcpInterfaceAddJob;
20 import org.opendaylight.netvirt.dhcpservice.jobs.DhcpInterfaceRemoveJob;
21 import org.opendaylight.netvirt.dhcpservice.jobs.DhcpInterfaceUpdateJob;
22 import org.opendaylight.netvirt.elanmanager.api.IElanService;
23 import org.opendaylight.netvirt.neutronvpn.api.utils.NeutronConstants;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class DhcpInterfaceEventListener
36         extends AsyncDataTreeChangeListenerBase<Interface, DhcpInterfaceEventListener> {
37
38     private static final Logger LOG = LoggerFactory.getLogger(DhcpInterfaceEventListener.class);
39
40     private final DataBroker dataBroker;
41     private final DhcpManager dhcpManager;
42     private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
43     private final JobCoordinator jobCoordinator;
44     private final IInterfaceManager interfaceManager;
45     private final IElanService elanService;
46     private final DhcpPortCache dhcpPortCache;
47
48     public DhcpInterfaceEventListener(DhcpManager dhcpManager, DataBroker dataBroker,
49                                       DhcpExternalTunnelManager dhcpExternalTunnelManager,
50                                       IInterfaceManager interfaceManager, IElanService elanService,
51                                       DhcpPortCache dhcpPortCache, JobCoordinator jobCoordinator) {
52         super(Interface.class, DhcpInterfaceEventListener.class);
53         this.dhcpManager = dhcpManager;
54         this.dataBroker = dataBroker;
55         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
56         this.interfaceManager = interfaceManager;
57         this.elanService = elanService;
58         this.dhcpPortCache = dhcpPortCache;
59         this.jobCoordinator = jobCoordinator;
60         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
61     }
62
63     @Override
64     public void close() {
65         super.close();
66         LOG.info("DhcpInterfaceEventListener Closed");
67     }
68
69     @Override
70     protected void remove(InstanceIdentifier<Interface> identifier, Interface del) {
71         if (!L2vlan.class.equals(del.getType()) && !Tunnel.class.equals(del.getType())) {
72             return;
73         }
74         List<String> ofportIds = del.getLowerLayerIf();
75         if (ofportIds == null || ofportIds.isEmpty()) {
76             return;
77         }
78         String interfaceName = del.getName();
79         Port port = dhcpPortCache.get(interfaceName);
80         if (NeutronConstants.IS_DHCP_PORT.test(port)) {
81             return;
82         }
83         NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
84         BigInteger dpnId = BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
85         DhcpInterfaceRemoveJob job = new DhcpInterfaceRemoveJob(dhcpManager, dhcpExternalTunnelManager,
86                 dataBroker, del, dpnId, interfaceManager, elanService, port);
87         jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job, DhcpMConstants.RETRY_COUNT);
88         dhcpPortCache.remove(interfaceName);
89     }
90
91     @Override
92     protected void update(InstanceIdentifier<Interface> identifier,
93             Interface original, Interface update) {
94         // We're only interested in Vlan and Tunnel ports
95         if (!L2vlan.class.equals(update.getType()) && !Tunnel.class.equals(update.getType())) {
96             return;
97         }
98         if ((original.getOperStatus().getIntValue() ^ update.getOperStatus().getIntValue()) == 0) {
99             LOG.trace("Interface operstatus {} is same", update.getOperStatus());
100             return;
101         }
102
103         if (original.getOperStatus().equals(OperStatus.Unknown) || update.getOperStatus().equals(OperStatus.Unknown)) {
104             LOG.trace("New/old interface state is unknown not handling");
105             return;
106         }
107
108         List<String> ofportIds = update.getLowerLayerIf();
109         if (ofportIds == null || ofportIds.isEmpty()) {
110             return;
111         }
112         NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
113         BigInteger dpnId = BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
114         String interfaceName = update.getName();
115         DhcpInterfaceUpdateJob job = new DhcpInterfaceUpdateJob(dhcpExternalTunnelManager, dataBroker,
116                 interfaceName, dpnId, update.getOperStatus(), interfaceManager);
117         jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job, DhcpMConstants.RETRY_COUNT);
118     }
119
120     @Override
121     protected void add(InstanceIdentifier<Interface> identifier, Interface add) {
122         // We're only interested in Vlan and Tunnel ports
123         if (!L2vlan.class.equals(add.getType()) && !Tunnel.class.equals(add.getType())) {
124             return;
125         }
126         String interfaceName = add.getName();
127         LOG.trace("DhcpInterfaceAddJob to be created for interface {}", interfaceName);
128         List<String> ofportIds = add.getLowerLayerIf();
129         if (ofportIds == null || ofportIds.isEmpty()) {
130             return;
131         }
132         Port port = dhcpManager.getNeutronPort(interfaceName);
133         if (NeutronConstants.IS_DHCP_PORT.test(port)) {
134             return;
135         }
136         dhcpPortCache.put(interfaceName, port);
137         NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
138         BigInteger dpnId = BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
139         DhcpInterfaceAddJob job = new DhcpInterfaceAddJob(dhcpManager, dhcpExternalTunnelManager, dataBroker,
140                 add, dpnId, interfaceManager, elanService);
141         jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job, DhcpMConstants.RETRY_COUNT);
142     }
143
144     @Override
145     protected InstanceIdentifier<Interface> getWildCardPath() {
146         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
147     }
148
149     @Override
150     protected DhcpInterfaceEventListener getDataTreeChangeListener() {
151         return DhcpInterfaceEventListener.this;
152     }
153 }