go.ligato.io/vpp-agent/v3@v3.5.0/examples/kvscheduler/plugin_skeleton/with_metadata/descriptor/skeleton.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 descriptor
    16  
    17  import (
    18  	"go.ligato.io/cn-infra/v2/idxmap"
    19  	"go.ligato.io/cn-infra/v2/logging"
    20  
    21  	"go.ligato.io/vpp-agent/v3/examples/kvscheduler/plugin_skeleton/with_metadata/descriptor/adapter"
    22  	"go.ligato.io/vpp-agent/v3/examples/kvscheduler/plugin_skeleton/with_metadata/metaidx"
    23  	"go.ligato.io/vpp-agent/v3/examples/kvscheduler/plugin_skeleton/with_metadata/model"
    24  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
    25  )
    26  
    27  const (
    28  	// SkeletonDescriptorName is the name of the descriptor skeleton.
    29  	SKeletonDescriptorName = "skeleton"
    30  )
    31  
    32  // SkeletonDescriptor is only a skeleton of a descriptor, which can be used
    33  // as a starting point to build a new descriptor from.
    34  type SkeletonDescriptor struct {
    35  	log logging.Logger
    36  }
    37  
    38  // NewSkeletonDescriptor creates a new instance of the descriptor.
    39  func NewSkeletonDescriptor(log logging.PluginLogger) *kvs.KVDescriptor {
    40  	// descriptors are supposed to be stateless, so use the structure only
    41  	// as a context for things that do not change once the descriptor is
    42  	// constructed - e.g. a reference to the logger to use within the descriptor
    43  	descrCtx := &SkeletonDescriptor{
    44  		log: log.NewLogger("skeleton-descriptor"),
    45  	}
    46  
    47  	// use adapter to convert typed descriptor into generic descriptor API
    48  	typedDescr := &adapter.SkeletonDescriptor{
    49  		Name:                 SKeletonDescriptorName,
    50  		NBKeyPrefix:          model.ValueModel.KeyPrefix(),
    51  		ValueTypeName:        model.ValueModel.ProtoName(),
    52  		KeySelector:          model.ValueModel.IsKeyValid,
    53  		KeyLabel:             model.ValueModel.StripKeyPrefix,
    54  		WithMetadata:         true,
    55  		MetadataMapFactory:   descrCtx.MetadataMapFactory,
    56  		ValueComparator:      descrCtx.EquivalentValues,
    57  		Validate:             descrCtx.Validate,
    58  		Create:               descrCtx.Create,
    59  		Delete:               descrCtx.Delete,
    60  		Update:               descrCtx.Update,
    61  		UpdateWithRecreate:   descrCtx.UpdateWithRecreate,
    62  		Retrieve:             descrCtx.Retrieve,
    63  		IsRetriableFailure:   descrCtx.IsRetriableFailure,
    64  		DerivedValues:        descrCtx.DerivedValues,
    65  		Dependencies:         descrCtx.Dependencies,
    66  		RetrieveDependencies: []string{}, // list the names of the descriptors to Retrieve first
    67  	}
    68  	return adapter.NewSkeletonDescriptor(typedDescr)
    69  }
    70  
    71  // EquivalentInterfaces compares two revisions of the same value for equality.
    72  func (d *SkeletonDescriptor) EquivalentValues(key string, old, new *model.ValueSkeleton) bool {
    73  	// compare **non-primary** attributes here (none in the ValueSkeleton)
    74  	return true
    75  }
    76  
    77  // MetadataMapFactory is a factory for index-map with skeleton metadata.
    78  func (d *SkeletonDescriptor) MetadataMapFactory() idxmap.NamedMappingRW {
    79  	return metaidx.NewSkeletonIndex(d.log, "skeleton-index")
    80  }
    81  
    82  // Validate validates value before it is applied.
    83  func (d *SkeletonDescriptor) Validate(key string, value *model.ValueSkeleton) error {
    84  	return nil
    85  }
    86  
    87  // Create creates new value.
    88  func (d *SkeletonDescriptor) Create(key string, value *model.ValueSkeleton) (metadata *metaidx.SkeletonMetadata, err error) {
    89  	return nil, nil
    90  }
    91  
    92  // Delete removes an existing value.
    93  func (d *SkeletonDescriptor) Delete(key string, value *model.ValueSkeleton, metadata *metaidx.SkeletonMetadata) error {
    94  	return nil
    95  }
    96  
    97  // Update updates existing value.
    98  func (d *SkeletonDescriptor) Update(key string, old, new *model.ValueSkeleton, oldMetadata *metaidx.SkeletonMetadata) (newMetadata *metaidx.SkeletonMetadata, err error) {
    99  	return nil, nil
   100  }
   101  
   102  // UpdateWithRecreate returns true if value update requires full re-creation.
   103  func (d *SkeletonDescriptor) UpdateWithRecreate(key string, old, new *model.ValueSkeleton, metadata *metaidx.SkeletonMetadata) bool {
   104  	return false
   105  }
   106  
   107  // Retrieve retrieves values from SB.
   108  func (d *SkeletonDescriptor) Retrieve(correlate []adapter.SkeletonKVWithMetadata) (retrieved []adapter.SkeletonKVWithMetadata, err error) {
   109  	return retrieved, nil
   110  }
   111  
   112  // IsRetriableFailure returns true if the given error, returned by one of the CRUD
   113  // operations, can be theoretically fixed by merely repeating the operation.
   114  func (d *SkeletonDescriptor) IsRetriableFailure(err error) bool {
   115  	return true
   116  }
   117  
   118  // DerivedValues breaks the value into multiple part handled/referenced
   119  // separately.
   120  func (d *SkeletonDescriptor) DerivedValues(key string, value *model.ValueSkeleton) (derived []kvs.KeyValuePair) {
   121  	return derived
   122  }
   123  
   124  // Dependencies lists dependencies of the given value.
   125  func (d *SkeletonDescriptor) Dependencies(key string, value *model.ValueSkeleton) (deps []kvs.Dependency) {
   126  	return deps
   127  }