From c60a4ee4e6114ff0dc3cbc9fd9a58321ca2a8abc Mon Sep 17 00:00:00 2001 From: Rastislav Szabo Date: Mon, 22 May 2017 11:24:42 +0200 Subject: fixed golint issues Change-Id: I325fa618d8db7a9f1783ec7d208fd7b6e853d9a3 Signed-off-by: Rastislav Szabo --- Makefile | 2 +- adapter/mock/binapi_reflect/binapi_reflect.go | 74 ---------------------- adapter/mock/mock_adapter.go | 91 ++++++++++++++------------- adapter/mock/util/binapi_reflect.go | 74 ++++++++++++++++++++++ 4 files changed, 124 insertions(+), 117 deletions(-) delete mode 100644 adapter/mock/binapi_reflect/binapi_reflect.go create mode 100644 adapter/mock/util/binapi_reflect.go diff --git a/Makefile b/Makefile index 859ed1e..a11ffa4 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,6 @@ generate: @cd examples && go generate ./... lint: - @golint ./... | grep -v vendor | grep -v bin_api + @golint ./... | grep -v vendor | grep -v bin_api || true .PHONY: build test install clean generate diff --git a/adapter/mock/binapi_reflect/binapi_reflect.go b/adapter/mock/binapi_reflect/binapi_reflect.go deleted file mode 100644 index f860150..0000000 --- a/adapter/mock/binapi_reflect/binapi_reflect.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (c) 2017 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 binapi_reflect is a helper package for generic handling of VPP binary API messages -// in the mock adapter and integration tests. -package binapi_reflect - -import ( - "reflect" -) - -const SwIfIndex = "SwIfIndex" -const Retval = "Retval" -const Reply = "_reply" - -// TODO comment -func FindFieldOfType(reply reflect.Type, fieldName string) (reflect.StructField, bool) { - if reply.Kind() == reflect.Struct { - field, found := reply.FieldByName(fieldName) - return field, found - } else if reply.Kind() == reflect.Ptr && reply.Elem().Kind() == reflect.Struct { - field, found := reply.Elem().FieldByName(fieldName) - return field, found - } - return reflect.StructField{}, false -} - -// TODO comment -func FindFieldOfValue(reply reflect.Value, fieldName string) (reflect.Value, bool) { - if reply.Kind() == reflect.Struct { - field := reply.FieldByName(fieldName) - return field, field.IsValid() - } else if reply.Kind() == reflect.Ptr && reply.Elem().Kind() == reflect.Struct { - field := reply.Elem().FieldByName(fieldName) - return field, field.IsValid() - } - return reflect.Value{}, false -} - -// TODO comment -func IsReplySwIfIdx(reply reflect.Type) bool { - _, found := FindFieldOfType(reply, SwIfIndex) - return found -} - -// TODO comment -func SetSwIfIdx(reply reflect.Value, swIfIndex uint32) { - if field, found := FindFieldOfValue(reply, SwIfIndex); found { - field.Set(reflect.ValueOf(swIfIndex)) - } -} - -// TODO comment -func SetRetVal(reply reflect.Value, retVal int32) { - if field, found := FindFieldOfValue(reply, Retval); found { - field.Set(reflect.ValueOf(retVal)) - } -} - -// TODO comment -func ReplyNameFor(request string) (string, bool) { - return request + Reply, true -} diff --git a/adapter/mock/mock_adapter.go b/adapter/mock/mock_adapter.go index 796b96e..1076ec2 100644 --- a/adapter/mock/mock_adapter.go +++ b/adapter/mock/mock_adapter.go @@ -25,7 +25,7 @@ import ( "github.com/lunixbochs/struc" "git.fd.io/govpp.git/adapter" - "git.fd.io/govpp.git/adapter/mock/binapi_reflect" + "git.fd.io/govpp.git/adapter/mock/util" "git.fd.io/govpp.git/api" ) @@ -34,9 +34,9 @@ type VppAdapter struct { callback func(context uint32, msgId uint16, data []byte) msgNameToIds *map[string]uint16 - msgIdsToName *map[uint16]string - msgIdSeq uint16 - binApiTypes map[string]reflect.Type + msgIDsToName *map[uint16]string + msgIDSeq uint16 + binAPITypes map[string]reflect.Type access sync.RWMutex } @@ -53,8 +53,8 @@ type replyHeader struct { Context uint32 } -// replyHeader represents a common header of each VPP reply message. -type vppOtherHeader struct { +// otherHeader represents a common header of each VPP reply message. +type otherHeader struct { VlMsgID uint16 } @@ -63,7 +63,7 @@ type defaultReply struct { Retval int32 } -// MessageDTO is a structure used for propageating informations to ReplyHandlers +// MessageDTO is a structure used for propagating information to ReplyHandlers. type MessageDTO struct { MsgID uint16 MsgName string @@ -72,18 +72,17 @@ type MessageDTO struct { } // ReplyHandler is a type that allows to extend the behaviour of VPP mock. -// Return value prepared is used to signalize that mock reply is calculated. -type ReplyHandler func(request MessageDTO) (reply []byte, msgID uint16, prepared bool) +// Return value ok is used to signalize that mock reply is calculated and ready to be used. +type ReplyHandler func(request MessageDTO) (reply []byte, msgID uint16, ok bool) const ( - //defaultMsgID = 1 // default message ID to be returned from GetMsgId - defaultReplyMsgID = 2 // default message ID for the reply to be sent back via callback + defaultReplyMsgID = 1 // default message ID for the reply to be sent back via callback ) var replies []api.Message // FIFO queue of messages var replyHandlers []ReplyHandler // callbacks that are able to calculate mock responses var repliesLock sync.Mutex // mutex for the queue -var mode = 0 +var mode = 0 // mode in which the mock operates const useRepliesQueue = 1 // use replies in the queue instead of the default one const useReplyHandlers = 2 //use ReplyHandler @@ -103,8 +102,9 @@ func (a *VppAdapter) Disconnect() { // no op } -func (a *VppAdapter) GetMsgNameByID(msgId uint16) (string, bool) { - switch msgId { +// GetMsgNameByID returns message name for specified message ID. +func (a *VppAdapter) GetMsgNameByID(msgID uint16) (string, bool) { + switch msgID { case 100: return "control_ping", true case 101: @@ -118,26 +118,28 @@ func (a *VppAdapter) GetMsgNameByID(msgId uint16) (string, bool) { a.access.Lock() defer a.access.Unlock() a.initMaps() - msgName, found := (*a.msgIdsToName)[msgId] + msgName, found := (*a.msgIDsToName)[msgID] return msgName, found } -func (a *VppAdapter) RegisterBinApiTypes(binApiTypes map[string]reflect.Type) { +// RegisterBinAPITypes registers binary API message types in the mock adapter. +func (a *VppAdapter) RegisterBinAPITypes(binAPITypes map[string]reflect.Type) { a.access.Lock() defer a.access.Unlock() a.initMaps() - for _, v := range binApiTypes { + for _, v := range binAPITypes { if msg, ok := reflect.New(v).Interface().(api.Message); ok { - a.binApiTypes[msg.GetMessageName()] = v + a.binAPITypes[msg.GetMessageName()] = v } } } +// ReplyTypeFor returns reply message type for given request message name. func (a *VppAdapter) ReplyTypeFor(requestMsgName string) (reflect.Type, uint16, bool) { - replyName, foundName := binapi_reflect.ReplyNameFor(requestMsgName) + replyName, foundName := util.ReplyNameFor(requestMsgName) if foundName { - if reply, found := a.binApiTypes[replyName]; found { + if reply, found := a.binAPITypes[replyName]; found { msgID, err := a.GetMsgID(replyName, "") if err == nil { return reply, msgID, found @@ -148,6 +150,7 @@ func (a *VppAdapter) ReplyTypeFor(requestMsgName string) (reflect.Type, uint16, return nil, 0, false } +// ReplyFor returns reply message for given request message name. func (a *VppAdapter) ReplyFor(requestMsgName string) (api.Message, uint16, bool) { replType, msgID, foundReplType := a.ReplyTypeFor(requestMsgName) if foundReplType { @@ -161,17 +164,18 @@ func (a *VppAdapter) ReplyFor(requestMsgName string) (api.Message, uint16, bool) return nil, 0, false } +// ReplyBytes encodes the mocked reply into binary format. func (a *VppAdapter) ReplyBytes(request MessageDTO, reply api.Message) ([]byte, error) { - replyMsgId, err := a.GetMsgID(reply.GetMessageName(), reply.GetCrcString()) + replyMsgID, err := a.GetMsgID(reply.GetMessageName(), reply.GetCrcString()) if err != nil { - log.Println("ReplyBytesE ", replyMsgId, " ", reply.GetMessageName(), " clientId: ", request.ClientID, + log.Println("ReplyBytesE ", replyMsgID, " ", reply.GetMessageName(), " clientId: ", request.ClientID, " ", err) return nil, err } - log.Println("ReplyBytes ", replyMsgId, " ", reply.GetMessageName(), " clientId: ", request.ClientID) + log.Println("ReplyBytes ", replyMsgID, " ", reply.GetMessageName(), " clientId: ", request.ClientID) buf := new(bytes.Buffer) - struc.Pack(buf, &replyHeader{VlMsgID: replyMsgId, Context: request.ClientID}) + struc.Pack(buf, &replyHeader{VlMsgID: replyMsgID, Context: request.ClientID}) struc.Pack(buf, reply) return buf.Bytes(), nil @@ -194,29 +198,31 @@ func (a *VppAdapter) GetMsgID(msgName string, msgCrc string) (uint16, error) { defer a.access.Unlock() a.initMaps() - if msgId, found := (*a.msgNameToIds)[msgName]; found { - return msgId, nil - } else { - a.msgIdSeq++ - msgId = a.msgIdSeq - (*a.msgNameToIds)[msgName] = msgId - (*a.msgIdsToName)[msgId] = msgName + msgID, found := (*a.msgNameToIds)[msgName] + if found { + return msgID, nil + } - log.Println("VPP GetMessageId ", msgId, " name:", msgName, " crc:", msgCrc) + a.msgIDSeq++ + msgID = a.msgIDSeq + (*a.msgNameToIds)[msgName] = msgID + (*a.msgIDsToName)[msgID] = msgName - return msgId, nil - } + log.Println("VPP GetMessageId ", msgID, " name:", msgName, " crc:", msgCrc) + + return msgID, nil } +// initMaps initializes internal maps (if not already initialized). func (a *VppAdapter) initMaps() { - if a.msgIdsToName == nil { - a.msgIdsToName = &map[uint16]string{} + if a.msgIDsToName == nil { + a.msgIDsToName = &map[uint16]string{} a.msgNameToIds = &map[string]uint16{} - a.msgIdSeq = 1000 + a.msgIDSeq = 1000 } - if a.binApiTypes == nil { - a.binApiTypes = map[string]reflect.Type{} + if a.binAPITypes == nil { + a.binAPITypes = map[string]reflect.Type{} } } @@ -233,7 +239,7 @@ func (a *VppAdapter) SendMsg(clientID uint32, data []byte) error { struc.Unpack(buf, &reqHeader) a.access.Lock() - reqMsgName, _ := (*a.msgIdsToName)[reqHeader.VlMsgID] + reqMsgName, _ := (*a.msgIDsToName)[reqHeader.VlMsgID] a.access.Unlock() reply, msgID, finished := replyHandler(MessageDTO{reqHeader.VlMsgID, reqMsgName, @@ -290,8 +296,7 @@ func (a *VppAdapter) SetMsgCallback(cb func(context uint32, msgID uint16, data [ // MockReply stores a message to be returned when the next request comes. It is a FIFO queue - multiple replies // can be pushed into it, the first one will be popped when some request comes. -// -// It is able to also receive callback that calculates the reply +// Using of this method automatically switches the mock into th useRepliesQueue mode. func (a *VppAdapter) MockReply(msg api.Message) { repliesLock.Lock() defer repliesLock.Unlock() @@ -300,6 +305,8 @@ func (a *VppAdapter) MockReply(msg api.Message) { mode = useRepliesQueue } +// MockReplyHandler registers a handler function that is supposed to generate mock responses to incoming requests. +// Using of this method automatically switches the mock into th useReplyHandlers mode. func (a *VppAdapter) MockReplyHandler(replyHandler ReplyHandler) { repliesLock.Lock() defer repliesLock.Unlock() diff --git a/adapter/mock/util/binapi_reflect.go b/adapter/mock/util/binapi_reflect.go new file mode 100644 index 0000000..0ab065d --- /dev/null +++ b/adapter/mock/util/binapi_reflect.go @@ -0,0 +1,74 @@ +// Copyright (c) 2017 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 util is a helper package for generic handling of VPP binary +// API messages in the mock adapter and integration tests. +package util + +import ( + "reflect" +) + +const swIfIndexName = "swIfIndex" +const retvalName = "retval" +const replySuffix = "_reply" + +// findFieldOfType finds the field specified by its name in provided message defined as reflect.Type data type. +func findFieldOfType(reply reflect.Type, fieldName string) (reflect.StructField, bool) { + if reply.Kind() == reflect.Struct { + field, found := reply.FieldByName(fieldName) + return field, found + } else if reply.Kind() == reflect.Ptr && reply.Elem().Kind() == reflect.Struct { + field, found := reply.Elem().FieldByName(fieldName) + return field, found + } + return reflect.StructField{}, false +} + +// findFieldOfValue finds the field specified by its name in provided message defined as reflect.Value data type. +func findFieldOfValue(reply reflect.Value, fieldName string) (reflect.Value, bool) { + if reply.Kind() == reflect.Struct { + field := reply.FieldByName(fieldName) + return field, field.IsValid() + } else if reply.Kind() == reflect.Ptr && reply.Elem().Kind() == reflect.Struct { + field := reply.Elem().FieldByName(fieldName) + return field, field.IsValid() + } + return reflect.Value{}, false +} + +// HasSwIfIdx checks whether provided message has the swIfIndex field. +func HasSwIfIdx(msg reflect.Type) bool { + _, found := findFieldOfType(msg, swIfIndexName) + return found +} + +// SetSwIfIdx sets the swIfIndex field of provided message to provided value. +func SetSwIfIdx(msg reflect.Value, swIfIndex uint32) { + if field, found := findFieldOfValue(msg, swIfIndexName); found { + field.Set(reflect.ValueOf(swIfIndex)) + } +} + +// SetRetval sets the retval field of provided message to provided value. +func SetRetval(msg reflect.Value, retVal int32) { + if field, found := findFieldOfValue(msg, retvalName); found { + field.Set(reflect.ValueOf(retVal)) + } +} + +// ReplyNameFor returns reply message name to the given request message name. +func ReplyNameFor(requestName string) (string, bool) { + return requestName + replySuffix, true +} -- cgit 1.2.3-korg