go.ligato.io/vpp-agent/v3@v3.5.0/examples/kvscheduler/plugin_skeleton/without_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   (variant without metadata) to build your own plugin in 5 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. for each of your value types, including derived types (with the exception
    33       of derived properties), implement one descriptor in the "descriptor"
    34       sub-package:
    35        - first replicate and update the go:generate comment below (**) to generate
    36          adapter for every descriptor to be implemented
    37        - generate new/updated adapters with `go generate .`
    38          (don't forget to remove skeleton.go adapter attached as an example)
    39        - use the attached skeleton descriptor to start from
    40        - please rename the descriptor(s) and the file(s), such that it is clear
    41          to which value type each of them belongs
    42        - implement CRUD methods and other attributes of the descriptor(s)
    43        - please remove callbacks/attributes which are not needed/relevant, or
    44          for which the default behaviour is sufficient
    45     4. register all your descriptors with KVScheduler in the Init method of the
    46        plugin
    47     5. finally, remove comments whose purpose was solely to guide you through
    48        the process of implementing a new plugin - like this one
    49        - make sure the remaining comments have no mention of the word "skeleton"
    50  
    51  Beware: Extensive copy-pasting is actually a bad practise, so use the skeleton
    52          with caution and eventually learn how to write your own plugins from the
    53          scratch, using the skeleton only as a reference.
    54  *******************************************************************************/
    55  
    56  // (*) generate golang code from your protobuf models here:
    57  //go:generate protoc --proto_path=. --go_out=paths=source_relative:. model/model.proto
    58  
    59  // (**) generate adapter(s) for your descriptor(s) here:
    60  //go:generate descriptor-adapter --descriptor-name Skeleton --value-type *model.ValueSkeleton --import "go.ligato.io/vpp-agent/v3/examples/kvscheduler/plugin_skeleton/without_metadata/model" --output-dir "descriptor"
    61  
    62  package plugin
    63  
    64  import (
    65  	"go.ligato.io/cn-infra/v2/infra"
    66  
    67  	"go.ligato.io/vpp-agent/v3/examples/kvscheduler/plugin_skeleton/without_metadata/descriptor"
    68  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
    69  )
    70  
    71  // SkeletonPlugin is a plugin skeleton that you can start building your own plugins
    72  // from.
    73  type SkeletonPlugin struct {
    74  	Deps
    75  }
    76  
    77  // Deps lists dependencies of the mock interface plugin.
    78  type Deps struct {
    79  	infra.PluginDeps
    80  
    81  	// the plugin depends on KVScheduler because it needs to register the descriptor(s)
    82  	KVScheduler kvs.KVScheduler
    83  }
    84  
    85  // Init method usually:
    86  //  - loads configuration from a file (if any)
    87  //  - registers descriptors for all objects the plugin implements
    88  //  - potentially starts go routine to watch for some asynchronous events
    89  //    (from which usually sends notifications to KVScheduler via PushSBNotification)
    90  //  - etc.
    91  func (p *SkeletonPlugin) Init() error {
    92  	var err error
    93  
    94  	// init & register descriptor(s) here:
    95  	skeletonDescriptor := descriptor.NewSkeletonDescriptor(p.Log)
    96  	err = p.KVScheduler.RegisterKVDescriptor(skeletonDescriptor)
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  // Close method usually:
   105  //  - stops all the associated go routines (if any)
   106  //  - closes channels, registrations, etc..
   107  // Note: it is not needed to un-register descriptors - there is no method for
   108  //       that anyway
   109  func (p *SkeletonPlugin) Close() error {
   110  	return nil
   111  }