Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -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 | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
| 20 | "github.com/opencord/voltha-go/common/log" |
| 21 | "runtime/debug" |
| 22 | "sync" |
| 23 | "time" |
| 24 | ) |
| 25 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 26 | type singletonProxyAccessControl struct { |
| 27 | sync.RWMutex |
| 28 | cache map[string]ProxyAccessControl |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 29 | } |
| 30 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 31 | var instanceProxyAccessControl *singletonProxyAccessControl |
| 32 | var onceProxyAccessControl sync.Once |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 33 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 34 | // PAC provides access to the proxy access control singleton instance |
| 35 | func PAC() *singletonProxyAccessControl { |
| 36 | onceProxyAccessControl.Do(func() { |
| 37 | instanceProxyAccessControl = &singletonProxyAccessControl{cache: make(map[string]ProxyAccessControl)} |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 38 | }) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 39 | return instanceProxyAccessControl |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 40 | } |
| 41 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 42 | // ReservePath will apply access control for a specific path within the model |
| 43 | func (singleton *singletonProxyAccessControl) ReservePath(path string, proxy *Proxy, pathLock string) ProxyAccessControl { |
| 44 | singleton.Lock() |
| 45 | defer singleton.Unlock() |
| 46 | var pac ProxyAccessControl |
| 47 | var exists bool |
| 48 | if pac, exists = singleton.cache[path]; !exists { |
| 49 | pac = NewProxyAccessControl(proxy, pathLock) |
| 50 | singleton.cache[path] = pac |
| 51 | } |
| 52 | |
| 53 | if exists { |
| 54 | log.Debugf("PAC exists for path: %s... re-using", path) |
| 55 | } else { |
| 56 | log.Debugf("PAC does not exists for path: %s... creating", path) |
| 57 | } |
| 58 | return pac |
| 59 | } |
| 60 | |
| 61 | // ReleasePath will remove access control for a specific path within the model |
| 62 | func (singleton *singletonProxyAccessControl) ReleasePath(pathLock string) { |
| 63 | singleton.Lock() |
| 64 | defer singleton.Unlock() |
| 65 | delete(singleton.cache, pathLock) |
| 66 | } |
| 67 | |
| 68 | // ProxyAccessControl is the abstraction interface to the base proxyAccessControl structure |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 69 | type ProxyAccessControl interface { |
| 70 | Get(path string, depth int, deep bool, txid string, control bool) interface{} |
| 71 | Update(path string, data interface{}, strict bool, txid string, control bool) interface{} |
| 72 | Add(path string, data interface{}, txid string, control bool) interface{} |
| 73 | Remove(path string, txid string, control bool) interface{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 74 | SetProxy(proxy *Proxy) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 77 | // proxyAccessControl holds details of the path and proxy that requires access control |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 78 | type proxyAccessControl struct { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 79 | sync.RWMutex |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 80 | Proxy *Proxy |
| 81 | PathLock chan struct{} |
| 82 | Path string |
| 83 | |
| 84 | start time.Time |
| 85 | stop time.Time |
| 86 | } |
| 87 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 88 | // NewProxyAccessControl creates a new instance of an access control structure |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 89 | func NewProxyAccessControl(proxy *Proxy, path string) ProxyAccessControl { |
| 90 | return &proxyAccessControl{ |
| 91 | Proxy: proxy, |
| 92 | Path: path, |
| 93 | PathLock: make(chan struct{}, 1), |
| 94 | } |
| 95 | } |
| 96 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 97 | // lock will prevent access to a model path |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 98 | func (pac *proxyAccessControl) lock() { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 99 | pac.PathLock <- struct{}{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 100 | pac.setStart(time.Now()) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 103 | // unlock will release control of a model path |
| 104 | func (pac *proxyAccessControl) unlock() { |
| 105 | <-pac.PathLock |
| 106 | pac.setStop(time.Now()) |
| 107 | GetProfiling().AddToInMemoryLockTime(pac.getStop().Sub(pac.getStart()).Seconds()) |
| 108 | } |
| 109 | |
| 110 | // getStart is used for profiling purposes and returns the time at which access control was applied |
| 111 | func (pac *proxyAccessControl) getStart() time.Time { |
| 112 | pac.Lock() |
| 113 | defer pac.Unlock() |
| 114 | return pac.start |
| 115 | } |
| 116 | |
| 117 | // getStart is used for profiling purposes and returns the time at which access control was removed |
| 118 | func (pac *proxyAccessControl) getStop() time.Time { |
| 119 | pac.Lock() |
| 120 | defer pac.Unlock() |
| 121 | return pac.stop |
| 122 | } |
| 123 | |
| 124 | // getPath returns the access controlled path |
| 125 | func (pac *proxyAccessControl) getPath() string { |
| 126 | pac.Lock() |
| 127 | defer pac.Unlock() |
| 128 | return pac.Path |
| 129 | } |
| 130 | |
| 131 | // getProxy returns the proxy used to reach a specific location in the data model |
| 132 | func (pac *proxyAccessControl) getProxy() *Proxy { |
| 133 | pac.Lock() |
| 134 | defer pac.Unlock() |
| 135 | return pac.Proxy |
| 136 | } |
| 137 | |
| 138 | // setStart is for profiling purposes and applies a start time value at which access control was started |
| 139 | func (pac *proxyAccessControl) setStart(time time.Time) { |
| 140 | pac.Lock() |
| 141 | defer pac.Unlock() |
| 142 | pac.start = time |
| 143 | } |
| 144 | |
| 145 | // setStop is for profiling purposes and applies a stop time value at which access control was stopped |
| 146 | func (pac *proxyAccessControl) setStop(time time.Time) { |
| 147 | pac.Lock() |
| 148 | defer pac.Unlock() |
| 149 | pac.stop = time |
| 150 | } |
| 151 | |
| 152 | // SetProxy is used to changed the proxy object of an access controlled path |
| 153 | func (pac *proxyAccessControl) SetProxy(proxy *Proxy) { |
| 154 | pac.Lock() |
| 155 | defer pac.Unlock() |
| 156 | pac.Proxy = proxy |
| 157 | } |
| 158 | |
| 159 | // Get retrieves data linked to a data model path |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 160 | func (pac *proxyAccessControl) Get(path string, depth int, deep bool, txid string, control bool) interface{} { |
| 161 | if control { |
| 162 | pac.lock() |
| 163 | defer pac.unlock() |
| 164 | log.Debugf("controlling get, stack = %s", string(debug.Stack())) |
| 165 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 166 | |
| 167 | // FIXME: Forcing depth to 0 for now due to problems deep copying the data structure |
| 168 | // The data traversal through reflection currently corrupts the content |
| 169 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 170 | return pac.getProxy().GetRoot().Get(path, "", depth, deep, txid) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 171 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 172 | |
| 173 | // Update changes the content of the data model at the specified location with the provided data |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 174 | func (pac *proxyAccessControl) Update(path string, data interface{}, strict bool, txid string, control bool) interface{} { |
| 175 | if control { |
| 176 | pac.lock() |
| 177 | defer pac.unlock() |
| 178 | log.Debugf("controlling update, stack = %s", string(debug.Stack())) |
| 179 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 180 | result := pac.getProxy().GetRoot().Update(path, data, strict, txid, nil) |
| 181 | |
| 182 | if result != nil { |
| 183 | return result.GetData() |
| 184 | } |
| 185 | return nil |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 186 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 187 | |
| 188 | // Add creates a new data model entry at the specified location with the provided data |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 189 | func (pac *proxyAccessControl) Add(path string, data interface{}, txid string, control bool) interface{} { |
| 190 | if control { |
| 191 | pac.lock() |
| 192 | defer pac.unlock() |
| 193 | log.Debugf("controlling add, stack = %s", string(debug.Stack())) |
| 194 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 195 | result := pac.getProxy().GetRoot().Add(path, data, txid, nil) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 196 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 197 | if result != nil { |
| 198 | return result.GetData() |
| 199 | } |
| 200 | return nil |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 201 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 202 | |
| 203 | // Remove discards information linked to the data model path |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 204 | func (pac *proxyAccessControl) Remove(path string, txid string, control bool) interface{} { |
| 205 | if control { |
| 206 | pac.lock() |
| 207 | defer pac.unlock() |
| 208 | log.Debugf("controlling remove, stack = %s", string(debug.Stack())) |
| 209 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 210 | return pac.getProxy().GetRoot().Remove(path, txid, nil) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 211 | } |