184 lines
4.5 KiB
Go
184 lines
4.5 KiB
Go
package service
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
|
||
"ems.agt/src/framework/redis"
|
||
"ems.agt/src/modules/network_element/model"
|
||
"ems.agt/src/modules/network_element/repository"
|
||
)
|
||
|
||
// 实例化服务层 UDMAuthImpl 结构体
|
||
var NewUDMAuthImpl = &UDMAuthImpl{
|
||
udmAuthRepository: repository.NewUDMAuthImpl,
|
||
}
|
||
|
||
// UDM鉴权信息 服务层处理
|
||
type UDMAuthImpl struct {
|
||
// UDM鉴权信息数据信息
|
||
udmAuthRepository repository.IUDMAuth
|
||
}
|
||
|
||
// authDataByRedis UDM鉴权用户
|
||
func (r *UDMAuthImpl) authDataByRedis(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
|
||
}
|
||
|
||
status := "1" // 默认给1
|
||
|
||
amf := ""
|
||
if v, ok := m["amf"]; ok {
|
||
amf = strings.Replace(v, "\r\n", "", 1)
|
||
}
|
||
a := model.UDMAuth{
|
||
Imsi: imsi,
|
||
Amf: amf,
|
||
Status: status,
|
||
Ki: m["ki"],
|
||
AlgoIndex: m["algo"],
|
||
Opc: m["opc"],
|
||
NeID: neID,
|
||
}
|
||
arr = append(arr, a)
|
||
}
|
||
return arr
|
||
}
|
||
|
||
// Save UDM鉴权用户-获取redis全部保存数据库
|
||
func (r *UDMAuthImpl) Save(neID string) int64 {
|
||
var num int64 = 0
|
||
authArr := r.authDataByRedis("*", neID)
|
||
// 有数据才清空
|
||
if len(authArr) == 0 {
|
||
return num
|
||
}
|
||
go r.udmAuthRepository.ClearAndInsert(neID, authArr)
|
||
return int64(len(authArr))
|
||
}
|
||
|
||
// Page UDM鉴权用户-分页查询数据库
|
||
func (r *UDMAuthImpl) Page(query map[string]any) map[string]any {
|
||
return r.udmAuthRepository.SelectPage(query)
|
||
}
|
||
|
||
// List UDM鉴权用户-查询数据库
|
||
func (r *UDMAuthImpl) List(authUser model.UDMAuth) []model.UDMAuth {
|
||
return r.udmAuthRepository.SelectList(authUser)
|
||
}
|
||
|
||
// Insert UDM鉴权用户-新增单个
|
||
// imsi长度15,ki长度32,opc长度0或者32
|
||
func (r *UDMAuthImpl) Insert(neID string, authUser model.UDMAuth) int64 {
|
||
authArr := r.authDataByRedis(authUser.Imsi, neID)
|
||
if len(authArr) > 0 {
|
||
r.udmAuthRepository.Delete(neID, authUser.Imsi)
|
||
r.udmAuthRepository.Inserts(authArr)
|
||
}
|
||
return 0
|
||
}
|
||
|
||
// Insert UDM鉴权用户-批量添加
|
||
func (r *UDMAuthImpl) Inserts(neID string, authUser model.UDMAuth, num string) int64 {
|
||
startIMSI := authUser.Imsi
|
||
startIMSI = startIMSI[:len(startIMSI)-len(num)] + "*"
|
||
// keys udm-sd:4600001000004*
|
||
authArr := r.authDataByRedis(startIMSI, neID)
|
||
if len(authArr) > 0 {
|
||
for _, v := range authArr {
|
||
r.udmAuthRepository.Delete(neID, v.Imsi)
|
||
}
|
||
return r.udmAuthRepository.Inserts(authArr)
|
||
}
|
||
return 0
|
||
}
|
||
|
||
// InsertCSV UDM鉴权用户-批量添加
|
||
func (r *UDMAuthImpl) InsertCSV(neID string, data []map[string]string) int64 {
|
||
prefixes := make(map[string]struct{})
|
||
for _, v := range data {
|
||
imsi := v["imsi"]
|
||
if len(imsi) < 5 {
|
||
continue
|
||
}
|
||
prefix := imsi[:len(imsi)-4]
|
||
prefixes[prefix] = struct{}{}
|
||
}
|
||
// 分组插入
|
||
var num int64 = 0
|
||
for prefix := range prefixes {
|
||
r.udmAuthRepository.DeletePrefixImsi(neID, prefix)
|
||
subArr := r.authDataByRedis(prefix+"*", neID)
|
||
if len(subArr) > 0 {
|
||
num += r.udmAuthRepository.Inserts(subArr)
|
||
}
|
||
}
|
||
return num
|
||
}
|
||
|
||
// InsertTxt UDM鉴权用户-批量添加
|
||
func (r *UDMAuthImpl) InsertTxt(neID string, data [][]string) int64 {
|
||
prefixes := make(map[string]struct{})
|
||
for _, v := range data {
|
||
imsi := v[0]
|
||
if len(imsi) < 5 {
|
||
continue
|
||
}
|
||
prefix := imsi[:len(imsi)-4]
|
||
prefixes[prefix] = struct{}{}
|
||
}
|
||
// 分组插入
|
||
var num int64 = 0
|
||
for prefix := range prefixes {
|
||
r.udmAuthRepository.DeletePrefixImsi(neID, prefix)
|
||
subArr := r.authDataByRedis(prefix+"*", neID)
|
||
if len(subArr) > 0 {
|
||
num += r.udmAuthRepository.Inserts(subArr)
|
||
}
|
||
}
|
||
return num
|
||
}
|
||
|
||
// Insert UDM鉴权用户-修改更新
|
||
func (r *UDMAuthImpl) Update(neID string, authUser model.UDMAuth) int64 {
|
||
authArr := r.authDataByRedis(authUser.Imsi, neID)
|
||
if len(authArr) > 0 {
|
||
r.udmAuthRepository.Delete(neID, authUser.Imsi)
|
||
return r.udmAuthRepository.Inserts(authArr)
|
||
}
|
||
return 0
|
||
}
|
||
|
||
// Insert UDM鉴权用户-删除单个
|
||
func (r *UDMAuthImpl) Delete(neID, imsi string) int64 {
|
||
return r.udmAuthRepository.Delete(neID, imsi)
|
||
}
|
||
|
||
// Insert UDM鉴权用户-删除范围
|
||
func (r *UDMAuthImpl) Deletes(neID, imsi, num string) int64 {
|
||
prefix := imsi[:len(imsi)-len(num)-1]
|
||
// 直接删除前缀的记录
|
||
r.udmAuthRepository.DeletePrefixImsi(neID, prefix)
|
||
// keys ausf:4600001000004*
|
||
authArr := r.authDataByRedis(prefix+"*", neID)
|
||
if len(authArr) > 0 {
|
||
return r.udmAuthRepository.Inserts(authArr)
|
||
}
|
||
return 0
|
||
}
|