Add cmpv2Certificate flag, removed hyphens from config under postgres and enhanced...
[dcaegen2/platform.git] / mod2 / helm-generator / helmchartgenerator-core / src / main / java / org / onap / dcaegen2 / platform / helmchartgenerator / chartbuilder / AddOnsManager.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.apache.commons.io.FileUtils;
23 import org.onap.dcaegen2.platform.helmchartgenerator.Utils;
24 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.base.ComponentSpec;
25 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.common.TlsInfo;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.stereotype.Component;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34
35 /**
36  * manages addOn template files
37  */
38 @Slf4j
39 @Component
40 public class AddOnsManager {
41
42     public AddOnsManager(Utils utils) {
43         this.utils = utils;
44     }
45
46     @Autowired
47     private Utils utils;
48
49     /**
50      * include addons template files based on parameters in componentSpec file
51      * @param specFileLocation spec file location
52      * @param chart chart directory
53      * @param chartTemplateLocation chart template location
54      */
55     public void includeAddons(String specFileLocation, File chart, String chartTemplateLocation) {
56         if(externalTlsExists(specFileLocation)){
57             includeCertificateYamlAddOn(chart, chartTemplateLocation);
58         }
59     }
60
61     private void includeCertificateYamlAddOn(File chart, String chartTemplateLocation) {
62         Path certificateYaml = Paths.get(chartTemplateLocation, "addons/templates/certificates.yaml");
63         if(!Files.exists(certificateYaml)) {
64             throw new RuntimeException("certificates.yaml not found under templates directory in addons");
65         }
66         try {
67             File templates = new File(chart, "templates");
68             FileUtils.copyFileToDirectory(certificateYaml.toFile(), templates);
69         } catch (IOException e) {
70             log.error(e.getMessage(), e);
71             throw new RuntimeException("could not add certificates.yaml to templates directory");
72         }
73     }
74
75     private boolean externalTlsExists(String specFileLocation) {
76         ComponentSpec cs = utils.deserializeJsonFileToModel(specFileLocation, ComponentSpec.class);
77         TlsInfo tlsInfo = cs.getAuxilary().getTlsInfo();
78         return tlsInfo != null && tlsInfo.getUseExternalTls() != null && tlsInfo.getUseExternalTls();
79     }
80 }
81