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