REM:
1.修改获取全部处置任务接口单元测试
2.修改controller层获取全部处置任务接口,增加入参字段
3.修改manager层、service层分页获取处置任务方法,增加获取全部任务方法
4.修改查询处置任务类
This commit is contained in:
chenlinghy 2020-09-24 18:56:25 +08:00
parent 74b8508e83
commit 14e1bb35c3
8 changed files with 180 additions and 90 deletions

View File

@ -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<ErrorCode, DisposeTask> 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<ErrorCode, DisposeTask> 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<? extends BaseRespStatus> getAllDisposeTask(
@Validated(ValidGroups.ProtocolCommonValid.class)
@RequestBody ProtocolReqDTO<BasePagesGetReq> mr
@Validated(ValidGroups.ProtocolCommonValid.class)
@RequestBody ProtocolReqDTO<BasePagesGetReq> mr
) {
GetTaskRsp rspInfo = new GetTaskRsp();
GetTaskRsp rspInfo = GetTaskRsp.builder().build();
List<GetTaskDetail> getTaskDetail = new ArrayList<>();
MulReturnType<PageInfo<DisposeTask>, List<DisposeTask>> ret =
disposeTaskService.getPageDisposeTask(mr.getMsgContent().getStartPage(),
mr.getMsgContent().getPageSize());
//获取TaskId列表
List<Long> 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<DisposeTask> taskList = disposeTaskService.getAllDisposeTask();
List<Long> 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<PageInfo<DisposeTask>, List<DisposeTask>> 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);
}

View File

@ -64,13 +64,22 @@ public interface DisposeTaskManager {
*/
DisposeTask getDisposeTaskById(Long taskId);
/**
* Gets dispose task by id.
*
* @return the dispose task list
*/
List<DisposeTask> 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<PageInfo<DisposeTask>, List<DisposeTask>> getPagesOfTask(Integer startPage,
MulReturnType<PageInfo<DisposeTask>, List<DisposeTask>> getPagesOfTask(List<Long> taskId,
Integer startPage,
Integer pageSize);
}

View File

@ -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<DisposeTask> 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<PageInfo<DisposeTask>, List<DisposeTask>> getPagesOfTask(Integer startPage,
public MulReturnType<PageInfo<DisposeTask>, List<DisposeTask>> getPagesOfTask(List<Long> taskId,
Integer startPage,
Integer pageSize) {
List<DisposeTask> rspList = new ArrayList<>();
// 设置分页信息
PageHelper.startPage(startPage, pageSize);
List<DisposeTask> taskList = disposeTaskMapper.selectPageAll();
//获取所有可用处置任务
List<DisposeTask> taskList = disposeTaskMapper.selectPageAll().stream().
filter(v->taskId.size() == 0 || taskId.contains(v.getId())).collect(Collectors.toList());
if (taskList == null) {
taskList = new ArrayList<>();

View File

@ -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.
*/

View File

@ -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.
*/

View File

@ -38,13 +38,22 @@ public interface DisposeTaskService {
*/
DisposeTask getDisposeTask(Long taskId);
/**
* Gets dispose task.
*
* @return the dispose task list
*/
List<DisposeTask> 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<PageInfo<DisposeTask>, List<DisposeTask>> getPageDisposeTask(Integer startPage,
MulReturnType<PageInfo<DisposeTask>, List<DisposeTask>> getPageDisposeTask(List<Long> taskId,
Integer startPage,
Integer pageSize);
}

View File

@ -41,14 +41,14 @@ public class DisposeTaskServiceImpl implements DisposeTaskService {
@Override
public MulReturnType<ErrorCode, DisposeTask> 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<DisposeTask> 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<PageInfo<DisposeTask>, List<DisposeTask>> getPageDisposeTask(Integer startPage,
public MulReturnType<PageInfo<DisposeTask>, List<DisposeTask>> getPageDisposeTask(List<Long> taskId,
Integer startPage,
Integer pageSize) {
return disposeTaskManager.getPagesOfTask(startPage, pageSize);
return disposeTaskManager.getPagesOfTask(taskId, startPage, pageSize);
}
}

View File

@ -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<PageInfo<DisposeTask>, List<DisposeTask>> result = disposeTaskService.getPageDisposeTask(req.getStartPage(), req.getPageSize());
List<Long> taskIds = Arrays.stream(req.getTaskId()).map(Long::parseLong).collect(Collectors.toList());
MulReturnType<PageInfo<DisposeTask>, List<DisposeTask>> result = disposeTaskService.getPageDisposeTask(taskIds, req.getStartPage(), req.getPageSize());
Assert.assertTrue(rspInfo.getMsgContent().getTotalItems() >= result.getSecondParam().size());
}
}