khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -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 | */ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 20 | "context" |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 21 | "crypto/md5" |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 22 | "errors" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 23 | "fmt" |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 24 | "github.com/google/uuid" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/common/log" |
| 26 | "reflect" |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 27 | "runtime" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 28 | "strings" |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 29 | "sync" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 30 | ) |
| 31 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 32 | // OperationContext holds details on the information used during an operation |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 33 | type OperationContext struct { |
| 34 | Path string |
| 35 | Data interface{} |
| 36 | FieldName string |
| 37 | ChildKey string |
| 38 | } |
| 39 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 40 | // NewOperationContext instantiates a new OperationContext structure |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 41 | func 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 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 51 | // Update applies new data to the context structure |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 52 | func (oc *OperationContext) Update(data interface{}) *OperationContext { |
| 53 | oc.Data = data |
| 54 | return oc |
| 55 | } |
| 56 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 57 | // Proxy holds the information for a specific location with the data model |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 58 | type Proxy struct { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 59 | mutex sync.RWMutex |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 60 | Root *root |
| 61 | Node *node |
| 62 | ParentNode *node |
| 63 | Path string |
| 64 | FullPath string |
| 65 | Exclusive bool |
| 66 | Callbacks map[CallbackType]map[string]*CallbackTuple |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 67 | operation ProxyOperation |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 70 | // NewProxy instantiates a new proxy to a specific location |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 71 | func NewProxy(root *root, node *node, parentNode *node, path string, fullPath string, exclusive bool) *Proxy { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 72 | callbacks := make(map[CallbackType]map[string]*CallbackTuple) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 73 | if fullPath == "/" { |
| 74 | fullPath = "" |
| 75 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 76 | p := &Proxy{ |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 77 | Root: root, |
| 78 | Node: node, |
| 79 | ParentNode: parentNode, |
| 80 | Exclusive: exclusive, |
| 81 | Path: path, |
| 82 | FullPath: fullPath, |
| 83 | Callbacks: callbacks, |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 84 | } |
| 85 | return p |
| 86 | } |
| 87 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 88 | // GetRoot returns the root attribute of the proxy |
| 89 | func (p *Proxy) GetRoot() *root { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 90 | return p.Root |
| 91 | } |
| 92 | |
| 93 | // getPath returns the path attribute of the proxy |
| 94 | func (p *Proxy) getPath() string { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 95 | return p.Path |
| 96 | } |
| 97 | |
| 98 | // getFullPath returns the full path attribute of the proxy |
| 99 | func (p *Proxy) getFullPath() string { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 100 | return p.FullPath |
| 101 | } |
| 102 | |
| 103 | // getCallbacks returns the full list of callbacks associated to the proxy |
| 104 | func (p *Proxy) getCallbacks(callbackType CallbackType) map[string]*CallbackTuple { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 105 | p.mutex.RLock() |
| 106 | defer p.mutex.RUnlock() |
| 107 | |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 108 | 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()}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 114 | } |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | // getCallback returns a specific callback matching the type and function hash |
| 119 | func (p *Proxy) getCallback(callbackType CallbackType, funcHash string) *CallbackTuple { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 120 | p.mutex.Lock() |
| 121 | defer p.mutex.Unlock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 122 | 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 |
| 129 | func (p *Proxy) setCallbacks(callbackType CallbackType, callbacks map[string]*CallbackTuple) { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 130 | p.mutex.Lock() |
| 131 | defer p.mutex.Unlock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 132 | p.Callbacks[callbackType] = callbacks |
| 133 | } |
| 134 | |
| 135 | // setCallback applies a callback to a type and hash value |
| 136 | func (p *Proxy) setCallback(callbackType CallbackType, funcHash string, tuple *CallbackTuple) { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 137 | p.mutex.Lock() |
| 138 | defer p.mutex.Unlock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 139 | p.Callbacks[callbackType][funcHash] = tuple |
| 140 | } |
| 141 | |
| 142 | // DeleteCallback removes a callback matching the type and hash |
| 143 | func (p *Proxy) DeleteCallback(callbackType CallbackType, funcHash string) { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 144 | p.mutex.Lock() |
| 145 | defer p.mutex.Unlock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 146 | delete(p.Callbacks[callbackType], funcHash) |
| 147 | } |
| 148 | |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 149 | // CallbackType is an enumerated value to express when a callback should be executed |
| 150 | type ProxyOperation uint8 |
| 151 | |
| 152 | // Enumerated list of callback types |
| 153 | const ( |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 154 | PROXY_NONE ProxyOperation = iota |
| 155 | PROXY_GET |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 156 | PROXY_LIST |
| 157 | PROXY_ADD |
| 158 | PROXY_UPDATE |
| 159 | PROXY_REMOVE |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 160 | PROXY_CREATE |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 161 | PROXY_WATCH |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 162 | ) |
| 163 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 164 | var proxyOperationTypes = []string{ |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 165 | "PROXY_NONE", |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 166 | "PROXY_GET", |
| 167 | "PROXY_LIST", |
| 168 | "PROXY_ADD", |
| 169 | "PROXY_UPDATE", |
| 170 | "PROXY_REMOVE", |
| 171 | "PROXY_CREATE", |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 172 | "PROXY_WATCH", |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | func (t ProxyOperation) String() string { |
| 176 | return proxyOperationTypes[t] |
| 177 | } |
| 178 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 179 | func (p *Proxy) GetOperation() ProxyOperation { |
| 180 | p.mutex.RLock() |
| 181 | defer p.mutex.RUnlock() |
| 182 | return p.operation |
| 183 | } |
| 184 | |
| 185 | func (p *Proxy) SetOperation(operation ProxyOperation) { |
| 186 | p.mutex.Lock() |
| 187 | defer p.mutex.Unlock() |
| 188 | p.operation = operation |
| 189 | } |
| 190 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 191 | // parseForControlledPath verifies if a proxy path matches a pattern |
| 192 | // for locations that need to be access controlled. |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 193 | func (p *Proxy) parseForControlledPath(path string) (pathLock string, controlled bool) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 194 | // TODO: Add other path prefixes that may need control |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 195 | if strings.HasPrefix(path, "/devices") || |
| 196 | strings.HasPrefix(path, "/logical_devices") || |
| 197 | strings.HasPrefix(path, "/adapters") { |
| 198 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 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 | |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 215 | // List will retrieve information from the data model at the specified path location |
| 216 | // A list operation will force access to persistence storage |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 217 | func (p *Proxy) List(ctx context.Context, path string, depth int, deep bool, txid string) interface{} { |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 218 | 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 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 227 | p.SetOperation(PROXY_LIST) |
| 228 | defer p.SetOperation(PROXY_NONE) |
| 229 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 230 | log.Debugw("proxy-list", log.Fields{ |
| 231 | "path": path, |
| 232 | "effective": effectivePath, |
| 233 | "pathLock": pathLock, |
| 234 | "controlled": controlled, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 235 | "operation": p.GetOperation(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 236 | }) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 237 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 238 | rv := p.GetRoot().List(ctx, path, "", depth, deep, txid) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 239 | |
| 240 | return rv |
| 241 | } |
| 242 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 243 | // Get will retrieve information from the data model at the specified path location |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 244 | func (p *Proxy) Get(ctx context.Context, path string, depth int, deep bool, txid string) interface{} { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 245 | var effectivePath string |
| 246 | if path == "/" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 247 | effectivePath = p.getFullPath() |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 248 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 249 | effectivePath = p.getFullPath() + path |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 253 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 254 | p.SetOperation(PROXY_GET) |
| 255 | defer p.SetOperation(PROXY_NONE) |
| 256 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 257 | log.Debugw("proxy-get", log.Fields{ |
| 258 | "path": path, |
| 259 | "effective": effectivePath, |
| 260 | "pathLock": pathLock, |
| 261 | "controlled": controlled, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 262 | "operation": p.GetOperation(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 263 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 264 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 265 | rv := p.GetRoot().Get(ctx, path, "", depth, deep, txid) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 266 | |
| 267 | return rv |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 270 | // Update will modify information in the data model at the specified location with the provided data |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 271 | func (p *Proxy) Update(ctx context.Context, path string, data interface{}, strict bool, txid string) interface{} { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 272 | if !strings.HasPrefix(path, "/") { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 273 | log.Errorf("invalid path: %s", path) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 274 | return nil |
| 275 | } |
| 276 | var fullPath string |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 277 | var effectivePath string |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 278 | if path == "/" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 279 | fullPath = p.getPath() |
| 280 | effectivePath = p.getFullPath() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 281 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 282 | fullPath = p.getPath() + path |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 283 | effectivePath = p.getFullPath() + path |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 284 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 285 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 286 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 287 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 288 | p.SetOperation(PROXY_UPDATE) |
| 289 | defer p.SetOperation(PROXY_NONE) |
| 290 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 291 | log.Debugw("proxy-update", log.Fields{ |
| 292 | "path": path, |
| 293 | "effective": effectivePath, |
| 294 | "full": fullPath, |
| 295 | "pathLock": pathLock, |
| 296 | "controlled": controlled, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 297 | "operation": p.GetOperation(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 298 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 299 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 300 | 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 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 304 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 305 | result := p.GetRoot().Update(ctx, fullPath, data, strict, txid, nil) |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 306 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 307 | if result != nil { |
| 308 | return result.GetData() |
| 309 | } |
| 310 | |
| 311 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 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. |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 317 | func (p *Proxy) AddWithID(ctx context.Context, path string, id string, data interface{}, txid string) interface{} { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 318 | 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 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 334 | p.SetOperation(PROXY_ADD) |
| 335 | defer p.SetOperation(PROXY_NONE) |
| 336 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 337 | log.Debugw("proxy-add-with-id", log.Fields{ |
| 338 | "path": path, |
| 339 | "effective": effectivePath, |
| 340 | "full": fullPath, |
| 341 | "pathLock": pathLock, |
| 342 | "controlled": controlled, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 343 | "operation": p.GetOperation(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 344 | }) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 345 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 346 | 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 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 350 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 351 | result := p.GetRoot().Add(ctx, fullPath, data, txid, nil) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 352 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 353 | if result != nil { |
| 354 | return result.GetData() |
| 355 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 356 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 357 | return nil |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // Add will insert new data at specified location. |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 361 | func (p *Proxy) Add(ctx context.Context, path string, data interface{}, txid string) interface{} { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 362 | if !strings.HasPrefix(path, "/") { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 363 | log.Errorf("invalid path: %s", path) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 364 | return nil |
| 365 | } |
| 366 | var fullPath string |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 367 | var effectivePath string |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 368 | if path == "/" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 369 | fullPath = p.getPath() |
| 370 | effectivePath = p.getFullPath() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 371 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 372 | fullPath = p.getPath() + path |
| 373 | effectivePath = p.getFullPath() + path |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 374 | } |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 375 | |
| 376 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 377 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 378 | p.SetOperation(PROXY_ADD) |
| 379 | defer p.SetOperation(PROXY_NONE) |
| 380 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 381 | log.Debugw("proxy-add", log.Fields{ |
| 382 | "path": path, |
| 383 | "effective": effectivePath, |
| 384 | "full": fullPath, |
| 385 | "pathLock": pathLock, |
| 386 | "controlled": controlled, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 387 | "operation": p.GetOperation(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 388 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 389 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 390 | 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 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 394 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 395 | result := p.GetRoot().Add(ctx, fullPath, data, txid, nil) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 396 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 397 | if result != nil { |
| 398 | return result.GetData() |
| 399 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 400 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 401 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 402 | } |
| 403 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 404 | // Remove will delete an entry at the specified location |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 405 | func (p *Proxy) Remove(ctx context.Context, path string, txid string) interface{} { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 406 | if !strings.HasPrefix(path, "/") { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 407 | log.Errorf("invalid path: %s", path) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 408 | return nil |
| 409 | } |
| 410 | var fullPath string |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 411 | var effectivePath string |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 412 | if path == "/" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 413 | fullPath = p.getPath() |
| 414 | effectivePath = p.getFullPath() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 415 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 416 | fullPath = p.getPath() + path |
| 417 | effectivePath = p.getFullPath() + path |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 418 | } |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 419 | |
| 420 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 421 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 422 | p.SetOperation(PROXY_REMOVE) |
| 423 | defer p.SetOperation(PROXY_NONE) |
| 424 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 425 | log.Debugw("proxy-remove", log.Fields{ |
| 426 | "path": path, |
| 427 | "effective": effectivePath, |
| 428 | "full": fullPath, |
| 429 | "pathLock": pathLock, |
| 430 | "controlled": controlled, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 431 | "operation": p.GetOperation(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 432 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 433 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 434 | 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 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 438 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 439 | result := p.GetRoot().Remove(ctx, fullPath, txid, nil) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 440 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 441 | if result != nil { |
| 442 | return result.GetData() |
| 443 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 444 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 445 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 446 | } |
| 447 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 448 | // CreateProxy to interact with specific path directly |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 449 | func (p *Proxy) CreateProxy(ctx context.Context, path string, exclusive bool) *Proxy { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 450 | 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 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 467 | p.SetOperation(PROXY_CREATE) |
| 468 | defer p.SetOperation(PROXY_NONE) |
| 469 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 470 | log.Debugw("proxy-create", log.Fields{ |
| 471 | "path": path, |
| 472 | "effective": effectivePath, |
| 473 | "full": fullPath, |
| 474 | "pathLock": pathLock, |
| 475 | "controlled": controlled, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 476 | "operation": p.GetOperation(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 477 | }) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 478 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 479 | 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 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 483 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 484 | return p.GetRoot().CreateProxy(ctx, fullPath, exclusive) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 485 | } |
| 486 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 487 | // OpenTransaction creates a new transaction branch to isolate operations made to the data model |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 488 | func (p *Proxy) OpenTransaction() *Transaction { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 489 | txid := p.GetRoot().MakeTxBranch() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 490 | return NewTransaction(p, txid) |
| 491 | } |
| 492 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 493 | // commitTransaction will apply and merge modifications made in the transaction branch to the data model |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 494 | func (p *Proxy) commitTransaction(txid string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 495 | p.GetRoot().FoldTxBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 496 | } |
| 497 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 498 | // cancelTransaction will terminate a transaction branch along will all changes within it |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 499 | func (p *Proxy) cancelTransaction(txid string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 500 | p.GetRoot().DeleteTxBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 501 | } |
| 502 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 503 | // CallbackFunction is a type used to define callback functions |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 504 | type CallbackFunction func(args ...interface{}) interface{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 505 | |
| 506 | // CallbackTuple holds the function and arguments details of a callback |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 507 | type CallbackTuple struct { |
| 508 | callback CallbackFunction |
| 509 | args []interface{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 510 | } |
| 511 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 512 | // Execute will process the a callback with its provided arguments |
| 513 | func (tuple *CallbackTuple) Execute(contextArgs []interface{}) interface{} { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 514 | args := []interface{}{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 515 | |
| 516 | for _, ta := range tuple.args { |
| 517 | args = append(args, ta) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 518 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 519 | |
| 520 | if contextArgs != nil { |
| 521 | for _, ca := range contextArgs { |
| 522 | args = append(args, ca) |
| 523 | } |
| 524 | } |
| 525 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 526 | return tuple.callback(args...) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 527 | } |
| 528 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 529 | // RegisterCallback associates a callback to the proxy |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 530 | func (p *Proxy) RegisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 531 | if p.getCallbacks(callbackType) == nil { |
| 532 | p.setCallbacks(callbackType, make(map[string]*CallbackTuple)) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 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 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 538 | p.setCallback(callbackType, funcHash, &CallbackTuple{callback, args}) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 539 | } |
| 540 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 541 | // UnregisterCallback removes references to a callback within a proxy |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 542 | func (p *Proxy) UnregisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 543 | if p.getCallbacks(callbackType) == nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 544 | log.Errorf("no such callback type - %s", callbackType.String()) |
| 545 | return |
| 546 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 547 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 548 | funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name() |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 549 | funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 550 | |
| 551 | log.Debugf("value of function: %s", funcName) |
| 552 | |
| 553 | if p.getCallback(callbackType, funcHash) == nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 554 | log.Errorf("function with hash value: '%s' not registered with callback type: '%s'", funcHash, callbackType) |
| 555 | return |
| 556 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 557 | |
| 558 | p.DeleteCallback(callbackType, funcHash) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 559 | } |
| 560 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 561 | func (p *Proxy) invoke(callback *CallbackTuple, context []interface{}) (result interface{}, err error) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 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 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 575 | // InvokeCallbacks executes all callbacks associated to a specific type |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 576 | func (p *Proxy) InvokeCallbacks(args ...interface{}) (result interface{}) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 577 | callbackType := args[0].(CallbackType) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 578 | proceedOnError := args[1].(bool) |
| 579 | context := args[2:] |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 580 | |
| 581 | var err error |
| 582 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 583 | if callbacks := p.getCallbacks(callbackType); callbacks != nil { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 584 | p.mutex.Lock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 585 | for _, callback := range callbacks { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 586 | if result, err = p.invoke(callback, context); err != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 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 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame^] | 594 | p.mutex.Unlock() |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 595 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 596 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 597 | return result |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 598 | } |