fc250f0feff3efa2e73cc2bc6d38e9d1098b6054
[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         uenibprotobuf "gerrit.o-ran-sc.org/r/ric-plt/ue-nib/uenibprotobuf"
27         uenibreader "gerrit.o-ran-sc.org/r/ric-plt/ue-nib/uenibreader"
28         uenibwriter "gerrit.o-ran-sc.org/r/ric-plt/ue-nib/uenibwriter"
29         rnibwriter "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/rnib"
30         "sync"
31         "time"
32 )
33
34 // To be removed later
35 type SDLStatistics struct{}
36
37 var SDLCounterOpts = []CounterOpts{
38         {Name: "Stored", Help: "The total number of stored SDL transactions"},
39         {Name: "StoreError", Help: "The total number of SDL store errors"},
40 }
41
42 type SDLClient struct {
43         db    *sdl.SdlInstance
44         stat  map[string]Counter
45         mux   sync.Mutex
46         ready bool
47 }
48
49 // Alias
50 type EventCategory = uenibreader.EventCategory
51 type EventCallback = uenibreader.EventCallback
52 type MeasResultNR = uenibprotobuf.MeasResultNR
53 type MeasQuantityResults = uenibprotobuf.MeasResultNR_MeasQuantityResults
54 type MeasResultServMO = uenibprotobuf.MeasResults_MeasResultServMO
55 type MeasResults = uenibprotobuf.MeasResults
56
57 type UENIBClient struct {
58         reader *uenibreader.Reader
59         writer *uenibwriter.Writer
60 }
61
62 // Alias
63 type RNIBNodeType = rnibentities.Node_Type
64 type RNIBGlobalNbId = rnibentities.GlobalNbId
65 type RNIBNodebInfo = rnibentities.NodebInfo
66 type RNIBIRNibError = error
67 type RNIBCells = rnibentities.Cells
68 type RNIBNbIdentity = rnibentities.NbIdentity
69 type RNIBCellType = rnibentities.Cell_Type
70 type RNIBCell = rnibentities.Cell
71 type RNIBEnb = rnibentities.Enb
72 type RNIBGnb = rnibentities.Gnb
73
74 const RNIBNodeENB = rnibentities.Node_ENB
75 const RNIBNodeGNB = rnibentities.Node_GNB
76
77 type RNIBServedCellInfo = rnibentities.ServedCellInfo
78 type RNIBNodebInfoEnb = rnibentities.NodebInfo_Enb
79 type RNIBNodebInfoGnb = rnibentities.NodebInfo_Gnb
80 type RNIBServedNRCell = rnibentities.ServedNRCell
81 type RNIBServedNRCellInformation = rnibentities.ServedNRCellInformation
82 type RNIBNrNeighbourInformation = rnibentities.NrNeighbourInformation
83
84 type RNIBClient struct {
85         reader rnibreader.RNibReader
86         writer rnibwriter.RNibWriter
87 }
88
89 // NewSDLClient returns a new SDLClient.
90 func NewSDLClient(ns string) *SDLClient {
91         return &SDLClient{
92                 db:    sdl.NewSdlInstance(ns, sdl.NewDatabase()),
93                 stat:  Metric.RegisterCounterGroup(SDLCounterOpts, "SDL"),
94                 ready: false,
95         }
96 }
97
98 func (s *SDLClient) TestConnection() {
99         // Test DB connection, and wait until ready!
100         for {
101                 if _, err := s.db.GetAll(); err == nil {
102                         break
103                 }
104                 Logger.Warn("Database connection not ready, waiting ...")
105                 time.Sleep(time.Duration(5 * time.Second))
106         }
107         s.ready = true
108         Logger.Info("Connection to database established!")
109 }
110
111 func (s *SDLClient) IsReady() bool {
112         return s.ready
113 }
114
115 func (s *SDLClient) doSet(pairs ...interface{}) (err error) {
116         err = s.db.Set(pairs)
117         if err != nil {
118                 s.UpdateStatCounter("StoreError")
119         } else {
120                 s.UpdateStatCounter("Stored")
121         }
122         return
123 }
124
125 func (s *SDLClient) Store(key string, value interface{}) (err error) {
126         return s.doSet(key, value)
127 }
128
129 func (s *SDLClient) MStore(pairs ...interface{}) (err error) {
130         return s.doSet(pairs)
131 }
132
133 func (s *SDLClient) Read(key string) (value map[string]interface{}, err error) {
134         return s.db.Get([]string{key})
135 }
136
137 func (s *SDLClient) MRead(key []string) (value map[string]interface{}, err error) {
138         return s.db.Get(key)
139 }
140
141 func (s *SDLClient) ReadAllKeys(key string) (value []string, err error) {
142         return s.db.GetAll()
143 }
144
145 func (s *SDLClient) Subscribe(cb func(string, ...string), channel string) error {
146         return s.db.SubscribeChannel(cb, channel)
147 }
148
149 func (s *SDLClient) MSubscribe(cb func(string, ...string), channels ...string) error {
150         return s.db.SubscribeChannel(cb, channels...)
151 }
152
153 func (s *SDLClient) StoreAndPublish(channel string, event string, pairs ...interface{}) error {
154         return s.db.SetAndPublish([]string{channel, event}, pairs...)
155 }
156
157 func (s *SDLClient) MStoreAndPublish(channelsAndEvents []string, pairs ...interface{}) error {
158         return s.db.SetAndPublish(channelsAndEvents, pairs...)
159 }
160
161 func (s *SDLClient) Delete(keys []string) (err error) {
162         return s.db.Remove(keys)
163 }
164
165 func (s *SDLClient) Clear() {
166         s.db.RemoveAll()
167 }
168
169 func (s *SDLClient) RegisterMetrics() {
170         s.stat = Metric.RegisterCounterGroup(SDLCounterOpts, "SDL")
171 }
172
173 func (s *SDLClient) UpdateStatCounter(name string) {
174         s.mux.Lock()
175         s.stat[name].Inc()
176         s.mux.Unlock()
177 }
178
179 func (c *SDLClient) GetStat() (t SDLStatistics) {
180         return
181 }
182
183 func NewUENIBClient() *UENIBClient {
184         return &UENIBClient{
185                 reader: uenibreader.NewReader(),
186                 writer: uenibwriter.NewWriter(),
187         }
188 }
189
190 func (u *UENIBClient) StoreUeMeasurement(gNbId string, gNbUeX2ApId string, data *uenibprotobuf.MeasResults) error {
191         return u.writer.UpdateUeMeasurement(gNbId, gNbUeX2ApId, data)
192 }
193
194 func (u *UENIBClient) CreateUeContext(gNbId string, gNbUeX2ApId string) error {
195         return u.writer.UeContextAddComplete(gNbId, gNbUeX2ApId)
196 }
197
198 func (u *UENIBClient) ReleaseUeContext(gNbId string, gNbUeX2ApId string) error {
199         return u.writer.RemoveUeContext(gNbId, gNbUeX2ApId)
200 }
201
202 func (u *UENIBClient) ReadUeMeasurement(gNbId string, gNbUeX2ApId string) (*uenibprotobuf.MeasResults, error) {
203         return u.reader.GetUeMeasurement(gNbId, gNbUeX2ApId)
204 }
205
206 func (u *UENIBClient) SubscribeEvents(gNbIDs []string, eventCategories []EventCategory, cb EventCallback) error {
207         return u.reader.SubscribeEvents(gNbIDs, eventCategories, cb)
208 }
209
210 func NewRNIBClient(ns string) *RNIBClient {
211         rnibreader.Init("e2Manager", 1)
212         rnibwriter.InitWriter("e2Manager", 1)
213         return &RNIBClient{
214                 reader: nil,
215                 writer: nil,
216         }
217 }
218
219 func (r *RNIBClient) GetNodeb(invName string) (*RNIBNodebInfo, RNIBIRNibError) {
220         return rnibreader.GetRNibReader().GetNodeb(invName)
221 }
222
223 func (r *RNIBClient) GetNodebByGlobalNbId(t RNIBNodeType, gid *RNIBGlobalNbId) (*RNIBNodebInfo, RNIBIRNibError) {
224         return rnibreader.GetRNibReader().GetNodebByGlobalNbId(t, gid)
225 }
226
227 func (r *RNIBClient) GetCellList(invName string) (*RNIBCells, RNIBIRNibError) {
228         return rnibreader.GetRNibReader().GetCellList(invName)
229 }
230
231 func (r *RNIBClient) GetListGnbIds() ([]*RNIBNbIdentity, RNIBIRNibError) {
232         return rnibreader.GetRNibReader().GetListGnbIds()
233 }
234
235 func (r *RNIBClient) GetListEnbIds() ([]*RNIBNbIdentity, RNIBIRNibError) {
236         return rnibreader.GetRNibReader().GetListEnbIds()
237 }
238
239 func (r *RNIBClient) GetCountGnbList() (int, RNIBIRNibError) {
240         return rnibreader.GetRNibReader().GetCountGnbList()
241 }
242
243 func (r *RNIBClient) GetCell(invName string, pci uint32) (*RNIBCell, RNIBIRNibError) {
244         return rnibreader.GetRNibReader().GetCell(invName, pci)
245 }
246
247 func (r *RNIBClient) GetCellById(cellType RNIBCellType, cellId string) (*RNIBCell, RNIBIRNibError) {
248         return rnibreader.GetRNibReader().GetCellById(cellType, cellId)
249 }
250
251 func (r *RNIBClient) SaveNodeb(nbIdentity *RNIBNbIdentity, entity *RNIBNodebInfo) RNIBIRNibError {
252         return rnibwriter.GetRNibWriter().SaveNodeb(nbIdentity, entity)
253 }