272d0a88b977915d26a0f0c28907b2b977776361
[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.validators;
20
21 import com.google.gson.Gson;
22 import java.io.InputStream;
23 import java.util.Optional;
24 import java.util.Set;
25 import org.onap.config.api.ConfigurationManager;
26 import org.openecomp.core.validation.ErrorMessageCode;
27 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
28 import org.openecomp.core.validation.types.GlobalValidationContext;
29 import org.openecomp.sdc.common.http.client.api.HttpRequestHandler;
30 import org.openecomp.sdc.datatypes.error.ErrorLevel;
31 import org.openecomp.sdc.heat.datatypes.manifest.FileData.Type;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.validation.Validator;
35 import org.openecomp.sdc.validation.impl.util.HelmValidatorConfigReader;
36 import org.openecomp.sdc.validation.impl.util.HelmValidatorHttpClient;
37 import org.openecomp.sdc.validation.type.helmvalidator.HelmValidatorConfig;
38 import org.openecomp.sdc.validation.type.helmvalidator.HelmValidatorErrorResponse;
39 import org.openecomp.sdc.validation.type.helmvalidator.HelmValidatorResponse;
40
41 public class HelmValidator implements Validator {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(HelmValidator.class);
44     private static final ErrorMessageCode VALIDATOR_ERROR_CODE = new ErrorMessageCode("HELM VALIDATOR");
45     private static final String EXCEPTION_MESSAGE = "Could not execute file %s validation using Helm";
46
47     private final HelmValidatorHttpClient helmValidatorHttpClient;
48     private final HelmValidatorConfig helmValidatorConfig;
49
50     public HelmValidator() {
51         this(new HelmValidatorHttpClient(HttpRequestHandler.get()),
52             new HelmValidatorConfigReader(ConfigurationManager.lookup()).getHelmValidatorConfig());
53     }
54
55     HelmValidator(HelmValidatorHttpClient helmValidatorHttpClient, HelmValidatorConfig helmValidatorConfig) {
56         this.helmValidatorHttpClient = helmValidatorHttpClient;
57         this.helmValidatorConfig = helmValidatorConfig;
58     }
59
60     @Override
61     public void validate(GlobalValidationContext globalContext) {
62         if (helmValidatorConfig.isEnabled()) {
63             Set<String> manifestFiles = GlobalContextUtil.findFilesByType(globalContext, Type.HELM);
64             manifestFiles.forEach(file -> tryValidateSingleChart(globalContext, file));
65         }
66     }
67
68     private void tryValidateSingleChart(GlobalValidationContext globalContext, String fileName) {
69         Optional<InputStream> fileContent = globalContext.getFileContent(fileName);
70         if (fileContent.isPresent()) {
71             try {
72                 validateSingleHelmChart(fileName, fileContent.get().readAllBytes(), globalContext);
73             } catch (Exception exception) {
74                 String validationErrorMessage = String.format(EXCEPTION_MESSAGE, fileName);
75                 LOGGER.error(validationErrorMessage + " exception: " + exception.getMessage());
76                 addError(fileName, globalContext, validationErrorMessage, ErrorLevel.WARNING);
77             }
78         } else {
79             LOGGER.debug("File content is not present " + fileName);
80         }
81     }
82
83     private void validateSingleHelmChart(String fileName, byte[] file, GlobalValidationContext globalContext)
84         throws Exception {
85         var httpResponse = helmValidatorHttpClient.execute(fileName, file, helmValidatorConfig);
86         if (httpResponse.getStatusCode() == 200) {
87             var helmValidatorResponse = new Gson()
88                 .fromJson(httpResponse.getResponse(), HelmValidatorResponse.class);
89             helmValidatorResponse.getRenderErrors().forEach(error ->
90                 addError(fileName, globalContext, error, ErrorLevel.ERROR));
91             helmValidatorResponse.getLintError().forEach(lintError ->
92                 addError(fileName, globalContext, lintError, ErrorLevel.WARNING));
93             helmValidatorResponse.getLintWarning().forEach(lintWarning ->
94                 addError(fileName, globalContext, lintWarning, ErrorLevel.WARNING));
95         } else {
96             var errorResponse = new Gson().fromJson(httpResponse.getResponse(), HelmValidatorErrorResponse.class);
97             addError(fileName, globalContext, errorResponse.getMessage(), ErrorLevel.WARNING);
98         }
99     }
100
101     private void addError(String fileName, GlobalValidationContext globalContext, String error, ErrorLevel level) {
102         globalContext.addMessage(fileName, level, ErrorMessagesFormatBuilder
103             .getErrorWithParameters(VALIDATOR_ERROR_CODE, error, fileName));
104     }
105
106 }