Skip to content

Commit ca8ef40

Browse files
committed
Change return type to PortAttr instead of PortMapping
1 parent 154d603 commit ca8ef40

File tree

2 files changed

+56
-89
lines changed

2 files changed

+56
-89
lines changed

ovs/openflow.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func (o *OpenFlowService) DumpAggregate(bridge string, flow *MatchFlow) (*FlowSt
403403

404404
// DumpPortMapping retrieves port mapping (ofport and MAC address) for a
405405
// specific interface from the specified bridge.
406-
func (o *OpenFlowService) DumpPortMapping(bridge string, interfaceName string) (*PortMapping, error) {
406+
func (o *OpenFlowService) DumpPortMapping(bridge string, interfaceName string) (*PortAttr, error) {
407407
mappings, err := o.DumpPortMappings(bridge)
408408
if err != nil {
409409
return nil, err
@@ -419,7 +419,7 @@ func (o *OpenFlowService) DumpPortMapping(bridge string, interfaceName string) (
419419

420420
// DumpPortMappings retrieves port mappings (ofport and MAC address) for all
421421
// interfaces from the specified bridge. The output is parsed from 'ovs-ofctl show' command.
422-
func (o *OpenFlowService) DumpPortMappings(bridge string) (map[string]*PortMapping, error) {
422+
func (o *OpenFlowService) DumpPortMappings(bridge string) (map[string]*PortAttr, error) {
423423
args := []string{"show", bridge}
424424
args = append(args, o.c.ofctlFlags...)
425425

@@ -428,7 +428,7 @@ func (o *OpenFlowService) DumpPortMappings(bridge string) (map[string]*PortMappi
428428
return nil, err
429429
}
430430

431-
mappings := make(map[string]*PortMapping)
431+
mappings := make(map[string]*PortAttr)
432432
err = parseEachPort(out, showPrefix, func(line []byte) error {
433433
// Parse the PortMapping - UnmarshalText validates format and extracts all fields
434434
pm := new(PortMapping)
@@ -437,8 +437,11 @@ func (o *OpenFlowService) DumpPortMappings(bridge string) (map[string]*PortMappi
437437
return nil
438438
}
439439

440-
// Use interface name from PortMapping as map key
441-
mappings[pm.ifName] = pm
440+
// Use interface name from PortMapping as map key, copy PortAttr values
441+
mappings[pm.ifName] = &PortAttr{
442+
ofPort: pm.portAttr.ofPort,
443+
macAddress: pm.portAttr.macAddress,
444+
}
442445
return nil
443446
})
444447

ovs/openflow_test.go

Lines changed: 48 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,48 +1417,30 @@ func mustVerifyFlowBundle(t *testing.T, stdin io.Reader, flows []*Flow, matchFlo
14171417
}
14181418

14191419
func TestClientOpenFlowDumpPortMappingsOK(t *testing.T) {
1420-
want := map[string]*PortMapping{
1420+
want := map[string]*PortAttr{
14211421
"interface1": {
1422-
portAttr: PortAttr{
1423-
ofPort: 7,
1424-
macAddress: "fe:4f:76:09:88:2b",
1425-
},
1426-
ifName: "interface1",
1422+
ofPort: 7,
1423+
macAddress: "fe:4f:76:09:88:2b",
14271424
},
14281425
"interface2": {
1429-
portAttr: PortAttr{
1430-
ofPort: 8,
1431-
macAddress: "fe:be:7b:0d:53:d8",
1432-
},
1433-
ifName: "interface2",
1426+
ofPort: 8,
1427+
macAddress: "fe:be:7b:0d:53:d8",
14341428
},
14351429
"interface3": {
1436-
portAttr: PortAttr{
1437-
ofPort: 9,
1438-
macAddress: "fe:b6:4c:d5:40:79",
1439-
},
1440-
ifName: "interface3",
1430+
ofPort: 9,
1431+
macAddress: "fe:b6:4c:d5:40:79",
14411432
},
14421433
"interface4": {
1443-
portAttr: PortAttr{
1444-
ofPort: 20,
1445-
macAddress: "fe:cf:a6:90:30:29",
1446-
},
1447-
ifName: "interface4",
1434+
ofPort: 20,
1435+
macAddress: "fe:cf:a6:90:30:29",
14481436
},
14491437
"LOCAL": {
1450-
portAttr: PortAttr{
1451-
ofPort: 65534,
1452-
macAddress: "fe:74:0f:80:cf:9a",
1453-
},
1454-
ifName: "LOCAL",
1438+
ofPort: 65534,
1439+
macAddress: "fe:74:0f:80:cf:9a",
14551440
},
14561441
"eth0": {
1457-
portAttr: PortAttr{
1458-
ofPort: 1,
1459-
macAddress: "aa:bb:cc:dd:ee:ff",
1460-
},
1461-
ifName: "eth0",
1442+
ofPort: 1,
1443+
macAddress: "aa:bb:cc:dd:ee:ff",
14621444
},
14631445
}
14641446

@@ -1529,14 +1511,14 @@ actions: output enqueue set_vlan_vid set_vlan_pcp strip_vlan mod_dl_src mod_dl_d
15291511
t.Fatalf("missing mapping for interface %q", name)
15301512
}
15311513

1532-
if wantMapping.portAttr.ofPort != gotMapping.portAttr.ofPort {
1514+
if wantMapping.ofPort != gotMapping.ofPort {
15331515
t.Fatalf("unexpected ofPort for %q:\n- want: %d\n- got: %d",
1534-
name, wantMapping.portAttr.ofPort, gotMapping.portAttr.ofPort)
1516+
name, wantMapping.ofPort, gotMapping.ofPort)
15351517
}
15361518

1537-
if wantMapping.portAttr.macAddress != gotMapping.portAttr.macAddress {
1519+
if wantMapping.macAddress != gotMapping.macAddress {
15381520
t.Fatalf("unexpected macAddress for %q:\n- want: %q\n- got: %q",
1539-
name, wantMapping.portAttr.macAddress, gotMapping.portAttr.macAddress)
1521+
name, wantMapping.macAddress, gotMapping.macAddress)
15401522
}
15411523
}
15421524

@@ -1564,20 +1546,14 @@ func TestClientOpenFlowDumpPortMappingsEmptyOutput(t *testing.T) {
15641546
}
15651547

15661548
func TestClientOpenFlowDumpPortMappingsAllInterfaces(t *testing.T) {
1567-
want := map[string]*PortMapping{
1549+
want := map[string]*PortAttr{
15681550
"eth0": {
1569-
portAttr: PortAttr{
1570-
ofPort: 1,
1571-
macAddress: "aa:bb:cc:dd:ee:ff",
1572-
},
1573-
ifName: "eth0",
1551+
ofPort: 1,
1552+
macAddress: "aa:bb:cc:dd:ee:ff",
15741553
},
15751554
"eth1": {
1576-
portAttr: PortAttr{
1577-
ofPort: 2,
1578-
macAddress: "11:22:33:44:55:66",
1579-
},
1580-
ifName: "eth1",
1555+
ofPort: 2,
1556+
macAddress: "11:22:33:44:55:66",
15811557
},
15821558
}
15831559

@@ -1606,14 +1582,14 @@ OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
16061582
t.Fatalf("missing mapping for interface %q", name)
16071583
}
16081584

1609-
if wantMapping.portAttr.ofPort != gotMapping.portAttr.ofPort {
1585+
if wantMapping.ofPort != gotMapping.ofPort {
16101586
t.Fatalf("unexpected ofPort for %q:\n- want: %d\n- got: %d",
1611-
name, wantMapping.portAttr.ofPort, gotMapping.portAttr.ofPort)
1587+
name, wantMapping.ofPort, gotMapping.ofPort)
16121588
}
16131589

1614-
if wantMapping.portAttr.macAddress != gotMapping.portAttr.macAddress {
1590+
if wantMapping.macAddress != gotMapping.macAddress {
16151591
t.Fatalf("unexpected macAddress for %q:\n- want: %q\n- got: %q",
1616-
name, wantMapping.portAttr.macAddress, gotMapping.portAttr.macAddress)
1592+
name, wantMapping.macAddress, gotMapping.macAddress)
16171593
}
16181594
}
16191595
}
@@ -1622,18 +1598,15 @@ func TestClientOpenFlowDumpPortMappingsForInterface(t *testing.T) {
16221598
tests := []struct {
16231599
name string
16241600
interfaceName string
1625-
want *PortMapping
1601+
want *PortAttr
16261602
output string
16271603
}{
16281604
{
16291605
name: "LOCAL interface",
16301606
interfaceName: "LOCAL",
1631-
want: &PortMapping{
1632-
portAttr: PortAttr{
1633-
ofPort: 65534,
1634-
macAddress: "fe:74:0f:80:cf:9a",
1635-
},
1636-
ifName: "LOCAL",
1607+
want: &PortAttr{
1608+
ofPort: 65534,
1609+
macAddress: "fe:74:0f:80:cf:9a",
16371610
},
16381611
output: `
16391612
OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
@@ -1643,12 +1616,9 @@ OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
16431616
{
16441617
name: "interface1",
16451618
interfaceName: "interface1",
1646-
want: &PortMapping{
1647-
portAttr: PortAttr{
1648-
ofPort: 7,
1649-
macAddress: "fe:4f:76:09:88:2b",
1650-
},
1651-
ifName: "interface1",
1619+
want: &PortAttr{
1620+
ofPort: 7,
1621+
macAddress: "fe:4f:76:09:88:2b",
16521622
},
16531623
output: `
16541624
OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
@@ -1675,14 +1645,14 @@ OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
16751645
t.Fatalf("missing mapping for interface %q", tt.interfaceName)
16761646
}
16771647

1678-
if tt.want.portAttr.ofPort != gotMapping.portAttr.ofPort {
1648+
if tt.want.ofPort != gotMapping.ofPort {
16791649
t.Fatalf("unexpected ofPort for %q:\n- want: %d\n- got: %d",
1680-
tt.interfaceName, tt.want.portAttr.ofPort, gotMapping.portAttr.ofPort)
1650+
tt.interfaceName, tt.want.ofPort, gotMapping.ofPort)
16811651
}
16821652

1683-
if tt.want.portAttr.macAddress != gotMapping.portAttr.macAddress {
1653+
if tt.want.macAddress != gotMapping.macAddress {
16841654
t.Fatalf("unexpected macAddress for %q:\n- want: %q\n- got: %q",
1685-
tt.interfaceName, tt.want.portAttr.macAddress, gotMapping.portAttr.macAddress)
1655+
tt.interfaceName, tt.want.macAddress, gotMapping.macAddress)
16861656
}
16871657
})
16881658
}
@@ -1710,20 +1680,17 @@ func TestClientOpenFlowDumpPortMapping(t *testing.T) {
17101680
tests := []struct {
17111681
name string
17121682
interfaceName string
1713-
want *PortMapping
1683+
want *PortAttr
17141684
output string
17151685
wantErr bool
17161686
errMsg string
17171687
}{
17181688
{
17191689
name: "successful retrieval",
17201690
interfaceName: "interface1",
1721-
want: &PortMapping{
1722-
portAttr: PortAttr{
1723-
ofPort: 7,
1724-
macAddress: "fe:4f:76:09:88:2b",
1725-
},
1726-
ifName: "interface1",
1691+
want: &PortAttr{
1692+
ofPort: 7,
1693+
macAddress: "fe:4f:76:09:88:2b",
17271694
},
17281695
output: `
17291696
OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
@@ -1745,12 +1712,9 @@ OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
17451712
{
17461713
name: "LOCAL interface",
17471714
interfaceName: "LOCAL",
1748-
want: &PortMapping{
1749-
portAttr: PortAttr{
1750-
ofPort: 65534,
1751-
macAddress: "fe:74:0f:80:cf:9a",
1752-
},
1753-
ifName: "LOCAL",
1715+
want: &PortAttr{
1716+
ofPort: 65534,
1717+
macAddress: "fe:74:0f:80:cf:9a",
17541718
},
17551719
output: `
17561720
OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
@@ -1783,14 +1747,14 @@ OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
17831747
t.Fatalf("unexpected error: %v", err)
17841748
}
17851749

1786-
if tt.want.portAttr.ofPort != got.portAttr.ofPort {
1750+
if tt.want.ofPort != got.ofPort {
17871751
t.Fatalf("unexpected ofPort:\n- want: %d\n- got: %d",
1788-
tt.want.portAttr.ofPort, got.portAttr.ofPort)
1752+
tt.want.ofPort, got.ofPort)
17891753
}
17901754

1791-
if tt.want.portAttr.macAddress != got.portAttr.macAddress {
1755+
if tt.want.macAddress != got.macAddress {
17921756
t.Fatalf("unexpected macAddress:\n- want: %q\n- got: %q",
1793-
tt.want.portAttr.macAddress, got.portAttr.macAddress)
1757+
tt.want.macAddress, got.macAddress)
17941758
}
17951759
})
17961760
}

0 commit comments

Comments
 (0)