Skip to content

Commit

Permalink
refactor config
Browse files Browse the repository at this point in the history
Signed-off-by: Raguideau <[email protected]>
  • Loading branch information
Raguideau committed Dec 13, 2023
1 parent 6c72307 commit 12717f5
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 92 deletions.
56 changes: 29 additions & 27 deletions cmd/packetrusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,29 @@ import (
"my5G-RANTester/config"
"my5G-RANTester/internal/templates"
pcap "my5G-RANTester/internal/utils"

// "fmt"
"os"

"github.com/davecgh/go-spew/spew"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"os"
)

const version = "1.0.1"

func init() {

cfg, err := config.GetConfig()
if err != nil {
//return nil
log.Fatal("Error in get configuration")
}

// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)

// Only log the warning severity or above.
if cfg.Logs.Level == 0 {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.Level(cfg.Logs.Level))
}

spew.Config.Indent = "\t"

log.Info("PacketRusher version " + version)
}

func main() {

app := &cli.App{
Flags: []cli.Flag{
&cli.PathFlag{Name: "config", Usage: "Configuration file path. (Default: ./config/config.yml)"},
},
Commands: []*cli.Command{
{
Name: "ue",
Expand All @@ -51,9 +38,10 @@ func main() {
},
Action: func(c *cli.Context) error {
name := "Testing an ue attached with configuration"
cfg := config.Data
cfg := setConfig(*c)
tunnelEnabled := !c.Bool("disableTunnel")

log.Info("PacketRusher version " + version)
log.Info("---------------------------------------")
log.Info("[TESTER] Starting test function: ", name)
log.Info("[TESTER][UE] Number of UEs: ", 1)
Expand All @@ -77,8 +65,9 @@ func main() {
Usage: "Launch only a gNB",
Action: func(c *cli.Context) error {
name := "Testing an gnb attached with configuration"
cfg := config.Data
cfg := setConfig(*c)

log.Info("PacketRusher version " + version)
log.Info("---------------------------------------")
log.Info("[TESTER] Starting test function: ", name)
log.Info("[TESTER][GNB] Number of GNBs: ", 1)
Expand Down Expand Up @@ -110,15 +99,15 @@ func main() {
Action: func(c *cli.Context) error {
var numUes int
name := "Testing registration of multiple UEs"
cfg := config.Data

cfg := setConfig(*c)
if c.IsSet("number-of-ues") {
numUes = c.Int("number-of-ues")
} else {
log.Info(c.Command.Usage)
return nil
}

log.Info("PacketRusher version " + version)
log.Info("---------------------------------------")
log.Info("[TESTER] Starting test function: ", name)
log.Info("[TESTER][UE] Number of UEs: ", numUes)
Expand All @@ -137,12 +126,14 @@ func main() {
},
},
{
Name: "custom-scenario",
Name: "custom-scenario",
Aliases: []string{"c"},
Flags: []cli.Flag{
&cli.PathFlag{Name: "scenario", Usage: "Specify the scenario path in .wasm"},
},
Action: func(c *cli.Context) error {
setConfig(*c)

var scenarioPath string

if c.IsSet("scenario") {
Expand Down Expand Up @@ -171,11 +162,12 @@ func main() {
var numRqs int

name := "Test AMF responses in interval"
cfg := config.Data
cfg := setConfig(*c)

numRqs = c.Int("number-of-requests")
time = c.Int("time")

log.Info("PacketRusher version " + version)
log.Info("---------------------------------------")
log.Warn("[TESTER] Starting test function: ", name)
log.Warn("[TESTER][UE] Number of Requests per second: ", numRqs)
Expand All @@ -199,10 +191,10 @@ func main() {
var time int

name := "Test availability of AMF"
cfg := config.Data

cfg := setConfig(*c)
time = c.Int("time")

log.Info("PacketRusher version " + version)
log.Info("---------------------------------------")
log.Warn("[TESTER] Starting test function: ", name)
log.Warn("[TESTER][UE] Interval of test: ", time, " seconds")
Expand All @@ -221,3 +213,13 @@ func main() {
log.Fatal(err)
}
}

func setConfig(c cli.Context) config.Config {
var cfg config.Config
if c.IsSet("config") {
cfg = config.Load(c.Path("config"))
} else {
cfg = config.LoadDefaultConfig()
}
return cfg
}
72 changes: 46 additions & 26 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
package config

import (
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"

"github.com/free5gc/nas/nasMessage"
"github.com/free5gc/nas/nasType"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)

// Conf: Used for access to configuration
var Data = getConfig()
var config *Config

type Config struct {
GNodeB GNodeB `yaml:"gnodeb"`
Expand Down Expand Up @@ -97,44 +95,66 @@ type Logs struct {
Level int `yaml:"level"`
}

func RootDir() string {
_, b, _, _ := runtime.Caller(0)
d := path.Join(path.Dir(b))
return filepath.Dir(d)
func GetConfig() Config {
if config == nil {
LoadDefaultConfig()
}
return *config
}

func LoadDefaultConfig() Config {
return Load(getDefautlConfigPath())
}

func getConfig() Config {
func Load(configPath string) Config {
c := readConfig(configPath)
config = &c

setLogLevel(*config)
log.Info("Loaded config at: ", configPath)
return *config
}

func readConfig(configPath string) Config {
var cfg = Config{}
Ddir := RootDir()
configPath, err := filepath.Abs(Ddir + "/config/config.yml")
log.Debug(configPath)
f, err := os.Open(configPath)
if err != nil {
log.Fatal("Could not find config in: ", configPath)
log.Fatal("Could not open config at \"", configPath, "\". ", err.Error())
}
file, err := ioutil.ReadFile(configPath)
err = yaml.Unmarshal([]byte(file), &cfg)
defer f.Close()

decoder := yaml.NewDecoder(f)
err = decoder.Decode(&cfg)
if err != nil {
log.Fatal("Could not read file in: ", configPath)
log.Fatal("Could not unmarshal yaml config at \"", configPath, "\". ", err.Error())
}

return cfg
}

func GetConfig() (Config, error) {
var cfg = Config{}
Ddir := RootDir()
configPath, err := filepath.Abs(Ddir + "/config/config.yml")
log.Debug(configPath)
func getDefautlConfigPath() string {
b, err := os.Executable()
if err != nil {
return Config{}, nil
log.Fatal("Failed to get executable path. ", err.Error())
}
file, err := ioutil.ReadFile(configPath)
err = yaml.Unmarshal([]byte(file), &cfg)
dir := path.Dir(b)
configPath, err := filepath.Abs(dir + "/config/config.yml")
if err != nil {
return Config{}, nil
log.Fatal("Could not find defautl config at \"", configPath, "\". ", err.Error())
}
return configPath
}

func setLogLevel(cfg Config) {
// Output to stdout instead of the default stderr
log.SetOutput(os.Stdout)

if cfg.Logs.Level == 0 {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.Level(cfg.Logs.Level))
}

return cfg, nil
}

func (config *Config) GetUESecurityCapability() *nasType.UESecurityCapability {
Expand Down
9 changes: 3 additions & 6 deletions internal/templates/test-amf-availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@
package templates

import (
log "github.com/sirupsen/logrus"
"my5G-RANTester/config"
"my5G-RANTester/internal/control_test_engine/gnb"
"my5G-RANTester/internal/monitoring"
"time"

log "github.com/sirupsen/logrus"
)

func TestAvailability(interval int) {

monitor := monitoring.Monitor{}

conf, err := config.GetConfig()
if err != nil {
//return nil
log.Fatal("Error in get configuration")
}
conf := config.GetConfig()

ranPort := 1000
for y := 1; y <= interval; y++ {
Expand Down
11 changes: 3 additions & 8 deletions internal/templates/test-amf-requests-per-second.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ package templates
import (
"strconv"
"sync"
)

import (
log "github.com/sirupsen/logrus"
"my5G-RANTester/config"
"my5G-RANTester/internal/control_test_engine/gnb"
"my5G-RANTester/internal/monitoring"

log "github.com/sirupsen/logrus"
)

// rajada de mensagens por segundo enviadas
Expand All @@ -28,11 +27,7 @@ func TestRqsLoop(numRqs int, interval int) int64 {
RqsG: 0,
}

cfg, err := config.GetConfig()
if err != nil {
//return nil
log.Fatal("Error in get configuration")
}
cfg := config.GetConfig()

ranPort := 1000
for y := 1; y <= interval; y++ {
Expand Down
7 changes: 1 addition & 6 deletions internal/templates/test-attach-gnb-with-configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package templates

import (
log "github.com/sirupsen/logrus"
"my5G-RANTester/config"
"my5G-RANTester/internal/control_test_engine/gnb"
"sync"
Expand All @@ -15,11 +14,7 @@ func TestAttachGnbWithConfiguration() {

wg := sync.WaitGroup{}

cfg, err := config.GetConfig()
if err != nil {
//return nil
log.Fatal("Error in get configuration")
}
cfg := config.GetConfig()

// wrong messages:
// cfg.GNodeB.PlmnList.Mcc = "891"
Expand Down
17 changes: 7 additions & 10 deletions internal/templates/test-custom-scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package templates

import (
log "github.com/sirupsen/logrus"
"github.com/tetratelabs/wazero"
"my5G-RANTester/config"
"my5G-RANTester/internal/control_test_engine/gnb"
"my5G-RANTester/internal/control_test_engine/procedures"
Expand All @@ -15,16 +13,15 @@ import (
"os"
"sync"
"time"

log "github.com/sirupsen/logrus"
"github.com/tetratelabs/wazero"
)

func TestWithCustomScenario(scenarioPath string) {
wg := sync.WaitGroup{}

cfg, err := config.GetConfig()
if err != nil {
//return nil
log.Fatal("Error in get configuration")
}
cfg := config.GetConfig()

wg.Add(1)

Expand All @@ -40,7 +37,7 @@ func TestWithCustomScenario(scenarioPath string) {

ctx, runtime := script.NewCustomScenario(scenarioPath)

_, err = runtime.NewHostModuleBuilder("env").
_, err := runtime.NewHostModuleBuilder("env").
NewFunctionBuilder().
WithFunc(func(ueId uint32) {
ueChan <- procedures.UeTesterMessage{Type: procedures.Registration}
Expand All @@ -53,12 +50,12 @@ func TestWithCustomScenario(scenarioPath string) {
Export("detach").
NewFunctionBuilder().
WithFunc(func(ueId uint32, pduSessionId uint8) {
ueChan <- procedures.UeTesterMessage{Type: procedures.NewPDUSession, Param: pduSessionId-1}
ueChan <- procedures.UeTesterMessage{Type: procedures.NewPDUSession, Param: pduSessionId - 1}
}).
Export("pduSessionRequest").
NewFunctionBuilder().
WithFunc(func(ueId uint32, pduSessionId uint8) {
ueChan <- procedures.UeTesterMessage{Type: procedures.DestroyPDUSession, Param: pduSessionId-1}
ueChan <- procedures.UeTesterMessage{Type: procedures.DestroyPDUSession, Param: pduSessionId - 1}
}).
Export("pduSessionRelease").
NewFunctionBuilder().
Expand Down
Loading

0 comments on commit 12717f5

Please sign in to comment.