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