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 org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.netvirt.dhcpservice.api.DHCPMConstants;
18 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
19 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
20 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase;
21 import org.opendaylight.genius.mdsalutil.MDSALUtil;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.google.common.base.Optional;
38 public class DhcpUCastMacListener extends AsyncClusteredDataChangeListenerBase<LocalUcastMacs, DhcpUCastMacListener> implements AutoCloseable {
40 private static final Logger logger = LoggerFactory.getLogger(DhcpUCastMacListener.class);
41 private DataBroker broker;
42 private ListenerRegistration<DataChangeListener> listenerRegistration;
43 private DhcpExternalTunnelManager dhcpExternalTunnelManager;
45 public DhcpUCastMacListener(DhcpExternalTunnelManager dhcpManager, DataBroker dataBroker) {
46 super(LocalUcastMacs.class, DhcpUCastMacListener.class);
47 this.broker = dataBroker;
48 this.dhcpExternalTunnelManager = dhcpManager;
52 protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
53 return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
54 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
58 public void close() throws Exception {
59 if (listenerRegistration != null) {
61 listenerRegistration.close();
62 } catch (final Exception e) {
63 logger.error("Error when cleaning up DataChangeListener.", e);
65 listenerRegistration = null;
67 logger.info("DhcpUCastMacListener Closed");
71 protected void remove(InstanceIdentifier<LocalUcastMacs> identifier,
73 // Flow removal for table 18 is handled in Neutron Port delete.
77 protected void update(InstanceIdentifier<LocalUcastMacs> identifier,
78 LocalUcastMacs original, LocalUcastMacs update) {
79 // TODO Auto-generated method stub
84 protected void add(InstanceIdentifier<LocalUcastMacs> identifier,
86 NodeId torNodeId = identifier.firstKeyOf(Node.class).getNodeId();
87 InstanceIdentifier<LogicalSwitches> logicalSwitchRef = (InstanceIdentifier<LogicalSwitches>) add.getLogicalSwitchRef().getValue();
88 Optional<LogicalSwitches> logicalSwitchOptional = MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, logicalSwitchRef);
89 if ( !logicalSwitchOptional.isPresent() ) {
90 logger.error("Logical Switch ref doesn't have data {}", logicalSwitchRef);
93 LogicalSwitches logicalSwitch = logicalSwitchOptional.get();
94 String elanInstanceName = logicalSwitch.getHwvtepNodeName().getValue();
95 String macAddress = add.getMacEntryKey().getValue();
96 BigInteger vni = new BigInteger(logicalSwitch.getTunnelKey());
97 Port port = dhcpExternalTunnelManager.readVniMacToPortCache(vni, macAddress);
99 logger.trace("No neutron port created for macAddress {}, tunnelKey {}", macAddress, vni);
102 L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanInstanceName, torNodeId.getValue());
103 if (device == null) {
104 logger.error("Logical Switch Device with name {} is not present in L2GWCONN cache", elanInstanceName);
107 IpAddress tunnelIp = device.getTunnelIp();
108 BigInteger designatedDpnId = dhcpExternalTunnelManager.readDesignatedSwitchesForExternalTunnel(tunnelIp, elanInstanceName);
109 if (designatedDpnId == null || designatedDpnId.equals(DHCPMConstants.INVALID_DPID)) {
110 logger.trace("Unable to install flows for macAddress {}. TunnelIp {}, elanInstanceName {}, designatedDpn {} ", macAddress, tunnelIp, elanInstanceName, designatedDpnId);
113 dhcpExternalTunnelManager.installDhcpFlowsForVms(tunnelIp, elanInstanceName, DhcpServiceUtils.getListOfDpns(broker), designatedDpnId, macAddress);
117 protected ClusteredDataChangeListener getDataChangeListener() {
118 return DhcpUCastMacListener.this;
122 protected DataChangeScope getDataChangeScope() {
123 return DataChangeScope.SUBTREE;