Merge "[MOD/Runtime] Upgrade to Helmgen-core 1.0.4"
[dcaegen2/platform.git] / mod2 / helm-generator / helmchartgenerator-core / src / main / java / org / onap / dcaegen2 / platform / helmchartgenerator / chartbuilder / ComponentSpecParser.java
index b0830d1..91fedf5 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * # ============LICENSE_START=======================================================
- * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
+ * # Copyright (c) 2021-2022 AT&T Intellectual Property. All rights reserved.
  * # ================================================================================
  * # Licensed under the Apache License, Version 2.0 (the "License");
  * # you may not use this file except in compliance with the License.
@@ -18,6 +18,9 @@
 
 package org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import lombok.extern.slf4j.Slf4j;
 import org.onap.dcaegen2.platform.helmchartgenerator.Utils;
 import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.ChartInfo;
 import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.Metadata;
@@ -39,6 +42,7 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -48,6 +52,7 @@ import java.util.Map;
  * ComponentSpecParser reads a componentspec file and collects useful data for helm chart generation.
  * @author Dhrumin Desai
  */
+@Slf4j
 @Component
 public class ComponentSpecParser {
 
@@ -91,7 +96,9 @@ public class ComponentSpecParser {
             utils.putIfNotNull(outerValues, "tlsServer", cs.getAuxilary().getTlsInfo().getUseTls());
         }
         if(cs.getAuxilary() != null && cs.getAuxilary().getLogInfo() != null) {
-            utils.putIfNotNull(outerValues,"logDirectory", cs.getAuxilary().getLogInfo().get("log_directory"));
+            Map<String, Object> logPath = new LinkedHashMap<>();
+            logPath.put("path", cs.getAuxilary().getLogInfo().get("log_directory"));
+            outerValues.put("log", logPath);
         }
         if(imageUriExistsForFirstArtifact(cs)){
             utils.putIfNotNull(outerValues,"image", cs.getArtifacts()[0].getUri());
@@ -117,11 +124,28 @@ public class ComponentSpecParser {
         Map<String, Object> applicationConfig = new LinkedHashMap<>();
          Parameters[] parameters = cs.getParameters();
          for(Parameters param : parameters){
-            applicationConfig.put(param.getName(), param.getValue());
+             if (Arrays.asList("streams_publishes", "streams_subscribes").contains(param.getName())){
+                 applicationConfig.put(param.getName(), parseStringToMap(param.getValue()));
+             }else
+             {
+                 applicationConfig.put(param.getName(), param.getValue());
+             }
          }
         utils.putIfNotNull(outerValues,"applicationConfig", applicationConfig);
     }
 
+    private Object parseStringToMap(Object value) {
+        if (value instanceof String){
+            try {
+                return new ObjectMapper().readValue((String)value, Map.class);
+            } catch (JsonProcessingException e) {
+                log.error(e.getMessage(), e);
+                log.warn("could not parse streams_publishes / streams_subscribes. Default value will be used.");
+            }
+        }
+        return value;
+    }
+
     private void populateReadinessSection(Map<String, Object> outerValues, ComponentSpec cs) {
 
         if (!healthCheckExists(cs)) return;
@@ -221,7 +245,8 @@ public class ComponentSpecParser {
         Map<String, Object> keystore = new LinkedHashMap<>();
         Map<String, Object> passwordsSecretRef = new LinkedHashMap<>();
         TlsInfo tlsInfo = cs.getAuxilary().getTlsInfo();
-        String componentName = getComponentNameWithOmitFirstWord(cs);
+        String componentName = getComponentNameWithOmitFirstWordAndTrimHyphens(cs);
+        outerValues.put("useCmpv2Certificates", false);
         if(externalTlsExists(tlsInfo)) {
             String mountPath = tlsInfo.getCertDirectory();
             if(tlsInfo.getUseExternalTls() != null && tlsInfo.getUseExternalTls()) {
@@ -238,15 +263,16 @@ public class ComponentSpecParser {
             utils.putIfNotNull(certificate,"dnsNames", List.of(cs.getSelf().getName()));
             certificate.put("keystore", keystore);
             outerValues.put("certificates", List.of(certificate));
+            outerValues.put("useCmpv2Certificates", true);
         }
     }
 
-    private String getComponentNameWithOmitFirstWord(ComponentSpec cs) {
-        return cs.getSelf().getName().substring(cs.getSelf().getName().indexOf("-") + 1);
+    private String getComponentNameWithOmitFirstWordAndTrimHyphens(ComponentSpec cs) {
+        return cs.getSelf().getName().substring(cs.getSelf().getName().indexOf("-") + 1).replaceAll("-","");
     }
 
     private boolean externalTlsExists(TlsInfo tlsInfo) {
-        return tlsInfo != null && tlsInfo.getUseExternalTls() != null && tlsInfo.getUseExternalTls().equals(true);
+        return tlsInfo != null && tlsInfo.getUseExternalTls() != null && tlsInfo.getUseExternalTls();
     }
 
     private void checkCertificateYamlExists(String chartTemplateLocation) {
@@ -279,7 +305,7 @@ public class ComponentSpecParser {
     private void populatePostgresSection(Map<String, Object> outerValues, ComponentSpec cs) {
         if(cs.getAuxilary().getDatabases() != null) {
             String componentFullName = cs.getSelf().getName();
-            String component = getComponentNameWithOmitFirstWord(cs);
+            String component = getComponentNameWithOmitFirstWordAndTrimHyphens(cs);
             Map<String, Object> postgres = new LinkedHashMap<>();
             Map<String, Object> service = new LinkedHashMap<>();
             Map<String, Object> container = new LinkedHashMap<>();
@@ -310,7 +336,7 @@ public class ComponentSpecParser {
 
     private void populateSecretsSection(Map<String, Object> outerValues, ComponentSpec cs) {
         if(cs.getAuxilary().getDatabases() != null) {
-            String component = getComponentNameWithOmitFirstWord(cs);
+            String component = getComponentNameWithOmitFirstWordAndTrimHyphens(cs);
             List<Object> secrets = new ArrayList<>();
             Map<String, Object> secret = new LinkedHashMap<>();
             secret.put("uid", "pg-user-creds");