blob: d1ea66f077ff749eb65a929d77abff2dea846e2f [file] [log] [blame]
Mohamed Abukar5120ec12020-02-04 11:01:24 +02001/*
2==================================================================================
3 Copyright (c) 2019 Nokia
4==================================================================================
5*/
6
7package xapp
8
9import (
Mohamed Abukar5953f7e2020-04-02 10:08:14 +030010 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientmodel"
Mohamed Abukar8dcedb42020-03-10 13:20:56 +020011 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
12 "github.com/stretchr/testify/assert"
Mohamed Abukar5120ec12020-02-04 11:01:24 +020013 "testing"
14 "time"
Mohamed Abukar5953f7e2020-04-02 10:08:14 +030015 "fmt"
Mohamed Abukar5120ec12020-02-04 11:01:24 +020016)
17
18var suite *testing.T
19
Mohamed Abukar429da192020-02-26 16:46:34 +020020var meid = "gnb123456"
21var funId = int64(1)
Mohamed Abukar47faec82020-04-09 22:08:19 +030022var clientEndpoint = "localhost:4560"
Mohamed Abukar5120ec12020-02-04 11:01:24 +020023var direction = int64(0)
24var procedureCode = int64(27)
25var typeOfMessage = int64(1)
26
Mohamed Abukar5953f7e2020-04-02 10:08:14 +030027var reportParams = clientmodel.ReportParams{
28 Meid: meid,
29 RANFunctionID: &funId,
Mohamed Abukar429da192020-02-26 16:46:34 +020030 ClientEndpoint: &clientEndpoint,
Mohamed Abukar5953f7e2020-04-02 10:08:14 +030031 EventTriggers: clientmodel.EventTriggerList{
32 &clientmodel.EventTrigger{
Mohamed Abukar429da192020-02-26 16:46:34 +020033 InterfaceDirection: direction,
34 ProcedureCode: procedureCode,
35 TypeOfMessage: typeOfMessage,
Mohamed Abukar5120ec12020-02-04 11:01:24 +020036 },
37 },
38}
39
Mohamed Abukar5953f7e2020-04-02 10:08:14 +030040var policyParams = clientmodel.PolicyParams{
41 Meid: &meid,
42 RANFunctionID: &funId,
Mohamed Abukar429da192020-02-26 16:46:34 +020043 ClientEndpoint: &clientEndpoint,
Mohamed Abukar5953f7e2020-04-02 10:08:14 +030044 EventTriggers: clientmodel.EventTriggerList{
45 &clientmodel.EventTrigger{
Mohamed Abukar429da192020-02-26 16:46:34 +020046 InterfaceDirection: direction,
47 ProcedureCode: procedureCode,
48 TypeOfMessage: typeOfMessage,
49 },
50 },
Mohamed Abukar5953f7e2020-04-02 10:08:14 +030051 PolicyActionDefinitions: &clientmodel.PolicyActionDefinition{},
52}
53
54func processSubscriptions(subscriptionId string) {
55 // Generate requestorId, instanceId
56 reqId := int64(11)
57 instanceId := int64(22)
58
59 resp := &models.SubscriptionResponse{
60 SubscriptionID: &subscriptionId,
61 SubscriptionInstances: []*models.SubscriptionInstance{
62 &models.SubscriptionInstance{RequestorID: &reqId, InstanceID: &instanceId},
63 },
64 }
65
66 // Notify the client: don't worry about errors ... Notify() will handle retries, etc.
67 Subscription.Notify(resp, clientEndpoint)
Mohamed Abukar5120ec12020-02-04 11:01:24 +020068}
69
Mohamed Abukarb6341a52020-03-23 08:55:05 +020070func subscriptionHandler(stype models.SubscriptionType, params interface{}) (*models.SubscriptionResponse, error) {
Mohamed Abukar5120ec12020-02-04 11:01:24 +020071 switch stype {
72 case models.SubscriptionTypeReport:
73 p := params.(*models.ReportParams)
Mohamed Abukar429da192020-02-26 16:46:34 +020074 assert.Equal(suite, meid, p.Meid)
75 assert.Equal(suite, funId, *p.RANFunctionID)
76 assert.Equal(suite, clientEndpoint, *p.ClientEndpoint)
77 assert.Equal(suite, direction, p.EventTriggers[0].InterfaceDirection)
78 assert.Equal(suite, procedureCode, p.EventTriggers[0].ProcedureCode)
79 assert.Equal(suite, typeOfMessage, p.EventTriggers[0].TypeOfMessage)
Mohamed Abukar5120ec12020-02-04 11:01:24 +020080 case models.SubscriptionTypePolicy:
81 p := params.(*models.PolicyParams)
Mohamed Abukar429da192020-02-26 16:46:34 +020082 assert.Equal(suite, clientEndpoint, *p.ClientEndpoint)
Mohamed Abukar5120ec12020-02-04 11:01:24 +020083 }
84
Mohamed Abukar5953f7e2020-04-02 10:08:14 +030085 // Generate a unique subscriptionId
86 subscriptionId := fmt.Sprintf("%s-%s", meid, clientEndpoint)
87
88 // Process subscriptions on the background
89 go processSubscriptions(subscriptionId)
90
91 // and send response immediately
Mohamed Abukarb6341a52020-03-23 08:55:05 +020092 return &models.SubscriptionResponse{
Mohamed Abukar5953f7e2020-04-02 10:08:14 +030093 SubscriptionID: &subscriptionId,
Mohamed Abukar429da192020-02-26 16:46:34 +020094 }, nil
Mohamed Abukar5120ec12020-02-04 11:01:24 +020095}
96
Mohamed Abukar9568a2d2020-02-18 16:50:32 +020097func queryHandler() (models.SubscriptionList, error) {
98 resp := models.SubscriptionList{
99 &models.SubscriptionData{
100 SubscriptionID: 11,
Mohamed Abukar8dcedb42020-03-10 13:20:56 +0200101 Meid: "Test-Gnb",
102 Endpoint: []string{"127.0.0.1:4056"},
Mohamed Abukar9568a2d2020-02-18 16:50:32 +0200103 },
104 }
105
106 return resp, nil
107}
108
Mohamed Abukar429da192020-02-26 16:46:34 +0200109func deleteHandler(ep string) error {
110 assert.Equal(suite, clientEndpoint, ep)
111
112 return nil
113}
114
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200115func TestSetup(t *testing.T) {
116 suite = t
117
118 // Start the server to simulate SubManager
Mohamed Abukar429da192020-02-26 16:46:34 +0200119 go Subscription.Listen(subscriptionHandler, queryHandler, deleteHandler)
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200120 time.Sleep(time.Duration(2) * time.Second)
121}
122
Mohamed Abukar9568a2d2020-02-18 16:50:32 +0200123func TestSubscriptionQueryHandling(t *testing.T) {
124 resp, err := Subscription.QuerySubscriptions()
125
126 assert.Equal(t, err, nil)
127 assert.Equal(t, resp[0].SubscriptionID, int64(11))
128 assert.Equal(t, resp[0].Meid, "Test-Gnb")
129 assert.Equal(t, resp[0].Endpoint, []string{"127.0.0.1:4056"})
130}
131
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200132func TestSubscriptionReportHandling(t *testing.T) {
Mohamed Abukar5953f7e2020-04-02 10:08:14 +0300133 Subscription.SetResponseCB(func(resp *clientmodel.SubscriptionResponse) {
134 assert.Equal(t, len(resp.SubscriptionInstances), 1)
135 assert.Equal(t, *resp.SubscriptionInstances[0].RequestorID, int64(11))
136 assert.Equal(t, *resp.SubscriptionInstances[0].InstanceID, int64(22))
137 })
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200138
Mohamed Abukar5953f7e2020-04-02 10:08:14 +0300139 _, err := Subscription.SubscribeReport(&reportParams)
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200140 assert.Equal(t, err, nil)
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200141}
142
143func TestSubscriptionPolicytHandling(t *testing.T) {
Mohamed Abukar5953f7e2020-04-02 10:08:14 +0300144 Subscription.SetResponseCB(func(resp *clientmodel.SubscriptionResponse) {
145 assert.Equal(t, len(resp.SubscriptionInstances), 1)
146 assert.Equal(t, *resp.SubscriptionInstances[0].RequestorID, int64(11))
147 assert.Equal(t, *resp.SubscriptionInstances[0].InstanceID, int64(22))
148 })
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200149
Mohamed Abukar5953f7e2020-04-02 10:08:14 +0300150 _, err := Subscription.SubscribePolicy(&policyParams)
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200151 assert.Equal(t, err, nil)
Mohamed Abukar8dcedb42020-03-10 13:20:56 +0200152}
Mohamed Abukar429da192020-02-26 16:46:34 +0200153
154func TestSubscriptionDeleteHandling(t *testing.T) {
155 err := Subscription.UnSubscribe(clientEndpoint)
156
157 assert.Equal(t, err, nil)
Mohamed Abukar5953f7e2020-04-02 10:08:14 +0300158}