/
orm.go
662 lines (568 loc) · 19 KB
/
orm.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
* Portions copyright 2019-present Open Networking Foundation
* Original copyright 2019-present Ciena Corporation
*
* 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 commands
import (
"context"
"fmt"
"github.com/fullstorydev/grpcurl"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/dynamic"
corderrors "github.com/opencord/cordctl/internal/pkg/error"
"google.golang.org/grpc"
"io"
"strconv"
"strings"
"time"
)
// Flags for calling the *WithRetry methods
const (
GM_QUIET = 1
GM_UNTIL_FOUND = 2
GM_UNTIL_ENACTED = 4
GM_UNTIL_STATUS = 8
)
// Valid choices for FilterModels `Kind` argument
const (
FILTER_DEFAULT = "DEFAULT"
FILTER_ALL = "ALL"
FILTER_DIRTY = "SYNCHRONIZER_DIRTY_OBJECTS"
FILTER_DELETED = "SYNCHRONIZER_DELETED_OBJECTS"
FILTER_DIRTYPOL = "SYNCHRONIZER_DIRTY_POLICIES"
FILTER_DELETEDPOL = "SYNCHRONIZER_DELETED_POLICIES"
)
type QueryEventHandler struct {
RpcEventHandler
Elements map[string]string
Model *desc.MessageDescriptor
Kind string
EOF bool
}
// Separate the operator from the query value.
// For example,
// "==foo" --> "EQUAL", "foo"
func DecodeOperator(query string) (string, string, bool, error) {
if strings.HasPrefix(query, "!=") {
return strings.TrimSpace(query[2:]), "EQUAL", true, nil
} else if strings.HasPrefix(query, "==") {
return "", "", false, corderrors.NewInvalidInputError("Operator == is now allowed. Suggest using = instead.")
} else if strings.HasPrefix(query, "~=") {
return strings.TrimSpace(query[2:]), "REGEX", false, nil
} else if strings.HasPrefix(query, "[=") {
return strings.TrimSpace(query[2:]), "CONTAINS", false, nil
} else if strings.HasPrefix(query, "=") {
return strings.TrimSpace(query[1:]), "EQUAL", false, nil
} else if strings.HasPrefix(query, ">=") {
return strings.TrimSpace(query[2:]), "GREATER_THAN_OR_EQUAL", false, nil
} else if strings.HasPrefix(query, ">") {
return strings.TrimSpace(query[1:]), "GREATER_THAN", false, nil
} else if strings.HasPrefix(query, "<=") {
return strings.TrimSpace(query[2:]), "LESS_THAN_OR_EQUAL", false, nil
} else if strings.HasPrefix(query, "<") {
return strings.TrimSpace(query[1:]), "LESS_THAN", false, nil
} else {
return strings.TrimSpace(query), "EQUAL", false, nil
}
}
// Generate the parameters for Query messages.
func (h *QueryEventHandler) GetParams(msg proto.Message) error {
dmsg, err := dynamic.AsDynamicMessage(msg)
if err != nil {
return err
}
//fmt.Printf("MessageName: %s\n", dmsg.XXX_MessageName())
if h.EOF {
return io.EOF
}
// Get the MessageType for the `elements` field
md := dmsg.GetMessageDescriptor()
elements_fld := md.FindFieldByName("elements")
elements_mt := elements_fld.GetMessageType()
for field_name, element := range h.Elements {
value, operator, invert, err := DecodeOperator(element)
if err != nil {
return err
}
nm := dynamic.NewMessage(elements_mt)
field_descriptor := h.Model.FindFieldByName(field_name)
if field_descriptor == nil {
return corderrors.WithStackTrace(&corderrors.FieldDoesNotExistError{ModelName: h.Model.GetName(), FieldName: field_name})
}
field_type := field_descriptor.GetType()
switch field_type {
case descriptor.FieldDescriptorProto_TYPE_INT32:
var i int64
i, err = strconv.ParseInt(value, 10, 32)
nm.SetFieldByName("iValue", int32(i))
case descriptor.FieldDescriptorProto_TYPE_UINT32:
var i int64
i, err = strconv.ParseInt(value, 10, 32)
nm.SetFieldByName("iValue", uint32(i))
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
err = corderrors.NewInvalidInputError("Floating point filters are unsupported")
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
err = corderrors.NewInvalidInputError("Floating point filters are unsupported")
default:
nm.SetFieldByName("sValue", value)
err = nil
}
if err != nil {
return err
}
nm.SetFieldByName("name", field_name)
nm.SetFieldByName("invert", invert)
SetEnumValue(nm, "operator", operator)
dmsg.AddRepeatedFieldByName("elements", nm)
}
SetEnumValue(dmsg, "kind", h.Kind)
h.EOF = true
return nil
}
// Take a string list of queries and turns it into a map of queries
func QueryStringsToMap(query_args []string, allow_inequality bool) (map[string]string, error) {
queries := make(map[string]string)
for _, query_str := range query_args {
query_str := strings.TrimSpace(query_str)
operator_pos := -1
for i, ch := range query_str {
if allow_inequality {
if (ch == '!') || (ch == '=') || (ch == '>') || (ch == '<') || (ch == '~') || (ch == '[') {
operator_pos = i
break
}
} else {
if ch == '=' {
operator_pos = i
break
}
}
}
if operator_pos == -1 {
return nil, corderrors.WithStackTrace(&corderrors.IllegalQueryError{Query: query_str})
}
queries[strings.TrimSpace(query_str[:operator_pos])] = query_str[operator_pos:]
}
return queries, nil
}
// Take a string of comma-separated queries and turn it into a map of queries
func CommaSeparatedQueryToMap(query_str string, allow_inequality bool) (map[string]string, error) {
if query_str == "" {
return nil, nil
}
query_strings := strings.Split(query_str, ",")
return QueryStringsToMap(query_strings, allow_inequality)
}
// Convert a string into the appropriate gRPC type for a given field
func TypeConvert(source grpcurl.DescriptorSource, modelName string, field_name string, v string) (interface{}, error) {
model_descriptor, err := source.FindSymbol("xos." + modelName)
if err != nil {
return nil, err
}
model_md, ok := model_descriptor.(*desc.MessageDescriptor)
if !ok {
return nil, corderrors.WithStackTrace(&corderrors.TypeConversionError{Source: modelName, Destination: "messageDescriptor"})
}
field_descriptor := model_md.FindFieldByName(field_name)
if field_descriptor == nil {
return nil, corderrors.WithStackTrace(&corderrors.FieldDoesNotExistError{ModelName: modelName, FieldName: field_name})
}
field_type := field_descriptor.GetType()
var result interface{}
switch field_type {
case descriptor.FieldDescriptorProto_TYPE_INT32:
var i int64
i, err = strconv.ParseInt(v, 10, 32)
result = int32(i)
case descriptor.FieldDescriptorProto_TYPE_UINT32:
var i int64
i, err = strconv.ParseInt(v, 10, 32)
result = uint32(i)
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
var f float64
f, err = strconv.ParseFloat(v, 32)
result = float32(f)
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
var f float64
f, err = strconv.ParseFloat(v, 64)
result = f
default:
result = v
err = nil
}
return result, err
}
// Return a list of all available model names
func GetModelNames(source grpcurl.DescriptorSource) (map[string]bool, error) {
models := make(map[string]bool)
methods, err := grpcurl.ListMethods(source, "xos.xos")
if err != nil {
return nil, err
}
for _, method := range methods {
if strings.HasPrefix(method, "xos.xos.Get") {
models[method[11:]] = true
}
}
return models, nil
}
// Check to see if a model name is valid
func CheckModelName(source grpcurl.DescriptorSource, name string) error {
models, err := GetModelNames(source)
if err != nil {
return err
}
_, present := models[name]
if !present {
return corderrors.WithStackTrace(&corderrors.UnknownModelTypeError{Name: name})
}
return nil
}
// Create a model in XOS given a map of fields
func CreateModel(conn *grpc.ClientConn, descriptor grpcurl.DescriptorSource, modelName string, fields map[string]interface{}) error {
ctx, cancel := GrpcTimeoutContext(context.Background())
defer cancel()
headers := GenerateHeaders()
h := &RpcEventHandler{
Fields: map[string]map[string]interface{}{"xos." + modelName: fields},
}
err := grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.xos.Create"+modelName, headers, h, h.GetParams)
if err != nil {
return corderrors.RpcErrorWithModelNameToCordError(err, modelName)
} else if h.Status != nil && h.Status.Err() != nil {
return corderrors.RpcErrorWithModelNameToCordError(h.Status.Err(), modelName)
}
resp, err := dynamic.AsDynamicMessage(h.Response)
if err != nil {
return err
}
fields["id"] = resp.GetFieldByName("id").(int32)
if resp.HasFieldName("uuid") {
fields["uuid"] = resp.GetFieldByName("uuid").(string)
}
return nil
}
// Update a model in XOS given a map of fields
func UpdateModel(conn *grpc.ClientConn, descriptor grpcurl.DescriptorSource, modelName string, fields map[string]interface{}) error {
ctx, cancel := GrpcTimeoutContext(context.Background())
defer cancel()
headers := GenerateHeaders()
h := &RpcEventHandler{
Fields: map[string]map[string]interface{}{"xos." + modelName: fields},
}
err := grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.xos.Update"+modelName, headers, h, h.GetParams)
if err != nil {
return corderrors.RpcErrorWithModelNameToCordError(err, modelName)
} else if h.Status != nil && h.Status.Err() != nil {
return corderrors.RpcErrorWithModelNameToCordError(h.Status.Err(), modelName)
}
resp, err := dynamic.AsDynamicMessage(h.Response)
if err != nil {
return err
}
// TODO: Do we need to do anything with the response?
_ = resp
return nil
}
// Get a model from XOS given its ID
func GetModel(ctx context.Context, conn *grpc.ClientConn, descriptor grpcurl.DescriptorSource, modelName string, id int32) (*dynamic.Message, error) {
ctx, cancel := GrpcTimeoutContext(context.Background())
defer cancel()
headers := GenerateHeaders()
h := &RpcEventHandler{
Fields: map[string]map[string]interface{}{"xos.ID": map[string]interface{}{"id": id}},
}
err := grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.xos.Get"+modelName, headers, h, h.GetParams)
if err != nil {
return nil, corderrors.RpcErrorWithIdToCordError(err, modelName, id)
}
if h.Status != nil && h.Status.Err() != nil {
return nil, corderrors.RpcErrorWithIdToCordError(h.Status.Err(), modelName, id) //h.Status.Err()
}
d, err := dynamic.AsDynamicMessage(h.Response)
if err != nil {
return nil, err
}
return d, nil
}
// Get a model, but retry under a variety of circumstances
func GetModelWithRetry(ctx context.Context, conn *grpc.ClientConn, descriptor grpcurl.DescriptorSource, modelName string, id int32, flags uint32) (*grpc.ClientConn, *dynamic.Message, error) {
quiet := (flags & GM_QUIET) != 0
until_found := (flags & GM_UNTIL_FOUND) != 0
until_enacted := (flags & GM_UNTIL_ENACTED) != 0
until_status := (flags & GM_UNTIL_STATUS) != 0
for {
var err error
if conn == nil {
conn, err = NewConnection()
if err != nil {
return nil, nil, err
}
}
model, err := GetModel(ctx, conn, descriptor, modelName, id)
if err != nil {
if strings.Contains(err.Error(), "rpc error: code = Unavailable") ||
strings.Contains(err.Error(), "rpc error: code = Internal desc = stream terminated by RST_STREAM") {
if !quiet {
fmt.Print(".")
}
select {
case <-time.After(100 * time.Millisecond):
case <-ctx.Done():
return nil, nil, ctx.Err()
}
conn.Close()
conn = nil
continue
}
_, is_not_found_error := err.(*corderrors.ModelNotFoundError)
if until_found && is_not_found_error {
if !quiet {
fmt.Print("x")
}
select {
case <-time.After(100 * time.Millisecond):
case <-ctx.Done():
return nil, nil, ctx.Err()
}
continue
}
return nil, nil, err
}
if until_enacted && !IsEnacted(model) {
if !quiet {
fmt.Print("o")
}
select {
case <-time.After(100 * time.Millisecond):
case <-ctx.Done():
return nil, nil, ctx.Err()
}
continue
}
if until_status && model.GetFieldByName("status") == nil {
if !quiet {
fmt.Print("O")
}
select {
case <-time.After(100 * time.Millisecond):
case <-ctx.Done():
return nil, nil, ctx.Err()
}
continue
}
return conn, model, nil
}
}
func ItemsToDynamicMessageList(items interface{}) []*dynamic.Message {
result := make([]*dynamic.Message, len(items.([]interface{})))
for i, item := range items.([]interface{}) {
result[i] = item.(*dynamic.Message)
}
return result
}
// List all objects of a given model
func ListModels(ctx context.Context, conn *grpc.ClientConn, descriptor grpcurl.DescriptorSource, modelName string) ([]*dynamic.Message, error) {
ctx, cancel := GrpcTimeoutContext(context.Background())
defer cancel()
headers := GenerateHeaders()
h := &RpcEventHandler{}
err := grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.xos.List"+modelName, headers, h, h.GetParams)
if err != nil {
return nil, corderrors.RpcErrorWithModelNameToCordError(err, modelName)
}
if h.Status != nil && h.Status.Err() != nil {
return nil, corderrors.RpcErrorWithModelNameToCordError(h.Status.Err(), modelName)
}
d, err := dynamic.AsDynamicMessage(h.Response)
if err != nil {
return nil, err
}
items, err := d.TryGetFieldByName("items")
if err != nil {
return nil, err
}
return ItemsToDynamicMessageList(items), nil
}
// Filter models based on field values
// queries is a map of <field_name> to <operator><query>
// For example,
// map[string]string{"name": "==mysite"}
func FilterModels(ctx context.Context, conn *grpc.ClientConn, descriptor grpcurl.DescriptorSource, modelName string, kind string, queries map[string]string) ([]*dynamic.Message, error) {
ctx, cancel := GrpcTimeoutContext(context.Background())
defer cancel()
headers := GenerateHeaders()
model_descriptor, err := descriptor.FindSymbol("xos." + modelName)
if err != nil {
return nil, err
}
model_md, ok := model_descriptor.(*desc.MessageDescriptor)
if !ok {
return nil, corderrors.WithStackTrace(&corderrors.TypeConversionError{Source: modelName, Destination: "messageDescriptor"})
}
h := &QueryEventHandler{
RpcEventHandler: RpcEventHandler{
Fields: map[string]map[string]interface{}{"xos.Query": map[string]interface{}{"kind": 0}},
},
Elements: queries,
Model: model_md,
Kind: kind,
}
err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.xos.Filter"+modelName, headers, h, h.GetParams)
if err != nil {
return nil, corderrors.RpcErrorWithQueriesToCordError(err, modelName, queries)
}
if h.Status != nil && h.Status.Err() != nil {
return nil, corderrors.RpcErrorWithQueriesToCordError(h.Status.Err(), modelName, queries)
}
d, err := dynamic.AsDynamicMessage(h.Response)
if err != nil {
return nil, err
}
items, err := d.TryGetFieldByName("items")
if err != nil {
return nil, err
}
return ItemsToDynamicMessageList(items), nil
}
// Call ListModels or FilterModels as appropriate
func ListOrFilterModels(ctx context.Context, conn *grpc.ClientConn, descriptor grpcurl.DescriptorSource, modelName string, kind string, queries map[string]string) ([]*dynamic.Message, error) {
if (len(queries) == 0) && (kind == FILTER_DEFAULT) {
return ListModels(ctx, conn, descriptor, modelName)
} else {
return FilterModels(ctx, conn, descriptor, modelName, kind, queries)
}
}
// Get a model from XOS given a fieldName/fieldValue
func FindModel(ctx context.Context, conn *grpc.ClientConn, descriptor grpcurl.DescriptorSource, modelName string, queries map[string]string) (*dynamic.Message, error) {
models, err := FilterModels(ctx, conn, descriptor, modelName, FILTER_DEFAULT, queries)
if err != nil {
return nil, err
}
if len(models) == 0 {
cordError := &corderrors.ModelNotFoundError{}
cordError.Obj = corderrors.ObjectReference{ModelName: modelName, Queries: queries}
return nil, corderrors.WithStackTrace(cordError)
}
return models[0], nil
}
// Find a model, but retry under a variety of circumstances
func FindModelWithRetry(ctx context.Context, conn *grpc.ClientConn, descriptor grpcurl.DescriptorSource, modelName string, queries map[string]string, flags uint32) (*grpc.ClientConn, *dynamic.Message, error) {
quiet := (flags & GM_QUIET) != 0
until_found := (flags & GM_UNTIL_FOUND) != 0
until_enacted := (flags & GM_UNTIL_ENACTED) != 0
until_status := (flags & GM_UNTIL_STATUS) != 0
for {
var err error
if conn == nil {
conn, err = NewConnection()
if err != nil {
return nil, nil, err
}
}
model, err := FindModel(ctx, conn, descriptor, modelName, queries)
if err != nil {
if strings.Contains(err.Error(), "rpc error: code = Unavailable") ||
strings.Contains(err.Error(), "rpc error: code = Internal desc = stream terminated by RST_STREAM") {
if !quiet {
fmt.Print(".")
}
select {
case <-time.After(100 * time.Millisecond):
case <-ctx.Done():
return nil, nil, ctx.Err()
}
conn.Close()
conn = nil
continue
}
_, is_not_found_error := err.(*corderrors.ModelNotFoundError)
if until_found && is_not_found_error {
if !quiet {
fmt.Print("x")
}
select {
case <-time.After(100 * time.Millisecond):
case <-ctx.Done():
return nil, nil, ctx.Err()
}
continue
}
return nil, nil, err
}
if until_enacted && !IsEnacted(model) {
if !quiet {
fmt.Print("o")
}
select {
case <-time.After(100 * time.Millisecond):
case <-ctx.Done():
return nil, nil, ctx.Err()
}
continue
}
if until_status && model.GetFieldByName("status") == nil {
if !quiet {
fmt.Print("O")
}
select {
case <-time.After(100 * time.Millisecond):
case <-ctx.Done():
return nil, nil, ctx.Err()
}
continue
}
return conn, model, nil
}
}
// Get a model from XOS given its ID
func DeleteModel(conn *grpc.ClientConn, descriptor grpcurl.DescriptorSource, modelName string, id int32) error {
ctx, cancel := GrpcTimeoutContext(context.Background())
defer cancel()
headers := GenerateHeaders()
h := &RpcEventHandler{
Fields: map[string]map[string]interface{}{"xos.ID": map[string]interface{}{"id": id}},
}
err := grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.xos.Delete"+modelName, headers, h, h.GetParams)
if err != nil {
return corderrors.RpcErrorWithIdToCordError(err, modelName, id)
}
if h.Status != nil && h.Status.Err() != nil {
return corderrors.RpcErrorWithIdToCordError(h.Status.Err(), modelName, id)
}
_, err = dynamic.AsDynamicMessage(h.Response)
if err != nil {
return err
}
return nil
}
// Takes a *dynamic.Message and turns it into a map of fields to interfaces
// TODO: Might be more useful to convert the values to strings and ints
func MessageToMap(d *dynamic.Message) map[string]interface{} {
fields := make(map[string]interface{})
for _, field_desc := range d.GetKnownFields() {
field_name := field_desc.GetName()
fields[field_name] = d.GetFieldByName(field_name)
}
return fields
}
// Returns True if a message has been enacted
func IsEnacted(d *dynamic.Message) bool {
enacted := d.GetFieldByName("enacted").(float64)
updated := d.GetFieldByName("updated").(float64)
return (enacted >= updated)
}