diff --git a/Dockerfile b/Dockerfile index 4233e034dff757050c178529ea8f9c429cf50e53..104a8c11999451a8ab7c953c17851c3c8e2d1c26 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,8 +12,7 @@ RUN go mod download FROM installer as builder -COPY controller.go . -COPY http.go . +COPY *.go ./ COPY ./api ./api COPY ./cmd ./cmd COPY ./database ./database diff --git a/config.go b/config.go new file mode 100644 index 0000000000000000000000000000000000000000..9e09fcc913adc38e9fc94d9f4d1a94c83c9f5316 --- /dev/null +++ b/config.go @@ -0,0 +1,61 @@ +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 +} diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000000000000000000000000000000000000..89bc5f276013c73b9b89ec6d1ec74e68042f6860 --- /dev/null +++ b/config_test.go @@ -0,0 +1,41 @@ +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) + } +} diff --git a/controller.go b/controller.go index aa798f3c0e2fe384aaab716896d18725fdc89574..3f80002be18899d67c0b1b13cec6c8f03ac80b4f 100644 --- a/controller.go +++ b/controller.go @@ -64,7 +64,14 @@ func initialize() error { coreLock.Lock() startHttpServer() coreLock.Unlock() - return createSouthboundInterfaces() + + config := Config{} + err := config.InitializeConfig() + if err != nil { + return err + } + + return createSouthboundInterfaces(config) } func startGrpc() error { @@ -92,14 +99,14 @@ func startGrpc() error { } // createSouthboundInterfaces initializes the controller with its supported SBIs -func createSouthboundInterfaces() error { - sbi := nucleus.NewSBI(spb.Type_OPENCONFIG) - return createPrincipalNetworkDomain(sbi) +func createSouthboundInterfaces(config Config) error { + sbi := nucleus.NewSBI(spb.Type(config.BaseSouthBoundType), config.BaseSouthBoundUUID) + return createPrincipalNetworkDomain(sbi, config) } // createPrincipalNetworkDomain initializes the controller with an initial PND -func createPrincipalNetworkDomain(s southbound.SouthboundInterface) error { - pnd, err := nucleus.NewPND("base", "gosdn base pnd", uuid.New(), s, c.csbiClient, callback) +func createPrincipalNetworkDomain(s southbound.SouthboundInterface, config Config) error { + pnd, err := nucleus.NewPND("base", "gosdn base pnd", config.BasePndUUID, s, c.csbiClient, callback) if err != nil { return err } diff --git a/nucleus/southbound.go b/nucleus/southbound.go index 84c7f7ff535f9117a4fbf2fb1b07247421bba720..290708e4bd321181e3ce4d9c07b02ca00012d90f 100644 --- a/nucleus/southbound.go +++ b/nucleus/southbound.go @@ -26,10 +26,18 @@ func init() { } // 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 { case spb.Type_OPENCONFIG: - return &OpenConfig{id: uuid.New()} + return &OpenConfig{id: id} default: return nil } diff --git a/nucleus/southbound_test.go b/nucleus/southbound_test.go index 513d84a0a178d1926dcf2266ed17b6c22bbc9399..7f05b2301879da14f3faceb7686f33608ecb37da 100644 --- a/nucleus/southbound_test.go +++ b/nucleus/southbound_test.go @@ -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()) + } +}