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