-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sherrie Shen <sherrie@sherries-mbp.lan>
- Loading branch information
1 parent
f39300f
commit 110fa41
Showing
7 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"jsonrpc": "2.0", | ||
"result": { | ||
"body": { | ||
"TABLE_vlancounters": { | ||
"ROW_vlancounters": [ | ||
{ | ||
"vlanshowbr-vlanid": 1, | ||
"l2_ing_ucast_b": 2386, | ||
"l2_ing_ucast_p": 26, | ||
"l2_ing_mcast_b": 218920, | ||
"l2_ing_mcast_p": 2952, | ||
"l2_ing_bcast_b": 135172550, | ||
"l2_ing_bcast_p": 392491, | ||
"l2_egr_ucast_b": 2386, | ||
"l2_egr_ucast_p": 26, | ||
"l3_ucast_rcv_b": 0, | ||
"l3_ucast_rcv_p": 0 | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"id": 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"jsonrpc": "2.0", | ||
"result": { | ||
"body": { | ||
"TABLE_vlancounters": { | ||
"ROW_vlancounters": [ | ||
{ | ||
"vlanshowbr-vlanid": 1, | ||
"l2_ing_ucast_b": 2386, | ||
"l2_ing_ucast_p": 26, | ||
"l2_ing_mcast_b": 218920, | ||
"l2_ing_mcast_p": 2952, | ||
"l2_ing_bcast_b": 135172550, | ||
"l2_ing_bcast_p": 392491, | ||
"l2_egr_ucast_b": 2386, | ||
"l2_egr_ucast_p": 26, | ||
"l3_ucast_rcv_b": 0, | ||
"l3_ucast_rcv_p": 0 | ||
}, | ||
{ | ||
"vlanshowbr-vlanid": 108, | ||
"l2_ing_ucast_b": 19662301, | ||
"l2_ing_ucast_p": 58354, | ||
"l2_ing_mcast_b": 460220144, | ||
"l2_ing_mcast_p": 3687034, | ||
"l2_ing_bcast_b": 52223390, | ||
"l2_ing_bcast_p": 627387, | ||
"l2_egr_ucast_b": 56424051, | ||
"l2_egr_ucast_p": 214253, | ||
"l3_ucast_rcv_b": 0, | ||
"l3_ucast_rcv_p": 0 | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"id": 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type vlanCountersResponse struct { | ||
ID uint64 `json:"id" xml:"id"` | ||
Version string `json:"jsonrpc" xml:"jsonrpc"` | ||
Result vlanCountersResponseResult `json:"result" xml:"result"` | ||
} | ||
|
||
type vlanCountersResponseResult struct { | ||
Body vlanCountersResponseResultBody `json:"body" xml:"body"` | ||
} | ||
|
||
type vlanCountersResponseResultBody struct { | ||
VlanCountersTable vlanCountersResponseResultBodyVlanCountersTable `json:"TABLE_vlancounters" xml:"TABLE_vlancounters"` | ||
} | ||
|
||
type vlanCountersResponseResultBodyVlanCountersTable struct { | ||
VlanCountersRow []vlanCountersResponseResultBodyVlanCountersRow `json:"ROW_vlancounters" xml:"ROW_vlancounters"` | ||
} | ||
|
||
type vlanCountersResponseResultBodyVlanCountersRow struct { | ||
ID uint64 `json:"vlanshowbr-vlanid" xml:"vlanshowbr-vlanid"` | ||
InputUnicastBytes uint64 `json:"l2_ing_ucast_b" xml:"l2_ing_ucast_b"` | ||
InputUnicastPackets uint64 `json:"l2_ing_ucast_p" xml:"l2_ing_ucast_p"` | ||
InputMulticastBytes uint64 `json:"l2_ing_mcast_b" xml:"l2_ing_mcast_b"` | ||
InputMulticastPackets uint64 `json:"l2_ing_mcast_p" xml:"l2_ing_mcast_p"` | ||
InputBroadcastBytes uint64 `json:"l2_ing_bcast_b" xml:"l2_ing_bcast_b"` | ||
InputBroadcastPackets uint64 `json:"l2_ing_bcast_p" xml:"l2_ing_bcast_p"` | ||
OutputUnicastBytes uint64 `json:"l2_egr_ucast_b" xml:"l2_egr_ucast_b"` | ||
OutputUnicastPackets uint64 `json:"l2_egr_ucast_p" xml:"l2_egr_ucast_p"` | ||
L3InputUnicastBytes uint64 `json:"l3_ucast_rcv_b" xml:"l3_ucast_rcv_b"` | ||
L3InputUnicastPackets uint64 `json:"l3_ucast_rcv_p" xml:"l3_ucast_rcv_p"` | ||
} | ||
|
||
type VlanCounters struct { | ||
ID uint64 `json:"id" xml:"id"` | ||
InputUnicastBytes uint64 `json:"input_ucast_bytes" xml:"input_ucast_bytes"` | ||
InputUnicastBytesL3 uint64 `json:"input_ucast_bytes_l3" xml:"input_ucast_bytes_l3"` | ||
InputMulticastBytes uint64 `json:"input_mcast_bytes" xml:"input_mcast_bytes"` | ||
InputBroadcastBytes uint64 `json:"input_bcast_bytes" xml:"input_bcast_bytes"` | ||
InputUnicastPackets uint64 `json:"input_ucast_packets" xml:"input_ucast_packets"` | ||
InputUnicastPacketsL3 uint64 `json:"input_ucast_packets_l3" xml:"input_ucast_packets_l3"` | ||
InputMulticastPackets uint64 `json:"input_mcast_packets" xml:"input_mcast_packets"` | ||
InputBroadcastPackets uint64 `json:"input_bcast_packets" xml:"input_bcast_packets"` | ||
OutputUnicastBytes uint64 `json:"output_ucast_bytes" xml:"output_ucast_bytes"` | ||
OutputUnicastPackets uint64 `json:"output_ucast_packets" xml:"output_ucast_packets"` | ||
} | ||
|
||
// NewVlanCountersFromString returns VlanCounter instance from an input string. | ||
func NewVlanCountersFromString(s string) ([]*VlanCounters, error) { | ||
return NewVlanCountersFromBytes([]byte(s)) | ||
} | ||
|
||
// NewVlanCountersFromBytes returns VlanCounter instance from an input byte array. | ||
func NewVlanCountersFromBytes(s []byte) ([]*VlanCounters, error) { | ||
var vlanCounters []*VlanCounters | ||
vCountersResponse := &vlanCountersResponse{} | ||
err := json.Unmarshal(s, vCountersResponse) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing error: %s, server response: %s", err, string(s[:])) | ||
} | ||
if len(vCountersResponse.Result.Body.VlanCountersTable.VlanCountersRow) < 1 { | ||
return nil, fmt.Errorf("Error parsing the received response: %s", s) | ||
} | ||
for _, v := range vCountersResponse.Result.Body.VlanCountersTable.VlanCountersRow { | ||
vlanCounter := &VlanCounters{} | ||
vlanCounter.ID = v.ID | ||
vlanCounter.InputUnicastBytes = v.InputUnicastBytes | ||
vlanCounter.InputUnicastBytesL3 = v.L3InputUnicastBytes | ||
vlanCounter.InputMulticastBytes = v.InputMulticastBytes | ||
vlanCounter.InputBroadcastBytes = v.InputBroadcastBytes | ||
vlanCounter.InputUnicastPackets = v.InputUnicastPackets | ||
vlanCounter.InputUnicastPacketsL3 = v.L3InputUnicastPackets | ||
vlanCounter.InputMulticastPackets = v.InputMulticastPackets | ||
vlanCounter.InputBroadcastPackets = v.InputBroadcastPackets | ||
vlanCounter.OutputUnicastBytes = v.OutputUnicastBytes | ||
vlanCounter.OutputUnicastPackets = v.OutputUnicastPackets | ||
vlanCounters = append(vlanCounters, vlanCounter) | ||
} | ||
return vlanCounters, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"testing" | ||
) | ||
|
||
func TestParseShowVlanCountersJsonOutput(t *testing.T) { | ||
testFailed := 0 | ||
outputDir := "../../assets/requests" | ||
for i, test := range []struct { | ||
input string | ||
exp *VlanCounters | ||
count int | ||
shouldFail bool | ||
shouldErr bool | ||
}{ | ||
{ | ||
input: "show.vlan.counters.1", | ||
exp: &VlanCounters{}, | ||
count: 1, | ||
shouldFail: false, | ||
shouldErr: false, | ||
}, | ||
{ | ||
input: "show.vlan.counters.2", | ||
exp: &VlanCounters{}, | ||
count: 2, | ||
shouldFail: false, | ||
shouldErr: false, | ||
}, | ||
} { | ||
fp := fmt.Sprintf("%s/resp.%s.json", outputDir, test.input) | ||
content, err := ioutil.ReadFile(fp) | ||
if err != nil { | ||
t.Logf("FAIL: Test %d: failed reading '%s', error: %v", i, fp, err) | ||
testFailed++ | ||
continue | ||
} | ||
vlanCounters, err := NewVlanCountersFromBytes(content) | ||
if err != nil { | ||
if !test.shouldErr { | ||
t.Logf("FAIL: Test %d: input '%s', expected to pass, but threw error: %v", i, test.input, err) | ||
testFailed++ | ||
continue | ||
} | ||
} else { | ||
if test.shouldErr { | ||
t.Logf("FAIL: Test %d: input '%s', expected to throw error, but passed: %v", i, test.input, vlanCounters) | ||
testFailed++ | ||
continue | ||
} | ||
} | ||
|
||
if vlanCounters != nil { | ||
if (len(vlanCounters) != test.count) && !test.shouldFail { | ||
t.Logf("FAIL: Test %d: input '%s', expected to pass, but failed due to len(vlanCounters) [%d] != %d", i, test.input, len(vlanCounters), test.count) | ||
testFailed++ | ||
continue | ||
} | ||
} | ||
|
||
if test.shouldFail { | ||
t.Logf("PASS: Test %d: input '%s', expected to fail, failed", i, test.input) | ||
} else { | ||
t.Logf("PASS: Test %d: input '%s', expected to pass, passed", i, test.input) | ||
} | ||
} | ||
if testFailed > 0 { | ||
t.Fatalf("Failed %d tests", testFailed) | ||
} | ||
} |