package org.zstack.compute.zone; 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.zone.*; 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 ZoneCascadeExtension extends AbstractAsyncCascadeExtension { private static final CLogger logger = Utils.getLogger(ZoneCascadeExtension.class); @Autowired private DatabaseFacade dbf; @Autowired protected ZoneExtensionPointEmitter extpEmitter; @Autowired private CloudBus bus; @Autowired private ErrorFacade errf; private static final String NAME = ZoneVO.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(ZoneVO.class); completion.success(); } private void handleDeletion(final CascadeAction action, final Completion completion) { final List<ZoneInventory> zones = action.getParentIssuerContext(); List<ZoneDeletionMsg> msgs = new ArrayList<ZoneDeletionMsg>(); for (ZoneInventory z : zones) { ZoneDeletionMsg msg = new ZoneDeletionMsg(); msg.setForceDelete(action.isActionCode(CascadeConstant.DELETION_FORCE_DELETE_CODE)); msg.setZoneUuid(z.getUuid()); bus.makeTargetServiceIdByResourceUuid(msg, ZoneConstant.SERVICE_ID, z.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) { ZoneInventory inv = zones.get(replies.indexOf(r)); uuids.add(inv.getUuid()); logger.debug(String.format("delete zone[uuid:%s, name:%s]", inv.getUuid(), inv.getName())); } dbf.removeByPrimaryKeys(uuids, ZoneVO.class); completion.success(); } }); } private void handleDeletionCheck(CascadeAction action, Completion completion) { List<ZoneInventory> zones = action.getParentIssuerContext(); try { for (ZoneInventory zinv : zones) { extpEmitter.preDelete(zinv); } completion.success(); } catch (ZoneException e) { completion.fail(errf.throwableToInternalError(e)); } } @Override public List<String> getEdgeNames() { return Arrays.asList(); } @Override public String getCascadeResourceName() { return NAME; } @Override public CascadeAction createActionForChildResource(CascadeAction action) { if (CascadeConstant.DELETION_CODES.contains(action.getActionCode())) { return action; } return null; } }