|
| 1 | +package org.zstack.compute.vm; |
| 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.asyncbatch.While; |
| 7 | +import org.zstack.core.componentloader.PluginRegistry; |
| 8 | +import org.zstack.header.core.Completion; |
| 9 | +import org.zstack.header.core.WhileDoneCompletion; |
| 10 | +import org.zstack.header.core.workflow.Flow; |
| 11 | +import org.zstack.header.core.workflow.FlowRollback; |
| 12 | +import org.zstack.header.core.workflow.FlowTrigger; |
| 13 | +import org.zstack.header.errorcode.ErrorCode; |
| 14 | +import org.zstack.header.errorcode.ErrorCodeList; |
| 15 | +import org.zstack.header.vm.*; |
| 16 | +import org.zstack.utils.Utils; |
| 17 | +import org.zstack.utils.logging.CLogger; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import static org.zstack.core.progress.ProgressReportService.taskProgress; |
| 23 | + |
| 24 | +/** |
| 25 | + * Placed after VmAllocateNicIpFlow in the flow chain. |
| 26 | + * |
| 27 | + * For each NIC belonging to an SDN-managed L2 network, this flow delegates |
| 28 | + * to the registered {@link AfterAllocateSdnNicExtensionPoint} implementations |
| 29 | + * (typically {@code SdnControllerManagerImpl}) which: |
| 30 | + * |
| 31 | + * - OVN: calls addLogicalPorts() with already-allocated IPs |
| 32 | + * - ZNS: calls createSegmentPort(), receives IP back from ZNS, writes to DB |
| 33 | + * - H3C/Sugon: default no-op |
| 34 | + * |
| 35 | + * Non-SDN NICs are skipped by the extension implementation internally. |
| 36 | + */ |
| 37 | +@Configurable(preConstruction = true, autowire = Autowire.BY_TYPE) |
| 38 | +public class VmAllocateSdnNicFlow implements Flow { |
| 39 | + private static final CLogger logger = Utils.getLogger(VmAllocateSdnNicFlow.class); |
| 40 | + |
| 41 | + @Autowired |
| 42 | + private PluginRegistry pluginRgty; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void run(final FlowTrigger trigger, final Map data) { |
| 46 | + taskProgress("create SDN ports for nics"); |
| 47 | + |
| 48 | + final VmInstanceSpec spec = (VmInstanceSpec) data.get(VmInstanceConstant.Params.VmInstanceSpec.toString()); |
| 49 | + final List<VmNicInventory> nics = spec.getDestNics(); |
| 50 | + |
| 51 | + if (nics == null || nics.isEmpty()) { |
| 52 | + trigger.next(); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + List<AfterAllocateSdnNicExtensionPoint> exts = |
| 57 | + pluginRgty.getExtensionList(AfterAllocateSdnNicExtensionPoint.class); |
| 58 | + if (exts.isEmpty()) { |
| 59 | + trigger.next(); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + new While<>(exts).each((ext, wcomp) -> { |
| 64 | + ext.afterAllocateSdnNic(spec, nics, new Completion(wcomp) { |
| 65 | + @Override |
| 66 | + public void success() { |
| 67 | + wcomp.done(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void fail(ErrorCode errorCode) { |
| 72 | + wcomp.addError(errorCode); |
| 73 | + wcomp.allDone(); |
| 74 | + } |
| 75 | + }); |
| 76 | + }).run(new WhileDoneCompletion(trigger) { |
| 77 | + @Override |
| 78 | + public void done(ErrorCodeList errorCodeList) { |
| 79 | + if (!errorCodeList.getCauses().isEmpty()) { |
| 80 | + trigger.fail(errorCodeList.getCauses().get(0)); |
| 81 | + } else { |
| 82 | + trigger.next(); |
| 83 | + } |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void rollback(final FlowRollback chain, Map data) { |
| 90 | + final VmInstanceSpec spec = (VmInstanceSpec) data.get(VmInstanceConstant.Params.VmInstanceSpec.toString()); |
| 91 | + final List<VmNicInventory> nics = spec.getDestNics(); |
| 92 | + |
| 93 | + List<AfterAllocateSdnNicExtensionPoint> exts = |
| 94 | + pluginRgty.getExtensionList(AfterAllocateSdnNicExtensionPoint.class); |
| 95 | + |
| 96 | + if (exts.isEmpty() || nics == null || nics.isEmpty()) { |
| 97 | + chain.rollback(); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + new While<>(exts).each((ext, wcomp) -> { |
| 102 | + ext.rollbackSdnNic(spec, nics, new Completion(wcomp) { |
| 103 | + @Override |
| 104 | + public void success() { |
| 105 | + wcomp.done(); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void fail(ErrorCode errorCode) { |
| 110 | + // best-effort: log and continue |
| 111 | + logger.warn(String.format("failed to rollback SDN nic: %s", errorCode)); |
| 112 | + wcomp.done(); |
| 113 | + } |
| 114 | + }); |
| 115 | + }).run(new WhileDoneCompletion(chain) { |
| 116 | + @Override |
| 117 | + public void done(ErrorCodeList errorCodeList) { |
| 118 | + chain.rollback(); |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | +} |
0 commit comments