Turn down OLM power AtoZ and ZtoA in parallel
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / provisiondevice / RendererServiceOperationsImpl.java
index 486b321120021d6ead5d672b509f683df3e76195..0fdf79e0493878256a95c632da61127a0014fec9 100644 (file)
@@ -11,6 +11,7 @@ import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.ListeningExecutorService;
 import com.google.common.util.concurrent.MoreExecutors;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -23,7 +24,6 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.stream.Collectors;
 import org.opendaylight.mdsal.binding.api.DataBroker;
-import org.opendaylight.mdsal.binding.api.NotificationPublishService;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.transportpce.common.ResponseCodes;
 import org.opendaylight.transportpce.common.StringConstants;
@@ -32,11 +32,13 @@ import org.opendaylight.transportpce.common.mapping.PortMapping;
 import org.opendaylight.transportpce.common.service.ServiceTypes;
 import org.opendaylight.transportpce.renderer.ModelMappingUtils;
 import org.opendaylight.transportpce.renderer.ServicePathInputData;
+import org.opendaylight.transportpce.renderer.provisiondevice.notification.Notification;
 import org.opendaylight.transportpce.renderer.provisiondevice.servicepath.ServicePathDirection;
 import org.opendaylight.transportpce.renderer.provisiondevice.tasks.DeviceRenderingRollbackTask;
 import org.opendaylight.transportpce.renderer.provisiondevice.tasks.DeviceRenderingTask;
 import org.opendaylight.transportpce.renderer.provisiondevice.tasks.OlmPowerSetupRollbackTask;
 import org.opendaylight.transportpce.renderer.provisiondevice.tasks.OlmPowerSetupTask;
+import org.opendaylight.transportpce.renderer.provisiondevice.tasks.OlmPowerTurnDownTask;
 import org.opendaylight.transportpce.renderer.provisiondevice.tasks.OtnDeviceRenderingTask;
 import org.opendaylight.transportpce.renderer.provisiondevice.tasks.RollbackProcessor;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.Action;
@@ -45,12 +47,8 @@ import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkut
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmInputBuilder;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutput;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupInput;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownInputBuilder;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutput;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.TransportpceOlmService;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.get.pm.output.Measurements;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.RendererRpcResultSp;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.RendererRpcResultSpBuilder;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceDeleteInput;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceDeleteOutput;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceImplementationRequestInput;
@@ -62,7 +60,7 @@ import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.types.rev191129.NodeTypes;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.types.rev161014.ResourceTypeEnum;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.format.rev191129.ServiceFormat;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.list.Services;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.list.Services;
 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.PathDescription;
 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.RpcStatusEx;
 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.ServicePathNotificationTypes;
@@ -74,7 +72,6 @@ import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926
 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.olm.get.pm.input.ResourceIdentifierBuilder;
 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.optical.renderer.nodes.Nodes;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.Notification;
 import org.opendaylight.yangtools.yang.common.Uint32;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
@@ -93,8 +90,8 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
             "OLM power setup was not successful! Rendering and OLM will be rolled back.";
     private static final String RENDERING_DEVICES_A_Z_MSG = "Rendering devices A-Z";
     private static final String RENDERING_DEVICES_Z_A_MSG = "Rendering device Z-A";
-    private static final String TURNING_DOWN_POWER_ON_A_TO_Z_PATH_MSG = "Turning down power on A-to-Z path";
-    private static final String FAILED = "Failed";
+    private static final String ATOZPATH = "A-to-Z";
+    private static final String ZTOAPATH = "Z-to-A";
     private static final String OPERATION_FAILED = "Operation Failed";
     private static final String OPERATION_SUCCESSFUL = "Operation Successful";
     private static final int NUMBER_OF_THREADS = 4;
@@ -103,7 +100,7 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
     private final OtnDeviceRendererService otnDeviceRenderer;
     private final TransportpceOlmService olmService;
     private final DataBroker dataBroker;
-    private final NotificationPublishService notificationPublishService;
+    private final Notification notification;
     private final PortMapping portMapping;
     private ListeningExecutorService executor;
 
@@ -112,13 +109,13 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
             @Reference OtnDeviceRendererService otnDeviceRenderer,
             @Reference TransportpceOlmService olmService,
             @Reference DataBroker dataBroker,
-            @Reference NotificationPublishService notificationPublishService,
+            @Reference Notification notification,
             @Reference PortMapping portMapping) {
         this.deviceRenderer = deviceRenderer;
         this.otnDeviceRenderer = otnDeviceRenderer;
         this.olmService = olmService;
         this.dataBroker = dataBroker;
-        this.notificationPublishService = notificationPublishService;
+        this.notification = notification;
         this.portMapping = portMapping;
         this.executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUMBER_OF_THREADS));
         LOG.debug("RendererServiceOperationsImpl instantiated");
@@ -140,7 +137,7 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
                     "Service compliant, submitting service implementation Request ...");
                 Uint32 serviceRate = getServiceRate(input);
                 LOG.info("Using {}G rate", serviceRate);
