+func (r *Registry) allocateSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, resetTestFlag bool) (*Subscription, error) {
+ if len(r.subIds) > 0 {
+ subId := r.subIds[0]
+ r.subIds = r.subIds[1:]
+ if _, ok := r.register[subId]; ok == true {
+ r.subIds = append(r.subIds, subId)
+ return nil, fmt.Errorf("Registry: Failed to reserve subscription exists")
+ }
+ subs := &Subscription{
+ registry: r,
+ Meid: trans.Meid,
+ SubReqMsg: subReqMsg,
+ valid: true,
+ RetryFromXapp: false,
+ SubRespRcvd: false,
+ DeleteFromDb: false,
+ NoRespToXapp: false,
+ DoNotWaitSubResp: false,
+ }
+ subs.ReqId.Id = subReqMsg.RequestId.Id
+ subs.ReqId.InstanceId = subId
+ if resetTestFlag == true {
+ subs.DoNotWaitSubResp = true
+ }
+
+ if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false {
+ r.subIds = append(r.subIds, subs.ReqId.InstanceId)
+ return nil, fmt.Errorf("Registry: Endpoint existing already in subscription")
+ }
+ return subs, nil
+ }
+ return nil, fmt.Errorf("Registry: Failed to reserve subscription no free ids")
+}
+
+func (r *Registry) findExistingSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, bool) {
+
+ for _, subs := range r.register {
+ if subs.IsMergeable(trans, subReqMsg) {
+
+ //
+ // check if there has been race conditions
+ //
+ subs.mutex.Lock()
+ //subs has been set to invalid
+ if subs.valid == false {
+ subs.mutex.Unlock()
+ continue
+ }
+ // If size is zero, entry is to be deleted
+ if subs.EpList.Size() == 0 {
+ subs.mutex.Unlock()
+ continue
+ }
+ // Try to add to endpointlist. Adding fails if endpoint is already in the list
+ if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false {
+ subs.mutex.Unlock()
+ 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, false
+ }
+ }
+ return nil, false
+}
+
+func (r *Registry) AssignToSubscription(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, resetTestFlag bool, c *Control) (*Subscription, error) {
+ var err error
+ var newAlloc bool
+ r.mutex.Lock()
+ defer r.mutex.Unlock()
+
+ //
+ // Check validity of subscription action types
+ //
+ actionType, err := r.CheckActionTypes(subReqMsg)
+ if err != nil {
+ xapp.Logger.Info("CREATE %s", err)
+ err = fmt.Errorf("E2 content validation failed")
+ return nil, err
+ }
+
+ //
+ // Find possible existing Policy subscription
+ //
+ 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())
+ // Update message data to subscription
+ subs.SubReqMsg = subReqMsg
+ subs.SetCachedResponse(nil, true)
+ return subs, nil
+ }
+ }
+
+ subs, endPointFound := r.findExistingSubs(trans, subReqMsg)
+ if subs == nil {
+ if subs, err = r.allocateSubs(trans, subReqMsg, resetTestFlag); err != nil {
+ xapp.Logger.Error("%s", err.Error())
+ err = fmt.Errorf("subscription not allocated")
+ return nil, err
+ }
+ newAlloc = true
+ } else if endPointFound == true {
+ // Requesting endpoint is already present in existing subscription. This can happen if xApp is restarted.
+ subs.RetryFromXapp = true
+ xapp.Logger.Debug("CREATE subReqMsg.InstanceId=%v. Same subscription %s already exists.", subReqMsg.InstanceId, subs.String())
+ c.UpdateCounter(cDuplicateE2SubReq)
+ return subs, nil
+ }
+
+ //
+ // Add to subscription
+ //
+ subs.mutex.Lock()
+ defer subs.mutex.Unlock()
+
+ epamount := subs.EpList.Size()
+ xapp.Logger.Info("AssignToSubscription subs.EpList.Size()=%v", subs.EpList.Size())
+
+ r.mutex.Unlock()
+ //
+ // Subscription route updates
+ //
+ if epamount == 1 {
+ err = r.RouteCreate(subs, c)