diff --git a/src/main/java/com/dispose/controller/DisposeTaskController.java b/src/main/java/com/dispose/controller/DisposeTaskController.java index 0c5ef841..1d516126 100644 --- a/src/main/java/com/dispose/controller/DisposeTaskController.java +++ b/src/main/java/com/dispose/controller/DisposeTaskController.java @@ -45,9 +45,11 @@ import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import javax.validation.constraints.NotNull; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; /** * The type Dispose task controller. @@ -93,30 +95,30 @@ public class DisposeTaskController { // 构造处置任务参数 DisposeTask task = DisposeTask.builder() - .deviceId(Long.parseLong(Optional.ofNullable(req.getId()).orElse("-1"))) - .accountId(userAccountService.getUserIdByAuthHead(Objects.requireNonNull(headers.get("Authorization")) - .get(0))) - .disposeCapacity(CommonEnumHandler.codeOf(DisposeCapacityType.class, - req.getType())) - .disposeIp(Helper.ipAddressNormalize(req.getDisposeIp())) - .planEndTime(String.valueOf(req.getDisposeTime())) - .flowDirection(CommonEnumHandler.codeOf(NetflowDirection.class, - Optional.ofNullable(req.getFlowDirection()) - .orElse(2))) - .attackType(DDoSAttackType.getTypeMaskFromAttackType( - Optional.ofNullable(req.getAttackType()) - .orElse(new Integer[]{DDoSAttackType.ALL_ATTACKS.getValue()}))) - .flowBandWidth(Optional.ofNullable(req.getFlowBandwidth()) - .orElse(DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH)) - .build(); + .deviceId(Long.parseLong(Optional.ofNullable(req.getId()).orElse("-1"))) + .accountId(userAccountService.getUserIdByAuthHead(Objects.requireNonNull(headers.get("Authorization")) + .get(0))) + .disposeCapacity(CommonEnumHandler.codeOf(DisposeCapacityType.class, + req.getType())) + .disposeIp(Helper.ipAddressNormalize(req.getDisposeIp())) + .planEndTime(String.valueOf(req.getDisposeTime())) + .flowDirection(CommonEnumHandler.codeOf(NetflowDirection.class, + Optional.ofNullable(req.getFlowDirection()) + .orElse(2))) + .attackType(DDoSAttackType.getTypeMaskFromAttackType( + Optional.ofNullable(req.getAttackType()) + .orElse(new Integer[]{DDoSAttackType.ALL_ATTACKS.getValue()}))) + .flowBandWidth(Optional.ofNullable(req.getFlowBandwidth()) + .orElse(DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH)) + .build(); // 创建处置任务 MulReturnType ret = disposeTaskService.createTask(task); // 设置返回消息 TaskStartRsp rspInfo = TaskStartRsp.builder() - .disposeIp(task.getDisposeIp()) - .build(); + .disposeIp(task.getDisposeIp()) + .build(); rspInfo.setStatus(ret.getFirstParam().getCode()); rspInfo.setMessage(new String[]{ret.getFirstParam().getMsg()}); @@ -154,40 +156,40 @@ public class DisposeTaskController { DisposeCapacityType capType = CommonEnumHandler.codeOf(DisposeCapacityType.class, req.getType()); String endTime = String.valueOf(req.getDisposeTime()); NetflowDirection netDir = CommonEnumHandler.codeOf(NetflowDirection.class, - Optional.ofNullable(req.getFlowDirection()).orElse(2)); + Optional.ofNullable(req.getFlowDirection()).orElse(2)); Long attackType = DDoSAttackType.getTypeMaskFromAttackType(Optional.ofNullable(req.getAttackType()) - .orElse(new Integer[]{DDoSAttackType.ALL_ATTACKS.getValue()})); + .orElse(new Integer[]{DDoSAttackType.ALL_ATTACKS.getValue()})); Integer flowBand = Optional.ofNullable(req.getFlowBandwidth()) - .orElse(DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH); + .orElse(DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH); TaskStartMulRsp mulRsp = TaskStartMulRsp.builder() - .items(new ArrayList<>()) - .build(); + .items(new ArrayList<>()) + .build(); for (String ip : req.getMulDisposeIp()) { // 构造处置任务参数 DisposeTask task = DisposeTask.builder() - .deviceId(devId) - .accountId(aId) - .disposeCapacity(capType) - .disposeIp(Helper.ipAddressNormalize(ip)) - .planEndTime(endTime) - .flowDirection(netDir) - .attackType(attackType) - .flowBandWidth(flowBand) - .build(); + .deviceId(devId) + .accountId(aId) + .disposeCapacity(capType) + .disposeIp(Helper.ipAddressNormalize(ip)) + .planEndTime(endTime) + .flowDirection(netDir) + .attackType(attackType) + .flowBandWidth(flowBand) + .build(); // 创建处置任务 MulReturnType ret = disposeTaskService.createTask(task); // 设置返回消息 TaskStartRsp rspInfo = TaskStartRsp.builder() - .disposeIp(ip) - .build(); + .disposeIp(ip) + .build(); // 启动任务成功 if (ret.getFirstParam() == ErrorCode.ERR_OK || - ret.getFirstParam() == ErrorCode.ERR_TASKRUNNING) { + ret.getFirstParam() == ErrorCode.ERR_TASKRUNNING) { rspInfo.setTaskId(ret.getSecondParam().getId().toString()); rspInfo.setExpireTime(ret.getSecondParam().getPlanEndTime()); } @@ -228,13 +230,13 @@ public class DisposeTaskController { // 停止成功 if (ret.getFirstParam() == ErrorCode.ERR_OK) { rspInfo.setDisposeDevice(ret.getSecondParam().getDeviceTask().stream() - .map(DeviceTask::getDeviceId) - .map(String::valueOf) - .toArray(String[]::new)); + .map(DeviceTask::getDeviceId) + .map(String::valueOf) + .toArray(String[]::new)); rspInfo.setType(ret.getSecondParam().getDisposeCapacity().getValue()); rspInfo.setDisposeIp(ret.getSecondParam().getDisposeIp()); rspInfo.setLeftTime(String.valueOf(Math.abs( - Helper.getTimestampDiffNow(ret.getSecondParam().getPlanEndTime())) / 60)); + Helper.getTimestampDiffNow(ret.getSecondParam().getPlanEndTime())) / 60)); } rspInfo.setTaskId(tId); @@ -257,53 +259,79 @@ public class DisposeTaskController { @ResponseBody @ApiOperation("获取处置任务") public ProtocolRespDTO getAllDisposeTask( - @Validated(ValidGroups.ProtocolCommonValid.class) - @RequestBody ProtocolReqDTO mr + @Validated(ValidGroups.ProtocolCommonValid.class) + @RequestBody ProtocolReqDTO mr ) { - GetTaskRsp rspInfo = new GetTaskRsp(); + GetTaskRsp rspInfo = GetTaskRsp.builder().build(); + List getTaskDetail = new ArrayList<>(); - MulReturnType, List> ret = - disposeTaskService.getPageDisposeTask(mr.getMsgContent().getStartPage(), - mr.getMsgContent().getPageSize()); + //获取TaskId列表 + List reqTaskIds = Arrays.stream(mr.getMsgContent().getTaskId()). + map(Long::parseLong).collect(Collectors.toList()); - // 拼装返回数据 - rspInfo.setStatus(ErrorCode.ERR_OK.getCode()); - rspInfo.setMessage(new String[]{ErrorCode.ERR_OK.getMsg()}); + //获取所有可用处置任务 + List taskList = disposeTaskService.getAllDisposeTask(); + List idArray = taskList.stream().filter(v -> reqTaskIds.size() == 0 || reqTaskIds.contains(v.getId())) + .map(DisposeTask::getId).collect(Collectors.toList()); - // 分页信息 - rspInfo.setCurPageNumber(ret.getFirstParam().getPageNum()); - rspInfo.setPageSize(ret.getFirstParam().getPageSize()); - rspInfo.setTotalItems((int) ret.getFirstParam().getTotal()); - rspInfo.setTotalPages(ret.getFirstParam().getPages()); + if (idArray.size() > 0) { + MulReturnType, List> ret = + disposeTaskService.getPageDisposeTask(idArray, + mr.getMsgContent().getStartPage(), + mr.getMsgContent().getPageSize()); - ret.getSecondParam().forEach(v -> { - GetTaskDetail taskDetail = GetTaskDetail.builder() - .taskId(String.valueOf(v.getId())) - .accountId(String.valueOf(v.getAccountId())) - .disposeCapacity(v.getDisposeCapacity().getValue()) - .disposeIp(v.getDisposeIp()) - .createTime(v.getCreateTime()) - .planEndTime(v.getPlanEndTime()) - .endTime(v.getEndTime()) - .flowDirection(v.getFlowDirection().getValue()) - .attackType(v.getAttackType().toString()) - .flowBandWidth(v.getFlowBandWidth()) - .currentStatus(v.getCurrentStatus().getValue()) - .deviceTask(new ArrayList<>()) - .build(); - v.getDeviceTask().forEach(k -> { - GetDeviceTaskDetail deviceTaskDetail = GetDeviceTaskDetail.builder() - .devTaskId(String.valueOf(k.getId())) - .devId(String.valueOf(k.getDeviceId())) - .externId(String.valueOf(k.getExternId())) - .status(k.getStatus()) - .build(); + // 分页信息 + rspInfo.setCurPageNumber(ret.getFirstParam().getPageNum()); + rspInfo.setPageSize(ret.getFirstParam().getPageSize()); + rspInfo.setTotalItems((int) ret.getFirstParam().getTotal()); + rspInfo.setTotalPages(ret.getFirstParam().getPages()); - taskDetail.getDeviceTask().add(deviceTaskDetail); + ret.getSecondParam().forEach(v -> { + GetTaskDetail taskDetail = GetTaskDetail.builder() + .taskId(String.valueOf(v.getId())) + .accountId(String.valueOf(v.getAccountId())) + .disposeCapacity(v.getDisposeCapacity().getValue()) + .disposeIp(v.getDisposeIp()) + .createTime(v.getCreateTime()) + .planEndTime(v.getPlanEndTime()) + .endTime(v.getEndTime()) + .flowDirection(v.getFlowDirection().getValue()) + .attackType(v.getAttackType().toString()) + .flowBandWidth(v.getFlowBandWidth()) + .currentStatus(v.getCurrentStatus().getValue()) + .status(ErrorCode.ERR_OK.getCode()) + .message(new String[]{ErrorCode.ERR_OK.getMsg()}) + .deviceTask(new ArrayList<>()) + .build(); + v.getDeviceTask().forEach(k -> { + GetDeviceTaskDetail deviceTaskDetail = GetDeviceTaskDetail.builder() + .devTaskId(String.valueOf(k.getId())) + .devId(String.valueOf(k.getDeviceId())) + .externId(String.valueOf(k.getExternId())) + .status(k.getStatus()) + .build(); + + taskDetail.getDeviceTask().add(deviceTaskDetail); + }); + + getTaskDetail.add(taskDetail); }); + } - rspInfo.getItems().add(taskDetail); - }); + // 设置不存在taskId的错误码 + if (reqTaskIds.stream().anyMatch(v -> taskList.stream().noneMatch(k -> k.getId().equals(v)))) { + reqTaskIds.stream() + .filter(v -> taskList.stream().noneMatch(k -> k.getId().equals(v))) + .forEach(v -> { + GetTaskDetail td = GetTaskDetail.builder().build(); + td.setTaskId(String.valueOf(v)); + td.setStatus(ErrorCode.ERR_NOSUCHTASK.getCode()); + td.setMessage(new String[]{ErrorCode.ERR_NOSUCHTASK.getMsg()}); + getTaskDetail.add(td); + }); + } + + rspInfo.setItems(getTaskDetail); return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo); } diff --git a/src/main/java/com/dispose/manager/DisposeTaskManager.java b/src/main/java/com/dispose/manager/DisposeTaskManager.java index 224a1907..685aed3b 100644 --- a/src/main/java/com/dispose/manager/DisposeTaskManager.java +++ b/src/main/java/com/dispose/manager/DisposeTaskManager.java @@ -64,13 +64,22 @@ public interface DisposeTaskManager { */ DisposeTask getDisposeTaskById(Long taskId); + /** + * Gets dispose task by id. + * + * @return the dispose task list + */ + List getDisposeTasks(); + /** * Gets pages of task. * + * @param taskId the task id * @param startPage the start page * @param pageSize the page size * @return the pages of task */ - MulReturnType, List> getPagesOfTask(Integer startPage, + MulReturnType, List> getPagesOfTask(List taskId, + Integer startPage, Integer pageSize); } diff --git a/src/main/java/com/dispose/manager/impl/DisposeTaskManagerImpl.java b/src/main/java/com/dispose/manager/impl/DisposeTaskManagerImpl.java index 6349ddee..ebebca2f 100644 --- a/src/main/java/com/dispose/manager/impl/DisposeTaskManagerImpl.java +++ b/src/main/java/com/dispose/manager/impl/DisposeTaskManagerImpl.java @@ -109,20 +109,35 @@ public class DisposeTaskManagerImpl implements DisposeTaskManager { return disposeTaskMapper.getDisposeTaskById(taskId); } + /** + * Gets dispose task by id. + * + * @return the dispose task list + */ + @Override + public List getDisposeTasks() { + return disposeTaskMapper.selectAll(); + } + /** * Gets pages of task. * + * @param taskId the task id * @param startPage the start page * @param pageSize the page size * @return the pages of task */ @Override - public MulReturnType, List> getPagesOfTask(Integer startPage, + public MulReturnType, List> getPagesOfTask(List taskId, + Integer startPage, Integer pageSize) { List rspList = new ArrayList<>(); // 设置分页信息 PageHelper.startPage(startPage, pageSize); - List taskList = disposeTaskMapper.selectPageAll(); + + //获取所有可用处置任务 + List taskList = disposeTaskMapper.selectPageAll().stream(). + filter(v->taskId.size() == 0 || taskId.contains(v.getId())).collect(Collectors.toList()); if (taskList == null) { taskList = new ArrayList<>(); diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/BasePagesGetReq.java b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/BasePagesGetReq.java index f911255b..85ebbcdc 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/BasePagesGetReq.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/BasePagesGetReq.java @@ -10,6 +10,7 @@ import lombok.NoArgsConstructor; import javax.validation.constraints.NotNull; import javax.validation.constraints.Positive; +import javax.validation.constraints.Size; /** * The type Get device req. @@ -22,6 +23,12 @@ import javax.validation.constraints.Positive; @AllArgsConstructor @JsonInclude(JsonInclude.Include.NON_NULL) public class BasePagesGetReq { + /** + * The Task Id. + */ + @NotNull(message = "taskId 字段不能为空", groups = ValidGroups.ProtocolCommonValid.class) + @Size(min = 1, message = "taskId 字段必须指定明确任务id编号", groups = ValidGroups.ExplicitIdArrayValid.class) + private String[] taskId; /** * The Start page. */ diff --git a/src/main/java/com/dispose/pojo/dto/protocol/task/GetTaskDetail.java b/src/main/java/com/dispose/pojo/dto/protocol/task/GetTaskDetail.java index f6e32d92..d880a86a 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/task/GetTaskDetail.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/task/GetTaskDetail.java @@ -19,7 +19,7 @@ import java.util.List; @NoArgsConstructor @AllArgsConstructor @JsonPropertyOrder({"taskId", "accountId", "disposeCapacity", "disposeIp", "createTime", "planEndTime", "endTime", - "flowDirection", "attackType", "flowBandWidth", "currentStatus", "deviceTask"}) + "flowDirection", "attackType", "flowBandWidth", "currentStatus", "deviceTask", "status", "message"}) @JsonInclude(JsonInclude.Include.NON_NULL) public class GetTaskDetail { @@ -67,6 +67,15 @@ public class GetTaskDetail { * The Current status. */ private Integer currentStatus; + /** + * 0:成功;其它:失败原因. + */ + private Integer status; + /** + * 登录消息: status状态码对应的提示信息 + */ + private String[] message; + /** * The Device task. */ diff --git a/src/main/java/com/dispose/service/DisposeTaskService.java b/src/main/java/com/dispose/service/DisposeTaskService.java index aabb2ff0..0651b164 100644 --- a/src/main/java/com/dispose/service/DisposeTaskService.java +++ b/src/main/java/com/dispose/service/DisposeTaskService.java @@ -38,13 +38,22 @@ public interface DisposeTaskService { */ DisposeTask getDisposeTask(Long taskId); + /** + * Gets dispose task. + * + * @return the dispose task list + */ + List getAllDisposeTask(); + /** * Gets page dispose task. * + * @param taskId the task id * @param startPage the start page * @param pageSize the page size * @return the page dispose task */ - MulReturnType, List> getPageDisposeTask(Integer startPage, + MulReturnType, List> getPageDisposeTask(List taskId, + Integer startPage, Integer pageSize); } diff --git a/src/main/java/com/dispose/service/impl/DisposeTaskServiceImpl.java b/src/main/java/com/dispose/service/impl/DisposeTaskServiceImpl.java index c3f54d4c..62b23978 100644 --- a/src/main/java/com/dispose/service/impl/DisposeTaskServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/DisposeTaskServiceImpl.java @@ -41,14 +41,14 @@ public class DisposeTaskServiceImpl implements DisposeTaskService { @Override public MulReturnType createTask(DisposeTask task) { DisposeTask exitTask = disposeTaskManager.getDisposeTask(task.getDeviceId(), - task.getDisposeIp(), task.getDisposeCapacity()); + task.getDisposeIp(), task.getDisposeCapacity()); if (exitTask != null) { return new MulReturnType<>(ErrorCode.ERR_TASKRUNNING, exitTask); } ErrorCode err = disposeAbilityRouterService.verifyDisposeCapacity( - task.getDeviceId(), task.getDisposeIp(), task.getDisposeCapacity() + task.getDeviceId(), task.getDisposeIp(), task.getDisposeCapacity() ); if (err != ErrorCode.ERR_OK) { @@ -56,7 +56,7 @@ public class DisposeTaskServiceImpl implements DisposeTaskService { } return new MulReturnType<>(disposeTaskManager.addDisposeTask(task), - disposeTaskManager.getDisposeTaskById(task.getId())); + disposeTaskManager.getDisposeTaskById(task.getId())); } /** @@ -95,6 +95,15 @@ public class DisposeTaskServiceImpl implements DisposeTaskService { return disposeTaskManager.getDisposeTaskById(taskId); } + /** + * Gets dispose task. + * + * @return the dispose task list + */ + public List getAllDisposeTask() { + return disposeTaskManager.getDisposeTasks(); + } + /** * Gets page dispose task. * @@ -103,8 +112,9 @@ public class DisposeTaskServiceImpl implements DisposeTaskService { * @return the page dispose task */ @Override - public MulReturnType, List> getPageDisposeTask(Integer startPage, + public MulReturnType, List> getPageDisposeTask(List taskId, + Integer startPage, Integer pageSize) { - return disposeTaskManager.getPagesOfTask(startPage, pageSize); + return disposeTaskManager.getPagesOfTask(taskId, startPage, pageSize); } } diff --git a/src/test/java/com/dispose/test/dev/controller/DisposeTaskControllerTest.java b/src/test/java/com/dispose/test/dev/controller/DisposeTaskControllerTest.java index 89a5bf79..0a0600e1 100644 --- a/src/test/java/com/dispose/test/dev/controller/DisposeTaskControllerTest.java +++ b/src/test/java/com/dispose/test/dev/controller/DisposeTaskControllerTest.java @@ -46,6 +46,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -347,7 +348,9 @@ public class DisposeTaskControllerTest extends InitTestEnvironment { */ @Test public void a5_getAllDisposeTask() throws Exception { + String[] taskId = {}; BasePagesGetReq req = BasePagesGetReq.builder() + .taskId(taskId) .startPage(1) .pageSize(20) .build(); @@ -379,9 +382,9 @@ public class DisposeTaskControllerTest extends InitTestEnvironment { Assert.assertNotNull(rspInfo.getMsgContent()); Assert.assertTrue(rspInfo.getMsgContent().getCurPageNumber() <= req.getPageSize()); - Assert.assertEquals(rspInfo.getMsgContent().getPageSize(), req.getPageSize()); - MulReturnType, List> result = disposeTaskService.getPageDisposeTask(req.getStartPage(), req.getPageSize()); + List taskIds = Arrays.stream(req.getTaskId()).map(Long::parseLong).collect(Collectors.toList()); + MulReturnType, List> result = disposeTaskService.getPageDisposeTask(taskIds, req.getStartPage(), req.getPageSize()); Assert.assertTrue(rspInfo.getMsgContent().getTotalItems() >= result.getSecondParam().size()); } }