-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathappid.go
More file actions
77 lines (64 loc) · 2.49 KB
/
appid.go
File metadata and controls
77 lines (64 loc) · 2.49 KB
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"
)
// ReceiveAppIdNotifications starts an AppId notification stream
// and sends notifications to channel `AppId`.
// If the main execution intends to continue running after calling this method,
// it should be called as a goroutine.
// `AppId` chan carries values of type ndk.AppIdentNotification
func (a *Agent) ReceiveAppIdNotifications(ctx context.Context) {
defer close(a.Notifications.AppId)
AppIdStream := a.startAppIdNotificationStream(ctx)
for AppIdStreamResp := range AppIdStream {
b, err := prototext.MarshalOptions{Multiline: true, Indent: " "}.Marshal(AppIdStreamResp)
if err != nil {
a.logger.Info().
Msgf("AppId notification Marshal failed: %+v", err)
continue
}
a.logger.Info().
Msgf("Received AppId notifications:\n%s", b)
for _, n := range AppIdStreamResp.GetNotifications() {
AppIdNotif := n.GetAppId()
if AppIdNotif == nil {
a.logger.Info().
Msgf("Empty AppId notification:%+v", n)
continue
}
a.Notifications.AppId <- AppIdNotif
}
}
}
// startAppIdNotificationStream starts a notification stream for AppId service notifications.
func (a *Agent) startAppIdNotificationStream(ctx context.Context) chan *ndk.NotificationStreamResponse {
streamID := a.createNotificationStream(ctx)
a.logger.Info().
Uint64("stream-id", streamID).
Msg("AppId Notification stream created")
a.addAppIdSubscription(ctx, streamID)
streamChan := make(chan *ndk.NotificationStreamResponse)
go a.startNotificationStream(ctx, streamID,
"AppId", streamChan)
return streamChan
}
// addAppIdSubscription adds a subscription for AppId service notifications
// to the allocated notification stream.
func (a *Agent) addAppIdSubscription(ctx context.Context, streamID uint64) {
// create notification register request for AppId service
// using acquired stream ID
notificationRegisterReq := &ndk.NotificationRegisterRequest{
Op: ndk.NotificationRegisterRequest_OPERATION_ADD_SUBSCRIPTION,
StreamId: streamID,
SubscriptionTypes: &ndk.NotificationRegisterRequest_AppId{ // AppId service
AppId: &ndk.AppIdentSubscriptionRequest{},
},
}
registerResp, err := a.stubs.sdkMgrService.NotificationRegister(ctx, notificationRegisterReq)
if err != nil || registerResp.GetStatus() != ndk.SdkMgrStatus_SDK_MGR_STATUS_SUCCESS {
a.logger.Printf("agent %s failed registering to notification with req=%+v: %v",
a.Name, notificationRegisterReq, err)
}
}