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 1b2b51a
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 92 deletions.
61 changes: 34 additions & 27 deletions cmd/packetrusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,21 @@ 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() {
Expand All @@ -48,12 +32,14 @@ func main() {
Flags: []cli.Flag{
&cli.BoolFlag{Name: "disableTunnel", Aliases: []string{"t"}, Usage: "Disable the creation of the GTP-U tunnel interface."},
&cli.PathFlag{Name: "pcap", Usage: "Capture traffic to given PCAP file when a path is given", Value: "./dump.pcap"},
&cli.PathFlag{Name: "config", Usage: "Configuration file path. (Default: ./config/config.yml)"},
},
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 @@ -75,10 +61,14 @@ func main() {
Name: "gnb",
Aliases: []string{"gnb"},
Usage: "Launch only a gNB",
Flags: []cli.Flag{
&cli.PathFlag{Name: "config", Usage: "Configuration file path. (Default: ./config/config.yml)"},
},
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 @@ -106,19 +96,20 @@ func main() {
&cli.BoolFlag{Name: "tunnel", Aliases: []string{"t"}, Usage: "Enable the creation of the GTP-U tunnel interface."},
&cli.BoolFlag{Name: "dedicatedGnb", Aliases: []string{"d"}, Usage: "Enable the creation of a dedicated gNB per UE. Require one IP on N2/N3 per gNB."},
&cli.PathFlag{Name: "pcap", Usage: "Capture traffic to given PCAP file when a path is given", Value: "./dump.pcap"},
&cli.PathFlag{Name: "config", Usage: "Configuration file path. (Default: ./config/config.yml)"},
},
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 +128,15 @@ 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"},
&cli.PathFlag{Name: "config", Usage: "Configuration file path. (Default: ./config/config.yml)"},
},
Action: func(c *cli.Context) error {
setConfig(*c)

var scenarioPath string

if c.IsSet("scenario") {
Expand All @@ -165,17 +159,19 @@ func main() {
Flags: []cli.Flag{
&cli.IntFlag{Name: "number-of-requests", Value: 1, Aliases: []string{"n"}},
&cli.IntFlag{Name: "time", Value: 1, Aliases: []string{"t"}},
&cli.PathFlag{Name: "config", Usage: "Configuration file path. (Default: ./config/config.yml)"},
},
Action: func(c *cli.Context) error {
var time int
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 @@ -194,15 +190,16 @@ func main() {
"Test availability of AMF in 20 seconds: amf-availability -t 20\n",
Flags: []cli.Flag{
&cli.IntFlag{Name: "time", Value: 1, Aliases: []string{"t"}},
&cli.PathFlag{Name: "config", Usage: "Configuration file path. (Default: ./config/config.yml)"},
},
Action: func(c *cli.Context) error {
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 +218,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
}
76 changes: 50 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,70 @@ 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)
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())
} else {
log.Info("Loaded config at: ", configPath)
}

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()
log.Info(b)
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
// 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))
}

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
Loading

0 comments on commit 1b2b51a

Please sign in to comment.