Add new interfaces
[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) doSet(pairs ...interface{}) (err error) {
74         err = s.db.Set(pairs)
75         if err != nil {
76                 s.UpdateStatCounter("StoreError")
77         } else {
78                 s.UpdateStatCounter("Stored")
79         }
80         return
81 }
82
83 func (s *SDLClient) Store(key string, value interface{}) (err error) {
84         return s.doSet(key, value)
85 }
86
87 func (s *SDLClient) MStore(pairs ...interface{}) (err error) {
88         return s.doSet(pairs)
89 }
90
91 func (s *SDLClient) Read(key string) (value map[string]interface{}, err error) {
92         return s.db.Get([]string{key})
93 }
94
95 func (s *SDLClient) MRead(key []string) (value map[string]interface{}, err error) {
96         return s.db.Get(key)
97 }
98
99 func (s *SDLClient) ReadAllKeys(key string) (value []string, err error) {
100         return s.db.GetAll()
101 }
102
103 func (s *SDLClient) Subscribe(cb func(string, ...string), channel string) error {
104         return s.db.SubscribeChannel(cb, channel)
105 }
106
107 func (s *SDLClient) MSubscribe(cb func(string, ...string), channels ...string) error {
108         return s.db.SubscribeChannel(cb, channels...)
109 }
110
111 func (s *SDLClient) StoreAndPublish(channel string, event string, pairs ...interface{}) error {
112         return s.db.SetAndPublish([]string{channel, event}, pairs...)
113 }
114
115 func (s *SDLClient) MStoreAndPublish(channelsAndEvents []string, pairs ...interface{}) error {
116         return s.db.SetAndPublish(channelsAndEvents, pairs...)
117 }
118
119 func (s *SDLClient) Clear() {
120         s.db.RemoveAll()
121 }
122
123 func (s *SDLClient) RegisterMetrics() {
124         s.stat = Metric.RegisterCounterGroup(SDLCounterOpts, "SDL")
125 }
126
127 func (s *SDLClient) UpdateStatCounter(name string) {
128         s.mux.Lock()
129         s.stat[name].Inc()
130         s.mux.Unlock()
131 }
132
133 func (c *SDLClient) GetStat() (t SDLStatistics) {
134         return
135 }
136
137 // To be removed ...
138 func NewRNIBClient(ns string) *RNIBClient {
139         return &RNIBClient{
140                 db: sdl.NewSdlInstance(ns, sdl.NewDatabase()),
141         }
142 }
143
144 func (r *RNIBClient) GetgNBList() (values map[string]interface{}, err error) {
145         keys, err := r.db.GetAll()
146         if err == nil {
147                 values = make(map[string]interface{})
148                 for _, key := range keys {
149                         v, err := r.db.Get([]string{key})
150                         if err == nil {
151                                 values[key] = v[key]
152                         }
153                 }
154         }
155         return
156 }
157
158 func (r *RNIBClient) GetNRCellList(key string) (value map[string]interface{}, err error) {
159         return r.db.Get([]string{key})
160 }
161
162 func (r *RNIBClient) GetUE(key1, key2 string) (value map[string]interface{}, err error) {
163         return r.db.Get([]string{key1 + key2})
164 }
165
166 func (r *RNIBClient) Store(key string, value interface{}) (err error) {
167         return r.db.Set(key, value)
168 }
169
170 func (r *RNIBClient) Clear() {
171         r.db.RemoveAll()
172 }