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.util.ArrayList;
11 import java.util.List;
12 import java.util.Optional;
13 import java.util.concurrent.ExecutionException;
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.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
21 import org.opendaylight.genius.infra.Datastore.Configuration;
22 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
23 import org.opendaylight.genius.infra.TypedWriteTransaction;
24 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
25 import org.opendaylight.genius.mdsalutil.ActionInfo;
26 import org.opendaylight.genius.mdsalutil.FlowEntity;
27 import org.opendaylight.genius.mdsalutil.InstructionInfo;
28 import org.opendaylight.genius.mdsalutil.MDSALUtil;
29 import org.opendaylight.genius.mdsalutil.MatchInfo;
30 import org.opendaylight.genius.mdsalutil.NwConstants;
31 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
32 import org.opendaylight.genius.mdsalutil.actions.ActionPuntToController;
33 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
34 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
35 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
36 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
37 import org.opendaylight.mdsal.binding.api.DataBroker;
38 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
39 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
40 import org.opendaylight.netvirt.elanmanager.api.IElanService;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.config.rev150710.DhcpserviceConfig;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.common.Uint64;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 public class DhcpManager {
60 private static final Logger LOG = LoggerFactory.getLogger(DhcpManager.class);
61 private final IMdsalApiManager mdsalUtil;
62 private final DhcpserviceConfig config;
63 private final DataBroker broker;
64 private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
65 private final IInterfaceManager interfaceManager;
66 private final IElanService elanService;
67 private final JobCoordinator jobCoordinator;
68 private DhcpPortCache dhcpPortCache;
69 private final ItmRpcService itmRpcService;
70 private final DhcpServiceCounters dhcpServiceCounters;
72 private volatile int dhcpOptLeaseTime = 0;
73 private volatile String dhcpOptDefDomainName;
74 private DhcpInterfaceEventListener dhcpInterfaceEventListener;
75 private DhcpInterfaceConfigListener dhcpInterfaceConfigListener;
78 public DhcpManager(final IMdsalApiManager mdsalApiManager,
79 final DhcpserviceConfig config, final DataBroker dataBroker,
80 final DhcpExternalTunnelManager dhcpExternalTunnelManager, final IInterfaceManager interfaceManager,
81 @Named("elanService") IElanService ielanService, final DhcpPortCache dhcpPortCache,
82 final JobCoordinator jobCoordinator, final ItmRpcService itmRpcService,
83 DhcpServiceCounters dhcpServiceCounters) {
84 this.mdsalUtil = mdsalApiManager;
86 this.broker = dataBroker;
87 this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
88 this.interfaceManager = interfaceManager;
89 this.elanService = ielanService;
90 this.dhcpPortCache = dhcpPortCache;
91 this.jobCoordinator = jobCoordinator;
92 this.itmRpcService = itmRpcService;
93 this.dhcpServiceCounters = dhcpServiceCounters;
94 configureLeaseDuration(DhcpMConstants.DEFAULT_LEASE_TIME);
99 LOG.trace("Netvirt DHCP Manager Init .... {}",config.isControllerDhcpEnabled());
100 if (config.isControllerDhcpEnabled()) {
101 dhcpInterfaceEventListener = new DhcpInterfaceEventListener(this, broker, dhcpExternalTunnelManager,
102 interfaceManager, elanService, dhcpPortCache, jobCoordinator, itmRpcService);
103 dhcpInterfaceConfigListener = new DhcpInterfaceConfigListener(broker, dhcpExternalTunnelManager, this,
105 LOG.info("DHCP Service initialized");
110 public void close() {
111 if (dhcpInterfaceEventListener != null) {
112 dhcpInterfaceEventListener.close();
114 if (dhcpInterfaceConfigListener != null) {
115 dhcpInterfaceConfigListener.close();
117 LOG.info("DHCP Service closed");
120 public int setLeaseDuration(int leaseDuration) {
121 configureLeaseDuration(leaseDuration);
122 return getDhcpLeaseTime();
125 public String setDefaultDomain(String defaultDomain) {
126 this.dhcpOptDefDomainName = defaultDomain;
127 return getDhcpDefDomain();
130 public int getDhcpLeaseTime() {
131 return this.dhcpOptLeaseTime;
134 public int getDhcpRenewalTime() {
135 return this.dhcpOptLeaseTime;
138 public int getDhcpRebindingTime() {
139 return this.dhcpOptLeaseTime;
142 public String getDhcpDefDomain() {
143 return this.dhcpOptDefDomainName;
146 private void configureLeaseDuration(int leaseTime) {
147 this.dhcpOptLeaseTime = leaseTime;
151 public Subnet getNeutronSubnet(Port port) {
153 // DHCP Service is only interested in IPv4 IPs/Subnets
154 return getNeutronSubnet(new ArrayList<FixedIps>(port.getFixedIps().values()));
160 public Subnet getNeutronSubnet(List<FixedIps> fixedIps) {
161 for (FixedIps fixedIp: fixedIps) {
162 if (fixedIp.getIpAddress().getIpv4Address() != null) {
163 return getNeutronSubnet(fixedIp.getSubnetId());
170 private Subnet getNeutronSubnet(Uuid subnetId) {
171 Subnet subnet = null;
172 InstanceIdentifier<Subnet> inst = InstanceIdentifier.create(Neutron.class).child(Subnets.class).child(Subnet
173 .class, new SubnetKey(subnetId));
176 sn = SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.CONFIGURATION,
178 } catch (ExecutionException | InterruptedException e) {
179 LOG.error("Exception while reading subnet DS for the subnetId {}", subnetId.getValue(), e);
182 if (sn.isPresent()) {
185 LOG.trace("Subnet {} = {}", subnetId, subnet);
190 public Port getNeutronPort(String name) {
192 InstanceIdentifier<Port> inst = InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class,
193 new PortKey(new Uuid(name)));
196 port = SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.CONFIGURATION,
198 } catch (ExecutionException | InterruptedException e) {
199 LOG.error("Exception while reading port DS for the port {}", name, e);
202 if (port.isPresent()) {
205 LOG.trace("Port {} = {}", name, prt);
209 public void installDhcpEntries(@Nullable Uint64 dpnId, @Nullable String vmMacAddress,
210 TypedReadWriteTransaction<Configuration> tx) throws ExecutionException, InterruptedException {
211 DhcpServiceUtils.setupDhcpFlowEntry(dpnId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.ADD_FLOW,
212 mdsalUtil, dhcpServiceCounters, tx);
215 public void unInstallDhcpEntries(@Nullable Uint64 dpId, @Nullable String vmMacAddress,
216 TypedReadWriteTransaction<Configuration> tx) throws ExecutionException, InterruptedException {
217 DhcpServiceUtils.setupDhcpFlowEntry(dpId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.DEL_FLOW,
218 mdsalUtil, dhcpServiceCounters, tx);
221 public void setupDefaultDhcpFlows(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
222 setupTableMissForDhcpTable(tx, dpId);
223 if (config.isDhcpDynamicAllocationPoolEnabled()) {
224 setupDhcpAllocationPoolFlow(tx, dpId);
228 private void setupTableMissForDhcpTable(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
229 List<MatchInfo> matches = new ArrayList<>();
230 List<InstructionInfo> instructions = new ArrayList<>();
231 List<ActionInfo> actionsInfos = new ArrayList<>();
232 actionsInfos.add(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
233 instructions.add(new InstructionApplyActions(actionsInfos));
234 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE, "DHCPTableMissFlow",
235 0, "DHCP Table Miss Flow", 0, 0,
236 DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
237 dhcpServiceCounters.installDhcpTableMissFlow();
238 mdsalUtil.addFlow(tx, flowEntity);
239 setupTableMissForHandlingExternalTunnel(tx, dpId);
242 private void setupDhcpAllocationPoolFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
243 List<MatchInfo> matches = DhcpServiceUtils.getDhcpMatch();
244 List<InstructionInfo> instructions = new ArrayList<>();
245 List<ActionInfo> actionsInfos = new ArrayList<>();
246 actionsInfos.add(new ActionPuntToController());
247 instructions.add(new InstructionApplyActions(actionsInfos));
248 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE,
249 "DhcpAllocationPoolFlow", DhcpMConstants.DEFAULT_DHCP_ALLOCATION_POOL_FLOW_PRIORITY,
250 "Dhcp Allocation Pool Flow", 0, 0, DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
251 LOG.trace("Installing DHCP Allocation Pool Flow DpId {}", dpId);
252 dhcpServiceCounters.installDhcpFlow();
253 mdsalUtil.addFlow(tx, flowEntity);
256 private void setupTableMissForHandlingExternalTunnel(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
257 List<MatchInfo> matches = new ArrayList<>();
258 List<InstructionInfo> instructions = new ArrayList<>();
259 instructions.add(new InstructionGotoTable(NwConstants.EXTERNAL_TUNNEL_TABLE));
261 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL,
262 "DHCPTableMissFlowForExternalTunnel",
263 0, "DHCP Table Miss Flow For External Tunnel", 0, 0,
264 DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
265 dhcpServiceCounters.installDhcpTableMissFlowForExternalTable();
266 mdsalUtil.addFlow(tx, flowEntity);