aboutsummaryrefslogtreecommitdiffstats
path: root/binapigen/vppapi/parse_json.go
blob: 45b57960c021edebc5f27333a26aed7538aba551 (plain)
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
//  Copyright (c) 2020 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 vppapi

import (
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"strings"

	"github.com/bennyscetbun/jsongo"
	"github.com/sirupsen/logrus"
)

var Logger *logrus.Logger

func init() {
	if strings.Contains(os.Getenv("DEBUG_GOVPP"), "parser") {
		Logger = logrus.StandardLogger()
	}
}

func logf(f string, v ...interface{}) {
	if Logger != nil {
		Logger.Debugf(f, v...)
	}
}

const (
	// file
	objAPIVersion = "vl_api_version"
	objTypes      = "types"
	objMessages   = "messages"
	objUnions     = "unions"
	objEnums      = "enums"
	objServices   = "services"
	objAliases    = "aliases"
	objOptions    = "options"
	objImports    = "imports"

	// message
	messageFieldCrc = "crc"

	// alias
	aliasFieldLength = "length"
	aliasFieldType   = "type"

	// service
	serviceFieldReply     = "reply"
	serviceFieldStream    = "stream"
	serviceFieldStreamMsg = "stream_msg"
	serviceFieldEvents    = "events"
)

const (
	// file
	fileOptionVersion = "version"

	// field
	fieldOptionLimit   = "limit"
	fieldOptionDefault = "default"

	// service
	serviceReplyNull = "null"
)

func parseJSON(data []byte) (module *File, err error) {
	defer func() {
		if e := recover(); e != nil {
			err = fmt.Errorf("recovered panic: %v", e)
		}
	}()

	// parse JSON data into objects
	jsonRoot := new(jsongo.Node)
	if err := json.Unmarshal(data, jsonRoot); err != nil {
		return nil, fmt.Errorf("unmarshalling JSON failed: %v", err)
	}

	logf("file contents:")
	for _, key := range jsonRoot.GetKeys() {
		if jsonRoot.At(key).Len() > 0 {
			logf("  - %2d %s", jsonRoot.At(key).Len(), key)
		}
	}

	module = new(File)

	// parse CRC
	if crc := jsonRoot.At(objAPIVersion); crc.GetType() == jsongo.TypeValue {
		module.CRC = crc.Get().(string)
	}

	// parse options
	opt := jsonRoot.Map(objOptions)
	if opt.GetType() == jsongo.TypeMap {
		module.Options = make(map[string]string, 0)
		for _, key := range opt.GetKeys() {
			optionsNode := opt.At(key)
			optionKey := key.(string)
			optionValue := optionsNode.Get().(string)
			module.Options[optionKey] = optionValue
		}
	}

	// parse imports
	imports := jsonRoot.Map(objImports)
	module.Imports = make([]string, 0)
	imported := make(map[string]struct{})
	for i := 0; i < imports.Len(); i++ {
		importNode := imports.At(i)
		imp, err := parseImport(importNode)
		if err != nil {
			return nil, err
		}
		if _, ok := imported[*imp]; ok {
			logf("duplicate import found: %v", *imp)
			continue
		}
		imported[*imp] = struct{}{}
		module.Imports = append(module.Imports, *imp)
	}

	// avoid duplicate objects
	known := make(map[string]struct{})
	exists := func(name string) bool {
		if _, ok := known[name]; ok {
			logf("duplicate object found: %v", name)
			return true
		}
		known[name] = struct{}{}
		return false
	}

	// parse enum types
	enumsNode := jsonRoot.Map(objEnums)
	module.EnumTypes = make([]EnumType, 0)
	for i := 0; i < enumsNode.Len(); i++ {
		enumNode := enumsNode.At(i)
		enum, err := parseEnum(enumNode)
		if err != nil {
			return nil, err
		}
		if exists(enum.Name) {
			continue
		}
		module.EnumTypes = append(module.EnumTypes, *enum)
	}

	// parse alias types
	aliasesNode := jsonRoot.Map(objAliases)
	if aliasesNode.GetType() == jsongo.TypeMap {
		module.AliasTypes = make([]AliasType, 0)
		for _, key := range aliasesNode.GetKeys() {
			aliasNode := aliasesNode.At(key)
			aliasName := key.(string)
			alias, err := parseAlias(aliasName, aliasNode)
			if err != nil {
				return nil, err
			}
			if exists(alias.Name) {
				continue
			}
			module.AliasTypes = append(module.AliasTypes, *alias)
		}
	}

	// parse struct types
	typesNode := jsonRoot.Map(objTypes)
	module.StructTypes = make([]StructType, 0)
	for i := 0; i < typesNode.Len(); i++ {
		typNode := typesNode.At(i)
		structyp, err := parseStruct(typNode)
		if err != nil {
			return nil, err
		}
		if exists(structyp.Name) {
			continue
		}
		module.StructTypes = append(module.StructTypes, *structyp)
	}

	// parse union types
	unionsNode := jsonRoot.Map(objUnions)
	module.UnionTypes = make([]UnionType, 0)
	for i := 0; i < unionsNode.Len(); i++ {
		unionNode := unionsNode.At(i)
		union, err := parseUnion(unionNode)
		if err != nil {
			return nil, err
		}
		if exists(union.Name) {
			continue
		}
		module.UnionTypes = append(module.UnionTypes, *union)
	}

	// parse messages
	messagesNode := jsonRoot.Map(objMessages)
	if messagesNode.GetType() == jsongo.TypeArray {
		module.Messages = make([]Message, messagesNode.Len())
		for i := 0; i < messagesNode.Len(); i++ {
			msgNode := messagesNode.At(i)
			msg, err := parseMessage(msgNode)
			if err != nil {
				return nil, err
			}
			module.Messages[i] = *msg
		}
	}

	// parse services
	servicesNode := jsonRoot.Map(objServices)
	if servicesNode.GetType() == jsongo.TypeMap {
		module.Service = &Service{
			RPCs: make([]RPC, servicesNode.Len()),
		}
		for i, key := range servicesNode.GetKeys() {
			rpcNode := servicesNode.At(key)
			rpcName := key.(string)
			svc, err := parseServiceRPC(rpcName, rpcNode)
			if err != nil {
				return nil, err
			}
			module.Service.RPCs[i] = *svc
		}
	}

	return module, nil
}

