Remove unnecessary generic types
[netvirt.git] / vpnservice / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / netvirt / vpnmanager / VpnRpcServiceImpl.java
index 2a17909dd73f7aee732aace3ef5ef4add2697626..5bf6b9520c496fcf20b978704d8696cdcf89cf15 100644 (file)
@@ -7,12 +7,28 @@
  */
 package org.opendaylight.netvirt.vpnmanager;
 
+import com.google.common.base.Optional;
 import com.google.common.util.concurrent.SettableFuture;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.concurrent.Future;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
+import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkCache;
+import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkDataComposite;
+import org.opendaylight.netvirt.vpnmanager.intervpnlink.InterVpnLinkUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.*;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteOutputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelOutputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveStaticRouteInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveVpnLabelInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.VpnRpcService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.links.InterVpnLink;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
@@ -21,29 +37,30 @@ import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.concurrent.Future;
-
 public class VpnRpcServiceImpl implements VpnRpcService {
     private static final Logger LOG = LoggerFactory.getLogger(VpnRpcServiceImpl.class);
     private final DataBroker dataBroker;
     private final IdManagerService idManager;
     private final VpnInterfaceManager vpnInterfaceMgr;
-    private final IFibManager fibManager;
+    private IFibManager fibManager;
+    private IBgpManager bgpManager;
 
     public VpnRpcServiceImpl(final DataBroker dataBroker, final IdManagerService idManager,
-                             final VpnInterfaceManager vpnIfaceMgr, final IFibManager fibManager) {
+        final VpnInterfaceManager vpnIfaceMgr, final IFibManager fibManager, IBgpManager bgpManager) {
         this.dataBroker = dataBroker;
         this.idManager = idManager;
         this.vpnInterfaceMgr = vpnIfaceMgr;
         this.fibManager = fibManager;
+        this.bgpManager = bgpManager;
+    }
+
+    public void setFibManager(IFibManager fibMgr) {
+        this.fibManager = fibMgr;
     }
 
+
     /**
-     * to generate label for the given ip prefix from the associated VPN
-     *
+     * Generate label for the given ip prefix from the associated VPN.
      */
     @Override
     public Future<RpcResult<GenerateVpnLabelOutput>> generateVpnLabel(GenerateVpnLabelInput input) {
@@ -52,12 +69,12 @@ public class VpnRpcServiceImpl implements VpnRpcService {
         SettableFuture<RpcResult<GenerateVpnLabelOutput>> futureResult = SettableFuture.create();
         String rd = VpnUtil.getVpnRd(dataBroker, vpnName);
         long label = VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
-                        VpnUtil.getNextHopLabelKey((rd != null) ? rd : vpnName, ipPrefix));
+            VpnUtil.getNextHopLabelKey((rd != null) ? rd : vpnName, ipPrefix));
         if (label == 0) {
-            String msg = String.format("Could not retrieve the label for prefix {} in VPN {}", ipPrefix, vpnName);
+            String msg = "Could not retrieve the label for prefix " + ipPrefix + " in VPN " + vpnName;
             LOG.error(msg);
             futureResult.set(RpcResultBuilder.<GenerateVpnLabelOutput>failed().withError(ErrorType.APPLICATION, msg)
-                                                                              .build());
+                .build());
         } else {
             GenerateVpnLabelOutput output = new GenerateVpnLabelOutputBuilder().setLabel(label).build();
             futureResult.set(RpcResultBuilder.success(output).build());
@@ -66,8 +83,7 @@ public class VpnRpcServiceImpl implements VpnRpcService {
     }
 
     /**
-     * to remove label for the given ip prefix from the associated VPN
-     *
+     * Remove label for the given ip prefix from the associated VPN.
      */
     @Override
     public Future<RpcResult<Void>> removeVpnLabel(RemoveVpnLabelInput input) {
@@ -76,31 +92,33 @@ public class VpnRpcServiceImpl implements VpnRpcService {
         String rd = VpnUtil.getVpnRd(dataBroker, vpnName);
         SettableFuture<RpcResult<Void>> futureResult = SettableFuture.create();
         VpnUtil.releaseId(idManager, VpnConstants.VPN_IDPOOL_NAME,
-                VpnUtil.getNextHopLabelKey((rd != null) ? rd : vpnName, ipPrefix));
+            VpnUtil.getNextHopLabelKey((rd != null) ? rd : vpnName, ipPrefix));
         futureResult.set(RpcResultBuilder.<Void>success().build());
         return futureResult;
     }
 
     private Collection<RpcError> validateAddStaticRouteInput(AddStaticRouteInput input) {
-        Collection<RpcError> rpcErrors = new ArrayList<RpcError>();
+        Collection<RpcError> rpcErrors = new ArrayList<>();
         String destination = input.getDestination();
         String vpnInstanceName = input.getVpnInstanceName();
         String nexthop = input.getNexthop();
-        if ( destination == null || destination.isEmpty() ) {
+        if (destination == null || destination.isEmpty()) {
             String message = "destination parameter is mandatory";
             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
         }
-        if ( vpnInstanceName == null || vpnInstanceName.isEmpty() ) {
+        if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
             String message = "vpnInstanceName parameter is mandatory";
             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
         }
-        if ( nexthop == null || nexthop.isEmpty() ) {
+        if (nexthop == null || nexthop.isEmpty()) {
             String message = "nexthop parameter is mandatory";
             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
         }
         return rpcErrors;
     }
 
+    // TODO Clean up the exception handling
+    @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
     public Future<RpcResult<AddStaticRouteOutput>> addStaticRoute(AddStaticRouteInput input) {
 
@@ -110,40 +128,51 @@ public class VpnRpcServiceImpl implements VpnRpcService {
         String nexthop = input.getNexthop();
         Long label = input.getLabel();
         LOG.info("Adding static route for Vpn {} with destination {}, nexthop {} and label {}",
-                 vpnInstanceName, destination, nexthop, label);
+            vpnInstanceName, destination, nexthop, label);
 
         Collection<RpcError> rpcErrors = validateAddStaticRouteInput(input);
-        if ( !rpcErrors.isEmpty() ) {
+        if (!rpcErrors.isEmpty()) {
             result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withRpcErrors(rpcErrors).build());
             return result;
         }
 
-        if ( label == null || label == 0 ) {
+        if (label == null || label == 0) {
             label = (long) VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
-                                               VpnUtil.getNextHopLabelKey(vpnInstanceName, destination));
-            if ( label == 0 ) {
+                VpnUtil.getNextHopLabelKey(vpnInstanceName, destination));
+            if (label == 0) {
                 String message = "Unable to retrieve a new Label for the new Route";
                 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
-                                                                                     message).build());
+                    message).build());
                 return result;
             }
         }
 
         String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
