package org.zstack.image;

import org.apache.logging.log4j.ThreadContext;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.zstack.core.Platform;
import org.zstack.core.cloudbus.CloudBus;
import org.zstack.core.cloudbus.CloudBusCallBack;
import org.zstack.core.db.DatabaseFacade;
import org.zstack.header.Constants;
import org.zstack.header.core.Completion;
import org.zstack.header.core.ReturnValueCompletion;
import org.zstack.header.image.*;
import org.zstack.header.longjob.LongJobFor;
import org.zstack.header.longjob.LongJobVO;
import org.zstack.header.message.APIEvent;
import org.zstack.header.message.MessageReply;
import org.zstack.header.longjob.LongJob;
import org.zstack.utils.Utils;
import org.zstack.utils.gson.JSONObjectUtil;
import org.zstack.utils.logging.CLogger;


/**
 * Created by on camile 2018/2/2.
 */
@LongJobFor(APIAddImageMsg.class)
@Configurable(preConstruction = true, autowire = Autowire.BY_TYPE)
public class AddImageLongJob implements LongJob {
    private static final CLogger logger = Utils.getLogger(AddImageLongJob.class);

    @Autowired
    protected CloudBus bus;
    @Autowired
    protected DatabaseFacade dbf;

    protected String auditResourceUuid;

    @Override
    public void start(LongJobVO job, ReturnValueCompletion<APIEvent> completion) {
        AddImageMsg msg = JSONObjectUtil.toObject(job.getJobData(), AddImageMsg.class);
        bus.makeLocalServiceId(msg, ImageConstant.SERVICE_ID);
        bus.send(msg, new CloudBusCallBack(completion) {
            @Override
            public void run(MessageReply reply) {
                if (reply.isSuccess()) {
                    AddImageReply r = reply.castReply();
                    APIAddImageEvent evt = new APIAddImageEvent(ThreadContext.get(Constants.THREAD_CONTEXT_API));

                    auditResourceUuid = r.getInventory().getUuid();
                    evt.setInventory(r.getInventory());
                    completion.success(evt);
                } else {
                    auditResourceUuid = msg.getResourceUuid();
                    completion.fail(reply.getError());
                }
            }
        });
    }

    @Override
    public void cancel(LongJobVO job, Completion completion) {
        // TODO
        completion.fail(Platform.operr("not supported"));
    }

    @Override
    public Class getAuditType() {
        return ImageVO.class;
    }

    @Override
    public String getAuditResourceUuid() {
        return auditResourceUuid;
    }
}

@@ -495,32 +495,78 @@
 ore.Completion;%0A
+import org.zstack.header.core.NopeCompletion;%0A
 import org.zstac
@@ -594,32 +594,78 @@
 alueCompletion;%0A
+import org.zstack.header.errorcode.ErrorCode;%0A
 import org.zstac
@@ -727,16 +727,63 @@
 JobFor;%0A
+import org.zstack.header.longjob.LongJobState;%0A
 import o
@@ -2424,32 +2424,847 @@
      %7D);%0A    %7D%0A%0A
+    @Override%0A    public void resume(LongJobVO job) %7B%0A        AddImageMsg msg = JSONObjectUtil.toObject(job.getJobData(), AddImageMsg.class);%0A        ImageDeletionMsg dmsg = new ImageDeletionMsg();%0A        dmsg.setImageUuid(msg.getResourceUuid());%0A        dmsg.setForceDelete(true);%0A        dmsg.setBackupStorageUuids(msg.getBackupStorageUuids());%0A        bus.makeTargetServiceIdByResourceUuid(dmsg, ImageConstant.SERVICE_ID, dmsg.getImageUuid());%0A        MessageReply reply = bus.call(dmsg);%0A        if (!reply.isSuccess()) %7B%0A            logger.warn(String.format(%22delete image %5B%25s%5D failed after management node restarted%22, msg.getResourceUuid()));%0A        %7D%0A        ThreadContext.clearMap();%0A        job.setJobResult(%22Failed because management node restarted.%22);%0A        job.setState(LongJobState.Failed);%0A    %7D%0A%0A
     @Override%0A  

delete downloading image after management restarted