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 | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 20 | "crypto/md5" |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 21 | "errors" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 22 | "fmt" |
| 23 | "github.com/opencord/voltha-go/common/log" |
| 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" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 28 | ) |
| 29 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 30 | // OperationContext holds details on the information used during an operation |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 31 | type OperationContext struct { |
| 32 | Path string |
| 33 | Data interface{} |
| 34 | FieldName string |
| 35 | ChildKey string |
| 36 | } |
| 37 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 38 | // NewOperationContext instantiates a new OperationContext structure |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 39 | func NewOperationContext(path string, data interface{}, fieldName string, childKey string) *OperationContext { |
| 40 | oc := &OperationContext{ |
| 41 | Path: path, |
| 42 | Data: data, |
| 43 | FieldName: fieldName, |
| 44 | ChildKey: childKey, |
| 45 | } |
| 46 | return oc |
| 47 | } |
| 48 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 49 | // Update applies new data to the context structure |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 50 | func (oc *OperationContext) Update(data interface{}) *OperationContext { |
| 51 | oc.Data = data |
| 52 | return oc |
| 53 | } |
| 54 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 55 | // Proxy holds the information for a specific location with the data model |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 56 | type Proxy struct { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 57 | sync.RWMutex |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 58 | Root *root |
| 59 | Node *node |
| 60 | ParentNode *node |
| 61 | Path string |
| 62 | FullPath string |
| 63 | Exclusive bool |
| 64 | Callbacks map[CallbackType]map[string]*CallbackTuple |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 67 | // NewProxy instantiates a new proxy to a specific location |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 68 | 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] | 69 | callbacks := make(map[CallbackType]map[string]*CallbackTuple) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 70 | if fullPath == "/" { |
| 71 | fullPath = "" |
| 72 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 73 | p := &Proxy{ |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 74 | Root: root, |
| 75 | Node: node, |
| 76 | ParentNode: parentNode, |
| 77 | Exclusive: exclusive, |
| 78 | Path: path, |
| 79 | FullPath: fullPath, |
| 80 | Callbacks: callbacks, |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 81 | } |
| 82 | return p |
| 83 | } |
| 84 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 85 | // GetRoot returns the root attribute of the proxy |
| 86 | func (p *Proxy) GetRoot() *root { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 87 | return p.Root |
| 88 | } |
| 89 | |
| 90 | // getPath returns the path attribute of the proxy |
| 91 | func (p *Proxy) getPath() string { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 92 | return p.Path |
| 93 | } |
| 94 | |
| 95 | // getFullPath returns the full path attribute of the proxy |
| 96 | func (p *Proxy) getFullPath() string { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 97 | return p.FullPath |
| 98 | } |
| 99 | |
| 100 | // getCallbacks returns the full list of callbacks associated to the proxy |
| 101 | func (p *Proxy) getCallbacks(callbackType CallbackType) map[string]*CallbackTuple { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 102 | if cb, exists := p.Callbacks[callbackType]; exists { |
| 103 | return cb |
| 104 | } |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | // getCallback returns a specific callback matching the type and function hash |
| 109 | func (p *Proxy) getCallback(callbackType CallbackType, funcHash string) *CallbackTuple { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 110 | if tuple, exists := p.Callbacks[callbackType][funcHash]; exists { |
| 111 | return tuple |
| 112 | } |
| 113 | return nil |
| 114 | } |
| 115 | |
| 116 | // setCallbacks applies a callbacks list to a type |
| 117 | func (p *Proxy) setCallbacks(callbackType CallbackType, callbacks map[string]*CallbackTuple) { |
| 118 | p.Lock() |
| 119 | defer p.Unlock() |
| 120 | p.Callbacks[callbackType] = callbacks |
| 121 | } |
| 122 | |
| 123 | // setCallback applies a callback to a type and hash value |
| 124 | func (p *Proxy) setCallback(callbackType CallbackType, funcHash string, tuple *CallbackTuple) { |
| 125 | p.Lock() |
| 126 | defer p.Unlock() |
| 127 | p.Callbacks[callbackType][funcHash] = tuple |
| 128 | } |
| 129 | |
| 130 | // DeleteCallback removes a callback matching the type and hash |
| 131 | func (p *Proxy) DeleteCallback(callbackType CallbackType, funcHash string) { |
| 132 | p.Lock() |
| 133 | defer p.Unlock() |
| 134 | delete(p.Callbacks[callbackType], funcHash) |
| 135 | } |
| 136 | |
| 137 | // parseForControlledPath verifies if a proxy path matches a pattern |
| 138 | // for locations that need to be access controlled. |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 139 | func (p *Proxy) parseForControlledPath(path string) (pathLock string, controlled bool) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 140 | // TODO: Add other path prefixes that may need control |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 141 | if strings.HasPrefix(path, "/devices") || |
| 142 | strings.HasPrefix(path, "/logical_devices") || |
| 143 | strings.HasPrefix(path, "/adapters") { |
| 144 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 145 | split := strings.SplitN(path, "/", -1) |
| 146 | switch len(split) { |
| 147 | case 2: |
| 148 | controlled = false |
| 149 | pathLock = "" |
| 150 | break |
| 151 | case 3: |
| 152 | fallthrough |
| 153 | default: |
| 154 | pathLock = fmt.Sprintf("%s/%s", split[1], split[2]) |
| 155 | controlled = true |
| 156 | } |
| 157 | } |
| 158 | return pathLock, controlled |
| 159 | } |
| 160 | |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 161 | // List will retrieve information from the data model at the specified path location |
| 162 | // A list operation will force access to persistence storage |
| 163 | func (p *Proxy) List(path string, depth int, deep bool, txid string) interface{} { |
| 164 | var effectivePath string |
| 165 | if path == "/" { |
| 166 | effectivePath = p.getFullPath() |
| 167 | } else { |
| 168 | effectivePath = p.getFullPath() + path |
| 169 | } |
| 170 | |
| 171 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 172 | |
| 173 | log.Debugf("Path: %s, Effective: %s, PathLock: %s", path, effectivePath, pathLock) |
| 174 | |
| 175 | pac := PAC().ReservePath(effectivePath, p, pathLock) |
| 176 | defer PAC().ReleasePath(pathLock) |
| 177 | pac.SetProxy(p) |
| 178 | |
| 179 | rv := pac.List(path, depth, deep, txid, controlled) |
| 180 | |
| 181 | return rv |
| 182 | } |
| 183 | |
| 184 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 185 | // Get will retrieve information from the data model at the specified path location |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 186 | func (p *Proxy) Get(path string, depth int, deep bool, txid string) interface{} { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 187 | var effectivePath string |
| 188 | if path == "/" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 189 | effectivePath = p.getFullPath() |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 190 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 191 | effectivePath = p.getFullPath() + path |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 195 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 196 | log.Debugf("Path: %s, Effective: %s, PathLock: %s", path, effectivePath, pathLock) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 197 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 198 | pac := PAC().ReservePath(effectivePath, p, pathLock) |
| 199 | defer PAC().ReleasePath(pathLock) |
| 200 | pac.SetProxy(p) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 201 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 202 | rv := pac.Get(path, depth, deep, txid, controlled) |
| 203 | |
| 204 | return rv |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 207 | // Update will modify information in the data model at the specified location with the provided data |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 208 | func (p *Proxy) Update(path string, data interface{}, strict bool, txid string) interface{} { |
| 209 | if !strings.HasPrefix(path, "/") { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 210 | log.Errorf("invalid path: %s", path) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 211 | return nil |
| 212 | } |
| 213 | var fullPath string |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 214 | var effectivePath string |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 215 | if path == "/" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 216 | fullPath = p.getPath() |
| 217 | effectivePath = p.getFullPath() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 218 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 219 | fullPath = p.getPath() + path |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 220 | effectivePath = p.getFullPath() + path |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 221 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 222 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 223 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 224 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 225 | log.Debugf("Path: %s, Effective: %s, Full: %s, PathLock: %s", path, effectivePath, fullPath, pathLock) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 226 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 227 | pac := PAC().ReservePath(effectivePath, p, pathLock) |
| 228 | defer PAC().ReleasePath(pathLock) |
| 229 | pac.SetProxy(p) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 230 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 231 | return pac.Update(fullPath, data, strict, txid, controlled) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 232 | } |
| 233 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 234 | // AddWithID will insert new data at specified location. |
| 235 | // This method also allows the user to specify the ID of the data entry to ensure |
| 236 | // that access control is active while inserting the information. |
| 237 | func (p *Proxy) AddWithID(path string, id string, data interface{}, txid string) interface{} { |
| 238 | if !strings.HasPrefix(path, "/") { |
| 239 | log.Errorf("invalid path: %s", path) |
| 240 | return nil |
| 241 | } |
| 242 | var fullPath string |
| 243 | var effectivePath string |
| 244 | if path == "/" { |
| 245 | fullPath = p.getPath() |
| 246 | effectivePath = p.getFullPath() |
| 247 | } else { |
| 248 | fullPath = p.getPath() + path |
| 249 | effectivePath = p.getFullPath() + path + "/" + id |
| 250 | } |
| 251 | |
| 252 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 253 | |
| 254 | log.Debugf("Path: %s, Effective: %s, Full: %s, PathLock: %s", path, effectivePath, fullPath, pathLock) |
| 255 | |
| 256 | pac := PAC().ReservePath(path, p, pathLock) |
| 257 | defer PAC().ReleasePath(pathLock) |
| 258 | pac.SetProxy(p) |
| 259 | |
| 260 | return pac.Add(fullPath, data, txid, controlled) |
| 261 | } |
| 262 | |
| 263 | // Add will insert new data at specified location. |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 264 | func (p *Proxy) Add(path string, data interface{}, txid string) interface{} { |
| 265 | if !strings.HasPrefix(path, "/") { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 266 | log.Errorf("invalid path: %s", path) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 267 | return nil |
| 268 | } |
| 269 | var fullPath string |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 270 | var effectivePath string |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 271 | if path == "/" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 272 | fullPath = p.getPath() |
| 273 | effectivePath = p.getFullPath() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 274 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 275 | fullPath = p.getPath() + path |
| 276 | effectivePath = p.getFullPath() + path |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 277 | } |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 278 | |
| 279 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 280 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 281 | log.Debugf("Path: %s, Effective: %s, Full: %s, PathLock: %s", path, effectivePath, fullPath, pathLock) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 282 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 283 | pac := PAC().ReservePath(path, p, pathLock) |
| 284 | defer PAC().ReleasePath(pathLock) |
| 285 | pac.SetProxy(p) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 286 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 287 | return pac.Add(fullPath, data, txid, controlled) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 290 | // Remove will delete an entry at the specified location |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 291 | func (p *Proxy) Remove(path string, txid string) interface{} { |
| 292 | if !strings.HasPrefix(path, "/") { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 293 | log.Errorf("invalid path: %s", path) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 294 | return nil |
| 295 | } |
| 296 | var fullPath string |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 297 | var effectivePath string |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 298 | if path == "/" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 299 | fullPath = p.getPath() |
| 300 | effectivePath = p.getFullPath() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 301 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 302 | fullPath = p.getPath() + path |
| 303 | effectivePath = p.getFullPath() + path |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 304 | } |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 305 | |
| 306 | pathLock, controlled := p.parseForControlledPath(effectivePath) |
| 307 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 308 | log.Debugf("Path: %s, Effective: %s, Full: %s, PathLock: %s", path, effectivePath, fullPath, pathLock) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 309 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 310 | pac := PAC().ReservePath(effectivePath, p, pathLock) |
| 311 | defer PAC().ReleasePath(pathLock) |
| 312 | pac.SetProxy(p) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 313 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 314 | return pac.Remove(fullPath, txid, controlled) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 317 | // 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] | 318 | func (p *Proxy) OpenTransaction() *Transaction { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 319 | txid := p.GetRoot().MakeTxBranch() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 320 | return NewTransaction(p, txid) |
| 321 | } |
| 322 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 323 | // 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] | 324 | func (p *Proxy) commitTransaction(txid string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 325 | p.GetRoot().FoldTxBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 328 | // cancelTransaction will terminate a transaction branch along will all changes within it |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 329 | func (p *Proxy) cancelTransaction(txid string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 330 | p.GetRoot().DeleteTxBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 331 | } |
| 332 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 333 | // CallbackFunction is a type used to define callback functions |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 334 | type CallbackFunction func(args ...interface{}) interface{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 335 | |
| 336 | // CallbackTuple holds the function and arguments details of a callback |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 337 | type CallbackTuple struct { |
| 338 | callback CallbackFunction |
| 339 | args []interface{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 340 | } |
| 341 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 342 | // Execute will process the a callback with its provided arguments |
| 343 | func (tuple *CallbackTuple) Execute(contextArgs []interface{}) interface{} { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 344 | args := []interface{}{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 345 | |
| 346 | for _, ta := range tuple.args { |
| 347 | args = append(args, ta) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 348 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 349 | |
| 350 | if contextArgs != nil { |
| 351 | for _, ca := range contextArgs { |
| 352 | args = append(args, ca) |
| 353 | } |
| 354 | } |
| 355 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 356 | return tuple.callback(args...) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 357 | } |
| 358 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 359 | // RegisterCallback associates a callback to the proxy |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 360 | func (p *Proxy) RegisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 361 | if p.getCallbacks(callbackType) == nil { |
| 362 | p.setCallbacks(callbackType, make(map[string]*CallbackTuple)) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 363 | } |
| 364 | funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name() |
| 365 | log.Debugf("value of function: %s", funcName) |
| 366 | funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12] |
| 367 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 368 | p.setCallback(callbackType, funcHash, &CallbackTuple{callback, args}) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 369 | } |
| 370 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 371 | // UnregisterCallback removes references to a callback within a proxy |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 372 | func (p *Proxy) UnregisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 373 | if p.getCallbacks(callbackType) == nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 374 | log.Errorf("no such callback type - %s", callbackType.String()) |
| 375 | return |
| 376 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 377 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 378 | funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name() |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 379 | funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 380 | |
| 381 | log.Debugf("value of function: %s", funcName) |
| 382 | |
| 383 | if p.getCallback(callbackType, funcHash) == nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 384 | log.Errorf("function with hash value: '%s' not registered with callback type: '%s'", funcHash, callbackType) |
| 385 | return |
| 386 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 387 | |
| 388 | p.DeleteCallback(callbackType, funcHash) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 389 | } |
| 390 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 391 | func (p *Proxy) invoke(callback *CallbackTuple, context []interface{}) (result interface{}, err error) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 392 | defer func() { |
| 393 | if r := recover(); r != nil { |
| 394 | errStr := fmt.Sprintf("callback error occurred: %+v", r) |
| 395 | err = errors.New(errStr) |
| 396 | log.Error(errStr) |
| 397 | } |
| 398 | }() |
| 399 | |
| 400 | result = callback.Execute(context) |
| 401 | |
| 402 | return result, err |
| 403 | } |
| 404 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 405 | // InvokeCallbacks executes all callbacks associated to a specific type |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 406 | func (p *Proxy) InvokeCallbacks(args ...interface{}) (result interface{}) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 407 | callbackType := args[0].(CallbackType) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 408 | proceedOnError := args[1].(bool) |
| 409 | context := args[2:] |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 410 | |
| 411 | var err error |
| 412 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 413 | if callbacks := p.getCallbacks(callbackType); callbacks != nil { |
| 414 | p.Lock() |
| 415 | for _, callback := range callbacks { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 416 | if result, err = p.invoke(callback, context); err != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 417 | if !proceedOnError { |
| 418 | log.Info("An error occurred. Stopping callback invocation") |
| 419 | break |
| 420 | } |
| 421 | log.Info("An error occurred. Invoking next callback") |
| 422 | } |
| 423 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 424 | p.Unlock() |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 425 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 426 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 427 | return result |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 428 | } |