Merge "Fix for DB read after VM restart and REST subscription query aded"
[ric-plt/submgr.git] / pkg / teststube2ap / stubE2.go
index 5d28d3b..72baf27 100644 (file)
@@ -21,6 +21,7 @@ package teststube2ap
 
 import (
        "fmt"
+       "runtime/debug"
        "strconv"
        "testing"
        "time"
@@ -48,6 +49,7 @@ type RmrTransactionId struct {
 type E2RestIds struct {
        RestSubsId string
        E2SubsId   uint32
+       ErrorCause string
 }
 
 func (trans *RmrTransactionId) String() string {
@@ -663,24 +665,38 @@ func (tc *E2Stub) SendSubsDelFail(t *testing.T, req *e2ap.E2APSubscriptionDelete
 // Callback handler for subscription response notifications
 //-----------------------------------------------------------------------------
 func (tc *E2Stub) SubscriptionRespHandler(resp *clientmodel.SubscriptionResponse) {
+
        if tc.subscriptionId == "SUBSCRIPTIONID NOT SET" {
-               tc.Info("REST notification received for %v while no SubscriptionID was not set for InstanceID=%v, RequestorID=%v (%v)",
-                       *resp.SubscriptionID, *resp.SubscriptionInstances[0].InstanceID, *resp.SubscriptionInstances[0].RequestorID, tc)
-               tc.CallBackNotification <- *resp.SubscriptionInstances[0].InstanceID
+               tc.Info("REST notification received for %v while no SubscriptionID was not set for E2EventInstanceID=%v, XappEventInstanceID=%v (%v)",
+                       *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID, tc)
+               tc.CallBackNotification <- *resp.SubscriptionInstances[0].E2EventInstanceID
        } else if tc.subscriptionId == *resp.SubscriptionID {
-               tc.Info("REST notification received SubscriptionID=%s, InstanceID=%v, RequestorID=%v (%v)",
-                       *resp.SubscriptionID, *resp.SubscriptionInstances[0].InstanceID, *resp.SubscriptionInstances[0].RequestorID, tc)
-               tc.CallBackNotification <- *resp.SubscriptionInstances[0].InstanceID
+               tc.Info("REST notification received SubscriptionID=%s, E2EventInstanceID=%v, RequestorID=%v (%v)",
+                       *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID, tc)
+               tc.CallBackNotification <- *resp.SubscriptionInstances[0].E2EventInstanceID
        } else {
-               tc.Info("MISMATCHING REST notification received SubscriptionID=%s<>%s, InstanceID=%v, RequestorID=%v (%v)",
-                       tc.subscriptionId, *resp.SubscriptionID, *resp.SubscriptionInstances[0].InstanceID, *resp.SubscriptionInstances[0].RequestorID, tc)
+               tc.Info("MISMATCHING REST notification received SubscriptionID=%s<>%s, E2EventInstanceID=%v, XappEventInstanceID=%v (%v)",
+                       tc.subscriptionId, *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID, tc)
        }
 }
 
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------
+
 func (tc *E2Stub) ExpectRESTNotification(t *testing.T, restSubsId string) {
+       tc.expectNotification(t, restSubsId, "")
+}
+
+func (tc *E2Stub) ExpectRESTNotificationOk(t *testing.T, restSubsId string) {
+       tc.expectNotification(t, restSubsId, "allOk")
+}
+
+func (tc *E2Stub) ExpectRESTNotificationNok(t *testing.T, restSubsId string, expectError string) {
+       tc.expectNotification(t, restSubsId, expectError)
+}
+
+func (tc *E2Stub) expectNotification(t *testing.T, restSubsId string, expectError string) {
 
        tc.Info("### Started to wait REST notification for %v on port %v f(RMR port %v), %v responses expected", restSubsId, *tc.clientEndpoint.HTTPPort, *tc.clientEndpoint.RMRPort, tc.requestCount)
        tc.restSubsIdList = []string{restSubsId}
@@ -694,7 +710,11 @@ func (tc *E2Stub) ExpectRESTNotification(t *testing.T, restSubsId string) {
                        if tc.requestCount == 0 {
                                tc.TestError(t, "### REST notification count unexpectedly ZERO for %s (%v)", restSubsId, tc)
                        } else if e2Ids.RestSubsId != restSubsId {
-                               tc.TestError(t, "### Unexpected REST notifications received |%s:%s| (%v)", e2Ids.RestSubsId, restSubsId, tc)
+                               tc.TestError(t, "### Unexpected REST notifications received, expected %s bunt got %s instead| (%v)", e2Ids.RestSubsId, restSubsId, tc)
+                       } else if e2Ids.ErrorCause == "" && expectError == "allFail" {
+                               tc.TestError(t, "### Unexpected ok cause received from REST notifications |%s:%s| (%v)", e2Ids.RestSubsId, restSubsId, tc)
+                       } else if e2Ids.ErrorCause != "" && expectError == "allOk" {
+                               tc.TestError(t, "### Unexpected error cause (%s) received from REST notifications |%s:%s| (%v)", e2Ids.ErrorCause, e2Ids.RestSubsId, restSubsId, tc)
                        } else {
                                tc.requestCount--
                                if tc.requestCount == 0 {
@@ -702,8 +722,14 @@ func (tc *E2Stub) ExpectRESTNotification(t *testing.T, restSubsId string) {
                                } else {
                                        tc.Info("### Expected REST notifications received for %s, (%v)", e2Ids.RestSubsId, tc)
                                }
+                               if e2Ids.ErrorCause != "" && expectError == "allFail" {
+                                       tc.Info("### REST Notification: %s, ErrorCause: %v", e2Ids.RestSubsId, e2Ids.ErrorCause)
+                               }
                                tc.Info("### REST Notification received Notif for %s : %v", e2Ids.RestSubsId, e2Ids.E2SubsId)
                                tc.ListedRESTNotifications <- e2Ids
+                               if len(tc.ListedRESTNotifications) > 1 {
+                                       panic("expectNotification - ListedRESTNotifications stacking up")
+                               }
                        }
                case <-time.After(15 * time.Second):
                        err := fmt.Errorf("### Timeout 15s expired while expecting REST notification for subsId: %v", restSubsId)
@@ -713,38 +739,57 @@ func (tc *E2Stub) ExpectRESTNotification(t *testing.T, restSubsId string) {
 }
 
 func (tc *E2Stub) WaitRESTNotification(t *testing.T, restSubsId string) uint32 {
+
+       stack := string(debug.Stack())
+
        select {
        case e2SubsId := <-tc.ListedRESTNotifications:
                if e2SubsId.RestSubsId == restSubsId {
                        tc.Info("### Expected REST notifications received %s, e2SubsId %v for endpoint=%s, (%v)", e2SubsId.RestSubsId, e2SubsId.E2SubsId, tc.clientEndpoint, tc)
                        return e2SubsId.E2SubsId
                } else {
-                       tc.TestError(t, "### Unexpected REST notifications received %s, expected %s for endpoint=%s, (%v)", e2SubsId.RestSubsId, restSubsId, tc.clientEndpoint, tc)
+                       tc.TestError(t, "### Unexpected REST notification %s received, expected %s for endpoint=%s, (%v)", e2SubsId.RestSubsId, restSubsId, tc.clientEndpoint, tc)
+                       xapp.Logger.Info("CALL STACK:\n %s", stack)
                        return 0
                }
        case <-time.After(15 * time.Second):
                err := fmt.Errorf("### Timeout 15s expired while waiting REST notification for subsId: %v", restSubsId)
                tc.TestError(t, "%s", err.Error())
+               xapp.Logger.Info("CALL STACK:\n %s", stack)
                panic("WaitRESTNotification - timeout error")
        }
-       return 0
 }
 
-func (tc *E2Stub) WaitRESTNotificationForAnySubscriptionId(t *testing.T) {
+// Note, this function should be followed by a handling of <-xappConn1.RESTNotification.
+func (tc *E2Stub) ExpectAnyNotification(t *testing.T) {
        go func() {
-               tc.Info("### REST notifications received for endpoint=%s, (%v)", tc.clientEndpoint, tc)
+               tc.Info("### Started waiting ANY REST notifications received for endpoint=%s, (%v)", tc.clientEndpoint, tc)
                select {
                case e2SubsId := <-tc.CallBackNotification:
-                       tc.Info("### REST notifications received e2SubsId %v for endpoint=%s, (%v)", e2SubsId, tc.clientEndpoint, tc)
+                       tc.Info("### ANY REST notifications received e2SubsId %v for endpoint=%s, (%v) via CallBackNotification", e2SubsId, tc.clientEndpoint, tc)
                        tc.RESTNotification <- (uint32)(e2SubsId)
                case <-time.After(15 * time.Second):
-                       err := fmt.Errorf("### Timeout 15s expired while waiting REST notification")
+                       err := fmt.Errorf("### Timeout 15s expired while waiting ANY REST notification")
                        tc.TestError(t, "%s", err.Error())
                        tc.RESTNotification <- 0
                }
        }()
 }
 
+func (tc *E2Stub) WaitAnyRESTNotification(t *testing.T) uint32 {
+
+       select {
+       case e2SubsId := <-tc.RESTNotification:
+               tc.Info("### Expected ANY REST notification received - e2SubsId %v for endpoint=%s, (%v)", e2SubsId, tc.clientEndpoint, tc)
+               return e2SubsId
+
+       case <-time.After(15 * time.Second):
+               err := fmt.Errorf("### Timeout 15s expired while waiting ANY REST notification")
+               tc.TestError(t, "%s", err.Error())
+               panic("WaitRESTNotification - timeout error")
+       }
+}
+
 func (tc *E2Stub) ListedRestNotifHandler(resp *clientmodel.SubscriptionResponse) {
 
        if len(tc.restSubsIdList) == 0 {
@@ -752,23 +797,27 @@ func (tc *E2Stub) ListedRestNotifHandler(resp *clientmodel.SubscriptionResponse)
        } else {
                for i, subsId := range tc.restSubsIdList {
                        if *resp.SubscriptionID == subsId {
-                               //tc.Info("Listed REST notifications received SubscriptionID=%s, InstanceID=%v, RequestorID=%v",
-                               //      *resp.SubscriptionID, *resp.SubscriptionInstances[0].InstanceID, *resp.SubscriptionInstances[0].RequestorID)
+                               //tc.Info("Listed REST notifications received SubscriptionID=%s, InstanceID=%v, XappEventInstanceID=%v",
+                               //      *resp.SubscriptionID, *resp.SubscriptionInstances[0].InstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID)
 
                                tc.restSubsIdList = append(tc.restSubsIdList[:i], tc.restSubsIdList[i+1:]...)
                                //tc.Info("Removed %s from Listed REST notifications, %v entries left", *resp.SubscriptionID, len(tc.restSubsIdList))
 
-                               tc.CallBackListedNotifications <- E2RestIds{*resp.SubscriptionID, uint32(*resp.SubscriptionInstances[0].InstanceID)}
+                               if resp.SubscriptionInstances[0].ErrorCause != nil {
+                                       tc.CallBackListedNotifications <- E2RestIds{*resp.SubscriptionID, uint32(*resp.SubscriptionInstances[0].E2EventInstanceID), *resp.SubscriptionInstances[0].ErrorCause}
+                               } else {
+                                       tc.CallBackListedNotifications <- E2RestIds{*resp.SubscriptionID, uint32(*resp.SubscriptionInstances[0].E2EventInstanceID), ""}
+                               }
 
                                if len(tc.restSubsIdList) == 0 {
-                                       //tc.Info("All listed REST notifications received for endpoint=%s", tc.clientEndpoint)
+                                       tc.Info("All listed REST notifications received for endpoint=%s", tc.clientEndpoint)
                                }
 
                                return
                        }
                }
-               tc.Error("UNKONWN REST notification received SubscriptionID=%s<>%s, InstanceID=%v, RequestorID=%v (%v)",
-                       tc.subscriptionId, *resp.SubscriptionID, *resp.SubscriptionInstances[0].InstanceID, *resp.SubscriptionInstances[0].RequestorID, tc)
+               tc.Error("UNKONWN REST notification received SubscriptionID=%s<>%s, InstanceID=%v, XappEventInstanceID=%v (%v)",
+                       tc.subscriptionId, *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID, tc)
        }
 }
 
@@ -807,7 +856,7 @@ func (tc *E2Stub) SendRESTSubsReq(t *testing.T, params *RESTSubsReqParams) strin
                return ""
        }
 
-       tc.subscriptionId = "SUBSCIPTIONID NOT SET"
+       tc.subscriptionId = "SUBSCRIPTIONID NOT SET"
 
        resp, err := xapp.Subscription.Subscribe(&params.SubsReqParams)
        if err != nil {
@@ -824,27 +873,10 @@ func (tc *E2Stub) SendRESTSubsReq(t *testing.T, params *RESTSubsReqParams) strin
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------
-func (tc *E2Stub) GetRESTSubsReqReportParams(subReqCount int, parameterSet int, actionDefinitionPresent bool, actionParamCount int) *RESTSubsReqParams {
-
-       reportParams := RESTSubsReqParams{}
-       if parameterSet == 1 {
-               reportParams.GetRESTSubsReqReportParams1(subReqCount, actionDefinitionPresent, actionParamCount, &tc.clientEndpoint, &tc.meid)
-       } else if parameterSet == 2 {
-
-       } else {
-               tc.Error("Invalid parameterSet=%v", parameterSet)
-       }
-       tc.requestCount = subReqCount
-       return &reportParams
-}
-
-//-----------------------------------------------------------------------------
-//
-//-----------------------------------------------------------------------------
-func (tc *E2Stub) GetRESTSubsReqReportParams1(subReqCount int, actionDefinitionPresent bool, actionParamCount int) *RESTSubsReqParams {
+func (tc *E2Stub) GetRESTSubsReqReportParams(subReqCount int) *RESTSubsReqParams {
 
        reportParams := RESTSubsReqParams{}
-       reportParams.GetRESTSubsReqReportParams1(subReqCount, actionDefinitionPresent, actionParamCount, &tc.clientEndpoint, &tc.meid)
+       reportParams.GetRESTSubsReqReportParams(subReqCount, &tc.clientEndpoint, &tc.meid)
        tc.requestCount = subReqCount
        return &reportParams
 }
@@ -856,7 +888,7 @@ type RESTSubsReqParams struct {
        SubsReqParams clientmodel.SubscriptionParams
 }
 
-func (p *RESTSubsReqParams) GetRESTSubsReqReportParams1(subReqCount int, actionDefinitionPresent bool, actionParamCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
+func (p *RESTSubsReqParams) GetRESTSubsReqReportParams(subReqCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
 
        // E2SM-gNB-X2
        p.SubsReqParams.ClientEndpoint = clientEndpoint
@@ -873,19 +905,17 @@ func (p *RESTSubsReqParams) GetRESTSubsReqReportParams1(subReqCount int, actionD
 
        for requestCount := 0; requestCount < subReqCount; requestCount++ {
                reqId := int64(requestCount) + 1
-               seqId := int64(requestCount) + 1
                subscriptionDetail := &clientmodel.SubscriptionDetail{
-                       RequestorID: &reqId,
-                       InstanceID:  &seqId,
-                       EventTriggers: &clientmodel.EventTriggerDefinition{
-                               OctetString: "1234" + strconv.Itoa(requestCount),
+                       XappEventInstanceID: &reqId,
+                       EventTriggers: clientmodel.EventTriggerDefinition{
+                               int64(1234 + requestCount),
                        },
                        ActionToBeSetupList: clientmodel.ActionsToBeSetup{
                                &clientmodel.ActionToBeSetup{
                                        ActionID:   &actionId,
                                        ActionType: &actionType,
-                                       ActionDefinition: &clientmodel.ActionDefinition{
-                                               OctetString: "5678" + strconv.Itoa(requestCount),
+                                       ActionDefinition: clientmodel.ActionDefinition{
+                                               int64(5678 + requestCount),
                                        },
                                        SubsequentAction: &clientmodel.SubsequentAction{
                                                SubsequentActionType: &subsequestActioType,
@@ -957,34 +987,32 @@ func (p *RESTSubsReqParams) SetSubActionIDs(actionId int64) {
        }
 }
 
-func (p *RESTSubsReqParams) SetSubActionDefinition(actionDefinition string) {
+func (p *RESTSubsReqParams) SetSubActionDefinition(actionDefinition []int64) {
 
        for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
                for _, action := range subDetail.ActionToBeSetupList {
                        if action != nil {
-                               action.ActionDefinition.OctetString = actionDefinition
+                               action.ActionDefinition = actionDefinition
                        }
                }
        }
 }
 
-func (p *RESTSubsReqParams) SetSubEventTriggerDefinition(eventTriggerDefinition string) {
+func (p *RESTSubsReqParams) SetSubEventTriggerDefinition(eventTriggerDefinition []int64) {
 
        for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
                if subDetail != nil {
-                       subDetail.EventTriggers.OctetString = eventTriggerDefinition
+                       subDetail.EventTriggers = eventTriggerDefinition
                }
        }
 }
 
-func (p *RESTSubsReqParams) AppendActionToActionToBeSetupList(actionId int64, actionType string, actionDefinition string, subsequentActionType string, timeToWait string) {
+func (p *RESTSubsReqParams) AppendActionToActionToBeSetupList(actionId int64, actionType string, actionDefinition []int64, subsequentActionType string, timeToWait string) {
 
        actionToBeSetup := &clientmodel.ActionToBeSetup{
-               ActionID:   &actionId,
-               ActionType: &actionType,
-               ActionDefinition: &clientmodel.ActionDefinition{
-                       OctetString: actionDefinition,
-               },
+               ActionID:         &actionId,
+               ActionType:       &actionType,
+               ActionDefinition: clientmodel.ActionDefinition(actionDefinition),
                SubsequentAction: &clientmodel.SubsequentAction{
                        SubsequentActionType: &subsequentActionType,
                        TimeToWait:           &timeToWait,
@@ -1021,11 +1049,21 @@ func (p *RESTSubsReqParams) SetTimeToWait(timeToWait string) {
        }
 }
 
+func (p *RESTSubsReqParams) SetSubscriptionID(SubscriptionID *string) {
+
+       if *SubscriptionID == "" {
+               return
+       }
+       p.SubsReqParams.SubscriptionID = *SubscriptionID
+}
+
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------
 func (tc *E2Stub) SendRESTSubsDelReq(t *testing.T, subscriptionID *string) {
 
+       tc.Info("======== Posting REST DELETE subscription(s) to Submgr ======")
+
        if *subscriptionID == "" {
                tc.Error("REST error in deleting subscriptions. Empty SubscriptionID = %s", *subscriptionID)
        }
@@ -1041,10 +1079,10 @@ func (tc *E2Stub) SendRESTSubsDelReq(t *testing.T, subscriptionID *string) {
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------
-func (tc *E2Stub) GetRESTSubsReqPolicyParams(subReqCount int, actionDefinitionPresent bool, policyParamCount int) *RESTSubsReqParams {
+func (tc *E2Stub) GetRESTSubsReqPolicyParams(subReqCount int) *RESTSubsReqParams {
 
        policyParams := RESTSubsReqParams{}
-       policyParams.GetRESTSubsReqPolicyParams(subReqCount, actionDefinitionPresent, policyParamCount, &tc.clientEndpoint, &tc.meid)
+       policyParams.GetRESTSubsReqPolicyParams(subReqCount, &tc.clientEndpoint, &tc.meid)
        tc.requestCount = subReqCount
        return &policyParams
 }
@@ -1052,7 +1090,18 @@ func (tc *E2Stub) GetRESTSubsReqPolicyParams(subReqCount int, actionDefinitionPr
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------
-func (p *RESTSubsReqParams) GetRESTSubsReqPolicyParams(subReqCount int, actionDefinitionPresent bool, policyParamCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
+func (tc *E2Stub) DecrementRequestCount() {
+       if tc.requestCount > 0 {
+               tc.requestCount -= 1
+       } else {
+               tc.Error("FAILED to decrement request count, count already ZERO")
+       }
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+func (p *RESTSubsReqParams) GetRESTSubsReqPolicyParams(subReqCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
 
        p.SubsReqParams.ClientEndpoint = clientEndpoint
        p.SubsReqParams.Meid = meid
@@ -1068,19 +1117,17 @@ func (p *RESTSubsReqParams) GetRESTSubsReqPolicyParams(subReqCount int, actionDe
 
        for requestCount := 0; requestCount < subReqCount; requestCount++ {
                reqId := int64(requestCount) + 1
-               seqId := int64(0)
                subscriptionDetail := &clientmodel.SubscriptionDetail{
-                       RequestorID: &reqId,
-                       InstanceID:  &seqId,
-                       EventTriggers: &clientmodel.EventTriggerDefinition{
-                               OctetString: "1234" + strconv.Itoa(requestCount),
+                       XappEventInstanceID: &reqId,
+                       EventTriggers: clientmodel.EventTriggerDefinition{
+                               int64(1234 + requestCount),
                        },
                        ActionToBeSetupList: clientmodel.ActionsToBeSetup{
                                &clientmodel.ActionToBeSetup{
                                        ActionID:   &actionId,
                                        ActionType: &actionType,
-                                       ActionDefinition: &clientmodel.ActionDefinition{
-                                               OctetString: "5678" + strconv.Itoa(requestCount),
+                                       ActionDefinition: clientmodel.ActionDefinition{
+                                               int64(5678 + requestCount),
                                        },
                                        SubsequentAction: &clientmodel.SubsequentAction{
                                                SubsequentActionType: &subsequestActioType,