RIC:1060: Change in PTL
[ric-plt/xapp-frame.git] / pkg / xapp / db.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 package xapp
21
22 import (
23         rnibcommon "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
24         rnibentities "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
25         rnibreader "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
26         sdl "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
27         rnibwriter "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/rnib"
28         "sync"
29         "time"
30 )
31
32 // To be removed later
33 type SDLStatistics struct{}
34
35 var SDLCounterOpts = []CounterOpts{
36         {Name: "Stored", Help: "The total number of stored SDL transactions"},
37         {Name: "StoreError", Help: "The total number of SDL store errors"},
38 }
39
40 type SDLStorage struct {
41         db    *sdl.SyncStorage
42         stat  map[string]Counter
43         mux   sync.Mutex
44         ready bool
45 }
46
47 //Deprecated: Will be removed in a future release, please use SDLStorage type
48 type SDLClient struct {
49         db        *SDLStorage
50         nameSpace string
51 }
52
53 // Alias
54 type RNIBNodeType = rnibentities.Node_Type
55 type RNIBGlobalNbId = rnibentities.GlobalNbId
56 type RNIBNodebInfo = rnibentities.NodebInfo
57 type RNIBIRNibError = error
58 type RNIBCells = rnibentities.Cells
59 type RNIBNbIdentity = rnibentities.NbIdentity
60 type RNIBCellType = rnibentities.Cell_Type
61 type RNIBCell = rnibentities.Cell
62 type RNIBEnb = rnibentities.Enb
63 type RNIBGnb = rnibentities.Gnb
64
65 const RNIBNodeENB = rnibentities.Node_ENB
66 const RNIBNodeGNB = rnibentities.Node_GNB
67
68 type RNIBServedCellInfo = rnibentities.ServedCellInfo
69 type RNIBNodebInfoEnb = rnibentities.NodebInfo_Enb
70 type RNIBNodebInfoGnb = rnibentities.NodebInfo_Gnb
71 type RNIBServedNRCell = rnibentities.ServedNRCell
72 type RNIBServedNRCellInformation = rnibentities.ServedNRCellInformation
73 type RNIBNrNeighbourInformation = rnibentities.NrNeighbourInformation
74
75 type RNIBClient struct {
76         db     rnibcommon.ISdlSyncStorage
77         reader rnibreader.RNibReader
78         writer rnibwriter.RNibWriter
79 }
80
81 // NewSdlStorage returns a new instance of SDLStorage type.
82 func NewSdlStorage() *SDLStorage {
83         return &SDLStorage{
84                 db:    sdl.NewSyncStorage(),
85                 stat:  Metric.RegisterCounterGroup(SDLCounterOpts, "SDL"),
86                 ready: false,
87         }
88 }
89
90 func (s *SDLStorage) TestConnection(namespace string) {
91         // Test DB connection, and wait until ready!
92         for {
93                 if _, err := s.db.GetAll(namespace); err == nil {
94                         break
95                 }
96                 Logger.Warn("Database connection not ready, waiting ...")
97                 time.Sleep(time.Duration(5 * time.Second))
98         }
99         s.ready = true
100         Logger.Info("Connection to database established!")
101 }
102
103 func (s *SDLStorage) IsReady() bool {
104         return s.ready
105 }
106
107 func (s *SDLStorage) doSet(namespace string, pairs ...interface{}) (err error) {
108         err = s.db.Set(namespace, pairs)
109         if err != nil {
110                 s.UpdateStatCounter("StoreError")
111         } else {
112                 s.UpdateStatCounter("Stored")
113         }
114         return
115 }
116
117 func (s *SDLStorage) Store(namespace string, key string, value interface{}) (err error) {
118         return s.doSet(namespace, key, value)
119 }
120
121 func (s *SDLStorage) MStore(namespace string, pairs ...interface{}) (err error) {
122         return s.doSet(namespace, pairs)
123 }
124
125 func (s *SDLStorage) Read(namespace string, key string) (value map[string]interface{}, err error) {
126         return s.db.Get(namespace, []string{key})
127 }
128
129 func (s *SDLStorage) MRead(namespace string, key []string) (value map[string]interface{}, err error) {
130         return s.db.Get(namespace, key)
131 }
132
133 func (s *SDLStorage) ReadAllKeys(namespace string) (value []string, err error) {
134         return s.db.GetAll(namespace)
135 }
136
137 func (s *SDLStorage) Subscribe(namespace string, cb func(string, ...string), channel string) error {
138         return s.db.SubscribeChannel(namespace, cb, channel)
139 }
140
141 func (s *SDLStorage) MSubscribe(namespace string, cb func(string, ...string), channels ...string) error {
142         return s.db.SubscribeChannel(namespace, cb, channels...)
143 }
144
145 func (s *SDLStorage) StoreAndPublish(namespace string, channel string, event string, pairs ...interface{}) error {
146         return s.db.SetAndPublish(namespace, []string{channel, event}, pairs...)
147 }
148
149 func (s *SDLStorage) MStoreAndPublish(namespace string, channelsAndEvents []string, pairs ...interface{}) error {
150         return s.db.SetAndPublish(namespace, channelsAndEvents, pairs...)
151 }
152
153 func (s *SDLStorage) Delete(namespace string, keys []string) (err error) {
154         return s.db.Remove(namespace, keys)
155 }
156
157 func (s *SDLStorage) Clear(namespace string) {
158         s.db.RemoveAll(namespace)
159 }
160
161 func (s *SDLStorage) RegisterMetrics() {
162         s.stat = Metric.RegisterCounterGroup(SDLCounterOpts, "SDL")
163 }
164
165 func (s *SDLStorage) UpdateStatCounter(name string) {
166         s.mux.Lock()
167         s.stat[name].Inc()
168         s.mux.Unlock()
169 }
170
171 func (s *SDLStorage) GetStat() (t SDLStatistics) {
172         return
173 }
174
175 //NewSDLClient returns a new SDLClient.
176 //Deprecated: Will be removed in a future release, please use NewSdlStorage
177 func NewSDLClient(ns string) *SDLClient {
178         if ns == "" {
179                 ns = "sdl"
180         }
181         return &SDLClient{
182                 db:        NewSdlStorage(),
183                 nameSpace: ns,
184         }
185 }
186
187 //Deprecated: Will be removed in a future release, please use the TestConnection Receiver function of the SDLStorage type.
188 func (s *SDLClient) TestConnection() {
189         s.db.TestConnection(s.nameSpace)
190 }
191
192 func (s *SDLClient) IsReady() bool {
193         return s.db.ready
194 }
195
196 //Deprecated: Will be removed in a future release, please use the Store Receiver function of the SDLStorage type.
197 func (s *SDLClient) Store(key string, value interface{}) (err error) {
198         return s.db.Store(s.nameSpace, key, value)
199 }
200
201 //Deprecated: Will be removed in a future release, please use the MStore Receiver function of the SDLStorage type.
202 func (s *SDLClient) MStore(pairs ...interface{}) (err error) {
203         return s.db.MStore(s.nameSpace, pairs)
204 }
205
206 //Deprecated: Will be removed in a future release, please use the Read Receiver function of the SDLStorage type.
207 func (s *SDLClient) Read(key string) (value map[string]interface{}, err error) {
208         return s.db.Read(s.nameSpace, key)
209 }
210
211 //Deprecated: Will be removed in a future release, please use the MRead Receiver function of the SDLStorage type.
212 func (s *SDLClient) MRead(key []string) (value map[string]interface{}, err error) {
213         return s.db.MRead(s.nameSpace, key)
214 }
215
216 //Deprecated: Will be removed in a future release, please use the ReadAllKeys Receiver function of the SDLStorage type.
217 func (s *SDLClient) ReadAllKeys(key string) (value []string, err error) {
218         return s.db.ReadAllKeys(s.nameSpace)
219 }
220
221 //Deprecated: Will be removed in a future release, please use the Subscribe Receiver function of the SDLStorage type.
222 func (s *SDLClient) Subscribe(cb func(string, ...string), channel string) error {
223         return s.db.Subscribe(s.nameSpace, cb, channel)
224 }
225
226 //Deprecated: Will be removed in a future release, please use the MSubscribe Receiver function of the SDLStorage type.
227 func (s *SDLClient) MSubscribe(cb func(string, ...string), channels ...string) error {
228         return s.db.MSubscribe(s.nameSpace, cb, channels...)
229 }
230
231 //Deprecated: Will be removed in a future release, please use the StoreAndPublish Receiver function of the SDLStorage type.
232 func (s *SDLClient) StoreAndPublish(channel string, event string, pairs ...interface{}) error {
233         return s.db.StoreAndPublish(s.nameSpace, channel, event, pairs...)
234 }
235
236 //Deprecated: Will be removed in a future release, please use the MStoreAndPublish Receiver function of the SDLStorage type.
237 func (s *SDLClient) MStoreAndPublish(channelsAndEvents []string, pairs ...interface{}) error {
238         return s.db.MStoreAndPublish(s.nameSpace, channelsAndEvents, pairs...)
239 }
240
241 //Deprecated: Will be removed in a future release, please use the Delete Receiver function of the SDLStorage type.
242 func (s *SDLClient) Delete(keys []string) (err error) {
243         return s.db.Delete(s.nameSpace, keys)
244 }
245
246 //Deprecated: Will be removed in a future release, please use the Clear Receiver function of the SDLStorage type.
247 func (s *SDLClient) Clear() {
248         s.db.Clear(s.nameSpace)
249 }
250
251 //Deprecated: Will be removed in a future release, please use the RegisterMetrics Receiver function of the SDLStorage type.
252 func (s *SDLClient) RegisterMetrics() {
253         s.db.RegisterMetrics()
254 }
255
256 //Deprecated: Will be removed in a future release, please use the UpdateStatCounter Receiver function of the SDLStorage type.
257 func (s *SDLClient) UpdateStatCounter(name string) {
258         s.db.UpdateStatCounter(name)
259 }
260
261 //Deprecated: Will be removed in a future release, please use the GetStat Receiver function of the SDLStorage type.
262 func (c *SDLClient) GetStat() (t SDLStatistics) {
263         return c.db.GetStat()
264 }
265
266 func GetNewRnibClient(sdlStorage rnibcommon.ISdlSyncStorage) *RNIBClient {
267         return &RNIBClient{
268                 db:     sdlStorage,
269                 reader: rnibreader.GetNewRNibReader(sdlStorage),
270                 writer: rnibwriter.GetNewRNibWriter(sdlStorage),
271         }
272 }
273
274 //Deprecated: Will be removed in a future release, please use GetNewRnibClient instead.
275 func NewRNIBClient() *RNIBClient {
276         s := sdl.NewSyncStorage()
277         return &RNIBClient{
278                 db:     s,
279                 reader: rnibreader.GetNewRNibReader(s),
280                 writer: rnibwriter.GetNewRNibWriter(s),
281         }
282 }
283
284 func (r *RNIBClient) Subscribe(cb func(string, ...string), channel string) error {
285         return r.db.SubscribeChannel(rnibcommon.GetRNibNamespace(), cb, channel)
286 }
287
288 func (r *RNIBClient) StoreAndPublish(channel string, event string, pairs ...interface{}) error {
289         return r.db.SetAndPublish(rnibcommon.GetRNibNamespace(), []string{channel, event}, pairs...)
290 }
291
292 func (r *RNIBClient) GetNodeb(invName string) (*RNIBNodebInfo, RNIBIRNibError) {
293         return r.reader.GetNodeb(invName)
294 }
295
296 func (r *RNIBClient) GetNodebByGlobalNbId(t RNIBNodeType, gid *RNIBGlobalNbId) (*RNIBNodebInfo, RNIBIRNibError) {
297         return r.reader.GetNodebByGlobalNbId(t, gid)
298 }
299
300 func (r *RNIBClient) GetCellList(invName string) (*RNIBCells, RNIBIRNibError) {
301         return r.reader.GetCellList(invName)
302 }
303
304 func (r *RNIBClient) GetListGnbIds() ([]*RNIBNbIdentity, RNIBIRNibError) {
305         return r.reader.GetListGnbIds()
306 }
307
308 func (r *RNIBClient) GetListEnbIds() ([]*RNIBNbIdentity, RNIBIRNibError) {
309         return r.reader.GetListEnbIds()
310 }
311
312 func (r *RNIBClient) GetCountGnbList() (int, RNIBIRNibError) {
313         return r.reader.GetCountGnbList()
314 }
315
316 func (r *RNIBClient) GetCell(invName string, pci uint32) (*RNIBCell, RNIBIRNibError) {
317         return r.reader.GetCell(invName, pci)
318 }
319
320 func (r *RNIBClient) GetCellById(cellType RNIBCellType, cellId string) (*RNIBCell, RNIBIRNibError) {
321         return r.reader.GetCellById(cellType, cellId)
322 }
323
324 func (r *RNIBClient) SaveNodeb(nbIdentity *RNIBNbIdentity, entity *RNIBNodebInfo) RNIBIRNibError {
325         return r.writer.SaveNodeb(nbIdentity, entity)
326 }