-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
144 lines (130 loc) · 3.89 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/signal"
"time"
xr "github.com/nleiva/xrgrpc"
"github.com/nleiva/xrgrpc/proto/telemetry"
lldp "github.com/nleiva/xrgrpc/proto/telemetry/lldp"
"google.golang.org/protobuf/proto"
)
func prettyprint(b []byte) ([]byte, error) {
var out bytes.Buffer
err := json.Indent(&out, b, "", " ")
return out.Bytes(), err
}
func main() {
// Subs options; LLDP, we will add some more
p := flag.String("subs", "LLDP", "Telemetry Subscription")
// Encoding option; defaults to GPB (only one supported in this example)
enc := flag.String("enc", "gpb", "Encoding: 'json', 'gpb' or 'gpbkv'")
// Config file; defaults to "config.json"
cfg := flag.String("cfg", "../input/config.json", "Configuration file")
flag.Parse()
mape := map[string]int64{
"gpb": 2,
"gpbkv": 3,
"json": 4,
}
e, ok := mape[*enc]
if !ok {
log.Fatalf("encoding option '%v' not supported", *enc)
}
// Determine the ID for the transaction.
r := rand.New(rand.NewSource(time.Now().UnixNano()))
id := r.Int63n(1000)
// Define target parameters from the configuration file
targets := xr.NewDevices()
err := xr.DecodeJSONConfig(targets, *cfg)
if err != nil {
log.Fatalf("could not read the config: %v\n", err)
}
// Setup a connection to the target. 'd' is the index of the router
// in the config file.
d := 1
// Adjust timeout to increase gRPC session lifespan to be able to receive
// Streaming Telemetry data for a period of time.
targets.Routers[d].Timeout = 20
conn, ctx, err := xr.Connect(targets.Routers[d])
if err != nil {
log.Fatalf("could not setup a client connection to %s, %v", targets.Routers[d].Host, err)
}
defer conn.Close()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ch, ech, err := xr.GetSubscription(ctx, conn, *p, id, e)
if err != nil {
log.Fatalf("could not setup Telemetry Subscription: %v\n", err)
}
c := make(chan os.Signal, 1)
// If no signals are provided, all incoming signals will be relayed to c.
// Otherwise, just the provided signals will. E.g.: signal.Notify(c, os.Interrupt, syscall.SIGTERM)
signal.Notify(c, os.Interrupt)
defer func() {
signal.Stop(c)
cancel()
}()
go func() {
select {
case <-c:
fmt.Printf("\nmanually cancelled the session to %v\n\n", targets.Routers[d].Host)
cancel()
return
case <-ctx.Done():
// Timeout: "context deadline exceeded"
err = ctx.Err()
fmt.Printf("\ngRPC session timed out after %v seconds: %v\n\n", targets.Routers[d].Timeout, err.Error())
return
case err = <-ech:
// Session canceled: "context canceled"
fmt.Printf("\ngRPC session to %v failed: %v\n\n", targets.Routers[d].Host, err.Error())
return
}
}()
for tele := range ch {
message := new(telemetry.Telemetry)
err := proto.Unmarshal(tele, message)
if err != nil {
log.Fatalf("could not unmarshall the message: %v\n", err)
}
fmt.Printf("Time %v, Path: %v\n", message.GetMsgTimestamp(), message.GetEncodingPath())
for _, row := range message.GetDataGpb().GetRow() {
// Keys
keys := new(lldp.LldpNeighborEntry_KEYS)
output, err := decode(row.GetKeys(), keys)
if err != nil {
log.Fatalf("could decode Keys: %v\n", err)
}
fmt.Printf("Decoded Keys:\n%v\n", output)
// Content
nbrs := new(lldp.LldpNeighborEntry)
output, err = decode(row.GetContent(), nbrs)
if err != nil {
log.Fatalf("could not decode Content: %v\n", err)
}
fmt.Printf("Decoded JSON Neighbors:\n%v\n", output)
}
}
}
func decode(bk []byte, m proto.Message) (string, error) {
err := proto.Unmarshal(bk, m)
if err != nil {
return "", fmt.Errorf("could not unmarshall the message: %w", err)
}
b, err := json.Marshal(m)
if err != nil {
return "", fmt.Errorf("could not marshall into JSON: %w", err)
}
b, err = prettyprint(b)
if err != nil {
return "", fmt.Errorf("could not pretty-print the message: %w", err)
}
return string(b), err
}