-                org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922
+                org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221
                         .network.Nodes mappingNode =
                     portMapping.isNodeExist(input.getServiceAEnd().getNodeId())
                         ? portMapping.getNode(input.getServiceAEnd().getNodeId())
@@ -273,7 +270,7 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
         });
     }
 
-    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
+    @SuppressFBWarnings(
         value = "UPM_UNCALLED_PRIVATE_METHOD",
         justification = "call in call() method")
     private Uint32 getServiceRate(ServiceImplementationRequestInput input) {
@@ -317,20 +314,7 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
             .get(serviceName);
     }
 
-    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
-            value = "UPM_UNCALLED_PRIVATE_METHOD",
-            justification = "call in call() method")
-    private ServicePowerTurndownOutput olmPowerTurndown(ServicePathInputData servicePathInputData)
-            throws InterruptedException, ExecutionException, TimeoutException {
-        LOG.debug(TURNING_DOWN_POWER_ON_A_TO_Z_PATH_MSG);
-        return this.olmService
-            .servicePowerTurndown(
-                new ServicePowerTurndownInputBuilder(servicePathInputData.getServicePathInput()).build())
-            .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS)
-            .getResult();
-    }
-
-    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
+    @SuppressFBWarnings(
             value = "UPM_UNCALLED_PRIVATE_METHOD",
             justification = "call in call() method")
     private Optional<org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118
@@ -353,7 +337,7 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
         }
     }
 
-    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
+    @SuppressFBWarnings(
             value = "UPM_UNCALLED_PRIVATE_METHOD",
             justification = "call in call() method")
     private List<DeviceRenderingResult> deviceRendering(
@@ -415,7 +399,7 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
         return renderingResults;
     }
 
-    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
+    @SuppressFBWarnings(
         value = "UPM_UNCALLED_PRIVATE_METHOD",
         justification = "call in call() method")
     private List<OtnDeviceRenderingResult> otnDeviceRendering(
@@ -473,7 +457,7 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
         return otnRenderingResults;
     }
 
-    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
+    @SuppressFBWarnings(
             value = "UPM_UNCALLED_PRIVATE_METHOD",
             justification = "call in call() method")
     private void olmPowerSetup(
@@ -540,7 +524,7 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
                         powerSetupInputZtoA));
     }
 
-    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
+    @SuppressFBWarnings(
             value = "UPM_UNCALLED_PRIVATE_METHOD",
             justification = "call in call() method")
     private boolean isServiceActivated(String nodeId, String tpId) {
@@ -623,7 +607,7 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
         return result <= threshold;
     }
 
-    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
+    @SuppressFBWarnings(
         value = "UPM_UNCALLED_PRIVATE_METHOD",
         justification = "call in call() method")
     private boolean manageServicePathCreation(ServiceImplementationRequestInput input, String serviceType,
@@ -703,7 +687,7 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
         return true;
     }
 
-    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
+    @SuppressFBWarnings(
         value = "UPM_UNCALLED_PRIVATE_METHOD",
         justification = "call in call() method")
     private boolean manageServicePathDeletion(String serviceName, PathDescription pathDescription, String serviceType)
@@ -712,48 +696,48 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
             ModelMappingUtils.rendererCreateServiceInputAToZ(serviceName, pathDescription, Action.Delete);
         ServicePathInputData servicePathInputDataZtoA =
             ModelMappingUtils.rendererCreateServiceInputZToA(serviceName, pathDescription, Action.Delete);
