package org.safehaus.subutai.flume.services;


import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.safehaus.subutai.api.agentmanager.AgentManager;
import org.safehaus.subutai.api.flume.Config;
import org.safehaus.subutai.api.flume.Flume;
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<Config> configs = flumeManager.getClusters();
        ArrayList<String> clusterNames = new ArrayList();

        for ( Config 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
    ) {
        Config 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" ) List<String> nodes
    ) {

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

        for ( String node : nodes ) {
            Agent agent = agentManager.getAgentByHostname( node );
            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 );
    }

}

@@ -114,17 +114,16 @@
 l.UUID;%0A
-%0A
 import j
@@ -258,17 +258,16 @@
 iaType;%0A
-%0A
 import o
@@ -1814,28 +1814,22 @@
 odes%22 ) 
-List%3C
 String
-%3E
  nodes%0A 
@@ -1913,32 +1913,76 @@
 clusterName );%0A%0A
+        String%5B%5D arr = nodes.split(%22%5B,;%5D%22);%0A
         for ( St
@@ -1976,19 +1976,17 @@
      for
- ( 
+(
 String n
@@ -1995,14 +1995,11 @@
 e : 
-nodes 
+arr
 ) %7B%0A
@@ -2056,22 +2056,20 @@
 ostname(
- 
 node
- 
 );%0A     
@@ -2070,24 +2070,42 @@
 %0A           
+ if(agent != null)
  config.getN
@@ -2115,23 +2115,21 @@
 s().add(
- 
 agent
- 
 );%0A     
@@ -2217,33 +2217,32 @@
 JsonUtil.toJson(
- 
 OPERATION_ID, uu
@@ -2235,33 +2235,32 @@
 ERATION_ID, uuid
- 
 );%0A    %7D%0A%0A%0A    @

fix REST installation