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 | |
| 162 | // Get retrieves data linked to a data model path |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 163 | func (pac *proxyAccessControl) Get(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 get, stack = %s", string(debug.Stack())) |
| 168 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 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 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 173 | return pac.getProxy().GetRoot().Get(path, "", depth, deep, txid) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 174 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 175 | |
| 176 | // 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] | 177 | func (pac *proxyAccessControl) Update(path string, data interface{}, strict bool, txid string, control bool) interface{} { |
| 178 | if control { |
| 179 | pac.lock() |
| 180 | defer pac.unlock() |
| 181 | log.Debugf("controlling update, stack = %s", string(debug.Stack())) |
| 182 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 183 | result := pac.getProxy().GetRoot().Update(path, data, strict, txid, nil) |
| 184 | |
| 185 | if result != nil { |
| 186 | return result.GetData() |
| 187 | } |
| 188 | return nil |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 189 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 190 | |
| 191 | // 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] | 192 | func (pac *proxyAccessControl) Add(path string, data interface{}, txid string, control bool) interface{} { |
| 193 | if control { |
| 194 | pac.lock() |
| 195 | defer pac.unlock() |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 196 | log.Debugf("controlling add %s, stack = %s", pac.Path, string(debug.Stack())) |
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().Add(path, data, txid, nil) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 199 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 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 | // Remove discards information linked to the data model path |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 207 | func (pac *proxyAccessControl) Remove(path string, txid string, control bool) interface{} { |
| 208 | if control { |
| 209 | pac.lock() |
| 210 | defer pac.unlock() |
| 211 | log.Debugf("controlling remove, stack = %s", string(debug.Stack())) |
| 212 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 213 | return pac.getProxy().GetRoot().Remove(path, txid, nil) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 214 | } |