blob: 36fbfc1e63c475a799296b35740a96bd5dceaa4b [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 (
23 sdl "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
Mohamed Abukar2e78e422019-06-02 11:45:52 +030024 "sync"
25 "time"
26)
27
28// To be removed later
29type SDLStatistics struct{}
30
31var 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
36type SDLClient struct {
Mohamed Abukar349a0982019-06-08 18:15:42 +030037 db *sdl.SdlInstance
38 stat map[string]Counter
39 mux sync.Mutex
40 ready bool
Mohamed Abukar2e78e422019-06-02 11:45:52 +030041}
42
43type RNIBClient struct {
44 db *sdl.SdlInstance
45}
46
47// NewSDLClient returns a new SDLClient.
48func NewSDLClient(ns string) *SDLClient {
49 return &SDLClient{
50 db: sdl.NewSdlInstance(ns, sdl.NewDatabase()),
Mohamed Abukar349a0982019-06-08 18:15:42 +030051 stat: Metric.RegisterCounterGroup(SDLCounterOpts, "SDL"),
52 ready: false,
Mohamed Abukar2e78e422019-06-02 11:45:52 +030053 }
54}
55
56func (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 }
Mohamed Abukar349a0982019-06-08 18:15:42 +030065 s.ready = true
Mohamed Abukar2e78e422019-06-02 11:45:52 +030066 Logger.Info("Connection to database established!")
Mohamed Abukar349a0982019-06-08 18:15:42 +030067}
Mohamed Abukar2e78e422019-06-02 11:45:52 +030068
Mohamed Abukar349a0982019-06-08 18:15:42 +030069func (s *SDLClient) IsReady() bool {
70 return s.ready
Mohamed Abukar2e78e422019-06-02 11:45:52 +030071}
72
73func (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
83func (s *SDLClient) Read(key string) (value map[string]interface{}, err error) {
84 value, err = s.db.Get([]string{key})
85 return
86}
87
88func (s *SDLClient) Subscribe(cb func(string, ...string), channel string) error {
89 return s.db.SubscribeChannel(cb, channel)
90}
91
92func (s *SDLClient) MSubscribe(cb func(string, ...string), channels ...string) error {
93 return s.db.SubscribeChannel(cb, channels...)
94}
95
96func (s *SDLClient) StoreAndPublish(channel string, event string, pairs ...interface{}) error {
97 return s.db.SetAndPublish([]string{channel, event}, pairs...)
98}
99
100func (s *SDLClient) MStoreAndPublish(channelsAndEvents []string, pairs ...interface{}) error {
101 return s.db.SetAndPublish(channelsAndEvents, pairs...)
102}
103
104func (s *SDLClient) Clear() {
105 s.db.RemoveAll()
106}
107
108func (s *SDLClient) RegisterMetrics() {
109 s.stat = Metric.RegisterCounterGroup(SDLCounterOpts, "SDL")
110}
111
112func (s *SDLClient) UpdateStatCounter(name string) {
113 s.mux.Lock()
114 s.stat[name].Inc()
115 s.mux.Unlock()
116}
117
118func (c *SDLClient) GetStat() (t SDLStatistics) {
119 return
120}
121
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300122// To be removed ...
123func NewRNIBClient(ns string) *RNIBClient {
124 return &RNIBClient{
125 db: sdl.NewSdlInstance(ns, sdl.NewDatabase()),
126 }
127}
128
129func (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
143func (r *RNIBClient) GetNRCellList(key string) (value map[string]interface{}, err error) {
144 return r.db.Get([]string{key})
145}
146
147func (r *RNIBClient) GetUE(key1, key2 string) (value map[string]interface{}, err error) {
148 return r.db.Get([]string{key1 + key2})
149}
150
151func (r *RNIBClient) Store(key string, value interface{}) (err error) {
152 return r.db.Set(key, value)
153}
154
155func (r *RNIBClient) Clear() {
156 r.db.RemoveAll()
157}