Added REST notify error cause

Change-Id: I428177806ab0e9539017e2cf7bac0bc5a96ea71a
Signed-off-by: Markku Virtanen <markku.virtanen@nokia.com>
diff --git a/pkg/control/control.go b/pkg/control/control.go
index 580c8fc..70570cd 100755
--- a/pkg/control/control.go
+++ b/pkg/control/control.go
@@ -300,12 +300,14 @@
 
 	_, xAppRmrEndpoint, err := ConstructEndpointAddresses(*clientEndpoint)
 	if err != nil {
-		xapp.Logger.Error("%s", err.Error())
+		c.registry.DeleteRESTSubscription(restSubId)
+		xapp.Logger.Error("XAPP-SubReq transaction not created, endpoint createtion failed for RESTSubId=%s, Meid=%s", *restSubId, *meid)
 		return
 	}
 
 	var xAppEventInstanceID int64
 	var e2EventInstanceID int64
+	var errorCause string
 	for index := 0; index < len(subReqList.E2APSubscriptionRequests); index++ {
 		subReqMsg := subReqList.E2APSubscriptionRequests[index]
 
@@ -323,18 +325,19 @@
 		if err != nil {
 			// Send notification to xApp that prosessing of a Subscription Request has failed.
 			e2EventInstanceID = (int64)(0)
+			errorCause = err.Error()
 			resp := &models.SubscriptionResponse{
 				SubscriptionID: restSubId,
 				SubscriptionInstances: []*models.SubscriptionInstance{
 					&models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
-						ErrorCause:          nil, //TODO: Suitable Error cause.
+						ErrorCause:          &errorCause,
 						XappEventInstanceID: &xAppEventInstanceID},
 				},
 			}
 			// Mark REST subscription request processed.
 			restSubscription.SetProcessed()
-			xapp.Logger.Info("Sending unsuccessful REST notification to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
-				clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
+			xapp.Logger.Info("Sending unsuccessful REST notification (cause %s) to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
+				errorCause, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
 			xapp.Subscription.Notify(resp, *clientEndpoint)
 			c.UpdateCounter(cRestSubFailNotifToXapp)
 		} else {
@@ -375,15 +378,14 @@
 
 	err := c.tracker.Track(trans)
 	if err != nil {
-		err = fmt.Errorf("XAPP-SubReq: %s", idstring(err, trans))
-		xapp.Logger.Error("%s", err.Error())
+		xapp.Logger.Error("XAPP-SubReq Tracking error: %s", idstring(err, trans))
+		err = fmt.Errorf("Tracking failure")
 		return nil, err
 	}
 
 	subs, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c)
 	if err != nil {
-		err = fmt.Errorf("XAPP-SubReq: %s", idstring(err, trans))
-		xapp.Logger.Error("%s", err.Error())
+		xapp.Logger.Error("XAPP-SubReq Assign error: %s", idstring(err, trans))
 		return nil, err
 	}
 
@@ -400,14 +402,17 @@
 			trans.Release()
 			return themsg, nil
 		case *e2ap.E2APSubscriptionFailure:
-			err = fmt.Errorf("SubscriptionFailure received")
+			err = fmt.Errorf("E2 SubscriptionFailure received")
 			return nil, err
 		default:
+			err = fmt.Errorf("unexpected E2 subscription response received")
 			break
 		}
+	} else {
+		err = fmt.Errorf("E2 subscription response timeout")
 	}
-	err = fmt.Errorf("XAPP-SubReq: failed %s", idstring(err, trans, subs))
-	xapp.Logger.Error("%s", err.Error())
+
+	xapp.Logger.Error("XAPP-SubReq E2 subscription failed %s", idstring(err, trans, subs))
 	c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
 	return nil, err
 }
diff --git a/pkg/control/registry.go b/pkg/control/registry.go
index 416b415..bd4e15a 100644
--- a/pkg/control/registry.go
+++ b/pkg/control/registry.go
@@ -216,7 +216,8 @@
 	//
 	actionType, err := r.CheckActionTypes(subReqMsg)
 	if err != nil {
-		xapp.Logger.Debug("CREATE %s", err)
+		xapp.Logger.Info("CREATE %s", err)
+		err = fmt.Errorf("E2 content validation failed")
 		return nil, err
 	}
 
@@ -236,6 +237,8 @@
 	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
