1 // Copyright (c) 2017 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.
15 // Package util is a helper package for generic handling of VPP binary
16 // API messages in the mock adapter and integration tests.
23 const swIfIndexName = "swIfIndex"
24 const retvalName = "retval"
25 const replySuffix = "_reply"
27 // findFieldOfType finds the field specified by its name in provided message defined as reflect.Type data type.
28 func findFieldOfType(reply reflect.Type, fieldName string) (reflect.StructField, bool) {
29 if reply.Kind() == reflect.Struct {
30 field, found := reply.FieldByName(fieldName)
32 } else if reply.Kind() == reflect.Ptr && reply.Elem().Kind() == reflect.Struct {
33 field, found := reply.Elem().FieldByName(fieldName)
36 return reflect.StructField{}, false
39 // findFieldOfValue finds the field specified by its name in provided message defined as reflect.Value data type.
40 func findFieldOfValue(reply reflect.Value, fieldName string) (reflect.Value, bool) {
41 if reply.Kind() == reflect.Struct {
42 field := reply.FieldByName(fieldName)
43 return field, field.IsValid()
44 } else if reply.Kind() == reflect.Ptr && reply.Elem().Kind() == reflect.Struct {
45 field := reply.Elem().FieldByName(fieldName)
46 return field, field.IsValid()
48 return reflect.Value{}, false
51 // HasSwIfIdx checks whether provided message has the swIfIndex field.
52 func HasSwIfIdx(msg reflect.Type) bool {
53 _, found := findFieldOfType(msg, swIfIndexName)
57 // SetSwIfIdx sets the swIfIndex field of provided message to provided value.
58 func SetSwIfIdx(msg reflect.Value, swIfIndex uint32) {
59 if field, found := findFieldOfValue(msg, swIfIndexName); found {
60 field.Set(reflect.ValueOf(swIfIndex))
64 // SetRetval sets the retval field of provided message to provided value.
65 func SetRetval(msg reflect.Value, retVal int32) {
66 if field, found := findFieldOfValue(msg, retvalName); found {
67 field.Set(reflect.ValueOf(retVal))
71 // ReplyNameFor returns reply message name to the given request message name.
72 func ReplyNameFor(requestName string) (string, bool) {
73 return requestName + replySuffix, true