NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / vpnmanager / impl / src / main / java / org / opendaylight / netvirt / vpnmanager / VpnRpcServiceImpl.java
index a2a60b20b31ba2039858e5f16169eeb334ecb943..2488cbe2cb39ca5f93bc1129f8012341dc443508 100644 (file)
@@ -7,15 +7,21 @@
  */
 package org.opendaylight.netvirt.vpnmanager;
 
-import com.google.common.base.Optional;
+import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
 import java.util.function.Consumer;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
+import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
@@ -23,12 +29,14 @@ import org.opendaylight.netvirt.vpnmanager.api.IVpnManager;
 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.fibmanager.rev150330.vrfentries.VrfEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
 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.ApplyArpConfigInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.ApplyArpConfigOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.ApplyArpConfigOutputBuilder;
 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;
@@ -43,6 +51,7 @@ import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.helpers.FormattingTuple;
@@ -51,23 +60,25 @@ import org.slf4j.helpers.MessageFormatter;
 @Singleton
 public class VpnRpcServiceImpl implements VpnRpcService {
     private static final Logger LOG = LoggerFactory.getLogger(VpnRpcServiceImpl.class);
-    private final DataBroker dataBroker;
-    private final IdManagerService idManager;
+    private final ManagedNewTransactionRunner txRunner;
     private final IFibManager fibManager;
     private final IBgpManager bgpManager;
     private final IVpnManager vpnManager;
     private final InterVpnLinkCache interVpnLinkCache;
+    private final VpnUtil vpnUtil;
+    private final InterVpnLinkUtil interVpnLinkUtil;
 
     @Inject
-    public VpnRpcServiceImpl(final DataBroker dataBroker, final IdManagerService idManager,
+    public VpnRpcServiceImpl(final DataBroker dataBroker,
             final IFibManager fibManager, IBgpManager bgpManager, final IVpnManager vpnManager,
-            final InterVpnLinkCache interVpnLinkCache) {
-        this.dataBroker = dataBroker;
-        this.idManager = idManager;
+            final InterVpnLinkCache interVpnLinkCache, VpnUtil vpnUtil, InterVpnLinkUtil interVpnLinkUtil) {
+        this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.fibManager = fibManager;
         this.bgpManager = bgpManager;
         this.vpnManager = vpnManager;
         this.interVpnLinkCache = interVpnLinkCache;
+        this.vpnUtil = vpnUtil;
+        this.interVpnLinkUtil = interVpnLinkUtil;
     }
 
     /**
@@ -78,10 +89,10 @@ public class VpnRpcServiceImpl implements VpnRpcService {
         String vpnName = input.getVpnName();
         String ipPrefix = input.getIpPrefix();
         SettableFuture<RpcResult<GenerateVpnLabelOutput>> futureResult = SettableFuture.create();
-        String rd = VpnUtil.getVpnRd(dataBroker, vpnName);
-        long label = VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
+        String rd = vpnUtil.getVpnRd(vpnName);
+        Uint32 label = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME,
             VpnUtil.getNextHopLabelKey(rd != null ? rd : vpnName, ipPrefix));
-        if (label == 0) {
+        if (label == null || label.longValue() == VpnConstants.INVALID_LABEL) {
             futureResult.set(RpcResultBuilder.<GenerateVpnLabelOutput>failed().withError(ErrorType.APPLICATION,
                     formatAndLog(LOG::error, "Could not retrieve the label for prefix {} in VPN {}", ipPrefix,
                             vpnName)).build());
@@ -99,8 +110,8 @@ public class VpnRpcServiceImpl implements VpnRpcService {
     public ListenableFuture<RpcResult<RemoveVpnLabelOutput>> removeVpnLabel(RemoveVpnLabelInput input) {
         String vpnName = input.getVpnName();
         String ipPrefix = input.getIpPrefix();
-        String rd = VpnUtil.getVpnRd(dataBroker, vpnName);
-        VpnUtil.releaseId(idManager, VpnConstants.VPN_IDPOOL_NAME,
+        String rd = vpnUtil.getVpnRd(vpnName);
+        vpnUtil.releaseId(VpnConstants.VPN_IDPOOL_NAME,
             VpnUtil.getNextHopLabelKey(rd != null ? rd : vpnName, ipPrefix));
         return RpcResultBuilder.success(new RemoveVpnLabelOutputBuilder().build()).buildFuture();
     }
@@ -134,7 +145,7 @@ public class VpnRpcServiceImpl implements VpnRpcService {
         String destination = input.getDestination();
         String vpnInstanceName = input.getVpnInstanceName();
         String nexthop = input.getNexthop();
-        Long label = input.getLabel();
+        Uint32 label = input.getLabel();
         LOG.info("Adding static route for Vpn {} with destination {}, nexthop {} and label {}",
             vpnInstanceName, destination, nexthop, label);
 
@@ -144,19 +155,21 @@ public class VpnRpcServiceImpl implements VpnRpcService {
             return result;
         }
 
-        if (label == null || label == 0) {
-            label = (long) VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
+        if (label == null || label.longValue() == VpnConstants.INVALID_LABEL) {
+            label = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME,
                 VpnUtil.getNextHopLabelKey(vpnInstanceName, destination));
-            if (label == 0) {
+            if (label.longValue() == VpnConstants.INVALID_LABEL) {
                 String message = "Unable to retrieve a new Label for the new Route";
                 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
                     message).build());
+                LOG.error("addStaticRoute: Unable to retrieve label for static route with destination {}, vpninstance"
+                        + " {}, nexthop {}", destination, vpnInstanceName, nexthop);
                 return result;
             }
         }
 
-        String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
-        VpnInstanceOpDataEntry vpnOpEntry = VpnUtil.getVpnInstanceOpData(dataBroker, vpnRd);
+        String vpnRd = vpnUtil.getVpnRd(input.getVpnInstanceName());
+        VpnInstanceOpDataEntry vpnOpEntry = vpnUtil.getVpnInstanceOpData(vpnRd);
         Boolean isVxlan = VpnUtil.isL3VpnOverVxLan(vpnOpEntry.getL3vni());
         VrfEntry.EncapType encapType = VpnUtil.getEncapType(isVxlan);
         if (vpnRd == null) {
@@ -169,9 +182,7 @@ public class VpnRpcServiceImpl implements VpnRpcService {
         Optional<InterVpnLinkDataComposite> optIVpnLink = interVpnLinkCache.getInterVpnLinkByEndpoint(nexthop);
         if (optIVpnLink.isPresent()) {
             try {
-                InterVpnLinkUtil.handleStaticRoute(optIVpnLink.get(), vpnInstanceName, destination, nexthop,
-                                                   label.intValue(),
-                                                   dataBroker, fibManager, bgpManager);
+                interVpnLinkUtil.handleStaticRoute(optIVpnLink.get(), vpnInstanceName, destination, nexthop, label);
             } catch (Exception e) {
                 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(ErrorType.APPLICATION,
                         formatAndLog(LOG::warn,
@@ -180,9 +191,18 @@ public class VpnRpcServiceImpl implements VpnRpcService {
                 return result;
             }
         } else {
-            vpnManager.addExtraRoute(vpnInstanceName, destination, nexthop, vpnRd, null /* routerId */,
-                    vpnOpEntry.getL3vni(), RouteOrigin.STATIC, null /* intfName */,
-                            null /*Adjacency*/, encapType, null);
+            try {
+                txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
+                    confTx -> vpnManager.addExtraRoute(vpnInstanceName, destination, nexthop, vpnRd,
+                            null /* routerId */, vpnOpEntry.getL3vni(),
+                            RouteOrigin.STATIC, null /* intfName */,
+                        null /*Adjacency*/, encapType, new HashSet<>() /*prefixListForRefreshFib*/,confTx)).get();
+            } catch (InterruptedException | ExecutionException e) {
+                LOG.error("Error adding static route {}", input, e);
+                result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(ErrorType.APPLICATION,
+                    "Error adding static route " + input, e).build());
+                return result;
+            }
         }
 
         AddStaticRouteOutput labelOutput = new AddStaticRouteOutputBuilder().setLabel(label).build();
