DHCP Handling for TOR VM
[vpnservice.git] / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / vpnservice / dhcpservice / DhcpManager.java
index 2309defb00a5ad69c9b46bcd792508228718d20a..af36ca955d24949290f1b1a0b990fe13987fe5d2 100644 (file)
@@ -7,36 +7,22 @@
  */
 package org.opendaylight.vpnservice.dhcpservice;
 
-import org.opendaylight.vpnservice.neutronvpn.interfaces.INeutronVpnManager;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
-
-import com.google.common.base.Optional;
-
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-
 import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 
-import com.google.common.util.concurrent.FutureCallback;
-
+import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.vpnservice.dhcpservice.api.DHCPMConstants;
-import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
-import org.opendaylight.vpnservice.mdsalutil.ActionType;
 import org.opendaylight.vpnservice.mdsalutil.FlowEntity;
 import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
 import org.opendaylight.vpnservice.mdsalutil.InstructionType;
 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
-import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
-import org.opendaylight.vpnservice.mdsalutil.packet.IPProtocols;
+import org.opendaylight.vpnservice.neutronvpn.interfaces.INeutronVpnManager;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
 import org.slf4j.Logger;
@@ -53,16 +39,8 @@ public class DhcpManager implements AutoCloseable {
     private int dhcpOptRebindingTime = 0;
     private String dhcpOptDefDomainName;
     private INeutronVpnManager neutronVpnService;
-
-    private static final FutureCallback<Void> DEFAULT_CALLBACK =
-        new FutureCallback<Void>() {
-            public void onSuccess(Void result) {
-                logger.debug("Success in Datastore write operation");
-            }
-            public void onFailure(Throwable error) {
-                logger.error("Error in Datastore write operation", error);
-            };
-        };
+    // cache used to maintain DpnId and physical address for each interface.
+    private static HashMap<String, ImmutablePair<BigInteger, String>> interfaceToDpnIdMacAddress = new HashMap<String, ImmutablePair<BigInteger, String>>();
 
     /**
     * @param db - dataBroker reference
@@ -86,43 +64,6 @@ public class DhcpManager implements AutoCloseable {
         logger.info("DHCP Manager Closed");
     }
 
-    public void installDhcpEntries(BigInteger dpnId) {
-        logger.debug("Installing Default DHCP Flow tp DPN: {}", dpnId);
-        setupDefaultDhcpFlow(dpnId, NwConstants.DHCP_TABLE, NwConstants.ADD_FLOW);
-    }
-
-    private void setupDefaultDhcpFlow(BigInteger dpId,  short tableId, int addOrRemove) {
-
-        List<MatchInfo> matches = new ArrayList<MatchInfo>();
-
-        matches.add(new MatchInfo(MatchFieldType.eth_type,
-                new long[] { NwConstants.ETHTYPE_IPV4 }));
-        matches.add(new MatchInfo(MatchFieldType.ip_proto,
-                new long[] { IPProtocols.UDP.intValue() }));
-        matches.add(new MatchInfo(MatchFieldType.udp_src,
-                new long[] { DHCPMConstants.dhcpClientPort }));
-        matches.add(new MatchInfo(MatchFieldType.udp_dst,
-                new long[] { DHCPMConstants.dhcpServerPort }));
-
-        List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
-        List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
-
-        // Punt to controller
-        actionsInfos.add(new ActionInfo(ActionType.punt_to_controller,
-                new String[] {}));
-        instructions.add(new InstructionInfo(InstructionType.write_actions,
-                actionsInfos));
-        FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, tableId,
-                getDefaultDhcpFlowRef(dpId, tableId),DHCPMConstants.DEFAULT_DHCP_FLOW_PRIORITY, "DHCP", 0, 0,
-                DHCPMConstants.COOKIE_DHCP_BASE, matches, instructions);
-        mdsalUtil.installFlow(flowEntity);
-    }
-
-    private String getDefaultDhcpFlowRef(BigInteger dpId, long tableId) {
-        return new StringBuffer().append(DHCPMConstants.FLOWID_PREFIX).append(dpId)
-                        .append(NwConstants.FLOWID_SEPARATOR).append(tableId).toString();
-    }
-
     public int setLeaseDuration(int leaseDuration) {
         configureLeaseDuration(leaseDuration);
         return getDhcpLeaseTime();
@@ -174,6 +115,8 @@ public class DhcpManager implements AutoCloseable {
     public Port getNeutronPort(String name) {
         try {
             return neutronVpnService.getNeutronPort(name);
+        } catch (IllegalArgumentException e) {
+            return null;
         } catch (Exception ex) {
             logger.trace("In getNeutronPort interface name passed {} exception message {}.", name, ex.getMessage());
             return null;
@@ -181,59 +124,11 @@ public class DhcpManager implements AutoCloseable {
     }
 
     public void installDhcpEntries(BigInteger dpnId, String vmMacAddress) {
-        setupDhcpFlowEntry(dpnId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.ADD_FLOW);
-    }
-
-    private void setupDhcpFlowEntry(BigInteger dpId, short tableId, String vmMacAddress, int addOrRemove) {
-        if (dpId == null || dpId == DHCPMConstants.INVALID_DPID || vmMacAddress == null) {
-            return;
-        }
-        List<MatchInfo> matches = new ArrayList<MatchInfo>();
-
-        matches.add(new MatchInfo(MatchFieldType.eth_type,
-                new long[] { NwConstants.ETHTYPE_IPV4 }));
-        matches.add(new MatchInfo(MatchFieldType.ip_proto,
-                new long[] { IPProtocols.UDP.intValue() }));
-        matches.add(new MatchInfo(MatchFieldType.udp_src,
-                new long[] { DHCPMConstants.dhcpClientPort }));
-        matches.add(new MatchInfo(MatchFieldType.udp_dst,
-                new long[] { DHCPMConstants.dhcpServerPort }));
-        matches.add(new MatchInfo(MatchFieldType.eth_src,
-                new String[] { vmMacAddress }));
-
-        List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
-        List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
-
-        // Punt to controller
-        actionsInfos.add(new ActionInfo(ActionType.punt_to_controller,
-                new String[] {}));
-        instructions.add(new InstructionInfo(InstructionType.write_actions,
-                actionsInfos));
-        if (addOrRemove == NwConstants.DEL_FLOW) {
-            FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, tableId,
-                    getDhcpFlowRef(dpId, tableId, vmMacAddress),
-                    DHCPMConstants.DEFAULT_DHCP_FLOW_PRIORITY, "DHCP", 0, 0,
-                    DHCPMConstants.COOKIE_DHCP_BASE, matches, null);
-            logger.trace("Removing DHCP Flow DpId {}, vmMacAddress {}", dpId, vmMacAddress);
-            mdsalUtil.removeFlow(flowEntity);
-        } else {
-            FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, tableId,
-                    getDhcpFlowRef(dpId, tableId, vmMacAddress),DHCPMConstants.DEFAULT_DHCP_FLOW_PRIORITY, "DHCP", 0, 0,
-                    DHCPMConstants.COOKIE_DHCP_BASE, matches, instructions);
-            logger.trace("Installing DHCP Flow DpId {}, vmMacAddress {}", dpId, vmMacAddress);
-            mdsalUtil.installFlow(flowEntity);
-        }
-    }
-
-    private String getDhcpFlowRef(BigInteger dpId, long tableId, String vmMacAddress) {
-        return new StringBuffer().append(DHCPMConstants.FLOWID_PREFIX)
-                .append(dpId).append(NwConstants.FLOWID_SEPARATOR)
-                .append(tableId).append(NwConstants.FLOWID_SEPARATOR)
-                .append(vmMacAddress).toString();
+        DhcpServiceUtils.setupDhcpFlowEntry(dpnId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.ADD_FLOW, mdsalUtil);
     }
 
     public void unInstallDhcpEntries(BigInteger dpId, String vmMacAddress) {
-        setupDhcpFlowEntry(dpId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.DEL_FLOW);
+        DhcpServiceUtils.setupDhcpFlowEntry(dpId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.DEL_FLOW, mdsalUtil);
     }
 
     public void setupTableMissForDhcpTable(BigInteger dpId) {
@@ -245,5 +140,29 @@ public class DhcpManager implements AutoCloseable {
                 0, "DHCP Table Miss Flow", 0, 0,
                 DHCPMConstants.COOKIE_DHCP_BASE, matches, instructions);
         mdsalUtil.installFlow(flowEntity);
+        setupTableMissForHandlingExternalTunnel(dpId);
+    }
+
+    private void setupTableMissForHandlingExternalTunnel(BigInteger dpId) {
+        List<MatchInfo> matches = new ArrayList<MatchInfo>();
+        List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
+        instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.EXTERNAL_TUNNEL_TABLE }));
+
+        FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL, "DHCPTableMissFlowForExternalTunnel",
+                0, "DHCP Table Miss Flow For External Tunnel", 0, 0,
+                DHCPMConstants.COOKIE_DHCP_BASE, matches, instructions);
+        mdsalUtil.installFlow(flowEntity);
+    }
+
+    public void updateInterfaceCache(String interfaceName, ImmutablePair<BigInteger, String> pair) {
+        interfaceToDpnIdMacAddress.put(interfaceName, pair);
+    }
+
+    public ImmutablePair<BigInteger, String> getInterfaceCache(String interfaceName) {
+        return interfaceToDpnIdMacAddress.get(interfaceName);
+    }
+
+    public void removeInterfaceCache(String interfaceName) {
+        interfaceToDpnIdMacAddress.remove(interfaceName);
     }
 }