2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Modifications Copyright (c) 2019 Samsung
 
   8  * ================================================================================
 
   9  * Licensed under the Apache License, Version 2.0 (the "License");
 
  10  * you may not use this file except in compliance with the License.
 
  11  * You may obtain a copy of the License at
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  15  * Unless required by applicable law or agreed to in writing, software
 
  16  * distributed under the License is distributed on an "AS IS" BASIS,
 
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  18  * See the License for the specific language governing permissions and
 
  19  * limitations under the License.
 
  20  * ============LICENSE_END=========================================================
 
  23 package org.onap.so.bpmn.infrastructure.workflow.tasks;
 
  25 import java.sql.Timestamp;
 
  26 import java.util.Optional;
 
  27 import org.camunda.bpm.engine.delegate.BpmnError;
 
  28 import org.camunda.bpm.engine.delegate.DelegateExecution;
 
  29 import org.onap.so.bpmn.core.WorkflowException;
 
  30 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
 
  31 import org.onap.so.constants.Status;
 
  32 import org.onap.so.db.request.beans.InfraActiveRequests;
 
  33 import org.onap.so.db.request.client.RequestsDbClient;
 
  34 import org.slf4j.Logger;
 
  35 import org.slf4j.LoggerFactory;
 
  36 import org.springframework.beans.factory.annotation.Autowired;
 
  37 import org.springframework.stereotype.Component;
 
  40 public class WorkflowActionBBFailure {
 
  42     private static final String DEACTIVATE_FABRIC_CONFIGURATION_FLOW = "DeactivateFabricConfigurationBB";
 
  43     private static final String UNASSIGN_FABRIC_CONFIGURATION_FLOW = "UnassignFabricConfigurationBB";
 
  44     private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBFailure.class);
 
  45     public static final String ROLLBACK_TARGET_STATE = "rollbackTargetState";
 
  47     private RequestsDbClient requestDbclient;
 
  49     private WorkflowAction workflowAction;
 
  51     protected void updateRequestErrorStatusMessage(DelegateExecution execution) {
 
  53             String requestId = (String) execution.getVariable("mso-request-id");
 
  54             InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
 
  56             Optional<String> errorMsgOp = retrieveErrorMessage(execution);
 
  57             if (errorMsgOp.isPresent()) {
 
  58                 errorMsg = errorMsgOp.get();
 
  60                 errorMsg = "Failed to determine error message";
 
  62             Boolean isRollback = (Boolean) execution.getVariable("isRollback");
 
  63             if (!Boolean.TRUE.equals(isRollback)) {
 
  64                 request.setStatusMessage(errorMsg);
 
  66                 request.setRollbackStatusMessage(errorMsg);
 
  68             request.setProgress(Long.valueOf(100));
 
  69             request.setLastModifiedBy("CamundaBPMN");
 
  70             request.setEndTime(new Timestamp(System.currentTimeMillis()));
 
  71             requestDbclient.updateInfraActiveRequests(request);
 
  72         } catch (Exception e) {
 
  74                     "Failed to update Request db with the status message after retry or rollback has been initialized.",
 
  79     public void updateRequestStatusToFailed(DelegateExecution execution) {
 
  81             String requestId = (String) execution.getVariable("mso-request-id");
 
  82             InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
 
  83             String rollbackErrorMsg = "";
 
  85             Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete");
 
  86             Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
 
  87             ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
 
  88             if (rollbackCompletedSuccessfully == null)
 
  89                 rollbackCompletedSuccessfully = false;
 
  91             if (isRollbackFailure == null)
 
  92                 isRollbackFailure = false;
 
  94             if (rollbackCompletedSuccessfully) {
 
  95                 rollbackErrorMsg = "Rollback has been completed successfully.";
 
  96                 request.setRollbackStatusMessage(rollbackErrorMsg);
 
  97                 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
 
  98                 String rollbackTargetState = (String) execution.getVariable(ROLLBACK_TARGET_STATE);
 
  99                 request.setRequestStatus(rollbackTargetState);
 
 100             } else if (isRollbackFailure) {
 
 101                 if (ebb != null && ebb.getBuildingBlock() != null && ebb.getBuildingBlock().getBpmnFlowName() != null) {
 
 102                     String flowName = ebb.getBuildingBlock().getBpmnFlowName();
 
 103                     if (DEACTIVATE_FABRIC_CONFIGURATION_FLOW.equalsIgnoreCase(flowName)
 
 104                             || UNASSIGN_FABRIC_CONFIGURATION_FLOW.equalsIgnoreCase(flowName)) {
 
 105                         String statusMessage = String.format(
 
 106                                 "%s Warning: The vf-module is active but configuration was not removed completely for one or more VMs.",
 
 107                                 request.getStatusMessage());
 
 108                         request.setStatusMessage(statusMessage);
 
 111                 Optional<String> rollbackErrorMsgOp = retrieveErrorMessage(execution);
 
 112                 if (rollbackErrorMsgOp.isPresent()) {
 
 113                     rollbackErrorMsg = rollbackErrorMsgOp.get();
 
 115                     rollbackErrorMsg = "Failed to determine rollback error message.";
 
 117                 request.setRollbackStatusMessage(rollbackErrorMsg);
 
 118                 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
 
 119                 request.setRequestStatus(Status.FAILED.toString());
 
 121                 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
 
 122                 if (errorMsgOp.isPresent()) {
 
 123                     errorMsg = errorMsgOp.get();
 
 125                     errorMsg = "Failed to determine error message";
 
 127                 request.setStatusMessage(errorMsg);
 
 128                 execution.setVariable("ErrorMessage", errorMsg);
 
 129                 String handlingCode = (String) execution.getVariable("handlingCode");
 
 130                 if ("Abort".equalsIgnoreCase(handlingCode)) {
 
 131                     request.setRequestStatus(Status.ABORTED.toString());
 
 133                     request.setRequestStatus(Status.FAILED.toString());
 
 136             if (ebb != null && ebb.getBuildingBlock() != null) {
 
 137                 String flowStatus = "";
 
 138                 if (rollbackCompletedSuccessfully) {
 
 139                     flowStatus = "All Rollback flows have completed successfully";
 
 141                     flowStatus = ebb.getBuildingBlock().getBpmnFlowName() + " has failed.";
 
 143                 request.setFlowStatus(flowStatus);
 
 144                 execution.setVariable("flowStatus", flowStatus);
 
 147             request.setProgress(Long.valueOf(100));
 
 148             request.setLastModifiedBy("CamundaBPMN");
 
 149             request.setEndTime(new Timestamp(System.currentTimeMillis()));
 
 150             requestDbclient.updateInfraActiveRequests(request);
 
 151         } catch (Exception e) {
 
 152             workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e);
 
 156     private Optional<String> retrieveErrorMessage(DelegateExecution execution) {
 
 157         String errorMsg = null;
 
 159             WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
 
 160             if (exception != null && (exception.getErrorMessage() != null || !"".equals(exception.getErrorMessage()))) {
 
 161                 errorMsg = exception.getErrorMessage();
 
 163             if (errorMsg == null || "".equals(errorMsg)) {
 
 164                 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
 
 166             if (errorMsg == null) {
 
 167                 throw new IllegalStateException(
 
 168                         "could not find WorkflowException or WorkflowExceptionErrorMessage in execution");
 
 170             return Optional.of(errorMsg);
 
 171         } catch (Exception ex) {
 
 172             logger.error("Failed to extract workflow exception from execution.", ex);
 
 174         return Optional.empty();
 
 177     public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
 
 178         execution.setVariable("isRollbackComplete", true);
 
 179         updateRequestStatusToFailed(execution);
 
 182     public void abortCallErrorHandling() {
 
 183         String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
 
 185         throw new BpmnError(msg);