summaryrefslogtreecommitdiffstats
path: root/bpmn/mso-infrastructure-bpmn/src/main
diff options
context:
space:
mode:
authorPlummer, Brittany <brittany.plummer@att.com>2020-05-28 12:57:06 -0400
committerBenjamin, Max (mb388a) <mb388a@att.com>2020-05-28 12:57:06 -0400
commitfa093d0285ed7a589d52eef3cafdb62b75810571 (patch)
treecf737e17335c8fa7a203c99dea1ddc4a35792a58 /bpmn/mso-infrastructure-bpmn/src/main
parent40a3997517147e638a7e87a8cad668db154414a7 (diff)
implement automatic migration for process
Initial setup of AutoProcessInstanceMigrationService Setup migration plan for auto migration Started adding unit tests for auto migration Setup processDefinitions as configurable list Moved autoMigrationEnabled and added default for keys Issue-ID: SO-2968 Signed-off-by: Benjamin, Max (mb388a) <mb388a@att.com> Change-Id: Ic3e4aed611e2a90a2083ffd5cd4f1a0e1b43d145
Diffstat (limited to 'bpmn/mso-infrastructure-bpmn/src/main')
-rw-r--r--bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/common/workflow/service/AutoProcessInstanceMigrationService.java110
1 files changed, 110 insertions, 0 deletions
diff --git a/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/common/workflow/service/AutoProcessInstanceMigrationService.java b/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/common/workflow/service/AutoProcessInstanceMigrationService.java
new file mode 100644
index 0000000000..fd7498f468
--- /dev/null
+++ b/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/common/workflow/service/AutoProcessInstanceMigrationService.java
@@ -0,0 +1,110 @@
+package org.onap.so.bpmn.common.workflow.service;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.camunda.bpm.engine.ProcessEngine;
+import org.camunda.bpm.engine.RepositoryService;
+import org.camunda.bpm.engine.RuntimeService;
+import org.camunda.bpm.engine.migration.MigrationInstruction;
+import org.camunda.bpm.engine.migration.MigrationPlan;
+import org.camunda.bpm.engine.migration.MigrationPlanExecutionBuilder;
+import org.camunda.bpm.engine.repository.ProcessDefinition;
+import org.camunda.bpm.engine.runtime.ProcessInstance;
+import org.camunda.bpm.engine.runtime.ProcessInstanceQuery;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.context.event.ApplicationReadyEvent;
+import org.springframework.context.event.EventListener;
+import org.springframework.core.env.Environment;
+import org.springframework.stereotype.Service;
+
+@Service
+public class AutoProcessInstanceMigrationService {
+
+ private static Logger logger = LoggerFactory.getLogger(AutoProcessInstanceMigrationService.class);
+
+ @Autowired
+ private Environment env;
+ @Autowired
+ protected ProcessEngine processEngine;
+
+ @Value("${migration.autoMigrationEnabled:false}")
+ private boolean autoMigrationEnabled;
+
+ private RuntimeService runtimeService;
+ private RepositoryService repositoryService;
+ private List<String> processDefinitionKeys;
+
+ @EventListener(ApplicationReadyEvent.class)
+ protected void executeAutoProcessInstanceMigrations() {
+ if (autoMigrationEnabled) {
+ runtimeService = processEngine.getRuntimeService();
+ repositoryService = processEngine.getRepositoryService();
+ for (ProcessDefinition definition : getProcessDefinitions()) {
+ for (ProcessDefinition procDefOld : getOldProcessDefinitions(definition.getKey(),
+ definition.getVersion())) {
+ migrate(procDefOld.getId(), definition.getId());
+ }
+ }
+ }
+ }
+
+ protected List<ProcessDefinition> getProcessDefinitions() {
+ List<ProcessDefinition> processDefinitions = new ArrayList<ProcessDefinition>();
+ processDefinitionKeys = env.getProperty("migration.processDefinitionKeys", List.class, new ArrayList<String>());
+ for (String key : processDefinitionKeys) {
+ processDefinitions.add(repositoryService.createProcessDefinitionQuery().processDefinitionKey(key)
+ .latestVersion().singleResult());
+ }
+ return processDefinitions;
+ }
+
+ private void migrate(String sourceProcessDefinitionId, String targetProcessDefinitionId) {
+ MigrationPlan migrationPlan =
+ runtimeService.createMigrationPlan(sourceProcessDefinitionId, targetProcessDefinitionId)
+ .mapEqualActivities().updateEventTriggers().build();
+ List<String> activityIds = new ArrayList<>();
+
+ for (MigrationInstruction instruction : migrationPlan.getInstructions()) {
+ activityIds.add(instruction.getSourceActivityId());
+ }
+ for (String activityId : activityIds) {
+ ProcessInstanceQuery activeProcessInstancesQuery = runtimeService.createProcessInstanceQuery()
+ .processDefinitionId(sourceProcessDefinitionId).activityIdIn(activityId).active();
+ if (!activeProcessInstancesQuery.list().isEmpty()) {
+ logger.info("Migrating {} process instance(s) from {} to {}",
+ Long.valueOf(activeProcessInstancesQuery.count()), sourceProcessDefinitionId,
+ targetProcessDefinitionId);
+ MigrationPlanExecutionBuilder migration =
+ runtimeService.newMigration(migrationPlan).processInstanceQuery(activeProcessInstancesQuery);
+ migration.executeAsync();
+ }
+ }
+ suspendEmptyProcessDefinition(sourceProcessDefinitionId);
+ }
+
+ private void suspendEmptyProcessDefinition(String sourceProcessDefinitionId) {
+ List<ProcessInstance> activeProcessInstances = runtimeService.createProcessInstanceQuery()
+ .processDefinitionId(sourceProcessDefinitionId).active().list();
+ if (activeProcessInstances.isEmpty()) {
+ repositoryService.suspendProcessDefinitionById(sourceProcessDefinitionId);
+ } else {
+ logger.info("Unable to migrate {} process instance(s) from {}",
+ Integer.valueOf(activeProcessInstances.size()), sourceProcessDefinitionId);
+ }
+ }
+
+ protected List<ProcessDefinition> getOldProcessDefinitions(String key, int version) {
+ List<ProcessDefinition> processDefinitions =
+ repositoryService.createProcessDefinitionQuery().processDefinitionKey(key).list();
+ List<ProcessDefinition> oldProcessDefinitions = new ArrayList<>();
+ for (ProcessDefinition processDef : processDefinitions) {
+ if (!processDef.isSuspended() && (processDef.getVersion() != version)) {
+ oldProcessDefinitions.add(processDef);
+ }
+ }
+ return oldProcessDefinitions;
+ }
+}