diff options
6 files changed, 95 insertions, 8 deletions
diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditStackService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditStackService.java index fb7e925d88..7cc7c02f23 100644 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditStackService.java +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/AuditStackService.java @@ -34,12 +34,15 @@ import org.springframework.stereotype.Component; @Profile("!test") public class AuditStackService { + private static final String MSO_AUDIT_LOCK_TIME = "mso.audit.lock-time"; + private static final Logger logger = LoggerFactory.getLogger(AuditStackService.class); private static final String DEFAULT_AUDIT_LOCK_TIME = "60000"; private static final String DEFAULT_MAX_CLIENTS_FOR_TOPIC = "10"; + @Autowired public Environment env; @@ -60,7 +63,7 @@ public class AuditStackService { for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); client.subscribe("InventoryAddAudit") - .lockDuration(Long.parseLong(env.getProperty("mso.audit.lock-time", DEFAULT_AUDIT_LOCK_TIME))) + .lockDuration(Long.parseLong(env.getProperty(MSO_AUDIT_LOCK_TIME, DEFAULT_AUDIT_LOCK_TIME))) .handler(auditCreateStack::executeExternalTask).open(); } } @@ -70,7 +73,7 @@ public class AuditStackService { for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); client.subscribe("InventoryDeleteAudit") - .lockDuration(Long.parseLong(env.getProperty("mso.audit.lock-time", DEFAULT_AUDIT_LOCK_TIME))) + .lockDuration(Long.parseLong(env.getProperty(MSO_AUDIT_LOCK_TIME, DEFAULT_AUDIT_LOCK_TIME))) .handler(auditDeleteStack::executeExternalTask).open(); } } @@ -80,7 +83,7 @@ public class AuditStackService { for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); client.subscribe("InventoryQueryAudit") - .lockDuration(Long.parseLong(env.getProperty("mso.audit.lock-time", DEFAULT_AUDIT_LOCK_TIME))) + .lockDuration(Long.parseLong(env.getProperty(MSO_AUDIT_LOCK_TIME, DEFAULT_AUDIT_LOCK_TIME))) .handler(auditQueryStack::executeExternalTask).open(); } } diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryService.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryService.java index 5261f8ae19..4ab4d14dac 100644 --- a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryService.java +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/inventory/create/CreateInventoryService.java @@ -43,7 +43,6 @@ public class CreateInventoryService { @PostConstruct public void auditAAIInventory() throws Exception { - ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); client.subscribe("InventoryCreate") .lockDuration(Long.parseLong(env.getProperty("mso.audit.lock-time", "60000"))) diff --git a/adapters/so-appc-orchestrator/src/main/java/org/onap/so/adapters/appc/orchestrator/AppcOrchestratorApplication.java b/adapters/so-appc-orchestrator/src/main/java/org/onap/so/adapters/appc/orchestrator/AppcOrchestratorApplication.java index 258c95ac89..0230d7f518 100644 --- a/adapters/so-appc-orchestrator/src/main/java/org/onap/so/adapters/appc/orchestrator/AppcOrchestratorApplication.java +++ b/adapters/so-appc-orchestrator/src/main/java/org/onap/so/adapters/appc/orchestrator/AppcOrchestratorApplication.java @@ -22,6 +22,7 @@ package org.onap.so.adapters.appc.orchestrator; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; /** * @since Version 1.0 @@ -29,6 +30,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; */ @SpringBootApplication(scanBasePackages = {"org.onap"}) +@EnableScheduling public class AppcOrchestratorApplication { private static final String LOGS_DIR = "logs_dir"; diff --git a/adapters/so-appc-orchestrator/src/main/java/org/onap/so/adapters/appc/orchestrator/service/ApplicationControllerTaskService.java b/adapters/so-appc-orchestrator/src/main/java/org/onap/so/adapters/appc/orchestrator/service/ApplicationControllerTaskService.java index 2a33124eac..3484e0992f 100644 --- a/adapters/so-appc-orchestrator/src/main/java/org/onap/so/adapters/appc/orchestrator/service/ApplicationControllerTaskService.java +++ b/adapters/so-appc-orchestrator/src/main/java/org/onap/so/adapters/appc/orchestrator/service/ApplicationControllerTaskService.java @@ -1,6 +1,7 @@ package org.onap.so.adapters.appc.orchestrator.service; import javax.annotation.PostConstruct; +import org.camunda.bpm.client.ExternalTaskClient; import org.onap.so.utils.ExternalTaskServiceUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; @@ -23,8 +24,9 @@ public class ApplicationControllerTaskService { @PostConstruct public void appcOrchestrator() throws Exception { for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { - externalTaskServiceUtils.createExternalTaskClient().subscribe("AppcService").lockDuration(604800000) - .handler(appcOrchestrator::executeExternalTask).open(); + ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); + client.subscribe("AppcService").lockDuration(604800000).handler(appcOrchestrator::executeExternalTask) + .open(); } } diff --git a/common/src/main/java/org/onap/so/utils/ExternalTaskServiceUtils.java b/common/src/main/java/org/onap/so/utils/ExternalTaskServiceUtils.java index e43b431821..4f13cec8f4 100644 --- a/common/src/main/java/org/onap/so/utils/ExternalTaskServiceUtils.java +++ b/common/src/main/java/org/onap/so/utils/ExternalTaskServiceUtils.java @@ -1,6 +1,8 @@ package org.onap.so.utils; import java.security.GeneralSecurityException; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import org.camunda.bpm.client.ExternalTaskClient; import org.camunda.bpm.client.interceptor.ClientRequestInterceptor; import org.camunda.bpm.client.interceptor.auth.BasicAuthProvider; @@ -8,21 +10,30 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; + + @Component public class ExternalTaskServiceUtils { @Autowired public Environment env; + protected Set<ExternalTaskClient> taskClients = ConcurrentHashMap.newKeySet(); + + private static final Logger logger = LoggerFactory.getLogger(ExternalTaskServiceUtils.class); public ExternalTaskClient createExternalTaskClient() throws Exception { String auth = getAuth(); ClientRequestInterceptor interceptor = createClientInterceptor(auth); - return ExternalTaskClient.create().baseUrl(env.getRequiredProperty("mso.workflow.endpoint")).maxTasks(1) - .addInterceptor(interceptor).asyncResponseTimeout(120000).build(); + ExternalTaskClient client = + ExternalTaskClient.create().baseUrl(env.getRequiredProperty("mso.workflow.endpoint")).maxTasks(1) + .addInterceptor(interceptor).asyncResponseTimeout(120000).build(); + taskClients.add(client); + return client; } protected ClientRequestInterceptor createClientInterceptor(String auth) { @@ -42,5 +53,13 @@ public class ExternalTaskServiceUtils { return Integer.parseInt(env.getProperty("workflow.topics.maxClients", "3")); } + @Scheduled(fixedDelay = 30000) + public void checkAllClientsActive() { + getClients().stream().filter(client -> !client.isActive()).forEach(ExternalTaskClient::start); + } + + protected Set<ExternalTaskClient> getClients() { + return taskClients; + } } diff --git a/common/src/test/java/org/onap/so/utils/ExternalTaskServiceUtilsTest.java b/common/src/test/java/org/onap/so/utils/ExternalTaskServiceUtilsTest.java new file mode 100644 index 0000000000..c9fc27ebe0 --- /dev/null +++ b/common/src/test/java/org/onap/so/utils/ExternalTaskServiceUtilsTest.java @@ -0,0 +1,62 @@ +package org.onap.so.utils; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import org.camunda.bpm.client.ExternalTaskClient; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.core.env.Environment; + +@RunWith(MockitoJUnitRunner.class) +public class ExternalTaskServiceUtilsTest { + + @Spy + @InjectMocks + private ExternalTaskServiceUtils utils = new ExternalTaskServiceUtils(); + + @Mock + private ExternalTaskClient actualClient1; + + @Mock + private ExternalTaskClient actualClient2; + + @Mock + private ExternalTaskClient actualClient3; + + @Mock + private ExternalTaskClient actualClient4; + + @Test + public void testCheckActiveClients() throws Exception { + Set<ExternalTaskClient> taskClients = ConcurrentHashMap.newKeySet(); + taskClients.add(actualClient1); + taskClients.add(actualClient2); + taskClients.add(actualClient3); + taskClients.add(actualClient4); + when(utils.getClients()).thenReturn(taskClients); + when(actualClient1.isActive()).thenReturn(false); + when(actualClient2.isActive()).thenReturn(true); + when(actualClient3.isActive()).thenReturn(false); + when(actualClient4.isActive()).thenReturn(true); + utils.checkAllClientsActive(); + verify(actualClient1, times(1)).isActive(); + verify(actualClient2, times(1)).isActive(); + verify(actualClient3, times(1)).isActive(); + verify(actualClient4, times(1)).isActive(); + verify(actualClient1, times(1)).start(); + verify(actualClient3, times(1)).start(); + } + +} |