BUG:5186 Fix for change in extraroutes type
[vpnservice.git] / neutronvpn / neutronvpn-impl / src / main / java / org / opendaylight / vpnservice / neutronvpn / NeutronRouterChangeListener.java
index 826f9f839b52a35c0082c336f631031fe79ff456..9da9f7d45b05ef7398eaded76f1a7f64ba2f1237 100644 (file)
@@ -8,6 +8,8 @@
 package org.opendaylight.vpnservice.neutronvpn;
 
 
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.l3.attributes.Routes;
+
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
@@ -18,12 +20,10 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.router
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.Interfaces;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.vpnmaps.VpnMap;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -83,21 +83,16 @@ public class NeutronRouterChangeListener extends AbstractDataChangeListener<Rout
         if (LOG.isTraceEnabled()) {
             LOG.trace("Removing router : key: " + identifier + ", value=" + input);
         }
-        // check if this router has internal-VPN
         Uuid routerId = input.getUuid();
-        VpnMap vpnmap = NeutronvpnUtils.getVpnMap(broker, routerId);
-        if (vpnmap != null) {
-            // if yes, remove corresponding internal vpn
-            LOG.trace("removing internal-vpn for router {}", routerId);
-            nvpnManager.removeL3Vpn(routerId);
-        } else {
-            // if not, it is associated with some VPN
-            // remove VPN-router association
-            Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId);
-            LOG.trace("dissociating router {} from vpn {}", routerId, vpnId);
-            nvpnManager.dissociateRouterFromVpn(vpnId, routerId);
+        // fetch subnets associated to router
+        List<Interfaces> routerInterfaces = input.getInterfaces();
+        List<Uuid> routerSubnetIds = new ArrayList<Uuid>();
+        if (routerInterfaces != null) {
+            for (Interfaces rtrIf : routerInterfaces) {
+                routerSubnetIds.add(rtrIf.getSubnetId());
+            }
         }
-
+        nvpnManager.handleNeutronRouterDeleted(routerId, routerSubnetIds);
     }
 
     @Override
@@ -107,14 +102,17 @@ public class NeutronRouterChangeListener extends AbstractDataChangeListener<Rout
                     update);
         }
         Uuid routerId = update.getUuid();
-        Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId);
+        Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
+        // internal vpn always present in case external vpn not found
+        if (vpnId == null) {
+            vpnId = routerId;
+        }
         List<Interfaces> oldInterfaces = (original.getInterfaces() != null) ? original.getInterfaces() : new
                 ArrayList<Interfaces>();
         List<Interfaces> newInterfaces = (update.getInterfaces() != null) ? update.getInterfaces() : new
                 ArrayList<Interfaces>();
-        List<String> oldRoutes = (original.getRoutes() != null) ? original.getRoutes() : new ArrayList<String>();
-        List<String> newRoutes = (update.getRoutes() != null) ? update.getRoutes() : new ArrayList<String>();
-
+        List<Routes> oldRoutes = (original.getRoutes() != null) ? original.getRoutes() : new ArrayList<Routes>();
+        List<Routes> newRoutes = (update.getRoutes() != null) ? update.getRoutes() : new ArrayList<Routes>();
         if (!oldInterfaces.equals(newInterfaces)) {
             for (Interfaces intrf : newInterfaces) {
                 if (!oldInterfaces.remove(intrf)) {
@@ -128,9 +126,9 @@ public class NeutronRouterChangeListener extends AbstractDataChangeListener<Rout
             }
         }
         if (!oldRoutes.equals(newRoutes)) {
-            Iterator<String> iterator = newRoutes.iterator();
+            Iterator<Routes> iterator = newRoutes.iterator();
             while (iterator.hasNext()) {
-                String route = iterator.next();
+                Routes route = iterator.next();
                 if (oldRoutes.remove(route)) {
                     iterator.remove();
                 }