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.openecomp.sdc.common.log.wrappers.Logger;
14 import org.springframework.stereotype.Component;
16 import javax.annotation.PostConstruct;
17 import javax.ws.rs.core.HttpHeaders;
18 import javax.ws.rs.core.MediaType;
19 import java.net.ConnectException;
20 import java.net.SocketTimeoutException;
21 import java.util.Properties;
22 import java.util.UUID;
25 public class AaiRequestHandler {
27 private static final Logger logger = Logger.getLogger(AaiRequestHandler.class);
28 private ExternalServiceConfig aaiConfig;
30 protected static final String OPERATIONAL_ENV_RESOURCE_CONFIG_PARAM = "operationalEnvironments";
31 protected static final String OPERATIONAL_ENV_RESOURCE = "/operational-environment";
35 logger.debug("AaiRequestHandler has been initialized.");
37 aaiConfig = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getAaiConfig();
38 logger.debug("AaiRequestHandler Configuration={}", aaiConfig);
42 public HttpResponse<String> getOperationalEnvById(String id) {
43 Properties headers = createHeaders();
44 String url = String.format("%s%s%s/%s",
45 aaiConfig.getHttpRequestConfig().getServerRootUrl(),
46 aaiConfig.getHttpRequestConfig().getResourceNamespaces().get(OPERATIONAL_ENV_RESOURCE_CONFIG_PARAM),
47 OPERATIONAL_ENV_RESOURCE, id);
49 SupplierThrows<HttpResponse<String>, Exception> httpGet = () -> HttpRequest.get(url, headers, aaiConfig.getHttpClientConfig());
50 long maxRetries = aaiConfig.getHttpClientConfig().getNumOfRetries();
52 return FunctionalInterfaces.retryMethodOnException(httpGet, this::retryOnException, maxRetries);
55 logger.debug("Request failed with exception {}", getCause(e).getMessage());
56 return Responses.INTERNAL_SERVER_ERROR;
61 private boolean retryOnException(Exception e) {
62 Throwable cause = getCause(e);
63 return !(cause instanceof ConnectTimeoutException || cause instanceof ConnectException || cause instanceof SocketTimeoutException);
67 private Throwable getCause(Exception e) {
68 if (e instanceof HttpExecuteException) {
75 private Properties createHeaders() {
76 Properties headers = new Properties();
77 headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
78 headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
79 headers.put(Constants.X_TRANSACTION_ID_HEADER, UUID.randomUUID().toString());