Subscription REST interface update
[ric-plt/submgr.git] / pkg / control / subscription.go
index 4514d00..1152520 100644 (file)
 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"
-       "strconv"
+
        "sync"
-       "time"
 )
 
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------
 type Subscription struct {
-       mutex      sync.Mutex      // Lock
-       registry   *Registry       // Registry
-       Seq        uint16          // SubsId
-       Meid       *xapp.RMRMeid   // Meid/ RanName
-       EpList     RmrEndpointList // Endpoints
-       DelEpList  RmrEndpointList // Endpoints
-       DelSeq     uint64
-       TransLock  sync.Mutex                     // Lock transactions, only one executed per time for subs
-       TheTrans   *Transaction                   // Ongoing transaction from xapp
-       SubReqMsg  *e2ap.E2APSubscriptionRequest  // Subscription information
-       SubRespMsg *e2ap.E2APSubscriptionResponse // Subscription information
-}
-
-func (s *Subscription) stringImpl() string {
-       return "subs(" + strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.Meid.RanName + "/" + s.EpList.String() + ")"
+       mutex            sync.Mutex                    // Lock
+       valid            bool                          // valid
+       registry         *Registry                     // Registry
+       ReqId            RequestId                     // ReqId (Requestor Id + Seq Nro a.k.a subsid)
+       Meid             *xapp.RMRMeid                 // Meid/RanName
+       EpList           xapp.RmrEndpointList          // Endpoints
+       RMRRouteCreated  bool                          // Does subscription have RMR route
+       TransLock        sync.Mutex                    // Lock transactions, only one executed per time for subs
+       TheTrans         TransactionIf                 // Ongoing transaction
+       SubReqMsg        *e2ap.E2APSubscriptionRequest // Subscription information
+       SubRFMsg         interface{}                   // Subscription information
+       PolicyUpdate     bool                          // This is true when policy subscrition is being updated. Used not to send delete for update after timeout or restart
+       RetryFromXapp    bool                          // Retry form xApp for subscription that already exist
+       SubRespRcvd      bool                          // Subscription response received
+       DeleteFromDb     bool                          // Delete subscription from db
+       NoRespToXapp     bool                          // Send no response for subscription delete to xApp after restart
+       DoNotWaitSubResp bool                          // Test flag. Response is not waited for Subscription Request
 }
 
 func (s *Subscription) String() string {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       return s.stringImpl()
-}
-
-func (s *Subscription) GetSubId() uint16 {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       return s.Seq
-}
-
-func (s *Subscription) GetMeid() *xapp.RMRMeid {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
+       meidstr := "N/A"
        if s.Meid != nil {
-               return s.Meid
+               meidstr = s.Meid.String()
        }
-       return nil
+       return "subs(" + s.ReqId.String() + "/" + meidstr + "/" + s.EpList.String() + ")"
 }
 
