This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acl.go
234 lines (212 loc) · 7.21 KB
/
acl.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
234
// Code generated by adapter-generator. DO NOT EDIT.
package adapter
import (
"github.com/gogo/protobuf/proto"
"github.com/ligato/vpp-agent/api/models/vpp/acl"
. "github.com/ligato/vpp-agent/plugins/kvscheduler/api"
"github.com/ligato/vpp-agent/plugins/vpp/aclplugin/aclidx"
)
////////// type-safe key-value pair with metadata //////////
type ACLKVWithMetadata struct {
Key string
Value *vpp_acl.ACL
Metadata *aclidx.ACLMetadata
Origin ValueOrigin
}
////////// type-safe Descriptor structure //////////
type ACLDescriptor struct {
Name string
KeySelector KeySelector
ValueTypeName string
KeyLabel func(key string) string
ValueComparator func(key string, oldValue, newValue *vpp_acl.ACL) bool
NBKeyPrefix string
WithMetadata bool
MetadataMapFactory MetadataMapFactory
Validate func(key string, value *vpp_acl.ACL) error
Create func(key string, value *vpp_acl.ACL) (metadata *aclidx.ACLMetadata, err error)
Delete func(key string, value *vpp_acl.ACL, metadata *aclidx.ACLMetadata) error
Update func(key string, oldValue, newValue *vpp_acl.ACL, oldMetadata *aclidx.ACLMetadata) (newMetadata *aclidx.ACLMetadata, err error)
UpdateWithRecreate func(key string, oldValue, newValue *vpp_acl.ACL, metadata *aclidx.ACLMetadata) bool
Retrieve func(correlate []ACLKVWithMetadata) ([]ACLKVWithMetadata, error)
IsRetriableFailure func(err error) bool
DerivedValues func(key string, value *vpp_acl.ACL) []KeyValuePair
Dependencies func(key string, value *vpp_acl.ACL) []Dependency
RetrieveDependencies []string /* descriptor name */
}
////////// Descriptor adapter //////////
type ACLDescriptorAdapter struct {
descriptor *ACLDescriptor
}
func NewACLDescriptor(typedDescriptor *ACLDescriptor) *KVDescriptor {
adapter := &ACLDescriptorAdapter{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 *ACLDescriptorAdapter) ValueComparator(key string, oldValue, newValue proto.Message) bool {
typedOldValue, err1 := castACLValue(key, oldValue)
typedNewValue, err2 := castACLValue(key, newValue)
if err1 != nil || err2 != nil {
return false
}
return da.descriptor.ValueComparator(key, typedOldValue, typedNewValue)
}
func (da *ACLDescriptorAdapter) Validate(key string, value proto.Message) (err error) {
typedValue, err := castACLValue(key, value)
if err != nil {
return err
}
return da.descriptor.Validate(key, typedValue)
}
func (da *ACLDescriptorAdapter) Create(key string, value proto.Message) (metadata Metadata, err error) {
typedValue, err := castACLValue(key, value)
if err != nil {
return nil, err
}
return da.descriptor.Create(key, typedValue)
}
func (da *ACLDescriptorAdapter) Update(key string, oldValue, newValue proto.Message, oldMetadata Metadata) (newMetadata Metadata, err error) {
oldTypedValue, err := castACLValue(key, oldValue)
if err != nil {
return nil, err
}
newTypedValue, err := castACLValue(key, newValue)
if err != nil {
return nil, err
}
typedOldMetadata, err := castACLMetadata(key, oldMetadata)
if err != nil {
return nil, err
}
return da.descriptor.Update(key, oldTypedValue, newTypedValue, typedOldMetadata)
}
func (da *ACLDescriptorAdapter) Delete(key string, value proto.Message, metadata Metadata) error {
typedValue, err := castACLValue(key, value)
if err != nil {
return err
}
typedMetadata, err := castACLMetadata(key, metadata)
if err != nil {
return err
}
return da.descriptor.Delete(key, typedValue, typedMetadata)
}
func (da *ACLDescriptorAdapter) UpdateWithRecreate(key string, oldValue, newValue proto.Message, metadata Metadata) bool {
oldTypedValue, err := castACLValue(key, oldValue)
if err != nil {
return true
}
newTypedValue, err := castACLValue(key, newValue)
if err != nil {
return true
}
typedMetadata, err := castACLMetadata(key, metadata)
if err != nil {
return true
}
return da.descriptor.UpdateWithRecreate(key, oldTypedValue, newTypedValue, typedMetadata)
}
func (da *ACLDescriptorAdapter) Retrieve(correlate []KVWithMetadata) ([]KVWithMetadata, error) {
var correlateWithType []ACLKVWithMetadata
for _, kvpair := range correlate {
typedValue, err := castACLValue(kvpair.Key, kvpair.Value)
if err != nil {
continue
}
typedMetadata, err := castACLMetadata(kvpair.Key, kvpair.Metadata)
if err != nil {
continue
}
correlateWithType = append(correlateWithType,
ACLKVWithMetadata{
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 *ACLDescriptorAdapter) DerivedValues(key string, value proto.Message) []KeyValuePair {
typedValue, err := castACLValue(key, value)
if err != nil {
return nil
}
return da.descriptor.DerivedValues(key, typedValue)
}
func (da *ACLDescriptorAdapter) Dependencies(key string, value proto.Message) []Dependency {
typedValue, err := castACLValue(key, value)
if err != nil {
return nil
}
return da.descriptor.Dependencies(key, typedValue)
}
////////// Helper methods //////////
func castACLValue(key string, value proto.Message) (*vpp_acl.ACL, error) {
typedValue, ok := value.(*vpp_acl.ACL)
if !ok {
return nil, ErrInvalidValueType(key, value)
}
return typedValue, nil
}
func castACLMetadata(key string, metadata Metadata) (*aclidx.ACLMetadata, error) {
if metadata == nil {
return nil, nil
}
typedMetadata, ok := metadata.(*aclidx.ACLMetadata)
if !ok {
return nil, ErrInvalidMetadataType(key)
}
return typedMetadata, nil
}