Helm-generator - seedcode delivery for helm chart generation tool from component...
[dcaegen2/platform.git] / mod2 / helm-generator / helmchartgenerator-core / src / main / java / org / onap / dcaegen2 / platform / helmchartgenerator / validation / ComponentSpecValidatorImpl.java
1 /*
2  * # ============LICENSE_START=======================================================
3  * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
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  * #
11  * # Unless required by applicable law or agreed to in writing, software
12  * # distributed under the License is distributed on an "AS IS" BASIS,
13  * # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * # See the License for the specific language governing permissions and
15  * # limitations under the License.
16  * # ============LICENSE_END=========================================================
17  */
18
19 package org.onap.dcaegen2.platform.helmchartgenerator.validation;
20
21 import lombok.extern.slf4j.Slf4j;
22 import org.apache.commons.io.FileUtils;
23 import org.everit.json.schema.Schema;
24 import org.everit.json.schema.loader.SchemaLoader;
25 import org.json.JSONObject;
26 import org.json.JSONTokener;
27 import org.onap.dcaegen2.platform.helmchartgenerator.Utils;
28 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.base.ComponentSpec;
29 import org.springframework.core.io.ClassPathResource;
30 import org.springframework.stereotype.Service;
31
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.FileNotFoundException;
35 import java.io.IOException;
36 import java.io.InputStream;
37
38 /**
39  * A class for Component specification validation.
40  * @author Dhrumin Desai
41  */
42 @Service
43 @Slf4j
44 public class ComponentSpecValidatorImpl implements ComponentSpecValidator {
45
46     /**
47      * Validates the spec json file  against schema and prints errors if found
48      * @param specFileLocation specification json file location
49      * @param specSchemaLocation json schema file location
50      * @throws IOException
51      */
52     @Override
53     public void validateSpecFile(String specFileLocation, String specSchemaLocation) throws IOException {
54         File schemaFile = getSchemaFile(specSchemaLocation);
55         ComponentSpec cs = Utils.deserializeJsonFileToModel(specFileLocation, ComponentSpec.class);
56         validateSpecSchema(new File(specFileLocation), schemaFile);
57         validateHelmRequirements(cs);
58     }
59
60     private File getSchemaFile(String specSchemaLocation) throws IOException {
61         if(specSchemaLocation.isEmpty()){
62             return defaultSchemaFile();
63         }
64         else{
65             return new File(specSchemaLocation);
66         }
67     }
68
69     private File defaultSchemaFile() throws IOException {
70         File schemaFile = File.createTempFile("schema", ".json");
71         InputStream inputStream = new ClassPathResource("component-spec-schema.json").getInputStream();
72         FileUtils.copyInputStreamToFile(inputStream, schemaFile);
73         return schemaFile;
74     }
75
76     private void validateSpecSchema(File specFile, File schemaFile) {
77         try {
78             JSONTokener schemaData = new JSONTokener(new FileInputStream(schemaFile));
79             JSONObject jsonSchema = new JSONObject(schemaData);
80
81             JSONTokener jsonDataFile = new JSONTokener(new FileInputStream(specFile));
82             JSONObject jsonObject = new JSONObject(jsonDataFile);
83
84             Schema schemaValidator = SchemaLoader.load(jsonSchema);
85             schemaValidator.validate(jsonObject);
86         } catch (FileNotFoundException e) {
87             log.error(e.getMessage());
88             throw new RuntimeException("Specfile or Schemafile not found.");
89         }
90     }
91
92     private void validateHelmRequirements(ComponentSpec cs) {
93         checkHealthCheckSection(cs);
94         checkHelmSection(cs);
95     }
96
97     private void checkHealthCheckSection(ComponentSpec cs) {
98         if(cs.getAuxilary().getHealthcheck().getPort() == null) {
99             throw new RuntimeException("port in healthcheck section is a required field but was not found");
100         }
101     }
102
103     private void checkHelmSection(ComponentSpec cs) {
104         if(cs.getAuxilary().getHelm() == null){
105             throw new RuntimeException("helm section in component spec is required but was not found");
106         }
107     }
108 }