multi-tenant
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/lib/core/utils/ctx"
|
||||
"be.ems/lib/dborm"
|
||||
"be.ems/lib/global"
|
||||
"be.ems/lib/log"
|
||||
@@ -18,38 +19,11 @@ import (
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// AmfNBInfo AMF的NodeB信息
|
||||
type AmfNBInfo struct {
|
||||
ID string `json:"id"` //NodeB ID
|
||||
Name string `json:"name"` // NodeB name
|
||||
Address string `json:"address"` // 基站地址
|
||||
UENum int `jons:"ueNum"` // UE数量
|
||||
}
|
||||
|
||||
// SmfUENum SMF在线用户数
|
||||
type SmfUENum struct {
|
||||
UENum int `json:"ueNum"` // 当前在线用户数
|
||||
}
|
||||
|
||||
// SmfUEInfo SMF在线用户信息
|
||||
type SmfUEInfo struct {
|
||||
IMSI string `json:"imsi"`
|
||||
MSISDN string `json:"msisdn"`
|
||||
RatType string `json:"ratType"`
|
||||
PduSessionInfo []struct {
|
||||
PduSessionID int `json:"pduSessionID"`
|
||||
IPv4 string `json:"ipv4"`
|
||||
IPv6 string `json:"ipv6"`
|
||||
Dnn string `json:"dnn"`
|
||||
Tai string `json:"tai"`
|
||||
SstSD string `json:"sstSD"`
|
||||
UpfN3IP string `json:"upfN3IP"`
|
||||
RanN3IP string `json:"ranN3IP"`
|
||||
Activetime string `json:"activeTime"`
|
||||
UpState string `json:"upState"`
|
||||
} `json:"pduSessionInfo"`
|
||||
}
|
||||
|
||||
// ImsUEInfo IMS在线用户信息
|
||||
type ImsUEInfo struct {
|
||||
IMSI string `json:"imsi"`
|
||||
@@ -239,7 +213,53 @@ func GetSubscriptionsFromNSSF(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Get UEInfo from NF/NFs
|
||||
// SmfUEInfo SMF在线用户信息
|
||||
type SmfUEInfo struct {
|
||||
IMSI string `json:"imsi"`
|
||||
MSISDN string `json:"msisdn"`
|
||||
RatType string `json:"ratType"`
|
||||
PduSessionInfo []struct {
|
||||
PduSessionID int `json:"pduSessionID"`
|
||||
IPv4 string `json:"ipv4"`
|
||||
IPv6 string `json:"ipv6"`
|
||||
Dnn string `json:"dnn"`
|
||||
Tai string `json:"tai"`
|
||||
SstSD string `json:"sstSD"`
|
||||
UpfN3IP string `json:"upfN3IP"`
|
||||
RanN3IP string `json:"ranN3IP"`
|
||||
Activetime string `json:"activeTime"`
|
||||
UpState string `json:"upState"`
|
||||
} `json:"pduSessionInfo"`
|
||||
}
|
||||
|
||||
type TenantUEInfo struct {
|
||||
IMSI string `json:"imsi"`
|
||||
MSISDN string `json:"msisdn"`
|
||||
RatType string `json:"ratType"`
|
||||
PduSessionInfo []struct {
|
||||
PduSessionID int `json:"pduSessionID"`
|
||||
IPv4 string `json:"ipv4"`
|
||||
IPv6 string `json:"ipv6"`
|
||||
Dnn string `json:"dnn"`
|
||||
Tai string `json:"tai"`
|
||||
SstSD string `json:"sstSD"`
|
||||
UpfN3IP string `json:"upfN3IP"`
|
||||
RanN3IP string `json:"ranN3IP"`
|
||||
Activetime string `json:"activeTime"`
|
||||
UpState string `json:"upState"`
|
||||
} `json:"pduSessionInfo"`
|
||||
TenantName string `json:"tenantName"`
|
||||
}
|
||||
|
||||
type SmfUEInfoResponse struct {
|
||||
Data []SmfUEInfo `json:"data"`
|
||||
}
|
||||
|
||||
type UEInfoResponse struct {
|
||||
Data []TenantUEInfo `json:"data"`
|
||||
}
|
||||
|
||||
// Get UEInfo from NF/NFs, SMF Online users
|
||||
func GetUEInfoFromNF(w http.ResponseWriter, r *http.Request) {
|
||||
log.Info("GetUEInfoFromNF processing... ")
|
||||
|
||||
@@ -258,7 +278,11 @@ func GetUEInfoFromNF(w http.ResponseWriter, r *http.Request) {
|
||||
services.ResponseNotFound404UriNotExist(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var tenantName string
|
||||
tenantNames := services.GetParamsArrByName("tenantName", r)
|
||||
if len(tenantNames) > 0 {
|
||||
tenantName = tenantNames[0]
|
||||
}
|
||||
// token, err := services.CheckFrontValidRequest(w, r)
|
||||
// if err != nil {
|
||||
// log.Error("Request error:", err)
|
||||
@@ -281,6 +305,9 @@ func GetUEInfoFromNF(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
hostUri := fmt.Sprintf("http://%s:%v", neInfo.Ip, neInfo.Port)
|
||||
requestURI2NF := fmt.Sprintf("%s%s", hostUri, r.RequestURI)
|
||||
// for apifox mock test
|
||||
// hostUri := "http://127.0.0.1:4523"
|
||||
// requestURI2NF := hostUri + "/m1/3157310-1528434-default/api/rest/ueManagement/v1/elementType/smf/objectType/ueInfo?apifoxApiId=128609449"
|
||||
|
||||
log.Debug("requestURI2NF:", requestURI2NF)
|
||||
|
||||
@@ -296,14 +323,56 @@ func GetUEInfoFromNF(w http.ResponseWriter, r *http.Request) {
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
return
|
||||
} else {
|
||||
var response services.MapResponse
|
||||
_ = json.Unmarshal(resp.Body(), &response)
|
||||
services.ResponseWithJson(w, resp.StatusCode(), response)
|
||||
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 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)
|
||||
} 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// POST User Info from NF/NFs
|
||||
// POST PCF User Info from NF/NFs
|
||||
func PostPCFUserInfo(w http.ResponseWriter, r *http.Request) {
|
||||
log.Info("PostPCFUserInfo processing... ")
|
||||
|
||||
@@ -580,7 +649,27 @@ func GetUENumFromNF(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Get Radio Info from NF/NFs
|
||||
// AmfNBInfo AMF的NodeB信息
|
||||
type AmfNBInfo struct {
|
||||
ID string `json:"id"` //NodeB ID
|
||||
Name string `json:"name"` // NodeB name
|
||||
Address string `json:"address"` // 基站地址
|
||||
UENum int `json:"ueNum"` // UE数量
|
||||
}
|
||||
|
||||
type TenantNBInfo struct {
|
||||
ID string `json:"id"` //NodeB ID
|
||||
Name string `json:"name"` // NodeB name
|
||||
Address string `json:"address"` // 基站地址
|
||||
UENum int `json:"ueNum"` // UE数量
|
||||
TenantName string `json:"tenantName"` // Tenant Name
|
||||
}
|
||||
|
||||
type NBInfoResponse struct {
|
||||
Data []TenantNBInfo `json:"data"`
|
||||
}
|
||||
|
||||
// Get AMF Radio Info from NF/NFs
|
||||
func GetNBInfoFromNF(w http.ResponseWriter, r *http.Request) {
|
||||
log.Info("GetNBInfoFromNF processing... ")
|
||||
|
||||
@@ -599,7 +688,11 @@ func GetNBInfoFromNF(w http.ResponseWriter, r *http.Request) {
|
||||
services.ResponseNotFound404UriNotExist(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var tenantName string
|
||||
tenantNames := services.GetParamsArrByName("tenantName", r)
|
||||
if len(tenantNames) > 0 {
|
||||
tenantName = tenantNames[0]
|
||||
}
|
||||
// token, err := services.CheckFrontValidRequest(w, r)
|
||||
// if err != nil {
|
||||
// log.Error("Request error:", err)
|
||||
@@ -623,6 +716,10 @@ func GetNBInfoFromNF(w http.ResponseWriter, r *http.Request) {
|
||||
hostUri := fmt.Sprintf("http://%s:%v", neInfo.Ip, neInfo.Port)
|
||||
requestURI2NF := fmt.Sprintf("%s%s", hostUri, r.RequestURI)
|
||||
|
||||
// for apifox mock test
|
||||
// hostUri := "http://127.0.0.1:4523"
|
||||
// requestURI2NF := hostUri + "/m1/3157310-1528434-default/api/rest/ueManagement/v1/elementType/amf/objectType/nbInfo?apifoxApiId=128848301"
|
||||
|
||||
log.Debug("requestURI2NF:", requestURI2NF)
|
||||
|
||||
resp, err := client.R().
|
||||
@@ -637,10 +734,52 @@ func GetNBInfoFromNF(w http.ResponseWriter, r *http.Request) {
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
return
|
||||
} else {
|
||||
var response services.MapResponse
|
||||
_ = json.Unmarshal(resp.Body(), &response)
|
||||
services.ResponseWithJson(w, resp.StatusCode(), response)
|
||||
return
|
||||
// multi-tenancy solution
|
||||
var tenantID, where, userName 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)
|
||||
}
|
||||
log.Tracef("tenantName: %s userName: %s tenantID: %s", tenantName, userName, tenantID)
|
||||
if tenantID == "" {
|
||||
var response NBInfoResponse
|
||||
//var nbInfo *TenantNBInfo
|
||||
_ = json.Unmarshal(resp.Body(), &response)
|
||||
for i := 0; i < len(response.Data); i++ {
|
||||
where = fmt.Sprintf("status='1' and tenancy_type='RADIO' and tenancy_key='%s'", response.Data[i].ID)
|
||||
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)
|
||||
} else {
|
||||
// var response services.MapResponse
|
||||
var tenantNBInfos []TenantNBInfo
|
||||
var nbInfos NBInfoResponse
|
||||
var nbIDs []string
|
||||
// Get nodeB IDs by tenantID
|
||||
where = fmt.Sprintf("status='1' and tenancy_type='RADIO' and parent_id='%s'", tenantID)
|
||||
dborm.XormFindColStringArrayByWhere("sys_tenant", "tenancy_key", where, &nbIDs)
|
||||
_ = json.Unmarshal(resp.Body(), &nbInfos)
|
||||
for _, n := range nbInfos.Data {
|
||||
if global.IsMember(n.ID, nbIDs) {
|
||||
n.TenantName = tenantName
|
||||
tenantNBInfos = append(tenantNBInfos, n)
|
||||
}
|
||||
}
|
||||
log.Tracef("nbIDs: %v", nbIDs)
|
||||
nbInfos.Data = tenantNBInfos
|
||||
services.ResponseWithJson(w, resp.StatusCode(), &nbInfos)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user