From f4c3fdc23752733a4c4f670a1cddb460747fd738 Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Wed, 21 Jun 2023 13:53:10 +0100 Subject: Remove Thread support in http participant Due the Thread support in Intermediary, Thread support in http participant could be removed. Issue-ID: POLICY-4736 Change-Id: I2d947ab9f62b626b037c593f326e2d0af9bc7a9e Signed-off-by: FrancescoFioraEst --- .../AutomationCompositionElementHandler.java | 49 +------ .../http/main/webclient/AcHttpClient.java | 13 +- .../http/handler/AcElementHandlerTest.java | 141 ++++++++++----------- .../http/webclient/AcHttpClientTest.java | 14 +- 4 files changed, 79 insertions(+), 138 deletions(-) (limited to 'participant') diff --git a/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/acm/participant/http/main/handler/AutomationCompositionElementHandler.java b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/acm/participant/http/main/handler/AutomationCompositionElementHandler.java index 711887423..e3ebf3bef 100644 --- a/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/acm/participant/http/main/handler/AutomationCompositionElementHandler.java +++ b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/acm/participant/http/main/handler/AutomationCompositionElementHandler.java @@ -20,21 +20,14 @@ package org.onap.policy.clamp.acm.participant.http.main.handler; -import java.io.Closeable; -import java.io.IOException; import java.lang.invoke.MethodHandles; import java.util.List; import java.util.Map; import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.stream.Collectors; import javax.validation.Validation; import javax.ws.rs.core.Response.Status; import lombok.RequiredArgsConstructor; -import org.apache.commons.lang3.tuple.Pair; import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest; import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient; import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener; @@ -50,7 +43,6 @@ import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; @@ -61,14 +53,12 @@ import org.springframework.stereotype.Component; */ @Component @RequiredArgsConstructor -public class AutomationCompositionElementHandler implements AutomationCompositionElementListener, Closeable { +public class AutomationCompositionElementHandler implements AutomationCompositionElementListener { private static final Coder CODER = new StandardCoder(); private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - private final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); - private final ParticipantIntermediaryApi intermediaryApi; private final AcHttpClient acHttpClient; @@ -97,7 +87,7 @@ public class AutomationCompositionElementHandler implements AutomationCompositio throws PfModelException { try { var configRequest = getConfigRequest(properties); - var restResponseMap = invokeHttpClient(configRequest); + var restResponseMap = acHttpClient.run(configRequest); var failedResponseStatus = restResponseMap.values().stream() .filter(response -> !HttpStatus.valueOf(response.getKey()).is2xxSuccessful()) .collect(Collectors.toList()); @@ -130,29 +120,6 @@ public class AutomationCompositionElementHandler implements AutomationCompositio } } - /** - * Invoke a runnable thread to execute http requests. - * - * @param configRequest ConfigRequest - */ - private Map> invokeHttpClient(ConfigRequest configRequest) - throws PfModelException { - try { - Map> restResponseMap = new ConcurrentHashMap<>(); - // Invoke runnable thread to execute https requests of all config entities - var result = executor.submit(() -> acHttpClient.run(configRequest, restResponseMap), restResponseMap); - if (!result.get().isEmpty()) { - LOGGER.debug("Http Request Completed: {}", result.isDone()); - } - return restResponseMap; - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new PfModelException(Status.BAD_REQUEST, "Error invoking ExecutorService ", e); - } catch (ExecutionException e) { - throw new PfModelException(Status.BAD_REQUEST, "Error invoking the http request for the config ", e); - } - } - @Override public void lock(UUID instanceId, UUID elementId) throws PfModelException { intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED, @@ -189,16 +156,4 @@ public class AutomationCompositionElementHandler implements AutomationCompositio intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR, "Deprimed"); } - - /** - * Closes this stream and releases any system resources associated - * with it. If the stream is already closed then invoking this - * method has no effect. - * - * @throws IOException if an I/O error occurs - */ - @Override - public void close() throws IOException { - executor.shutdown(); - } } diff --git a/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/acm/participant/http/main/webclient/AcHttpClient.java b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/acm/participant/http/main/webclient/AcHttpClient.java index 1956b0d6c..66ca1b31a 100644 --- a/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/acm/participant/http/main/webclient/AcHttpClient.java +++ b/participant/participant-impl/participant-impl-http/src/main/java/org/onap/policy/clamp/acm/participant/http/main/webclient/AcHttpClient.java @@ -22,6 +22,7 @@ package org.onap.policy.clamp.acm.participant.http.main.webclient; import java.lang.invoke.MethodHandles; import java.time.Duration; +import java.util.HashMap; import java.util.Map; import java.util.Objects; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -50,22 +51,25 @@ public class AcHttpClient { /** * Runnable to execute http requests. */ - public void run(ConfigRequest configRequest, Map> responseMap) { + public Map> run(ConfigRequest configRequest) { var webClient = WebClient.builder().baseUrl(configRequest.getBaseUrl()) .defaultHeaders(httpHeaders -> httpHeaders.addAll(createHeaders(configRequest))).build(); + Map> responseMap = new HashMap<>(); for (var configurationEntity : configRequest.getConfigurationEntities()) { LOGGER.info("Executing http requests for the config entity {}", configurationEntity.getConfigurationEntityId()); - executeRequest(webClient, configRequest, configurationEntity, responseMap); + responseMap.putAll(executeRequest(webClient, configRequest, configurationEntity)); } + return responseMap; } - private void executeRequest(WebClient client, ConfigRequest configRequest, ConfigurationEntity configurationEntity, - Map> responseMap) { + private Map> executeRequest(WebClient client, + ConfigRequest configRequest, ConfigurationEntity configurationEntity) { + Map> responseMap = new HashMap<>(); // Iterate the sequence of http requests for (var request : configurationEntity.getRestSequence()) { try { @@ -97,6 +101,7 @@ public class AcHttpClient { responseMap.put(request.getRestRequestId(), new ImmutablePair<>(404, ex.getMessage())); } } + return responseMap; } private HttpHeaders createHeaders(ConfigRequest request) { diff --git a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/handler/AcElementHandlerTest.java b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/handler/AcElementHandlerTest.java index 12b8ee4c3..f0f991302 100644 --- a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/handler/AcElementHandlerTest.java +++ b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/handler/AcElementHandlerTest.java @@ -21,11 +21,9 @@ package org.onap.policy.clamp.acm.participant.http.handler; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyMap; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -50,52 +48,50 @@ class AcElementHandlerTest { "org.onap.domain.database.Http_PMSHMicroserviceAutomationCompositionElement"; @Test - void testUndeploy() throws IOException { + void testUndeploy() { var instanceId = commonTestData.getAutomationCompositionId(); var element = commonTestData.getAutomationCompositionElement(); var acElementId = element.getId(); var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class); - try (var automationCompositionElementHandler = - new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) { - automationCompositionElementHandler.undeploy(instanceId, acElementId); - verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, - DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, ""); - } + var automationCompositionElementHandler = + new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class)); + automationCompositionElementHandler.undeploy(instanceId, acElementId); + verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, + DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, ""); + } @Test - void testDeployConstraintViolations() throws IOException, PfModelException { + void testDeployConstraintViolations() throws PfModelException { var instanceId = commonTestData.getAutomationCompositionId(); var element = commonTestData.getAutomationCompositionElement(); var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class); - try (var automationCompositionElementHandler = - new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) { + var automationCompositionElementHandler = + new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class)); - Map map = new HashMap<>(); - automationCompositionElementHandler.deploy(instanceId, element, map); - verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), - DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, - "Constraint violations in the config request"); - } + Map map = new HashMap<>(); + automationCompositionElementHandler.deploy(instanceId, element, map); + verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Constraint violations in the config request"); } @Test - void testDeployError() throws IOException, PfModelException { + void testDeployError() throws PfModelException { var instanceId = commonTestData.getAutomationCompositionId(); var element = commonTestData.getAutomationCompositionElement(); var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class); - try (var automationCompositionElementHandler = - new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) { + var automationCompositionElementHandler = + new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class)); + + Map map = new HashMap<>(); + map.put("httpHeaders", 1); + automationCompositionElementHandler.deploy(instanceId, element, map); + verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Error extracting ConfigRequest "); - Map map = new HashMap<>(); - map.put("httpHeaders", 1); - automationCompositionElementHandler.deploy(instanceId, element, map); - verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), - DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Error extracting ConfigRequest "); - } } @Test @@ -109,102 +105,95 @@ class AcElementHandlerTest { var acHttpClient = mock(AcHttpClient.class); var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class); - try (var automationCompositionElementHandler = - new AutomationCompositionElementHandler(participantIntermediaryApi, acHttpClient)) { + var automationCompositionElementHandler = + new AutomationCompositionElementHandler(participantIntermediaryApi, acHttpClient); - automationCompositionElementHandler.deploy(instanceId, element, map); - verify(acHttpClient).run(any(ConfigRequest.class), anyMap()); - verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), - DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed"); - } + automationCompositionElementHandler.deploy(instanceId, element, map); + verify(acHttpClient).run(any(ConfigRequest.class)); + verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed"); } @Test - void testUpdate() throws Exception { + void testUpdate() throws PfModelException { var instanceId = commonTestData.getAutomationCompositionId(); var element = commonTestData.getAutomationCompositionElement(); var acElementId = element.getId(); var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class); - try (var automationCompositionElementHandler = - new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) { + var automationCompositionElementHandler = + new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class)); - automationCompositionElementHandler.update(instanceId, element, Map.of()); - verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, - DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported"); - } + automationCompositionElementHandler.update(instanceId, element, Map.of()); + verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported"); } @Test - void testLock() throws Exception { + void testLock() throws PfModelException { var instanceId = commonTestData.getAutomationCompositionId(); var acElementId = UUID.randomUUID(); var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class); - try (var automationCompositionElementHandler = - new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) { + var automationCompositionElementHandler = + new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class)); - automationCompositionElementHandler.lock(instanceId, acElementId); - verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, null, - LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked"); - } + automationCompositionElementHandler.lock(instanceId, acElementId); + verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, null, + LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked"); } @Test - void testUnlock() throws Exception { + void testUnlock() throws PfModelException { var instanceId = commonTestData.getAutomationCompositionId(); var acElementId = UUID.randomUUID(); var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class); - try (var automationCompositionElementHandler = - new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) { + var automationCompositionElementHandler = + new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class)); - automationCompositionElementHandler.unlock(instanceId, acElementId); - verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, null, - LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked"); - } + automationCompositionElementHandler.unlock(instanceId, acElementId); + verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, null, + LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked"); } @Test - void testDelete() throws Exception { + void testDelete() throws PfModelException { var instanceId = commonTestData.getAutomationCompositionId(); var acElementId = UUID.randomUUID(); var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class); - try (var automationCompositionElementHandler = - new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) { + var automationCompositionElementHandler = + new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class)); - automationCompositionElementHandler.delete(instanceId, acElementId); - verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, - DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted"); - } + automationCompositionElementHandler.delete(instanceId, acElementId); + verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, + DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted"); } @Test - void testPrime() throws Exception { + void testPrime() throws PfModelException { var compositionId = UUID.randomUUID(); var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class); - try (var automationCompositionElementHandler = - new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) { + var automationCompositionElementHandler = + new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class)); - automationCompositionElementHandler.prime(compositionId, List.of()); - verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, - StateChangeResult.NO_ERROR, "Primed"); - } + automationCompositionElementHandler.prime(compositionId, List.of()); + verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, + StateChangeResult.NO_ERROR, "Primed"); } @Test - void testDeprime() throws Exception { + void testDeprime() throws PfModelException { var compositionId = UUID.randomUUID(); var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class); - try (var automationCompositionElementHandler = - new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) { + var automationCompositionElementHandler = + new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class)); - automationCompositionElementHandler.deprime(compositionId); - verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, - StateChangeResult.NO_ERROR, "Deprimed"); - } + automationCompositionElementHandler.deprime(compositionId); + verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, + StateChangeResult.NO_ERROR, "Deprimed"); } } diff --git a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/webclient/AcHttpClientTest.java b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/webclient/AcHttpClientTest.java index 56170eb3e..ebeba5297 100644 --- a/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/webclient/AcHttpClientTest.java +++ b/participant/participant-impl/participant-impl-http/src/test/java/org/onap/policy/clamp/acm/participant/http/webclient/AcHttpClientTest.java @@ -21,13 +21,9 @@ package org.onap.policy.clamp.acm.participant.http.webclient; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import java.io.IOException; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -37,7 +33,6 @@ import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient; import org.onap.policy.clamp.acm.participant.http.utils.CommonTestData; import org.onap.policy.clamp.acm.participant.http.utils.MockServerRest; import org.onap.policy.common.utils.network.NetworkUtil; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.springframework.test.context.junit.jupiter.SpringExtension; @ExtendWith(SpringExtension.class) @@ -73,14 +68,13 @@ class AcHttpClientTest { void test_validRequest() { // Add valid rest requests POST, GET var configurationEntity = commonTestData.getConfigurationEntity(); - Map> responseMap = new HashMap<>(); var headers = commonTestData.getHeaders(); var configRequest = new ConfigRequest(MOCK_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10); var client = new AcHttpClient(); - assertDoesNotThrow(() -> client.run(configRequest, responseMap)); + var responseMap = client.run(configRequest); assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId()); var restResponseMap = responseMap.get(commonTestData.restParamsWithGet().getRestRequestId()); @@ -91,14 +85,13 @@ class AcHttpClientTest { void test_invalidRequest() { // Add rest requests Invalid POST, Valid GET var configurationEntity = commonTestData.getInvalidConfigurationEntity(); - Map> responseMap = new HashMap<>(); var headers = commonTestData.getHeaders(); var configRequest = new ConfigRequest(MOCK_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10); var client = new AcHttpClient(); - assertDoesNotThrow(() -> client.run(configRequest, responseMap)); + var responseMap = client.run(configRequest); assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId()); var response = responseMap.get(commonTestData.restParamsWithInvalidPost().getRestRequestId()); assertThat(response.getKey()).isEqualTo(404); @@ -108,14 +101,13 @@ class AcHttpClientTest { void test_WrongUrl() { // Add rest requests Invalid URL var configurationEntity = commonTestData.getInvalidConfigurationEntity(); - Map> responseMap = new HashMap<>(); var headers = commonTestData.getHeaders(); var configRequest = new ConfigRequest(WRONG_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10); var client = new AcHttpClient(); - assertDoesNotThrow(() -> client.run(configRequest, responseMap)); + var responseMap = client.run(configRequest); assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId()); var response = responseMap.get(commonTestData.restParamsWithInvalidPost().getRestRequestId()); assertThat(response.getKey()).isEqualTo(404); -- cgit 1.2.3-korg