1 // Copyright (c) 2019 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 "git.fd.io/govpp.git/api"
24 "git.fd.io/govpp.git/core"
32 // Connect dials remote proxy server on given address and
33 // returns new client if successful.
34 func Connect(addr string) (*Client, error) {
35 client, err := rpc.DialHTTP("tcp", addr)
37 return nil, fmt.Errorf("connection error:%v", err)
46 // NewStatsClient returns new StatsClient which implements api.StatsProvider.
47 func (c *Client) NewStatsClient() (*StatsClient, error) {
48 stats := &StatsClient{
54 // NewBinapiClient returns new BinapiClient which implements api.Channel.
55 func (c *Client) NewBinapiClient() (*BinapiClient, error) {
56 binapi := &BinapiClient{
58 timeout: core.DefaultReplyTimeout,
63 type StatsClient struct {
67 func (s *StatsClient) GetSystemStats(sysStats *api.SystemStats) error {
68 // we need to start with a clean, zeroed item before decoding
69 // 'cause if the new values are 'zero' for the type, they will be ignored
70 // by the decoder. (i.e the old values will be left unchanged).
71 req := StatsRequest{StatsType: "system"}
72 resp := StatsResponse{SysStats: new(api.SystemStats)}
73 if err := s.rpc.Call("StatsRPC.GetStats", req, &resp); err != nil {
76 *sysStats = *resp.SysStats
80 func (s *StatsClient) GetNodeStats(nodeStats *api.NodeStats) error {
81 req := StatsRequest{StatsType: "node"}
82 resp := StatsResponse{NodeStats: new(api.NodeStats)}
83 if err := s.rpc.Call("StatsRPC.GetStats", req, &resp); err != nil {
86 *nodeStats = *resp.NodeStats
90 func (s *StatsClient) GetInterfaceStats(ifaceStats *api.InterfaceStats) error {
91 req := StatsRequest{StatsType: "interface"}
92 resp := StatsResponse{IfaceStats: new(api.InterfaceStats)}
93 if err := s.rpc.Call("StatsRPC.GetStats", req, &resp); err != nil {
96 *ifaceStats = *resp.IfaceStats
100 func (s *StatsClient) GetErrorStats(errStats *api.ErrorStats) error {
101 req := StatsRequest{StatsType: "error"}
102 resp := StatsResponse{ErrStats: new(api.ErrorStats)}
103 if err := s.rpc.Call("StatsRPC.GetStats", req, &resp); err != nil {
106 *errStats = *resp.ErrStats
110 func (s *StatsClient) GetBufferStats(bufStats *api.BufferStats) error {
111 req := StatsRequest{StatsType: "buffer"}
112 resp := StatsResponse{BufStats: new(api.BufferStats)}
113 if err := s.rpc.Call("StatsRPC.GetStats", req, &resp); err != nil {
116 *bufStats = *resp.BufStats
120 type BinapiClient struct {
122 timeout time.Duration
125 func (b *BinapiClient) SendRequest(msg api.Message) api.RequestCtx {
131 log.Debugf("SendRequest: %T %+v", msg, msg)
135 type requestCtx struct {
138 timeout time.Duration
141 func (r *requestCtx) ReceiveReply(msg api.Message) error {
142 req := BinapiRequest{
147 resp := BinapiResponse{}
149 err := r.rpc.Call("BinapiRPC.Invoke", req, &resp)
151 return fmt.Errorf("RPC call failed: %v", err)
154 // we set the value of msg to the value from response
155 reflect.ValueOf(msg).Elem().Set(reflect.ValueOf(resp.Msg).Elem())
160 func (b *BinapiClient) SendMultiRequest(msg api.Message) api.MultiRequestCtx {
161 req := &multiRequestCtx{
166 log.Debugf("SendMultiRequest: %T %+v", msg, msg)
170 type multiRequestCtx struct {
173 timeout time.Duration
176 replies []api.Message
179 func (r *multiRequestCtx) ReceiveReply(msg api.Message) (stop bool, err error) {
180 // we call Invoke only on first ReceiveReply
182 req := BinapiRequest{
188 resp := BinapiResponse{}
190 err := r.rpc.Call("BinapiRPC.Invoke", req, &resp)
192 return false, fmt.Errorf("RPC call failed: %v", err)
195 r.replies = resp.Msgs
198 if r.index >= len(r.replies) {
202 // we set the value of msg to the value from response
203 reflect.ValueOf(msg).Elem().Set(reflect.ValueOf(r.replies[r.index]).Elem())
209 func (b *BinapiClient) SubscribeNotification(notifChan chan api.Message, event api.Message) (api.SubscriptionCtx, error) {
210 panic("implement me")
213 func (b *BinapiClient) SetReplyTimeout(timeout time.Duration) {
217 func (b *BinapiClient) CheckCompatiblity(msgs ...api.Message) error {
218 msgNamesCrscs := make([]string, 0, len(msgs))
220 for _, msg := range msgs {
221 msgNamesCrscs = append(msgNamesCrscs, msg.GetMessageName()+"_"+msg.GetCrcString())
224 req := BinapiCompatibilityRequest{MsgNameCrcs: msgNamesCrscs}
225 resp := BinapiCompatibilityResponse{}
227 if err := b.rpc.Call("BinapiRPC.Compatibility", req, &resp); err != nil {
234 func (b *BinapiClient) Close() {