package org.safehaus.subutai.plugin.flume.impl.handler;

import org.safehaus.subutai.plugin.flume.impl.FlumeImpl;
import org.safehaus.subutai.plugin.flume.impl.Commands;
import org.safehaus.subutai.plugin.flume.impl.CommandType;
import java.util.Arrays;
import java.util.HashSet;
import org.safehaus.subutai.api.commandrunner.*;
import org.safehaus.subutai.plugin.flume.api.FlumeConfig;
import org.safehaus.subutai.shared.operation.AbstractOperationHandler;
import org.safehaus.subutai.shared.operation.ProductOperation;
import org.safehaus.subutai.shared.protocol.Agent;
import org.safehaus.subutai.shared.protocol.enums.NodeState;

public class StatusHandler extends AbstractOperationHandler<FlumeImpl> {

    private final String hostname;

    public StatusHandler(FlumeImpl manager, String clusterName, String hostname) {
        super(manager, clusterName);
        this.hostname = hostname;
        this.productOperation = manager.getTracker().createProductOperation(
                FlumeConfig.PRODUCT_KEY, "Check status of " + hostname);
    }

    @Override
    public void run() {
        ProductOperation po = productOperation;
        if(manager.getCluster(clusterName) == null) {
            po.addLogFailed("Cluster does not exist: " + clusterName);
            return;
        }

        Agent node = manager.getAgentManager().getAgentByHostname(hostname);
        if(node == null) {
            po.addLogFailed("Node is not connected: " + hostname);
            return;
        }

        po.addLog("Checking node...");
        Command cmd = manager.getCommandRunner().createCommand(
                new RequestBuilder(Commands.make(CommandType.STATUS)),
                new HashSet<>(Arrays.asList(node)));
        manager.getCommandRunner().runCommand(cmd);

        NodeState nodeState = NodeState.UNKNOWN;
        if(cmd.hasSucceeded()) {
            AgentResult result = cmd.getResults().get(node.getUuid());
            if(result.getStdOut().contains("is running"))
                nodeState = NodeState.RUNNING;
            else if(result.getStdOut().contains("is not running"))
                nodeState = NodeState.STOPPED;
        }

        if(NodeState.UNKNOWN.equals(nodeState))
            po.addLogFailed(String.format("Failed to check status of %s, %s",
                    hostname, cmd.getAllErrors()));
        else
            po.addLogDone(String.format("Node %s is %s", hostname,
                    nodeState));
    }

}

@@ -61,56 +61,99 @@
 ort 
-org.safehaus.subutai.plugin.flume.impl.FlumeImpl
+java.util.Arrays;%0Aimport java.util.HashSet;%0Aimport org.safehaus.subutai.api.commandrunner.*
 ;%0Aim
@@ -195,21 +195,23 @@
 ume.
-impl.Commands
+api.FlumeConfig
 ;%0Aim
@@ -278,99 +278,55 @@
 ort 
-java.util.Arrays;%0Aimport java.util.HashSet;%0Aimport org.safehaus.subutai.api.commandrunner.*
+org.safehaus.subutai.plugin.flume.impl.Commands
 ;%0Aim
@@ -360,39 +360,38 @@
 lugin.flume.
-api
+impl
 .Flume
-Config
+Impl
 ;%0Aimport org
@@ -568,69 +568,8 @@
 ent;
-%0Aimport org.safehaus.subutai.shared.protocol.enums.NodeState;
 %0A%0Apu
@@ -1715,57 +1715,8 @@
 );%0A%0A
-        NodeState nodeState = NodeState.UNKNOWN;%0A
     
@@ -1862,182 +1862,105 @@
 ins(
-%22is running%22))%0A                nodeState = NodeState.RUNNING;%0A            else if(result.getStdOut().contains(%22is not running%22))%0A                nodeState = NodeState.STOPPED
+Commands.PACKAGE_NAME))%0A                po.addLogDone(%22Flume installed on %22 + node.getHostname())
 ;%0A  
@@ -1969,167 +1969,137 @@
     
-%7D%0A%0A
     
-    if(NodeState.UNKNOWN.equals(nodeState))%0A            po.addLogFailed(String.format(%22Failed to check status of %25s, %25s%22,%0A                    hostname, 
+else%0A                po.addLogFailed(%22Flume not installed on %22 + node.getHostname());%0A        %7D else %7B%0A            po.addLog(
 cmd.
@@ -2113,17 +2113,16 @@
 rrors())
-)
 ;%0A      
@@ -2127,69 +2127,57 @@
     
-else%0A            po.addLogDone(String.format(%22Node %25s is %25s%22,
+    po.addLogFailed(%22Failed to check Flume on %22 +
  hos
@@ -2181,17 +2181,18 @@
 hostname
-,
+);
 %0A       
@@ -2196,32 +2196,9 @@
     
-            nodeState));
+%7D
 %0A   

fix status check operation