go.ligato.io/vpp-agent/v3@v3.5.0/examples/customize/custom_vpp_plugin/syslog/vppcalls/vppcalls.go (about)

     1  //  Copyright (c) 2020 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  	"net"
    19  
    20  	govppapi "go.fd.io/govpp/api"
    21  	"go.ligato.io/cn-infra/v2/logging"
    22  
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp"
    24  )
    25  
    26  type SenderConfig struct {
    27  	Source    net.IP
    28  	Collector net.IP
    29  	Port      int
    30  }
    31  
    32  // SyslogVppAPI defines VPP handler API in vpp-version agnostic way.
    33  // It cannot not use any generated binary API code directly.
    34  type SyslogVppAPI interface {
    35  	SetSender(sender SenderConfig) error
    36  	GetSender() (*SenderConfig, error)
    37  	DisableSender() error
    38  }
    39  
    40  var handler = vpp.RegisterHandler(vpp.HandlerDesc{
    41  	Name:       "syslog",
    42  	HandlerAPI: (*SyslogVppAPI)(nil),
    43  })
    44  
    45  type NewHandlerFunc func(ch govppapi.Channel, log logging.Logger) SyslogVppAPI
    46  
    47  // AddHandlerVersion is used to register implementations of VPP handler API
    48  // interface for a specific VPP version.
    49  func AddHandlerVersion(version vpp.Version, msgs []govppapi.Message, h NewHandlerFunc) {
    50  	handler.AddVersion(vpp.HandlerVersion{
    51  		Version: version,
    52  		Check: func(c vpp.Client) error {
    53  			ch, err := c.NewAPIChannel()
    54  			if err != nil {
    55  				return err
    56  			}
    57  			return ch.CheckCompatiblity(msgs...)
    58  		},
    59  		NewHandler: func(c vpp.Client, a ...interface{}) vpp.HandlerAPI {
    60  			ch, err := c.NewAPIChannel()
    61  			if err != nil {
    62  				return err
    63  			}
    64  			return h(ch, a[0].(logging.Logger))
    65  		},
    66  	})
    67  }
    68  
    69  // CompatibleVppHandler checks all the registered implementations to find the
    70  // compatible handler implementation.
    71  func CompatibleVppHandler(c vpp.Client, log logging.Logger) SyslogVppAPI {
    72  	if v := handler.FindCompatibleVersion(c); v != nil {
    73  		return v.NewHandler(c, log).(SyslogVppAPI)
    74  	}
    75  	return nil
    76  }