go.ligato.io/vpp-agent/v3@v3.5.0/examples/kvscheduler/plugin_skeleton/with_metadata/metaidx/metaidx.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 metaidx 16 17 import ( 18 "time" 19 20 "go.ligato.io/cn-infra/v2/idxmap" 21 "go.ligato.io/cn-infra/v2/logging" 22 23 "go.ligato.io/vpp-agent/v3/pkg/idxvpp" 24 ) 25 26 // SkeletonMetadataIndex provides read-only access to mapping between skeleton values 27 // and their metadata. 28 type SkeletonMetadataIndex interface { 29 // LookupName looks up previously stored item identified by name in the mapping. 30 LookupByName(name string) (metadata *SkeletonMetadata, exists bool) 31 32 // TODO: define additional secondary lookups here ... 33 34 // WatchSkeletonMetadata allows to watch for changes in the mapping. 35 WatchSkeletonMetadata(subscriber string, channel chan<- SkeletonMetadataDto) 36 } 37 38 // SkeletonMetadataIndexRW is a mapping between skeleton values and their metadata. 39 type SkeletonMetadataIndexRW interface { 40 SkeletonMetadataIndex 41 idxmap.NamedMappingRW 42 } 43 44 // SkeletonMetadata represents metadata for skeleton value. 45 type SkeletonMetadata struct { 46 // TODO: put metadata attributes here ... 47 } 48 49 // SkeletonMetadataDto represents an item sent through a watch channel. 50 type SkeletonMetadataDto struct { 51 idxmap.NamedMappingEvent 52 Metadata *SkeletonMetadata 53 } 54 55 // skeletonMetadataIndex is an implementation of SkeletonMetadataIndexRW. 56 type skeletonMetadataIndex struct { 57 idxmap.NamedMappingRW 58 log logging.Logger 59 } 60 61 // NewSkeletonIndex creates new instance of skeletonMetadataIndex. 62 func NewSkeletonIndex(logger logging.Logger, title string) SkeletonMetadataIndexRW { 63 mapping := idxvpp.NewNameToIndex(logger, title, indexMetadata) 64 return &skeletonMetadataIndex{ 65 NamedMappingRW: mapping, 66 log: logger, 67 } 68 } 69 70 // LookupName looks up previously stored item identified by name in mapping. 71 func (idx *skeletonMetadataIndex) LookupByName(name string) (metadata *SkeletonMetadata, exists bool) { 72 meta, found := idx.GetValue(name) 73 if found { 74 if typedMeta, ok := meta.(*SkeletonMetadata); ok { 75 return typedMeta, found 76 } 77 } 78 return nil, false 79 } 80 81 // TODO: add secondary lookup functions here... 82 83 // WatchSkeletonMetadata allows to watch for changes in the mapping. 84 func (idx *skeletonMetadataIndex) WatchSkeletonMetadata(subscriber string, channel chan<- SkeletonMetadataDto) { 85 watcher := func(dto idxmap.NamedMappingGenericEvent) { 86 typedMeta, ok := dto.Value.(*SkeletonMetadata) 87 if !ok { 88 return 89 } 90 msg := SkeletonMetadataDto{ 91 NamedMappingEvent: dto.NamedMappingEvent, 92 Metadata: typedMeta, 93 } 94 select { 95 case channel <- msg: 96 case <-time.After(idxmap.DefaultNotifTimeout): 97 idx.log.Warn("Unable to deliver notification") 98 } 99 } 100 if err := idx.Watch(subscriber, watcher); err != nil { 101 idx.log.Error(err) 102 } 103 } 104 105 // indexMetadata is an index function used for skeleton metadata. 106 func indexMetadata(metadata interface{}) map[string][]string { 107 indexes := make(map[string][]string) 108 109 // TODO: define secondary indexes here ... 110 111 return indexes 112 }