优化平台ppoe和非平台ppoe配置方式的虚拟网卡上报带宽值处理.

优化pppoe虚拟网卡处理逻辑
This commit is contained in:
gaoyutao
2026-01-09 18:04:13 +08:00
parent f5f5a97744
commit 52090c6f37
13 changed files with 209 additions and 32 deletions
@@ -4,6 +4,8 @@ import com.tongran.common.core.annotation.Excel;
import com.tongran.common.core.web.domain.BaseEntity;
import lombok.Data;
import java.math.BigDecimal;
/**
* 客户端网络接口子接口信息对象 rm_network_interface_child
*
@@ -74,4 +76,8 @@ public class RmNetworkInterfaceChild extends BaseEntity
/** 是否连通外网(0否,1是) */
@Excel(name = "是否连通外网(0否,1是)")
private String status;
/** 虚拟网卡上报带宽值(Mbps) */
private BigDecimal bandwidthResult;
/** 有ipv4的标识 */
private boolean ipv4Flag;
}
@@ -50,6 +50,8 @@ public class RmPppoeConfigSub extends BaseEntity
private String macAddress;
/** 客户端id */
private String clientId;
/** 接口名称 */
private String interfaceName;
public RmPppoeConfigSubVo toVo() {
RmPppoeConfigSubVo vo = new RmPppoeConfigSubVo();
@@ -1134,7 +1134,7 @@ public class MessageHandler {
Date createTime = new Date(millis / 1000 * 1000);
List<NetworkInfo> networkInfoList = registerMsg.getNetworkInfo();
if(networkInfoList.isEmpty()) {
if (networkInfoList.isEmpty()) {
return;
}
@@ -1148,6 +1148,7 @@ public class MessageHandler {
// 构建数据库中当前网卡MAC地址的集合
Set<String> currentMacSet = currentInterfaces.stream()
.map(RmNetworkInterface::getMacAddress)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
// 构建新数据中网卡MAC地址的集合
@@ -1169,8 +1170,27 @@ public class MessageHandler {
if (!macsToDelete.isEmpty() && !isRegister) {
deleteNetworkInterfaces(clientId, macsToDelete);
}
// 获取当前客户端的所有子网卡
RmNetworkInterfaceChild childQuery = new RmNetworkInterfaceChild();
childQuery.setClientId(clientId);
List<RmNetworkInterfaceChild> existingChildren = rmNetworkInterfaceChildService.selectRmNetworkInterfaceChildList(childQuery);
if(existingChildren == null){
existingChildren = new ArrayList<>();
}
// 构建数据库中所有子网卡的key集合 (clientId_mac_name)
Map<String, RmNetworkInterfaceChild> existingChildMap = new HashMap<>();
for (RmNetworkInterfaceChild child : existingChildren) {
String key = child.getClientId() + "_" + child.getMacAddress() + "_" + child.getInterfaceName();
existingChildMap.put(key, child);
}
// 构建本次接收到的所有子网卡key集合
Set<String> newChildKeys = new HashSet<>();
for (NetworkInfo networkInfo : networkInfoList) {
List<NetworkInfo> childList = networkInfo.getSubInterfaces();
// 查询该网卡信息是否存在
RmNetworkInterface queryParam = new RmNetworkInterface();
queryParam.setClientId(clientId);
@@ -1178,7 +1198,7 @@ public class MessageHandler {
queryParam.setNewFlag(1);
List<RmNetworkInterface> exits = rmNetworkInterfaceService.selectRmNetworkInterfaceList(queryParam);
if(exits.isEmpty()) {
if (exits.isEmpty()) {
// 新增网卡信息
RmNetworkInterface insertData = new RmNetworkInterface();
setNetworkInterfaceData(insertData, networkInfo, clientId);
@@ -1188,18 +1208,27 @@ public class MessageHandler {
insertData.setBindIp("3");
}
rmNetworkInterfaceService.insertRmNetworkInterface(insertData);
if(childList != null && !childList.isEmpty()){
rmNetworkInterfaceChildService.deleteRmNetworkInterfaceChildByClientId(clientId);
// 处理子网卡
if (childList != null && !childList.isEmpty()) {
for (NetworkInfo info : childList) {
RmNetworkInterfaceChild insertChild = new RmNetworkInterfaceChild();
setNetworkInterfaceChildData(insertChild, info, clientId, networkInfo.getName());
insertChild.setUpdateTime(DateUtils.getNowDate());
// 设置bindIp
if (isSingleInterface) {
insertChild.setBindIp("3");
}
// 构建子网卡唯一key
String childKey = clientId + "_" + info.getMac() + "_" + info.getName();
newChildKeys.add(childKey);
// 插入或更新子网卡
rmNetworkInterfaceChildService.insertRmNetworkInterfaceChild(insertChild);
}
}
// 如果网卡数量有变动,需要更新网卡绑定状态
if (!isRegister && interfaceCountChanged && !isSingleInterface) {
RmResourceRegistrationRemote updateParam = new RmResourceRegistrationRemote();
@@ -1210,22 +1239,48 @@ public class MessageHandler {
} else {
// 更新网卡信息
RmNetworkInterface oldInterfaceMsg = exits.get(0);
// cleanChildOldRecords(clientId, networkInfo.getMac());
// 判断是否需要创建新记录
boolean needCreateNew = (!StringUtils.equals(networkInfo.getName(), oldInterfaceMsg.getInterfaceName())
|| !StringUtils.equals(networkInfo.getGateway(), oldInterfaceMsg.getGateway())) && StringUtils.equals(networkInfo.getMac(), oldInterfaceMsg.getMacAddress());
if(childList != null && !childList.isEmpty()){
rmNetworkInterfaceChildService.deleteRmNetworkInterfaceChildByClientId(clientId);
// 处理子网卡
if (childList != null && !childList.isEmpty()) {
for (NetworkInfo info : childList) {
RmNetworkInterfaceChild insertChild = new RmNetworkInterfaceChild();
setNetworkInterfaceChildData(insertChild, info, clientId, networkInfo.getName());
insertChild.setUpdateTime(DateUtils.getNowDate());
// 构建子网卡唯一key
String childKey = clientId + "_" + info.getMac() + "_" + info.getName();
newChildKeys.add(childKey);
// 插入或更新子网卡
rmNetworkInterfaceChildService.insertRmNetworkInterfaceChild(insertChild);
}
}
// 判断是否需要创建新记录
boolean needCreateNew = (!StringUtils.equals(networkInfo.getName(), oldInterfaceMsg.getInterfaceName())
|| !StringUtils.equals(networkInfo.getGateway(), oldInterfaceMsg.getGateway())) && StringUtils.equals(networkInfo.getMac(), oldInterfaceMsg.getMacAddress());
// 更新现有记录
updateNetworkInterface(networkInfo, clientId, oldInterfaceMsg, isSingleInterface, isRegister);
}
}
// 删除数据库中不存在于本次接收数据中的子网卡
// 计算需要删除的子网卡:数据库中有的,但本次没有接收到的
Set<String> childKeysToDelete = new HashSet<>(existingChildMap.keySet());
childKeysToDelete.removeAll(newChildKeys);
if (!childKeysToDelete.isEmpty() && !isRegister) {
for (String keyToDelete : childKeysToDelete) {
String[] parts = keyToDelete.split("_", 3);
if (parts.length == 3) {
String clientIdPart = parts[0];
String mac = parts[1];
String name = parts[2];
// 只删除当前客户端的子网卡
if (clientId.equals(clientIdPart)) {
deleteChildNetworkInterfaces(clientId, mac, name);
}
}
}
}
}
/**
* 删除过时的网卡信息
@@ -1249,7 +1304,28 @@ public class MessageHandler {
// 记录删除操作日志
log.info("删除客户端 {} 的过时网卡信息,MAC地址: {}", clientId, macsToDelete);
}
/**
* 删除过时的子网卡信息
* @param clientId
* @param name
* @param mac
*/
private void deleteChildNetworkInterfaces(String clientId, String mac, String name) {
RmNetworkInterfaceChild query = new RmNetworkInterfaceChild();
query.setClientId(clientId);
query.setMacAddress(mac);
query.setInterfaceName(name);
query.setNewFlag(1);
List<RmNetworkInterfaceChild> oldExits = rmNetworkInterfaceChildService.selectRmNetworkInterfaceChildList(query);
if(!oldExits.isEmpty()) {
oldExits.forEach(oldMsg -> {
rmNetworkInterfaceChildService.deleteRmNetworkInterfaceChildById(oldMsg.getId());
});
}
// 记录删除操作日志
log.info("删除客户端 {} 的过时子网卡信息,MAC地址: {},网卡名称:{}", clientId, mac, name);
}
/**
* 设置网卡信息公共字段
*/
@@ -1350,6 +1426,10 @@ public class MessageHandler {
updateData.setInterfaceType(networkInfo.getType());
needUpdate = true;
}
if (!StringUtils.equals(networkInfo.getGateway(), oldInterfaceMsg.getGateway())) {
updateData.setGateway(networkInfo.getGateway());
needUpdate = true;
}
if (!StringUtils.equals(networkInfo.getName(), oldInterfaceMsg.getInterfaceName())) {
if(networkInfo.getName() != null){
// 添加业务变更记录
@@ -1,6 +1,7 @@
package com.tongran.rocketmq.mapper;
import com.tongran.rocketmq.domain.RmNetworkInterfaceChild;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@@ -61,4 +62,6 @@ public interface RmNetworkInterfaceChildMapper
public int deleteRmNetworkInterfaceChildByIds(Long[] ids);
void deleteRmNetworkInterfaceChildByClientId(String clientId);
void deleteByParentInterfaceName(@Param("clientId") String clientId, @Param("name") String name);
}
@@ -61,4 +61,6 @@ public interface IRmNetworkInterfaceChildService
public int deleteRmNetworkInterfaceChildById(Long id);
void deleteRmNetworkInterfaceChildByClientId(String clientId);
void deleteByParentInterfaceName(String clientId, String name);
}
@@ -618,12 +618,7 @@ public class InitialBandwidthTrafficServiceImpl implements IInitialBandwidthTraf
@Override
public Map<String, Object> serverDetails(InitialBandwidthTraffic initialBandwidthTraffic) {
InitialBandwidthTraffic sppedMsg = getNetInterfaceDetailsMsg(initialBandwidthTraffic);
Map<String, Object> resultMap = new HashMap<>();
if(sppedMsg == null){
sppedMsg = new InitialBandwidthTraffic();
}
resultMap.put("speed", sppedMsg.getSpeed());
InitialSystemOtherCollectData memQuery = new InitialSystemOtherCollectData();
memQuery.setClientId(initialBandwidthTraffic.getClientId());
memQuery.setTableName(TableSubUtil.getTableName(DateUtils.getNowDate(), "initial_system_other_collect_data"));
@@ -210,9 +210,11 @@ public class RmAgentManagementServiceImpl implements IRmAgentManagementService
if(managementList != null && !managementList.isEmpty()){
RmAgentManagement agentManagement = managementList.get(0);
String addr = agentManagement.getFileUrl();
if(addr.contains(rmAgentManagement.getAgentVersion())){
agentManagement.setNewFlag(true);
return agentManagement;
if(addr != null){
if(addr.contains(rmAgentManagement.getAgentVersion())){
agentManagement.setNewFlag(true);
return agentManagement;
}
}
}
return new RmAgentManagement();
@@ -99,4 +99,9 @@ public class RmNetworkInterfaceChildServiceImpl implements IRmNetworkInterfaceCh
public void deleteRmNetworkInterfaceChildByClientId(String clientId) {
rmNetworkInterfaceChildMapper.deleteRmNetworkInterfaceChildByClientId(clientId);
}
@Override
public void deleteByParentInterfaceName(String clientId, String name) {
rmNetworkInterfaceChildMapper.deleteByParentInterfaceName(clientId, name);
}
}
@@ -4,11 +4,13 @@ import com.alibaba.fastjson.JSONObject;
import com.tongran.common.core.enums.MsgEnum;
import com.tongran.common.core.utils.DateUtils;
import com.tongran.rocketmq.domain.DeviceMessage;
import com.tongran.rocketmq.domain.RmNetworkInterfaceChild;
import com.tongran.rocketmq.domain.RmPppoeConfigMain;
import com.tongran.rocketmq.domain.RmPppoeConfigSub;
import com.tongran.rocketmq.domain.vo.PolicyTypeVo;
import com.tongran.rocketmq.domain.vo.PppoeVo;
import com.tongran.rocketmq.domain.vo.RmPppoeConfigSubVo;
import com.tongran.rocketmq.mapper.RmNetworkInterfaceChildMapper;
import com.tongran.rocketmq.mapper.RmPppoeConfigMainMapper;
import com.tongran.rocketmq.mapper.RmPppoeConfigSubMapper;
import com.tongran.rocketmq.model.ProducerMode;
@@ -37,6 +39,8 @@ public class RmPppoeConfigMainServiceImpl implements IRmPppoeConfigMainService
private RmPppoeConfigSubMapper rmPppoeConfigSubMapper;
@Autowired
private ProducerMode producerMode;
@Autowired
private RmNetworkInterfaceChildMapper rmNetworkInterfaceChildMapper;
@Override
public RmPppoeConfigMain selectRmPppoeConfigMainByClientId(RmPppoeConfigMain rmPppoeConfigMain)
@@ -49,6 +53,28 @@ public class RmPppoeConfigMainServiceImpl implements IRmPppoeConfigMainService
RmPppoeConfigSub rmPppoeConfigSub = new RmPppoeConfigSub();
rmPppoeConfigSub.setClientId(rmPppoeConfigMain.getClientId());
List<RmPppoeConfigSub> subList = rmPppoeConfigSubMapper.selectRmPppoeConfigSubList(rmPppoeConfigSub);
if("2".equals(pppoeConfigMain.getPppoeConfigMode())){
subList = new ArrayList<>();
// 查询虚拟网卡信息
RmNetworkInterfaceChild childQuery = new RmNetworkInterfaceChild();
childQuery.setNewFlag(1);
childQuery.setIpv4Flag(true);
childQuery.setClientId(rmPppoeConfigMain.getClientId());
List<RmNetworkInterfaceChild> childList = rmNetworkInterfaceChildMapper.selectRmNetworkInterfaceChildList(childQuery);
if(childList != null && !childList.isEmpty()){
for (RmNetworkInterfaceChild child : childList) {
RmPppoeConfigSub pppoeConfigSub = new RmPppoeConfigSub();
pppoeConfigSub.setClientId(child.getClientId());
pppoeConfigSub.setInterfaceName(child.getInterfaceName());
pppoeConfigSub.setMacAddress(child.getMacAddress());
pppoeConfigSub.setIpv4Address(child.getIpv4Address());
pppoeConfigSub.setIpv4Gateway(child.getGateway());
pppoeConfigSub.setStatus(child.getStatus());
pppoeConfigSub.setBandwidthResult(child.getBandwidthResult());
subList.add(pppoeConfigSub);
}
}
}
pppoeConfigMain.setSubList(subList);
return pppoeConfigMain;
}
@@ -78,19 +104,23 @@ public class RmPppoeConfigMainServiceImpl implements IRmPppoeConfigMainService
int rows = rmPppoeConfigMainMapper.insertRmPppoeConfigMain(rmPppoeConfigMain);
List<RmPppoeConfigSub> subList = rmPppoeConfigMain.getSubList();
List<RmPppoeConfigSubVo> voList = new ArrayList<>();
if(subList != null && !subList.isEmpty()){
for (RmPppoeConfigSub rmPppoeConfigSub : subList) {
rmPppoeConfigSub.setClientId(rmPppoeConfigMain.getClientId());
rmPppoeConfigSub.setCreateTime(DateUtils.getNowDate());
rmPppoeConfigSub.setUpdateTime(DateUtils.getNowDate());
if("1".equals(rmPppoeConfigMain.getPppoeConfigMode())){
if(subList != null && !subList.isEmpty()){
for (RmPppoeConfigSub rmPppoeConfigSub : subList) {
rmPppoeConfigSub.setClientId(rmPppoeConfigMain.getClientId());
rmPppoeConfigSub.setCreateTime(DateUtils.getNowDate());
rmPppoeConfigSub.setUpdateTime(DateUtils.getNowDate());
}
// 删除子表信息
rmPppoeConfigSubMapper.deleteRmPppoeConfigSubByClientId(rmPppoeConfigMain.getClientId());
// 批量新增
rmPppoeConfigSubMapper.batchInsertRmPppoeConfigSub(subList);
voList = subList.stream()
.map(RmPppoeConfigSub::toVo)
.collect(Collectors.toList());
}
// 删除子表信息
rmPppoeConfigSubMapper.deleteRmPppoeConfigSubByClientId(rmPppoeConfigMain.getClientId());
// 批量新增
rmPppoeConfigSubMapper.batchInsertRmPppoeConfigSub(subList);
voList = subList.stream()
.map(RmPppoeConfigSub::toVo)
.collect(Collectors.toList());
}else if("2".equals(rmPppoeConfigMain.getPppoeConfigMode())) {
processBandwidthResult(subList);
}
// 构建pppoe策略
PppoeVo pppoeVo = new PppoeVo();
@@ -115,6 +145,20 @@ public class RmPppoeConfigMainServiceImpl implements IRmPppoeConfigMainService
return 1;
}
public void processBandwidthResult(List<RmPppoeConfigSub> subList){
if(subList != null && !subList.isEmpty()){
for (RmPppoeConfigSub rmPppoeConfigSub : subList) {
RmNetworkInterfaceChild insertData = new RmNetworkInterfaceChild();
insertData.setBandwidthResult(rmPppoeConfigSub.getBandwidthResult());
insertData.setMacAddress(rmPppoeConfigSub.getMacAddress());
insertData.setInterfaceName(rmPppoeConfigSub.getInterfaceName());
insertData.setClientId(rmPppoeConfigSub.getClientId());
insertData.setNewFlag(1);
rmNetworkInterfaceChildMapper.insertRmNetworkInterfaceChild(insertData);
}
}
}
/**
* 修改PPPoE配置主表
*