added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / camunda / workflow / handler / FailedJobIncidentHandler.java
diff --git a/otf-camunda/src/main/java/org/oran/otf/camunda/workflow/handler/FailedJobIncidentHandler.java b/otf-camunda/src/main/java/org/oran/otf/camunda/workflow/handler/FailedJobIncidentHandler.java
new file mode 100644 (file)
index 0000000..f01d550
--- /dev/null
@@ -0,0 +1,145 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
+#                                                                              #\r
+#   Licensed under the Apache License, Version 2.0 (the "License");            #\r
+#   you may not use this file except in compliance with the License.           #\r
+#   You may obtain a copy of the License at                                    #\r
+#                                                                              #\r
+#       http://www.apache.org/licenses/LICENSE-2.0                             #\r
+#                                                                              #\r
+#   Unless required by applicable law or agreed to in writing, software        #\r
+#   distributed under the License is distributed on an "AS IS" BASIS,          #\r
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
+#   See the License for the specific language governing permissions and        #\r
+#   limitations under the License.                                             #\r
+##############################################################################*/\r
+\r
+\r
+package org.oran.otf.camunda.workflow.handler;\r
+\r
+import org.oran.otf.camunda.configuration.OtfCamundaConfiguration;\r
+import org.oran.otf.camunda.exception.TestExecutionException;\r
+import org.oran.otf.camunda.model.ExecutionConstants.TestResult;\r
+import org.oran.otf.camunda.workflow.utility.WorkflowTask;\r
+import org.oran.otf.common.model.TestExecution;\r
+import org.oran.otf.common.repository.TestExecutionRepository;\r
+import org.oran.otf.common.utility.Utility;\r
+import org.oran.otf.service.impl.DeleteProcessInstanceServiceImpl;\r
+import com.google.common.base.Strings;\r
+import com.mongodb.client.result.UpdateResult;\r
+\r
+import java.util.Date;\r
+import java.util.List;\r
+\r
+import org.camunda.bpm.BpmPlatform;\r
+import org.camunda.bpm.engine.RuntimeService;\r
+import org.camunda.bpm.engine.impl.incident.IncidentContext;\r
+import org.camunda.bpm.engine.impl.incident.IncidentHandler;\r
+import org.camunda.bpm.engine.runtime.Execution;\r
+import org.camunda.bpm.engine.runtime.Incident;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.boot.context.event.ApplicationReadyEvent;\r
+import org.springframework.context.event.EventListener;\r
+import org.springframework.data.mongodb.core.MongoTemplate;\r
+import org.springframework.data.mongodb.core.query.Criteria;\r
+import org.springframework.data.mongodb.core.query.Query;\r
+import org.springframework.data.mongodb.core.query.Update;\r
+import org.springframework.stereotype.Service;\r
+\r
+@Service\r
+public class FailedJobIncidentHandler implements IncidentHandler {\r
+\r
+  private static final Logger logger = LoggerFactory.getLogger(FailedJobIncidentHandler.class);\r
+  private static final String logPrefix = Utility.getLoggerPrefix();\r
+\r
+  @Autowired\r
+  private TestExecutionRepository testExecutionRepository;\r
+  @Autowired\r
+  private MongoTemplate mongoOperation;\r
+  @Autowired\r
+  private DeleteProcessInstanceServiceImpl deleteProcessInstanceService;\r
+\r
+  @Override\r
+  public String getIncidentHandlerType() {\r
+    return Incident.FAILED_JOB_HANDLER_TYPE;\r
+  }\r
+\r
+  @Override\r
+  public Incident handleIncident(IncidentContext context, String message) {\r
+    String executionId = context.getExecutionId();\r
+    if (Strings.isNullOrEmpty(executionId)) {\r
+      return null;\r
+    }\r
+    RuntimeService runtimeService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getRuntimeService();\r
+\r
+    Execution execution = runtimeService.createExecutionQuery().executionId(executionId).singleResult();\r
+    String processInstanceId = execution.getProcessInstanceId();\r
+    TestExecution testExecution =\r
+        testExecutionRepository.findFirstByProcessInstanceId(processInstanceId).orElse(null);\r
+\r
+    if (testExecution == null) {\r
+      String error = String.format(\r
+          "%sUnable to find testExecution with processInstanceId %s. This process instance will forcefully be terminated to avoid a rogue process.",\r
+          logPrefix, processInstanceId);\r
+      logger.error(error);\r
+      deleteProcessInstanceService.deleteProcessInstanceInternal(processInstanceId, error);\r
+\r
+    } else {\r
+      if(!testExecution.getTestResult().equals(TestResult.TERMINATED)){\r
+        updateTestResult(testExecution, TestResult.WORKFLOW_ERROR, message);\r
+      }\r
+      deleteProcessInstanceService.deleteProcessInstanceInternal(processInstanceId, message);\r
+\r
+    }\r
+\r
+    List<WorkflowTask> workflowTasks =\r
+        WorkflowTask.workflowTasksByExecutionId.getOrDefault(processInstanceId, null);\r
+\r
+    if (workflowTasks != null) {\r
+      logger.debug("Forcefully terminating workflow tasks for processInstanceId: " + processInstanceId);\r
+      for (WorkflowTask workflowTask : workflowTasks) {\r
+        workflowTask.shutdown(true);\r
+      }\r
+    }\r
+\r
+    return null;\r
+  }\r
+\r
+  private void updateTestResult(TestExecution testExecution, String testResult, String testResultMessage) {\r
+    // Set the test result\r
+    testExecution.setTestResult(testResult);\r
+    testExecution.setTestResultMessage(testResultMessage);\r
+    Query query = new Query();\r
+    query.addCriteria(Criteria.where("_id").is(testExecution.get_id()));\r
+    // Also add businessKey as a criteria because the object won't be found if the business key\r
+    // was somehow modified in the workflow.\r
+    query.addCriteria(Criteria.where("businessKey").is(testExecution.getBusinessKey()));\r
+    Update update = new Update();\r
+    update.set("testResult", testExecution.getTestResult());\r
+    update.set("testResultMessage", testExecution.getTestResultMessage());\r
+    update.set("endTime", new Date(System.currentTimeMillis()));\r
+    UpdateResult result = mongoOperation.updateFirst(query, update, TestExecution.class);\r
+    // Check the status of the findAndUpdate database, and appropriately handle the errors.\r
+    if (result.getMatchedCount() == 0) {\r
+      throw new TestExecutionException(\r
+          String.format(\r
+              "Unable to log the test result because a testExecution associated with _id, %s and businessKey %s, was not found.",\r
+              testExecution.get_id(), testExecution.getBusinessKey()));\r
+    } else if (result.getModifiedCount() == 0) {\r
+      throw new TestExecutionException("Unable to persist the testExecution to the database.");\r
+    }\r
+  }\r
+\r
+  @Override\r
+  public void resolveIncident(IncidentContext context) {\r
+    //    logger.info("incident resolved");\r
+\r
+  }\r
+\r
+  @Override\r
+  public void deleteIncident(IncidentContext context) {\r
+    //    logger.info("incident deleted");\r
+\r
+  }\r
+}\r