package org.safehaus.subutai.plugin.flume.rest;

import java.util.*;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import org.safehaus.subutai.api.agentmanager.AgentManager;
import org.safehaus.subutai.plugin.flume.api.Flume;
import org.safehaus.subutai.plugin.flume.api.FlumeConfig;
import org.safehaus.subutai.common.JsonUtil;
import org.safehaus.subutai.shared.protocol.Agent;

public class RestService {

    private static final String OPERATION_ID = "OPERATION_ID";

    private Flume flumeManager;

    private AgentManager agentManager;

    public void setAgentManager(AgentManager agentManager) {
        this.agentManager = agentManager;
    }

    public void setFlumeManager(Flume flumeManager) {
        this.flumeManager = flumeManager;
    }

    @GET
    @Path("getClusters")
    @Produces({MediaType.APPLICATION_JSON})
    public String getClusters() {

        List<FlumeConfig> configs = flumeManager.getClusters();
        ArrayList<String> clusterNames = new ArrayList();

        for(FlumeConfig config : configs) {
            clusterNames.add(config.getClusterName());
        }

        return JsonUtil.GSON.toJson(clusterNames);
    }

    @GET
    @Path("getCluster")
    @Produces({MediaType.APPLICATION_JSON})
    public String getCluster(
            @QueryParam("clusterName") String clusterName
    ) {
        FlumeConfig config = flumeManager.getCluster(clusterName);

        return JsonUtil.GSON.toJson(config);
    }

    @GET
    @Path("installCluster")
    @Produces({MediaType.APPLICATION_JSON})
    public String installCluster(
            @QueryParam("clusterName") String clusterName,
            @QueryParam("nodes") String nodes
    ) {

        FlumeConfig config = new FlumeConfig();
        config.setClusterName(clusterName);

        String[] arr = nodes.split("[,;]");
        for(String node : arr) {
            Agent agent = agentManager.getAgentByHostname(node);
            if(agent != null) config.getNodes().add(agent);
        }

        UUID uuid = flumeManager.installCluster(config);

        return JsonUtil.toJson(OPERATION_ID, uuid);
    }

    @GET
    @Path("uninstallCluster")
    @Produces({MediaType.APPLICATION_JSON})
    public String uninstallCluster(
            @QueryParam("clusterName") String clusterName
    ) {
        UUID uuid = flumeManager.uninstallCluster(clusterName);

        return JsonUtil.toJson(OPERATION_ID, uuid);
    }

    @GET
    @Path("addNode")
    @Produces({MediaType.APPLICATION_JSON})
    public String addNode(
            @QueryParam("clusterName") String clusterName,
            @QueryParam("node") String node
    ) {
        UUID uuid = flumeManager.addNode(clusterName, node);

        return JsonUtil.toJson(OPERATION_ID, uuid);
    }

    @GET
    @Path("destroyNode")
    @Produces({MediaType.APPLICATION_JSON})
    public String destroyNode(
            @QueryParam("clusterName") String clusterName,
            @QueryParam("node") String node
    ) {
        UUID uuid = flumeManager.destroyNode(clusterName, node);

        return JsonUtil.toJson(OPERATION_ID, uuid);
    }

    @GET
    @Path("startNode")
    @Produces({MediaType.APPLICATION_JSON})
    public String startNode(
            @QueryParam("clusterName") String clusterName,
            @QueryParam("node") String node
    ) {
        UUID uuid = flumeManager.startNode(clusterName, node);

        return JsonUtil.toJson(OPERATION_ID, uuid);
    }

    @GET
    @Path("stopNode")
    @Produces({MediaType.APPLICATION_JSON})
    public String stopNode(
            @QueryParam("clusterName") String clusterName,
            @QueryParam("node") String node
    ) {
        UUID uuid = flumeManager.stopNode(clusterName, node);

        return JsonUtil.toJson(OPERATION_ID, uuid);
    }

    @GET
    @Path("checkNode")
    @Produces({MediaType.APPLICATION_JSON})
    public String checkNode(
            @QueryParam("clusterName") String clusterName,
            @QueryParam("node") String node
    ) {
        UUID uuid = flumeManager.checkNode(clusterName, node);

        return JsonUtil.toJson(OPERATION_ID, uuid);
    }
}

@@ -178,16 +178,61 @@
 anager;%0A
+import org.safehaus.subutai.common.JsonUtil;%0A
 import o
@@ -357,39 +357,117 @@
 aus.subutai.
-common.JsonUtil
+plugin.flume.api.SetupType;%0Aimport org.safehaus.subutai.plugin.hadoop.api.HadoopClusterConfig
 ;%0Aimport org
@@ -1856,24 +1856,76 @@
 meConfig();%0A
+        config.setSetupType(SetupType.OVER_HADOOP);%0A
         conf
@@ -2279,32 +2279,1261 @@
 , uuid);%0A    %7D%0A%0A
+    @GET%0A    @Path(%22install/%7Bname%7D/%7BhadoopName%7D/%7BslaveNodesCount%7D/%7BreplFactor%7D/%7BdomainName%7D%22)%0A    @Produces(%7BMediaType.APPLICATION_JSON%7D)%0A    public String install(@PathParam(%22name%22) String name,%0A            @PathParam(%22hadoopName%22) String hadoopName,%0A            @PathParam(%22slaveNodesCount%22) String slaveNodesCount,%0A            @PathParam(%22replFactor%22) String replFactor,%0A            @PathParam(%22domainName%22) String domainName) %7B%0A%0A        FlumeConfig config = new FlumeConfig();%0A        config.setClusterName(name);%0A        config.setHadoopClusterName(hadoopName);%0A        config.setSetupType(SetupType.WITH_HADOOP);%0A%0A        HadoopClusterConfig hc = new HadoopClusterConfig();%0A        hc.setClusterName(hadoopName);%0A        if(domainName != null) hc.setDomainName(domainName);%0A        try %7B%0A            int i = Integer.parseInt(slaveNodesCount);%0A            hc.setCountOfSlaveNodes(i);%0A        %7D catch(NumberFormatException ex) %7B%0A        %7D%0A        try %7B%0A            int i = Integer.parseInt(replFactor);%0A            hc.setReplicationFactor(i);%0A        %7D catch(NumberFormatException ex) %7B%0A        %7D%0A%0A        UUID trackId = flumeManager.installCluster(config, hc);%0A%0A        return JsonUtil.toJson(OPERATION_ID, trackId);%0A    %7D%0A%0A
     @GET%0A    @Pa

add REST endpoint to install using templates