NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / dhcpservice / impl / src / main / java / org / opendaylight / netvirt / dhcpservice / jobs / DhcpInterfaceRemoveJob.java
index 77fcaa6b16d2ddb2870db1e33217d2c33487fbf2..509e65ef1e6cd175f24d2d45e2606b0384bcfec3 100644 (file)
@@ -7,21 +7,22 @@
  */
 package org.opendaylight.netvirt.dhcpservice.jobs;
 
-import com.google.common.base.Optional;
-import com.google.common.util.concurrent.FutureCallback;
+import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
+
 import com.google.common.util.concurrent.ListenableFuture;
-import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.Callable;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import java.util.concurrent.ExecutionException;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.genius.infra.Datastore;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
+import org.opendaylight.genius.infra.TypedReadWriteTransaction;
 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
-import org.opendaylight.genius.mdsalutil.MDSALDataStoreUtils;
-import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.netvirt.dhcpservice.DhcpExternalTunnelManager;
 import org.opendaylight.netvirt.dhcpservice.DhcpManager;
 import org.opendaylight.netvirt.dhcpservice.DhcpServiceUtils;
@@ -36,38 +37,27 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpserv
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.api.rev150710._interface.name.mac.addresses.InterfaceNameMacAddressKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.api.rev150710.subnet.dhcp.port.data.SubnetToDhcpPort;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class DhcpInterfaceRemoveJob implements Callable<List<ListenableFuture<Void>>> {
+public class DhcpInterfaceRemoveJob implements Callable<List<? extends ListenableFuture<?>>> {
 
     private static final Logger LOG = LoggerFactory.getLogger(DhcpInterfaceRemoveJob.class);
 
-    private static final FutureCallback<Void> DEFAULT_CALLBACK = new FutureCallback<Void>() {
-        @Override
-        public void onSuccess(Void result) {
-            LOG.debug("Success in Datastore write operation");
-        }
-
-        @Override
-        public void onFailure(Throwable error) {
-            LOG.error("Error in Datastore write operation", error);
-        }
-    };
-
     private final DhcpManager dhcpManager;
     private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
     private final DataBroker dataBroker;
     private final ManagedNewTransactionRunner txRunner;
     private final Interface interfaceDel;
-    private final BigInteger dpnId;
+    private final Uint64 dpnId;
     private final IInterfaceManager interfaceManager;
     private final IElanService elanService;
     private final Port port;
 
     public DhcpInterfaceRemoveJob(DhcpManager dhcpManager, DhcpExternalTunnelManager dhcpExternalTunnelManager,
                                   DataBroker dataBroker,
-                                  Interface interfaceDel, BigInteger dpnId, IInterfaceManager interfaceManager,
+                                  Interface interfaceDel, Uint64 dpnId, IInterfaceManager interfaceManager,
                                   IElanService elanService, Port port) {
         this.dhcpManager = dhcpManager;
         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
@@ -81,15 +71,15 @@ public class DhcpInterfaceRemoveJob implements Callable<List<ListenableFuture<Vo
     }
 
     @Override
-    public List<ListenableFuture<Void>> call() {
+    public List<ListenableFuture<Void>> call() throws ExecutionException, InterruptedException {
         String interfaceName = interfaceDel.getName();
         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
                 interfaceManager.getInterfaceInfoFromConfigDataStore(interfaceName);
         if (iface != null) {
-            IfTunnel tunnelInterface = iface.getAugmentation(IfTunnel.class);
+            IfTunnel tunnelInterface = iface.augmentation(IfTunnel.class);
             if (tunnelInterface != null && !tunnelInterface.isInternal()) {
                 IpAddress tunnelIp = tunnelInterface.getTunnelDestination();
-                List<BigInteger> dpns = DhcpServiceUtils.getListOfDpns(dataBroker);
+                List<Uint64> dpns = DhcpServiceUtils.getListOfDpns(dataBroker);
                 if (dpns.contains(dpnId)) {
                     return dhcpExternalTunnelManager.handleTunnelStateDown(tunnelIp, dpnId);
                 }
@@ -98,7 +88,7 @@ public class DhcpInterfaceRemoveJob implements Callable<List<ListenableFuture<Vo
         }
         List<ListenableFuture<Void>> futures = new ArrayList<>();
         // Support for VM migration use cases.
-        futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
+        futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
             tx -> DhcpServiceUtils.unbindDhcpService(interfaceName, tx)));
         java.util.Optional<String> subnetId = DhcpServiceUtils.getNeutronSubnetId(port);
         if (subnetId.isPresent()) {
@@ -106,7 +96,7 @@ public class DhcpInterfaceRemoveJob implements Callable<List<ListenableFuture<Vo
                     subnetId.get());
             if (subnetToDhcp.isPresent()) {
                 LOG.trace("Removing ArpResponder flow for last interface {} on DPN {}", interfaceName, dpnId);
-                ArpResponderInput arpInput = new ArpResponderInput.ArpReponderInputBuilder().setDpId(dpnId)
+                ArpResponderInput arpInput = new ArpResponderInput.ArpReponderInputBuilder().setDpId(dpnId.toJava())
                         .setInterfaceName(interfaceName).setSpa(subnetToDhcp.get().getPortFixedip())
                         .setLportTag(interfaceDel.getIfIndex()).buildForRemoveFlow();
                 elanService.removeArpResponderFlow(arpInput);
@@ -116,26 +106,30 @@ public class DhcpInterfaceRemoveJob implements Callable<List<ListenableFuture<Vo
         return futures;
     }
 
-    private List<ListenableFuture<Void>> unInstallDhcpEntries(String interfaceName, BigInteger dpId) {
-        String vmMacAddress = getAndRemoveVmMacAddress(interfaceName);
-        return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
+    private List<ListenableFuture<Void>> unInstallDhcpEntries(String interfaceName, Uint64 dpId)
+        throws ExecutionException, InterruptedException {
+        String vmMacAddress = txRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
+            tx -> getAndRemoveVmMacAddress(tx, interfaceName)).get();
+        return Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
             tx -> dhcpManager.unInstallDhcpEntries(dpId, vmMacAddress, tx)));
     }
 
-    private String getAndRemoveVmMacAddress(String interfaceName) {
+    @Nullable
+    private String getAndRemoveVmMacAddress(TypedReadWriteTransaction<Datastore.Operational> tx, String interfaceName)
+        throws ExecutionException, InterruptedException {
         InstanceIdentifier<InterfaceNameMacAddress> instanceIdentifier =
                 InstanceIdentifier.builder(InterfaceNameMacAddresses.class)
                         .child(InterfaceNameMacAddress.class, new InterfaceNameMacAddressKey(interfaceName)).build();
-        Optional<InterfaceNameMacAddress> existingEntry =
-                MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, instanceIdentifier);
-        if (existingEntry.isPresent()) {
-            String vmMacAddress = existingEntry.get().getMacAddress();
-            LOG.trace("Entry for interface found in InterfaceNameVmMacAddress map {}, {}", interfaceName, vmMacAddress);
-            MDSALDataStoreUtils.asyncRemove(dataBroker, LogicalDatastoreType.OPERATIONAL,
-                    instanceIdentifier, DEFAULT_CALLBACK);
-            return vmMacAddress;
-        }
-        LOG.trace("Entry for interface {} missing in InterfaceNameVmMacAddress map", interfaceName);
-        return null;
+        return tx.read(instanceIdentifier).get().map(
+            interfaceNameMacAddress -> {
+                String vmMacAddress = interfaceNameMacAddress.getMacAddress();
+                LOG.trace("Entry for interface found in InterfaceNameVmMacAddress map {}, {}", interfaceName,
+                        vmMacAddress);
+                tx.delete(instanceIdentifier);
+                return vmMacAddress;
+            }).orElseGet(() -> {
+                LOG.trace("Entry for interface {} missing in InterfaceNameVmMacAddress map", interfaceName);
+                return null;
+            });
     }
 }