-// Reserves and returns the next free sequence number
-func (r *Registry) ReserveSequenceNumber() (uint16, bool) {
- // Check is current SequenceNumber valid
- registryMutex.Lock()
- defer registryMutex.Unlock()
- sequenceNumber := r.counter
- if _, ok := r.register[sequenceNumber]; ok {
- xapp.Logger.Error("Invalid SeqenceNumber sequenceNumber: %v",sequenceNumber)
- return sequenceNumber, false
- }
- r.register[sequenceNumber] = false
-
- // Allocate next SequenceNumber value
- if r.counter == 65535 {
- r.counter = 0
+func (r *Registry) findExistingSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) *Subscription {
+
+ 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.
+ if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false {
+ subs.mutex.Unlock()
+ continue
+ }
+ subs.mutex.Unlock()
+
+ xapp.Logger.Debug("Registry: Mergeable subs found %s for %s", subs.String(), trans.String())
+ return subs
+ }
+ }
+ return nil
+}
+
+func (r *Registry) AssignToSubscription(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) (*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.Debug("CREATE %s", err)
+ 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 := r.findExistingSubs(trans, subReqMsg)
+ if subs == nil {
+ subs, err = r.allocateSubs(trans, subReqMsg)
+ if err != nil {
+ return nil, err
+ }
+ newAlloc = true
+ }
+
+ //
+ // Add to subscription
+ //
+ subs.mutex.Lock()
+ defer subs.mutex.Unlock()
+
+ epamount := subs.EpList.Size()
+
+ r.mutex.Unlock()
+ //
+ // Subscription route updates
+ //
+ if epamount == 1 {
+ subRouteAction := SubRouteInfo{subs.EpList, uint16(subs.ReqId.InstanceId)}
+ err = r.rtmgrClient.SubscriptionRequestCreate(subRouteAction)