Newer
Older
package server
import (
"context"
"time"
subpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/subscriptionmanagement"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus"
"code.fbi.h-da.de/danet/gosdn/forks/goarista/gnmi"
"github.com/google/uuid"
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
)
// SubManagementServer represents a SubsriptionManagementServer.
type SubManagementServer struct {
subpb.UnimplementedSubscriptionManagementServiceServer
networkElementWatchter *nucleus.NetworkElementWatcher
}
// NewSubManagementServer returns a new SubManagementServer.
func NewSubManagementServer(networkElementWatchter *nucleus.NetworkElementWatcher) *SubManagementServer {
return &SubManagementServer{
networkElementWatchter: networkElementWatchter,
}
}
// ResetAllSubscriptions stops all running subscriptions and restarts them the same way they ran before.
func (s *SubManagementServer) ResetAllSubscriptions(ctx context.Context, request *subpb.ResetAllSubscriptionsRequest) (*subpb.ResetAllSubscriptionsResponse, error) {
subscriptions := s.networkElementWatchter.GetAllSubscriptionInformations()
s.networkElementWatchter.StopAndRemoveAllNetworkElementSubscriptions()
s.networkElementWatchter.SubscribeToNetworkElements(subscriptions)
return &subpb.ResetAllSubscriptionsResponse{
Timestamp: time.Now().UnixNano(),
}, nil
}
// GetAll returns information about all running subscriptions.
func (s *SubManagementServer) GetAll(ctx context.Context, request *subpb.GetAllRequest) (*subpb.GetAllResponse, error) {
subInfos := s.networkElementWatchter.GetAllSubscriptionInformations()
subInfosToReturn := make([]*subpb.Subscription, 0)
for _, info := range subInfos {
tmpPaths := make([]*subpb.Path, 0)
for _, path := range info.Opts.Paths {
tmpPaths = append(tmpPaths, &subpb.Path{
Elem: path,
})
}
subInfosToReturn = append(subInfosToReturn, &subpb.Subscription{
Subid: info.SubID,
Pid: info.PndID,
Mneid: info.MneID,
MneName: info.MneName,
Paths: tmpPaths,
SubscribeOptions: &subpb.SubscribeOptions{
GnmiMode: info.Opts.Mode,
GnmiStreamMode: info.Opts.StreamMode,
SampleInterval: info.Opts.SampleInterval,
},
})
}
return &subpb.GetAllResponse{
Timestamp: time.Now().UnixNano(),
Subscriptions: subInfosToReturn,
}, nil
}
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
166
167
// Get fetches detailed info for one specific available subscription.
func (s *SubManagementServer) Get(ctx context.Context, request *subpb.GetRequest) (*subpb.GetResponse, error) {
subUUID, err := uuid.Parse(request.GetSubid())
if err != nil {
return &subpb.GetResponse{
Timestamp: time.Now().UnixNano(),
}, err
}
info, err := s.networkElementWatchter.GetSubscriptionInformations(subUUID)
if err != nil {
return &subpb.GetResponse{
Timestamp: time.Now().UnixNano(),
}, err
}
var pathsToReturn []*subpb.Path
for _, path := range info.Opts.Paths {
var elems []string
for _, elem := range path {
elems = append(elems, elem)
}
pathsToReturn = append(pathsToReturn, &subpb.Path{Elem: elems})
}
return &subpb.GetResponse{
Timestamp: time.Now().UnixNano(),
Subscriptions: &subpb.Subscription{
Subid: info.SubID,
Pid: info.PndID,
Mneid: info.MneID,
MneName: info.MneName,
Paths: pathsToReturn,
SubscribeOptions: &subpb.SubscribeOptions{
GnmiMode: info.Opts.Mode,
GnmiStreamMode: info.Opts.StreamMode,
SampleInterval: info.Opts.SampleInterval,
},
},
}, nil
}
// Delete stops and removes one specific running subscription is a no-op if object connected to ID does not exist.
func (s *SubManagementServer) Delete(ctx context.Context, request *subpb.DeleteRequest) (*subpb.DeleteResponse, error) {
subUUID, err := uuid.Parse(request.GetSubid())
if err != nil {
return &subpb.DeleteResponse{
Timestamp: time.Now().UnixNano(),
}, err
}
s.networkElementWatchter.StopAndRemoveNetworkElementSubscription(subUUID)
return &subpb.DeleteResponse{
Timestamp: time.Now().UnixNano(),
}, nil
}
// Add creates a new subscription for the network element matching the provided ID using the provided subscribe options.
func (s *SubManagementServer) Add(ctx context.Context, request *subpb.AddRequest) (*subpb.AddResponse, error) {
mneID, err := uuid.Parse(request.GetMneid())
if err != nil {
return &subpb.AddResponse{
Timestamp: time.Now().UnixNano(),
}, err
}
var paths [][]string
for _, path := range request.Subscription.Paths {
var elems []string
for _, elem := range path.Elem {
elems = append(elems, elem)
}
paths = append(paths, elems)
}
err = s.networkElementWatchter.SubscribeToNetworkElementWithID(mneID,
&gnmi.SubscribeOptions{
Mode: request.Subscription.SubscribeOptions.GnmiMode,
StreamMode: request.Subscription.SubscribeOptions.GnmiStreamMode,
SampleInterval: request.Subscription.SubscribeOptions.SampleInterval,
Paths: paths,
},
)
if err != nil {
return &subpb.AddResponse{
Timestamp: time.Now().UnixNano(),
}, err
}
return &subpb.AddResponse{
Timestamp: time.Now().UnixNano(),
}, nil
}