Working with OVS
[vpnservice.git] / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / vpnservice / InterfaceChangeListener.java
index 7e054145c915596141b6ab1dd951f6a4e4ebb9e5..d6c481ffd382c98735706538b0bc94ae098219de 100644 (file)
@@ -1,3 +1,10 @@
+/*
+ * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
 package org.opendaylight.vpnservice;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
@@ -5,20 +12,25 @@ import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
+import org.opendaylight.vpnservice.mdsalutil.NwConstants;
+import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.math.BigInteger;
+
 public class InterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(InterfaceChangeListener.class);
 
     private ListenerRegistration<DataChangeListener> listenerRegistration;
     private final DataBroker broker;
     private VpnInterfaceManager vpnInterfaceManager;
+    private IInterfaceManager interfaceManager;
 
 
     public InterfaceChangeListener(final DataBroker db, VpnInterfaceManager vpnInterfaceManager) {
@@ -28,6 +40,10 @@ public class InterfaceChangeListener extends AbstractDataChangeListener<Interfac
         registerListener(db);
     }
 
+    public void setInterfaceManager(IInterfaceManager interfaceManager) {
+      this.interfaceManager = interfaceManager;
+  }
+
     @Override
     public void close() throws Exception {
         if (listenerRegistration != null) {
@@ -66,10 +82,23 @@ public class InterfaceChangeListener extends AbstractDataChangeListener<Interfac
     @Override
     protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
         LOG.trace("Remove interface event - key: {}, value: {}", identifier, intrf );
+        if (intrf.getType().equals(Tunnel.class)) {
+          BigInteger dpnId =  interfaceManager.getDpnForInterface(intrf);
+          String ifName = intrf.getName();
+          LOG.debug("Removing tunnel interface associated with Interface {}", intrf.getName());
+          vpnInterfaceManager.makeTunnelIngressFlow(dpnId, ifName, NwConstants.DEL_FLOW);
+      }
+        else {
         VpnInterface vpnInterface = vpnInterfaceManager.getVpnInterface(intrf.getName());
-        InstanceIdentifier<VpnInterface> id = VpnUtil.getVpnInterfaceIdentifier(intrf.getName());
-        LOG.debug("Removing VPN Interface associated with Interface {}", intrf.getName());
-        vpnInterfaceManager.remove(id, vpnInterface);
+          if (vpnInterface !=null) {
+            InstanceIdentifier<VpnInterface> id = VpnUtil.getVpnInterfaceIdentifier(intrf.getName());
+            LOG.debug("Removing VPN Interface associated with Interface {}", intrf.getName());
+            vpnInterfaceManager.remove(id, vpnInterface);
+          }
+          else {
+            LOG.debug("No VPN Interface associated with Interface {}", intrf.getName());
+          }
+        }
     }
 
     @Override