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.rest;
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.*;
31 import org.openecomp.sdc.common.http.config.BasicAuthorization;
32 import org.openecomp.sdc.common.http.config.ExternalServiceConfig;
33 import org.openecomp.sdc.common.http.config.HttpClientConfig;
34 import org.openecomp.sdc.common.log.wrappers.Logger;
35 import org.springframework.stereotype.Component;
37 import java.util.Properties;
40 public class MSORestClient {
42 private static final Logger logger = Logger.getLogger(MSORestClient.class.getName());
43 private static final Gson gson = new Gson();
45 static final String DISTRIBUTIONS_RESOURCE_CONFIG_PARAM = "distributions";
47 private ExternalServiceConfig serviceConfig = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getMsoConfig();
49 public MSORestClient() {
50 HttpClientConfig httpClientConfig = serviceConfig.getHttpClientConfig();
51 int numOfRetries = httpClientConfig.getNumOfRetries();
52 if ( numOfRetries > 0 ) {
53 httpClientConfig.setRetryHandler(RetryHandlers.getDefault(numOfRetries));
57 public HttpResponse<String> notifyDistributionComplete(String distributionId, DistributionStatusNotificationEnum distributionStatusEnum, String errReason) {
59 return doNotifyDistributionComplete(distributionId, distributionStatusEnum, errReason);
61 catch(HttpExecuteException e) {
62 logger.debug("The request to mso failed with exception ", e);
63 return Responses.INTERNAL_SERVER_ERROR;
67 private HttpResponse<String> doNotifyDistributionComplete(String distributionId, DistributionStatusNotificationEnum distributionStatusEnum, String errReason) throws HttpExecuteException {
68 StringEntity entity = new StringEntity(gson.toJson(new DistributionStatusRequest(distributionStatusEnum.name(), errReason)), ContentType.APPLICATION_JSON);
69 HttpResponse<String> response = HttpRequest.patch(buildMsoDistributionUrl(distributionId), buildReqHeader(), entity, serviceConfig.getHttpClientConfig());
70 logger.info("response from mso - status code: {}, status description: {}, response: {}, ", response.getStatusCode(), response.getDescription(), response.getResponse());
74 private Properties buildReqHeader() {
75 Properties properties = new Properties();
76 BasicAuthorization basicAuth = serviceConfig.getHttpClientConfig().getBasicAuthorization();
77 RestUtils.addBasicAuthHeader(properties, basicAuth.getUserName(), basicAuth.getPassword());
81 private String buildMsoDistributionUrl(String distributionId) {
82 String msoBaseUrl = serviceConfig.getHttpRequestConfig().getServerRootUrl();
83 String distributionsPath = serviceConfig.getHttpRequestConfig().getResourceNamespaces().get(DISTRIBUTIONS_RESOURCE_CONFIG_PARAM);
84 String distributionsApiPath = distributionsPath + URIUtil.SLASH + distributionId;
85 return msoBaseUrl + distributionsApiPath;