blob: 86d426a548828cb0c530995b7a4d123f4b74b426 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -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 */
Stephane Barbariedc5022d2018-11-19 15:21:44 -050016
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040017package model
18
19import (
Stephane Barbarie694e2b92018-09-07 12:17:36 -040020 "crypto/md5"
Stephane Barbarie694e2b92018-09-07 12:17:36 -040021 "errors"
khenaidoob9203542018-09-17 22:56:37 -040022 "fmt"
23 "github.com/opencord/voltha-go/common/log"
24 "reflect"
Stephane Barbarie694e2b92018-09-07 12:17:36 -040025 "runtime"
khenaidoob9203542018-09-17 22:56:37 -040026 "strings"
Stephane Barbariea188d942018-10-16 16:43:04 -040027 "sync"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040028)
29
Stephane Barbariedc5022d2018-11-19 15:21:44 -050030// OperationContext holds details on the information used during an operation
Stephane Barbarieec0919b2018-09-05 14:14:29 -040031type OperationContext struct {
32 Path string
33 Data interface{}
34 FieldName string
35 ChildKey string
36}
37
Stephane Barbariedc5022d2018-11-19 15:21:44 -050038// NewOperationContext instantiates a new OperationContext structure
Stephane Barbarieec0919b2018-09-05 14:14:29 -040039func NewOperationContext(path string, data interface{}, fieldName string, childKey string) *OperationContext {
40 oc := &OperationContext{
41 Path: path,
42 Data: data,
43 FieldName: fieldName,
44 ChildKey: childKey,
45 }
46 return oc
47}
48
Stephane Barbariedc5022d2018-11-19 15:21:44 -050049// Update applies new data to the context structure
Stephane Barbarieec0919b2018-09-05 14:14:29 -040050func (oc *OperationContext) Update(data interface{}) *OperationContext {
51 oc.Data = data
52 return oc
53}
54
Stephane Barbariedc5022d2018-11-19 15:21:44 -050055// Proxy holds the information for a specific location with the data model
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040056type Proxy struct {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050057 sync.RWMutex
Stephane Barbariec53a2752019-03-08 17:50:10 -050058 Root *root
59 Node *node
60 ParentNode *node
61 Path string
62 FullPath string
63 Exclusive bool
64 Callbacks map[CallbackType]map[string]*CallbackTuple
65 Operation ProxyOperation
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040066}
67
Stephane Barbariedc5022d2018-11-19 15:21:44 -050068// NewProxy instantiates a new proxy to a specific location
Stephane Barbarie1ab43272018-12-08 21:42:13 -050069func NewProxy(root *root, node *node, parentNode *node, path string, fullPath string, exclusive bool) *Proxy {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050070 callbacks := make(map[CallbackType]map[string]*CallbackTuple)
Stephane Barbariea188d942018-10-16 16:43:04 -040071 if fullPath == "/" {
72 fullPath = ""
73 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040074 p := &Proxy{
Stephane Barbariec53a2752019-03-08 17:50:10 -050075 Root: root,
76 Node: node,
77 ParentNode: parentNode,
78 Exclusive: exclusive,
79 Path: path,
80 FullPath: fullPath,
81 Callbacks: callbacks,
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040082 }
83 return p
84}
85
Stephane Barbariedc5022d2018-11-19 15:21:44 -050086// GetRoot returns the root attribute of the proxy
87func (p *Proxy) GetRoot() *root {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050088 return p.Root
89}
90
91// getPath returns the path attribute of the proxy
92func (p *Proxy) getPath() string {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050093 return p.Path
94}
95
96// getFullPath returns the full path attribute of the proxy
97func (p *Proxy) getFullPath() string {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050098 return p.FullPath
99}
100
101// getCallbacks returns the full list of callbacks associated to the proxy
102func (p *Proxy) getCallbacks(callbackType CallbackType) map[string]*CallbackTuple {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500103 if cb, exists := p.Callbacks[callbackType]; exists {
104 return cb
105 }
106 return nil
107}
108
109// getCallback returns a specific callback matching the type and function hash
110func (p *Proxy) getCallback(callbackType CallbackType, funcHash string) *CallbackTuple {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500111 p.Lock()
112 defer p.Unlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500113 if tuple, exists := p.Callbacks[callbackType][funcHash]; exists {
114 return tuple
115 }
116 return nil
117}
118
119// setCallbacks applies a callbacks list to a type
120func (p *Proxy) setCallbacks(callbackType CallbackType, callbacks map[string]*CallbackTuple) {
121 p.Lock()
122 defer p.Unlock()
123 p.Callbacks[callbackType] = callbacks
124}
125
126// setCallback applies a callback to a type and hash value
127func (p *Proxy) setCallback(callbackType CallbackType, funcHash string, tuple *CallbackTuple) {
128 p.Lock()
129 defer p.Unlock()
130 p.Callbacks[callbackType][funcHash] = tuple
131}
132
133// DeleteCallback removes a callback matching the type and hash
134func (p *Proxy) DeleteCallback(callbackType CallbackType, funcHash string) {
135 p.Lock()
136 defer p.Unlock()
137 delete(p.Callbacks[callbackType], funcHash)
138}
139
Stephane Barbariec53a2752019-03-08 17:50:10 -0500140// CallbackType is an enumerated value to express when a callback should be executed
141type ProxyOperation uint8
142
143// Enumerated list of callback types
144const (
145 PROXY_GET ProxyOperation = iota
146 PROXY_LIST
147 PROXY_ADD
148 PROXY_UPDATE
149 PROXY_REMOVE
150)
151
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500152// parseForControlledPath verifies if a proxy path matches a pattern
153// for locations that need to be access controlled.
Stephane Barbariea188d942018-10-16 16:43:04 -0400154func (p *Proxy) parseForControlledPath(path string) (pathLock string, controlled bool) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500155 // TODO: Add other path prefixes that may need control
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500156 if strings.HasPrefix(path, "/devices") ||
157 strings.HasPrefix(path, "/logical_devices") ||
158 strings.HasPrefix(path, "/adapters") {
159
Stephane Barbariea188d942018-10-16 16:43:04 -0400160 split := strings.SplitN(path, "/", -1)
161 switch len(split) {
162 case 2:
163 controlled = false
164 pathLock = ""
165 break
166 case 3:
167 fallthrough
168 default:
169 pathLock = fmt.Sprintf("%s/%s", split[1], split[2])
170 controlled = true
171 }
172 }
173 return pathLock, controlled
174}
175
Stephane Barbarieaa467942019-02-06 14:09:44 -0500176// List will retrieve information from the data model at the specified path location
177// A list operation will force access to persistence storage
178func (p *Proxy) List(path string, depth int, deep bool, txid string) interface{} {
179 var effectivePath string
180 if path == "/" {
181 effectivePath = p.getFullPath()
182 } else {
183 effectivePath = p.getFullPath() + path
184 }
185
186 pathLock, controlled := p.parseForControlledPath(effectivePath)
187
188 log.Debugf("Path: %s, Effective: %s, PathLock: %s", path, effectivePath, pathLock)
189
190 pac := PAC().ReservePath(effectivePath, p, pathLock)
191 defer PAC().ReleasePath(pathLock)
192 pac.SetProxy(p)
193
194 rv := pac.List(path, depth, deep, txid, controlled)
195
196 return rv
197}
198
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500199// Get will retrieve information from the data model at the specified path location
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400200func (p *Proxy) Get(path string, depth int, deep bool, txid string) interface{} {
Stephane Barbariea188d942018-10-16 16:43:04 -0400201 var effectivePath string
202 if path == "/" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500203 effectivePath = p.getFullPath()
Stephane Barbariea188d942018-10-16 16:43:04 -0400204 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500205 effectivePath = p.getFullPath() + path
Stephane Barbariea188d942018-10-16 16:43:04 -0400206 }
207
208 pathLock, controlled := p.parseForControlledPath(effectivePath)
209
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500210 log.Debugf("Path: %s, Effective: %s, PathLock: %s", path, effectivePath, pathLock)
Stephane Barbariea188d942018-10-16 16:43:04 -0400211
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500212 pac := PAC().ReservePath(effectivePath, p, pathLock)
213 defer PAC().ReleasePath(pathLock)
214 pac.SetProxy(p)
Stephane Barbariea188d942018-10-16 16:43:04 -0400215
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500216 rv := pac.Get(path, depth, deep, txid, controlled)
217
218 return rv
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400219}
220
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500221// Update will modify information in the data model at the specified location with the provided data
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400222func (p *Proxy) Update(path string, data interface{}, strict bool, txid string) interface{} {
223 if !strings.HasPrefix(path, "/") {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400224 log.Errorf("invalid path: %s", path)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400225 return nil
226 }
227 var fullPath string
Stephane Barbariea188d942018-10-16 16:43:04 -0400228 var effectivePath string
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400229 if path == "/" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500230 fullPath = p.getPath()
231 effectivePath = p.getFullPath()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400232 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500233 fullPath = p.getPath() + path
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500234 effectivePath = p.getFullPath() + path
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400235 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400236
Stephane Barbariea188d942018-10-16 16:43:04 -0400237 pathLock, controlled := p.parseForControlledPath(effectivePath)
238
Stephane Barbarie260a5632019-02-26 16:12:49 -0500239 log.Debugf("Path: %s, Effective: %s, Full: %s, PathLock: %s, Controlled: %b", path, effectivePath, fullPath, pathLock, controlled)
Stephane Barbariea188d942018-10-16 16:43:04 -0400240
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500241 pac := PAC().ReservePath(effectivePath, p, pathLock)
242 defer PAC().ReleasePath(pathLock)
Stephane Barbariec53a2752019-03-08 17:50:10 -0500243
244 p.Operation = PROXY_UPDATE
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500245 pac.SetProxy(p)
Stephane Barbariea188d942018-10-16 16:43:04 -0400246
Stephane Barbariec53a2752019-03-08 17:50:10 -0500247 log.Debugw("proxy-operation--update", log.Fields{"operation":p.Operation})
248
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500249 return pac.Update(fullPath, data, strict, txid, controlled)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400250}
251
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500252// AddWithID will insert new data at specified location.
253// This method also allows the user to specify the ID of the data entry to ensure
254// that access control is active while inserting the information.
255func (p *Proxy) AddWithID(path string, id string, data interface{}, txid string) interface{} {
256 if !strings.HasPrefix(path, "/") {
257 log.Errorf("invalid path: %s", path)
258 return nil
259 }
260 var fullPath string
261 var effectivePath string
262 if path == "/" {
263 fullPath = p.getPath()
264 effectivePath = p.getFullPath()
265 } else {
266 fullPath = p.getPath() + path
267 effectivePath = p.getFullPath() + path + "/" + id
268 }
269
270 pathLock, controlled := p.parseForControlledPath(effectivePath)
271
272 log.Debugf("Path: %s, Effective: %s, Full: %s, PathLock: %s", path, effectivePath, fullPath, pathLock)
273
274 pac := PAC().ReservePath(path, p, pathLock)
275 defer PAC().ReleasePath(pathLock)
Stephane Barbariec53a2752019-03-08 17:50:10 -0500276
277 p.Operation = PROXY_ADD
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500278 pac.SetProxy(p)
279
Stephane Barbariec53a2752019-03-08 17:50:10 -0500280 log.Debugw("proxy-operation--add", log.Fields{"operation":p.Operation})
281
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500282 return pac.Add(fullPath, data, txid, controlled)
283}
284
285// Add will insert new data at specified location.
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400286func (p *Proxy) Add(path string, data interface{}, txid string) interface{} {
287 if !strings.HasPrefix(path, "/") {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400288 log.Errorf("invalid path: %s", path)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400289 return nil
290 }
291 var fullPath string
Stephane Barbariea188d942018-10-16 16:43:04 -0400292 var effectivePath string
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400293 if path == "/" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500294 fullPath = p.getPath()
295 effectivePath = p.getFullPath()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400296 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500297 fullPath = p.getPath() + path
298 effectivePath = p.getFullPath() + path
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400299 }
Stephane Barbariea188d942018-10-16 16:43:04 -0400300
301 pathLock, controlled := p.parseForControlledPath(effectivePath)
302
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500303 log.Debugf("Path: %s, Effective: %s, Full: %s, PathLock: %s", path, effectivePath, fullPath, pathLock)
Stephane Barbariea188d942018-10-16 16:43:04 -0400304
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500305 pac := PAC().ReservePath(path, p, pathLock)
306 defer PAC().ReleasePath(pathLock)
Stephane Barbariec53a2752019-03-08 17:50:10 -0500307
308 p.Operation = PROXY_ADD
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500309 pac.SetProxy(p)
Stephane Barbariea188d942018-10-16 16:43:04 -0400310
Stephane Barbariec53a2752019-03-08 17:50:10 -0500311 log.Debugw("proxy-operation--add", log.Fields{"operation":p.Operation})
312
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500313 return pac.Add(fullPath, data, txid, controlled)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400314}
315
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500316// Remove will delete an entry at the specified location
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400317func (p *Proxy) Remove(path string, txid string) interface{} {
318 if !strings.HasPrefix(path, "/") {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400319 log.Errorf("invalid path: %s", path)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400320 return nil
321 }
322 var fullPath string
Stephane Barbariea188d942018-10-16 16:43:04 -0400323 var effectivePath string
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400324 if path == "/" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500325 fullPath = p.getPath()
326 effectivePath = p.getFullPath()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400327 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500328 fullPath = p.getPath() + path
329 effectivePath = p.getFullPath() + path
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400330 }
Stephane Barbariea188d942018-10-16 16:43:04 -0400331
332 pathLock, controlled := p.parseForControlledPath(effectivePath)
333
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500334 log.Debugf("Path: %s, Effective: %s, Full: %s, PathLock: %s", path, effectivePath, fullPath, pathLock)
Stephane Barbariea188d942018-10-16 16:43:04 -0400335
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500336 pac := PAC().ReservePath(effectivePath, p, pathLock)
337 defer PAC().ReleasePath(pathLock)
Stephane Barbariec53a2752019-03-08 17:50:10 -0500338
339 p.Operation = PROXY_REMOVE
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500340 pac.SetProxy(p)
Stephane Barbariea188d942018-10-16 16:43:04 -0400341
Stephane Barbariec53a2752019-03-08 17:50:10 -0500342 log.Debugw("proxy-operation--remove", log.Fields{"operation":p.Operation})
343
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500344 return pac.Remove(fullPath, txid, controlled)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400345}
346
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500347// OpenTransaction creates a new transaction branch to isolate operations made to the data model
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400348func (p *Proxy) OpenTransaction() *Transaction {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500349 txid := p.GetRoot().MakeTxBranch()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400350 return NewTransaction(p, txid)
351}
352
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500353// commitTransaction will apply and merge modifications made in the transaction branch to the data model
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400354func (p *Proxy) commitTransaction(txid string) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500355 p.GetRoot().FoldTxBranch(txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400356}
357
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500358// cancelTransaction will terminate a transaction branch along will all changes within it
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400359func (p *Proxy) cancelTransaction(txid string) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500360 p.GetRoot().DeleteTxBranch(txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400361}
362
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500363// CallbackFunction is a type used to define callback functions
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400364type CallbackFunction func(args ...interface{}) interface{}
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500365
366// CallbackTuple holds the function and arguments details of a callback
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400367type CallbackTuple struct {
368 callback CallbackFunction
369 args []interface{}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400370}
371
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500372// Execute will process the a callback with its provided arguments
373func (tuple *CallbackTuple) Execute(contextArgs []interface{}) interface{} {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400374 args := []interface{}{}
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500375
376 for _, ta := range tuple.args {
377 args = append(args, ta)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400378 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500379
380 if contextArgs != nil {
381 for _, ca := range contextArgs {
382 args = append(args, ca)
383 }
384 }
385
Stephane Barbarie126101e2018-10-11 16:18:48 -0400386 return tuple.callback(args...)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400387}
388
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500389// RegisterCallback associates a callback to the proxy
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400390func (p *Proxy) RegisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500391 if p.getCallbacks(callbackType) == nil {
392 p.setCallbacks(callbackType, make(map[string]*CallbackTuple))
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400393 }
394 funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name()
395 log.Debugf("value of function: %s", funcName)
396 funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12]
397
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500398 p.setCallback(callbackType, funcHash, &CallbackTuple{callback, args})
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400399}
400
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500401// UnregisterCallback removes references to a callback within a proxy
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400402func (p *Proxy) UnregisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500403 if p.getCallbacks(callbackType) == nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400404 log.Errorf("no such callback type - %s", callbackType.String())
405 return
406 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500407
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400408 funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name()
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400409 funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500410
411 log.Debugf("value of function: %s", funcName)
412
413 if p.getCallback(callbackType, funcHash) == nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400414 log.Errorf("function with hash value: '%s' not registered with callback type: '%s'", funcHash, callbackType)
415 return
416 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500417
418 p.DeleteCallback(callbackType, funcHash)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400419}
420
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500421func (p *Proxy) invoke(callback *CallbackTuple, context []interface{}) (result interface{}, err error) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400422 defer func() {
423 if r := recover(); r != nil {
424 errStr := fmt.Sprintf("callback error occurred: %+v", r)
425 err = errors.New(errStr)
426 log.Error(errStr)
427 }
428 }()
429
430 result = callback.Execute(context)
431
432 return result, err
433}
434
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500435// InvokeCallbacks executes all callbacks associated to a specific type
Stephane Barbarie126101e2018-10-11 16:18:48 -0400436func (p *Proxy) InvokeCallbacks(args ...interface{}) (result interface{}) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400437 callbackType := args[0].(CallbackType)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400438 proceedOnError := args[1].(bool)
439 context := args[2:]
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400440
441 var err error
442
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500443 if callbacks := p.getCallbacks(callbackType); callbacks != nil {
444 p.Lock()
445 for _, callback := range callbacks {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400446 if result, err = p.invoke(callback, context); err != nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400447 if !proceedOnError {
448 log.Info("An error occurred. Stopping callback invocation")
449 break
450 }
451 log.Info("An error occurred. Invoking next callback")
452 }
453 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500454 p.Unlock()
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400455 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500456
Stephane Barbariea188d942018-10-16 16:43:04 -0400457 return result
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400458}