DHCP Handling for TOR VM
[vpnservice.git] / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / vpnservice / dhcpservice / DhcpManager.java
1 /*
2  * Copyright (c) 2015 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.vpnservice.dhcpservice;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14
15 import org.apache.commons.lang3.tuple.ImmutablePair;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.vpnservice.dhcpservice.api.DHCPMConstants;
18 import org.opendaylight.vpnservice.mdsalutil.FlowEntity;
19 import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
20 import org.opendaylight.vpnservice.mdsalutil.InstructionType;
21 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
22 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
23 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
24 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
25 import org.opendaylight.vpnservice.neutronvpn.interfaces.INeutronVpnManager;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class DhcpManager implements AutoCloseable {
32
33     private static final Logger logger = LoggerFactory.getLogger(DhcpManager.class);
34     private final DataBroker broker;
35     IMdsalApiManager mdsalUtil;
36
37     private int dhcpOptLeaseTime = 0;
38     private int dhcpOptRenewalTime = 0;
39     private int dhcpOptRebindingTime = 0;
40     private String dhcpOptDefDomainName;
41     private INeutronVpnManager neutronVpnService;
42     // cache used to maintain DpnId and physical address for each interface.
43     private static HashMap<String, ImmutablePair<BigInteger, String>> interfaceToDpnIdMacAddress = new HashMap<String, ImmutablePair<BigInteger, String>>();
44
45     /**
46     * @param db - dataBroker reference
47     */
48     public DhcpManager(final DataBroker db) {
49         broker = db;
50         configureLeaseDuration(DHCPMConstants.DEFAULT_LEASE_TIME);
51     }
52
53     public void setMdsalManager(IMdsalApiManager mdsalManager) {
54         this.mdsalUtil = mdsalManager;
55     }
56
57     public void setNeutronVpnService(INeutronVpnManager neutronVpnService) {
58         logger.debug("Setting NeutronVpn dependency");
59         this.neutronVpnService = neutronVpnService;
60     }
61
62     @Override
63     public void close() throws Exception {
64         logger.info("DHCP Manager Closed");
65     }
66
67     public int setLeaseDuration(int leaseDuration) {
68         configureLeaseDuration(leaseDuration);
69         return getDhcpLeaseTime();
70     }
71
72     public String setDefaultDomain(String defaultDomain) {
73         this.dhcpOptDefDomainName = defaultDomain;
74         return getDhcpDefDomain();
75     }
76
77     protected int getDhcpLeaseTime() {
78         return this.dhcpOptLeaseTime;
79     }
80
81     protected int getDhcpRenewalTime() {
82         return this.dhcpOptLeaseTime;
83     }
84
85     protected int getDhcpRebindingTime() {
86         return this.dhcpOptLeaseTime;
87     }
88
89     protected String getDhcpDefDomain() {
90         return this.dhcpOptDefDomainName;
91     }
92
93     private void configureLeaseDuration(int leaseTime) {
94         this.dhcpOptLeaseTime = leaseTime;
95         if(leaseTime > 0) {
96             this.dhcpOptRenewalTime = this.dhcpOptLeaseTime/2;
97             this.dhcpOptRebindingTime = (this.dhcpOptLeaseTime*7)/8;
98         } else {
99             this.dhcpOptRenewalTime = -1;
100             this.dhcpOptRebindingTime = -1;
101         }
102     }
103
104     public Subnet getNeutronSubnet(Port nPort) {
105         if (nPort != null) {
106             try {
107                 return neutronVpnService.getNeutronSubnet(nPort.getFixedIps().get(0).getSubnetId());
108             } catch (Exception e) {
109                 logger.warn("Failed to get Neutron Subnet from Port: {}", e);
110             }
111         }
112         return null;
113     }
114
115     public Port getNeutronPort(String name) {
116         try {
117             return neutronVpnService.getNeutronPort(name);
118         } catch (IllegalArgumentException e) {
119             return null;
120         } catch (Exception ex) {
121             logger.trace("In getNeutronPort interface name passed {} exception message {}.", name, ex.getMessage());
122             return null;
123         }
124     }
125
126     public void installDhcpEntries(BigInteger dpnId, String vmMacAddress) {
127         DhcpServiceUtils.setupDhcpFlowEntry(dpnId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.ADD_FLOW, mdsalUtil);
128     }
129
130     public void unInstallDhcpEntries(BigInteger dpId, String vmMacAddress) {
131         DhcpServiceUtils.setupDhcpFlowEntry(dpId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.DEL_FLOW, mdsalUtil);
132     }
133
134     public void setupTableMissForDhcpTable(BigInteger dpId) {
135         List<MatchInfo> matches = new ArrayList<MatchInfo>();
136         List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
137         instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.LPORT_DISPATCHER_TABLE }));
138
139         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE, "DHCPTableMissFlow",
140                 0, "DHCP Table Miss Flow", 0, 0,
141                 DHCPMConstants.COOKIE_DHCP_BASE, matches, instructions);
142         mdsalUtil.installFlow(flowEntity);
143         setupTableMissForHandlingExternalTunnel(dpId);
144     }
145
146     private void setupTableMissForHandlingExternalTunnel(BigInteger dpId) {
147         List<MatchInfo> matches = new ArrayList<MatchInfo>();
148         List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
149         instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.EXTERNAL_TUNNEL_TABLE }));
150
151         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL, "DHCPTableMissFlowForExternalTunnel",
152                 0, "DHCP Table Miss Flow For External Tunnel", 0, 0,
153                 DHCPMConstants.COOKIE_DHCP_BASE, matches, instructions);
154         mdsalUtil.installFlow(flowEntity);
155     }
156
157     public void updateInterfaceCache(String interfaceName, ImmutablePair<BigInteger, String> pair) {
158         interfaceToDpnIdMacAddress.put(interfaceName, pair);
159     }
160
161     public ImmutablePair<BigInteger, String> getInterfaceCache(String interfaceName) {
162         return interfaceToDpnIdMacAddress.get(interfaceName);
163     }
164
165     public void removeInterfaceCache(String interfaceName) {
166         interfaceToDpnIdMacAddress.remove(interfaceName);
167     }
168 }