-
Notifications
You must be signed in to change notification settings - Fork 7
/
sr_policy.go
68 lines (55 loc) · 1.15 KB
/
sr_policy.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
// Copyright (c) 2022 NTT Communications Corporation
//
// This software is released under the MIT License.
// see https://github.com/nttcom/pola/blob/main/LICENSE
package table
import (
"errors"
"net/netip"
"strconv"
)
type SRPolicy struct {
PlspID uint32
Name string
SegmentList []Segment
SrcAddr netip.Addr
DstAddr netip.Addr
Color uint32
Preference uint32
}
type Segment interface {
SidString() string
}
func NewSegment(sid string) (Segment, error) {
addr, err := netip.ParseAddr(sid)
if err == nil && addr.Is6() {
return NewSegmentSRv6(addr), nil
}
i, err := strconv.ParseUint(sid, 10, 32)
if err == nil {
return NewSegmentSRMPLS(uint32(i)), nil
}
return nil, errors.New("invalid SID")
}
type SegmentSRv6 struct {
Sid netip.Addr
}
func (seg SegmentSRv6) SidString() string {
return seg.Sid.String()
}
func NewSegmentSRv6(sid netip.Addr) SegmentSRv6 {
return SegmentSRv6{
Sid: sid,
}
}
type SegmentSRMPLS struct {
Sid uint32
}
func (seg SegmentSRMPLS) SidString() string {
return strconv.Itoa(int(seg.Sid))
}
func NewSegmentSRMPLS(sid uint32) SegmentSRMPLS {
return SegmentSRMPLS{
Sid: sid,
}
}