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