go.ligato.io/vpp-agent/v3@v3.5.0/plugins/govppmux/vppcalls/vpp_handler_api.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 vppcalls
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	govppapi "go.fd.io/govpp/api"
    22  	"go.ligato.io/cn-infra/v2/logging"
    23  
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp"
    25  )
    26  
    27  // VppCoreAPI provides methods for core VPP functionality.
    28  type VppCoreAPI interface {
    29  	// Ping sends control ping to VPP.
    30  	Ping(context.Context) error
    31  	// RunCli sends CLI command to VPP.
    32  	RunCli(ctx context.Context, cmd string) (string, error)
    33  	// GetVersion retrieves info about VPP version.
    34  	GetVersion(context.Context) (*VersionInfo, error)
    35  	// GetSession retrieves info about active session.
    36  	GetSession(context.Context) (*SessionInfo, error)
    37  	// GetModules retrieves info about VPP API modules.
    38  	GetModules(context.Context) ([]APIModule, error)
    39  	// GetPlugins retrieves info about loaded VPP plugins.
    40  	GetPlugins(context.Context) ([]PluginInfo, error)
    41  	// GetThreads retrieves info about VPP threads.
    42  	GetThreads(ctx context.Context) ([]ThreadInfo, error)
    43  }
    44  
    45  // SessionInfo contains info about VPP session.
    46  type SessionInfo struct {
    47  	PID       uint32
    48  	ClientIdx uint32
    49  	Uptime    float64
    50  }
    51  
    52  // VersionInfo contains VPP version info.
    53  type VersionInfo struct {
    54  	Program        string
    55  	Version        string
    56  	BuildDate      string
    57  	BuildDirectory string
    58  }
    59  
    60  // Release returns version in shortened format YY.MM that describes release.
    61  func (v VersionInfo) Release() string {
    62  	if len(v.Version) < 5 {
    63  		return ""
    64  	}
    65  	return v.Version[:5]
    66  }
    67  
    68  // APIModule contains info about VPP API module.
    69  type APIModule struct {
    70  	Name  string
    71  	Major uint32
    72  	Minor uint32
    73  	Patch uint32
    74  }
    75  
    76  func (m APIModule) String() string {
    77  	return fmt.Sprintf("%s %d.%d.%d", m.Name, m.Major, m.Minor, m.Patch)
    78  }
    79  
    80  // PluginInfo contains info about loaded VPP plugin.
    81  type PluginInfo struct {
    82  	Name        string
    83  	Path        string
    84  	Version     string
    85  	Description string
    86  }
    87  
    88  // ThreadInfo wraps all thread data counters.
    89  type ThreadInfo struct {
    90  	Name      string
    91  	ID        uint32
    92  	Type      string
    93  	PID       uint32
    94  	CPUID     uint32
    95  	Core      uint32
    96  	CPUSocket uint32
    97  }
    98  
    99  func (p PluginInfo) String() string {
   100  	return fmt.Sprintf("%s - %s", p.Name, p.Description)
   101  }
   102  
   103  var Handler = vpp.RegisterHandler(vpp.HandlerDesc{
   104  	Name:       "core",
   105  	HandlerAPI: (*VppCoreAPI)(nil),
   106  	NewFunc:    (*NewHandlerFunc)(nil),
   107  })
   108  
   109  type NewHandlerFunc func(vpp.Client) VppCoreAPI
   110  
   111  // AddVersion registers vppcalls Handler for the given version.
   112  func AddVersion(version vpp.Version, msgs []govppapi.Message, h NewHandlerFunc) {
   113  	Handler.AddVersion(vpp.HandlerVersion{
   114  		Version: version,
   115  		Check: func(c vpp.Client) error {
   116  			return c.CheckCompatiblity(msgs...)
   117  		},
   118  		NewHandler: func(c vpp.Client, a ...interface{}) vpp.HandlerAPI {
   119  			return h(c)
   120  		},
   121  		New: h,
   122  	})
   123  }
   124  
   125  func NewHandler(c vpp.Client) (VppCoreAPI, error) {
   126  	v, err := Handler.GetCompatibleVersion(c)
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  	return v.New.(NewHandlerFunc)(c), nil
   131  }
   132  
   133  // CompatibleHandler is helper for returning compatible Handler.
   134  func CompatibleHandler(c vpp.Client) VppCoreAPI {
   135  	v, err := NewHandler(c)
   136  	if err != nil {
   137  		logging.Warn(err)
   138  	}
   139  	return v
   140  }