2 * Copyright (c) 2016 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 com.google.common.base.Optional;
11 import javax.annotation.PostConstruct;
12 import javax.annotation.PreDestroy;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.apache.commons.lang3.tuple.ImmutablePair;
16 import org.apache.commons.lang3.tuple.Pair;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
21 import org.opendaylight.genius.mdsalutil.MDSALUtil;
22 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
23 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
24 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.config.rev150710.DhcpserviceConfig;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.common.Uint64;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 public class DhcpUCastMacListener
43 extends AsyncClusteredDataTreeChangeListenerBase<LocalUcastMacs, DhcpUCastMacListener> {
45 private static final Logger LOG = LoggerFactory.getLogger(DhcpUCastMacListener.class);
46 private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
47 private final DhcpManager dhcpManager;
48 private final DataBroker broker;
49 private final DhcpserviceConfig config;
52 public DhcpUCastMacListener(final DhcpManager dhcpManager, final DhcpExternalTunnelManager dhcpExtTunnelMgr,
53 final DataBroker dataBroker, final DhcpserviceConfig config) {
54 super(LocalUcastMacs.class, DhcpUCastMacListener.class);
55 this.broker = dataBroker;
56 this.dhcpExternalTunnelManager = dhcpExtTunnelMgr;
57 this.dhcpManager = dhcpManager;
63 if (config.isControllerDhcpEnabled()) {
64 registerListener(LogicalDatastoreType.OPERATIONAL, broker);
69 protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
70 return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
71 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
78 LOG.info("DhcpUCastMacListener Closed");
82 protected void remove(InstanceIdentifier<LocalUcastMacs> identifier,
84 // Flow removal for table 18 is handled in Neutron Port delete.
85 //remove the new CR-DHCP
86 NodeId torNodeId = identifier.firstKeyOf(Node.class).getNodeId();
87 LogicalSwitches logicalSwitch = getLogicalSwitches(del);
88 if (null == logicalSwitch) {
89 LOG.error("DhcpUCastMacListener remove :Logical Switch ref doesn't have data");
92 String elanInstanceName = logicalSwitch.getHwvtepNodeName().getValue();
93 L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanInstanceName, torNodeId.getValue());
95 LOG.error("Logical Switch Device with name {} is not present in L2GWCONN cache", elanInstanceName);
98 IpAddress tunnelIp = device.getTunnelIp();
99 Pair<IpAddress, String> tunnelIpElanName = new ImmutablePair<>(tunnelIp, elanInstanceName);
100 dhcpExternalTunnelManager.removeFromAvailableCache(tunnelIpElanName);
104 protected void update(InstanceIdentifier<LocalUcastMacs> identifier,
105 LocalUcastMacs original, LocalUcastMacs update) {
106 // TODO Auto-generated method stub
111 protected void add(InstanceIdentifier<LocalUcastMacs> identifier,
112 LocalUcastMacs add) {
113 NodeId torNodeId = identifier.firstKeyOf(Node.class).getNodeId();
114 InstanceIdentifier<LogicalSwitches> logicalSwitchRef =
115 (InstanceIdentifier<LogicalSwitches>) add.getLogicalSwitchRef().getValue();
116 Optional<LogicalSwitches> logicalSwitchOptional =
117 MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, logicalSwitchRef);
118 if (!logicalSwitchOptional.isPresent()) {
119 LOG.error("Logical Switch ref doesn't have data {}", logicalSwitchRef);
122 LogicalSwitches logicalSwitch = logicalSwitchOptional.get();
123 String elanInstanceName = logicalSwitch.getHwvtepNodeName().getValue();
124 String macAddress = add.getMacEntryKey().getValue();
125 Uint64 vni = Uint64.valueOf(logicalSwitch.getTunnelKey());
126 Port port = dhcpExternalTunnelManager.readVniMacToPortCache(vni, macAddress);
128 LOG.trace("No neutron port created for macAddress {}, tunnelKey {}", macAddress, vni);
131 L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanInstanceName, torNodeId.getValue());
132 if (device == null) {
133 LOG.error("Logical Switch Device with name {} is not present in L2GWCONN cache", elanInstanceName);
136 IpAddress tunnelIp = device.getTunnelIp();
137 Subnet subnet = dhcpManager.getNeutronSubnet(port);
138 if (null != subnet && !subnet.isEnableDhcp()) {
139 dhcpExternalTunnelManager.updateExistingVMTunnelIPCache(tunnelIp, elanInstanceName, macAddress);
140 LOG.warn("DhcpUCastMacListener add: flag for the subnetId {} is False so Table 18 entries are not added",
144 Uint64 designatedDpnId =
145 dhcpExternalTunnelManager.readDesignatedSwitchesForExternalTunnel(tunnelIp, elanInstanceName);
146 if (designatedDpnId == null || designatedDpnId.equals(DhcpMConstants.INVALID_DPID)) {
147 LOG.trace("Unable to install flows for macAddress {}. TunnelIp {}, elanInstanceName {}, designatedDpn {} ",
148 macAddress, tunnelIp, elanInstanceName, designatedDpnId);
149 dhcpExternalTunnelManager.updateLocalCache(tunnelIp, elanInstanceName, macAddress);
152 dhcpExternalTunnelManager.installDhcpFlowsForVms(tunnelIp, elanInstanceName,
153 DhcpServiceUtils.getListOfDpns(broker), designatedDpnId, macAddress);
157 private LogicalSwitches getLogicalSwitches(LocalUcastMacs ucastMacs) {
158 InstanceIdentifier<LogicalSwitches> logicalSwitchRef =
159 (InstanceIdentifier<LogicalSwitches>)ucastMacs.getLogicalSwitchRef().getValue();
160 return MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, logicalSwitchRef).orNull();
164 protected DhcpUCastMacListener getDataTreeChangeListener() {
165 return DhcpUCastMacListener.this;