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 | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 16 | package model |
| 17 | |
| 18 | import ( |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 19 | "crypto/md5" |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 20 | "errors" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 21 | "fmt" |
| 22 | "github.com/opencord/voltha-go/common/log" |
| 23 | "reflect" |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 24 | "runtime" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 25 | "strings" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 26 | ) |
| 27 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 28 | type OperationContext struct { |
| 29 | Path string |
| 30 | Data interface{} |
| 31 | FieldName string |
| 32 | ChildKey string |
| 33 | } |
| 34 | |
| 35 | func NewOperationContext(path string, data interface{}, fieldName string, childKey string) *OperationContext { |
| 36 | oc := &OperationContext{ |
| 37 | Path: path, |
| 38 | Data: data, |
| 39 | FieldName: fieldName, |
| 40 | ChildKey: childKey, |
| 41 | } |
| 42 | return oc |
| 43 | } |
| 44 | |
| 45 | func (oc *OperationContext) Update(data interface{}) *OperationContext { |
| 46 | oc.Data = data |
| 47 | return oc |
| 48 | } |
| 49 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 50 | type Proxy struct { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 51 | Root *root |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 52 | Path string |
| 53 | Exclusive bool |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 54 | Callbacks map[CallbackType]map[string]CallbackTuple |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 57 | func NewProxy(root *root, path string, exclusive bool) *Proxy { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 58 | callbacks := make(map[CallbackType]map[string]CallbackTuple) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 59 | p := &Proxy{ |
| 60 | Root: root, |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 61 | Exclusive: exclusive, |
| 62 | Path: path, |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 63 | Callbacks: callbacks, |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 64 | } |
| 65 | return p |
| 66 | } |
| 67 | |
| 68 | func (p *Proxy) Get(path string, depth int, deep bool, txid string) interface{} { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 69 | return p.Root.Get(path, "", depth, deep, txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | func (p *Proxy) Update(path string, data interface{}, strict bool, txid string) interface{} { |
| 73 | if !strings.HasPrefix(path, "/") { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 74 | log.Errorf("invalid path: %s", path) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 75 | return nil |
| 76 | } |
| 77 | var fullPath string |
| 78 | if path == "/" { |
| 79 | fullPath = p.Path |
| 80 | } else { |
| 81 | fullPath = p.Path + path |
| 82 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 83 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 84 | return p.Root.Update(fullPath, data, strict, txid, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | func (p *Proxy) Add(path string, data interface{}, txid string) interface{} { |
| 88 | if !strings.HasPrefix(path, "/") { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 89 | log.Errorf("invalid path: %s", path) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 90 | return nil |
| 91 | } |
| 92 | var fullPath string |
| 93 | if path == "/" { |
| 94 | fullPath = p.Path |
| 95 | } else { |
| 96 | fullPath = p.Path + path |
| 97 | } |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 98 | return p.Root.Add(fullPath, data, txid, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func (p *Proxy) Remove(path string, txid string) interface{} { |
| 102 | if !strings.HasPrefix(path, "/") { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 103 | log.Errorf("invalid path: %s", path) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 104 | return nil |
| 105 | } |
| 106 | var fullPath string |
| 107 | if path == "/" { |
| 108 | fullPath = p.Path |
| 109 | } else { |
| 110 | fullPath = p.Path + path |
| 111 | } |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 112 | return p.Root.Remove(fullPath, txid, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 115 | func (p *Proxy) OpenTransaction() *Transaction { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 116 | txid := p.Root.MakeTxBranch() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 117 | return NewTransaction(p, txid) |
| 118 | } |
| 119 | |
| 120 | func (p *Proxy) commitTransaction(txid string) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 121 | p.Root.FoldTxBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | func (p *Proxy) cancelTransaction(txid string) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 125 | p.Root.DeleteTxBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 128 | type CallbackFunction func(args ...interface{}) interface{} |
| 129 | type CallbackTuple struct { |
| 130 | callback CallbackFunction |
| 131 | args []interface{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 132 | } |
| 133 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 134 | func (tuple *CallbackTuple) Execute(contextArgs interface{}) interface{} { |
| 135 | args := []interface{}{} |
| 136 | args = append(args, tuple.args...) |
| 137 | if contextArgs != nil { |
| 138 | args = append(args, contextArgs) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 139 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 140 | return tuple.callback(args...) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 143 | func (p *Proxy) RegisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) { |
| 144 | if _, exists := p.Callbacks[callbackType]; !exists { |
| 145 | p.Callbacks[callbackType] = make(map[string]CallbackTuple) |
| 146 | } |
| 147 | funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name() |
| 148 | log.Debugf("value of function: %s", funcName) |
| 149 | funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12] |
| 150 | |
| 151 | p.Callbacks[callbackType][funcHash] = CallbackTuple{callback, args} |
| 152 | } |
| 153 | |
| 154 | func (p *Proxy) UnregisterCallback(callbackType CallbackType, callback CallbackFunction, args ...interface{}) { |
| 155 | if _, exists := p.Callbacks[callbackType]; !exists { |
| 156 | log.Errorf("no such callback type - %s", callbackType.String()) |
| 157 | return |
| 158 | } |
| 159 | // TODO: Not sure if this is the best way to do it. |
| 160 | funcName := runtime.FuncForPC(reflect.ValueOf(callback).Pointer()).Name() |
| 161 | log.Debugf("value of function: %s", funcName) |
| 162 | funcHash := fmt.Sprintf("%x", md5.Sum([]byte(funcName)))[:12] |
| 163 | if _, exists := p.Callbacks[callbackType][funcHash]; !exists { |
| 164 | log.Errorf("function with hash value: '%s' not registered with callback type: '%s'", funcHash, callbackType) |
| 165 | return |
| 166 | } |
| 167 | delete(p.Callbacks[callbackType], funcHash) |
| 168 | } |
| 169 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 170 | func (p *Proxy) invoke(callback CallbackTuple, context ...interface{}) (result interface{}, err error) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 171 | defer func() { |
| 172 | if r := recover(); r != nil { |
| 173 | errStr := fmt.Sprintf("callback error occurred: %+v", r) |
| 174 | err = errors.New(errStr) |
| 175 | log.Error(errStr) |
| 176 | } |
| 177 | }() |
| 178 | |
| 179 | result = callback.Execute(context) |
| 180 | |
| 181 | return result, err |
| 182 | } |
| 183 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 184 | func (p *Proxy) InvokeCallbacks(args ...interface{}) (result interface{}) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 185 | callbackType := args[0].(CallbackType) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 186 | proceedOnError := args[1].(bool) |
| 187 | context := args[2:] |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 188 | |
| 189 | var err error |
| 190 | |
| 191 | if _, exists := p.Callbacks[callbackType]; exists { |
| 192 | for _, callback := range p.Callbacks[callbackType] { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 193 | if result, err = p.invoke(callback, context); err != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 194 | if !proceedOnError { |
| 195 | log.Info("An error occurred. Stopping callback invocation") |
| 196 | break |
| 197 | } |
| 198 | log.Info("An error occurred. Invoking next callback") |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | return context |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 203 | } |