From 362ca7ab20eea3242f0b0f0a20831205754b39bd Mon Sep 17 00:00:00 2001 From: "Zaalouk, Adel" Date: Mon, 6 May 2019 00:36:15 +0200 Subject: [PATCH] Add support for configuring sFlow --- ovs/vswitch.go | 25 +++++++++++++++++++++++ ovs/vswitch_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/ovs/vswitch.go b/ovs/vswitch.go index d06b302..5260c91 100644 --- a/ovs/vswitch.go +++ b/ovs/vswitch.go @@ -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...) diff --git a/ovs/vswitch_test.go b/ovs/vswitch_test.go index 8dfa9c8..d206663 100644 --- a/ovs/vswitch_test.go +++ b/ovs/vswitch_test.go @@ -197,6 +197,55 @@ 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