forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.go
613 lines (528 loc) · 18.7 KB
/
rest.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kvscheduler
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
"github.com/unrolled/render"
"go.ligato.io/cn-infra/v2/rpc/rest"
kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
"go.ligato.io/vpp-agent/v3/plugins/kvscheduler/internal/graph"
"go.ligato.io/vpp-agent/v3/plugins/kvscheduler/internal/utils"
"go.ligato.io/vpp-agent/v3/proto/ligato/kvscheduler"
)
const (
// prefix used for REST urls of the KVScheduler.
urlPrefix = "/scheduler/"
// txnHistoryURL is URL used to obtain the transaction history.
txnHistoryURL = urlPrefix + "txn-history"
// sinceArg is the name of the argument used to define the start of the time
// window for the transaction history to display.
sinceArg = "since"
// untilArg is the name of the argument used to define the end of the time
// window for the transaction history to display.
untilArg = "until"
// seqNumArg is the name of the argument used to define the sequence number
// of the transaction to display (txnHistoryURL).
seqNumArg = "seq-num"
// formatArg is the name of the argument used to set the output format
// for the transaction history API.
formatArg = "format"
// recognized formats:
formatJSON = "json"
formatText = "text"
// keyTimelineURL is URL used to obtain timeline of value changes for a given key.
keyTimelineURL = urlPrefix + "key-timeline"
// keyArg is the name of the argument used to define key for "key-timeline" and "status" API.
keyArg = "key"
// graphSnapshotURL is URL used to obtain graph snapshot from a given point in time.
graphSnapshotURL = urlPrefix + "graph-snapshot"
// flagStatsURL is URL used to obtain flag statistics.
flagStatsURL = urlPrefix + "flag-stats"
// flagArg is the name of the argument used to define flag for "flag-stats" API.
flagArg = "flag"
// prefixArg is the name of the argument used to define prefix to filter keys
// for "flag-stats" API.
prefixArg = "prefix"
// time is the name of the argument used to define point in time for a graph snapshot
// to retrieve. Value = number of nanoseconds since the start of the epoch.
timeArg = "time"
// downstreamResyncURL is URL used to trigger downstream-resync.
downstreamResyncURL = urlPrefix + "downstream-resync"
// retryArg is the name of the argument used for "downstream-resync" API to tell whether
// to retry failed operations or not.
retryArg = "retry"
// verboseArg is the name of the argument used for "downstream-resync" API
// to tell whether the refreshed graph should be printed to stdout or not.
verboseArg = "verbose"
// dumpURL is URL used to dump either SB or scheduler's internal state of kv-pairs
// under the given descriptor / key-prefix.
dumpURL = urlPrefix + "dump"
// descriptorArg is the name of the argument used to define descriptor for "dump" API.
descriptorArg = "descriptor"
// keyPrefixArg is the name of the argument used to define key prefix for "dump" API.
keyPrefixArg = "key-prefix"
// viewArg is the name of the argument used for "dump" API to chooses from
// which point of view to look at the key-value space when dumping values.
// See type View from kvscheduler's API to learn the set of possible values.
viewArg = "view"
// txnArg allows to display graph at the time when the referenced transaction
// has just finalized
txnArg = "txn" // value = txn sequence number
// statusURL is URL used to print the state of values under the given
// descriptor / key-prefix or all of them.
statusURL = urlPrefix + "status"
)
// errorString wraps string representation of an error that, unlike the original
// error, can be marshalled.
type errorString struct {
Error string
}
// dumpIndex defines "index" page for the Dump REST API.
type dumpIndex struct {
Descriptors []string
KeyPrefixes []string
Views []string
}
// recordKVsWithMetadata converts a list of key-value pairs with metadata
// into an equivalent list with proto.Message recorded for proper marshalling.
func recordKVsWithMetadata(in []kvs.KVWithMetadata) (out []kvs.RecordedKVWithMetadata) {
for _, kv := range in {
out = append(out, kvs.RecordedKVWithMetadata{
RecordedKVPair: kvs.RecordedKVPair{
Key: kv.Key,
Value: utils.RecordProtoMessage(kv.Value),
Origin: kv.Origin,
},
Metadata: kv.Metadata,
})
}
return out
}
// registerHandlers registers all supported REST APIs.
func (s *Scheduler) registerHandlers(http rest.HTTPHandlers) {
if http == nil {
s.Log.Debug("No http handler provided, skipping registration of KVScheduler REST handlers")
return
}
http.RegisterHTTPHandler(txnHistoryURL, s.txnHistoryGetHandler, "GET")
http.RegisterHTTPHandler(keyTimelineURL, s.keyTimelineGetHandler, "GET")
http.RegisterHTTPHandler(graphSnapshotURL, s.graphSnapshotGetHandler, "GET")
http.RegisterHTTPHandler(flagStatsURL, s.flagStatsGetHandler, "GET")
http.RegisterHTTPHandler(downstreamResyncURL, s.downstreamResyncPostHandler, "POST")
http.RegisterHTTPHandler(dumpURL, s.dumpGetHandler, "GET")
http.RegisterHTTPHandler(statusURL, s.statusGetHandler, "GET")
http.RegisterHTTPHandler(urlPrefix+"graph", s.graphHandler, "GET")
http.RegisterHTTPHandler(urlPrefix+"stats", s.statsHandler, "GET")
}
// txnHistoryGetHandler is the GET handler for "txn-history" API.
func (s *Scheduler) txnHistoryGetHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
var since, until time.Time
var seqNum uint64
args := req.URL.Query()
// parse optional *format* argument (default = JSON)
format := formatJSON
if formatStr, withFormat := args[formatArg]; withFormat && len(formatStr) == 1 {
format = formatStr[0]
if format != formatJSON && format != formatText {
err := errors.New("unrecognized output format")
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
return
}
}
// parse optional *seq-num* argument
if seqNumStr, withSeqNum := args[seqNumArg]; withSeqNum && len(seqNumStr) == 1 {
var err error
seqNum, err = strconv.ParseUint(seqNumStr[0], 10, 64)
if err != nil {
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
return
}
// sequence number takes precedence over the since-until time window
txn := s.GetRecordedTransaction(seqNum)
if txn == nil {
err := errors.New("transaction with such sequence number is not recorded")
s.logError(formatter.JSON(w, http.StatusNotFound, errorString{err.Error()}))
return
}
if format == formatJSON {
s.logError(formatter.JSON(w, http.StatusOK, txn))
} else {
s.logError(formatter.Text(w, http.StatusOK, txn.StringWithOpts(false, true, 0)))
}
return
}
// parse optional *until* argument
if untilStr, withUntil := args[untilArg]; withUntil && len(untilStr) == 1 {
var err error
until, err = stringToTime(untilStr[0])
if err != nil {
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
return
}
}
// parse optional *since* argument
if sinceStr, withSince := args[sinceArg]; withSince && len(sinceStr) == 1 {
var err error
since, err = stringToTime(sinceStr[0])
if err != nil {
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
return
}
}
txnHistory := s.GetTransactionHistory(since, until)
if format == formatJSON {
s.logError(formatter.JSON(w, http.StatusOK, txnHistory))
} else {
s.logError(formatter.Text(w, http.StatusOK, txnHistory.StringWithOpts(false, false, 0)))
}
}
}
// keyTimelineGetHandler is the GET handler for "key-timeline" API.
func (s *Scheduler) keyTimelineGetHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
args := req.URL.Query()
// parse optional *time* argument
var timeVal time.Time
if timeStr, withTime := args[timeArg]; withTime && len(timeStr) == 1 {
var err error
timeVal, err = stringToTime(timeStr[0])
if err != nil {
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
return
}
}
// parse mandatory *key* argument
if keys, withKey := args[keyArg]; withKey && len(keys) == 1 {
graphR := s.graph.Read()
defer graphR.Release()
timeline := graphR.GetNodeTimeline(keys[0])
if !timeVal.IsZero() {
var nodeRecord *graph.RecordedNode
for _, record := range timeline {
if record.Since.Before(timeVal) &&
(record.Until.IsZero() || record.Until.After(timeVal)) {
nodeRecord = record
break
}
}
s.logError(formatter.JSON(w, http.StatusOK, nodeRecord))
return
}
s.logError(formatter.JSON(w, http.StatusOK, timeline))
return
}
err := errors.New("missing key argument")
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
}
}
// graphSnapshotGetHandler is the GET handler for "graph-snapshot" API.
func (s *Scheduler) graphSnapshotGetHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
timeVal := time.Now()
args := req.URL.Query()
// parse optional *time* argument
if timeStr, withTime := args[timeArg]; withTime && len(timeStr) == 1 {
var err error
timeVal, err = stringToTime(timeStr[0])
if err != nil {
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
return
}
}
graphR := s.graph.Read()
defer graphR.Release()
snapshot := graphR.GetSnapshot(timeVal)
s.logError(formatter.JSON(w, http.StatusOK, snapshot))
}
}
// flagStatsGetHandler is the GET handler for "flag-stats" API.
func (s *Scheduler) flagStatsGetHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
args := req.URL.Query()
// parse repeated *prefix* argument
prefixes := args[prefixArg]
if flags, withFlag := args[flagArg]; withFlag && len(flags) == 1 {
graphR := s.graph.Read()
defer graphR.Release()
stats := graphR.GetFlagStats(flagNameToIndex(flags[0]), func(key string) bool {
if len(prefixes) == 0 {
return true
}
for _, prefix := range prefixes {
if strings.HasPrefix(key, prefix) {
return true
}
}
return false
})
s.logError(formatter.JSON(w, http.StatusOK, stats))
return
}
err := errors.New("missing flag argument")
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
}
}
// downstreamResyncPostHandler is the POST handler for "downstream-resync" API.
func (s *Scheduler) downstreamResyncPostHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
// parse optional *retry* argument
args := req.URL.Query()
retry := false
if retryStr, withRetry := args[retryArg]; withRetry && len(retryStr) == 1 {
retryVal := retryStr[0]
if retryVal == "true" || retryVal == "1" {
retry = true
}
}
// parse optional *verbose* argument
verbose := false
if verboseStr, withVerbose := args[verboseArg]; withVerbose && len(verboseStr) == 1 {
verboseVal := verboseStr[0]
if verboseVal == "true" || verboseVal == "1" {
verbose = true
}
}
ctx := context.Background()
ctx = kvs.WithResync(ctx, kvs.DownstreamResync, verbose)
if retry {
ctx = kvs.WithRetryDefault(ctx)
}
seqNum, err := s.StartNBTransaction().Commit(ctx)
if err != nil {
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
return
}
txn := s.GetRecordedTransaction(seqNum)
s.logError(formatter.JSON(w, http.StatusOK, txn))
}
}
func parseDumpAndStatusCommonArgs(args url.Values) (descriptor, keyPrefix, key string, err error) {
// parse optional *descriptor* argument
descriptors, withDescriptor := args[descriptorArg]
if withDescriptor && len(descriptors) != 1 {
err = errors.New("descriptor argument listed more than once")
return
}
if withDescriptor {
descriptor = descriptors[0]
}
// parse optional *key-prefix* argument
keyPrefixes, withKeyPrefix := args[keyPrefixArg]
if withKeyPrefix && len(keyPrefixes) != 1 {
err = errors.New("key-prefix argument listed more than once")
return
}
if withKeyPrefix {
keyPrefix = keyPrefixes[0]
}
// parse optional *key* argument
keys, withKey := args[keyArg]
if withKey && len(keys) != 1 {
err = errors.New("key argument listed more than once")
return
}
if withKey {
key = keys[0]
}
return
}
// dumpGetHandler is the GET handler for "dump" API.
func (s *Scheduler) dumpGetHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
args := req.URL.Query()
descriptor, keyPrefix, _, err := parseDumpAndStatusCommonArgs(args)
if err != nil {
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
return
}
// without descriptor and key prefix return "index" page
if descriptor == "" && keyPrefix == "" {
s.txnLock.Lock()
defer s.txnLock.Unlock()
index := dumpIndex{Views: []string{
kvs.SBView.String(), kvs.NBView.String(), kvs.CachedView.String()}}
for _, descriptor := range s.registry.GetAllDescriptors() {
index.Descriptors = append(index.Descriptors, descriptor.Name)
index.KeyPrefixes = append(index.KeyPrefixes, descriptor.NBKeyPrefix)
}
s.logError(formatter.JSON(w, http.StatusOK, index))
return
}
// parse optional *view* argument (default = SBView)
var view kvs.View
if viewStr, withState := args[viewArg]; withState && len(viewStr) == 1 {
switch viewStr[0] {
case kvs.SBView.String():
view = kvs.SBView
case kvs.NBView.String():
view = kvs.NBView
case kvs.CachedView.String():
view = kvs.CachedView
default:
err := errors.New("unrecognized system view")
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
return
}
}
var dump []kvs.KVWithMetadata
if descriptor != "" {
dump, err = s.DumpValuesByDescriptor(descriptor, view)
if err != nil {
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
return
}
} else {
dump, err = s.DumpValuesByKeyPrefix(keyPrefix, view)
if err != nil {
s.logError(formatter.JSON(w, http.StatusNotFound, errorString{err.Error()}))
return
}
}
s.logError(formatter.JSON(w, http.StatusOK, recordKVsWithMetadata(dump)))
}
}
// statusGetHandler is the GET handler for "status" API.
func (s *Scheduler) statusGetHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
args := req.URL.Query()
descriptor, keyPrefix, key, err := parseDumpAndStatusCommonArgs(args)
if err != nil {
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
return
}
graphR := s.graph.Read()
defer graphR.Release()
if key != "" {
singleStatus := getValueStatus(graphR.GetNode(key), key)
s.logError(formatter.JSON(w, http.StatusOK, singleStatus))
return
}
if descriptor == "" && keyPrefix != "" {
descriptor = s.getDescriptorForKeyPrefix(keyPrefix)
if descriptor == "" {
err = errors.New("unknown key prefix")
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
return
}
}
var nodes []graph.Node
if descriptor == "" {
// get all nodes with base values
nodes = graphR.GetNodes(nil, graph.WithoutFlags(&DerivedFlag{}))
} else {
// get nodes with base values under the given descriptor
nodes = graphR.GetNodes(nil,
graph.WithFlags(&DescriptorFlag{descriptor}),
graph.WithoutFlags(&DerivedFlag{}))
}
var status []*kvscheduler.BaseValueStatus
for _, node := range nodes {
status = append(status, getValueStatus(node, node.GetKey()))
}
// sort by keys
sort.Slice(status, func(i, j int) bool {
return status[i].Value.Key < status[j].Value.Key
})
s.logError(formatter.JSON(w, http.StatusOK, status))
}
}
func (s *Scheduler) graphHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
args := req.URL.Query()
s.txnLock.Lock()
defer s.txnLock.Unlock()
graphRead := s.graph.Read()
defer graphRead.Release()
var txn *kvs.RecordedTxn
timestamp := time.Now()
// parse optional *txn* argument
if txnStr, withTxn := args[txnArg]; withTxn && len(txnStr) == 1 {
txnSeqNum, err := strconv.ParseUint(txnStr[0], 10, 64)
if err != nil {
s.logError(formatter.JSON(w, http.StatusInternalServerError, errorString{err.Error()}))
return
}
txn = s.GetRecordedTransaction(txnSeqNum)
if txn == nil {
err := errors.New("transaction with such sequence number is not recorded")
s.logError(formatter.JSON(w, http.StatusNotFound, errorString{err.Error()}))
return
}
timestamp = txn.Stop
}
graphSnapshot := graphRead.GetSnapshot(timestamp)
output, err := s.renderDotOutput(graphSnapshot, txn)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
format := req.FormValue("format")
switch format {
case "raw":
_, err = w.Write(output)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
case "dot":
dot, err := validateDot(output)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write(dot)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
default:
format = "svg"
}
img, err := dotToImage("", format, output)
if err != nil {
http.Error(w, fmt.Sprintf("rendering image %v failed: %v\n%s", img, err, output), http.StatusInternalServerError)
return
}
s.Log.Debug("serving graph image from:", img)
http.ServeFile(w, req, img)
}
}
func (s *Scheduler) statsHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
s.logError(formatter.JSON(w, http.StatusOK, GetStats()))
}
}
// logError logs non-nil errors from JSON formatter
func (s *Scheduler) logError(err error) {
if err != nil {
s.Log.Error(err)
}
}
// stringToTime converts Unix timestamp from string to time.Time.
func stringToTime(s string) (time.Time, error) {
nsec, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(0, nsec), nil
}