DHCP Handling for TOR VM
[vpnservice.git] / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / vpnservice / dhcpservice / DhcpInterfaceEventListener.java
index 5cc245653a097bdf6d4f4ac3b5f59344a06fd34c..199db9136be5478683dc9210f6ad55845c1992a6 100644 (file)
@@ -10,21 +10,28 @@ package org.opendaylight.vpnservice.dhcpservice;
 import java.math.BigInteger;
 import java.util.List;
 
+import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.vpnservice.dhcpservice.api.DHCPMConstants;
 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
 import org.opendaylight.vpnservice.mdsalutil.MDSALDataStoreUtils;
 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.api.rev150710.InterfaceNameMacAddresses;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.api.rev150710._interface.name.mac.addresses.InterfaceNameMacAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.api.rev150710._interface.name.mac.addresses.InterfaceNameMacAddressBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.api.rev150710._interface.name.mac.addresses.InterfaceNameMacAddressKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
@@ -40,26 +47,30 @@ public class DhcpInterfaceEventListener extends AbstractDataChangeListener<Inter
     private ListenerRegistration<DataChangeListener> listenerRegistration;
     private DataBroker dataBroker;
     private static final FutureCallback<Void> DEFAULT_CALLBACK = new FutureCallback<Void>() {
+        @Override
         public void onSuccess(Void result) {
             logger.debug("Success in Datastore write operation");
         }
 
+        @Override
         public void onFailure(Throwable error) {
             logger.error("Error in Datastore write operation", error);
         }
     };
+    private DhcpExternalTunnelManager dhcpExternalTunnelManager;
 
