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