forked from jwhited/bgpls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
165 lines (142 loc) · 3.93 KB
/
event.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package bgpls
import "time"
// Event is a Collector event associated with a neighbor.
//
// Neighbor() returns the associated neighbor's configuration.
//
// Timestamp() returns the time at which the event occurred.
//
// Type() returns the event type.
type Event interface {
Neighbor() *NeighborConfig
Timestamp() time.Time
Type() EventType
}
// EventType describes the type of event
type EventType int
// EventType values
const (
_ EventType = iota
EventTypeNeighborErr
EventTypeNeighborHoldTimerExpired
EventTypeNeighborStateTransition
EventTypeNeighborUpdateReceived
EventTypeNeighborNotificationReceived
)
func (e EventType) String() string {
switch e {
case EventTypeNeighborErr:
return "neighbor error"
case EventTypeNeighborHoldTimerExpired:
return "neighbor hold timer expired"
case EventTypeNeighborStateTransition:
return "neighbor state changed"
case EventTypeNeighborUpdateReceived:
return "received update message from neighbor"
case EventTypeNeighborNotificationReceived:
return "received notification message from neighbor"
default:
return "unknown event type"
}
}
// BaseEvent is included in every Event
type BaseEvent struct {
t time.Time
n *NeighborConfig
}
// Timestamp returns the time at which the event occurred
func (b *BaseEvent) Timestamp() time.Time {
return b.t
}
// Neighbor returns the NeighborConfig associated with the event
func (b *BaseEvent) Neighbor() *NeighborConfig {
return b.n
}
// EventNeighborErr is generated when a neighbor encounters an error
type EventNeighborErr struct {
BaseEvent
Err error
}
// Type returns the appropriate EventType for EventNeighborErr
func (e *EventNeighborErr) Type() EventType {
return EventTypeNeighborErr
}
func newEventNeighborErr(c *NeighborConfig, err error) Event {
return &EventNeighborErr{
BaseEvent: BaseEvent{
t: time.Now(),
n: c,
},
Err: err,
}
}
// EventNeighborHoldTimerExpired is generated when a neighbor's hold timer expires
type EventNeighborHoldTimerExpired struct {
BaseEvent
}
// Type returns the appropriate EventType for EventNeighborHoldTimerExpired
func (e *EventNeighborHoldTimerExpired) Type() EventType {
return EventTypeNeighborHoldTimerExpired
}
func newEventNeighborHoldTimerExpired(c *NeighborConfig) Event {
return &EventNeighborHoldTimerExpired{
BaseEvent: BaseEvent{
t: time.Now(),
n: c,
},
}
}
// EventNeighborStateTransition is generated when a neighbor's fsm transitions to a new state
type EventNeighborStateTransition struct {
BaseEvent
State FSMState
}
// Type returns the appropriate EventType for EventNeighborStateTransition
func (e *EventNeighborStateTransition) Type() EventType {
return EventTypeNeighborStateTransition
}
func newEventNeighborStateTransition(c *NeighborConfig, s FSMState) Event {
return &EventNeighborStateTransition{
BaseEvent: BaseEvent{
t: time.Now(),
n: c,
},
State: s,
}
}
// EventNeighborUpdateReceived is generated when an update message is received
type EventNeighborUpdateReceived struct {
BaseEvent
Message *UpdateMessage
}
// Type returns the appropriate EventType for EventNeighborUpdateReceived
func (e *EventNeighborUpdateReceived) Type() EventType {
return EventTypeNeighborUpdateReceived
}
func newEventNeighborUpdateReceived(c *NeighborConfig, u *UpdateMessage) Event {
return &EventNeighborUpdateReceived{
BaseEvent: BaseEvent{
t: time.Now(),
n: c,
},
Message: u,
}
}
// EventNeighborNotificationReceived is generated when a notification message is received
type EventNeighborNotificationReceived struct {
BaseEvent
Message *NotificationMessage
}
// Type returns the appropriate EventType for EventNeighborNotificationReceived
func (e *EventNeighborNotificationReceived) Type() EventType {
return EventTypeNeighborNotificationReceived
}
func newEventNeighborNotificationReceived(c *NeighborConfig, n *NotificationMessage) Event {
return &EventNeighborNotificationReceived{
BaseEvent: BaseEvent{
t: time.Now(),
n: c,
},
Message: n,
}
}