go.ligato.io/vpp-agent/v3@v3.5.0/cmd/agentctl/client/vpp.go (about) 1 // Copyright (c) 2019 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 client 16 17 import ( 18 "context" 19 "encoding/json" 20 "fmt" 21 22 "go.fd.io/govpp/api" 23 ) 24 25 func (c *Client) VppRunCli(ctx context.Context, cmd string) (reply string, err error) { 26 data := map[string]interface{}{ 27 "vppclicommand": cmd, 28 } 29 resp, err := c.post(ctx, "/vpp/command", nil, data, nil) 30 if err != nil { 31 return "", fmt.Errorf("HTTP POST request failed: %v", err) 32 } 33 if err := json.NewDecoder(resp.body).Decode(&reply); err != nil { 34 return "", fmt.Errorf("decoding reply failed: %v", err) 35 } 36 return reply, nil 37 } 38 39 func (c *Client) VppGetStats(ctx context.Context, typ string) error { 40 // TODO: implement more generic stats provider that goes beyond GoVPP StatsProvider (git.fd.io/govpp/api/stats.go) 41 // and can dump any possible stats or all of them (just like in stats dump example in 42 // git.fd.io/govpp/examples/stats-client/stats_api.go) 43 return nil 44 } 45 46 func (c *Client) VppGetInterfaceStats() (*api.InterfaceStats, error) { 47 statsProvider, err := c.vppStatsProvider() 48 if err != nil { 49 return nil, fmt.Errorf("can't get vpp stats provider for interface stats retrieval due to: %v", err) 50 } 51 var stats api.InterfaceStats 52 if err := statsProvider.GetInterfaceStats(&stats); err != nil { 53 return nil, fmt.Errorf("getting interface stats failed due to: %v", err) 54 } 55 return &stats, nil 56 } 57 58 func (c *Client) VppGetErrorStats() (*api.ErrorStats, error) { 59 statsProvider, err := c.vppStatsProvider() 60 if err != nil { 61 return nil, fmt.Errorf("can't get vpp stats provider for error stats retrieval due to: %v", err) 62 } 63 var stats api.ErrorStats 64 if err := statsProvider.GetErrorStats(&stats); err != nil { 65 return nil, fmt.Errorf("getting error stats failed due to: %v", err) 66 } 67 return &stats, nil 68 } 69 70 func (c *Client) VppGetSystemStats() (*api.SystemStats, error) { 71 statsProvider, err := c.vppStatsProvider() 72 if err != nil { 73 return nil, fmt.Errorf("can't get vpp stats provider for system stats retrieval due to: %v", err) 74 } 75 var stats api.SystemStats 76 if err := statsProvider.GetSystemStats(&stats); err != nil { 77 return nil, fmt.Errorf("getting system stats failed due to: %v", err) 78 } 79 return &stats, nil 80 } 81 82 func (c *Client) VppGetNodeStats() (*api.NodeStats, error) { 83 statsProvider, err := c.vppStatsProvider() 84 if err != nil { 85 return nil, fmt.Errorf("can't get vpp stats provider for node stats retrieval due to: %v", err) 86 } 87 var stats api.NodeStats 88 if err := statsProvider.GetNodeStats(&stats); err != nil { 89 return nil, fmt.Errorf("getting node stats failed due to: %v", err) 90 } 91 return &stats, nil 92 } 93 94 func (c *Client) VppGetBufferStats() (*api.BufferStats, error) { 95 statsProvider, err := c.vppStatsProvider() 96 if err != nil { 97 return nil, fmt.Errorf("can't get vpp stats provider for buffer stats retrieval due to: %v", err) 98 } 99 var stats api.BufferStats 100 if err := statsProvider.GetBufferStats(&stats); err != nil { 101 return nil, fmt.Errorf("getting buffer stats failed due to: %v", err) 102 } 103 return &stats, nil 104 } 105 106 func (c *Client) vppStatsProvider() (api.StatsProvider, error) { 107 proxyClient, err := c.GoVPPProxyClient() 108 if err != nil { 109 return nil, fmt.Errorf("can't get GoVPP proxy client due to: %v", err) 110 } 111 statsProvider, err := proxyClient.NewStatsClient() 112 if err != nil { 113 return nil, fmt.Errorf("can't get GoVPP's proxy stats client due to: %v", err) 114 } 115 return statsProvider, nil 116 }