Bug 6743: added futures to InterfaceManager and VppNodeManager
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / iface / InterfaceManager.java
index ad77da246c7e5e1280d936a2ca81b296afac1e8d..07c90336243fa896eb21704d5885c422e3730684 100644 (file)
@@ -10,11 +10,16 @@ package org.opendaylight.groupbasedpolicy.renderer.vpp.iface;
 
 import java.util.List;
 import java.util.Set;
-import java.util.concurrent.ExecutionException;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
+import com.google.common.eventbus.Subscribe;
+import com.google.common.util.concurrent.AsyncFunction;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -62,10 +67,6 @@ import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.SetMultimap;
-import com.google.common.eventbus.Subscribe;
-import com.google.common.util.concurrent.AsyncFunction;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
 
 public class InterfaceManager implements AutoCloseable {
 
@@ -82,37 +83,87 @@ public class InterfaceManager implements AutoCloseable {
     @Subscribe
     @SuppressWarnings("OptionalGetWithoutIsPresent")
     public synchronized void vppEndpointChanged(VppEndpointConfEvent event) {
-        try {
-            switch (event.getDtoModificationType()) {
-                case CREATED: {
-                    vppEndpointCreated(event.getAfter().get()).get();
-                    updatePolicyExcludedEndpoints(event.getAfter().get(), true);
-                }
-                break;
-                case UPDATED:
-                    vppEndpointUpdated(event.getBefore().get(), event.getAfter().get()).get();
-                    updatePolicyExcludedEndpoints(event.getBefore().get(), false);
-                    updatePolicyExcludedEndpoints(event.getAfter().get(), true);
-                    break;
-                case DELETED:
-                    vppEndpointDeleted(event.getBefore().get()).get();
-                    updatePolicyExcludedEndpoints(event.getBefore().get(), false);
-                    break;
+        ListenableFuture<Void> modificationFuture;
+        ListenableFuture<Boolean> policyExcludedFuture;
+        String message;
+        final VppEndpoint oldVppEndpoint = event.getBefore().orNull();
+        final VppEndpoint newVppEndpoint = event.getAfter().orNull();
+        switch (event.getDtoModificationType()) {
+            case CREATED: {
+                Preconditions.checkNotNull(newVppEndpoint);
+                modificationFuture = vppEndpointCreated(newVppEndpoint);
+                message = String.format("Vpp endpoint %s on node %s and interface %s created",
+                        newVppEndpoint.getAddress(), newVppEndpoint.getVppNodeId().getValue(),
+                        newVppEndpoint.getVppInterfaceName());
+                policyExcludedFuture = updatePolicyExcludedEndpoints(newVppEndpoint, true);
+            }
+            break;
+            case UPDATED: {
+                Preconditions.checkNotNull(oldVppEndpoint);
+                Preconditions.checkNotNull(newVppEndpoint);
+                modificationFuture = vppEndpointUpdated(oldVppEndpoint, newVppEndpoint);
+                message = String.format("Vpp endpoint %s on node %s and interface %s updated",
+                        newVppEndpoint.getAddress(), newVppEndpoint.getVppNodeId().getValue(),
+                        newVppEndpoint.getVppInterfaceName());
+                final ListenableFuture<Boolean> partialOldPolicyExcludedFuture =
+                        updatePolicyExcludedEndpoints(oldVppEndpoint, false);
+                policyExcludedFuture =
+                        Futures.transform(partialOldPolicyExcludedFuture, (AsyncFunction<Boolean, Boolean>) input ->
+                                updatePolicyExcludedEndpoints(newVppEndpoint, true));
+            }
+            break;
+            case DELETED: {
+                Preconditions.checkNotNull(oldVppEndpoint);
+                modificationFuture = vppEndpointDeleted(oldVppEndpoint);
+                message = String.format("Vpp endpoint %s on node %s and interface %s removed",
+                        oldVppEndpoint.getAddress(), oldVppEndpoint.getVppNodeId().getValue(),
+                        oldVppEndpoint.getVppInterfaceName());
+                policyExcludedFuture = updatePolicyExcludedEndpoints(event.getBefore().get(), false);
+            }
+            break;
+            default: {
+                message = "Unknown event modification type: " + event.getDtoModificationType();
+                modificationFuture = Futures.immediateFailedFuture(new VppRendererProcessingException(message));
+                policyExcludedFuture = Futures.immediateFailedFuture(new VppRendererProcessingException(message));
             }
-        } catch (InterruptedException | ExecutionException e) {
-            LOG.warn("Failed to update Vpp Endpoint. {}", event, e);
         }
+        // Modification
+        Futures.addCallback(modificationFuture, new FutureCallback<Void>() {
+            @Override
+            public void onSuccess(@Nullable Void result) {
+                LOG.info(message);
+            }
+
+            @Override
+            public void onFailure(@Nonnull Throwable t) {
+                LOG.warn("Vpp endpoint change event failed. Old ep: {}, new ep: {}", oldVppEndpoint, newVppEndpoint);
+            }
+        });
+
+        // Excluded policy
+        Futures.addCallback(policyExcludedFuture, new FutureCallback<Boolean>() {
+            @Override
+            public void onSuccess(@Nullable Boolean input) {
+                // NO-OP
+            }
+
+            @Override
+            public void onFailure(@Nonnull Throwable throwable) {
+                LOG.warn("Vpp endpoint exclusion failed. Odl ep: {}, new ep: {}", oldVppEndpoint, newVppEndpoint);
+            }
+        });
     }
 
-    private void updatePolicyExcludedEndpoints(VppEndpoint vppEndpoint, boolean created) {
-        if (vppEndpoint.getAugmentation(ExcludeFromPolicy.class) == null) {
-            return;
+    private ListenableFuture<Boolean> updatePolicyExcludedEndpoints(VppEndpoint vppEndpoint, boolean created) {
+        if (vppEndpoint == null || vppEndpoint.getAugmentation(ExcludeFromPolicy.class) == null) {
+            return Futures.immediateFuture(true);
         }
         if (created) {
             excludedFromPolicy.put(vppEndpoint.getVppNodeId(), vppEndpoint.getVppInterfaceName());
-            return;
+            return Futures.immediateFuture(true);
         }
-        excludedFromPolicy.remove(vppEndpoint.getVppNodeId(), vppEndpoint.getVppInterfaceName());
+        return Futures.immediateFuture(excludedFromPolicy.remove(vppEndpoint.getVppNodeId(),
+                vppEndpoint.getVppInterfaceName()));
     }
 
     private ListenableFuture<Void> vppEndpointCreated(VppEndpoint vppEndpoint) {
@@ -126,7 +177,6 @@ public class InterfaceManager implements AutoCloseable {
         } else if (interfaceTypeChoice instanceof LoopbackCase){
             potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.PUT);
         }
-
         if (!potentialIfaceCommand.isPresent()) {
             LOG.debug("Interface/PUT command was not created for VppEndpoint point {}", vppEndpoint);
             return Futures.immediateFuture(null);
@@ -145,7 +195,7 @@ public class InterfaceManager implements AutoCloseable {
 
     public ListenableFuture<Void> createInterfaceOnVpp(final ConfigCommand createIfaceWithoutBdCommand,
                                                        final DataBroker vppDataBroker) {
-        final boolean transactionState = GbpNetconfTransaction.write(vppDataBroker, createIfaceWithoutBdCommand,
+        final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(vppDataBroker, createIfaceWithoutBdCommand,
                 GbpNetconfTransaction.RETRY_COUNT);
         if (transactionState) {
             LOG.trace("Creating Interface on VPP: {}", createIfaceWithoutBdCommand);
@@ -161,7 +211,7 @@ public class InterfaceManager implements AutoCloseable {
                                                                        final DataBroker vppDataBroker,
                                                                        final VppEndpoint vppEndpoint,
                                                                        final InstanceIdentifier<?> vppNodeIid) {
-        final boolean transactionState = GbpNetconfTransaction.write(vppDataBroker, createIfaceWithoutBdCommand,
+        final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(vppDataBroker, createIfaceWithoutBdCommand,
                 GbpNetconfTransaction.RETRY_COUNT);
         if (transactionState) {
             LOG.debug("Create interface on VPP command was successful. VPP: {} Command: {}", vppNodeIid,
@@ -176,8 +226,7 @@ public class InterfaceManager implements AutoCloseable {
     }
 
     private ListenableFuture<Void> vppEndpointUpdated(@Nonnull final VppEndpoint oldVppEndpoint,
-                                                      @Nonnull final VppEndpoint newVppEndpoint)
-            throws ExecutionException, InterruptedException {
+                                                      @Nonnull final VppEndpoint newVppEndpoint) {
         if(!oldVppEndpoint.equals(newVppEndpoint)) {
             LOG.debug("Updating vpp endpoint, old EP: {} new EP: {}", oldVppEndpoint, newVppEndpoint);
             return Futures.transform(vppEndpointDeleted(oldVppEndpoint),
@@ -217,8 +266,8 @@ public class InterfaceManager implements AutoCloseable {
 
     private ListenableFuture<Void> deleteIfaceOnVpp(ConfigCommand deleteIfaceWithoutBdCommand,
             DataBroker vppDataBroker, VppEndpoint vppEndpoint, InstanceIdentifier<?> vppNodeIid) {
-        final boolean transactionState = GbpNetconfTransaction.deleteIfExists(vppDataBroker, deleteIfaceWithoutBdCommand,
-                GbpNetconfTransaction.RETRY_COUNT);
+        final boolean transactionState = GbpNetconfTransaction.netconfSyncedDelete(vppDataBroker,
+            deleteIfaceWithoutBdCommand, GbpNetconfTransaction.RETRY_COUNT);
         if (transactionState) {
             LOG.debug("Delete interface on VPP command was successful: VPP: {} Command: {}", vppNodeIid,
                     deleteIfaceWithoutBdCommand);
@@ -253,7 +302,7 @@ public class InterfaceManager implements AutoCloseable {
         }
     }
 
-    public static Optional<ConfigCommand> createInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp,
+    private Optional<ConfigCommand> createInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp,
             @Nonnull Operations operations) {
         if (!hasNodeAndInterface(vppEp)) {
             LOG.debug("Interface command is not created for {}", vppEp);
@@ -277,7 +326,7 @@ public class InterfaceManager implements AutoCloseable {
         return Optional.of(vhostUserCommand);
     }
 
-    private static Optional<ConfigCommand> createTapInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp,
+    private Optional<ConfigCommand> createTapInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp,
             @Nonnull Operations operation) {
         if (!hasNodeAndInterface(vppEp)) {
             LOG.debug("Interface command is not created for {}", vppEp);
@@ -303,7 +352,7 @@ public class InterfaceManager implements AutoCloseable {
         return Optional.of(tapPortCommand);
     }
 
-    private static Optional<ConfigCommand> createLoopbackWithoutBdCommand(@Nonnull VppEndpoint vppEp,
+    private Optional<ConfigCommand> createLoopbackWithoutBdCommand(@Nonnull VppEndpoint vppEp,
         @Nonnull Operations operation) {
         if (!hasNodeAndInterface(vppEp)) {
             LOG.debug("Interface command is not created for {}", vppEp);
@@ -392,7 +441,7 @@ public class InterfaceManager implements AutoCloseable {
                 .setBridgedVirtualInterface(enableBvi)
                 .build()).build();
         LOG.debug("Adding bridge domain {} to interface {}", bridgeDomainName, interfacePath);
-        final boolean transactionState = GbpNetconfTransaction.write(mountpoint, l2Iid, l2,
+        final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(mountpoint, l2Iid, l2,
                 GbpNetconfTransaction.RETRY_COUNT);
         if (transactionState) {
             LOG.debug("Adding bridge domain {} to interface {} successful", bridgeDomainName, interfacePath);
@@ -426,8 +475,8 @@ public class InterfaceManager implements AutoCloseable {
             .setBridgeDomain(bridgeDomainName)
             .setBridgedVirtualInterface(enableBvi)
             .build()).build();
-        final boolean transactionState = GbpNetconfTransaction.write(mountPoint, VppIidFactory.getL2ForInterfaceIid(ifaceKey),
-                l2, GbpNetconfTransaction.RETRY_COUNT);
+        final boolean transactionState = GbpNetconfTransaction.netconfSyncedWrite(mountPoint,
+            VppIidFactory.getL2ForInterfaceIid(ifaceKey), l2, GbpNetconfTransaction.RETRY_COUNT);
         if (transactionState) {
             LOG.debug("Adding bridge domain {} to interface {}", bridgeDomainName, VppIidFactory.getInterfaceIID(ifaceKey));
             return Futures.immediateFuture(null);
@@ -445,7 +494,7 @@ public class InterfaceManager implements AutoCloseable {
             LOG.warn("Interface already not in bridge domain {} ", ifaceKey);
             return Futures.immediateFuture(null);
         }
-        final boolean transactionState = GbpNetconfTransaction.deleteIfExists(mountPoint,
+        final boolean transactionState = GbpNetconfTransaction.netconfSyncedDelete(mountPoint,
                 VppIidFactory.getL2ForInterfaceIid(ifaceKey), GbpNetconfTransaction.RETRY_COUNT);
         if (transactionState) {
             LOG.debug("Removing bridge domain from interface {}", VppIidFactory.getInterfaceIID(ifaceKey));
@@ -523,10 +572,10 @@ public class InterfaceManager implements AutoCloseable {
                 interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build();
         LOG.debug("Deleting bridge domain from interface {}", interfacePath);
         final boolean transactionState =
-                GbpNetconfTransaction.deleteIfExists(mountpoint, l2Iid, GbpNetconfTransaction.RETRY_COUNT);
+                GbpNetconfTransaction.netconfSyncedDelete(mountpoint, l2Iid, GbpNetconfTransaction.RETRY_COUNT);
         if (transactionState) {
-            AccessListWrapper.removeAclsForInterface(mountpoint, interfaceIid.firstKeyOf(Interface.class));
             AccessListWrapper.removeAclRefFromIface(mountpoint, interfaceIid.firstKeyOf(Interface.class));
+            AccessListWrapper.removeAclsForInterface(mountpoint, interfaceIid.firstKeyOf(Interface.class));
             return vppEndpointLocationProvider.replaceLocationForEndpoint(
                     new ExternalLocationCaseBuilder().setExternalNode(null)
                         .setExternalNodeMountPoint(vppNodeIid)