afb52b5c64145a79b4d9293a80ee0a628c6ae712
[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 SDLClient struct {
40         db    *sdl.SdlInstance
41         stat  map[string]Counter
42         mux   sync.Mutex
43         ready bool
44 }
45
46 // Alias
47 type RNIBNodeType = rnibentities.Node_Type
48 type RNIBGlobalNbId = rnibentities.GlobalNbId
49 type RNIBNodebInfo = rnibentities.NodebInfo
50 type RNIBIRNibError = error
51 type RNIBCells = rnibentities.Cells
52 type RNIBNbIdentity = rnibentities.NbIdentity
53 type RNIBCellType = rnibentities.Cell_Type
54 type RNIBCell = rnibentities.Cell
55 type RNIBEnb = rnibentities.Enb
56 type RNIBGnb = rnibentities.Gnb
57
58 const RNIBNodeENB = rnibentities.Node_ENB
59 const RNIBNodeGNB = rnibentities.Node_GNB
60
61 type RNIBServedCellInfo = rnibentities.ServedCellInfo
62 type RNIBNodebInfoEnb = rnibentities.NodebInfo_Enb
63 type RNIBNodebInfoGnb = rnibentities.NodebInfo_Gnb
64 type RNIBServedNRCell = rnibentities.ServedNRCell
65 type RNIBServedNRCellInformation = rnibentities.ServedNRCellInformation
66 type RNIBNrNeighbourInformation = rnibentities.NrNeighbourInformation
67
68 type RNIBClient struct {
69         db     *sdl.SdlInstance
70         reader rnibreader.RNibReader
71         writer rnibwriter.RNibWriter
72 }
73
74 // NewSDLClient returns a new SDLClient.
75 func NewSDLClient(ns string) *SDLClient {
76         return &SDLClient{
77                 db:    sdl.NewSdlInstance(ns, sdl.NewDatabase()),
78                 stat:  Metric.RegisterCounterGroup(SDLCounterOpts, "SDL"),
79                 ready: false,
80         }
81 }
82
83 func (s *SDLClient) TestConnection() {
84         // Test DB connection, and wait until ready!
85         for {
86                 if _, err := s.db.GetAll(); err == nil {
87                         break
88                 }
89                 Logger.Warn("Database connection not ready, waiting ...")
90                 time.Sleep(time.Duration(5 * time.Second))
91         }
92         s.ready = true
93         Logger.Info("Connection to database established!")
94 }
95
96 func (s *SDLClient) IsReady() bool {
97         return s.ready
98 }
99
100 func (s *SDLClient) doSet(pairs ...interface{}) (err error) {
101         err = s.db.Set(pairs)
102         if err != nil {
103                 s.UpdateStatCounter("StoreError")
104         } else {
105                 s.UpdateStatCounter("Stored")
106         }
107         return
108 }
109
110 func (s *SDLClient) Store(key string, value interface{}) (err error) {
111         return s.doSet(key, value)
112 }
113
114 func (s *SDLClient) MStore(pairs ...interface{}) (err error) {
115         return s.doSet(pairs)
116 }
117
118 func (s *SDLClient) Read(key string) (value map[string]interface{}, err error) {
119         return s.db.Get([]string{key})
120 }
121
122 func (s *SDLClient) MRead(key []string) (value map[string]interface{}, err error) {
123         return s.db.Get(key)
124 }
125
126 func (s *SDLClient) ReadAllKeys(key string) (value []string, err error) {
127         return s.db.GetAll()
128 }
129
130 func (s *SDLClient) Subscribe(cb func(string, ...string), channel string) error {
131         return s.db.SubscribeChannel(cb, channel)
132 }
133
134 func (s *SDLClient) MSubscribe(cb func(string, ...string), channels ...string) error {
135         return s.db.SubscribeChannel(cb, channels...)
136 }
137
138 func (s *SDLClient) StoreAndPublish(channel string, event string, pairs ...interface{}) error {
139         return s.db.SetAndPublish([]string{channel, event}, pairs...)
140 }
141
142 func (s *SDLClient) MStoreAndPublish(channelsAndEvents []string, pairs ...interface{}) error {
143         return s.db.SetAndPublish(channelsAndEvents, pairs...)
144 }
145
146 func (s *SDLClient) Delete(keys []string) (err error) {
147         return s.db.Remove(keys)
148 }
149
150 func (s *SDLClient) Clear() {
151         s.db.RemoveAll()
152 }
153
154 func (s *SDLClient) RegisterMetrics() {
155         s.stat = Metric.RegisterCounterGroup(SDLCounterOpts, "SDL")
156 }
157
158 func (s *SDLClient) UpdateStatCounter(name string) {
159         s.mux.Lock()
160         s.stat[name].Inc()
161         s.mux.Unlock()
162 }
163
164 func (c *SDLClient) GetStat() (t SDLStatistics) {
165         return
166 }
167
168 func NewRNIBClient(ns string) *RNIBClient {
169         s := sdl.NewSdlInstance("e2Manager", sdl.NewDatabase())
170         return &RNIBClient{
171                 db:     s,
172                 reader: nil,
173                 writer: nil,
174         }
175 }
176
177 func (r *RNIBClient) GetNodeb(invName string) (*RNIBNodebInfo, RNIBIRNibError) {
178         return rnibreader.GetRNibReader(r.db).GetNodeb(invName)
179 }
180
181 func (r *RNIBClient) GetNodebByGlobalNbId(t RNIBNodeType, gid *RNIBGlobalNbId) (*RNIBNodebInfo, RNIBIRNibError) {
182         return rnibreader.GetRNibReader(r.db).GetNodebByGlobalNbId(t, gid)
183 }
184
185 func (r *RNIBClient) GetCellList(invName string) (*RNIBCells, RNIBIRNibError) {
186         return rnibreader.GetRNibReader(r.db).GetCellList(invName)
187 }
188
189 func (r *RNIBClient) GetListGnbIds() ([]*RNIBNbIdentity, RNIBIRNibError) {
190         return rnibreader.GetRNibReader(r.db).GetListGnbIds()
191 }
192
193 func (r *RNIBClient) GetListEnbIds() ([]*RNIBNbIdentity, RNIBIRNibError) {
194         return rnibreader.GetRNibReader(r.db).GetListEnbIds()
195 }
196
197 func (r *RNIBClient) GetCountGnbList() (int, RNIBIRNibError) {
198         return rnibreader.GetRNibReader(r.db).GetCountGnbList()
199 }
200
201 func (r *RNIBClient) GetCell(invName string, pci uint32) (*RNIBCell, RNIBIRNibError) {
202         return rnibreader.GetRNibReader(r.db).GetCell(invName, pci)
203 }
204
205 func (r *RNIBClient) GetCellById(cellType RNIBCellType, cellId string) (*RNIBCell, RNIBIRNibError) {
206         return rnibreader.GetRNibReader(r.db).GetCellById(cellType, cellId)
207 }
208
209 func (r *RNIBClient) SaveNodeb(nbIdentity *RNIBNbIdentity, entity *RNIBNodebInfo) RNIBIRNibError {
210         return rnibwriter.GetRNibWriter(r.db).SaveNodeb(nbIdentity, entity)
211 }