Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: SBI logic #42

Merged
merged 12 commits into from
Jun 25, 2024
366 changes: 210 additions & 156 deletions .golangci.yml

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"context"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/urfave/cli"

Expand Down Expand Up @@ -40,23 +43,30 @@ func action(cliCtx *cli.Context) error {
if err != nil {
return err
}

logger.MainLog.Infoln("NRF version: ", version.GetVersion())

ctx, cancel := context.WithCancel(context.Background())

sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigCh // Wait for interrupt signal to gracefully shutdown
cancel() // Notify each goroutine and wait them stopped
}()

cfg, err := factory.ReadConfig(cliCtx.String("config"))
if err != nil {
return err
}
factory.NrfConfig = cfg

nrf, err := service.NewApp(cfg)
nrf, err := service.NewApp(ctx, cfg, tlsKeyLogPath)
if err != nil {
return err
}
NRF = nrf

nrf.Start(tlsKeyLogPath)

nrf.Start()
return nil
}

Expand All @@ -81,6 +91,5 @@ func initLogFile(logNfPath []string) (string, error) {
_, name := filepath.Split(factory.NrfDefaultTLSKeyLogPath)
logTlsKeyPath = filepath.Join(tmpDir, name)
}

return logTlsKeyPath, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/free5gc/openapi v1.0.8
github.com/free5gc/openapi v1.0.9-0.20240503143645-eac9f06c2f6b
github.com/free5gc/util v1.0.6
github.com/gin-gonic/gin v1.9.1
github.com/golang-jwt/jwt v3.2.1+incompatible
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ github.com/evanphx/json-patch v0.5.2 h1:xVCHIVMUu1wtM/VkR9jVZ45N3FhZfYMMYGorLCR8
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
github.com/free5gc/openapi v1.0.8 h1:QjfQdB6VVA1GRnzOJ7nILzrI7gMiY0lH64JHVW7vF34=
github.com/free5gc/openapi v1.0.8/go.mod h1:w6y9P/uySczc1d9OJZAEuB2FImR/z60Wg2BekPAVt3M=
github.com/free5gc/openapi v1.0.9-0.20240503143645-eac9f06c2f6b h1:+VcgZq+3apB6Xr4jEqgGf/uAECRF038SwixEvvxhYrM=
github.com/free5gc/openapi v1.0.9-0.20240503143645-eac9f06c2f6b/go.mod h1:0qRW+H1/Nyzw5tjjvyp+90m+2SOZZefGQC9QV8iPwu8=
github.com/free5gc/util v1.0.6 h1:dBt9drcXtYKE/cY5XuQcuffgsYclPIpIArhSeS6M+DQ=
github.com/free5gc/util v1.0.6/go.mod h1:eSGN7POUM8LNTvg/E591XR6447a6/w1jFWGKNZPHcXw=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
Expand Down
30 changes: 28 additions & 2 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"

"github.com/google/uuid"
"github.com/pkg/errors"
Expand All @@ -25,8 +26,20 @@ type NRFContext struct {
NrfPrivKey *rsa.PrivateKey
NrfPubKey *rsa.PublicKey
NrfCert *x509.Certificate
NfRegistNum int
nfRegistNumLock sync.RWMutex
}

const (
NfProfileCollName string = "NfProfile"
)

type NFContext interface {
AuthorizationCheck(token string, serviceName models.ServiceName) error
}

var _ NFContext = &NRFContext{}

var nrfContext NRFContext

