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 ++++-- 2 files changed, 11 insertions(+), 51 deletions(-) (limited to 'participant/participant-impl/participant-impl-http/src/main/java') 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) { -- cgit 1.2.3-korg