FCA RIC-1353 xApp restart

Change-Id: I5ec8e61340a7d7b0d30eb284dd93000f55365a0a
Signed-off-by: Anssi Mannila <anssi.mannila@nokia.com>
diff --git a/pkg/control/control.go b/pkg/control/control.go
index 5128458..a5d81fb 100755
--- a/pkg/control/control.go
+++ b/pkg/control/control.go
@@ -54,12 +54,11 @@
 //
 //-----------------------------------------------------------------------------
 
-var e2tSubReqTimeout time.Duration = 5 * time.Second
-var e2tSubDelReqTime time.Duration = 5 * time.Second
-var e2tMaxSubReqTryCount uint64 = 2    // Initial try + retry
-var e2tMaxSubDelReqTryCount uint64 = 2 // Initial try + retry
-
-var e2tRecvMsgTimeout time.Duration = 5 * time.Second
+var e2tSubReqTimeout time.Duration
+var e2tSubDelReqTime time.Duration
+var e2tRecvMsgTimeout time.Duration
+var e2tMaxSubReqTryCount uint64    // Initial try + retry
+var e2tMaxSubDelReqTryCount uint64 // Initial try + retry
 
 type Control struct {
 	*xapp.RMRClient
@@ -85,6 +84,18 @@
 
 func NewControl() *Control {
 
+	// viper.GetDuration returns nanoseconds
+	e2tSubReqTimeout = viper.GetDuration("controls.e2tSubReqTimeout_ms") * 1000000
+	xapp.Logger.Info("e2tSubReqTimeout %v", e2tSubReqTimeout)
+	e2tSubDelReqTime = viper.GetDuration("controls.e2tSubDelReqTime_ms") * 1000000
+	xapp.Logger.Info("e2tSubDelReqTime %v", e2tSubDelReqTime)
+	e2tRecvMsgTimeout = viper.GetDuration("controls.e2tRecvMsgTimeout_ms") * 1000000
+	xapp.Logger.Info("e2tRecvMsgTimeout %v", e2tRecvMsgTimeout)
+	e2tMaxSubReqTryCount = viper.GetUint64("controls.e2tMaxSubReqTryCount")
+	xapp.Logger.Info("e2tMaxSubReqTryCount %v", e2tMaxSubReqTryCount)
+	e2tMaxSubDelReqTryCount = viper.GetUint64("controls.e2tMaxSubDelReqTryCount")
+	xapp.Logger.Info("e2tMaxSubDelReqTryCount %v", e2tMaxSubDelReqTryCount)
+
 	transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
 	rtmgrClient := RtmgrClient{rtClient: rtmgrclient.New(transport, strfmt.Default)}
 
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
 	}
 
 	//
diff --git a/pkg/control/ut_messaging_test.go b/pkg/control/ut_messaging_test.go
index 36e4f29..6068d66 100644
--- a/pkg/control/ut_messaging_test.go
+++ b/pkg/control/ut_messaging_test.go
@@ -1653,3 +1653,71 @@
 	e2termConn1.TestMsgChanEmpty(t)
 	mainCtrl.wait_registry_empty(t, 10)
 }
+
+//-----------------------------------------------------------------------------
+// TestSubReqRetransmissionWithSameSubIdDiffXid
+//
+// This case simulates case where xApp restarts and starts sending same
+// subscription requests which have already subscribed successfully
+
+//   stub                          stub
+// +-------+     +---------+    +---------+
+// | xapp  |     | submgr  |    | e2term  |
+// +-------+     +---------+    +---------+
+//     |              |              |
+//     |  SubReq      |              |
+//     |------------->|              |
+//     |              |              |
+//     |              | SubReq       |
+//     |              |------------->|
+//     |              |              |
+//     |              |      SubResp |
+//     |              |<-------------|
+//     |              |              |
+//     |      SubResp |              |
+//     |<-------------|              |
+//     |              |              |
+//     | xApp restart |              |
+//     |              |              |
+//     |  SubReq      |              |
+//     | (retrans with same xApp generated subid but diff xid)
+//     |------------->|              |
+//     |              |              |
+//     |      SubResp |              |
+//     |<-------------|              |
+//     |              |              |
+//     |         [SUBS DELETE]       |
+//     |              |              |
+//
+//-----------------------------------------------------------------------------
+func TestSubReqRetransmissionWithSameSubIdDiffXid(t *testing.T) {
+	CaseBegin("TestSubReqRetransmissionWithSameSubIdDiffXid")
+
+	//Subs Create
+	cretrans := xappConn1.SendSubsReq(t, nil, nil)
+	crereq, cremsg := e2termConn1.RecvSubsReq(t)
+	e2termConn1.SendSubsResp(t, crereq, cremsg)
+	e2SubsId := xappConn1.RecvSubsResp(t, cretrans)
+
+	// xApp restart here
+	// --> artificial delay
+	<-time.After(1 * time.Second)
+
+	//Subs Create
+	cretrans = xappConn1.SendSubsReq(t, nil, nil) //Retransmitted SubReq
+	e2SubsId = xappConn1.RecvSubsResp(t, cretrans)
+
+	//Subs Delete
+	deltrans := xappConn1.SendSubsDelReq(t, nil, e2SubsId)
+	delreq, delmsg := e2termConn1.RecvSubsDelReq(t)
+	e2termConn1.SendSubsDelResp(t, delreq, delmsg)
+	xappConn1.RecvSubsDelResp(t, deltrans)
+
+	//Wait that subs is cleaned
+	mainCtrl.wait_subs_clean(t, e2SubsId, 10)
+
+	xappConn1.TestMsgChanEmpty(t)
+	xappConn2.TestMsgChanEmpty(t)
+	e2termConn1.TestMsgChanEmpty(t)
+	mainCtrl.wait_registry_empty(t, 10)
+}