ELAN: skip remote unicast MACs
[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.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;
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     private final JobCoordinator jobCoordinator;
55     private DhcpPortCache dhcpPortCache;
56
57     private volatile int dhcpOptLeaseTime = 0;
58     private volatile String dhcpOptDefDomainName;
59     private DhcpInterfaceEventListener dhcpInterfaceEventListener;
60     private DhcpInterfaceConfigListener dhcpInterfaceConfigListener;
61
62     @Inject
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;
71         this.config = config;
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);
79     }
80
81     @PostConstruct
82     public void init() {
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,
88                     jobCoordinator);
89             LOG.info("DHCP Service initialized");
90         }
91     }
92
93     @PreDestroy
94     public void close() throws Exception {
95         if (dhcpInterfaceEventListener != null) {
96             dhcpInterfaceEventListener.close();
97         }
98         if (dhcpInterfaceConfigListener != null) {
99             dhcpInterfaceConfigListener.close();
100         }
101         LOG.info("DHCP Service closed");
102     }
103
104     public int setLeaseDuration(int leaseDuration) {
105         configureLeaseDuration(leaseDuration);
106         return getDhcpLeaseTime();
107     }
108
109     public String setDefaultDomain(String defaultDomain) {
110         this.dhcpOptDefDomainName = defaultDomain;
111         return getDhcpDefDomain();
112     }
113
114     public int getDhcpLeaseTime() {
115         return this.dhcpOptLeaseTime;
116     }
117
118     public int getDhcpRenewalTime() {
119         return this.dhcpOptLeaseTime;
120     }
121
122     public int getDhcpRebindingTime() {
123         return this.dhcpOptLeaseTime;
124     }
125
126     public String getDhcpDefDomain() {
127         return this.dhcpOptDefDomainName;
128     }
129
130     private void configureLeaseDuration(int leaseTime) {
131         this.dhcpOptLeaseTime = leaseTime;
132     }
133
134     public Subnet getNeutronSubnet(Port port) {
135         if (port != null) {
136             // DHCP Service is only interested in IPv4 IPs/Subnets
137             return getNeutronSubnet(port.getFixedIps());
138         }
139         return null;
140     }
141
142     public Subnet getNeutronSubnet(List<FixedIps> fixedIps) {
143         for (FixedIps fixedIp: fixedIps) {
144             if (fixedIp.getIpAddress().getIpv4Address() != null) {
145                 return neutronVpnService.getNeutronSubnet(fixedIp.getSubnetId());
146             }
147         }
148         return null;
149     }
150
151     public Port getNeutronPort(String name) {
152         try {
153             return neutronVpnService.getNeutronPort(name);
154         } catch (IllegalArgumentException e) {
155             return null;
156         }
157     }
158
159     public void installDhcpEntries(BigInteger dpnId, String vmMacAddress, WriteTransaction tx) {
160         DhcpServiceUtils.setupDhcpFlowEntry(dpnId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.ADD_FLOW,
161                 mdsalUtil, tx);
162     }
163
164     public void unInstallDhcpEntries(BigInteger dpId, String vmMacAddress, WriteTransaction tx) {
165         DhcpServiceUtils.setupDhcpFlowEntry(dpId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.DEL_FLOW,
166                 mdsalUtil, tx);
167     }
168
169     public void setupDefaultDhcpFlows(BigInteger dpId) {
170         setupTableMissForDhcpTable(dpId);
171         if (config.isDhcpDynamicAllocationPoolEnabled()) {
172             setupDhcpAllocationPoolFlow(dpId);
173         }
174     }
175
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);
188     }
189
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);
202     }
203
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));
208
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);
215     }
216 }