2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
20 package org.openecomp.sdc.be.model.operations.impl;
22 import fj.data.Either;
24 import java.util.Properties;
25 import lombok.NoArgsConstructor;
26 import org.apache.http.HttpStatus;
27 import org.openecomp.sdc.be.config.Configuration.OnboardingConfig;
28 import org.openecomp.sdc.be.config.ConfigurationManager;
29 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
30 import org.openecomp.sdc.common.api.Constants;
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.log.wrappers.Logger;
34 import org.openecomp.sdc.common.zip.ZipUtils;
37 @org.springframework.stereotype.Component("onboarding-client")
38 public class OnboardingClient {
40 private static final Logger log = Logger.getLogger(OnboardingClient.class.getName());
41 private static Properties downloadCsarHeaders = new Properties();
44 downloadCsarHeaders.put("Accept", "application/octet-stream");
47 public Either<Map<String, byte[]>, StorageOperationStatus> getCsar(String csarUuid, String userId) {
48 String url = buildDownloadCsarUrl() + "/" + csarUuid;
49 Properties headers = new Properties();
50 if (downloadCsarHeaders != null) {
51 downloadCsarHeaders.forEach(headers::put);
54 headers.put(Constants.USER_ID_HEADER, userId);
56 log.debug("Url for downloading csar is {}. Headers are {}", url, headers);
58 HttpResponse<byte[]> httpResponse = HttpRequest.getAsByteArray(url, headers);
59 log.debug("After fetching csar {}. Http return code is {}", csarUuid, httpResponse.getStatusCode());
60 switch (httpResponse.getStatusCode()) {
61 case HttpStatus.SC_OK:
62 byte[] data = httpResponse.getResponse();
63 if (data != null && data.length > 0) {
64 Map<String, byte[]> readZip = ZipUtils.readZip(data, false);
65 return Either.left(readZip);
67 log.debug("Data received from rest is null or empty");
68 return Either.right(StorageOperationStatus.NOT_FOUND);
70 case HttpStatus.SC_NOT_FOUND:
71 return Either.right(StorageOperationStatus.CSAR_NOT_FOUND);
73 return Either.right(StorageOperationStatus.GENERAL_ERROR);
75 } catch (Exception e) {
76 log.debug("Request failed with exception", e);
77 return Either.right(StorageOperationStatus.GENERAL_ERROR);
81 public Either<String, StorageOperationStatus> getPackages(String userId) {
82 String url = buildDownloadCsarUrl();
83 Properties headers = new Properties();
84 headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
87 headers.put(Constants.USER_ID_HEADER, userId);
90 log.debug("Url for downloading packages is {}. Headers are {}", url, headers);
93 HttpResponse<String> httpResposne = HttpRequest.get(url, headers);
94 log.debug("After fetching packages. Http return code is {}", httpResposne.getStatusCode());
96 switch (httpResposne.getStatusCode()) {
97 case HttpStatus.SC_OK:
98 String data = httpResposne.getResponse();
99 return Either.left(data);
101 case HttpStatus.SC_NOT_FOUND:
102 return Either.right(StorageOperationStatus.CSAR_NOT_FOUND);
105 return Either.right(StorageOperationStatus.GENERAL_ERROR);
107 } catch (Exception e) {
108 log.debug("Request failed with exception", e);
109 return Either.right(StorageOperationStatus.GENERAL_ERROR);
114 * Build the url for download CSAR
116 * E.g., http://0.0.0.0:8181/onboarding-api/v1.0/vendor-software-products/packages/
120 public String buildDownloadCsarUrl() {
121 OnboardingConfig onboardingConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getOnboarding();
122 String protocol = onboardingConfig.getProtocol();
123 String host = onboardingConfig.getHost();
124 Integer port = onboardingConfig.getPort();
125 String uri = onboardingConfig.getDownloadCsarUri();
126 return protocol + "://" + host + ":" + port + uri;