Update logging
[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         "sync"
25         "time"
26 )
27
28 // To be removed later
29 type SDLStatistics struct{}
30
31 var SDLCounterOpts = []CounterOpts{
32         {Name: "Stored", Help: "The total number of stored SDL transactions"},
33         {Name: "StoreError", Help: "The total number of SDL store errors"},
34 }
35
36 type SDLClient struct {
37         db    *sdl.SdlInstance
38         stat  map[string]Counter
39         mux   sync.Mutex
40         ready bool
41 }
42
43 type RNIBClient struct {
44         db *sdl.SdlInstance
45 }
46
47 // NewSDLClient returns a new SDLClient.
48 func NewSDLClient(ns string) *SDLClient {
49         return &SDLClient{
50                 db:    sdl.NewSdlInstance(ns, sdl.NewDatabase()),
51                 stat:  Metric.RegisterCounterGroup(SDLCounterOpts, "SDL"),
52                 ready: false,
53         }
54 }
55
56 func (s *SDLClient) TestConnection() {
57         // Test DB connection, and wait until ready!
58         for {
59                 if _, err := s.db.GetAll(); err == nil {
60                         break
61                 }
62                 Logger.Warn("Database connection not ready, waiting ...")
63                 time.Sleep(time.Duration(5 * time.Second))
64         }
65         s.ready = true
66         Logger.Info("Connection to database established!")
67 }
68
69 func (s *SDLClient) IsReady() bool {
70         return s.ready
71 }
72
73 func (s *SDLClient) Store(key string, value interface{}) (err error) {
74         err = s.db.Set(key, value)
75         if err != nil {
76                 s.UpdateStatCounter("StoreError")
77         } else {
78                 s.UpdateStatCounter("Stored")
79         }
80         return
81 }
82
83 func (s *SDLClient) Read(key string) (value map[string]interface{}, err error) {
84         value, err = s.db.Get([]string{key})
85         return
86 }
87
88 func (s *SDLClient) Subscribe(cb func(string, ...string), channel string) error {
89         return s.db.SubscribeChannel(cb, channel)
90 }
91
92 func (s *SDLClient) MSubscribe(cb func(string, ...string), channels ...string) error {
93         return s.db.SubscribeChannel(cb, channels...)
94 }
95
96 func (s *SDLClient) StoreAndPublish(channel string, event string, pairs ...interface{}) error {
97         return s.db.SetAndPublish([]string{channel, event}, pairs...)
98 }
99
100 func (s *SDLClient) MStoreAndPublish(channelsAndEvents []string, pairs ...interface{}) error {
101         return s.db.SetAndPublish(channelsAndEvents, pairs...)
102 }
103
104 func (s *SDLClient) Clear() {
105         s.db.RemoveAll()
106 }
107
108 func (s *SDLClient) RegisterMetrics() {
109         s.stat = Metric.RegisterCounterGroup(SDLCounterOpts, "SDL")
110 }
111
112 func (s *SDLClient) UpdateStatCounter(name string) {
113         s.mux.Lock()
114         s.stat[name].Inc()
115         s.mux.Unlock()
116 }
117
118 func (c *SDLClient) GetStat() (t SDLStatistics) {
119         return
120 }
121
122 // To be removed ...
123 func NewRNIBClient(ns string) *RNIBClient {
124         return &RNIBClient{
125                 db: sdl.NewSdlInstance(ns, sdl.NewDatabase()),
126         }
127 }
128
129 func (r *RNIBClient) GetgNBList() (values map[string]interface{}, err error) {
130         keys, err := r.db.GetAll()
131         if err == nil {
132                 values = make(map[string]interface{})
133                 for _, key := range keys {
134                         v, err := r.db.Get([]string{key})
135                         if err == nil {
136                                 values[key] = v[key]
137                         }
138                 }
139         }
140         return
141 }
142
143 func (r *RNIBClient) GetNRCellList(key string) (value map[string]interface{}, err error) {
144         return r.db.Get([]string{key})
145 }
146
147 func (r *RNIBClient) GetUE(key1, key2 string) (value map[string]interface{}, err error) {
148         return r.db.Get([]string{key1 + key2})
149 }
150
151 func (r *RNIBClient) Store(key string, value interface{}) (err error) {
152         return r.db.Set(key, value)
153 }
154
155 func (r *RNIBClient) Clear() {
156         r.db.RemoveAll()
157 }