1 package org.opendaylight.vpnservice;
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;
19 public class InterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
20 private static final Logger LOG = LoggerFactory.getLogger(InterfaceChangeListener.class);
22 private ListenerRegistration<DataChangeListener> listenerRegistration;
23 private final DataBroker broker;
24 private VpnInterfaceManager vpnInterfaceManager;
25 private IInterfaceManager interfaceManager;
28 public InterfaceChangeListener(final DataBroker db, VpnInterfaceManager vpnInterfaceManager) {
29 super(Interface.class);
31 this.vpnInterfaceManager = vpnInterfaceManager;
35 public void setInterfaceManager(IInterfaceManager interfaceManager) {
36 this.interfaceManager = interfaceManager;
40 public void close() throws Exception {
41 if (listenerRegistration != null) {
43 listenerRegistration.close();
44 } catch (final Exception e) {
45 LOG.error("Error when cleaning up DataChangeListener.", e);
47 listenerRegistration = null;
49 LOG.info("Interface listener Closed");
53 private void registerListener(final DataBroker db) {
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);
64 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
65 LOG.trace("Adding Interface : key: " + identifier + ", value=" + intrf );
70 private InstanceIdentifier<Interface> getWildCardPath() {
71 return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
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);
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);
91 LOG.debug("No VPN Interface associated with Interface {}", intrf.getName());
97 protected void update(InstanceIdentifier<Interface> identifier,
98 Interface original, Interface update) {
99 // TODO Auto-generated method stub