blob: 2d2c24e8014c396e11a8025222b280135ea59019 [file] [log] [blame]
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package model
18
19import (
Manikkaraj kb1d51442019-07-23 10:41:02 -040020 "context"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040021 "crypto/md5"
22 "errors"
23 "fmt"
Manikkaraj kb1d51442019-07-23 10:41:02 -040024 "github.com/google/uuid"
Scott Bakerf8424cc2019-10-18 11:26:50 -070025 "github.com/opencord/voltha-lib-go/pkg/log"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040026 "reflect"
27 "runtime"
28 "strings"
29 "sync"
30)
31
32// OperationContext holds details on the information used during an operation
33type OperationContext struct {
34 Path string
35 Data interface{}
36 FieldName string
37 ChildKey string
38}
39
40// NewOperationContext instantiates a new OperationContext structure
41func NewOperationContext(path string, data interface{}, fieldName string, childKey string) *OperationContext {
42 oc := &OperationContext{
43 Path: path,
44 Data: data,
45 FieldName: fieldName,
46 ChildKey: childKey,
47 }
48 return oc
49}
50
51// Update applies new data to the context structure
52func (oc *OperationContext) Update(data interface{}) *OperationContext {
53 oc.Data = data
54 return oc
55}
56
57// Proxy holds the information for a specific location with the data model
58type Proxy struct {
Manikkaraj kb1d51442019-07-23 10:41:02 -040059 mutex sync.RWMutex
Matt Jeanneretcab955f2019-04-10 15:45:57 -040060 Root *root
61 Node *node
62 ParentNode *node
63 Path string
64 FullPath string
65 Exclusive bool
66 Callbacks map[CallbackType]map[string]*CallbackTuple
Manikkaraj kb1d51442019-07-23 10:41:02 -040067 operation ProxyOperation
Matt Jeanneretcab955f2019-04-10 15:45:57 -040068}
69
70// NewProxy instantiates a new proxy to a specific location
71func NewProxy(root *root, node *node, parentNode *node, path string, fullPath string, exclusive bool) *Proxy {
72 callbacks := make(map[CallbackType]map[string]*CallbackTuple)
73 if fullPath == "/" {
74 fullPath = ""
75 }
76 p := &Proxy{
77 Root: root,
78 Node: node,
79 ParentNode: parentNode,
80 Exclusive: exclusive,
81 Path: path,
82 FullPath: fullPath,
83 Callbacks: callbacks,
84 }
85 return p
86}
87
88// GetRoot returns the root attribute of the proxy
89func (p *Proxy) GetRoot() *root {
90 return p.Root
91}
92
93// getPath returns the path attribute of the proxy
94func (p *Proxy) getPath() string {
95 return p.Path
96}
97
98// getFullPath returns the full path attribute of the proxy
99func (p *Proxy) getFullPath() string {
100 return p.FullPath
101}
102
103// getCallbacks returns the full list of callbacks associated to the proxy
104func (p *Proxy) getCallbacks(callbackType CallbackType) map[string]*CallbackTuple {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400105 p.mutex.RLock()
106 defer p.mutex.RUnlock()
107
Mahir Gunyele77977b2019-06-27 05:36:22 -0700108 if p != nil {
109 if cb, exists := p.Callbacks[callbackType]; exists {
110 return cb
111 }
112 } else {
113 log.Debugw("proxy-is-nil", log.Fields{"callback-type": callbackType.String()})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400114 }
115 return nil
116}
117
118// getCallback returns a specific callback matching the type and function hash
119func (p *Proxy) getCallback(callbackType CallbackType, funcHash string) *CallbackTuple {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400120 p.mutex.Lock()
121 defer p.mutex.Unlock()
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400122 if tuple, exists := p.Callbacks[callbackType][funcHash]; exists {
123 return tuple
124 }
125 return nil
126}
127
128// setCallbacks applies a callbacks list to a type
129func (p *Proxy) setCallbacks(callbackType CallbackType, callbacks map[string]*CallbackTuple) {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400130 p.mutex.Lock()
131 defer p.mutex.Unlock()
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400132 p.Callbacks[callbackType] = callbacks
133}
134
135// setCallback applies a callback to a type and hash value
136func (p *Proxy) setCallback(callbackType CallbackType, funcHash string, tuple *CallbackTuple) {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400137 p.mutex.Lock()
138 defer p.mutex.Unlock()
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400139 p.Callbacks[callbackType][funcHash] = tuple
140}
141
142// DeleteCallback removes a callback matching the type and hash
143func (p *Proxy) DeleteCallback(callbackType CallbackType, funcHash string) {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400144 p.mutex.Lock()
145 defer p.mutex.Unlock()
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400146 delete(p.Callbacks[callbackType], funcHash)
147}
148
149// CallbackType is an enumerated value to express when a callback should be executed
150type ProxyOperation uint8
151
152// Enumerated list of callback types
153const (
Manikkaraj kb1d51442019-07-23 10:41:02 -0400154 PROXY_NONE ProxyOperation = iota
155 PROXY_GET
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400156 PROXY_LIST
157 PROXY_ADD
158 PROXY_UPDATE
159 PROXY_REMOVE
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400160 PROXY_CREATE
Mahir Gunyele77977b2019-06-27 05:36:22 -0700161 PROXY_WATCH
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400162)
163
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400164var proxyOperationTypes = []string{
Manikkaraj kb1d51442019-07-23 10:41:02 -0400165 "PROXY_NONE",
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400166 "PROXY_GET",
167 "PROXY_LIST",
168 "PROXY_ADD",
169 "PROXY_UPDATE",
170 "PROXY_REMOVE",
171 "PROXY_CREATE",
Mahir Gunyele77977b2019-06-27 05:36:22 -0700172 "PROXY_WATCH",
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400173}
174
175func (t ProxyOperation) String() string {
176 return proxyOperationTypes[t]
177}
178
Manikkaraj kb1d51442019-07-23 10:41:02 -0400179func (p *Proxy) GetOperation() ProxyOperation {
180 p.mutex.RLock()
181 defer p.mutex.RUnlock()
182 return p.operation
183}
184
185func (p *Proxy) SetOperation(operation ProxyOperation) {
186 p.mutex.Lock()
187 defer p.mutex.Unlock()
188 p.operation = operation
189}
190
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400191// parseForControlledPath verifies if a proxy path matches a pattern
192// for locations that need to be access controlled.
193func (p *Proxy) parseForControlledPath(path string) (pathLock string, controlled bool) {
194 // TODO: Add other path prefixes that may need control
195 if strings.HasPrefix(path, "/devices") ||
196 strings.HasPrefix(path, "/logical_devices") ||
197 strings.HasPrefix(path, "/adapters") {
198
199 split := strings.SplitN(path, "/", -1)
200 switch len(split) {
201 case 2:
202 controlled = false
203 pathLock = ""
204 break
205 case 3:
206 fallthrough
207 default:
208 pathLock = fmt.Sprintf("%s/%s", split[1], split[2])
209 controlled = true
210 }
211 }
212 return pathLock, controlled
213}
214
215// List will retrieve information from the data model at the specified path location
216// A list operation will force access to persistence storage
Manikkaraj kb1d51442019-07-23 10:41:02 -0400217func (p *Proxy) List(ctx context.Context, path string, depth int, deep bool, txid string) interface{} {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400218 var effectivePath string
219 if path == "/" {
220 effectivePath = p.getFullPath()
221 } else {
222 effectivePath = p.getFullPath() + path
223 }
224
225 pathLock, controlled := p.parseForControlledPath(effectivePath)
226
Manikkaraj kb1d51442019-07-23 10:41:02 -0400227 p.SetOperation(PROXY_LIST)
228 defer p.SetOperation(PROXY_NONE)
229
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400230 log.Debugw("proxy-list", log.Fields{
231 "path": path,
232 "effective": effectivePath,
233 "pathLock": pathLock,
234 "controlled": controlled,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400235 "operation": p.GetOperation(),
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400236 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400237
Manikkaraj kb1d51442019-07-23 10:41:02 -0400238 rv := p.GetRoot().List(ctx, path, "", depth, deep, txid)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400239
240 return rv
241}
242
243// Get will retrieve information from the data model at the specified path location
Manikkaraj kb1d51442019-07-23 10:41:02 -0400244func (p *Proxy) Get(ctx context.Context, path string, depth int, deep bool, txid string) interface{} {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400245 var effectivePath string
246 if path == "/" {
247 effectivePath = p.getFullPath()
248 } else {
249 effectivePath = p.getFullPath() + path
250 }
251
252 pathLock, controlled := p.parseForControlledPath(effectivePath)
253
Manikkaraj kb1d51442019-07-23 10:41:02 -0400254 p.SetOperation(PROXY_GET)
255 defer p.SetOperation(PROXY_NONE)
256
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400257 log.Debugw("proxy-get", log.Fields{
258 "path": path,
259 "effective": effectivePath,
260 "pathLock": pathLock,
261 "controlled": controlled,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400262 "operation": p.GetOperation(),
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400263 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400264
Manikkaraj kb1d51442019-07-23 10:41:02 -0400265 rv := p.GetRoot().Get(ctx, path, "", depth, deep, txid)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400266
267 return rv
268}
269
270// Update will modify information in the data model at the specified location with the provided data
Manikkaraj kb1d51442019-07-23 10:41:02 -0400271func (p *Proxy) Update(ctx context.Context, path string, data interface{}, strict bool, txid string) interface{} {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400272 if !strings.HasPrefix(path, "/") {
273 log.Errorf("invalid path: %s", path)
274 return nil
275 }
276 var fullPath string
277 var effectivePath string
278 if path == "/" {
279 fullPath = p.getPath()
280 effectivePath = p.getFullPath()
281 } else {
282 fullPath = p.getPath() + path
283 effectivePath = p.getFullPath() + path
284 }
285
286 pathLock, controlled := p.parseForControlledPath(effectivePath)
287
Manikkaraj kb1d51442019-07-23 10:41:02 -0400288 p.SetOperation(PROXY_UPDATE)
289 defer p.SetOperation(PROXY_NONE)
290
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400291 log.Debugw("proxy-update", log.Fields{
292 "path": path,
293 "effective": effectivePath,
294 "full": fullPath,
295 "pathLock": pathLock,
296 "controlled": controlled,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400297 "operation": p.GetOperation(),
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400298 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400299
Manikkaraj kb1d51442019-07-23 10:41:02 -0400300 if p.GetRoot().KvStore != nil {
301 p.GetRoot().KvStore.Client.Reserve(pathLock+"_", uuid.New().String(), ReservationTTL)
302 defer p.GetRoot().KvStore.Client.ReleaseReservation(pathLock + "_")
303 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400304
Manikkaraj kb1d51442019-07-23 10:41:02 -0400305 result := p.GetRoot().Update(ctx, fullPath, data, strict, txid, nil)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400306
Manikkaraj kb1d51442019-07-23 10:41:02 -0400307 if result != nil {
308 return result.GetData()
309 }
310
311 return nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400312}
313
314// AddWithID will insert new data at specified location.
315// This method also allows the user to specify the ID of the data entry to ensure
316// that access control is active while inserting the information.
Manikkaraj kb1d51442019-07-23 10:41:02 -0400317func (p *Proxy) AddWithID(ctx context.Context, path string, id string, data interface{}, txid string) interface{} {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400318 if !strings.HasPrefix(path, "/") {
319 log.Errorf("invalid path: %s", path)
320 return nil
321 }
322 var fullPath string
323 var effectivePath string
324 if path == "/" {
325 fullPath = p.getPath()
326 effectivePath = p.getFullPath()
327 } else {
328 fullPath = p.getPath() + path
329 effectivePath = p.getFullPath() + path + "/" + id
330 }
331
332 pathLock, controlled := p.parseForControlledPath(effectivePath)
333
Manikkaraj kb1d51442019-07-23 10:41:02 -0400334 p.SetOperation(PROXY_ADD)
335 defer p.SetOperation(PROXY_NONE)
336
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400337 log.Debugw("proxy-add-with-id", log.Fields{
338 "path": path,
339 "effective": effectivePath,
340 "full": fullPath,
341 "pathLock": pathLock,
342 "controlled": controlled,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400343 "operation": p.GetOperation(),
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400344 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400345
Manikkaraj kb1d51442019-07-23 10:41:02 -0400346 if p.GetRoot().KvStore != nil {
347 p.GetRoot().KvStore.Client.Reserve(pathLock+"_", uuid.New().String(), ReservationTTL)
348 defer p.GetRoot().KvStore.Client.ReleaseReservation(pathLock + "_")
349 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400350
Manikkaraj kb1d51442019-07-23 10:41:02 -0400351 result := p.GetRoot().Add(ctx, fullPath, data, txid, nil)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400352
Manikkaraj kb1d51442019-07-23 10:41:02 -0400353 if result != nil {
354 return result.GetData()
355 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400356
Manikkaraj kb1d51442019-07-23 10:41:02 -0400357 return nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400358}
359
360// Add will insert new data at specified location.
Manikkaraj kb1d51442019-07-23 10:41:02 -0400361func (p *Proxy) Add(ctx context.Context, path string, data interface{}, txid string) interface{} {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400362 if !strings.HasPrefix(path, "/") {
363 log.Errorf("invalid path: %s", path)
364 return nil
365 }
366 var fullPath string
367 var effectivePath string
368 if path == "/" {
369 fullPath = p.getPath()
370 effectivePath = p.getFullPath()
371 } else {
372 fullPath = p.getPath() + path
373 effectivePath = p.getFullPath() + path
374 }
375
376 pathLock, controlled := p.parseForControlledPath(effectivePath)
377
Manikkaraj kb1d51442019-07-23 10:41:02 -0400378 p.SetOperation(PROXY_ADD)
379 defer p.SetOperation(PROXY_NONE)
380
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400381 log.Debugw("proxy-add", log.Fields{
382 "path": path,
383 "effective": effectivePath,
384 "full": fullPath,
385 "pathLock": pathLock,
386 "controlled": controlled,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400387 "operation": p.GetOperation(),
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400388 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400389
Manikkaraj kb1d51442019-07-23 10:41:02 -0400390 if p.GetRoot().KvStore != nil {
391 p.GetRoot().KvStore.Client.Reserve(pathLock+"_", uuid.New().String(), ReservationTTL)
392 defer p.GetRoot().KvStore.Client.ReleaseReservation(pathLock + "_")
393 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400394
Manikkaraj kb1d51442019-07-23 10:41:02 -0400395 result := p.GetRoot().Add(ctx, fullPath, data, txid, nil)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400396
Manikkaraj kb1d51442019-07-23 10:41:02 -0400397 if result != nil {
398 return result.GetData()
399 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400400
Manikkaraj kb1d51442019-07-23 10:41:02 -0400401 return nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400402}
403
404// Remove will delete an entry at the specified location
Manikkaraj kb1d51442019-07-23 10:41:02 -0400405func (p *Proxy) Remove(ctx context.Context, path string, txid string) interface{} {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400406 if !strings.HasPrefix(path, "/") {
407 log.Errorf("invalid path: %s", path)
408 return nil
409 }
410 var fullPath string
411 var effectivePath string
412 if path == "/" {
413 fullPath = p.getPath()
414 effectivePath = p.getFullPath()
415 } else {
416 fullPath = p.getPath() + path
417 effectivePath = p.getFullPath() + path
418 }
419
420 pathLock, controlled := p.parseForControlledPath(effectivePath)
421
Manikkaraj kb1d51442019-07-23 10:41:02 -0400422 p.SetOperation(PROXY_REMOVE)
423 defer p.SetOperation(PROXY_NONE)
424
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400425 log.Debugw("proxy-remove", log.Fields{
426 "path": path,
427 "effective": effectivePath,
428 "full": fullPath,
429 "pathLock": pathLock,
430 "controlled": controlled,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400431 "operation": p.GetOperation(),
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400432 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400433
Manikkaraj kb1d51442019-07-23 10:41:02 -0400434 if p.GetRoot().KvStore != nil {
435 p.GetRoot().KvStore.Client.Reserve(pathLock+"_", uuid.New().String(), ReservationTTL)
436 defer p.GetRoot().KvStore.Client.ReleaseReservation(pathLock + "_")
437 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400438
Manikkaraj kb1d51442019-07-23 10:41:02 -0400439 result := p.GetRoot().Remove(ctx, fullPath, txid, nil)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400440
Manikkaraj kb1d51442019-07-23 10:41:02 -0400441 if result != nil {
442 return result.GetData()
443 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400444
Manikkaraj kb1d51442019-07-23 10:41:02 -0400445 return nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400446}
447
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400448// CreateProxy to interact with specific path directly
Manikkaraj kb1d51442019-07-23 10:41:02 -0400449func (p *Proxy) CreateProxy(ctx context.Context, path string, exclusive bool) *Proxy {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400450 if !strings.HasPrefix(path, "/") {
451 log.Errorf("invalid path: %s", path)
452 return nil
453 }
454
455 var fullPath string
456 var effectivePath string
457 if path == "/" {
458 fullPath = p.getPath()
459 effectivePath = p.getFullPath()
460 } else {
461 fullPath = p.getPath() + path
462 effectivePath = p.getFullPath() + path
463 }
464
465 pathLock, controlled := p.parseForControlledPath(effectivePath)
466
Manikkaraj kb1d51442019-07-23 10:41:02 -0400467 p.SetOperation(PROXY_CREATE)
468 defer p.SetOperation(PROXY_NONE)
469
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400470 log.Debugw("proxy-create", log.Fields{
471 "path": path,
472 "effective": effectivePath,
473 "full": fullPath,
474 "pathLock": pathLock,
475 "controlled": controlled,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400476 "operation": p.GetOperation(),
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400477 })
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400478
Manikkaraj kb1d51442019-07-23 10:41:02 -0400479 if p.GetRoot().KvStore != nil {
480 p.GetRoot().KvStore.Client.Reserve(pathLock+"_", uuid.New().String(), ReservationTTL)
481 defer p.GetRoot().KvStore.Client.ReleaseReservation(pathLock + "_")
482 }
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400483
Manikkaraj kb1d51442019-07-23 10:41:02 -0400484 return p.GetRoot().CreateProxy(ctx, fullPath, exclusive)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400485}
486
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400487// OpenTransaction creates a new transaction branch to isolate operations made to the data model
488func (p *Proxy) OpenTransaction() *Transaction {
489 txid := p.GetRoot().MakeTxBranch()
490 return NewTransaction(p, txid)
491}
492
493// commitTransaction will apply and merge modifications made in the transaction branch to the data model
494func (p *Proxy) commitTransaction(txid string) {
495 p.GetRoot().FoldTxBranch(txid)
496}
497
498// cancelTransaction will terminate a transaction branch along will all changes within it
499func (p *Proxy) cancelTransaction(txid string) {
500 p.GetRoot().DeleteTxBranch(txid)
501}
502
503// CallbackFunction is a type used to define callback functions
504type CallbackFunction func(args ...interface{}) interface{}
505
506// CallbackTuple holds the function and arguments details of a callback
507type CallbackTuple struct {
508 callback CallbackFunction
509 args []interface{}
510}
511
512// Execute will process the a callback with its provided arguments
513func (tuple *CallbackTuple) Execute(contextArgs []interface{}) interface{} {
514 args := []interface{}{}
515
516 for _, ta := range tuple.args {
517 args = append(args, ta)
518 }
519
520 if contextArgs != nil {
521 for _, ca := range contextArgs {
522 args = append(args, ca)
523 }
524 }
525
526 return tuple.callback(args...)
527}
528
529// RegisterCallback associates a callback to the proxy
530func (p *Proxy) RegisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) {
531 if p.getCallbacks(callbackType) == nil {
532 p.setCallbacks(callbackType, make(map[string]*CallbackTuple))
533 }
534 funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name()
535 log.Debugf("value of function: %s", funcName)
536 funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12]
537
538 p.setCallback(callbackType, funcHash, &CallbackTuple{callback, args})
539}
540
541// UnregisterCallback removes references to a callback within a proxy
542func (p *Proxy) UnregisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) {
543 if p.getCallbacks(callbackType) == nil {
544 log.Errorf("no such callback type - %s", callbackType.String())
545 return
546 }
547
548 funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name()
549 funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12]
550
551 log.Debugf("value of function: %s", funcName)
552
553 if p.getCallback(callbackType, funcHash) == nil {
554 log.Errorf("function with hash value: '%s' not registered with callback type: '%s'", funcHash, callbackType)
555 return
556 }
557
558 p.DeleteCallback(callbackType, funcHash)
559}
560
561func (p *Proxy) invoke(callback *CallbackTuple, context []interface{}) (result interface{}, err error) {
562 defer func() {
563 if r := recover(); r != nil {
564 errStr := fmt.Sprintf("callback error occurred: %+v", r)
565 err = errors.New(errStr)
566 log.Error(errStr)
567 }
568 }()
569
570 result = callback.Execute(context)
571
572 return result, err
573}
574
575// InvokeCallbacks executes all callbacks associated to a specific type
576func (p *Proxy) InvokeCallbacks(args ...interface{}) (result interface{}) {
577 callbackType := args[0].(CallbackType)
578 proceedOnError := args[1].(bool)
579 context := args[2:]
580
581 var err error
582
583 if callbacks := p.getCallbacks(callbackType); callbacks != nil {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400584 p.mutex.Lock()
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400585 for _, callback := range callbacks {
586 if result, err = p.invoke(callback, context); err != nil {
587 if !proceedOnError {
588 log.Info("An error occurred. Stopping callback invocation")
589 break
590 }
591 log.Info("An error occurred. Invoking next callback")
592 }
593 }
Manikkaraj kb1d51442019-07-23 10:41:02 -0400594 p.mutex.Unlock()
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400595 }
596
597 return result
598}