Prepare for SdlInstance usage removal in xapp-frame 32/6732/1
authorTimo Tietavainen <timo.tietavainen@nokia.com>
Wed, 22 Sep 2021 22:34:07 +0000 (01:34 +0300)
committerTimo Tietavainen <timo.tietavainen@nokia.com>
Wed, 22 Sep 2021 22:34:07 +0000 (01:34 +0300)
SdlInstance type with its API functions has been deprecated and its
source code will be removed at some point of time from sdlgo repository.
sdlgo repository provides the SyncStorage type what should be used
instead of the SdlInstance type.

With this commit add SDLStorage class type, which provides functions
to write and read by using SDL SyncStorage API services. Add a public
variable SdlStorage of the type SDLStorage to xapp-frame's xapp package.
Xapp developers can use SdlStorage and its write and read functions when
they need to operate with SDL DB.

Mark xapp-frame's types and functions to be deprecated, which are
related to old SDL SdlInstance type. This should be a heads up for xapp
developers to start using of SdlStorage instead of old deprecated
implementation. Deprecated xapp-frame types and functions will be
removed in a later release of the xapp-frame.

Add also xapp name to SDL namespace string what is used for
configuration updating via REST API feature in xapp-frame. Otherwise SDL
keys could collide if different xapps are updating their configuration
via REST API.

Issue-Id: RIC-805

Signed-off-by: Timo Tietavainen <timo.tietavainen@nokia.com>
Change-Id: I9c3a6616864eb967721b9b848cae56f9ef5f259f

pkg/xapp/config.go
pkg/xapp/db.go
pkg/xapp/xapp.go
pkg/xapp/xapp_test.go

