Zero timeout is not allowed. Make sure that there is some default values
[ric-plt/submgr.git] / pkg / control / registry.go
index 5e16c0a..7ad54c1 100644 (file)
@@ -22,7 +22,6 @@ package control
 import (
        "fmt"
        "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
-       "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/xapptweaks"
        "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
        "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
        "sync"
@@ -88,7 +87,7 @@ func (r *Registry) allocateSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubs
        return nil, fmt.Errorf("Registry: Failed to reserve subscription no free ids")
 }
 
-func (r *Registry) findExistingSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) *Subscription {
+func (r *Registry) findExistingSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, bool) {
 
        for _, subs := range r.register {
                if subs.IsMergeable(trans, subReqMsg) {
@@ -107,18 +106,19 @@ func (r *Registry) findExistingSubs(trans *TransactionXapp, subReqMsg *e2ap.E2AP
                                subs.mutex.Unlock()
                                continue
                        }
-                       // try to add to endpointlist.
+                       // Try to add to endpointlist. Adding fails if endpoint is already in the list
                        if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false {
                                subs.mutex.Unlock()
-                               continue
+                               xapp.Logger.Debug("Registry: Subs with requesting endpoint found. %s for %s", subs.String(), trans.String())
+                               return subs, true
                        }
                        subs.mutex.Unlock()
 
-                       xapp.Logger.Debug("Registry: Mergeable subs found %s for %s", subs.String(), trans.String())
-                       return subs
+                       xapp.Logger.Debug("Registry: Mergeable subs found. %s for %s", subs.String(), trans.String())
+                       return subs, false
                }
        }
-       return nil
+       return nil, false
 }
 
 func (r *Registry) AssignToSubscription(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) {
@@ -141,19 +141,26 @@ func (r *Registry) AssignToSubscription(trans *TransactionXapp, subReqMsg *e2ap.
        //
        if actionType == e2ap.E2AP_ActionTypePolicy {
                if subs, ok := r.register[trans.GetSubId()]; ok {
-                       xapp.Logger.Debug("CREATE %s. Existing subscription for Policy found", subs.String())
+                       xapp.Logger.Debug("CREATE %s. Existing subscription for Policy found.", subs.String())
+                       // Update message data to subscription
+                       subs.SubReqMsg = subReqMsg
                        subs.SetCachedResponse(nil, true)
                        return subs, nil
                }
        }
 
-       subs := r.findExistingSubs(trans, subReqMsg)
+       subs, endPointFound := r.findExistingSubs(trans, subReqMsg)
        if subs == nil {
                subs, err = r.allocateSubs(trans, subReqMsg)
                if err != nil {
                        return nil, err
                }
                newAlloc = true
+       } else if endPointFound == true {
+               // Requesting endpoint is already present in existing subscription. This can happen if xApp is restarted.
+               xapp.Logger.Debug("CREATE: subscription already exists. %s", subs.String())
+               xapp.Logger.Debug("Registry: substable=%v", r.register)
+               return subs, nil
        }
 
        //
@@ -195,6 +202,7 @@ func (r *Registry) AssignToSubscription(trans *TransactionXapp, subReqMsg *e2ap.
 func (r *Registry) CheckActionTypes(subReqMsg *e2ap.E2APSubscriptionRequest) (uint64, error) {
        var reportFound bool = false
        var policyFound bool = false
+       var insertFound bool = false
 
        for _, acts := range subReqMsg.ActionSetups {
                if acts.ActionType == e2ap.E2AP_ActionTypeReport {
@@ -203,9 +211,12 @@ func (r *Registry) CheckActionTypes(subReqMsg *e2ap.E2APSubscriptionRequest) (ui
                if acts.ActionType == e2ap.E2AP_ActionTypePolicy {
                        policyFound = true
                }
+               if acts.ActionType == e2ap.E2AP_ActionTypeInsert {
+                       insertFound = true
+               }
        }
-       if reportFound == true && policyFound == true {
-               return e2ap.E2AP_ActionTypeInvalid, fmt.Errorf("Report and Policy in same RICactions-ToBeSetup-List")
+       if reportFound == true && policyFound == true || reportFound == true && insertFound == true || policyFound == true && insertFound == true {
+               return e2ap.E2AP_ActionTypeInvalid, fmt.Errorf("Different action types (Report, Policy or Insert) in same RICactions-ToBeSetup-List")
        }
        if reportFound == true {
                return e2ap.E2AP_ActionTypeReport, nil
@@ -213,6 +224,9 @@ func (r *Registry) CheckActionTypes(subReqMsg *e2ap.E2APSubscriptionRequest) (ui
        if policyFound == true {
                return e2ap.E2AP_ActionTypePolicy, nil
        }
+       if insertFound == true {
+               return e2ap.E2AP_ActionTypeInsert, nil
+       }
        return e2ap.E2AP_ActionTypeInvalid, fmt.Errorf("Invalid action type in RICactions-ToBeSetup-List")
 }
 
@@ -245,7 +259,7 @@ func (r *Registry) RemoveFromSubscription(subs *Subscription, trans *Transaction
                        //
                        // Subscription route delete
                        //
-                       tmpList := xapptweaks.RmrEndpointList{}
+                       tmpList := xapp.RmrEndpointList{}
                        tmpList.AddEndpoint(trans.GetEndpoint())
                        subRouteAction := SubRouteInfo{tmpList, uint16(subId)}
                        r.rtmgrClient.SubscriptionRequestDelete(subRouteAction)