Skip to content

Commit

Permalink
refactor: abstract implement to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pf-lin committed May 17, 2024
1 parent fec9afc commit 02adb5d
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 49 deletions.
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,12 @@ require (
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.0.0-20210810183815-faf39c7919d5 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.17.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
mvdan.cc/gofumpt v0.6.0 // indirect
)
15 changes: 0 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
Expand Down Expand Up @@ -169,8 +168,6 @@ github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZX
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -201,9 +198,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
Expand Down Expand Up @@ -288,8 +282,6 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -333,8 +325,6 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -416,12 +406,9 @@ golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
Expand Down Expand Up @@ -520,8 +507,6 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
mvdan.cc/gofumpt v0.6.0 h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo=
mvdan.cc/gofumpt v0.6.0/go.mod h1:4L0wf+kgIPZtcCWXynNS2e6bhmj73umwnuXSZarixzA=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
Expand Down
2 changes: 1 addition & 1 deletion internal/sbi/api_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *Server) HTTPChargingNotification(c *gin.Context) {
reqWrapper.Params["notifyUri"] = c.Params.ByName("notifyUri")
smContextRef := strings.Split(reqWrapper.Params["notifyUri"], "_")[1]

HTTPResponse := s.processor.HandleChargingNotification(reqWrapper.Body.(models.ChargingNotifyRequest), smContextRef)
HTTPResponse := s.Processor().HandleChargingNotification(reqWrapper.Body.(models.ChargingNotifyRequest), smContextRef)

for key, val := range HTTPResponse.Header {
c.Header(key, val[0])
Expand Down
4 changes: 2 additions & 2 deletions internal/sbi/api_oam.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func (s *Server) HTTPGetUEPDUSessionInfo(c *gin.Context) {
req.Params["smContextRef"] = c.Params.ByName("smContextRef")

smContextRef := req.Params["smContextRef"]
HTTPResponse := s.processor.HandleOAMGetUEPDUSessionInfo(smContextRef)
HTTPResponse := s.Processor().HandleOAMGetUEPDUSessionInfo(smContextRef)

c.JSON(HTTPResponse.Status, HTTPResponse.Body)
}

func (s *Server) HTTPGetSMFUserPlaneInfo(c *gin.Context) {
HTTPResponse := s.processor.HandleGetSMFUserPlaneInfo()
HTTPResponse := s.Processor().HandleGetSMFUserPlaneInfo()

c.JSON(HTTPResponse.Status, HTTPResponse.Body)
}
6 changes: 3 additions & 3 deletions internal/sbi/api_pdusession.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (s *Server) HTTPReleaseSmContext(c *gin.Context) {
req.Params["smContextRef"] = c.Params.ByName("smContextRef")

smContextRef := req.Params["smContextRef"]
s.processor.HandlePDUSessionSMContextRelease(
s.Processor().HandlePDUSessionSMContextRelease(
smContextRef, req.Body.(models.ReleaseSmContextRequest))

c.Status(http.StatusNoContent)
Expand Down Expand Up @@ -127,7 +127,7 @@ func (s *Server) HTTPUpdateSmContext(c *gin.Context) {
req.Params["smContextRef"] = c.Params.ByName("smContextRef")

smContextRef := req.Params["smContextRef"]
HTTPResponse := s.processor.HandlePDUSessionSMContextUpdate(
HTTPResponse := s.Processor().HandlePDUSessionSMContextUpdate(
smContextRef, req.Body.(models.UpdateSmContextRequest))

if HTTPResponse.Status < 300 {
Expand Down Expand Up @@ -172,7 +172,7 @@ func (s *Server) HTTPPostSmContexts(c *gin.Context) {

req := httpwrapper.NewRequest(c.Request, request)
isDone := c.Done()
HTTPResponse := s.processor.HandlePDUSessionSMContextCreate(isDone,
HTTPResponse := s.Processor().HandlePDUSessionSMContextCreate(isDone,
req.Body.(models.PostSmContextsRequest))
// Http Response to AMF
for key, val := range HTTPResponse.Header {
Expand Down
4 changes: 2 additions & 2 deletions internal/sbi/api_upi.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *Server) PostUpNodesLinks(c *gin.Context) {
// only associate new ones
if upf.UPF.UPFStatus == smf_context.NotAssociated {
upf.UPF.Ctx, upf.UPF.CancelFunc = context.WithCancel(context.Background())
go association.ToBeAssociatedWithUPF(smf_context.GetSelf().Ctx, upf.UPF, s.processor)
go association.ToBeAssociatedWithUPF(smf_context.GetSelf().Ctx, upf.UPF, s.Processor())
}
}
c.JSON(http.StatusOK, gin.H{"status": "OK"})
Expand All @@ -97,7 +97,7 @@ func (s *Server) DeleteUpNodeLink(c *gin.Context) {
defer upi.Mu.Unlock()
if upNode, ok := upi.UPNodes[upNodeRef]; ok {
if upNode.Type == smf_context.UPNODE_UPF {
go association.ReleaseAllResourcesOfUPF(upNode.UPF, s.processor)
go association.ReleaseAllResourcesOfUPF(upNode.UPF, s.Processor())
}
upi.UpNodeDelete(upNodeRef)
upNode.UPF.CancelFunc()
Expand Down
6 changes: 4 additions & 2 deletions internal/sbi/processor/pdu_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"github.com/free5gc/smf/internal/pfcp/udp"
"github.com/free5gc/smf/internal/sbi/consumer"
"github.com/free5gc/smf/internal/sbi/processor"
"github.com/free5gc/smf/pkg/app"
"github.com/free5gc/smf/pkg/factory"
"github.com/free5gc/smf/pkg/service"
"github.com/free5gc/util/httpwrapper"
)

Expand Down Expand Up @@ -575,7 +575,7 @@ func TestHandlePDUSessionSMContextCreate(t *testing.T) {
}
}

mockSmf := app.NewMockApp(gomock.NewController(t))
mockSmf := service.NewMockSmfAppInterface(gomock.NewController(t))
consumer, err := consumer.NewConsumer(mockSmf)
if err != nil {
t.Fatalf("Failed to create consumer: %+v", err)
Expand All @@ -585,6 +585,8 @@ func TestHandlePDUSessionSMContextCreate(t *testing.T) {
t.Fatalf("Failed to create processor: %+v", err)
}

service.SMF = mockSmf

for _, tc := range testCases {
t.Run(tc.paramStr, func(t *testing.T) {
httpResp := processor.HandlePDUSessionSMContextCreate(nil, tc.request)
Expand Down
12 changes: 8 additions & 4 deletions internal/sbi/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import (
"github.com/free5gc/smf/pkg/app"
)

type Processor struct {
type ProcessorSmf interface {
app.App
}

type Processor struct {
ProcessorSmf

consumer *consumer.Consumer
}
Expand All @@ -17,10 +21,10 @@ type HandlerResponse struct {
Body interface{}
}

func NewProcessor(smf app.App, consumer *consumer.Consumer) (*Processor, error) {
func NewProcessor(smf ProcessorSmf, consumer *consumer.Consumer) (*Processor, error) {
p := &Processor{
App: smf,
consumer: consumer,
ProcessorSmf: smf,
consumer: consumer,
}
return p, nil
}
Expand Down
24 changes: 13 additions & 11 deletions internal/sbi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,23 @@ func applyRoutes(group *gin.RouterGroup, routes []Route) {
}
}

type Server struct {
type ServerSmf interface {
app.App

Consumer() *consumer.Consumer
Processor() *processor.Processor
}

type Server struct {
ServerSmf

httpServer *http.Server
router *gin.Engine
consumer *consumer.Consumer
processor *processor.Processor
}

func NewServer(
smf app.App, tlsKeyLogPath string, consumer *consumer.Consumer, processor *processor.Processor,
) (_ *Server, err error) {
func NewServer(smf ServerSmf, tlsKeyLogPath string) (*Server, error) {
s := &Server{
App: smf,
consumer: consumer,
processor: processor,
ServerSmf: smf,
}

smf_context.InitSmfContext(factory.SmfConfig)
Expand All @@ -75,6 +76,7 @@ func NewServer(
s.router = newRouter(s)

bindAddr := fmt.Sprintf("%s:%d", s.Context().BindingIPv4, s.Context().SBIPort)
var err error
if s.httpServer, err = httpwrapper.NewHttp2Server(bindAddr, tlsKeyLogPath, s.router); err != nil {
logger.InitLog.Errorf("Initialize HTTP server failed: %v", err)
return nil, err
Expand Down Expand Up @@ -127,9 +129,9 @@ func newRouter(s *Server) *gin.Engine {
}

func (s *Server) Run(traceCtx context.Context, wg *sync.WaitGroup) error {
err := s.consumer.RegisterNFInstance()
err := s.Consumer().RegisterNFInstance()
if err != nil {
retry_err := s.consumer.RetrySendNFRegistration(10)
retry_err := s.Consumer().RetrySendNFRegistration(10)
if retry_err != nil {
logger.InitLog.Errorln(retry_err)
return err
Expand Down
17 changes: 12 additions & 5 deletions pkg/service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ import (
"github.com/free5gc/smf/pkg/factory"
)

var SMF *SmfApp
type SmfAppInterface interface {
app.App

Consumer() *consumer.Consumer
Processor() *processor.Processor
}

var SMF SmfAppInterface

type SmfApp struct {
SmfAppInterface

cfg *factory.Config
smfCtx *smf_context.SMFContext
ctx context.Context
Expand All @@ -35,9 +44,7 @@ type SmfApp struct {
pfcpTerminate func()
}

var _ app.App = &SmfApp{}

func GetApp() *SmfApp {
func GetApp() SmfAppInterface {
return SMF
}

Expand Down Expand Up @@ -70,7 +77,7 @@ func NewApp(
smf.processor = processor

// TODO: Initialize sbi server
sbiServer, err := sbi.NewServer(smf, tlsKeyLogPath, consumer, processor)
sbiServer, err := sbi.NewServer(smf, tlsKeyLogPath)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 02adb5d

Please sign in to comment.