065b1d5f217db4093d0ac1414b5f9feccea746fa
[netvirt.git] / dhcpservice / impl / src / main / java / org / opendaylight / netvirt / dhcpservice / jobs / DhcpInterfaceUpdateJob.java
1 /*
2  * Copyright (c) 2016 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.jobs;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.concurrent.Callable;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
16 import org.opendaylight.netvirt.dhcpservice.DhcpExternalTunnelManager;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.Tunnel;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
21 import org.opendaylight.yangtools.yang.common.Uint64;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class DhcpInterfaceUpdateJob implements Callable<List<ListenableFuture<Void>>> {
26
27     private static final Logger LOG = LoggerFactory.getLogger(DhcpInterfaceUpdateJob.class);
28     private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
29     private final DataBroker dataBroker;
30     private final String interfaceName;
31     private final Uint64 dpnId;
32     private final OperStatus operStatus;
33     private final IInterfaceManager interfaceManager;
34
35     public DhcpInterfaceUpdateJob(DhcpExternalTunnelManager dhcpExternalTunnelManager,
36                                   DataBroker dataBroker, String interfaceName, Uint64 dpnId,
37                                   OperStatus operStatus, IInterfaceManager interfaceManager) {
38         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
39         this.dataBroker = dataBroker;
40         this.interfaceName = interfaceName;
41         this.dpnId = dpnId;
42         this.operStatus = operStatus;
43         this.interfaceManager = interfaceManager;
44     }
45
46     @Override
47     public List<ListenableFuture<Void>> call() {
48         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
49                 interfaceManager.getInterfaceInfoFromConfigDataStore(interfaceName);
50         if (iface == null) {
51             LOG.trace("Interface {} is not present in the config DS", interfaceName);
52             return Collections.emptyList();
53         }
54         if (Tunnel.class.equals(iface.getType())) {
55             IfTunnel tunnelInterface = iface.augmentation(IfTunnel.class);
56             if (tunnelInterface != null && !tunnelInterface.isInternal()) {
57                 IpAddress tunnelIp = tunnelInterface.getTunnelDestination();
58                 if (operStatus == OperStatus.Down) {
59                     return dhcpExternalTunnelManager.handleTunnelStateDown(tunnelIp, dpnId);
60                 } else if (operStatus == OperStatus.Up) {
61                     return dhcpExternalTunnelManager.handleTunnelStateUp(tunnelIp, dpnId);
62                 }
63             }
64         }
65         return Collections.emptyList();
66     }
67 }