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: NRF Server & processor #40

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package main

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

"github.com/urfave/cli"

Expand Down Expand Up @@ -43,13 +48,39 @@ func action(cliCtx *cli.Context) error {

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)
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
defer wg.Done()
<-sigCh // Wait for interrupt signal to gracefully shutdown UPF

logger.MainLog.Warnln("Terminating... (Wait 2s for other NFs to deregister)")
time.Sleep(2 * time.Second) // Waiting for other NFs to deregister
cancel() // Notify each goroutine and wait them stopped
if NRF != nil {
NRF.WaitRoutineStopped()
}
}()

defer func() {
select {
case sigCh <- nil: // Send signal in case of returning with error
default:
}
wg.Wait()
logger.MainLog.Infof("NRF 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
}
Expand Down
34 changes: 20 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,54 @@ require (
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
)

require (
github.com/antihax/optional v1.0.0 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/evanphx/json-patch v0.5.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/cors v1.7.2
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pelletier/go-toml/v2 v2.2.1 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/tim-ywliu/nested-logrus-formatter v1.3.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.0.2 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.0.0-20210810183815-faf39c7919d5 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.0 // indirect
gopkg.in/h2non/gock.v1 v1.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
83 changes: 47 additions & 36 deletions go.sum

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
CfgLog *logrus.Entry
CtxLog *logrus.Entry
GinLog *logrus.Entry
SBILog *logrus.Entry
ConsumerLog *logrus.Entry
NfmLog *logrus.Entry
AccTokenLog *logrus.Entry
Expand All @@ -33,6 +34,7 @@ func init() {
CfgLog = NfLog.WithField(logger_util.FieldCategory, "CFG")
CtxLog = NfLog.WithField(logger_util.FieldCategory, "CTX")
GinLog = NfLog.WithField(logger_util.FieldCategory, "GIN")
SBILog = NfLog.WithField(logger_util.FieldCategory, "SBI")
ConsumerLog = NfLog.WithField(logger_util.FieldCategory, "Consumer")
NfmLog = NfLog.WithField(logger_util.FieldCategory, "NFM")
AccTokenLog = NfLog.WithField(logger_util.FieldCategory, "Token")
Expand Down
84 changes: 0 additions & 84 deletions internal/sbi/accesstoken/routers.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
*
* NRF OAuth2 Authorization
*
* API version: 1.0.0
* API version: 1.2.1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/

package accesstoken
package sbi

import (
"encoding/json"
Expand All @@ -17,17 +17,38 @@ import (
"github.com/gin-gonic/gin"

"github.com/free5gc/nrf/internal/logger"
"github.com/free5gc/nrf/internal/sbi/producer"
"github.com/free5gc/nrf/pkg/factory"
"github.com/free5gc/openapi/models"
"github.com/free5gc/util/httpwrapper"
)

// AccessTokenRequest - Access Token Request
func HTTPAccessTokenRequest(c *gin.Context) {
logger.AccTokenLog.Infoln("In HTTPAccessTokenRequest")
// Index is the index handler.
func Index(c *gin.Context) {
c.String(http.StatusOK, "Hello World!")
}

func (s *Server) getAccessTokenRoutes() []Route {
return []Route{
{
Method: http.MethodGet,
Pattern: "/",
APIFunc: func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"status": "Hello World!"})
},
},
{
Method: http.MethodPost,
Pattern: "/oauth2/token",
APIFunc: s.apiAccessTokenRequest,
},
}
}

func (s *Server) apiAccessTokenRequest(c *gin.Context) {
logger.AccTokenLog.Debugln("Handle AccessTokenRequest")

authEnable := factory.NrfConfig.GetOAuth()

if !factory.NrfConfig.GetOAuth() {
if !authEnable {
rsp := models.ProblemDetails{
Title: "OAuth2 not enable",
Status: http.StatusBadRequest,
Expand Down Expand Up @@ -77,7 +98,9 @@ func HTTPAccessTokenRequest(c *gin.Context) {
}
}

err = c.Bind(&accessTokenReq)
if err1 := s.bindData(c, &accessTokenReq); err1 != nil {
KunLee76 marked this conversation as resolved.
Show resolved Hide resolved
return
}
if err != nil {
problemDetail := "[Request Body] " + err.Error()
rsp := models.ProblemDetails{
Expand All @@ -89,11 +112,6 @@ func HTTPAccessTokenRequest(c *gin.Context) {
c.JSON(http.StatusBadRequest, rsp)
return
}

req := httpwrapper.NewRequest(c.Request, accessTokenReq)
req.Params["paramName"] = c.Params.ByName("paramName")

httpResponse := producer.HandleAccessTokenRequest(req)

c.JSON(httpResponse.Status, httpResponse.Body)
hdlRsp := s.Processor().AccessTokenProcedure(accessTokenReq)
s.buildAndSendHttpResponse(c, hdlRsp, false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,47 @@
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/

package discovery
package sbi

import (
"net/http"

"github.com/gin-gonic/gin"

"github.com/free5gc/nrf/internal/logger"
"github.com/free5gc/nrf/internal/sbi/producer"
"github.com/free5gc/openapi"
"github.com/free5gc/openapi/models"
"github.com/free5gc/util/httpwrapper"
)

func (s *Server) getNFDiscoveryRoutes() []Route {
return []Route{
{
Method: http.MethodGet,
Pattern: "/",
APIFunc: func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"status": "Hello World!"})
},
},
{
Method: http.MethodPost,
Pattern: "/nf-instances",
APIFunc: s.getSearchNFInstances,
},
}
}

// SearchNFInstances - Search a collection of NF Instances
func HTTPSearchNFInstances(c *gin.Context) {
auth_err := authorizationCheck(c)
func (s *Server) getSearchNFInstances(c *gin.Context) {
auth_err := authorizationCheck(c, "nnrf-disc")
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
}

req := httpwrapper.NewRequest(c.Request, nil)
req.Query = c.Request.URL.Query()
httpResponse := producer.HandleNFDiscoveryRequest(req)
httpResponse := s.processor.HandleNFDiscoveryRequest(req)
KunLee76 marked this conversation as resolved.
Show resolved Hide resolved

responseBody, err := openapi.Serialize(httpResponse.Body, "application/json")
if err != nil {
Expand Down
Loading
Loading