2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.dhcpservice;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
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;
31 public class DhcpManager implements AutoCloseable {
33 private static final Logger logger = LoggerFactory.getLogger(DhcpManager.class);
34 private final DataBroker broker;
35 IMdsalApiManager mdsalUtil;
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>>();
46 * @param db - dataBroker reference
48 public DhcpManager(final DataBroker db) {
50 configureLeaseDuration(DHCPMConstants.DEFAULT_LEASE_TIME);
53 public void setMdsalManager(IMdsalApiManager mdsalManager) {
54 this.mdsalUtil = mdsalManager;
57 public void setNeutronVpnService(INeutronVpnManager neutronVpnService) {
58 logger.debug("Setting NeutronVpn dependency");
59 this.neutronVpnService = neutronVpnService;
63 public void close() throws Exception {
64 logger.info("DHCP Manager Closed");
67 public int setLeaseDuration(int leaseDuration) {
68 configureLeaseDuration(leaseDuration);
69 return getDhcpLeaseTime();
72 public String setDefaultDomain(String defaultDomain) {
73 this.dhcpOptDefDomainName = defaultDomain;
74 return getDhcpDefDomain();
77 protected int getDhcpLeaseTime() {
78 return this.dhcpOptLeaseTime;
81 protected int getDhcpRenewalTime() {
82 return this.dhcpOptLeaseTime;
85 protected int getDhcpRebindingTime() {
86 return this.dhcpOptLeaseTime;
89 protected String getDhcpDefDomain() {
90 return this.dhcpOptDefDomainName;
93 private void configureLeaseDuration(int leaseTime) {
94 this.dhcpOptLeaseTime = leaseTime;
96 this.dhcpOptRenewalTime = this.dhcpOptLeaseTime/2;
97 this.dhcpOptRebindingTime = (this.dhcpOptLeaseTime*7)/8;
99 this.dhcpOptRenewalTime = -1;
100 this.dhcpOptRebindingTime = -1;
104 public Subnet getNeutronSubnet(Port nPort) {
107 return neutronVpnService.getNeutronSubnet(nPort.getFixedIps().get(0).getSubnetId());
108 } catch (Exception e) {
109 logger.warn("Failed to get Neutron Subnet from Port: {}", e);
115 public Port getNeutronPort(String name) {
117 return neutronVpnService.getNeutronPort(name);
118 } catch (IllegalArgumentException e) {
120 } catch (Exception ex) {
121 logger.trace("In getNeutronPort interface name passed {} exception message {}.", name, ex.getMessage());
126 public void installDhcpEntries(BigInteger dpnId, String vmMacAddress) {
127 DhcpServiceUtils.setupDhcpFlowEntry(dpnId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.ADD_FLOW, mdsalUtil);
130 public void unInstallDhcpEntries(BigInteger dpId, String vmMacAddress) {
131 DhcpServiceUtils.setupDhcpFlowEntry(dpId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.DEL_FLOW, mdsalUtil);
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 }));
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);
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 }));
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);
157 public void updateInterfaceCache(String interfaceName, ImmutablePair<BigInteger, String> pair) {
158 interfaceToDpnIdMacAddress.put(interfaceName, pair);
161 public ImmutablePair<BigInteger, String> getInterfaceCache(String interfaceName) {
162 return interfaceToDpnIdMacAddress.get(interfaceName);
165 public void removeInterfaceCache(String interfaceName) {
166 interfaceToDpnIdMacAddress.remove(interfaceName);