2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.so.bpmn.infrastructure.manualhandling.tasks;
 
  24 import java.time.Duration;
 
  25 import java.util.Date;
 
  26 import java.util.HashMap;
 
  27 import org.camunda.bpm.engine.TaskService;
 
  28 import org.camunda.bpm.engine.delegate.BpmnError;
 
  29 import org.camunda.bpm.engine.delegate.DelegateTask;
 
  30 import org.camunda.bpm.engine.delegate.DelegateExecution;
 
  31 import org.onap.so.bpmn.common.BuildingBlockExecution;
 
  32 import org.onap.so.bpmn.core.WorkflowException;
 
  33 import org.onap.so.client.exception.ExceptionBuilder;
 
  34 import org.onap.so.constants.Status;
 
  35 import org.onap.so.db.request.beans.InfraActiveRequests;
 
  36 import org.onap.so.db.request.client.RequestsDbClient;
 
  37 import org.slf4j.Logger;
 
  38 import org.slf4j.LoggerFactory;
 
  39 import org.springframework.beans.factory.annotation.Autowired;
 
  40 import org.springframework.core.env.Environment;
 
  41 import org.springframework.stereotype.Component;
 
  44 public class ManualHandlingTasks {
 
  45     private static final Logger logger = LoggerFactory.getLogger(ManualHandlingTasks.class);
 
  47     private static final String TASK_TYPE_PAUSE = "pause";
 
  48     private static final String TASK_TYPE_FALLOUT = "fallout";
 
  49     public static final String VNF_TYPE = "vnfType";
 
  50     public static final String DESCRIPTION = "description";
 
  51     public static final String SERVICE_TYPE = "serviceType";
 
  52     public static final String MSO_REQUEST_ID = "mso-request-id";
 
  53     public static final String REQUESTOR_ID = "requestorId";
 
  54     public static final String ERROR_CODE = "errorCode";
 
  55     public static final String VALID_RESPONSES = "validResponses";
 
  56     public static final String TASK_TIMEOUT = "taskTimeout";
 
  57     public static final String RESPONSE_VALUE_TASK = "responseValueTask";
 
  58     public static final String RESPONSE_VALUE = "responseValue";
 
  59     private static final String ASTERISK = "*";
 
  60     private static final String WORKSTEP = "workStep";
 
  62     public static final String TASK_VARIABLE_TYPE = "type";
 
  63     public static final String TASK_VARIABLE_NFROLE = "nfRole";
 
  64     public static final String TASK_VARIABLE_SUBSCRIPTION_SERVICE_TYPE = "subscriptionServiceType";
 
  65     public static final String TASK_VARIABLE_ORIGINAL_REQUEST_ID = "originalRequestId";
 
  66     public static final String TASK_VARIABLE_ORIGINAL_REQUESTOR_ID = "originalRequestorId";
 
  67     public static final String TASK_VARIABLE_ERROR_SOURCE = "errorSource";
 
  68     public static final String TASK_VARIABLE_ERROR_CODE = "errorCode";
 
  69     public static final String TASK_VARIABLE_ERROR_MESSAGE = "errorMessage";
 
  70     public static final String TASK_VARIABLE_BUILDING_BLOCK_NAME = "buildingBlockName";
 
  71     public static final String TASK_VARIABLE_BUILDING_BLOCK_STEP = "buildingBlockStep";
 
  72     public static final String TASK_VARIABLE_DESCRIPTION = "description";
 
  73     public static final String TASK_VARIABLE_TIMEOUT = "timeout";
 
  74     public static final String TASK_VARIABLE_VALID_RESPONSES = "validResponses";
 
  76     public static final String BPMN_EXCEPTION = "BPMN exception: ";
 
  77     public static final String RAINY_DAY_SERVICE_TYPE = "rainyDayServiceType";
 
  78     public static final String RAINY_DAY_VNF_TYPE = "rainyDayVnfType";
 
  79     public static final String RAINY_DAY_VNF_NAME = "rainyDayVnfName";
 
  80     public static final String G_BUILDING_BLOCK_EXECUTION = "gBuildingBlockExecution";
 
  81     public static final String WORKFLOW_EXCEPTION = "WorkflowException";
 
  84     private ExceptionBuilder exceptionUtil;
 
  87     private RequestsDbClient requestDbclient;
 
  90     private Environment environment;
 
  93     private ExternalTicketCreation externalTicketCreation;
 
  95     protected String manualTaskTimeoutPath = "mso.rainyDay.manualTask.taskTimeout";
 
  96     protected String validResponsesPath = "mso.rainyDay.manualTask.validResponses";
 
  99     public void initRainyDayManualHandling(BuildingBlockExecution execution) {
 
 101             String manualTaskTimeout = this.environment.getProperty(manualTaskTimeoutPath);
 
 102             execution.setVariable(TASK_TIMEOUT, manualTaskTimeout);
 
 103         } catch (Exception e) {
 
 104             logger.error("Exception occurred", e);
 
 105             throw new BpmnError("Unknown error reading configuration for manual task handling");
 
 109     public void setFalloutTaskVariables(DelegateTask task) {
 
 111         DelegateExecution execution = task.getExecution();
 
 113             logger.debug("Setting fallout task variables:");
 
 114             String taskId = task.getId();
 
 115             logger.debug("taskId is: " + taskId);
 
 116             String type = TASK_TYPE_FALLOUT;
 
 117             BuildingBlockExecution gBuildingBlockExecution =
 
 118                     (BuildingBlockExecution) execution.getVariable(G_BUILDING_BLOCK_EXECUTION);
 
 119             WorkflowException workflowException = (WorkflowException) execution.getVariable(WORKFLOW_EXCEPTION);
 
 120             String nfRole = (String) execution.getVariable(RAINY_DAY_VNF_TYPE);
 
 121             logger.debug(TASK_VARIABLE_NFROLE + ": " + nfRole);
 
 122             String subscriptionServiceType = (String) execution.getVariable(RAINY_DAY_SERVICE_TYPE);
 
 123             logger.debug(TASK_VARIABLE_SUBSCRIPTION_SERVICE_TYPE + ": " + subscriptionServiceType);
 
 124             String originalRequestId = (String) execution.getVariable(MSO_REQUEST_ID);
 
 125             logger.debug(TASK_VARIABLE_ORIGINAL_REQUEST_ID + ": " + originalRequestId);
 
 126             String originalRequestorId =
 
 127                     gBuildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getRequestorId();
 
 128             logger.debug(TASK_VARIABLE_ORIGINAL_REQUESTOR_ID + ": " + originalRequestorId);
 
 129             String description = "Manual user task to handle a failure of a BB execution";
 
 130             logger.debug(TASK_VARIABLE_DESCRIPTION + ": " + description);
 
 131             String taskTimeout = (String) gBuildingBlockExecution.getVariable(TASK_TIMEOUT);
 
 132             String timeout = Date.from((new Date()).toInstant().plus(Duration.parse(taskTimeout))).toGMTString();
 
 133             logger.debug(TASK_VARIABLE_TIMEOUT + ": " + timeout);
 
 134             String errorSource = ASTERISK;
 
 135             if (workflowException != null && workflowException.getExtSystemErrorSource() != null) {
 
 136                 errorSource = workflowException.getExtSystemErrorSource().toString();
 
 138             logger.debug(TASK_VARIABLE_ERROR_SOURCE + ": " + errorSource);
 
 139             String errorCode = ASTERISK;
 
 140             if (workflowException != null) {
 
 141                 errorCode = workflowException.getErrorCode() + "";
 
 143             logger.debug(TASK_VARIABLE_ERROR_CODE + ": " + errorCode);
 
 144             String errorMessage = ASTERISK;
 
 145             if (workflowException != null) {
 
 146                 errorMessage = workflowException.getErrorMessage();
 
 148             logger.debug(TASK_VARIABLE_ERROR_MESSAGE + ": " + errorMessage);
 
 149             String buildingBlockName = gBuildingBlockExecution.getFlowToBeCalled();
 
 150             logger.debug(TASK_VARIABLE_BUILDING_BLOCK_NAME + ": " + buildingBlockName);
 
 151             String buildingBlockStep = ASTERISK;
 
 152             if (workflowException != null) {
 
 153                 buildingBlockStep = workflowException.getWorkStep();
 
 155             execution.setVariable(WORKSTEP, buildingBlockStep);
 
 156             logger.debug(TASK_VARIABLE_BUILDING_BLOCK_STEP + ": " + buildingBlockStep);
 
 157             String validResponses = this.environment.getProperty(validResponsesPath);
 
 158             logger.debug(TASK_VARIABLE_VALID_RESPONSES + ": " + validResponses);
 
 160             Map<String, String> taskVariables = new HashMap<>();
 
 161             taskVariables.put(TASK_VARIABLE_TYPE, type);
 
 162             taskVariables.put(TASK_VARIABLE_NFROLE, nfRole);
 
 163             taskVariables.put(TASK_VARIABLE_SUBSCRIPTION_SERVICE_TYPE, subscriptionServiceType);
 
 164             taskVariables.put(TASK_VARIABLE_ORIGINAL_REQUEST_ID, originalRequestId);
 
 165             taskVariables.put(TASK_VARIABLE_ORIGINAL_REQUESTOR_ID, originalRequestorId);
 
 166             taskVariables.put(TASK_VARIABLE_ERROR_SOURCE, errorSource);
 
 167             taskVariables.put(TASK_VARIABLE_ERROR_CODE, errorCode);
 
 168             taskVariables.put(TASK_VARIABLE_ERROR_MESSAGE, errorMessage);
 
 169             taskVariables.put(TASK_VARIABLE_BUILDING_BLOCK_NAME, buildingBlockName);
 
 170             taskVariables.put(TASK_VARIABLE_BUILDING_BLOCK_STEP, buildingBlockStep);
 
 171             taskVariables.put(TASK_VARIABLE_VALID_RESPONSES, validResponses);
 
 172             taskVariables.put(TASK_VARIABLE_TIMEOUT, timeout);
 
 173             taskVariables.put(TASK_VARIABLE_DESCRIPTION, description);
 
 174             TaskService taskService = execution.getProcessEngineServices().getTaskService();
 
 176             taskService.setVariables(taskId, taskVariables);
 
 177             logger.debug("successfully created fallout task: " + taskId);
 
 178         } catch (BpmnError e) {
 
 179             logger.debug(BPMN_EXCEPTION + e.getMessage());
 
 181         } catch (Exception ex) {
 
 182             String msg = "Exception in setFalloutTaskVariables " + ex.getMessage();
 
 184             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
 
 188     public void setPauseTaskVariables(DelegateTask task) {
 
 190         DelegateExecution execution = task.getExecution();
 
 193             String taskId = task.getId();
 
 194             logger.debug("taskId is: " + taskId);
 
 195             String type = TASK_TYPE_PAUSE;
 
 197             String nfRole = (String) execution.getVariable(VNF_TYPE);
 
 198             String subscriptionServiceType = (String) execution.getVariable(SERVICE_TYPE);
 
 199             String originalRequestId = (String) execution.getVariable(MSO_REQUEST_ID);
 
 200             String originalRequestorId = (String) execution.getVariable(REQUESTOR_ID);
 
 201             String description = (String) execution.getVariable(DESCRIPTION);
 
 203             String errorSource = ASTERISK;
 
 204             String errorCode = ASTERISK;
 
 205             String errorMessage = ASTERISK;
 
 206             String buildingBlockName = ASTERISK;
 
 207             String buildingBlockStep = ASTERISK;
 
 208             String validResponses = (String) execution.getVariable(VALID_RESPONSES);
 
 210             Map<String, String> taskVariables = new HashMap<>();
 
 211             taskVariables.put(TASK_VARIABLE_TYPE, type);
 
 212             taskVariables.put(TASK_VARIABLE_NFROLE, nfRole);
 
 213             taskVariables.put(TASK_VARIABLE_DESCRIPTION, description);
 
 214             taskVariables.put(TASK_VARIABLE_TIMEOUT, timeout);
 
 215             taskVariables.put(TASK_VARIABLE_SUBSCRIPTION_SERVICE_TYPE, subscriptionServiceType);
 
 216             taskVariables.put(TASK_VARIABLE_ORIGINAL_REQUEST_ID, originalRequestId);
 
 217             taskVariables.put(TASK_VARIABLE_ORIGINAL_REQUESTOR_ID, originalRequestorId);
 
 218             taskVariables.put(TASK_VARIABLE_ERROR_SOURCE, errorSource);
 
 219             taskVariables.put(TASK_VARIABLE_ERROR_CODE, errorCode);
 
 220             taskVariables.put(TASK_VARIABLE_ERROR_MESSAGE, errorMessage);
 
 221             taskVariables.put(TASK_VARIABLE_BUILDING_BLOCK_NAME, buildingBlockName);
 
 222             taskVariables.put(TASK_VARIABLE_BUILDING_BLOCK_STEP, buildingBlockStep);
 
 223             taskVariables.put(TASK_VARIABLE_VALID_RESPONSES, validResponses);
 
 224             TaskService taskService = execution.getProcessEngineServices().getTaskService();
 
 226             taskService.setVariables(taskId, taskVariables);
 
 227             logger.debug("successfully created pause task: " + taskId);
 
 228         } catch (BpmnError e) {
 
 229             logger.debug(BPMN_EXCEPTION + e.getMessage());
 
 231         } catch (Exception ex) {
 
 232             String msg = "Exception in setPauseTaskVariables " + ex.getMessage();
 
 234             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
 
 239     public void completeTask(DelegateTask task) {
 
 241         DelegateExecution execution = task.getExecution();
 
 245             String taskId = task.getId();
 
 246             logger.debug("taskId is: " + taskId);
 
 247             TaskService taskService = execution.getProcessEngineServices().getTaskService();
 
 249             Map<String, Object> taskVariables = taskService.getVariables(taskId);
 
 250             String responseValue = (String) taskVariables.get(RESPONSE_VALUE);
 
 252             logger.debug("Received responseValue on completion: " + responseValue);
 
 253             // Have to set the first letter of the response to upper case
 
 254             String responseValueUppercaseStart =
 
 255                     responseValue.substring(0, 1).toUpperCase() + responseValue.substring(1);
 
 256             logger.debug("ResponseValue to taskListener: " + responseValueUppercaseStart);
 
 257             execution.setVariable(RESPONSE_VALUE_TASK, responseValueUppercaseStart);
 
 259         } catch (BpmnError e) {
 
 260             logger.debug(BPMN_EXCEPTION + e.getMessage());
 
 262         } catch (Exception ex) {
 
 263             String msg = "Exception in completeManualTask " + ex.getMessage();
 
 265             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
 
 270     public void createExternalTicket(BuildingBlockExecution execution) {
 
 271         externalTicketCreation.createExternalTicket(execution);
 
 274     public void updateRequestDbStatus(BuildingBlockExecution execution, String status) {
 
 276             String requestId = (String) execution.getVariable(MSO_REQUEST_ID);
 
 277             InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
 
 279             if (status.equalsIgnoreCase(Status.TIMEOUT.name())) {
 
 280                 execution.setVariable(RESPONSE_VALUE_TASK, "Timeout");
 
 282             request.setRequestStatus(status);
 
 283             request.setLastModifiedBy("ManualHandling");
 
 285             requestDbclient.updateInfraActiveRequests(request);
 
 286         } catch (Exception e) {
 
 287             logger.error("Unable to save the updated request status to the DB", e);