xapp-frame v0.8.2 integration to submgr
[ric-plt/submgr.git] / pkg / control / tracker.go
index 20af9f0..df3d56e 100644 (file)
@@ -21,6 +21,7 @@ package control
 
 import (
        "fmt"
+       "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
        "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
        "sync"
 )
@@ -30,59 +31,78 @@ import (
 //-----------------------------------------------------------------------------
 type Tracker struct {
        mutex                sync.Mutex
-       transactionXappTable map[TransactionXappKey]*Transaction
+       transactionXappTable map[TransactionXappKey]*TransactionXapp
+       transSeq             uint64
 }
 
 func (t *Tracker) Init() {
-       t.transactionXappTable = make(map[TransactionXappKey]*Transaction)
+       t.transactionXappTable = make(map[TransactionXappKey]*TransactionXapp)
 }
 
-func (t *Tracker) TrackTransaction(
-       endpoint *RmrEndpoint,
+func (t *Tracker) initTransaction(transBase *Transaction) {
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
+       transBase.EventChan = make(chan interface{})
+       transBase.tracker = t
+       transBase.Seq = t.transSeq
+       t.transSeq++
+}
+
+func (t *Tracker) NewSubsTransaction(subs *Subscription) *TransactionSubs {
+       trans := &TransactionSubs{}
+       trans.Meid = subs.GetMeid()
+       t.initTransaction(&trans.Transaction)
+       xapp.Logger.Debug("CREATE %s", trans.String())
+       return trans
+}
+
+func (t *Tracker) NewXappTransaction(
+       endpoint *xapp.RmrEndpoint,
        xid string,
-       meid *xapp.RMRMeid,
-       respReceived bool,
-       forwardRespToXapp bool) (*Transaction, error) {
+       requestId e2ap.RequestId,
+       meid *xapp.RMRMeid) *TransactionXapp {
 
-       if endpoint == nil {
-               err := fmt.Errorf("Tracker: No valid endpoint given")
-               return nil, err
-       }
+       trans := &TransactionXapp{}
+       trans.XappKey = &TransactionXappKey{requestId.Id, *endpoint, xid}
+       trans.Meid = meid
+       trans.RequestId = requestId
+       t.initTransaction(&trans.Transaction)
+       xapp.Logger.Debug("CREATE %s", trans.String())
+       return trans
+}
 
-       trans := &Transaction{
-               tracker:           nil,
-               Subs:              nil,
-               RmrEndpoint:       *endpoint,
-               Xid:               xid,
-               Meid:              meid,
-               RespReceived:      respReceived,
-               ForwardRespToXapp: forwardRespToXapp,
+func (t *Tracker) Track(trans *TransactionXapp) error {
+
+       if trans.GetEndpoint() == nil {
+               err := fmt.Errorf("Tracker: No valid endpoint given in %s", trans.String())
+               return err
        }
 
        t.mutex.Lock()
        defer t.mutex.Unlock()
 
-       xappkey := TransactionXappKey{*endpoint, xid}
-       if othtrans, ok := t.transactionXappTable[xappkey]; ok {
-               err := fmt.Errorf("Tracker: %s is ongoing, %s not created ", othtrans, trans)
-               return nil, err
+       theKey := *trans.XappKey
+
+       if othtrans, ok := t.transactionXappTable[theKey]; ok {
+               err := fmt.Errorf("Tracker: %s is ongoing, not tracking %s", othtrans, trans)
+               return err
        }
 
        trans.tracker = t
-       t.transactionXappTable[xappkey] = trans
-       xapp.Logger.Info("Tracker: Create %s", trans.String())
-       xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
-       return trans, nil
+       t.transactionXappTable[theKey] = trans
+       xapp.Logger.Debug("Tracker: Append %s", trans.String())
+       //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
+       return nil
 }
 
-func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*Transaction, error) {
+func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*TransactionXapp, error) {
        t.mutex.Lock()
        defer t.mutex.Unlock()
        if trans, ok2 := t.transactionXappTable[xappKey]; ok2 {
-               xapp.Logger.Info("Tracker: Delete %s", trans.String())
+               xapp.Logger.Debug("Tracker: Remove %s", trans.String())
                delete(t.transactionXappTable, xappKey)
-               xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
+               //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
                return trans, nil
        }
-       return nil, fmt.Errorf("Tracker: No record %s", xappKey)
+       return nil, fmt.Errorf("Tracker: No record %v", xappKey)
 }