feat: 网元数据模块添加UDM鉴权和签约
This commit is contained in:
28
src/modules/network_data/service/udm_auth.go
Normal file
28
src/modules/network_data/service/udm_auth.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package service
|
||||
|
||||
import "be.ems/src/modules/network_data/model"
|
||||
|
||||
// UDM鉴权信息 服务层接口
|
||||
type IUDMAuth interface {
|
||||
// ResetData 重置鉴权用户数据,清空数据库重新同步Redis数据
|
||||
ResetData(neId string) int64
|
||||
|
||||
// SelectPage 分页查询数据库
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectList 查询数据库
|
||||
SelectList(u model.UDMAuth) []model.UDMAuth
|
||||
|
||||
// Insert 从数据中读取后删除imsi再存入数据库
|
||||
// imsi长度15,ki长度32,opc长度0或者32
|
||||
Insert(neId string, u model.UDMAuth) int64
|
||||
|
||||
// InsertData 导入文件数据 dataType目前两种:txt/csv
|
||||
InsertData(neId, dataType string, data any) int64
|
||||
|
||||
// Delete 删除单个不重新加载
|
||||
Delete(neID, imsi string) int64
|
||||
|
||||
// LoadData 删除范围后重新加载 num表示imsi后几位
|
||||
LoadData(neID, imsi, num string) int64
|
||||
}
|
||||
146
src/modules/network_data/service/udm_auth.impl.go
Normal file
146
src/modules/network_data/service/udm_auth.impl.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"be.ems/src/framework/redis"
|
||||
"be.ems/src/modules/network_data/model"
|
||||
"be.ems/src/modules/network_data/repository"
|
||||
)
|
||||
|
||||
// 实例化服务层 UDMAuthImpl 结构体
|
||||
var NewUDMAuthImpl = &UDMAuthImpl{
|
||||
udmAuthRepository: repository.NewUDMAuthImpl,
|
||||
}
|
||||
|
||||
// UDM鉴权信息 服务层处理
|
||||
type UDMAuthImpl struct {
|
||||
// UDM鉴权信息数据信息
|
||||
udmAuthRepository repository.IUDMAuth
|
||||
}
|
||||
|
||||
// dataByRedis UDM鉴权用户 db:0 中 ausf:*
|
||||
func (r *UDMAuthImpl) dataByRedis(imsi, neId string) []model.UDMAuth {
|
||||
arr := []model.UDMAuth{}
|
||||
key := fmt.Sprintf("ausf:%s", imsi)
|
||||
ausfArr, err := redis.GetKeys("udmuser", key)
|
||||
if err != nil {
|
||||
return arr
|
||||
}
|
||||
for _, key := range ausfArr {
|
||||
m, err := redis.GetHash("udmuser", key)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// 跳过-号数据
|
||||
imsi := key[5:]
|
||||
if strings.Contains(imsi, "-") {
|
||||
continue
|
||||
}
|
||||
|
||||
amf := ""
|
||||
if v, ok := m["amf"]; ok {
|
||||
amf = strings.Replace(v, "\r\n", "", 1)
|
||||
}
|
||||
a := model.UDMAuth{
|
||||
IMSI: imsi,
|
||||
Amf: amf,
|
||||
Status: "1", // 默认给1
|
||||
Ki: m["ki"],
|
||||
AlgoIndex: m["algo"],
|
||||
Opc: m["opc"],
|
||||
NeId: neId,
|
||||
}
|
||||
arr = append(arr, a)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// ResetData 重置鉴权用户数据,清空数据库重新同步Redis数据
|
||||
func (r *UDMAuthImpl) ResetData(neId string) int64 {
|
||||
authArr := r.dataByRedis("*", neId)
|
||||
// 数据清空后添加
|
||||
go r.udmAuthRepository.ClearAndInsert(neId, authArr)
|
||||
return int64(len(authArr))
|
||||
}
|
||||
|
||||
// SelectPage 分页查询数据库
|
||||
func (r *UDMAuthImpl) SelectPage(query map[string]any) map[string]any {
|
||||
return r.udmAuthRepository.SelectPage(query)
|
||||
}
|
||||
|
||||
// SelectList 查询数据库
|
||||
func (r *UDMAuthImpl) SelectList(u model.UDMAuth) []model.UDMAuth {
|
||||
return r.udmAuthRepository.SelectList(u)
|
||||
}
|
||||
|
||||
// Insert 从数据中读取后删除imsi再存入数据库
|
||||
// imsi长度15,ki长度32,opc长度0或者32
|
||||
func (r *UDMAuthImpl) Insert(neId string, u model.UDMAuth) int64 {
|
||||
uArr := r.dataByRedis(u.IMSI, neId)
|
||||
if len(uArr) > 0 {
|
||||
r.udmAuthRepository.Delete(neId, u.IMSI)
|
||||
return r.udmAuthRepository.Inserts(uArr)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// InsertData 导入文件数据 dataType目前两种:txt/csv
|
||||
func (r *UDMAuthImpl) InsertData(neId, dataType string, data any) int64 {
|
||||
// imsi截取前缀,重新获取部分数据
|
||||
prefixes := make(map[string]struct{})
|
||||
|
||||
if dataType == "csv" {
|
||||
for _, v := range data.([]map[string]string) {
|
||||
imsi := v["imsi"]
|
||||
if len(imsi) < 6 {
|
||||
continue
|
||||
}
|
||||
prefix := imsi[:len(imsi)-4]
|
||||
prefixes[prefix] = struct{}{}
|
||||
}
|
||||
}
|
||||
if dataType == "txt" {
|
||||
for _, v := range data.([][]string) {
|
||||
imsi := v[0]
|
||||
if len(imsi) < 6 {
|
||||
continue
|
||||
}
|
||||
prefix := imsi[:len(imsi)-4]
|
||||
prefixes[prefix] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// 根据前缀重新加载插入
|
||||
var num int64 = 0
|
||||
for prefix := range prefixes {
|
||||
// 直接删除前缀的记录
|
||||
r.udmAuthRepository.DeletePrefixByIMSI(neId, prefix)
|
||||
// keys ausf:4600001000004*
|
||||
authArr := r.dataByRedis(prefix+"*", neId)
|
||||
if len(authArr) > 0 {
|
||||
num += r.udmAuthRepository.Inserts(authArr)
|
||||
}
|
||||
}
|
||||
return num
|
||||
}
|
||||
|
||||
// Delete 删除单个不重新加载
|
||||
func (r *UDMAuthImpl) Delete(neId, imsi string) int64 {
|
||||
return r.udmAuthRepository.Delete(neId, imsi)
|
||||
}
|
||||
|
||||
// LoadData 删除范围后重新加载 num表示imsi后几位
|
||||
func (r *UDMAuthImpl) LoadData(neId, imsi, num string) int64 {
|
||||
prefix := imsi[:len(imsi)-len(num)-1]
|
||||
// 直接删除前缀的记录
|
||||
delNum := r.udmAuthRepository.DeletePrefixByIMSI(neId, prefix)
|
||||
// keys ausf:4600001000004*
|
||||
authArr := r.dataByRedis(prefix+"*", neId)
|
||||
if len(authArr) > 0 {
|
||||
return r.udmAuthRepository.Inserts(authArr)
|
||||
}
|
||||
return delNum
|
||||
}
|
||||
28
src/modules/network_data/service/udm_sub.go
Normal file
28
src/modules/network_data/service/udm_sub.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package service
|
||||
|
||||
import "be.ems/src/modules/network_data/model"
|
||||
|
||||
// UDM签约用户信息 服务层接口
|
||||
type IUDMSub interface {
|
||||
// ResetData 重置鉴权用户数据,清空数据库重新同步Redis数据
|
||||
ResetData(neId string) int64
|
||||
|
||||
// SelectPage 分页查询数据库
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectList 查询数据库
|
||||
SelectList(u model.UDMSub) []model.UDMSub
|
||||
|
||||
// Insert 从数据中读取后删除imsi再存入数据库
|
||||
// imsi长度15,ki长度32,opc长度0或者32
|
||||
Insert(neId string, u model.UDMSub) int64
|
||||
|
||||
// InsertData 导入文件数据 dataType目前两种:txt/csv
|
||||
InsertData(neId, dataType string, data any) int64
|
||||
|
||||
// Delete 删除单个不重新加载
|
||||
Delete(neId, imsi string) int64
|
||||
|
||||
// LoadData 删除范围后重新加载 num表示imsi后几位
|
||||
LoadData(neId, imsi, num string) int64
|
||||
}
|
||||
164
src/modules/network_data/service/udm_sub.impl.go
Normal file
164
src/modules/network_data/service/udm_sub.impl.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"be.ems/src/framework/redis"
|
||||
"be.ems/src/modules/network_data/model"
|
||||
"be.ems/src/modules/network_data/repository"
|
||||
)
|
||||
|
||||
// 实例化服务层 UDMSubImpl 结构体
|
||||
var NewUDMSubImpl = &UDMSubImpl{
|
||||
udmSubRepository: repository.NewUDMSubImpl,
|
||||
}
|
||||
|
||||
// UDM签约信息 服务层处理
|
||||
type UDMSubImpl struct {
|
||||
// UDM签约信息数据信息
|
||||
udmSubRepository repository.IUDMSub
|
||||
}
|
||||
|
||||
// dataByRedis UDM签约用户 db:0 中 udm-sd:*
|
||||
func (r *UDMSubImpl) dataByRedis(imsi, neId string) []model.UDMSub {
|
||||
arr := []model.UDMSub{}
|
||||
key := fmt.Sprintf("udm-sd:%s", imsi)
|
||||
udmsdArr, err := redis.GetKeys("udmuser", key)
|
||||
if err != nil {
|
||||
return arr
|
||||
}
|
||||
for _, key := range udmsdArr {
|
||||
m, err := redis.GetHash("udmuser", key)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
a := model.UDMSub{
|
||||
IMSI: key[7:],
|
||||
Msisdn: m["gpsi"], // 46003550072
|
||||
SmfSel: m["smf-sel"],
|
||||
SmData: m["sm-dat"], // 1-000001&cmnet&ims&3gnet
|
||||
NeId: neId,
|
||||
}
|
||||
|
||||
// def_ambr,def_nssai,0,def_arfb,def_sar,3,1,12000,1,1000,0,1,-
|
||||
if v, ok := m["am-dat"]; ok {
|
||||
arr := strings.Split(v, ",")
|
||||
a.Ambr = arr[0]
|
||||
a.Nssai = arr[1]
|
||||
a.Rat = arr[2]
|
||||
a.Arfb = arr[3]
|
||||
a.Sar = arr[4]
|
||||
a.Cn = arr[5]
|
||||
}
|
||||
// 1,64,24,65,def_eps,1,2,010200000000,-
|
||||
if v, ok := m["eps-dat"]; ok {
|
||||
arr := strings.Split(v, ",")
|
||||
// 跳过非常规数据
|
||||
if len(arr) > 9 {
|
||||
continue
|
||||
}
|
||||
a.EpsDat = v
|
||||
a.EpsFlag = arr[0]
|
||||
a.EpsOdb = arr[1]
|
||||
a.HplmnOdb = arr[2]
|
||||
a.Ard = arr[3]
|
||||
a.Epstpl = arr[4]
|
||||
a.ContextId = arr[5]
|
||||
a.ApnContext = arr[7]
|
||||
// [6] 是不要的,导入和导出不用
|
||||
a.StaticIp = arr[8]
|
||||
}
|
||||
|
||||
arr = append(arr, a)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// ResetData 重置鉴权用户数据,清空数据库重新同步Redis数据
|
||||
func (r *UDMSubImpl) ResetData(neId string) int64 {
|
||||
subArr := r.dataByRedis("*", neId)
|
||||
// 数据清空后添加
|
||||
go r.udmSubRepository.ClearAndInsert(neId, subArr)
|
||||
return int64(len(subArr))
|
||||
}
|
||||
|
||||
// SelectPage 分页查询数据库
|
||||
func (r *UDMSubImpl) SelectPage(query map[string]any) map[string]any {
|
||||
return r.udmSubRepository.SelectPage(query)
|
||||
}
|
||||
|
||||
// SelectList 查询数据库
|
||||
func (r *UDMSubImpl) SelectList(u model.UDMSub) []model.UDMSub {
|
||||
return r.udmSubRepository.SelectList(u)
|
||||
}
|
||||
|
||||
// Insert 从数据中读取后删除imsi再存入数据库
|
||||
// imsi长度15,ki长度32,opc长度0或者32
|
||||
func (r *UDMSubImpl) Insert(neId string, u model.UDMSub) int64 {
|
||||
uArr := r.dataByRedis(u.IMSI, neId)
|
||||
if len(uArr) > 0 {
|
||||
r.udmSubRepository.Delete(neId, u.IMSI)
|
||||
return r.udmSubRepository.Inserts(uArr)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// InsertData 导入文件数据 dataType目前两种:txt/csv
|
||||
func (r *UDMSubImpl) InsertData(neId, dataType string, data any) int64 {
|
||||
// imsi截取前缀,重新获取部分数据
|
||||
prefixes := make(map[string]struct{})
|
||||
|
||||
if dataType == "csv" {
|
||||
for _, v := range data.([]map[string]string) {
|
||||
imsi := v["imsi"]
|
||||
if len(imsi) < 6 {
|
||||
continue
|
||||
}
|
||||
prefix := imsi[:len(imsi)-4]
|
||||
prefixes[prefix] = struct{}{}
|
||||
}
|
||||
}
|
||||
if dataType == "txt" {
|
||||
for _, v := range data.([][]string) {
|
||||
imsi := v[0]
|
||||
if len(imsi) < 6 {
|
||||
continue
|
||||
}
|
||||
prefix := imsi[:len(imsi)-4]
|
||||
prefixes[prefix] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// 根据前缀重新加载插入
|
||||
var num int64 = 0
|
||||
for prefix := range prefixes {
|
||||
// 直接删除前缀的记录
|
||||
r.udmSubRepository.DeletePrefixByIMSI(neId, prefix)
|
||||
// keys udm-sd:4600001000004*
|
||||
subArr := r.dataByRedis(prefix+"*", neId)
|
||||
if len(subArr) > 0 {
|
||||
num += r.udmSubRepository.Inserts(subArr)
|
||||
}
|
||||
}
|
||||
return num
|
||||
}
|
||||
|
||||
// Delete 删除单个不重新加载
|
||||
func (r *UDMSubImpl) Delete(neId, imsi string) int64 {
|
||||
return r.udmSubRepository.Delete(neId, imsi)
|
||||
}
|
||||
|
||||
// LoadData 删除范围后重新加载 num表示imsi后几位
|
||||
func (r *UDMSubImpl) LoadData(neId, imsi, num string) int64 {
|
||||
prefix := imsi[:len(imsi)-len(num)-1]
|
||||
// 直接删除前缀的记录
|
||||
delNum := r.udmSubRepository.DeletePrefixByIMSI(neId, prefix)
|
||||
// keys udm-sd:4600001000004*
|
||||
authArr := r.dataByRedis(prefix+"*", neId)
|
||||
if len(authArr) > 0 {
|
||||
return r.udmSubRepository.Inserts(authArr)
|
||||
}
|
||||
return delNum
|
||||
}
|
||||
Reference in New Issue
Block a user