index ac60ac5..56986d3 100755 (executable)
@@ -124,13 +124,17 @@ func AddConfigChangeListener(f ConfigChangeCB) {
 
 func PublishConfigChange(appName, eventJson string) error {
        channel := fmt.Sprintf("CM_UPDATE:%s", appName)
-       if err := Sdl.StoreAndPublish(channel, eventJson, appName, eventJson); err != nil {
+       if err := SdlStorage.StoreAndPublish(getCmSdlNs(), channel, eventJson, appName, eventJson); err != nil {
                Logger.Error("Sdl.Store failed: %v", err)
                return err
        }
        return nil
 }
 
+func ReadConfig(appName string) (map[string]interface{}, error) {
+       return SdlStorage.Read(getCmSdlNs(), appName)
+}
+
 func GetPortData(pname string) (d PortData) {
        var getPolicies = func(policies []interface{}) (plist []int) {
                for _, p := range policies {
@@ -178,8 +182,12 @@ func GetPortData(pname string) (d PortData) {
        return
 }
 
+func getCmSdlNs() string {
+       return fmt.Sprintf("cm/%s", viper.GetString("name"))
+}
+
 func (*Configurator) SetSDLNotificationCB(appName string, sdlNotificationCb SDLNotificationCB) error {
-       return Sdl.Subscribe(sdlNotificationCb, fmt.Sprintf("CM_UPDATE:%s", appName))
+       return SdlStorage.Subscribe(getCmSdlNs(), sdlNotificationCb, fmt.Sprintf("CM_UPDATE:%s", appName))
 }
 
 func (*Configurator) GetString(key string) string {
index e422cfd..4cfc399 100755 (executable)
@@ -36,13 +36,19 @@ var SDLCounterOpts = []CounterOpts{
        {Name: "StoreError", Help: "The total number of SDL store errors"},
 }
 
-type SDLClient struct {
-       db    *sdl.SdlInstance
+type SDLStorage struct {
+       db    *sdl.SyncStorage
        stat  map[string]Counter
        mux   sync.Mutex
        ready bool
 }
 
+//Deprecated: Will be removed in a future release, please use SDLStorage type
+type SDLClient struct {
+       db        *SDLStorage
+       nameSpace string
+}
+
 // Alias
 type RNIBNodeType = rnibentities.Node_Type
 type RNIBGlobalNbId = rnibentities.GlobalNbId
@@ -71,22 +77,19 @@ type RNIBClient struct {
        writer rnibwriter.RNibWriter
 }
 
-// NewSDLClient returns a new SDLClient.
-func NewSDLClient(ns string) *SDLClient {
-       if ns == "" {
-               ns = "sdl"
-       }
-       return &SDLClient{
-               db:    sdl.NewSdlInstance(ns, sdl.NewDatabase()),
+// NewSdlStorage returns a new instance of SDLStorage type.
+func NewSdlStorage() *SDLStorage {
+       return &SDLStorage{
+               db:    sdl.NewSyncStorage(),
                stat:  Metric.RegisterCounterGroup(SDLCounterOpts, "SDL"),
                ready: false,
        }
 }
 
-func (s *SDLClient) TestConnection() {
+func (s *SDLStorage) TestConnection(namespace string) {
        // Test DB connection, and wait until ready!
        for {
-               if _, err := s.db.GetAll(); err == nil {
+               if _, err := s.db.GetAll(namespace); err == nil {
                        break
                }
                Logger.Warn("Database connection not ready, waiting ...")
@@ -96,12 +99,12 @@ func (s *SDLClient) TestConnection() {
        Logger.Info("Connection to database established!")
 }
 
-func (s *SDLClient) IsReady() bool {
+func (s *SDLStorage) IsReady() bool {
        return s.ready
 }
 
-func (s *SDLClient) doSet(pairs ...interface{}) (err error) {
-       err = s.db.Set(pairs)
+func (s *SDLStorage) doSet(namespace string, pairs ...interface{}) (err error) {
+       err = s.db.Set(namespace, pairs)
        if err != nil {
                s.UpdateStatCounter("StoreError")
        } else {
@@ -110,62 +113,153 @@ func (s *SDLClient) doSet(pairs ...interface{}) (err error) {
        return
 }
 
+func (s *SDLStorage) Store(namespace string, key string, value interface{}) (err error) {
+       return s.doSet(namespace, key, value)
+}
+
+func (s *SDLStorage) MStore(namespace string, pairs ...interface{}) (err error) {
+       return s.doSet(namespace, pairs)
+}
+
+func (s *SDLStorage) Read(namespace string, key string) (value map[string]interface{}, err error) {
+       return s.db.Get(namespace, []string{key})
+}
+
+func (s *SDLStorage) MRead(namespace string, key []string) (value map[string]interface{}, err error) {
+       return s.db.Get(namespace, key)
+}
+
+func (s *SDLStorage) ReadAllKeys(namespace string) (value []string, err error) {
+       return s.db.GetAll(namespace)
+}
+
+func (s *SDLStorage) Subscribe(namespace string, cb func(string, ...string), channel string) error {
+       return s.db.SubscribeChannel(namespace, cb, channel)
+}
+
+func (s *SDLStorage) MSubscribe(namespace string, cb func(string, ...string), channels ...string) error {
+       return s.db.SubscribeChannel(namespace, cb, channels...)
+}
+
+func (s *SDLStorage) StoreAndPublish(namespace string, channel string, event string, pairs ...interface{}) error {
+       return s.db.SetAndPublish(namespace, []string{channel, event}, pairs...)
+}
+
+func (s *SDLStorage) MStoreAndPublish(namespace string, channelsAndEvents []string, pairs ...interface{}) error {
+       return s.db.SetAndPublish(namespace, channelsAndEvents, pairs...)
+}
+
+func (s *SDLStorage) Delete(namespace string, keys []string) (err error) {
+       return s.db.Remove(namespace, keys)
+}
+
+func (s *SDLStorage) Clear(namespace string) {
+       s.db.RemoveAll(namespace)
+}
+
+func (s *SDLStorage) RegisterMetrics() {
+       s.stat = Metric.RegisterCounterGroup(SDLCounterOpts, "SDL")
+}
+
+func (s *SDLStorage) UpdateStatCounter(name string) {
+       s.mux.Lock()
+       s.stat[name].Inc()
+       s.mux.Unlock()
+}
+
+func (s *SDLStorage) GetStat() (t SDLStatistics) {
+       return
+}
+
+//NewSDLClient returns a new SDLClient.
+//Deprecated: Will be removed in a future release, please use NewSdlStorage
+func NewSDLClient(ns string) *SDLClient {
+       if ns == "" {
+               ns = "sdl"
+       }
+       return &SDLClient{
+               db:        NewSdlStorage(),
+               nameSpace: ns,
+       }
+}
+
+//Deprecated: Will be removed in a future release, please use the TestConnection Receiver function of the SDLStorage type.
+func (s *SDLClient) TestConnection() {
+       s.db.TestConnection(s.nameSpace)
+}
+
+func (s *SDLClient) IsReady() bool {
+       return s.db.ready
+}
+
+//Deprecated: Will be removed in a future release, please use the Store Receiver function of the SDLStorage type.
 func (s *SDLClient) Store(key string, value interface{}) (err error) {
-       return s.doSet(key, value)
+       return s.db.Store(s.nameSpace, key, value)
 }
 
+//Deprecated: Will be removed in a future release, please use the MStore Receiver function of the SDLStorage type.
 func (s *SDLClient) MStore(pairs ...interface{}) (err error) {
-       return s.doSet(pairs)
+       return s.db.MStore(s.nameSpace, pairs)
 }
 
+//Deprecated: Will be removed in a future release, please use the Read Receiver function of the SDLStorage type.
 func (s *SDLClient) Read(key string) (value map[string]interface{}, err error) {
-       return s.db.Get([]string{key})
+       return s.db.Read(s.nameSpace, key)
 }
 
+//Deprecated: Will be removed in a future release, please use the MRead Receiver function of the SDLStorage type.
 func (s *SDLClient) MRead(key []string) (value map[string]interface{}, err error) {
-       return s.db.Get(key)
+       return s.db.MRead(s.nameSpace, key)
 }
 
+//Deprecated: Will be removed in a future release, please use the ReadAllKeys Receiver function of the SDLStorage type.
 func (s *SDLClient) ReadAllKeys(key string) (value []string, err error) {
-       return s.db.GetAll()
+       return s.db.ReadAllKeys(s.nameSpace)
 }
 
+//Deprecated: Will be removed in a future release, please use the Subscribe Receiver function of the SDLStorage type.
 func (s *SDLClient) Subscribe(cb func(string, ...string), channel string) error {
-       return s.db.SubscribeChannel(cb, channel)
+       return s.db.Subscribe(s.nameSpace, cb, channel)
 }
 
+//Deprecated: Will be removed in a future release, please use the MSubscribe Receiver function of the SDLStorage type.
 func (s *SDLClient) MSubscribe(cb func(string, ...string), channels ...string) error {
-       return s.db.SubscribeChannel(cb, channels...)
+       return s.db.MSubscribe(s.nameSpace, cb, channels...)
 }
 
+//Deprecated: Will be removed in a future release, please use the StoreAndPublish Receiver function of the SDLStorage type.
 func (s *SDLClient) StoreAndPublish(channel string, event string, pairs ...interface{}) error {
-       return s.db.SetAndPublish([]string{channel, event}, pairs...)
+       return s.db.StoreAndPublish(s.nameSpace, channel, event, pairs...)
 }
 
+//Deprecated: Will be removed in a future release, please use the MStoreAndPublish Receiver function of the SDLStorage type.
 func (s *SDLClient) MStoreAndPublish(channelsAndEvents []string, pairs ...interface{}) error {
-       return s.db.SetAndPublish(channelsAndEvents, pairs...)
+       return s.db.MStoreAndPublish(s.nameSpace, channelsAndEvents, pairs...)
 }
 
+//Deprecated: Will be removed in a future release, please use the Delete Receiver function of the SDLStorage type.
 func (s *SDLClient) Delete(keys []string) (err error) {
-       return s.db.Remove(keys)
+       return s.db.Delete(s.nameSpace, keys)
 }
 
+//Deprecated: Will be removed in a future release, please use the Clear Receiver function of the SDLStorage type.
 func (s *SDLClient) Clear() {
-       s.db.RemoveAll()
+       s.db.Clear(s.nameSpace)
 }
 
+//Deprecated: Will be removed in a future release, please use the RegisterMetrics Receiver function of the SDLStorage type.
 func (s *SDLClient) RegisterMetrics() {
-       s.stat = Metric.RegisterCounterGroup(SDLCounterOpts, "SDL")
+       s.db.RegisterMetrics()
 }
 
+//Deprecated: Will be removed in a future release, please use the UpdateStatCounter Receiver function of the SDLStorage type.
 func (s *SDLClient) UpdateStatCounter(name string) {
-       s.mux.Lock()
-       s.stat[name].Inc()
-       s.mux.Unlock()
+       s.db.UpdateStatCounter(name)
 }
 
+//Deprecated: Will be removed in a future release, please use the GetStat Receiver function of the SDLStorage type.
 func (c *SDLClient) GetStat() (t SDLStatistics) {
-       return
+       return c.db.GetStat()
 }
 
 func NewRNIBClient() *RNIBClient {
index ad62e36..a316d9c 100755 (executable)
@@ -48,6 +48,7 @@ var (
        // XApp is an application instance
        Rmr           *RMRClient
        Sdl           *SDLClient
+       SdlStorage    *SDLStorage
        Rnib          *RNIBClient
        Resource      *Router
        Metric        *Metrics
@@ -64,7 +65,7 @@ var (
 )
 
 func IsReady() bool {
-       return Rmr != nil && Rmr.IsReady() && Sdl != nil && Sdl.IsReady()
+       return Rmr != nil && Rmr.IsReady() && SdlStorage != nil && SdlStorage.IsReady()
 }
 
 func SetReadyCB(cb ReadyCB, params interface{}) {
@@ -262,6 +263,7 @@ func init() {
        Config = Configurator{}
        Metric = NewMetrics(viper.GetString("metrics.url"), viper.GetString("metrics.namespace"), Resource.router)
        Subscription = NewSubscriber(viper.GetString("controls.subscription.host"), viper.GetInt("controls.subscription.timeout"))
+       SdlStorage = NewSdlStorage()
        Sdl = NewSDLClient(viper.GetString("controls.db.namespace"))
        Rnib = NewRNIBClient()
        Util = NewUtils()
@@ -279,7 +281,7 @@ func RunWithParams(c MessageConsumer, sdlcheck bool) {
        Logger.Info(fmt.Sprintf("Xapp started, listening on: %s", host))
 
        if sdlcheck {
-               Sdl.TestConnection()
+               SdlStorage.TestConnection(viper.GetString("controls.db.namespace"))
        }
        go registerXapp()
 
index b9ea423..c4aec60 100755 (executable)
@@ -335,7 +335,7 @@ func TestAddConfigChangeListener(t *testing.T) {
 }
 
 func TestConfigAccess(t *testing.T) {
-       Logger.Info("CASE: AddConfigChangeListener")
+       Logger.Info("CASE: TestConfigAccess")
 
        assert.Equal(t, Config.GetString("name"), "xapp")
        assert.Equal(t, Config.GetInt("controls.logger.level"), 3)
@@ -348,8 +348,9 @@ func TestConfigAccess(t *testing.T) {
 }
 
 func TestPublishConfigChange(t *testing.T) {
-       Logger.Info("CASE: AddConfigChangeListener")
+       Logger.Info("CASE: TestPublishConfigChange")
        PublishConfigChange("testApp", "values")
+       ReadConfig("testApp")
 }
 
 func TestNewSubscriber(t *testing.T) {
@@ -391,11 +392,13 @@ func TestSdlInterfaces(t *testing.T) {
        Sdl.Store("myKey", "Values")
        Sdl.MStore("myKey", "Values")
        Sdl.RegisterMetrics()
+       Sdl.UpdateStatCounter("Stored")
 
        // Misc.
        var NotificationCb = func(ch string, events ...string) {}
        Sdl.Subscribe(NotificationCb, "channel1")
        Sdl.MSubscribe(NotificationCb, "channel1", "channel2")
+       Sdl.StoreAndPublish("channel1", "event", "key1", "data1")
        Sdl.MStoreAndPublish([]string{"channel1"}, "event", "key1", "data1")
 }