Skip to content

Commit

Permalink
mfw-2312 (#119)
Browse files Browse the repository at this point in the history
* added some helper functions to types.go

* moved functions around

* moved functions around

* added unit tests

* added some logs for debugging

* removed debug logs

version: minor
  • Loading branch information
abriles1216 authored Jan 3, 2023
1 parent 8b73b81 commit 1772f29
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
71 changes: 71 additions & 0 deletions services/discovery/collectors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package discovery

import (
"fmt"
"strings"

disco "github.com/untangle/golang-shared/structs/protocolbuffers/Discoverd"
)

Expand Down Expand Up @@ -44,3 +47,71 @@ func fromRPCResponse(rpcResponse disco.CallDiscoveryResponse) CallCollectorsResp
int32(rpcResponse.Result),
}
}

// Normalizes the data in each collector entry
// Returns an error if the data couldn't be normalized or
// if the provided argument isn't a pointer to a collector struct
func NormalizeCollectorEntry(collector interface{}) error {
switch collectorWithType := collector.(type) {
case *disco.LLDP:
collectorWithType.Ip = strings.ToUpper(collectorWithType.Ip)
collectorWithType.Mac = strings.ToUpper(collectorWithType.Mac)
case *disco.NEIGH:
collectorWithType.Ip = strings.ToUpper(collectorWithType.Ip)
collectorWithType.Mac = strings.ToUpper(collectorWithType.Mac)
case *disco.NMAP:
collectorWithType.Ip = strings.ToUpper(collectorWithType.Ip)
collectorWithType.Mac = strings.ToUpper(collectorWithType.Mac)
default:
return fmt.Errorf("provided argument was not a pointer to a collector struct")
}

return nil
}

// Wraps an LLDP, NMAP, or NEIGH collector struct in a device entry
// If the collector struct is missing a field needed to initialize the
// collector struct, and error is returned.
func WrapCollectorInDeviceEntry(collector interface{}) (*DeviceEntry, error) {
var deviceEntry DeviceEntry

switch collectorWithType := collector.(type) {
case *disco.LLDP:
if collectorWithType.Ip == "" {
return nil, fmt.Errorf("LLDP entry missing IP field")
}

deviceEntry.MacAddress = collectorWithType.Mac
deviceEntry.LastUpdate = collectorWithType.LastUpdate

deviceEntry.Lldp = make(map[string]*disco.LLDP)
deviceEntry.Lldp[collectorWithType.Ip] = collectorWithType

case *disco.NEIGH:
if collectorWithType.Ip == "" {
return nil, fmt.Errorf("NEIGH entry missing IP field")
}

deviceEntry.MacAddress = collectorWithType.Mac
deviceEntry.LastUpdate = collectorWithType.LastUpdate

deviceEntry.Neigh = make(map[string]*disco.NEIGH)
deviceEntry.Neigh[collectorWithType.Ip] = collectorWithType

case *disco.NMAP:
if collectorWithType.Ip == "" {
return nil, fmt.Errorf(("NMAP entry missing IP field"))
}

deviceEntry.MacAddress = collectorWithType.Mac
deviceEntry.LastUpdate = collectorWithType.LastUpdate

deviceEntry.Nmap = make(map[string]*disco.NMAP)
deviceEntry.Nmap[collectorWithType.Ip] = collectorWithType

default:
return nil, fmt.Errorf("argument provided to the function was not an LLDP, NMAP, or NEIGH struct")
}

return &deviceEntry, nil
}
76 changes: 76 additions & 0 deletions services/discovery/collectors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package discovery

import (
"testing"

"github.com/stretchr/testify/assert"
disco "github.com/untangle/golang-shared/structs/protocolbuffers/Discoverd"
)

func TestNormalizeCollectorEntry(t *testing.T) {
expectedIp := "2345:0425:2CA1:0000:0000:0567:5673:23B5"
expectedMac := "FF:FF"

lldp := &disco.LLDP{Mac: "ff:ff", Ip: "2345:0425:2ca1:0000:0000:0567:5673:23b5"}
nmap := &disco.NMAP{Mac: "ff:ff", Ip: "2345:0425:2ca1:0000:0000:0567:5673:23b5"}
neighbor := &disco.NEIGH{Mac: "ff:ff", Ip: "2345:0425:2ca1:0000:0000:0567:5673:23b5"}

err := NormalizeCollectorEntry(lldp)
assert.NoError(t, err)

err = NormalizeCollectorEntry(nmap)
assert.NoError(t, err)

err = NormalizeCollectorEntry(neighbor)
assert.NoError(t, err)

err = NormalizeCollectorEntry(nil)
assert.Error(t, err)

assert.Equal(t, expectedIp, lldp.Ip)
assert.Equal(t, expectedMac, lldp.Mac)

assert.Equal(t, expectedIp, nmap.Ip)
assert.Equal(t, expectedMac, nmap.Mac)

assert.Equal(t, expectedIp, neighbor.Ip)
assert.Equal(t, expectedMac, neighbor.Mac)
}

func TestWrapCollectorInDeviceEntry(t *testing.T) {
testIp := "2345:0425:2ca1:0000:0000:0567:5673:23b5"
testMac := "ff:ff"

lldp := &disco.LLDP{Mac: testMac, Ip: testIp}
nmap := &disco.NMAP{Mac: testMac, Ip: testIp}
neighbor := &disco.NEIGH{Mac: testMac, Ip: testIp}

expectedLldp := &DeviceEntry{}
expectedLldp.MacAddress = testMac
expectedLldp.Lldp = map[string]*disco.LLDP{testIp: lldp}

expectedNmap := &DeviceEntry{}
expectedNmap.MacAddress = testMac
expectedNmap.Nmap = map[string]*disco.NMAP{testIp: nmap}

expectedNeighbor := &DeviceEntry{}
expectedNeighbor.MacAddress = testMac
expectedNeighbor.Neigh = map[string]*disco.NEIGH{testIp: neighbor}

actualLldp, err := WrapCollectorInDeviceEntry(lldp)
assert.NoError(t, err)
assert.Equal(t, expectedLldp, actualLldp)

actualNmap, err := WrapCollectorInDeviceEntry(nmap)
assert.NoError(t, err)
assert.Equal(t, expectedNmap, actualNmap)

actualNeighbor, err := WrapCollectorInDeviceEntry(neighbor)
assert.NoError(t, err)
assert.Equal(t, expectedNeighbor, actualNeighbor)

badCollector := &disco.LLDP{Mac: "", Ip: ""}
actualBad, err := WrapCollectorInDeviceEntry(badCollector)
assert.Error(t, err)
assert.Nil(t, actualBad)
}

0 comments on commit 1772f29

Please sign in to comment.