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
95 // 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
102 // from VPP, or until an error occurs). The reply will be decoded into the msg
103 // argument. Error will be returned if the response cannot be received or decoded.
104 ReceiveReply(msg Message) error
107 // MultiRequestCtx is helper interface which allows to receive reply on multi-request.
108 type MultiRequestCtx interface {
109 // ReceiveReply receives a reply from VPP (blocks until a reply is delivered
110 // from VPP, or until an error occurs).The reply will be decoded into the msg
111 // argument. If the last reply has been already consumed, lastReplyReceived is
112 // set to true. Do not use the message itself if lastReplyReceived is
113 // true - it won't be filled with actual data.Error will be returned if the
114 // response cannot be received or decoded.
115 ReceiveReply(msg Message) (lastReplyReceived bool, err error)
118 // SubscriptionCtx is helper interface which allows to control subscription for
119 // notification events.
120 type SubscriptionCtx interface {
121 // Unsubscribe unsubscribes from receiving the notifications tied to the
122 // subscription context.
126 // CompatibilityError is the error type usually returned by CheckCompatibility
127 // method of Channel. It contains list of all the compatible/incompatible messages.
128 type CompatibilityError struct {
129 CompatibleMessages []string
130 IncompatibleMessages []string
133 func (c *CompatibilityError) Error() string {
134 return fmt.Sprintf("%d/%d messages incompatible", len(c.IncompatibleMessages), len(c.CompatibleMessages)+len(c.IncompatibleMessages))
138 registeredMessageTypes = make(map[reflect.Type]string)
139 registeredMessages = make(map[string]Message)
142 // RegisterMessage is called from generated code to register message.
143 func RegisterMessage(x Message, name string) {
144 typ := reflect.TypeOf(x)
145 namecrc := x.GetMessageName() + "_" + x.GetCrcString()
146 if _, ok := registeredMessageTypes[typ]; ok {
147 panic(fmt.Errorf("govpp: message type %v already registered as %s (%s)", typ, name, namecrc))
149 registeredMessages[namecrc] = x
150 registeredMessageTypes[typ] = name
153 // GetRegisteredMessages returns list of all registered messages.
154 func GetRegisteredMessages() map[string]Message {
155 return registeredMessages
158 // GetRegisteredMessageTypes returns list of all registered message types.
159 func GetRegisteredMessageTypes() map[reflect.Type]string {
160 return registeredMessageTypes
163 // GoVppAPIPackageIsVersionX is referenced from generated binapi files
164 // to assert that that code is compatible with this version of the GoVPP api package.
165 const GoVppAPIPackageIsVersion1 = true