Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / distribution / engine / rest / MSORestClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.distribution.engine.rest;
22
23 import com.google.common.annotations.VisibleForTesting;
24 import com.google.gson.Gson;
25 import org.apache.http.entity.ContentType;
26 import org.apache.http.entity.StringEntity;
27 import org.eclipse.jetty.util.URIUtil;
28 import org.openecomp.sdc.be.components.distribution.engine.DistributionStatusNotificationEnum;
29 import org.openecomp.sdc.be.config.ConfigurationManager;
30 import org.openecomp.sdc.common.http.client.api.HttpExecuteException;
31 import org.openecomp.sdc.common.http.client.api.HttpRequest;
32 import org.openecomp.sdc.common.http.client.api.HttpResponse;
33 import org.openecomp.sdc.common.http.client.api.Responses;
34 import org.openecomp.sdc.common.http.client.api.RestUtils;
35 import org.openecomp.sdc.common.http.client.api.RetryHandlers;
36 import org.openecomp.sdc.common.http.config.BasicAuthorization;
37 import org.openecomp.sdc.common.http.config.ExternalServiceConfig;
38 import org.openecomp.sdc.common.http.config.HttpClientConfig;
39 import org.openecomp.sdc.common.log.wrappers.Logger;
40 import org.springframework.stereotype.Component;
41
42 import java.util.Properties;
43
44 @Component
45 public class MSORestClient {
46
47     private static final Logger logger = Logger.getLogger(MSORestClient.class.getName());
48     private static final Gson gson = new Gson();
49     @VisibleForTesting
50     static final String DISTRIBUTIONS_RESOURCE_CONFIG_PARAM = "distributions";
51
52     private ExternalServiceConfig serviceConfig = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getMsoConfig();
53
54     public MSORestClient() {
55         HttpClientConfig httpClientConfig = serviceConfig.getHttpClientConfig();
56         int numOfRetries = httpClientConfig.getNumOfRetries(); 
57         if ( numOfRetries > 0 ) {
58             httpClientConfig.setRetryHandler(RetryHandlers.getDefault(numOfRetries));
59         }
60         serviceConfig.getHttpClientConfig().setEnableMetricLogging(true);
61     }
62
63     public HttpResponse<String> notifyDistributionComplete(String distributionId, DistributionStatusNotificationEnum distributionStatusEnum, String errReason) {
64         try {
65             return doNotifyDistributionComplete(distributionId, distributionStatusEnum, errReason);
66         }
67         catch(HttpExecuteException e) {
68             logger.debug("The request to mso failed with exception ", e);
69             return Responses.INTERNAL_SERVER_ERROR;
70         }
71     }
72
73     private HttpResponse<String> doNotifyDistributionComplete(String distributionId, DistributionStatusNotificationEnum distributionStatusEnum, String errReason) throws HttpExecuteException {
74         StringEntity entity = new StringEntity(gson.toJson(new DistributionStatusRequest(distributionStatusEnum.name(), errReason)), ContentType.APPLICATION_JSON);
75         HttpResponse<String> response = HttpRequest.patch(buildMsoDistributionUrl(distributionId), buildReqHeader(), entity, serviceConfig.getHttpClientConfig());
76         return response;
77     }
78
79     private Properties buildReqHeader() {
80         Properties properties = new Properties();
81         BasicAuthorization basicAuth = serviceConfig.getHttpClientConfig().getBasicAuthorization();
82         RestUtils.addBasicAuthHeader(properties, basicAuth.getUserName(), basicAuth.getPassword());
83         return properties;
84     }
85
86     private String buildMsoDistributionUrl(String distributionId) {
87         String msoBaseUrl = serviceConfig.getHttpRequestConfig().getServerRootUrl();
88         String distributionsPath = serviceConfig.getHttpRequestConfig().getResourceNamespaces().get(DISTRIBUTIONS_RESOURCE_CONFIG_PARAM);
89         String distributionsApiPath = distributionsPath + URIUtil.SLASH + distributionId;
90         return msoBaseUrl + distributionsApiPath;
91     }
92
93 }