blob: afb52b5c64145a79b4d9293a80ee0a628c6ae712 [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 {
76 return &SDLClient{
Mohamed Abukar775722c2019-06-10 16:41:57 +030077 db: sdl.NewSdlInstance(ns, sdl.NewDatabase()),
78 stat: Metric.RegisterCounterGroup(SDLCounterOpts, "SDL"),
Mohamed Abukar349a0982019-06-08 18:15:42 +030079 ready: false,
Mohamed Abukar2e78e422019-06-02 11:45:52 +030080 }
81}
82
83func (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 }
Mohamed Abukar349a0982019-06-08 18:15:42 +030092 s.ready = true
Mohamed Abukar2e78e422019-06-02 11:45:52 +030093 Logger.Info("Connection to database established!")
Mohamed Abukar349a0982019-06-08 18:15:42 +030094}
Mohamed Abukar2e78e422019-06-02 11:45:52 +030095
Mohamed Abukar349a0982019-06-08 18:15:42 +030096func (s *SDLClient) IsReady() bool {
97 return s.ready
Mohamed Abukar2e78e422019-06-02 11:45:52 +030098}
99
Mohamed Abukar192518d2019-06-11 18:06:50 +0300100func (s *SDLClient) doSet(pairs ...interface{}) (err error) {
101 err = s.db.Set(pairs)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300102 if err != nil {
103 s.UpdateStatCounter("StoreError")
104 } else {
105 s.UpdateStatCounter("Stored")
106 }
107 return
108}
109
Mohamed Abukar192518d2019-06-11 18:06:50 +0300110func (s *SDLClient) Store(key string, value interface{}) (err error) {
111 return s.doSet(key, value)
112}
113
114func (s *SDLClient) MStore(pairs ...interface{}) (err error) {
115 return s.doSet(pairs)
116}
117
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300118func (s *SDLClient) Read(key string) (value map[string]interface{}, err error) {
Mohamed Abukar192518d2019-06-11 18:06:50 +0300119 return s.db.Get([]string{key})
120}
121
122func (s *SDLClient) MRead(key []string) (value map[string]interface{}, err error) {
123 return s.db.Get(key)
124}
125
126func (s *SDLClient) ReadAllKeys(key string) (value []string, err error) {
127 return s.db.GetAll()
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300128}
129
130func (s *SDLClient) Subscribe(cb func(string, ...string), channel string) error {
131 return s.db.SubscribeChannel(cb, channel)
132}
133
134func (s *SDLClient) MSubscribe(cb func(string, ...string), channels ...string) error {
135 return s.db.SubscribeChannel(cb, channels...)
136}
137
138func (s *SDLClient) StoreAndPublish(channel string, event string, pairs ...interface{}) error {
139 return s.db.SetAndPublish([]string{channel, event}, pairs...)
140}
141
142func (s *SDLClient) MStoreAndPublish(channelsAndEvents []string, pairs ...interface{}) error {
143 return s.db.SetAndPublish(channelsAndEvents, pairs...)
144}
145
Mohamed Abukarf11ab7a2019-08-14 16:55:01 +0300146func (s *SDLClient) Delete(keys []string) (err error) {
147 return s.db.Remove(keys)
148}
149
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300150func (s *SDLClient) Clear() {
151 s.db.RemoveAll()
152}
153
154func (s *SDLClient) RegisterMetrics() {
155 s.stat = Metric.RegisterCounterGroup(SDLCounterOpts, "SDL")
156}
157
158func (s *SDLClient) UpdateStatCounter(name string) {
159 s.mux.Lock()
160 s.stat[name].Inc()
161 s.mux.Unlock()
162}
163
164func (c *SDLClient) GetStat() (t SDLStatistics) {
165 return
166}
167
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300168func NewRNIBClient(ns string) *RNIBClient {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300169 s := sdl.NewSdlInstance("e2Manager", sdl.NewDatabase())
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300170 return &RNIBClient{
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300171 db: s,
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300172 reader: nil,
173 writer: nil,
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300174 }
175}
176
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300177func (r *RNIBClient) GetNodeb(invName string) (*RNIBNodebInfo, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300178 return rnibreader.GetRNibReader(r.db).GetNodeb(invName)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300179}
180
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300181func (r *RNIBClient) GetNodebByGlobalNbId(t RNIBNodeType, gid *RNIBGlobalNbId) (*RNIBNodebInfo, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300182 return rnibreader.GetRNibReader(r.db).GetNodebByGlobalNbId(t, gid)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300183}
184
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300185func (r *RNIBClient) GetCellList(invName string) (*RNIBCells, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300186 return rnibreader.GetRNibReader(r.db).GetCellList(invName)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300187}
188
Mohamed Abukare17b3da2019-09-09 16:40:44 +0300189func (r *RNIBClient) GetListGnbIds() ([]*RNIBNbIdentity, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300190 return rnibreader.GetRNibReader(r.db).GetListGnbIds()
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300191}
192
Mohamed Abukare17b3da2019-09-09 16:40:44 +0300193func (r *RNIBClient) GetListEnbIds() ([]*RNIBNbIdentity, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300194 return rnibreader.GetRNibReader(r.db).GetListEnbIds()
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300195}
196
197func (r *RNIBClient) GetCountGnbList() (int, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300198 return rnibreader.GetRNibReader(r.db).GetCountGnbList()
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300199}
200
201func (r *RNIBClient) GetCell(invName string, pci uint32) (*RNIBCell, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300202 return rnibreader.GetRNibReader(r.db).GetCell(invName, pci)
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300203}
204
205func (r *RNIBClient) GetCellById(cellType RNIBCellType, cellId string) (*RNIBCell, RNIBIRNibError) {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300206 return rnibreader.GetRNibReader(r.db).GetCellById(cellType, cellId)
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300207}
208
209func (r *RNIBClient) SaveNodeb(nbIdentity *RNIBNbIdentity, entity *RNIBNodebInfo) RNIBIRNibError {
Mohamed Abukarfbf8bcf2020-05-14 18:33:54 +0300210 return rnibwriter.GetRNibWriter(r.db).SaveNodeb(nbIdentity, entity)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300211}