diff --git a/src/main/java/com/dispose/ability/DisposeAbility.java b/src/main/java/com/dispose/ability/DisposeAbility.java index ed05ec64..db8a1ac0 100644 --- a/src/main/java/com/dispose/ability/DisposeAbility.java +++ b/src/main/java/com/dispose/ability/DisposeAbility.java @@ -14,7 +14,7 @@ import javax.annotation.Nullable; * * @author */ -public interface DisposeAbility { +public interface DisposeAbility { /** * Init device env. @@ -35,22 +35,7 @@ public interface DisposeAbility { * @param duration the duration * @return the mul return type */ - MulReturnType runDispose(String ip, DisposeCapacityType capType, - @Nullable NetflowDirection[] nfDirection, - @Nullable DDoSAttackType[] attackType, - @Nullable Long duration); - - /** - * Run dispose mul return type. - * - * @param ip the ip - * @param capType the cap type - * @param nfDirection the nf direction - * @param attackType the attack type - * @param duration the duration - * @return the mul return type - */ - MulReturnType runDispose(String ip, DisposeCapacityType capType, + T runDispose(String ip, DisposeCapacityType capType, @Nullable NetflowDirection nfDirection, @Nullable DDoSAttackType attackType, @Nullable Long duration); @@ -65,23 +50,7 @@ public interface DisposeAbility { * @param taskId the task id * @return the mul return type */ - MulReturnType stopDispose(String ip, DisposeCapacityType capType, - @Nullable NetflowDirection[] nfDirection, - @Nullable DDoSAttackType[] attackType, - @Nullable Long taskId); - - - /** - * Stop dispose mul return type. - * - * @param ip the ip - * @param capType the cap type - * @param nfDirection the nf direction - * @param attackType the attack type - * @param taskId the task id - * @return the mul return type - */ - MulReturnType stopDispose(String ip, DisposeCapacityType capType, + T stopDispose(String ip, DisposeCapacityType capType, @Nullable NetflowDirection nfDirection, @Nullable DDoSAttackType attackType, @Nullable Long taskId); diff --git a/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java b/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java index ac783d71..71dd58a5 100644 --- a/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java +++ b/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java @@ -1,6 +1,8 @@ package com.dispose.ability.impl; +import com.dispose.ability.DeviceDisposeAbility; import com.dispose.ability.DisposeAbility; +import com.dispose.ability.DisposeTaskCallback; import com.dispose.common.DDoSAttackType; import com.dispose.common.DisposeCapacityType; import com.dispose.common.DpTechAttackType; @@ -22,14 +24,10 @@ import org.apache.wss4j.dom.WSConstants; import org.apache.wss4j.dom.handler.WSHandlerConstants; import org.springframework.stereotype.Component; -import javax.annotation.Nullable; import javax.xml.ws.BindingProvider; -import java.util.Arrays; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; -import java.util.stream.Collectors; /** * The type Dp tech ability. @@ -38,7 +36,7 @@ import java.util.stream.Collectors; */ @Component @Slf4j -public class DpTechAbilityImpl implements DisposeAbility { +public class DpTechAbilityImpl implements DisposeAbility, DeviceDisposeAbility { /** * The Clean type port. */ @@ -96,72 +94,17 @@ public class DpTechAbilityImpl implements DisposeAbility { * @return the mul return type */ @Override - public MulReturnType runDispose(String ip, DisposeCapacityType capType, - @Nullable NetflowDirection[] nfDirection, - @Nullable DDoSAttackType[] attackType, - @Nullable Long duration) { - ErrorCode err = ErrorCode.ERR_OK; - - try { - log.info("++++Begging DPTech Start Cleanup Task: {}", ip); - - // 查找需要处置的流量方向集合 - List dirList = Arrays.stream(NetflowDirection.values()) - .filter(d -> nfDirection == null || nfDirection.length == 0 || Arrays.asList(nfDirection).contains(d)) - .collect(Collectors.toList()); - - // 查找需要处理的攻击类型集合 - List typeList = Arrays.stream(DDoSAttackType.values()) - .filter(t -> attackType == null || attackType.length == 0 || Arrays.asList(attackType).contains(t)) - .flatMap(DpTechAttackType::fromDdosAttackTypeValue) - .distinct() - .collect(Collectors.toList()); - - dirList.forEach(d -> typeList.forEach(t -> CompletableFuture.supplyAsync(() -> - cleanTypePort.startAbnormalTaskForUMC(ip, t.getValue(), d.getValue())) - .whenComplete((v, ex) -> { - if (ex != null) { - log.error("DPTech run dispose: {}, {}, error:{}", ip, t.getValue(), - ex.getMessage()); - } else { - log.debug("Cleanup: {} --> {}:{}", d, t.getDescription(), t.getValue()); - if (v.getResultRetVal() != ErrorCode.ERR_OK.getCode()) { - log.error("DPTech run dispose {} error: {}", ip, v.getResultInfo()); - } - } - }))); - log.info("----Finish DPTech Start Cleanup Task: {}", ip); - } catch (Exception ex) { - log.error(ex.getMessage()); - log.error("----Error DPTech Start Cleanup Task: {}", ip); - err = ErrorCode.ERR_SYSTEMEXCEPTION; - } - - return new MulReturnType<>(err, null); - } - - /** - * Run dispose mul return type. - * - * @param ip the ip - * @param capType the cap type - * @param nfDirection the nf direction - * @param attackType the attack type - * @param duration the duration - * @return the mul return type - */ - @Override - public MulReturnType runDispose(String ip, DisposeCapacityType capType, - NetflowDirection nfDirection, - DDoSAttackType attackType, - Long duration) { + public ErrorCode runDispose(String ip, DisposeCapacityType capType, + NetflowDirection nfDirection, + DDoSAttackType attackType, + Long duration) { ErrorCode err = ErrorCode.ERR_OK; try { log.info("++++Begging DPTech Start Cleanup Task: {}, {}, {} ", ip, attackType, nfDirection); NtcRequestResultInfo ret = cleanTypePort.startAbnormalTaskForUMC(ip, attackType.getValue(), - nfDirection.getValue()); + nfDirection.getValue()); if (ret.getResultRetVal() != 0) { err = ErrorCode.ERR_CALLDEVICE; @@ -173,62 +116,9 @@ public class DpTechAbilityImpl implements DisposeAbility { log.error("----Exception Error DPTech Start Cleanup Task: {}", ip); } - return new MulReturnType<>(err, null); + return err; } - /** - * Stop dispose mul return type. - * - * @param ipAddr the ip addr - * @param capType the cap type - * @param nfDirection the nf direction - * @param attackType the attack type - * @param taskId the task id - * @return the mul return type - */ - @Override - public MulReturnType stopDispose(String ipAddr, DisposeCapacityType capType, - @Nullable NetflowDirection[] nfDirection, - @Nullable DDoSAttackType[] attackType, - @Nullable Long taskId) { - ErrorCode err = ErrorCode.ERR_OK; - - try { - log.info("++++Begging DPTech Stop Cleanup Task: {}", ipAddr); - - // 查找需要处置的流量方向集合 - List dirList = Arrays.stream(NetflowDirection.values()) - .filter(d -> nfDirection == null || nfDirection.length == 0 || Arrays.asList(nfDirection).contains(d)) - .collect(Collectors.toList()); - - // 查找需要处理的攻击类型集合 - List typeList = Arrays.stream(DDoSAttackType.values()) - .filter(t -> attackType == null || attackType.length == 0 || Arrays.asList(attackType).contains(t)) - .flatMap(DpTechAttackType::fromDdosAttackTypeValue) - .distinct() - .collect(Collectors.toList()); - - dirList.forEach(d -> typeList.forEach(t -> CompletableFuture.supplyAsync(() -> - cleanTypePort.stopAbnormalTaskForUMC(ipAddr, t.getValue(), d.getValue())) - .whenComplete((v, ex) -> { - if (ex != null) { - log.error("DPTech stop dispose: {}, {}, error:{}", ipAddr, t.getValue(), ex.getMessage()); - } else { - log.debug("Stop Cleanup: {} --> {}:{}", d, t.getDescription(), t.getValue()); - if (v.getResultRetVal() != ErrorCode.ERR_OK.getCode()) { - log.error("DPTech stop dispose {} error: {}", ipAddr, v.getResultInfo()); - } - } - }))); - log.info("----Finish DPTech Stop Cleanup Task: {}", ipAddr); - } catch (Exception ex) { - log.error(ex.getMessage()); - log.error("----Error DPTech Stop Cleanup Task: {}", ipAddr); - err = ErrorCode.ERR_SYSTEMEXCEPTION; - } - - return new MulReturnType<>(err, null); - } /** * Stop dispose mul return type. @@ -241,18 +131,18 @@ public class DpTechAbilityImpl implements DisposeAbility { * @return the mul return type */ @Override - public MulReturnType stopDispose(String ip, - DisposeCapacityType capType, - NetflowDirection nfDirection, - DDoSAttackType attackType, - Long taskId) { + public ErrorCode stopDispose(String ip, + DisposeCapacityType capType, + NetflowDirection nfDirection, + DDoSAttackType attackType, + Long taskId) { ErrorCode err = ErrorCode.ERR_OK; try { log.info("++++Begging DPTech Stop Cleanup Task: {}, {}, {} ", ip, attackType, nfDirection); NtcRequestResultInfo ret = cleanTypePort.stopAbnormalTaskForUMC(ip, attackType.getValue(), - nfDirection.getValue()); + nfDirection.getValue()); if (ret.getResultRetVal() != 0) { err = ErrorCode.ERR_CALLDEVICE; @@ -265,7 +155,7 @@ public class DpTechAbilityImpl implements DisposeAbility { log.error("----Exception Error DPTech Stop Cleanup Task: {}", ip); } - return new MulReturnType<>(err, null); + return err; } /** @@ -294,18 +184,18 @@ public class DpTechAbilityImpl implements DisposeAbility { @Override public MulReturnType getAbilityDeviceFireware() { return new MulReturnType<>(ErrorCode.ERR_OK, - DeviceFirewareInfo.builder() - .vendor("DpTech") - .model("UMC") - .firmware("Unknown") - .os("Windows Server") - .kernel("Windows") - .arch("x86_64") - .version("5.7.31") - .memory(-1) - .freeMemory(-1) - .cpuUsed(-1) - .build()); + DeviceFirewareInfo.builder() + .vendor("DpTech") + .model("UMC") + .firmware("Unknown") + .os("Windows Server") + .kernel("Windows") + .arch("x86_64") + .version("5.7.31") + .memory(-1) + .freeMemory(-1) + .cpuUsed(-1) + .build()); } /** @@ -318,4 +208,52 @@ public class DpTechAbilityImpl implements DisposeAbility { public Long toDeviceAttackType(Long ddosAttackTypeMask) { return DpTechAttackType.formDdosAttackTypeMaks(ddosAttackTypeMask); } + + /** + * Device dispose exec error code. + * + * @param callback the callback + * @param capType the cap type + * @param ipAddr the ip addr + * @param args the args + * @return the error code + */ + @Override + public ErrorCode deviceDisposeExec(DisposeTaskCallback callback, DisposeCapacityType capType, String ipAddr, + Object... args) { + CompletableFuture.supplyAsync(() -> runDispose(ipAddr, capType, (NetflowDirection) args[0], + (DDoSAttackType) args[1], null)) + .whenComplete((v, ex) -> { + if (ex != null) { + callback.upgradeStatusCallback(ErrorCode.ERR_SYSTEMEXCEPTION); + } else { + callback.upgradeStatusCallback(v); + } + }); + + return ErrorCode.ERR_OK; + } + + /** + * Device dispose stop error code. + * + * @param callback the callback + * @param capType the cap type + * @param args the args + * @return the error code + */ + @Override + public ErrorCode deviceDisposeStop(DisposeTaskCallback callback, DisposeCapacityType capType, Object... args) { + CompletableFuture.supplyAsync(() -> stopDispose((String) args[0], capType, (NetflowDirection) args[1], + (DDoSAttackType) args[2], null)) + .whenComplete((v, ex) -> { + if (ex != null) { + callback.upgradeStatusCallback(ErrorCode.ERR_SYSTEMEXCEPTION); + } else { + callback.upgradeStatusCallback(v); + } + }); + + return ErrorCode.ERR_OK; + } } diff --git a/src/main/java/com/dispose/ability/impl/HaoHanAbilityImpl.java b/src/main/java/com/dispose/ability/impl/HaoHanAbilityImpl.java index f7c99a8c..90a1b93d 100644 --- a/src/main/java/com/dispose/ability/impl/HaoHanAbilityImpl.java +++ b/src/main/java/com/dispose/ability/impl/HaoHanAbilityImpl.java @@ -24,7 +24,8 @@ import javax.annotation.Nullable; */ @Component @Slf4j -public class HaoHanAbilityImpl implements DisposeAbility, DeviceDisposeAbility> { +public class HaoHanAbilityImpl implements DisposeAbility>, + DeviceDisposeAbility> { /** * The constant DISPOSE_PLATFORM_NAME. @@ -51,27 +52,6 @@ public class HaoHanAbilityImpl implements DisposeAbility, DeviceDisposeAbility runDispose(String ip, DisposeCapacityType capType, - @Nullable NetflowDirection[] nfDirection, - @Nullable DDoSAttackType[] attackType, - @Nullable Long duration) { - return runDispose(ip, capType, - NetflowDirection.DIRECTION_BI, - DDoSAttackType.ALL_ATTACKS, - duration); - } - /** * Run dispose mul return type. * @@ -111,27 +91,6 @@ public class HaoHanAbilityImpl implements DisposeAbility, DeviceDisposeAbility(ErrorCode.ERR_OK, (long) resp.getCleanTaskId()); } - /** - * Stop dispose mul return type. - * - * @param ip the ip - * @param capType the cap type - * @param nfDirection the nf direction - * @param attackType the attack type - * @param taskId the task id - * @return the mul return type - */ - @Override - public MulReturnType stopDispose(String ip, DisposeCapacityType capType, - @Nullable NetflowDirection[] nfDirection, - @Nullable DDoSAttackType[] attackType, - @Nullable Long taskId) { - return stopDispose(ip, capType, - NetflowDirection.DIRECTION_BI, - DDoSAttackType.ALL_ATTACKS, - taskId); - } - /** * Stop dispose mul return type. * @@ -244,7 +203,7 @@ public class HaoHanAbilityImpl implements DisposeAbility, DeviceDisposeAbility deviceDisposeStop(DisposeTaskCallback callback, DisposeCapacityType capType, Object... taskId) { - MulReturnType ret = stopDispose(null, + MulReturnType ret = stopDispose(null, null, null, DDoSAttackType.ALL_ATTACKS, diff --git a/src/main/java/com/dispose/ability/impl/VirtualAbilityImpl.java b/src/main/java/com/dispose/ability/impl/VirtualAbilityImpl.java index ca095c41..f3de9e86 100644 --- a/src/main/java/com/dispose/ability/impl/VirtualAbilityImpl.java +++ b/src/main/java/com/dispose/ability/impl/VirtualAbilityImpl.java @@ -17,7 +17,7 @@ import javax.annotation.Nullable; * * @author */ -public class VirtualAbilityImpl implements DisposeAbility, DeviceDisposeAbility { +public class VirtualAbilityImpl implements DisposeAbility, DeviceDisposeAbility { /** * Init device env. * @@ -41,28 +41,10 @@ public class VirtualAbilityImpl implements DisposeAbility, DeviceDisposeAbility< * @return the mul return type */ @Override - public MulReturnType runDispose(String ip, DisposeCapacityType capType, - @Nullable NetflowDirection[] nfDirection, - @Nullable DDoSAttackType[] attackType, - @Nullable Long duration) { - return new MulReturnType<>(ErrorCode.ERR_OK, null); - } - - /** - * Run dispose mul return type. - * - * @param ip the ip - * @param capType the cap type - * @param nfDirection the nf direction - * @param attackType the attack type - * @param duration the duration - * @return the mul return type - */ - @Override - public MulReturnType runDispose(String ip, DisposeCapacityType capType, + public ErrorCode runDispose(String ip, DisposeCapacityType capType, @Nullable NetflowDirection nfDirection, @Nullable DDoSAttackType attackType, @Nullable Long duration) { - return new MulReturnType<>(ErrorCode.ERR_OK, null); + return ErrorCode.ERR_OK; } /** @@ -76,28 +58,10 @@ public class VirtualAbilityImpl implements DisposeAbility, DeviceDisposeAbility< * @return the mul return type */ @Override - public MulReturnType stopDispose(String ip, DisposeCapacityType capType, - @Nullable NetflowDirection[] nfDirection, - @Nullable DDoSAttackType[] attackType, - @Nullable Long taskId) { - return new MulReturnType<>(ErrorCode.ERR_OK, null); - } - - /** - * Stop dispose mul return type. - * - * @param ip the ip - * @param capType the cap type - * @param nfDirection the nf direction - * @param attackType the attack type - * @param taskId the task id - * @return the mul return type - */ - @Override - public MulReturnType stopDispose(String ip, DisposeCapacityType capType, + public ErrorCode stopDispose(String ip, DisposeCapacityType capType, @Nullable NetflowDirection nfDirection, @Nullable DDoSAttackType attackType, @Nullable Long taskId) { - return new MulReturnType<>(ErrorCode.ERR_OK, null); + return ErrorCode.ERR_OK; } /** @@ -158,7 +122,7 @@ public class VirtualAbilityImpl implements DisposeAbility, DeviceDisposeAbility< capType, null, DDoSAttackType.ALL_ATTACKS, - null).getFirstParam(); + null); callback.upgradeStatusCallback(ret); @@ -178,7 +142,7 @@ public class VirtualAbilityImpl implements DisposeAbility, DeviceDisposeAbility< capType, null, DDoSAttackType.ALL_ATTACKS, - null).getFirstParam(); + null); callback.upgradeStatusCallback(ret); diff --git a/src/main/java/com/dispose/manager/DeviceTaskManager.java b/src/main/java/com/dispose/manager/DeviceTaskManager.java index 0ecd7804..ecf3bdf5 100644 --- a/src/main/java/com/dispose/manager/DeviceTaskManager.java +++ b/src/main/java/com/dispose/manager/DeviceTaskManager.java @@ -66,4 +66,13 @@ public interface DeviceTaskManager { * @return the task err retry times */ boolean setTaskErrRetryTimes(Long id, Integer rTimes); + + /** + * Sets task extern id. + * + * @param id the id + * @param externId the extern id + * @return the task extern id + */ + boolean setTaskExternId(Long id, Long externId); } diff --git a/src/main/java/com/dispose/manager/impl/DeviceTaskManagerImpl.java b/src/main/java/com/dispose/manager/impl/DeviceTaskManagerImpl.java index cb7bf402..f7f1ee67 100644 --- a/src/main/java/com/dispose/manager/impl/DeviceTaskManagerImpl.java +++ b/src/main/java/com/dispose/manager/impl/DeviceTaskManagerImpl.java @@ -115,8 +115,27 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { return ret == 1; } + /** + * Sets task err retry times. + * + * @param id the id + * @param rTimes the r times + * @return the task err retry times + */ @Override public boolean setTaskErrRetryTimes(Long id, Integer rTimes) { return deviceTaskMapper.changeErrRetry(id, rTimes) == 1; } + + /** + * Sets task extern id. + * + * @param id the id + * @param externId the extern id + * @return the task extern id + */ + @Override + public boolean setTaskExternId(Long id, Long externId) { + return deviceTaskMapper.changeExternId(id, externId) == 1; + } } diff --git a/src/main/java/com/dispose/mapper/DeviceTaskMapper.java b/src/main/java/com/dispose/mapper/DeviceTaskMapper.java index 8e7d5467..d9f8355d 100644 --- a/src/main/java/com/dispose/mapper/DeviceTaskMapper.java +++ b/src/main/java/com/dispose/mapper/DeviceTaskMapper.java @@ -121,4 +121,14 @@ public interface DeviceTaskMapper { */ int changeErrRetry(@Param("id") Long id, @Param("errRetry") Integer errRetry); + + /** + * Change extern id int. + * + * @param id the id + * @param externId the extern id + * @return the int + */ + int changeExternId(@Param("id") Long id, + @Param("externId") Long externId); } diff --git a/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java b/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java index 3b498f14..adcb7ebd 100644 --- a/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java @@ -11,6 +11,7 @@ import com.dispose.manager.DeviceTaskManager; import com.dispose.manager.DisposeTaskManager; import com.dispose.pojo.entity.DisposeTask; import com.dispose.pojo.po.AbilityInfo; +import com.dispose.pojo.po.MulReturnType; import com.dispose.service.DeviceTaskManagerService; import com.dispose.service.DisposeAbilityRouterService; import lombok.extern.slf4j.Slf4j; @@ -91,107 +92,93 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { @Async("deviceTaskExecutor") @Scheduled(fixedDelay = 1000) public void deviceTaskRunnerSchedule() { - deviceTaskManager.getNewDisposeDeviceTaskInfo() - .forEach(v -> { - DisposeTask task = disposeTaskManager.getDisposeTaskById(v.getTaskId()); - assert task != null; - log.info("Run task {}", task); + deviceTaskManager.getNewDisposeDeviceTaskInfo().forEach(v -> { + DisposeTask task = disposeTaskManager.getDisposeTaskById(v.getTaskId()); + assert task != null; + log.info("Run task {}", task); - AbilityInfo ai = disposeAbilityRouterService.getAbilityDevice(task.getDeviceId()); + AbilityInfo ai = disposeAbilityRouterService.getAbilityDevice(task.getDeviceId()); - switch (ai.getDev().getDeviceType()) { - case DPTECH_UMC: - break; + switch (ai.getDev().getDeviceType()) { + case DPTECH_UMC: + break; - case HAOHAN_PLATFORM: - // 设置任务状态为启动中 - deviceTaskManager.changeDisposeDeviceTaskInfoStatus(v.getId(), DisposeTaskStatus.TASK_STARTING); - // 设置启动任务攻击类型状态 - deviceTaskManager.setExecAttackType(v.getId(), NetflowDirection.DIRECTION_BI, - v.getTaskAttackType()); - // 调用设备处置能力 - ((HaoHanAbilityImpl) ai.getDb()).deviceDisposeExec( - errorCode -> { - // 启动处置任务成功 - if (errorCode == ErrorCode.ERR_OK) { - // 设置攻击类型任务启动结果 - deviceTaskManager.setAttackTypeStatus(v.getId(), - task.getFlowDirection(), v.getTaskAttackType()); - // 更改处置任务状态为处置中 - deviceTaskManager.changeDisposeDeviceTaskInfoStatus(v.getId(), - DisposeTaskStatus.TASK_STARTED); - log.info("HAOHAN_PLATFORM setup task succeed: {}", v); - } else if (v.getErrRetry() < DisposeConfigValue.CALL_ERROR_RETRY_TIMES) { - // 设置该任务为新任务,待下次重试启动 - deviceTaskManager.changeDisposeDeviceTaskInfoStatus(v.getId(), - DisposeTaskStatus.TASK_NEW); - // 记录任务出错重试次数 - deviceTaskManager.setTaskErrRetryTimes(v.getId(), v.getErrRetry() + 1); - log.error("HAOHAN_PLATFORM setup task times {} error {}: {}", - v.getErrRetry(), errorCode, v); - } else { - // 任务出错,不在重试,当做失败任务处理 - deviceTaskManager.setAttackTypeStatus(v.getId(), - task.getFlowDirection(), ~v.getTaskAttackType()); - log.error("HAOHAN_PLATFORM setup task error {}: {}", errorCode, v); - } - }, - task.getDisposeCapacity(), - task.getDisposeIp(), - (long) Helper.getTimestampDiffNow(task.getPlanEndTime())); - break; + case HAOHAN_PLATFORM: + // 设置任务状态为启动中 + deviceTaskManager.changeDisposeDeviceTaskInfoStatus(v.getId(), DisposeTaskStatus.TASK_STARTING); + // 设置启动任务攻击类型状态 + deviceTaskManager.setExecAttackType(v.getId(), NetflowDirection.DIRECTION_BI, + v.getTaskAttackType()); + // 调用设备处置能力 + MulReturnType ret = ((HaoHanAbilityImpl) ai.getDb()).deviceDisposeExec( + errorCode -> { + // 启动处置任务成功 + if (errorCode == ErrorCode.ERR_OK) { + // 设置攻击类型任务启动结果 + deviceTaskManager.setAttackTypeStatus(v.getId(), + task.getFlowDirection(), v.getTaskAttackType()); + // 更改处置任务状态为处置中 + deviceTaskManager.changeDisposeDeviceTaskInfoStatus(v.getId(), + DisposeTaskStatus.TASK_STARTED); + log.info("HAOHAN_PLATFORM setup task succeed: {}", v); + } else if (v.getErrRetry() < DisposeConfigValue.CALL_ERROR_RETRY_TIMES) { + // 设置该任务为新任务,待下次重试启动 + deviceTaskManager.changeDisposeDeviceTaskInfoStatus(v.getId(), + DisposeTaskStatus.TASK_NEW); + // 记录任务出错重试次数 + deviceTaskManager.setTaskErrRetryTimes(v.getId(), v.getErrRetry() + 1); + log.error("HAOHAN_PLATFORM setup task times {} error {}: {}", + v.getErrRetry(), errorCode, v); + } else { + // 任务出错,不在重试,当做失败任务处理 + deviceTaskManager.setAttackTypeStatus(v.getId(), + task.getFlowDirection(), ~v.getTaskAttackType()); + log.error("HAOHAN_PLATFORM setup task error {}: {}", errorCode, v); + } + }, + task.getDisposeCapacity(), + task.getDisposeIp(), + (long) Helper.getTimestampDiffNow(task.getPlanEndTime())); - case VIRTUAL_DISPOSE: - // 设置任务状态为启动中 - deviceTaskManager.changeDisposeDeviceTaskInfoStatus(v.getId(), DisposeTaskStatus.TASK_STARTING); - // 设置启动任务攻击类型状态 - deviceTaskManager.setExecAttackType(v.getId(), NetflowDirection.DIRECTION_BI, - v.getTaskAttackType()); + // 启动任务成功,保存浩瀚任务ID到数据库 + if (ret.getFirstParam() == ErrorCode.ERR_OK) { + deviceTaskManager.setTaskExternId(v.getId(), ret.getSecondParam()); + } + break; - ((VirtualAbilityImpl) ai.getDb()).deviceDisposeExec( - errorCode -> { - // 启动处置任务成功 - if (errorCode == ErrorCode.ERR_OK) { - // 设置攻击类型任务启动结果 - deviceTaskManager.setAttackTypeStatus(v.getId(), - task.getFlowDirection(), v.getTaskAttackType()); - // 更改处置任务状态为处置中 - deviceTaskManager.changeDisposeDeviceTaskInfoStatus(v.getId(), - DisposeTaskStatus.TASK_STARTED); - log.info("VIRTUAL_DISPOSE setup task succeed: {}", v); - } else { - // 任务出错,不在重试,当做失败任务处理 - deviceTaskManager.setAttackTypeStatus(v.getId(), - task.getFlowDirection(), ~v.getTaskAttackType()); - log.error("VIRTUAL_DISPOSE setup task error {}: {}", errorCode, v); - } - }, - task.getDisposeCapacity(), - task.getDisposeIp()); - break; + case VIRTUAL_DISPOSE: + // 设置任务状态为启动中 + deviceTaskManager.changeDisposeDeviceTaskInfoStatus(v.getId(), DisposeTaskStatus.TASK_STARTING); + // 设置启动任务攻击类型状态 + deviceTaskManager.setExecAttackType(v.getId(), NetflowDirection.DIRECTION_BI, + v.getTaskAttackType()); - default: - log.error("Unknown dispose device type: {}", ai.getDev()); - break; - } + ((VirtualAbilityImpl) ai.getDb()).deviceDisposeExec( + errorCode -> { + // 启动处置任务成功 + if (errorCode == ErrorCode.ERR_OK) { + // 设置攻击类型任务启动结果 + deviceTaskManager.setAttackTypeStatus(v.getId(), + task.getFlowDirection(), v.getTaskAttackType()); + // 更改处置任务状态为处置中 + deviceTaskManager.changeDisposeDeviceTaskInfoStatus(v.getId(), + DisposeTaskStatus.TASK_STARTED); + log.info("VIRTUAL_DISPOSE setup task succeed: {}", v); + } else { + // 任务出错,不在重试,当做失败任务处理 + deviceTaskManager.setAttackTypeStatus(v.getId(), + task.getFlowDirection(), ~v.getTaskAttackType()); + log.error("VIRTUAL_DISPOSE setup task error {}: {}", errorCode, v); + } + }, + task.getDisposeCapacity(), + task.getDisposeIp()); + break; - -// if (task != null) { -// -// CompletableFuture.supplyAsync(() -> -// disposeAbilityRouterService -// .getAbilityDevice(task.getDeviceId()) -// .getDb() -// .runDispose(task.getDisposeIp(), -// task.getDisposeCapacity(), -// task.getFlowDirection(), -// null, -// (long)Helper.getTimestampDiffNow(task.getPlanEndTime()))) -// .whenComplete((k, ex) -> { -// -// }); -// } - - }); + default: + log.error("Unknown dispose device type: {}", ai.getDev()); + break; + } + }); } } diff --git a/src/main/resources/mappers/DeviceTask.xml b/src/main/resources/mappers/DeviceTask.xml index ff48610c..b98e4ec9 100644 --- a/src/main/resources/mappers/DeviceTask.xml +++ b/src/main/resources/mappers/DeviceTask.xml @@ -106,4 +106,10 @@ WHERE id = #{id} + + UPDATE device_task + SET externId = #{externId} + WHERE id = #{id} + + \ No newline at end of file diff --git a/src/test/java/com/dispose/test/dptech/DPTechInterfaceTestCase.java b/src/test/java/com/dispose/test/dptech/DPTechInterfaceTestCase.java index 59c28785..e5b757ac 100644 --- a/src/test/java/com/dispose/test/dptech/DPTechInterfaceTestCase.java +++ b/src/test/java/com/dispose/test/dptech/DPTechInterfaceTestCase.java @@ -2,7 +2,13 @@ package com.dispose.test.dptech; import com.dispose.ability.DisposeAbility; import com.dispose.ability.impl.DpTechAbilityImpl; -import com.dispose.common.*; +import com.dispose.common.DDoSAttackType; +import com.dispose.common.DisposeCapacityType; +import com.dispose.common.DisposeDeviceType; +import com.dispose.common.ErrorCode; +import com.dispose.common.HttpType; +import com.dispose.common.NetflowDirection; +import com.dispose.common.ObjectStatus; import com.dispose.pojo.entity.DisposeDevice; import com.dispose.pojo.po.MulReturnType; import com.dispose.pojo.vo.DeviceFirewareInfo; @@ -49,11 +55,7 @@ public class DPTechInterfaceTestCase extends InitTestEnvironment { */ @Test public void t1_runDisposeTest() { - DisposeAbility db; - - NetflowDirection[] nfDirection = {NetflowDirection.DIRECTION_IN}; - - DDoSAttackType[] ddoSAttackType = {DDoSAttackType.ACK_FLOOD, DDoSAttackType.CHARGED_AMPLIFICATION}; + DisposeAbility db; DisposeDevice dev = DisposeDevice.builder() .ipAddr("10.88.77.15") @@ -82,10 +84,10 @@ public class DPTechInterfaceTestCase extends InitTestEnvironment { // 初始化设备 db.initDeviceEnv(url, dev.getUserName(), dev.getPassword()); - MulReturnType ret = db.runDispose("192.168.3.5", DisposeCapacityType.CLEANUP, - nfDirection, ddoSAttackType, -1L); + ErrorCode ret = db.runDispose("192.168.3.5", DisposeCapacityType.CLEANUP, + NetflowDirection.DIRECTION_BI, DDoSAttackType.ALL_ATTACKS, -1L); - Assert.assertEquals(ret.getFirstParam(), ErrorCode.ERR_OK); + Assert.assertEquals(ret, ErrorCode.ERR_OK); } } @@ -94,11 +96,7 @@ public class DPTechInterfaceTestCase extends InitTestEnvironment { */ @Test public void t2_stopDisposeTest() { - DisposeAbility db; - - NetflowDirection[] nfDirection = {NetflowDirection.DIRECTION_IN}; - - DDoSAttackType[] ddoSAttackType = {DDoSAttackType.ACK_FLOOD, DDoSAttackType.CHARGED_AMPLIFICATION}; + DisposeAbility db; DisposeDevice dev = DisposeDevice.builder() .ipAddr("10.88.77.15") @@ -128,10 +126,10 @@ public class DPTechInterfaceTestCase extends InitTestEnvironment { db.initDeviceEnv(url, dev.getUserName(), dev.getPassword()); Assert.assertTrue(db.getDeviceLinkStatus()); - MulReturnType ret = db.stopDispose("192.168.3.5", DisposeCapacityType.CLEANUP, - nfDirection, ddoSAttackType, -1L); + ErrorCode ret = db.stopDispose("192.168.3.5", DisposeCapacityType.CLEANUP, + NetflowDirection.DIRECTION_BI, DDoSAttackType.ALL_ATTACKS, -1L); - Assert.assertEquals(ret.getFirstParam(), ErrorCode.ERR_OK); + Assert.assertEquals(ret, ErrorCode.ERR_OK); } } @@ -140,7 +138,7 @@ public class DPTechInterfaceTestCase extends InitTestEnvironment { */ @Test public void t3_getAbilityDeviceFirewareTest() { - DisposeAbility db; + DisposeAbility db; DisposeDevice dev = DisposeDevice.builder() .ipAddr("10.88.77.15") diff --git a/src/test/java/com/dispose/test/haohan/HAOHanInterfaceTestCast.java b/src/test/java/com/dispose/test/haohan/HAOHanInterfaceTestCast.java index 51bc6ab8..a22fda18 100644 --- a/src/test/java/com/dispose/test/haohan/HAOHanInterfaceTestCast.java +++ b/src/test/java/com/dispose/test/haohan/HAOHanInterfaceTestCast.java @@ -2,7 +2,11 @@ package com.dispose.test.haohan; import com.dispose.ability.DisposeAbility; import com.dispose.ability.impl.HaoHanAbilityImpl; -import com.dispose.common.*; +import com.dispose.common.DisposeCapacityType; +import com.dispose.common.DisposeDeviceType; +import com.dispose.common.ErrorCode; +import com.dispose.common.HttpType; +import com.dispose.common.ObjectStatus; import com.dispose.pojo.entity.DisposeDevice; import com.dispose.pojo.po.MulReturnType; import com.dispose.pojo.vo.DeviceFirewareInfo; @@ -49,28 +53,24 @@ public class HAOHanInterfaceTestCast extends InitTestEnvironment { */ @Test public void t1_runDisposeTest() { - DisposeAbility db; - - NetflowDirection[] nfDirection = {NetflowDirection.DIRECTION_IN}; - - DDoSAttackType[] ddoSAttackType = {DDoSAttackType.ACK_FLOOD, DDoSAttackType.CHARGED_AMPLIFICATION}; + DisposeAbility> db; DisposeDevice dev = DisposeDevice.builder() - .ipAddr("10.88.77.88") - .ipPort("18080") - .deviceType(DisposeDeviceType.HAOHAN_PLATFORM) - .areaCode(0) - .deviceName("中移杭研实验室浩瀚清洗设备") - .manufacturer("Haohan") - .model("Unknown") - .version("Unknown") - .userName("") - .password("") - .urlPath("DDoSClean/clean") - .urlType(HttpType.HTTP) - .readme("实验室测试设备") - .status(ObjectStatus.NORMAL) - .build(); + .ipAddr("10.88.77.88") + .ipPort("18080") + .deviceType(DisposeDeviceType.HAOHAN_PLATFORM) + .areaCode(0) + .deviceName("中移杭研实验室浩瀚清洗设备") + .manufacturer("Haohan") + .model("Unknown") + .version("Unknown") + .userName("") + .password("") + .urlPath("DDoSClean/clean") + .urlType(HttpType.HTTP) + .readme("实验室测试设备") + .status(ObjectStatus.NORMAL) + .build(); String httpType = dev.getUrlType() == HttpType.HTTP ? "http://" : "https://"; String addr = getAbilityMapKey(dev.getIpAddr(), dev.getIpPort()); @@ -82,8 +82,9 @@ public class HAOHanInterfaceTestCast extends InitTestEnvironment { // 初始化设备 db.initDeviceEnv(url, dev.getUserName(), dev.getPassword()); - MulReturnType ret = db.runDispose("192.168.3.5", DisposeCapacityType.CLEANUP, - nfDirection, ddoSAttackType, -1L); + MulReturnType ret = db.runDispose("192.168.3.5", + DisposeCapacityType.CLEANUP, + null, null, -1L); Assert.assertEquals(ret.getFirstParam(), ErrorCode.ERR_OK); } @@ -94,28 +95,24 @@ public class HAOHanInterfaceTestCast extends InitTestEnvironment { */ @Test public void t2_stopDisposeTest() { - DisposeAbility db; - - NetflowDirection[] nfDirection = {NetflowDirection.DIRECTION_IN}; - - DDoSAttackType[] ddoSAttackType = {DDoSAttackType.ACK_FLOOD, DDoSAttackType.CHARGED_AMPLIFICATION}; + DisposeAbility> db; DisposeDevice dev = DisposeDevice.builder() - .ipAddr("10.88.77.88") - .ipPort("18080") - .deviceType(DisposeDeviceType.HAOHAN_PLATFORM) - .areaCode(0) - .deviceName("中移杭研实验室浩瀚清洗设备") - .manufacturer("Haohan") - .model("Unknown") - .version("Unknown") - .userName("") - .password("") - .urlPath("DDoSClean/clean") - .urlType(HttpType.HTTP) - .readme("实验室测试设备") - .status(ObjectStatus.NORMAL) - .build(); + .ipAddr("10.88.77.88") + .ipPort("18080") + .deviceType(DisposeDeviceType.HAOHAN_PLATFORM) + .areaCode(0) + .deviceName("中移杭研实验室浩瀚清洗设备") + .manufacturer("Haohan") + .model("Unknown") + .version("Unknown") + .userName("") + .password("") + .urlPath("DDoSClean/clean") + .urlType(HttpType.HTTP) + .readme("实验室测试设备") + .status(ObjectStatus.NORMAL) + .build(); String httpType = dev.getUrlType() == HttpType.HTTP ? "http://" : "https://"; String addr = getAbilityMapKey(dev.getIpAddr(), dev.getIpPort()); @@ -128,8 +125,9 @@ public class HAOHanInterfaceTestCast extends InitTestEnvironment { db.initDeviceEnv(url, dev.getUserName(), dev.getPassword()); Assert.assertTrue(db.getDeviceLinkStatus()); - MulReturnType ret = db.stopDispose("192.168.3.5", DisposeCapacityType.CLEANUP, - nfDirection, ddoSAttackType, -1L); + MulReturnType ret = db.stopDispose("192.168.3.5", + DisposeCapacityType.CLEANUP, + null, null, -1L); Assert.assertEquals(ret.getFirstParam(), ErrorCode.ERR_OK); } @@ -140,24 +138,24 @@ public class HAOHanInterfaceTestCast extends InitTestEnvironment { */ @Test public void t3_getAbilityDeviceFirewareTest() { - DisposeAbility db; + DisposeAbility> db; DisposeDevice dev = DisposeDevice.builder() - .ipAddr("10.88.77.88") - .ipPort("18080") - .deviceType(DisposeDeviceType.HAOHAN_PLATFORM) - .areaCode(0) - .deviceName("中移杭研实验室浩瀚清洗设备") - .manufacturer("Haohan") - .model("Unknown") - .version("Unknown") - .userName("") - .password("") - .urlPath("DDoSClean/clean") - .urlType(HttpType.HTTP) - .readme("实验室测试设备") - .status(ObjectStatus.NORMAL) - .build(); + .ipAddr("10.88.77.88") + .ipPort("18080") + .deviceType(DisposeDeviceType.HAOHAN_PLATFORM) + .areaCode(0) + .deviceName("中移杭研实验室浩瀚清洗设备") + .manufacturer("Haohan") + .model("Unknown") + .version("Unknown") + .userName("") + .password("") + .urlPath("DDoSClean/clean") + .urlType(HttpType.HTTP) + .readme("实验室测试设备") + .status(ObjectStatus.NORMAL) + .build(); String httpType = dev.getUrlType() == HttpType.HTTP ? "http://" : "https://"; String addr = getAbilityMapKey(dev.getIpAddr(), dev.getIpPort());