From 99d8f668be0b541781f71ccb47c45784981634d8 Mon Sep 17 00:00:00 2001 From: chenlinghy Date: Tue, 14 Sep 2021 15:08:12 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E4=BC=98=E5=8C=96controller?= =?UTF-8?q?=E5=B1=82=E4=BB=A3=E7=A0=81=202.=20=E4=BC=98=E5=8C=96=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E5=AD=98=E5=82=A8MsgSerial=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E5=92=8C=E8=8E=B7=E5=8F=96=E6=9C=80=E5=A4=A7MsgSerial=E5=80=BC?= =?UTF-8?q?=E6=96=B9=E6=B3=95=204.=20=E4=BF=AE=E6=94=B9=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=85=A5=E5=8F=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/dispose/common/Constants.java | 24 +++- .../dispose/controller/kafkaController.java | 136 ++++++++++-------- .../com/dispose/manager/MsgSerialManager.java | 4 +- .../manager/impl/MsgSerialManagerImpl.java | 16 ++- .../com/dispose/service/MsgSerialService.java | 5 +- .../service/impl/MsgSerialServiceImpl.java | 20 ++- .../dev/controller/KafkaControllerTest.java | 6 +- 7 files changed, 137 insertions(+), 74 deletions(-) diff --git a/src/main/java/com/dispose/common/Constants.java b/src/main/java/com/dispose/common/Constants.java index 29c7272f..39f1a00b 100644 --- a/src/main/java/com/dispose/common/Constants.java +++ b/src/main/java/com/dispose/common/Constants.java @@ -6,6 +6,8 @@ import java.util.Map; /** * The emos constants. + * + * @author */ public class Constants { /** @@ -62,6 +64,22 @@ public class Constants { } /** + * 各省份城市 + */ + public static final String REGION_BEIJING = "北京"; + public static final String REGION_SHANGHAI = "上海"; + public static final String REGION_TIANJIN = "天津"; + public static final String REGION_CHONGQING = "重庆"; + + /** + * 处置类型(1:清洗,2:黑洞,3:高防) + */ + public static final int CLEANUP = 1; + public static final int BLACKHOOL = 2; + public static final int HIDEPEND = 3; + + /** + * /** * 派单eoms模板 */ public static final String DISPATCH_TEMPLATE = "\nMsgSerial:{0}\nSDN:{1}\nNeName:{2}\nEquipmentClass:99236\n" + @@ -71,9 +89,9 @@ public class Constants { "HolderType: \nAlarmStatus:{15}\nCorrelateAlarmFlag: \nAlarmActCount: \nNeIp:\nEmsId: \nVendor:99083\nAlarmText:{16}\n" + "NeAlias: \nVersion: \nRemoteNe: \nAlarmProvince:{17}\nAlarmRegion:{18}\nAlarmCounty: \nSite: \nSiteType: \nSiteProperty: \n" + "MachineroomIDofZGTT: \nBusinessSystem:{19}\nCircuitNo: \nMac: \nSpecialty:9\nNetworkType:903\nNeSubType: \nEffectCircuitNum: \n" + - "CircuitLevel: \nAlarmSeverity:3\nNmsAlarmId:0903-083-056-10-900001\nStandardAlarmName:{21}\nAlarmLogicClass:{22}\n" + - "AlarmLogicSubClass:{23}\nEffectOnEquipment:5\nEffectOnBusiness:4\nNmsAlarmType:1\nSendGroupFlag: \nStandardFlag:2\n" + - "AlarmExplanation:{24}\nBusinessType: \nBusinessInfo:{25}\nIsRelatedRemote: \nLocateNeStatus:1300\nProjectNo: \n" + + "CircuitLevel: \nAlarmSeverity:3\nNmsAlarmId:0903-083-056-10-900001\nStandardAlarmName:{20}\nAlarmLogicClass:{21}\n" + + "AlarmLogicSubClass:{22}\nEffectOnEquipment:5\nEffectOnBusiness:4\nNmsAlarmType:1\nSendGroupFlag: \nStandardFlag:2\n" + + "AlarmExplanation:{23}\nBusinessType: \nBusinessInfo:{24}\nIsRelatedRemote: \nLocateNeStatus:1300\nProjectNo: \n" + "ProjectName: \nProjectStartTime: \nProjectEndTime: \nGroupCustomer: \nCustomerLevel: \nServiceType: \nServiceLevel: \n" + "ServiceName: \nServiceCrossDomainType: \nInterruptCircuitState: \nCircuitLocateInfo: \nHomeClientNum: \nHomeCellNum: \n" + "LinkOnuNum: \n"; diff --git a/src/main/java/com/dispose/controller/kafkaController.java b/src/main/java/com/dispose/controller/kafkaController.java index 7923c019..a955473f 100644 --- a/src/main/java/com/dispose/controller/kafkaController.java +++ b/src/main/java/com/dispose/controller/kafkaController.java @@ -10,6 +10,7 @@ import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO; import com.dispose.pojo.dto.protocol.kafka.EmosAlarmInfo; import com.dispose.pojo.dto.protocol.kafka.AlarmInfoReq; import com.dispose.pojo.entity.MsgSerial; +import com.dispose.pojo.po.MulReturnType; import com.dispose.security.annotation.Decryption; import com.dispose.security.annotation.Encryption; import com.dispose.service.MsgSerialService; @@ -35,7 +36,7 @@ import java.util.Objects; /** * The type Auth controller. * - * @author + * @author */ @Controller @RequestMapping(value = "/kafka") @@ -108,18 +109,15 @@ public class kafkaController { */ private String createSendContent(EmosAlarmInfo alarmInfo) { try { - //告警序号的最大值2^32-1 - long indexEnd = 4294967296L; - //告警序号的最大值 - long indexStart = 1L; - //编号从1开始,以实时消息发布通道为单位进行编号。如果编号超过最大正整数(2^32-1),重新从1开始编号。 - long increment = msgSerialService.getMaxMessageSerial(); - increment = increment + 1; - if (increment > (indexEnd - 1)) { - increment = indexStart; - } + long dbIncrement = msgSerialService.getMaxMessageSerial(); + long increment = dbIncrement + 1; MsgSerial msgSerial = MsgSerial.builder().msgSerial(increment).build(); - msgSerialService.addMessageSerial(msgSerial); + MulReturnType returnType = msgSerialService.addMessageSerial(msgSerial); + if (returnType.getFirstParam() == ErrorCode.ERR_OK) { + increment = returnType.getSecondParam(); + } else { + increment = dbIncrement; + } String dstIp = alarmInfo.getDstIp(); String alarmId = alarmInfo.getAlarmId(); @@ -130,7 +128,7 @@ public class kafkaController { //告警类型vendorAlarmType、告警级别vendorSeverity、告警号vendorAlarmId、告警标题AlarmTitle、告警可能原因ProbableCauseTxt String type = Constants.ATTACK_TYPE.get(alarmInfo.getAttackType()); - String vendorSeverity = changeCharacatorCode("一级"); + String vendorSeverity = characterEncode("一级"); String vendorAlarmType; String vendorAlarmId; if (type != null) { @@ -144,87 +142,103 @@ public class kafkaController { } else { vendorAlarmType = alarmInfo.getAttackType(); vendorAlarmId = "0"; - log.info("get unKnow attack type:" + vendorAlarmType); + log.info("unKnown attack type:" + vendorAlarmType); } String alarmStatus = String.valueOf(Constants.ACTIVE_ALARM_STATUS); - String alarmText = changeCharacatorCode(getAlarmText(alarmInfo)); - String alarmExplanation = changeCharacatorCode(getAlarmExplanation(alarmInfo)); + String alarmText = characterEncode(getAlarmText(alarmInfo)); + String alarmExplanation = characterEncode(getAlarmExplanation(alarmInfo)); String content = MessageFormat.format(Constants.DISPATCH_TEMPLATE, increment, dstIp, dstIp, alarmId, alarmId, dstIp, dstIp, locateInfo, eventTime, cancelTime, - vendorAlarmType, vendorSeverity, vendorAlarmId, changeCharacatorCode("重保攻击事件告警"), - changeCharacatorCode("DDos攻击事件"), alarmStatus, alarmText, - changeCharacatorCode(alarmInfo.getDstProvince()), changeCharacatorCode(alarmInfo.getDstCity()), - changeCharacatorCode("网络部集中抗D系统"), changeCharacatorCode("DDOS攻击事件告警"), - changeCharacatorCode("安全告警"), changeCharacatorCode("DDOS告警"), - alarmExplanation, changeCharacatorCode("集中抗D")); + vendorAlarmType, vendorSeverity, vendorAlarmId, characterEncode("重保攻击事件告警"), + characterEncode("DDos攻击事件"), alarmStatus, alarmText, + characterEncode(alarmInfo.getDstProvince()), characterEncode(alarmInfo.getDstCity()), + characterEncode("网络部集中抗D系统"), characterEncode("DDOS攻击事件告警"), + characterEncode("安全告警"), characterEncode("DDOS告警"), + alarmExplanation, characterEncode("集中抗D")); return content; - } catch (Exception e) { log.error("createSendContent告警消息异常,详细信息:{}", ExceptionUtils.getStackTrace(e)); return null; } } + /** + * 获取告警事件AlarmEvent + */ private String getAlarmEvent(EmosAlarmInfo a) { return a.getDstIp() + " ddos attack alarm, " + a.getAttackType() + ", " + a.getStartTime() + ", " + a.getMaxBps() + ", " + a.getMaxPps(); } - private String changeCharacatorCode(String con) { + /** + * 数据编码都采用GBK编码方式 + */ + private String characterEncode(String character) { try { - return new String(con.getBytes("GBK"), "GBK"); + return new String(character.getBytes("GBK"), "GBK"); } catch (Exception e) { - log.info("change failed:" + e.getMessage()); - return con; + log.info("character encoding failed:" + e.getMessage()); + return character; } } + /** + * 获取告警区域 + */ private String getAreaDes(String province, String city) { - if (province.compareTo("北京") == 0) { - return "北京市"; + String areaDes; + if (Constants.REGION_BEIJING.equals(province)) { + areaDes = "北京市"; + } else if (Constants.REGION_SHANGHAI.equals(province)) { + areaDes = "上海市"; + } else if (Constants.REGION_TIANJIN.equals(province)) { + areaDes = "天津市"; + } else if (Constants.REGION_CHONGQING.equals(province)) { + areaDes = "重庆市"; + } else { + areaDes = province + "省" + city + "市"; } - if (province.compareTo("上海") == 0) { - return "上海市"; - } - if (province.compareTo("天津") == 0) { - return "天津市"; - } - if (province.compareTo("重庆") == 0) { - return "重庆市"; - } - return province + "省" + city + "市"; + + return areaDes; } + /** + * 获取处置类型(1:清洗,2:黑洞,3:高防) + */ + private String getOperateType(Integer disposeType) { + String operateType = null; + if (Constants.CLEANUP == disposeType) { + operateType = "清洗"; + } else if (Constants.BLACKHOOL == disposeType) { + operateType = "流控"; + } else if (Constants.HIDEPEND == disposeType) { + operateType = "黑洞"; + } + return operateType; + } + + /** + * 获取告警正文AlarmText + */ private String getAlarmText(EmosAlarmInfo a) { String area = getAreaDes(a.getDstProvince(), a.getDstCity()); - String op = ""; - if (a.getDisposeType() == 1) { - op = "清洗"; - } else if (a.getDisposeType() == 2) { - op = "流控"; - } else if (a.getDisposeType() == 3) { - op = "黑洞"; - } - return "攻击目的IP:" + a.getDstIp() + "," + area + "," + "处置操作:" + op + "," + "处置时长:" + a.getDisposeTime() + "分钟"; + String operateType = getOperateType(a.getDisposeType()); + return "攻击目的IP:" + a.getDstIp() + "," + area + "," + "处置操作:" + operateType + "," + "处置时长:" + a.getDisposeTime() + "分钟"; } + /** + * 获取告警解释AlarmExplanation + */ private String getAlarmExplanation(EmosAlarmInfo a) { - String op = ""; - if (a.getDisposeType() == 1) { - op = "清洗"; - } else if (a.getDisposeType() == 2) { - op = "流控"; - } else if (a.getDisposeType() == 3) { - op = "黑洞"; - } - String srcIp = ""; + String operateType = getOperateType(a.getDisposeType()); + StringBuilder srcIp = new StringBuilder(); for (String ip : a.getSrcIpLs()) { - srcIp = srcIp + ip + ","; + srcIp.append(ip).append(","); } - if (!srcIp.isEmpty()) { - srcIp = srcIp.substring(0, srcIp.length() - 1); + if (srcIp.length() > 0) { + srcIp = new StringBuilder(srcIp.substring(0, srcIp.length() - 1)); } - return "攻击目的IP:" + a.getDstIp() + "," + "攻击源地址:(" + srcIp + ")," + "处置操作:" + op + "," + "处置时长:" + a.getDisposeTime() + "分钟"; + return "攻击目的IP:" + a.getDstIp() + "," + "攻击源地址:(" + srcIp + ")," + "处置操作:" + operateType + "," + "处置时长:" + a.getDisposeTime() + "分钟"; } } diff --git a/src/main/java/com/dispose/manager/MsgSerialManager.java b/src/main/java/com/dispose/manager/MsgSerialManager.java index b279c41b..bebda584 100644 --- a/src/main/java/com/dispose/manager/MsgSerialManager.java +++ b/src/main/java/com/dispose/manager/MsgSerialManager.java @@ -1,5 +1,6 @@ package com.dispose.manager; +import com.dispose.common.ErrorCode; import com.dispose.pojo.entity.MsgSerial; public interface MsgSerialManager { @@ -7,8 +8,9 @@ public interface MsgSerialManager { * Add user business error code. * * @param msgSerial the message serial + * @return the error code */ - void addMsgSerialNumber(MsgSerial msgSerial); + ErrorCode addMsgSerialNumber(MsgSerial msgSerial); /** * get new max message serial. diff --git a/src/main/java/com/dispose/manager/impl/MsgSerialManagerImpl.java b/src/main/java/com/dispose/manager/impl/MsgSerialManagerImpl.java index 1e4a787a..ab26b187 100644 --- a/src/main/java/com/dispose/manager/impl/MsgSerialManagerImpl.java +++ b/src/main/java/com/dispose/manager/impl/MsgSerialManagerImpl.java @@ -1,5 +1,6 @@ package com.dispose.manager.impl; +import com.dispose.common.ErrorCode; import com.dispose.manager.MsgSerialManager; import com.dispose.mapper.MsgSerialMapper; import com.dispose.pojo.entity.MsgSerial; @@ -17,10 +18,19 @@ public class MsgSerialManagerImpl implements MsgSerialManager { @Resource private MsgSerialMapper msgSerialMapper; - + /** + * Add user business error code. + * + * @param msgSerial the message serial + * @return the error code + */ @Override - public void addMsgSerialNumber(MsgSerial msgSerial) { - msgSerialMapper.addMsgSerial(msgSerial); + public ErrorCode addMsgSerialNumber(MsgSerial msgSerial) { + if (msgSerialMapper.addMsgSerial(msgSerial) == 1) { + return ErrorCode.ERR_OK; + } else { + return ErrorCode.ERR_DATABASE; + } } /** diff --git a/src/main/java/com/dispose/service/MsgSerialService.java b/src/main/java/com/dispose/service/MsgSerialService.java index f36dee2b..6d6af602 100644 --- a/src/main/java/com/dispose/service/MsgSerialService.java +++ b/src/main/java/com/dispose/service/MsgSerialService.java @@ -1,14 +1,17 @@ package com.dispose.service; +import com.dispose.common.ErrorCode; import com.dispose.pojo.entity.MsgSerial; +import com.dispose.pojo.po.MulReturnType; public interface MsgSerialService { /** * add message serial. * * @param msgSerial the message serial + * @return the mul return type */ - void addMessageSerial(MsgSerial msgSerial); + MulReturnType addMessageSerial(MsgSerial msgSerial); /** * get new max message serial. diff --git a/src/main/java/com/dispose/service/impl/MsgSerialServiceImpl.java b/src/main/java/com/dispose/service/impl/MsgSerialServiceImpl.java index b3c19d2b..63be8481 100644 --- a/src/main/java/com/dispose/service/impl/MsgSerialServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/MsgSerialServiceImpl.java @@ -1,7 +1,9 @@ package com.dispose.service.impl; +import com.dispose.common.ErrorCode; import com.dispose.manager.MsgSerialManager; import com.dispose.pojo.entity.MsgSerial; +import com.dispose.pojo.po.MulReturnType; import com.dispose.service.MsgSerialService; import org.springframework.stereotype.Service; @@ -16,10 +18,24 @@ public class MsgSerialServiceImpl implements MsgSerialService { * add message serial. * * @param msgSerial the message serial + * @return the mul return type */ @Override - public void addMessageSerial(MsgSerial msgSerial) { - msgSerialManager.addMsgSerialNumber(msgSerial); + public MulReturnType addMessageSerial(MsgSerial msgSerial) { + //告警序号的最大值2^32-1 + long indexEnd = 4294967295L; + long indexStart = 1L; + long currentSerial = msgSerial.getMsgSerial(); + //编号从1开始,以实时消息发布通道为单位进行编号。如果编号超过最大正整数(2^32-1),重新从1开始编号。 + if (currentSerial > indexEnd) { + currentSerial = indexStart; + } + + if (msgSerialManager.addMsgSerialNumber(msgSerial) == ErrorCode.ERR_OK) { + return new MulReturnType<>(ErrorCode.ERR_OK, currentSerial); + } else { + return new MulReturnType<>(ErrorCode.ERR_DATABASE, null); + } } /** diff --git a/src/test/java/com/dispose/test/dev/controller/KafkaControllerTest.java b/src/test/java/com/dispose/test/dev/controller/KafkaControllerTest.java index 83a52d64..6521d0c3 100644 --- a/src/test/java/com/dispose/test/dev/controller/KafkaControllerTest.java +++ b/src/test/java/com/dispose/test/dev/controller/KafkaControllerTest.java @@ -75,10 +75,10 @@ public class KafkaControllerTest extends InitTestEnvironment { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); disposeParam.set("alarmId", "1"); disposeParam.set("dstIp", "192.168.1.1"); - disposeParam.set("attackType", "1"); + disposeParam.set("attackType", "RSTFlood"); disposeParam.set("bpspps", "bps"); - disposeParam.set("dstProvince", "ZHEJIANG"); - disposeParam.set("dstCity", "HANGZHOU"); + disposeParam.set("dstProvince", "浙江"); + disposeParam.set("dstCity", "杭州"); disposeParam.set("startTime", sdf.format(new Date())); // 1清洗,2流控,3黑洞 disposeParam.set("disposeType", 1);