Neutron Port allocation for DHCP Service
[netvirt.git] / vpnservice / dhcpservice / 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
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.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;
42
43 @Singleton
44 public class DhcpManager {
45
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
55     private int dhcpOptLeaseTime = 0;
56     private String dhcpOptDefDomainName;
57     private DhcpInterfaceEventListener dhcpInterfaceEventListener;
58     private DhcpInterfaceConfigListener dhcpInterfaceConfigListener;
59
60     @Inject
61     public DhcpManager(final IMdsalApiManager mdsalApiManager,
62             final INeutronVpnManager neutronVpnManager,
63             final DhcpserviceConfig config, final DataBroker dataBroker,
64             final DhcpExternalTunnelManager dhcpExternalTunnelManager, final IInterfaceManager interfaceManager,
65             final @Named("elanService") IElanService ielanService) {
66         this.mdsalUtil = mdsalApiManager;
67         this.neutronVpnService = neutronVpnManager;
68         this.config = config;
69         this.broker = dataBroker;
70         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
71         this.interfaceManager = interfaceManager;
72         this.elanService = ielanService;
73         configureLeaseDuration(DhcpMConstants.DEFAULT_LEASE_TIME);
74     }
75
76     @PostConstruct
77     public void init() {
78         LOG.trace("Netvirt DHCP Manager Init .... {}",config.isControllerDhcpEnabled());
79         if (config.isControllerDhcpEnabled()) {
80             dhcpInterfaceEventListener = new DhcpInterfaceEventListener(this, broker, dhcpExternalTunnelManager,
81                     interfaceManager, elanService);
82             dhcpInterfaceConfigListener = new DhcpInterfaceConfigListener(broker, dhcpExternalTunnelManager, this);
83             LOG.info("DHCP Service initialized");
84         }
85     }
86
87     @PreDestroy
88     public void close() throws Exception {
89         if (dhcpInterfaceEventListener != null) {
90             dhcpInterfaceEventListener.close();
91         }
92         if (dhcpInterfaceConfigListener != null) {
93             dhcpInterfaceConfigListener.close();
94         }
95         LOG.info("DHCP Service closed");
96     }
97
98     public int setLeaseDuration(int leaseDuration) {
99         configureLeaseDuration(leaseDuration);
100         return getDhcpLeaseTime();
101     }
102
103     public String setDefaultDomain(String defaultDomain) {
104         this.dhcpOptDefDomainName = defaultDomain;
105         return getDhcpDefDomain();
106     }
107
108     protected int getDhcpLeaseTime() {
109         return this.dhcpOptLeaseTime;
110     }
111
112     protected int getDhcpRenewalTime() {
113         return this.dhcpOptLeaseTime;
114     }
115
116     protected int getDhcpRebindingTime() {
117         return this.dhcpOptLeaseTime;
118     }
119
120     protected String getDhcpDefDomain() {
121         return this.dhcpOptDefDomainName;
122     }
123
124     private void configureLeaseDuration(int leaseTime) {
125         this.dhcpOptLeaseTime = leaseTime;
126     }
127
128     public Subnet getNeutronSubnet(Port port) {
129         if (port != null) {
130             // DHCP Service is only interested in IPv4 IPs/Subnets
131             for (FixedIps fixedIp: port.getFixedIps()) {
132                 if (fixedIp.getIpAddress().getIpv4Address() != null) {
133                     return neutronVpnService.getNeutronSubnet(fixedIp.getSubnetId());
134                 }
135             }
136         }
137         return null;
138     }
139
140     public Port getNeutronPort(String name) {
141         try {
142             return neutronVpnService.getNeutronPort(name);
143         } catch (IllegalArgumentException e) {
144             return null;
145         }
146     }
147
148     public void installDhcpEntries(BigInteger dpnId, String vmMacAddress, WriteTransaction tx) {
149         DhcpServiceUtils.setupDhcpFlowEntry(dpnId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.ADD_FLOW,
150                 mdsalUtil, tx);
151     }
152
153     public void unInstallDhcpEntries(BigInteger dpId, String vmMacAddress, WriteTransaction tx) {
154         DhcpServiceUtils.setupDhcpFlowEntry(dpId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.DEL_FLOW,
155                 mdsalUtil, tx);
156     }
157
158     public void setupDefaultDhcpFlows(BigInteger dpId) {
159         setupTableMissForDhcpTable(dpId);
160         if (config.isDhcpDynamicAllocationPoolEnabled()) {
161             setupDhcpAllocationPoolFlow(dpId);
162         }
163     }
164
165     private void setupTableMissForDhcpTable(BigInteger dpId) {
166         List<MatchInfo> matches = new ArrayList<>();
167         List<InstructionInfo> instructions = new ArrayList<>();
168         List<ActionInfo> actionsInfos = new ArrayList<>();
169         actionsInfos.add(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
170         instructions.add(new InstructionApplyActions(actionsInfos));
171         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE, "DHCPTableMissFlow",
172                 0, "DHCP Table Miss Flow", 0, 0,
173                 DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
174         DhcpServiceCounters.install_dhcp_table_miss_flow.inc();
175         mdsalUtil.installFlow(flowEntity);
176         setupTableMissForHandlingExternalTunnel(dpId);
177     }
178
179     private void setupDhcpAllocationPoolFlow(BigInteger dpId) {
180         List<MatchInfo> matches = DhcpServiceUtils.getDhcpMatch();
181         List<InstructionInfo> instructions = new ArrayList<>();
182         List<ActionInfo> actionsInfos = new ArrayList<>();
183         actionsInfos.add(new ActionPuntToController());
184         instructions.add(new InstructionApplyActions(actionsInfos));
185         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE,
186                 "DhcpAllocationPoolFlow", DhcpMConstants.DEFAULT_DHCP_ALLOCATION_POOL_FLOW_PRIORITY,
187                 "Dhcp Allocation Pool Flow", 0, 0, DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
188         LOG.trace("Installing DHCP Allocation Pool Flow DpId {}", dpId);
189         DhcpServiceCounters.install_dhcp_flow.inc();
190         mdsalUtil.installFlow(flowEntity);
191     }
192
193     private void setupTableMissForHandlingExternalTunnel(BigInteger dpId) {
194         List<MatchInfo> matches = new ArrayList<>();
195         List<InstructionInfo> instructions = new ArrayList<>();
196         instructions.add(new InstructionGotoTable(NwConstants.EXTERNAL_TUNNEL_TABLE));
197
198         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL,
199                 "DHCPTableMissFlowForExternalTunnel",
200                 0, "DHCP Table Miss Flow For External Tunnel", 0, 0,
201                 DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
202         DhcpServiceCounters.install_dhcp_table_miss_flow_for_external_table.inc();
203         mdsalUtil.installFlow(flowEntity);
204     }
205 }