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