Convert streams_publishes and streams_subscribes json strings under applicationConfig...
[dcaegen2/platform.git] / mod2 / helm-generator / helmchartgenerator-core / src / main / java / org / onap / dcaegen2 / platform / helmchartgenerator / chartbuilder / ComponentSpecParser.java
index 942e5ae..9a9f8f1 100644 (file)
@@ -18,7 +18,9 @@
 
 package org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder;
 
-import org.jetbrains.annotations.NotNull;
+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;
@@ -40,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;
@@ -49,18 +52,24 @@ 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 {
 
     @Autowired
     private ComponentSpecValidator specValidator;
 
+    @Autowired
+    private Utils utils;
+
     /**
      * Constructor for ComponentSpecParser
      * @param specValidator ComponentSpecValidator implementation
+     * @param utils
      */
-    public ComponentSpecParser(ComponentSpecValidator specValidator) {
+    public ComponentSpecParser(ComponentSpecValidator specValidator, Utils utils) {
         this.specValidator = specValidator;
+        this.utils = utils;
     }
 
     /**
@@ -73,7 +82,7 @@ public class ComponentSpecParser {
      */
     public ChartInfo extractChartInfo(String specFileLocation, String chartTemplateLocation, String specSchemaLocation) throws Exception {
         specValidator.validateSpecFile(specFileLocation, specSchemaLocation);
-        ComponentSpec cs = Utils.deserializeJsonFileToModel(specFileLocation, ComponentSpec.class);
+        ComponentSpec cs = utils.deserializeJsonFileToModel(specFileLocation, ComponentSpec.class);
         ChartInfo chartInfo = new ChartInfo();
         chartInfo.setMetadata(extractMetadata(cs.getSelf()));
         chartInfo.setValues(extractValues(cs, chartTemplateLocation));
@@ -83,14 +92,14 @@ public class ComponentSpecParser {
     private Map<String, Object> extractValues(ComponentSpec cs, String chartTemplateLocation) {
         Map<String, Object> outerValues = new LinkedHashMap<>();
         if(cs.getAuxilary() != null && cs.getAuxilary().getTlsInfo() != null){
-            Utils.putIfNotNull(outerValues,"certDirectory", cs.getAuxilary().getTlsInfo().getCertDirectory());
-            Utils.putIfNotNull(outerValues, "tlsServer", cs.getAuxilary().getTlsInfo().getUseTls());
+            utils.putIfNotNull(outerValues,"certDirectory", cs.getAuxilary().getTlsInfo().getCertDirectory());
+            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"));
+            utils.putIfNotNull(outerValues,"logDirectory", cs.getAuxilary().getLogInfo().get("log_directory"));
         }
         if(imageUriExistsForFirstArtifact(cs)){
-            Utils.putIfNotNull(outerValues,"image", cs.getArtifacts()[0].getUri());
+            utils.putIfNotNull(outerValues,"image", cs.getArtifacts()[0].getUri());
         }
         populateApplicationConfigSection(outerValues, cs);
         populateReadinessSection(outerValues, cs);
@@ -113,9 +122,26 @@ 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);
+        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) {
@@ -124,7 +150,7 @@ public class ComponentSpecParser {
 
         Map<String, Object> readiness = new LinkedHashMap<>();
         final HealthCheck healthcheck = cs.getAuxilary().getHealthcheck();
-        Utils.putIfNotNull(readiness, "initialDelaySeconds", healthcheck.getInitialDelaySeconds());
+        utils.putIfNotNull(readiness, "initialDelaySeconds", healthcheck.getInitialDelaySeconds());
 
         if(healthcheck.getInterval() != null) {
             readiness.put("periodSeconds", getSeconds(healthcheck.getInterval(), "interval"));
@@ -159,7 +185,7 @@ public class ComponentSpecParser {
     private void populateApplicationEnvSection(Map<String, Object> outerValues, ComponentSpec cs){
         if(applicationEnvExists(cs)) {
             Object applicationEnv = cs.getAuxilary().getHelm().getApplicationEnv();
-            Utils.putIfNotNull(outerValues,"applicationEnv", applicationEnv);
+            utils.putIfNotNull(outerValues,"applicationEnv", applicationEnv);
         }
     }
 
@@ -178,10 +204,10 @@ public class ComponentSpecParser {
         if(serviceFromSpec.getPorts() != null){
             List<Object> ports = mapServicePorts(serviceFromSpec.getPorts());
             service.put("ports", ports);
-            Utils.putIfNotNull(service, "type", serviceFromSpec.getType());
+            utils.putIfNotNull(service, "type", serviceFromSpec.getType());
         }
-        Utils.putIfNotNull(service,"name", serviceFromSpec.getName());
-        Utils.putIfNotNull(service,"has_internal_only_ports", serviceFromSpec.getHasInternalOnlyPorts());
+        utils.putIfNotNull(service,"name", serviceFromSpec.getName());
+        utils.putIfNotNull(service,"has_internal_only_ports", serviceFromSpec.getHasInternalOnlyPorts());
         outerValues.put("service", service);
     }
 
@@ -230,8 +256,8 @@ public class ComponentSpecParser {
             keystore.put("outputType", List.of("jks"));
             keystore.put("passwordSecretRef", passwordsSecretRef);
             certificate.put("mountPath", mountPath);
-            Utils.putIfNotNull(certificate,"commonName", cs.getSelf().getName());
-            Utils.putIfNotNull(certificate,"dnsNames", List.of(cs.getSelf().getName()));
+            utils.putIfNotNull(certificate,"commonName", cs.getSelf().getName());
+            utils.putIfNotNull(certificate,"dnsNames", List.of(cs.getSelf().getName()));
             certificate.put("keystore", keystore);
             outerValues.put("certificates", List.of(certificate));
         }