package org.zstack.network.l3;
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.db.SimpleQuery;
import org.zstack.core.errorcode.ErrorFacade;
import org.zstack.header.core.Completion;
import org.zstack.header.message.MessageReply;
import org.zstack.header.network.l3.*;
import org.zstack.utils.CollectionUtils;
import org.zstack.utils.Utils;
import org.zstack.utils.function.Function;
import org.zstack.utils.logging.CLogger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*/
public class IpRangeCascadeExtension extends AbstractAsyncCascadeExtension {
private static final CLogger logger = Utils.getLogger(IpRangeCascadeExtension.class);
@Autowired
private DatabaseFacade dbf;
@Autowired
private CloudBus bus;
private static final String NAME = IpRangeVO.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(IpRangeVO.class);
completion.success();
}
private void handleDeletion(final CascadeAction action, final Completion completion) {
final List<IpRangeInventory> iprinvs = ipRangeFromAction(action);
if (iprinvs == null) {
completion.success();
return;
}
List<IpRangeDeletionMsg> msgs = new ArrayList<IpRangeDeletionMsg>();
for (IpRangeInventory iprinv : iprinvs) {
IpRangeDeletionMsg msg = new IpRangeDeletionMsg();
msg.setForceDelete(action.isActionCode(CascadeConstant.DELETION_FORCE_DELETE_CODE));
msg.setL3NetworkUuid(iprinv.getL3NetworkUuid());
msg.setIpRangeUuid(iprinv.getUuid());
bus.makeTargetServiceIdByResourceUuid(msg, L3NetworkConstant.SERVICE_ID, iprinv.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;
}
}
}
completion.success();
}
});
}
private void handleDeletionCheck(CascadeAction action, Completion completion) {
completion.success();
}
@Override
public List<String> getEdgeNames() {
return Arrays.asList(L3NetworkVO.class.getSimpleName());
}
@Override
public String getCascadeResourceName() {
return NAME;
}
private List<IpRangeInventory> ipRangeFromAction(CascadeAction action) {
List<IpRangeInventory> ret = null;
if (L3NetworkVO.class.getSimpleName().equals(action.getParentIssuer())) {
List<String> l3uuids = CollectionUtils.transformToList((List<L3NetworkInventory>)action.getParentIssuerContext(), new Function<String, L3NetworkInventory>() {
@Override
public String call(L3NetworkInventory arg) {
return arg.getUuid();
}
});
SimpleQuery<IpRangeVO> q = dbf.createQuery(IpRangeVO.class);
q.add(IpRangeVO_.l3NetworkUuid, SimpleQuery.Op.IN, l3uuids);
List<IpRangeVO> iprvos = q.list();
if (!iprvos.isEmpty()) {
ret = IpRangeInventory.valueOf(iprvos);
}
} else if (NAME.equals(action.getParentIssuer())) {
ret = action.getParentIssuerContext();
}
return ret;
}
@Override
public CascadeAction createActionForChildResource(CascadeAction action) {
if (CascadeConstant.DELETION_CODES.contains(action.getActionCode())) {
List<IpRangeInventory> ctx = ipRangeFromAction(action);
if (ctx != null) {
return action.copy().setParentIssuer(NAME).setParentIssuerContext(ctx);
}
}
return null;
}
}