Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / distribution / engine / AaiRequestHandler.java
1 package org.openecomp.sdc.be.components.distribution.engine;
2
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;
16
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;
24
25 @Component
26 public class AaiRequestHandler {
27
28     private static final Logger logger = LoggerFactory.getLogger(AaiRequestHandler.class);
29     private ExternalServiceConfig aaiConfig;
30     
31     protected static final String OPERATIONAL_ENV_RESOURCE_CONFIG_PARAM = "operationalEnvironments";
32     protected static final String OPERATIONAL_ENV_RESOURCE = "/operational-environment";
33
34     @PostConstruct
35     public void init() {
36         logger.debug("AaiRequestHandler has been initialized.");
37
38         aaiConfig = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getAaiConfig();
39         logger.debug("AaiRequestHandler Configuration={}", aaiConfig);
40     }
41
42
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);
49         
50         SupplierThrows<HttpResponse<String>, Exception> httpGet = () -> HttpRequest.get(url, headers, aaiConfig.getHttpClientConfig());
51         long maxRetries = aaiConfig.getHttpClientConfig().getNumOfRetries();
52         try {
53             return FunctionalInterfaces.retryMethodOnException(httpGet, this::retryOnException, maxRetries);
54         }
55         catch (Exception e) {
56             logger.debug("Request failed with exception {}", getCause(e).getMessage());
57             return Responses.INTERNAL_SERVER_ERROR;
58         }
59     }
60     
61
62     private boolean retryOnException(Exception e) {
63         Throwable cause = getCause(e);
64         return !(cause instanceof ConnectTimeoutException || cause instanceof ConnectException || cause instanceof SocketTimeoutException);
65     }
66
67
68     private Throwable getCause(Exception e) {
69         if (e instanceof HttpExecuteException) {
70             return e.getCause();
71         }
72         return e;
73     }
74     
75     
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());
81
82         return headers;
83     }
84 }