Skip to content

Commit

Permalink
Add support for configuring sFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
zanetworker committed May 6, 2019
1 parent c06b927 commit 279c8d5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ovs/vswitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,31 @@ func (v *VSwitchService) GetController(bridge string) (string, error) {
return strings.TrimSpace(string(address)), nil
}

// CreateSFlow configures an sFlow collector for the vSwitch.
func (v *VSwitchService) CreateSFlow(bridgeName string, agentIP string, collectorIP string, collectorPort string, headerBytes string, samplingN string, pollingSecs string) (string, error) {
var (
agent = fmt.Sprintf("agent=%s", agentIP)
target = fmt.Sprintf("target=\"%s:%s\"", collectorIP, collectorPort)
header = fmt.Sprintf("header=%s", headerBytes)
sampling = fmt.Sprintf("sampling=%s", samplingN)
polling = fmt.Sprintf("polling=%s", pollingSecs)

sFlowID = "sflow"
)

output, err := v.exec(
"--",
fmt.Sprintf("--id=@%s", sFlowID), "create", "sflow", agent, target, header, sampling, polling,
"--",
"set", "bridge", bridgeName, fmt.Sprintf("sflow=@%s", sFlowID),
)
if err != nil {
return "", err
}

return strings.TrimSpace(string(output)), nil
}

// exec executes an ExecFunc using 'ovs-vsctl'.
func (v *VSwitchService) exec(args ...string) ([]byte, error) {
return v.c.exec("ovs-vsctl", args...)
Expand Down
48 changes: 48 additions & 0 deletions ovs/vswitch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,54 @@ func TestClientVSwitchGetControllerOK(t *testing.T) {
}
}

func TestClienCreateSFlow(t *testing.T) {
var (
id = "830bab0b-4149-4f5e-b213-06c6d8a727b9"
bridge = "br0"
collectorIP = "10.0.0.10"
collectorPort = "6343"
agentIP = "ovsbr0"
headerBytes = "128"
samplingN = "64"
pollingSecs = "5"
)

// Apply Timeout option to verify arguments
c := testClient([]OptionFunc{Timeout(1)}, func(cmd string, args ...string) ([]byte, error) {
// Verify correct command and arguments passed, including option flags
if want, got := "ovs-vsctl", cmd; want != got {
t.Fatalf("incorrect command:\n- want: %v\n- got: %v",
want, got)
}

wantArgs := []string{
"--timeout=1",
"--",
"--id=@sflow", "create", "sflow", fmt.Sprintf("agent=%s", agentIP), fmt.Sprintf("target=\"%s:%s\"", collectorIP, collectorPort),
fmt.Sprintf("header=%s", headerBytes), fmt.Sprintf("sampling=%s", samplingN), fmt.Sprintf("polling=%s", pollingSecs),
"--",
"set", "bridge", bridge, "sflow=@sflow",
}

if want, got := wantArgs, args; !reflect.DeepEqual(want, got) {
t.Fatalf("incorrect arguments\n- want: %v\n- got: %v",
want, got)
}

return []byte(id), nil
})

sflowID, err := c.VSwitch.CreateSFlow(bridge, agentIP, collectorIP, collectorPort, headerBytes, samplingN, pollingSecs)
if err != nil {
t.Fatalf("unexpected error for Client.VSwitch.CreateSflow: %v", err)
}

if sflowID != id {
t.Fatalf("sFlowID missmatch\n- got: %v\n- want: %v", sflowID, id)
}

}

func TestClientVSwitchListPorts(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 279c8d5

Please sign in to comment.