diff options
Diffstat (limited to 'bpmn/mso-infrastructure-bpmn')
7 files changed, 263 insertions, 18 deletions
diff --git a/bpmn/mso-infrastructure-bpmn/.gitignore b/bpmn/mso-infrastructure-bpmn/.gitignore new file mode 100644 index 0000000000..da7560e07f --- /dev/null +++ b/bpmn/mso-infrastructure-bpmn/.gitignore @@ -0,0 +1 @@ +/.apt_generated_tests/ diff --git a/bpmn/mso-infrastructure-bpmn/pom.xml b/bpmn/mso-infrastructure-bpmn/pom.xml index a3d0dd50b3..b621853f2b 100644 --- a/bpmn/mso-infrastructure-bpmn/pom.xml +++ b/bpmn/mso-infrastructure-bpmn/pom.xml @@ -3,7 +3,7 @@ <parent> <groupId>org.onap.so</groupId> <artifactId>bpmn</artifactId> - <version>1.6.0-SNAPSHOT</version> + <version>1.7.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>mso-infrastructure-bpmn</artifactId> @@ -131,9 +131,7 @@ </execution> <execution> <id>original</id> - <configuration> - <skip>false</skip> - </configuration> + <phase>package</phase> </execution> </executions> </plugin> @@ -158,6 +156,9 @@ <executions> <execution> <id>extract-docker-file</id> + <configuration> + <skip>false</skip> + </configuration> </execution> </executions> </plugin> @@ -335,12 +336,16 @@ <dependency> <groupId>org.onap.aaf.authz</groupId> <artifactId>aaf-cadi-aaf</artifactId> - <version>2.1.9</version> + <version>${aaf.cadi.version}</version> <exclusions> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> + <exclusion> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + </exclusion> </exclusions> </dependency> </dependencies> 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..58d0c6d374 --- /dev/null +++ b/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/common/workflow/service/AutoProcessInstanceMigrationService.java @@ -0,0 +1,111 @@ +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; + } +} diff --git a/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/common/workflow/service/WorkflowAsyncResource.java b/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/common/workflow/service/WorkflowAsyncResource.java index 4fb63651eb..2d41eb4e26 100644 --- a/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/common/workflow/service/WorkflowAsyncResource.java +++ b/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/common/workflow/service/WorkflowAsyncResource.java @@ -45,6 +45,7 @@ import org.slf4j.LoggerFactory; import org.slf4j.MDC; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.springframework.core.env.Environment; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -68,6 +69,7 @@ public class WorkflowAsyncResource extends ProcessEngineAwareService { private static final WorkflowContextHolder contextHolder = WorkflowContextHolder.getInstance(); long workflowPollInterval = 1000; + private static final String ASYNC_WAIT_TIME = "mso.workflow.async.waitTime"; @Autowired private WorkflowProcessor processor; @@ -75,6 +77,9 @@ public class WorkflowAsyncResource extends ProcessEngineAwareService { @Autowired private WorkflowContextHolder workflowContext; + @Autowired + private Environment env; + public void setProcessor(WorkflowProcessor processor) { this.processor = processor; } @@ -119,7 +124,7 @@ public class WorkflowAsyncResource extends ProcessEngineAwareService { protected WorkflowResponse waitForResponse(Map<String, Object> inputVariables) throws Exception { String requestId = getRequestId(inputVariables); long currentWaitTime = 0; - long waitTime = getWaitTime(inputVariables); + long waitTime = getWaitTime(); logger.debug("WorkflowAsyncResource.waitForResponse using timeout: " + waitTime); while (waitTime > currentWaitTime) { Thread.sleep(workflowPollInterval); @@ -185,18 +190,8 @@ public class WorkflowAsyncResource extends ProcessEngineAwareService { * @param inputVariables * @return */ - private long getWaitTime(Map<String, Object> inputVariables) { - String timeout = inputVariables.get("mso-service-request-timeout") == null ? null - : inputVariables.get("mso-service-request-timeout").toString(); - - if (timeout != null) { - try { - return Long.parseLong(timeout) * 1000; - } catch (NumberFormatException nex) { - logger.debug("Invalid input for mso-service-request-timeout"); - } - } - return DEFAULT_WAIT_TIME; + private long getWaitTime() { + return env.getProperty(ASYNC_WAIT_TIME, Long.class, new Long(DEFAULT_WAIT_TIME)); } } diff --git a/bpmn/mso-infrastructure-bpmn/src/main/resources/application.yaml b/bpmn/mso-infrastructure-bpmn/src/main/resources/application.yaml index 1ad95b3ab9..e08cf0f578 100644 --- a/bpmn/mso-infrastructure-bpmn/src/main/resources/application.yaml +++ b/bpmn/mso-infrastructure-bpmn/src/main/resources/application.yaml @@ -5,6 +5,7 @@ server: mso: infra: auditInventory: false + camundaAuth: AE2E9BE6EF9249085AF98689C4EE087736A5500629A72F35068FFB88813A023581DD6E765071F1C04075B36EA4213A spring: datasource: hikari: @@ -42,3 +43,9 @@ management: prometheus: enabled: true # Whether exporting of metrics to Prometheus is enabled. step: 1m # Step size (i.e. reporting frequency) to use. +org: + onap: + so: + adapters: + network: + encryptionKey: 07a7159d3bf51a0e53be7a8f89699be7 diff --git a/bpmn/mso-infrastructure-bpmn/src/test/java/org/onap/so/bpmn/common/OofHomingIT.java b/bpmn/mso-infrastructure-bpmn/src/test/java/org/onap/so/bpmn/common/OofHomingIT.java index c128f58103..089d034efc 100644 --- a/bpmn/mso-infrastructure-bpmn/src/test/java/org/onap/so/bpmn/common/OofHomingIT.java +++ b/bpmn/mso-infrastructure-bpmn/src/test/java/org/onap/so/bpmn/common/OofHomingIT.java @@ -20,6 +20,7 @@ package org.onap.so.bpmn.common; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.onap.so.bpmn.mock.StubResponseDatabase.MockGetServiceResourcesCatalogDataByModelUuid; @@ -530,6 +531,7 @@ public class OofHomingIT extends BaseIntegrationTest { .contains("WorkflowException[processKey=Homing,errorCode=400,errorMessage=OOF Async Callback " + "Response contains error: Unable to find any candidate for demand *** Response:"); assert (errorMatch); + assertNotNull(businessKey); } @Test diff --git a/bpmn/mso-infrastructure-bpmn/src/test/java/org/onap/so/bpmn/common/workflow/service/AutoProcessInstanceMigrationServiceTest.java b/bpmn/mso-infrastructure-bpmn/src/test/java/org/onap/so/bpmn/common/workflow/service/AutoProcessInstanceMigrationServiceTest.java new file mode 100644 index 0000000000..36e828448d --- /dev/null +++ b/bpmn/mso-infrastructure-bpmn/src/test/java/org/onap/so/bpmn/common/workflow/service/AutoProcessInstanceMigrationServiceTest.java @@ -0,0 +1,124 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.so.bpmn.common.workflow.service; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doReturn; +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.repository.ProcessDefinition; +import org.camunda.bpm.engine.repository.ProcessDefinitionQuery; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.core.env.Environment; + +@RunWith(MockitoJUnitRunner.class) +public class AutoProcessInstanceMigrationServiceTest { + + @Mock + private ProcessEngine processEngine; + + @Mock + private ProcessDefinition outdated; + + @Mock + private ProcessDefinition newDef; + + @Mock + private ProcessDefinition key; + + @Mock + private ProcessDefinition testKey; + + @Mock + private ProcessDefinition suspendedDef; + + @Mock + private RepositoryService repositoryService; + + @Mock + private ProcessDefinitionQuery query; + + @Mock + private ProcessDefinitionQuery keyQuery; + + @Mock + private Environment env; + + @Spy + @InjectMocks + private AutoProcessInstanceMigrationService migrationService; + + + @Test + public void getOldProcessDefinitionsTest() { + List<ProcessDefinition> expectedList = new ArrayList<>(); + expectedList.add(outdated); + + List<ProcessDefinition> defList = new ArrayList<>(); + defList.add(outdated); + defList.add(newDef); + defList.add(suspendedDef); + + + doReturn(query).when(repositoryService).createProcessDefinitionQuery(); + doReturn(query).when(query).processDefinitionKey("test"); + doReturn(defList).when(query).list(); + doReturn(3).when(outdated).getVersion(); + doReturn(4).when(newDef).getVersion(); + doReturn(true).when(suspendedDef).isSuspended(); + List<ProcessDefinition> outdatedList = migrationService.getOldProcessDefinitions("test", 4); + + assertEquals(expectedList, outdatedList); + } + + @Test + public void getProcessDefinitionsTest() { + List<ProcessDefinition> expected = new ArrayList<ProcessDefinition>(); + expected.add(testKey); + expected.add(key); + + List<String> processDefinitionKeys = new ArrayList<String>(); + processDefinitionKeys.add("testKey"); + processDefinitionKeys.add("key"); + + doReturn(processDefinitionKeys).when(env).getProperty("migration.processDefinitionKeys", List.class, + new ArrayList<String>()); + + doReturn(query).when(repositoryService).createProcessDefinitionQuery(); + doReturn(query).when(query).processDefinitionKey("testKey"); + doReturn(query).when(query).latestVersion(); + doReturn(testKey).when(query).singleResult(); + + doReturn(keyQuery).when(query).processDefinitionKey("key"); + doReturn(keyQuery).when(keyQuery).latestVersion(); + doReturn(key).when(keyQuery).singleResult(); + + List<ProcessDefinition> actualProcessDefinitions = migrationService.getProcessDefinitions(); + + assertEquals(expected, actualProcessDefinitions); + } +} |