+ // try to add to endpointlist.
+ if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false {
+ subs.mutex.Unlock()
+ continue
+ }
+ subs.mutex.Unlock()
+
+ //Race collision during parallel incoming and deleted
+ xapp.Logger.Debug("Registry: Identical subs found %s for %s", subs.String(), trans.String())
+ return subs
+ }
+ }
+ return nil
+}
+
+func (r *Registry) AssignToSubscription(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) {
+ var err error
+ var newAlloc bool
+ r.mutex.Lock()
+ defer r.mutex.Unlock()
+
+ 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{CREATE, subs.EpList, subs.Seq}
+ err = r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
+ } else {
+ subRouteAction := SubRouteInfo{UPDATE, subs.EpList, subs.Seq}
+ err = r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
+ }
+ r.mutex.Lock()
+
+ if err != nil {
+ if newAlloc {
+ r.subIds = append(r.subIds, subs.Seq)
+ }
+ return nil, err
+ }
+
+ if newAlloc {
+ r.register[subs.Seq] = subs
+ }
+ xapp.Logger.Debug("Registry: Create %s", subs.String())
+ xapp.Logger.Debug("Registry: substable=%v", r.register)
+ return subs, nil
+}
+
+// TODO: Needs better logic when there is concurrent calls
+func (r *Registry) RemoveFromSubscription(subs *Subscription, trans *Transaction, waitRouteClean time.Duration) error {
+
+ r.mutex.Lock()
+ defer r.mutex.Unlock()
+ subs.mutex.Lock()
+ defer subs.mutex.Unlock()
+
+ delStatus := subs.EpList.DelEndpoint(trans.GetEndpoint())
+ epamount := subs.EpList.Size()
+
+ //
+ // If last endpoint remove from register map
+ //
+ if epamount == 0 {
+ if _, ok := r.register[subs.Seq]; ok {
+ xapp.Logger.Debug("Registry: Delete %s", subs.String())
+ delete(r.register, subs.Seq)