From 87fbaa635d45e6df20e408d788109ace29d992f6 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Wed, 29 Apr 2020 18:47:16 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E6=9B=B4=E6=AD=A3=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=8D=8F=E8=AE=AE=E5=AD=97=E6=AE=B5=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=202.=20=E5=A2=9E=E5=8A=A0=E5=81=9C=E6=AD=A2=E5=A4=84=E7=BD=AE?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=8E=A5=E5=8F=A3=203.=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E5=A4=84=E7=BD=AEIP=E5=81=9C=E6=AD=A2?= =?UTF-8?q?=E5=A4=84=E7=BD=AE=E4=BB=BB=E5=8A=A1=E6=8E=A5=E5=8F=A3=204.=20?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E9=83=A8=E5=88=86=E5=8D=8F=E8=AE=AE=E7=BC=BA?= =?UTF-8?q?=E5=B0=91=E5=BD=93=E5=89=8D=E4=BB=BB=E5=8A=A1=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/dispose/common/ErrorCode.java | 2 + src/main/java/com/dispose/common/Helper.java | 19 ++++++ .../DisposeNodeManagerController.java | 2 +- .../controller/DisposeTaskController.java | 68 +++++++++++++++---- .../dispose/pojo/vo/common/TaskInfoData.java | 2 +- .../dispose/pojo/vo/device/AddNodeRsp.java | 4 +- .../java/com/dispose/service/TaskService.java | 10 +++ .../service/impl/DisposeNodeManagerImpl.java | 3 - .../dispose/service/impl/TaskServiceImpl.java | 30 ++++++-- .../com/dispose/task/TaskManagerTask.java | 1 - .../DeviceNodeManagerControllerTest.java | 2 +- .../controller/TaskControllerTest.java | 8 ++- 12 files changed, 121 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/dispose/common/ErrorCode.java b/src/main/java/com/dispose/common/ErrorCode.java index 0a9e5dec..b68a45a1 100644 --- a/src/main/java/com/dispose/common/ErrorCode.java +++ b/src/main/java/com/dispose/common/ErrorCode.java @@ -129,6 +129,8 @@ public enum ErrorCode { * Err nosuchtask error code. */ ERR_NOSUCHTASK(30, "没有该任务"), + + ERR_TASKNOTRUNNING(31, "该任务没有运行"), ; /** diff --git a/src/main/java/com/dispose/common/Helper.java b/src/main/java/com/dispose/common/Helper.java index 445053fe..e218f36e 100644 --- a/src/main/java/com/dispose/common/Helper.java +++ b/src/main/java/com/dispose/common/Helper.java @@ -1,5 +1,6 @@ package com.dispose.common; +import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; @@ -54,4 +55,22 @@ public class Helper { return intArray.toArray(new Integer[0]); } + + public static int getTimestampSecond(String dateTime) { + return (int)(Timestamp.valueOf(dateTime).toInstant().toEpochMilli() / 1000); + } + + public static int getTimestampDiff(String begin, String end) { + int starTime = getTimestampSecond(begin); + int endTm = getTimestampSecond(end); + + return endTm - starTime; + } + + public static int getTimestampDiffNow(String begin) { + int starTime = getTimestampSecond(begin); + int endTm = (int)(System.currentTimeMillis() / 1000); + + return endTm - starTime; + } } diff --git a/src/main/java/com/dispose/controller/DisposeNodeManagerController.java b/src/main/java/com/dispose/controller/DisposeNodeManagerController.java index 4301407c..be1de6a0 100644 --- a/src/main/java/com/dispose/controller/DisposeNodeManagerController.java +++ b/src/main/java/com/dispose/controller/DisposeNodeManagerController.java @@ -104,7 +104,7 @@ public class DisposeNodeManagerController { retStatus.setDevId(ret.getSecondParam()); retStatus.setStatus(ret.getFirstParam().getCode()); retStatus.setMessage(ret.getFirstParam().getMsg()); - rspInfo.getResult().add(retStatus); + rspInfo.getItems().add(retStatus); }); return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo); diff --git a/src/main/java/com/dispose/controller/DisposeTaskController.java b/src/main/java/com/dispose/controller/DisposeTaskController.java index 1b6e7180..a1cf5208 100644 --- a/src/main/java/com/dispose/controller/DisposeTaskController.java +++ b/src/main/java/com/dispose/controller/DisposeTaskController.java @@ -1,7 +1,9 @@ package com.dispose.controller; import com.dispose.common.ErrorCode; +import com.dispose.common.FlowDirection; import com.dispose.common.Helper; +import com.dispose.manager.TaskCacheManager; import com.dispose.pojo.dto.ProtocolReqDTO; import com.dispose.pojo.dto.ProtocolRespDTO; import com.dispose.pojo.entity.DisposeDevice; @@ -48,12 +50,27 @@ import java.util.stream.Collectors; @Api(value = "抗DDoS处置平台处置任务接口", tags = "抗DDoS处置平台处置任务接口") @Component public class DisposeTaskController { + /** + * The Dispose node manager. + */ @Resource private DisposeNodeManager disposeNodeManager; + /** + * The Task service. + */ @Resource private TaskService taskService; + /** + * The Task cache manager. + */ + @Resource + private TaskCacheManager taskCacheManager; + + /** + * The User account service. + */ @Resource private UserAccountService userAccountService; @@ -86,8 +103,8 @@ public class DisposeTaskController { .type(reqInfo.getType()) .disposeIp(reqInfo.getDisposeIp()) .planEndTime(String.valueOf(reqInfo.getDisposeTime())) - .flowDirection(reqInfo.getFlowDirection()) - .flowBandWidth(reqInfo.getFlowBandWidth()) + .flowDirection(reqInfo.getFlowDirection() != null ? reqInfo.getFlowDirection() : FlowDirection.DIRECTION_TWOWAY.getCode()) + .flowBandWidth(reqInfo.getFlowBandWidth() != null ? reqInfo.getFlowBandWidth() : 1024) .attackType(Helper.attackArrayToString(reqInfo.getAttackType())) .build(); @@ -129,9 +146,22 @@ public class DisposeTaskController { } for (String v : reqInfo.getTaskId()) { - TaskInfoData taskData = TaskInfoData.builder() - .taskId(v) - .build(); + TaskInfoData taskData = TaskInfoData.builder().taskId(v).build(); + TaskInfoDetail task = taskCacheManager.getTaskById(Long.parseLong(v)); + + if (task == null) { + err = ErrorCode.ERR_TASKNOTRUNNING; + } else { + err = taskService.stopTask(Long.parseLong(v)); + + if (err == ErrorCode.ERR_OK) { + taskData.setTaskId(v); + taskData.setDisposeIp(task.getDisposeIp()); + taskData.setType(task.getType()); + taskData.setLeftTime(Math.abs(Helper.getTimestampDiffNow(task.getBeginTime()) + - Helper.getTimestampDiffNow(task.getPlanEndTime()))); + } + } taskData.setStatus(err.getCode()); taskData.setMessage(err.getMsg()); @@ -139,7 +169,7 @@ public class DisposeTaskController { rspInfo.getItems().add(taskData); } - return ProtocolRespDTO.result(err, rspInfo); + return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo); } /** @@ -166,13 +196,27 @@ public class DisposeTaskController { TaskInfoRsp rspInfo = new TaskInfoRsp(); reqInfo.getItems().forEach(v -> { - TaskInfoData taskData = TaskInfoData.builder() - .type(v.getType()) - .build(); + ErrorCode retError; + + TaskInfoData taskData = TaskInfoData.builder().type(v.getType()).build(); + + List> ret = taskService.stopTaskByDisposeIp(Long.parseLong(v.getId()), v.getType(), v.getDisposeIp()); + + if (ret.size() == 0) { + retError = ErrorCode.ERR_NOSUCHTASK; + } else { + retError = ret.get(0).getFirstParam(); + TaskInfoDetail taskInfo = ret.get(0).getSecondParam(); + taskData.setTaskId(String.valueOf(taskInfo.getId())); + taskData.setType(taskInfo.getType()); + taskData.setDisposeIp(v.getDisposeIp()); + taskData.setLeftTime(Math.abs(Helper.getTimestampDiffNow(taskInfo.getBeginTime()) + - Helper.getTimestampDiffNow(taskInfo.getPlanEndTime()))); + } taskData.setId(v.getId()); - taskData.setStatus(err.getCode()); - taskData.setMessage(err.getMsg()); + taskData.setStatus(retError.getCode()); + taskData.setMessage(retError.getMsg()); rspInfo.getItems().add(taskData); }); @@ -207,7 +251,7 @@ public class DisposeTaskController { */ @PostMapping("/stop_all") @ResponseBody - @ApiOperation("启动处置任务") + @ApiOperation("停止所有处置任务") public ProtocolRespDTO taskStopAll(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { diff --git a/src/main/java/com/dispose/pojo/vo/common/TaskInfoData.java b/src/main/java/com/dispose/pojo/vo/common/TaskInfoData.java index 191f5729..f9a88764 100644 --- a/src/main/java/com/dispose/pojo/vo/common/TaskInfoData.java +++ b/src/main/java/com/dispose/pojo/vo/common/TaskInfoData.java @@ -16,7 +16,7 @@ import lombok.NoArgsConstructor; @Builder @NoArgsConstructor @AllArgsConstructor -@JsonPropertyOrder({"id", "taskId", "type", "disposeIp", "startTime", "disposeTime", +@JsonPropertyOrder({"id", "taskId", "currentStatus", "type", "disposeIp", "startTime", "disposeTime", "flowDirection", "attackType", "flowBandWidth", "flowAttack", "flowClean", "leftTime", "status", "message"}) @JsonInclude(JsonInclude.Include.NON_NULL) public class TaskInfoData extends IDReturnStatus { diff --git a/src/main/java/com/dispose/pojo/vo/device/AddNodeRsp.java b/src/main/java/com/dispose/pojo/vo/device/AddNodeRsp.java index 4b138e25..de9243a6 100644 --- a/src/main/java/com/dispose/pojo/vo/device/AddNodeRsp.java +++ b/src/main/java/com/dispose/pojo/vo/device/AddNodeRsp.java @@ -19,12 +19,12 @@ public class AddNodeRsp { /** * The Result. */ - List result; + List items; /** * Instantiates a new Add node rsp. */ public AddNodeRsp() { - this.result = new ArrayList<>(); + this.items = new ArrayList<>(); } } diff --git a/src/main/java/com/dispose/service/TaskService.java b/src/main/java/com/dispose/service/TaskService.java index a0e5c4b0..4907f26c 100644 --- a/src/main/java/com/dispose/service/TaskService.java +++ b/src/main/java/com/dispose/service/TaskService.java @@ -38,6 +38,16 @@ public interface TaskService { */ ErrorCode stopTask(Long taskId); + /** + * Stop task by dispose ip error code. + * + * @param devId the dev id + * @param type the type + * @param ipAddr the ip addr + * @return the error code + */ + List> stopTaskByDisposeIp(Long devId, int type, String ipAddr); + /** * Finish task error code. * diff --git a/src/main/java/com/dispose/service/impl/DisposeNodeManagerImpl.java b/src/main/java/com/dispose/service/impl/DisposeNodeManagerImpl.java index 7d987067..60d88d01 100644 --- a/src/main/java/com/dispose/service/impl/DisposeNodeManagerImpl.java +++ b/src/main/java/com/dispose/service/impl/DisposeNodeManagerImpl.java @@ -73,7 +73,6 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager { */ @Override public ErrorCode delDisposeDeviceById(Long id) { - DisposeEntryManager dp; DisposeDevice dev = disposeDeviceMapper.getDeviceById(id); if (dev == null) { @@ -91,8 +90,6 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager { */ @Override public ErrorCode delDisposeDeviceByIp(String ipAddr) { - DisposeEntryManager dp; - if (disposeDeviceMapper.isDeviceExistsByIp(ipAddr) == 0 || !disposeDevMap.containsKey(ipAddr)) { return ErrorCode.ERR_NOSUCHDEVICE; diff --git a/src/main/java/com/dispose/service/impl/TaskServiceImpl.java b/src/main/java/com/dispose/service/impl/TaskServiceImpl.java index 9a6f2927..1bd570f6 100644 --- a/src/main/java/com/dispose/service/impl/TaskServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/TaskServiceImpl.java @@ -210,8 +210,7 @@ public class TaskServiceImpl implements TaskService { taskCacheManager.upgradeTaskStatus(taskId, DisposeTaskStatus.TASK_RUNNING.getCode()); // 异步启动处置任务 - CompletableFuture future = CompletableFuture - .supplyAsync(() -> dp.runDispose(task.getDisposeIp(), DeviceCapacity.values()[task.getType()])) + CompletableFuture.supplyAsync(() -> dp.runDispose(task.getDisposeIp(), DeviceCapacity.values()[task.getType()])) .whenComplete((v, ex) -> { if (ex != null) { log.error(ex.getMessage()); @@ -260,8 +259,7 @@ public class TaskServiceImpl implements TaskService { int prdStatus = task.getCurrentStatus(); // 异步启动处置任务 - CompletableFuture future = CompletableFuture - .supplyAsync(() -> dp.stopDispose(task.getDisposeIp(), DeviceCapacity.values()[task.getType()])) + CompletableFuture.supplyAsync(() -> dp.stopDispose(task.getDisposeIp(), DeviceCapacity.values()[task.getType()])) .whenComplete((v, ex) -> { if (ex != null) { // 恢复缓存中任务状态到先前状态 @@ -274,11 +272,10 @@ public class TaskServiceImpl implements TaskService { log.error("Start task {}:{} error {}", task.getId(), task.getDisposeIp(), err.getMsg()); } else { // 任务执行完成后更新数据库处置任务状态 - disposeTaskMapper.changeTaskCurrentStatus(taskId, DisposeTaskStatus.TASK_STOP.getCode()); + finishTask(taskId); } } }); - } return ErrorCode.ERR_OK; @@ -401,4 +398,25 @@ public class TaskServiceImpl implements TaskService { && Objects.equals(v.getDisposeIp(), disposeIp) && Objects.equals(v.getType(), disposeType)); } + + @Override + public List> stopTaskByDisposeIp(Long devId, int type, String ipAddr) { + List> retList = new ArrayList<>(); + + // 根据处置IP,拿出所有正在处置的任务 + List taskList = taskCacheManager.getAllTask().parallelStream() + .filter(v -> ((devId == null || devId == -1L) || Objects.equals(v.getDeviceId(), devId) + && Objects.equals(v.getType(), type)) + && Objects.equals(v.getDisposeIp(), ipAddr) + && taskIsRunning(v)) + .collect(Collectors.toList()); + + // 对符合条件的任务调用停止命令 + taskList.parallelStream().forEach(v -> { + ErrorCode err = stopTask(v.getId()); + retList.add(new MReturnType<>(err, v)); + }); + + return retList; + } } diff --git a/src/main/java/com/dispose/task/TaskManagerTask.java b/src/main/java/com/dispose/task/TaskManagerTask.java index 66ca98b0..577daf7b 100644 --- a/src/main/java/com/dispose/task/TaskManagerTask.java +++ b/src/main/java/com/dispose/task/TaskManagerTask.java @@ -46,7 +46,6 @@ public class TaskManagerTask { log.info("Finish expired task {}:{} begin at {}", taskData.getId(), taskData.getDisposeIp(), taskData.getBeginTime()); taskService.stopTask(taskData.getId()); - taskService.finishTask(taskData.getId()); continue; } diff --git a/src/test/java/com/dispose/controller/DeviceNodeManagerControllerTest.java b/src/test/java/com/dispose/controller/DeviceNodeManagerControllerTest.java index d2ca385f..d3b2eb26 100644 --- a/src/test/java/com/dispose/controller/DeviceNodeManagerControllerTest.java +++ b/src/test/java/com/dispose/controller/DeviceNodeManagerControllerTest.java @@ -142,7 +142,7 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment { //将json字符串转为AddNodeRsp类 AddNodeRsp addNodeRsp = objectMapper.readValue(msgContent, AddNodeRsp.class); - List addNodeList = addNodeRsp.getResult(); + List addNodeList = addNodeRsp.getItems(); for (AddNodeRetData d : addNodeList ) { //before: no device information after: device information exists diff --git a/src/test/java/com/dispose/controller/TaskControllerTest.java b/src/test/java/com/dispose/controller/TaskControllerTest.java index 3874229d..d0f3ffda 100644 --- a/src/test/java/com/dispose/controller/TaskControllerTest.java +++ b/src/test/java/com/dispose/controller/TaskControllerTest.java @@ -133,7 +133,7 @@ public class TaskControllerTest extends InitTestEnvironment { @Test public void t2_stopTask() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .taskId(new String[]{"1", "2"}) + .taskId(new String[]{"40", "2"}) .build(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -233,8 +233,10 @@ public class TaskControllerTest extends InitTestEnvironment { @Test public void t5_stopAllTask() throws Exception { StopTaskData itemData = StopTaskData.builder() - .type(DeviceCapacity.CLEANUP.getCode()) - .build(); + .disposeIp("192.168.1.1") + .type(DeviceCapacity.CLEANUP.getCode()) + .id("210") + .build(); StopTaskReq reqData = new StopTaskReq();