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 mock is an alternative VPP adapter aimed for unit/integration testing where the
16 // actual communication with VPP is not demanded.
25 "github.com/lunixbochs/struc"
27 "gerrit.fd.io/r/govpp/adapter"
28 "gerrit.fd.io/r/govpp/adapter/mock/binapi_reflect"
29 "gerrit.fd.io/r/govpp/api"
32 // VppAdapter represents a mock VPP adapter that can be used for unit/integration testing instead of the vppapiclient adapter.
33 type VppAdapter struct {
34 callback func(context uint32, msgId uint16, data []byte)
36 msgNameToIds *map[string]uint16
37 msgIdsToName *map[uint16]string
39 binApiTypes map[string]reflect.Type
43 // replyHeader represents a common header of each VPP request message.
44 type requestHeader struct {
50 // replyHeader represents a common header of each VPP reply message.
51 type replyHeader struct {
56 // replyHeader represents a common header of each VPP reply message.
57 type vppOtherHeader struct {
61 // defaultReply is a default reply message that mock adapter returns for a request.
62 type defaultReply struct {
66 // MessageDTO is a structure used for propageating informations to ReplyHandlers
67 type MessageDTO struct {
74 // ReplyHandler is a type that allows to extend the behaviour of VPP mock.
75 // Return value prepared is used to signalize that mock reply is calculated.
76 type ReplyHandler func(request MessageDTO) (reply []byte, msgID uint16, prepared bool)
79 //defaultMsgID = 1 // default message ID to be returned from GetMsgId
80 defaultReplyMsgID = 2 // default message ID for the reply to be sent back via callback
83 var replies []api.Message // FIFO queue of messages
84 var replyHandlers []ReplyHandler // callbacks that are able to calculate mock responses
85 var repliesLock sync.Mutex // mutex for the queue
88 const useRepliesQueue = 1 // use replies in the queue instead of the default one
89 const useReplyHandlers = 2 //use ReplyHandler
91 // NewVppAdapter returns a new mock adapter.
92 func NewVppAdapter() adapter.VppAdapter {
96 // Connect emulates connecting the process to VPP.
97 func (a *VppAdapter) Connect() error {
101 // Disconnect emulates disconnecting the process from VPP.
102 func (a *VppAdapter) Disconnect() {
106 func (a *VppAdapter) GetMsgNameByID(msgId uint16) (string, bool) {
111 return "control_ping", true
113 return "control_ping_reply", true
115 return "sw_interface_dump", true
117 return "sw_interface_details", true
120 msgName, found := (*a.msgIdsToName)[msgId]
122 return msgName, found
125 func (a *VppAdapter) RegisterBinApiTypes(binApiTypes map[string]reflect.Type) {
127 for _, v := range binApiTypes {
128 if msg, ok := reflect.New(v).Interface().(api.Message); ok {
129 a.binApiTypes[msg.GetMessageName()] = v
134 func (a *VppAdapter) ReplyTypeFor(requestMsgName string) (reflect.Type, uint16, bool) {
135 replyName, foundName := binapi_reflect.ReplyNameFor(requestMsgName)
137 if reply, found := a.binApiTypes[replyName]; found {
138 msgID, err := a.GetMsgID(replyName, "")
140 return reply, msgID, found
148 func (a *VppAdapter) ReplyFor(requestMsgName string) (api.Message, uint16, bool) {
149 replType, msgID, foundReplType := a.ReplyTypeFor(requestMsgName)
151 msgVal := reflect.New(replType)
152 if msg, ok := msgVal.Interface().(api.Message); ok {
153 log.Println("FFF ", replType, msgID, foundReplType)
154 return msg, msgID, true
161 func (a *VppAdapter) ReplyBytes(request MessageDTO, reply api.Message) ([]byte, error) {
162 replyMsgId, err := a.GetMsgID(reply.GetMessageName(), reply.GetCrcString())
164 log.Println("ReplyBytesE ", replyMsgId, " ", reply.GetMessageName(), " clientId: ", request.ClientID,
168 log.Println("ReplyBytes ", replyMsgId, " ", reply.GetMessageName(), " clientId: ", request.ClientID)
170 buf := new(bytes.Buffer)
171 struc.Pack(buf, &replyHeader{VlMsgID: replyMsgId, Context: request.ClientID})
172 struc.Pack(buf, reply)
174 return buf.Bytes(), nil
177 // GetMsgID returns mocked message ID for the given message name and CRC.
178 func (a *VppAdapter) GetMsgID(msgName string, msgCrc string) (uint16, error) {
182 case "control_ping_reply":
184 case "sw_interface_dump":
186 case "sw_interface_details":
192 if msgId, found := (*a.msgNameToIds)[msgName]; found {
197 (*a.msgNameToIds)[msgName] = msgId
198 (*a.msgIdsToName)[msgId] = msgName
200 log.Println("VPP GetMessageId ", msgId, " name:", msgName, " crc:", msgCrc)
206 func (a *VppAdapter) initMaps() {
207 if a.msgIdsToName == nil {
208 a.msgIdsToName = &map[uint16]string{}
209 a.msgNameToIds = &map[string]uint16{}
213 if a.binApiTypes == nil {
214 a.binApiTypes = map[string]reflect.Type{}
218 // SendMsg emulates sending a binary-encoded message to VPP.
219 func (a *VppAdapter) SendMsg(clientID uint32, data []byte) error {
221 case useReplyHandlers:
222 for i := len(replyHandlers) - 1; i >= 0; i-- {
223 replyHandler := replyHandlers[i]
225 buf := bytes.NewReader(data)
226 reqHeader := requestHeader{}
227 struc.Unpack(buf, &reqHeader)
230 reqMsgName, _ := (*a.msgIdsToName)[reqHeader.VlMsgID]
232 reply, msgID, finished := replyHandler(MessageDTO{reqHeader.VlMsgID, reqMsgName,
235 a.callback(clientID, msgID, reply)
240 case useRepliesQueue:
242 defer repliesLock.Unlock()
244 // pop all replies from queue
245 for i, reply := range replies {
246 if i > 0 && reply.GetMessageName() == "control_ping_reply" {
247 // hack - do not send control_ping_reply immediately, leave it for the the next callback
248 replies = []api.Message{}
249 replies = append(replies, reply)
252 msgID, _ := a.GetMsgID(reply.GetMessageName(), reply.GetCrcString())
253 buf := new(bytes.Buffer)
254 if reply.GetMessageType() == api.ReplyMessage {
255 struc.Pack(buf, &replyHeader{VlMsgID: msgID, Context: clientID})
257 struc.Pack(buf, &requestHeader{VlMsgID: msgID, Context: clientID})
259 struc.Pack(buf, reply)
260 a.callback(clientID, msgID, buf.Bytes())
262 if len(replies) > 0 {
263 replies = []api.Message{}
269 // return default reply
270 buf := new(bytes.Buffer)
271 msgID := uint16(defaultReplyMsgID)
272 struc.Pack(buf, &replyHeader{VlMsgID: msgID, Context: clientID})
273 struc.Pack(buf, &defaultReply{})
274 a.callback(clientID, msgID, buf.Bytes())
279 // SetMsgCallback sets a callback function that will be called by the adapter whenever a message comes from the mock.
280 func (a *VppAdapter) SetMsgCallback(cb func(context uint32, msgID uint16, data []byte)) {
284 // MockReply stores a message to be returned when the next request comes. It is a FIFO queue - multiple replies
285 // can be pushed into it, the first one will be popped when some request comes.
287 // It is able to also receive callback that calculates the reply
288 func (a *VppAdapter) MockReply(msg api.Message) {
290 defer repliesLock.Unlock()
292 replies = append(replies, msg)
293 mode = useRepliesQueue
296 func (a *VppAdapter) MockReplyHandler(replyHandler ReplyHandler) {
298 defer repliesLock.Unlock()
300 replyHandlers = append(replyHandlers, replyHandler)
301 mode = useReplyHandlers