-
Notifications
You must be signed in to change notification settings - Fork 0
/
ribipv6.go
79 lines (72 loc) · 1.66 KB
/
ribipv6.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package nettable
import (
"fmt"
"io"
"net"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
)
// RIPv6s contains a list of RIB entries
type RIPv6s struct {
list []*ripv6
}
type ripv6 struct {
hostname string
prefix net.IP
lenght uint32
protocol string
adminDistance uint32
metric uint32
paths uint32
nxHops []nxHop
}
type nxHop struct {
address net.IP
source net.IP
}
func (d *RIPv6s) Read(r io.Reader) error {
data := new(RIBIPv6)
err := decodeTelemetry(data, r)
if err != nil {
return errors.Wrap(err, "error decoding JSON file")
}
for _, b := range data.Rows {
i := new(ripv6)
i.hostname = data.Telemetry.NodeIDStr
c := b.Content
i.prefix = c.Prefix
i.lenght = c.PrefixLength
i.protocol = c.ProtocolName
i.adminDistance = c.Distance
i.metric = c.Metric
i.paths = c.PathsCount
nh := c.RoutePath.Ipv6RibEdmPath
if len(nh) > 0 {
for _, n := range nh {
i.nxHops = append(i.nxHops, nxHop{
address: n.Address,
source: n.InformationSource,
})
}
}
d.list = append(d.list, i)
}
return nil
}
// DisplayTable pretty prints the info populated.
func (d *RIPv6s) DisplayTable(w io.Writer) {
var data [][]string
for _, s := range d.list {
for _, p := range s.nxHops {
pf := s.prefix.String() + "/" + fmt.Sprint(s.lenght)
data = append(data, []string{s.hostname, pf, s.protocol,
p.address.String(), p.address.String(), fmt.Sprint(s.metric)})
}
}
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"hostname", "prefix", "protocol", "next Hop", "source", "metric"})
for _, v := range data {
table.Append(v)
}
table.Render() // Send output
}