package org.opentosca.toscana.plugins.cloudfoundry;

import java.io.IOException;
import java.util.Map;

import org.opentosca.toscana.core.plugin.PluginFileAccess;

/**
 Creates all files which are necessary to deploy the application
 Files: manifest.yml, builpack additions, deployScript
 */
public class CloudFoundryFileCreator {

    private final PluginFileAccess fileAccess;
    private final CloudFoundryApplication app;

    public CloudFoundryFileCreator(PluginFileAccess fileAccess, CloudFoundryApplication app) {
        this.fileAccess = fileAccess;
        this.app = app;
    }

    public void createFiles() throws IOException {
        createManifest();
        createBuildpackAdditionsFile();
        createDeployScript();
    }

    private void createManifest() throws IOException {
        String manifestContent;
        manifestContent = createManifestHead();
        manifestContent += createEnvironmentVariables();
        manifestContent += createService();
        fileAccess.access("/manifest.yml").append(manifestContent).close();
    }

    private String createManifestHead() {
        return "applications: \n- name: " + app.getAppName();
    }

    private String createEnvironmentVariables() {
        String envBlock;
        envBlock = "\n  env:";
        for (Map.Entry<String, String> entry : app.getEnvironmentVariables().entrySet()) {
            envBlock = envBlock + "\n    " + entry.getKey() + ": " + entry.getValue();
        }
        return envBlock;
    }

    private String createService() {
        String serviceBlock;
        serviceBlock = "\n  service:";
        for (String service : app.getServices()) {
            serviceBlock += "\n    - " + service;
        }
        return serviceBlock;
    }

    private void createDeployScript() throws IOException {
        fileAccess.access("/deploy_" + app.getAppName() + ".sh")
            .append("cf -push " + app.getAppName()).close();
    }

    private void createBuildpackAdditionsFile() throws IOException {
        String buildPackAdditions = "{\"PHP-EXTENSIONS\":[";
        for (String bp : app.getBuildpackAdditions()) {
            buildPackAdditions += "\"" + bp + "\",";
        }
        buildPackAdditions += "]}";
        fileAccess.access("/.bp-config/options.json").append(buildPackAdditions).close();
    }
}

@@ -157,16 +157,103 @@
 ccess;%0A%0A
+import org.json.JSONArray;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0A
 /**%0A Cre
@@ -711,32 +711,47 @@
 rows IOException
+, JSONException
  %7B%0A        creat
@@ -1899,32 +1899,251 @@
 s IOException %7B%0A
+        String deploy = null;%0A        for (String service : app.getServices()) %7B%0A            deploy += %22cf create-service %7Bplan%7D %7Bservice%7D %22 + service + %22%5Cn%22;%0A        %7D%0A        deploy += %22cf push %22 + app.getAppName();%0A%0A
         fileAcce
@@ -2215,55 +2215,50 @@
 end(
-%22cf -push %22 + app.getAppName()).close();%0A    %7D%0A
+deploy).close();%0A    %7D%0A%0A    //only for PHP
 %0A   
@@ -2312,32 +2312,47 @@
 rows IOException
+, JSONException
  %7B%0A        Strin
@@ -2346,22 +2346,26 @@
         
-String
+JSONObject
  buildPa
@@ -2379,34 +2379,79 @@
 ions
- = %22%7B%5C%22PHP-EXTENSIONS%5C%22:%5B%22
+Json = new JSONObject();%0A        JSONArray buildPacks = new JSONArray()
 ;%0A  
@@ -2469,17 +2469,24 @@
 String b
-p
+uildPack
  : app.g
@@ -2536,38 +2536,24 @@
 Pack
-Additions += %22%5C%22%22 + bp + %22%5C%22,%22
+s.put(buildPack)
 ;%0A  
@@ -2590,16 +2590,46 @@
 ions
- += %22%5D%7D%22
+Json.put(%22PHP-EXTENSIONS%22, buildPacks)
 ;%0A  
@@ -2705,16 +2705,31 @@
 dditions
+Json.toString()
 ).close(

Change build pack creation in the cloud foundry file creator (#282)