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.vpnservice.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.elanmanager.utils.ElanL2GwCacheUtils;
18 import org.opendaylight.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
19 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
20 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.L2GatewayDevice;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.common.base.Optional;
36 public class DhcpUCastMacListener extends AsyncClusteredDataChangeListenerBase<LocalUcastMacs, DhcpUCastMacListener> implements AutoCloseable {
38 private static final Logger logger = LoggerFactory.getLogger(DhcpUCastMacListener.class);
39 private DataBroker broker;
40 private ListenerRegistration<DataChangeListener> listenerRegistration;
41 private DhcpExternalTunnelManager dhcpExternalTunnelManager;
43 public DhcpUCastMacListener(DhcpExternalTunnelManager dhcpManager, DataBroker dataBroker) {
44 super(LocalUcastMacs.class, DhcpUCastMacListener.class);
45 this.broker = dataBroker;
46 this.dhcpExternalTunnelManager = dhcpManager;
50 protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
51 return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
52 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
56 public void close() throws Exception {
57 if (listenerRegistration != null) {
59 listenerRegistration.close();
60 } catch (final Exception e) {
61 logger.error("Error when cleaning up DataChangeListener.", e);
63 listenerRegistration = null;
65 logger.info("DhcpUCastMacListener Closed");
69 protected void remove(InstanceIdentifier<LocalUcastMacs> identifier,
71 // Flow removal for table 18 is handled in Neutron Port delete.
75 protected void update(InstanceIdentifier<LocalUcastMacs> identifier,
76 LocalUcastMacs original, LocalUcastMacs update) {
77 // TODO Auto-generated method stub
82 protected void add(InstanceIdentifier<LocalUcastMacs> identifier,
84 NodeId torNodeId = identifier.firstKeyOf(Node.class).getNodeId();
85 InstanceIdentifier<LogicalSwitches> logicalSwitchRef = (InstanceIdentifier<LogicalSwitches>) add.getLogicalSwitchRef().getValue();
86 Optional<LogicalSwitches> logicalSwitchOptional = MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, logicalSwitchRef);
87 if ( !logicalSwitchOptional.isPresent() ) {
88 logger.error("Logical Switch ref doesn't have data {}", logicalSwitchRef);
91 LogicalSwitches logicalSwitch = logicalSwitchOptional.get();
92 String elanInstanceName = logicalSwitch.getHwvtepNodeName().getValue();
93 L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanInstanceName, torNodeId.getValue());
95 logger.error("Logical Switch Device with name {} is not present in L2GWCONN cache", elanInstanceName);
98 IpAddress tunnelIp = device.getTunnelIp();
99 BigInteger designatedDpnId = dhcpExternalTunnelManager.readDesignatedSwitchesForExternalTunnel(tunnelIp, elanInstanceName);
100 dhcpExternalTunnelManager.installDhcpFlowsForVms(tunnelIp, elanInstanceName, DhcpServiceUtils.getListOfDpns(broker), designatedDpnId, add.getMacEntryKey().getValue());
104 protected ClusteredDataChangeListener getDataChangeListener() {
105 return DhcpUCastMacListener.this;
109 protected DataChangeScope getDataChangeScope() {
110 return DataChangeScope.SUBTREE;