FCA RIC-1353 xApp restart

Change-Id: I5ec8e61340a7d7b0d30eb284dd93000f55365a0a
Signed-off-by: Anssi Mannila <anssi.mannila@nokia.com>
diff --git a/pkg/control/registry.go b/pkg/control/registry.go
index 9e4eaaa..7ad54c1 100644
--- a/pkg/control/registry.go
+++ b/pkg/control/registry.go
@@ -87,7 +87,7 @@
 	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) {
@@ -106,18 +106,19 @@
 				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) {
@@ -140,7 +141,7 @@
 	//
 	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)
@@ -148,13 +149,18 @@
 		}
 	}
 
-	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
 	}
 
 	//