REM:
1. 优化controller层代码
2. 优化数据库存储MsgSerial字段和获取最大MsgSerial值方法
4. 修改单元测试入参
This commit is contained in:
chenlinghy 2021-09-14 15:08:12 +08:00
parent 39531233a2
commit 99d8f668be
7 changed files with 137 additions and 74 deletions

View File

@ -6,6 +6,8 @@ import java.util.Map;
/** /**
* The emos constants. * The emos constants.
*
* @author <chenling@cmhi.chinamoblie.com>
*/ */
public class Constants { 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模板 * 派单eoms模板
*/ */
public static final String DISPATCH_TEMPLATE = "<AlarmStart>\nMsgSerial:{0}\nSDN:{1}\nNeName:{2}\nEquipmentClass:99236\n" + public static final String DISPATCH_TEMPLATE = "<AlarmStart>\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" + "HolderType: \nAlarmStatus:{15}\nCorrelateAlarmFlag: \nAlarmActCount: \nNeIp:\nEmsId: \nVendor:99083\nAlarmText:{16}\n" +
"NeAlias: \nVersion: \nRemoteNe: \nAlarmProvince:{17}\nAlarmRegion:{18}\nAlarmCounty: \nSite: \nSiteType: \nSiteProperty: \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" + "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" + "CircuitLevel: \nAlarmSeverity:3\nNmsAlarmId:0903-083-056-10-900001\nStandardAlarmName:{20}\nAlarmLogicClass:{21}\n" +
"AlarmLogicSubClass:{23}\nEffectOnEquipment:5\nEffectOnBusiness:4\nNmsAlarmType:1\nSendGroupFlag: \nStandardFlag:2\n" + "AlarmLogicSubClass:{22}\nEffectOnEquipment:5\nEffectOnBusiness:4\nNmsAlarmType:1\nSendGroupFlag: \nStandardFlag:2\n" +
"AlarmExplanation:{24}\nBusinessType: \nBusinessInfo:{25}\nIsRelatedRemote: \nLocateNeStatus:1300\nProjectNo: \n" + "AlarmExplanation:{23}\nBusinessType: \nBusinessInfo:{24}\nIsRelatedRemote: \nLocateNeStatus:1300\nProjectNo: \n" +
"ProjectName: \nProjectStartTime: \nProjectEndTime: \nGroupCustomer: \nCustomerLevel: \nServiceType: \nServiceLevel: \n" + "ProjectName: \nProjectStartTime: \nProjectEndTime: \nGroupCustomer: \nCustomerLevel: \nServiceType: \nServiceLevel: \n" +
"ServiceName: \nServiceCrossDomainType: \nInterruptCircuitState: \nCircuitLocateInfo: \nHomeClientNum: \nHomeCellNum: \n" + "ServiceName: \nServiceCrossDomainType: \nInterruptCircuitState: \nCircuitLocateInfo: \nHomeClientNum: \nHomeCellNum: \n" +
"LinkOnuNum: \n<AlarmEnd>"; "LinkOnuNum: \n<AlarmEnd>";

View File

@ -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.EmosAlarmInfo;
import com.dispose.pojo.dto.protocol.kafka.AlarmInfoReq; import com.dispose.pojo.dto.protocol.kafka.AlarmInfoReq;
import com.dispose.pojo.entity.MsgSerial; import com.dispose.pojo.entity.MsgSerial;
import com.dispose.pojo.po.MulReturnType;
import com.dispose.security.annotation.Decryption; import com.dispose.security.annotation.Decryption;
import com.dispose.security.annotation.Encryption; import com.dispose.security.annotation.Encryption;
import com.dispose.service.MsgSerialService; import com.dispose.service.MsgSerialService;
@ -35,7 +36,7 @@ import java.util.Objects;
/** /**
* The type Auth controller. * The type Auth controller.
* *
* @author <huangxin@cmhi.chinamoblie.com> * @author <chenling@cmhi.chinamoblie.com>
*/ */
@Controller @Controller
@RequestMapping(value = "/kafka") @RequestMapping(value = "/kafka")
@ -108,18 +109,15 @@ public class kafkaController {
*/ */
private String createSendContent(EmosAlarmInfo alarmInfo) { private String createSendContent(EmosAlarmInfo alarmInfo) {
try { try {
//告警序号的最大值2^32-1 long dbIncrement = msgSerialService.getMaxMessageSerial();
long indexEnd = 4294967296L; long increment = dbIncrement + 1;
//告警序号的最大值
long indexStart = 1L;
//编号从1开始以实时消息发布通道为单位进行编号如果编号超过最大正整数(2^32-1)重新从1开始编号
long increment = msgSerialService.getMaxMessageSerial();
increment = increment + 1;
if (increment > (indexEnd - 1)) {
increment = indexStart;
}
MsgSerial msgSerial = MsgSerial.builder().msgSerial(increment).build(); MsgSerial msgSerial = MsgSerial.builder().msgSerial(increment).build();
msgSerialService.addMessageSerial(msgSerial); MulReturnType<ErrorCode, Long> returnType = msgSerialService.addMessageSerial(msgSerial);
if (returnType.getFirstParam() == ErrorCode.ERR_OK) {
increment = returnType.getSecondParam();
} else {
increment = dbIncrement;
}
String dstIp = alarmInfo.getDstIp(); String dstIp = alarmInfo.getDstIp();
String alarmId = alarmInfo.getAlarmId(); String alarmId = alarmInfo.getAlarmId();
@ -130,7 +128,7 @@ public class kafkaController {
//告警类型vendorAlarmType告警级别vendorSeverity告警号vendorAlarmId告警标题AlarmTitle告警可能原因ProbableCauseTxt //告警类型vendorAlarmType告警级别vendorSeverity告警号vendorAlarmId告警标题AlarmTitle告警可能原因ProbableCauseTxt
String type = Constants.ATTACK_TYPE.get(alarmInfo.getAttackType()); String type = Constants.ATTACK_TYPE.get(alarmInfo.getAttackType());
String vendorSeverity = changeCharacatorCode("一级"); String vendorSeverity = characterEncode("一级");
String vendorAlarmType; String vendorAlarmType;
String vendorAlarmId; String vendorAlarmId;
if (type != null) { if (type != null) {
@ -144,87 +142,103 @@ public class kafkaController {
} else { } else {
vendorAlarmType = alarmInfo.getAttackType(); vendorAlarmType = alarmInfo.getAttackType();
vendorAlarmId = "0"; vendorAlarmId = "0";
log.info("get unKnow attack type:" + vendorAlarmType); log.info("unKnown attack type:" + vendorAlarmType);
} }
String alarmStatus = String.valueOf(Constants.ACTIVE_ALARM_STATUS); String alarmStatus = String.valueOf(Constants.ACTIVE_ALARM_STATUS);
String alarmText = changeCharacatorCode(getAlarmText(alarmInfo)); String alarmText = characterEncode(getAlarmText(alarmInfo));
String alarmExplanation = changeCharacatorCode(getAlarmExplanation(alarmInfo)); String alarmExplanation = characterEncode(getAlarmExplanation(alarmInfo));
String content = MessageFormat.format(Constants.DISPATCH_TEMPLATE, increment, dstIp, String content = MessageFormat.format(Constants.DISPATCH_TEMPLATE, increment, dstIp,
dstIp, alarmId, alarmId, dstIp, dstIp, locateInfo, eventTime, cancelTime, dstIp, alarmId, alarmId, dstIp, dstIp, locateInfo, eventTime, cancelTime,
vendorAlarmType, vendorSeverity, vendorAlarmId, changeCharacatorCode("重保攻击事件告警"), vendorAlarmType, vendorSeverity, vendorAlarmId, characterEncode("重保攻击事件告警"),
changeCharacatorCode("DDos攻击事件"), alarmStatus, alarmText, characterEncode("DDos攻击事件"), alarmStatus, alarmText,
changeCharacatorCode(alarmInfo.getDstProvince()), changeCharacatorCode(alarmInfo.getDstCity()), characterEncode(alarmInfo.getDstProvince()), characterEncode(alarmInfo.getDstCity()),
changeCharacatorCode("网络部集中抗D系统"), changeCharacatorCode("DDOS攻击事件告警"), characterEncode("网络部集中抗D系统"), characterEncode("DDOS攻击事件告警"),
changeCharacatorCode("安全告警"), changeCharacatorCode("DDOS告警"), characterEncode("安全告警"), characterEncode("DDOS告警"),
alarmExplanation, changeCharacatorCode("集中抗D")); alarmExplanation, characterEncode("集中抗D"));
return content; return content;
} catch (Exception e) { } catch (Exception e) {
log.error("createSendContent告警消息异常详细信息{}", ExceptionUtils.getStackTrace(e)); log.error("createSendContent告警消息异常详细信息{}", ExceptionUtils.getStackTrace(e));
return null; return null;
} }
} }
/**
* 获取告警事件AlarmEvent
*/
private String getAlarmEvent(EmosAlarmInfo a) { private String getAlarmEvent(EmosAlarmInfo a) {
return a.getDstIp() + " ddos attack alarm, " + a.getAttackType() + ", " + a.getStartTime() + return a.getDstIp() + " ddos attack alarm, " + a.getAttackType() + ", " + a.getStartTime() +
", " + a.getMaxBps() + ", " + a.getMaxPps(); ", " + a.getMaxBps() + ", " + a.getMaxPps();
} }
private String changeCharacatorCode(String con) { /**
* 数据编码都采用GBK编码方式
*/
private String characterEncode(String character) {
try { try {
return new String(con.getBytes("GBK"), "GBK"); return new String(character.getBytes("GBK"), "GBK");
} catch (Exception e) { } catch (Exception e) {
log.info("change failed:" + e.getMessage()); log.info("character encoding failed:" + e.getMessage());
return con; return character;
} }
} }
/**
* 获取告警区域
*/
private String getAreaDes(String province, String city) { private String getAreaDes(String province, String city) {
if (province.compareTo("北京") == 0) { String areaDes;
return "北京市"; 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 "上海市"; return areaDes;
}
if (province.compareTo("天津") == 0) {
return "天津市";
}
if (province.compareTo("重庆") == 0) {
return "重庆市";
}
return province + "" + city + "";
} }
/**
* 获取处置类型(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) { private String getAlarmText(EmosAlarmInfo a) {
String area = getAreaDes(a.getDstProvince(), a.getDstCity()); String area = getAreaDes(a.getDstProvince(), a.getDstCity());
String op = ""; String operateType = getOperateType(a.getDisposeType());
if (a.getDisposeType() == 1) { return "攻击目的IP" + a.getDstIp() + "," + area + "," + "处置操作:" + operateType + "," + "处置时长:" + a.getDisposeTime() + "分钟";
op = "清洗";
} else if (a.getDisposeType() == 2) {
op = "流控";
} else if (a.getDisposeType() == 3) {
op = "黑洞";
}
return "攻击目的IP" + a.getDstIp() + "," + area + "," + "处置操作:" + op + "," + "处置时长:" + a.getDisposeTime() + "分钟";
} }
/**
* 获取告警解释AlarmExplanation
*/
private String getAlarmExplanation(EmosAlarmInfo a) { private String getAlarmExplanation(EmosAlarmInfo a) {
String op = ""; String operateType = getOperateType(a.getDisposeType());
if (a.getDisposeType() == 1) { StringBuilder srcIp = new StringBuilder();
op = "清洗";
} else if (a.getDisposeType() == 2) {
op = "流控";
} else if (a.getDisposeType() == 3) {
op = "黑洞";
}
String srcIp = "";
for (String ip : a.getSrcIpLs()) { for (String ip : a.getSrcIpLs()) {
srcIp = srcIp + ip + ","; srcIp.append(ip).append(",");
} }
if (!srcIp.isEmpty()) { if (srcIp.length() > 0) {
srcIp = srcIp.substring(0, srcIp.length() - 1); 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() + "分钟";
} }
} }

View File

@ -1,5 +1,6 @@
package com.dispose.manager; package com.dispose.manager;
import com.dispose.common.ErrorCode;
import com.dispose.pojo.entity.MsgSerial; import com.dispose.pojo.entity.MsgSerial;
public interface MsgSerialManager { public interface MsgSerialManager {
@ -7,8 +8,9 @@ public interface MsgSerialManager {
* Add user business error code. * Add user business error code.
* *
* @param msgSerial the message serial * @param msgSerial the message serial
* @return the error code
*/ */
void addMsgSerialNumber(MsgSerial msgSerial); ErrorCode addMsgSerialNumber(MsgSerial msgSerial);
/** /**
* get new max message serial. * get new max message serial.

View File

@ -1,5 +1,6 @@
package com.dispose.manager.impl; package com.dispose.manager.impl;
import com.dispose.common.ErrorCode;
import com.dispose.manager.MsgSerialManager; import com.dispose.manager.MsgSerialManager;
import com.dispose.mapper.MsgSerialMapper; import com.dispose.mapper.MsgSerialMapper;
import com.dispose.pojo.entity.MsgSerial; import com.dispose.pojo.entity.MsgSerial;
@ -17,10 +18,19 @@ public class MsgSerialManagerImpl implements MsgSerialManager {
@Resource @Resource
private MsgSerialMapper msgSerialMapper; private MsgSerialMapper msgSerialMapper;
/**
* Add user business error code.
*
* @param msgSerial the message serial
* @return the error code
*/
@Override @Override
public void addMsgSerialNumber(MsgSerial msgSerial) { public ErrorCode addMsgSerialNumber(MsgSerial msgSerial) {
msgSerialMapper.addMsgSerial(msgSerial); if (msgSerialMapper.addMsgSerial(msgSerial) == 1) {
return ErrorCode.ERR_OK;
} else {
return ErrorCode.ERR_DATABASE;
}
} }
/** /**

View File

@ -1,14 +1,17 @@
package com.dispose.service; package com.dispose.service;
import com.dispose.common.ErrorCode;
import com.dispose.pojo.entity.MsgSerial; import com.dispose.pojo.entity.MsgSerial;
import com.dispose.pojo.po.MulReturnType;
public interface MsgSerialService { public interface MsgSerialService {
/** /**
* add message serial. * add message serial.
* *
* @param msgSerial the message serial * @param msgSerial the message serial
* @return the mul return type
*/ */
void addMessageSerial(MsgSerial msgSerial); MulReturnType<ErrorCode, Long> addMessageSerial(MsgSerial msgSerial);
/** /**
* get new max message serial. * get new max message serial.

View File

@ -1,7 +1,9 @@
package com.dispose.service.impl; package com.dispose.service.impl;
import com.dispose.common.ErrorCode;
import com.dispose.manager.MsgSerialManager; import com.dispose.manager.MsgSerialManager;
import com.dispose.pojo.entity.MsgSerial; import com.dispose.pojo.entity.MsgSerial;
import com.dispose.pojo.po.MulReturnType;
import com.dispose.service.MsgSerialService; import com.dispose.service.MsgSerialService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -16,10 +18,24 @@ public class MsgSerialServiceImpl implements MsgSerialService {
* add message serial. * add message serial.
* *
* @param msgSerial the message serial * @param msgSerial the message serial
* @return the mul return type
*/ */
@Override @Override
public void addMessageSerial(MsgSerial msgSerial) { public MulReturnType<ErrorCode, Long> addMessageSerial(MsgSerial msgSerial) {
msgSerialManager.addMsgSerialNumber(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);
}
} }
/** /**

View File

@ -75,10 +75,10 @@ public class KafkaControllerTest extends InitTestEnvironment {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
disposeParam.set("alarmId", "1"); disposeParam.set("alarmId", "1");
disposeParam.set("dstIp", "192.168.1.1"); disposeParam.set("dstIp", "192.168.1.1");
disposeParam.set("attackType", "1"); disposeParam.set("attackType", "RSTFlood");
disposeParam.set("bpspps", "bps"); disposeParam.set("bpspps", "bps");
disposeParam.set("dstProvince", "ZHEJIANG"); disposeParam.set("dstProvince", "浙江");
disposeParam.set("dstCity", "HANGZHOU"); disposeParam.set("dstCity", "杭州");
disposeParam.set("startTime", sdf.format(new Date())); disposeParam.set("startTime", sdf.format(new Date()));
// 1清洗2流控3黑洞 // 1清洗2流控3黑洞
disposeParam.set("disposeType", 1); disposeParam.set("disposeType", 1);