Refactor GoVPP
[govpp.git] / api / api.go
1 // Copyright (c) 2017 Cisco and/or its affiliates.
2 //
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:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package api
16
17 import (
18         "fmt"
19         "time"
20 )
21
22 // MessageType represents the type of a VPP message.
23 // Note: this is currently derived from the message header (fields),
24 // and in many cases it does not represent the actual type of VPP message.
25 // This means that some replies can be identified as requests, etc.
26 // TODO: use services to identify type of message
27 type MessageType int
28
29 const (
30         // RequestMessage represents a VPP request message
31         RequestMessage MessageType = iota
32         // ReplyMessage represents a VPP reply message
33         ReplyMessage
34         // EventMessage represents a VPP event message
35         EventMessage
36         // OtherMessage represents other VPP message
37         OtherMessage
38 )
39
40 // Message is an interface that is implemented by all VPP Binary API messages generated by the binapigenerator.
41 type Message interface {
42         // GetMessageName returns the original VPP name of the message, as defined in the VPP API.
43         GetMessageName() string
44
45         // GetCrcString returns the string with CRC checksum of the message definition (the string represents a hexadecimal number).
46         GetCrcString() string
47
48         // GetMessageType returns the type of the VPP message.
49         GetMessageType() MessageType
50 }
51
52 // DataType is an interface that is implemented by all VPP Binary API data types by the binapi_generator.
53 type DataType interface {
54         // GetTypeName returns the original VPP name of the data type, as defined in the VPP API.
55         GetTypeName() string
56
57         // GetCrcString returns the string with CRC checksum of the data type definition (the string represents a hexadecimal number).
58         GetCrcString() string
59 }
60
61 // MessageDecoder provides functionality for decoding binary data to generated API messages.
62 type MessageDecoder interface {
63         // DecodeMsg decodes binary-encoded data of a message into provided Message structure.
64         DecodeMsg(data []byte, msg Message) error
65 }
66
67 // MessageIdentifier provides identification of generated API messages.
68 type MessageIdentifier interface {
69         // GetMessageID returns message identifier of given API message.
70         GetMessageID(msg Message) (uint16, error)
71
72         // LookupByID looks up message name and crc by ID
73         LookupByID(msgID uint16) (Message, error)
74 }
75
76 // ChannelProvider provides the communication channel with govpp core.
77 type ChannelProvider interface {
78         // NewAPIChannel returns a new channel for communication with VPP via govpp core.
79         // It uses default buffer sizes for the request and reply Go channels.
80         NewAPIChannel() (Channel, error)
81
82         // NewAPIChannelBuffered returns a new channel for communication with VPP via govpp core.
83         // It allows to specify custom buffer sizes for the request and reply Go channels.
84         NewAPIChannelBuffered(reqChanBufSize, replyChanBufSize int) (Channel, error)
85 }
86
87 // Channel provides methods for direct communication with VPP channel.
88 type Channel interface {
89         // GetID returns channel's ID
90         GetID() uint16
91
92         // SendRequest asynchronously sends a request to VPP. Returns a request context, that can be used to call ReceiveReply.
93         // In case of any errors by sending, the error will be delivered to ReplyChan (and returned by ReceiveReply).
94         SendRequest(msg Message) RequestCtx
95
96         // SendMultiRequest asynchronously sends a multipart request (request to which multiple responses are expected) to VPP.
97         // Returns a multipart request context, that can be used to call ReceiveReply.
98         // In case of any errors by sending, the error will be delivered to ReplyChan (and returned by ReceiveReply).
99         SendMultiRequest(msg Message) MultiRequestCtx
100
101         // SubscribeNotification subscribes for receiving of the specified notification messages via provided Go channel.
102         // Note that the caller is responsible for creating the Go channel with preferred buffer size. If the channel's
103         // buffer is full, the notifications will not be delivered into it.
104         SubscribeNotification(notifChan chan Message, msgFactory func() Message) (*NotifSubscription, error)
105
106         // UnsubscribeNotification unsubscribes from receiving the notifications tied to the provided notification subscription.
107         UnsubscribeNotification(subscription *NotifSubscription) error
108
109         // SetReplyTimeout sets the timeout for replies from VPP. It represents the maximum time the API waits for a reply
110         // from VPP before returning an error.
111         SetReplyTimeout(timeout time.Duration)
112
113         // Close closes the API channel and releases all API channel-related resources in the ChannelProvider.
114         Close()
115 }
116
117 // RequestCtx is helper interface which allows to receive reply on request context data
118 type RequestCtx interface {
119         // ReceiveReply receives a reply from VPP (blocks until a reply is delivered from VPP, or until an error occurs).
120         // The reply will be decoded into the msg argument. Error will be returned if the response cannot be received or decoded.
121         ReceiveReply(msg Message) error
122 }
123
124 // MultiRequestCtx is helper interface which allows to receive reply on multi-request context data
125 type MultiRequestCtx interface {
126         // ReceiveReply receives a reply from VPP (blocks until a reply is delivered from VPP, or until an error occurs).
127         // The reply will be decoded into the msg argument. If the last reply has been already consumed, lastReplyReceived is
128         // set to true. Do not use the message itself if lastReplyReceived is true - it won't be filled with actual data.
129         // Error will be returned if the response cannot be received or decoded.
130         ReceiveReply(msg Message) (lastReplyReceived bool, err error)
131 }
132
133 // NotifSubscription represents a subscription for delivery of specific notification messages.
134 type NotifSubscription struct {
135         NotifChan  chan Message   // channel where notification messages will be delivered to
136         MsgFactory func() Message // function that returns a new instance of the specific message that is expected as a notification
137         // TODO: use Message directly here, not a factory, eliminating need to allocation
138 }
139
140 var registeredMessages = make(map[string]Message)
141
142 // RegisterMessage is called from generated code to register message.
143 func RegisterMessage(x Message, name string) {
144         if _, ok := registeredMessages[name]; ok {
145                 panic(fmt.Errorf("govpp: duplicate message registered: %s (%s)", name, x.GetCrcString()))
146         }
147         registeredMessages[name] = x
148 }
149
150 // GetAllMessages returns list of all registered messages.
151 func GetAllMessages() map[string]Message {
152         return registeredMessages
153 }