-
Notifications
You must be signed in to change notification settings - Fork 0
/
lldp.go
77 lines (64 loc) · 2.54 KB
/
lldp.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
package bond
import (
"context"
"github.com/nokia/srlinux-ndk-go/ndk"
"google.golang.org/protobuf/encoding/prototext"
)
// ReceiveLLDPNotifications starts an LLDP neighbor notification
// stream and sends notifications to channel `Lldp`.
// If the main execution intends to continue running after calling this method,
// it should be called as a goroutine.
// `Lldp` chan carries values of type ndk.LldpNeighborNotification
func (a *Agent) ReceiveLLDPNotifications(ctx context.Context) {
defer close(a.Notifications.Lldp)
LldpStream := a.startLldpNotificationStream(ctx)
for LldpStreamResp := range LldpStream {
b, err := prototext.MarshalOptions{Multiline: true, Indent: " "}.Marshal(LldpStreamResp)
if err != nil {
a.logger.Info().
Msgf("Lldp Neighbor notification Marshal failed: %+v", err)
continue
}
a.logger.Info().
Msgf("Received Lldp Neighbor notifications:\n%s", b)
for _, n := range LldpStreamResp.GetNotification() {
LldpNotif := n.GetLldpNeighbor()
if LldpNotif == nil {
a.logger.Info().
Msgf("Empty Lldp Neighbor notification:%+v", n)
continue
}
a.Notifications.Lldp <- LldpNotif
}
}
}
// startLldpNotificationStream starts a notification stream for Lldp Neighbor service notifications.
func (a *Agent) startLldpNotificationStream(ctx context.Context) chan *ndk.NotificationStreamResponse {
streamID := a.createNotificationStream(ctx)
a.logger.Info().
Uint64("stream-id", streamID).
Msg("Lldp Neighbor notification stream created")
a.addLldpSubscription(ctx, streamID)
streamChan := make(chan *ndk.NotificationStreamResponse)
go a.startNotificationStream(ctx, streamID,
"Lldp neighbor", streamChan)
return streamChan
}
// addLldpSubscription adds a subscription for Lldp Neighbor service notifications
// to the allocated notification stream.
func (a *Agent) addLldpSubscription(ctx context.Context, streamID uint64) {
// create notification register request for Lldp service
// using acquired stream ID
notificationRegisterReq := &ndk.NotificationRegisterRequest{
Op: ndk.NotificationRegisterRequest_AddSubscription,
StreamId: streamID,
SubscriptionTypes: &ndk.NotificationRegisterRequest_LldpNeighbor{ // Lldp service
LldpNeighbor: &ndk.LldpNeighborSubscriptionRequest{},
},
}
registerResp, err := a.stubs.sdkMgrService.NotificationRegister(ctx, notificationRegisterReq)
if err != nil || registerResp.GetStatus() != ndk.SdkMgrStatus_kSdkMgrSuccess {
a.logger.Printf("agent %s failed registering to notification with req=%+v: %v",
a.Name, notificationRegisterReq, err)
}
}