Skip to content

Commit

Permalink
Merge pull request #24 from andy89923/refactor/processor
Browse files Browse the repository at this point in the history
Refactor: processor
  • Loading branch information
ianchen0119 authored Apr 30, 2024
2 parents 3c7f4b5 + e9da28f commit a722608
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 282 deletions.
8 changes: 5 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import (
"github.com/urfave/cli"

"github.com/free5gc/chf/internal/logger"
"github.com/free5gc/chf/internal/repository"
"github.com/free5gc/chf/pkg/factory"
"github.com/free5gc/chf/pkg/service"
logger_util "github.com/free5gc/util/logger"
"github.com/free5gc/util/version"
)

var CHF = &service.ChfApp{}
var CHF *service.ChfApp

func main() {
defer func() {
Expand Down Expand Up @@ -81,15 +82,16 @@ func action(cliCtx *cli.Context) error {
return err
}
factory.ChfConfig = cfg
runtimeRepo := repository.NewRuntimeRepository(cfg)

chf, err := service.NewApp(ctx, cfg, tlsKeyLogPath)
chf, err := service.NewApp(ctx, runtimeRepo, tlsKeyLogPath)
if err != nil {
sigCh <- nil
return err
}
CHF = chf

chf.Start()
chf.Start(tlsKeyLogPath)
CHF.WaitRoutineStopped()

return nil
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ require (
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.opencensus.io v0.24.0 // 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/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/api v0.114.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -657,8 +657,8 @@ golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -686,8 +686,8 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -761,13 +761,13 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
26 changes: 26 additions & 0 deletions internal/repository/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package repository

import (
chf_context "github.com/free5gc/chf/internal/context"
"github.com/free5gc/chf/pkg/factory"
)

type RuntimeRepository struct {
config *factory.Config
chfCtx *chf_context.CHFContext
}

func NewRuntimeRepository(cfg *factory.Config) *RuntimeRepository {
return &RuntimeRepository{
config: cfg,
chfCtx: chf_context.GetSelf(),
}
}

func (rr RuntimeRepository) Config() *factory.Config {
return rr.config
}

func (rr RuntimeRepository) Context() *chf_context.CHFContext {
return rr.chfCtx
}
69 changes: 7 additions & 62 deletions internal/sbi/api_convergedcharging.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ import (
"github.com/gin-gonic/gin"

"github.com/free5gc/chf/internal/logger"
"github.com/free5gc/util/httpwrapper"
)

// Index is the index handler.
func Index(c *gin.Context) {
c.String(http.StatusOK, "Hello World!")
}

func (s *Server) getConvergenChargingEndpoints() []Endpoint {
return []Endpoint{
func (s *Server) getConvergenChargingRoutes() []Route {
return []Route{
{
Method: http.MethodGet,
Pattern: "/",
Expand Down Expand Up @@ -92,27 +91,9 @@ func (s *Server) ChargingdataChargingDataRefReleasePost(c *gin.Context) {
c.JSON(http.StatusBadRequest, rsp)
return
}
chargingSessionId := c.Param("ChargingDataRef")

req := httpwrapper.NewRequest(c.Request, chargingDataReq)
req.Params["ChargingDataRef"] = c.Param("ChargingDataRef")

rsp := s.Processor().HandleChargingdataRelease(req)

for key, value := range rsp.Header {
c.Header(key, value[0])
}
responseBody, err := openapi.Serialize(rsp.Body, "application/json")
if err != nil {
logger.ChargingdataPostLog.Errorln(err)
problemDetails := models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "SYSTEM_FAILURE",
Detail: err.Error(),
}
c.JSON(http.StatusInternalServerError, problemDetails)
} else {
c.Data(rsp.Status, "application/json", responseBody)
}
s.Processor().HandleChargingdataRelease(c, chargingDataReq, chargingSessionId)
}

// ChargingdataChargingDataRefUpdatePost
Expand Down Expand Up @@ -144,27 +125,9 @@ func (s *Server) ChargingdataChargingDataRefUpdatePost(c *gin.Context) {
c.JSON(http.StatusBadRequest, rsp)
return
}
chargingSessionId := c.Param("ChargingDataRef")

req := httpwrapper.NewRequest(c.Request, chargingDataReq)
req.Params["ChargingDataRef"] = c.Param("ChargingDataRef")

rsp := s.Processor().HandleChargingdataUpdate(req)

for key, value := range rsp.Header {
c.Header(key, value[0])
}
responseBody, err := openapi.Serialize(rsp.Body, "application/json")
if err != nil {
logger.ChargingdataPostLog.Errorln(err)
problemDetails := models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "SYSTEM_FAILURE",
Detail: err.Error(),
}
c.JSON(http.StatusInternalServerError, problemDetails)
} else {
c.Data(rsp.Status, "application/json", responseBody)
}
s.Processor().HandleChargingdataUpdate(c, chargingDataReq, chargingSessionId)
}

// ChargingdataPost
Expand Down Expand Up @@ -197,25 +160,7 @@ func (s *Server) ChargingdataPost(c *gin.Context) {
return
}

req := httpwrapper.NewRequest(c.Request, chargingDataReq)

rsp := s.Processor().HandleChargingdataInitial(req)

for key, value := range rsp.Header {
c.Header(key, value[0])
}
responseBody, err := openapi.Serialize(rsp.Body, "application/json")
if err != nil {
logger.ChargingdataPostLog.Errorln(err)
problemDetails := models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "SYSTEM_FAILURE",
Detail: err.Error(),
}
c.JSON(http.StatusInternalServerError, problemDetails)
} else {
c.Data(rsp.Status, "application/json", responseBody)
}
s.Processor().HandleChargingdataInitial(c, chargingDataReq)
}

func (s *Server) RechargeGet(c *gin.Context) {
Expand Down
20 changes: 8 additions & 12 deletions internal/sbi/consumer/consumer.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
package consumer

import (
"context"
"github.com/free5gc/chf/internal/repository"

"github.com/free5gc/chf/pkg/factory"
"github.com/free5gc/openapi/Nnrf_NFDiscovery"
"github.com/free5gc/openapi/Nnrf_NFManagement"

chf_context "github.com/free5gc/chf/internal/context"
)

type chf interface {
Config() *factory.Config
Context() *chf_context.CHFContext
CancelContext() context.Context
type Chf interface {
// Consumer doesn't need any App component now
}

type Consumer struct {
chf
Chf
RuntimeRepository *repository.RuntimeRepository

// consumer services
*nnrfService
}

func NewConsumer(chf chf) (*Consumer, error) {
func NewConsumer(chf Chf, runtimeRepo *repository.RuntimeRepository) (*Consumer, error) {
c := &Consumer{
chf: chf,
Chf: chf,
RuntimeRepository: runtimeRepo,
}

c.nnrfService = &nnrfService{
Expand Down
40 changes: 0 additions & 40 deletions internal/sbi/consumer/nf_discovery.go

This file was deleted.

50 changes: 0 additions & 50 deletions internal/sbi/consumer/nf_management_test.go.old

This file was deleted.

6 changes: 3 additions & 3 deletions internal/sbi/consumer/nrf_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *nnrfService) SendSearchNFInstances(
nrfUri string, targetNfType, requestNfType models.NfType, param Nnrf_NFDiscovery.SearchNFInstancesParamOpts) (
*models.SearchResult, error) {
// Set client and set url
chfContext := s.consumer.chf.Context()
chfContext := s.consumer.RuntimeRepository.Context()

client := s.getNFDiscClient(chfContext.NrfUri)

Expand Down Expand Up @@ -109,7 +109,7 @@ func (s *nnrfService) SendDeregisterNFInstance() (problemDetails *models.Problem
return pd, err
}

chfContext := s.consumer.chf.Context()
chfContext := s.consumer.RuntimeRepository.Context()
client := s.getNFManagementClient(chfContext.NrfUri)

var res *http.Response
Expand All @@ -136,7 +136,7 @@ func (s *nnrfService) SendDeregisterNFInstance() (problemDetails *models.Problem

func (s *nnrfService) RegisterNFInstance(ctx context.Context) (
resouceNrfUri string, retrieveNfInstanceID string, err error) {
chfContext := s.consumer.chf.Context()
chfContext := s.consumer.RuntimeRepository.Context()

client := s.getNFManagementClient(chfContext.NrfUri)
nfProfile, err := s.buildNfProfile(chfContext)
Expand Down
Loading

0 comments on commit a722608

Please sign in to comment.