-func (s *Subscription) AddEndpoint(ep *RmrEndpoint) error {
+func (s *Subscription) GetCachedResponse() (interface{}, bool) {
        s.mutex.Lock()
        defer s.mutex.Unlock()
-       if ep == nil {
-               return fmt.Errorf("AddEndpoint no endpoint given")
-       }
-       if s.EpList.AddEndpoint(ep) {
-               s.DelEpList.DelEndpoint(ep)
-               if s.EpList.Size() == 1 {
-                       return s.updateRouteImpl(CREATE)
-               }
-               return s.updateRouteImpl(MERGE)
-       }
-       return nil
+       return s.SubRFMsg, s.valid
 }
 
-func (s *Subscription) DelEndpoint(ep *RmrEndpoint) error {
+func (s *Subscription) SetCachedResponse(subRFMsg interface{}, valid bool) (interface{}, bool) {
        s.mutex.Lock()
        defer s.mutex.Unlock()
-       var err error
-       if ep == nil {
-               return fmt.Errorf("DelEndpoint no endpoint given")
-       }
-       if s.EpList.HasEndpoint(ep) == false {
-               return fmt.Errorf("DelEndpoint endpoint not found")
-       }
-       if s.DelEpList.HasEndpoint(ep) == true {
-               return fmt.Errorf("DelEndpoint endpoint already under del")
-       }
-       s.DelEpList.AddEndpoint(ep)
-       go s.CleanCheck()
-       return err
+       s.SubRFMsg = subRFMsg
+       s.valid = valid
+       return s.SubRFMsg, s.valid
 }
 
-func (s *Subscription) CleanCheck() {
+func (s *Subscription) GetReqId() *RequestId {
        s.mutex.Lock()
        defer s.mutex.Unlock()
-       s.DelSeq++
-       // Only one clean ongoing
-       if s.DelSeq > 1 {
-               return
-       }
-       var currSeq uint64 = 0
-       // Make sure that routes to be deleted
-       // are not deleted too fast
-       for currSeq < s.DelSeq {
-               currSeq = s.DelSeq
-               s.mutex.Unlock()
-               time.Sleep(5 * time.Second)
-               s.mutex.Lock()
-       }
-       xapp.Logger.Info("DelEndpoint: delete cleaning %s", s.stringImpl())
-       if s.EpList.Size() <= s.DelEpList.Size() {
-               s.updateRouteImpl(DELETE)
-               go s.registry.DelSubscription(s.Seq)
-       } else if s.EpList.DelEndpoints(&s.DelEpList) {
-               s.updateRouteImpl(MERGE)
-       }
-       s.DelSeq = 0
-
+       return &s.ReqId
 }
 
-func (s *Subscription) IsTransactionReserved() bool {
+func (s *Subscription) GetMeid() *xapp.RMRMeid {
        s.mutex.Lock()
        defer s.mutex.Unlock()
-       if s.TheTrans != nil {
-               return true
-       }
-       return false
-
+       return s.Meid
 }
 
-func (s *Subscription) GetTransaction() *Transaction {
+func (s *Subscription) GetTransaction() TransactionIf {
        s.mutex.Lock()
        defer s.mutex.Unlock()
        return s.TheTrans
 }
 
-func (s *Subscription) WaitTransactionTurn(trans *Transaction) {
+func (s *Subscription) WaitTransactionTurn(trans TransactionIf) {
        s.TransLock.Lock()
        s.mutex.Lock()
        s.TheTrans = trans
        s.mutex.Unlock()
 }
 
-func (s *Subscription) ReleaseTransactionTurn(trans *Transaction) {
+func (s *Subscription) ReleaseTransactionTurn(trans TransactionIf) {
        s.mutex.Lock()
        if trans != nil && trans == s.TheTrans {
                s.TheTrans = nil
@@ -164,11 +105,68 @@ func (s *Subscription) ReleaseTransactionTurn(trans *Transaction) {
        s.TransLock.Unlock()
 }
 
-func (s *Subscription) updateRouteImpl(act Action) error {
-       subRouteAction := SubRouteInfo{act, s.EpList, s.Seq}
-       err := s.registry.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
-       if err != nil {
-               return fmt.Errorf("%s %s", s.stringImpl(), err.Error())
+func (s *Subscription) IsMergeable(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) bool {
+       s.mutex.Lock()
+       defer s.mutex.Unlock()
+
+       if s.valid == false {
+               return false
+       }
+
+       if s.SubReqMsg == nil {
+               return false
+       }
+
+       if s.Meid.RanName != trans.Meid.RanName {
+               return false
+       }
+
+       // EventTrigger check
+       if s.SubReqMsg.EventTriggerDefinition.Data.Length != subReqMsg.EventTriggerDefinition.Data.Length {
+               return false
+       }
+       for i := uint64(0); i < s.SubReqMsg.EventTriggerDefinition.Data.Length; i++ {
+               if s.SubReqMsg.EventTriggerDefinition.Data.Data[i] != subReqMsg.EventTriggerDefinition.Data.Data[i] {
+                       return false
+               }
+       }
+
+       // Actions check
+       if len(s.SubReqMsg.ActionSetups) != len(subReqMsg.ActionSetups) {
+               return false
+       }
+
+       for _, acts := range s.SubReqMsg.ActionSetups {
+               for _, actt := range subReqMsg.ActionSetups {
+                       if acts.ActionId != actt.ActionId {
+                               return false
+                       }
+                       if acts.ActionType != actt.ActionType {
+                               return false
+                       }
+
+                       if acts.ActionType != e2ap.E2AP_ActionTypeReport {
+                               return false
+                       }
+
+                       if acts.RicActionDefinitionPresent != actt.RicActionDefinitionPresent {
+                               return false
+                       }
+
+                       if acts.ActionDefinitionChoice.Data.Length != actt.ActionDefinitionChoice.Data.Length {
+                               return false
+                       }
+                       for i := uint64(0); i < acts.ActionDefinitionChoice.Data.Length; i++ {
+                               if acts.ActionDefinitionChoice.Data.Data[i] != actt.ActionDefinitionChoice.Data.Data[i] {
+                                       return false
+                               }
+                       }
+                       if acts.SubsequentAction.Present != actt.SubsequentAction.Present ||
+                               acts.SubsequentAction.Type != actt.SubsequentAction.Type ||
+                               acts.SubsequentAction.TimetoWait != actt.SubsequentAction.TimetoWait {
+                               return false
+                       }
+               }
        }
-       return nil
+       return true
 }