RICPLT-3015 Reusing subscription if same EventTrigger and Action
[ric-plt/submgr.git] / pkg / control / subscription.go
index 6a72f74..5bfe2e1 100644 (file)
@@ -20,7 +20,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"
        "strconv"
        "sync"
@@ -30,20 +30,21 @@ import (
 //
 //-----------------------------------------------------------------------------
 type Subscription struct {
-       mutex    sync.Mutex
-       registry *Registry
-       Seq      uint16
-       Active   bool
-       //
-       Meid        *xapp.RMRMeid
-       RmrEndpoint // xapp endpoint. Now only one xapp can have relation to single subscription. To be changed in merge
-       Trans       *Transaction
+       mutex      sync.Mutex                     // Lock
+       valid      bool                           // valid
+       registry   *Registry                      // Registry
+       Seq        uint16                         // SubsId
+       Meid       *xapp.RMRMeid                  // Meid/ RanName
+       EpList     RmrEndpointList                // Endpoints
+       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
+       SubFailMsg *e2ap.E2APSubscriptionFailure  // Subscription information
 }
 
 func (s *Subscription) String() string {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       return strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.RmrEndpoint.String() + "/" + s.Meid.RanName
+       return "subs(" + strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.Meid.RanName + "/" + s.EpList.String() + ")"
 }
 
 func (s *Subscription) GetSubId() uint16 {
@@ -61,80 +62,112 @@ func (s *Subscription) GetMeid() *xapp.RMRMeid {
        return nil
 }
 
-func (s *Subscription) Confirmed() {
+func (s *Subscription) IsTransactionReserved() bool {
        s.mutex.Lock()
        defer s.mutex.Unlock()
-       s.Active = true
+       if s.TheTrans != nil {
+               return true
+       }
+       return false
+
 }
 
-func (s *Subscription) UnConfirmed() {
+func (s *Subscription) GetTransaction() *Transaction {
        s.mutex.Lock()
        defer s.mutex.Unlock()
-       s.Active = false
+       return s.TheTrans
 }
 
-func (s *Subscription) IsConfirmed() bool {
+func (s *Subscription) WaitTransactionTurn(trans *Transaction) {
+       s.TransLock.Lock()
        s.mutex.Lock()
-       defer s.mutex.Unlock()
-       return s.Active
+       s.TheTrans = trans
+       s.mutex.Unlock()
+}
+
+func (s *Subscription) ReleaseTransactionTurn(trans *Transaction) {
+       s.mutex.Lock()
+       if trans != nil && trans == s.TheTrans {
+               s.TheTrans = nil
+       }
+       s.mutex.Unlock()
+       s.TransLock.Unlock()
 }
 
-func (s *Subscription) SetTransaction(trans *Transaction) error {
+func (s *Subscription) IsSame(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) bool {
        s.mutex.Lock()
        defer s.mutex.Unlock()
 
-       subString := strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.RmrEndpoint.String() + "/" + s.Meid.RanName
+       if s.valid == false {
+               return false
+       }
 
-       if (s.RmrEndpoint.Addr != trans.RmrEndpoint.Addr) || (s.RmrEndpoint.Port != trans.RmrEndpoint.Port) {
-               return fmt.Errorf("Subscription: %s endpoint mismatch with trans: %s", subString, trans)
+       if s.SubReqMsg == nil {
+               return false
        }
-       if s.Trans != nil {
-               return fmt.Errorf("Subscription: %s trans %s exist, can not register %s", subString, s.Trans, trans)
+
+       if s.Meid.RanName != trans.Meid.RanName {
+               return false
        }
-       trans.Subs = s
-       s.Trans = trans
-       return nil
-}
 
-func (s *Subscription) UnSetTransaction(trans *Transaction) bool {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       if trans == nil || trans == s.Trans {
-               s.Trans = nil
-               return true
+       if s.EpList.Size() == 0 {
+               return false
        }
-       return false
-}
 
-func (s *Subscription) GetTransaction() *Transaction {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       return s.Trans
-}
+       //Somehow special case ... ?
+       if s.EpList.HasEndpoint(trans.GetEndpoint()) == true {
+               return false
+       }
 
-func (s *Subscription) updateRouteImpl(act Action) error {
-       xapp.Logger.Info("Subscription: Starting routing manager route add. SubId: %d, RmrEndpoint: %s", s.Seq, s.RmrEndpoint)
-       subRouteAction := SubRouteInfo{act, s.RmrEndpoint.Addr, s.RmrEndpoint.Port, s.Seq}
-       err := s.registry.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
-       if err != nil {
-               return fmt.Errorf("Subscription: Failed to add route. SubId: %d, RmrEndpoint: %s", s.Seq, s.RmrEndpoint)
+       // EventTrigger check
+       if s.SubReqMsg.EventTriggerDefinition.InterfaceDirection != subReqMsg.EventTriggerDefinition.InterfaceDirection ||
+               s.SubReqMsg.EventTriggerDefinition.ProcedureCode != subReqMsg.EventTriggerDefinition.ProcedureCode ||
+               s.SubReqMsg.EventTriggerDefinition.TypeOfMessage != subReqMsg.EventTriggerDefinition.TypeOfMessage {
+               return false
        }
-       return nil
-}
 
-func (s *Subscription) UpdateRoute(act Action) error {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       return s.updateRouteImpl(act)
-}
+       if s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.Present != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.Present ||
+               s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId ||
+               s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.Val[0] != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.Val[0] ||
+               s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.Val[1] != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.Val[1] ||
+               s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.Val[2] != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.Val[2] {
+               return false
+       }
 
-func (s *Subscription) Release() {
-       xapp.Logger.Info("Subscription: Releasing %s", s)
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       s.registry.DelSubscription(s.Seq)
-       err := s.updateRouteImpl(DELETE)
-       if err != nil {
-               xapp.Logger.Error("Registry: Failed to del route. SubId: %d, RmrEndpoint: %s", s.Seq, s.RmrEndpoint)
+       if s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.Present != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.Present ||
+               s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.NodeId != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.NodeId ||
+               s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.PlmnIdentity.Val[0] != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.PlmnIdentity.Val[0] ||
+               s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.PlmnIdentity.Val[1] != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.PlmnIdentity.Val[1] ||
+               s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.PlmnIdentity.Val[2] != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.PlmnIdentity.Val[2] {
+               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.ActionDefinition.Present != actt.ActionDefinition.Present ||
+                               acts.ActionDefinition.StyleId != actt.ActionDefinition.StyleId ||
+                               acts.ActionDefinition.ParamId != actt.ActionDefinition.ParamId {
+                               return false
+                       }
+                       if acts.SubsequentAction.Present != actt.SubsequentAction.Present ||
+                               acts.SubsequentAction.Type != actt.SubsequentAction.Type ||
+                               acts.SubsequentAction.TimetoWait != actt.SubsequentAction.TimetoWait {
+                               return false
+                       }
+               }
+       }
+
+       return true
 }