diff --git a/src/main/java/com/dispose/common/ErrorCode.java b/src/main/java/com/dispose/common/ErrorCode.java index c278949c..0a9e5dec 100644 --- a/src/main/java/com/dispose/common/ErrorCode.java +++ b/src/main/java/com/dispose/common/ErrorCode.java @@ -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, "没有该任务"), ; /** diff --git a/src/main/java/com/dispose/controller/DisposeNodeInfoController.java b/src/main/java/com/dispose/controller/DisposeNodeInfoController.java index 9f500e10..f4d57b64 100644 --- a/src/main/java/com/dispose/controller/DisposeNodeInfoController.java +++ b/src/main/java/com/dispose/controller/DisposeNodeInfoController.java @@ -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()); @@ -560,7 +562,7 @@ public class DisposeNodeInfoController { @ResponseBody @ApiOperation("获取正在进行的处置任务") public ProtocolRespDTO getRunningDisposeTaskList(@RequestBody ProtocolReqDTO mr, - @RequestHeader HttpHeaders headers) + @RequestHeader HttpHeaders headers) throws JsonProcessingException { ErrorCode err = mr.verifyRequest(headers); @@ -632,7 +634,7 @@ public class DisposeNodeInfoController { @ResponseBody @ApiOperation("获取所有处置任务") public ProtocolRespDTO getAllDisposeTaskList(@RequestBody ProtocolReqDTO mr, - @RequestHeader HttpHeaders headers) + @RequestHeader HttpHeaders headers) throws JsonProcessingException { ErrorCode err = mr.verifyRequest(headers); @@ -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; diff --git a/src/main/java/com/dispose/controller/DisposeTaskController.java b/src/main/java/com/dispose/controller/DisposeTaskController.java index 14643632..68a3f368 100644 --- a/src/main/java/com/dispose/controller/DisposeTaskController.java +++ b/src/main/java/com/dispose/controller/DisposeTaskController.java @@ -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 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 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 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 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()); + }); - rspInfo.getItems().add(taskData); + if (reqInfo.getTaskId().length != 0) { + List 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); diff --git a/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java b/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java index beb7f500..00a73a0f 100644 --- a/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java +++ b/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java @@ -1,73 +1,98 @@ 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 { - @Resource - private DisposeDeviceMapper disposeDeviceMapper; + /** + * The Dispose device mapper. + */ + @Resource + private DisposeDeviceMapper disposeDeviceMapper; - @Override - public int saveDisposeDevice(DisposeDevice dd) { - // TODO Auto-generated method stub - return disposeDeviceMapper.insert(dd); - } + /** + * 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); + } - @Override - public DisposeDevice findDisposeDeviceByIp(String ip) { - // TODO Auto-generated method stub - log.info("根据IP {} 查询处置设备信息", ip); - Example example = new Example(DisposeDevice.class); - Criteria c = example.createCriteria(); - c.andEqualTo("ipAddr", ip); - List list = disposeDeviceMapper.selectByExample(example); - if (CollectionUtil.isNotEmpty(list)) { - return list.get(0); - } - return null; - } + /** + * 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 + log.info("根据IP {} 查询处置设备信息", ip); + Example example = new Example(DisposeDevice.class); + Criteria c = example.createCriteria(); + c.andEqualTo("ipAddr", ip); + List list = disposeDeviceMapper.selectByExample(example); + if (CollectionUtil.isNotEmpty(list)) { + return list.get(0); + } + return null; + } - @Override - public DisposeDevice findDisposeDeviceById(Long id) { - // TODO Auto-generated method stub - return disposeDeviceMapper.selectByPrimaryKey(id); - } + /** + * 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); + } - @Override - public int delDisposeDeviceByIp(String ip) { - // TODO Auto-generated method stub - log.info("根据IP {} 删除处置设备信息", ip); - Example example = new Example(DisposeDevice.class); - Criteria c = example.createCriteria(); - c.andEqualTo("ipAddr", ip); - return disposeDeviceMapper.deleteByExample(example); - } + /** + * Del dispose device by ip int. + * + * @param ip the ip + * @return the int + */ + @Override + public int delDisposeDeviceByIp(String ip) { + // TODO Auto-generated method stub + log.info("根据IP {} 删除处置设备信息", ip); + Example example = new Example(DisposeDevice.class); + Criteria c = example.createCriteria(); + c.andEqualTo("ipAddr", ip); + return disposeDeviceMapper.deleteByExample(example); + } - @Override - public List findAllDisposeDevice() { - // TODO Auto-generated method stub - return disposeDeviceMapper.selectAll(); - } + /** + * Find all dispose device list. + * + * @return the list + */ + @Override + public List findAllDisposeDevice() { + // TODO Auto-generated method stub + return disposeDeviceMapper.selectAll(); + } } 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 5c9f0681..d8f715e6 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,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 { /** diff --git a/src/main/java/com/dispose/pojo/vo/task/GetTaskReq.java b/src/main/java/com/dispose/pojo/vo/task/GetTaskReq.java new file mode 100644 index 00000000..df74aca1 --- /dev/null +++ b/src/main/java/com/dispose/pojo/vo/task/GetTaskReq.java @@ -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; +} diff --git a/src/main/java/com/dispose/service/TaskService.java b/src/main/java/com/dispose/service/TaskService.java index 6efb1b31..caa28d5b 100644 --- a/src/main/java/com/dispose/service/TaskService.java +++ b/src/main/java/com/dispose/service/TaskService.java @@ -76,4 +76,11 @@ public interface TaskService { * @return the node all task */ List getNodeAllTask(Long devId); + + /** + * Gets all task. + * + * @return the all task + */ + List getAllTask(); } diff --git a/src/main/java/com/dispose/service/impl/TaskServiceImpl.java b/src/main/java/com/dispose/service/impl/TaskServiceImpl.java index 2fbd6d50..cf10a761 100644 --- a/src/main/java/com/dispose/service/impl/TaskServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/TaskServiceImpl.java @@ -202,6 +202,26 @@ public class TaskServiceImpl implements TaskService { */ @Override public List getNodeAllTask(Long devId) { + List 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 getAllTask() { return taskCacheManager.getAllTask(); } } diff --git a/src/test/java/com/dispose/controller/TaskControllerTest.java b/src/test/java/com/dispose/controller/TaskControllerTest.java index ca65b735..df3fec55 100644 --- a/src/test/java/com/dispose/controller/TaskControllerTest.java +++ b/src/test/java/com/dispose/controller/TaskControllerTest.java @@ -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 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 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();