Initial version
[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         sdl "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
24         "gitlabe1.ext.net.nokia.com/ric_dev/ue-nib/api"
25         "gitlabe1.ext.net.nokia.com/ric_dev/ue-nib/pkg/uenibreader"
26         "gitlabe1.ext.net.nokia.com/ric_dev/ue-nib/pkg/uenibwriter"
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 }
44
45 type UENIBClient struct {
46         reader *uenibreader.Reader
47         writer *uenibwriter.Writer
48 }
49
50 type RNIBClient struct {
51         db *sdl.SdlInstance
52 }
53
54 // NewSDLClient returns a new SDLClient.
55 func NewSDLClient(ns string) *SDLClient {
56         return &SDLClient{
57                 db: sdl.NewSdlInstance(ns, sdl.NewDatabase()),
58         }
59 }
60
61 func (s *SDLClient) TestConnection() {
62         // Test DB connection, and wait until ready!
63         for {
64                 if _, err := s.db.GetAll(); err == nil {
65                         break
66                 }
67                 Logger.Warn("Database connection not ready, waiting ...")
68                 time.Sleep(time.Duration(5 * time.Second))
69         }
70         Logger.Info("Connection to database established!")
71
72         s.RegisterMetrics()
73 }
74
75 func (s *SDLClient) Store(key string, value interface{}) (err error) {
76         err = s.db.Set(key, value)
77         if err != nil {
78                 s.UpdateStatCounter("StoreError")
79         } else {
80                 s.UpdateStatCounter("Stored")
81         }
82         return
83 }
84
85 func (s *SDLClient) Read(key string) (value map[string]interface{}, err error) {
86         value, err = s.db.Get([]string{key})
87         return
88 }
89
90 func (s *SDLClient) Subscribe(cb func(string, ...string), channel string) error {
91         return s.db.SubscribeChannel(cb, channel)
92 }
93
94 func (s *SDLClient) MSubscribe(cb func(string, ...string), channels ...string) error {
95         return s.db.SubscribeChannel(cb, channels...)
96 }
97
98 func (s *SDLClient) StoreAndPublish(channel string, event string, pairs ...interface{}) error {
99         return s.db.SetAndPublish([]string{channel, event}, pairs...)
100 }
101
102 func (s *SDLClient) MStoreAndPublish(channelsAndEvents []string, pairs ...interface{}) error {
103         return s.db.SetAndPublish(channelsAndEvents, pairs...)
104 }
105
106 func (s *SDLClient) Clear() {
107         s.db.RemoveAll()
108 }
109
110 func (s *SDLClient) RegisterMetrics() {
111         s.stat = Metric.RegisterCounterGroup(SDLCounterOpts, "SDL")
112 }
113
114 func (s *SDLClient) UpdateStatCounter(name string) {
115         s.mux.Lock()
116         s.stat[name].Inc()
117         s.mux.Unlock()
118 }
119
120 func (c *SDLClient) GetStat() (t SDLStatistics) {
121         return
122 }
123
124 func NewUENIBClient() *UENIBClient {
125         return &UENIBClient{
126                 reader: uenibreader.NewReader(),
127                 writer: uenibwriter.NewWriter(),
128         }
129 }
130
131 func (u *UENIBClient) StoreUeMeasurement(gNbId string, gNbUeX2ApId string, data *api.MeasResults) error {
132         return u.writer.UpdateUeMeasurement(gNbId, gNbUeX2ApId, data)
133 }
134
135 func (u *UENIBClient) ReadUeMeasurement(gNbId string, gNbUeX2ApId string) (*api.MeasResults, error) {
136         return u.reader.GetUeMeasurement(gNbId, gNbUeX2ApId)
137 }
138
139 // To be removed ...
140 func NewRNIBClient(ns string) *RNIBClient {
141         return &RNIBClient{
142                 db: sdl.NewSdlInstance(ns, sdl.NewDatabase()),
143         }
144 }
145
146 func (r *RNIBClient) GetgNBList() (values map[string]interface{}, err error) {
147         keys, err := r.db.GetAll()
148         if err == nil {
149                 values = make(map[string]interface{})
150                 for _, key := range keys {
151                         v, err := r.db.Get([]string{key})
152                         if err == nil {
153                                 values[key] = v[key]
154                         }
155                 }
156         }
157         return
158 }
159
160 func (r *RNIBClient) GetNRCellList(key string) (value map[string]interface{}, err error) {
161         return r.db.Get([]string{key})
162 }
163
164 func (r *RNIBClient) GetUE(key1, key2 string) (value map[string]interface{}, err error) {
165         return r.db.Get([]string{key1 + key2})
166 }
167
168 func (r *RNIBClient) Store(key string, value interface{}) (err error) {
169         return r.db.Set(key, value)
170 }
171
172 func (r *RNIBClient) Clear() {
173         r.db.RemoveAll()
174 }