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 "git.fd.io/govpp.git/adapter"
26 "git.fd.io/govpp.git/adapter/mock/binapi"
27 "git.fd.io/govpp.git/api"
28 "git.fd.io/govpp.git/codec"
35 useRepliesQueue // use replies in the queue
36 useReplyHandlers // use reply handler
39 // VppAPI represents a mock VPP adapter that can be used for unit/integration testing instead of the vppapiclient adapter.
40 type VppAdapter struct {
41 callback adapter.MsgCallback
45 msgNameToIds map[string]uint16
46 msgIDsToName map[uint16]string
47 binAPITypes map[string]reflect.Type
49 repliesLock sync.Mutex // mutex for the queue
50 replies []reply // FIFO queue of messages
51 replyHandlers []ReplyHandler // callbacks that are able to calculate mock responses
52 mode replyMode // mode in which the mock operates
55 // defaultReply is a default reply message that mock adapter returns for a request.
56 type defaultReply struct {
60 func (*defaultReply) GetMessageName() string { return "mock_default_reply" }
61 func (*defaultReply) GetCrcString() string { return "xxxxxxxx" }
62 func (*defaultReply) GetMessageType() api.MessageType {
63 return api.ReplyMessage
65 func (m *defaultReply) Size() int {
74 func (m *defaultReply) Marshal(b []byte) ([]byte, error) {
77 buf = codec.NewBuffer(make([]byte, m.Size()))
79 buf = codec.NewBuffer(b)
82 buf.EncodeUint32(uint32(m.Retval))
83 return buf.Bytes(), nil
85 func (m *defaultReply) Unmarshal(b []byte) error {
86 buf := codec.NewBuffer(b)
88 m.Retval = int32(buf.DecodeUint32())
92 // MessageDTO is a structure used for propagating information to ReplyHandlers.
93 type MessageDTO struct {
100 // reply for one request (can be multipart, contain replies to previously timeouted requests, etc.)
102 msgs []MsgWithContext
105 // MsgWithContext encapsulates reply message with possibly sequence number and is-multipart flag.
106 type MsgWithContext struct {
111 /* set by mock adapter */
115 // ReplyHandler is a type that allows to extend the behaviour of VPP mock.
116 // Return value ok is used to signalize that mock reply is calculated and ready to be used.
117 type ReplyHandler func(request MessageDTO) (reply []byte, msgID uint16, ok bool)
120 defaultReplyMsgID = 1 // default message ID for the reply to be sent back via callback
123 // NewVppAdapter returns a new mock adapter.
124 func NewVppAdapter() *VppAdapter {
127 msgIDsToName: make(map[uint16]string),
128 msgNameToIds: make(map[string]uint16),
129 binAPITypes: make(map[string]reflect.Type),
131 a.registerBinAPITypes()
135 // Connect emulates connecting the process to VPP.
136 func (a *VppAdapter) Connect() error {
140 // Disconnect emulates disconnecting the process from VPP.
141 func (a *VppAdapter) Disconnect() error {
145 // GetMsgNameByID returns message name for specified message ID.
146 func (a *VppAdapter) GetMsgNameByID(msgID uint16) (string, bool) {
149 return "control_ping", true
151 return "control_ping_reply", true
153 return "sw_interface_dump", true
155 return "sw_interface_details", true
159 defer a.access.Unlock()
160 msgName, found := a.msgIDsToName[msgID]
162 return msgName, found
165 func (a *VppAdapter) registerBinAPITypes() {
167 defer a.access.Unlock()
168 for _, msg := range api.GetRegisteredMessages() {
169 a.binAPITypes[msg.GetMessageName()] = reflect.TypeOf(msg).Elem()
173 // ReplyTypeFor returns reply message type for given request message name.
174 func (a *VppAdapter) ReplyTypeFor(requestMsgName string) (reflect.Type, uint16, bool) {
175 replyName, foundName := binapi.ReplyNameFor(requestMsgName)
177 if reply, found := a.binAPITypes[replyName]; found {
178 msgID, err := a.GetMsgID(replyName, "")
180 return reply, msgID, found
188 // ReplyFor returns reply message for given request message name.
189 func (a *VppAdapter) ReplyFor(requestMsgName string) (api.Message, uint16, bool) {
190 replType, msgID, foundReplType := a.ReplyTypeFor(requestMsgName)
192 msgVal := reflect.New(replType)
193 if msg, ok := msgVal.Interface().(api.Message); ok {
194 log.Println("FFF ", replType, msgID, foundReplType)
195 return msg, msgID, true
202 // ReplyBytes encodes the mocked reply into binary format.
203 func (a *VppAdapter) ReplyBytes(request MessageDTO, reply api.Message) ([]byte, error) {
204 replyMsgID, err := a.GetMsgID(reply.GetMessageName(), reply.GetCrcString())
206 log.Println("ReplyBytesE ", replyMsgID, " ", reply.GetMessageName(), " clientId: ", request.ClientID,
210 log.Println("ReplyBytes ", replyMsgID, " ", reply.GetMessageName(), " clientId: ", request.ClientID)
212 data, err := codec.DefaultCodec.EncodeMsg(reply, replyMsgID)
216 if reply.GetMessageType() == api.ReplyMessage {
217 binary.BigEndian.PutUint32(data[2:6], request.ClientID)
218 } else if reply.GetMessageType() == api.RequestMessage {
219 binary.BigEndian.PutUint32(data[6:10], request.ClientID)
224 // GetMsgID returns mocked message ID for the given message name and CRC.
225 func (a *VppAdapter) GetMsgID(msgName string, msgCrc string) (uint16, error) {
229 case "control_ping_reply":
231 case "sw_interface_dump":
233 case "sw_interface_details":
238 defer a.access.Unlock()
240 msgID, found := a.msgNameToIds[msgName]
247 a.msgNameToIds[msgName] = msgID
248 a.msgIDsToName[msgID] = msgName
253 // SendMsg emulates sending a binary-encoded message to VPP.
254 func (a *VppAdapter) SendMsg(clientID uint32, data []byte) error {
257 a.repliesLock.Unlock()
259 case useReplyHandlers:
260 for i := len(a.replyHandlers) - 1; i >= 0; i-- {
261 replyHandler := a.replyHandlers[i]
263 msgID := binary.BigEndian.Uint16(data[0:2])
266 reqMsgName := a.msgIDsToName[msgID]
269 reply, msgID, finished := replyHandler(MessageDTO{
276 a.callback(msgID, reply)
282 case useRepliesQueue:
284 defer a.repliesLock.Unlock()
286 // pop the first reply
287 if len(a.replies) > 0 {
288 reply := a.replies[0]
289 for _, msg := range reply.msgs {
290 msgID, _ := a.GetMsgID(msg.Msg.GetMessageName(), msg.Msg.GetCrcString())
293 context = setMultipart(context, msg.Multipart)
294 context = setSeqNum(context, msg.SeqNum)
296 data, err := codec.DefaultCodec.EncodeMsg(msg.Msg, msgID)
300 if msg.Msg.GetMessageType() == api.ReplyMessage {
301 binary.BigEndian.PutUint32(data[2:6], context)
302 } else if msg.Msg.GetMessageType() == api.RequestMessage {
303 binary.BigEndian.PutUint32(data[6:10], context)
305 a.callback(msgID, data)
308 a.replies = a.replies[1:]
309 if len(a.replies) == 0 && len(a.replyHandlers) > 0 {
310 // Switch back to handlers once the queue is empty to revert back
311 // the fallthrough effect.
312 a.mode = useReplyHandlers
319 // return default reply
320 msgID := uint16(defaultReplyMsgID)
321 data, err := codec.DefaultCodec.EncodeMsg(&defaultReply{}, msgID)
325 binary.BigEndian.PutUint32(data[2:6], clientID)
326 a.callback(msgID, data)
331 // SetMsgCallback sets a callback function that will be called by the adapter whenever a message comes from the mock.
332 func (a *VppAdapter) SetMsgCallback(cb adapter.MsgCallback) {
336 // WaitReady mocks waiting for VPP
337 func (a *VppAdapter) WaitReady() error {
341 // MockReply stores a message or a list of multipart messages to be returned when
342 // the next request comes. It is a FIFO queue - multiple replies can be pushed into it,
343 // the first message or the first set of multi-part messages will be popped when
344 // some request comes.
345 // Using of this method automatically switches the mock into the useRepliesQueue mode.
347 // Note: multipart requests are implemented using two requests actually - the multipart
348 // request itself followed by control ping used to tell which multipart message
349 // is the last one. A mock reply to a multipart request has to thus consist of
350 // exactly two calls of this method.
353 // mockVpp.MockReply( // push multipart messages all at once
354 // &interfaces.SwInterfaceDetails{SwIfIndex:1},
355 // &interfaces.SwInterfaceDetails{SwIfIndex:2},
356 // &interfaces.SwInterfaceDetails{SwIfIndex:3},
358 // mockVpp.MockReply(&vpe.ControlPingReply{})
360 // Even if the multipart request has no replies, MockReply has to be called twice:
362 // mockVpp.MockReply() // zero multipart messages
363 // mockVpp.MockReply(&vpe.ControlPingReply{})
364 func (a *VppAdapter) MockReply(msgs ...api.Message) {
366 defer a.repliesLock.Unlock()
369 for _, msg := range msgs {
370 r.msgs = append(r.msgs, MsgWithContext{Msg: msg, hasCtx: false})
372 a.replies = append(a.replies, r)
373 a.mode = useRepliesQueue
376 // MockReplyWithContext queues next reply like MockReply() does, except that the
377 // sequence number and multipart flag (= context minus channel ID) can be customized
378 // and not necessarily match with the request.
379 // The purpose of this function is to test handling of sequence numbers and as such
380 // it is not really meant to be used outside the govpp UTs.
381 func (a *VppAdapter) MockReplyWithContext(msgs ...MsgWithContext) {
383 defer a.repliesLock.Unlock()
386 for _, msg := range msgs {
387 r.msgs = append(r.msgs,
388 MsgWithContext{Msg: msg.Msg, SeqNum: msg.SeqNum, Multipart: msg.Multipart, hasCtx: true})
390 a.replies = append(a.replies, r)
391 a.mode = useRepliesQueue
394 // MockReplyHandler registers a handler function that is supposed to generate mock responses to incoming requests.
395 // Using of this method automatically switches the mock into th useReplyHandlers mode.
396 func (a *VppAdapter) MockReplyHandler(replyHandler ReplyHandler) {
398 defer a.repliesLock.Unlock()
400 a.replyHandlers = append(a.replyHandlers, replyHandler)
401 a.mode = useReplyHandlers
404 // MockClearReplyHanders clears all reply handlers that were registered
405 // Will also set the mode to useReplyHandlers
406 func (a *VppAdapter) MockClearReplyHandlers() {
408 defer a.repliesLock.Unlock()
410 a.replyHandlers = a.replyHandlers[:0]
411 a.mode = useReplyHandlers
414 func setSeqNum(context uint32, seqNum uint16) (newContext uint32) {
415 context &= 0xffff0000
416 context |= uint32(seqNum)
420 func setMultipart(context uint32, isMultipart bool) (newContext uint32) {
421 context &= 0xfffeffff