itm-direct tunnel related changes for scaling
[netvirt.git] / dhcpservice / impl / src / main / java / org / opendaylight / netvirt / 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.netvirt.dhcpservice;
9
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;
44
45 @Singleton
46 public class DhcpManager {
47
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;
59
60     private volatile int dhcpOptLeaseTime = 0;
61     private volatile String dhcpOptDefDomainName;
62     private DhcpInterfaceEventListener dhcpInterfaceEventListener;
63     private DhcpInterfaceConfigListener dhcpInterfaceConfigListener;
64
65     @Inject
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;
74         this.config = config;
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);
83     }
84
85     @PostConstruct
86     public void init() {
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,
92                     jobCoordinator);
93             LOG.info("DHCP Service initialized");
94         }
95     }
96
97     @PreDestroy
98     public void close() {
99         if (dhcpInterfaceEventListener != null) {
100             dhcpInterfaceEventListener.close();
101         }
102         if (dhcpInterfaceConfigListener != null) {
103             dhcpInterfaceConfigListener.close();
104         }
105         LOG.info("DHCP Service closed");
106     }
107
108     public int setLeaseDuration(int leaseDuration) {
109         configureLeaseDuration(leaseDuration);
110         return getDhcpLeaseTime();
111     }
112
113     public String setDefaultDomain(String defaultDomain) {
114         this.dhcpOptDefDomainName = defaultDomain;
115         return getDhcpDefDomain();
116     }
117
118     public int getDhcpLeaseTime() {
119         return this.dhcpOptLeaseTime;
120     }
121
122     public int getDhcpRenewalTime() {
123         return this.dhcpOptLeaseTime;
124     }
125
126     public int getDhcpRebindingTime() {
127         return this.dhcpOptLeaseTime;
128     }
129
130     public String getDhcpDefDomain() {
131         return this.dhcpOptDefDomainName;
132     }
133
134     private void configureLeaseDuration(int leaseTime) {
135         this.dhcpOptLeaseTime = leaseTime;
136     }
137
138     public Subnet getNeutronSubnet(Port port) {
139         if (port != null) {
140             // DHCP Service is only interested in IPv4 IPs/Subnets
141             return getNeutronSubnet(port.getFixedIps());
142         }
143         return null;
144     }
145
146     public Subnet getNeutronSubnet(List<FixedIps> fixedIps) {
147         for (FixedIps fixedIp: fixedIps) {
148             if (fixedIp.getIpAddress().getIpv4Address() != null) {
149                 return neutronVpnService.getNeutronSubnet(fixedIp.getSubnetId());
150             }
151         }
152         return null;
153     }
154
155     public Port getNeutronPort(String name) {
156         try {
157             return neutronVpnService.getNeutronPort(name);
158         } catch (IllegalArgumentException e) {
159             return null;
160         }
161     }
162
163     public void installDhcpEntries(@Nullable BigInteger dpnId, @Nullable String vmMacAddress, WriteTransaction tx) {
164         DhcpServiceUtils.setupDhcpFlowEntry(dpnId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.ADD_FLOW,
165                 mdsalUtil, tx);
166     }
167
168     public void unInstallDhcpEntries(@Nullable BigInteger dpId, @Nullable String vmMacAddress, WriteTransaction tx) {
169         DhcpServiceUtils.setupDhcpFlowEntry(dpId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.DEL_FLOW,
170                 mdsalUtil, tx);
171     }
172
173     public void setupDefaultDhcpFlows(BigInteger dpId) {
174         setupTableMissForDhcpTable(dpId);
175         if (config.isDhcpDynamicAllocationPoolEnabled()) {
176             setupDhcpAllocationPoolFlow(dpId);
177         }
178     }
179
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);
192     }
193
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);
206     }
207
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));
212
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);
219     }
220 }