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.dcae.runtime.core.helm;
21 import lombok.extern.slf4j.Slf4j;
22 import org.apache.commons.io.FileUtils;
23 import org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder.ChartBuilder;
24 import org.onap.dcaegen2.platform.helmchartgenerator.distribution.ChartDistributor;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.beans.factory.annotation.Value;
27 import org.springframework.context.annotation.ComponentScan;
28 import org.springframework.stereotype.Component;
31 import java.io.FileWriter;
32 import java.io.IOException;
33 import java.nio.file.Files;
36 * Implementation class for Helm Chart Generator Client
40 @ComponentScan(basePackages = "org.onap.dcaegen2.platform.helmchartgenerator")
41 public class HelmChartGeneratorClientImpl implements HelmChartGeneratorClient {
44 private final ChartBuilder chartBuilder;
47 private final ChartDistributor distributor;
49 @Value("${helm.base.chart.template.location}")
50 private String templateLocation;
52 public HelmChartGeneratorClientImpl(ChartBuilder chartBuilder, ChartDistributor distributor) {
53 this.chartBuilder = chartBuilder;
54 this.distributor = distributor;
58 * Generate Helm Chart for a component spec
59 * @param componentSpec component spec as String
60 * @return packaged helm chart
63 public File generateHelmChart(String componentSpec){
65 return chartBuilder.build(createTempSpecFile(componentSpec),templateLocation,
66 createTempChartOutputLocation(), "");
67 } catch (Exception e) {
68 log.error(e.getMessage(), e);
69 throw new RuntimeException("Error while generating the helm chart.");
73 private String createTempChartOutputLocation() {
75 return Files.createTempDirectory("chart").toAbsolutePath().toString();
76 } catch (IOException e) {
77 log.error(e.getMessage(), e);
78 throw new RuntimeException("Error creating a temporary chart dir.");
82 private String createTempSpecFile(String componentSpec) throws IOException{
83 File tmpFile = File.createTempFile("spec",".json");
84 try (FileWriter writer = new FileWriter(tmpFile)) {
85 writer.write(componentSpec);
87 return tmpFile.getAbsolutePath();
91 * Distributes helm chart to Chart Museum
92 * @param helmChart packaged chart location
95 public void distribute(File helmChart) {
97 distributor.distribute(helmChart);
101 removeChartLocally(helmChart);
105 private void removeChartLocally(File helmChart) {
107 FileUtils.forceDelete(helmChart);
108 } catch (IOException e) {
109 log.warn("Could not delete a temporary helm chart " + helmChart.getName());