REM:
1. 增加处置设备处置任务数据库相关内容
This commit is contained in:
HuangXin 2020-08-13 16:28:50 +08:00
parent 12ba2b9be3
commit c6d3b3f0fe
10 changed files with 280 additions and 16 deletions

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true">
<!-- ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF-->
<property name="LOG_PATH" value="./logs"/>
<property name="LOG_LEVEL" value="debug"/>

View File

@ -226,11 +226,11 @@ public enum DDoSAttackType implements BaseEnum {
if (type.equals(ALL_ATTACKS)) {
continue;
}
mask |= type.getValue() << 1;
mask |= (long)1 << type.getValue();
}
} else {
for (DDoSAttackType type : types) {
mask |= type.getValue() << 1;
mask |= (long)1 << type.getValue();
}
}

View File

@ -0,0 +1,44 @@
package com.dispose.mapper;
import com.dispose.pojo.entity.TaskInfo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* The interface Task info mapper.
*
* @author <huangxin@cmhi.chinamoblie.com>
*/
public interface TaskInfoMapper {
/**
* Select all list.
*
* @return the list
*/
List<TaskInfo> selectAll();
/**
* Gets task info by task id.
*
* @param taskId the task id
* @return the task info by task id
*/
List<TaskInfo> getTaskInfoByTaskId(@Param("taskId") Long taskId);
/**
* Add new task info list int.
*
* @param taskInfos the task infos
* @return the int
*/
int addNewTaskInfoList(@Param("taskInfos") List<TaskInfo> taskInfos);
/**
* Add new task info int.
*
* @param taskInfo the task info
* @return the int
*/
int addNewTaskInfo(@Param("taskInfo") TaskInfo taskInfo);
}

View File

@ -16,6 +16,7 @@ import tk.mybatis.mapper.code.Style;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.List;
/**
* The type Dispose task.
@ -52,9 +53,9 @@ public class DisposeTask implements Serializable {
*/
private Long accountId;
/**
* The Capacity type.
* The Dispose capacity.
*/
private DisposeCapacityType capacityType;
private DisposeCapacityType disposeCapacity;
/**
* The Dispose ip.
*/
@ -87,4 +88,9 @@ public class DisposeTask implements Serializable {
* The Current status.
*/
private DisposeTaskStatus currentStatus;
/**
* The Task info.
*/
private List<TaskInfo> taskInfo;
}

View File

@ -0,0 +1,79 @@
package com.dispose.pojo.entity;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import tk.mybatis.mapper.annotation.KeySql;
import tk.mybatis.mapper.annotation.NameStyle;
import tk.mybatis.mapper.code.Style;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
/**
* The type Task info.
*
* @author <huangxin@cmhi.chinamoblie.com>
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder(alphabetic = true)
@Table(name = "task_info")
@NameStyle(Style.normal)
public class TaskInfo implements Serializable {
/**
* The constant serialVersionUID.
*/
private static final long serialVersionUID = 1L;
/**
* The Id.
*/
@Id
@KeySql(useGeneratedKeys = true)
private Long id;
/**
* The Task id.
*/
private Long taskId;
/**
* The Device id.
*/
private Long deviceId;
/**
* The Begin time.
*/
private String beginTime;
/**
* The End time.
*/
private String endTime;
/**
* The Task attack type.
*/
private Long taskAttackType;
/**
* The Exec attack type.
*/
private Long execAttackType;
/**
* The Attack type status.
*/
private Long attackTypeStatus;
/**
* The Extern id.
*/
private Long externId;
/**
* The Status.
*/
private Long status;
}

View File

@ -37,7 +37,7 @@
WHERE dispose_capacity.deviceId = #{deviceId}
</delete>
<select id="getDeviceDisposeCapacity" resultType="com.dispose.pojo.entity.DisposeCapacity">
<select id="getDeviceDisposeCapacity" resultMap="dispose_capacity">
SELECT *
FROM dispose_capacity
WHERE dispose_capacity.deviceId = #{deviceId}

View File

