forked from sandia-minimega/discovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arista.go
100 lines (82 loc) · 1.87 KB
/
arista.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
// Copyright 2018 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
package main
import (
"bufio"
"os"
"path/filepath"
"strings"
"pkg/discovery"
"pkg/minigraph"
log "pkg/minilog"
)
func parseArista(f *os.File, dc *discovery.Client) error {
e := &minigraph.Endpoint{
D: map[string]string{
"router": "true",
"type": "arista",
"name": filepath.Base(f.Name()),
"icon": "router",
},
}
var ID int
if !*f_dryrun {
es, err := dc.InsertEndpoints(e)
if err != nil {
return err
}
ID = es[0].ID()
}
// state machine is "interface" -> description,ip[v6] address, !,interface
scanner := bufio.NewScanner(f)
var desc, ip, ip6 string
addInterface := func() error {
if err := AddInterface(dc, ID, desc, ip, ip6); err != nil {
return err
}
desc = ""
ip = ""
ip6 = ""
return nil
}
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
log.Debug("processing line: %v", line)
switch fields[0] {
case "interface":
if err := addInterface(); err != nil {
return err
}
case "description":
desc = strings.Join(fields[1:], " ")
case "ip", "ipv6":
if len(fields) != 3 || fields[1] != "address" {
continue
}
if fields[0] == "ip" {
ip = fields[2]
log.Debug("got ip address: %v", ip)
} else if !strings.Contains(line, "link-local") {
ip6 = fields[2]
log.Debug("got ipv6 address: %v", ip6)
}
case "router-id":
// found router-id, flush any previous interface
if err := addInterface(); err != nil {
return err
}
desc = "router-id"
ip = fields[1] + "/32"
log.Debug("got router-id: %v", ip)
}
}
if err := addInterface(); err != nil {
return err
}
return scanner.Err()
}