Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: link type everywhere #239

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions database/influxdb/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func (conn *Connection) InsertLink(link *runtime.Link, t time.Time) {
tags.SetString("source.addr", link.SourceAddress)
tags.SetString("target.id", link.TargetID)
tags.SetString("target.addr", link.TargetAddress)
tags.SetString("type", link.Type.String())
if link.SourceHostname != "" {
tags.SetString("source.hostname", link.SourceHostname)
}
Expand Down
4 changes: 4 additions & 0 deletions database/influxdb/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestToInflux(t *testing.T) {
assert := assert.New(t)

node := &runtime.Node{
Online: true,
Statistics: &data.Statistics{
NodeID: "deadbeef",
LoadAverage: 0.5,
Expand Down Expand Up @@ -105,6 +106,7 @@ func TestToInflux(t *testing.T) {
}

neighbour := &runtime.Node{
Online: true,
Nodeinfo: &data.Nodeinfo{
NodeID: "foobar",
Network: data.Network{
Expand Down Expand Up @@ -132,6 +134,7 @@ func TestToInflux(t *testing.T) {

// do not add a empty statistics of a node
droppednode := &runtime.Node{
Online: true,
Nodeinfo: &data.Nodeinfo{
NodeID: "notfound",
Network: data.Network{
Expand Down Expand Up @@ -184,6 +187,7 @@ func TestToInflux(t *testing.T) {
"source.addr": "a-interface-mac",
"target.id": "foobar",
"target.addr": "BAFF1E5",
"type": "vpn",
}, tags)
assert.EqualValues(80, fields["tq"])

Expand Down
3 changes: 2 additions & 1 deletion database/influxdb2/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func (conn *Connection) InsertLink(link *runtime.Link, t time.Time) {
AddTag("source.id", link.SourceID).
AddTag("source.addr", link.SourceAddress).
AddTag("target.id", link.TargetID).
AddTag("target.addr", link.TargetAddress)
AddTag("target.addr", link.TargetAddress).
AddTag("type", link.Type.String())
if link.SourceHostname != "" {
p.AddTag("source.hostname", link.SourceHostname)
}
Expand Down
68 changes: 4 additions & 64 deletions output/meshviewer-ffrgb/meshviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@ import (
"fmt"
"strings"

"github.com/bdlm/log"

"github.com/FreifunkBremen/yanic/lib/jsontime"
"github.com/FreifunkBremen/yanic/runtime"
)

const (
LINK_TYPE_WIRELESS = "wifi"
LINK_TYPE_TUNNEL = "vpn"
LINK_TYPE_FALLBACK = "other"
)

func transform(nodes *runtime.Nodes) *Meshviewer {

meshviewer := &Meshviewer{
Expand All @@ -25,7 +17,6 @@ func transform(nodes *runtime.Nodes) *Meshviewer {
}

links := make(map[string]*Link)
typeList := make(map[string]string)

nodes.RLock()
defer nodes.RUnlock()
Expand All @@ -38,19 +29,6 @@ func transform(nodes *runtime.Nodes) *Meshviewer {
continue
}

if nodeinfo := nodeOrigin.Nodeinfo; nodeinfo != nil {
if meshes := nodeinfo.Network.Mesh; meshes != nil {
for _, mesh := range meshes {
for _, addr := range mesh.Interfaces.Wireless {
typeList[addr] = LINK_TYPE_WIRELESS
}
for _, addr := range mesh.Interfaces.Tunnel {
typeList[addr] = LINK_TYPE_TUNNEL
}
}
}
}

for _, linkOrigin := range nodes.NodeLinks(nodeOrigin) {
var key string
// keep source and target in the same order
Expand All @@ -62,36 +40,11 @@ func transform(nodes *runtime.Nodes) *Meshviewer {
}

if link := links[key]; link != nil {
linkType, linkTypeFound := typeList[linkOrigin.SourceAddress]
if !linkTypeFound {
linkType, linkTypeFound = typeList[linkOrigin.TargetAddress]
}

if switchSourceTarget {
link.TargetTQ = linkOrigin.TQ

linkType, linkTypeFound = typeList[linkOrigin.TargetAddress]
if !linkTypeFound {
linkType, linkTypeFound = typeList[linkOrigin.SourceAddress]
}
} else {
link.SourceTQ = linkOrigin.TQ
}

if linkTypeFound && linkType != link.Type {
if link.Type == LINK_TYPE_FALLBACK {
link.Type = linkType
} else {
log.WithFields(map[string]interface{}{
"link": fmt.Sprintf("%s-%s", linkOrigin.SourceAddress, linkOrigin.TargetAddress),
"prev": link.Type,
"new": linkType,
"source": typeList[linkOrigin.SourceAddress],
"target": typeList[linkOrigin.TargetAddress],
}).Warn("different linktypes")
}
}

continue
}
link := &Link{
Expand All @@ -101,11 +54,7 @@ func transform(nodes *runtime.Nodes) *Meshviewer {
TargetAddress: linkOrigin.TargetAddress,
SourceTQ: linkOrigin.TQ,
TargetTQ: 0,
}

linkType, linkTypeFound := typeList[linkOrigin.SourceAddress]
if !linkTypeFound {
linkType, linkTypeFound = typeList[linkOrigin.TargetAddress]
Type: linkOrigin.Type.String(),
}

if switchSourceTarget {
Expand All @@ -115,22 +64,13 @@ func transform(nodes *runtime.Nodes) *Meshviewer {
link.TargetTQ = linkOrigin.TQ
link.Target = linkOrigin.SourceID
link.TargetAddress = linkOrigin.SourceAddress

linkType, linkTypeFound = typeList[linkOrigin.TargetAddress]
if !linkTypeFound {
linkType, linkTypeFound = typeList[linkOrigin.SourceAddress]
}
}

if linkTypeFound {
link.Type = linkType
} else {
link.Type = LINK_TYPE_FALLBACK
}
links[key] = link
meshviewer.Links = append(meshviewer.Links, link)
}
}

for _, link := range links {
meshviewer.Links = append(meshviewer.Links, link)
}
return meshviewer
}
41 changes: 27 additions & 14 deletions output/meshviewer-ffrgb/meshviewer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func TestTransform(t *testing.T) {
Other []string `json:"other,omitempty"`
Tunnel []string `json:"tunnel,omitempty"`
}{
Wireless: []string{"node:b:mac:wifi"},
Other: []string{"node:b:mac:lan"},
Wireless: []string{"node:d:mac:wifi"},
Other: []string{"node:d:mac:lan"},
},
},
},
Expand All @@ -155,28 +155,41 @@ func TestTransform(t *testing.T) {
meshviewer := transform(nodes)
assert.NotNil(meshviewer)
assert.Len(meshviewer.Nodes, 4)
/*
links:
a:wifi <-> b:wifi 153 / 204
a:lan -> b:lan 51
c:lan <-> b:lan 102 / 204
d:lan -> c:lan 204 (but offline)
d:wifi -> a:wifi 204 (but offline)
*/
links := meshviewer.Links
assert.Len(links, 3)

counter := 0
for _, link := range links {
switch link.SourceAddress {
case "node:a:mac:lan":
assert.Equal("other", link.Type)
assert.Equal("node:b:mac:lan", link.TargetAddress)
assert.Equal(float32(0.2), link.SourceTQ)
assert.Equal(float32(0), link.TargetTQ)
assert.Equal("node:b:mac:lan", link.TargetAddress, "a:lan -> b:lan")
assert.Equal("other", link.Type, "a:lan -> b:lan")
assert.Equal(float32(0.2), link.SourceTQ, "a:lan -> b:lan")
assert.Equal(float32(0), link.TargetTQ, "a:lan -> b:lan")
counter++
case "node:a:mac:wifi":
assert.Equal("wifi", link.Type)
assert.Equal("node:b:mac:wifi", link.TargetAddress)
assert.Equal(float32(0.6), link.SourceTQ)
assert.Equal(float32(0.8), link.TargetTQ)
assert.Equal("node:b:mac:wifi", link.TargetAddress, "a:wifi <-> b:wifi")
assert.Equal("wifi", link.Type, "a:wifi <-> b:wifi")
assert.Equal(float32(0.6), link.SourceTQ, "a:wifi <-> b:wifi")
assert.Equal(float32(0.8), link.TargetTQ, "a:wifi <-> b:wifi")
counter++
case "node:b:mac:lan":
assert.Equal("other", link.Type)
assert.Equal("node:c:mac:lan", link.TargetAddress)
assert.Equal(float32(0.8), link.SourceTQ)
assert.Equal(float32(0.4), link.TargetTQ)
assert.Equal("other", link.Type, "b:lan <-> c:lan")
assert.Equal("node:c:mac:lan", link.TargetAddress, "b:lan <-> c:lan")
assert.Equal(float32(0.8), link.SourceTQ, "b:lan <-> c:lan")
assert.Equal(float32(0.4), link.TargetTQ, "b:lan <-> c:lan")
counter++
default:
assert.False(true, "invalid link.SourceAddress found")
}
}
assert.Equal(3, counter, "not found every link")
}
56 changes: 56 additions & 0 deletions runtime/link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package runtime

type LinkType int

const (
UnknownLinkType LinkType = iota
WirelessLinkType
TunnelLinkType
OtherLinkType
)

func (lt LinkType) String() string {
switch lt {
case WirelessLinkType:
return "wifi"
case TunnelLinkType:
return "vpn"
case OtherLinkType:
return "other"
}
return "unknown"
}

type LinkProtocol int

const (
UnknownLinkProtocol LinkProtocol = iota
BatadvLinkProtocol
BabelLinkProtocol
LLDPLinkProtocol
)

func (lp LinkProtocol) String() string {
switch lp {
case BatadvLinkProtocol:
return "batadv"
case BabelLinkProtocol:
return "babel"
case LLDPLinkProtocol:
return "lldp"
}
return "unkown"
}

// Link represents a link between two nodes
type Link struct {
SourceID string
SourceHostname string
SourceAddress string
TargetID string
TargetAddress string
TargetHostname string
TQ float32
Type LinkType
Protocol LinkProtocol
}
11 changes: 0 additions & 11 deletions runtime/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,6 @@ type Node struct {
CustomFields map[string]interface{} `json:"custom_fields"`
}

// Link represents a link between two nodes
type Link struct {
SourceID string
SourceHostname string
SourceAddress string
TargetID string
TargetAddress string
TargetHostname string
TQ float32
}

// IsGateway returns whether the node is a gateway
func (node *Node) IsGateway() bool {
if info := node.Nodeinfo; info != nil {
Expand Down
Loading
Loading