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