go.ligato.io/vpp-agent/v3@v3.5.0/examples/kvscheduler/mock_plugins/l2plugin/l2plugin.go (about)

     1  // Copyright (c) 2018 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  // Generate golang code from the protobuf model of our mock BDs and FIBs:
    16  //go:generate protoc --proto_path=. --go_out=paths=source_relative:. model/bridge-domain.proto
    17  //go:generate protoc --proto_path=. --go_out=paths=source_relative:. model/fib.proto
    18  
    19  // Generate adapters for the descriptors of our mock BDs and FIBs:
    20  //go:generate descriptor-adapter --descriptor-name BridgeDomain --value-type *mock_l2.BridgeDomain --meta-type *idxvpp.OnlyIndex --import "go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/l2plugin/model" --import "go.ligato.io/vpp-agent/v3/pkg/idxvpp" --output-dir "descriptor"
    21  //go:generate descriptor-adapter --descriptor-name BDInterface --value-type *mock_l2.BridgeDomain_Interface --import "go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/l2plugin/model" --output-dir "descriptor"
    22  //go:generate descriptor-adapter --descriptor-name FIB  --value-type *mock_l2.FIBEntry --import "go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/l2plugin/model" --output-dir "descriptor"
    23  
    24  package ifplugin
    25  
    26  import (
    27  	"github.com/pkg/errors"
    28  
    29  	"go.ligato.io/cn-infra/v2/infra"
    30  
    31  	"go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/ifplugin"
    32  	"go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/l2plugin/descriptor"
    33  	"go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/l2plugin/mockcalls"
    34  	"go.ligato.io/vpp-agent/v3/pkg/idxvpp"
    35  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
    36  )
    37  
    38  // L2Plugin configures mock bridge domains and L2 FIBs.
    39  type L2Plugin struct {
    40  	Deps
    41  
    42  	// access to our mock SB
    43  	// - it is recommended to put implementation of every SB call needed for
    44  	//   your descriptor into a separate package `<southband-name>calls/` and
    45  	//   expose them via interface. This will allow to replace access to SB with
    46  	//   mocks and make unit testing easier (in our example these calls are
    47  	//   already mocks)
    48  	bdHandler  mockcalls.MockBDAPI
    49  	fibHandler mockcalls.MockFIBAPI
    50  
    51  	// descriptors for BDs and L2 FIBs
    52  	bdDescriptor      *descriptor.BridgeDomainDescriptor
    53  	bdIfaceDescriptor *descriptor.BDInterfaceDescriptor // for derived bindings between BD and interfaces
    54  	fibDescriptor     *descriptor.FIBDescriptor
    55  
    56  	// metadata index map (exposed read-only for other plugins)
    57  	bdIndex idxvpp.NameToIndex
    58  }
    59  
    60  // Deps lists dependencies of the mock interface plugin.
    61  type Deps struct {
    62  	infra.PluginDeps
    63  
    64  	// the plugin depends on KVScheduler because it needs to register the
    65  	// descriptors for BDs and FIBs.
    66  	KVScheduler kvs.KVScheduler
    67  
    68  	// ifplugin is needed to convert interface name to the corresponding integer
    69  	// handle used in the mock SB
    70  	IfPlugin ifplugin.API
    71  }
    72  
    73  // Init of a real (not-mock) plugin usually:
    74  //  - loads configuration from a file (if any)
    75  //  - registers descriptors for all objects the plugin implements
    76  //  - potentially starts go routine to watch for some asynchronous events
    77  //    (from which usually sends notifications to KVScheduler via PushSBNotification)
    78  //  - etc.
    79  //
    80  // In this mock ifplugin, we only create mock SB handlers and register the descriptors.
    81  func (p *L2Plugin) Init() error {
    82  	var err error
    83  
    84  	// init BD handler
    85  	p.bdHandler = mockcalls.NewMockBDHandler(p.IfPlugin.GetInterfaceIndex(), p.Log)
    86  
    87  	// init & register BD descriptor
    88  	bdDescriptor := descriptor.NewBridgeDomainDescriptor(p.bdHandler, p.Log)
    89  	err = p.KVScheduler.RegisterKVDescriptor(bdDescriptor)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	// obtain read-only reference to index map with bridge domain metadata
    95  	var withIndex bool
    96  	metadataMap := p.KVScheduler.GetMetadataMap(bdDescriptor.Name)
    97  	p.bdIndex, withIndex = metadataMap.(idxvpp.NameToIndex)
    98  	if !withIndex {
    99  		return errors.New("missing index with bridge domain metadata")
   100  	}
   101  
   102  	// init & register descriptor for bindings between bridge domains and interfaces
   103  	bdIfaceDescriptor := descriptor.NewBDInterfaceDescriptor(p.bdIndex, p.bdHandler, p.Log)
   104  	err = p.KVScheduler.RegisterKVDescriptor(bdIfaceDescriptor)
   105  	if err != nil {
   106  		return err
   107  	}
   108  
   109  	// init FIB handler
   110  	p.fibHandler = mockcalls.NewMockFIBHandler(p.IfPlugin.GetInterfaceIndex(), p.bdIndex, p.Log)
   111  
   112  	// init & register descriptor for L2 FIBs
   113  	fibDescriptor := descriptor.NewFIBDescriptor(p.fibHandler, p.Log)
   114  	err = p.KVScheduler.RegisterKVDescriptor(fibDescriptor)
   115  	if err != nil {
   116  		return err
   117  	}
   118  
   119  	return nil
   120  }
   121  
   122  // Close of a real (not-mock) plugin usually:
   123  //  - stops all the associated go routines (if any)
   124  //  - closes channels, registrations, etc..
   125  //
   126  // In this example we do nothing (no need to un-register descriptor).
   127  func (p *L2Plugin) Close() error {
   128  	return nil
   129  }
   130  
   131  // GetBDIndex gives read-only access to map with metadata of all configured
   132  // mock bridge domains.
   133  func (p *L2Plugin) GetBDIndex() idxvpp.NameToIndex {
   134  	return p.bdIndex
   135  }