blob: 6e74c395a35598499c8efada583e2619b8a75b79 [file] [log] [blame]
pceicicd74b83332021-04-20 19:02:13 +00001# ==================================================================================
2# Copyright (c) 2020 China Mobile Technology (USA) Inc. Intellectual Property.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15# ==================================================================================
16import json
17import time
18from contextlib import suppress
19from lp import main, sdl
pceicicd63df1922021-07-03 23:01:13 +000020from lp.exceptions import UENotFound, CellNotFound
21from ricxappframe.xapp_frame import Xapp, RMRXapp
22import pytest
23import pdb
24
25@pytest.fixture
26def ue_metrics():
27 return {
28 "UEID": "9876543",
29 "ServingCellID": "460-7-797-57315",
30 "MeasTimestampUEPDCPBytes": "2021-06-20 13:58:29.220",
31 "MeasPeriodUEPDCPBytes": 20,
32 "UEPDCPBytesDL": 250000,
33 "UEPDCPBytesUL": 100000,
34 "MeasTimestampUEPRBUsage": "2021-06-20 13:58:29.220",
35 "MeasPeriodUEPRBUsage": 20,
36 "UEPRBUsageDL": 10,
37 "UEPRBUsageUL": 30,
38 "MeasTimestampRF": "2021-06-20 13:58:29.210",
39 "MeasPeriodRF": 40,
40 "ServingCellRF": {"RSRP": -115, "RSRQ": -16, "RSSINR": -5},
41 "NeighborCellRF": [
42 {"CID": "460-7-797-57314", "CellRF": {"RSRP": -90, "RSRQ": -13, "RSSINR": -2.5}},
43 {"CID": "460-7-797-57316", "CellRF": {"RSRP": -140, "RSRQ": -17, "RSSINR": -6}},
44 ],
45 "FAKE_BAD_DATA_TEST": "THIS SHOULD GET DELETED",
46 }
47
48
49@pytest.fixture
50def ue_metrics_with_bad_cell():
51 return {
52 "UEID": "3456789",
53 "ServingCellID": "460-7-797-57315",
54 "MeasTimestampUEPDCPBytes": "2021-06-20 13:58:29.220",
55 "MeasPeriodUEPDCPBytes": 20,
56 "UEPDCPBytesDL": 250000,
57 "UEPDCPBytesUL": 100000,
58 "MeasTimestampUEPRBUsage": "2021-06-20 13:58:29.220",
59 "MeasPeriodUEPRBUsage": 20,
60 "UEPRBUsageDL": 10,
61 "UEPRBUsageUL": 30,
62 "MeasTimestampRF": "2021-06-20 13:58:29.210",
63 "MeasPeriodRF": 40,
64 "ServingCellRF": {"RSRP": -115, "RSRQ": -16, "RSSINR": -5},
65 "NeighborCellRF": [
66 {"CID": "460-7-797-57314", "CellRF": {"RSRP": -90, "RSRQ": -13, "RSSINR": -2.5}},
67 {"CID": "CANTTOUCHTHIS", "CellRF": {"RSRP": -140, "RSRQ": -17, "RSSINR": -6}},
68 ],
69 }
70
71
72@pytest.fixture
73def cell_metrics_1():
74 return {
75 "CellID": "460-7-797-57314",
76 "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220",
77 "MeasPeriodPDCPBytes": 20,
78 "PDCPBytesDL": 2000000,
79 "PDCPBytesUL": 1200000,
80 "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220",
81 "MeasPeriodAvailPRB": 20,
82 "AvailPRBDL": 30,
83 "AvailPRBUL": 50,
84 }
85
86
87@pytest.fixture
88def cell_metrics_2():
89 return {
90 "CellID": "460-7-797-57315",
91 "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220",
92 "MeasPeriodPDCPBytes": 20,
93 "PDCPBytesDL": 800000,
94 "PDCPBytesUL": 400000,
95 "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220",
96 "MeasPeriodAvailPRB": 20,
97 "AvailPRBDL": 30,
98 "AvailPRBUL": 45,
99 "FAKE_BAD_DATA_TEST": "THIS SHOULD GET DELETED",
100 }
101
102
103@pytest.fixture
104def cell_metrics_3():
105 return {
106 "CellID": "460-7-797-57316",
107 "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220",
108 "MeasPeriodPDCPBytes": 20,
109 "PDCPBytesDL": 1900000,
110 "PDCPBytesUL": 1000000,
111 "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220",
112 "MeasPeriodAvailPRB": 20,
113 "AvailPRBDL": 60,
114 "AvailPRBUL": 80,
115 }
116
117
118@pytest.fixture
119def good_cell():
120 return {
121 "PredictionUE": "9876543",
122 "UEMeasurements": {
123 "ServingCellID": "460-7-797-57315",
124 "MeasTimestampUEPDCPBytes": "2021-06-20 13:58:29.220",
125 "MeasPeriodUEPDCPBytes": 20,
126 "UEPDCPBytesDL": 250000,
127 "UEPDCPBytesUL": 100000,
128 "MeasTimestampUEPRBUsage": "2021-06-20 13:58:29.220",
129 "MeasPeriodUEPRBUsage": 20,
130 "UEPRBUsageDL": 10,
131 "UEPRBUsageUL": 30,
132 },
133 "CellMeasurements": [
134 {
135 "CellID": "460-7-797-57314",
136 "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220",
137 "MeasPeriodPDCPBytes": 20,
138 "PDCPBytesDL": 2000000,
139 "PDCPBytesUL": 1200000,
140 "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220",
141 "MeasPeriodAvailPRB": 20,
142 "AvailPRBDL": 30,
143 "AvailPRBUL": 50,
144 "MeasTimestampRF": "2021-06-20 13:58:29.210",
145 "MeasPeriodRF": 40,
146 "RFMeasurements": {"RSRP": -90, "RSRQ": -13, "RSSINR": -2.5},
147 },
148 {
149 "CellID": "460-7-797-57316",
150 "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220",
151 "MeasPeriodPDCPBytes": 20,
152 "PDCPBytesDL": 1900000,
153 "PDCPBytesUL": 1000000,
154 "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220",
155 "MeasPeriodAvailPRB": 20,
156 "AvailPRBDL": 60,
157 "AvailPRBUL": 80,
158 "MeasTimestampRF": "2021-06-20 13:58:29.210",
159 "MeasPeriodRF": 40,
160 "RFMeasurements": {"RSRP": -140, "RSRQ": -17, "RSSINR": -6},
161 },
162 {
163 "CellID": "460-7-797-57315",
164 "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220",
165 "MeasPeriodPDCPBytes": 20,
166 "PDCPBytesDL": 800000,
167 "PDCPBytesUL": 400000,
168 "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220",
169 "MeasPeriodAvailPRB": 20,
170 "AvailPRBDL": 30,
171 "AvailPRBUL": 45,
172 "MeasTimestampRF": "2021-06-20 13:58:29.210",
173 "MeasPeriodRF": 40,
174 "RFMeasurements": {"RSRP": -115, "RSRQ": -16, "RSSINR": -5},
175 },
176 ],
177 }
178
179
180@pytest.fixture
181def bad_cell():
182 return {
183 "PredictionUE": "3456789",
184 "UEMeasurements": {
185 "ServingCellID": "460-7-797-57315",
186 "MeasTimestampUEPDCPBytes": "2021-06-20 13:58:29.220",
187 "MeasPeriodUEPDCPBytes": 20,
188 "UEPDCPBytesDL": 250000,
189 "UEPDCPBytesUL": 100000,
190 "MeasTimestampUEPRBUsage": "2021-06-20 13:58:29.220",
191 "MeasPeriodUEPRBUsage": 20,
192 "UEPRBUsageDL": 10,
193 "UEPRBUsageUL": 30,
194 },
195 "CellMeasurements": [
196 {
197 "CellID": "460-7-797-57314",
198 "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220",
199 "MeasPeriodPDCPBytes": 20,
200 "PDCPBytesDL": 2000000,
201 "PDCPBytesUL": 1200000,
202 "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220",
203 "MeasPeriodAvailPRB": 20,
204 "AvailPRBDL": 30,
205 "AvailPRBUL": 50,
206 "MeasTimestampRF": "2021-06-20 13:58:29.210",
207 "MeasPeriodRF": 40,
208 "RFMeasurements": {"RSRP": -90, "RSRQ": -13, "RSSINR": -2.5},
209 },
210 {
211 "CellID": "460-7-797-57315",
212 "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220",
213 "MeasPeriodPDCPBytes": 20,
214 "PDCPBytesDL": 800000,
215 "PDCPBytesUL": 400000,
216 "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220",
217 "MeasPeriodAvailPRB": 20,
218 "AvailPRBDL": 30,
219 "AvailPRBUL": 45,
220 "MeasTimestampRF": "2021-06-20 13:58:29.210",
221 "MeasPeriodRF": 40,
222 "RFMeasurements": {"RSRP": -115, "RSRQ": -16, "RSSINR": -5},
223 },
224 ],
225 }
pceicicd74b83332021-04-20 19:02:13 +0000226
227mock_lp_xapp = None
228# tox.ini sets env var to this value
229config_file_path = "/tmp/config.json"
230
231def init_config_file():
232 with open(config_file_path, "w") as file:
233 file.write('{ "version_int" : 1 }')
234
235
236def write_config_file():
237 # generate an inotify/config event
238 with open(config_file_path, "w") as file:
239 file.write('{ "version_int" : 2 }')
240
241
pceicicd63df1922021-07-03 23:01:13 +0000242def test_init_xapp(monkeypatch, ue_metrics, cell_metrics_1, cell_metrics_2, cell_metrics_3, ue_metrics_with_bad_cell, good_cell):
243
244 _original_post_init = main.post_init
245
246 def fake_post_init(self):
247 _original_post_init(self)
248 self.sdl_set(sdl.UE_NS, "9876543", json.dumps(ue_metrics).encode(), usemsgpack=False)
249 self.sdl_set(sdl.UE_NS, "3456789", json.dumps(ue_metrics_with_bad_cell).encode(), usemsgpack=False)
250 self.sdl_set(sdl.CELL_NS, "460-7-797-57314", json.dumps(cell_metrics_1).encode(), usemsgpack=False)
251 self.sdl_set(sdl.CELL_NS, "460-7-797-57315", json.dumps(cell_metrics_2).encode(), usemsgpack=False)
252 self.sdl_set(sdl.CELL_NS, "460-7-797-57316", json.dumps(cell_metrics_3).encode(), usemsgpack=False)
253
254 expected=sdl.get_uedata(self, "9876543")
255 assert expected==good_cell
James Li77b8beb2021-12-13 23:20:27 +0000256 expected=main.run_prediction(self)
257 assert expected=="Normal"
pceicicdcb7ba952021-09-16 03:40:38 +0000258 expected=main.predict(self, expected)
259 assert expected=="Normal"
pceicicd63df1922021-07-03 23:01:13 +0000260 expected=sdl.get_uedata(self, "3456789")
261 try:
262 sdl.get_uedata(self, "1234567")
263 except UENotFound:
264 self.logger.warning("UE doesn't exist!")
265
266
267 # patch
268 monkeypatch.setattr("lp.main.post_init", fake_post_init)
269
pceicicd74b83332021-04-20 19:02:13 +0000270 # establish config
271 init_config_file()
272
James Liaf0884b2021-10-22 16:55:35 -0700273 # test db connection
274 main.connectdb(thread=True)
275
pceicicd63df1922021-07-03 23:01:13 +0000276 # start lp
James Li77b8beb2021-12-13 23:20:27 +0000277 #main.start(thread=False)
pceicicd63df1922021-07-03 23:01:13 +0000278
pceicicd74b83332021-04-20 19:02:13 +0000279 # wait a bit then update config
280 time.sleep(1)
281 write_config_file()
282
pceicicd63df1922021-07-03 23:01:13 +0000283def test_mock_xapp():
284 # define a mock traffic steering xapp
285 def mock_ts_entry(self):
286
287 # make sure a bad steering request doesn't blow up in lp
288 val = "".encode() # send empty string encoded message
289 self.rmr_send(val, 30000)
290 val = "just a string".encode() # not json
291 self.rmr_send(val, 30000)
292 val = json.dumps({"key": "value"}).encode() # json but missing UEPredictionSet
293 self.rmr_send(val, 30000)
294
295 # valid request body but missing cell id
296 val = json.dumps({"UEPredictionSet": ["NOTVALIDUEs"]}).encode()
297 self.rmr_send(val, 30000)
298
299 # good traffic steering request
300 val = json.dumps({"UEPredictionSet": ["9876543", "3456789"]}).encode()
301 self.rmr_send(val, 30000)
302
303 # should trigger the default handler and do nothing
304 val = json.dumps({"send other message types": 1}).encode()
305 self.rmr_send(val, 60001)
306
307 global mock_ts_xapp
308 mock_ts_xapp = Xapp(entrypoint=mock_ts_entry, rmr_port=4564, use_fake_sdl=True)
309 mock_ts_xapp.run() # this will return since entry isn't a loop
310
311def teardown_module():
312 """
313 this is like a "finally"; the name of this function is pytest magic
314 safer to put down here since certain failures above can lead to pytest never returning
315 for example if an exception gets raised before stop is called in any test function above,
316 pytest will hang forever
317 """
318 with suppress(Exception):
319 mock_ts_xapp.stop()
320 with suppress(Exception):
321 mock_lp_xapp.stop()
322 with suppress(Exception):
323 main.stop()