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