func InitNrfContext() error {
Expand All @@ -38,6 +51,7 @@ func InitNrfContext() error {
nrfContext.NrfNfProfile.NfInstanceId = uuid.New().String()
nrfContext.NrfNfProfile.NfType = models.NfType_NRF
nrfContext.NrfNfProfile.NfStatus = models.NfStatus_REGISTERED
nrfContext.NfRegistNum = 0

serviceNameList := configuration.ServiceNameList

Expand Down Expand Up @@ -189,14 +203,26 @@ func GetSelf() *NRFContext {
return &nrfContext
}

func (context *NRFContext) AuthorizationCheck(token, serviceName string) error {
func (context *NRFContext) AuthorizationCheck(token string, serviceName models.ServiceName) error {
if !factory.NrfConfig.GetOAuth() {
return nil
}
err := oauth.VerifyOAuth(token, serviceName, factory.NrfConfig.GetNrfCertPemPath())
err := oauth.VerifyOAuth(token, string(serviceName), factory.NrfConfig.GetNrfCertPemPath())
if err != nil {
logger.AccTokenLog.Warningln("AuthorizationCheck:", err)
return err
}
return nil
}

func (ctx *NRFContext) AddNfRegister() {
ctx.nfRegistNumLock.Lock()
defer ctx.nfRegistNumLock.Unlock()
ctx.NfRegistNum += 1
}

func (ctx *NRFContext) DelNfRegister() {
ctx.nfRegistNumLock.Lock()
defer ctx.nfRegistNumLock.Unlock()
ctx.NfRegistNum -= 1
}
13 changes: 6 additions & 7 deletions internal/context/dataconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// Ipv6ToInt - Convert Ipv6 string to *bigInt
func Ipv6ToInt(ipv6 string) *big.Int {
ipv6 = ipv6 + "/32"
ipv6 += "/32"
ip, _, err := net.ParseCIDR(ipv6)
if err != nil {
fmt.Println("Error", ip, err)
Expand All @@ -24,7 +24,7 @@ func Ipv6ToInt(ipv6 string) *big.Int {

// Ipv4ToInt - Convert Ipv4 string to int64
func Ipv4ToInt(ipv4 string) int64 {
ipv4 = ipv4 + "/24"
ipv4 += "/24"
ip, _, err := net.ParseCIDR(ipv4)
if err != nil {
fmt.Println("Error", ip, err)
Expand Down Expand Up @@ -70,14 +70,13 @@ func EncodeGroupId(groupId string) string {
for i := 0; i < (10 - len(externalGroupIdentitySplit[0])); i++ {
groupServiceIdentifierPadding += "0"
}
encodedGroupId = encodedGroupId + groupServiceIdentifierPadding + strconv.Itoa(encodedGroupServiceIdentifier)

encodedGroupId = encodedGroupId + externalGroupIdentitySplit[1]
encodedGroupId += groupServiceIdentifierPadding + strconv.Itoa(encodedGroupServiceIdentifier)
encodedGroupId += externalGroupIdentitySplit[1]

if len(externalGroupIdentitySplit[2]) == 2 {
encodedGroupId = encodedGroupId + "0" + externalGroupIdentitySplit[2]
encodedGroupId += "0" + externalGroupIdentitySplit[2]
} else {
encodedGroupId = encodedGroupId + externalGroupIdentitySplit[2]
encodedGroupId += externalGroupIdentitySplit[2]
}

var encodedLocalGroupId int
Expand Down
24 changes: 13 additions & 11 deletions internal/context/management_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ func NnrfNFManagementDataModel(nf *models.NfProfile, nfprofile models.NfProfile)
}

func SetsubscriptionId() (string, error) {
buffer := make([]byte, 16)
subscriptionIdSize := 16

buffer := make([]byte, subscriptionIdSize)
_, err := rand.Read(buffer)
if err != nil {
return "", err
Expand Down Expand Up @@ -454,7 +456,7 @@ func setUriListByFilter(filter bson.M, uriList *[]string) {
}

var filterNfTypeResults []models.NrfSubscriptionData
if err := openapi.Convert(filterNfTypeResultsRaw, &filterNfTypeResults); err != nil {
if err = openapi.Convert(filterNfTypeResultsRaw, &filterNfTypeResults); err != nil {
logger.NfmLog.Errorf("setUriListByFilter err: %+v", err)
}

Expand All @@ -463,7 +465,7 @@ func setUriListByFilter(filter bson.M, uriList *[]string) {
}
}

func nnrfUriList(originalUL *UriList, UL *UriList, location []string) {
func nnrfUriList(originalUL *UriList, ul *UriList, location []string) {
var i int
var b *Links
var flag bool
Expand Down Expand Up @@ -495,7 +497,7 @@ func nnrfUriList(originalUL *UriList, UL *UriList, location []string) {
}

b.Item = c
UL.Link = *b
ul.Link = *b
}

func GetNofificationUri(nfProfile models.NfProfile) []string {
Expand Down Expand Up @@ -536,8 +538,8 @@ func GetNofificationUri(nfProfile models.NfProfile) []string {
if nfProfile.AmfInfo != nil {
amfCond := bson.M{
"subscrCond": bson.M{
"amfSetId": (*nfProfile.AmfInfo).AmfSetId,
"amfRegionId": (*nfProfile.AmfInfo).AmfRegionId,
"amfSetId": nfProfile.AmfInfo.AmfSetId,
"amfRegionId": nfProfile.AmfInfo.AmfRegionId,
},
}
setUriListByFilter(amfCond, &uriList)
Expand All @@ -546,9 +548,9 @@ func GetNofificationUri(nfProfile models.NfProfile) []string {
// GuamiListCond
if nfProfile.AmfInfo != nil {
var guamiListFilter bson.M
if (*nfProfile.AmfInfo).GuamiList != nil {
if nfProfile.AmfInfo.GuamiList != nil {
var guamiListBsonArray bson.A
for _, guami := range *(*nfProfile.AmfInfo).GuamiList {
for _, guami := range *nfProfile.AmfInfo.GuamiList {
tmp, err := json.Marshal(guami)
if err != nil {
logger.NfmLog.Error(err)
Expand Down Expand Up @@ -623,23 +625,23 @@ func GetNofificationUri(nfProfile models.NfProfile) []string {
nfGroupCond := bson.M{
"subscrCond": bson.M{
"nfType": nfProfile.NfType,
"nfGroupId": (*nfProfile.UdrInfo).GroupId,
"nfGroupId": nfProfile.UdrInfo.GroupId,
},
}
setUriListByFilter(nfGroupCond, &uriList)
} else if nfProfile.UdmInfo != nil {
nfGroupCond := bson.M{
"subscrCond": bson.M{
"nfType": nfProfile.NfType,
"nfGroupId": (*nfProfile.UdmInfo).GroupId,
"nfGroupId": nfProfile.UdmInfo.GroupId,
},
}
setUriListByFilter(nfGroupCond, &uriList)
} else if nfProfile.AusfInfo != nil {
nfGroupCond := bson.M{
"subscrCond": bson.M{
"nfType": nfProfile.NfType,
"nfGroupId": (*nfProfile.AusfInfo).GroupId,
"nfGroupId": nfProfile.AusfInfo.GroupId,
},
}
setUriListByFilter(nfGroupCond, &uriList)
Expand Down
4 changes: 4 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ var (
InitLog *logrus.Entry
CfgLog *logrus.Entry
CtxLog *logrus.Entry
SBILog *logrus.Entry
GinLog *logrus.Entry
ConsumerLog *logrus.Entry
NfmLog *logrus.Entry
AccTokenLog *logrus.Entry
DiscLog *logrus.Entry
UtilLog *logrus.Entry
)

func init() {
Expand All @@ -32,9 +34,11 @@ func init() {
InitLog = NfLog.WithField(logger_util.FieldCategory, "Init")
CfgLog = NfLog.WithField(logger_util.FieldCategory, "CFG")
CtxLog = NfLog.WithField(logger_util.FieldCategory, "CTX")
SBILog = NfLog.WithField(logger_util.FieldCategory, "SBI")
GinLog = NfLog.WithField(logger_util.FieldCategory, "GIN")
ConsumerLog = NfLog.WithField(logger_util.FieldCategory, "Consumer")
NfmLog = NfLog.WithField(logger_util.FieldCategory, "NFM")
AccTokenLog = NfLog.WithField(logger_util.FieldCategory, "Token")
DiscLog = NfLog.WithField(logger_util.FieldCategory, "DISC")
UtilLog = NfLog.WithField(logger_util.FieldCategory, "Util")
}
84 changes: 0 additions & 84 deletions internal/sbi/accesstoken/routers.go

This file was deleted.

Loading
Loading