From 61e4bf3e3ab0fc246171fd665196437dfcaf9e9e Mon Sep 17 00:00:00 2001 From: Alejandro Tjaarda <alexdecb@yahoo.es> Date: Fri, 27 Sep 2024 19:04:39 +0000 Subject: [PATCH] switch: refactored some code So it can be better reused between the l2sm-switch and the l2sm-ned --- src/switch/api/v1/conf.go | 8 ++++ src/switch/api/v1/topology.go | 17 +++++++ src/switch/internal/inits/init.go | 70 +++++++++++++++++++++++++++++ src/switch/pkg/mqtt_sub/mqtt_sub.go | 2 +- 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/switch/api/v1/conf.go create mode 100644 src/switch/api/v1/topology.go create mode 100644 src/switch/internal/inits/init.go diff --git a/src/switch/api/v1/conf.go b/src/switch/api/v1/conf.go new file mode 100644 index 0000000..d4e2369 --- /dev/null +++ b/src/switch/api/v1/conf.go @@ -0,0 +1,8 @@ +package v1 + +type NedSettings struct { + ConfigDir string + ControllerIP string + NodeName string + NedName string +} diff --git a/src/switch/api/v1/topology.go b/src/switch/api/v1/topology.go new file mode 100644 index 0000000..7533ae6 --- /dev/null +++ b/src/switch/api/v1/topology.go @@ -0,0 +1,17 @@ +package v1 + +type Node struct { + Name string `json:"name"` + NodeIP string `json:"nodeIP"` + NeighborNodes []string `json:"neighborNodes,omitempty"` +} + +type Link struct { + EndpointNodeA string `json:"endpointA"` + EndpointNodeB string `json:"endpointB"` +} + +type Topology struct { + Nodes []Node `json:"Nodes"` + Links []Link `json:"Links"` +} diff --git a/src/switch/internal/inits/init.go b/src/switch/internal/inits/init.go new file mode 100644 index 0000000..a0268fa --- /dev/null +++ b/src/switch/internal/inits/init.go @@ -0,0 +1,70 @@ +package inits + +import ( + "encoding/json" + "fmt" + "os" + "os/exec" + "regexp" + + topo "l2sm.local/ovs-switch/api/v1" + "l2sm.local/ovs-switch/pkg/ovs" +) + +func InitializeSwitch(switchName, controllerIP string) (ovs.Bridge, error) { + + re := regexp.MustCompile(`\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b`) + if !re.MatchString(controllerIP) { + out, _ := exec.Command("host", controllerIP).Output() + controllerIP = re.FindString(string(out)) + } + + controller := fmt.Sprintf("tcp:%s:6633", controllerIP) + + datapathId := ovs.GenerateDatapathID(switchName) + bridge, err := ovs.NewBridge(ovs.Bridge{Name: switchName, Controller: controller, Protocol: "OpenFlow13", DatapathId: datapathId}) + + return bridge, err +} + +func ReadFile(configDir string, dataStruct interface{}) error { + + /// Read file and save in memory the JSON info + data, err := os.ReadFile(configDir) + if err != nil { + fmt.Println("No input file was found.", err) + return err + } + + err = json.Unmarshal(data, &dataStruct) + if err != nil { + return err + } + + return nil + +} + +/* +* +Example: + + { + "Name": "l2sm1", + "nodeIP": "10.1.14.53", + "neighborNodes":["10.4.2.3","10.4.2.5"] + } +*/ +func ConnectToNeighbors(bridge ovs.Bridge, node topo.Node) error { + for vxlanNumber, neighborIp := range node.NeighborNodes { + vxlanId := fmt.Sprintf("vxlan%d", vxlanNumber) + err := bridge.CreateVxlan(ovs.Vxlan{VxlanId: vxlanId, LocalIp: node.NodeIP, RemoteIp: neighborIp, UdpPort: "7000"}) + + if err != nil { + return fmt.Errorf("could not create vxlan with neighbor %s", neighborIp) + } else { + fmt.Printf("Created vxlan with neighbor %s", neighborIp) + } + } + return nil +} diff --git a/src/switch/pkg/mqtt_sub/mqtt_sub.go b/src/switch/pkg/mqtt_sub/mqtt_sub.go index 4d05eb3..a2948ca 100644 --- a/src/switch/pkg/mqtt_sub/mqtt_sub.go +++ b/src/switch/pkg/mqtt_sub/mqtt_sub.go @@ -4,7 +4,7 @@ import ( "encoding/json" "log" - ovs "ovs-switch/pkg/ovs" + "l2sm.local/ovs-switch/pkg/ovs" mqtt "github.com/eclipse/paho.mqtt.golang" ) -- GitLab