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" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 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 |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 217 | func (p *Proxy) List(ctx context.Context, path string, depth int, deep bool, txid string) (interface{}, error) { |
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 | }) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 237 | return p.GetRoot().List(ctx, path, "", depth, deep, txid) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 238 | } |
| 239 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 240 | // Get will retrieve information from the data model at the specified path location |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 241 | func (p *Proxy) Get(ctx context.Context, path string, depth int, deep bool, txid string) (interface{}, error) { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 242 | var effectivePath string |
| 243 | if path == "/" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 244 | effectivePath = p.getFullPath() |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 245 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 246 | effectivePath = p.getFullPath() + path |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 250 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 251 | p.SetOperation(PROXY_GET) |
| 252 | defer p.SetOperation(PROXY_NONE) |
| 253 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 254 | log.Debugw("proxy-get", log.Fields{ |
| 255 | "path": path, |
| 256 | "effective": effectivePath, |
| 257 | "pathLock": pathLock, |
| 258 | "controlled": controlled, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 259 | "operation": p.GetOperation(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 260 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 261 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 262 | return p.GetRoot().Get(ctx, path, "", depth, deep, txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 263 | } |
| 264 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 265 | // Update will modify information in the data model at the specified location with the provided data |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 266 | func (p *Proxy) Update(ctx context.Context, path string, data interface{}, strict bool, txid string) (interface{}, error) { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 267 | if !strings.HasPrefix(path, "/") { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 268 | log.Errorf("invalid path: %s", path) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 269 | return nil, fmt.Errorf("invalid path: %s", path) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 270 | } |
| 271 | var fullPath string |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 272 | var effectivePath string |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 273 | if path == "/" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 274 | fullPath = p.getPath() |
| 275 | effectivePath = p.getFullPath() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 276 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 277 | fullPath = p.getPath() + path |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 278 | effectivePath = p.getFullPath() + path |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 279 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 280 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 281 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 282 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 283 | p.SetOperation(PROXY_UPDATE) |
| 284 | defer p.SetOperation(PROXY_NONE) |
| 285 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 286 | log.Debugw("proxy-update", log.Fields{ |
| 287 | "path": path, |
| 288 | "effective": effectivePath, |
| 289 | "full": fullPath, |
| 290 | "pathLock": pathLock, |
| 291 | "controlled": controlled, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 292 | "operation": p.GetOperation(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 293 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 294 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 295 | if p.GetRoot().KvStore != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 296 | if _, err := p.GetRoot().KvStore.Client.Reserve(pathLock+"_", uuid.New().String(), ReservationTTL); err != nil { |
| 297 | log.Errorw("unable-to-acquire-key-from-kvstore", log.Fields{"error": err}) |
| 298 | return nil, err |
| 299 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 300 | defer p.GetRoot().KvStore.Client.ReleaseReservation(pathLock + "_") |
| 301 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 302 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 303 | result := p.GetRoot().Update(ctx, fullPath, data, strict, txid, nil) |
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 | if result != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 306 | return result.GetData(), nil |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 307 | } |
| 308 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 309 | return nil, nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 310 | } |
| 311 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 312 | // AddWithID will insert new data at specified location. |
| 313 | // This method also allows the user to specify the ID of the data entry to ensure |
| 314 | // that access control is active while inserting the information. |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 315 | func (p *Proxy) AddWithID(ctx context.Context, path string, id string, data interface{}, txid string) (interface{}, error) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 316 | if !strings.HasPrefix(path, "/") { |
| 317 | log.Errorf("invalid path: %s", path) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 318 | return nil, fmt.Errorf("invalid path: %s", path) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 319 | } |
| 320 | var fullPath string |
| 321 | var effectivePath string |
| 322 | if path == "/" { |
| 323 | fullPath = p.getPath() |
| 324 | effectivePath = p.getFullPath() |
| 325 | } else { |
| 326 | fullPath = p.getPath() + path |
| 327 | effectivePath = p.getFullPath() + path + "/" + id |
| 328 | } |
| 329 | |
| 330 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 331 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 332 | p.SetOperation(PROXY_ADD) |
| 333 | defer p.SetOperation(PROXY_NONE) |
| 334 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 335 | log.Debugw("proxy-add-with-id", log.Fields{ |
| 336 | "path": path, |
| 337 | "effective": effectivePath, |
| 338 | "full": fullPath, |
| 339 | "pathLock": pathLock, |
| 340 | "controlled": controlled, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 341 | "operation": p.GetOperation(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 342 | }) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 343 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 344 | if p.GetRoot().KvStore != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 345 | if _, err := p.GetRoot().KvStore.Client.Reserve(pathLock+"_", uuid.New().String(), ReservationTTL); err != nil { |
| 346 | log.Errorw("unable-to-acquire-key-from-kvstore", log.Fields{"error": err}) |
| 347 | return nil, err |
| 348 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 349 | defer p.GetRoot().KvStore.Client.ReleaseReservation(pathLock + "_") |
| 350 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 351 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 352 | result := p.GetRoot().Add(ctx, fullPath, data, txid, nil) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 353 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 354 | if result != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 355 | return result.GetData(), nil |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 356 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 357 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 358 | return nil, nil |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // Add will insert new data at specified location. |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 362 | func (p *Proxy) Add(ctx context.Context, path string, data interface{}, txid string) (interface{}, error) { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 363 | if !strings.HasPrefix(path, "/") { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 364 | log.Errorf("invalid path: %s", path) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 365 | return nil, fmt.Errorf("invalid path: %s", path) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 366 | } |
| 367 | var fullPath string |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 368 | var effectivePath string |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 369 | if path == "/" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 370 | fullPath = p.getPath() |
| 371 | effectivePath = p.getFullPath() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 372 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 373 | fullPath = p.getPath() + path |
| 374 | effectivePath = p.getFullPath() + path |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 375 | } |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 376 | |
| 377 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 378 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 379 | p.SetOperation(PROXY_ADD) |
| 380 | defer p.SetOperation(PROXY_NONE) |
| 381 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 382 | log.Debugw("proxy-add", log.Fields{ |
| 383 | "path": path, |
| 384 | "effective": effectivePath, |
| 385 | "full": fullPath, |
| 386 | "pathLock": pathLock, |
| 387 | "controlled": controlled, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 388 | "operation": p.GetOperation(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 389 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 390 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 391 | if p.GetRoot().KvStore != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 392 | if _, err := p.GetRoot().KvStore.Client.Reserve(pathLock+"_", uuid.New().String(), ReservationTTL); err != nil { |
| 393 | log.Errorw("unable-to-acquire-key-from-kvstore", log.Fields{"error": err}) |
| 394 | return nil, err |
| 395 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 396 | defer p.GetRoot().KvStore.Client.ReleaseReservation(pathLock + "_") |
| 397 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 398 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 399 | result := p.GetRoot().Add(ctx, fullPath, data, txid, nil) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 400 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 401 | if result != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 402 | return result.GetData(), nil |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 403 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 404 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 405 | return nil, nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 406 | } |
| 407 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 408 | // Remove will delete an entry at the specified location |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 409 | func (p *Proxy) Remove(ctx context.Context, path string, txid string) (interface{}, error) { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 410 | if !strings.HasPrefix(path, "/") { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 411 | log.Errorf("invalid path: %s", path) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 412 | return nil, fmt.Errorf("invalid path: %s", path) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 413 | } |
| 414 | var fullPath string |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 415 | var effectivePath string |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 416 | if path == "/" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 417 | fullPath = p.getPath() |
| 418 | effectivePath = p.getFullPath() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 419 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 420 | fullPath = p.getPath() + path |
| 421 | effectivePath = p.getFullPath() + path |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 422 | } |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 423 | |
| 424 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 425 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 426 | p.SetOperation(PROXY_REMOVE) |
| 427 | defer p.SetOperation(PROXY_NONE) |
| 428 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 429 | log.Debugw("proxy-remove", log.Fields{ |
| 430 | "path": path, |
| 431 | "effective": effectivePath, |
| 432 | "full": fullPath, |
| 433 | "pathLock": pathLock, |
| 434 | "controlled": controlled, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 435 | "operation": p.GetOperation(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 436 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 437 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 438 | if p.GetRoot().KvStore != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 439 | if _, err := p.GetRoot().KvStore.Client.Reserve(pathLock+"_", uuid.New().String(), ReservationTTL); err != nil { |
| 440 | log.Errorw("unable-to-acquire-key-from-kvstore", log.Fields{"error": err}) |
| 441 | return nil, err |
| 442 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 443 | defer p.GetRoot().KvStore.Client.ReleaseReservation(pathLock + "_") |
| 444 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 445 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 446 | result := p.GetRoot().Remove(ctx, fullPath, txid, nil) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 447 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 448 | if result != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 449 | return result.GetData(), nil |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 450 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 451 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 452 | return nil, nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 453 | } |
| 454 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 455 | // CreateProxy to interact with specific path directly |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 456 | func (p *Proxy) CreateProxy(ctx context.Context, path string, exclusive bool) (*Proxy, error) { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 457 | if !strings.HasPrefix(path, "/") { |
| 458 | log.Errorf("invalid path: %s", path) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 459 | return nil, fmt.Errorf("invalid path: %s", path) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | var fullPath string |
| 463 | var effectivePath string |
| 464 | if path == "/" { |
| 465 | fullPath = p.getPath() |
| 466 | effectivePath = p.getFullPath() |
| 467 | } else { |
| 468 | fullPath = p.getPath() + path |
| 469 | effectivePath = p.getFullPath() + path |
| 470 | } |
| 471 | |
| 472 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 473 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 474 | p.SetOperation(PROXY_CREATE) |
| 475 | defer p.SetOperation(PROXY_NONE) |
| 476 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 477 | log.Debugw("proxy-create", log.Fields{ |
| 478 | "path": path, |
| 479 | "effective": effectivePath, |
| 480 | "full": fullPath, |
| 481 | "pathLock": pathLock, |
| 482 | "controlled": controlled, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 483 | "operation": p.GetOperation(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 484 | }) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 485 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 486 | if p.GetRoot().KvStore != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 487 | if _, err := p.GetRoot().KvStore.Client.Reserve(pathLock+"_", uuid.New().String(), ReservationTTL); err != nil { |
| 488 | log.Errorw("unable-to-acquire-key-from-kvstore", log.Fields{"error": err}) |
| 489 | return nil, err |
| 490 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 491 | defer p.GetRoot().KvStore.Client.ReleaseReservation(pathLock + "_") |
| 492 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 493 | return p.GetRoot().CreateProxy(ctx, fullPath, exclusive) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 494 | } |
| 495 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 496 | // 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] | 497 | func (p *Proxy) OpenTransaction() *Transaction { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 498 | txid := p.GetRoot().MakeTxBranch() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 499 | return NewTransaction(p, txid) |
| 500 | } |
| 501 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 502 | // 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] | 503 | func (p *Proxy) commitTransaction(txid string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 504 | p.GetRoot().FoldTxBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 505 | } |
| 506 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 507 | // cancelTransaction will terminate a transaction branch along will all changes within it |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 508 | func (p *Proxy) cancelTransaction(txid string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 509 | p.GetRoot().DeleteTxBranch(txid) |
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 | // CallbackFunction is a type used to define callback functions |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 513 | type CallbackFunction func(args ...interface{}) interface{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 514 | |
| 515 | // CallbackTuple holds the function and arguments details of a callback |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 516 | type CallbackTuple struct { |
| 517 | callback CallbackFunction |
| 518 | args []interface{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 519 | } |
| 520 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 521 | // Execute will process the a callback with its provided arguments |
| 522 | func (tuple *CallbackTuple) Execute(contextArgs []interface{}) interface{} { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 523 | args := []interface{}{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 524 | |
| 525 | for _, ta := range tuple.args { |
| 526 | args = append(args, ta) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 527 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 528 | |
| 529 | if contextArgs != nil { |
| 530 | for _, ca := range contextArgs { |
| 531 | args = append(args, ca) |
| 532 | } |
| 533 | } |
| 534 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 535 | return tuple.callback(args...) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 536 | } |
| 537 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 538 | // RegisterCallback associates a callback to the proxy |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 539 | func (p *Proxy) RegisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 540 | if p.getCallbacks(callbackType) == nil { |
| 541 | p.setCallbacks(callbackType, make(map[string]*CallbackTuple)) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 542 | } |
| 543 | funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name() |
| 544 | log.Debugf("value of function: %s", funcName) |
| 545 | funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12] |
| 546 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 547 | p.setCallback(callbackType, funcHash, &CallbackTuple{callback, args}) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 548 | } |
| 549 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 550 | // UnregisterCallback removes references to a callback within a proxy |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 551 | func (p *Proxy) UnregisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 552 | if p.getCallbacks(callbackType) == nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 553 | log.Errorf("no such callback type - %s", callbackType.String()) |
| 554 | return |
| 555 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 556 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 557 | funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name() |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 558 | funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 559 | |
| 560 | log.Debugf("value of function: %s", funcName) |
| 561 | |
| 562 | if p.getCallback(callbackType, funcHash) == nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 563 | log.Errorf("function with hash value: '%s' not registered with callback type: '%s'", funcHash, callbackType) |
| 564 | return |
| 565 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 566 | |
| 567 | p.DeleteCallback(callbackType, funcHash) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 568 | } |
| 569 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 570 | func (p *Proxy) invoke(callback *CallbackTuple, context []interface{}) (result interface{}, err error) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 571 | defer func() { |
| 572 | if r := recover(); r != nil { |
| 573 | errStr := fmt.Sprintf("callback error occurred: %+v", r) |
| 574 | err = errors.New(errStr) |
| 575 | log.Error(errStr) |
| 576 | } |
| 577 | }() |
| 578 | |
| 579 | result = callback.Execute(context) |
| 580 | |
| 581 | return result, err |
| 582 | } |
| 583 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 584 | // InvokeCallbacks executes all callbacks associated to a specific type |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 585 | func (p *Proxy) InvokeCallbacks(args ...interface{}) (result interface{}) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 586 | callbackType := args[0].(CallbackType) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 587 | proceedOnError := args[1].(bool) |
| 588 | context := args[2:] |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 589 | |
| 590 | var err error |
| 591 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 592 | if callbacks := p.getCallbacks(callbackType); callbacks != nil { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 593 | p.mutex.Lock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 594 | for _, callback := range callbacks { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 595 | if result, err = p.invoke(callback, context); err != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 596 | if !proceedOnError { |
| 597 | log.Info("An error occurred. Stopping callback invocation") |
| 598 | break |
| 599 | } |
| 600 | log.Info("An error occurred. Invoking next callback") |
| 601 | } |
| 602 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 603 | p.mutex.Unlock() |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 604 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 605 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 606 | return result |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 607 | } |