go.ligato.io/vpp-agent/v3@v3.5.0/examples/kvscheduler/mock_plugins/main.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 main
    16  
    17  import (
    18  	"log"
    19  
    20  	"go.ligato.io/cn-infra/v2/agent"
    21  
    22  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler"
    23  	"go.ligato.io/vpp-agent/v3/plugins/orchestrator"
    24  
    25  	"go.ligato.io/cn-infra/v2/logging"
    26  
    27  	mock_ifplugin "go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/ifplugin"
    28  	mock_l2plugin "go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/l2plugin"
    29  	"go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/scenario"
    30  )
    31  
    32  /*
    33  	This is a simple example for demonstrating kvscheduler with mock plugins.
    34  */
    35  func main() {
    36  	exampleAgent := &ExampleAgent{
    37  		Orchestrator: &orchestrator.DefaultPlugin,
    38  		KVScheduler:  &kvs.DefaultPlugin,
    39  		MockIfPlugin: &mock_ifplugin.DefaultPlugin,
    40  		MockL2Plugin: &mock_l2plugin.DefaultPlugin,
    41  	}
    42  
    43  	a := agent.NewAgent(
    44  		agent.AllPlugins(exampleAgent),
    45  	)
    46  	if err := a.Run(); err != nil {
    47  		log.Fatal(err)
    48  	}
    49  }
    50  
    51  // ExampleAgent is an example agent based on mock plugins demonstrating
    52  // the KVScheduler framework.
    53  type ExampleAgent struct {
    54  	// mock plugins
    55  	MockIfPlugin *mock_ifplugin.IfPlugin
    56  	MockL2Plugin *mock_l2plugin.L2Plugin
    57  
    58  	// agent core infrastructure - must be listed AFTER plugins
    59  	KVScheduler  *kvs.Scheduler
    60  	Orchestrator *orchestrator.Plugin
    61  }
    62  
    63  // String returns plugin name
    64  func (a *ExampleAgent) String() string {
    65  	return "example-agent"
    66  }
    67  
    68  // Init handles initialization phase.
    69  func (a *ExampleAgent) Init() error {
    70  	return nil
    71  }
    72  
    73  // AfterInit handles the phase after initialization.
    74  func (a *ExampleAgent) AfterInit() error {
    75  	go scenario.Run(a.KVScheduler, func(debugMode bool) {
    76  		if debugMode {
    77  			a.KVScheduler.Log.SetLevel(logging.DebugLevel)
    78  		} else {
    79  			a.Orchestrator.Log.SetLevel(logging.ErrorLevel)
    80  			a.MockIfPlugin.Log.SetLevel(logging.ErrorLevel)
    81  			a.MockL2Plugin.Log.SetLevel(logging.ErrorLevel)
    82  			logging.DefaultRegistry.SetLevel(
    83  				a.Orchestrator.String()+".dispatcher", logging.ErrorLevel.String())
    84  		}
    85  	})
    86  	return nil
    87  }
    88  
    89  // Close cleans up the resources.
    90  func (a *ExampleAgent) Close() error {
    91  	return nil
    92  }