|
| 1 | +package org.zstack.network.service.virtualrouter.vyos; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowire; |
| 4 | +import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.beans.factory.annotation.Configurable; |
| 6 | +import org.zstack.core.cloudbus.CloudBus; |
| 7 | +import org.zstack.core.cloudbus.CloudBusCallBack; |
| 8 | +import org.zstack.core.timeout.ApiTimeoutManager; |
| 9 | +import org.zstack.header.core.workflow.FlowTrigger; |
| 10 | +import org.zstack.header.core.workflow.NoRollbackFlow; |
| 11 | +import org.zstack.header.errorcode.ErrorCode; |
| 12 | +import org.zstack.header.message.MessageReply; |
| 13 | +import org.zstack.header.vm.VmInstanceConstant; |
| 14 | +import org.zstack.header.vm.VmNicInventory; |
| 15 | +import org.zstack.network.service.virtualrouter.*; |
| 16 | +import org.zstack.utils.CollectionUtils; |
| 17 | +import org.zstack.utils.Utils; |
| 18 | +import org.zstack.utils.function.Function; |
| 19 | +import org.zstack.utils.logging.CLogger; |
| 20 | + |
| 21 | +import java.util.*; |
| 22 | + |
| 23 | +import static org.zstack.core.Platform.operr; |
| 24 | + |
| 25 | +/** |
| 26 | + * Created by shixin.ruan on 18-03-10. |
| 27 | + */ |
| 28 | +@Configurable(preConstruction = true, autowire = Autowire.BY_TYPE) |
| 29 | +public class VyosChangePrivateL3FirewallDefaultActionFlow extends NoRollbackFlow { |
| 30 | + @Autowired |
| 31 | + protected CloudBus bus; |
| 32 | + @Autowired |
| 33 | + protected ApiTimeoutManager apiTimeoutManager; |
| 34 | + |
| 35 | + private final static CLogger logger = Utils.getLogger(VyosChangePrivateL3FirewallDefaultActionFlow.class); |
| 36 | + |
| 37 | + @Override |
| 38 | + public void run(FlowTrigger trigger, Map data) { |
| 39 | + String action = VyosGlobalConfig.PRIVATE_L3_FIREWALL_DEFAULT_ACTION.value(String.class); |
| 40 | + |
| 41 | + final VirtualRouterVmInventory servedVm = (VirtualRouterVmInventory) data.get(VirtualRouterConstant.Param.VR.toString()); |
| 42 | + List<VirtualRouterCommands.NicInfo> infos = CollectionUtils.transformToList(servedVm.getGuestNics(), new Function<VirtualRouterCommands.NicInfo, VmNicInventory>() { |
| 43 | + @Override |
| 44 | + public VirtualRouterCommands.NicInfo call(VmNicInventory arg) { |
| 45 | + VirtualRouterCommands.NicInfo info = new VirtualRouterCommands.NicInfo(); |
| 46 | + info.setIp(arg.getIp()); |
| 47 | + info.setDefaultRoute(false); |
| 48 | + info.setGateway(arg.getGateway()); |
| 49 | + info.setMac(arg.getMac()); |
| 50 | + info.setNetmask(arg.getNetmask()); |
| 51 | + info.setFirewallDefaultAction(action); |
| 52 | + |
| 53 | + return info; |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + if (infos == null || infos.isEmpty()) { |
| 58 | + trigger.next(); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + VirtualRouterCommands.ConfigureNicFirewallDefaultActionCmd cmd = new VirtualRouterCommands.ConfigureNicFirewallDefaultActionCmd(); |
| 63 | + cmd.setNics(infos); |
| 64 | + |
| 65 | + VirtualRouterAsyncHttpCallMsg cmsg = new VirtualRouterAsyncHttpCallMsg(); |
| 66 | + cmsg.setCommand(cmd); |
| 67 | + cmsg.setCommandTimeout(apiTimeoutManager.getTimeout(cmd.getClass(), "30m")); |
| 68 | + cmsg.setPath(VirtualRouterConstant.VR_CONFIGURE_NIC_FIREWALL_DEFAULT_ACTION_PATH); |
| 69 | + cmsg.setVmInstanceUuid(servedVm.getUuid()); |
| 70 | + bus.makeTargetServiceIdByResourceUuid(cmsg, VmInstanceConstant.SERVICE_ID, servedVm.getUuid()); |
| 71 | + bus.send(cmsg, new CloudBusCallBack(trigger) { |
| 72 | + /* failure in this flow will not block normal process */ |
| 73 | + @Override |
| 74 | + public void run(MessageReply reply) { |
| 75 | + if (!reply.isSuccess()) { |
| 76 | + logger.debug(String.format("failed to change nic firewall default action of virtual router vm[uuid:%s ip:%s], because %s", |
| 77 | + servedVm.getUuid(), servedVm.getManagementNic().getIp(), reply.getError())); |
| 78 | + trigger.next(); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + VirtualRouterAsyncHttpCallReply re = reply.castReply(); |
| 83 | + VirtualRouterCommands.ConfigureNicFirewallDefaultActionRsp rsp = re.toResponse(VirtualRouterCommands.ConfigureNicFirewallDefaultActionRsp.class); |
| 84 | + if (rsp.isSuccess()) { |
| 85 | + logger.debug(String.format("successfully change nic firewall default action of virtual router vm[uuid:%s, ip:%s]", |
| 86 | + servedVm.getUuid(), servedVm.getManagementNic().getIp())); |
| 87 | + trigger.next(); |
| 88 | + } else { |
| 89 | + logger.debug(String.format("failed to change nic firewall default action of virtual router vm[uuid:%s ip:%s], because %s", |
| 90 | + servedVm.getUuid(), servedVm.getManagementNic().getIp(), rsp.getError())); |
| 91 | + trigger.next(); |
| 92 | + } |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | +} |
0 commit comments