blob: 6aa32dabefcd4bdc7d9922df24cf859cfea7a2b1 [file] [log] [blame]
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -04001# ==================================================================================
2# Copyright (c) 2020 Nokia
3# Copyright (c) 2020 AT&T Intellectual Property.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16# ==================================================================================
17import json
18import time
19from contextlib import suppress
yc999.jang750eb5b2021-06-10 19:04:18 +090020from ricxappframe.xapp_frame import _BaseXapp, Xapp, RMRXapp, RIC_HEALTH_CHECK_REQ, RIC_HEALTH_CHECK_RESP
21from ricxappframe.constants import sdl_namespaces
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040022
23rmr_xapp = None
Tommy Carpenter09894e32020-04-02 19:45:19 -040024rmr_xapp_health = None
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040025gen_xapp = None
yc999.jang750eb5b2021-06-10 19:04:18 +090026rnib_xapp = None
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040027
28
Tommy Carpenter09894e32020-04-02 19:45:19 -040029def test_rmr_init():
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040030
31 # test variables
Tommy Carpenter09894e32020-04-02 19:45:19 -040032 def_pay = None
33 sixty_pay = None
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040034
35 # create rmr app
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040036
37 def default_handler(self, summary, sbuf):
Tommy Carpenter09894e32020-04-02 19:45:19 -040038 nonlocal def_pay
39 def_pay = json.loads(summary["payload"])
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040040 self.rmr_free(sbuf)
41
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040042 def sixtythou_handler(self, summary, sbuf):
Tommy Carpenter09894e32020-04-02 19:45:19 -040043 nonlocal sixty_pay
44 sixty_pay = json.loads(summary["payload"])
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040045 self.rmr_free(sbuf)
46
Tommy Carpenter09894e32020-04-02 19:45:19 -040047 global rmr_xapp
48 rmr_xapp = RMRXapp(default_handler, rmr_port=4564, use_fake_sdl=True)
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040049 rmr_xapp.register_callback(sixtythou_handler, 60000)
Tommy Carpenter1c9ce6b2020-03-13 09:36:36 -040050 rmr_xapp.run(thread=True) # in unit tests we need to thread here or else execution is not returned!
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040051
52 time.sleep(1)
53
Tommy Carpenter09894e32020-04-02 19:45:19 -040054 # create a general xapp that will demonstrate some SDL functionality and launch some requests against the rmr xapp
55
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040056 def entry(self):
57
Tommy Carpenter09894e32020-04-02 19:45:19 -040058 time.sleep(1)
59
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040060 self.sdl_set("testns", "mykey", 6)
61 assert self.sdl_get("testns", "mykey") == 6
62 assert self.sdl_find_and_get("testns", "myk") == {"mykey": 6}
63 assert self.healthcheck()
64
65 val = json.dumps({"test send 60000": 1}).encode()
66 self.rmr_send(val, 60000)
67
68 val = json.dumps({"test send 60001": 2}).encode()
69 self.rmr_send(val, 60001)
70
Lott, Christopher (cl778h)e87ea192020-06-16 16:12:26 -040071 self.sdl_delete("testns", "bogus")
72
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040073 global gen_xapp
Tommy Carpenter09894e32020-04-02 19:45:19 -040074 gen_xapp = Xapp(entrypoint=entry, use_fake_sdl=True)
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040075 gen_xapp.run()
76
77 time.sleep(1)
78
Tommy Carpenter09894e32020-04-02 19:45:19 -040079 assert def_pay == {"test send 60001": 2}
80 assert sixty_pay == {"test send 60000": 1}
81
82
83def test_rmr_healthcheck():
84 # thanos uses the rmr xapp to healthcheck the rmr xapp
85
86 # test variables
87 health_pay = None
88
89 def post_init(self):
90 self.rmr_send(b"", RIC_HEALTH_CHECK_REQ)
91
92 def default_handler(self, summary, sbuf):
93 pass
94
95 global rmr_xapp_health
96 rmr_xapp_health = RMRXapp(default_handler, post_init=post_init, rmr_port=4666, use_fake_sdl=True)
97
98 def health_handler(self, summary, sbuf):
99 nonlocal health_pay
100 health_pay = summary["payload"]
101 self.rmr_free(sbuf)
102
103 rmr_xapp_health.register_callback(health_handler, RIC_HEALTH_CHECK_RESP)
104 rmr_xapp_health.run(thread=True) # in unit tests we need to thread here or else execution is not returned!
105
106 time.sleep(1)
107
108 assert health_pay == b"OK\n"
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -0400109
110
yc999.jang750eb5b2021-06-10 19:04:18 +0900111def test_get_list_nodeb(rnib_information):
112 global rnib_xapp
113 rnib_xapp = _BaseXapp(rmr_port=4777, rmr_wait_for_ready=False, use_fake_sdl=True)
114
115 # Test there is no rnib information.
116 gnb_list = rnib_xapp.get_list_gnb_ids()
117 enb_list = rnib_xapp.get_list_enb_ids()
118 assert len(gnb_list) == 0
119 assert len(enb_list) == 0
120
121 # Add rnib information directly.
122 for rnib in rnib_information:
123 rnib_xapp.sdl.add_member(sdl_namespaces.E2_MANAGER, "ENB", rnib, usemsgpack=False)
124 rnib_xapp.sdl.add_member(sdl_namespaces.E2_MANAGER, "GNB", rnib, usemsgpack=False)
125
126 gnb_list = rnib_xapp.get_list_gnb_ids()
127 assert len(gnb_list) == len(rnib_information)
128 for gnb in gnb_list:
129 assert gnb.SerializeToString() in rnib_information
130
131 enb_list = rnib_xapp.get_list_enb_ids()
132 assert len(enb_list) == len(rnib_information)
133 for enb in enb_list:
134 assert enb.SerializeToString() in rnib_information
135
136
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -0400137def teardown_module():
138 """
139 this is like a "finally"; the name of this function is pytest magic
140 safer to put down here since certain failures above can lead to pytest never returning
141 for example if an exception gets raised before stop is called in any test function above, pytest will hang forever
142 """
143 with suppress(Exception):
144 gen_xapp.stop()
145 with suppress(Exception):
146 rmr_xapp.stop()
Tommy Carpenter09894e32020-04-02 19:45:19 -0400147 with suppress(Exception):
148 rmr_xapp_health.stop()
yc999.jang750eb5b2021-06-10 19:04:18 +0900149 with suppress(Exception):
150 rnib_xapp.stop()