-        if ( vpnRd == null ) {
+        if (vpnRd == null) {
             String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
             result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
-                                                                                 message).build());
+                message).build());
             return result;
         }
 
-        InterVpnLink interVpnLink = VpnUtil.getInterVpnLinkByEndpointIp(dataBroker, nexthop);
-        if ( interVpnLink != null ) {
-            // A static route pointing to an InterVpnLink endpoint: just write the VrfEntry
-            fibManager.addOrUpdateFibEntry(dataBroker, vpnRd, destination, Arrays.asList(nexthop), label.intValue(), RouteOrigin.STATIC, null);
+        Optional<InterVpnLinkDataComposite> optIVpnLink = InterVpnLinkCache.getInterVpnLinkByEndpoint(nexthop);
+        if (optIVpnLink.isPresent()) {
+
+            try {
+                InterVpnLinkUtil.handleStaticRoute(optIVpnLink.get(), vpnInstanceName, destination, nexthop,
+                                                   label.intValue(),
+                                                   dataBroker, fibManager, bgpManager);
+            } catch (Exception e) {
+                String errMsg = "Could not advertise route [vpn=" + vpnRd + ", prefix=" + destination + ", label="
+                        + label + ", nexthop=" + nexthop + ", ] to BGP. Reason: " + e.getMessage();
+                LOG.warn(errMsg, e);
+                result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(ErrorType.APPLICATION, errMsg)
+                      .build());
+                return result;
+            }
         } else {
             vpnInterfaceMgr.addExtraRoute(destination, nexthop, vpnRd, null /*routerId */, label.intValue(),
-                                          null /* intfName */);
+                RouteOrigin.STATIC, null /* intfName */, null, null);
         }
 
         AddStaticRouteOutput labelOutput = new AddStaticRouteOutputBuilder().setLabel(label).build();
