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.chartbuilder;
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;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.PrintWriter;
31 import java.nio.file.Paths;
35 * KeyValueMerger merges helm base templates key-values with key-values parsed from component specification file
36 * @author Dhrumin Desai
40 public class KeyValueMerger {
45 public KeyValueMerger(Yaml yaml) {
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
54 public void mergeValuesToChart(ChartInfo chartInfo, File chartDir) {
55 mergeChartYamlFile(chartInfo, chartDir);
56 mergeValuesYamlFile(chartInfo, chartDir);
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.");
63 Map<String, Object> chartYamlKV;
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());
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;
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());
88 private void checkIfFIleExists(String filePath, String message) {
89 File valuesYaml = new File(filePath);
90 if (!valuesYaml.exists()) {
91 throw new RuntimeException(message);