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.
21 // MessageType represents the type of a VPP message.
22 // Note: this is currently derived from the message header (fields),
23 // and in many cases it does not represent the actual type of VPP message.
24 // This means that some replies can be identified as requests, etc.
25 // TODO: use services to identify type of message
29 // RequestMessage represents a VPP request message
30 RequestMessage MessageType = iota
31 // ReplyMessage represents a VPP reply message
33 // EventMessage represents a VPP event message
35 // OtherMessage represents other VPP message
39 // Message is an interface that is implemented by all VPP Binary API messages generated by the binapigenerator.
40 type Message interface {
41 // GetMessageName returns the original VPP name of the message, as defined in the VPP API.
42 GetMessageName() string
44 // GetCrcString returns the string with CRC checksum of the message definition (the string represents a hexadecimal number).
47 // GetMessageType returns the type of the VPP message.
48 GetMessageType() MessageType
51 // DataType is an interface that is implemented by all VPP Binary API data types by the binapi_generator.
52 type DataType interface {
53 // GetTypeName returns the original VPP name of the data type, as defined in the VPP API.
56 // GetCrcString returns the string with CRC checksum of the data type definition (the string represents a hexadecimal number).
60 // ChannelProvider provides the communication channel with govpp core.
61 type ChannelProvider interface {
62 // NewAPIChannel returns a new channel for communication with VPP via govpp core.
63 // It uses default buffer sizes for the request and reply Go channels.
64 NewAPIChannel() (Channel, error)
66 // NewAPIChannelBuffered returns a new channel for communication with VPP via govpp core.
67 // It allows to specify custom buffer sizes for the request and reply Go channels.
68 NewAPIChannelBuffered(reqChanBufSize, replyChanBufSize int) (Channel, error)
71 // Channel provides methods for direct communication with VPP channel.
72 type Channel interface {
73 // SendRequest asynchronously sends a request to VPP. Returns a request context, that can be used to call ReceiveReply.
74 // In case of any errors by sending, the error will be delivered to ReplyChan (and returned by ReceiveReply).
75 SendRequest(msg Message) RequestCtx
77 // SendMultiRequest asynchronously sends a multipart request (request to which multiple responses are expected) to VPP.
78 // Returns a multipart request context, that can be used to call ReceiveReply.
79 // In case of any errors by sending, the error will be delivered to ReplyChan (and returned by ReceiveReply).
80 SendMultiRequest(msg Message) MultiRequestCtx
82 // SubscribeNotification subscribes for receiving of the specified notification messages via provided Go channel.
83 // Note that the caller is responsible for creating the Go channel with preferred buffer size. If the channel's
84 // buffer is full, the notifications will not be delivered into it.
85 SubscribeNotification(notifChan chan Message, event Message) (SubscriptionCtx, error)
87 // SetReplyTimeout sets the timeout for replies from VPP. It represents the maximum time the API waits for a reply
88 // from VPP before returning an error.
89 SetReplyTimeout(timeout time.Duration)
91 // CheckCompatibility checks the compatiblity for the given messages.
92 // It will return an error if any of the given messages are not compatible.
93 CheckCompatiblity(msgs ...Message) error
95 // Close closes the API channel and releases all API channel-related resources in the ChannelProvider.
99 // RequestCtx is helper interface which allows to receive reply on request.
100 type RequestCtx interface {
101 // ReceiveReply receives a reply from VPP (blocks until a reply is delivered from VPP, or until an error occurs).
102 // The reply will be decoded into the msg argument. Error will be returned if the response cannot be received or decoded.
103 ReceiveReply(msg Message) error
106 // MultiRequestCtx is helper interface which allows to receive reply on multi-request.
107 type MultiRequestCtx interface {
108 // ReceiveReply receives a reply from VPP (blocks until a reply is delivered from VPP, or until an error occurs).
109 // The reply will be decoded into the msg argument. If the last reply has been already consumed, lastReplyReceived is
110 // set to true. Do not use the message itself if lastReplyReceived is true - it won't be filled with actual data.
111 // Error will be returned if the response cannot be received or decoded.
112 ReceiveReply(msg Message) (lastReplyReceived bool, err error)
115 // SubscriptionCtx is helper interface which allows to control subscription for notification events.
116 type SubscriptionCtx interface {
117 // Unsubscribe unsubscribes from receiving the notifications tied to the subscription context.
121 var registeredMessages = make(map[string]Message)
123 // RegisterMessage is called from generated code to register message.
124 func RegisterMessage(x Message, name string) {
125 name = x.GetMessageName() + "_" + x.GetCrcString()
126 /*if _, ok := registeredMessages[name]; ok {
127 panic(fmt.Errorf("govpp: duplicate message registered: %s (%s)", name, x.GetCrcString()))
129 registeredMessages[name] = x
132 // GetRegisteredMessages returns list of all registered messages.
133 func GetRegisteredMessages() map[string]Message {
134 return registeredMessages
137 // GoVppAPIPackageIsVersion1 is referenced from generated binapi files
138 // to assert that that code is compatible with this version of the GoVPP api package.
139 const GoVppAPIPackageIsVersion1 = true