-        // OLM turn down power
-        try {
-            LOG.debug(TURNING_DOWN_POWER_ON_A_TO_Z_PATH_MSG);
-            sendNotifications(
-                ServicePathNotificationTypes.ServiceDelete,
-                serviceName,
-                RpcStatusEx.Pending,
-                TURNING_DOWN_POWER_ON_A_TO_Z_PATH_MSG);
-            // TODO add some flag rather than string
-            if (FAILED.equals(
-                    olmPowerTurndown(servicePathInputDataAtoZ)
-                        .getResult())) {
-                LOG.error("Service power turndown failed on A-to-Z path for service {}!", serviceName);
-                sendNotifications(
-                    ServicePathNotificationTypes.ServiceDelete,
+
+        ListenableFuture<OLMRenderingResult> olmPowerTurnDownFutureAtoZ =
+            this.executor.submit(
+                new OlmPowerTurnDownTask(
                     serviceName,
-                    RpcStatusEx.Failed,
-                    "Service power turndown failed on A-to-Z path for service");
-                return false;
-            }
-            LOG.debug("Turning down power on Z-to-A path");
-            sendNotifications(
-                ServicePathNotificationTypes.ServiceDelete,
-                serviceName,
-                RpcStatusEx.Pending,
-                "Turning down power on Z-to-A path");
-            // TODO add some flag rather than string
-            if (FAILED.equals(
-                    olmPowerTurndown(servicePathInputDataZtoA)
-                        .getResult())) {
-                LOG.error("Service power turndown failed on Z-to-A path for service {}!", serviceName);
-                sendNotifications(
-                    ServicePathNotificationTypes.ServiceDelete,
+                    ATOZPATH,
+                    olmService,
+                    servicePathInputDataAtoZ,
+                    notification
+                )
+            );
+
+        ListenableFuture<OLMRenderingResult> olmPowerTurnDownFutureZtoA =
+            this.executor.submit(
+                new OlmPowerTurnDownTask(
                     serviceName,
-                    RpcStatusEx.Failed,
-                    "Service power turndown failed on Z-to-A path for service");
-                return false;
-            }
+                    ZTOAPATH,
+                    olmService,
+                    servicePathInputDataZtoA,
+                    notification
+                )
+            );
+
+        ListenableFuture<List<OLMRenderingResult>> olmPowerTurnDownFutures =
+            Futures.allAsList(olmPowerTurnDownFutureAtoZ, olmPowerTurnDownFutureZtoA);
+
+        List<OLMRenderingResult> olmRenderingResults;
+        // OLM turn down power
+        try {
+            LOG.info("Waiting for A-Z and Z-A OLM power turn down ...");
+            olmRenderingResults = olmPowerTurnDownFutures.get(
+                Timeouts.OLM_TIMEOUT, TimeUnit.MILLISECONDS
+            );
         } catch (InterruptedException | ExecutionException | TimeoutException e) {
             LOG.error("Error while turning down power!", e);
             return false;
         }
+        if (!olmRenderingResults.get(0).isSuccess() || !olmRenderingResults.get(1).isSuccess()) {
+            LOG.error("Error while turning down power!");
+            return false;
+        }
+        LOG.info("OLM power successfully turned down!");
         // delete service path with renderer
         LOG.info("Deleting service path via renderer");
         sendNotifications(
@@ -780,7 +764,7 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
         return true;
     }
 
-    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
+    @SuppressFBWarnings(
         value = "UPM_UNCALLED_PRIVATE_METHOD",
         justification = "call in call() method")
     private boolean manageOtnServicePathCreation(
@@ -836,7 +820,7 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
         return true;
     }
 
-    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
+    @SuppressFBWarnings(
         value = "UPM_UNCALLED_PRIVATE_METHOD",
         justification = "call in call() method")
     private boolean manageOtnServicePathDeletion(
@@ -901,9 +885,13 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
             String serviceName,
             RpcStatusEx rpcStatusEx,
             String message) {
-        send(
-            buildNotification(servicePathNotificationTypes, serviceName, rpcStatusEx, message,
-                null, null, null, null));
+
+        notification.send(
+            servicePathNotificationTypes,
+            serviceName,
+            rpcStatusEx,
+            message
+        );
     }
 
     /**
@@ -923,60 +911,19 @@ public class RendererServiceOperationsImpl implements RendererServiceOperations
             Link notifLink,
             Set<String> supportedLinks,
             String serviceType) {
-        send(
-            buildNotification(servicePathNotificationTypes, serviceName, rpcStatusEx, message,
-                pathDescription, notifLink, supportedLinks, serviceType));
-    }
 
-    /**
-     * Build notification containing path description information.
-     * @param servicePathNotificationTypes ServicePathNotificationTypes
-     * @param serviceName String
-     * @param rpcStatusEx RpcStatusEx
-     * @param message String
-     * @param pathDescription PathDescription
-     * @return notification with RendererRpcResultSp type.
-     */
-    private RendererRpcResultSp buildNotification(
-            ServicePathNotificationTypes servicePathNotificationTypes,
-            String serviceName,
-            RpcStatusEx rpcStatusEx,
-            String message,
-            PathDescription pathDescription,
-            Link notifLink,
-            Set<String> supportedLinks,
-            String serviceType) {
-        RendererRpcResultSpBuilder builder =
-            new RendererRpcResultSpBuilder()
-                .setNotificationType(servicePathNotificationTypes).setServiceName(serviceName).setStatus(rpcStatusEx)
-                .setStatusMessage(message)
-                .setServiceType(serviceType);
-        if (pathDescription != null) {
-            builder
-                .setAToZDirection(pathDescription.getAToZDirection())
-                .setZToADirection(pathDescription.getZToADirection());
-        }
-        if (notifLink != null) {
-            builder.setLink(notifLink);
-        }
-        if (supportedLinks != null) {
-            builder.setLinkId(supportedLinks);
-        }
-        return builder.build();
-    }
-
-    /**
-     * Send renderer notification.
-     * @param notification Notification
-     */
-    private void send(Notification<?> notification) {
-        try {
-            LOG.info("Sending notification {}", notification);
-            notificationPublishService.putNotification(notification);
-        } catch (InterruptedException e) {
-            LOG.info("notification offer rejected: ", e);
-            Thread.currentThread().interrupt();
-        }
+        notification.send(
+            notification.buildNotification(
+                servicePathNotificationTypes,
+                serviceName,
+                rpcStatusEx,
+                message,
+                pathDescription,
+                notifLink,
+                supportedLinks,
+                serviceType
+            )
+        );
     }
 
     private Link createLinkForNotif(List<LinkTp> otnLinkTerminationPoints) {