@@ -226,7 +246,7 @@ public class VpnRpcServiceImpl implements VpnRpcService {
             return result;
         }
 
-        String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
+        String vpnRd = vpnUtil.getVpnRd(input.getVpnInstanceName());
         if (vpnRd == null) {
             String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
             result.set(RpcResultBuilder.<RemoveStaticRouteOutput>failed()
@@ -240,11 +260,13 @@ public class VpnRpcServiceImpl implements VpnRpcService {
             bgpManager.withdrawPrefix(vpnRd, destination);
         } else {
             vpnManager.delExtraRoute(vpnInstanceName, destination,
-                    nexthop, vpnRd, null /* routerId */, null /* intfName */, null);
+                    nexthop, vpnRd, null /* routerId */, null /* intfName */, null,
+                    null);
         }
         result.set(RpcResultBuilder.success(new RemoveStaticRouteOutputBuilder().build()).build());
 
         return result;
+
     }
 
     private String formatAndLog(Consumer<String> logger, String template, Object arg1, Object arg2) {
@@ -260,4 +282,16 @@ public class VpnRpcServiceImpl implements VpnRpcService {
         logger.accept(message);
         return message;
     }
+
+    @Override
+    public ListenableFuture<RpcResult<ApplyArpConfigOutput>> applyArpConfig(ApplyArpConfigInput input) {
+        Boolean isArpLearningEnabled = input.isEnableArpLearning();
+        LOG.info("isArpLearningEnabled {}", isArpLearningEnabled);
+        SettableFuture<RpcResult<ApplyArpConfigOutput>> result = SettableFuture.create();
+        ApplyArpConfigOutputBuilder output = new ApplyArpConfigOutputBuilder();
+        VpnUtil.enableArpLearning(isArpLearningEnabled);
+        output.setEnableArpLearning(VpnUtil.isArpLearningEnabled());
+        result.set(RpcResultBuilder.success(output).build());
+        return result;
+    }
 }