Skip to content
Snippets Groups Projects
Commit 0be86688 authored by Alejandro Tjaarda's avatar Alejandro Tjaarda
Browse files

switches: bug fix with bridge names

Autogenerated bridge names would sometimes not work at all, just because the names weren't compliant. Made a digest function to make unique bridge names but compliant with the linux interfaces api
parent d48bb9fb
No related branches found
No related tags found
1 merge request!2repo: added new directory where utils scripts will be
......@@ -2,7 +2,6 @@ package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
......@@ -18,6 +17,11 @@ import (
"l2sm.local/ovs-switch/internal/inits"
"l2sm.local/ovs-switch/pkg/nedpb"
"l2sm.local/ovs-switch/pkg/ovs"
"l2sm.local/ovs-switch/pkg/utils"
)
const (
DEFAULT_CONFIG_PATH = "/etc/l2sm"
)
// server is used to implement nedpb.VxlanServiceServer
......@@ -45,7 +49,8 @@ func main() {
fmt.Println("Initializing switch, connected to controller: ", settings.ControllerIP)
bridge, err := inits.InitializeSwitch(settings.NedName, settings.ControllerIP)
nedBridgeName, _ := utils.GenerateBridgeName(settings.NedName)
bridge, err := inits.InitializeSwitch(nedBridgeName, settings.ControllerIP)
if err != nil {
log.Fatalf("error initializing ned: %v", err)
}
......@@ -158,17 +163,10 @@ func AddInterfaceToBridge(bridgeName string) (string, error) {
func takeArguments() (string, string, error) {
configDir := flag.String("config_dir", "", "directory where the ned settings are specified. Required")
neighborsDir := flag.String("neighbors_dir", "", "directory where the ned's neighbors are specified. Required")
configDir := flag.String("config_dir", fmt.Sprintf("%s/config.json", DEFAULT_CONFIG_PATH), "directory where the ned settings are specified. Required")
neighborsDir := flag.String("neighbors_dir", fmt.Sprintf("%s/neighbors.json", DEFAULT_CONFIG_PATH), "directory where the ned's neighbors are specified. Required")
flag.Parse()
switch {
case *configDir == "":
return "", "", errors.New("config directory is not defined")
case *neighborsDir == "":
return "", "", errors.New("provider name is not defined")
}
return *configDir, *neighborsDir, nil
}
package utils
import (
"crypto/sha256"
"encoding/hex"
"fmt"
)
func GenerateBridgeName(uniqueIdentifier string) (string, error) {
// Create a SHA-256 hash of the input string
hash := sha256.New()
_, err := hash.Write([]byte(uniqueIdentifier))
if err != nil {
return "", err
}
// Get the full hashed value in hex format
fullHash := hex.EncodeToString(hash.Sum(nil))
// Truncate to the first 5 characters of the hash
digestedName := fullHash[:5]
// Return the formatted bridge name
return fmt.Sprintf("br-%s", digestedName), nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment