-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
85 lines (73 loc) · 1.68 KB
/
generate.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
package main
import (
"context"
"encoding/binary"
"flag"
"fmt"
"net/netip"
"os"
"text/template"
"github.com/hairyhenderson/gomplate/v3"
"github.com/hairyhenderson/gomplate/v3/data"
)
type Node struct {
RouterId string
}
type NodeData struct {
Region int
Site int
Layer int
Type string
ASN string
}
var tmpl *template.Template
func loadNodeData(routerId string) (NodeData, error) {
nd := NodeData{}
ip, err := netip.ParseAddr(routerId)
if err != nil {
return nd, err
}
data := ip.AsSlice()[2]
typeByte := data & 12 >> 2
if typeByte^1 == 0 {
nd.Type = "leaf"
nd.Layer = 1
} else if typeByte^2 == 0 {
nd.Type = "spine"
nd.Layer = 2
} else if typeByte^3 == 0 {
nd.Type = "superspine"
nd.Layer = 3
} else {
nd.Type = "server"
nd.Layer = 0
}
nd.Region = int(data & 192 >> 6)
nd.Site = int(data & 48 >> 4)
nd.ASN = fmt.Sprintf("65%d%d%d.%d", nd.Region, nd.Site, nd.Layer, int(ip.AsSlice()[2])*256+int(ip.AsSlice()[3]))
return nd, nil
}
func IPv4ToInt(routerId string) (uint32, error) {
ip, err := netip.ParseAddr(routerId)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint32(ip.AsSlice()), nil
}
func init() {
gfuncs := gomplate.CreateFuncs(context.Background(), new(data.Data))
myFuncs := template.FuncMap{}
myFuncs["loadNodeData"] = loadNodeData
myFuncs["IPv4ToInt"] = IPv4ToInt
delete(gfuncs, "slice")
tmpl = template.Must(template.New("").Funcs(gfuncs).Funcs(myFuncs).ParseGlob("templates/*.tpl"))
}
func main() {
var routerId string
flag.StringVar(&routerId, "router-id", "172.18.4.2", "")
flag.Parse()
td := Node{routerId}
if err := tmpl.ExecuteTemplate(os.Stdout, "DCS-7280SR2A-48YC6.tpl", td); err != nil {
panic(err)
}
}