Skip to content

Commit

Permalink
feat: start and stop func move to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
donald1218 committed May 1, 2024
1 parent 3c4187d commit 2076683
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 131 deletions.
133 changes: 3 additions & 130 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
package main

import (
"fmt"
"os"
"path/filepath"
"runtime/debug"

"github.com/gin-contrib/cors"
"github.com/urfave/cli"

amf_context "github.com/free5gc/amf/internal/context"
"github.com/free5gc/amf/internal/logger"
"github.com/free5gc/amf/internal/ngap"
ngap_message "github.com/free5gc/amf/internal/ngap/message"
ngap_service "github.com/free5gc/amf/internal/ngap/service"
"github.com/free5gc/amf/internal/sbi/communication"
"github.com/free5gc/amf/internal/sbi/eventexposure"
"github.com/free5gc/amf/internal/sbi/httpcallback"
"github.com/free5gc/amf/internal/sbi/location"
"github.com/free5gc/amf/internal/sbi/mt"
"github.com/free5gc/amf/internal/sbi/oam"
"github.com/free5gc/amf/internal/sbi/producer/callback"
"github.com/free5gc/amf/pkg/factory"
"github.com/free5gc/amf/pkg/service"
"github.com/free5gc/openapi/models"
"github.com/free5gc/util/httpwrapper"
"github.com/free5gc/amf/pkg/utils"
logger_util "github.com/free5gc/util/logger"
"github.com/free5gc/util/version"
)
Expand Down Expand Up @@ -73,121 +59,8 @@ func action(cliCtx *cli.Context) error {
}
factory.AmfConfig = cfg

appStart := func(a *service.AmfApp) {
router := logger_util.NewGinWithLogrus(logger.GinLog)
router.Use(cors.New(cors.Config{
AllowMethods: []string{"GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE"},
AllowHeaders: []string{
"Origin", "Content-Length", "Content-Type", "User-Agent", "Referrer", "Host",
"Token", "X-Requested-With",
},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowAllOrigins: true,
MaxAge: 86400,
}))

httpcallback.AddService(router)
oam.AddService(router)
for _, serviceName := range factory.AmfConfig.Configuration.ServiceNameList {
switch models.ServiceName(serviceName) {
case models.ServiceName_NAMF_COMM:
communication.AddService(router)
case models.ServiceName_NAMF_EVTS:
eventexposure.AddService(router)
case models.ServiceName_NAMF_MT:
mt.AddService(router)
case models.ServiceName_NAMF_LOC:
location.AddService(router)
}
}

pemPath := factory.AmfDefaultCertPemPath
keyPath := factory.AmfDefaultPrivateKeyPath
sbi := factory.AmfConfig.Configuration.Sbi
if sbi.Tls != nil {
pemPath = sbi.Tls.Pem
keyPath = sbi.Tls.Key
}

self := a.Context()
amf_context.InitAmfContext(self)

addr := fmt.Sprintf("%s:%d", self.BindingIPv4, self.SBIPort)

// Register to NRF
var profile models.NfProfile
if profileTmp, err1 := service.GetApp().Consumer().BuildNFInstance(a.Context()); err1 != nil {
logger.InitLog.Error("Build AMF Profile Error")
} else {
profile = profileTmp
}
_, nfId, err_reg := service.GetApp().Consumer().SendRegisterNFInstance(a.Context().NrfUri, a.Context().NfId, profile)
if err_reg != nil {
logger.InitLog.Warnf("Send Register NF Instance failed: %+v", err_reg)
} else {
a.Context().NfId = nfId
}

// ngap
ngapHandler := ngap_service.NGAPHandler{
HandleMessage: ngap.Dispatch,
HandleNotification: ngap.HandleSCTPNotification,
HandleConnectionError: ngap.HandleSCTPConnError,
}

sctpConfig := ngap_service.NewSctpConfig(factory.AmfConfig.GetSctpConfig())
ngap_service.Run(a.Context().NgapIpList, a.Context().NgapPort, ngapHandler, sctpConfig)

server, err_http := httpwrapper.NewHttp2Server(addr, tlsKeyLogPath, router)

if server == nil {
logger.InitLog.Errorf("Initialize HTTP server failed: %+v", err_http)
return
}

if err_http != nil {
logger.InitLog.Warnf("Initialize HTTP server: %+v", err_http)
}

serverScheme := factory.AmfConfig.GetSbiScheme()
if serverScheme == "http" {
err = server.ListenAndServe()
} else if serverScheme == "https" {
err = server.ListenAndServeTLS(pemPath, keyPath)
}

if err != nil {
logger.InitLog.Fatalf("HTTP server setup failed: %+v", err)
}
}

