Skip to content
Snippets Groups Projects
Commit 7807fa75 authored by Clemens Barth's avatar Clemens Barth
Browse files

move structs and config parsing to separate Go files

parent 15634c49
No related branches found
No related tags found
1 merge request!103Draft: Resolve "Create a config generator to be able to easily create topologies"
Pipeline #193571 passed
...@@ -8,5 +8,6 @@ import ( ...@@ -8,5 +8,6 @@ import (
func main() { func main() {
fmt.Println("Welcome to QuantConfigGenerator") fmt.Println("Welcome to QuantConfigGenerator")
templateCreate.ParseTemplateCreateConfig()
templateCreate.OutputConfig() templateCreate.OutputConfig()
} }
package templateCreate
import (
"flag"
"os"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
var configPath_default = "templateCreate/templateCreate.clab_vars.yaml" // config path if no other path is given via -config flag
var tcConf *TemplateCreateConfig // pointer to parsed config
// ipPrefix: ipBase without number after last ".".
// e.g. ipBase = "172.100.20.0" -> ipPrefix = "172.100.20.".
var ipPrefix string
func ParseTemplateCreateConfig() {
// TODO: flag validation
configPath := flag.String("config", "", "path to the config file")
logLevel := flag.String("log", "", "logrus log level (debug, info, warn, error, fatal, panic)")
flag.Parse()
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
// parse string, this is built-in feature of logrus
ll, err := logrus.ParseLevel(*logLevel)
if err != nil {
ll = logrus.InfoLevel
logrus.Info("-log flag not provided, using default: ", ll)
}
// set global log level
logrus.SetLevel(ll)
logrus.Info("Setting log level to ", ll)
// if no argument
if *configPath == "" {
err = flag.Set("config", configPath_default)
if err != nil {
ll = logrus.InfoLevel
logrus.Info("-log flag not provided, using default: ", ll)
}
logrus.Info("-config flag not provided, using default: ", *configPath)
}
// unmarshal config
config := &TemplateCreateConfig{}
file, err := os.ReadFile(*configPath)
if err != nil {
logrus.Fatal(err)
}
if err := yaml.Unmarshal(file, config); err != nil {
logrus.Fatal(err)
}
// set TemplateCreateConfig variable
tcConf = config
//logrus.Info(*tcConf) // print entire parsed config
}
...@@ -17,7 +17,7 @@ general: ...@@ -17,7 +17,7 @@ general:
# dll (doubly linked list) - dllr (doubly linked list ring) # dll (doubly linked list) - dllr (doubly linked list ring)
# rndmx (random max peers for all EKMS) - rndr (random with random number of peer connections for every EKMS) # rndmx (random max peers for all EKMS) - rndr (random with random number of peer connections for every EKMS)
# fm (full mesh) - link every EKMS with every other EKMS, or n-1 peers per EKMS for n EKMS # fm (full mesh) - link every EKMS with every other EKMS, or n-1 peers per EKMS for n EKMS
peer_gen_mode: rndr peer_gen_mode: dllr
# TODO: docker compose format # TODO: docker compose format
file_format: containerlab file_format: containerlab
......
...@@ -2,8 +2,6 @@ package templateCreate ...@@ -2,8 +2,6 @@ package templateCreate
import ( import (
"crypto/rand" "crypto/rand"
"flag"
"fmt"
"math/big" "math/big"
"os" "os"
"strconv" "strconv"
...@@ -13,293 +11,8 @@ import ( ...@@ -13,293 +11,8 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/muonsoft/validation/validate" "github.com/muonsoft/validation/validate"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
) )
type AKMS struct {
Name string
Address string
CkmsServerPort int
}
type AKMSNode struct {
AkmsName string
AkmsKind string
AkmsImage string
AkmsIp string
}
// AkmsURL & AkmsCkmsServerPort are optional, only 2 EKMS will have them when there are 2 AKMS.
type EKMS struct {
UUID uuid.UUID
Name string
InterComAddr string
QuantumAddr string
GRPCAddr string
AkmsURL string
AkmsCkmsServerPort int
Peers []EKMSPeer
ConfigFileName string
IpWithoutPort string
PeerType string
}
type EKMSPeer struct {
PeerId uuid.UUID
PeerInterComAddr string
PeerEKMSName string
Sync bool
Type string
QModule struct {
Type string
Address string
}
}
type EkmsNode struct {
EkmsName string // name from generated EKMS
EkmsKind string // kind should be config var
EkmsImage string // config var, always the same for now
EkmsIp string // IP from generated EKMS - without ports!
EkmsBinds []NodeBind // container of local-remote path bindings
EkmsCmd string // the console command to start the EKMS node
EkmsCmdPath string // the file to be used in the command
}
type QuantumLayer struct {
Name string // name in playground file. not in EKMS files.
KMSAddr string // verbundenes KMS
UDPAddr string // IP dieses Layers
PeerUDPAddr string // IP des Peer-Quantum-Layers
GenerateKeys bool // ein Peer generiert Keys, der andere nicht
ConfigFileName string
IpWithoutPort string
}
type QLayerNode struct {
QLayerName string // name from generated QuantumLayer
QLayerKind string // configurable
QLayerImage string // image is config var
QLayerIp string // IP from generated QuantumLayer
QLayerBinds []NodeBind // container of local-remote path bindings
QLayerCmd string // the console command to start the QLayer node
QLayerCmdPath string // the file to be used in the command
}
type NodeBind struct {
ConfigFileLocal string // local path of generated ekms0.yaml config file
ConfigFileRemote string // remote (docker) path, generated from local path
}
type Playground struct {
PlaygroundName string // name of the playground config
MgmtIpBase string
MgmtNetworkName string // name of the MGMT (config management) network
MgmtSubnetSize int // size of the MGMT subnet for EKMS and QLayers
GosdnConfig GosdnConfig
PluginRegistryConfig PluginRegistryConfig
GosdncConfig GosdncConfig
MongoDBConfig MongoDBConfig
RabbitMQConfig RabbitMQConfig
AkmsNodes []AKMSNode
EkmsNodes []EkmsNode
QLayerNodes []QLayerNode
Links []EndPoints
}
type GosdnConfig struct {
Kind string
Image string
Ip string
Ports []string
Binds []string
Cmd string
AdminPw string
}
type GosdncConfig struct {
Kind string
Image string
Ip string
Binds []string
Exec []string
}
type PluginRegistryConfig struct {
Kind string
Image string
Ip string
}
type MongoDBConfig struct {
Kind string
Image string
Ports []string
Ip string
Env struct {
RootUserName string
RootUserPw string
}
}
type RabbitMQConfig struct {
Kind string
Image string
Ports []string
Ip string
}
type TemplateCreateConfig struct {
General struct {
TemplateDir string `yaml:"template_dir"`
OutputDir string `yaml:"output_dir"`
IpBase string `yaml:"ip_base"`
IpSubnetSize int `yaml:"ip_subnet_size"`
PlaygroundName string `yaml:"playground_name"`
MGMTNetworkName string `yaml:"mgmt_network_name"`
ControlNodeCount int `yaml:"control_node_count"`
PeerGenMode string `yaml:"peer_gen_mode"`
}
AKMS struct {
Num int `yaml:"num"`
NamePrefix string `yaml:"name_prefix"`
Kind string `yaml:"kind"`
Image string `yaml:"image"`
IpSuffix string `yaml:"ip_suffix"`
CkmsServerPort int `yaml:"ckms_server_port"`
}
EKMS struct {
Num int `yaml:"num"`
NamePrefix string `yaml:"name_prefix"`
Kind string `yaml:"kind"`
Image string `yaml:"image"`
InterComAddrPort int `yaml:"ica_port"`
QuantumAddrPort int `yaml:"qa_port"`
GRPCAddrPort int `yaml:"grpca_port"`
PathPrefixLocal string `yaml:"path_prefix_local"`
PathPrefixRemote string `yaml:"path_prefix_remote"`
Cmd string `yaml:"cmd"`
PeerType string `yaml:"peer_type"`
QModuleType string `yaml:"qmodule_type"`
MaxPeers int `yaml:"max_peers"`
MinPeers int `yaml:"min_peers"`
}
QLayer struct {
NumPairs int `yaml:"num_pairs"`
NamePrefix string `yaml:"name_prefix"`
Kind string `yaml:"kind"`
Image string `yaml:"image"`
UPDPort int `yaml:"udp_port"`
PathPrefixLocal string `yaml:"path_prefix_local"`
PathPrefixRemote string `yaml:"path_prefix_remote"`
Cmd string `yaml:"cmd"`
}
Gosdn struct {
Kind string `yaml:"kind"`
Image string `yaml:"image"`
Ports []string `yaml:"ports"`
Binds []string `yaml:"binds"`
Cmd string `yaml:"cmd"`
AdminPw string `yaml:"admin_pw"`
SubnetNr int `yaml:"subnet_nr"`
}
Gosdnc struct {
Kind string `yaml:"kind"`
Image string `yaml:"image"`
Binds []string `yaml:"binds"`
Exec []string `yaml:"exec"`
SubnetNr int `yaml:"subnet_nr"`
}
PluginRegistry struct {
Kind string `yaml:"kind"`
Image string `yaml:"image"`
SubnetNr int `yaml:"subnet_nr"`
}
MongoDB struct {
Kind string `yaml:"kind"`
Image string `yaml:"image"`
Ports []string `yaml:"ports"`
Env struct {
RootUserName string `yaml:"MONGO_INITDB_ROOT_USERNAME"`
RootUserPw string `yaml:"MONGO_INITDB_ROOT_PASSWORD"`
}
SubnetNr int `yaml:"subnet_nr"`
}
RabbitMQ struct {
Kind string `yaml:"kind"`
Image string `yaml:"image"`
Ports []string `yaml:"ports"`
SubnetNr int `yaml:"subnet_nr"`
}
Links []EndPoints
}
type EndPoints struct {
EndPoints []string `yaml:"endpoints"`
}
// returns a string representation of the form "kms1:eth1", "kms2:eth1", ..., "kmsN:ethN".
func (e EndPoints) String() string {
var s string
ep_len := len(e.EndPoints)
for i := 0; i < ep_len; i++ {
s = s + fmt.Sprintf("\"%v\"", e.EndPoints[i])
if i < ep_len-1 {
s = s + ", "
}
}
return s
}
// #region Control variables
var configPath_default = "templateCreate/templateCreate.clab_vars.yaml" // config path if no other path is given via -config flag
var tcConf *TemplateCreateConfig // pointer to parsed config
// ipPrefix: ipBase without number after last ".".
// e.g. ipBase = "172.100.20.0" -> ipPrefix = "172.100.20.".
var ipPrefix string
func parseTemplateCreateConfig() {
// TODO: flag validation
configPath := flag.String("config", "", "path to the config file")
logLevel := flag.String("log", "", "logrus log level (debug, info, warn, error, fatal, panic)")
flag.Parse()
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
// parse string, this is built-in feature of logrus
ll, err := logrus.ParseLevel(*logLevel)
if err != nil {
ll = logrus.InfoLevel
logrus.Info("-log flag not provided, using default: ", ll)
}
// set global log level
logrus.SetLevel(ll)
logrus.Info("Setting log level to ", ll)
// if no argument
if *configPath == "" {
err = flag.Set("config", configPath_default)
if err != nil {
ll = logrus.InfoLevel
logrus.Info("-log flag not provided, using default: ", ll)
}
logrus.Info("-config flag not provided, using default: ", *configPath)
}
// unmarshal config
config := &TemplateCreateConfig{}
file, err := os.ReadFile(*configPath)
if err != nil {
logrus.Fatal(err)
}
if err := yaml.Unmarshal(file, config); err != nil {
logrus.Fatal(err)
}
// set TemplateCreateConfig variable
tcConf = config
//logrus.Info(*tcConf) // print entire parsed config
}
func createUUID() uuid.UUID { func createUUID() uuid.UUID {
uu1 := uuid.New() uu1 := uuid.New()
//logrus.Info(uu1) //logrus.Info(uu1)
...@@ -727,7 +440,7 @@ func getEkmsWithUUID(ekmsList []EKMS, id uuid.UUID) int { ...@@ -727,7 +440,7 @@ func getEkmsWithUUID(ekmsList []EKMS, id uuid.UUID) int {
} }
// for modes "rndmx" and "rndr". // for modes "rndmx" and "rndr".
// "rndmx": genRandomNumberOfPeers == false; "rndr": genRandomNumberOfPeers == true // "rndmx": genRandomNumberOfPeers == false; "rndr": genRandomNumberOfPeers == true.
func peerGen_random(generatedEkms []EKMS, generatedQLayers []QuantumLayer, curSubnetNr int, minPeers int, maxPeers int, genRandomNumberOfPeers bool) ([]EKMS, []QuantumLayer, int) { func peerGen_random(generatedEkms []EKMS, generatedQLayers []QuantumLayer, curSubnetNr int, minPeers int, maxPeers int, genRandomNumberOfPeers bool) ([]EKMS, []QuantumLayer, int) {
// get number of existing EKMS, create a copy of them // get number of existing EKMS, create a copy of them
ekmsCount := len(generatedEkms) ekmsCount := len(generatedEkms)
...@@ -798,7 +511,6 @@ func peerGen_random(generatedEkms []EKMS, generatedQLayers []QuantumLayer, curSu ...@@ -798,7 +511,6 @@ func peerGen_random(generatedEkms []EKMS, generatedQLayers []QuantumLayer, curSu
} }
} }
} }
} }
return generatedEkms, generatedQLayers, curSubnetNr return generatedEkms, generatedQLayers, curSubnetNr
} }
...@@ -807,7 +519,7 @@ func peerGen_random(generatedEkms []EKMS, generatedQLayers []QuantumLayer, curSu ...@@ -807,7 +519,7 @@ func peerGen_random(generatedEkms []EKMS, generatedQLayers []QuantumLayer, curSu
// creates EKMS, QLayer and playgroundConfig.yaml files in outputDir. // creates EKMS, QLayer and playgroundConfig.yaml files in outputDir.
func OutputConfig() { func OutputConfig() {
// get the config data // get the config data
parseTemplateCreateConfig() //parseTemplateCreateConfig()
peerGenMode := tcConf.General.PeerGenMode peerGenMode := tcConf.General.PeerGenMode
logrus.Infof("Generation mode is %v", peerGenMode) logrus.Infof("Generation mode is %v", peerGenMode)
......
package templateCreate
import (
"fmt"
"github.com/google/uuid"
)
type AKMS struct {
Name string
Address string
CkmsServerPort int
}
type AKMSNode struct {
AkmsName string
AkmsKind string
AkmsImage string
AkmsIp string
}
// AkmsURL & AkmsCkmsServerPort are optional, only 2 EKMS will have them when there are 2 AKMS.
type EKMS struct {
UUID uuid.UUID
Name string
InterComAddr string
QuantumAddr string
GRPCAddr string
AkmsURL string
AkmsCkmsServerPort int
Peers []EKMSPeer
ConfigFileName string
IpWithoutPort string
PeerType string
}
type EKMSPeer struct {
PeerId uuid.UUID
PeerInterComAddr string
PeerEKMSName string
Sync bool
Type string
QModule struct {
Type string
Address string
}
}
type EkmsNode struct {
EkmsName string // name from generated EKMS
EkmsKind string // kind should be config var
EkmsImage string // config var, always the same for now
EkmsIp string // IP from generated EKMS - without ports!
EkmsBinds []NodeBind // container of local-remote path bindings
EkmsCmd string // the console command to start the EKMS node
EkmsCmdPath string // the file to be used in the command
}
type QuantumLayer struct {
Name string // name in playground file. not in EKMS files.
KMSAddr string // verbundenes KMS
UDPAddr string // IP dieses Layers
PeerUDPAddr string // IP des Peer-Quantum-Layers
GenerateKeys bool // ein Peer generiert Keys, der andere nicht
ConfigFileName string
IpWithoutPort string
}
type QLayerNode struct {
QLayerName string // name from generated QuantumLayer
QLayerKind string // configurable
QLayerImage string // image is config var
QLayerIp string // IP from generated QuantumLayer
QLayerBinds []NodeBind // container of local-remote path bindings
QLayerCmd string // the console command to start the QLayer node
QLayerCmdPath string // the file to be used in the command
}
type NodeBind struct {
ConfigFileLocal string // local path of generated ekms0.yaml config file
ConfigFileRemote string // remote (docker) path, generated from local path
}
type Playground struct {
PlaygroundName string // name of the playground config
MgmtIpBase string
MgmtNetworkName string // name of the MGMT (config management) network
MgmtSubnetSize int // size of the MGMT subnet for EKMS and QLayers
GosdnConfig GosdnConfig
PluginRegistryConfig PluginRegistryConfig
GosdncConfig GosdncConfig
MongoDBConfig MongoDBConfig
RabbitMQConfig RabbitMQConfig
AkmsNodes []AKMSNode
EkmsNodes []EkmsNode
QLayerNodes []QLayerNode
Links []EndPoints
}
type GosdnConfig struct {
Kind string
Image string
Ip string
Ports []string
Binds []string
Cmd string
AdminPw string
}
type GosdncConfig struct {
Kind string
Image string
Ip string
Binds []string
Exec []string
}
type PluginRegistryConfig struct {
Kind string
Image string
Ip string
}
type MongoDBConfig struct {
Kind string
Image string
Ports []string
Ip string
Env struct {
RootUserName string
RootUserPw string
}
}
type RabbitMQConfig struct {
Kind string
Image string
Ports []string
Ip string
}
type TemplateCreateConfig struct {
General struct {
TemplateDir string `yaml:"template_dir"`
OutputDir string `yaml:"output_dir"`
IpBase string `yaml:"ip_base"`
IpSubnetSize int `yaml:"ip_subnet_size"`
PlaygroundName string `yaml:"playground_name"`
MGMTNetworkName string `yaml:"mgmt_network_name"`
ControlNodeCount int `yaml:"control_node_count"`
PeerGenMode string `yaml:"peer_gen_mode"`
}
AKMS struct {
Num int `yaml:"num"`
NamePrefix string `yaml:"name_prefix"`
Kind string `yaml:"kind"`
Image string `yaml:"image"`
IpSuffix string `yaml:"ip_suffix"`
CkmsServerPort int `yaml:"ckms_server_port"`
}
EKMS struct {
Num int `yaml:"num"`
NamePrefix string `yaml:"name_prefix"`
Kind string `yaml:"kind"`
Image string `yaml:"image"`
InterComAddrPort int `yaml:"ica_port"`
QuantumAddrPort int `yaml:"qa_port"`
GRPCAddrPort int `yaml:"grpca_port"`
PathPrefixLocal string `yaml:"path_prefix_local"`
PathPrefixRemote string `yaml:"path_prefix_remote"`
Cmd string `yaml:"cmd"`
PeerType string `yaml:"peer_type"`
QModuleType string `yaml:"qmodule_type"`
MaxPeers int `yaml:"max_peers"`
MinPeers int `yaml:"min_peers"`
}
QLayer struct {
NumPairs int `yaml:"num_pairs"`
NamePrefix string `yaml:"name_prefix"`
Kind string `yaml:"kind"`
Image string `yaml:"image"`
UPDPort int `yaml:"udp_port"`
PathPrefixLocal string `yaml:"path_prefix_local"`
PathPrefixRemote string `yaml:"path_prefix_remote"`
Cmd string `yaml:"cmd"`
}
Gosdn struct {
Kind string `yaml:"kind"`
Image string `yaml:"image"`
Ports []string `yaml:"ports"`
Binds []string `yaml:"binds"`
Cmd string `yaml:"cmd"`
AdminPw string `yaml:"admin_pw"`
SubnetNr int `yaml:"subnet_nr"`
}
Gosdnc struct {
Kind string `yaml:"kind"`
Image string `yaml:"image"`
Binds []string `yaml:"binds"`
Exec []string `yaml:"exec"`
SubnetNr int `yaml:"subnet_nr"`
}
PluginRegistry struct {
Kind string `yaml:"kind"`
Image string `yaml:"image"`
SubnetNr int `yaml:"subnet_nr"`
}
MongoDB struct {
Kind string `yaml:"kind"`
Image string `yaml:"image"`
Ports []string `yaml:"ports"`
Env struct {
RootUserName string `yaml:"MONGO_INITDB_ROOT_USERNAME"`
RootUserPw string `yaml:"MONGO_INITDB_ROOT_PASSWORD"`
}
SubnetNr int `yaml:"subnet_nr"`
}
RabbitMQ struct {
Kind string `yaml:"kind"`
Image string `yaml:"image"`
Ports []string `yaml:"ports"`
SubnetNr int `yaml:"subnet_nr"`
}
Links []EndPoints
}
type EndPoints struct {
EndPoints []string `yaml:"endpoints"`
}
// returns a string representation of the form "kms1:eth1", "kms2:eth1", ..., "kmsN:ethN".
func (e EndPoints) String() string {
var s string
ep_len := len(e.EndPoints)
for i := 0; i < ep_len; i++ {
s = s + fmt.Sprintf("\"%v\"", e.EndPoints[i])
if i < ep_len-1 {
s = s + ", "
}
}
return s
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment