ims online user support multi-tenancy

This commit is contained in:
2024-07-11 10:47:55 +08:00
parent d89d2c37bf
commit cec142e598
2 changed files with 166 additions and 57 deletions

View File

@@ -25,16 +25,6 @@ type SmfUENum struct {
UENum int `json:"ueNum"` // 当前在线用户数
}
// ImsUEInfo IMS在线用户信息
type ImsUEInfo struct {
IMSI string `json:"imsi"`
MSISDN string `json:"msisdn"`
IMPU string `json:"impu"`
Barring int `json:"barring"`
RegState int `json:"regState"`
Activetime string `json:"activeTime"`
}
// N3iwfUEInfo N3IWF在线用户信息
type N3iwfUEInfo struct {
IMSI string `json:"imsi"`
@@ -50,7 +40,8 @@ type N3iwfUEData struct {
var (
UriNBInfo = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/nbInfo"
UriUEInfo = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/ueInfo"
UriSMFUEInfo = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/smf/objectType/ueInfo"
UriIMSUEInfo = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/ims/objectType/ueInfo"
UriUENum = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/ueNum"
UriPCFUser = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/pcf/objectType/ueInfo"
UriPCFUserM = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/pcf/objectType/ueInfo/batch/{number}"
@@ -60,7 +51,8 @@ var (
UriNSSFSubscriptions = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/subscriptions"
CustomUriNBInfo = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/nbInfo"
CustomUriUEInfo = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/ueInfo"
CustomUriSMFUEInfo = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/smf/objectType/ueInfo"
CustomUriIMSUEInfo = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/ims/objectType/ueInfo"
CustomUriUENum = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/ueNum"
CustomUriPCFUser = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/pcf/objectType/ueInfo"
CustomUriPCFUserM = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/pcf/objectType/ueInfo/batch/{number}"
@@ -214,6 +206,35 @@ func GetSubscriptionsFromNSSF(w http.ResponseWriter, r *http.Request) {
}
}
// ImsUEInfo IMS在线用户信息
type ImsUEInfo struct {
IMSI string `json:"imsi"`
MSISDN string `json:"msisdn"`
IMPU string `json:"impu"`
Barring int `json:"barring"`
RegState int `json:"regState"`
Activetime string `json:"activeTime"`
}
// ImsUEInfo IMS在线用户信息
type TenantImsUEInfo struct {
IMSI string `json:"imsi"`
MSISDN string `json:"msisdn"`
IMPU string `json:"impu"`
Barring int `json:"barring"`
RegState int `json:"regState"`
Activetime string `json:"activeTime"`
TenantName string `json:"tenantName"`
}
type ImsUEInfoResponse struct {
Data []ImsUEInfo `json:"data"`
}
type TenantImsUEInfoResponse struct {
Data []TenantImsUEInfo `json:"data"`
}
// SmfUEInfo SMF在线用户信息
type SmfUEInfo struct {
IMSI string `json:"imsi"`
@@ -265,7 +286,7 @@ func GetUEInfoFromNF(w http.ResponseWriter, r *http.Request) {
log.Debug("GetUEInfoFromNF processing... ")
neId := ctx.GetQuery(r, "neId")
neType := ctx.GetParam(r, "elementTypeValue")
neType := "smf"
if neType == "" || neId == "" {
log.Error("elementTypeValue/neId is empty")
services.ResponseNotFound404UriNotExist(w, r)
@@ -282,6 +303,7 @@ func GetUEInfoFromNF(w http.ResponseWriter, r *http.Request) {
var response services.MapResponse
if neInfo.NeId == neId && neInfo.NeId != "" {
requestURI2NF := fmt.Sprintf("http://%s:%v%s", neInfo.IP, neInfo.Port, r.RequestURI)
//requestURI2NF := "http://127.0.0.1:4523/m1/3157310-1528434-default/api/rest/ueManagement/v1/elementType/smf/objectType/ueInfo?apifoxApiId=128609449"
log.Debug("requestURI2NF:", requestURI2NF)
resp, err := client.R().
EnableTrace().
@@ -292,57 +314,140 @@ func GetUEInfoFromNF(w http.ResponseWriter, r *http.Request) {
log.Error("Failed to Get from NF:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
// multi-tenancy solution
var tenantID, where string
//var cols []string
if tenantName != "" {
where = fmt.Sprintf("status='1' and tenant_name='%s'", tenantName)
tenantID, _ = dborm.XormGetSingleColStringByWhere("sys_tenant", "tenant_id", where)
} else {
// multi-tenancy solution
var tenantID, where string
//var cols []string
if tenantName != "" {
where = fmt.Sprintf("status='1' and tenant_name='%s'", tenantName)
tenantID, _ = dborm.XormGetSingleColStringByWhere("sys_tenant", "tenant_id", where)
} else {
userName := ctx.LoginUserToUserName(r)
where = fmt.Sprintf("status='1' and user_name='%s'", userName)
tenantID, _ = dborm.XormGetSingleColStringByWhere("sys_user", "tenant_id", where)
userName := ctx.LoginUserToUserName(r)
where = fmt.Sprintf("status='1' and user_name='%s'", userName)
tenantID, _ = dborm.XormGetSingleColStringByWhere("sys_user", "tenant_id", where)
where = fmt.Sprintf("status='1' and tenant_id='%s'", tenantID)
tenantName, _ = dborm.XormGetSingleColStringByWhere("sys_tenant", "tenant_name", where)
}
if tenantID == "" {
var response UEInfoResponse
_ = json.Unmarshal(resp.Body(), &response)
for i := 0; i < len(response.Data); i++ {
where = fmt.Sprintf("status='1' and tenancy_type='IMSI' and '%s' like tenancy_key", response.Data[i].IMSI[5:])
tenantID, _ = dborm.XormGetSingleColStringByWhere("sys_tenant", "parent_id", where)
where = fmt.Sprintf("status='1' and tenant_id='%s'", tenantID)
tenantName, _ = dborm.XormGetSingleColStringByWhere("sys_tenant", "tenant_name", where)
log.Tracef("tenantName: %s tenantID: %s", tenantName, tenantID)
response.Data[i].TenantName = tenantName
}
if tenantID == "" {
var response UEInfoResponse
_ = json.Unmarshal(resp.Body(), &response)
for i := 0; i < len(response.Data); i++ {
where = fmt.Sprintf("status='1' and tenancy_type='IMSI' and '%s' like tenancy_key", response.Data[i].IMSI[5:])
tenantID, _ = dborm.XormGetSingleColStringByWhere("sys_tenant", "parent_id", where)
where = fmt.Sprintf("status='1' and tenant_id='%s'", tenantID)
tenantName, _ = dborm.XormGetSingleColStringByWhere("sys_tenant", "tenant_name", where)
log.Tracef("tenantName: %s tenantID: %s", tenantName, tenantID)
response.Data[i].TenantName = tenantName
services.ResponseWithJson(w, resp.StatusCode(), &response)
return
} else {
var tenantUEInfos []TenantUEInfo
var ueInfos UEInfoResponse
log.Trace("body:", string(resp.Body()))
_ = json.Unmarshal(resp.Body(), &ueInfos)
log.Trace("ueInfos:", ueInfos)
for _, n := range ueInfos.Data {
log.Trace("tenantID, n.IMSI[5:]:", tenantID, n.IMSI[5:])
// "imsi": "imsi-460000100029999",
where = fmt.Sprintf("status='1' and parent_id='%s' and tenancy_type='IMSI' and '%s' like tenancy_key", tenantID, n.IMSI[5:])
has, err := dborm.XormExistTableOne("sys_tenant", where)
if err == nil && has {
n.TenantName = tenantName
tenantUEInfos = append(tenantUEInfos, n)
}
services.ResponseWithJson(w, resp.StatusCode(), &response)
return
} else {
var tenantUEInfos []TenantUEInfo
var ueInfos UEInfoResponse
log.Trace("body:", string(resp.Body()))
_ = json.Unmarshal(resp.Body(), &ueInfos)
log.Trace("ueInfos:", ueInfos)
for _, n := range ueInfos.Data {
log.Trace("tenantID, n.IMSI[5:]:", tenantID, n.IMSI[5:])
// "imsi": "imsi-460000100029999",
where = fmt.Sprintf("status='1' and parent_id='%s' and tenancy_type='IMSI' and '%s' like tenancy_key", tenantID, n.IMSI[5:])
has, err := dborm.XormExistTableOne("sys_tenant", where)
if err == nil && has {
n.TenantName = tenantName
tenantUEInfos = append(tenantUEInfos, n)
}
}
var response UEInfoResponse
response.Data = tenantUEInfos
services.ResponseWithJson(w, resp.StatusCode(), &response)
return
}
var response UEInfoResponse
response.Data = tenantUEInfos
services.ResponseWithJson(w, resp.StatusCode(), &response)
return
}
}
services.ResponseWithJson(w, http.StatusOK, response)
}
// Get UEInfo from NF/NFs, IMS Online users
func GetIMSUEInfoFromNF(w http.ResponseWriter, r *http.Request) {
log.Debug("GetUEInfoFromNF processing... ")
neId := ctx.GetQuery(r, "neId")
neType := "ims"
if neType == "" || neId == "" {
log.Error("elementTypeValue/neId is empty")
services.ResponseNotFound404UriNotExist(w, r)
return
}
var tenantName string
tenantNames := services.GetParamsArrByName("tenantName", r)
if len(tenantNames) > 0 {
tenantName = tenantNames[0]
}
neInfo := neService.NewNeInfoImpl.SelectNeInfoByNeTypeAndNeID(neType, neId)
var response services.MapResponse
if neInfo.NeId == neId && neInfo.NeId != "" {
requestURI2NF := fmt.Sprintf("http://%s:%v%s", neInfo.IP, neInfo.Port, r.RequestURI)
//requestURI2NF := "http://127.0.0.1:4523/m1/3157310-1528434-default/api/rest/ueManagement/v1/elementType/ims/objectType/ueInfo?apifoxApiId=128609458"
log.Debug("requestURI2NF:", requestURI2NF)
resp, err := client.R().
EnableTrace().
SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
Get(requestURI2NF)
if err != nil {
log.Error("Failed to Get from NF:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
// multi-tenancy solution
var tenantID, where string
//var cols []string
if tenantName != "" {
where = fmt.Sprintf("status='1' and tenant_name='%s'", tenantName)
tenantID, _ = dborm.XormGetSingleColStringByWhere("sys_tenant", "tenant_id", where)
} else {
userName := ctx.LoginUserToUserName(r)
where = fmt.Sprintf("status='1' and user_name='%s'", userName)
tenantID, _ = dborm.XormGetSingleColStringByWhere("sys_user", "tenant_id", where)
where = fmt.Sprintf("status='1' and tenant_id='%s'", tenantID)
tenantName, _ = dborm.XormGetSingleColStringByWhere("sys_tenant", "tenant_name", where)
}
if tenantID == "" {
var response TenantImsUEInfoResponse
_ = json.Unmarshal(resp.Body(), &response)
for i := 0; i < len(response.Data); i++ {
where = fmt.Sprintf("status='1' and tenancy_type='IMSI' and '%s' like tenancy_key", response.Data[i].IMSI)
tenantID, _ = dborm.XormGetSingleColStringByWhere("sys_tenant", "parent_id", where)
where = fmt.Sprintf("status='1' and tenant_id='%s'", tenantID)
tenantName, _ = dborm.XormGetSingleColStringByWhere("sys_tenant", "tenant_name", where)
log.Tracef("tenantName: %s tenantID: %s", tenantName, tenantID)
response.Data[i].TenantName = tenantName
}
services.ResponseWithJson(w, resp.StatusCode(), &response)
return
} else {
var tenantUEInfos []TenantImsUEInfo
var ueInfos TenantImsUEInfoResponse
log.Trace("body:", string(resp.Body()))
_ = json.Unmarshal(resp.Body(), &ueInfos)
log.Trace("ueInfos:", ueInfos)
for _, n := range ueInfos.Data {
log.Trace("tenantID, n.IMSI:", tenantID, n.IMSI)
// "imsi": "460000100029999",
where = fmt.Sprintf("status='1' and parent_id='%s' and tenancy_type='IMSI' and '%s' like tenancy_key", tenantID, n.IMSI)
has, err := dborm.XormExistTableOne("sys_tenant", where)
if err == nil && has {
n.TenantName = tenantName
tenantUEInfos = append(tenantUEInfos, n)
}
}
var response TenantImsUEInfoResponse
response.Data = tenantUEInfos
services.ResponseWithJson(w, resp.StatusCode(), &response)
return
}
}
services.ResponseWithJson(w, http.StatusOK, response)
}