Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
| 3 | Copyright (c) 2019 Nokia |
| 4 | ================================================================================== |
| 5 | */ |
| 6 | |
| 7 | package xapp |
| 8 | |
| 9 | import ( |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 10 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientmodel" |
Mohamed Abukar | 8dcedb4 | 2020-03-10 13:20:56 +0200 | [diff] [blame] | 11 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models" |
| 12 | "github.com/stretchr/testify/assert" |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 13 | "testing" |
| 14 | "time" |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 15 | "fmt" |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 16 | ) |
| 17 | |
| 18 | var suite *testing.T |
| 19 | |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 20 | var meid = "gnb123456" |
| 21 | var funId = int64(1) |
Mohamed Abukar | 47faec8 | 2020-04-09 22:08:19 +0300 | [diff] [blame] | 22 | var clientEndpoint = "localhost:4560" |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 23 | var direction = int64(0) |
| 24 | var procedureCode = int64(27) |
| 25 | var typeOfMessage = int64(1) |
| 26 | |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 27 | var reportParams = clientmodel.ReportParams{ |
| 28 | Meid: meid, |
| 29 | RANFunctionID: &funId, |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 30 | ClientEndpoint: &clientEndpoint, |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 31 | EventTriggers: clientmodel.EventTriggerList{ |
| 32 | &clientmodel.EventTrigger{ |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 33 | InterfaceDirection: direction, |
| 34 | ProcedureCode: procedureCode, |
| 35 | TypeOfMessage: typeOfMessage, |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 36 | }, |
| 37 | }, |
| 38 | } |
| 39 | |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 40 | var policyParams = clientmodel.PolicyParams{ |
| 41 | Meid: &meid, |
| 42 | RANFunctionID: &funId, |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 43 | ClientEndpoint: &clientEndpoint, |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 44 | EventTriggers: clientmodel.EventTriggerList{ |
| 45 | &clientmodel.EventTrigger{ |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 46 | InterfaceDirection: direction, |
| 47 | ProcedureCode: procedureCode, |
| 48 | TypeOfMessage: typeOfMessage, |
| 49 | }, |
| 50 | }, |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 51 | PolicyActionDefinitions: &clientmodel.PolicyActionDefinition{}, |
| 52 | } |
| 53 | |
| 54 | func 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 Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Mohamed Abukar | b6341a5 | 2020-03-23 08:55:05 +0200 | [diff] [blame] | 70 | func subscriptionHandler(stype models.SubscriptionType, params interface{}) (*models.SubscriptionResponse, error) { |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 71 | switch stype { |
| 72 | case models.SubscriptionTypeReport: |
| 73 | p := params.(*models.ReportParams) |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 74 | 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 Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 80 | case models.SubscriptionTypePolicy: |
| 81 | p := params.(*models.PolicyParams) |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 82 | assert.Equal(suite, clientEndpoint, *p.ClientEndpoint) |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 85 | // 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 Abukar | b6341a5 | 2020-03-23 08:55:05 +0200 | [diff] [blame] | 92 | return &models.SubscriptionResponse{ |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 93 | SubscriptionID: &subscriptionId, |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 94 | }, nil |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 97 | func queryHandler() (models.SubscriptionList, error) { |
| 98 | resp := models.SubscriptionList{ |
| 99 | &models.SubscriptionData{ |
| 100 | SubscriptionID: 11, |
Mohamed Abukar | 8dcedb4 | 2020-03-10 13:20:56 +0200 | [diff] [blame] | 101 | Meid: "Test-Gnb", |
| 102 | Endpoint: []string{"127.0.0.1:4056"}, |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 103 | }, |
| 104 | } |
| 105 | |
| 106 | return resp, nil |
| 107 | } |
| 108 | |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 109 | func deleteHandler(ep string) error { |
| 110 | assert.Equal(suite, clientEndpoint, ep) |
| 111 | |
| 112 | return nil |
| 113 | } |
| 114 | |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 115 | func TestSetup(t *testing.T) { |
| 116 | suite = t |
| 117 | |
| 118 | // Start the server to simulate SubManager |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 119 | go Subscription.Listen(subscriptionHandler, queryHandler, deleteHandler) |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 120 | time.Sleep(time.Duration(2) * time.Second) |
| 121 | } |
| 122 | |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 123 | func 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 Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 132 | func TestSubscriptionReportHandling(t *testing.T) { |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 133 | 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 Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 138 | |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 139 | _, err := Subscription.SubscribeReport(&reportParams) |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 140 | assert.Equal(t, err, nil) |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | func TestSubscriptionPolicytHandling(t *testing.T) { |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 144 | 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 Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 149 | |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 150 | _, err := Subscription.SubscribePolicy(&policyParams) |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 151 | assert.Equal(t, err, nil) |
Mohamed Abukar | 8dcedb4 | 2020-03-10 13:20:56 +0200 | [diff] [blame] | 152 | } |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 153 | |
| 154 | func TestSubscriptionDeleteHandling(t *testing.T) { |
| 155 | err := Subscription.UnSubscribe(clientEndpoint) |
| 156 | |
| 157 | assert.Equal(t, err, nil) |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 158 | } |