Merge "Fix for BUG 3623"
[vpnservice.git] / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / vpnservice / InterfaceChangeListener.java
1 package org.opendaylight.vpnservice;
2
3 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
4 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
5 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
6 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
7 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
8 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
9 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
10 import org.opendaylight.yangtools.concepts.ListenerRegistration;
11 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
12 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 public class InterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
17     private static final Logger LOG = LoggerFactory.getLogger(InterfaceChangeListener.class);
18
19     private ListenerRegistration<DataChangeListener> listenerRegistration;
20     private final DataBroker broker;
21     private VpnInterfaceManager vpnInterfaceManager;
22
23
24     public InterfaceChangeListener(final DataBroker db, VpnInterfaceManager vpnInterfaceManager) {
25         super(Interface.class);
26         broker = db;
27         this.vpnInterfaceManager = vpnInterfaceManager;
28         registerListener(db);
29     }
30
31     @Override
32     public void close() throws Exception {
33         if (listenerRegistration != null) {
34             try {
35                 listenerRegistration.close();
36             } catch (final Exception e) {
37                 LOG.error("Error when cleaning up DataChangeListener.", e);
38             }
39             listenerRegistration = null;
40         }
41         LOG.info("Interface listener Closed");
42     }
43
44
45     private void registerListener(final DataBroker db) {
46         try {
47             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
48                     getWildCardPath(), InterfaceChangeListener.this, DataChangeScope.SUBTREE);
49         } catch (final Exception e) {
50             LOG.error("Interface DataChange listener registration failed", e);
51             throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
52         }
53     }
54
55     @Override
56     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
57         LOG.trace("Adding Interface : key: " + identifier + ", value=" + intrf );
58
59     }
60
61
62     private InstanceIdentifier<Interface> getWildCardPath() {
63         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
64     }
65
66     @Override
67     protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
68         LOG.trace("Remove interface event - key: {}, value: {}", identifier, intrf );
69         VpnInterface vpnInterface = vpnInterfaceManager.getVpnInterface(intrf.getName());
70         InstanceIdentifier<VpnInterface> id = VpnUtil.getVpnInterfaceIdentifier(intrf.getName());
71         LOG.debug("Removing VPN Interface associated with Interface {}", intrf.getName());
72         vpnInterfaceManager.remove(id, vpnInterface);
73     }
74
75     @Override
76     protected void update(InstanceIdentifier<Interface> identifier,
77             Interface original, Interface update) {
78         // TODO Auto-generated method stub
79
80     }
81
82 }