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