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.
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
30 // RequestMessage represents a VPP request message
31 RequestMessage MessageType = iota
32 // ReplyMessage represents a VPP reply message
34 // EventMessage represents a VPP event message
36 // OtherMessage represents other VPP message
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
45 // GetCrcString returns the string with CRC checksum of the message definition (the string represents a hexadecimal number).
48 // GetMessageType returns the type of the VPP message.
49 GetMessageType() MessageType
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.
59 registeredMessageTypes = make(map[reflect.Type]string)
60 registeredMessages = make(map[string]Message)
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))
70 registeredMessages[namecrc] = x
71 registeredMessageTypes[typ] = name
74 // GetRegisteredMessages returns list of all registered messages.
75 func GetRegisteredMessages() map[string]Message {
76 return registeredMessages
79 // GetRegisteredMessageTypes returns list of all registered message types.
80 func GetRegisteredMessageTypes() map[reflect.Type]string {
81 return registeredMessageTypes
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