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