// parseImport parses VPP binary API import from JSON node
func parseImport(importNode *jsongo.Node) (*string, error) {
	if importNode.GetType() != jsongo.TypeValue {
		return nil, errors.New("invalid JSON for import specified")
	}

	importName, ok := importNode.Get().(string)
	if !ok {
		return nil, fmt.Errorf("import name is %T, not a string", importNode.Get())
	}

	return &importName, nil
}

// parseEnum parses VPP binary API enum object from JSON node
func parseEnum(enumNode *jsongo.Node) (*EnumType, 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 := EnumType{
		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().(float64)

			enum.Entries = append(enum.Entries, EnumEntry{
				Name:  entryName,
				Value: uint32(entryVal),
			})
		}
	}

	return &enum, nil
}

// parseUnion parses VPP binary API union object from JSON node
func parseUnion(unionNode *jsongo.Node) (*UnionType, 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())
	}

	union := UnionType{
		Name: unionName,
	}

	// loop through union fields, skip first (name)
	for j := 1; j < unionNode.Len(); j++ {
		if unionNode.At(j).GetType() == jsongo.TypeArray {
			fieldNode := unionNode.At(j)

			field, err := parseField(fieldNode)
			if err != nil {
				return nil, err
			}

			union.Fields = append(union.Fields, *field)
		}
	}

	return &union, nil
}

// parseStruct parses VPP binary API type object from JSON node
func parseStruct(typeNode *jsongo.Node) (*StructType, 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())
	}

	typ := StructType{
		Name: typeName,
	}

	// loop through type fields, skip first (name)
	for j := 1; j < typeNode.Len(); j++ {
		if typeNode.At(j).GetType() == jsongo.TypeArray {
			fieldNode := typeNode.At(j)

			field, err := parseField(fieldNode)
			if err != nil {
				return nil, err
			}

			typ.Fields = append(typ.Fields, *field)
		}
	}

	return &typ, nil
}