@@ -152,19 +181,19 @@ public class VpnRpcServiceImpl implements VpnRpcService {
     }
 
     private Collection<RpcError> validateRemoveStaticRouteInput(RemoveStaticRouteInput input) {
-        Collection<RpcError> rpcErrors = new ArrayList<RpcError>();
+        Collection<RpcError> rpcErrors = new ArrayList<>();
         String destination = input.getDestination();
         String vpnInstanceName = input.getVpnInstanceName();
         String nexthop = input.getNexthop();
-        if ( destination == null || destination.isEmpty() ) {
+        if (destination == null || destination.isEmpty()) {
             String message = "destination parameter is mandatory";
             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
         }
-        if ( vpnInstanceName == null || vpnInstanceName.isEmpty() ) {
+        if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
             String message = "vpnInstanceName parameter is mandatory";
             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
         }
-        if ( nexthop == null || nexthop.isEmpty() ) {
+        if (nexthop == null || nexthop.isEmpty()) {
             String message = "nexthop parameter is mandatory";
             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
         }
@@ -180,26 +209,26 @@ public class VpnRpcServiceImpl implements VpnRpcService {
         String vpnInstanceName = input.getVpnInstanceName();
         String nexthop = input.getNexthop();
         LOG.info("Removing static route with destination={}, nexthop={} in VPN={}",
-                 destination, nexthop, vpnInstanceName);
+            destination, nexthop, vpnInstanceName);
         Collection<RpcError> rpcErrors = validateRemoveStaticRouteInput(input);
-        if ( !rpcErrors.isEmpty() ) {
+        if (!rpcErrors.isEmpty()) {
             result.set(RpcResultBuilder.<Void>failed().withRpcErrors(rpcErrors).build());
             return result;
         }
 
         String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
-        if ( vpnRd == null ) {
+        if (vpnRd == null) {
             String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
             result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, message).build());
             return result;
         }
 
-        InterVpnLink interVpnLink = VpnUtil.getInterVpnLinkByEndpointIp(dataBroker, nexthop);
-        if ( interVpnLink != null ) {
-            // A static route pointing to an InterVpnLink endpoint: just remove the VrfEntry from DS
-            fibManager.removeOrUpdateFibEntry(dataBroker,  vpnRd, destination, nexthop, null);
+        Optional<InterVpnLink> optVpnLink = InterVpnLinkUtil.getInterVpnLinkByEndpointIp(dataBroker, nexthop);
+        if (optVpnLink.isPresent()) {
+            fibManager.removeOrUpdateFibEntry(dataBroker, vpnRd, destination, nexthop, null);
+            bgpManager.withdrawPrefix(vpnRd, destination);
         } else {
-            vpnInterfaceMgr.delExtraRoute(destination, nexthop, vpnRd, null /*routerId*/, null /*intfName*/);
+            vpnInterfaceMgr.delExtraRoute(destination, nexthop, vpnRd, null /*routerId*/, null /*intfName*/, null);
         }
         result.set(RpcResultBuilder.<Void>success().build());