From 6cbc49ed48bb4bdd7542a324f7238159e954710a Mon Sep 17 00:00:00 2001 From: HuangXin Date: Mon, 10 Aug 2020 16:49:34 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A2=9E=E5=8A=A0=E6=B5=A9?= =?UTF-8?q?=E7=80=9A=E5=A4=84=E7=BD=AE=E8=AE=BE=E5=A4=87=E6=94=AF=E6=8C=81?= =?UTF-8?q?=202.=20=E5=A2=9E=E5=8A=A0=E8=99=9A=E6=8B=9F=E5=A4=84=E7=BD=AE?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dispose/ability/DisposeAbility.java | 6 +- .../ability/impl/DpTechAbilityImpl.java | 11 +- .../ability/impl/HaoHanAbilityImpl.java | 133 ++++++++++++++++ .../ability/impl/VirtualAbilityImpl.java | 75 +++++++++ .../dispose/common/DisposeConfigValue.java | 3 + .../impl/DisposeDeviceManagerImpl.java | 30 ++-- .../dispose/pojo/entity/DisposeDevice.java | 12 -- .../java/com/dispose/pojo/po/AbilityInfo.java | 34 ++++ .../service/DisposeAbilityRouterService.java | 29 ++++ .../impl/DisposeAbilityRouterServiceImpl.java | 101 +++++++++--- .../com/dispose/task/DeviceManagerTask.java | 24 ++- .../HaoHanGetCleanTaskNetflowInfoReq.java | 24 +++ .../HaoHanGetCleanTaskNetflowInfoResp.java | 20 +++ .../common/HaoHanGetCleanTaskStatusReq.java | 24 +++ .../common/HaoHanGetCleanTaskStatusResp.java | 31 ++++ .../HaoHanGetCleaningNetflowInfoReq.java | 22 +++ .../HaoHanGetCleaningNetflowInfoResp.java | 30 ++++ .../dispose/common/HaoHanNetflowInfo.java | 40 +++++ .../com/haohan/dispose/common/HaoHanResp.java | 26 ++++ .../dispose/common/HaoHanStartCleanReq.java | 32 ++++ .../dispose/common/HaoHanStartCleanResp.java | 28 ++++ .../dispose/common/HaoHanStopCleanReq.java | 29 ++++ .../dispose/common/HaoHanStopCleanResp.java | 20 +++ .../dispose/protocol/RestfulInterface.java | 145 ++++++++++++++++++ 24 files changed, 864 insertions(+), 65 deletions(-) create mode 100644 src/main/java/com/dispose/ability/impl/HaoHanAbilityImpl.java create mode 100644 src/main/java/com/dispose/ability/impl/VirtualAbilityImpl.java create mode 100644 src/main/java/com/dispose/pojo/po/AbilityInfo.java create mode 100644 src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskNetflowInfoReq.java create mode 100644 src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskNetflowInfoResp.java create mode 100644 src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskStatusReq.java create mode 100644 src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskStatusResp.java create mode 100644 src/main/java/com/haohan/dispose/common/HaoHanGetCleaningNetflowInfoReq.java create mode 100644 src/main/java/com/haohan/dispose/common/HaoHanGetCleaningNetflowInfoResp.java create mode 100644 src/main/java/com/haohan/dispose/common/HaoHanNetflowInfo.java create mode 100644 src/main/java/com/haohan/dispose/common/HaoHanResp.java create mode 100644 src/main/java/com/haohan/dispose/common/HaoHanStartCleanReq.java create mode 100644 src/main/java/com/haohan/dispose/common/HaoHanStartCleanResp.java create mode 100644 src/main/java/com/haohan/dispose/common/HaoHanStopCleanReq.java create mode 100644 src/main/java/com/haohan/dispose/common/HaoHanStopCleanResp.java create mode 100644 src/main/java/com/haohan/dispose/protocol/RestfulInterface.java diff --git a/src/main/java/com/dispose/ability/DisposeAbility.java b/src/main/java/com/dispose/ability/DisposeAbility.java index 6d90459d..373e855d 100644 --- a/src/main/java/com/dispose/ability/DisposeAbility.java +++ b/src/main/java/com/dispose/ability/DisposeAbility.java @@ -16,7 +16,7 @@ import javax.annotation.Nullable; public interface DisposeAbility { /** - * Init device env error code. + * Init device env. * * @param urlPath the url path * @param username the username @@ -46,11 +46,13 @@ public interface DisposeAbility { * @param capType the cap type * @param nfDirection the nf direction * @param attackType the attack type + * @param taskId the task id * @return the mul return type */ MulReturnType stopDispose(String ip, DisposeCapacityType capType, @Nullable NetflowDirection[] nfDirection, - @Nullable DDoSAttackType[] attackType); + @Nullable DDoSAttackType[] attackType, + @Nullable Long taskId); /** diff --git a/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java b/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java index 113a28e4..e7f77431 100644 --- a/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java +++ b/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java @@ -45,7 +45,7 @@ public class DpTechAbilityImpl implements DisposeAbility { private AbnormalFlowCleaningServicePortType cleanTypePort; /** - * Init device env error code. + * Init device env. * * @param urlPath the url path * @param username the username @@ -147,12 +147,14 @@ public class DpTechAbilityImpl implements DisposeAbility { * @param capType the cap type * @param nfDirection the nf direction * @param attackType the attack type + * @param taskId the task id * @return the mul return type */ @Override public MulReturnType stopDispose(String ipAddr, DisposeCapacityType capType, @Nullable NetflowDirection[] nfDirection, - @Nullable DDoSAttackType[] attackType) { + @Nullable DDoSAttackType[] attackType, + @Nullable Long taskId) { ErrorCode err = ErrorCode.ERR_OK; try { @@ -192,6 +194,11 @@ public class DpTechAbilityImpl implements DisposeAbility { return new MulReturnType<>(err, null); } + /** + * Gets device link status. + * + * @return the device link status + */ @Override public boolean getDeviceLinkStatus() { try { diff --git a/src/main/java/com/dispose/ability/impl/HaoHanAbilityImpl.java b/src/main/java/com/dispose/ability/impl/HaoHanAbilityImpl.java new file mode 100644 index 00000000..95a4602c --- /dev/null +++ b/src/main/java/com/dispose/ability/impl/HaoHanAbilityImpl.java @@ -0,0 +1,133 @@ +package com.dispose.ability.impl; + +import com.dispose.ability.DisposeAbility; +import com.dispose.common.DDoSAttackType; +import com.dispose.common.DisposeCapacityType; +import com.dispose.common.ErrorCode; +import com.dispose.common.NetflowDirection; +import com.dispose.pojo.po.MulReturnType; +import com.haohan.dispose.common.HaoHanStartCleanResp; +import com.haohan.dispose.common.HaoHanStopCleanResp; +import com.haohan.dispose.protocol.RestfulInterface; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import javax.annotation.Nullable; + +/** + * The type Hao han ability. + * + * @author + */ +@Component +@Slf4j +public class HaoHanAbilityImpl implements DisposeAbility { + + /** + * The constant DISPOSE_PLATFORM_NAME. + */ + private static final String DISPOSE_PLATFORM_NAME = "HANGYAN_CMCC"; + /** + * The Restful interface. + */ + private final RestfulInterface restfulInterface = new RestfulInterface(); + /** + * The Url root path. + */ + private String urlRootPath; + + /** + * Init device env. + * + * @param urlPath the url path + * @param username the username + * @param password the password + */ + @Override + public void initDeviceEnv(String urlPath, String username, String password) { + this.urlRootPath = urlPath; + } + + /** + * Run dispose mul return type. + * + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type + * @param duration the duration + * @return the mul return type + */ + @Override + public MulReturnType runDispose(String ip, DisposeCapacityType capType, + @Nullable NetflowDirection[] nfDirection, + @Nullable DDoSAttackType[] attackType, + @Nullable Long duration) { + log.info("++++Begging Haohan Start Cleanup Task: {}", ip); + + // 适配处置时间参数, -1为不限制处置时间 + if (duration == null || duration < 0) { + duration = -1L; + } + + HaoHanStartCleanResp resp = restfulInterface.startClean(this.urlRootPath, ip, duration.intValue(), + DISPOSE_PLATFORM_NAME); + + if (resp == null) { + log.error("----Error Haohan start clean {} server return error", ip); + return new MulReturnType<>(ErrorCode.ERR_HAOHAN_ERROR, null); + } + + if (resp.getState() != ErrorCode.ERR_OK.getCode()) { + log.error("----Error Haohan start clean {} return error: {}, {}", ip, resp.getState(), resp.getMsg()); + return new MulReturnType<>(ErrorCode.ERR_HAOHAN_ERROR, null); + } + + log.info("----Finish Haohan Start Cleanup Task: {}", ip); + return new MulReturnType<>(ErrorCode.ERR_OK, (long) resp.getCleanTaskId()); + } + + /** + * Stop dispose mul return type. + * + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type + * @param taskId the task id + * @return the mul return type + */ + @Override + public MulReturnType stopDispose(String ip, DisposeCapacityType capType, + @Nullable NetflowDirection[] nfDirection, + @Nullable DDoSAttackType[] attackType, + @Nullable Long taskId) { + log.info("++++Begging Haohan Stop Cleanup Task: {}", taskId); + + if (taskId == null) { + return new MulReturnType<>(ErrorCode.ERR_PARAMS, null); + } + + HaoHanStopCleanResp resp = restfulInterface.stopClean(this.urlRootPath, taskId.intValue(), + DISPOSE_PLATFORM_NAME); + + if (resp == null) { + log.error("----Error Haohan stop task{} server return error", taskId); + return new MulReturnType<>(ErrorCode.ERR_HAOHAN_ERROR, null); + } + + log.info("----Finish Haohan Stop Cleanup Task: {}", taskId); + return new MulReturnType<>(ErrorCode.ERR_OK, null); + } + + /** + * Gets device link status. + * + * @return the device link status + */ + @Override + public boolean getDeviceLinkStatus() { + // 获取任务信息接口调用成功认为设备心跳正常 + return (restfulInterface.getCleanTaskStatus(this.urlRootPath, -1) != null); + } +} diff --git a/src/main/java/com/dispose/ability/impl/VirtualAbilityImpl.java b/src/main/java/com/dispose/ability/impl/VirtualAbilityImpl.java new file mode 100644 index 00000000..4c1dbcad --- /dev/null +++ b/src/main/java/com/dispose/ability/impl/VirtualAbilityImpl.java @@ -0,0 +1,75 @@ +package com.dispose.ability.impl; + +import com.dispose.ability.DisposeAbility; +import com.dispose.common.DDoSAttackType; +import com.dispose.common.DisposeCapacityType; +import com.dispose.common.ErrorCode; +import com.dispose.common.NetflowDirection; +import com.dispose.pojo.po.MulReturnType; + +import javax.annotation.Nullable; + +/** + * The type Virtual ability. + * + * @author + */ +public class VirtualAbilityImpl implements DisposeAbility { + /** + * Init device env. + * + * @param urlPath the url path + * @param username the username + * @param password the password + */ + @Override + public void initDeviceEnv(String urlPath, String username, String password) { + + } + + /** + * Run dispose mul return type. + * + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type + * @param duration the duration + * @return the mul return type + */ + @Override + public MulReturnType runDispose(String ip, DisposeCapacityType capType, + @Nullable NetflowDirection[] nfDirection, + @Nullable DDoSAttackType[] attackType, + @Nullable Long duration) { + return new MulReturnType<>(ErrorCode.ERR_OK, null); + } + + /** + * Stop dispose mul return type. + * + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type + * @param taskId the task id + * @return the mul return type + */ + @Override + public MulReturnType stopDispose(String ip, DisposeCapacityType capType, + @Nullable NetflowDirection[] nfDirection, + @Nullable DDoSAttackType[] attackType, + @Nullable Long taskId) { + return new MulReturnType<>(ErrorCode.ERR_OK, null); + } + + /** + * Gets device link status. + * + * @return the device link status + */ + @Override + public boolean getDeviceLinkStatus() { + return true; + } +} diff --git a/src/main/java/com/dispose/common/DisposeConfigValue.java b/src/main/java/com/dispose/common/DisposeConfigValue.java index 317044fe..7bab54f7 100644 --- a/src/main/java/com/dispose/common/DisposeConfigValue.java +++ b/src/main/java/com/dispose/common/DisposeConfigValue.java @@ -17,5 +17,8 @@ public class DisposeConfigValue { */ public static volatile long REQUEST_TIMEOUT_MS = 5 * 1000; + /** + * The constant USED_PRIVACY_PROTECT. + */ public static volatile boolean USED_PRIVACY_PROTECT = false; } diff --git a/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java b/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java index 78268790..c30eb69c 100644 --- a/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java +++ b/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java @@ -8,6 +8,7 @@ import com.dispose.mapper.DisposeCapacityMapper; import com.dispose.mapper.DisposeDeviceMapper; import com.dispose.pojo.entity.DisposeDevice; import com.dispose.pojo.po.MulReturnType; +import com.dispose.service.DisposeAbilityRouterService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import lombok.extern.slf4j.Slf4j; @@ -18,7 +19,6 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.ConcurrentHashMap; /** * The type Dispose device manager. @@ -28,11 +28,6 @@ import java.util.concurrent.ConcurrentHashMap; @Component @Slf4j public class DisposeDeviceManagerImpl implements DisposeDeviceManager { - - /** - * The Dispose device. - */ - private final ConcurrentHashMap disposeDevice = new ConcurrentHashMap<>(); /** * The Dispose capacity mapper. */ @@ -44,6 +39,12 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager { @Resource private DisposeDeviceMapper disposeDeviceMapper; + /** + * The Dispose ability router service. + */ + @Resource + private DisposeAbilityRouterService disposeAbilityRouterService; + /** * Add dispose device mul return type. * @@ -71,11 +72,14 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager { return new MulReturnType<>(ErrorCode.ERR_DATABASE, -1L); } else { // 添加设备到缓存中,方便调用时查找 - String devKey = dev.getIpAddr() + ":" + dev.getIpPort(); - disposeDevice.put(devKey, dev); + ErrorCode err = disposeAbilityRouterService.addDisposeAbilityDevice(dev); + // 添加设备能力信息 - disposeCapacityMapper.addNewDisposeCapacity(dev.getDevCapacity()); - return new MulReturnType<>(ErrorCode.ERR_OK, dev.getId()); + if (err == ErrorCode.ERR_OK) { + disposeCapacityMapper.addNewDisposeCapacity(dev.getDevCapacity()); + } + + return new MulReturnType<>(err, dev.getId()); } } @@ -155,7 +159,7 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager { disposeCapacityMapper.delDeviceDisposeCapacity(tDev.getId()); // 新增能力信息完成更新 - if(tDev.getDevCapacity().size() != 0){ + if (tDev.getDevCapacity().size() != 0) { disposeCapacityMapper.addNewDisposeCapacity(tDev.getDevCapacity()); } @@ -220,7 +224,7 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager { // 获取分页数据 List devList = disposeDeviceMapper.selectAll(); - if(devList == null) { + if (devList == null) { devList = new ArrayList<>(); } @@ -239,7 +243,7 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager { public List getAllDisposeDevices() { List devList = disposeDeviceMapper.selectAll(); - if(devList == null) { + if (devList == null) { devList = new ArrayList<>(); } diff --git a/src/main/java/com/dispose/pojo/entity/DisposeDevice.java b/src/main/java/com/dispose/pojo/entity/DisposeDevice.java index 804a24fa..ae45dc18 100644 --- a/src/main/java/com/dispose/pojo/entity/DisposeDevice.java +++ b/src/main/java/com/dispose/pojo/entity/DisposeDevice.java @@ -124,16 +124,4 @@ public class DisposeDevice implements Serializable { */ @Transient private List devCapacity; - - /** - * The Dev info. - */ - //@Transient - //private DeviceInfo devInfo; - - /** - * The Link status. - */ - //@Transient - //private Integer linkStatus; } diff --git a/src/main/java/com/dispose/pojo/po/AbilityInfo.java b/src/main/java/com/dispose/pojo/po/AbilityInfo.java new file mode 100644 index 00000000..6f313672 --- /dev/null +++ b/src/main/java/com/dispose/pojo/po/AbilityInfo.java @@ -0,0 +1,34 @@ +package com.dispose.pojo.po; + +import com.dispose.ability.DisposeAbility; +import com.dispose.pojo.entity.DisposeDevice; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * The type Ability info. + * + * @author + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class AbilityInfo { + /** + * The Db. + */ + private DisposeAbility db; + + /** + * The Dev. + */ + private DisposeDevice dev; + + /** + * The Link status. + */ + private boolean linkStatus; +} diff --git a/src/main/java/com/dispose/service/DisposeAbilityRouterService.java b/src/main/java/com/dispose/service/DisposeAbilityRouterService.java index e5b1fd46..e07ccf7c 100644 --- a/src/main/java/com/dispose/service/DisposeAbilityRouterService.java +++ b/src/main/java/com/dispose/service/DisposeAbilityRouterService.java @@ -1,9 +1,38 @@ package com.dispose.service; +import com.dispose.common.ErrorCode; +import com.dispose.pojo.entity.DisposeDevice; +import com.dispose.pojo.po.AbilityInfo; + +import java.util.List; + /** * The interface Dispose ability router service. * * @author */ public interface DisposeAbilityRouterService { + /** + * Gets ability device. + * + * @param ipAddr the ip addr + * @param ipPort the ip port + * @return the ability device + */ + AbilityInfo getAbilityDevice(String ipAddr, String ipPort); + + /** + * Gets all ability devices. + * + * @return the all ability devices + */ + List getAllAbilityDevices(); + + /** + * Add dispose ability device error code. + * + * @param dev the dev + * @return the error code + */ + ErrorCode addDisposeAbilityDevice(DisposeDevice dev); } diff --git a/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java b/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java index c59b06ec..18f5414d 100644 --- a/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java @@ -2,15 +2,20 @@ package com.dispose.service.impl; import com.dispose.ability.DisposeAbility; import com.dispose.ability.impl.DpTechAbilityImpl; +import com.dispose.ability.impl.HaoHanAbilityImpl; +import com.dispose.ability.impl.VirtualAbilityImpl; +import com.dispose.common.ErrorCode; import com.dispose.common.HttpType; import com.dispose.manager.DisposeDeviceManager; import com.dispose.pojo.entity.DisposeDevice; +import com.dispose.pojo.po.AbilityInfo; import com.dispose.service.DisposeAbilityRouterService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.ConcurrentHashMap; @@ -25,7 +30,7 @@ public class DisposeAbilityRouterServiceImpl implements DisposeAbilityRouterServ /** * The Dispose ability map. */ - private ConcurrentHashMap disposeAbilityMap = new ConcurrentHashMap<>(); + private final ConcurrentHashMap disposeAbilityMap = new ConcurrentHashMap<>(); /** * The Dispose device manager. @@ -37,31 +42,83 @@ public class DisposeAbilityRouterServiceImpl implements DisposeAbilityRouterServ * Init dispose ability. */ @PostConstruct - public void initDisposeAbility() { + private void initDisposeAbility() { List devList = disposeDeviceManager.getAllDisposeDevices(); + devList.forEach(this::addDisposeAbilityDevice); + } - devList.forEach(v -> { - DisposeAbility db; + /** + * Gets ability device. + * + * @param ipAddr the ip addr + * @param ipPort the ip port + * @return the ability device + */ + @Override + public AbilityInfo getAbilityDevice(String ipAddr, String ipPort) { + return disposeAbilityMap.get(getAbilityMapKey(ipAddr, ipPort)); + } - String httpType = v.getUrlType() == HttpType.HTTP ? "http://" : "https://"; - String addr = v.getIpPort() == null || v.getIpPort().length() == 0 ? v.getIpAddr() : - v.getIpAddr() + ":" + v.getIpPort(); + /** + * Gets all ability devices. + * + * @return the all ability devices + */ + @Override + public List getAllAbilityDevices() { + return new ArrayList<>(disposeAbilityMap.values()); + } - String url = httpType + addr + "/" + v.getUrlPath(); + /** + * Add dispose ability device error code. + * + * @param dev the dev + * @return the error code + */ + @Override + public ErrorCode addDisposeAbilityDevice(DisposeDevice dev) { + DisposeAbility db; - switch (v.getDeviceType()) { - case DPTECH_UMC: - db = new DpTechAbilityImpl(); - db.initDeviceEnv(url, v.getUserName(), v.getPassword()); - disposeAbilityMap.put(addr, db); - break; - case HAOHAN_PLATFORM: - break; - case VIRTUAL_DISPOSE: - break; - default: - break; - } - }); + String httpType = dev.getUrlType() == HttpType.HTTP ? "http://" : "https://"; + String addr = getAbilityMapKey(dev.getIpAddr(), dev.getIpPort()); + String url = httpType + addr + "/" + dev.getUrlPath(); + + switch (dev.getDeviceType()) { + case DPTECH_UMC: + db = new DpTechAbilityImpl(); + break; + case HAOHAN_PLATFORM: + db = new HaoHanAbilityImpl(); + break; + case VIRTUAL_DISPOSE: + db = new VirtualAbilityImpl(); + break; + default: + log.error("Unknown dispose device type: {}", dev.getDeviceType()); + return ErrorCode.ERR_PARAMS; + } + + // 初始化设备 + db.initDeviceEnv(url, dev.getUserName(), dev.getPassword()); + + // 缓存处置设备到Hash表中 + disposeAbilityMap.put(addr, AbilityInfo.builder() + .db(db) + .dev(dev) + .linkStatus(false) + .build()); + + return ErrorCode.ERR_OK; + } + + /** + * Gets ability map key. + * + * @param ipAddr the ip addr + * @param ipPort the ip port + * @return the ability map key + */ + private String getAbilityMapKey(String ipAddr, String ipPort) { + return (ipPort == null || ipPort.length() == 0) ? ipAddr : (ipAddr + ":" + ipPort); } } diff --git a/src/main/java/com/dispose/task/DeviceManagerTask.java b/src/main/java/com/dispose/task/DeviceManagerTask.java index 5e896f54..84ce4f77 100644 --- a/src/main/java/com/dispose/task/DeviceManagerTask.java +++ b/src/main/java/com/dispose/task/DeviceManagerTask.java @@ -1,13 +1,11 @@ package com.dispose.task; -import com.dispose.ability.DisposeAbility; -import com.dispose.manager.DisposeDeviceManager; +import com.dispose.service.DisposeAbilityRouterService; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; -import javax.annotation.PostConstruct; import javax.annotation.Resource; /** @@ -18,11 +16,11 @@ import javax.annotation.Resource; @Component @Slf4j public class DeviceManagerTask { + /** + * The Dispose ability router service. + */ @Resource - private DisposeDeviceManager disposeDeviceManager; - - @Resource - private DisposeAbility disposeAbility; + private DisposeAbilityRouterService disposeAbilityRouterService; /** * Thread pool task. @@ -30,12 +28,10 @@ public class DeviceManagerTask { @Async("bizExecutor") @Scheduled(cron = "0/5 * * * * ?") public void threadPoolTask() { - log.info("task run......... {}", disposeAbility.getDeviceLinkStatus()); - } - - @PostConstruct - public void init() { - disposeAbility.initDeviceEnv("http://10.88.77.15/UMC/service/AbnormalFlowCleaningService", - "admin", "UMCAdministrator"); + disposeAbilityRouterService.getAllAbilityDevices().forEach(v -> log.info("{}{} get link status {}", + v.getDev().getIpAddr(), + ((v.getDev().getIpPort() == null || v.getDev().getIpPort().length() == 0) ? + "" : ":" + v.getDev().getIpPort()), + v.getDb().getDeviceLinkStatus())); } } diff --git a/src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskNetflowInfoReq.java b/src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskNetflowInfoReq.java new file mode 100644 index 00000000..a60319c9 --- /dev/null +++ b/src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskNetflowInfoReq.java @@ -0,0 +1,24 @@ +package com.haohan.dispose.common; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +/** + * The type Hao han get clean task netflow info req. + * + * @author + */ +@EqualsAndHashCode(callSuper = true) +@Data +@NoArgsConstructor +public class HaoHanGetCleanTaskNetflowInfoReq extends HaoHanGetCleanTaskStatusReq { + /** + * Instantiates a new Hao han get clean task netflow info req. + * + * @param taskId the task id + */ + public HaoHanGetCleanTaskNetflowInfoReq(Integer taskId) { + this.setCleanTaskId(taskId); + } +} diff --git a/src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskNetflowInfoResp.java b/src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskNetflowInfoResp.java new file mode 100644 index 00000000..f60d9b53 --- /dev/null +++ b/src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskNetflowInfoResp.java @@ -0,0 +1,20 @@ +package com.haohan.dispose.common; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +/** + * The type Hao han get clean task netflow info resp. + * + * @author + */ +@EqualsAndHashCode(callSuper = true) +@Data +@NoArgsConstructor +@JsonPropertyOrder({"cleanTaskState", "state", "msg"}) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class HaoHanGetCleanTaskNetflowInfoResp extends HaoHanGetCleaningNetflowInfoResp { +} diff --git a/src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskStatusReq.java b/src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskStatusReq.java new file mode 100644 index 00000000..9bae9776 --- /dev/null +++ b/src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskStatusReq.java @@ -0,0 +1,24 @@ +package com.haohan.dispose.common; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * The type Hao han get clean task status req. + * + * @author + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class HaoHanGetCleanTaskStatusReq { + /** + * The Clean task state. + */ + private Integer cleanTaskId; +} diff --git a/src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskStatusResp.java b/src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskStatusResp.java new file mode 100644 index 00000000..1490114e --- /dev/null +++ b/src/main/java/com/haohan/dispose/common/HaoHanGetCleanTaskStatusResp.java @@ -0,0 +1,31 @@ +package com.haohan.dispose.common; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +import javax.annotation.Nullable; + +/** + * The type Hao han get clean task status. + * + * @author + */ +@EqualsAndHashCode(callSuper = true) +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonPropertyOrder({"cleanTaskState", "state", "msg"}) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class HaoHanGetCleanTaskStatusResp extends HaoHanResp { + /** + * The Clean task state. + */ + @Nullable + private Integer cleanTaskState; +} diff --git a/src/main/java/com/haohan/dispose/common/HaoHanGetCleaningNetflowInfoReq.java b/src/main/java/com/haohan/dispose/common/HaoHanGetCleaningNetflowInfoReq.java new file mode 100644 index 00000000..5bb0f9c0 --- /dev/null +++ b/src/main/java/com/haohan/dispose/common/HaoHanGetCleaningNetflowInfoReq.java @@ -0,0 +1,22 @@ +package com.haohan.dispose.common; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * The type Hao han get cleaning netflow info req. + * + * @author + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class HaoHanGetCleaningNetflowInfoReq { + /** + * The Order from. + */ + private String orderFrom; +} diff --git a/src/main/java/com/haohan/dispose/common/HaoHanGetCleaningNetflowInfoResp.java b/src/main/java/com/haohan/dispose/common/HaoHanGetCleaningNetflowInfoResp.java new file mode 100644 index 00000000..5ea99634 --- /dev/null +++ b/src/main/java/com/haohan/dispose/common/HaoHanGetCleaningNetflowInfoResp.java @@ -0,0 +1,30 @@ +package com.haohan.dispose.common; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * The type Hao han get cleaning netflow info resp. + * + * @author + */ +@EqualsAndHashCode(callSuper = true) +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonPropertyOrder({"cleanTaskState", "state", "msg"}) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class HaoHanGetCleaningNetflowInfoResp extends HaoHanResp { + /** + * The Data. + */ + List data; +} diff --git a/src/main/java/com/haohan/dispose/common/HaoHanNetflowInfo.java b/src/main/java/com/haohan/dispose/common/HaoHanNetflowInfo.java new file mode 100644 index 00000000..bcdea3ce --- /dev/null +++ b/src/main/java/com/haohan/dispose/common/HaoHanNetflowInfo.java @@ -0,0 +1,40 @@ +package com.haohan.dispose.common; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * The type Hao han netflow info. + * + * @author + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class HaoHanNetflowInfo { + /** + * The Ip. + */ + private String ip; + /** + * The In flow. + */ + private Double inFlow; + /** + * The In packets. + */ + private Integer inPackets; + /** + * The Out flow. + */ + private Double outFlow; + /** + * The Out packets. + */ + private Integer outPackets; +} diff --git a/src/main/java/com/haohan/dispose/common/HaoHanResp.java b/src/main/java/com/haohan/dispose/common/HaoHanResp.java new file mode 100644 index 00000000..f008b240 --- /dev/null +++ b/src/main/java/com/haohan/dispose/common/HaoHanResp.java @@ -0,0 +1,26 @@ +package com.haohan.dispose.common; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * The type Hao han resp. + * + * @author + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class HaoHanResp { + /** + * The State. + */ + private Integer state; + /** + * The Msg. + */ + private String msg; +} diff --git a/src/main/java/com/haohan/dispose/common/HaoHanStartCleanReq.java b/src/main/java/com/haohan/dispose/common/HaoHanStartCleanReq.java new file mode 100644 index 00000000..c33be83a --- /dev/null +++ b/src/main/java/com/haohan/dispose/common/HaoHanStartCleanReq.java @@ -0,0 +1,32 @@ +package com.haohan.dispose.common; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * The type Hao han start clean req. + * + * @author + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class HaoHanStartCleanReq { + /** + * The Ip. + */ + private String ip; + /** + * The Duration. + */ + private Integer duration; + /** + * The Order form. + */ + private String orderForm; +} diff --git a/src/main/java/com/haohan/dispose/common/HaoHanStartCleanResp.java b/src/main/java/com/haohan/dispose/common/HaoHanStartCleanResp.java new file mode 100644 index 00000000..834880be --- /dev/null +++ b/src/main/java/com/haohan/dispose/common/HaoHanStartCleanResp.java @@ -0,0 +1,28 @@ +package com.haohan.dispose.common; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +/** + * The type Hao han start clean resp. + * + * @author + */ +@EqualsAndHashCode(callSuper = true) +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonPropertyOrder({"cleanTaskId", "state", "msg"}) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class HaoHanStartCleanResp extends HaoHanResp { + /** + * The Clean task id. + */ + private Integer cleanTaskId; +} diff --git a/src/main/java/com/haohan/dispose/common/HaoHanStopCleanReq.java b/src/main/java/com/haohan/dispose/common/HaoHanStopCleanReq.java new file mode 100644 index 00000000..8e598cb5 --- /dev/null +++ b/src/main/java/com/haohan/dispose/common/HaoHanStopCleanReq.java @@ -0,0 +1,29 @@ +package com.haohan.dispose.common; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * The type Hao han stop clean req. + * + * @author + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class HaoHanStopCleanReq { + /** + * The Clean task id. + */ + private Integer cleanTaskId; + + /** + * The Order form. + */ + private String orderForm; +} diff --git a/src/main/java/com/haohan/dispose/common/HaoHanStopCleanResp.java b/src/main/java/com/haohan/dispose/common/HaoHanStopCleanResp.java new file mode 100644 index 00000000..5cf82e0b --- /dev/null +++ b/src/main/java/com/haohan/dispose/common/HaoHanStopCleanResp.java @@ -0,0 +1,20 @@ +package com.haohan.dispose.common; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import lombok.Builder; +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * The type Hao han stop clean resp. + * + * @author + */ +@EqualsAndHashCode(callSuper = true) +@Data +@Builder +@JsonPropertyOrder({"state", "msg"}) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class HaoHanStopCleanResp extends HaoHanResp { +} diff --git a/src/main/java/com/haohan/dispose/protocol/RestfulInterface.java b/src/main/java/com/haohan/dispose/protocol/RestfulInterface.java new file mode 100644 index 00000000..8dac7dd6 --- /dev/null +++ b/src/main/java/com/haohan/dispose/protocol/RestfulInterface.java @@ -0,0 +1,145 @@ +package com.haohan.dispose.protocol; + +import cn.hutool.http.Header; +import cn.hutool.http.HttpRequest; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.haohan.dispose.common.HaoHanGetCleanTaskNetflowInfoReq; +import com.haohan.dispose.common.HaoHanGetCleanTaskNetflowInfoResp; +import com.haohan.dispose.common.HaoHanGetCleanTaskStatusReq; +import com.haohan.dispose.common.HaoHanGetCleanTaskStatusResp; +import com.haohan.dispose.common.HaoHanGetCleaningNetflowInfoReq; +import com.haohan.dispose.common.HaoHanGetCleaningNetflowInfoResp; +import com.haohan.dispose.common.HaoHanStartCleanReq; +import com.haohan.dispose.common.HaoHanStartCleanResp; +import com.haohan.dispose.common.HaoHanStopCleanReq; +import com.haohan.dispose.common.HaoHanStopCleanResp; +import lombok.extern.slf4j.Slf4j; + +import java.util.HashMap; +import java.util.Map; + +/** + * The type Restful interface. + * + * @author + */ +@Slf4j +public class RestfulInterface { + /** + * The constant OBJECT_MAPPER. + */ + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + /** + * Post json string. + * + * @param url the url + * @param header the header + * @param body the body + * @return the string + */ + private static String postJson(String url, Map header, String body) { + return HttpRequest.post(url).header(Header.CONTENT_TYPE, "application/json").addHeaders(header).body(body) + .execute().body(); + } + + /** + * Protocol run t. + * + * @param the type parameter + * @param the type parameter + * @param url the url + * @param obj the obj + * @param outType the out type + * @return the t + */ + private T protocolRun(String url, E obj, Class outType) { + try { + Map httpHeadMap = new HashMap<>(2); + httpHeadMap.put(String.valueOf(Header.CONNECTION), "keep-alive"); + httpHeadMap.put(String.valueOf(Header.ACCEPT), "*/*"); + + String svrResp = postJson(url, httpHeadMap, OBJECT_MAPPER.writeValueAsString(obj)); + + if (svrResp == null) { + log.debug("Server return null: {}", url); + return null; + } + + return OBJECT_MAPPER.readValue(svrResp, outType); + } catch (JsonProcessingException e) { + log.debug("System exception: ", e); + return null; + } + } + + /** + * Gets clean task status. + * + * @param baseUrlPath the base url path + * @param taskId the task id + * @return the clean task status + */ + public HaoHanGetCleanTaskStatusResp getCleanTaskStatus(String baseUrlPath, Integer taskId) { + return protocolRun(baseUrlPath + "/getCleanTaskState", + HaoHanGetCleanTaskStatusReq.builder().cleanTaskId(taskId).build(), + HaoHanGetCleanTaskStatusResp.class); + } + + /** + * Start clean hao han start clean resp. + * + * @param baseUrlPath the base url path + * @param ipAddr the ip addr + * @param times the times + * @param readme the readme + * @return the hao han start clean resp + */ + public HaoHanStartCleanResp startClean(String baseUrlPath, String ipAddr, int times, String readme) { + return protocolRun(baseUrlPath + "/sendTow", + new HaoHanStartCleanReq(ipAddr, times, readme), + HaoHanStartCleanResp.class); + } + + /** + * Stop clean hao han stop clean resp. + * + * @param baseUrlPath the base url path + * @param taskId the task id + * @param readme the readme + * @return the hao han stop clean resp + */ + public HaoHanStopCleanResp stopClean(String baseUrlPath, Integer taskId, String readme) { + return protocolRun(baseUrlPath + "/delTow", + new HaoHanStopCleanReq(taskId, readme), + HaoHanStopCleanResp.class); + } + + /** + * Gets cleaning netflow. + * + * @param baseUrlPath the base url path + * @param readme the readme + * @return the cleaning netflow + */ + public HaoHanGetCleaningNetflowInfoResp getCleaningNetflow(String baseUrlPath, String readme) { + return protocolRun(baseUrlPath + "/allIpFlow", + new HaoHanGetCleaningNetflowInfoReq(readme), + HaoHanGetCleaningNetflowInfoResp.class); + } + + /** + * Gets clean task netflow. + * + * @param baseUrlPath the base url path + * @param taskId the task id + * @return the clean task netflow + */ + public HaoHanGetCleanTaskNetflowInfoResp getCleanTaskNetflow(String baseUrlPath, Integer taskId) { + return protocolRun(baseUrlPath + "/cleanTaskFlow", + new HaoHanGetCleanTaskNetflowInfoReq(taskId), + HaoHanGetCleanTaskNetflowInfoResp.class + ); + } +}