// parseAlias parses VPP binary API alias object from JSON node
func parseAlias(aliasName string, aliasNode *jsongo.Node) (*AliasType, error) {
	if aliasNode.Len() == 0 || aliasNode.At(aliasFieldType).GetType() != jsongo.TypeValue {
		return nil, errors.New("invalid JSON for alias specified")
	}

	alias := AliasType{
		Name: aliasName,
	}

	if typeNode := aliasNode.At(aliasFieldType); typeNode.GetType() == jsongo.TypeValue {
		typ, ok := typeNode.Get().(string)
		if !ok {
			return nil, fmt.Errorf("alias type is %T, not a string", typeNode.Get())
		}
		if typ != "null" {
			alias.Type = typ
		}
	}

	if lengthNode := aliasNode.At(aliasFieldLength); lengthNode.GetType() == jsongo.TypeValue {
		length, ok := lengthNode.Get().(float64)
		if !ok {
			return nil, fmt.Errorf("alias length is %T, not a float64", lengthNode.Get())
		}
		alias.Length = int(length)
	}

	return &alias, nil
}

// parseMessage parses VPP binary API message object from JSON node
func parseMessage(msgNode *jsongo.Node) (*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(messageFieldCrc).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(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(field *jsongo.Node) (*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())
	}

	f := &Field{
		Name: fieldName,
		Type: fieldType,
	}

	if field.Len() >= 3 {
		switch field.At(2).GetType() {
		case jsongo.TypeValue:
			fieldLength, ok := field.At(2).Get().(float64)
			if !ok {
				return nil, fmt.Errorf("field length is %T, not float64", field.At(2).Get())
			}
			f.Length = int(fieldLength)
			f.Array = true

		case jsongo.TypeMap:
			fieldMeta := field.At(2)

			for _, key := range fieldMeta.GetKeys() {
				metaNode := fieldMeta.At(key)
				metaName := key.(string)
				metaValue := metaNode.Get()

				switch metaName {
				case fieldOptionLimit:
					metaValue = int(metaNode.Get().(float64))
				case fieldOptionDefault:
					metaValue = metaNode.Get()
				default:
					logrus.Warnf("unknown meta info (%s=%v) for field (%s)", metaName, metaValue, fieldName)
				}

				if f.Meta == nil {
					f.Meta = map[string]interface{}{}
				}
				f.Meta[metaName] = metaValue
			}
		default:
			return nil, errors.New("invalid JSON for field specified")
		}
	}
	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())
		}
		f.SizeFrom = fieldLengthFrom
	}

	return f, nil
}

// parseServiceRPC parses VPP binary API service object from JSON node
func parseServiceRPC(rpcName string, rpcNode *jsongo.Node) (*RPC, error) {
	if rpcNode.Len() == 0 || rpcNode.At(serviceFieldReply).GetType() != jsongo.TypeValue {
		return nil, errors.New("invalid JSON for service RPC specified")
	}

	rpc := RPC{
		Name:       rpcName,
		RequestMsg: rpcName,
	}

	if replyNode := rpcNode.At(serviceFieldReply); replyNode.GetType() == jsongo.TypeValue {
		reply, ok := replyNode.Get().(string)
		if !ok {
			return nil, fmt.Errorf("service RPC reply is %T, not a string", replyNode.Get())
		}
		if reply != serviceReplyNull {
			rpc.ReplyMsg = reply
		}
	}

	// is stream (dump)
	if streamNode := rpcNode.At(serviceFieldStream); streamNode.GetType() == jsongo.TypeValue {
		var ok bool
		rpc.Stream, ok = streamNode.Get().(bool)
		if !ok {
			return nil, fmt.Errorf("service RPC stream is %T, not a boolean", streamNode.Get())
		}
	}

	// stream message
	if streamMsgNode := rpcNode.At(serviceFieldStreamMsg); streamMsgNode.GetType() == jsongo.TypeValue {
		var ok bool
		rpc.StreamMsg, ok = streamMsgNode.Get().(string)
		if !ok {
			return nil, fmt.Errorf("service RPC stream msg is %T, not a string", streamMsgNode.Get())
		}
	}

	// events service (event subscription)
	if eventsNode := rpcNode.At(serviceFieldEvents); eventsNode.GetType() == jsongo.TypeArray {
		for j := 0; j < eventsNode.Len(); j++ {
			event := eventsNode.At(j).Get().(string)
			rpc.Events = append(rpc.Events, event)
		}
	}

	return &rpc, nil
}