DHCP Handling for TOR VM
[vpnservice.git] / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / vpnservice / dhcpservice / DhcpInterfaceConfigListener.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
9 package org.opendaylight.vpnservice.dhcpservice;
10
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class DhcpInterfaceConfigListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
27
28     private static final Logger logger = LoggerFactory.getLogger(DhcpInterfaceConfigListener.class);
29     private ListenerRegistration<DataChangeListener> listenerRegistration;
30     private DataBroker dataBroker;
31     private DhcpExternalTunnelManager dhcpExternalTunnelManager;
32
33     public DhcpInterfaceConfigListener(DataBroker dataBroker, DhcpExternalTunnelManager dhcpExternalTunnelManager) {
34         super(Interface.class);
35         this.dataBroker = dataBroker;
36         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
37         registerListener();
38     }
39
40     private void registerListener() {
41         try {
42             listenerRegistration = dataBroker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
43                     getWildCardPath(), DhcpInterfaceConfigListener.this, DataChangeScope.SUBTREE);
44         } catch (final Exception e) {
45             logger.error("DhcpInterfaceEventListener DataChange listener registration fail!", e);
46             throw new IllegalStateException("DhcpInterfaceEventListener registration Listener failed.", e);
47         }
48     }
49
50     private InstanceIdentifier<Interface> getWildCardPath() {
51         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
52     }
53
54     @Override
55     public void close() throws Exception {
56         if (listenerRegistration != null) {
57             try {
58                 listenerRegistration.close();
59             } catch (final Exception e) {
60                 logger.error("Error when cleaning up DataChangeListener.", e);
61             }
62             listenerRegistration = null;
63         }
64         logger.info("DhcpInterfaceConfigListener Closed");
65     }
66     @Override
67     protected void remove(InstanceIdentifier<Interface> identifier, Interface del) {
68         IfTunnel tunnelInterface = del.getAugmentation(IfTunnel.class);
69         if (tunnelInterface != null && !tunnelInterface.isInternal()) {
70             IpAddress tunnelIp = tunnelInterface.getTunnelDestination();
71             ParentRefs interfce = del.getAugmentation(ParentRefs.class);
72             if (interfce != null) {
73                 dhcpExternalTunnelManager.handleTunnelStateDown(tunnelIp, interfce.getDatapathNodeIdentifier());
74             }
75         }
76     }
77
78     @Override
79     protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
80         // Handled in update () DhcpInterfaceEventListener
81     }
82
83     @Override
84     protected void add(InstanceIdentifier<Interface> identifier, Interface add) {
85         // Handled in add() DhcpInterfaceEventListener
86     }
87 }