From 7bc5f2ef1a6ad6e3cbb7f647f442a559d39b799a Mon Sep 17 00:00:00 2001 From: HuangXin Date: Thu, 14 Jan 2021 09:58:15 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A2=9E=E5=8A=A0=E8=BF=AA?= =?UTF-8?q?=E6=99=AE=E9=98=B2=E6=8A=A4=E5=AF=B9=E8=B1=A1=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ability/impl/DpTechAbilityImpl.java | 41 +----- .../ability/impl/DpTechBypassAbilityImpl.java | 136 +++++++++++++++++- .../com/dispose/common/DpTechConfigValue.java | 5 + .../device/ability/DpProtectObject.java | 10 +- .../java/com/dispose/test/dev/debug/demo.java | 18 +++ 5 files changed, 172 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java b/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java index 718151f8..a68f9025 100644 --- a/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java +++ b/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java @@ -30,9 +30,7 @@ import org.apache.wss4j.dom.handler.WSHandlerConstants; import javax.xml.ws.BindingProvider; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; @@ -51,10 +49,6 @@ public class DpTechAbilityImpl implements DisposeAbility { * The Protect ip v 6. */ private final List protectIpV6 = new ArrayList<>(); - /** - * The All cleanup devices. - */ - private final HashSet allCleanupDevices = new HashSet<>(); /** * The Timer cnt. */ @@ -69,36 +63,14 @@ public class DpTechAbilityImpl implements DisposeAbility { private AbnormalFlowCleaningServicePortType cleanTypePort; /** - * Init cleanup devices. + * Gets clean type port. + * + * @return the clean type port */ - private void initCleanupDevices() { - try { - log.info("++++Begging DPTech Get All Detect Devices"); - String dev = cleanTypePort.getAllProtectDevices(); - log.info("----Finish DPTech Get All Detect Devices: {}", dev); - - Collections.addAll(allCleanupDevices, dev.split(DisposeConfigValue.SPLIT_CHAR)); - } catch (Exception ex) { - log.error("----Exception DPTech Get All Detect Devices"); - } + public AbnormalFlowCleaningServicePortType getCleanTypePort() { + return cleanTypePort; } - /** - * Init protection objects. - */ - private void initProtectionObjects() { - try { - log.info("++++Begging DPTech Get All Protection Objects"); - ArrayOfProtectionObjectDataForService ret = cleanTypePort.getAllProtectionObjectFromUMC(); - - String dev = cleanTypePort.getAllProtectDevices(); - log.info("----Finish DPTech Get All Protection Objects: {}", dev); - - Collections.addAll(allCleanupDevices, dev.split(DisposeConfigValue.SPLIT_CHAR)); - } catch (Exception ex) { - log.error("----Exception DPTech Get All Protection Objects"); - } - } /** * Init device env. @@ -139,9 +111,6 @@ public class DpTechAbilityImpl implements DisposeAbility { //读取超时 policy.setReceiveTimeout(DpTechConfigValue.SOAP_RECEIVE_TIMEOUT_SECOND); conduit.setClient(policy); - - // 获取所有检测设备 - initCleanupDevices(); } /** diff --git a/src/main/java/com/dispose/ability/impl/DpTechBypassAbilityImpl.java b/src/main/java/com/dispose/ability/impl/DpTechBypassAbilityImpl.java index 516ca674..a0a13a73 100644 --- a/src/main/java/com/dispose/ability/impl/DpTechBypassAbilityImpl.java +++ b/src/main/java/com/dispose/ability/impl/DpTechBypassAbilityImpl.java @@ -1,17 +1,151 @@ package com.dispose.ability.impl; +import com.dispose.common.DisposeConfigValue; +import com.dispose.common.DpTechConfigValue; +import com.dispose.common.IpAddrType; +import com.dispose.pojo.dto.protocol.device.ability.DpProtectObject; +import com.dptech.dispose.ArrayOfProtectionObjectDataForService; +import com.dptech.dispose.NtcRequestResultInfo; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Hashtable; + /** * The type Dp tech bypass ability. * * @author */ +@Slf4j public class DpTechBypassAbilityImpl extends DpTechAbilityImpl { + /** + * The Obj prefix. + */ + private final String OBJ_PREFIX = "CMHI"; + /** + * The All cleanup devices. + */ + private final HashSet allCleanupDevices = new HashSet<>(); + + /** + * The Umc object. + */ + private final Hashtable umcObject = new Hashtable<>(); + /** + * The Timer cnt. + */ + private long timerCnt = 0; + + /** + * Init cleanup devices. + */ + private void initCleanupDevices() { + try { + log.info("++++Begging DPTech Get All Protection Devices"); + String dev = getCleanTypePort().getAllProtectDevices(); + log.info("----Finish DPTech Get All Protection Devices: {}", dev); + + Collections.addAll(allCleanupDevices, dev.split(DisposeConfigValue.SPLIT_CHAR)); + } catch (Exception ex) { + log.error("----Exception DPTech Get All Protection Devices: {}", ex.getMessage()); + } + } + + /** + * 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) { + super.initDeviceEnv(urlPath, username, password); + + // 获取所有检测设备 + initCleanupDevices(); + } + + /** + * Gets dispose device protect object. + * 防护对象命名规则 前缀_对象唯一名称_IP地址类型 + * CMHI_xxxx_V4 + * CMHI_XXXX_V6 + */ + @Override + public void getDisposeDeviceProtectObject() { + try { + log.info("++++Begging DPTech Get All Protection Objects"); + ArrayOfProtectionObjectDataForService ret = getCleanTypePort().getAllProtectionObjectFromUMC(); + + if (ret == null) { + log.error("----Finish DPTech Get All Protection Objects error"); + return; + } + + ret.getProtectionObjectDataForService().forEach(k -> { + String objName = k.getProtectionName().getValue(); + // 删除非法的防护对象 + if (!objName.startsWith(OBJ_PREFIX)) { + log.error("!!!!Found Unexpect Protection Object [{}, {}], Deleted it.", + objName, + k.getIpSegment().getValue()); + + NtcRequestResultInfo rsp = getCleanTypePort().deleteProtectionObjectForUMC(objName); + + if (rsp.getResultRetVal() == 0) { + log.warn("!!!!Remove Protection Object {} Succeed", objName); + } else { + log.error("!!!!Remove Protection Object {} Error: {}", objName, rsp.getResultInfo().getValue()); + } + } else { + // 缓存所有防护对象 + DpProtectObject obj = DpProtectObject.builder() + .cleanupDevices(k.getCleaningDevices().getValue()) + .protectName(objName) + .detectionDevices(k.getDetectionDevices().getValue()) + .ipType(k.getIpType() == 0 ? IpAddrType.IPV4 : IpAddrType.IPV6) + .ipSegment(new Hashtable<>()) + .build(); + + String ipSeg = k.getIpSegment().getValue(); + log.debug("DpTech response type: {}, value: {}", k.getIpType(), ipSeg); + // 分割IP段 + Arrays.stream(StringUtils.deleteWhitespace(ipSeg).split(",")).forEach(v -> { + String ipValue = v.replaceAll("\\d+_", ""); + String key = v.replace(ipValue, "").replace("_", ""); + // 添加到缓存 + obj.getIpSegment().put(key, ipValue); + }); + + // 添加到缓存 + umcObject.put(objName, obj); + } + }); + + log.info("----Finish DPTech Get All Protection Objects: {}", ret.getProtectionObjectDataForService() + .size()); + } catch (Exception ex) { + log.error("----Exception DPTech Get All Protection Objects: {}", ex.getMessage()); + } + } + /** * Period task runtime. */ @Override public void periodTaskRuntime() { - super.periodTaskRuntime(); + synchronized (this) { + super.periodTaskRuntime(); + + if (timerCnt++ % DpTechConfigValue.PROTECTION_UPGRADE_PERIOD == 0) { + // 定时检测防护对象 + getDisposeDeviceProtectObject(); + } + } } } diff --git a/src/main/java/com/dispose/common/DpTechConfigValue.java b/src/main/java/com/dispose/common/DpTechConfigValue.java index 18c297a4..14a45166 100644 --- a/src/main/java/com/dispose/common/DpTechConfigValue.java +++ b/src/main/java/com/dispose/common/DpTechConfigValue.java @@ -21,4 +21,9 @@ public class DpTechConfigValue { * The constant SOAP_RECEIVE_TIMEOUT_SECOND. */ public static volatile long SOAP_RECEIVE_TIMEOUT_SECOND = 60; + + /** + * The Protection upgrade period. + */ + public static final long PROTECTION_UPGRADE_PERIOD = 10; } diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/ability/DpProtectObject.java b/src/main/java/com/dispose/pojo/dto/protocol/device/ability/DpProtectObject.java index 8f1bce47..018eee75 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/device/ability/DpProtectObject.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/ability/DpProtectObject.java @@ -1,6 +1,8 @@ package com.dispose.pojo.dto.protocol.device.ability; +import com.dispose.common.IpAddrType; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @@ -14,6 +16,7 @@ import java.util.Hashtable; @Data @NoArgsConstructor @AllArgsConstructor +@Builder public class DpProtectObject { /** * The Protect name. @@ -31,7 +34,12 @@ public class DpProtectObject { private String cleanupDevices; /** - * The Ip segment v 4. + * The Ip type. + */ + private IpAddrType ipType; + + /** + * The Ip segment. */ private Hashtable ipSegment; } diff --git a/src/test/java/com/dispose/test/dev/debug/demo.java b/src/test/java/com/dispose/test/dev/debug/demo.java index 3461517f..1f251c2c 100644 --- a/src/test/java/com/dispose/test/dev/debug/demo.java +++ b/src/test/java/com/dispose/test/dev/debug/demo.java @@ -607,4 +607,22 @@ public class demo { @Test public void codeDebug() { } + + @Test + public void dpIpSegmentTest() { + String ipSegmentString = "1_192.168.10.1-192.168.10.12,2_192.168.1.2-192.168.1.20,3_-"; + + Arrays.stream(ipSegmentString.split(",")).forEach( + k -> { + String value = k.replaceAll("\\d+_", ""); + String key = k.replace(value, "").replace("_", ""); + + if (Pattern.matches(ConstValue.ipAddrSegmentReg(), value)) { + log.info("Result: {}, {}", key, value); + } else { + log.error("Result: {}, {} error", key, value); + } + } + ); + } }