Fix for bug 3357, handled update on vpn interface 88/21088/3
authorSasidharan Sambasivam <sasidharan.s.sambasivam@ericsson.com>
Tue, 26 May 2015 08:04:15 +0000 (13:34 +0530)
committerSasidharan Sambasivam <sasidharan.s.sambasivam@ericsson.com>
Wed, 27 May 2015 06:43:36 +0000 (12:13 +0530)
Change-Id: I656d0fc51c32c4c7135b05452412604fd2af9566
Signed-off-by: Sasidharan Sambasivam <sasidharan.s.sambasivam@ericsson.com>
vpnmanager/vpnmanager-impl/src/main/java/org/opendaylight/vpnservice/VpnInterfaceManager.java
vpnmanager/vpnmanager-impl/src/main/java/org/opendaylight/vpnservice/VpnManager.java
vpnmanager/vpnmanager-impl/src/main/java/org/opendaylight/vpnservice/VpnserviceProvider.java

index 0f29dea16e9241eab61f042bd590b419dcf65209..e2101882f1fe4fef0d528c6ee177a23e03b78853 100644 (file)
@@ -395,8 +395,6 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
                 }
             }
         }
-//        InstanceIdentifier<VpnInterface> interfaceId = VpnUtil.getVpnInterfaceIdentifier(intfName);
-//        delete(LogicalDatastoreType.OPERATIONAL, interfaceId);
     }
 
     private <T extends DataObject> void delete(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
@@ -436,7 +434,6 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
     }
 
     private void removePrefixFromBGP(String rd, Adjacency nextHop) {
-        //public void deletePrefix(String rd, String prefix) throws Exception;
         try {
             bgpManager.deletePrefix(rd, nextHop.getIpAddress());
         } catch(Exception e) {
@@ -447,8 +444,57 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
     @Override
     protected void update(InstanceIdentifier<VpnInterface> identifier, 
                                    VpnInterface original, VpnInterface update) {
-        // TODO Auto-generated method stub
+        LOG.trace("Update VPN Interface {} , original {}, update {}", 
+                                                  identifier, original, update);
+        String vpnName = original.getVpnInstanceName();
+
+        boolean vpnNameChanged = false;
+        String rd = getRouteDistinguisher(vpnName);
+        String newRd = rd;
+        if(!vpnName.equals(update.getVpnInstanceName())) {
+            //VPN for this interface got changed. 
+            //Remove the interface from old VPN and add it to new VPN
+            String newVpnName = update.getVpnInstanceName();
+            newRd = getRouteDistinguisher(newVpnName);
+            if(newRd.equals("")) {
+                LOG.warn("VPN Instance {} not found. Update operation aborted", newVpnName);
+                return;
+            }
+            vpnNameChanged = true;
+            LOG.debug("New VPN Name for the interface {} is {}", newVpnName, original.getName());
+        }
+
+        BigInteger dpnId = interfaceManager.getDpnForInterface(original.getName());
+        String nextHopIp = interfaceManager.getEndpointIpForDpn(dpnId);
+        //List<Adjacency> oldAdjs = original.getAugmentation(Adjacencies.class).getAdjacency();
+        List<Adjacency> newAdjs = update.getAugmentation(Adjacencies.class).getAdjacency();
+        if(vpnNameChanged && newAdjs != null && !newAdjs.isEmpty()) {
+            long label = VpnConstants.INVALID_ID;
+            InstanceIdentifier<Adjacencies> path = identifier.augmentation(Adjacencies.class);
+            Optional<Adjacencies> adjacencies = read(LogicalDatastoreType.OPERATIONAL, path);
+            if (adjacencies.isPresent()) {
+                List<Adjacency> nextHops = adjacencies.get().getAdjacency();
+                for(Adjacency nextHop : nextHops) {
+                    label = nextHop.getLabel();
+                    if(label == VpnConstants.INVALID_ID) {
+                        //Generate label using ID Manager
+                        label = getUniqueId(nextHop.getIpAddress());
+                    }
+                    removePrefixFromBGP(rd, nextHop);
+                    updatePrefixToBGP(newRd, nextHop, nextHopIp, label);
+                }
+                asyncUpdate(LogicalDatastoreType.OPERATIONAL, identifier, update, DEFAULT_CALLBACK);
+            }
+        } else {
+            LOG.debug("No Update information is available for VPN Interface to proceed");
+        }
+    }
 
+    protected <T extends DataObject> void asyncUpdate(LogicalDatastoreType datastoreType,
+            InstanceIdentifier<T> path, T data, FutureCallback<Void> callback) {
+        WriteTransaction tx = broker.newWriteOnlyTransaction();
+        tx.merge(datastoreType, path, data, true);
+        Futures.addCallback(tx.submit(), callback);
     }
 
     private <T extends DataObject> void asyncWrite(LogicalDatastoreType datastoreType,
index 3689cbbe21395c9f5b07089a026d0d716888482a..435130c3a8ab6a63a7adf8bcc8e0a76998f5dfce 100644 (file)
@@ -26,9 +26,11 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnAfConfig;
+import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceBuilder;
+import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstance1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstance1Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.FibEntries;
@@ -52,6 +54,7 @@ public class VpnManager extends AbstractDataChangeListener<VpnInstance> implemen
     private final DataBroker broker;
     private final IBgpManager bgpManager;
     private IdManagerService idManager;
+    private VpnInterfaceManager vpnInterfaceManager;
     private final FibEntriesListener fibListener;
 
     private static final FutureCallback<Void> DEFAULT_CALLBACK =
@@ -95,11 +98,30 @@ public class VpnManager extends AbstractDataChangeListener<VpnInstance> implemen
         this.idManager = idManager;
     }
 
+    public void setVpnInterfaceManager(VpnInterfaceManager vpnInterfaceManager) {
+        this.vpnInterfaceManager = vpnInterfaceManager;
+    }
+
     @Override
     protected void remove(InstanceIdentifier<VpnInstance> identifier, VpnInstance del) {
-        LOG.trace("Remove event - Key: {}, value: {}", identifier, del);
+        LOG.trace("Remove VPN event - Key: {}, value: {}", identifier, del);
         String vpnName = del.getVpnInstanceName();
         InstanceIdentifier<VpnInstance> vpnIdentifier = VpnUtil.getVpnInstanceIdentifier(vpnName);
+        //Clean up vpn Interface
+        InstanceIdentifier<VpnInterfaces> vpnInterfacesId = InstanceIdentifier.builder(VpnInterfaces.class).build();
+        Optional<VpnInterfaces> optionalVpnInterfaces = read(LogicalDatastoreType.OPERATIONAL, vpnInterfacesId);
+
+        if(optionalVpnInterfaces.isPresent()) {
+            List<VpnInterface> vpnInterfaces = optionalVpnInterfaces.get().getVpnInterface();
+            for(VpnInterface vpnInterface : vpnInterfaces) {
+                if(vpnInterface.getVpnInstanceName().equals(vpnName)) {
+                    LOG.debug("VpnInterface {} will be removed from VPN {}", vpnInterface.getName(), vpnName);
+                    vpnInterfaceManager.remove(
+                            VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getName()), vpnInterface);
+                }
+            }
+        }
+
         delete(LogicalDatastoreType.OPERATIONAL, vpnIdentifier);
 
         String rd = del.getIpv4Family().getRouteDistinguisher();
@@ -219,7 +241,7 @@ public class VpnManager extends AbstractDataChangeListener<VpnInstance> implemen
             } else {
                 LOG.warn("RPC Call to Get Unique Id returned with Errors {}", rpcResult.getErrors());
             }
-        } catch (NullPointerException | InterruptedException | ExecutionException e) {
+        } catch (InterruptedException | ExecutionException e) {
             LOG.warn("Exception when getting Unique Id",e);
         }
         return 0;
index 9acdc36c6995d7a6da2b234b15688855a11869ed..a5198ba74d77f56b2a02b0e2d47054315aa79de4 100644 (file)
@@ -51,6 +51,7 @@ public class VpnserviceProvider implements BindingAwareProvider, IVpnManager,
             vpnInterfaceManager.setMdsalManager(mdsalManager);
             vpnInterfaceManager.setInterfaceManager(interfaceManager);
             vpnInterfaceManager.setIdManager(idManager);
+            vpnManager.setVpnInterfaceManager(vpnInterfaceManager);
             createIdPool();
         } catch (Exception e) {
             LOG.error("Error initializing services", e);