@@ -289,6 +292,8 @@
 	err := r.rtmgrClient.SubscriptionRequestCreate(subRouteAction)
 	if err != nil {
 		c.UpdateCounter(cRouteCreateFail)
+		xapp.Logger.Error("%s", err.Error())
+		err = fmt.Errorf("RTMGR route create failure")
 	}
 	return err
 }
@@ -298,6 +303,8 @@
 	err := r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
 	if err != nil {
 		c.UpdateCounter(cRouteCreateUpdateFail)
+		xapp.Logger.Error("%s", err.Error())
+		err = fmt.Errorf("RTMGR route update failure")
 		return err
 	}
 	c.UpdateCounter(cMergedSubscriptions)
diff --git a/pkg/control/restendpoint.go b/pkg/control/restendpoint.go
index 59c30d0..3b4f8f5 100644
--- a/pkg/control/restendpoint.go
+++ b/pkg/control/restendpoint.go
@@ -41,7 +41,7 @@
 	var xAppRMREndPoint string
 
 	if host == "" || (HTTP_port == 0 && RMR_port == 0) {
-		err := fmt.Errorf("ClientEndpoint aprovided no PORT numbers")
+		err := fmt.Errorf("ClientEndpoint provided without PORT numbers")
 		return "INVALID_HTTP_ADDRESS:" + host + (string)(*clientEndpoint.HTTPPort),
 			"INVALID_RMR_ADDRESS:" + host + (string)(*clientEndpoint.RMRPort),
 			err
diff --git a/pkg/control/ut_messaging_test.go b/pkg/control/ut_messaging_test.go
index 4f93f20..f272848 100644
--- a/pkg/control/ut_messaging_test.go
+++ b/pkg/control/ut_messaging_test.go
@@ -2418,7 +2418,7 @@
 	// Req
 	params := xappConn1.GetRESTSubsReqReportParams(subReqCount)
 	restSubId := xappConn1.SendRESTSubsReq(t, params)
-	xappConn1.ExpectRESTNotification(t, restSubId)
+	xappConn1.ExpectRESTNotificationNok(t, restSubId, "failAll")
 	waiter.WaitResult(t)
 
 	e2SubsId := xappConn1.WaitRESTNotification(t, restSubId)
@@ -2465,7 +2465,7 @@
 	params.SetMeid("RAN_NAME_1")
 	restSubId2 := xappConn2.SendRESTSubsReq(t, params)
 	xapp.Logger.Info("Send REST subscriber request for second subscriber : %v", restSubId2)
-	xappConn2.ExpectRESTNotification(t, restSubId2)
+	xappConn2.ExpectRESTNotificationNok(t, restSubId2, "allFail")
 	waiter.WaitResult(t)
 	// e2SubsId2 := xappConn2.WaitRESTNotification(t, restSubId2) - TOD: missing delete
 	xappConn2.WaitRESTNotification(t, restSubId2)
@@ -2941,6 +2941,7 @@
 //     |                 |              |
 //
 //-----------------------------------------------------------------------------
+
 func TestRESTSubReqRetryNoRespSubDelRespInSubmgr(t *testing.T) {
 	CaseBegin("TestRESTSubReqTwoRetriesNoRespSubDelRespInSubmgr start")
 
@@ -2966,9 +2967,8 @@
 	xapp.Logger.Info("Ignore 2nd REST subscriber request for subscriber : %v", restSubId)
 
 	delreq, delmsg := e2termConn1.RecvSubsDelReq(t)
-	xappConn1.ExpectRESTNotification(t, restSubId)
+	xappConn1.ExpectRESTNotificationNok(t, restSubId, "allFail")
 	e2termConn1.SendSubsDelResp(t, delreq, delmsg)
-	// e2SubsId := xappConn1.WaitRESTNotification(t, restSubId)	- TODO:  Should we delete this?
 	xappConn1.WaitRESTNotification(t, restSubId)
 
 	// Wait that subs is cleaned
@@ -3004,7 +3004,7 @@
 	e2termConn1.RecvSubsDelReq(t)
 	xapp.Logger.Info("Ignore 1st INTERNAL delete request for subscriber : %v", restSubId)
 
-	xappConn1.ExpectRESTNotification(t, restSubId)
+	xappConn1.ExpectRESTNotificationNok(t, restSubId, "allFail")
 	e2termConn1.RecvSubsDelReq(t)
 	xapp.Logger.Info("Ignore 2nd INTERNAL delete request for subscriber : %v", restSubId)
 
@@ -3082,7 +3082,7 @@
 	e2termConn1.RecvSubsDelReq(t)
 	xapp.Logger.Info("Ignore 1st INTERNAL delete request for subscriber : %v", restSubId)
 
-	xappConn1.ExpectRESTNotification(t, restSubId)
+	xappConn1.ExpectRESTNotificationNok(t, restSubId, "allFail")
 	e2termConn1.RecvSubsDelReq(t)
 	xapp.Logger.Info("Ignore 2nd INTERNAL delete request for subscriber : %v", restSubId)
 
@@ -3149,7 +3149,7 @@
 	e2termConn1.SendSubsFail(t, fparams1, cremsg1)
 
 	delreq1, delmsg1 := e2termConn1.RecvSubsDelReq(t)
-	xappConn1.ExpectRESTNotification(t, restSubId)
+	xappConn1.ExpectRESTNotificationNok(t, restSubId, "allFail")
 	e2termConn1.SendSubsDelResp(t, delreq1, delmsg1)
 	e2SubsId := xappConn1.WaitRESTNotification(t, restSubId)
 	xapp.Logger.Info("TEST: REST notification received e2SubsId=%v", e2SubsId)
@@ -5167,7 +5167,7 @@
 
 	_, cremsg = e2termConn1.RecvSubsReq(t)
 
-	xappConn1.ExpectRESTNotification(t, restSubId)
+	xappConn1.ExpectRESTNotificationNok(t, restSubId, "allFail")
 
 	// Subscription already created in E2 Node.
 	fparams := &teststube2ap.E2StubSubsFailParams{}
@@ -5240,7 +5240,7 @@
 
 	_, cremsg = e2termConn1.RecvSubsReq(t)
 
-	xappConn1.ExpectRESTNotification(t, restSubId)
+	xappConn1.ExpectRESTNotificationNok(t, restSubId, "allFail")
 
 	// Subscription already created in E2 Node.
 	fparams := &teststube2ap.E2StubSubsFailParams{}
@@ -5318,7 +5318,7 @@
 
 	_, cremsg = e2termConn1.RecvSubsReq(t)
 
-	xappConn1.ExpectRESTNotification(t, restSubId)
+	xappConn1.ExpectRESTNotificationNok(t, restSubId, "allFail")
 
 	// Subscription already created in E2 Node.
 	fparams := &teststube2ap.E2StubSubsFailParams{}
@@ -5394,7 +5394,7 @@
 
 	_, cremsg = e2termConn1.RecvSubsReq(t)
 
-	xappConn1.ExpectRESTNotification(t, restSubId)
+	xappConn1.ExpectRESTNotificationNok(t, restSubId, "allFail")
 
 	// Subscription already created in E2 Node.
 	fparams := &teststube2ap.E2StubSubsFailParams{}
@@ -5468,7 +5468,7 @@
 
 	_, cremsg = e2termConn1.RecvSubsReq(t)
 
-	xappConn1.ExpectRESTNotification(t, restSubId)
+	xappConn1.ExpectRESTNotificationNok(t, restSubId, "allFail")
 
 	// Subscription already created in E2 Node.
 	fparams.SetCauseVal(0, 1, 3) // CauseRIC / duplicate-action
@@ -5541,7 +5541,7 @@
 
 	_, cremsg = e2termConn1.RecvSubsReq(t)
 
-	xappConn1.ExpectRESTNotification(t, restSubId)
+	xappConn1.ExpectRESTNotificationNok(t, restSubId, "allFail")
 
 	// Subscription already created in E2 Node.
 	fparams.SetCauseVal(0, 1, 3) // CauseRIC / duplicate-action
@@ -5984,7 +5984,7 @@
 
 	// E2t: Receive SubsDelReq
 	delreq, delmsg := e2termConn1.RecvSubsDelReq(t)
-	xappConn1.ExpectRESTNotification(t, restSubId)
+	xappConn1.ExpectRESTNotificationNok(t, restSubId, "allFail")
 
 	// Subscription does not exist in in E2 Node.
 	e2termConn1.SendSubsDelFail(t, delreq, delmsg)