1 package org.openecomp.sdc.be.components.distribution.engine;
3 import org.apache.http.conn.ConnectTimeoutException;
4 import org.openecomp.sdc.be.config.ConfigurationManager;
5 import org.openecomp.sdc.common.api.Constants;
6 import org.openecomp.sdc.common.datastructure.FunctionalInterfaces;
7 import org.openecomp.sdc.common.datastructure.FunctionalInterfaces.SupplierThrows;
8 import org.openecomp.sdc.common.http.client.api.HttpExecuteException;
9 import org.openecomp.sdc.common.http.client.api.HttpRequest;
10 import org.openecomp.sdc.common.http.client.api.HttpResponse;
11 import org.openecomp.sdc.common.http.client.api.Responses;
12 import org.openecomp.sdc.common.http.config.ExternalServiceConfig;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.springframework.stereotype.Component;
17 import javax.annotation.PostConstruct;
18 import javax.ws.rs.core.HttpHeaders;
19 import javax.ws.rs.core.MediaType;
20 import java.net.ConnectException;
21 import java.net.SocketTimeoutException;
22 import java.util.Properties;
23 import java.util.UUID;
26 public class AaiRequestHandler {
28 private static final Logger logger = LoggerFactory.getLogger(AaiRequestHandler.class);
29 private ExternalServiceConfig aaiConfig;
31 protected static final String OPERATIONAL_ENV_RESOURCE_CONFIG_PARAM = "operationalEnvironments";
32 protected static final String OPERATIONAL_ENV_RESOURCE = "/operational-environment";
36 logger.debug("AaiRequestHandler has been initialized.");
38 aaiConfig = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getAaiConfig();
39 logger.debug("AaiRequestHandler Configuration={}", aaiConfig);
43 public HttpResponse<String> getOperationalEnvById(String id) {
44 Properties headers = createHeaders();
45 String url = String.format("%s%s%s/%s",
46 aaiConfig.getHttpRequestConfig().getServerRootUrl(),
47 aaiConfig.getHttpRequestConfig().getResourceNamespaces().get(OPERATIONAL_ENV_RESOURCE_CONFIG_PARAM),
48 OPERATIONAL_ENV_RESOURCE, id);
50 SupplierThrows<HttpResponse<String>, Exception> httpGet = () -> HttpRequest.get(url, headers, aaiConfig.getHttpClientConfig());
51 long maxRetries = aaiConfig.getHttpClientConfig().getNumOfRetries();
53 return FunctionalInterfaces.retryMethodOnException(httpGet, this::retryOnException, maxRetries);
56 logger.debug("Request failed with exception {}", getCause(e).getMessage());
57 return Responses.INTERNAL_SERVER_ERROR;
62 private boolean retryOnException(Exception e) {
63 Throwable cause = getCause(e);
64 return !(cause instanceof ConnectTimeoutException || cause instanceof ConnectException || cause instanceof SocketTimeoutException);
68 private Throwable getCause(Exception e) {
69 if (e instanceof HttpExecuteException) {
76 private Properties createHeaders() {
77 Properties headers = new Properties();
78 headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
79 headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
80 headers.put(Constants.X_TRANSACTION_ID_HEADER, UUID.randomUUID().toString());