1 // Copyright (c) 2018 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
23 "github.com/bennyscetbun/jsongo"
26 // toApiType returns name that is used as type reference in VPP binary API
27 func toApiType(name string) string {
28 return fmt.Sprintf("vl_api_%s_t", name)
31 // parsePackage parses provided JSON data into objects prepared for code generation
32 func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
33 logf(" %s contains: %d services, %d messages, %d types, %d enums, %d unions, %d aliases (version: %s)",
35 jsonRoot.Map("services").Len(),
36 jsonRoot.Map("messages").Len(),
37 jsonRoot.Map("types").Len(),
38 jsonRoot.Map("enums").Len(),
39 jsonRoot.Map("unions").Len(),
40 jsonRoot.Map("aliases").Len(),
41 jsonRoot.Map("vl_api_version").Get(),
45 APIVersion: jsonRoot.Map("vl_api_version").Get().(string),
46 RefMap: make(map[string]string),
50 enums := jsonRoot.Map("enums")
51 pkg.Enums = make([]Enum, enums.Len())
52 for i := 0; i < enums.Len(); i++ {
53 enumNode := enums.At(i)
55 enum, err := parseEnum(ctx, enumNode)
60 pkg.RefMap[toApiType(enum.Name)] = enum.Name
64 aliases := jsonRoot.Map("aliases")
65 if aliases.GetType() == jsongo.TypeMap {
66 pkg.Aliases = make([]Alias, aliases.Len())
67 for i, key := range aliases.GetKeys() {
68 aliasNode := aliases.At(key)
70 alias, err := parseAlias(ctx, key.(string), aliasNode)
74 pkg.Aliases[i] = *alias
75 pkg.RefMap[toApiType(alias.Name)] = alias.Name
80 types := jsonRoot.Map("types")
81 pkg.Types = make([]Type, types.Len())
82 for i := 0; i < types.Len(); i++ {
83 typNode := types.At(i)
85 typ, err := parseType(ctx, typNode)
90 pkg.RefMap[toApiType(typ.Name)] = typ.Name
94 unions := jsonRoot.Map("unions")
95 pkg.Unions = make([]Union, unions.Len())
96 for i := 0; i < unions.Len(); i++ {
97 unionNode := unions.At(i)
99 union, err := parseUnion(ctx, unionNode)
103 pkg.Unions[i] = *union
104 pkg.RefMap[toApiType(union.Name)] = union.Name
108 messages := jsonRoot.Map("messages")
109 pkg.Messages = make([]Message, messages.Len())
110 for i := 0; i < messages.Len(); i++ {
111 msgNode := messages.At(i)
113 msg, err := parseMessage(ctx, msgNode)
117 pkg.Messages[i] = *msg
121 services := jsonRoot.Map("services")
122 if services.GetType() == jsongo.TypeMap {
123 pkg.Services = make([]Service, services.Len())
124 for i, key := range services.GetKeys() {
125 svcNode := services.At(key)
127 svc, err := parseService(ctx, key.(string), svcNode)
131 pkg.Services[i] = *svc
135 sort.Slice(pkg.Services, func(i, j int) bool {
137 if pkg.Services[i].Stream != pkg.Services[j].Stream {
138 return pkg.Services[i].Stream
140 return pkg.Services[i].RequestType < pkg.Services[j].RequestType
149 // printPackage prints all loaded objects for package
150 func printPackage(pkg *Package) {
151 if len(pkg.Enums) > 0 {
152 logf("loaded %d enums:", len(pkg.Enums))
153 for k, enum := range pkg.Enums {
154 logf(" - enum #%d\t%+v", k, enum)
157 if len(pkg.Unions) > 0 {
158 logf("loaded %d unions:", len(pkg.Unions))
159 for k, union := range pkg.Unions {
160 logf(" - union #%d\t%+v", k, union)
163 if len(pkg.Types) > 0 {
164 logf("loaded %d types:", len(pkg.Types))
165 for _, typ := range pkg.Types {
166 logf(" - type: %q (%d fields)", typ.Name, len(typ.Fields))
169 if len(pkg.Messages) > 0 {
170 logf("loaded %d messages:", len(pkg.Messages))
171 for _, msg := range pkg.Messages {
172 logf(" - message: %q (%d fields)", msg.Name, len(msg.Fields))
175 if len(pkg.Services) > 0 {
176 logf("loaded %d services:", len(pkg.Services))
177 for _, svc := range pkg.Services {
181 } else if len(svc.Events) > 0 {
182 info = fmt.Sprintf("(EVENTS: %v)", svc.Events)
184 logf(" - service: %q -> %q %s", svc.RequestType, svc.ReplyType, info)
189 // parseEnum parses VPP binary API enum object from JSON node
190 func parseEnum(ctx *context, enumNode *jsongo.JSONNode) (*Enum, error) {
191 if enumNode.Len() == 0 || enumNode.At(0).GetType() != jsongo.TypeValue {
192 return nil, errors.New("invalid JSON for enum specified")
195 enumName, ok := enumNode.At(0).Get().(string)
197 return nil, fmt.Errorf("enum name is %T, not a string", enumNode.At(0).Get())
199 enumType, ok := enumNode.At(enumNode.Len() - 1).At("enumtype").Get().(string)
201 return nil, fmt.Errorf("enum type invalid or missing")
209 // loop through enum entries, skip first (name) and last (enumtype)
210 for j := 1; j < enumNode.Len()-1; j++ {
211 if enumNode.At(j).GetType() == jsongo.TypeArray {
212 entry := enumNode.At(j)
214 if entry.Len() < 2 || entry.At(0).GetType() != jsongo.TypeValue || entry.At(1).GetType() != jsongo.TypeValue {
215 return nil, errors.New("invalid JSON for enum entry specified")
218 entryName, ok := entry.At(0).Get().(string)
220 return nil, fmt.Errorf("enum entry name is %T, not a string", entry.At(0).Get())
222 entryVal := entry.At(1).Get()
224 enum.Entries = append(enum.Entries, EnumEntry{
234 // parseUnion parses VPP binary API union object from JSON node
235 func parseUnion(ctx *context, unionNode *jsongo.JSONNode) (*Union, error) {
236 if unionNode.Len() == 0 || unionNode.At(0).GetType() != jsongo.TypeValue {
237 return nil, errors.New("invalid JSON for union specified")
240 unionName, ok := unionNode.At(0).Get().(string)
242 return nil, fmt.Errorf("union name is %T, not a string", unionNode.At(0).Get())
244 unionCRC, ok := unionNode.At(unionNode.Len() - 1).At("crc").Get().(string)
246 return nil, fmt.Errorf("union crc invalid or missing")
254 // loop through union fields, skip first (name) and last (crc)
255 for j := 1; j < unionNode.Len()-1; j++ {
256 if unionNode.At(j).GetType() == jsongo.TypeArray {
257 fieldNode := unionNode.At(j)
259 field, err := parseField(ctx, fieldNode)
264 union.Fields = append(union.Fields, *field)
271 // parseType parses VPP binary API type object from JSON node
272 func parseType(ctx *context, typeNode *jsongo.JSONNode) (*Type, error) {
273 if typeNode.Len() == 0 || typeNode.At(0).GetType() != jsongo.TypeValue {
274 return nil, errors.New("invalid JSON for type specified")
277 typeName, ok := typeNode.At(0).Get().(string)
279 return nil, fmt.Errorf("type name is %T, not a string", typeNode.At(0).Get())
281 typeCRC, ok := typeNode.At(typeNode.Len() - 1).At("crc").Get().(string)
283 return nil, fmt.Errorf("type crc invalid or missing")
291 // loop through type fields, skip first (name) and last (crc)
292 for j := 1; j < typeNode.Len()-1; j++ {
293 if typeNode.At(j).GetType() == jsongo.TypeArray {
294 fieldNode := typeNode.At(j)
296 field, err := parseField(ctx, fieldNode)
301 typ.Fields = append(typ.Fields, *field)
309 aliasesLength = "length"
313 // parseAlias parses VPP binary API alias object from JSON node
314 func parseAlias(ctx *context, aliasName string, aliasNode *jsongo.JSONNode) (*Alias, error) {
315 if aliasNode.Len() == 0 || aliasNode.At(aliasesType).GetType() != jsongo.TypeValue {
316 return nil, errors.New("invalid JSON for alias specified")
323 if typeNode := aliasNode.At(aliasesType); typeNode.GetType() == jsongo.TypeValue {
324 typ, ok := typeNode.Get().(string)
326 return nil, fmt.Errorf("alias type is %T, not a string", typeNode.Get())
333 if lengthNode := aliasNode.At(aliasesLength); lengthNode.GetType() == jsongo.TypeValue {
334 length, ok := lengthNode.Get().(float64)
336 return nil, fmt.Errorf("alias length is %T, not a float64", lengthNode.Get())
338 alias.Length = int(length)
344 // parseMessage parses VPP binary API message object from JSON node
345 func parseMessage(ctx *context, msgNode *jsongo.JSONNode) (*Message, error) {
346 if msgNode.Len() == 0 || msgNode.At(0).GetType() != jsongo.TypeValue {
347 return nil, errors.New("invalid JSON for message specified")
350 msgName, ok := msgNode.At(0).Get().(string)
352 return nil, fmt.Errorf("message name is %T, not a string", msgNode.At(0).Get())
354 msgCRC, ok := msgNode.At(msgNode.Len() - 1).At("crc").Get().(string)
357 return nil, fmt.Errorf("message crc invalid or missing")
365 // loop through message fields, skip first (name) and last (crc)
366 for j := 1; j < msgNode.Len()-1; j++ {
367 if msgNode.At(j).GetType() == jsongo.TypeArray {
368 fieldNode := msgNode.At(j)
370 field, err := parseField(ctx, fieldNode)
375 msg.Fields = append(msg.Fields, *field)
382 // parseField parses VPP binary API object field from JSON node
383 func parseField(ctx *context, field *jsongo.JSONNode) (*Field, error) {
384 if field.Len() < 2 || field.At(0).GetType() != jsongo.TypeValue || field.At(1).GetType() != jsongo.TypeValue {
385 return nil, errors.New("invalid JSON for field specified")
388 fieldType, ok := field.At(0).Get().(string)
390 return nil, fmt.Errorf("field type is %T, not a string", field.At(0).Get())
392 fieldName, ok := field.At(1).Get().(string)
394 return nil, fmt.Errorf("field name is %T, not a string", field.At(1).Get())
396 var fieldLength float64
397 if field.Len() >= 3 {
398 fieldLength, ok = field.At(2).Get().(float64)
400 return nil, fmt.Errorf("field length is %T, not float64", field.At(2).Get())
403 var fieldLengthFrom string
404 if field.Len() >= 4 {
405 fieldLengthFrom, ok = field.At(3).Get().(string)
407 return nil, fmt.Errorf("field length from is %T, not a string", field.At(3).Get())
414 Length: int(fieldLength),
415 SizeFrom: fieldLengthFrom,
419 // parseService parses VPP binary API service object from JSON node
420 func parseService(ctx *context, svcName string, svcNode *jsongo.JSONNode) (*Service, error) {
421 if svcNode.Len() == 0 || svcNode.At("reply").GetType() != jsongo.TypeValue {
422 return nil, errors.New("invalid JSON for service specified")
426 Name: ctx.moduleName + "." + svcName,
427 RequestType: svcName,
430 if replyNode := svcNode.At("reply"); replyNode.GetType() == jsongo.TypeValue {
431 reply, ok := replyNode.Get().(string)
433 return nil, fmt.Errorf("service reply is %T, not a string", replyNode.Get())
436 svc.ReplyType = reply
440 // stream service (dumps)
441 if streamNode := svcNode.At("stream"); streamNode.GetType() == jsongo.TypeValue {
443 svc.Stream, ok = streamNode.Get().(bool)
445 return nil, fmt.Errorf("service stream is %T, not a string", streamNode.Get())
449 // events service (event subscription)
450 if eventsNode := svcNode.At("events"); eventsNode.GetType() == jsongo.TypeArray {
451 for j := 0; j < eventsNode.Len(); j++ {
452 event := eventsNode.At(j).Get().(string)
453 svc.Events = append(svc.Events, event)
458 if svc.IsEventService() {
459 if !strings.HasPrefix(svc.RequestType, "want_") {
460 log.Warnf("Unusual EVENTS SERVICE: %+v\n"+
461 "- events service %q does not have 'want_' prefix in request.",
464 } else if svc.IsDumpService() {
465 if !strings.HasSuffix(svc.RequestType, "_dump") ||
466 !strings.HasSuffix(svc.ReplyType, "_details") {
467 log.Warnf("Unusual STREAM SERVICE: %+v\n"+
468 "- stream service %q does not have '_dump' suffix in request or reply does not have '_details' suffix.",
471 } else if svc.IsRequestService() {
472 if !strings.HasSuffix(svc.ReplyType, "_reply") {
473 log.Warnf("Unusual REQUEST SERVICE: %+v\n"+
474 "- service %q does not have '_reply' suffix in reply.",
482 // convertToGoType translates the VPP binary API type into Go type
483 func convertToGoType(ctx *context, binapiType string) (typ string) {
484 if t, ok := binapiTypes[binapiType]; ok {
487 } else if r, ok := ctx.packageData.RefMap[binapiType]; ok {
488 // specific types (enums/types/unions)
489 typ = camelCaseName(r)
492 log.Warnf("found unknown VPP binary API type %q, using byte", binapiType)
498 func getSizeOfType(typ *Type) (size int) {
499 for _, field := range typ.Fields {
500 size += getSizeOfBinapiTypeLength(field.Type, field.Length)
505 func getSizeOfBinapiTypeLength(typ string, length int) (size int) {
506 if n := getBinapiTypeSize(typ); n > 0 {
516 func getTypeByRef(ctx *context, ref string) *Type {
517 for _, typ := range ctx.packageData.Types {
518 if ref == toApiType(typ.Name) {
525 func getAliasByRef(ctx *context, ref string) *Alias {
526 for _, alias := range ctx.packageData.Aliases {
527 if ref == toApiType(alias.Name) {
534 func getUnionSize(ctx *context, union *Union) (maxSize int) {
535 for _, field := range union.Fields {
536 typ := getTypeByRef(ctx, field.Type)
538 if size := getSizeOfType(typ); size > maxSize {
543 alias := getAliasByRef(ctx, field.Type)
545 if size := getSizeOfBinapiTypeLength(alias.Type, alias.Length); size > maxSize {
550 logf("no type or alias found for union %s field type %q", union.Name, field.Type)