Xapp-frame, v0.8.1 Rest Subscription Creation /Query /Deletion
[ric-plt/submgr.git] / pkg / control / transaction.go
index 9adaeca..efb630c 100644 (file)
 package control
 
 import (
-       "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
-       "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 TransactionXappKey struct {
-       RmrEndpoint
-       Xid string // xapp xid in req
-}
-
-func (key *TransactionXappKey) String() string {
-       return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")"
+type TransactionIf interface {
+       String() string
+       Release()
+       SendEvent(interface{}, time.Duration) (bool, bool)
+       WaitEvent(time.Duration) (interface{}, bool)
 }
 
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------
+
 type Transaction struct {
-       mutex             sync.Mutex
-       tracker           *Tracker                             //tracker instance
-       Subs              *Subscription                        //related subscription
-       RmrEndpoint       RmrEndpoint                          //xapp endpoint
-       Xid               string                               //xapp xid in req
-       Meid              *xapp.RMRMeid                        //meid transaction related
-       SubReqMsg         *e2ap.E2APSubscriptionRequest        //SubReq TODO: maybe own transactions per type
-       SubRespMsg        *e2ap.E2APSubscriptionResponse       //SubResp TODO: maybe own transactions per type
-       SubFailMsg        *e2ap.E2APSubscriptionFailure        //SubFail TODO: maybe own transactions per type
-       SubDelReqMsg      *e2ap.E2APSubscriptionDeleteRequest  //SubDelReq TODO: maybe own transactions per type
-       SubDelRespMsg     *e2ap.E2APSubscriptionDeleteResponse //SubDelResp TODO: maybe own transactions per type
-       SubDelFailMsg     *e2ap.E2APSubscriptionDeleteFailure  //SubDelFail TODO: maybe own transactions per type
-       Mtype             int                                  //Encoded message type to be send
-       Payload           *packer.PackedData                   //Encoded message to be send
-       RespReceived      bool
-       ForwardRespToXapp bool
-}
-
-func (t *Transaction) StringImpl() string {
-       var subId string = "?"
-       if t.Subs != nil {
-               subId = strconv.FormatUint(uint64(t.Subs.Seq), 10)
-       }
-       return "trans(" + t.RmrEndpoint.String() + "/" + t.Xid + "/" + t.Meid.RanName + "/" + subId + ")"
+       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 *Transaction) String() string {
-       t.mutex.Lock()
-       defer t.mutex.Unlock()
-       return t.StringImpl()
+       meidstr := "N/A"
+       if t.Meid != nil {
+               meidstr = t.Meid.String()
+       }
+       return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + meidstr + ")"
 }
 
-func (t *Transaction) GetXid() string {
-       t.mutex.Lock()
-       defer t.mutex.Unlock()
-       return t.Xid
+func (t *Transaction) SendEvent(event interface{}, waittime time.Duration) (bool, bool) {
+       if waittime > 0 {
+               select {
+               case t.EventChan <- event:
+                       return true, false
+               case <-time.After(waittime):
+                       return false, true
+               }
+               return false, false
+       }
+       t.EventChan <- event
+       return true, false
+}
+
+func (t *Transaction) WaitEvent(waittime time.Duration) (interface{}, bool) {
+       if waittime > 0 {
+               select {
+               case event := <-t.EventChan:
+                       return event, false
+               case <-time.After(waittime):
+                       return nil, true
+               }
+       }
+       event := <-t.EventChan
+       return event, false
 }
 
 func (t *Transaction) GetMtype() int {
@@ -96,41 +102,103 @@ func (t *Transaction) GetMeid() *xapp.RMRMeid {
        return nil
 }
 
-func (t *Transaction) GetSrc() string {
+/*  // 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 {
+       xapp.RmrEndpoint
+       Xid string // xapp xid in req
+}
+
+func (key *TransactionXappKey) String() string {
+       return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")"
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type TransactionXapp struct {
+       Transaction
+       XappKey   *TransactionXappKey
+       RequestId e2ap.RequestId
+}
+
+func (t *TransactionXapp) String() string {
+       var transkey string = "transkey(N/A)"
+       if t.XappKey != nil {
+               transkey = t.XappKey.String()
+       }
+       return "transxapp(" + t.Transaction.String() + "/" + transkey + "/" + strconv.FormatUint(uint64(t.RequestId.InstanceId), 10) + ")"
+}
+
+func (t *TransactionXapp) GetEndpoint() *xapp.RmrEndpoint {
        t.mutex.Lock()
        defer t.mutex.Unlock()
-       return t.RmrEndpoint.String()
+       if t.XappKey != nil {
+               return &t.XappKey.RmrEndpoint
+       }
+       return nil
 }
 
-func (t *Transaction) CheckResponseReceived() bool {
+func (t *TransactionXapp) GetXid() string {
        t.mutex.Lock()
        defer t.mutex.Unlock()
-       if t.RespReceived == false {
-               t.RespReceived = true
-               return false
+       if t.XappKey != nil {
+               return t.XappKey.Xid
        }
-       return true
+       return ""
 }
 
-func (t *Transaction) RetryTransaction() {
+/*  // 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 {
+               return t.XappKey.RmrEndpoint.String()
+       }
+       return ""
+}
+*/
+func (t *TransactionXapp) GetSubId() uint32 {
        t.mutex.Lock()
        defer t.mutex.Unlock()
-       t.RespReceived = false
+       return t.RequestId.InstanceId
 }
 
-func (t *Transaction) Release() {
+func (t *TransactionXapp) Release() {
        t.mutex.Lock()
-       subs := t.Subs
+       xapp.Logger.Debug("RELEASE %s", t.String())
        tracker := t.tracker
-       xappkey := TransactionXappKey{t.RmrEndpoint, t.Xid}
-       t.Subs = nil
+       xappkey := t.XappKey
        t.tracker = nil
        t.mutex.Unlock()
 
-       if subs != nil {
-               subs.UnSetTransaction(t)
-       }
-       if tracker != nil {
-               tracker.UnTrackTransaction(xappkey)
+       if tracker != nil && xappkey != nil {
+               tracker.UnTrackTransaction(*xappkey)
        }
 }