REM:
1. 新增获取能力节点处置任务接口
2. 新增获取处置任务详细信息接口
3. 新增相关单元测试用例
This commit is contained in:
huangxin 2020-04-27 18:06:36 +08:00
parent 1bbe6125fb
commit acd182a9f5
9 changed files with 392 additions and 75 deletions

View File

@ -116,14 +116,19 @@ public enum ErrorCode {
ERR_UNSUPPORT(27, "不支持的操作"),
/**
* Err interrupt error code.
* The Err interrupt.
*/
ERR_INTERRUPT(28, "操作中断"),
/**
* Err calldevice error code.
* The Err calldevice.
*/
ERR_CALLDEVICE(29, "调用设备失败"),
/**
* Err nosuchtask error code.
*/
ERR_NOSUCHTASK(30, "没有该任务"),
;
/**

View File

@ -161,6 +161,8 @@ public class DisposeNodeInfoController {
VersionRsp ver = VersionRsp.builder()
.version(v.getVersion()).build();
ver.setId(v.getId().toString());
ver.setStatus(ErrorCode.ERR_OK.getCode());
ver.setMessage(ErrorCode.ERR_OK.getMsg());
@ -656,7 +658,7 @@ public class DisposeNodeInfoController {
taskData.setStatus(ErrorCode.ERR_OK.getCode());
taskData.setMessage(ErrorCode.ERR_OK.getMsg());
taskService.getNodeAllTask(v.getId()).parallelStream().forEach(k -> {
taskService.getNodeAllRunningTask(v.getId()).parallelStream().forEach(k -> {
Long tmLong = Timestamp.valueOf(k.getBeginTime()).toInstant().toEpochMilli() / 1000;
Long endTm = Timestamp.valueOf(k.getPlanEndTime()).toInstant().toEpochMilli() / 1000;
Long diff = endTm - tmLong;

View File

@ -3,15 +3,29 @@ package com.dispose.controller;
import com.dispose.common.ErrorCode;
import com.dispose.pojo.dto.ProtocolReqDTO;
import com.dispose.pojo.dto.ProtocolRespDTO;
import com.dispose.pojo.entity.DisposeDevice;
import com.dispose.pojo.vo.common.IDArrayReq;
import com.dispose.pojo.vo.common.TaskInfoData;
import com.dispose.pojo.vo.common.TaskInfoDetail;
import com.dispose.pojo.vo.information.NodeTaskData;
import com.dispose.pojo.vo.information.NodeTaskRsp;
import com.dispose.pojo.vo.task.GetTaskReq;
import com.dispose.pojo.vo.task.StartTaskReq;
import com.dispose.pojo.vo.task.StartTaskRsp;
import com.dispose.pojo.vo.task.StopTaskReq;
import com.dispose.pojo.vo.task.TaskInfoRsp;
import com.dispose.service.DisposeNodeManager;
import com.dispose.service.TaskService;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
@ -31,6 +45,12 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Api(value = "抗DDoS处置平台处置任务接口", tags = "抗DDoS处置平台处置任务接口")
@Component
public class DisposeTaskController {
@Resource
private DisposeNodeManager disposeNodeManager;
@Resource
private TaskService taskService;
/**
* Task start protocol resp dto.
*
@ -188,7 +208,85 @@ public class DisposeTaskController {
@ApiOperation("获取节点处置任务")
public ProtocolRespDTO getNodeTask(@RequestBody ProtocolReqDTO mr,
@RequestHeader HttpHeaders headers) throws JsonProcessingException {
return getAllTask(mr, headers);
ErrorCode err = mr.verifyRequest(headers);
if (err != ErrorCode.ERR_OK) {
return ProtocolRespDTO.result(err);
}
GetTaskReq reqInfo = mr.getRequestObject(GetTaskReq.class);
NodeTaskRsp rspInfo = new NodeTaskRsp();
List<DisposeDevice> valuableData = disposeNodeManager.getAllDisposeDevice()
.parallelStream()
.filter(v -> reqInfo.getId().length == 0
|| Arrays.stream(reqInfo.getId()).anyMatch(s -> s.equals(v.getId().toString())))
.collect(Collectors.toList());
valuableData.parallelStream().forEach(v -> {
NodeTaskData taskData = new NodeTaskData();
rspInfo.getItems().add(taskData);
taskData.setId(v.getId().toString());
AtomicBoolean bHasData = new AtomicBoolean(false);
taskService.getNodeAllTask(v.getId()).parallelStream().forEach(k -> {
if (v.getType().equals(reqInfo.getType()) || reqInfo.getType() == -1) {
Long tmLong = Timestamp.valueOf(k.getBeginTime()).toInstant().toEpochMilli() / 1000;
Long endTm = Timestamp.valueOf(k.getPlanEndTime()).toInstant().toEpochMilli() / 1000;
Long diff = endTm - tmLong;
TaskInfoData taskInfoData = TaskInfoData.builder()
.taskId(k.getId().toString())
.type(k.getType())
.startTime(tmLong.intValue())
.disposeTime(diff.intValue())
.disposeIp(k.getDisposeIp())
.flowAttack(0)
.flowClean(0)
.build();
taskData.getTaskArray().add(taskInfoData);
bHasData.compareAndSet(false, true);
}
});
if (bHasData.get()) {
taskData.setStatus(ErrorCode.ERR_OK.getCode());
taskData.setMessage(ErrorCode.ERR_OK.getMsg());
} else {
taskData.setStatus(ErrorCode.ERR_NOSUCHTYPE.getCode());
taskData.setMessage(ErrorCode.ERR_NOSUCHTYPE.getMsg());
taskData.setTaskArray(null);
}
});
if (reqInfo.getId().length != 0) {
List<String> unExists = Arrays.stream(reqInfo.getId())
.filter(v -> valuableData.parallelStream().noneMatch(k -> k.getId() == Long.parseLong(v)))
.collect(Collectors.toList());
unExists.parallelStream().forEach(v -> {
NodeTaskData taskData = new NodeTaskData();
rspInfo.getItems().add(taskData);
taskData.setId(v);
taskData.setStatus(ErrorCode.ERR_NOSUCHDEVICE.getCode());
taskData.setMessage(ErrorCode.ERR_NOSUCHDEVICE.getMsg());
taskData.setTaskArray(null);
});
}
return ProtocolRespDTO.result(err, rspInfo);
}
private Integer[] getIntArrayByString(String str) {
String[] spilt = str.split(",");
List<Integer> intArray = new ArrayList<>();
for (String s : spilt) {
intArray.add(Integer.parseInt(s));
}
return intArray.toArray(new Integer[0]);
}
/**
@ -214,20 +312,48 @@ public class DisposeTaskController {
IDArrayReq reqInfo = mr.getRequestObject(IDArrayReq.class);
TaskInfoRsp rspInfo = new TaskInfoRsp();
if (reqInfo.getTaskId() == null
|| reqInfo.getTaskId().length == 0) {
return ProtocolRespDTO.result(ErrorCode.ERR_INPUTMISS);
}
List<TaskInfoDetail> valuableData = taskService.getAllTask()
.parallelStream()
.filter(v -> reqInfo.getTaskId().length == 0
|| Arrays.stream(reqInfo.getTaskId()).anyMatch(s -> s.equals(v.getId().toString())))
.collect(Collectors.toList());
for (String v : reqInfo.getTaskId()) {
TaskInfoData taskData = TaskInfoData.builder()
.taskId(v)
valuableData.parallelStream().forEach(v -> {
Long tmLong = Timestamp.valueOf(v.getBeginTime()).toInstant().toEpochMilli() / 1000;
Long endTm = Timestamp.valueOf(v.getPlanEndTime()).toInstant().toEpochMilli() / 1000;
Long diff = endTm - tmLong;
TaskInfoData taskInfoData = TaskInfoData.builder()
.taskId(v.getId().toString())
.type(v.getType())
.startTime(tmLong.intValue())
.disposeTime(diff.intValue())
.disposeIp(v.getDisposeIp())
.attackType(getIntArrayByString(v.getAttackType()))
.flowDirection(v.getFlowDirection())
.flowBandWidth(v.getFlowBandWidth())
.flowAttack(0)
.flowClean(0)
.build();
taskData.setStatus(err.getCode());
taskData.setMessage(err.getMsg());
rspInfo.getItems().add(taskInfoData);
taskInfoData.setId(v.getId().toString());
taskInfoData.setStatus(ErrorCode.ERR_OK.getCode());
taskInfoData.setMessage(ErrorCode.ERR_OK.getMsg());
});
if (reqInfo.getTaskId().length != 0) {
List<String> unExists = Arrays.stream(reqInfo.getTaskId())
.filter(v -> valuableData.parallelStream().noneMatch(k -> k.getId() == Long.parseLong(v)))
.collect(Collectors.toList());
unExists.parallelStream().forEach(v -> {
TaskInfoData taskData = new TaskInfoData();
rspInfo.getItems().add(taskData);
taskData.setId(v);
taskData.setStatus(ErrorCode.ERR_NOSUCHDEVICE.getCode());
taskData.setMessage(ErrorCode.ERR_NOSUCHDEVICE.getMsg());
});
}
return ProtocolRespDTO.result(err, rspInfo);

View File

@ -1,39 +1,47 @@
package com.dispose.manager.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import cn.hutool.core.collection.CollectionUtil;
import com.dispose.manager.DisposeDeviceManager;
import com.dispose.mapper.DisposeDeviceMapper;
import com.dispose.pojo.entity.DisposeDevice;
import cn.hutool.core.collection.CollectionUtil;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
/**
*
*
* @author phoenix
* @date 2020年4月27日
* The type Dispose device manager.
*/
@Component
@Slf4j
public class DisposeDeviceManagerImpl implements DisposeDeviceManager {
/**
* The Dispose device mapper.
*/
@Resource
private DisposeDeviceMapper disposeDeviceMapper;
/**
* Save dispose device int.
*
* @param dd the dd
* @return the int
*/
@Override
public int saveDisposeDevice(DisposeDevice dd) {
// TODO Auto-generated method stub
return disposeDeviceMapper.insert(dd);
}
/**
* Find dispose device by ip dispose device.
*
* @param ip the ip
* @return the dispose device
*/
@Override
public DisposeDevice findDisposeDeviceByIp(String ip) {
// TODO Auto-generated method stub
@ -48,12 +56,24 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager {
return null;
}
/**
* Find dispose device by id dispose device.
*
* @param id the id
* @return the dispose device
*/
@Override
public DisposeDevice findDisposeDeviceById(Long id) {
// TODO Auto-generated method stub
return disposeDeviceMapper.selectByPrimaryKey(id);
}
/**
* Del dispose device by ip int.
*
* @param ip the ip
* @return the int
*/
@Override
public int delDisposeDeviceByIp(String ip) {
// TODO Auto-generated method stub
@ -64,6 +84,11 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager {
return disposeDeviceMapper.deleteByExample(example);
}
/**
* Find all dispose device list.
*
* @return the list
*/
@Override
public List<DisposeDevice> findAllDisposeDevice() {
// TODO Auto-generated method stub

View File

@ -16,7 +16,8 @@ import lombok.NoArgsConstructor;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonPropertyOrder({"id", "taskId", "type", "disposeIp", "leftTime", "status", "message"})
@JsonPropertyOrder({"id", "taskId", "type", "disposeIp", "startTime", "disposeTime",
"flowDirection", "attackType", "flowBandWidth", "flowAttack", "flowClean", "leftTime", "status", "message"})
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TaskInfoData extends IDReturnStatus {
/**

View File

@ -0,0 +1,22 @@
package com.dispose.pojo.vo.task;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class GetTaskReq {
/**
* The Id.
*/
private String[] id;
/**
* The Type.
*/
private Integer type;
}

View File

@ -76,4 +76,11 @@ public interface TaskService {
* @return the node all task
*/
List<TaskInfoDetail> getNodeAllTask(Long devId);
/**
* Gets all task.
*
* @return the all task
*/
List<TaskInfoDetail> getAllTask();
}

View File

@ -202,6 +202,26 @@ public class TaskServiceImpl implements TaskService {
*/
@Override
public List<TaskInfoDetail> getNodeAllTask(Long devId) {
List<TaskInfoDetail> taskList = taskCacheManager
.getAllTask()
.stream()
.filter(v -> v.getDeviceId().equals(devId))
.collect(Collectors.toList());
if(taskList.size() > 0) {
return taskList;
}
return null;
}
/**
* Gets all task.
*
* @return the all task
*/
@Override
public List<TaskInfoDetail> getAllTask() {
return taskCacheManager.getAllTask();
}
}

View File

@ -3,13 +3,19 @@ package com.dispose.controller;
import com.dispose.Global.InitTestEnvironment;
import com.dispose.common.ConstValue;
import com.dispose.common.DeviceCapacity;
import com.dispose.manager.TaskCacheManager;
import com.dispose.mapper.DisposeDeviceMapper;
import com.dispose.pojo.dto.ProtocolReqDTO;
import com.dispose.pojo.entity.DisposeDevice;
import com.dispose.pojo.vo.common.IDArrayReq;
import com.dispose.pojo.vo.common.TaskInfoDetail;
import com.dispose.pojo.vo.task.GetTaskReq;
import com.dispose.pojo.vo.task.StartTaskReq;
import com.dispose.pojo.vo.task.StopTaskData;
import com.dispose.pojo.vo.task.StopTaskReq;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -44,6 +50,49 @@ public class TaskControllerTest extends InitTestEnvironment {
@Resource
private ObjectMapper objectMapper;
/**
* The Task cache manager.
*/
@Resource
private TaskCacheManager taskCacheManager;
/**
* The Dispose device mapper.
*/
@Resource
private DisposeDeviceMapper disposeDeviceMapper;
/**
* Gets exists device id.
*
* @return the exists device id
*/
private Long getExistsDeviceId() {
List<DisposeDevice> lt = disposeDeviceMapper.selectAll();
if (lt != null && lt.size() > 0) {
return lt.get(0).getId();
}
return -1L;
}
/**
* Gets exists task id.
*
* @return the exists task id
*/
private Long getExistsTaskId() {
List<TaskInfoDetail> lt = taskCacheManager.getAllTask();
if(lt != null && lt.size() > 0) {
return lt.get(0).getId();
}
return -1L;
}
/**
* T 1 start task.
*
@ -210,14 +259,15 @@ public class TaskControllerTest extends InitTestEnvironment {
}
/**
* T 6 get node task.
* T 6 get node all task.
*
* @throws Exception the exception
*/
@Test
public void t6_getNodeTask() throws Exception {
IDArrayReq reqData = IDArrayReq.builder()
.taskId(new String[]{"1", "2"})
public void t6_getNodeAllTask() throws Exception {
GetTaskReq reqData = GetTaskReq.builder()
.id(new String[]{String.valueOf(getExistsDeviceId()), "123"})
.type(0)
.build();
ProtocolReqDTO reqInfo = new ProtocolReqDTO();
@ -239,14 +289,73 @@ public class TaskControllerTest extends InitTestEnvironment {
}
/**
* T 6 get all task.
* T 6 get node all task all.
*
* @throws Exception the exception
*/
@Test
public void t6_getAllTask() throws Exception {
public void t6_getNodeAllTaskAll() throws Exception {
GetTaskReq reqData = GetTaskReq.builder()
.id(new String[]{})
.type(2)
.build();
ProtocolReqDTO reqInfo = new ProtocolReqDTO();
reqInfo.setVer(ConstValue.Protocol.VERSION);
reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE);
reqInfo.setTimeStamp(System.currentTimeMillis());
reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData));
mockMvc.perform(MockMvcRequestBuilders
.post("/task/get_node")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + getLogToken())
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andReturn()
.getResponse()
.getContentAsString();
}
/**
* T 7 get all task.
*
* @throws Exception the exception
*/
@Test
public void t7_getAllTask() throws Exception {
IDArrayReq reqData = IDArrayReq.builder()
.taskId(new String[]{"1", "2"})
.taskId(new String[]{String.valueOf(getExistsTaskId()), "123"})
.build();
ProtocolReqDTO reqInfo = new ProtocolReqDTO();
reqInfo.setVer(ConstValue.Protocol.VERSION);
reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE);
reqInfo.setTimeStamp(System.currentTimeMillis());
reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData));
mockMvc.perform(MockMvcRequestBuilders
.post("/task/get")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + getLogToken())
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andReturn()
.getResponse()
.getContentAsString();
}
/**
* T 7 get all task all.
*
* @throws Exception the exception
*/
@Test
public void t7_getAllTaskAll() throws Exception {
IDArrayReq reqData = IDArrayReq.builder()
.taskId(new String[]{})
.build();
ProtocolReqDTO reqInfo = new ProtocolReqDTO();