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

switch: refactored some code

So it can be better reused between the l2sm-switch and the l2sm-ned
parent f9fb2332
No related branches found
No related tags found
1 merge request!2repo: added new directory where utils scripts will be
package v1
type NedSettings struct {
ConfigDir string
ControllerIP string
NodeName string
NedName string
}
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"`
}
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
}
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"log" "log"
ovs "ovs-switch/pkg/ovs" "l2sm.local/ovs-switch/pkg/ovs"
mqtt "github.com/eclipse/paho.mqtt.golang" mqtt "github.com/eclipse/paho.mqtt.golang"
) )
......
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