@ -5,7 +5,7 @@
<id column="id" property="id"/>
<id column = "deviceId" property="deviceId"/>
<id column = "accountId" property="accountId"/>
<result column="capacityType" property="capacityType" javaType="com.dispose.common.DisposeCapacityType"/>
<result column="disposeCapacity" property="disposeCapacity" javaType="com.dispose.common.DisposeCapacityType"/>
<result column="disposeIp" property="disposeIp"/>
<result column="createTime" property="createTime"/>
<result column="planEndTime" property="planEndTime"/>
@ -14,6 +14,17 @@
<result column="attackType" property="attackType"/>
<result column="flowBandWidth" property="flowBandWidth"/>
<result column="currentStatus" property="currentStatus" javaType="com.dispose.common.DisposeTaskStatus"/>
<collection property="taskInfo" ofType="com.dispose.pojo.entity.TaskInfo">
<id column="t_id" property="id"/>
<result column="taskId" property="taskId"/>
<result column="deviceId" property="deviceId"/>
<result column="beginTime" property="beginTime"/>
<result column="endTime" property="endTime"/>
<result column="taskAttackType" property="execAttackType"/>
<result column="attackTypeStatus" property="attackTypeStatus"/>
<result column="externId" property="externId"/>
<result column="status" property="status"/>
</collection>
</resultMap>
<select id="selectAll" resultMap="dispose_task">
@ -23,21 +34,21 @@
<insert id="addNewTask" useGeneratedKeys="true" keyProperty="id"
parameterType="com.dispose.pojo.entity.DisposeTask">
INSERT IGNORE INTO dispose_task(deviceId, accountId, capacityType, disposeIp,
INSERT IGNORE INTO dispose_task(deviceId, accountId, disposeCapacity, disposeIp,
planEndTime, flowDirection, attackType, flowBandWidth,
currentStatus)
VALUES (#{deviceId}, #{accountId}, #{capacityType}, #{disposeIp},
VALUES (#{deviceId}, #{accountId}, #{disposeCapacity}, #{disposeIp},
date_add(now(), interval #{planEndTime} MINUTE), #{flowDirection}, #{attackType},
#{flowBandWidth}, ${@com.dispose.common.DisposeTaskStatus@TASK_FINISHED.getValue()})
#{flowBandWidth}, ${@com.dispose.common.DisposeTaskStatus@TASK_NEW.getValue()})
</insert>
<update id="changTaskStatus">
UPDATE
dispose_task
SET
currentStatus = #{status, javaType="com.dispose.common.DisposeTaskStatus"}
currentStatus = #{status}
<if test="status == @com.dispose.common.DisposeTaskStatus@TASK_FINISHED
and status == @com.dispose.common.DisposeTaskStatus@TASK_CANCELED">
or status == @com.dispose.common.DisposeTaskStatus@TASK_CANCELED">
, endTime = CURRENT_TIMESTAMP
</if>
WHERE

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dispose.mapper.TaskInfoMapper">
<resultMap id="dispose_task" type="com.dispose.pojo.entity.TaskInfo">
<id column="id" property="id"/>
<result column="taskId" property="taskId"/>
<result column="deviceId" property="deviceId"/>
<result column="beginTime" property="beginTime"/>
<result column="endTime" property="endTime"/>
<result column="taskAttackType" property="execAttackType"/>
<result column="attackTypeStatus" property="attackTypeStatus"/>
<result column="externId" property="externId"/>
<result column="status" property="status"/>
</resultMap>
<select id="selectAll" resultMap="dispose_task">
SELECT *
FROM task_info
</select>
<insert id="addNewTaskInfoList" useGeneratedKeys="true" keyProperty="id"
parameterType="com.dispose.pojo.entity.TaskInfo">
INSERT IGNORE INTO task_info(taskId, deviceId, taskAttackType)
VALUES
<foreach collection="taskInfos" item="task" separator=",">
(#{task.taskId}, #{task.deviceId}, #{task.taskAttackType})
</foreach>
</insert>
<insert id="addNewTaskInfo" useGeneratedKeys="true" keyProperty="id"
parameterType="com.dispose.pojo.entity.TaskInfo">
INSERT IGNORE INTO task_info(taskId, deviceId, taskAttackType)
VALUES (#{taskId}, #{deviceId}, #{taskAttackType})
</insert>
<select id="getTaskInfoByTaskId" resultMap="dispose_task">
SELECT *
FROM task_info
WHERE taskId = #{taskId, jdbcType=INTEGER}
</select>
</mapper>

View File

@ -63,6 +63,9 @@ public class demo {
// Assert.assertTrue(IPAddrType.ipInRange("", "192.168.0.30"));
// }
/**
* Date time debug.
*/
@Test
public void dateTimeDebug() {
Integer v1 = null;
@ -76,6 +79,14 @@ public class demo {
log.info("v2: {}", Optional.ofNullable(v2).orElse(0));
}
/**
* Upgrade dispose device properties t.
*
* @param <T> the type parameter
* @param destDev the dest dev
* @param srcDev the src dev
* @return the t
*/
private <T> T upgradeDisposeDeviceProperties(T destDev, T srcDev) {
Field[] field = srcDev.getClass().getDeclaredFields();
@ -104,6 +115,11 @@ public class demo {
return destDev;
}
/**
* Class enum value.
*
* @throws JsonProcessingException the json processing exception
*/
@Test
public void classEnumValue() throws JsonProcessingException {
DisposeDevice dev = DisposeDevice.builder()
@ -132,6 +148,9 @@ public class demo {
.writeValueAsString(upgradeDisposeDeviceProperties(dev, dev2)));
}
/**
* Privacy helper.
*/
@Test
public void privacyHelper() {
log.info(PrivacyHelper.ipAddressPrivacy("192.168.0.123"));
@ -139,6 +158,9 @@ public class demo {
log.info(PrivacyHelper.ipAddressPrivacy("3fde::fde2"));
}
/**
* Gets type mask from attack type.
*/
@Test
public void getTypeMaskFromAttackType() {
Long ret = DDoSAttackType.getTypeMaskFromAttackType(new DDoSAttackType[] {DDoSAttackType.ALL_ATTACKS});

View File

@ -2,6 +2,7 @@ package com.dispose.test.mapper;
import com.dispose.common.DDoSAttackType;
import com.dispose.common.DisposeCapacityType;
import com.dispose.common.DisposeTaskStatus;
import com.dispose.common.NetflowDirection;
import com.dispose.mapper.DisposeDeviceMapper;
import com.dispose.mapper.DisposeTaskMapper;
@ -10,6 +11,7 @@ import com.dispose.pojo.entity.DisposeTask;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -20,23 +22,45 @@ import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
/**
* The type Dispose task mapper test.
*
* @author <huangxin@cmhi.chinamoblie.com>
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DisposeTaskMapperTest {
/**
* The Dispose task mapper.
*/
@Resource
private DisposeTaskMapper disposeTaskMapper;
/**
* The User account mapper.
*/
@Resource
private UserAccountMapper userAccountMapper;
/**
* The Dispose device mapper.
*/
@Resource
private DisposeDeviceMapper disposeDeviceMapper;
/**
* The Object mapper.
*/
@Resource
private ObjectMapper objectMapper;
/**
* A 1 add new dispose task.
*
* @throws JsonProcessingException the json processing exception
*/
@Test
public void a1_addNewDisposeTask() throws JsonProcessingException {
Long uId = userAccountMapper.selectAll().get(0).getId();
@ -44,7 +68,7 @@ public class DisposeTaskMapperTest {
DisposeTask task = DisposeTask.builder()
.deviceId(dId)
.accountId(uId)
.capacityType(DisposeCapacityType.CLEANUP)
.disposeCapacity(DisposeCapacityType.CLEANUP)
.disposeIp("192.168.0.1")
.planEndTime("30")
.flowDirection(NetflowDirection.DIRECTION_BI)
@ -54,12 +78,47 @@ public class DisposeTaskMapperTest {
int ret = disposeTaskMapper.addNewTask(task);
log.info("add ret {}: {}", ret,
objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(task));
Assert.assertEquals(ret, 1);
log.debug("add ret {}: {}", ret, objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(task));
List<DisposeTask> taskList = disposeTaskMapper.selectAll();
log.info("Database: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskList));
Assert.assertNotNull(taskList);
Assert.assertNotEquals(taskList.size(), 0);
log.debug("Database: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskList));
}
/**
* D 1 get task status.
*/
@Test
public void d1_getTaskStatus() {
List<DisposeTask> taskList = disposeTaskMapper.selectAll();
if (taskList != null && taskList.size() > 0) {
taskList.forEach(v -> {
DisposeTaskStatus status = disposeTaskMapper.getTaskStatus(v.getId());
Assert.assertEquals(v.getCurrentStatus(), status);
});
}
}
/**
* C 1 chang task status.
*/
@Test
public void c1_changTaskStatus() {
List<DisposeTask> taskList = disposeTaskMapper.selectAll();
if (taskList != null && taskList.size() > 0) {
taskList.forEach(v -> {
int ret = disposeTaskMapper.changTaskStatus(v.getId(), DisposeTaskStatus.TASK_FINISHED);
DisposeTaskStatus status = disposeTaskMapper.getTaskStatus(v.getId());
Assert.assertEquals(DisposeTaskStatus.TASK_FINISHED, status);
});
}
}
}