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 java.math.BigInteger;
12 import com.google.common.base.Optional;
13 import org.apache.commons.lang3.tuple.ImmutablePair;
14 import org.apache.commons.lang3.tuple.Pair;
15 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.netvirt.dhcpservice.api.DHCPMConstants;
21 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
22 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
23 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase;
24 import org.opendaylight.genius.mdsalutil.MDSALUtil;
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.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
35 import org.opendaylight.yangtools.concepts.ListenerRegistration;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.common.base.Optional;
42 public class DhcpUCastMacListener extends AsyncClusteredDataChangeListenerBase<LocalUcastMacs, DhcpUCastMacListener> implements AutoCloseable {
44 private static final Logger logger = LoggerFactory.getLogger(DhcpUCastMacListener.class);
45 private DataBroker broker;
46 private ListenerRegistration<DataChangeListener> listenerRegistration;
47 private DhcpExternalTunnelManager dhcpExternalTunnelManager;
48 private DhcpManager dhcpManager;
50 public DhcpUCastMacListener(DhcpManager dhcpManager,DhcpExternalTunnelManager dhcpExtTunnelMgr, DataBroker dataBroker) {
51 super(LocalUcastMacs.class, DhcpUCastMacListener.class);
52 this.broker = dataBroker;
53 this.dhcpExternalTunnelManager = dhcpExtTunnelMgr;
54 this.dhcpManager = dhcpManager;
58 protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
59 return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
60 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
64 public void close() throws Exception {
65 if (listenerRegistration != null) {
67 listenerRegistration.close();
68 } catch (final Exception e) {
69 logger.error("Error when cleaning up DataChangeListener.", e);
71 listenerRegistration = null;
73 logger.info("DhcpUCastMacListener Closed");
77 protected void remove(InstanceIdentifier<LocalUcastMacs> identifier,
79 // Flow removal for table 18 is handled in Neutron Port delete.
80 //remove the new CR-DHCP
81 NodeId torNodeId = identifier.firstKeyOf(Node.class).getNodeId();
82 LogicalSwitches logicalSwitch = getLogicalSwitches(del);
83 if (null == logicalSwitch) {
84 logger.error("DhcpUCastMacListener remove :Logical Switch ref doesn't have data {}", logicalSwitch);
87 String elanInstanceName = logicalSwitch.getHwvtepNodeName().getValue();
88 L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanInstanceName, torNodeId.getValue());
90 logger.error("Logical Switch Device with name {} is not present in L2GWCONN cache", elanInstanceName);
93 IpAddress tunnelIp = device.getTunnelIp();
94 Pair<IpAddress, String> tunnelIpElanName = new ImmutablePair<IpAddress, String>(tunnelIp, elanInstanceName);
95 dhcpExternalTunnelManager.removeFromAvailableCache(tunnelIpElanName);
99 protected void update(InstanceIdentifier<LocalUcastMacs> identifier,
100 LocalUcastMacs original, LocalUcastMacs update) {
101 // TODO Auto-generated method stub
106 protected void add(InstanceIdentifier<LocalUcastMacs> identifier,
107 LocalUcastMacs add) {
108 NodeId torNodeId = identifier.firstKeyOf(Node.class).getNodeId();
109 InstanceIdentifier<LogicalSwitches> logicalSwitchRef = (InstanceIdentifier<LogicalSwitches>) add.getLogicalSwitchRef().getValue();
110 Optional<LogicalSwitches> logicalSwitchOptional = MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, logicalSwitchRef);
111 if ( !logicalSwitchOptional.isPresent() ) {
112 logger.error("Logical Switch ref doesn't have data {}", logicalSwitchRef);
115 LogicalSwitches logicalSwitch = logicalSwitchOptional.get();
116 String elanInstanceName = logicalSwitch.getHwvtepNodeName().getValue();
117 String macAddress = add.getMacEntryKey().getValue();
118 BigInteger vni = new BigInteger(logicalSwitch.getTunnelKey());
119 Port port = dhcpExternalTunnelManager.readVniMacToPortCache(vni, macAddress);
121 logger.trace("No neutron port created for macAddress {}, tunnelKey {}", macAddress, vni);
124 L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanInstanceName, torNodeId.getValue());
125 if (device == null) {
126 logger.error("Logical Switch Device with name {} is not present in L2GWCONN cache", elanInstanceName);
129 IpAddress tunnelIp = device.getTunnelIp();
130 Subnet subnet = dhcpManager.getNeutronSubnet(port);
131 if (null != subnet && !subnet.isEnableDhcp()) {
132 dhcpExternalTunnelManager.updateExistingVMTunnelIPCache(tunnelIp, elanInstanceName, macAddress);
133 logger.warn("DhcpUCastMacListener add: flag for the subnetId "+subnet.getUuid()+ " is False so Table 18 " +
134 "entries are not added" );
137 BigInteger designatedDpnId = dhcpExternalTunnelManager.readDesignatedSwitchesForExternalTunnel(tunnelIp, elanInstanceName);
138 if (designatedDpnId == null || designatedDpnId.equals(DHCPMConstants.INVALID_DPID)) {
139 logger.trace("Unable to install flows for macAddress {}. TunnelIp {}, elanInstanceName {}, designatedDpn {} ", macAddress, tunnelIp, elanInstanceName, designatedDpnId);
140 dhcpExternalTunnelManager.updateLocalCache(tunnelIp, elanInstanceName, macAddress);
143 dhcpExternalTunnelManager.installDhcpFlowsForVms(tunnelIp, elanInstanceName, DhcpServiceUtils.getListOfDpns(broker), designatedDpnId, macAddress);
147 protected ClusteredDataChangeListener getDataChangeListener() {
148 return DhcpUCastMacListener.this;
152 protected DataChangeScope getDataChangeScope() {
153 return DataChangeScope.SUBTREE;
156 private LogicalSwitches getLogicalSwitches(LocalUcastMacs ucastMacs) {
157 LogicalSwitches logicalSwitch = null;
158 InstanceIdentifier<LogicalSwitches> logicalSwitchRef = (InstanceIdentifier<LogicalSwitches>)
159 ucastMacs.getLogicalSwitchRef().getValue();
160 Optional<LogicalSwitches> logicalSwitchOptional = MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL,
162 if (logicalSwitchOptional.isPresent()) {
163 logicalSwitch = logicalSwitchOptional.get();
165 return logicalSwitch;