go.ligato.io/vpp-agent/v3@v3.5.0/examples/customize/custom_vpp_plugin/main.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  // Example Custom VPP plugin contains a working example of custom agent which
    16  // adds support for a custom VPP plugin. This example can serve as a skeleton
    17  // code for developing custom agents adding new VPP functionality that is not
    18  // part of official VPP Agent.
    19  package main
    20  
    21  import (
    22  	"log"
    23  
    24  	"go.ligato.io/cn-infra/v2/agent"
    25  	"go.ligato.io/cn-infra/v2/datasync"
    26  	"go.ligato.io/cn-infra/v2/datasync/kvdbsync"
    27  	"go.ligato.io/cn-infra/v2/datasync/kvdbsync/local"
    28  	"go.ligato.io/cn-infra/v2/datasync/resync"
    29  	"go.ligato.io/cn-infra/v2/db/keyval/etcd"
    30  	"go.ligato.io/cn-infra/v2/health/statuscheck"
    31  	"go.ligato.io/cn-infra/v2/infra"
    32  
    33  	"go.ligato.io/vpp-agent/v3/cmd/vpp-agent/app"
    34  	"go.ligato.io/vpp-agent/v3/examples/customize/custom_vpp_plugin/syslog"
    35  	"go.ligato.io/vpp-agent/v3/plugins/orchestrator"
    36  )
    37  
    38  // This go generate directive will generate Go code for Proto definition.
    39  //go:generate protoc --proto_path=. --go_out=paths=source_relative:. proto/custom/vpp/syslog/syslog.proto
    40  
    41  func main() {
    42  	example := NewExample()
    43  
    44  	a := agent.NewAgent(
    45  		agent.AllPlugins(example),
    46  	)
    47  	if err := a.Run(); err != nil {
    48  		log.Fatal(err)
    49  	}
    50  }
    51  
    52  type Example struct {
    53  	infra.PluginDeps
    54  	app.VPP
    55  	Syslog       *syslog.SyslogPlugin
    56  	Orchestrator *orchestrator.Plugin
    57  }
    58  
    59  func NewExample() *Example {
    60  	example := &Example{
    61  		VPP:          app.DefaultVPP(),
    62  		Syslog:       syslog.NewSyslogPlugin(),
    63  		Orchestrator: &orchestrator.DefaultPlugin,
    64  	}
    65  	example.SetName("custom-vpp-plugin-example")
    66  	example.SetupLog()
    67  
    68  	etcdDataSync := kvdbsync.NewPlugin(kvdbsync.UseKV(&etcd.DefaultPlugin))
    69  	statuscheck.DefaultPlugin.Transport = etcdDataSync
    70  
    71  	watchers := datasync.KVProtoWatchers{
    72  		local.DefaultRegistry,
    73  		etcdDataSync,
    74  	}
    75  	example.Orchestrator.Watcher = watchers
    76  	example.Orchestrator.StatusPublisher = etcdDataSync
    77  	example.VPP.IfPlugin.DataSyncs = map[string]datasync.KeyProtoValWriter{
    78  		"etcd": etcdDataSync,
    79  	}
    80  
    81  	return example
    82  }
    83  
    84  func (p *Example) Init() error {
    85  	return nil
    86  }
    87  
    88  func (p *Example) AfterInit() error {
    89  	resync.DefaultPlugin.DoResync()
    90  	return nil
    91  }