go.ligato.io/vpp-agent/v3@v3.5.0/examples/kvscheduler/plugin_skeleton/with_metadata/plugin.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  /*******************************************************************************
    16   Feel free to use this skeleton of a VPP-Agent plugin with a single descriptor
    17   and a metadata index-map to build your own plugin in 6 steps:
    18    1. for each of your value types (e.g. interface, route, ...), define a separate
    19       protobuf message to carry the value data:
    20        - use the attached bare value definition, provided in model/model.proto
    21          as "Value", to start from
    22        - rename the proto message and the file appropriately, such that it is
    23          clear to which value type it belongs
    24        - for each of your value types, write a separate proto file
    25        - replicate and update the go:generate comment below (*) for each of your
    26          proto messages
    27        - when done, re-generate the golang code for the proto messages with
    28          `go generate .`
    29    2. define model for each of your value types in model/keys.go, which will be
    30       then used by the agent and the descriptor to build and parse keys for value
    31       instances
    32    3. use "metaidx" as a template for implementing your own index maps with
    33       secondary lookups over your metadata
    34        - note: if you only to keep an integer SB handle with each value instance,
    35                you may instead re-use the ligato/vpp-agent/pkg/idxvpp map, even
    36                if SB is not actually VPP (metadata type is a structure called
    37                "OnlyIndex") - for an example see descriptor for VPP bridge domains,
    38                located in ligato/vpp-agent/plugins/vpp/l2plugin/descriptor
    39    4. for each of your value types, including derived types (with the exception
    40       of derived properties), implement one descriptor in the "descriptor"
    41       sub-package:
    42        - first replicate and update the go:generate comment below (**) to generate
    43          adapter for every descriptor to be implemented
    44        - generate new/updated adapters with `go generate .`
    45          (don't forget to remove skeleton.go adapter attached as an example)
    46        - use the attached skeleton descriptor to start from
    47        - please rename the descriptor(s) and the file(s), such that it is clear
    48          to which value type each of them belongs
    49        - implement CRUD methods and other attributes of the descriptor(s)
    50        - please remove callbacks/attributes which are not needed/relevant, or
    51          for which the default behaviour is sufficient
    52     5. register all your descriptors with KVScheduler in the Init method of the
    53        plugin
    54     6. finally, remove comments whose purpose was solely to guide you through
    55        the process of implementing a new plugin - like this one
    56        - make sure the remaining comments have no mention of the word "skeleton"
    57  
    58  Beware: Extensive copy-pasting is actually a bad practise, so use the skeleton
    59          with caution and eventually learn how to write your own plugins from the
    60          scratch, using the skeleton only as a reference.
    61  *******************************************************************************/
    62  
    63  // (*) generate golang code from your protobuf models here:
    64  //go:generate protoc --proto_path=. --go_out=paths=source_relative:. model/model.proto
    65  
    66  // (**) generate adapter(s) for your descriptor(s) here:
    67  //go:generate descriptor-adapter --descriptor-name Skeleton --value-type *model.ValueSkeleton --meta-type *metaidx.SkeletonMetadata --import "go.ligato.io/vpp-agent/v3/examples/kvscheduler/plugin_skeleton/with_metadata/model" --import "go.ligato.io/vpp-agent/v3/examples/kvscheduler/plugin_skeleton/with_metadata/metaidx" --output-dir "descriptor"
    68  
    69  package plugin
    70  
    71  import (
    72  	"go.ligato.io/cn-infra/v2/infra"
    73  
    74  	"go.ligato.io/vpp-agent/v3/examples/kvscheduler/plugin_skeleton/with_metadata/descriptor"
    75  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
    76  )
    77  
    78  // SkeletonPlugin is a plugin skeleton that you can start building your own plugins
    79  // from.
    80  type SkeletonPlugin struct {
    81  	Deps
    82  }
    83  
    84  // Deps lists dependencies of the mock interface plugin.
    85  type Deps struct {
    86  	infra.PluginDeps
    87  
    88  	// the plugin depends on KVScheduler because it needs to register the descriptor(s)
    89  	KVScheduler kvs.KVScheduler
    90  }
    91  
    92  // Init method usually:
    93  //  - loads configuration from a file (if any)
    94  //  - registers descriptors for all objects the plugin implements
    95  //  - potentially starts go routine to watch for some asynchronous events
    96  //    (from which usually sends notifications to KVScheduler via PushSBNotification)
    97  //  - etc.
    98  func (p *SkeletonPlugin) Init() error {
    99  	var err error
   100  
   101  	// init & register descriptor(s) here:
   102  	skeletonDescriptor := descriptor.NewSkeletonDescriptor(p.Log)
   103  	err = p.KVScheduler.RegisterKVDescriptor(skeletonDescriptor)
   104  	if err != nil {
   105  		return err
   106  	}
   107  
   108  	return nil
   109  }
   110  
   111  // Close method usually:
   112  //  - stops all the associated go routines (if any)
   113  //  - closes channels, registrations, etc..
   114  // Note: it is not needed to un-register descriptors - there is no method for
   115  //       that anyway
   116  func (p *SkeletonPlugin) Close() error {
   117  	return nil
   118  }