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.beans.factory.annotation.Autowired;
30 import org.springframework.core.io.ClassPathResource;
31 import org.springframework.stereotype.Service;
34 import java.io.FileInputStream;
35 import java.io.FileNotFoundException;
36 import java.io.IOException;
37 import java.io.InputStream;
40 * A class for Component specification validation.
41 * @author Dhrumin Desai
45 public class ComponentSpecValidatorImpl implements ComponentSpecValidator {
50 public ComponentSpecValidatorImpl(Utils utils) {
55 * Validates the spec json file against schema and prints errors if found
56 * @param specFileLocation specification json file location
57 * @param specSchemaLocation json schema file location
61 public void validateSpecFile(String specFileLocation, String specSchemaLocation) throws IOException {
62 File schemaFile = getSchemaFile(specSchemaLocation);
63 ComponentSpec cs = utils.deserializeJsonFileToModel(specFileLocation, ComponentSpec.class);
64 validateSpecSchema(new File(specFileLocation), schemaFile);
65 validateHelmRequirements(cs);
68 private File getSchemaFile(String specSchemaLocation) throws IOException {
69 if(specSchemaLocation.isEmpty()){
70 return defaultSchemaFile();
73 return new File(specSchemaLocation);
77 private File defaultSchemaFile() throws IOException {
78 File schemaFile = File.createTempFile("schema", ".json");
79 InputStream inputStream = new ClassPathResource("component-spec-schema.json").getInputStream();
80 FileUtils.copyInputStreamToFile(inputStream, schemaFile);
84 private void validateSpecSchema(File specFile, File schemaFile) {
86 JSONTokener schemaData = new JSONTokener(new FileInputStream(schemaFile));
87 JSONObject jsonSchema = new JSONObject(schemaData);
89 JSONTokener jsonDataFile = new JSONTokener(new FileInputStream(specFile));
90 JSONObject jsonObject = new JSONObject(jsonDataFile);
92 Schema schemaValidator = SchemaLoader.load(jsonSchema);
93 schemaValidator.validate(jsonObject);
94 } catch (FileNotFoundException e) {
95 log.error(e.getMessage());
96 throw new RuntimeException("Specfile or Schemafile not found.");
100 private void validateHelmRequirements(ComponentSpec cs) {
101 checkHealthCheckSection(cs);
102 checkHelmSection(cs);
105 private void checkHealthCheckSection(ComponentSpec cs) {
106 if(cs.getAuxilary().getHealthcheck().getPort() == null) {
107 throw new RuntimeException("port in healthcheck section is a required field but was not found");
111 private void checkHelmSection(ComponentSpec cs) {
112 if(cs.getAuxilary().getHelm() == null){
113 throw new RuntimeException("helm section in component spec is required but was not found");