Xapp-frame, v0.8.1 Rest Subscription Creation /Query /Deletion
[ric-plt/submgr.git] / pkg / control / transaction.go
index 735954e..efb630c 100644 (file)
 package control
 
 import (
-       "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/packer"
-       "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
        "strconv"
        "sync"
        "time"
+
+       "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
+       "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
 )
 
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type TransactionIf interface {
+       String() string
+       Release()
+       SendEvent(interface{}, time.Duration) (bool, bool)
+       WaitEvent(time.Duration) (interface{}, bool)
+}
+
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------
 
-type TransactionBase struct {
-       mutex     sync.Mutex         //
-       Seq       uint64             //
-       tracker   *Tracker           //tracker instance
-       Meid      *xapp.RMRMeid      //meid transaction related
-       Mtype     int                //Encoded message type to be send
-       Payload   *packer.PackedData //Encoded message to be send
+type Transaction struct {
+       mutex     sync.Mutex       //
+       Seq       uint64           //transaction sequence
+       tracker   *Tracker         //tracker instance
+       Meid      *xapp.RMRMeid    //meid transaction related
+       Mtype     int              //Encoded message type to be send
+       Payload   *e2ap.PackedData //Encoded message to be send
        EventChan chan interface{}
 }
 
-func (t *TransactionBase) SendEvent(event interface{}, waittime time.Duration) (bool, bool) {
+func (t *Transaction) String() string {
+       meidstr := "N/A"
+       if t.Meid != nil {
+               meidstr = t.Meid.String()
+       }
+       return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + meidstr + ")"
+}
+
+func (t *Transaction) SendEvent(event interface{}, waittime time.Duration) (bool, bool) {
        if waittime > 0 {
                select {
                case t.EventChan <- event:
@@ -55,7 +74,7 @@ func (t *TransactionBase) SendEvent(event interface{}, waittime time.Duration) (
        return true, false
 }
 
-func (t *TransactionBase) WaitEvent(waittime time.Duration) (interface{}, bool) {
+func (t *Transaction) WaitEvent(waittime time.Duration) (interface{}, bool) {
        if waittime > 0 {
                select {
                case event := <-t.EventChan:
@@ -68,13 +87,13 @@ func (t *TransactionBase) WaitEvent(waittime time.Duration) (interface{}, bool)
        return event, false
 }
 
-func (t *TransactionBase) GetMtype() int {
+func (t *Transaction) GetMtype() int {
        t.mutex.Lock()
        defer t.mutex.Unlock()
        return t.Mtype
 }
 
-func (t *TransactionBase) GetMeid() *xapp.RMRMeid {
+func (t *Transaction) GetMeid() *xapp.RMRMeid {
        t.mutex.Lock()
        defer t.mutex.Unlock()
        if t.Meid != nil {
@@ -83,17 +102,36 @@ func (t *TransactionBase) GetMeid() *xapp.RMRMeid {
        return nil
 }
 
-func (t *TransactionBase) GetPayload() *packer.PackedData {
+/*  // This function is not used. Commented out to get better test coverage result
+func (t *Transaction) GetPayload() *e2ap.PackedData {
        t.mutex.Lock()
        defer t.mutex.Unlock()
        return t.Payload
 }
+*/
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type TransactionSubs struct {
+       Transaction //
+}
+
+func (t *TransactionSubs) String() string {
+       return "transsubs(" + t.Transaction.String() + ")"
+}
+
+func (t *TransactionSubs) Release() {
+       t.mutex.Lock()
+       xapp.Logger.Debug("RELEASE %s", t.String())
+       t.tracker = nil
+       t.mutex.Unlock()
+}
 
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------
 type TransactionXappKey struct {
-       RmrEndpoint
+       xapp.RmrEndpoint
        Xid string // xapp xid in req
 }
 
@@ -104,20 +142,21 @@ func (key *TransactionXappKey) String() string {
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------
-type Transaction struct {
-       TransactionBase                     //
-       XappKey         *TransactionXappKey //
+type TransactionXapp struct {
+       Transaction
+       XappKey   *TransactionXappKey
+       RequestId e2ap.RequestId
 }
 
-func (t *Transaction) String() string {
+func (t *TransactionXapp) String() string {
        var transkey string = "transkey(N/A)"
        if t.XappKey != nil {
                transkey = t.XappKey.String()
        }
-       return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + t.Meid.RanName + "/" + transkey + ")"
+       return "transxapp(" + t.Transaction.String() + "/" + transkey + "/" + strconv.FormatUint(uint64(t.RequestId.InstanceId), 10) + ")"
 }
 
-func (t *Transaction) GetEndpoint() *RmrEndpoint {
+func (t *TransactionXapp) GetEndpoint() *xapp.RmrEndpoint {
        t.mutex.Lock()
        defer t.mutex.Unlock()
        if t.XappKey != nil {
@@ -126,7 +165,7 @@ func (t *Transaction) GetEndpoint() *RmrEndpoint {
        return nil
 }
 
-func (t *Transaction) GetXid() string {
+func (t *TransactionXapp) GetXid() string {
        t.mutex.Lock()
        defer t.mutex.Unlock()
        if t.XappKey != nil {
@@ -135,7 +174,8 @@ func (t *Transaction) GetXid() string {
        return ""
 }
 
-func (t *Transaction) GetSrc() string {
+/*  // This function is not used. Commented out to get better test coverage result
+func (t *TransactionXapp) GetSrc() string {
        t.mutex.Lock()
        defer t.mutex.Unlock()
        if t.XappKey != nil {
@@ -143,10 +183,16 @@ func (t *Transaction) GetSrc() string {
        }
        return ""
 }
+*/
+func (t *TransactionXapp) GetSubId() uint32 {
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
+       return t.RequestId.InstanceId
+}
 
-func (t *Transaction) Release() {
+func (t *TransactionXapp) Release() {
        t.mutex.Lock()
-       xapp.Logger.Debug("Transaction: Release %s", t.String())
+       xapp.Logger.Debug("RELEASE %s", t.String())
        tracker := t.tracker
        xappkey := t.XappKey
        t.tracker = nil