Integrate RNIB with SDL SyncStorage to xapp-frame
[ric-plt/xapp-frame.git] / pkg / xapp / db.go
index e67c1c6..0a37caa 100755 (executable)
@@ -24,9 +24,6 @@ import (
        rnibentities "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
        rnibreader "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
        sdl "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
-       uenibprotobuf "gerrit.o-ran-sc.org/r/ric-plt/ue-nib/uernibprotobuf"
-       uenibreader "gerrit.o-ran-sc.org/r/ric-plt/ue-nib/uernibreader"
-       uenibwriter "gerrit.o-ran-sc.org/r/ric-plt/ue-nib/uernibwriter"
        rnibwriter "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/rnib"
        "sync"
        "time"
@@ -40,31 +37,24 @@ 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
 }
 
-// Alias
-type EventCategory = uenibreader.EventCategory
-type EventCallback = uenibreader.EventCallback
-type MeasResultNR = uenibprotobuf.MeasResultNR
-type MeasQuantityResults = uenibprotobuf.MeasResultNR_MeasQuantityResults
-type MeasResultServMO = uenibprotobuf.MeasResults_MeasResultServMO
-type MeasResults = uenibprotobuf.MeasResults
-
-type UENIBClient struct {
-       reader *uenibreader.Reader
-       writer *uenibwriter.Writer
+//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
 type RNIBNodebInfo = rnibentities.NodebInfo
-type RNIBIRNibError = rnibcommon.IRNibError
+type RNIBIRNibError = error
 type RNIBCells = rnibentities.Cells
 type RNIBNbIdentity = rnibentities.NbIdentity
 type RNIBCellType = rnibentities.Cell_Type
@@ -83,23 +73,24 @@ type RNIBServedNRCellInformation = rnibentities.ServedNRCellInformation
 type RNIBNrNeighbourInformation = rnibentities.NrNeighbourInformation
 
 type RNIBClient struct {
+       db     rnibcommon.ISdlSyncStorage
        reader rnibreader.RNibReader
        writer rnibwriter.RNibWriter
 }
 
-// NewSDLClient returns a new SDLClient.
-func NewSDLClient(ns string) *SDLClient {
-       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 ...")
@@ -109,12 +100,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 {
@@ -123,132 +114,213 @@ 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 NewUENIBClient() *UENIBClient {
-       return &UENIBClient{
-               reader: uenibreader.NewReader(),
-               writer: uenibwriter.NewWriter(),
+func GetNewRnibClient(sdlStorage rnibcommon.ISdlSyncStorage) *RNIBClient {
+       return &RNIBClient{
+               db:     sdlStorage,
+               reader: rnibreader.GetNewRNibReader(sdlStorage),
+               writer: rnibwriter.GetNewRNibWriter(sdlStorage),
        }
 }
 
-func (u *UENIBClient) StoreUeMeasurement(gNbId string, gNbUeX2ApId string, data *uenibprotobuf.MeasResults) error {
-       return u.writer.UpdateUeMeasurement(gNbId, gNbUeX2ApId, data)
-}
-
-func (u *UENIBClient) CreateUeContext(gNbId string, gNbUeX2ApId string) error {
-       return u.writer.UeContextAddComplete(gNbId, gNbUeX2ApId)
-}
-
-func (u *UENIBClient) ReleaseUeContext(gNbId string, gNbUeX2ApId string) error {
-       return u.writer.RemoveUeContext(gNbId, gNbUeX2ApId)
-}
-
-func (u *UENIBClient) ReadUeMeasurement(gNbId string, gNbUeX2ApId string) (*uenibprotobuf.MeasResults, error) {
-       return u.reader.GetUeMeasurement(gNbId, gNbUeX2ApId)
+//Deprecated: Will be removed in a future release, please use GetNewRnibClient instead.
+func NewRNIBClient() *RNIBClient {
+       s := sdl.NewSyncStorage()
+       return &RNIBClient{
+               db:     s,
+               reader: rnibreader.GetNewRNibReader(s),
+               writer: rnibwriter.GetNewRNibWriter(s),
+       }
 }
 
-func (u *UENIBClient) SubscribeEvents(gNbIDs []string, eventCategories []EventCategory, cb EventCallback) error {
-       return u.reader.SubscribeEvents(gNbIDs, eventCategories, cb)
+func (r *RNIBClient) Subscribe(cb func(string, ...string), channel string) error {
+       return r.db.SubscribeChannel(rnibcommon.GetRNibNamespace(), cb, channel)
 }
 
-func NewRNIBClient(ns string) *RNIBClient {
-       rnibreader.Init("rnib", 1)
-       rnibwriter.InitWriter("rnib", 1)
-       return &RNIBClient{
-               reader: nil,
-               writer: nil,
-       }
+func (r *RNIBClient) StoreAndPublish(channel string, event string, pairs ...interface{}) error {
+       return r.db.SetAndPublish(rnibcommon.GetRNibNamespace(), []string{channel, event}, pairs...)
 }
 
 func (r *RNIBClient) GetNodeb(invName string) (*RNIBNodebInfo, RNIBIRNibError) {
-       return rnibreader.GetRNibReader().GetNodeb(invName)
+       return r.reader.GetNodeb(invName)
 }
 
 func (r *RNIBClient) GetNodebByGlobalNbId(t RNIBNodeType, gid *RNIBGlobalNbId) (*RNIBNodebInfo, RNIBIRNibError) {
-       return rnibreader.GetRNibReader().GetNodebByGlobalNbId(t, gid)
+       return r.reader.GetNodebByGlobalNbId(t, gid)
 }
 
 func (r *RNIBClient) GetCellList(invName string) (*RNIBCells, RNIBIRNibError) {
-       return rnibreader.GetRNibReader().GetCellList(invName)
+       return r.reader.GetCellList(invName)
 }
 
-func (r *RNIBClient) GetListGnbIds() (*[]*RNIBNbIdentity, RNIBIRNibError) {
-       return rnibreader.GetRNibReader().GetListGnbIds()
+func (r *RNIBClient) GetListGnbIds() ([]*RNIBNbIdentity, RNIBIRNibError) {
+       return r.reader.GetListGnbIds()
 }
 
-func (r *RNIBClient) GetListEnbIds() (*[]*RNIBNbIdentity, RNIBIRNibError) {
-       return rnibreader.GetRNibReader().GetListEnbIds()
+func (r *RNIBClient) GetListEnbIds() ([]*RNIBNbIdentity, RNIBIRNibError) {
+       return r.reader.GetListEnbIds()
 }
 
 func (r *RNIBClient) GetCountGnbList() (int, RNIBIRNibError) {
-       return rnibreader.GetRNibReader().GetCountGnbList()
+       return r.reader.GetCountGnbList()
 }
 
 func (r *RNIBClient) GetCell(invName string, pci uint32) (*RNIBCell, RNIBIRNibError) {
-       return rnibreader.GetRNibReader().GetCell(invName, pci)
+       return r.reader.GetCell(invName, pci)
 }
 
 func (r *RNIBClient) GetCellById(cellType RNIBCellType, cellId string) (*RNIBCell, RNIBIRNibError) {
-       return rnibreader.GetRNibReader().GetCellById(cellType, cellId)
+       return r.reader.GetCellById(cellType, cellId)
 }
 
 func (r *RNIBClient) SaveNodeb(nbIdentity *RNIBNbIdentity, entity *RNIBNodebInfo) RNIBIRNibError {
-       return rnibwriter.GetRNibWriter().SaveNodeb(nbIdentity, entity)
+       return r.writer.SaveNodeb(nbIdentity, entity)
 }