go.ligato.io/vpp-agent/v3@v3.5.0/examples/customize/custom_vpp_plugin/syslog/syslog_plugin.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 syslog
    16  
    17  import (
    18  	"go.ligato.io/cn-infra/v2/infra"
    19  	"go.ligato.io/cn-infra/v2/logging"
    20  
    21  	"go.ligato.io/vpp-agent/v3/examples/customize/custom_vpp_plugin/syslog/descriptor"
    22  	"go.ligato.io/vpp-agent/v3/examples/customize/custom_vpp_plugin/syslog/descriptor/adapter"
    23  	"go.ligato.io/vpp-agent/v3/examples/customize/custom_vpp_plugin/syslog/vppcalls"
    24  	"go.ligato.io/vpp-agent/v3/plugins/govppmux"
    25  	"go.ligato.io/vpp-agent/v3/plugins/kvscheduler"
    26  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
    27  
    28  	// This blank import registers vppcalls implementation for VPP 20.05
    29  	_ "go.ligato.io/vpp-agent/v3/examples/customize/custom_vpp_plugin/syslog/vppcalls/vpp2106"
    30  )
    31  
    32  // This go generate directive generates adapter code for conversion between
    33  // generic KV descriptor API and Syslog model API defined in proto.
    34  //go:generate descriptor-adapter --output-dir descriptor --descriptor-name SyslogSender --value-type *vpp_syslog.Sender --import "go.ligato.io/vpp-agent/v3/examples/customize/custom_vpp_plugin/proto/custom/vpp/syslog"
    35  
    36  // SyslogPlugin is a Ligato plugin that initializes vppcalls handler and
    37  // registers KV descriptors providing API to VPP plugin syslog.
    38  type SyslogPlugin struct {
    39  	Deps
    40  
    41  	handler vppcalls.SyslogVppAPI
    42  
    43  	senderDescriptor *descriptor.SenderDescriptor
    44  }
    45  
    46  type Deps struct {
    47  	infra.PluginDeps
    48  	KVScheduler kvs.KVScheduler
    49  	VPP         govppmux.API
    50  }
    51  
    52  func NewSyslogPlugin() *SyslogPlugin {
    53  	p := &SyslogPlugin{}
    54  	p.PluginName = "vpp-syslog-plugin"
    55  	p.KVScheduler = &kvscheduler.DefaultPlugin
    56  	p.VPP = &govppmux.DefaultPlugin
    57  	p.Log = logging.ForPlugin(p.String())
    58  	return p
    59  }
    60  
    61  func (p *SyslogPlugin) Init() (err error) {
    62  	// Get compatible VPP calls handler
    63  	p.handler = vppcalls.CompatibleVppHandler(p.VPP, p.Log)
    64  
    65  	// Init and register KV descriptor
    66  	p.senderDescriptor = descriptor.NewSenderDescriptor(p.handler, p.Log)
    67  	senderDescriptor := adapter.NewSyslogSenderDescriptor(p.senderDescriptor.GetDescriptor())
    68  	err = p.KVScheduler.RegisterKVDescriptor(senderDescriptor)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	return nil
    74  }