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
|
// 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 main
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/bennyscetbun/jsongo"
)
func getTypeByRef(ctx *context, ref string) *Type {
for _, typ := range ctx.packageData.Types {
if ref == toApiType(typ.Name) {
return &typ
}
}
return nil
}
func getUnionSize(ctx *context, union *Union) (maxSize int) {
for _, field := range union.Fields {
if typ := getTypeByRef(ctx, field.Type); typ != nil {
if size := getSizeOfType(typ); size > maxSize {
maxSize = size
}
}
}
return
}
// toApiType returns name that is used as type reference in VPP binary API
func toApiType(name string) string {
return fmt.Sprintf("vl_api_%s_t", name)
}
// parsePackage parses provided JSON data into objects prepared for code generation
func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
logf(" %s contains: %d services, %d messages, %d types, %d enums, %d unions (version: %s)",
ctx.packageName,
jsonRoot.Map("services").Len(),
jsonRoot.Map("messages").Len(),
jsonRoot.Map("types").Len(),
jsonRoot.Map("enums").Len(),
jsonRoot.Map("unions").Len(),
jsonRoot.Map("vl_api_version").Get(),
)
pkg := Package{
APIVersion: jsonRoot.Map("vl_api_version").Get().(string),
RefMap: make(map[string]string),
}
// parse enums
enums := jsonRoot.Map("enums")
pkg.Enums = make([]Enum, enums.Len())
for i := 0; i < enums.Len(); i++ {
enumNode := enums.At(i)
enum, err := parseEnum(ctx, enumNode)
if err != nil {
return nil, err
}
pkg.Enums[i] = *enum
pkg.RefMap[toApiType(enum.Name)] = enum.Name
}
// parse types
types := jsonRoot.Map("types")
pkg.Types = make([]Type, types.Len())
for i := 0; i < types.Len(); i++ {
typNode := types.At(i)
typ, err := parseType(ctx, typNode)
if err != nil {
return nil, err
}
pkg.Types[i] = *typ
pkg.RefMap[toApiType(typ.Name)] = typ.Name
}
// parse unions
unions := jsonRoot.Map("unions")
pkg.Unions = make([]Union, unions.Len())
for i := 0; i < unions.Len(); i++ {
unionNode := unions.At(i)
union, err := parseUnion(ctx, unionNode)
if err != nil {
return nil, err
}
pkg.Unions[i] = *union
pkg.RefMap[toApiType(union.Name)] = union.Name
}
// parse messages
messages := jsonRoot.Map("messages")
pkg.Messages = make([]Message, messages.Len())
for i := 0; i < messages.Len(); i++ {
msgNode := messages.At(i)
msg, err := parseMessage(ctx, msgNode)
if err != nil {
return nil, err
}
pkg.Messages[i] = *msg
}
// parse services
services := jsonRoot.Map("services")
if services.GetType() == jsongo.TypeMap {
pkg.Services = make([]Service, services.Len())
for i, key := range services.GetKeys() {
svcNode := services.At(key)
svc, err := parseService(ctx, key.(string), svcNode)
if err != nil {
return nil, err
}
pkg.Services[i] = *svc
}
// sort services
sort.Slice(pkg.Services, func(i, j int) bool {
// dumps first
if pkg.Services[i].Stream != pkg.Services[j].Stream {
return pkg.Services[i].Stream
}
return pkg.Services[i].RequestType < pkg.Services[j].RequestType
})
}
printPackage(&pkg)
return &pkg, nil
}
// printPackage prints all loaded objects for package
func printPackage(pkg *Package) {
if len(pkg.Enums) > 0 {
logf("loaded %d enums:", len(pkg.Enums))
for k, enum := range pkg.Enums {
logf(" - enum #%d\t%+v", k, enum)
}
}
if len(pkg.Unions) > 0 {
logf("loaded %d unions:", len(pkg.Unions))
for k, union := range pkg.Unions {
logf(" - union #%d\t%+v", k, union)
}
}
if len(pkg.Types) > 0 {
logf("loaded %d types:", len(pkg.Types))
for _, typ := range pkg.Types {
logf(" - type: %q (%d fields)", typ.Name, len(typ.Fields))
}
}
if len(pkg.Messages) > 0 {
logf("loaded %d messages:", len(pkg.Messages))
for _, msg := range pkg.Messages {
logf(" - message: %q (%d fields)", msg.Name, len(msg.Fields))
}
}
if len(pkg.Services) > 0 {
logf("loaded %d services:", len(pkg.Services))
for _, svc := range pkg.Services {
var info string
if svc.Stream {
info = "(STREAM)"
} else if len(svc.Events) > 0 {
info = fmt.Sprintf("(EVENTS: %v)", svc.Events)
}
logf(" - service: %q -> %q %s", svc.RequestType, svc.ReplyType, info)
}
}
}
// parseEnum parses VPP binary API enum object from JSON node
func parseEnum(ctx *context, enumNode *jsongo.JSONNode) (*Enum, error) {
if enumNode.Len() == 0 || enumNode.At(0).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for enum specified")
}
enumName, ok := enumNode.At(0).Get().(string)
if !ok {
return nil, fmt.Errorf("enum name is %T, not a string", enumNode.At(0).Get())
}
enumType, ok := enumNode.At(enumNode.Len() - 1).At("enumtype").Get().(string)
if !ok {
return nil, fmt.Errorf("enum type invalid or missing")
}
enum := Enum{
Name: enumName,
Type: enumType,
}
// loop through enum entries, skip first (name) and last (enumtype)
for j := 1; j < enumNode.Len()-1; j++ {
if enumNode.At(j).GetType() == jsongo.TypeArray {
entry := enumNode.At(j)
if entry.Len() < 2 || entry.At(0).GetType() != jsongo.TypeValue || entry.At(1).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for enum entry specified")
}
entryName, ok := entry.At(0).Get().(string)
if !ok {
return nil, fmt.Errorf("enum entry name is %T, not a string", entry.At(0).Get())
}
entryVal := entry.At(1).Get()
enum.Entries = append(enum.Entries, EnumEntry{
Name: entryName,
Value: entryVal,
})
}
}
return &enum, nil
}
// parseUnion parses VPP binary API union object from JSON node
func parseUnion(ctx *context, unionNode *jsongo.JSONNode) (*Union, error) {
if unionNode.Len() == 0 || unionNode.At(0).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for union specified")
}
unionName, ok := unionNode.At(0).Get().(string)
if !ok {
return nil, fmt.Errorf("union name is %T, not a string", unionNode.At(0).Get())
}
unionCRC, ok := unionNode.At(unionNode.Len() - 1).At("crc").Get().(string)
if !ok {
return nil, fmt.Errorf("union crc invalid or missing")
}
union := Union{
Name: unionName,
CRC: unionCRC,
}
// loop through union fields, skip first (name) and last (crc)
for j := 1; j < unionNode.Len()-1; j++ {
if unionNode.At(j).GetType() == jsongo.TypeArray {
fieldNode := unionNode.At(j)
field, err := parseField(ctx, fieldNode)
if err != nil {
return nil, err
}
union.Fields = append(union.Fields, *field)
}
}
return &union, nil
}
// parseType parses VPP binary API type object from JSON node
func parseType(ctx *context, typeNode *jsongo.JSONNode) (*Type, error) {
if typeNode.Len() == 0 || typeNode.At(0).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for type specified")
}
typeName, ok := typeNode.At(0).Get().(string)
if !ok {
return nil, fmt.Errorf("type name is %T, not a string", typeNode.At(0).Get())
}
typeCRC, ok := typeNode.At(typeNode.Len() - 1).At("crc").Get().(string)
if !ok {
return nil, fmt.Errorf("type crc invalid or missing")
}
typ := Type{
Name: typeName,
CRC: typeCRC,
}
// loop through type fields, skip first (name) and last (crc)
for j := 1; j < typeNode.Len()-1; j++ {
if typeNode.At(j).GetType() == jsongo.TypeArray {
fieldNode := typeNode.At(j)
field, err := parseField(ctx, fieldNode)
if err != nil {
return nil, err
}
typ.Fields = append(typ.Fields, *field)
}
}
return &typ, nil
}
// parseMessage parses VPP binary API message object from JSON node
func parseMessage(ctx *context, msgNode *jsongo.JSONNode) (*Message, error) {
if msgNode.Len() == 0 || msgNode.At(0).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for message specified")
}
msgName, ok := msgNode.At(0).Get().(string)
if !ok {
return nil, fmt.Errorf("message name is %T, not a string", msgNode.At(0).Get())
}
msgCRC, ok := msgNode.At(msgNode.Len() - 1).At("crc").Get().(string)
if !ok {
return nil, fmt.Errorf("message crc invalid or missing")
}
msg := Message{
Name: msgName,
CRC: msgCRC,
}
// loop through message fields, skip first (name) and last (crc)
for j := 1; j < msgNode.Len()-1; j++ {
if msgNode.At(j).GetType() == jsongo.TypeArray {
fieldNode := msgNode.At(j)
field, err := parseField(ctx, fieldNode)
if err != nil {
return nil, err
}
msg.Fields = append(msg.Fields, *field)
}
}
return &msg, nil
}
// parseField parses VPP binary API object field from JSON node
func parseField(ctx *context, field *jsongo.JSONNode) (*Field, error) {
if field.Len() < 2 || field.At(0).GetType() != jsongo.TypeValue || field.At(1).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for field specified")
}
fieldType, ok := field.At(0).Get().(string)
if !ok {
return nil, fmt.Errorf("field type is %T, not a string", field.At(0).Get())
}
fieldName, ok := field.At(1).Get().(string)
if !ok {
return nil, fmt.Errorf("field name is %T, not a string", field.At(1).Get())
}
var fieldLength float64
if field.Len() >= 3 {
fieldLength, ok = field.At(2).Get().(float64)
if !ok {
return nil, fmt.Errorf("field length is %T, not an int", field.At(2).Get())
}
}
var fieldLengthFrom string
if field.Len() >= 4 {
fieldLengthFrom, ok = field.At(3).Get().(string)
if !ok {
return nil, fmt.Errorf("field length from is %T, not a string", field.At(3).Get())
}
}
return &Field{
Name: fieldName,
Type: fieldType,
Length: int(fieldLength),
SizeFrom: fieldLengthFrom,
}, nil
}
// parseService parses VPP binary API service object from JSON node
func parseService(ctx *context, svcName string, svcNode *jsongo.JSONNode) (*Service, error) {
if svcNode.Len() == 0 || svcNode.At("reply").GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for service specified")
}
svc := Service{
Name: ctx.moduleName + "." + svcName,
RequestType: svcName,
}
if replyNode := svcNode.At("reply"); replyNode.GetType() == jsongo.TypeValue {
reply, ok := replyNode.Get().(string)
if !ok {
return nil, fmt.Errorf("service reply is %T, not a string", replyNode.Get())
}
if reply != "null" {
svc.ReplyType = reply
}
}
// stream service (dumps)
if streamNode := svcNode.At("stream"); streamNode.GetType() == jsongo.TypeValue {
var ok bool
svc.Stream, ok = streamNode.Get().(bool)
if !ok {
return nil, fmt.Errorf("service stream is %T, not a string", streamNode.Get())
}
}
// events service (event subscription)
if eventsNode := svcNode.At("events"); eventsNode.GetType() == jsongo.TypeArray {
for j := 0; j < eventsNode.Len(); j++ {
event := eventsNode.At(j).Get().(string)
svc.Events = append(svc.Events, event)
}
}
// validate service
if svc.IsEventService() {
if !strings.HasPrefix(svc.RequestType, "want_") {
log.Warnf("Unusual EVENTS SERVICE: %+v\n"+
"- events service %q does not have 'want_' prefix in request.",
svc, svc.Name)
}
} else if svc.IsDumpService() {
if !strings.HasSuffix(svc.RequestType, "_dump") ||
!strings.HasSuffix(svc.ReplyType, "_details") {
log.Warnf("Unusual STREAM SERVICE: %+v\n"+
"- stream service %q does not have '_dump' suffix in request or reply does not have '_details' suffix.",
svc, svc.Name)
}
} else if svc.IsRequestService() {
if !strings.HasSuffix(svc.ReplyType, "_reply") {
log.Warnf("Unusual REQUEST SERVICE: %+v\n"+
"- service %q does not have '_reply' suffix in reply.",
svc, svc.Name)
}
}
return &svc, nil
}
// convertToGoType translates the VPP binary API type into Go type
func convertToGoType(ctx *context, binapiType string) (typ string) {
if t, ok := binapiTypes[binapiType]; ok {
// basic types
typ = t
} else if r, ok := ctx.packageData.RefMap[binapiType]; ok {
// specific types (enums/types/unions)
typ = camelCaseName(r)
} else {
// fallback type
log.Warnf("found unknown VPP binary API type %q, using byte", binapiType)
typ = "byte"
}
return typ
}
|