yanic/models/graph.go

199 lines
4.9 KiB
Go
Raw Normal View History

2016-03-07 01:29:31 +00:00
package models
import (
"fmt"
"strings"
)
// Graph a struct for all links between the nodes
2016-03-07 01:29:31 +00:00
type Graph struct {
Version int `json:"version"`
Batadv struct {
2016-06-16 16:03:45 +00:00
Directed bool `json:"directed"`
Graph []string `json:"graph"`
Nodes []*GraphNode `json:"nodes"`
Links []*GraphLink `json:"links"`
2016-03-07 01:29:31 +00:00
} `json:"batadv"`
}
// GraphNode small struct of a node for the graph struct
2016-05-16 09:41:45 +00:00
type GraphNode struct {
2016-06-16 16:03:45 +00:00
ID string `json:"id"`
NodeID string `json:"node_id"`
2016-05-16 09:41:45 +00:00
}
// GraphLink a struct for the link between two nodes
2016-03-07 01:29:31 +00:00
type GraphLink struct {
2016-06-16 19:17:06 +00:00
Source int `json:"source"`
Target int `json:"target"`
VPN bool `json:"vpn"`
TQ float32 `json:"tq"`
Bidirect bool `json:"bidirect"`
2016-03-07 01:29:31 +00:00
}
// GraphBuilder a temporaty struct during fill the graph from the node neighbours
type graphBuilder struct {
2016-03-07 11:05:53 +00:00
macToID map[string]string // mapping from MAC address to node id
idToMac map[string]string // mapping from node id to one MAC address
2016-03-07 11:05:53 +00:00
links map[string]*GraphLink // mapping from $idA-$idB to existing link
vpn map[string]interface{} // IDs/addresses of VPN servers
2016-03-07 01:29:31 +00:00
}
// BuildGraph transform from nodes (Neighbours) to Graph
2016-03-20 17:30:44 +00:00
func (nodes *Nodes) BuildGraph() *Graph {
builder := &graphBuilder{
2016-03-07 01:29:31 +00:00
macToID: make(map[string]string),
idToMac: make(map[string]string),
2016-03-07 01:29:31 +00:00
links: make(map[string]*GraphLink),
2016-03-07 11:05:53 +00:00
vpn: make(map[string]interface{}),
}
nodes.RLock()
2016-03-07 01:29:31 +00:00
builder.readNodes(nodes.List)
nodes.RUnlock()
2016-03-07 01:29:31 +00:00
2016-05-16 09:41:45 +00:00
graph := &Graph{Version: 1}
graph.Batadv.Directed = false
graph.Batadv.Nodes, graph.Batadv.Links = builder.extract()
2016-03-07 01:29:31 +00:00
return graph
}
func (builder *graphBuilder) readNodes(nodes map[string]*Node) {
2016-03-07 01:29:31 +00:00
// Fill mac->id map
2016-06-16 16:03:45 +00:00
for sourceID, node := range nodes {
if nodeinfo := node.Nodeinfo; nodeinfo != nil {
// is VPN address?
if nodeinfo.VPN {
2016-06-16 16:03:45 +00:00
builder.vpn[sourceID] = nil
}
2016-07-13 15:36:01 +00:00
if len(nodeinfo.Network.Mac) > 0 {
2017-03-07 10:47:41 +00:00
builder.idToMac[sourceID] = nodeinfo.Network.Mac
}
2016-07-13 15:36:01 +00:00
// Batman neighbours
2016-06-16 16:03:45 +00:00
for _, batinterface := range nodeinfo.Network.Mesh {
2016-04-29 06:25:45 +00:00
interfaces := batinterface.Interfaces
addresses := append(append(interfaces.Other, interfaces.Tunnel...), interfaces.Wireless...)
2016-04-29 06:25:45 +00:00
for _, sourceAddress := range addresses {
2016-06-16 16:03:45 +00:00
builder.macToID[sourceAddress] = sourceID
2016-03-07 11:05:53 +00:00
}
2016-03-07 01:29:31 +00:00
}
}
2016-07-13 15:36:01 +00:00
// Iterate over local MAC addresses from LLDP
if neighbours := node.Neighbours; neighbours != nil {
for sourceAddr := range neighbours.LLDP {
2016-07-13 15:36:01 +00:00
builder.macToID[sourceAddr] = sourceID
}
}
2016-03-07 01:29:31 +00:00
}
// Add links
2016-06-16 16:03:45 +00:00
for sourceID, node := range nodes {
if node.Flags.Online {
if neighbours := node.Neighbours; neighbours != nil {
2016-07-13 15:36:01 +00:00
// Batman neighbours
for _, batadvNeighbours := range neighbours.Batadv {
for targetAddress, link := range batadvNeighbours.Neighbours {
if targetID, found := builder.macToID[targetAddress]; found {
builder.addLink(targetID, sourceID, link.Tq)
}
2016-03-07 01:29:31 +00:00
}
}
2016-07-13 15:36:01 +00:00
// LLDP
for _, neighbours := range neighbours.LLDP {
for targetAddress := range neighbours {
2016-07-13 15:36:01 +00:00
if targetID, found := builder.macToID[targetAddress]; found {
builder.addLink(targetID, sourceID, 255)
}
}
}
2016-03-07 01:29:31 +00:00
}
}
}
}
type graphNodeCache struct {
idToMac map[string]string
idToIndex map[string]int
count int
Nodes []*GraphNode
}
2016-03-07 01:29:31 +00:00
func newGraphNodeCache(idToMac map[string]string) *graphNodeCache {
return &graphNodeCache{
idToMac: idToMac,
idToIndex: make(map[string]int),
}
}
func (gn *graphNodeCache) getIndex(nodeID string) int {
index, ok := gn.idToIndex[nodeID]
if !ok {
node := &GraphNode{
ID: gn.idToMac[nodeID],
2016-05-16 09:41:45 +00:00
NodeID: nodeID,
}
gn.Nodes = append(gn.Nodes, node)
gn.idToIndex[nodeID] = gn.count
index = gn.count
gn.count++
2016-05-16 09:41:45 +00:00
}
return index
}
func (builder *graphBuilder) extract() ([]*GraphNode, []*GraphLink) {
links := make([]*GraphLink, len(builder.links))
cache := newGraphNodeCache(builder.idToMac)
2016-06-16 19:17:06 +00:00
// collect links
i := 0
2016-05-16 09:41:45 +00:00
for key, link := range builder.links {
2016-06-16 19:17:06 +00:00
pos := strings.IndexByte(key, '-')
link.Source = cache.getIndex(key[:pos])
link.Target = cache.getIndex(key[pos+1:])
2016-06-16 19:17:06 +00:00
links[i] = link
i++
2016-03-07 01:29:31 +00:00
}
return cache.Nodes, links
2016-03-07 01:29:31 +00:00
}
func (builder *graphBuilder) isVPN(ids ...string) bool {
2016-03-07 11:05:53 +00:00
for _, id := range ids {
if _, found := builder.vpn[id]; found {
return true
}
}
return false
}
func (builder *graphBuilder) addLink(targetID string, sourceID string, linkTq int) {
2016-03-20 11:09:32 +00:00
// Sort IDs to generate the key
2016-03-07 01:29:31 +00:00
var key string
2016-06-16 16:03:45 +00:00
if strings.Compare(sourceID, targetID) > 0 {
key = fmt.Sprintf("%s-%s", sourceID, targetID)
2016-03-07 01:29:31 +00:00
} else {
2016-06-16 16:03:45 +00:00
key = fmt.Sprintf("%s-%s", targetID, sourceID)
2016-03-07 01:29:31 +00:00
}
var tq float32
if linkTq > 0 {
tq = float32(1.0 / (float32(linkTq) / 255.0))
}
if link, ok := builder.links[key]; !ok {
builder.links[key] = &GraphLink{
2016-06-16 16:03:45 +00:00
VPN: builder.isVPN(sourceID, targetID),
TQ: tq,
2016-03-07 01:29:31 +00:00
}
} else {
// Use lowest of both link qualities
if tq < link.TQ {
link.TQ = tq
}
link.Bidirect = true
}
}