-    public DhcpInterfaceEventListener(DhcpManager dhcpManager, DataBroker dataBroker) {
+    public DhcpInterfaceEventListener(DhcpManager dhcpManager, DataBroker dataBroker, DhcpExternalTunnelManager dhcpExternalTunnelManager) {
         super(Interface.class);
         this.dhcpManager = dhcpManager;
         this.dataBroker = dataBroker;
+        this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
         registerListener();
     }
 
     private void registerListener() {
         try {
             listenerRegistration = dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
-                    getWildCardPath(), DhcpInterfaceEventListener.this, DataChangeScope.SUBTREE);
+                getWildCardPath(), DhcpInterfaceEventListener.this, DataChangeScope.SUBTREE);
         } catch (final Exception e) {
             logger.error("DhcpInterfaceEventListener DataChange listener registration fail!", e);
             throw new IllegalStateException("DhcpInterfaceEventListener registration Listener failed.", e);
@@ -85,29 +96,109 @@ public class DhcpInterfaceEventListener extends AbstractDataChangeListener<Inter
 
 
     @Override
-    protected void remove(InstanceIdentifier<Interface> identifier,
-            Interface del) {
-        String interfaceName = del.getName();
+    protected void remove(InstanceIdentifier<Interface> identifier, Interface del) {
         List<String> ofportIds = del.getLowerLayerIf();
+        if (ofportIds == null || ofportIds.isEmpty()) {
+            return;
+        }
         NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
         BigInteger dpId = BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
+        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
+                DhcpServiceUtils.getInterfaceFromConfigDS(del.getName(), dataBroker);
+        if (iface != null) {
+            IfTunnel tunnelInterface = iface.getAugmentation(IfTunnel.class);
+            if (tunnelInterface != null && !tunnelInterface.isInternal()) {
+                IpAddress tunnelIp = tunnelInterface.getTunnelDestination();
+                List<BigInteger> dpns = DhcpServiceUtils.getListOfDpns(dataBroker);
+                if (dpns.contains(dpId)) {
+                    dhcpExternalTunnelManager.handleTunnelStateDown(tunnelIp, dpId);
+                }
+                return;
+            }
+        }
+        String interfaceName = del.getName();
         logger.trace("Received remove DCN for interface {} dpId {}", interfaceName, dpId);
         unInstallDhcpEntries(interfaceName, dpId);
+        dhcpManager.removeInterfaceCache(interfaceName);
     }
 
     @Override
     protected void update(InstanceIdentifier<Interface> identifier,
             Interface original, Interface update) {
+        List<String> ofportIds = update.getLowerLayerIf();
+        if (ofportIds == null || ofportIds.isEmpty()) {
+            return;
+        }
+        NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
+        BigInteger dpId = BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
+        String interfaceName = update.getName();
+        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
+                DhcpServiceUtils.getInterfaceFromConfigDS(interfaceName, dataBroker);
+        if (iface == null) {
+            logger.trace("Interface {} is not present in the config DS", interfaceName);
+            return;
+        }
+        if (update.getType() == null) {
+            logger.trace("Interface type for interface {} is null", interfaceName);
+            return;
+        }
+        if ((original.getOperStatus().getIntValue() ^ update.getOperStatus().getIntValue()) == 0) {
+            logger.trace("Interface operstatus {} is same", update.getOperStatus());
+            return;
+        }
+        if (Tunnel.class.equals(update.getType())) {
+            IfTunnel tunnelInterface = iface.getAugmentation(IfTunnel.class);
+            if (tunnelInterface != null && !tunnelInterface.isInternal()) {
+                IpAddress tunnelIp = tunnelInterface.getTunnelDestination();
+                List<BigInteger> dpns = DhcpServiceUtils.getListOfDpns(dataBroker);
+                if (dpns.contains(dpId)) {
+                    if (update.getOperStatus() == OperStatus.Down) {
+                        dhcpExternalTunnelManager.handleTunnelStateDown(tunnelIp, dpId);
+                    } else if (update.getOperStatus() == OperStatus.Up) {
+                        dhcpExternalTunnelManager.handleTunnelStateUp(tunnelIp, dpId);
+                    }
+                }
+            }
+            return;
+        }
+        if (update.getOperStatus() == OperStatus.Down) {
+            unInstallDhcpEntries(interfaceName, dpId);
+            dhcpManager.removeInterfaceCache(interfaceName);
+        } else if (update.getOperStatus() == OperStatus.Up) {
+            if (!dpId.equals(DHCPMConstants.INVALID_DPID)) {
+                installDhcpEntries(interfaceName, dpId);
+                dhcpManager.updateInterfaceCache(interfaceName, new ImmutablePair<BigInteger, String>(dpId, update.getPhysAddress().getValue()));
+            }
+        }
     }
 
     @Override
     protected void add(InstanceIdentifier<Interface> identifier, Interface add) {
         String interfaceName = add.getName();
         List<String> ofportIds = add.getLowerLayerIf();
+        if (ofportIds == null || ofportIds.isEmpty()) {
+            return;
+        }
         NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
         BigInteger dpId = BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
         logger.trace("Received add DCN for interface {}, dpid {}", interfaceName, dpId);
-        installDhcpEntries(interfaceName, dpId);
+        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
+                DhcpServiceUtils.getInterfaceFromConfigDS(add.getName(), dataBroker);
+        if (iface != null) {
+            IfTunnel tunnelInterface = iface.getAugmentation(IfTunnel.class);
+            if (tunnelInterface != null && !tunnelInterface.isInternal()) {
+                IpAddress tunnelIp = tunnelInterface.getTunnelDestination();
+                List<BigInteger> dpns = DhcpServiceUtils.getListOfDpns(dataBroker);
+                if (dpns.contains(dpId)) {
+                    dhcpExternalTunnelManager.handleTunnelStateUp(tunnelIp, dpId);
+                }
+                return;
+            }
+        }
+        if (!dpId.equals(DHCPMConstants.INVALID_DPID)) {
+            installDhcpEntries(interfaceName, dpId);
+            dhcpManager.updateInterfaceCache(interfaceName, new ImmutablePair<BigInteger, String>(dpId, add.getPhysAddress().getValue()));
+        }
     }
 
     private String getNeutronMacAddress(String interfaceName) {
@@ -116,7 +207,6 @@ public class DhcpInterfaceEventListener extends AbstractDataChangeListener<Inter
             logger.trace("Port found in neutron. Interface Name {}, port {}", interfaceName, port);
             return port.getMacAddress();
         }
-        logger.trace("Port not found in neutron. Interface Name {}", interfaceName);
         return null;
     }