forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xconnect.go
233 lines (211 loc) · 7.36 KB
/
xconnect.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Code generated by adapter-generator. DO NOT EDIT.
package adapter
import (
"github.com/gogo/protobuf/proto"
. "github.com/ligato/vpp-agent/plugins/kvscheduler/api"
"github.com/ligato/vpp-agent/api/models/vpp/l2"
)
////////// type-safe key-value pair with metadata //////////
type XConnectKVWithMetadata struct {
Key string
Value *vpp_l2.XConnectPair
Metadata interface{}
Origin ValueOrigin
}
////////// type-safe Descriptor structure //////////
type XConnectDescriptor struct {
Name string
KeySelector KeySelector
ValueTypeName string
KeyLabel func(key string) string
ValueComparator func(key string, oldValue, newValue *vpp_l2.XConnectPair) bool
NBKeyPrefix string
WithMetadata bool
MetadataMapFactory MetadataMapFactory
Validate func(key string, value *vpp_l2.XConnectPair) error
Create func(key string, value *vpp_l2.XConnectPair) (metadata interface{}, err error)
Delete func(key string, value *vpp_l2.XConnectPair, metadata interface{}) error
Update func(key string, oldValue, newValue *vpp_l2.XConnectPair, oldMetadata interface{}) (newMetadata interface{}, err error)
UpdateWithRecreate func(key string, oldValue, newValue *vpp_l2.XConnectPair, metadata interface{}) bool
Retrieve func(correlate []XConnectKVWithMetadata) ([]XConnectKVWithMetadata, error)
IsRetriableFailure func(err error) bool
DerivedValues func(key string, value *vpp_l2.XConnectPair) []KeyValuePair
Dependencies func(key string, value *vpp_l2.XConnectPair) []Dependency
RetrieveDependencies []string /* descriptor name */
}
////////// Descriptor adapter //////////
type XConnectDescriptorAdapter struct {
descriptor *XConnectDescriptor
}
func NewXConnectDescriptor(typedDescriptor *XConnectDescriptor) *KVDescriptor {
adapter := &XConnectDescriptorAdapter{descriptor: typedDescriptor}
descriptor := &KVDescriptor{
Name: typedDescriptor.Name,
KeySelector: typedDescriptor.KeySelector,
ValueTypeName: typedDescriptor.ValueTypeName,
KeyLabel: typedDescriptor.KeyLabel,
NBKeyPrefix: typedDescriptor.NBKeyPrefix,
WithMetadata: typedDescriptor.WithMetadata,
MetadataMapFactory: typedDescriptor.MetadataMapFactory,
IsRetriableFailure: typedDescriptor.IsRetriableFailure,
RetrieveDependencies: typedDescriptor.RetrieveDependencies,
}
if typedDescriptor.ValueComparator != nil {
descriptor.ValueComparator = adapter.ValueComparator
}
if typedDescriptor.Validate != nil {
descriptor.Validate = adapter.Validate
}
if typedDescriptor.Create != nil {
descriptor.Create = adapter.Create
}
if typedDescriptor.Delete != nil {
descriptor.Delete = adapter.Delete
}
if typedDescriptor.Update != nil {
descriptor.Update = adapter.Update
}
if typedDescriptor.UpdateWithRecreate != nil {
descriptor.UpdateWithRecreate = adapter.UpdateWithRecreate
}
if typedDescriptor.Retrieve != nil {
descriptor.Retrieve = adapter.Retrieve
}
if typedDescriptor.Dependencies != nil {
descriptor.Dependencies = adapter.Dependencies
}
if typedDescriptor.DerivedValues != nil {
descriptor.DerivedValues = adapter.DerivedValues
}
return descriptor
}
func (da *XConnectDescriptorAdapter) ValueComparator(key string, oldValue, newValue proto.Message) bool {
typedOldValue, err1 := castXConnectValue(key, oldValue)
typedNewValue, err2 := castXConnectValue(key, newValue)
if err1 != nil || err2 != nil {
return false
}
return da.descriptor.ValueComparator(key, typedOldValue, typedNewValue)
}
func (da *XConnectDescriptorAdapter) Validate(key string, value proto.Message) (err error) {
typedValue, err := castXConnectValue(key, value)
if err != nil {
return err
}
return da.descriptor.Validate(key, typedValue)
}
func (da *XConnectDescriptorAdapter) Create(key string, value proto.Message) (metadata Metadata, err error) {
typedValue, err := castXConnectValue(key, value)
if err != nil {
return nil, err
}
return da.descriptor.Create(key, typedValue)
}
func (da *XConnectDescriptorAdapter) Update(key string, oldValue, newValue proto.Message, oldMetadata Metadata) (newMetadata Metadata, err error) {
oldTypedValue, err := castXConnectValue(key, oldValue)
if err != nil {
return nil, err
}
newTypedValue, err := castXConnectValue(key, newValue)
if err != nil {
return nil, err
}
typedOldMetadata, err := castXConnectMetadata(key, oldMetadata)
if err != nil {
return nil, err
}
return da.descriptor.Update(key, oldTypedValue, newTypedValue, typedOldMetadata)
}
func (da *XConnectDescriptorAdapter) Delete(key string, value proto.Message, metadata Metadata) error {
typedValue, err := castXConnectValue(key, value)
if err != nil {
return err
}
typedMetadata, err := castXConnectMetadata(key, metadata)
if err != nil {
return err
}
return da.descriptor.Delete(key, typedValue, typedMetadata)
}
func (da *XConnectDescriptorAdapter) UpdateWithRecreate(key string, oldValue, newValue proto.Message, metadata Metadata) bool {
oldTypedValue, err := castXConnectValue(key, oldValue)
if err != nil {
return true
}
newTypedValue, err := castXConnectValue(key, newValue)
if err != nil {
return true
}
typedMetadata, err := castXConnectMetadata(key, metadata)
if err != nil {
return true
}
return da.descriptor.UpdateWithRecreate(key, oldTypedValue, newTypedValue, typedMetadata)
}
func (da *XConnectDescriptorAdapter) Retrieve(correlate []KVWithMetadata) ([]KVWithMetadata, error) {
var correlateWithType []XConnectKVWithMetadata
for _, kvpair := range correlate {
typedValue, err := castXConnectValue(kvpair.Key, kvpair.Value)
if err != nil {
continue
}
typedMetadata, err := castXConnectMetadata(kvpair.Key, kvpair.Metadata)
if err != nil {
continue
}
correlateWithType = append(correlateWithType,
XConnectKVWithMetadata{
Key: kvpair.Key,
Value: typedValue,
Metadata: typedMetadata,
Origin: kvpair.Origin,
})
}
typedValues, err := da.descriptor.Retrieve(correlateWithType)
if err != nil {
return nil, err
}
var values []KVWithMetadata
for _, typedKVWithMetadata := range typedValues {
kvWithMetadata := KVWithMetadata{
Key: typedKVWithMetadata.Key,
Metadata: typedKVWithMetadata.Metadata,
Origin: typedKVWithMetadata.Origin,
}
kvWithMetadata.Value = typedKVWithMetadata.Value
values = append(values, kvWithMetadata)
}
return values, err
}
func (da *XConnectDescriptorAdapter) DerivedValues(key string, value proto.Message) []KeyValuePair {
typedValue, err := castXConnectValue(key, value)
if err != nil {
return nil
}
return da.descriptor.DerivedValues(key, typedValue)
}
func (da *XConnectDescriptorAdapter) Dependencies(key string, value proto.Message) []Dependency {
typedValue, err := castXConnectValue(key, value)
if err != nil {
return nil
}
return da.descriptor.Dependencies(key, typedValue)
}
////////// Helper methods //////////
func castXConnectValue(key string, value proto.Message) (*vpp_l2.XConnectPair, error) {
typedValue, ok := value.(*vpp_l2.XConnectPair)
if !ok {
return nil, ErrInvalidValueType(key, value)
}
return typedValue, nil
}
func castXConnectMetadata(key string, metadata Metadata) (interface{}, error) {
if metadata == nil {
return nil, nil
}
typedMetadata, ok := metadata.(interface{})
if !ok {
return nil, ErrInvalidMetadataType(key)
}
return typedMetadata, nil
}