forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inmemory_name_to_idx_mapping.go
170 lines (149 loc) · 4.97 KB
/
inmemory_name_to_idx_mapping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nametoidx
import (
"github.com/ligato/cn-infra/core"
"github.com/ligato/cn-infra/idxmap"
"github.com/ligato/cn-infra/idxmap/mem"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/logging/logroot"
"github.com/ligato/vpp-agent/idxvpp"
"strconv"
"time"
)
const idxKey = "idxKey"
type nameToIdxMeta struct {
// added index
idx uint32
// original user's meta data
meta interface{}
}
type nameToIdxMem struct {
logging.Logger
internal idxmap.NamedMappingRW
}
// NewNameToIdx creates new instance implementing NameToIdxRW. Argument indexFunction might
// be nil if you do not want to use secondary indexes.
func NewNameToIdx(logger logging.Logger, owner core.PluginName, title string,
indexFunction func(interface{}) map[string][]string) idxvpp.NameToIdxRW {
m := nameToIdxMem{}
m.Logger = logger
m.internal = mem.NewNamedMapping(logger, owner, title, func(meta interface{}) map[string][]string {
var idxs map[string][]string
internalMeta, ok := meta.(*nameToIdxMeta)
if !ok {
return nil
}
if indexFunction != nil {
idxs = indexFunction(internalMeta.meta)
}
if idxs == nil {
idxs = map[string][]string{}
}
internal := indexInternalMetadata(meta)
for k, v := range internal {
idxs[k] = v
}
return idxs
})
return &m
}
// RegisterName inserts or updates index and metadata for the given name.
func (mem *nameToIdxMem) RegisterName(name string, idx uint32, metadata interface{}) {
mem.internal.Put(name, &nameToIdxMeta{idx, metadata})
}
// UnregisterName removes data associated with the given name.
func (mem *nameToIdxMem) UnregisterName(name string) (idx uint32, metadata interface{}, found bool) {
meta, found := mem.internal.Delete(name)
if found {
if internalMeta, ok := meta.(*nameToIdxMeta); ok {
return internalMeta.idx, internalMeta.meta, found
}
}
return
}
// GetRegistryTitle returns a name assigned to mapping.
func (mem *nameToIdxMem) GetRegistryTitle() string {
return mem.internal.GetRegistryTitle()
}
// LookupIdx allows to retrieve previously stored index for particular name.
func (mem *nameToIdxMem) LookupIdx(name string) (uint32, interface{}, bool) {
meta, found := mem.internal.GetValue(name)
if found {
if internalMeta, ok := meta.(*nameToIdxMeta); ok {
return internalMeta.idx, internalMeta.meta, found
}
}
return 0, nil, false
}
// LookupName looks up the name associated with the given softwareIfIndex.
func (mem *nameToIdxMem) LookupName(idx uint32) (name string, metadata interface{}, exists bool) {
res := mem.internal.ListNames(idxKey, strconv.FormatUint(uint64(idx), 10))
if len(res) != 1 {
return
}
m, found := mem.internal.GetValue(res[0])
if found {
if internalMeta, ok := m.(*nameToIdxMeta); ok {
return res[0], internalMeta.meta, found
}
}
return
}
func (mem *nameToIdxMem) LookupNameByMetadata(key string, value string) []string {
return mem.internal.ListNames(key, value)
}
// ListNames returns all names in the mapping
func (mem *nameToIdxMem) ListNames() (names []string) {
return mem.internal.ListAllNames()
}
// Watch start monitoring of change in the mapping. When a change occurs the callback is called.
// To receive changes through channel ToChan utility can be used.
func (mem *nameToIdxMem) Watch(subscriber core.PluginName, callback func(idxvpp.NameToIdxDto)) {
watcher := func(dto idxmap.NamedMappingGenericEvent) {
internalMeta, ok := dto.Value.(*nameToIdxMeta)
if !ok {
return
}
msg := idxvpp.NameToIdxDto{
NameToIdxDtoWithoutMeta: idxvpp.NameToIdxDtoWithoutMeta{
NamedMappingEvent: dto.NamedMappingEvent,
Idx: internalMeta.idx},
Metadata: internalMeta.meta,
}
callback(msg)
}
mem.internal.Watch(subscriber, watcher)
}
// ToChan is an utility that allows to received notification through a channel.
// If a notification can not be delivered until timeout, it is dropped.
func ToChan(ch chan idxvpp.NameToIdxDto) func(dto idxvpp.NameToIdxDto) {
return func(dto idxvpp.NameToIdxDto) {
select {
case ch <- dto:
case <-time.After(idxmap.DefaultNotifTimeout):
logroot.StandardLogger().Warn("Unable to deliver notification")
}
}
}
func indexInternalMetadata(metaData interface{}) map[string][]string {
indexes := map[string][]string{}
internalMeta, ok := metaData.(*nameToIdxMeta)
if !ok || internalMeta == nil {
return indexes
}
idx := internalMeta.idx
indexes[idxKey] = []string{strconv.FormatUint(uint64(idx), 10)}
return indexes
}