Bump versions by 0.1.0 for next dev cycle
[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.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.google.common.base.Optional;
36
37 public class DhcpUCastMacListener extends AsyncClusteredDataChangeListenerBase<LocalUcastMacs, DhcpUCastMacListener> implements AutoCloseable {
38
39     private static final Logger logger = LoggerFactory.getLogger(DhcpUCastMacListener.class);
40     private DataBroker broker;
41     private ListenerRegistration<DataChangeListener> listenerRegistration;
42     private DhcpExternalTunnelManager dhcpExternalTunnelManager;
43
44     public DhcpUCastMacListener(DhcpExternalTunnelManager dhcpManager, DataBroker dataBroker) {
45         super(LocalUcastMacs.class, DhcpUCastMacListener.class);
46         this.broker = dataBroker;
47         this.dhcpExternalTunnelManager = dhcpManager;
48     }
49
50     @Override
51     protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
52         return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
53                 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
54     }
55
56     @Override
57     public void close() throws Exception {
58         if (listenerRegistration != null) {
59             try {
60                 listenerRegistration.close();
61             } catch (final Exception e) {
62                 logger.error("Error when cleaning up DataChangeListener.", e);
63             }
64             listenerRegistration = null;
65         }
66         logger.info("DhcpUCastMacListener Closed");
67     }
68
69     @Override
70     protected void remove(InstanceIdentifier<LocalUcastMacs> identifier,
71             LocalUcastMacs del) {
72         // Flow removal for table 18 is handled in Neutron Port delete.
73     }
74
75     @Override
76     protected void update(InstanceIdentifier<LocalUcastMacs> identifier,
77             LocalUcastMacs original, LocalUcastMacs update) {
78         // TODO Auto-generated method stub
79
80     }
81
82     @Override
83     protected void add(InstanceIdentifier<LocalUcastMacs> identifier,
84             LocalUcastMacs add) {
85         NodeId torNodeId = identifier.firstKeyOf(Node.class).getNodeId();
86         InstanceIdentifier<LogicalSwitches> logicalSwitchRef = (InstanceIdentifier<LogicalSwitches>) add.getLogicalSwitchRef().getValue();
87         Optional<LogicalSwitches> logicalSwitchOptional = MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, logicalSwitchRef);
88         if ( !logicalSwitchOptional.isPresent() ) {
89             logger.error("Logical Switch ref doesn't have data {}", logicalSwitchRef);
90             return;
91         }
92         LogicalSwitches logicalSwitch = logicalSwitchOptional.get();
93         String elanInstanceName = logicalSwitch.getHwvtepNodeName().getValue();
94         String macAddress = add.getMacEntryKey().getValue();
95         BigInteger vni = new BigInteger(logicalSwitch.getTunnelKey());
96         Port port = dhcpExternalTunnelManager.readVniMacToPortCache(vni, macAddress);
97         if (port == null) {
98             logger.trace("No neutron port created for macAddress {}, tunnelKey {}", macAddress, vni);
99             return;
100         }
101         L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanInstanceName, torNodeId.getValue());
102         if (device == null) {
103             logger.error("Logical Switch Device with name {} is not present in L2GWCONN cache", elanInstanceName);
104             return;
105         }
106         IpAddress tunnelIp = device.getTunnelIp();
107         BigInteger designatedDpnId = dhcpExternalTunnelManager.readDesignatedSwitchesForExternalTunnel(tunnelIp, elanInstanceName);
108         dhcpExternalTunnelManager.installDhcpFlowsForVms(tunnelIp, elanInstanceName, DhcpServiceUtils.getListOfDpns(broker), designatedDpnId, macAddress);
109     }
110
111     @Override
112     protected ClusteredDataChangeListener getDataChangeListener() {
113         return DhcpUCastMacListener.this;
114     }
115
116     @Override
117     protected DataChangeScope getDataChangeScope() {
118         return DataChangeScope.SUBTREE;
119     }
120 }