RICPLT-2571 Make code change for MEID support
[ric-plt/submgr.git] / pkg / control / tracker.go
index e08f8db..0e6941d 100644 (file)
@@ -21,73 +21,133 @@ package control
 
 import (
        "fmt"
+       "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+       "strconv"
+       "sync"
 )
 
-/*
-Implements a record of ongoing transactions and helper functions to CRUD the records.
-*/
-type Tracker struct {
-       transaction_table map[Transaction_key]Transaction
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type TransactionXappKey struct {
+       RmrEndpoint
+       Xid string // xapp xid in req
 }
 
-func (t *Tracker) Init() {
-       t.transaction_table = make(map[Transaction_key]Transaction)
+func (key *TransactionXappKey) String() string {
+       return key.RmrEndpoint.String() + "/" + key.Xid
 }
 
-/*
-Checks if a tranascation with similar type has been ongoing. If not then creates one.
-Returns error if there is similar transatcion ongoing.
-*/
-func (t *Tracker) Track_transaction(key Transaction_key, xact Transaction) error{
-       if _, ok := t.transaction_table[key]; ok {
-               // TODO: Implement merge related check here. If the key is same but the value is different.
-               err := fmt.Errorf("Transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.trans_type )
-               return err
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type Transaction struct {
+       tracker           *Tracker // tracker instance
+       Subs              *Subscription
+       RmrEndpoint       RmrEndpoint
+       Xid               string          // xapp xid in req
+       OrigParams        *xapp.RMRParams // request orginal params
+       RespReceived      bool
+       ForwardRespToXapp bool
+       mutex             sync.Mutex
+}
+
+func (t *Transaction) String() string {
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
+       var subId string = "?"
+       if t.Subs != nil {
+               subId = strconv.FormatUint(uint64(t.Subs.Seq), 10)
        }
-       t.transaction_table[key] = xact
-       return nil
+       return subId + "/" + t.RmrEndpoint.String() + "/" + t.Xid
 }
 
-/*
-Retreives the transaction table entry for the given request.
-Returns error in case the transaction cannot be found.
-*/
-func (t *Tracker) Update_transaction(SubID uint16, trans_type Action, xact Transaction) error{
-       key := Transaction_key{SubID, trans_type}
-       if _, ok := t.transaction_table[key]; ok {
-               // TODO: Implement merge related check here. If the key is same but the value is different.
-               err := fmt.Errorf("Transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.trans_type )
-               return err
+func (t *Transaction) CheckResponseReceived() bool {
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
+       if t.RespReceived == false {
+               t.RespReceived = true
+               return false
        }
-       t.transaction_table[key] = xact
-       return nil
+       return true
 }
 
-/*
-Retreives the transaction table entry for the given request.
-Returns error in case the transaction cannot be found.
-*/
-func (t *Tracker) Retrive_transaction(subID uint16, act Action) (Transaction, error){
-       key := Transaction_key{subID, act}
-       var xact Transaction
-       if xact, ok := t.transaction_table[key]; ok {
-               return xact, nil
+func (t *Transaction) RetryTransaction() {
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
+       t.RespReceived = false
+}
+
+func (t *Transaction) Release() {
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
+       if t.Subs != nil {
+               t.Subs.UnSetTransaction(t)
+       }
+       if t.tracker != nil {
+               xappkey := TransactionXappKey{t.RmrEndpoint, t.Xid}
+               t.tracker.UnTrackTransaction(xappkey)
        }
-       err := fmt.Errorf("Tranaction record for Subscription ID %d and action %s does not exist", subID, act)
-       return xact, err
+       t.Subs = nil
+       t.tracker = nil
 }
 
-/*
-Deletes the transaction table entry for the given request and returns the deleted xapp's address and port for reference.
-Returns error in case the transaction cannot be found.
-*/
-func (t *Tracker) complete_transaction(subID uint16, act Action) (Transaction, error){
-       key := Transaction_key{subID, act}
-       var empty_transaction Transaction
-       if xact, ok := t.transaction_table[key]; ok {
-               delete(t.transaction_table, key)
-               return xact, nil
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type Tracker struct {
+       transactionXappTable map[TransactionXappKey]*Transaction
+       mutex                sync.Mutex
+}
+
+func (t *Tracker) Init() {
+       t.transactionXappTable = make(map[TransactionXappKey]*Transaction)
+}
+
+func (t *Tracker) TrackTransaction(subs *Subscription, endpoint RmrEndpoint, params *xapp.RMRParams, respReceived bool, forwardRespToXapp bool) (*Transaction, error) {
+
+       trans := &Transaction{
+               tracker:           nil,
+               Subs:              nil,
+               RmrEndpoint:       endpoint,
+               Xid:               params.Xid,
+               OrigParams:        params,
+               RespReceived:      respReceived,
+               ForwardRespToXapp: forwardRespToXapp,
+       }
+
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
+
+       xappkey := TransactionXappKey{endpoint, params.Xid}
+       if _, ok := t.transactionXappTable[xappkey]; ok {
+               err := fmt.Errorf("Tracker: Similar transaction with xappkey %s is ongoing, transaction %s not created ", xappkey, trans)
+               return nil, err
+       }
+
+       if subs.SetTransaction(trans) == false {
+               othTrans := subs.GetTransaction()
+               err := fmt.Errorf("Tracker: Subscription %s got already transaction ongoing: %s, transaction %s not created", subs, othTrans, trans)
+               return nil, err
+       }
+       trans.Subs = subs
+       if (trans.Subs.RmrEndpoint.Addr != trans.RmrEndpoint.Addr) || (trans.Subs.RmrEndpoint.Port != trans.RmrEndpoint.Port) {
+               err := fmt.Errorf("Tracker: Subscription endpoint %s mismatch with trans: %s", subs, trans)
+               trans.Subs.UnSetTransaction(nil)
+               return nil, err
+       }
+
+       trans.tracker = t
+       t.transactionXappTable[xappkey] = trans
+       return trans, nil
+}
+
+func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*Transaction, error) {
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
+       if trans, ok2 := t.transactionXappTable[xappKey]; ok2 {
+               delete(t.transactionXappTable, xappKey)
+               return trans, nil
        }
-       err := fmt.Errorf("Tranaction record for Subscription ID %d and action %s does not exist", subID, act)
-       return empty_transaction, err
+       return nil, fmt.Errorf("Tracker: No record for xappkey %s", xappKey)
 }