summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/ztpv6/parse_remote_id.go
blob: 9de9bdf1c475205e55e61fe30824dd349c31935b (plain)
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
package ztpv6

import (
	"errors"
	"fmt"
	"regexp"

	"github.com/insomniacslk/dhcp/dhcpv6"
)

var (
	// Arista Port, Vlan Pattern
	aristaPVPattern = regexp.MustCompile("Ethernet(?P<port>[0-9]+):(?P<vlan>[0-9]+)")
	// Arista Slot, Mod, Port Pattern
	aristaSMPPattern = regexp.MustCompile("Ethernet(?P<slot>[0-9]+)/(?P<module>[0-9]+)/(?P<port>[0-9]+)")
)

// CircuitID represents the structure of network vendor interface formats
type CircuitID struct {
	Slot    string
	Module  string
	Port    string
	SubPort string
	Vlan    string
}

// ParseRemoteId will parse the RemoteId Option data for Vendor Specific data
func ParseRemoteID(packet dhcpv6.DHCPv6) (*CircuitID, error) {
	// Need to decapsulate the packet after multiple relays in order to reach RemoteId data
	inner, err := dhcpv6.DecapsulateRelayIndex(packet, -1)
	if err != nil {
		return nil, fmt.Errorf("failed to decapsulate relay index: %v", err)
	}

	if rm, ok := inner.(*dhcpv6.RelayMessage); ok {
		if rid := rm.Options.RemoteID(); rid != nil {
			remoteID := string(rid.RemoteID)
			circ, err := matchCircuitId(remoteID)
			if err != nil {
				return nil, err
			}
			return circ, nil
		}
	}
	return nil, errors.New("failed to parse RemoteID option data")
}

func matchCircuitId(remoteID string) (*CircuitID, error) {
	var names, matches []string

	switch {
	case aristaPVPattern.MatchString(remoteID):
		matches = aristaPVPattern.FindStringSubmatch(remoteID)
		names = aristaPVPattern.SubexpNames()
	case aristaSMPPattern.MatchString(remoteID):
		matches = aristaSMPPattern.FindStringSubmatch(remoteID)
		names = aristaSMPPattern.SubexpNames()
	}

	if len(matches) == 0 {
		return nil, fmt.Errorf("no circuitId regex matches for %v", remoteID)
	}

	var circuit CircuitID
	for i, match := range matches {
		switch names[i] {
		case "port":
			circuit.Port = match
		case "slot":
			circuit.Slot = match
		case "module":
			circuit.Module = match
		case "subport":
			circuit.SubPort = match
		case "vlan":
			circuit.Vlan = match
		}
	}

	return &circuit, nil
}

// FormatCircuitID is the CircuitID format we send in our Bootfile URL for ZTP devices
func (c *CircuitID) FormatCircuitID() string {
	return fmt.Sprintf("%v,%v,%v,%v,%v", c.Slot, c.Module, c.Port, c.SubPort, c.Vlan)
}