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" ?> <?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true"> <configuration scan="true">
<!-- ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF-->
<property name="LOG_PATH" value="./logs"/> <property name="LOG_PATH" value="./logs"/>
<property name="LOG_LEVEL" value="debug"/> <property name="LOG_LEVEL" value="debug"/>

View File

@ -226,11 +226,11 @@ public enum DDoSAttackType implements BaseEnum {
if (type.equals(ALL_ATTACKS)) { if (type.equals(ALL_ATTACKS)) {
continue; continue;
} }
mask |= type.getValue() << 1; mask |= (long)1 << type.getValue();
} }
} else { } else {
for (DDoSAttackType type : types) { 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.Id;
import javax.persistence.Table; import javax.persistence.Table;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
/** /**
* The type Dispose task. * The type Dispose task.
@ -52,9 +53,9 @@ public class DisposeTask implements Serializable {
*/ */
private Long accountId; private Long accountId;
/** /**
* The Capacity type. * The Dispose capacity.
*/ */
private DisposeCapacityType capacityType; private DisposeCapacityType disposeCapacity;
/** /**
* The Dispose ip. * The Dispose ip.
*/ */
@ -87,4 +88,9 @@ public class DisposeTask implements Serializable {
* The Current status. * The Current status.
*/ */
private DisposeTaskStatus currentStatus; 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} WHERE dispose_capacity.deviceId = #{deviceId}
</delete> </delete>
<select id="getDeviceDisposeCapacity" resultType="com.dispose.pojo.entity.DisposeCapacity"> <select id="getDeviceDisposeCapacity" resultMap="dispose_capacity">
SELECT * SELECT *
FROM dispose_capacity FROM dispose_capacity
WHERE dispose_capacity.deviceId = #{deviceId} WHERE dispose_capacity.deviceId = #{deviceId}

View File

@ -5,7 +5,7 @@
<id column="id" property="id"/> <id column="id" property="id"/>
<id column = "deviceId" property="deviceId"/> <id column = "deviceId" property="deviceId"/>
<id column = "accountId" property="accountId"/> <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="disposeIp" property="disposeIp"/>
<result column="createTime" property="createTime"/> <result column="createTime" property="createTime"/>
<result column="planEndTime" property="planEndTime"/> <result column="planEndTime" property="planEndTime"/>
@ -14,6 +14,17 @@
<result column="attackType" property="attackType"/> <result column="attackType" property="attackType"/>
<result column="flowBandWidth" property="flowBandWidth"/> <result column="flowBandWidth" property="flowBandWidth"/>
<result column="currentStatus" property="currentStatus" javaType="com.dispose.common.DisposeTaskStatus"/> <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> </resultMap>
<select id="selectAll" resultMap="dispose_task"> <select id="selectAll" resultMap="dispose_task">
@ -23,21 +34,21 @@
<insert id="addNewTask" useGeneratedKeys="true" keyProperty="id" <insert id="addNewTask" useGeneratedKeys="true" keyProperty="id"
parameterType="com.dispose.pojo.entity.DisposeTask"> 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, planEndTime, flowDirection, attackType, flowBandWidth,
currentStatus) currentStatus)
VALUES (#{deviceId}, #{accountId}, #{capacityType}, #{disposeIp}, VALUES (#{deviceId}, #{accountId}, #{disposeCapacity}, #{disposeIp},
date_add(now(), interval #{planEndTime} MINUTE), #{flowDirection}, #{attackType}, 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> </insert>
<update id="changTaskStatus"> <update id="changTaskStatus">
UPDATE UPDATE
dispose_task dispose_task
SET SET
currentStatus = #{status, javaType="com.dispose.common.DisposeTaskStatus"} currentStatus = #{status}
<if test="status == @com.dispose.common.DisposeTaskStatus@TASK_FINISHED <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 , endTime = CURRENT_TIMESTAMP
</if> </if>
WHERE 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")); // Assert.assertTrue(IPAddrType.ipInRange("", "192.168.0.30"));
// } // }
/**
* Date time debug.
*/
@Test @Test
public void dateTimeDebug() { public void dateTimeDebug() {
Integer v1 = null; Integer v1 = null;
@ -76,6 +79,14 @@ public class demo {
log.info("v2: {}", Optional.ofNullable(v2).orElse(0)); 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) { private <T> T upgradeDisposeDeviceProperties(T destDev, T srcDev) {
Field[] field = srcDev.getClass().getDeclaredFields(); Field[] field = srcDev.getClass().getDeclaredFields();
@ -104,6 +115,11 @@ public class demo {
return destDev; return destDev;
} }
/**
* Class enum value.
*
* @throws JsonProcessingException the json processing exception
*/
@Test @Test
public void classEnumValue() throws JsonProcessingException { public void classEnumValue() throws JsonProcessingException {
DisposeDevice dev = DisposeDevice.builder() DisposeDevice dev = DisposeDevice.builder()
@ -132,6 +148,9 @@ public class demo {
.writeValueAsString(upgradeDisposeDeviceProperties(dev, dev2))); .writeValueAsString(upgradeDisposeDeviceProperties(dev, dev2)));
} }
/**
* Privacy helper.
*/
@Test @Test
public void privacyHelper() { public void privacyHelper() {
log.info(PrivacyHelper.ipAddressPrivacy("192.168.0.123")); log.info(PrivacyHelper.ipAddressPrivacy("192.168.0.123"));
@ -139,6 +158,9 @@ public class demo {
log.info(PrivacyHelper.ipAddressPrivacy("3fde::fde2")); log.info(PrivacyHelper.ipAddressPrivacy("3fde::fde2"));
} }
/**
* Gets type mask from attack type.
*/
@Test @Test
public void getTypeMaskFromAttackType() { public void getTypeMaskFromAttackType() {
Long ret = DDoSAttackType.getTypeMaskFromAttackType(new DDoSAttackType[] {DDoSAttackType.ALL_ATTACKS}); 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.DDoSAttackType;
import com.dispose.common.DisposeCapacityType; import com.dispose.common.DisposeCapacityType;
import com.dispose.common.DisposeTaskStatus;
import com.dispose.common.NetflowDirection; import com.dispose.common.NetflowDirection;
import com.dispose.mapper.DisposeDeviceMapper; import com.dispose.mapper.DisposeDeviceMapper;
import com.dispose.mapper.DisposeTaskMapper; 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.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -20,23 +22,45 @@ import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
/**
* The type Dispose task mapper test.
*
* @author <huangxin@cmhi.chinamoblie.com>
*/
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@Slf4j @Slf4j
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DisposeTaskMapperTest { public class DisposeTaskMapperTest {
/**
* The Dispose task mapper.
*/
@Resource @Resource
private DisposeTaskMapper disposeTaskMapper; private DisposeTaskMapper disposeTaskMapper;
/**
* The User account mapper.
*/
@Resource @Resource
private UserAccountMapper userAccountMapper; private UserAccountMapper userAccountMapper;
/**
* The Dispose device mapper.
*/
@Resource @Resource
private DisposeDeviceMapper disposeDeviceMapper; private DisposeDeviceMapper disposeDeviceMapper;
/**
* The Object mapper.
*/
@Resource @Resource
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
/**
* A 1 add new dispose task.
*
* @throws JsonProcessingException the json processing exception
*/
@Test @Test
public void a1_addNewDisposeTask() throws JsonProcessingException { public void a1_addNewDisposeTask() throws JsonProcessingException {
Long uId = userAccountMapper.selectAll().get(0).getId(); Long uId = userAccountMapper.selectAll().get(0).getId();
@ -44,22 +68,57 @@ public class DisposeTaskMapperTest {
DisposeTask task = DisposeTask.builder() DisposeTask task = DisposeTask.builder()
.deviceId(dId) .deviceId(dId)
.accountId(uId) .accountId(uId)
.capacityType(DisposeCapacityType.CLEANUP) .disposeCapacity(DisposeCapacityType.CLEANUP)
.disposeIp("192.168.0.1") .disposeIp("192.168.0.1")
.planEndTime("30") .planEndTime("30")
.flowDirection(NetflowDirection.DIRECTION_BI) .flowDirection(NetflowDirection.DIRECTION_BI)
.attackType(DDoSAttackType.getTypeMaskFromAttackType(new DDoSAttackType[] {DDoSAttackType.ALL_ATTACKS})) .attackType(DDoSAttackType.getTypeMaskFromAttackType(new DDoSAttackType[]{DDoSAttackType.ALL_ATTACKS}))
.flowBandWidth(1024) .flowBandWidth(1024)
.build(); .build();
int ret = disposeTaskMapper.addNewTask(task); int ret = disposeTaskMapper.addNewTask(task);
log.info("add ret {}: {}", ret, Assert.assertEquals(ret, 1);
objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(task));
log.debug("add ret {}: {}", ret, objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(task));
List<DisposeTask> taskList = disposeTaskMapper.selectAll(); 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);
});
}
} }
} }