Skip to content
Snippets Groups Projects
Commit ae623a74 authored by Manuel Kieweg's avatar Manuel Kieweg
Browse files

Merge branch 'istaester/load-controller-defaults-from-config' into 'develop'

Load controller defaults from config

See merge request cocsn/gosdn!175
parents 36c2c3e1 32659b28
No related branches found
No related tags found
9 merge requests!246Develop,!245Develop into Master,!244Master into develop2 into master,!219Draft: Testing,!214Test pipelines,!195DO NOT MERGE 2,!194DO NOT MERGE! just for testing,!175Load controller defaults from config,!138Develop
Pipeline #78171 passed
...@@ -12,8 +12,7 @@ RUN go mod download ...@@ -12,8 +12,7 @@ RUN go mod download
FROM installer as builder FROM installer as builder
COPY controller.go . COPY *.go ./
COPY http.go .
COPY ./api ./api COPY ./api ./api
COPY ./cmd ./cmd COPY ./cmd ./cmd
COPY ./database ./database COPY ./database ./database
......
package gosdn
import (
"github.com/google/uuid"
"github.com/spf13/viper"
)
// Config represents the nucleus configuration
type Config struct {
BasePndUUID uuid.UUID
BaseSouthBoundType int32
BaseSouthBoundUUID uuid.UUID
}
func getUUIDFromViper(viperKey string) (uuid.UUID, error) {
UUIDAsString := viper.GetString(viperKey)
if UUIDAsString == "" {
newUUID := uuid.New()
viper.Set(viperKey, newUUID.String())
viper.WriteConfig()
return newUUID, nil
}
parsedUUID, err := uuid.Parse(UUIDAsString)
if err != nil {
return uuid.Nil, err
}
return parsedUUID, nil
}
// InitializeConfig loads the configuration
func (c *Config) InitializeConfig() error {
var err error
basePNDUUIDKey := "basePNDUUID"
baseSouthBoundTypeKey := "baseSouthBoundType"
baseSouthBoundUUIDKey := "baseSouthBoundUUID"
basePNDUUID, err := getUUIDFromViper(basePNDUUIDKey)
if err != nil {
return err
}
c.BasePndUUID = basePNDUUID
baseSouthBoundUUID, err := getUUIDFromViper(baseSouthBoundUUIDKey)
if err != nil {
return err
}
c.BaseSouthBoundUUID = baseSouthBoundUUID
c.BaseSouthBoundType = viper.GetInt32("BaseSouthBoundType")
if c.BaseSouthBoundType != 0 {
viper.Set(baseSouthBoundTypeKey, 0)
viper.WriteConfig()
}
return nil
}
package gosdn
import (
"testing"
"github.com/spf13/viper"
)
func Test_Init(t *testing.T) {
viper.SetConfigFile("./config_test.toml")
viper.Set("baseSouthBoundType", 0)
viper.Set("baseSouthBoundUUID", "bf8160d4-4659-4a1b-98fd-f409a04111eb")
viper.Set("basePNDUUID", "bf8160d4-4659-4a1b-98fd-f409a04111ec")
}
func Test_UseExistingConfig(t *testing.T) {
Test_Init(t)
testConfig := Config{}
err := testConfig.InitializeConfig()
if err != nil {
t.Error(err)
return
}
if testConfig.BasePndUUID.String() != "bf8160d4-4659-4a1b-98fd-f409a04111ec" {
t.Fatalf("testConfig.BasePndUUID.String() is not bf8160d4-4659-4a1b-98fd-f409a04111ec. got=%s",
testConfig.BasePndUUID.String())
}
if testConfig.BaseSouthBoundUUID.String() != "bf8160d4-4659-4a1b-98fd-f409a04111eb" {
t.Fatalf("testConfig.BaseSouthBoundUUID.String() is not bf8160d4-4659-4a1b-98fd-f409a04111eb. got=%s",
testConfig.BaseSouthBoundUUID.String())
}
if testConfig.BaseSouthBoundType != 0 {
t.Fatalf("testConfig.BaseSouthBoundType is not 0. got=%d",
testConfig.BaseSouthBoundType)
}
}
...@@ -64,7 +64,14 @@ func initialize() error { ...@@ -64,7 +64,14 @@ func initialize() error {
coreLock.Lock() coreLock.Lock()
startHttpServer() startHttpServer()
coreLock.Unlock() coreLock.Unlock()
return createSouthboundInterfaces()
config := Config{}
err := config.InitializeConfig()
if err != nil {
return err
}
return createSouthboundInterfaces(config)
} }
func startGrpc() error { func startGrpc() error {
...@@ -92,14 +99,14 @@ func startGrpc() error { ...@@ -92,14 +99,14 @@ func startGrpc() error {
} }
// createSouthboundInterfaces initializes the controller with its supported SBIs // createSouthboundInterfaces initializes the controller with its supported SBIs
func createSouthboundInterfaces() error { func createSouthboundInterfaces(config Config) error {
sbi := nucleus.NewSBI(spb.Type_OPENCONFIG) sbi := nucleus.NewSBI(spb.Type(config.BaseSouthBoundType), config.BaseSouthBoundUUID)
return createPrincipalNetworkDomain(sbi) return createPrincipalNetworkDomain(sbi, config)
} }
// createPrincipalNetworkDomain initializes the controller with an initial PND // createPrincipalNetworkDomain initializes the controller with an initial PND
func createPrincipalNetworkDomain(s southbound.SouthboundInterface) error { func createPrincipalNetworkDomain(s southbound.SouthboundInterface, config Config) error {
pnd, err := nucleus.NewPND("base", "gosdn base pnd", uuid.New(), s, c.csbiClient, callback) pnd, err := nucleus.NewPND("base", "gosdn base pnd", config.BasePndUUID, s, c.csbiClient, callback)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -26,10 +26,18 @@ func init() { ...@@ -26,10 +26,18 @@ func init() {
} }
// NewSBI creates a SouthboundInterface of a given type. // NewSBI creates a SouthboundInterface of a given type.
func NewSBI(southbound spb.Type) southbound.SouthboundInterface { func NewSBI(southbound spb.Type, sbUUID ...uuid.UUID) southbound.SouthboundInterface {
var id uuid.UUID
if len(sbUUID) == 0 {
id = uuid.New()
} else {
id = sbUUID[0]
}
switch southbound { switch southbound {
case spb.Type_OPENCONFIG: case spb.Type_OPENCONFIG:
return &OpenConfig{id: uuid.New()} return &OpenConfig{id: id}
default: default:
return nil return nil
} }
......
...@@ -183,3 +183,20 @@ func Test_unmarshal(t *testing.T) { ...@@ -183,3 +183,20 @@ func Test_unmarshal(t *testing.T) {
}) })
} }
} }
func Test_CreateNewUUID(t *testing.T) {
sbi := NewSBI(spb.Type_OPENCONFIG)
if sbi.ID().String() == "" {
t.Errorf("sbi.ID().String() is not set.")
}
}
func Test_UseProvidedUUID(t *testing.T) {
providedSBIId := uuid.New()
sbi := NewSBI(spb.Type_OPENCONFIG, providedSBIId)
if sbi.ID() != providedSBIId {
t.Errorf("sbi.ID() is not %s. got=%s", providedSBIId.String(), sbi.ID().String())
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment