diff options
author | Steve Smokowski <ss835w@att.com> | 2019-09-10 11:58:11 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-09-10 11:58:11 +0000 |
commit | 8d19ead3b24e7ee654bb467565ad7e6b94ddc671 (patch) | |
tree | 7659987fa1e4d3a3696a33999f13ef25e8bf62e2 /common | |
parent | d6d0a05998f981bfc48f71c713f5b1f9797bf56e (diff) | |
parent | ff4b57e3632507a7a8e4b740722663f02caacc2a (diff) |
Merge "Created external task utils in a common location"
Diffstat (limited to 'common')
5 files changed, 223 insertions, 1 deletions
diff --git a/common/pom.xml b/common/pom.xml index 2553038e6b..1a4ffe13a8 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -191,7 +191,11 @@ <artifactId>javatuples</artifactId> <version>1.2</version> </dependency> - + <dependency> + <groupId>org.camunda.bpm</groupId> + <artifactId>camunda-external-task-client</artifactId> + <version>1.1.1</version> + </dependency> </dependencies> <dependencyManagement> <dependencies> diff --git a/common/src/main/java/org/onap/so/utils/ExternalTaskServiceUtils.java b/common/src/main/java/org/onap/so/utils/ExternalTaskServiceUtils.java new file mode 100644 index 0000000000..e43b431821 --- /dev/null +++ b/common/src/main/java/org/onap/so/utils/ExternalTaskServiceUtils.java @@ -0,0 +1,46 @@ +package org.onap.so.utils; + +import java.security.GeneralSecurityException; +import org.camunda.bpm.client.ExternalTaskClient; +import org.camunda.bpm.client.interceptor.ClientRequestInterceptor; +import org.camunda.bpm.client.interceptor.auth.BasicAuthProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +public class ExternalTaskServiceUtils { + + @Autowired + public Environment env; + + 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(); + } + + protected ClientRequestInterceptor createClientInterceptor(String auth) { + return new BasicAuthProvider(env.getRequiredProperty("mso.config.cadi.aafId"), auth); + } + + protected String getAuth() throws Exception { + try { + return CryptoUtils.decrypt(env.getRequiredProperty("mso.auth"), env.getRequiredProperty("mso.msoKey")); + } catch (IllegalStateException | GeneralSecurityException e) { + logger.error("Error Decrypting Password", e); + throw new Exception("Cannot load password"); + } + } + + public int getMaxClients() { + return Integer.parseInt(env.getProperty("workflow.topics.maxClients", "3")); + } + + +} diff --git a/common/src/main/java/org/onap/so/utils/ExternalTaskUtils.java b/common/src/main/java/org/onap/so/utils/ExternalTaskUtils.java new file mode 100644 index 0000000000..349767fc5b --- /dev/null +++ b/common/src/main/java/org/onap/so/utils/ExternalTaskUtils.java @@ -0,0 +1,46 @@ +package org.onap.so.utils; + +import org.camunda.bpm.client.task.ExternalTask; +import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +public abstract class ExternalTaskUtils { + + @Autowired + Environment env; + + private static final Logger logger = LoggerFactory.getLogger(ExternalTaskUtils.class); + + public long calculateRetryDelay(int currentRetries) { + int retrySequence = getRetrySequence().length - currentRetries; + return Integer.parseInt(getRetrySequence()[retrySequence]) * getRetryMutiplier(); + } + + protected Long getRetryMutiplier() { + return Long.parseLong(env.getProperty("mso.workflow.topics.retryMultiplier", "6000")); + } + + protected String[] getRetrySequence() { + String[] seq = {"1", "1", "2", "3", "5", "8", "13", "20"}; + if (env.getProperty("mso.workflow.topics.retrySequence") != null) { + seq = env.getProperty("mso.workflow.topics.retrySequence", String[].class); + } + return seq; + } + + protected void setupMDC(ExternalTask externalTask) { + logger.info(ONAPLogConstants.Markers.ENTRY, "Entering"); + String msoRequestId = externalTask.getVariable("mso-request-id"); + if (msoRequestId != null && !msoRequestId.isEmpty()) { + MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, msoRequestId); + } + MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, externalTask.getTopicName()); + } + +} 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..b2db986d02 --- /dev/null +++ b/common/src/test/java/org/onap/so/utils/ExternalTaskServiceUtilsTest.java @@ -0,0 +1,61 @@ +package org.onap.so.utils; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doReturn; +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 Environment mockEnv; + + @Mock + private ExternalTaskClient mockClient; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + doReturn("3").when(mockEnv).getProperty("workflow.topics.maxClients", "3"); + doReturn("07a7159d3bf51a0e53be7a8f89699be7").when(mockEnv).getRequiredProperty("mso.msoKey"); + doReturn("6B466C603A260F3655DBF91E53CE54667041C01406D10E8CAF9CC24D8FA5388D06F90BFE4C852052B436").when(mockEnv) + .getRequiredProperty("mso.auth"); + doReturn("someid").when(mockEnv).getRequiredProperty("mso.config.cadi.aafId"); + doReturn("http://camunda.com").when(mockEnv).getRequiredProperty("mso.workflow.endpoint"); + } + + @Test + public void testCreateExternalTaskClient() throws Exception { + ExternalTaskClient actualClient = utils.createExternalTaskClient(); + Assert.assertNotNull(actualClient); + } + + @Test + public void testGetAuth() throws Exception { + String actual = utils.getAuth(); + String expected = "Att32054Life!@"; + assertEquals(expected, actual); + } + + @Test + public void testGetMaxClients() throws Exception { + int actual = utils.getMaxClients(); + int expected = 3; + assertEquals(expected, actual); + } + +} diff --git a/common/src/test/java/org/onap/so/utils/ExternalTaskUtilsTest.java b/common/src/test/java/org/onap/so/utils/ExternalTaskUtilsTest.java new file mode 100644 index 0000000000..f918781b39 --- /dev/null +++ b/common/src/test/java/org/onap/so/utils/ExternalTaskUtilsTest.java @@ -0,0 +1,65 @@ +package org.onap.so.utils; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.core.env.Environment; + +@RunWith(MockitoJUnitRunner.class) +public class ExternalTaskUtilsTest { + + @Mock + private Environment mockenv; + + @InjectMocks + private ExternalTaskUtils externalTaskUtilsAnony = new ExternalTaskUtils() { + + }; + + @Test + public void retry_sequence_calculation_Test() { + Mockito.when(mockenv.getProperty("mso.workflow.topics.retryMultiplier", "6000")).thenReturn("6000"); + long firstRetry = externalTaskUtilsAnony.calculateRetryDelay(8); + assertEquals(6000L, firstRetry); + long secondRetry = externalTaskUtilsAnony.calculateRetryDelay(7); + assertEquals(6000L, secondRetry); + long thirdRetry = externalTaskUtilsAnony.calculateRetryDelay(6); + assertEquals(12000L, thirdRetry); + long fourthRetry = externalTaskUtilsAnony.calculateRetryDelay(5); + assertEquals(18000L, fourthRetry); + long fifthRetry = externalTaskUtilsAnony.calculateRetryDelay(4); + assertEquals(30000L, fifthRetry); + long sixRetry = externalTaskUtilsAnony.calculateRetryDelay(3); + assertEquals(48000L, sixRetry); + long seventhRetry = externalTaskUtilsAnony.calculateRetryDelay(2); + assertEquals(78000L, seventhRetry); + long eigthRetry = externalTaskUtilsAnony.calculateRetryDelay(1); + assertEquals(120000L, eigthRetry); + } + + @Test + public void retry_sequence_Test() { + Mockito.when(mockenv.getProperty("mso.workflow.topics.retryMultiplier", "6000")).thenReturn("6000"); + long firstRetry = externalTaskUtilsAnony.calculateRetryDelay(8); + assertEquals(6000L, firstRetry); + long secondRetry = externalTaskUtilsAnony.calculateRetryDelay(7); + assertEquals(6000L, secondRetry); + long thirdRetry = externalTaskUtilsAnony.calculateRetryDelay(6); + assertEquals(12000L, thirdRetry); + long fourthRetry = externalTaskUtilsAnony.calculateRetryDelay(5); + assertEquals(18000L, fourthRetry); + long fifthRetry = externalTaskUtilsAnony.calculateRetryDelay(4); + assertEquals(30000L, fifthRetry); + long sixRetry = externalTaskUtilsAnony.calculateRetryDelay(3); + assertEquals(48000L, sixRetry); + long seventhRetry = externalTaskUtilsAnony.calculateRetryDelay(2); + assertEquals(78000L, seventhRetry); + long eigthRetry = externalTaskUtilsAnony.calculateRetryDelay(1); + assertEquals(120000L, eigthRetry); + } + +} |