appStop := func(a *service.AmfApp) {
// deregister with NRF
problemDetails, err_deg := service.GetApp().Consumer().SendDeregisterNFInstance()
if problemDetails != nil {
logger.InitLog.Errorf("Deregister NF instance Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.InitLog.Errorf("Deregister NF instance Error[%+v]", err_deg)
} else {
logger.InitLog.Infof("[AMF] Deregister from NRF successfully")
}
// TODO: forward registered UE contexts to target AMF in the same AMF set if there is one

// ngap
// send AMF status indication to ran to notify ran that this AMF will be unavailable
logger.InitLog.Infof("Send AMF Status Indication to Notify RANs due to AMF terminating")
amfSelf := a.Context()
unavailableGuamiList := ngap_message.BuildUnavailableGUAMIList(amfSelf.ServedGuamiList)
amfSelf.AmfRanPool.Range(func(key, value interface{}) bool {
ran := value.(*amf_context.AmfRan)
ngap_message.SendAMFStatusIndication(ran, unavailableGuamiList)
return true
})
callback.SendAmfStatusChangeNotify((string)(models.StatusChange_UNAVAILABLE), amfSelf.ServedGuamiList)
}

amf, err := service.NewApp(cfg, appStart, appStop)
appStart, appStop := utils.InitFunc(tlsKeyLogPath)
amf, err := service.NewApp(cfg, appStart, appStop, tlsKeyLogPath)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func GetApp() AmfAppInterface {
return AMF
}

func NewApp(cfg *factory.Config, startFunc, terminateFunc func(*AmfApp)) (*AmfApp, error) {
func NewApp(cfg *factory.Config, startFunc, terminateFunc func(*AmfApp), tlsKeyLogPath string) (*AmfApp, error) {
amf := &AmfApp{
cfg: cfg,
start: startFunc,
Expand Down
135 changes: 135 additions & 0 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package utils

import (
"fmt"

amf_context "github.com/free5gc/amf/internal/context"
"github.com/free5gc/amf/internal/logger"
"github.com/free5gc/amf/internal/ngap"
ngap_message "github.com/free5gc/amf/internal/ngap/message"
ngap_service "github.com/free5gc/amf/internal/ngap/service"
"github.com/free5gc/amf/internal/sbi/communication"
"github.com/free5gc/amf/internal/sbi/eventexposure"
"github.com/free5gc/amf/internal/sbi/httpcallback"
"github.com/free5gc/amf/internal/sbi/location"
"github.com/free5gc/amf/internal/sbi/mt"
"github.com/free5gc/amf/internal/sbi/oam"
"github.com/free5gc/amf/internal/sbi/producer/callback"
"github.com/free5gc/amf/pkg/factory"
"github.com/free5gc/amf/pkg/service"
"github.com/free5gc/openapi/models"
"github.com/free5gc/util/httpwrapper"
logger_util "github.com/free5gc/util/logger"
)

var (
appStart func(a *service.AmfApp)
appStop func(a *service.AmfApp)
)

func InitFunc(tlsKeyLogPath string) (func(a *service.AmfApp), func(a *service.AmfApp)) {
appStart = func(a *service.AmfApp) {
router := logger_util.NewGinWithLogrus(logger.GinLog)
httpcallback.AddService(router)
oam.AddService(router)
for _, serviceName := range factory.AmfConfig.Configuration.ServiceNameList {
switch models.ServiceName(serviceName) {
case models.ServiceName_NAMF_COMM:
communication.AddService(router)
case models.ServiceName_NAMF_EVTS:
eventexposure.AddService(router)
case models.ServiceName_NAMF_MT:
mt.AddService(router)
case models.ServiceName_NAMF_LOC:
location.AddService(router)
}
}

pemPath := factory.AmfDefaultCertPemPath
keyPath := factory.AmfDefaultPrivateKeyPath
sbi := factory.AmfConfig.Configuration.Sbi
if sbi.Tls != nil {
pemPath = sbi.Tls.Pem
keyPath = sbi.Tls.Key
}

self := a.Context()
amf_context.InitAmfContext(self)

addr := fmt.Sprintf("%s:%d", self.BindingIPv4, self.SBIPort)

// Register to NRF
var profile models.NfProfile
if profileTmp, err1 := service.GetApp().Consumer().BuildNFInstance(a.Context()); err1 != nil {
logger.InitLog.Error("Build AMF Profile Error")
} else {
profile = profileTmp
}
_, nfId, err_reg := service.GetApp().Consumer().SendRegisterNFInstance(a.Context().NrfUri, a.Context().NfId, profile)
if err_reg != nil {
logger.InitLog.Warnf("Send Register NF Instance failed: %+v", err_reg)
} else {
a.Context().NfId = nfId
}

// ngap
ngapHandler := ngap_service.NGAPHandler{
HandleMessage: ngap.Dispatch,
HandleNotification: ngap.HandleSCTPNotification,
HandleConnectionError: ngap.HandleSCTPConnError,
}

sctpConfig := ngap_service.NewSctpConfig(factory.AmfConfig.GetSctpConfig())
ngap_service.Run(a.Context().NgapIpList, a.Context().NgapPort, ngapHandler, sctpConfig)

server, err_http := httpwrapper.NewHttp2Server(addr, tlsKeyLogPath, router)

if server == nil {
logger.InitLog.Errorf("Initialize HTTP server failed: %+v", err_http)
return
}

if err_http != nil {
logger.InitLog.Warnf("Initialize HTTP server: %+v", err_http)
}

var err error

serverScheme := factory.AmfConfig.GetSbiScheme()
if serverScheme == "http" {
err = server.ListenAndServe()
} else if serverScheme == "https" {
err = server.ListenAndServeTLS(pemPath, keyPath)
}

if err != nil {
logger.InitLog.Fatalf("HTTP server setup failed: %+v", err)
}
}

appStop = func(a *service.AmfApp) {
// deregister with NRF
problemDetails, err_deg := service.GetApp().Consumer().SendDeregisterNFInstance()
if problemDetails != nil {
logger.InitLog.Errorf("Deregister NF instance Failed Problem[%+v]", problemDetails)
} else if err_deg != nil {
logger.InitLog.Errorf("Deregister NF instance Error[%+v]", err_deg)
} else {
logger.InitLog.Infof("[AMF] Deregister from NRF successfully")
}
// TODO: forward registered UE contexts to target AMF in the same AMF set if there is one

// ngap
// send AMF status indication to ran to notify ran that this AMF will be unavailable
logger.InitLog.Infof("Send AMF Status Indication to Notify RANs due to AMF terminating")
amfSelf := a.Context()
unavailableGuamiList := ngap_message.BuildUnavailableGUAMIList(amfSelf.ServedGuamiList)
amfSelf.AmfRanPool.Range(func(key, value interface{}) bool {
ran := value.(*amf_context.AmfRan)
ngap_message.SendAMFStatusIndication(ran, unavailableGuamiList)
return true
})
callback.SendAmfStatusChangeNotify((string)(models.StatusChange_UNAVAILABLE), amfSelf.ServedGuamiList)
}
return appStart, appStop
}

0 comments on commit 2076683

Please sign in to comment.