Skip to content

Commit

Permalink
update generator
Browse files Browse the repository at this point in the history
  • Loading branch information
bewing committed Oct 2, 2022
1 parent ff6cb00 commit ecaaa5a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.tmp/
slides/dist
slides/pdf
bin/
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ slides: slides/dist/presentation.html

.PHONY: pdf
pdf: slides/pdf/presentation.pdf

bin/generate: generate.go
go build -o bin/ generate.go

.PHONY: generate
generate: bin/generate
48 changes: 28 additions & 20 deletions main.go → generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"flag"
"fmt"
"net/netip"
"os"
"text/template"
Expand All @@ -15,41 +16,48 @@ type Node struct {
RouterId string
}

type NodeData struct {
Region int
Site int
Layer int
Type string
ASN string
}

var tmpl *template.Template

func nodeType(routerId string) (string, error) {
func loadNodeData(routerId string) (NodeData, error) {
nd := NodeData{}
ip, err := netip.ParseAddr(routerId)
if err != nil {
return "", err
return nd, err
}
typeByte := ip.AsSlice()[2]
typeByte = typeByte >> 2
typeByte = typeByte & 3
data := ip.AsSlice()[2]
typeByte := data & 12 >> 2
if typeByte^1 == 0 {
return "leaf", nil
nd.Type = "leaf"
nd.Layer = 1
} else if typeByte^2 == 0 {
return "spine", nil
nd.Type = "spine"
nd.Layer = 2
} else if typeByte^3 == 0 {
return "superspine", nil
nd.Type = "superspine"
nd.Layer = 3
} else {
nd.Type = "server"
nd.Layer = 0
}
return "server", nil
}
nd.Region = int(data & 192 >> 6)
nd.Site = int(data & 48 >> 4)

func asnFromRouterId(routerId string) (int, error) {
ip, err := netip.ParseAddr(routerId)
if err != nil {
return -1, err
}
asnBytes := ip.AsSlice()[2:]
asn := int(asnBytes[0])*256 + int(asnBytes[1])
return asn, nil
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 init() {
gfuncs := gomplate.CreateFuncs(context.Background(), new(data.Data))
myFuncs := template.FuncMap{}
myFuncs["nodeType"] = nodeType
myFuncs["asnFromRouterId"] = asnFromRouterId
myFuncs["loadNodeData"] = loadNodeData
delete(gfuncs, "slice")
tmpl = template.Must(template.New("").Funcs(gfuncs).Funcs(myFuncs).ParseGlob("templates/*.tpl"))
}
Expand Down
33 changes: 22 additions & 11 deletions templates/DCS-7280SR2A-48YC6.tpl
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
hostname {{ .RouterId }}
{{ $nodeType := nodeType .RouterId }}
{{ $asn := asnFromRouterId .RouterId }}
{{ $nodeType }}
foo
{{ $nodeData := loadNodeData .RouterId }}
hostname node-{{ $nodeData.ASN }}
username admin privilege 15 secret admin
!
service routing protocols model multi-agent
!
ip routing
ipv6 unicast-routing
ip route vrf MGMT 0.0.0.0/0 192.0.2.254
ipv6 route vrf MGMT ::/0 2001:fb8::1
ipv6 route vrf MGMT ::/0 2001:fb8:100::1
!
vrf MGMT
!
interface Management1
vrf MGMT
ip address 192.0.2.0/24
ipv4 address 2001:fb8::10/64
ipv4 address 2001:fb8:100::10/64
!
interface Loopback0
ip address {{ .RouterId }}/32
ipv6 address 2001:fb8::{{ strings.ReplaceAll "." ":" .RouterId }}/128
!
interface range Ethernet1-52/1
no switchport
Expand All @@ -42,21 +40,33 @@ management api http-commands
no shutdown
!
!
router bgp 65500.{{ $asn }}
peer-filter ASN4-PRIVATE
10 match 4200000000-4294967294 result accept
!
router bgp {{ $nodeData.ASN }}
bgp asn notation asdot
router-id {{ .RouterId }}
neighbor SPINES peer-group
neighbor LEAVES peer-group
neighbor SERVERS peer-group
{{- if eq $nodeType "leaf" }}
neighbor interface Et1-48 peer-group SERVERS peer-filter SERVERS
neighbor interface Et49/1-52/1 peer-group SPINES peer-filter SPINES
{{- if eq $nodeData.Type "leaf" }}
neighbor interface Et1-48 peer-group SERVERS peer-filter ASN4-PRIVATE
neighbor interface Et49/1-52/1 peer-group SPINES peer-filter ASN4-PRIVATE
{{- end }}
{{- if eq $nodeData.Type "spine" }}
neighbor interface Et1-48 peer-group LEAVES peer-filter ASN4-PRIVATE
neighbor interface Et49/1-52/1 peer-group SUPERSPINES peer-filter ASN4-PRIVATE
{{- end }}
{{- if eq $nodeData.Type "superspine" }}
neighbor interface Et1-52/1 peer-group SPINES peer-filter ASN4-PRIVATE
{{- end }}
!
address-family ipv4
bgp next-hop address-family ipv6
neighbor SPINES activate
neighbor SPINES next-hop address-family ipv6 originate
neighbor SUPERSPINES activate
neighbor SUPERSPINES next-hop address-family ipv6 originate
neighbor LEAVES activate
neighbor LEAVES next-hop address-family ipv6 originate
neighbor SERVERS activate
Expand All @@ -65,6 +75,7 @@ router bgp 65500.{{ $asn }}
!
address-family ipv6
neighbor SPINES activate
neighbor SUPERSPINES activate
neighbor LEAVES activate
neighbor SERVERS activate
redistribute connected
Expand Down

0 comments on commit ecaaa5a

Please sign in to comment.