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