re base code
[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.openecomp.sdc.common.log.wrappers.Logger;
14 import org.springframework.stereotype.Component;
15
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;
23
24 @Component
25 public class AaiRequestHandler {
26
27     private static final Logger logger = Logger.getLogger(AaiRequestHandler.class);
28     private ExternalServiceConfig aaiConfig;
29     
30     protected static final String OPERATIONAL_ENV_RESOURCE_CONFIG_PARAM = "operationalEnvironments";
31     protected static final String OPERATIONAL_ENV_RESOURCE = "/operational-environment";
32
33     @PostConstruct
34     public void init() {
35         logger.debug("AaiRequestHandler has been initialized.");
36
37         aaiConfig = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getAaiConfig();
38         logger.debug("AaiRequestHandler Configuration={}", aaiConfig);
39     }
40
41
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);
48         
49         SupplierThrows<HttpResponse<String>, Exception> httpGet = () -> HttpRequest.get(url, headers, aaiConfig.getHttpClientConfig());
50         long maxRetries = aaiConfig.getHttpClientConfig().getNumOfRetries();
51         try {
52             return FunctionalInterfaces.retryMethodOnException(httpGet, this::retryOnException, maxRetries);
53         }
54         catch (Exception e) {
55             logger.debug("Request failed with exception {}", getCause(e).getMessage());
56             return Responses.INTERNAL_SERVER_ERROR;
57         }
58     }
59     
60
61     private boolean retryOnException(Exception e) {
62         Throwable cause = getCause(e);
63         return !(cause instanceof ConnectTimeoutException || cause instanceof ConnectException || cause instanceof SocketTimeoutException);
64     }
65
66
67     private Throwable getCause(Exception e) {
68         if (e instanceof HttpExecuteException) {
69             return e.getCause();
70         }
71         return e;
72     }
73     
74     
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());
80
81         return headers;
82     }
83 }