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
9 * # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
19 package org.onap.dcaegen2.platform.helmchartgenerator.validation;
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;
33 import java.io.FileInputStream;
34 import java.io.FileNotFoundException;
35 import java.io.IOException;
36 import java.io.InputStream;
39 * A class for Component specification validation.
40 * @author Dhrumin Desai
44 public class ComponentSpecValidatorImpl implements ComponentSpecValidator {
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
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);
60 private File getSchemaFile(String specSchemaLocation) throws IOException {
61 if(specSchemaLocation.isEmpty()){
62 return defaultSchemaFile();
65 return new File(specSchemaLocation);
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);
76 private void validateSpecSchema(File specFile, File schemaFile) {
78 JSONTokener schemaData = new JSONTokener(new FileInputStream(schemaFile));
79 JSONObject jsonSchema = new JSONObject(schemaData);
81 JSONTokener jsonDataFile = new JSONTokener(new FileInputStream(specFile));
82 JSONObject jsonObject = new JSONObject(jsonDataFile);
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.");
92 private void validateHelmRequirements(ComponentSpec cs) {
93 checkHealthCheckSection(cs);
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");
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");