package org.zstack.storage.ceph;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.zstack.header.core.Completion;
import org.zstack.header.core.ReturnValueCompletion;
import org.zstack.header.errorcode.ErrorCode;
import org.zstack.header.rest.JsonAsyncRESTCallback;
import org.zstack.header.rest.RESTFacade;
import org.zstack.utils.ssh.Ssh;

/**
 * Created by frank on 7/27/2015.
 */
@Configurable(preConstruction = true, autowire = Autowire.BY_TYPE, dependencyCheck = true)
public abstract class CephMonBase {
    protected CephMonAO self;

    @Autowired
    protected RESTFacade restf;

    public static class PingResult {
        public String failure;
        public boolean success;
        public String error;
    }

    public abstract void connect(Completion completion);

    public abstract void ping(ReturnValueCompletion<PingResult> completion);

    protected abstract int getAgentPort();

    public CephMonBase(CephMonAO self) {
        this.self = self;
    }

    protected void checkTools() {
        Ssh ssh = new Ssh();
        ssh.setHostname(self.getHostname()).setUsername(self.getSshUsername()).setPassword(self.getSshPassword()).setPort(self.getSshPort())
                .checkTool("ceph", "rbd").runErrorByExceptionAndClose();
    }

    protected String makeHttpPath(String ip, String path) {
        return String.format("http://%s:%s%s", ip, getAgentPort(), path);
    }

    public <T> void httpCall(final String path, final Object cmd, final Class<T> retClass, final ReturnValueCompletion<T> completion) {
        restf.asyncJsonPost(makeHttpPath(self.getHostname(), path), cmd, new JsonAsyncRESTCallback<T>(completion) {
            @Override
            public void fail(ErrorCode err) {
                completion.fail(err);
            }

            @Override
            public void success(T ret) {
                completion.success(ret);
            }

            @Override
            public Class<T> getReturnClass() {
                return retClass;
            }
        });
    }

    public CephMonAO getSelf() {
        return self;
    }
}

@@ -355,24 +355,86 @@
 .ErrorCode;%0A
+import org.zstack.header.errorcode.OperationFailureException;%0A
 import org.z
@@ -548,16 +548,105 @@
 ssh.Ssh;
+%0Aimport org.zstack.utils.ssh.SshException;%0A%0Aimport static org.zstack.core.Platform.operr;
 %0A%0A/**%0A *
@@ -1336,24 +1336,42 @@
  new Ssh();%0A
+        try %7B%0A    
         ssh.
@@ -1515,16 +1515,20 @@
         
+    
 .checkTo
@@ -1568,19 +1568,191 @@
 tion
-AndClose();
+();%0A        %7D catch (SshException e) %7B%0A            throw new OperationFailureException(operr(%22The problem may be caused by an incorrect user name or password or SSH port%22));%0A        %7D
 %0A   

fix bug