DHCP Handling for TOR VM
[vpnservice.git] / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / vpnservice / dhcpservice / DhcpDesignatedDpnListener.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 import java.util.List;
12
13 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
18 import org.opendaylight.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.dhcp.rev160428.DesignatedSwitchesForExternalTunnels;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.dhcp.rev160428.designated.switches._for.external.tunnels.DesignatedSwitchForTunnel;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class DhcpDesignatedDpnListener extends AsyncClusteredDataChangeListenerBase<DesignatedSwitchForTunnel, DhcpDesignatedDpnListener> implements AutoCloseable {
28
29     private static final Logger LOG = LoggerFactory.getLogger(DhcpDesignatedDpnListener.class);
30     private ListenerRegistration<DataChangeListener> listenerRegistration;
31     private DhcpExternalTunnelManager dhcpExternalTunnelManager;
32     private DataBroker broker;
33
34     public DhcpDesignatedDpnListener(final DhcpExternalTunnelManager dhcpExternalTunnelManager, final DataBroker broker) {
35         super(DesignatedSwitchForTunnel.class, DhcpDesignatedDpnListener.class);
36         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
37         this.broker = broker;
38     }
39
40     @Override
41     public void close() throws Exception {
42         if (listenerRegistration != null) {
43             try {
44                 listenerRegistration.close();
45             } catch (final Exception e) {
46                 LOG.error("Error when cleaning up DhcpDesignatedDpnListener.", e);
47             }
48             listenerRegistration = null;
49         }
50         LOG.debug("DhcpDesignatedDpnListener Listener Closed");
51     }
52
53     @Override
54     protected void remove(InstanceIdentifier<DesignatedSwitchForTunnel> identifier, DesignatedSwitchForTunnel del) {
55         dhcpExternalTunnelManager.removeFromLocalCache(BigInteger.valueOf(del.getDpId()), del.getTunnelRemoteIpAddress(), del.getElanInstanceName());
56         dhcpExternalTunnelManager.unInstallDhcpFlowsForVms(del.getElanInstanceName(), del.getTunnelRemoteIpAddress(), DhcpServiceUtils.getListOfDpns(broker));
57     }
58
59     @Override
60     protected void update(InstanceIdentifier<DesignatedSwitchForTunnel> identifier, DesignatedSwitchForTunnel original,
61             DesignatedSwitchForTunnel update) {
62         BigInteger designatedDpnId = BigInteger.valueOf(update.getDpId());
63         IpAddress tunnelRemoteIpAddress = update.getTunnelRemoteIpAddress();
64         String elanInstanceName = update.getElanInstanceName();
65         dhcpExternalTunnelManager.removeFromLocalCache(BigInteger.valueOf(original.getDpId()), original.getTunnelRemoteIpAddress(), original.getElanInstanceName());
66         dhcpExternalTunnelManager.updateLocalCache(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
67 /*        List<BigInteger> elanDpns = DhcpServiceUtils.getDpnsForElan(elanInstanceName, broker);
68         if (elanDpns == null || elanDpns.isEmpty()) {
69             dhcpExternalTunnelManager.installRemoteMcastMac(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
70         }*/
71     }
72
73     @Override
74     protected void add(InstanceIdentifier<DesignatedSwitchForTunnel> identifier, DesignatedSwitchForTunnel add) {
75         BigInteger designatedDpnId = BigInteger.valueOf(add.getDpId());
76         IpAddress tunnelRemoteIpAddress = add.getTunnelRemoteIpAddress();
77         String elanInstanceName = add.getElanInstanceName();
78         dhcpExternalTunnelManager.updateLocalCache(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
79 /*        List<BigInteger> elanDpns = DhcpServiceUtils.getDpnsForElan(elanInstanceName, broker);
80         if (elanDpns == null || elanDpns.isEmpty()) {
81             dhcpExternalTunnelManager.installRemoteMcastMac(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
82         }*/
83     }
84
85     @Override
86     protected InstanceIdentifier<DesignatedSwitchForTunnel> getWildCardPath() {
87         return InstanceIdentifier.create(DesignatedSwitchesForExternalTunnels.class).child(DesignatedSwitchForTunnel.class);
88     }
89
90     @Override
91     protected ClusteredDataChangeListener getDataChangeListener() {
92         return DhcpDesignatedDpnListener.this;
93     }
94
95     @Override
96     protected DataChangeScope getDataChangeScope() {
97         return AsyncDataBroker.DataChangeScope.SUBTREE;
98     }
99 }