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.Nullable;
14 import javax.annotation.PostConstruct;
15 import javax.annotation.PreDestroy;
16 import javax.inject.Inject;
17 import javax.inject.Named;
18 import javax.inject.Singleton;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
22 import org.opendaylight.genius.mdsalutil.ActionInfo;
23 import org.opendaylight.genius.mdsalutil.FlowEntity;
24 import org.opendaylight.genius.mdsalutil.InstructionInfo;
25 import org.opendaylight.genius.mdsalutil.MDSALUtil;
26 import org.opendaylight.genius.mdsalutil.MatchInfo;
27 import org.opendaylight.genius.mdsalutil.NwConstants;
28 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
29 import org.opendaylight.genius.mdsalutil.actions.ActionPuntToController;
30 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
31 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
32 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
33 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
34 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
35 import org.opendaylight.netvirt.elanmanager.api.IElanService;
36 import org.opendaylight.netvirt.neutronvpn.interfaces.INeutronVpnManager;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.config.rev150710.DhcpserviceConfig;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 public class DhcpManager {
48 private static final Logger LOG = LoggerFactory.getLogger(DhcpManager.class);
49 private final IMdsalApiManager mdsalUtil;
50 private final INeutronVpnManager neutronVpnService;
51 private final DhcpserviceConfig config;
52 private final DataBroker broker;
53 private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
54 private final IInterfaceManager interfaceManager;
55 private final IElanService elanService;
56 private final JobCoordinator jobCoordinator;
57 private DhcpPortCache dhcpPortCache;
58 private final ItmRpcService itmRpcService;
60 private volatile int dhcpOptLeaseTime = 0;
61 private volatile String dhcpOptDefDomainName;
62 private DhcpInterfaceEventListener dhcpInterfaceEventListener;
63 private DhcpInterfaceConfigListener dhcpInterfaceConfigListener;
66 public DhcpManager(final IMdsalApiManager mdsalApiManager,
67 final INeutronVpnManager neutronVpnManager,
68 final DhcpserviceConfig config, final DataBroker dataBroker,
69 final DhcpExternalTunnelManager dhcpExternalTunnelManager, final IInterfaceManager interfaceManager,
70 @Named("elanService") IElanService ielanService, final DhcpPortCache dhcpPortCache,
71 final JobCoordinator jobCoordinator, final ItmRpcService itmRpcService) {
72 this.mdsalUtil = mdsalApiManager;
73 this.neutronVpnService = neutronVpnManager;
75 this.broker = dataBroker;
76 this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
77 this.interfaceManager = interfaceManager;
78 this.elanService = ielanService;
79 this.dhcpPortCache = dhcpPortCache;
80 this.jobCoordinator = jobCoordinator;
81 this.itmRpcService = itmRpcService;
82 configureLeaseDuration(DhcpMConstants.DEFAULT_LEASE_TIME);
87 LOG.trace("Netvirt DHCP Manager Init .... {}",config.isControllerDhcpEnabled());
88 if (config.isControllerDhcpEnabled()) {
89 dhcpInterfaceEventListener = new DhcpInterfaceEventListener(this, broker, dhcpExternalTunnelManager,
90 interfaceManager, elanService, dhcpPortCache, jobCoordinator, itmRpcService);
91 dhcpInterfaceConfigListener = new DhcpInterfaceConfigListener(broker, dhcpExternalTunnelManager, this,
93 LOG.info("DHCP Service initialized");
99 if (dhcpInterfaceEventListener != null) {
100 dhcpInterfaceEventListener.close();
102 if (dhcpInterfaceConfigListener != null) {
103 dhcpInterfaceConfigListener.close();
105 LOG.info("DHCP Service closed");
108 public int setLeaseDuration(int leaseDuration) {
109 configureLeaseDuration(leaseDuration);
110 return getDhcpLeaseTime();
113 public String setDefaultDomain(String defaultDomain) {
114 this.dhcpOptDefDomainName = defaultDomain;
115 return getDhcpDefDomain();
118 public int getDhcpLeaseTime() {
119 return this.dhcpOptLeaseTime;
122 public int getDhcpRenewalTime() {
123 return this.dhcpOptLeaseTime;
126 public int getDhcpRebindingTime() {
127 return this.dhcpOptLeaseTime;
130 public String getDhcpDefDomain() {
131 return this.dhcpOptDefDomainName;
134 private void configureLeaseDuration(int leaseTime) {
135 this.dhcpOptLeaseTime = leaseTime;
138 public Subnet getNeutronSubnet(Port port) {
140 // DHCP Service is only interested in IPv4 IPs/Subnets
141 return getNeutronSubnet(port.getFixedIps());
146 public Subnet getNeutronSubnet(List<FixedIps> fixedIps) {
147 for (FixedIps fixedIp: fixedIps) {
148 if (fixedIp.getIpAddress().getIpv4Address() != null) {
149 return neutronVpnService.getNeutronSubnet(fixedIp.getSubnetId());
155 public Port getNeutronPort(String name) {
157 return neutronVpnService.getNeutronPort(name);
158 } catch (IllegalArgumentException e) {
163 public void installDhcpEntries(@Nullable BigInteger dpnId, @Nullable String vmMacAddress, WriteTransaction tx) {
164 DhcpServiceUtils.setupDhcpFlowEntry(dpnId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.ADD_FLOW,
168 public void unInstallDhcpEntries(@Nullable BigInteger dpId, @Nullable String vmMacAddress, WriteTransaction tx) {
169 DhcpServiceUtils.setupDhcpFlowEntry(dpId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.DEL_FLOW,
173 public void setupDefaultDhcpFlows(BigInteger dpId) {
174 setupTableMissForDhcpTable(dpId);
175 if (config.isDhcpDynamicAllocationPoolEnabled()) {
176 setupDhcpAllocationPoolFlow(dpId);
180 private void setupTableMissForDhcpTable(BigInteger dpId) {
181 List<MatchInfo> matches = new ArrayList<>();
182 List<InstructionInfo> instructions = new ArrayList<>();
183 List<ActionInfo> actionsInfos = new ArrayList<>();
184 actionsInfos.add(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
185 instructions.add(new InstructionApplyActions(actionsInfos));
186 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE, "DHCPTableMissFlow",
187 0, "DHCP Table Miss Flow", 0, 0,
188 DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
189 DhcpServiceCounters.install_dhcp_table_miss_flow.inc();
190 mdsalUtil.installFlow(flowEntity);
191 setupTableMissForHandlingExternalTunnel(dpId);
194 private void setupDhcpAllocationPoolFlow(BigInteger dpId) {
195 List<MatchInfo> matches = DhcpServiceUtils.getDhcpMatch();
196 List<InstructionInfo> instructions = new ArrayList<>();
197 List<ActionInfo> actionsInfos = new ArrayList<>();
198 actionsInfos.add(new ActionPuntToController());
199 instructions.add(new InstructionApplyActions(actionsInfos));
200 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE,
201 "DhcpAllocationPoolFlow", DhcpMConstants.DEFAULT_DHCP_ALLOCATION_POOL_FLOW_PRIORITY,
202 "Dhcp Allocation Pool Flow", 0, 0, DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
203 LOG.trace("Installing DHCP Allocation Pool Flow DpId {}", dpId);
204 DhcpServiceCounters.install_dhcp_flow.inc();
205 mdsalUtil.installFlow(flowEntity);
208 private void setupTableMissForHandlingExternalTunnel(BigInteger dpId) {
209 List<MatchInfo> matches = new ArrayList<>();
210 List<InstructionInfo> instructions = new ArrayList<>();
211 instructions.add(new InstructionGotoTable(NwConstants.EXTERNAL_TUNNEL_TABLE));
213 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL,
214 "DHCPTableMissFlowForExternalTunnel",
215 0, "DHCP Table Miss Flow For External Tunnel", 0, 0,
216 DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
217 DhcpServiceCounters.install_dhcp_table_miss_flow_for_external_table.inc();
218 mdsalUtil.installFlow(flowEntity);