DHCP Handling for TOR VM
[vpnservice.git] / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / vpnservice / dhcpservice / DhcpUCastMacListener.java
1 /*
2  * Copyright (c) 2016 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.vpnservice.dhcpservice;
9
10 import java.math.BigInteger;
11
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;
33
34 import com.google.common.base.Optional;
35
36 public class DhcpUCastMacListener extends AsyncClusteredDataChangeListenerBase<LocalUcastMacs, DhcpUCastMacListener> implements AutoCloseable {
37
38     private static final Logger logger = LoggerFactory.getLogger(DhcpUCastMacListener.class);
39     private DataBroker broker;
40     private ListenerRegistration<DataChangeListener> listenerRegistration;
41     private DhcpExternalTunnelManager dhcpExternalTunnelManager;
42
43     public DhcpUCastMacListener(DhcpExternalTunnelManager dhcpManager, DataBroker dataBroker) {
44         super(LocalUcastMacs.class, DhcpUCastMacListener.class);
45         this.broker = dataBroker;
46         this.dhcpExternalTunnelManager = dhcpManager;
47     }
48
49     @Override
50     protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
51         return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
52                 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
53     }
54
55     @Override
56     public void close() throws Exception {
57         if (listenerRegistration != null) {
58             try {
59                 listenerRegistration.close();
60             } catch (final Exception e) {
61                 logger.error("Error when cleaning up DataChangeListener.", e);
62             }
63             listenerRegistration = null;
64         }
65         logger.info("DhcpUCastMacListener Closed");
66     }
67
68     @Override
69     protected void remove(InstanceIdentifier<LocalUcastMacs> identifier,
70             LocalUcastMacs del) {
71         // Flow removal for table 18 is handled in Neutron Port delete.
72     }
73
74     @Override
75     protected void update(InstanceIdentifier<LocalUcastMacs> identifier,
76             LocalUcastMacs original, LocalUcastMacs update) {
77         // TODO Auto-generated method stub
78
79     }
80
81     @Override
82     protected void add(InstanceIdentifier<LocalUcastMacs> identifier,
83             LocalUcastMacs add) {
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);
89             return;
90         }
91         LogicalSwitches logicalSwitch = logicalSwitchOptional.get();
92         String elanInstanceName = logicalSwitch.getHwvtepNodeName().getValue();
93         L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanInstanceName, torNodeId.getValue());
94         if (device == null) {
95             logger.error("Logical Switch Device with name {} is not present in L2GWCONN cache", elanInstanceName);
96             return;
97         }
98         IpAddress tunnelIp = device.getTunnelIp();
99         BigInteger designatedDpnId = dhcpExternalTunnelManager.readDesignatedSwitchesForExternalTunnel(tunnelIp, elanInstanceName);
100         dhcpExternalTunnelManager.installDhcpFlowsForVms(tunnelIp, elanInstanceName, DhcpServiceUtils.getListOfDpns(broker), designatedDpnId, add.getMacEntryKey().getValue());
101     }
102
103     @Override
104     protected ClusteredDataChangeListener getDataChangeListener() {
105         return DhcpUCastMacListener.this;
106     }
107
108     @Override
109     protected DataChangeScope getDataChangeScope() {
110         return DataChangeScope.SUBTREE;
111     }
112 }