299d99656255ad007d42da9bf9fad8f40c126351
[sdc.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nokia
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19 package org.openecomp.sdc.validation.impl.util;
20
21 import org.apache.http.HttpEntity;
22 import org.apache.http.entity.ContentType;
23 import org.apache.http.entity.mime.MultipartEntityBuilder;
24 import org.openecomp.sdc.common.http.client.api.HttpExecuteException;
25 import org.openecomp.sdc.common.http.client.api.HttpRequestHandler;
26 import org.openecomp.sdc.common.http.client.api.HttpResponse;
27 import org.openecomp.sdc.common.http.config.HttpClientConfig;
28 import org.openecomp.sdc.common.http.config.Timeouts;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import org.openecomp.sdc.validation.type.helmvalidator.HelmValidatorConfig;
32
33 public class HelmValidatorHttpClient {
34
35     private static final Logger LOGGER = LoggerFactory.getLogger(HelmValidatorHttpClient.class);
36     private static final int TIMEOUT_MS = 10000;
37     private static final String FILE = "file";
38     private static final String IS_LINTED = "isLinted";
39     private static final String IS_STRICT_LINTED = "isStrictLinted";
40     private static final String VERSION_DESIRED = "versionDesired";
41     private final HttpRequestHandler httpRequestHandler;
42
43     public HelmValidatorHttpClient(HttpRequestHandler httpRequestHandler) {
44         this.httpRequestHandler = httpRequestHandler;
45     }
46
47     public HttpResponse<String> execute(String fileName, byte[] helmChartFile, HelmValidatorConfig validatorConfig) throws Exception{
48         LOGGER.info("Trying to execute Helm chart validation. File name: {}", fileName);
49         try {
50             HttpEntity entity = MultipartEntityBuilder.create()
51                 .addBinaryBody(FILE, helmChartFile, ContentType.DEFAULT_BINARY, fileName)
52                 .addTextBody(IS_LINTED, getString(validatorConfig.isLintable()))
53                 .addTextBody(IS_STRICT_LINTED, getString(validatorConfig.isStrictLintable()))
54                 .addTextBody(VERSION_DESIRED, validatorConfig.getVersion())
55                 .build();
56
57             HttpResponse<String> httpResponse = httpRequestHandler.post(validatorConfig.getValidatorUrl(),
58                 null, entity, new HttpClientConfig(new Timeouts(TIMEOUT_MS, TIMEOUT_MS)));
59             LOGGER.info("Received response from Helm chart validator with code {}", httpResponse.getStatusCode());
60             LOGGER.debug("Response from Helm chart validator: {}", httpResponse);
61
62             return httpResponse;
63         } catch (HttpExecuteException e) {
64             LOGGER.info("Exception during call to Helm validator {}", e.getMessage());
65         }
66         throw new Exception("Http response is invalid.");
67     }
68
69     private String getString(boolean helmValidatorConfig) {
70         return Boolean.toString(helmValidatorConfig);
71     }
72
73 }