Introduce Stream - experimental API for low-level access to VPP API
[govpp.git] / api / binapi.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         "reflect"
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
58 var (
59         registeredMessageTypes = make(map[reflect.Type]string)
60         registeredMessages     = make(map[string]Message)
61 )
62
63 // RegisterMessage is called from generated code to register message.
64 func RegisterMessage(x Message, name string) {
65         typ := reflect.TypeOf(x)
66         namecrc := x.GetMessageName() + "_" + x.GetCrcString()
67         if _, ok := registeredMessageTypes[typ]; ok {
68                 panic(fmt.Errorf("govpp: message type %v already registered as %s (%s)", typ, name, namecrc))
69         }
70         registeredMessages[namecrc] = x
71         registeredMessageTypes[typ] = name
72 }
73
74 // GetRegisteredMessages returns list of all registered messages.
75 func GetRegisteredMessages() map[string]Message {
76         return registeredMessages
77 }
78
79 // GetRegisteredMessageTypes returns list of all registered message types.
80 func GetRegisteredMessageTypes() map[reflect.Type]string {
81         return registeredMessageTypes
82 }
83
84 // GoVppAPIPackageIsVersionX is referenced from generated binapi files
85 // to assert that that code is compatible with this version of the GoVPP api package.
86 const GoVppAPIPackageIsVersion1 = true
87 const GoVppAPIPackageIsVersion2 = true