integrate helm-chart-generator to runtime api
[dcaegen2/platform.git] / mod / runtimeapi / runtime-core / src / main / java / org / onap / dcae / runtime / core / helm / HelmChartGeneratorClientImpl.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.dcae.runtime.core.helm;
20
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;
29
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.io.IOException;
33 import java.nio.file.Files;
34
35 /**
36  * Implementation class for Helm Chart Generator Client
37  */
38 @Slf4j
39 @Component
40 @ComponentScan(basePackages = "org.onap.dcaegen2.platform.helmchartgenerator")
41 public class HelmChartGeneratorClientImpl implements HelmChartGeneratorClient {
42
43     @Autowired
44     private final ChartBuilder chartBuilder;
45
46     @Autowired
47     private final ChartDistributor distributor;
48
49     @Value("${helm.base.chart.template.location}")
50     private String templateLocation;
51
52     public HelmChartGeneratorClientImpl(ChartBuilder chartBuilder, ChartDistributor distributor) {
53         this.chartBuilder = chartBuilder;
54         this.distributor = distributor;
55     }
56
57     /**
58      * Generate Helm Chart for a component spec
59      * @param componentSpec component spec as String
60      * @return packaged helm chart
61      */
62     @Override
63     public File generateHelmChart(String componentSpec){
64         try {
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.");
70         }
71     }
72
73     private String createTempChartOutputLocation() {
74         try {
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.");
79         }
80     }
81
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);
86         }
87         return tmpFile.getAbsolutePath();
88     }
89
90     /**
91      * Distributes helm chart to Chart Museum
92      * @param helmChart packaged chart location
93      */
94     @Override
95     public void distribute(File helmChart) {
96         try {
97             distributor.distribute(helmChart);
98         }catch (Exception e){
99             throw e;
100         } finally {
101             removeChartLocally(helmChart);
102         }
103     }
104
105     private void removeChartLocally(File helmChart) {
106         try {
107             FileUtils.forceDelete(helmChart);
108         } catch (IOException e) {
109             log.warn("Could not delete a temporary helm chart " + helmChart.getName());
110         }
111     }
112 }