Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package model |
| 18 | |
| 19 | import ( |
| 20 | "crypto/md5" |
| 21 | "errors" |
| 22 | "fmt" |
| 23 | "github.com/opencord/voltha-go/common/log" |
| 24 | "reflect" |
| 25 | "runtime" |
| 26 | "strings" |
| 27 | "sync" |
| 28 | ) |
| 29 | |
| 30 | // OperationContext holds details on the information used during an operation |
| 31 | type OperationContext struct { |
| 32 | Path string |
| 33 | Data interface{} |
| 34 | FieldName string |
| 35 | ChildKey string |
| 36 | } |
| 37 | |
| 38 | // NewOperationContext instantiates a new OperationContext structure |
| 39 | func 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 | |
| 49 | // Update applies new data to the context structure |
| 50 | func (oc *OperationContext) Update(data interface{}) *OperationContext { |
| 51 | oc.Data = data |
| 52 | return oc |
| 53 | } |
| 54 | |
| 55 | // Proxy holds the information for a specific location with the data model |
| 56 | type Proxy struct { |
| 57 | sync.RWMutex |
| 58 | 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 |
| 66 | } |
| 67 | |
| 68 | // NewProxy instantiates a new proxy to a specific location |
| 69 | func NewProxy(root *root, node *node, parentNode *node, path string, fullPath string, exclusive bool) *Proxy { |
| 70 | callbacks := make(map[CallbackType]map[string]*CallbackTuple) |
| 71 | if fullPath == "/" { |
| 72 | fullPath = "" |
| 73 | } |
| 74 | p := &Proxy{ |
| 75 | Root: root, |
| 76 | Node: node, |
| 77 | ParentNode: parentNode, |
| 78 | Exclusive: exclusive, |
| 79 | Path: path, |
| 80 | FullPath: fullPath, |
| 81 | Callbacks: callbacks, |
| 82 | } |
| 83 | return p |
| 84 | } |
| 85 | |
| 86 | // GetRoot returns the root attribute of the proxy |
| 87 | func (p *Proxy) GetRoot() *root { |
| 88 | return p.Root |
| 89 | } |
| 90 | |
| 91 | // getPath returns the path attribute of the proxy |
| 92 | func (p *Proxy) getPath() string { |
| 93 | return p.Path |
| 94 | } |
| 95 | |
| 96 | // getFullPath returns the full path attribute of the proxy |
| 97 | func (p *Proxy) getFullPath() string { |
| 98 | return p.FullPath |
| 99 | } |
| 100 | |
| 101 | // getCallbacks returns the full list of callbacks associated to the proxy |
| 102 | func (p *Proxy) getCallbacks(callbackType CallbackType) map[string]*CallbackTuple { |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 103 | if p != nil { |
| 104 | if cb, exists := p.Callbacks[callbackType]; exists { |
| 105 | return cb |
| 106 | } |
| 107 | } else { |
| 108 | log.Debugw("proxy-is-nil", log.Fields{"callback-type": callbackType.String()}) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 109 | } |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | // getCallback returns a specific callback matching the type and function hash |
| 114 | func (p *Proxy) getCallback(callbackType CallbackType, funcHash string) *CallbackTuple { |
| 115 | p.Lock() |
| 116 | defer p.Unlock() |
| 117 | if tuple, exists := p.Callbacks[callbackType][funcHash]; exists { |
| 118 | return tuple |
| 119 | } |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | // setCallbacks applies a callbacks list to a type |
| 124 | func (p *Proxy) setCallbacks(callbackType CallbackType, callbacks map[string]*CallbackTuple) { |
| 125 | p.Lock() |
| 126 | defer p.Unlock() |
| 127 | p.Callbacks[callbackType] = callbacks |
| 128 | } |
| 129 | |
| 130 | // setCallback applies a callback to a type and hash value |
| 131 | func (p *Proxy) setCallback(callbackType CallbackType, funcHash string, tuple *CallbackTuple) { |
| 132 | p.Lock() |
| 133 | defer p.Unlock() |
| 134 | p.Callbacks[callbackType][funcHash] = tuple |
| 135 | } |
| 136 | |
| 137 | // DeleteCallback removes a callback matching the type and hash |
| 138 | func (p *Proxy) DeleteCallback(callbackType CallbackType, funcHash string) { |
| 139 | p.Lock() |
| 140 | defer p.Unlock() |
| 141 | delete(p.Callbacks[callbackType], funcHash) |
| 142 | } |
| 143 | |
| 144 | // CallbackType is an enumerated value to express when a callback should be executed |
| 145 | type ProxyOperation uint8 |
| 146 | |
| 147 | // Enumerated list of callback types |
| 148 | const ( |
| 149 | PROXY_GET ProxyOperation = iota |
| 150 | PROXY_LIST |
| 151 | PROXY_ADD |
| 152 | PROXY_UPDATE |
| 153 | PROXY_REMOVE |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 154 | PROXY_CREATE |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 155 | PROXY_WATCH |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 156 | ) |
| 157 | |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 158 | var proxyOperationTypes = []string{ |
| 159 | "PROXY_GET", |
| 160 | "PROXY_LIST", |
| 161 | "PROXY_ADD", |
| 162 | "PROXY_UPDATE", |
| 163 | "PROXY_REMOVE", |
| 164 | "PROXY_CREATE", |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 165 | "PROXY_WATCH", |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | func (t ProxyOperation) String() string { |
| 169 | return proxyOperationTypes[t] |
| 170 | } |
| 171 | |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 172 | // parseForControlledPath verifies if a proxy path matches a pattern |
| 173 | // for locations that need to be access controlled. |
| 174 | func (p *Proxy) parseForControlledPath(path string) (pathLock string, controlled bool) { |
| 175 | // TODO: Add other path prefixes that may need control |
| 176 | if strings.HasPrefix(path, "/devices") || |
| 177 | strings.HasPrefix(path, "/logical_devices") || |
| 178 | strings.HasPrefix(path, "/adapters") { |
| 179 | |
| 180 | split := strings.SplitN(path, "/", -1) |
| 181 | switch len(split) { |
| 182 | case 2: |
| 183 | controlled = false |
| 184 | pathLock = "" |
| 185 | break |
| 186 | case 3: |
| 187 | fallthrough |
| 188 | default: |
| 189 | pathLock = fmt.Sprintf("%s/%s", split[1], split[2]) |
| 190 | controlled = true |
| 191 | } |
| 192 | } |
| 193 | return pathLock, controlled |
| 194 | } |
| 195 | |
| 196 | // List will retrieve information from the data model at the specified path location |
| 197 | // A list operation will force access to persistence storage |
| 198 | func (p *Proxy) List(path string, depth int, deep bool, txid string) interface{} { |
| 199 | var effectivePath string |
| 200 | if path == "/" { |
| 201 | effectivePath = p.getFullPath() |
| 202 | } else { |
| 203 | effectivePath = p.getFullPath() + path |
| 204 | } |
| 205 | |
| 206 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 207 | |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 208 | log.Debugw("proxy-list", log.Fields{ |
| 209 | "path": path, |
| 210 | "effective": effectivePath, |
| 211 | "pathLock": pathLock, |
| 212 | "controlled": controlled, |
| 213 | }) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 214 | |
| 215 | pac := PAC().ReservePath(effectivePath, p, pathLock) |
| 216 | defer PAC().ReleasePath(pathLock) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 217 | p.Operation = PROXY_LIST |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 218 | pac.SetProxy(p) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 219 | defer func(op ProxyOperation) { |
| 220 | pac.getProxy().Operation = op |
| 221 | }(PROXY_GET) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 222 | |
| 223 | rv := pac.List(path, depth, deep, txid, controlled) |
| 224 | |
| 225 | return rv |
| 226 | } |
| 227 | |
| 228 | // Get will retrieve information from the data model at the specified path location |
| 229 | func (p *Proxy) Get(path string, depth int, deep bool, txid string) interface{} { |
| 230 | var effectivePath string |
| 231 | if path == "/" { |
| 232 | effectivePath = p.getFullPath() |
| 233 | } else { |
| 234 | effectivePath = p.getFullPath() + path |
| 235 | } |
| 236 | |
| 237 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 238 | |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 239 | log.Debugw("proxy-get", log.Fields{ |
| 240 | "path": path, |
| 241 | "effective": effectivePath, |
| 242 | "pathLock": pathLock, |
| 243 | "controlled": controlled, |
| 244 | }) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 245 | |
| 246 | pac := PAC().ReservePath(effectivePath, p, pathLock) |
| 247 | defer PAC().ReleasePath(pathLock) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 248 | p.Operation = PROXY_GET |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 249 | pac.SetProxy(p) |
| 250 | |
| 251 | rv := pac.Get(path, depth, deep, txid, controlled) |
| 252 | |
| 253 | return rv |
| 254 | } |
| 255 | |
| 256 | // Update will modify information in the data model at the specified location with the provided data |
| 257 | func (p *Proxy) Update(path string, data interface{}, strict bool, txid string) interface{} { |
| 258 | if !strings.HasPrefix(path, "/") { |
| 259 | log.Errorf("invalid path: %s", path) |
| 260 | return nil |
| 261 | } |
| 262 | var fullPath string |
| 263 | var effectivePath string |
| 264 | if path == "/" { |
| 265 | fullPath = p.getPath() |
| 266 | effectivePath = p.getFullPath() |
| 267 | } else { |
| 268 | fullPath = p.getPath() + path |
| 269 | effectivePath = p.getFullPath() + path |
| 270 | } |
| 271 | |
| 272 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 273 | |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 274 | log.Debugw("proxy-update", log.Fields{ |
| 275 | "path": path, |
| 276 | "effective": effectivePath, |
| 277 | "full": fullPath, |
| 278 | "pathLock": pathLock, |
| 279 | "controlled": controlled, |
| 280 | }) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 281 | |
| 282 | pac := PAC().ReservePath(effectivePath, p, pathLock) |
| 283 | defer PAC().ReleasePath(pathLock) |
| 284 | |
| 285 | p.Operation = PROXY_UPDATE |
| 286 | pac.SetProxy(p) |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 287 | defer func(op ProxyOperation) { |
| 288 | pac.getProxy().Operation = op |
| 289 | }(PROXY_GET) |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 290 | log.Debugw("proxy-operation--update", log.Fields{"operation": p.Operation}) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 291 | |
| 292 | return pac.Update(fullPath, data, strict, txid, controlled) |
| 293 | } |
| 294 | |
| 295 | // AddWithID will insert new data at specified location. |
| 296 | // This method also allows the user to specify the ID of the data entry to ensure |
| 297 | // that access control is active while inserting the information. |
| 298 | func (p *Proxy) AddWithID(path string, id string, data interface{}, txid string) interface{} { |
| 299 | if !strings.HasPrefix(path, "/") { |
| 300 | log.Errorf("invalid path: %s", path) |
| 301 | return nil |
| 302 | } |
| 303 | var fullPath string |
| 304 | var effectivePath string |
| 305 | if path == "/" { |
| 306 | fullPath = p.getPath() |
| 307 | effectivePath = p.getFullPath() |
| 308 | } else { |
| 309 | fullPath = p.getPath() + path |
| 310 | effectivePath = p.getFullPath() + path + "/" + id |
| 311 | } |
| 312 | |
| 313 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 314 | |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 315 | log.Debugw("proxy-add-with-id", log.Fields{ |
| 316 | "path": path, |
| 317 | "effective": effectivePath, |
| 318 | "full": fullPath, |
| 319 | "pathLock": pathLock, |
| 320 | "controlled": controlled, |
| 321 | }) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 322 | |
| 323 | pac := PAC().ReservePath(path, p, pathLock) |
| 324 | defer PAC().ReleasePath(pathLock) |
| 325 | |
| 326 | p.Operation = PROXY_ADD |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 327 | defer func(op ProxyOperation) { |
| 328 | pac.getProxy().Operation = op |
| 329 | }(PROXY_GET) |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 330 | |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 331 | pac.SetProxy(p) |
| 332 | |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 333 | log.Debugw("proxy-operation--add", log.Fields{"operation": p.Operation}) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 334 | |
| 335 | return pac.Add(fullPath, data, txid, controlled) |
| 336 | } |
| 337 | |
| 338 | // Add will insert new data at specified location. |
| 339 | func (p *Proxy) Add(path string, data interface{}, txid string) interface{} { |
| 340 | if !strings.HasPrefix(path, "/") { |
| 341 | log.Errorf("invalid path: %s", path) |
| 342 | return nil |
| 343 | } |
| 344 | var fullPath string |
| 345 | var effectivePath string |
| 346 | if path == "/" { |
| 347 | fullPath = p.getPath() |
| 348 | effectivePath = p.getFullPath() |
| 349 | } else { |
| 350 | fullPath = p.getPath() + path |
| 351 | effectivePath = p.getFullPath() + path |
| 352 | } |
| 353 | |
| 354 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 355 | |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 356 | log.Debugw("proxy-add", log.Fields{ |
| 357 | "path": path, |
| 358 | "effective": effectivePath, |
| 359 | "full": fullPath, |
| 360 | "pathLock": pathLock, |
| 361 | "controlled": controlled, |
| 362 | }) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 363 | |
| 364 | pac := PAC().ReservePath(path, p, pathLock) |
| 365 | defer PAC().ReleasePath(pathLock) |
| 366 | |
| 367 | p.Operation = PROXY_ADD |
| 368 | pac.SetProxy(p) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 369 | defer func(op ProxyOperation) { |
| 370 | pac.getProxy().Operation = op |
| 371 | }(PROXY_GET) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 372 | |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 373 | log.Debugw("proxy-operation--add", log.Fields{"operation": p.Operation}) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 374 | |
| 375 | return pac.Add(fullPath, data, txid, controlled) |
| 376 | } |
| 377 | |
| 378 | // Remove will delete an entry at the specified location |
| 379 | func (p *Proxy) Remove(path string, txid string) interface{} { |
| 380 | if !strings.HasPrefix(path, "/") { |
| 381 | log.Errorf("invalid path: %s", path) |
| 382 | return nil |
| 383 | } |
| 384 | var fullPath string |
| 385 | var effectivePath string |
| 386 | if path == "/" { |
| 387 | fullPath = p.getPath() |
| 388 | effectivePath = p.getFullPath() |
| 389 | } else { |
| 390 | fullPath = p.getPath() + path |
| 391 | effectivePath = p.getFullPath() + path |
| 392 | } |
| 393 | |
| 394 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 395 | |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 396 | log.Debugw("proxy-remove", log.Fields{ |
| 397 | "path": path, |
| 398 | "effective": effectivePath, |
| 399 | "full": fullPath, |
| 400 | "pathLock": pathLock, |
| 401 | "controlled": controlled, |
| 402 | }) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 403 | |
| 404 | pac := PAC().ReservePath(effectivePath, p, pathLock) |
| 405 | defer PAC().ReleasePath(pathLock) |
| 406 | |
| 407 | p.Operation = PROXY_REMOVE |
| 408 | pac.SetProxy(p) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 409 | defer func(op ProxyOperation) { |
| 410 | pac.getProxy().Operation = op |
| 411 | }(PROXY_GET) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 412 | |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 413 | log.Debugw("proxy-operation--remove", log.Fields{"operation": p.Operation}) |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 414 | |
| 415 | return pac.Remove(fullPath, txid, controlled) |
| 416 | } |
| 417 | |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 418 | // CreateProxy to interact with specific path directly |
| 419 | func (p *Proxy) CreateProxy(path string, exclusive bool) *Proxy { |
| 420 | if !strings.HasPrefix(path, "/") { |
| 421 | log.Errorf("invalid path: %s", path) |
| 422 | return nil |
| 423 | } |
| 424 | |
| 425 | var fullPath string |
| 426 | var effectivePath string |
| 427 | if path == "/" { |
| 428 | fullPath = p.getPath() |
| 429 | effectivePath = p.getFullPath() |
| 430 | } else { |
| 431 | fullPath = p.getPath() + path |
| 432 | effectivePath = p.getFullPath() + path |
| 433 | } |
| 434 | |
| 435 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 436 | |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 437 | log.Debugw("proxy-create", log.Fields{ |
| 438 | "path": path, |
| 439 | "effective": effectivePath, |
| 440 | "full": fullPath, |
| 441 | "pathLock": pathLock, |
| 442 | "controlled": controlled, |
| 443 | }) |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 444 | |
| 445 | pac := PAC().ReservePath(path, p, pathLock) |
| 446 | defer PAC().ReleasePath(pathLock) |
| 447 | |
| 448 | p.Operation = PROXY_CREATE |
| 449 | pac.SetProxy(p) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 450 | defer func(op ProxyOperation) { |
| 451 | pac.getProxy().Operation = op |
| 452 | }(PROXY_GET) |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 453 | |
| 454 | log.Debugw("proxy-operation--create-proxy", log.Fields{"operation": p.Operation}) |
| 455 | |
| 456 | return pac.CreateProxy(fullPath, exclusive, controlled) |
| 457 | } |
| 458 | |
Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame] | 459 | // OpenTransaction creates a new transaction branch to isolate operations made to the data model |
| 460 | func (p *Proxy) OpenTransaction() *Transaction { |
| 461 | txid := p.GetRoot().MakeTxBranch() |
| 462 | return NewTransaction(p, txid) |
| 463 | } |
| 464 | |
| 465 | // commitTransaction will apply and merge modifications made in the transaction branch to the data model |
| 466 | func (p *Proxy) commitTransaction(txid string) { |
| 467 | p.GetRoot().FoldTxBranch(txid) |
| 468 | } |
| 469 | |
| 470 | // cancelTransaction will terminate a transaction branch along will all changes within it |
| 471 | func (p *Proxy) cancelTransaction(txid string) { |
| 472 | p.GetRoot().DeleteTxBranch(txid) |
| 473 | } |
| 474 | |
| 475 | // CallbackFunction is a type used to define callback functions |
| 476 | type CallbackFunction func(args ...interface{}) interface{} |
| 477 | |
| 478 | // CallbackTuple holds the function and arguments details of a callback |
| 479 | type CallbackTuple struct { |
| 480 | callback CallbackFunction |
| 481 | args []interface{} |
| 482 | } |
| 483 | |
| 484 | // Execute will process the a callback with its provided arguments |
| 485 | func (tuple *CallbackTuple) Execute(contextArgs []interface{}) interface{} { |
| 486 | args := []interface{}{} |
| 487 | |
| 488 | for _, ta := range tuple.args { |
| 489 | args = append(args, ta) |
| 490 | } |
| 491 | |
| 492 | if contextArgs != nil { |
| 493 | for _, ca := range contextArgs { |
| 494 | args = append(args, ca) |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | return tuple.callback(args...) |
| 499 | } |
| 500 | |
| 501 | // RegisterCallback associates a callback to the proxy |
| 502 | func (p *Proxy) RegisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) { |
| 503 | if p.getCallbacks(callbackType) == nil { |
| 504 | p.setCallbacks(callbackType, make(map[string]*CallbackTuple)) |
| 505 | } |
| 506 | funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name() |
| 507 | log.Debugf("value of function: %s", funcName) |
| 508 | funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12] |
| 509 | |
| 510 | p.setCallback(callbackType, funcHash, &CallbackTuple{callback, args}) |
| 511 | } |
| 512 | |
| 513 | // UnregisterCallback removes references to a callback within a proxy |
| 514 | func (p *Proxy) UnregisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) { |
| 515 | if p.getCallbacks(callbackType) == nil { |
| 516 | log.Errorf("no such callback type - %s", callbackType.String()) |
| 517 | return |
| 518 | } |
| 519 | |
| 520 | funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name() |
| 521 | funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12] |
| 522 | |
| 523 | log.Debugf("value of function: %s", funcName) |
| 524 | |
| 525 | if p.getCallback(callbackType, funcHash) == nil { |
| 526 | log.Errorf("function with hash value: '%s' not registered with callback type: '%s'", funcHash, callbackType) |
| 527 | return |
| 528 | } |
| 529 | |
| 530 | p.DeleteCallback(callbackType, funcHash) |
| 531 | } |
| 532 | |
| 533 | func (p *Proxy) invoke(callback *CallbackTuple, context []interface{}) (result interface{}, err error) { |
| 534 | defer func() { |
| 535 | if r := recover(); r != nil { |
| 536 | errStr := fmt.Sprintf("callback error occurred: %+v", r) |
| 537 | err = errors.New(errStr) |
| 538 | log.Error(errStr) |
| 539 | } |
| 540 | }() |
| 541 | |
| 542 | result = callback.Execute(context) |
| 543 | |
| 544 | return result, err |
| 545 | } |
| 546 | |
| 547 | // InvokeCallbacks executes all callbacks associated to a specific type |
| 548 | func (p *Proxy) InvokeCallbacks(args ...interface{}) (result interface{}) { |
| 549 | callbackType := args[0].(CallbackType) |
| 550 | proceedOnError := args[1].(bool) |
| 551 | context := args[2:] |
| 552 | |
| 553 | var err error |
| 554 | |
| 555 | if callbacks := p.getCallbacks(callbackType); callbacks != nil { |
| 556 | p.Lock() |
| 557 | for _, callback := range callbacks { |
| 558 | if result, err = p.invoke(callback, context); err != nil { |
| 559 | if !proceedOnError { |
| 560 | log.Info("An error occurred. Stopping callback invocation") |
| 561 | break |
| 562 | } |
| 563 | log.Info("An error occurred. Invoking next callback") |
| 564 | } |
| 565 | } |
| 566 | p.Unlock() |
| 567 | } |
| 568 | |
| 569 | return result |
| 570 | } |