blob: cfb2ef1b3f3043d1b090692b0a9d54708ee30fda [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 Barbariedc5022d2018-11-19 15:21:44 -050016
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040017package model
18
19import (
Stephane Barbarie694e2b92018-09-07 12:17:36 -040020 "crypto/md5"
Stephane Barbarie694e2b92018-09-07 12:17:36 -040021 "errors"
khenaidoob9203542018-09-17 22:56:37 -040022 "fmt"
23 "github.com/opencord/voltha-go/common/log"
24 "reflect"
Stephane Barbarie694e2b92018-09-07 12:17:36 -040025 "runtime"
khenaidoob9203542018-09-17 22:56:37 -040026 "strings"
Stephane Barbariea188d942018-10-16 16:43:04 -040027 "sync"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040028)
29
Stephane Barbariedc5022d2018-11-19 15:21:44 -050030// OperationContext holds details on the information used during an operation
Stephane Barbarieec0919b2018-09-05 14:14:29 -040031type OperationContext struct {
32 Path string
33 Data interface{}
34 FieldName string
35 ChildKey string
36}
37
Stephane Barbariedc5022d2018-11-19 15:21:44 -050038// NewOperationContext instantiates a new OperationContext structure
Stephane Barbarieec0919b2018-09-05 14:14:29 -040039func 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 Barbariedc5022d2018-11-19 15:21:44 -050049// Update applies new data to the context structure
Stephane Barbarieec0919b2018-09-05 14:14:29 -040050func (oc *OperationContext) Update(data interface{}) *OperationContext {
51 oc.Data = data
52 return oc
53}
54
Stephane Barbariedc5022d2018-11-19 15:21:44 -050055// Proxy holds the information for a specific location with the data model
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040056type Proxy struct {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050057 sync.RWMutex
Stephane Barbarie1ab43272018-12-08 21:42:13 -050058 Root *root
59 Node *node
60 ParentNode *node
61 Path string
62 FullPath string
63 Exclusive bool
64 Callbacks map[CallbackType]map[string]*CallbackTuple
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040065}
66
Stephane Barbariedc5022d2018-11-19 15:21:44 -050067// NewProxy instantiates a new proxy to a specific location
Stephane Barbarie1ab43272018-12-08 21:42:13 -050068func NewProxy(root *root, node *node, parentNode *node, path string, fullPath string, exclusive bool) *Proxy {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050069 callbacks := make(map[CallbackType]map[string]*CallbackTuple)
Stephane Barbariea188d942018-10-16 16:43:04 -040070 if fullPath == "/" {
71 fullPath = ""
72 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040073 p := &Proxy{
Stephane Barbarie1ab43272018-12-08 21:42:13 -050074 Root: root,
75 Node: node,
76 ParentNode: parentNode,
77 Exclusive: exclusive,
78 Path: path,
79 FullPath: fullPath,
80 Callbacks: callbacks,
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040081 }
82 return p
83}
84
Stephane Barbariedc5022d2018-11-19 15:21:44 -050085// GetRoot returns the root attribute of the proxy
86func (p *Proxy) GetRoot() *root {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050087 return p.Root
88}
89
90// getPath returns the path attribute of the proxy
91func (p *Proxy) getPath() string {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050092 return p.Path
93}
94
95// getFullPath returns the full path attribute of the proxy
96func (p *Proxy) getFullPath() string {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050097 return p.FullPath
98}
99
100// getCallbacks returns the full list of callbacks associated to the proxy
101func (p *Proxy) getCallbacks(callbackType CallbackType) map[string]*CallbackTuple {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500102 if cb, exists := p.Callbacks[callbackType]; exists {
103 return cb
104 }
105 return nil
106}
107
108// getCallback returns a specific callback matching the type and function hash
109func (p *Proxy) getCallback(callbackType CallbackType, funcHash string) *CallbackTuple {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500110 if tuple, exists := p.Callbacks[callbackType][funcHash]; exists {
111 return tuple
112 }
113 return nil
114}
115
116// setCallbacks applies a callbacks list to a type
117func (p *Proxy) setCallbacks(callbackType CallbackType, callbacks map[string]*CallbackTuple) {
118 p.Lock()
119 defer p.Unlock()
120 p.Callbacks[callbackType] = callbacks
121}
122
123// setCallback applies a callback to a type and hash value
124func (p *Proxy) setCallback(callbackType CallbackType, funcHash string, tuple *CallbackTuple) {
125 p.Lock()
126 defer p.Unlock()
127 p.Callbacks[callbackType][funcHash] = tuple
128}
129
130// DeleteCallback removes a callback matching the type and hash
131func (p *Proxy) DeleteCallback(callbackType CallbackType, funcHash string) {
132 p.Lock()
133 defer p.Unlock()
134 delete(p.Callbacks[callbackType], funcHash)
135}
136
137// parseForControlledPath verifies if a proxy path matches a pattern
138// for locations that need to be access controlled.
Stephane Barbariea188d942018-10-16 16:43:04 -0400139func (p *Proxy) parseForControlledPath(path string) (pathLock string, controlled bool) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500140 // TODO: Add other path prefixes that may need control
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500141 if strings.HasPrefix(path, "/devices") ||
142 strings.HasPrefix(path, "/logical_devices") ||
143 strings.HasPrefix(path, "/adapters") {
144
Stephane Barbariea188d942018-10-16 16:43:04 -0400145 split := strings.SplitN(path, "/", -1)
146 switch len(split) {
147 case 2:
148 controlled = false
149 pathLock = ""
150 break
151 case 3:
152 fallthrough
153 default:
154 pathLock = fmt.Sprintf("%s/%s", split[1], split[2])
155 controlled = true
156 }
157 }
158 return pathLock, controlled
159}
160
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500161// Get will retrieve information from the data model at the specified path location
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400162func (p *Proxy) Get(path string, depth int, deep bool, txid string) interface{} {
Stephane Barbariea188d942018-10-16 16:43:04 -0400163 var effectivePath string
164 if path == "/" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500165 effectivePath = p.getFullPath()
Stephane Barbariea188d942018-10-16 16:43:04 -0400166 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500167 effectivePath = p.getFullPath() + path
Stephane Barbariea188d942018-10-16 16:43:04 -0400168 }
169
170 pathLock, controlled := p.parseForControlledPath(effectivePath)
171
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500172 log.Debugf("Path: %s, Effective: %s, PathLock: %s", path, effectivePath, pathLock)
Stephane Barbariea188d942018-10-16 16:43:04 -0400173
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500174 pac := PAC().ReservePath(effectivePath, p, pathLock)
175 defer PAC().ReleasePath(pathLock)
176 pac.SetProxy(p)
Stephane Barbariea188d942018-10-16 16:43:04 -0400177
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500178 rv := pac.Get(path, depth, deep, txid, controlled)
179
180 return rv
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400181}
182
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500183// Update will modify information in the data model at the specified location with the provided data
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400184func (p *Proxy) Update(path string, data interface{}, strict bool, txid string) interface{} {
185 if !strings.HasPrefix(path, "/") {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400186 log.Errorf("invalid path: %s", path)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400187 return nil
188 }
189 var fullPath string
Stephane Barbariea188d942018-10-16 16:43:04 -0400190 var effectivePath string
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400191 if path == "/" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500192 fullPath = p.getPath()
193 effectivePath = p.getFullPath()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400194 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500195 fullPath = p.getPath() + path
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500196 effectivePath = p.getFullPath() + path
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400197 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400198
Stephane Barbariea188d942018-10-16 16:43:04 -0400199 pathLock, controlled := p.parseForControlledPath(effectivePath)
200
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500201 log.Debugf("Path: %s, Effective: %s, Full: %s, PathLock: %s", path, effectivePath, fullPath, pathLock)
Stephane Barbariea188d942018-10-16 16:43:04 -0400202
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500203 pac := PAC().ReservePath(effectivePath, p, pathLock)
204 defer PAC().ReleasePath(pathLock)
205 pac.SetProxy(p)
Stephane Barbariea188d942018-10-16 16:43:04 -0400206
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500207 return pac.Update(fullPath, data, strict, txid, controlled)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400208}
209
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500210// AddWithID will insert new data at specified location.
211// This method also allows the user to specify the ID of the data entry to ensure
212// that access control is active while inserting the information.
213func (p *Proxy) AddWithID(path string, id string, data interface{}, txid string) interface{} {
214 if !strings.HasPrefix(path, "/") {
215 log.Errorf("invalid path: %s", path)
216 return nil
217 }
218 var fullPath string
219 var effectivePath string
220 if path == "/" {
221 fullPath = p.getPath()
222 effectivePath = p.getFullPath()
223 } else {
224 fullPath = p.getPath() + path
225 effectivePath = p.getFullPath() + path + "/" + id
226 }
227
228 pathLock, controlled := p.parseForControlledPath(effectivePath)
229
230 log.Debugf("Path: %s, Effective: %s, Full: %s, PathLock: %s", path, effectivePath, fullPath, pathLock)
231
232 pac := PAC().ReservePath(path, p, pathLock)
233 defer PAC().ReleasePath(pathLock)
234 pac.SetProxy(p)
235
236 return pac.Add(fullPath, data, txid, controlled)
237}
238
239// Add will insert new data at specified location.
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400240func (p *Proxy) Add(path string, data interface{}, txid string) interface{} {
241 if !strings.HasPrefix(path, "/") {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400242 log.Errorf("invalid path: %s", path)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400243 return nil
244 }
245 var fullPath string
Stephane Barbariea188d942018-10-16 16:43:04 -0400246 var effectivePath string
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400247 if path == "/" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500248 fullPath = p.getPath()
249 effectivePath = p.getFullPath()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400250 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500251 fullPath = p.getPath() + path
252 effectivePath = p.getFullPath() + path
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400253 }
Stephane Barbariea188d942018-10-16 16:43:04 -0400254
255 pathLock, controlled := p.parseForControlledPath(effectivePath)
256
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500257 log.Debugf("Path: %s, Effective: %s, Full: %s, PathLock: %s", path, effectivePath, fullPath, pathLock)
Stephane Barbariea188d942018-10-16 16:43:04 -0400258
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500259 pac := PAC().ReservePath(path, p, pathLock)
260 defer PAC().ReleasePath(pathLock)
261 pac.SetProxy(p)
Stephane Barbariea188d942018-10-16 16:43:04 -0400262
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500263 return pac.Add(fullPath, data, txid, controlled)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400264}
265
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500266// Remove will delete an entry at the specified location
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400267func (p *Proxy) Remove(path string, txid string) interface{} {
268 if !strings.HasPrefix(path, "/") {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400269 log.Errorf("invalid path: %s", path)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400270 return nil
271 }
272 var fullPath string
Stephane Barbariea188d942018-10-16 16:43:04 -0400273 var effectivePath string
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400274 if path == "/" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500275 fullPath = p.getPath()
276 effectivePath = p.getFullPath()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400277 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500278 fullPath = p.getPath() + path
279 effectivePath = p.getFullPath() + path
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400280 }
Stephane Barbariea188d942018-10-16 16:43:04 -0400281
282 pathLock, controlled := p.parseForControlledPath(effectivePath)
283
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500284 log.Debugf("Path: %s, Effective: %s, Full: %s, PathLock: %s", path, effectivePath, fullPath, pathLock)
Stephane Barbariea188d942018-10-16 16:43:04 -0400285
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500286 pac := PAC().ReservePath(effectivePath, p, pathLock)
287 defer PAC().ReleasePath(pathLock)
288 pac.SetProxy(p)
Stephane Barbariea188d942018-10-16 16:43:04 -0400289
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500290 return pac.Remove(fullPath, txid, controlled)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400291}
292
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500293// OpenTransaction creates a new transaction branch to isolate operations made to the data model
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400294func (p *Proxy) OpenTransaction() *Transaction {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500295 txid := p.GetRoot().MakeTxBranch()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400296 return NewTransaction(p, txid)
297}
298
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500299// commitTransaction will apply and merge modifications made in the transaction branch to the data model
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400300func (p *Proxy) commitTransaction(txid string) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500301 p.GetRoot().FoldTxBranch(txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400302}
303
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500304// cancelTransaction will terminate a transaction branch along will all changes within it
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400305func (p *Proxy) cancelTransaction(txid string) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500306 p.GetRoot().DeleteTxBranch(txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400307}
308
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500309// CallbackFunction is a type used to define callback functions
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400310type CallbackFunction func(args ...interface{}) interface{}
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500311
312// CallbackTuple holds the function and arguments details of a callback
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400313type CallbackTuple struct {
314 callback CallbackFunction
315 args []interface{}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400316}
317
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500318// Execute will process the a callback with its provided arguments
319func (tuple *CallbackTuple) Execute(contextArgs []interface{}) interface{} {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400320 args := []interface{}{}
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500321
322 for _, ta := range tuple.args {
323 args = append(args, ta)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400324 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500325
326 if contextArgs != nil {
327 for _, ca := range contextArgs {
328 args = append(args, ca)
329 }
330 }
331
Stephane Barbarie126101e2018-10-11 16:18:48 -0400332 return tuple.callback(args...)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400333}
334
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500335// RegisterCallback associates a callback to the proxy
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400336func (p *Proxy) RegisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500337 if p.getCallbacks(callbackType) == nil {
338 p.setCallbacks(callbackType, make(map[string]*CallbackTuple))
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400339 }
340 funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name()
341 log.Debugf("value of function: %s", funcName)
342 funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12]
343
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500344 p.setCallback(callbackType, funcHash, &CallbackTuple{callback, args})
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400345}
346
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500347// UnregisterCallback removes references to a callback within a proxy
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400348func (p *Proxy) UnregisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500349 if p.getCallbacks(callbackType) == nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400350 log.Errorf("no such callback type - %s", callbackType.String())
351 return
352 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500353
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400354 funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name()
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400355 funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500356
357 log.Debugf("value of function: %s", funcName)
358
359 if p.getCallback(callbackType, funcHash) == nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400360 log.Errorf("function with hash value: '%s' not registered with callback type: '%s'", funcHash, callbackType)
361 return
362 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500363
364 p.DeleteCallback(callbackType, funcHash)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400365}
366
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500367func (p *Proxy) invoke(callback *CallbackTuple, context []interface{}) (result interface{}, err error) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400368 defer func() {
369 if r := recover(); r != nil {
370 errStr := fmt.Sprintf("callback error occurred: %+v", r)
371 err = errors.New(errStr)
372 log.Error(errStr)
373 }
374 }()
375
376 result = callback.Execute(context)
377
378 return result, err
379}
380
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500381// InvokeCallbacks executes all callbacks associated to a specific type
Stephane Barbarie126101e2018-10-11 16:18:48 -0400382func (p *Proxy) InvokeCallbacks(args ...interface{}) (result interface{}) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400383 callbackType := args[0].(CallbackType)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400384 proceedOnError := args[1].(bool)
385 context := args[2:]
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400386
387 var err error
388
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500389 if callbacks := p.getCallbacks(callbackType); callbacks != nil {
390 p.Lock()
391 for _, callback := range callbacks {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400392 if result, err = p.invoke(callback, context); err != nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400393 if !proceedOnError {
394 log.Info("An error occurred. Stopping callback invocation")
395 break
396 }
397 log.Info("An error occurred. Invoking next callback")
398 }
399 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500400 p.Unlock()
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400401 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500402
Stephane Barbariea188d942018-10-16 16:43:04 -0400403 return result
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400404}