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.netvirt.dhcpservice;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.annotation.PostConstruct;
14 import javax.annotation.PreDestroy;
15 import javax.inject.Inject;
16 import javax.inject.Named;
17 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
21 import org.opendaylight.genius.mdsalutil.ActionInfo;
22 import org.opendaylight.genius.mdsalutil.FlowEntity;
23 import org.opendaylight.genius.mdsalutil.InstructionInfo;
24 import org.opendaylight.genius.mdsalutil.MDSALUtil;
25 import org.opendaylight.genius.mdsalutil.MatchInfo;
26 import org.opendaylight.genius.mdsalutil.NwConstants;
27 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
28 import org.opendaylight.genius.mdsalutil.actions.ActionPuntToController;
29 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
30 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
31 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
32 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
33 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
34 import org.opendaylight.netvirt.elanmanager.api.IElanService;
35 import org.opendaylight.netvirt.neutronvpn.interfaces.INeutronVpnManager;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.config.rev150710.DhcpserviceConfig;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 public class DhcpManager {
46 private static final Logger LOG = LoggerFactory.getLogger(DhcpManager.class);
47 private final IMdsalApiManager mdsalUtil;
48 private final INeutronVpnManager neutronVpnService;
49 private final DhcpserviceConfig config;
50 private final DataBroker broker;
51 private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
52 private final IInterfaceManager interfaceManager;
53 private final IElanService elanService;
54 private final JobCoordinator jobCoordinator;
55 private DhcpPortCache dhcpPortCache;
57 private volatile int dhcpOptLeaseTime = 0;
58 private volatile String dhcpOptDefDomainName;
59 private DhcpInterfaceEventListener dhcpInterfaceEventListener;
60 private DhcpInterfaceConfigListener dhcpInterfaceConfigListener;
63 public DhcpManager(final IMdsalApiManager mdsalApiManager,
64 final INeutronVpnManager neutronVpnManager,
65 final DhcpserviceConfig config, final DataBroker dataBroker,
66 final DhcpExternalTunnelManager dhcpExternalTunnelManager, final IInterfaceManager interfaceManager,
67 @Named("elanService") IElanService ielanService, final DhcpPortCache dhcpPortCache,
68 final JobCoordinator jobCoordinator) {
69 this.mdsalUtil = mdsalApiManager;
70 this.neutronVpnService = neutronVpnManager;
72 this.broker = dataBroker;
73 this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
74 this.interfaceManager = interfaceManager;
75 this.elanService = ielanService;
76 this.dhcpPortCache = dhcpPortCache;
77 this.jobCoordinator = jobCoordinator;
78 configureLeaseDuration(DhcpMConstants.DEFAULT_LEASE_TIME);
83 LOG.trace("Netvirt DHCP Manager Init .... {}",config.isControllerDhcpEnabled());
84 if (config.isControllerDhcpEnabled()) {
85 dhcpInterfaceEventListener = new DhcpInterfaceEventListener(this, broker, dhcpExternalTunnelManager,
86 interfaceManager, elanService, dhcpPortCache, jobCoordinator);
87 dhcpInterfaceConfigListener = new DhcpInterfaceConfigListener(broker, dhcpExternalTunnelManager, this,
89 LOG.info("DHCP Service initialized");
94 public void close() throws Exception {
95 if (dhcpInterfaceEventListener != null) {
96 dhcpInterfaceEventListener.close();
98 if (dhcpInterfaceConfigListener != null) {
99 dhcpInterfaceConfigListener.close();
101 LOG.info("DHCP Service closed");
104 public int setLeaseDuration(int leaseDuration) {
105 configureLeaseDuration(leaseDuration);
106 return getDhcpLeaseTime();
109 public String setDefaultDomain(String defaultDomain) {
110 this.dhcpOptDefDomainName = defaultDomain;
111 return getDhcpDefDomain();
114 public int getDhcpLeaseTime() {
115 return this.dhcpOptLeaseTime;
118 public int getDhcpRenewalTime() {
119 return this.dhcpOptLeaseTime;
122 public int getDhcpRebindingTime() {
123 return this.dhcpOptLeaseTime;
126 public String getDhcpDefDomain() {
127 return this.dhcpOptDefDomainName;
130 private void configureLeaseDuration(int leaseTime) {
131 this.dhcpOptLeaseTime = leaseTime;
134 public Subnet getNeutronSubnet(Port port) {
136 // DHCP Service is only interested in IPv4 IPs/Subnets
137 return getNeutronSubnet(port.getFixedIps());
142 public Subnet getNeutronSubnet(List<FixedIps> fixedIps) {
143 for (FixedIps fixedIp: fixedIps) {
144 if (fixedIp.getIpAddress().getIpv4Address() != null) {
145 return neutronVpnService.getNeutronSubnet(fixedIp.getSubnetId());
151 public Port getNeutronPort(String name) {
153 return neutronVpnService.getNeutronPort(name);
154 } catch (IllegalArgumentException e) {
159 public void installDhcpEntries(BigInteger dpnId, String vmMacAddress, WriteTransaction tx) {
160 DhcpServiceUtils.setupDhcpFlowEntry(dpnId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.ADD_FLOW,
164 public void unInstallDhcpEntries(BigInteger dpId, String vmMacAddress, WriteTransaction tx) {
165 DhcpServiceUtils.setupDhcpFlowEntry(dpId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.DEL_FLOW,
169 public void setupDefaultDhcpFlows(BigInteger dpId) {
170 setupTableMissForDhcpTable(dpId);
171 if (config.isDhcpDynamicAllocationPoolEnabled()) {
172 setupDhcpAllocationPoolFlow(dpId);
176 private void setupTableMissForDhcpTable(BigInteger dpId) {
177 List<MatchInfo> matches = new ArrayList<>();
178 List<InstructionInfo> instructions = new ArrayList<>();
179 List<ActionInfo> actionsInfos = new ArrayList<>();
180 actionsInfos.add(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
181 instructions.add(new InstructionApplyActions(actionsInfos));
182 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE, "DHCPTableMissFlow",
183 0, "DHCP Table Miss Flow", 0, 0,
184 DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
185 DhcpServiceCounters.install_dhcp_table_miss_flow.inc();
186 mdsalUtil.installFlow(flowEntity);
187 setupTableMissForHandlingExternalTunnel(dpId);
190 private void setupDhcpAllocationPoolFlow(BigInteger dpId) {
191 List<MatchInfo> matches = DhcpServiceUtils.getDhcpMatch();
192 List<InstructionInfo> instructions = new ArrayList<>();
193 List<ActionInfo> actionsInfos = new ArrayList<>();
194 actionsInfos.add(new ActionPuntToController());
195 instructions.add(new InstructionApplyActions(actionsInfos));
196 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE,
197 "DhcpAllocationPoolFlow", DhcpMConstants.DEFAULT_DHCP_ALLOCATION_POOL_FLOW_PRIORITY,
198 "Dhcp Allocation Pool Flow", 0, 0, DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
199 LOG.trace("Installing DHCP Allocation Pool Flow DpId {}", dpId);
200 DhcpServiceCounters.install_dhcp_flow.inc();
201 mdsalUtil.installFlow(flowEntity);
204 private void setupTableMissForHandlingExternalTunnel(BigInteger dpId) {
205 List<MatchInfo> matches = new ArrayList<>();
206 List<InstructionInfo> instructions = new ArrayList<>();
207 instructions.add(new InstructionGotoTable(NwConstants.EXTERNAL_TUNNEL_TABLE));
209 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL,
210 "DHCPTableMissFlowForExternalTunnel",
211 0, "DHCP Table Miss Flow For External Tunnel", 0, 0,
212 DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
213 DhcpServiceCounters.install_dhcp_table_miss_flow_for_external_table.inc();
214 mdsalUtil.installFlow(flowEntity);