blob: e422cfde69ebbcb034c4c1ce6ef9799a893720f2 [file] [log] [blame]
Mohamed Abukar2e78e422019-06-02 11:45:52 +03001/*
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
20package xapp
21
22import (
Mohamed Abukar3e611c62019-07-05 13:40:11 +030023 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"
Mohamed Abukar2e78e422019-06-02 11:45:52 +030025 sdl "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
Mohamed Abukar3e611c62019-07-05 13:40:11 +030026 rnibwriter "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/rnib"
Mohamed Abukar2e78e422019-06-02 11:45:52 +030027 "sync"
28 "time"
29)
30
31// To be removed later
32type SDLStatistics struct{}
33
34var 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
39type SDLClient struct {
Mohamed Abukar775722c2019-06-10 16:41:57 +030040 db *sdl.SdlInstance
41 stat map[string]Counter
42 mux sync.Mutex
43 ready bool
Mohamed Abukar2e78e422019-06-02 11:45:52 +030044}
45
Mohamed Abukar3e611c62019-07-05 13:40:11 +030046// Alias
Mohamed Abukar3e611c62019-07-05 13:40:11 +030047type RNIBNodeType = rnibentities.Node_Type
48type RNIBGlobalNbId = rnibentities.GlobalNbId
49type RNIBNodebInfo = rnibentities.NodebInfo
Mohamed Abukare17b3da2019-09-09 16:40:44 +030050type RNIBIRNibError = error
Mohamed Abukar3e611c62019-07-05 13:40:11 +030051type RNIBCells = rnibentities.Cells
52type RNIBNbIdentity = rnibentities.NbIdentity
53type RNIBCellType = rnibentities.Cell_Type
54type RNIBCell = rnibentities.Cell
55type RNIBEnb = rnibentities.Enb
56type RNIBGnb = rnibentities.Gnb
57
58const RNIBNodeENB = rnibentities.Node_ENB
59const RNIBNodeGNB = rnibentities.Node_GNB
60
61type RNIBServedCellInfo = rnibentities.ServedCellInfo
62type RNIBNodebInfoEnb = rnibentities.NodebInfo_Enb
63type RNIBNodebInfoGnb = rnibentities.NodebInfo_Gnb
64type RNIBServedNRCell = rnibentities.ServedNRCell
65type RNIBServedNRCellInformation = rnibentities.ServedNRCellInformation
66type RNIBNrNeighbourInformation = rnibentities.NrNeighbourInformation
67
Mohamed Abukar2e78e422019-06-02 11:45:52 +030068type RNIBClient struct {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +030069 db *sdl.SdlInstance
Mohamed Abukar3e611c62019-07-05 13:40:11 +030070 reader rnibreader.RNibReader
71 writer rnibwriter.RNibWriter
Mohamed Abukar2e78e422019-06-02 11:45:52 +030072}
73
74// NewSDLClient returns a new SDLClient.
75func NewSDLClient(ns string) *SDLClient {
Mohamed Abukar827a6412020-11-12 10:02:41 +020076 if ns == "" {
77 ns = "sdl"
78 }
Mohamed Abukar2e78e422019-06-02 11:45:52 +030079 return &SDLClient{
Mohamed Abukar775722c2019-06-10 16:41:57 +030080 db: sdl.NewSdlInstance(ns, sdl.NewDatabase()),
81 stat: Metric.RegisterCounterGroup(SDLCounterOpts, "SDL"),
Mohamed Abukar349a0982019-06-08 18:15:42 +030082 ready: false,
Mohamed Abukar2e78e422019-06-02 11:45:52 +030083 }
84}
85
86func (s *SDLClient) TestConnection() {
87 // Test DB connection, and wait until ready!
88 for {
89 if _, err := s.db.GetAll(); err == nil {
90 break
91 }
92 Logger.Warn("Database connection not ready, waiting ...")
93 time.Sleep(time.Duration(5 * time.Second))
94 }
Mohamed Abukar349a0982019-06-08 18:15:42 +030095 s.ready = true
Mohamed Abukar2e78e422019-06-02 11:45:52 +030096 Logger.Info("Connection to database established!")
Mohamed Abukar349a0982019-06-08 18:15:42 +030097}
Mohamed Abukar2e78e422019-06-02 11:45:52 +030098
Mohamed Abukar349a0982019-06-08 18:15:42 +030099func (s *SDLClient) IsReady() bool {
100 return s.ready
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300101}
102
Mohamed Abukar192518d2019-06-11 18:06:50 +0300103func (s *SDLClient) doSet(pairs ...interface{}) (err error) {
104 err = s.db.Set(pairs)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300105 if err != nil {
106 s.UpdateStatCounter("StoreError")
107 } else {
108 s.UpdateStatCounter("Stored")
109 }
110 return
111}
112
Mohamed Abukar192518d2019-06-11 18:06:50 +0300113func (s *SDLClient) Store(key string, value interface{}) (err error) {
114 return s.doSet(key, value)
115}
116
117func (s *SDLClient) MStore(pairs ...interface{}) (err error) {
118 return s.doSet(pairs)
119}
120
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300121func (s *SDLClient) Read(key string) (value map[string]interface{}, err error) {
Mohamed Abukar192518d2019-06-11 18:06:50 +0300122 return s.db.Get([]string{key})
123}
124
125func (s *SDLClient) MRead(key []string) (value map[string]interface{}, err error) {
126 return s.db.Get(key)
127}
128
129func (s *SDLClient) ReadAllKeys(key string) (value []string, err error) {
130 return s.db.GetAll()
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300131}
132
133func (s *SDLClient) Subscribe(cb func(string, ...string), channel string) error {
134 return s.db.SubscribeChannel(cb, channel)
135}
136
137func (s *SDLClient) MSubscribe(cb func(string, ...string), channels ...string) error {
138 return s.db.SubscribeChannel(cb, channels...)
139}
140
141func (s *SDLClient) StoreAndPublish(channel string, event string, pairs ...interface{}) error {
142 return s.db.SetAndPublish([]string{channel, event}, pairs...)
143}
144
145func (s *SDLClient) MStoreAndPublish(channelsAndEvents []string, pairs ...interface{}) error {
146 return s.db.SetAndPublish(channelsAndEvents, pairs...)
147}
148
Mohamed Abukarf11ab7a2019-08-14 16:55:01 +0300149func (s *SDLClient) Delete(keys []string) (err error) {
150 return s.db.Remove(keys)
151}
152
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300153func (s *SDLClient) Clear() {
154 s.db.RemoveAll()
155}
156
157func (s *SDLClient) RegisterMetrics() {
158 s.stat = Metric.RegisterCounterGroup(SDLCounterOpts, "SDL")
159}
160
161func (s *SDLClient) UpdateStatCounter(name string) {
162 s.mux.Lock()
163 s.stat[name].Inc()
164 s.mux.Unlock()
165}
166
167func (c *SDLClient) GetStat() (t SDLStatistics) {
168 return
169}
170
Mohamed Abukarb8b191f2020-11-07 11:22:56 +0200171func NewRNIBClient() *RNIBClient {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300172 s := sdl.NewSdlInstance("e2Manager", sdl.NewDatabase())
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300173 return &RNIBClient{
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300174 db: s,
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300175 reader: nil,
176 writer: nil,
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300177 }
178}
179
Mohamed Abukar749726b2020-08-19 14:23:56 +0300180func (r *RNIBClient) Subscribe(cb func(string, ...string), channel string) error {
181 return r.db.SubscribeChannel(cb, channel)
182}
183
184func (r *RNIBClient) StoreAndPublish(channel string, event string, pairs ...interface{}) error {
185 return r.db.SetAndPublish([]string{channel, event}, pairs...)
186}
187
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300188func (r *RNIBClient) GetNodeb(invName string) (*RNIBNodebInfo, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300189 return rnibreader.GetRNibReader(r.db).GetNodeb(invName)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300190}
191
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300192func (r *RNIBClient) GetNodebByGlobalNbId(t RNIBNodeType, gid *RNIBGlobalNbId) (*RNIBNodebInfo, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300193 return rnibreader.GetRNibReader(r.db).GetNodebByGlobalNbId(t, gid)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300194}
195
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300196func (r *RNIBClient) GetCellList(invName string) (*RNIBCells, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300197 return rnibreader.GetRNibReader(r.db).GetCellList(invName)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300198}
199
Mohamed Abukare17b3da2019-09-09 16:40:44 +0300200func (r *RNIBClient) GetListGnbIds() ([]*RNIBNbIdentity, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300201 return rnibreader.GetRNibReader(r.db).GetListGnbIds()
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300202}
203
Mohamed Abukare17b3da2019-09-09 16:40:44 +0300204func (r *RNIBClient) GetListEnbIds() ([]*RNIBNbIdentity, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300205 return rnibreader.GetRNibReader(r.db).GetListEnbIds()
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300206}
207
208func (r *RNIBClient) GetCountGnbList() (int, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300209 return rnibreader.GetRNibReader(r.db).GetCountGnbList()
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300210}
211
212func (r *RNIBClient) GetCell(invName string, pci uint32) (*RNIBCell, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300213 return rnibreader.GetRNibReader(r.db).GetCell(invName, pci)
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300214}
215
216func (r *RNIBClient) GetCellById(cellType RNIBCellType, cellId string) (*RNIBCell, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300217 return rnibreader.GetRNibReader(r.db).GetCellById(cellType, cellId)
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300218}
219
220func (r *RNIBClient) SaveNodeb(nbIdentity *RNIBNbIdentity, entity *RNIBNodebInfo) RNIBIRNibError {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300221 return rnibwriter.GetRNibWriter(r.db).SaveNodeb(nbIdentity, entity)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300222}