Added unit tests for NextHopManager
[vpnservice.git] / nexthopmgr / nexthopmgr-impl / src / main / java / org / opendaylight / vpnservice / nexthopmgr / NexthopManager.java
index ab5b2772b4be21531ae5fe18df8d525712806c46..8ea89397fcce4f331d70f43b8fd407fc95b638af 100644 (file)
@@ -64,7 +64,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
     private static final FutureCallback<Void> DEFAULT_CALLBACK =
         new FutureCallback<Void>() {
             public void onSuccess(Void result) {
-                LOG.info("Success in Datastore write operation");
+                LOG.debug("Success in Datastore write operation");
             }
             public void onFailure(Throwable error) {
                 LOG.error("Error in Datastore write operation", error);
@@ -106,6 +106,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
             .build();
         //TODO: Error handling
         Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
+        LOG.trace("NextHopPointerPool result : {}", result);
 //            try {
 //                LOG.info("Result2: {}",result.get());
 //            } catch (InterruptedException | ExecutionException e) {
@@ -116,7 +117,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
     }
 
 
-    private long getVpnId(String vpnName) {
+    protected long getVpnId(String vpnName) {
         InstanceIdentifierBuilder<VpnInstance> idBuilder = InstanceIdentifier.builder(VpnInstances.class)
                 .child(VpnInstance.class, new VpnInstanceKey(vpnName));
 
@@ -125,6 +126,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
         Optional<VpnInstance1> vpn = read(LogicalDatastoreType.OPERATIONAL, idx);
 
         if (vpn.isPresent()) {
+            LOG.debug("VPN id returned: {}", vpn.get().getVpnId());
             return vpn.get().getVpnId();
         } else {
             return -1;
@@ -134,10 +136,11 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
     private long getDpnId(String ifName) {
         String[] fields = ifName.split(":");
         long dpn = Integer.parseInt(fields[1]);
+        LOG.debug("DpnId: {}", dpn);
         return dpn;
     }
 
-    private int createNextHopPointer(String nexthopKey) {
+    protected int createNextHopPointer(String nexthopKey) {
         GetUniqueIdInput getIdInput = new GetUniqueIdInputBuilder()
             .setPoolName("nextHopPointerPool").setIdKey(nexthopKey)
             .build();
@@ -159,6 +162,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
         long vpnId = getVpnId(vpnName);
         long dpnId = interfaceManager.getDpnForInterface(ifName);
         VpnNexthop nexthop = getVpnNexthop(vpnId, ipAddress);
+        LOG.trace("nexthop: {}", nexthop);
         if (nexthop == null) {
             List<BucketInfo> listBucketInfo = new ArrayList<BucketInfo>();
             List<ActionInfo> listActionInfo = interfaceManager.getInterfaceEgressActions(ifName);
@@ -166,8 +170,10 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
             // MAC re-write
             if (macAddress != null) {
                listActionInfo.add(0, new ActionInfo(ActionType.set_field_eth_dest, new String[]{macAddress}));
+               listActionInfo.add(0, new ActionInfo(ActionType.pop_mpls, new String[]{}));
             } else {
                 //FIXME: Log message here.
+                LOG.debug("mac address for new local nexthop is null");
             }
             listBucketInfo.add(bucket);
             GroupEntity groupEntity = MDSALUtil.buildGroupEntity(
@@ -207,7 +213,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
         }
     }
 
-    private void addVpnNexthopToDS(long vpnId, String ipPrefix, long egressPointer) {
+    protected void addVpnNexthopToDS(long vpnId, String ipPrefix, long egressPointer) {
 
         InstanceIdentifierBuilder<VpnNexthops> idBuilder = InstanceIdentifier.builder(L3nexthop.class)
                 .child(VpnNexthops.class, new VpnNexthopsKey(vpnId));
@@ -229,7 +235,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
 
         InstanceIdentifier<VpnNexthop> id1 = idBuilder
                 .child(VpnNexthop.class, new VpnNexthopKey(ipPrefix)).build();
-
+        LOG.trace("Adding vpnnextHop {} to Operational DS", nh);
         asyncWrite(LogicalDatastoreType.OPERATIONAL, id1, nh, DEFAULT_CALLBACK);
 
     }
@@ -247,6 +253,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
                 .setKey(new TunnelNexthopsKey(dpnId))
                 .setDpnId(dpnId)
                 .build();
+            LOG.trace("Adding tunnelnextHop {} to Operational DS for a new node", node);
             asyncWrite(LogicalDatastoreType.OPERATIONAL, id, node, DEFAULT_CALLBACK);
         }
 
@@ -258,12 +265,12 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
 
         InstanceIdentifier<TunnelNexthop> id1 = idBuilder
                 .child(TunnelNexthop.class, new TunnelNexthopKey(ipPrefix)).build();
-
+        LOG.trace("Adding tunnelnextHop {} to Operational DS for a dpn node", nh);
         asyncWrite(LogicalDatastoreType.OPERATIONAL, id1, nh, DEFAULT_CALLBACK);
 
     }
 
-    private VpnNexthop getVpnNexthop(long vpnId, String ipAddress) {
+    protected VpnNexthop getVpnNexthop(long vpnId, String ipAddress) {
 
         // check if vpn node is there 
         InstanceIdentifierBuilder<VpnNexthops> idBuilder = InstanceIdentifier.builder(L3nexthop.class)
@@ -277,6 +284,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
             for (VpnNexthop nexthop : nexthops) {
                 if (nexthop.getIpAddress().equals(ipAddress)) {
                     // return nexthop 
+                    LOG.trace("VpnNextHop : {}",nexthop);
                     return nexthop;
                 }
             }
@@ -297,6 +305,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
             List<TunnelNexthop> nexthops = dpnNexthops.get().getTunnelNexthop();
             for (TunnelNexthop nexthop : nexthops) {
                 if (nexthop.getIpAddress().equals(ipAddress)) {
+                    LOG.trace("TunnelNextHop : {}",nexthop);
                     return nexthop;
                 }
             }
@@ -311,6 +320,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
             return vpnNextHop.getEgressPointer();
         } else {
             TunnelNexthop tunnelNextHop = getTunnelNexthop(dpnId, nextHopIp);
+            LOG.trace("NExtHopPointer : {}", tunnelNextHop.getEgressPointer());
             return tunnelNextHop.getEgressPointer();
         }
     }
@@ -322,6 +332,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
                 .child(TunnelNexthop.class, new TunnelNexthopKey(ipPrefix));
         InstanceIdentifier<TunnelNexthop> id = idBuilder.build();
         // remove from DS     
+        LOG.trace("Removing tunnel next hop from datastore : {}", id);
         delete(LogicalDatastoreType.OPERATIONAL, id);
     }
 
@@ -332,25 +343,26 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
                 .child(VpnNexthop.class, new VpnNexthopKey(ipPrefix));
         InstanceIdentifier<VpnNexthop> id = idBuilder.build();
         // remove from DS
+        LOG.trace("Removing vpn next hop from datastore : {}", id);
         delete(LogicalDatastoreType.OPERATIONAL, id);
     }
 
  
-    public void removeLocalNextHop(String vpnName, String ipAddress) {
-
-        long vpnId = getVpnId(vpnName);
+    public void removeLocalNextHop(Long dpId, Long vpnId, String ipAddress) {
 
         VpnNexthop nh = getVpnNexthop(vpnId, ipAddress);
         if (nh != null) {
             // how to inform and remove dependent FIB entries??
             // we need to do it before the group is removed
-            
+            GroupEntity groupEntity = MDSALUtil.buildGroupEntity(
+                    dpId, nh.getEgressPointer(), ipAddress, GroupTypes.GroupIndirect, null);
             // remove Group ...
-            
+            mdsalManager.removeGroup(groupEntity);
             //update MD-SAL DS
             removeVpnNexthopFromDS(vpnId, ipAddress);
         } else {
             //throw error
+            LOG.error("removal of local next hop failed");
         }
 
     }
@@ -363,10 +375,15 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
             // we need to do it before the group is removed
 
             // remove Group ...
+            GroupEntity groupEntity = MDSALUtil.buildGroupEntity(
+                    dpnId, nh.getEgressPointer(), ipAddress, GroupTypes.GroupIndirect, null);
+            // remove Group ...
+            mdsalManager.removeGroup(groupEntity);
             //update MD-SAL DS
             removeTunnelNexthopFromDS(dpnId, ipAddress);
         } else {
             //throw error
+            LOG.error("removal of remote next hop failed : dpnid : {}, ipaddress : {}", dpnId, ipAddress);
         }
 
     }
@@ -378,6 +395,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
         GetEgressPointerOutputBuilder output = new GetEgressPointerOutputBuilder();
 
         String endpointIp = interfaceManager.getEndpointIpForDpn(input.getDpnId());
+        LOG.trace("getEgressPointer: input {}, endpointIp {}", input, endpointIp);
         if (input.getNexthopIp().equals(endpointIp)) {
             VpnNexthop vpnNextHop = getVpnNexthop(input.getVpnId(), input.getIpPrefix());
             output.setEgressPointer(vpnNextHop.getEgressPointer());
@@ -424,4 +442,20 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
         Futures.addCallback(tx.submit(), DEFAULT_CALLBACK);
     }
 
+    @Override
+    public Future<RpcResult<Void>> removeLocalNextHop(RemoveLocalNextHopInput input) {
+        VpnNexthop vpnNextHop = getVpnNexthop(input.getVpnId(), input.getIpPrefix());
+        RpcResultBuilder<Void> rpcResultBuilder;
+        LOG.debug("vpnnexthop is: {}", vpnNextHop);
+        try {
+            removeLocalNextHop(input.getDpnId(),input.getVpnId(), input.getIpPrefix());
+            rpcResultBuilder = RpcResultBuilder.success();
+        }
+        catch(Exception e){
+            LOG.error("Removal of local next hop for vpnNextHop {} failed {}" ,vpnNextHop, e);
+            rpcResultBuilder = RpcResultBuilder.failed();
+        }
+        return Futures.immediateFuture(rpcResultBuilder.build());
+    }
+
 }