OCT
REM: 1. 增加启动时自动加载所有处置设备功能 2. 增加调试相关配置项开关功能 3. 增加处置节点信息获取接口 4. 增加节点管理权限验证功能 5. 增加节点信息自动更新功能 6. 增加对应接口,功能单元测试用例
This commit is contained in:
parent
e7843898b7
commit
edff379c64
|
@ -5,7 +5,7 @@ server.tomcat.basedir=./basedir
|
|||
server.servlet.context-path=/dispose
|
||||
|
||||
# 配置数据源
|
||||
spring.datasource.url=jdbc:mysql://172.28.72.118:33061/dispose?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
|
||||
spring.datasource.url=jdbc:mysql://172.28.72.118:33061/dispose_v2?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
spring.datasource.username=phoenix
|
||||
spring.datasource.password=Hy@rfph32
|
||||
|
@ -33,6 +33,9 @@ logging.config=file:config/logback.xml
|
|||
#config tomcat
|
||||
server.tomcat.max-threads=128
|
||||
|
||||
#Json sort
|
||||
spring.jackson.mapper.sort-properties-alphabetically=true
|
||||
|
||||
#====custom config,begin with phoenix====
|
||||
#test
|
||||
phoenix.system-name=phoenix framework
|
||||
|
@ -60,3 +63,8 @@ phoenix.threat-info-key=7d79e180a6a20d4c0b5c81d146c5b85d1715d353b50ec094f966ec52
|
|||
phoenix.threat-info-version-url=https://tip.komect.com:9099/api/v1/search/cases/reputation
|
||||
phoenix.threat-info-download=https://tip.komect.com:9099/api/v1/resource/data/download
|
||||
#====custom config,begin with phoenix====
|
||||
|
||||
#调试配置
|
||||
dispose.check-protocol-timeout=false
|
||||
dispose.check-request-token=false
|
||||
dispose.check-admin-permission=false
|
||||
|
|
|
@ -28,6 +28,7 @@ public enum ErrorCode {
|
|||
ERR_PARAMEXCEPTION (21, "参数异常"),
|
||||
ERR_VERSION (23, "协议版本不兼容,请升级系统"),
|
||||
ERR_NOSUCHTYPE (24, "没有这个类型的处置设备"),
|
||||
ERR_REMOVEMORE (25, "禁止同时删除多个设备"),
|
||||
;
|
||||
|
||||
private int errno;
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package com.dispose.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "dispose")
|
||||
public class DisposeConfigure {
|
||||
private String checkProtocolTimeout;
|
||||
private String checkRequestToken;
|
||||
private String checkAdminPermission;
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package com.dispose.config;
|
||||
|
||||
import com.dispose.service.DisposeNodeManager;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
@ -9,16 +10,16 @@ import javax.annotation.Resource;
|
|||
@Component
|
||||
@Slf4j
|
||||
public class SetupInit implements CommandLineRunner {
|
||||
//@Resource
|
||||
//private DisposeDeviceManager disposeDevMgr;
|
||||
@Resource
|
||||
private DisposeNodeManager disposeNodeManager;
|
||||
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
public void run(String... args) {
|
||||
// TODO Auto-generated method stub
|
||||
log.info("System Setup................................................");
|
||||
//fileLoadService.mailFileRefresh();
|
||||
|
||||
//disposeDevMgr.loadDisposeFromDB();
|
||||
disposeNodeManager.loadDisposeNodeFromDB();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -83,8 +83,7 @@ public class AuthController {
|
|||
|
||||
LoginReq reqInfo = mr.getRequestObject(LoginReq.class);
|
||||
|
||||
err = userAccountService.logoutService(reqInfo.getUserName(),
|
||||
mr.getAuthToken(headers));
|
||||
err = userAccountService.logoutService(reqInfo.getUserName(), mr.getAuthToken());
|
||||
|
||||
if(err != ErrorCode.ERR_OK) {
|
||||
return ProtocolRespDTO.result(err);
|
||||
|
|
|
@ -6,20 +6,20 @@ import com.dispose.mapper.DisposeDeviceMapper;
|
|||
import com.dispose.pojo.dto.ProtocolReqDTO;
|
||||
import com.dispose.pojo.dto.ProtocolRespDTO;
|
||||
import com.dispose.pojo.entity.DisposeDevice;
|
||||
import com.dispose.pojo.po.MReturnType;
|
||||
import com.dispose.pojo.vo.common.IDArrayReq;
|
||||
import com.dispose.pojo.vo.information.LinkStatusRsp;
|
||||
import com.dispose.pojo.vo.information.*;
|
||||
import com.dispose.service.DisposeNodeManager;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.Builder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
|
@ -35,15 +35,19 @@ public class DisposeNodeInfoController {
|
|||
private ObjectMapper objectMapper;
|
||||
|
||||
@Resource
|
||||
private DisposeDeviceMapper disposeDeviceMapper;
|
||||
private DisposeNodeManager disposeNodeManager;
|
||||
|
||||
//@Resource
|
||||
//private DisposeDeviceMapper disposeDeviceMapper;
|
||||
|
||||
@PostMapping("/linkstatus")
|
||||
@ResponseBody
|
||||
@ApiOperation("链接状态")
|
||||
@Builder
|
||||
public ProtocolRespDTO getLinkStatus(@RequestBody ProtocolReqDTO mr)
|
||||
public ProtocolRespDTO getLinkStatus(@RequestBody ProtocolReqDTO mr,
|
||||
@RequestHeader HttpHeaders headers)
|
||||
throws JsonProcessingException {
|
||||
ErrorCode err = mr.verifyRequest();
|
||||
ErrorCode err = mr.verifyRequest(headers);
|
||||
|
||||
if(err != ErrorCode.ERR_OK) {
|
||||
return ProtocolRespDTO.result(err);
|
||||
|
@ -53,7 +57,7 @@ public class DisposeNodeInfoController {
|
|||
List<LinkStatusRsp> rspInfo = new ArrayList<>();
|
||||
|
||||
if(reqInfo.getId().length == 0) {
|
||||
List<DisposeDevice> devs = disposeDeviceMapper.selectAll();
|
||||
List<DisposeDevice> devs = disposeNodeManager.getAllDisposeDevice();
|
||||
|
||||
if(devs != null && devs.size() > 0) {
|
||||
devs.forEach(v->{
|
||||
|
@ -67,7 +71,7 @@ public class DisposeNodeInfoController {
|
|||
}
|
||||
} else {
|
||||
for (String v : reqInfo.getId()) {
|
||||
DisposeDevice dev = disposeDeviceMapper.getDeviceById(Long.valueOf(v));
|
||||
DisposeDevice dev = disposeNodeManager.getDisposeDeviceById(Long.valueOf(v));
|
||||
LinkStatusRsp linkStat;
|
||||
if(dev != null && dev.getId().equals(Long.valueOf(v))) {
|
||||
linkStat = LinkStatusRsp.builder().online(dev.getLinkStatus()).build();
|
||||
|
@ -88,4 +92,179 @@ public class DisposeNodeInfoController {
|
|||
return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo);
|
||||
}
|
||||
|
||||
@PostMapping("/version")
|
||||
@ResponseBody
|
||||
@ApiOperation("处置设备版本")
|
||||
@Builder
|
||||
public ProtocolRespDTO getVersion(@RequestBody ProtocolReqDTO mr,
|
||||
@RequestHeader HttpHeaders headers)
|
||||
throws JsonProcessingException {
|
||||
ErrorCode err = mr.verifyRequest(headers);
|
||||
|
||||
if(err != ErrorCode.ERR_OK) {
|
||||
return ProtocolRespDTO.result(err);
|
||||
}
|
||||
|
||||
IDArrayReq reqInfo = mr.getRequestObject(IDArrayReq.class);
|
||||
List<VersionRsp> rspInfo = new ArrayList<>();
|
||||
|
||||
if(reqInfo.getId().length == 0) {
|
||||
List<DisposeDevice> devList = disposeNodeManager.getAllDisposeDevice();
|
||||
|
||||
if(devList != null && devList.size() > 0) {
|
||||
devList.forEach(v->{
|
||||
VersionRsp ver = VersionRsp.builder()
|
||||
.version(v.getVersion()).build();
|
||||
|
||||
ver.setStatus(ErrorCode.ERR_OK.getCode());
|
||||
ver.setMessage(ErrorCode.ERR_OK.getMsg());
|
||||
|
||||
rspInfo.add(ver);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
for (String v : reqInfo.getId()) {
|
||||
DisposeDevice dev = disposeNodeManager.getDisposeDeviceById(Long.valueOf(v));
|
||||
VersionRsp ver;
|
||||
if(dev != null && dev.getId().equals(Long.valueOf(v))) {
|
||||
ver = VersionRsp.builder()
|
||||
.version(dev.getVersion()).build();
|
||||
ver.setId(dev.getId().toString());
|
||||
err = ErrorCode.ERR_OK;
|
||||
} else {
|
||||
ver = VersionRsp.builder().build();
|
||||
ver.setId(v);
|
||||
err = ErrorCode.ERR_NOSUCHDEVICE;
|
||||
}
|
||||
|
||||
ver.setStatus(err.getCode());
|
||||
ver.setMessage(err.getMsg());
|
||||
|
||||
rspInfo.add(ver);
|
||||
}
|
||||
}
|
||||
|
||||
return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo);
|
||||
}
|
||||
|
||||
@PostMapping("/deviceinfo")
|
||||
@ResponseBody
|
||||
@ApiOperation("处置设备信息")
|
||||
@Builder
|
||||
public ProtocolRespDTO getDeviceInfo(@RequestBody ProtocolReqDTO mr,
|
||||
@RequestHeader HttpHeaders headers)
|
||||
throws JsonProcessingException {
|
||||
ErrorCode err = mr.verifyRequest(headers);
|
||||
|
||||
if(err != ErrorCode.ERR_OK) {
|
||||
return ProtocolRespDTO.result(err);
|
||||
}
|
||||
|
||||
IDArrayReq reqInfo = mr.getRequestObject(IDArrayReq.class);
|
||||
DeviceInfoRsp rspInfo = new DeviceInfoRsp();
|
||||
|
||||
rspInfo.setItems(new ArrayList<>());
|
||||
|
||||
if(reqInfo.getId().length == 0) {
|
||||
List<DisposeDevice> devList = disposeNodeManager.getAllDisposeDevice();
|
||||
|
||||
if(devList != null && devList.size() > 0) {
|
||||
devList.forEach(v->{
|
||||
rspInfo.getItems().add(new DeviceInfoData(
|
||||
v.getId().toString(),
|
||||
v.getDevInfo().getVendor(),
|
||||
v.getDevInfo().getModel(),
|
||||
v.getDevInfo().getFirmware(),
|
||||
v.getDevInfo().getOs(),
|
||||
v.getDevInfo().getKernel(),
|
||||
v.getDevInfo().getArch(),
|
||||
v.getDevInfo().getMemory(),
|
||||
v.getDevInfo().getFreeMemory(),
|
||||
v.getDevInfo().getCpuUsed(),
|
||||
ErrorCode.ERR_OK
|
||||
));
|
||||
});
|
||||
}
|
||||
} else {
|
||||
for (String v : reqInfo.getId()) {
|
||||
DisposeDevice dev = disposeNodeManager.getDisposeDeviceById(Long.valueOf(v));
|
||||
if(dev != null
|
||||
&& dev.getId().equals(Long.valueOf(v))
|
||||
&& dev.getDevInfo() != null) {
|
||||
rspInfo.getItems().add(new DeviceInfoData(
|
||||
dev.getId().toString(),
|
||||
dev.getDevInfo().getVendor(),
|
||||
dev.getDevInfo().getModel(),
|
||||
dev.getDevInfo().getFirmware(),
|
||||
dev.getDevInfo().getOs(),
|
||||
dev.getDevInfo().getKernel(),
|
||||
dev.getDevInfo().getArch(),
|
||||
dev.getDevInfo().getMemory(),
|
||||
dev.getDevInfo().getFreeMemory(),
|
||||
dev.getDevInfo().getCpuUsed(),
|
||||
ErrorCode.ERR_OK
|
||||
));
|
||||
} else {
|
||||
rspInfo.getItems().add(new DeviceInfoData(
|
||||
v,
|
||||
ErrorCode.ERR_NOSUCHDEVICE
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo);
|
||||
}
|
||||
|
||||
@PostMapping("/capacity")
|
||||
@ResponseBody
|
||||
@ApiOperation("处置设备处置能力")
|
||||
@Builder
|
||||
public ProtocolRespDTO getCapacity(@RequestBody ProtocolReqDTO mr,
|
||||
@RequestHeader HttpHeaders headers)
|
||||
throws JsonProcessingException {
|
||||
ErrorCode err = mr.verifyRequest(headers);
|
||||
|
||||
if(err != ErrorCode.ERR_OK) {
|
||||
return ProtocolRespDTO.result(err);
|
||||
}
|
||||
|
||||
IDArrayReq reqInfo = mr.getRequestObject(IDArrayReq.class);
|
||||
|
||||
DeviceCapacityRsp rspInfo = new DeviceCapacityRsp();
|
||||
rspInfo.setItems(new ArrayList<>());
|
||||
|
||||
if(reqInfo.getId().length == 0) {
|
||||
List<DisposeDevice> devList = disposeNodeManager.getAllDisposeDevice();
|
||||
|
||||
if(devList != null && devList.size() > 0) {
|
||||
|
||||
devList.forEach(v->{
|
||||
DeviceCapacityData cpData = new DeviceCapacityData(v.getDevCaps());
|
||||
cpData.setId(v.getId().toString());
|
||||
cpData.setStatus(ErrorCode.ERR_OK.getCode());
|
||||
cpData.setMessage(ErrorCode.ERR_OK.getMsg());
|
||||
rspInfo.getItems().add(cpData);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
for (String v : reqInfo.getId()) {
|
||||
DisposeDevice dev = disposeNodeManager.getDisposeDeviceById(Long.valueOf(v));
|
||||
if(dev != null && dev.getId().equals(Long.valueOf(v))) {
|
||||
DeviceCapacityData cpData = new DeviceCapacityData(dev.getDevCaps());
|
||||
cpData.setId(dev.getId().toString());
|
||||
cpData.setStatus(ErrorCode.ERR_OK.getCode());
|
||||
cpData.setMessage(ErrorCode.ERR_OK.getMsg());
|
||||
rspInfo.getItems().add(cpData);
|
||||
} else {
|
||||
DeviceCapacityData cpData = new DeviceCapacityData();
|
||||
cpData.setId(v);
|
||||
cpData.setStatus(ErrorCode.ERR_NOSUCHDEVICE.getCode());
|
||||
cpData.setMessage(ErrorCode.ERR_NOSUCHDEVICE.getMsg());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package com.dispose.controller;
|
||||
|
||||
import com.dispose.common.ErrorCode;
|
||||
import com.dispose.manager.UserAccountCacheManager;
|
||||
import com.dispose.pojo.dto.ProtocolReqDTO;
|
||||
import com.dispose.pojo.dto.ProtocolRespDTO;
|
||||
import com.dispose.pojo.entity.DisposeDevice;
|
||||
import com.dispose.pojo.po.MReturnType;
|
||||
import com.dispose.pojo.vo.common.IDArrayReq;
|
||||
import com.dispose.pojo.vo.common.IDReturnStatus;
|
||||
import com.dispose.pojo.vo.device.AddNodeReq;
|
||||
import com.dispose.pojo.vo.device.AddNodeRetData;
|
||||
|
@ -15,11 +17,14 @@ import io.swagger.annotations.Api;
|
|||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.Builder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
@RequestMapping(value = "/manager")
|
||||
|
@ -30,14 +35,24 @@ public class DisposeNodeManagerController {
|
|||
@Resource
|
||||
private DisposeNodeManager disposeNodeManager;
|
||||
|
||||
@Resource
|
||||
private UserAccountCacheManager userAccountCacheManager;
|
||||
|
||||
@PutMapping("/device")
|
||||
@ResponseBody
|
||||
@ApiOperation("添加处置能力节点")
|
||||
@Builder
|
||||
public ProtocolRespDTO addDisposeNode(@RequestBody ProtocolReqDTO mr)
|
||||
public ProtocolRespDTO addDisposeNode(@RequestBody ProtocolReqDTO mr,
|
||||
@RequestHeader HttpHeaders headers)
|
||||
throws JsonProcessingException {
|
||||
|
||||
ErrorCode err = mr.verifyRequest();
|
||||
ErrorCode err = mr.verifyRequest(headers);
|
||||
|
||||
if(err != ErrorCode.ERR_OK) {
|
||||
return ProtocolRespDTO.result(err);
|
||||
}
|
||||
|
||||
err = userAccountCacheManager.verifyPermission(mr.getAuthToken());
|
||||
|
||||
if(err != ErrorCode.ERR_OK) {
|
||||
return ProtocolRespDTO.result(err);
|
||||
|
@ -75,4 +90,43 @@ public class DisposeNodeManagerController {
|
|||
|
||||
return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo);
|
||||
}
|
||||
|
||||
@DeleteMapping("/device")
|
||||
@ResponseBody
|
||||
@ApiOperation("删除处置能力节点")
|
||||
@Builder
|
||||
public ProtocolRespDTO removeDisposeNode(@RequestBody ProtocolReqDTO mr,
|
||||
@RequestHeader HttpHeaders headers)
|
||||
throws JsonProcessingException {
|
||||
ErrorCode err = mr.verifyRequest(headers);
|
||||
|
||||
if(err != ErrorCode.ERR_OK) {
|
||||
return ProtocolRespDTO.result(err);
|
||||
}
|
||||
|
||||
err = userAccountCacheManager.verifyPermission(mr.getAuthToken());
|
||||
|
||||
if(err != ErrorCode.ERR_OK) {
|
||||
return ProtocolRespDTO.result(err);
|
||||
}
|
||||
|
||||
IDArrayReq reqInfo = mr.getRequestObject(IDArrayReq.class);
|
||||
List<IDReturnStatus> rspInfo = new ArrayList<>();
|
||||
|
||||
if(reqInfo == null || reqInfo.getId().length > 1) {
|
||||
return ProtocolRespDTO.result(ErrorCode.ERR_REMOVEMORE);
|
||||
}
|
||||
|
||||
IDReturnStatus retItem = new IDReturnStatus();
|
||||
retItem.setId(reqInfo.getId()[0]);
|
||||
|
||||
err = disposeNodeManager.delDisposeDeviceById(Long.valueOf(reqInfo.getId()[0]));
|
||||
|
||||
retItem.setStatus(err.getCode());
|
||||
retItem.setMessage(err.getMsg());
|
||||
|
||||
rspInfo.add(retItem);
|
||||
|
||||
return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.dispose.dispose;
|
||||
|
||||
import com.dispose.common.ConstValue;
|
||||
import com.dispose.dispose.impl.DPTechImpl;
|
||||
|
||||
public class DeviceRouter {
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.dispose.dispose;
|
||||
|
||||
|
||||
import com.dispose.dispose.po.DeviceInfo;
|
||||
import com.dispose.pojo.entity.DisposeDevice;
|
||||
import com.dispose.pojo.po.DisposeDeviceCapacity;
|
||||
|
||||
|
@ -21,6 +22,9 @@ public interface DisposeEntryManager {
|
|||
|
||||
boolean getDeviceLinkStatus();
|
||||
|
||||
String getVersion();
|
||||
DeviceInfo getDeviceInfo();
|
||||
|
||||
//public NtcRequestResultInfo stopAbnormalTask(String abnormalIp, int attackType, int direction);
|
||||
|
||||
//public NtcRequestResultInfo startAbnormalTask(String abnormalIp, int attackType, int direction);
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package com.dispose.dispose;
|
||||
package com.dispose.dispose.impl;
|
||||
|
||||
import com.dispose.Interceptor.SoapPasswordCallbackHandler;
|
||||
import com.dispose.common.ConstValue;
|
||||
import com.dispose.dispose.DisposeEntryManager;
|
||||
import com.dispose.dispose.po.DeviceInfo;
|
||||
import com.dispose.pojo.entity.DisposeDevice;
|
||||
import com.dispose.pojo.po.DisposeDeviceCapacity;
|
||||
import com.dptech.dispose.AbnormalFlowCleaningServicePortType;
|
||||
|
@ -17,6 +19,25 @@ import org.apache.wss4j.dom.handler.WSHandlerConstants;
|
|||
import java.util.*;
|
||||
|
||||
public class DPTechImpl implements DisposeEntryManager {
|
||||
@Override
|
||||
public String getVersion() {
|
||||
return "Not Support";
|
||||
}
|
||||
|
||||
@Override
|
||||
public DeviceInfo getDeviceInfo() {
|
||||
return DeviceInfo.builder()
|
||||
.vendor("DPTech")
|
||||
.model("UMC")
|
||||
.firmware("Unknown")
|
||||
.os("Windows Server")
|
||||
.kernel("Windows")
|
||||
.arch("x86_64")
|
||||
.memory(-1)
|
||||
.freeMemory(-1)
|
||||
.cpuUsed(-1)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DisposeDeviceCapacity> getDeviceCapacity() {
|
|
@ -0,0 +1,41 @@
|
|||
package com.dispose.dispose.po;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
public class DeviceInfo {
|
||||
private String vendor;
|
||||
private String model;
|
||||
private String firmware;
|
||||
private String os;
|
||||
private String kernel;
|
||||
private String arch;
|
||||
private int memory;
|
||||
private int freeMemory;
|
||||
private int cpuUsed;
|
||||
|
||||
public DeviceInfo(String vendor,
|
||||
String model,
|
||||
String firmware,
|
||||
String os,
|
||||
String kernel,
|
||||
String arch,
|
||||
int memory,
|
||||
int freeMemory,
|
||||
int cpuUsed) {
|
||||
this.vendor = vendor;
|
||||
this.model = model;
|
||||
this.firmware = firmware;
|
||||
this.os = os;
|
||||
this.kernel = kernel;
|
||||
this.arch = arch;
|
||||
this.memory = memory;
|
||||
this.freeMemory = freeMemory;
|
||||
this.cpuUsed = cpuUsed;
|
||||
}
|
||||
}
|
|
@ -13,6 +13,7 @@ public interface UserAccountCacheManager {
|
|||
ErrorCode verifyUserLogin(String username, String token);
|
||||
ErrorCode verifyToken(String token);
|
||||
String getUsernameByToken(String token);
|
||||
ErrorCode verifyPermission(String token);
|
||||
|
||||
String getCacheUser() throws JsonProcessingException;
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ package com.dispose.manager.impl;
|
|||
import cn.hutool.core.convert.Convert;
|
||||
import com.dispose.common.ConstValue;
|
||||
import com.dispose.common.ErrorCode;
|
||||
import com.dispose.config.DisposeConfigure;
|
||||
import com.dispose.manager.UserAccountCacheManager;
|
||||
import com.dispose.pojo.entity.UserAccount;
|
||||
import com.dispose.pojo.po.UserAccountCache;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
@ -22,11 +22,31 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
@Component
|
||||
@Slf4j
|
||||
public class UserAccountCacheManagerImpl implements UserAccountCacheManager {
|
||||
@Resource
|
||||
private DisposeConfigure disposeConfigure;
|
||||
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private final ConcurrentHashMap<String, UserAccountCache> userAccountCache = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public ErrorCode verifyPermission(String token) {
|
||||
if(disposeConfigure.getCheckAdminPermission().equals("false")) {
|
||||
return ErrorCode.ERR_OK;
|
||||
}
|
||||
|
||||
String username = getUsernameByToken(token);
|
||||
|
||||
if(username == null) {
|
||||
return ErrorCode.ERR_LOGOUT;
|
||||
} else if(username.equals("admin")) {
|
||||
return ErrorCode.ERR_OK;
|
||||
}
|
||||
|
||||
return ErrorCode.ERR_PERMISSION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsernameByToken(String token) {
|
||||
if(userAccountCache.containsKey(token)){
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.dispose.pojo.dto;
|
|||
|
||||
import com.dispose.common.ConstValue;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
@ -13,6 +14,7 @@ import lombok.NoArgsConstructor;
|
|||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@ApiModel("通信协议实体")
|
||||
@JsonPropertyOrder({"ver", "cryptoType", "timeStamp", "msgContent"})
|
||||
public abstract class ProtocolDTO {
|
||||
@ApiModelProperty(value="协议版本号", required = true, example = "1")
|
||||
private int ver;
|
||||
|
|
|
@ -2,6 +2,10 @@ package com.dispose.pojo.dto;
|
|||
|
||||
import com.dispose.common.ConstValue;
|
||||
import com.dispose.common.ErrorCode;
|
||||
import com.dispose.manager.UserAccountCacheManager;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
@ -10,10 +14,9 @@ import lombok.EqualsAndHashCode;
|
|||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.dispose.common.ErrorCode;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
|
@ -21,14 +24,16 @@ import com.dispose.common.ErrorCode;
|
|||
public class ProtocolReqDTO extends ProtocolDTO {
|
||||
private static final ObjectMapper objMapper = new ObjectMapper();
|
||||
|
||||
private static String token;
|
||||
|
||||
public <T> T getRequestObject(Class<T> objType) throws JsonProcessingException {
|
||||
return objMapper.readValue(this.getMsgContent(), objType);
|
||||
}
|
||||
|
||||
public String getAuthToken(HttpHeaders headers) {
|
||||
return Objects.
|
||||
requireNonNull(headers.get("Authorization"))
|
||||
.get(0).replaceFirst("Bearer ", "");
|
||||
@JsonIgnore
|
||||
public String getAuthToken() {
|
||||
|
||||
return ProtocolReqDTO.token;
|
||||
}
|
||||
|
||||
public ErrorCode verifyRequest(HttpHeaders headers) {
|
||||
|
@ -37,9 +42,15 @@ public class ProtocolReqDTO extends ProtocolDTO {
|
|||
return ErrorCode.ERR_MISSAUTHHEAD;
|
||||
}
|
||||
|
||||
String token = getAuthToken(headers);
|
||||
try {
|
||||
ProtocolReqDTO.token = Objects.
|
||||
requireNonNull(headers.get("Authorization"))
|
||||
.get(0).replaceFirst("Bearer ", "");
|
||||
} catch (Exception ex) {
|
||||
return ErrorCode.ERR_MISSAUTHHEAD;
|
||||
}
|
||||
|
||||
if(token.length() == 0) {
|
||||
if(ProtocolReqDTO.token.length() == 0) {
|
||||
return ErrorCode.ERR_MISSAUTHHEAD;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,9 @@ package com.dispose.pojo.dto;
|
|||
import com.dispose.common.ConstValue;
|
||||
import com.dispose.common.ErrorCode;
|
||||
import com.dispose.pojo.po.ReturnStatus;
|
||||
import com.dispose.pojo.vo.common.DisposeCapacity;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
@ -22,6 +24,7 @@ public class ProtocolRespDTO extends ProtocolDTO {
|
|||
example = "200")
|
||||
private int code;
|
||||
|
||||
@JsonView(DisposeCapacity.CapacityView.class)
|
||||
private static String getObjectJson(Object obj) throws JsonProcessingException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
|
@ -29,8 +32,6 @@ public class ProtocolRespDTO extends ProtocolDTO {
|
|||
}
|
||||
|
||||
public static ProtocolRespDTO result(ErrorCode err) throws JsonProcessingException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
ProtocolRespDTO resp = new ProtocolRespDTO();
|
||||
|
||||
resp.setVer(ConstValue.Protocol.VERSION);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.dispose.pojo.entity;
|
||||
|
||||
import com.dispose.dispose.po.DeviceInfo;
|
||||
import com.dispose.pojo.po.DisposeDeviceCapacity;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.*;
|
||||
|
@ -90,4 +91,7 @@ public class DisposeDevice implements Serializable {
|
|||
*/
|
||||
@Transient
|
||||
private List<DisposeDeviceCapacity> devCaps;
|
||||
|
||||
@Transient
|
||||
private DeviceInfo devInfo;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package com.dispose.pojo.vo.common;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class DisposeCapacity {
|
||||
// 视图1
|
||||
public interface BaseView{}
|
||||
// 视图2 继承视图1
|
||||
public interface CapacityView extends BaseView{}
|
||||
|
||||
@JsonView(CapacityView.class)
|
||||
private int type;
|
||||
private String disposeIp;
|
||||
@JsonView(CapacityView.class)
|
||||
private int tolCapacity;
|
||||
@JsonView(CapacityView.class)
|
||||
private int usedCapacity;
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.dispose.pojo.vo.information;
|
||||
|
||||
import com.dispose.pojo.po.DisposeDeviceCapacity;
|
||||
import com.dispose.pojo.vo.common.DisposeCapacity;
|
||||
import com.dispose.pojo.vo.common.IDReturnStatus;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@JsonPropertyOrder({"id", "capacity", "status", "message"})
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class DeviceCapacityData extends IDReturnStatus {
|
||||
List<DisposeCapacity> capacity;
|
||||
|
||||
public DeviceCapacityData(List<DisposeDeviceCapacity> cpList) {
|
||||
this.capacity = new ArrayList<>();
|
||||
|
||||
if(cpList == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
cpList.forEach(v->{
|
||||
String ipAddrs = String.join(",", Arrays.asList(v.getProtectIpV4()))
|
||||
+ "," + String.join(",", Arrays.asList(v.getProtectIpV6())
|
||||
);
|
||||
|
||||
if(ipAddrs.endsWith(",")) {
|
||||
ipAddrs = ipAddrs.substring(0, ipAddrs.length() - 1);
|
||||
}
|
||||
|
||||
DisposeCapacity cp = DisposeCapacity.builder()
|
||||
.type(v.getCapacity())
|
||||
.tolCapacity(v.getTolFlowCapacity())
|
||||
.usedCapacity(0)
|
||||
.disposeIp(ipAddrs)
|
||||
.build();
|
||||
|
||||
this.capacity.add(cp);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.dispose.pojo.vo.information;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@JsonPropertyOrder({"id", "capacity", "status", "message"})
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class DeviceCapacityRsp {
|
||||
private List<DeviceCapacityData> items;
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.dispose.pojo.vo.information;
|
||||
|
||||
import com.dispose.common.ErrorCode;
|
||||
import com.dispose.dispose.po.DeviceInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@JsonPropertyOrder({"id"})
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class DeviceInfoData extends DeviceInfo {
|
||||
private String id;
|
||||
private int status;
|
||||
private String message;
|
||||
|
||||
public DeviceInfoData(String id,
|
||||
String vendor,
|
||||
String model,
|
||||
String firmware,
|
||||
String os,
|
||||
String kernel,
|
||||
String arch,
|
||||
int memory,
|
||||
int freeMemory,
|
||||
int cpuUsed,
|
||||
ErrorCode err) {
|
||||
super(vendor, model, firmware, os, kernel, arch, memory, freeMemory, cpuUsed);
|
||||
this.id = id;
|
||||
this.status = err.getCode();
|
||||
this.message = err.getMsg();
|
||||
}
|
||||
|
||||
public DeviceInfoData(String id,
|
||||
ErrorCode err) {
|
||||
this.id = id;
|
||||
this.status = err.getCode();
|
||||
this.message = err.getMsg();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package com.dispose.pojo.vo.information;
|
||||
|
||||
import com.dispose.dispose.po.DeviceInfo;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class DeviceInfoRsp {
|
||||
List<DeviceInfoData> items;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.dispose.pojo.vo.information;
|
||||
|
||||
import com.dispose.pojo.vo.common.IDReturnStatus;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import lombok.*;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonPropertyOrder({"id", "devId", "version", "status", "message"})
|
||||
public class VersionRsp extends IDReturnStatus {
|
||||
private String version;
|
||||
}
|
|
@ -10,6 +10,11 @@ public interface DisposeNodeManager {
|
|||
MReturnType<ErrorCode, String> addNewDisposeDevice(DisposeDevice dev);
|
||||
|
||||
ErrorCode delDisposeDeviceByIp(String ipAddr);
|
||||
ErrorCode delDisposeDeviceById(Long id);
|
||||
|
||||
List<DisposeDevice> getAllDisposeDevice();
|
||||
DisposeDevice getDisposeDevice(String ipAddr);
|
||||
DisposeDevice getDisposeDeviceByIp(String ipAddr);
|
||||
DisposeDevice getDisposeDeviceById(Long id);
|
||||
|
||||
void loadDisposeNodeFromDB();
|
||||
}
|
||||
|
|
|
@ -7,12 +7,15 @@ import com.dispose.dispose.DisposeEntryManager;
|
|||
import com.dispose.mapper.DisposeDeviceMapper;
|
||||
import com.dispose.pojo.entity.DisposeDevice;
|
||||
import com.dispose.pojo.po.MReturnType;
|
||||
import com.dispose.pojo.po.UserAccountCache;
|
||||
import com.dispose.service.DisposeNodeManager;
|
||||
import com.dispose.service.UserAccountService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Service
|
||||
|
@ -20,8 +23,46 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager {
|
|||
@Resource
|
||||
private DisposeDeviceMapper disposeDeviceMapper;
|
||||
|
||||
@Resource
|
||||
private UserAccountService userAccountService;
|
||||
|
||||
private final ConcurrentHashMap<String, DisposeDevice> disposeDevMap = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void loadDisposeNodeFromDB() {
|
||||
List<DisposeDevice> devList = disposeDeviceMapper.selectAll();
|
||||
|
||||
if(devList == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
devList.forEach(v -> {
|
||||
DisposeEntryManager dp = DeviceRouter.deviceRouterFactory(v.getType(),
|
||||
v.getIpAddr(),
|
||||
ConstValue.IPAddrType.getIpAddrType(v.getIpAddr()));
|
||||
if(dp != null ){
|
||||
v.setLinkStatus(dp.getDeviceLinkStatus() ? 1 : 0);
|
||||
v.setVersion(dp.getVersion());
|
||||
v.setDevInfo(dp.getDeviceInfo());
|
||||
v.setDevCaps(dp.getDeviceCapacity());
|
||||
}
|
||||
|
||||
disposeDevMap.put(v.getIpAddr(), v);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public ErrorCode delDisposeDeviceById(Long id) {
|
||||
DisposeEntryManager dp;
|
||||
DisposeDevice dev = disposeDeviceMapper.getDeviceById(id);
|
||||
|
||||
if(dev == null) {
|
||||
return ErrorCode.ERR_NOSUCHDEVICE;
|
||||
}
|
||||
|
||||
return delDisposeDeviceByIp(dev.getIpAddr());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ErrorCode delDisposeDeviceByIp(String ipAddr) {
|
||||
DisposeEntryManager dp;
|
||||
|
@ -78,6 +119,8 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager {
|
|||
dev.getDevCaps().addAll(dp.getDeviceCapacity());
|
||||
}
|
||||
|
||||
dev.setDevInfo(dp.getDeviceInfo());
|
||||
|
||||
disposeDeviceMapper.addNewDisposeDevice(dev);
|
||||
|
||||
disposeDevMap.put(dev.getIpAddr(), dev);
|
||||
|
@ -90,10 +133,19 @@ public class DisposeNodeManagerImpl implements DisposeNodeManager {
|
|||
}
|
||||
|
||||
@Override
|
||||
public DisposeDevice getDisposeDevice(String ipAddr) {
|
||||
public DisposeDevice getDisposeDeviceByIp(String ipAddr) {
|
||||
return disposeDevMap.get(ipAddr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DisposeDevice getDisposeDeviceById(Long id) {
|
||||
Optional<DisposeDevice> findRet = disposeDevMap.values().stream()
|
||||
.filter(disposeDevMap -> id.equals(disposeDevMap.getId()))
|
||||
.findFirst();
|
||||
|
||||
return findRet.orElse(null);
|
||||
}
|
||||
|
||||
private Boolean isDisposeDeviceExists(String ipAddr) {
|
||||
return disposeDevMap.containsKey(ipAddr);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import com.dispose.manager.UserAccountCacheManager;
|
|||
import com.dispose.mapper.UserAccountMapper;
|
||||
import com.dispose.pojo.entity.UserAccount;
|
||||
import com.dispose.pojo.po.MReturnType;
|
||||
import com.dispose.pojo.po.UserAccountCache;
|
||||
import com.dispose.service.UserAccountService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.dispose.task;
|
|||
import com.dispose.common.ConstValue;
|
||||
import com.dispose.dispose.DeviceRouter;
|
||||
import com.dispose.dispose.DisposeEntryManager;
|
||||
import com.dispose.dispose.po.DeviceInfo;
|
||||
import com.dispose.pojo.entity.DisposeDevice;
|
||||
import com.dispose.redis.RedisClient;
|
||||
import com.dispose.service.DisposeNodeManager;
|
||||
|
@ -35,7 +36,9 @@ public class DeviceManagerTask {
|
|||
|
||||
if(dp != null) {
|
||||
v.setLinkStatus(dp.getDeviceLinkStatus() ? 1 : 0);
|
||||
log.info("Upgrade {} Link Status", v.getIpAddr());
|
||||
v.setVersion(dp.getVersion());
|
||||
v.setDevInfo(dp.getDeviceInfo());
|
||||
log.info("Upgrade {} Device Status", v.getIpAddr());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -2,8 +2,10 @@ package com.dispose.TestSuit;
|
|||
|
||||
|
||||
import com.dispose.controller.AuthControllerTest;
|
||||
import com.dispose.controller.DeviceNodeInfoControllerTest;
|
||||
import com.dispose.controller.DeviceNodeManagerControllerTest;
|
||||
import com.dispose.dptech.DPTechInterfaceTestCase;
|
||||
import com.dispose.mapper.DisposeDeviceMapperTest;
|
||||
import com.dispose.mapper.UserAccountMapperTest;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
|
@ -12,8 +14,10 @@ import org.junit.runners.Suite;
|
|||
@Suite.SuiteClasses({
|
||||
DPTechInterfaceTestCase.class,
|
||||
UserAccountMapperTest.class,
|
||||
DisposeDeviceMapperTest.class,
|
||||
AuthControllerTest.class,
|
||||
DeviceNodeManagerControllerTest.class
|
||||
DeviceNodeManagerControllerTest.class,
|
||||
DeviceNodeInfoControllerTest.class
|
||||
})
|
||||
public class AllDisposePlatformTest {
|
||||
}
|
||||
|
|
|
@ -0,0 +1,135 @@
|
|||
package com.dispose.controller;
|
||||
|
||||
import com.dispose.common.ConstValue;
|
||||
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.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@AutoConfigureMockMvc
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
|
||||
@Slf4j
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class DeviceNodeInfoControllerTest {
|
||||
@Resource
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Resource
|
||||
private DisposeDeviceMapper disposeDeviceMapper;
|
||||
|
||||
|
||||
private Long getExistsDeviceId() {
|
||||
List<DisposeDevice> dp = disposeDeviceMapper.selectAll();
|
||||
|
||||
if(dp != null && dp.size() > 0) {
|
||||
return dp.get(0).getId();
|
||||
}
|
||||
|
||||
return -1L;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void t1_getVersion() throws Exception {
|
||||
IDArrayReq reqData = IDArrayReq.builder()
|
||||
.id(new String[] {String.valueOf(getExistsDeviceId()), "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));
|
||||
|
||||
log.info("Request Json:" + objectMapper.writeValueAsString(reqInfo));
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders
|
||||
.post("/information/version")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.header("Authorization", "Bearer 45509b805d955cfd5ef7093e27a8bb99b3733d9a7bf90e88ba528bcbd29c6122")
|
||||
.content(objectMapper.writeValueAsString(reqInfo)))
|
||||
.andDo(print()).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.code").value(200))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void t2_getDeviceInfo() throws Exception {
|
||||
IDArrayReq reqData = IDArrayReq.builder()
|
||||
.id(new String[] {String.valueOf(getExistsDeviceId()), "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));
|
||||
|
||||
log.info("Request Json:" + objectMapper.writeValueAsString(reqInfo));
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders
|
||||
.post("/information/deviceinfo")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.header("Authorization", "Bearer 45509b805d955cfd5ef7093e27a8bb99b3733d9a7bf90e88ba528bcbd29c6122")
|
||||
.content(objectMapper.writeValueAsString(reqInfo)))
|
||||
.andDo(print()).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.code").value(200))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void t3_getDeviceCapacity() throws Exception {
|
||||
IDArrayReq reqData = IDArrayReq.builder()
|
||||
.id(new String[] {String.valueOf(getExistsDeviceId()), "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));
|
||||
|
||||
log.info("Request Json:" + objectMapper.writeValueAsString(reqInfo));
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders
|
||||
.post("/information/capacity")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.header("Authorization", "Bearer 45509b805d955cfd5ef7093e27a8bb99b3733d9a7bf90e88ba528bcbd29c6122")
|
||||
.content(objectMapper.writeValueAsString(reqInfo)))
|
||||
.andDo(print()).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.code").value(200))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ package com.dispose.controller;
|
|||
import com.dispose.common.ConstValue;
|
||||
import com.dispose.pojo.dto.ProtocolReqDTO;
|
||||
import com.dispose.pojo.po.NewNodeInfo;
|
||||
import com.dispose.pojo.vo.common.IDArrayReq;
|
||||
import com.dispose.pojo.vo.device.AddNodeReq;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -37,7 +38,7 @@ public class DeviceNodeManagerControllerTest {
|
|||
private ObjectMapper objectMapper;
|
||||
|
||||
@Test
|
||||
public void addDevice() throws Exception {
|
||||
public void t1_addDevice() throws Exception {
|
||||
AddNodeReq addReq = AddNodeReq.builder()
|
||||
.items(new ArrayList<>())
|
||||
.build();
|
||||
|
@ -84,4 +85,30 @@ public class DeviceNodeManagerControllerTest {
|
|||
.getContentAsString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void t2_delDevice() throws Exception {
|
||||
IDArrayReq reqData = IDArrayReq.builder()
|
||||
.id(new String[] {"0"})
|
||||
.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));
|
||||
|
||||
log.info("Request Json:" + objectMapper.writeValueAsString(reqInfo));
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders
|
||||
.delete("/manager/device")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.header("Authorization", "Bearer 45509b805d955cfd5ef7093e27a8bb99b3733d9a7bf90e88ba528bcbd29c6122")
|
||||
.content(objectMapper.writeValueAsString(reqInfo)))
|
||||
.andDo(print()).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.code").value(200))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ public class DisposeDeviceMapperTest {
|
|||
dev.setAreaCode(0);
|
||||
dev.setDevCaps(devCaps);
|
||||
|
||||
disposeNodeManager.addNewDisposeDevice(dev);
|
||||
disposeDeviceMapper.addNewDisposeDevice(dev);
|
||||
|
||||
log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dev));
|
||||
|
||||
|
|
Loading…
Reference in New Issue