package org.zstack.storage.backup;
import org.springframework.beans.factory.annotation.Autowired;
import org.zstack.core.cascade.*;
import org.zstack.core.cloudbus.CloudBus;
import org.zstack.core.cloudbus.CloudBusListCallBack;
import org.zstack.core.db.DatabaseFacade;
import org.zstack.core.errorcode.ErrorFacade;
import org.zstack.header.core.Completion;
import org.zstack.header.message.MessageReply;
import org.zstack.header.storage.backup.*;
import org.zstack.utils.Utils;
import org.zstack.utils.logging.CLogger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*/
public class BackupStorageCascadeExtension extends AbstractAsyncCascadeExtension {
private static final CLogger logger = Utils.getLogger(BackupStorageCascadeExtension.class);
@Autowired
private DatabaseFacade dbf;
@Autowired
private BackupStorageExtensionPointEmitter extpEmitter;
@Autowired
private CloudBus bus;
@Autowired
private ErrorFacade errf;
private static final String NAME = BackupStorageVO.class.getSimpleName();
@Override
public void asyncCascade(CascadeAction action, Completion completion) {
if (action.isActionCode(CascadeConstant.DELETION_CHECK_CODE)) {
handleDeletionCheck(action, completion);
} else if (action.isActionCode(CascadeConstant.DELETION_DELETE_CODE, CascadeConstant.DELETION_FORCE_DELETE_CODE)) {
handleDeletion(action, completion);
} else if (action.isActionCode(CascadeConstant.DELETION_CLEANUP_CODE)) {
handleDeletionCleanup(action, completion);
} else {
completion.success();
}
}
private void handleDeletionCleanup(CascadeAction action, Completion completion) {
dbf.eoCleanup(BackupStorageVO.class);
completion.success();
}
private void handleDeletion(final CascadeAction action, final Completion completion) {
final List<BackupStorageInventory> bsinvs = backupStorageFromAction(action);
if (bsinvs == null) {
completion.success();
return;
}
List<BackupStorageDeletionMsg> msgs = new ArrayList<BackupStorageDeletionMsg>();
for (BackupStorageInventory bsinv : bsinvs) {
BackupStorageDeletionMsg msg = new BackupStorageDeletionMsg();
msg.setBackupStorageUuid(bsinv.getUuid());
msg.setForceDelete(action.isActionCode(CascadeConstant.DELETION_FORCE_DELETE_CODE));
bus.makeTargetServiceIdByResourceUuid(msg, BackupStorageConstant.SERVICE_ID, bsinv.getUuid());
msgs.add(msg);
}
bus.send(msgs, new CloudBusListCallBack(completion) {
@Override
public void run(List<MessageReply> replies) {
if (!action.isActionCode(CascadeConstant.DELETION_FORCE_DELETE_CODE)) {
for (MessageReply r : replies) {
if (!r.isSuccess()) {
completion.fail(r.getError());
return;
}
}
}
List<String> uuids = new ArrayList<String>();
for (MessageReply r : replies) {
BackupStorageInventory inv = bsinvs.get(replies.indexOf(r));
uuids.add(inv.getUuid());
logger.debug(String.format("delete backup storage[uuid:%s, name:%s]", inv.getUuid(), inv.getName()));
}
dbf.removeByPrimaryKeys(uuids, BackupStorageVO.class);
completion.success();
}
});
}
private void handleDeletionCheck(CascadeAction action, Completion completion) {
List<BackupStorageInventory> bsinvs = backupStorageFromAction(action);
if (bsinvs == null) {
completion.success();
return;
}
try {
for (BackupStorageInventory bsinv : bsinvs) {
extpEmitter.preDelete(bsinv);
}
completion.success();
} catch (BackupStorageException e) {
completion.fail(errf.throwableToInternalError(e));
}
}
@Override
public List<String> getEdgeNames() {
return Arrays.asList();
}
@Override
public String getCascadeResourceName() {
return NAME;
}
private List<BackupStorageInventory> backupStorageFromAction(CascadeAction action) {
List<BackupStorageInventory> ret = null;
if (NAME.equals(action.getParentIssuer())) {
ret = action.getParentIssuerContext();
}
return ret;
}
@Override
public CascadeAction createActionForChildResource(CascadeAction action) {
if (CascadeConstant.DELETION_CODES.contains(action.getActionCode())) {
List<BackupStorageInventory> ctx = backupStorageFromAction(action);
if (ctx != null) {
return action.copy().setParentIssuer(NAME).setParentIssuerContext(ctx);
}
}
return null;
}
}