OCT
REM: 1. 优化controller层代码 2. 优化数据库存储MsgSerial字段和获取最大MsgSerial值方法 4. 修改单元测试入参
This commit is contained in:
parent
39531233a2
commit
99d8f668be
|
@ -6,6 +6,8 @@ import java.util.Map;
|
|||
|
||||
/**
|
||||
* The emos constants.
|
||||
*
|
||||
* @author <chenling@cmhi.chinamoblie.com>
|
||||
*/
|
||||
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 = "<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" +
|
||||
"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<AlarmEnd>";
|
||||
|
|
|
@ -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 <huangxin@cmhi.chinamoblie.com>
|
||||
* @author <chenling@cmhi.chinamoblie.com>
|
||||
*/
|
||||
@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<ErrorCode, Long> 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 "北京市";
|
||||
}
|
||||
if (province.compareTo("上海") == 0) {
|
||||
return "上海市";
|
||||
}
|
||||
if (province.compareTo("天津") == 0) {
|
||||
return "天津市";
|
||||
}
|
||||
if (province.compareTo("重庆") == 0) {
|
||||
return "重庆市";
|
||||
}
|
||||
return province + "省" + city + "市";
|
||||
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 + "市";
|
||||
}
|
||||
|
||||
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() + "分钟";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<ErrorCode, Long> addMessageSerial(MsgSerial msgSerial);
|
||||
|
||||
/**
|
||||
* get new max message serial.
|
||||
|
|
|
@ -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<ErrorCode, Long> 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue