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 / chartbuilder / KeyValueMerger.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.chartbuilder;
20
21 import lombok.extern.slf4j.Slf4j;
22 import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.ChartInfo;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.stereotype.Component;
25 import org.yaml.snakeyaml.Yaml;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.PrintWriter;
31 import java.nio.file.Paths;
32 import java.util.Map;
33
34 /**
35  * KeyValueMerger merges helm base templates key-values with key-values parsed from component specification file
36  * @author Dhrumin Desai
37  */
38 @Component
39 @Slf4j
40 public class KeyValueMerger {
41
42     @Autowired
43     private Yaml yaml;
44
45     public KeyValueMerger(Yaml yaml) {
46         this.yaml = yaml;
47     }
48
49     /**
50      * merges helm base templates key-values with key-values parsed from component specification file
51      * @param chartInfo populated ChartInfo object which holds key-values parsed from component spec file
52      * @param chartDir location of the base helm chart template
53      */
54     public void mergeValuesToChart(ChartInfo chartInfo, File chartDir) {
55         mergeChartYamlFile(chartInfo, chartDir);
56         mergeValuesYamlFile(chartInfo, chartDir);
57     }
58
59     private void mergeChartYamlFile(ChartInfo chartInfo, File chartDir) {
60         String chartYamlFilePath = Paths.get(chartDir.getAbsolutePath(), "Chart.yaml").toString();
61         checkIfFIleExists(chartYamlFilePath, "Chart.yaml is not found in the given chart template.");
62
63         Map<String, Object> chartYamlKV;
64         try {
65             chartYamlKV = yaml.load(new FileInputStream(chartYamlFilePath));
66             chartYamlKV.put("name", chartInfo.getMetadata().getName());
67             chartYamlKV.put("version", chartInfo.getMetadata().getVersion());
68             chartYamlKV.put("description", chartInfo.getMetadata().getDescription());
69             yaml.dump(chartYamlKV, new PrintWriter(chartYamlFilePath));
70         } catch (FileNotFoundException e) {
71             log.error(e.getMessage());
72         }
73     }
74
75     private void mergeValuesYamlFile(ChartInfo chartInfo, File chartDir) {
76         String valuesYamlFilePath = Paths.get(chartDir.getAbsolutePath(), "values.yaml").toString();
77         checkIfFIleExists(valuesYamlFilePath, "values.yaml is not found in the given chart template.");
78         Map<String, Object> valuesYamlKv;
79         try {
80             valuesYamlKv = yaml.load(new FileInputStream(valuesYamlFilePath));
81             valuesYamlKv.putAll(chartInfo.getValues());
82             yaml.dump(valuesYamlKv, new PrintWriter(valuesYamlFilePath));
83         } catch (FileNotFoundException e) {
84             log.error(e.getMessage());
85         }
86     }
87
88     private void checkIfFIleExists(String filePath, String message) {
89         File valuesYaml = new File(filePath);
90         if (!valuesYaml.exists()) {
91             throw new RuntimeException(message);
92         }
93     }
94 }