From d1f24d37bd447b64e402298bb8eb2479681facf9 Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Fri, 17 Jul 2020 10:36:28 +0200 Subject: Improve binapi generator - simplified Size/Marshal/Unmarshal methods - replace struc in unions with custom marshal/unmarshal - fix imports in generated files - fix mock adapter - generate rpc service using low-level stream API (dumps generate control ping or stream msg..) - move examples/binapi to binapi and generate all API for latest release - add binapigen.Plugin for developing custom generator plugins - optionally generate HTTP handlers (REST API) for RPC services - add govpp program for browsing VPP API Change-Id: I092e9ed2b0c17972b3476463c3d4b14dd76ed42b Signed-off-by: Ondrej Fabry --- binapi/stn/stn.ba.go | 205 ++++++++++++++++++++++++++++++++++++++++++++++ binapi/stn/stn_rest.ba.go | 37 +++++++++ binapi/stn/stn_rpc.ba.go | 73 +++++++++++++++++ 3 files changed, 315 insertions(+) create mode 100644 binapi/stn/stn.ba.go create mode 100644 binapi/stn/stn_rest.ba.go create mode 100644 binapi/stn/stn_rpc.ba.go (limited to 'binapi/stn') diff --git a/binapi/stn/stn.ba.go b/binapi/stn/stn.ba.go new file mode 100644 index 0000000..34cb419 --- /dev/null +++ b/binapi/stn/stn.ba.go @@ -0,0 +1,205 @@ +// Code generated by GoVPP's binapi-generator. DO NOT EDIT. +// versions: +// binapi-generator: v0.4.0-dev +// VPP: 20.05-release +// source: /usr/share/vpp/api/plugins/stn.api.json + +// Package stn contains generated bindings for API file stn.api. +// +// Contents: +// 4 messages +// +package stn + +import ( + api "git.fd.io/govpp.git/api" + interface_types "git.fd.io/govpp.git/binapi/interface_types" + ip_types "git.fd.io/govpp.git/binapi/ip_types" + codec "git.fd.io/govpp.git/codec" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the GoVPP api package it is being compiled against. +// A compilation error at this line likely means your copy of the +// GoVPP api package needs to be updated. +const _ = api.GoVppAPIPackageIsVersion2 + +const ( + APIFile = "stn" + APIVersion = "2.0.0" + VersionCrc = 0x5a5079cd +) + +// StnAddDelRule defines message 'stn_add_del_rule'. +type StnAddDelRule struct { + IPAddress ip_types.Address `binapi:"address,name=ip_address" json:"ip_address,omitempty"` + SwIfIndex interface_types.InterfaceIndex `binapi:"interface_index,name=sw_if_index" json:"sw_if_index,omitempty"` + IsAdd bool `binapi:"bool,name=is_add" json:"is_add,omitempty"` +} + +func (m *StnAddDelRule) Reset() { *m = StnAddDelRule{} } +func (*StnAddDelRule) GetMessageName() string { return "stn_add_del_rule" } +func (*StnAddDelRule) GetCrcString() string { return "53f751e6" } +func (*StnAddDelRule) GetMessageType() api.MessageType { + return api.RequestMessage +} + +func (m *StnAddDelRule) Size() int { + if m == nil { + return 0 + } + var size int + size += 1 // m.IPAddress.Af + size += 1 * 16 // m.IPAddress.Un + size += 4 // m.SwIfIndex + size += 1 // m.IsAdd + return size +} +func (m *StnAddDelRule) Marshal(b []byte) ([]byte, error) { + var buf *codec.Buffer + if b == nil { + buf = codec.NewBuffer(make([]byte, m.Size())) + } else { + buf = codec.NewBuffer(b) + } + buf.EncodeUint8(uint8(m.IPAddress.Af)) + buf.EncodeBytes(m.IPAddress.Un.XXX_UnionData[:], 0) + buf.EncodeUint32(uint32(m.SwIfIndex)) + buf.EncodeBool(m.IsAdd) + return buf.Bytes(), nil +} +func (m *StnAddDelRule) Unmarshal(b []byte) error { + buf := codec.NewBuffer(b) + m.IPAddress.Af = ip_types.AddressFamily(buf.DecodeUint8()) + copy(m.IPAddress.Un.XXX_UnionData[:], buf.DecodeBytes(16)) + m.SwIfIndex = interface_types.InterfaceIndex(buf.DecodeUint32()) + m.IsAdd = buf.DecodeBool() + return nil +} + +// StnAddDelRuleReply defines message 'stn_add_del_rule_reply'. +type StnAddDelRuleReply struct { + Retval int32 `binapi:"i32,name=retval" json:"retval,omitempty"` +} + +func (m *StnAddDelRuleReply) Reset() { *m = StnAddDelRuleReply{} } +func (*StnAddDelRuleReply) GetMessageName() string { return "stn_add_del_rule_reply" } +func (*StnAddDelRuleReply) GetCrcString() string { return "e8d4e804" } +func (*StnAddDelRuleReply) GetMessageType() api.MessageType { + return api.ReplyMessage +} + +func (m *StnAddDelRuleReply) Size() int { + if m == nil { + return 0 + } + var size int + size += 4 // m.Retval + return size +} +func (m *StnAddDelRuleReply) Marshal(b []byte) ([]byte, error) { + var buf *codec.Buffer + if b == nil { + buf = codec.NewBuffer(make([]byte, m.Size())) + } else { + buf = codec.NewBuffer(b) + } + buf.EncodeUint32(uint32(m.Retval)) + return buf.Bytes(), nil +} +func (m *StnAddDelRuleReply) Unmarshal(b []byte) error { + buf := codec.NewBuffer(b) + m.Retval = int32(buf.DecodeUint32()) + return nil +} + +// StnRulesDetails defines message 'stn_rules_details'. +type StnRulesDetails struct { + IPAddress ip_types.Address `binapi:"address,name=ip_address" json:"ip_address,omitempty"` + SwIfIndex interface_types.InterfaceIndex `binapi:"interface_index,name=sw_if_index" json:"sw_if_index,omitempty"` +} + +func (m *StnRulesDetails) Reset() { *m = StnRulesDetails{} } +func (*StnRulesDetails) GetMessageName() string { return "stn_rules_details" } +func (*StnRulesDetails) GetCrcString() string { return "b0f6606c" } +func (*StnRulesDetails) GetMessageType() api.MessageType { + return api.ReplyMessage +} + +func (m *StnRulesDetails) Size() int { + if m == nil { + return 0 + } + var size int + size += 1 // m.IPAddress.Af + size += 1 * 16 // m.IPAddress.Un + size += 4 // m.SwIfIndex + return size +} +func (m *StnRulesDetails) Marshal(b []byte) ([]byte, error) { + var buf *codec.Buffer + if b == nil { + buf = codec.NewBuffer(make([]byte, m.Size())) + } else { + buf = codec.NewBuffer(b) + } + buf.EncodeUint8(uint8(m.IPAddress.Af)) + buf.EncodeBytes(m.IPAddress.Un.XXX_UnionData[:], 0) + buf.EncodeUint32(uint32(m.SwIfIndex)) + return buf.Bytes(), nil +} +func (m *StnRulesDetails) Unmarshal(b []byte) error { + buf := codec.NewBuffer(b) + m.IPAddress.Af = ip_types.AddressFamily(buf.DecodeUint8()) + copy(m.IPAddress.Un.XXX_UnionData[:], buf.DecodeBytes(16)) + m.SwIfIndex = interface_types.InterfaceIndex(buf.DecodeUint32()) + return nil +} + +// StnRulesDump defines message 'stn_rules_dump'. +type StnRulesDump struct{} + +func (m *StnRulesDump) Reset() { *m = StnRulesDump{} } +func (*StnRulesDump) GetMessageName() string { return "stn_rules_dump" } +func (*StnRulesDump) GetCrcString() string { return "51077d14" } +func (*StnRulesDump) GetMessageType() api.MessageType { + return api.RequestMessage +} + +func (m *StnRulesDump) Size() int { + if m == nil { + return 0 + } + var size int + return size +} +func (m *StnRulesDump) Marshal(b []byte) ([]byte, error) { + var buf *codec.Buffer + if b == nil { + buf = codec.NewBuffer(make([]byte, m.Size())) + } else { + buf = codec.NewBuffer(b) + } + return buf.Bytes(), nil +} +func (m *StnRulesDump) Unmarshal(b []byte) error { + return nil +} + +func init() { file_stn_binapi_init() } +func file_stn_binapi_init() { + api.RegisterMessage((*StnAddDelRule)(nil), "stn_add_del_rule_53f751e6") + api.RegisterMessage((*StnAddDelRuleReply)(nil), "stn_add_del_rule_reply_e8d4e804") + api.RegisterMessage((*StnRulesDetails)(nil), "stn_rules_details_b0f6606c") + api.RegisterMessage((*StnRulesDump)(nil), "stn_rules_dump_51077d14") +} + +// Messages returns list of all messages in this module. +func AllMessages() []api.Message { + return []api.Message{ + (*StnAddDelRule)(nil), + (*StnAddDelRuleReply)(nil), + (*StnRulesDetails)(nil), + (*StnRulesDump)(nil), + } +} diff --git a/binapi/stn/stn_rest.ba.go b/binapi/stn/stn_rest.ba.go new file mode 100644 index 0000000..826aee9 --- /dev/null +++ b/binapi/stn/stn_rest.ba.go @@ -0,0 +1,37 @@ +// Code generated by GoVPP's binapi-generator. DO NOT EDIT. + +package stn + +import ( + "encoding/json" + "io/ioutil" + "net/http" +) + +func RESTHandler(rpc RPCService) http.Handler { + mux := http.NewServeMux() + mux.HandleFunc("/stn_add_del_rule", func(w http.ResponseWriter, req *http.Request) { + var request = new(StnAddDelRule) + b, err := ioutil.ReadAll(req.Body) + if err != nil { + http.Error(w, "read body failed", http.StatusBadRequest) + return + } + if err := json.Unmarshal(b, request); err != nil { + http.Error(w, "unmarshal data failed", http.StatusBadRequest) + return + } + reply, err := rpc.StnAddDelRule(req.Context(), request) + if err != nil { + http.Error(w, "request failed: "+err.Error(), http.StatusInternalServerError) + return + } + rep, err := json.MarshalIndent(reply, "", " ") + if err != nil { + http.Error(w, "marshal failed: "+err.Error(), http.StatusInternalServerError) + return + } + w.Write(rep) + }) + return http.HandlerFunc(mux.ServeHTTP) +} diff --git a/binapi/stn/stn_rpc.ba.go b/binapi/stn/stn_rpc.ba.go new file mode 100644 index 0000000..932074c --- /dev/null +++ b/binapi/stn/stn_rpc.ba.go @@ -0,0 +1,73 @@ +// Code generated by GoVPP's binapi-generator. DO NOT EDIT. + +package stn + +import ( + "context" + "fmt" + api "git.fd.io/govpp.git/api" + vpe "git.fd.io/govpp.git/binapi/vpe" + "io" +) + +// RPCService defines RPC service stn. +type RPCService interface { + StnAddDelRule(ctx context.Context, in *StnAddDelRule) (*StnAddDelRuleReply, error) + StnRulesDump(ctx context.Context, in *StnRulesDump) (RPCService_StnRulesDumpClient, error) +} + +type serviceClient struct { + conn api.Connection +} + +func NewServiceClient(conn api.Connection) RPCService { + return &serviceClient{conn} +} + +func (c *serviceClient) StnAddDelRule(ctx context.Context, in *StnAddDelRule) (*StnAddDelRuleReply, error) { + out := new(StnAddDelRuleReply) + err := c.conn.Invoke(ctx, in, out) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *serviceClient) StnRulesDump(ctx context.Context, in *StnRulesDump) (RPCService_StnRulesDumpClient, error) { + stream, err := c.conn.NewStream(ctx) + if err != nil { + return nil, err + } + x := &serviceClient_StnRulesDumpClient{stream} + if err := x.Stream.SendMsg(in); err != nil { + return nil, err + } + if err = x.Stream.SendMsg(&vpe.ControlPing{}); err != nil { + return nil, err + } + return x, nil +} + +type RPCService_StnRulesDumpClient interface { + Recv() (*StnRulesDetails, error) + api.Stream +} + +type serviceClient_StnRulesDumpClient struct { + api.Stream +} + +func (c *serviceClient_StnRulesDumpClient) Recv() (*StnRulesDetails, error) { + msg, err := c.Stream.RecvMsg() + if err != nil { + return nil, err + } + switch m := msg.(type) { + case *StnRulesDetails: + return m, nil + case *vpe.ControlPingReply: + return nil, io.EOF + default: + return nil, fmt.Errorf("unexpected message: %T %v", m, m) + } +} -- cgit 1.2.3-korg