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.
23 // MessageType represents the type of a VPP message.
24 // Note: this is currently derived from the message header (fields),
25 // and in many cases it does not represent the actual type of VPP message.
26 // This means that some replies can be identified as requests, etc.
27 // TODO: use services to identify type of message
31 // RequestMessage represents a VPP request message
32 RequestMessage MessageType = iota
33 // ReplyMessage represents a VPP reply message
35 // EventMessage represents a VPP event message
37 // OtherMessage represents other VPP message
41 // Message is an interface that is implemented by all VPP Binary API messages generated by the binapigenerator.
42 type Message interface {
43 // GetMessageName returns the original VPP name of the message, as defined in the VPP API.
44 GetMessageName() string
46 // GetCrcString returns the string with CRC checksum of the message definition (the string represents a hexadecimal number).
49 // GetMessageType returns the type of the VPP message.
50 GetMessageType() MessageType
53 // DataType is an interface that is implemented by all VPP Binary API data types by the binapi_generator.
54 type DataType interface {
55 // GetTypeName returns the original VPP name of the data type, as defined in the VPP API.
59 // ChannelProvider provides the communication channel with govpp core.
60 type ChannelProvider interface {
61 // NewAPIChannel returns a new channel for communication with VPP via govpp core.
62 // It uses default buffer sizes for the request and reply Go channels.
63 NewAPIChannel() (Channel, error)
65 // NewAPIChannelBuffered returns a new channel for communication with VPP via govpp core.
66 // It allows to specify custom buffer sizes for the request and reply Go channels.
67 NewAPIChannelBuffered(reqChanBufSize, replyChanBufSize int) (Channel, error)
70 // Channel provides methods for direct communication with VPP channel.
71 type Channel interface {
72 // SendRequest asynchronously sends a request to VPP. Returns a request context, that can be used to call ReceiveReply.
73 // In case of any errors by sending, the error will be delivered to ReplyChan (and returned by ReceiveReply).
74 SendRequest(msg Message) RequestCtx
76 // SendMultiRequest asynchronously sends a multipart request (request to which multiple responses are expected) to VPP.
77 // Returns a multipart request context, that can be used to call ReceiveReply.
78 // In case of any errors by sending, the error will be delivered to ReplyChan (and returned by ReceiveReply).
79 SendMultiRequest(msg Message) MultiRequestCtx
81 // SubscribeNotification subscribes for receiving of the specified notification messages via provided Go channel.
82 // Note that the caller is responsible for creating the Go channel with preferred buffer size. If the channel's
83 // buffer is full, the notifications will not be delivered into it.
84 SubscribeNotification(notifChan chan Message, event Message) (SubscriptionCtx, error)
86 // SetReplyTimeout sets the timeout for replies from VPP. It represents the maximum time the API waits for a reply
87 // from VPP before returning an error.
88 SetReplyTimeout(timeout time.Duration)
90 // CheckCompatibility checks the compatiblity for the given messages.
91 // It will return an error if any of the given messages are not compatible.
92 CheckCompatiblity(msgs ...Message) error
94 // Close closes the API channel and releases all API channel-related resources in the ChannelProvider.
98 // RequestCtx is helper interface which allows to receive reply on request.
99 type RequestCtx interface {
100 // ReceiveReply receives a reply from VPP (blocks until a reply is delivered from VPP, or until an error occurs).
101 // The reply will be decoded into the msg argument. Error will be returned if the response cannot be received or decoded.
102 ReceiveReply(msg Message) error
105 // MultiRequestCtx is helper interface which allows to receive reply on multi-request.
106 type MultiRequestCtx interface {
107 // ReceiveReply receives a reply from VPP (blocks until a reply is delivered from VPP, or until an error occurs).
108 // The reply will be decoded into the msg argument. If the last reply has been already consumed, lastReplyReceived is
109 // set to true. Do not use the message itself if lastReplyReceived is true - it won't be filled with actual data.
110 // Error will be returned if the response cannot be received or decoded.
111 ReceiveReply(msg Message) (lastReplyReceived bool, err error)
114 // SubscriptionCtx is helper interface which allows to control subscription for notification events.
115 type SubscriptionCtx interface {
116 // Unsubscribe unsubscribes from receiving the notifications tied to the subscription context.
120 // CompatibilityError is the error type usually returned by CheckCompatibility
121 // method of Channel. It describes all of the incompatible messages.
122 type CompatibilityError struct {
123 // IncompatibleMessages is the list of all messages
124 // that failed compatibility check.
125 IncompatibleMessages []string
128 func (c *CompatibilityError) Error() string {
129 return fmt.Sprintf("%d incompatible messages: %v", len(c.IncompatibleMessages), c.IncompatibleMessages)
133 registeredMessageTypes = make(map[reflect.Type]string)
134 registeredMessages = make(map[string]Message)
137 // RegisterMessage is called from generated code to register message.
138 func RegisterMessage(x Message, name string) {
139 typ := reflect.TypeOf(x)
140 namecrc := x.GetMessageName() + "_" + x.GetCrcString()
141 if _, ok := registeredMessageTypes[typ]; ok {
142 panic(fmt.Errorf("govpp: message type %v already registered as %s (%s)", typ, name, namecrc))
144 registeredMessages[namecrc] = x
145 registeredMessageTypes[typ] = name
148 // GetRegisteredMessages returns list of all registered messages.
149 func GetRegisteredMessages() map[string]Message {
150 return registeredMessages
153 // GetRegisteredMessageTypes returns list of all registered message types.
154 func GetRegisteredMessageTypes() map[reflect.Type]string {
155 return registeredMessageTypes
158 // GoVppAPIPackageIsVersion1 is referenced from generated binapi files
159 // to assert that that code is compatible with this version of the GoVPP api package.
160 const GoVppAPIPackageIsVersion1 = true