Reformat catalog-model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / OnboardingClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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 package org.openecomp.sdc.be.model.operations.impl;
21
22 import fj.data.Either;
23 import java.util.Map;
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;
35
36 @NoArgsConstructor
37 @org.springframework.stereotype.Component("onboarding-client")
38 public class OnboardingClient {
39
40     private static final Logger log = Logger.getLogger(OnboardingClient.class.getName());
41     private static Properties downloadCsarHeaders = new Properties();
42
43     static {
44         downloadCsarHeaders.put("Accept", "application/octet-stream");
45     }
46
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);
52         }
53         if (userId != null) {
54             headers.put(Constants.USER_ID_HEADER, userId);
55         }
56         log.debug("Url for downloading csar is {}. Headers are {}", url, headers);
57         try {
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);
66                     } else {
67                         log.debug("Data received from rest is null or empty");
68                         return Either.right(StorageOperationStatus.NOT_FOUND);
69                     }
70                 case HttpStatus.SC_NOT_FOUND:
71                     return Either.right(StorageOperationStatus.CSAR_NOT_FOUND);
72                 default:
73                     return Either.right(StorageOperationStatus.GENERAL_ERROR);
74             }
75         } catch (Exception e) {
76             log.debug("Request failed with exception", e);
77             return Either.right(StorageOperationStatus.GENERAL_ERROR);
78         }
79     }
80
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");
85
86         if (userId != null) {
87             headers.put(Constants.USER_ID_HEADER, userId);
88         }
89
90         log.debug("Url for downloading packages is {}. Headers are {}", url, headers);
91
92         try {
93             HttpResponse<String> httpResposne = HttpRequest.get(url, headers);
94             log.debug("After fetching packages. Http return code is {}", httpResposne.getStatusCode());
95
96             switch (httpResposne.getStatusCode()) {
97                 case HttpStatus.SC_OK:
98                     String data = httpResposne.getResponse();
99                     return Either.left(data);
100
101                 case HttpStatus.SC_NOT_FOUND:
102                     return Either.right(StorageOperationStatus.CSAR_NOT_FOUND);
103
104                 default:
105                     return Either.right(StorageOperationStatus.GENERAL_ERROR);
106             }
107         } catch (Exception e) {
108             log.debug("Request failed with exception", e);
109             return Either.right(StorageOperationStatus.GENERAL_ERROR);
110         }
111     }
112
113     /**
114      * Build the url for download CSAR
115      * <p>
116      * E.g., http://0.0.0.0:8181/onboarding-api/v1.0/vendor-software-products/packages/
117      *
118      * @return
119      */
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;
127     }
128 }