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;
21 import com.fasterxml.jackson.databind.ObjectMapper;
22 import lombok.extern.slf4j.Slf4j;
23 import org.apache.commons.io.FileUtils;
24 import org.springframework.stereotype.Component;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
33 * An utility class for various file related tasks.
34 * @author Dhrumin Desai
40 private final ObjectMapper MAPPER = new ObjectMapper();
42 public <T> T deserializeJsonFileToModel(String filePath, Class<T> modelClass) {
43 return deserializeJsonFileToModel(new File(filePath), modelClass);
47 * maps json file to a model class
48 * @param file Json file which holds the data
49 * @param modelClass target model class for mapping
50 * @return mapped model instance
52 public <T> T deserializeJsonFileToModel(File file, Class<T> modelClass) {
54 return MAPPER.readValue(file, modelClass);
55 } catch (IOException e) {
56 log.error(e.getMessage());
57 throw new RuntimeException("Error occurred while converting file to the given model class");
62 * copies dir/file to a temp location on OS
66 public File cloneFileToTempLocation(String srcLocation) {
67 File cloneLocation = null;
69 Path tempRootDir = Files.createTempDirectory("chart");
70 cloneLocation = new File(tempRootDir.toString());
71 log.info("cloning dir/file at : " + tempRootDir);
72 FileUtils.copyDirectory(new File(srcLocation), cloneLocation);
73 } catch (IOException e) {
74 log.error(e.getMessage());
75 throw new RuntimeException("Error occured while cloning file to temp location.");
81 * deletes dir / file from temp location of OS
82 * @param dir dir to be deleted
84 public void deleteTempFileLocation(File dir) {
86 FileUtils.deleteDirectory(dir);
87 } catch (IOException e) {
88 log.warn("Could not delete dir/file: " + dir.getAbsolutePath());
93 * puts value into a map if only exists
96 * @param value a value
98 public void putIfNotNull(Map<String, Object> map, String key, Object value){