2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.distribution.engine;
23 import org.apache.http.conn.ConnectTimeoutException;
24 import org.openecomp.sdc.be.config.ConfigurationManager;
25 import org.openecomp.sdc.common.api.Constants;
26 import org.openecomp.sdc.common.datastructure.FunctionalInterfaces;
27 import org.openecomp.sdc.common.datastructure.FunctionalInterfaces.SupplierThrows;
28 import org.openecomp.sdc.common.http.client.api.HttpExecuteException;
29 import org.openecomp.sdc.common.http.client.api.HttpRequest;
30 import org.openecomp.sdc.common.http.client.api.HttpResponse;
31 import org.openecomp.sdc.common.http.client.api.Responses;
32 import org.openecomp.sdc.common.http.config.ExternalServiceConfig;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.springframework.stereotype.Component;
36 import javax.annotation.PostConstruct;
37 import javax.ws.rs.core.HttpHeaders;
38 import javax.ws.rs.core.MediaType;
39 import java.net.ConnectException;
40 import java.net.SocketTimeoutException;
41 import java.util.Properties;
42 import java.util.UUID;
45 public class AaiRequestHandler {
47 private static final Logger logger = Logger.getLogger(AaiRequestHandler.class);
48 private ExternalServiceConfig aaiConfig;
50 protected static final String OPERATIONAL_ENV_RESOURCE_CONFIG_PARAM = "operationalEnvironments";
51 protected static final String OPERATIONAL_ENV_RESOURCE = "/operational-environment";
55 logger.debug("AaiRequestHandler has been initialized.");
57 aaiConfig = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getAaiConfig();
58 aaiConfig.getHttpClientConfig().setEnableMetricLogging(true);
59 logger.debug("AaiRequestHandler Configuration={}", aaiConfig);
63 public HttpResponse<String> getOperationalEnvById(String id) {
64 Properties headers = createHeaders();
65 String url = String.format("%s%s%s/%s",
66 aaiConfig.getHttpRequestConfig().getServerRootUrl(),
67 aaiConfig.getHttpRequestConfig().getResourceNamespaces().get(OPERATIONAL_ENV_RESOURCE_CONFIG_PARAM),
68 OPERATIONAL_ENV_RESOURCE, id);
70 SupplierThrows<HttpResponse<String>, Exception> httpGet = () -> HttpRequest.get(url, headers, aaiConfig.getHttpClientConfig());
71 long maxRetries = aaiConfig.getHttpClientConfig().getNumOfRetries();
73 return FunctionalInterfaces.retryMethodOnException(httpGet, this::retryOnException, maxRetries);
75 } catch (Exception e) {
76 logger.debug("Request failed with exception {}", getCause(e).getMessage());
77 return Responses.INTERNAL_SERVER_ERROR;
84 private boolean retryOnException(Exception e) {
85 Throwable cause = getCause(e);
86 return !(cause instanceof ConnectTimeoutException || cause instanceof ConnectException || cause instanceof SocketTimeoutException);
90 private Throwable getCause(Exception e) {
91 if (e instanceof HttpExecuteException) {
98 private Properties createHeaders() {
99 Properties headers = new Properties();
100 headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
101 headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
102 headers.put(Constants.X_TRANSACTION_ID_HEADER, UUID.randomUUID().toString());