From 39513c00a3b98030cd5eb3b8f55375742bc8f0dd Mon Sep 17 00:00:00 2001 From: HuangXin Date: Fri, 15 Jan 2021 17:09:05 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A2=9E=E5=8A=A0=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E5=AF=B9=E8=B1=A1=E5=92=8CUMC=E9=98=B2=E6=8A=A4?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E5=90=8C=E6=AD=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ability/impl/DpTechBypassAbilityImpl.java | 172 +++++++++++++----- .../java/com/dispose/test/dev/debug/demo.java | 21 +++ 2 files changed, 146 insertions(+), 47 deletions(-) diff --git a/src/main/java/com/dispose/ability/impl/DpTechBypassAbilityImpl.java b/src/main/java/com/dispose/ability/impl/DpTechBypassAbilityImpl.java index 9d29a734..a896bfc7 100644 --- a/src/main/java/com/dispose/ability/impl/DpTechBypassAbilityImpl.java +++ b/src/main/java/com/dispose/ability/impl/DpTechBypassAbilityImpl.java @@ -69,6 +69,18 @@ public class DpTechBypassAbilityImpl extends DpTechAbilityImpl { */ private long timerCnt = 0; + /** + * Upgrade device bypass info. + */ + private void upgradeDeviceBypassInfo() { + // 更新防护策略模板和防护对象关联关系 + getProtectionStrategyTemplateAssociation(); + // 更新防护对象 + getDisposeDeviceProtectObject(); + // 更新手动旁路牵引策略 + upgradeTractionStrategy(); + } + /** * Remove umc protect object error code. * @@ -78,12 +90,8 @@ public class DpTechBypassAbilityImpl extends DpTechAbilityImpl { private ErrorCode removeUMCProtectObject(String objName) { NtcRequestResultInfo ret; - // 更新防护策略模板和防护对象关联关系 - getProtectionStrategyTemplateAssociation(); - // 更新防护对象 - getDisposeDeviceProtectObject(); - // 更新手动旁路牵引策略 - upgradeTractionStrategy(); + // 修改前同步更新设备信息 + upgradeDeviceBypassInfo(); // 清理关联的旁路手动牵引策略 for (DpTractionStrategy v : dpBypassManager.getTractionStrategyName().values()) { @@ -138,17 +146,15 @@ public class DpTechBypassAbilityImpl extends DpTechAbilityImpl { } /** - * Create umc portect object error code. + * Create protect object error code. * * @param objName the obj name * @param ipSegment the ip segment * @param ipType the ip type - * @param type the type - * @param bandWidth the band width + * @param template the template * @return the error code */ - private ErrorCode createUMCPortectObject(String objName, String ipSegment, int ipType, String type, - Long bandWidth) { + private ErrorCode createProtectObject(String objName, String ipSegment, int ipType, String template) { // 创建对象 NtcRequestResultInfo ret = getCleanTypePort() .addProtectionObjectForUMC("", @@ -167,20 +173,10 @@ public class DpTechBypassAbilityImpl extends DpTechAbilityImpl { return ErrorCode.ERR_CALLDEVICE; } - // 关联模板 - String template = Helper.getDpTemplate(type, bandWidth); - - // 未找到合适的模板 - if (TemplateConfigure.UMC_TEMPLATE.stream().noneMatch(v -> v.getName().equals(template))) { - log.error("!!!!DpTech Can't Math Template: {}, {} From {}", type, bandWidth, - TemplateConfigure.UMC_TEMPLATE); - return ErrorCode.ERR_NOSUCHTYPE; - } - // UMC关联模板 - ret = getCleanTypePort().linkProtectionStrategyTemplateForUMC(objName, template); + ErrorCode err = associationProtectionTemplate(objName, template, null); - if (ret.getResultRetVal() == 0) { + if (err == ErrorCode.ERR_OK) { log.debug("{} Link Protection Strategy Template {} Succeed", objName, template); } else { log.error("!!!!{} Link Protection Strategy Template {} Error: {}", objName, template, @@ -188,6 +184,51 @@ public class DpTechBypassAbilityImpl extends DpTechAbilityImpl { return ErrorCode.ERR_CALLDEVICE; } + // 修改设备后同步更新信息 + upgradeDeviceBypassInfo(); + return ErrorCode.ERR_OK; + } + + /** + * Association protection template error code. + * + * @param objName the obj name + * @param newTemplate the new template + * @param oldTemplate the old template + * @return the error code + */ + private ErrorCode associationProtectionTemplate(String objName, String newTemplate, String oldTemplate) { + NtcRequestResultInfo ret; + + // 如果关联了旧的防护模板,先解除关联 + if (oldTemplate != null + && TemplateConfigure.UMC_TEMPLATE.stream().anyMatch(m -> m.getName().equals(oldTemplate))) { + ret = getCleanTypePort().disableProtectionStrategyTemplateForUMC(objName, oldTemplate); + + if (ret.getResultRetVal() == 0) { + log.debug("Disable Protection Strategy Template {} Succeed", objName); + } else { + log.error("!!!!Disable Protection Strategy Template {} Error: {}", objName, + ret.getResultInfo().getValue()); + return ErrorCode.ERR_CALLDEVICE; + } + + // 更新防护策略模板和防护对象关联关系 + getProtectionStrategyTemplateAssociation(); + return ErrorCode.ERR_OK; + } + + // UMC关联模板 + ret = getCleanTypePort().linkProtectionStrategyTemplateForUMC(objName, newTemplate); + + if (ret.getResultRetVal() == 0) { + log.debug("{} Link Protection Strategy Template {} Succeed", objName, newTemplate); + } else { + log.error("!!!!{} Link Protection Strategy Template {} Error: {}", objName, newTemplate, + ret.getResultInfo().getValue()); + return ErrorCode.ERR_CALLDEVICE; + } + return ErrorCode.ERR_OK; } @@ -196,12 +237,21 @@ public class DpTechBypassAbilityImpl extends DpTechAbilityImpl { */ private void syncDpTechDeviceInfo() { disposeServiceGroup.values().forEach(v -> { - boolean changed = false; - NtcRequestResultInfo ret; + ErrorCode err; List ipV4 = new ArrayList<>(); List ipV6 = new ArrayList<>(); int ipV4Idx = 1; int ipV6Idx = 1; + + String template = Helper.getDpTemplate(v.getServiceType(), v.getServiceBandwidth()); + + // 未找到合适的模板 + if (TemplateConfigure.UMC_TEMPLATE.stream().noneMatch(m -> m.getName().equals(template))) { + log.error("!!!!{} Can't Math Template: {}, {} From {}", v.getServiceId(), v.getServiceType(), + v.getServiceBandwidth(), TemplateConfigure.UMC_TEMPLATE); + return; + } + // 将业务Ip根据IP地址类型进行拆分 for (String k : StringUtils.deleteWhitespace(v.getServiceIp()).split(DisposeConfigValue.SPLIT_CHAR)) { if (!k.contains(":")) { @@ -229,33 +279,61 @@ public class DpTechBypassAbilityImpl extends DpTechAbilityImpl { if (dpBypassManager.getProtectObject().containsKey(protectName)) { DpProtectObject dp = dpBypassManager.getProtectObject().get(protectName); - - } else { - // 创建一个防护对象 - ret = getCleanTypePort().addProtectionObjectForUMC("", - allCleanupDevices, - protectName, - ipSegment.get(t), - IpAddrType.IPV4.equals(t) ? 0 : 1, - 0); - - if (ret.getResultRetVal() == 0) { - log.info("Add Protection Object {} Succeed", protectName); + // 判断关联模板是否正确 + if (dpBypassManager.getProtectStrategy().containsKey(protectName)) { + // 已经关联防护模板 + String temp = dpBypassManager.getProtectStrategy().get(protectName).getProtectStrategyName(); + // 如果关联模板有变更 + if (!temp.equals(template)) { + // 重新关联防护模板 + associationProtectionTemplate(protectName, template, temp); + } } else { - log.error("!!!!Add Protection Object {} Error: {}", protectName, ret.getResultInfo() - .getValue()); + // 关联一个新的防护模板 + associationProtectionTemplate(protectName, template, null); } - changed = true; + boolean upgradeIpSegment = false; + + // 判断防护IP段是否有变化 + if (IpAddrType.IPV4.equals(t)) { + if (ipV4.size() != dp.getIpSegment().values().size() + || !ipV4.containsAll(dp.getIpSegment().values()) + || dp.getIpSegment().values().containsAll(ipV4)) { + upgradeIpSegment = true; + } + } else if (IpAddrType.IPV6.equals(t)) { + if (ipV6.size() != dp.getIpSegment().values().size() + || !ipV4.containsAll(dp.getIpSegment().values()) + || dp.getIpSegment().values().containsAll(ipV6)) { + upgradeIpSegment = true; + } + } + + if (upgradeIpSegment) { + removeUMCProtectObject(protectName); + err = createProtectObject(protectName, + ipSegment.get(t), + IpAddrType.IPV4.equals(t) ? 0 : 1, + template); + if (err == ErrorCode.ERR_OK) { + log.debug("Add Protection Object {} Succeed", protectName); + } else { + log.error("!!!!Add Protection Object {} Error: {}", protectName, err.getMsg()); + } + } + } else { + err = createProtectObject(protectName, + ipSegment.get(t), + IpAddrType.IPV4.equals(t) ? 0 : 1, + template); + if (err == ErrorCode.ERR_OK) { + log.debug("Add Protection Object {} Succeed", protectName); + } else { + log.error("!!!!Add Protection Object {} Error: {}", protectName, err.getMsg()); + } } } - - // 修改后重新从设备更新信息 - if (changed) { - getDisposeDeviceProtectObject(); - changed = false; - } - }); } 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 f6cdd96e..c4d42a7a 100644 --- a/src/test/java/com/dispose/test/dev/debug/demo.java +++ b/src/test/java/com/dispose/test/dev/debug/demo.java @@ -28,6 +28,7 @@ import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.regex.Matcher; @@ -637,12 +638,32 @@ public class demo { public void listToStringWithCharTest() { List demoList = new ArrayList<>(); List demoList2 = new ArrayList<>(); + Hashtable tmpTable = new Hashtable<>(); + Hashtable tmpTable2 = new Hashtable<>(); demoList.add("192.168.0.1"); demoList.add("192.168.0.2"); demoList.add("192.168.0.24/24"); + demoList2.add("192.168.0.2"); + demoList2.add("192.168.0.1"); + demoList2.add("192.168.0.24/24"); + //demoList2.add("192.168.0.24/23"); + + tmpTable.put("1", "192.168.0.1"); + tmpTable.put("2", "192.168.0.2"); + tmpTable.put("3", "192.168.0.3"); + + tmpTable2.put("2", "192.168.0.2"); + tmpTable2.put("1", "192.168.0.1"); + tmpTable2.put("3", "192.168.0.3"); + log.info("Result demoList: {}", String.join(",", demoList)); log.info("Result demoList: {}", String.join(",", demoList2)); + + log.info("demoList difference demoList2: {}", demoList2.containsAll(demoList)); + log.info("demoList2 difference demoList1: {}", demoList.containsAll(demoList2)); + log.info("demoList2 difference demoList1: {}", demoList.equals(demoList2)); + log.info("tmpTable difference tmpTable: {}", tmpTable.equals(tmpTable2)); } }