blob: 3706f302af93f34bd66130dc56728585e0eaae79 [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
naman.gupta26506752021-09-03 13:45:45 +053020
21from ricxappframe.util.constants import Constants
22from ricxappframe.xapp_frame import _BaseXapp, Xapp, RMRXapp
yc999.jang750eb5b2021-06-10 19:04:18 +090023from ricxappframe.constants import sdl_namespaces
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040024
25rmr_xapp = None
Tommy Carpenter09894e32020-04-02 19:45:19 -040026rmr_xapp_health = None
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040027gen_xapp = None
yc999.jang750eb5b2021-06-10 19:04:18 +090028rnib_xapp = None
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040029
30
Tommy Carpenter09894e32020-04-02 19:45:19 -040031def test_rmr_init():
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040032
33 # test variables
Tommy Carpenter09894e32020-04-02 19:45:19 -040034 def_pay = None
35 sixty_pay = None
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040036
37 # create rmr app
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040038
39 def default_handler(self, summary, sbuf):
Tommy Carpenter09894e32020-04-02 19:45:19 -040040 nonlocal def_pay
41 def_pay = json.loads(summary["payload"])
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040042 self.rmr_free(sbuf)
43
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040044 def sixtythou_handler(self, summary, sbuf):
Tommy Carpenter09894e32020-04-02 19:45:19 -040045 nonlocal sixty_pay
46 sixty_pay = json.loads(summary["payload"])
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040047 self.rmr_free(sbuf)
48
Tommy Carpenter09894e32020-04-02 19:45:19 -040049 global rmr_xapp
50 rmr_xapp = RMRXapp(default_handler, rmr_port=4564, use_fake_sdl=True)
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040051 rmr_xapp.register_callback(sixtythou_handler, 60000)
Tommy Carpenter1c9ce6b2020-03-13 09:36:36 -040052 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 -040053
54 time.sleep(1)
55
Tommy Carpenter09894e32020-04-02 19:45:19 -040056 # create a general xapp that will demonstrate some SDL functionality and launch some requests against the rmr xapp
57
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040058 def entry(self):
59
Tommy Carpenter09894e32020-04-02 19:45:19 -040060 time.sleep(1)
61
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040062 self.sdl_set("testns", "mykey", 6)
63 assert self.sdl_get("testns", "mykey") == 6
64 assert self.sdl_find_and_get("testns", "myk") == {"mykey": 6}
65 assert self.healthcheck()
66
67 val = json.dumps({"test send 60000": 1}).encode()
68 self.rmr_send(val, 60000)
69
70 val = json.dumps({"test send 60001": 2}).encode()
71 self.rmr_send(val, 60001)
72
Lott, Christopher (cl778h)e87ea192020-06-16 16:12:26 -040073 self.sdl_delete("testns", "bogus")
74
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040075 global gen_xapp
Tommy Carpenter09894e32020-04-02 19:45:19 -040076 gen_xapp = Xapp(entrypoint=entry, use_fake_sdl=True)
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -040077 gen_xapp.run()
78
79 time.sleep(1)
80
Tommy Carpenter09894e32020-04-02 19:45:19 -040081 assert def_pay == {"test send 60001": 2}
82 assert sixty_pay == {"test send 60000": 1}
83
84
85def test_rmr_healthcheck():
86 # thanos uses the rmr xapp to healthcheck the rmr xapp
87
88 # test variables
89 health_pay = None
90
91 def post_init(self):
naman.gupta26506752021-09-03 13:45:45 +053092 self.rmr_send(b"", Constants.RIC_HEALTH_CHECK_REQ)
Tommy Carpenter09894e32020-04-02 19:45:19 -040093
94 def default_handler(self, summary, sbuf):
95 pass
96
97 global rmr_xapp_health
98 rmr_xapp_health = RMRXapp(default_handler, post_init=post_init, rmr_port=4666, use_fake_sdl=True)
99
100 def health_handler(self, summary, sbuf):
101 nonlocal health_pay
102 health_pay = summary["payload"]
103 self.rmr_free(sbuf)
104
naman.gupta26506752021-09-03 13:45:45 +0530105 rmr_xapp_health.register_callback(health_handler, Constants.RIC_HEALTH_CHECK_RESP)
Tommy Carpenter09894e32020-04-02 19:45:19 -0400106 rmr_xapp_health.run(thread=True) # in unit tests we need to thread here or else execution is not returned!
107
108 time.sleep(1)
109
110 assert health_pay == b"OK\n"
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -0400111
112
yc999.jang750eb5b2021-06-10 19:04:18 +0900113def test_get_list_nodeb(rnib_information):
114 global rnib_xapp
115 rnib_xapp = _BaseXapp(rmr_port=4777, rmr_wait_for_ready=False, use_fake_sdl=True)
116
117 # Test there is no rnib information.
118 gnb_list = rnib_xapp.get_list_gnb_ids()
119 enb_list = rnib_xapp.get_list_enb_ids()
120 assert len(gnb_list) == 0
121 assert len(enb_list) == 0
122
123 # Add rnib information directly.
124 for rnib in rnib_information:
125 rnib_xapp.sdl.add_member(sdl_namespaces.E2_MANAGER, "ENB", rnib, usemsgpack=False)
126 rnib_xapp.sdl.add_member(sdl_namespaces.E2_MANAGER, "GNB", rnib, usemsgpack=False)
127
128 gnb_list = rnib_xapp.get_list_gnb_ids()
129 assert len(gnb_list) == len(rnib_information)
130 for gnb in gnb_list:
131 assert gnb.SerializeToString() in rnib_information
132
133 enb_list = rnib_xapp.get_list_enb_ids()
134 assert len(enb_list) == len(rnib_information)
135 for enb in enb_list:
136 assert enb.SerializeToString() in rnib_information
137
138
Tommy Carpenterf9cd5cc2020-03-09 13:46:37 -0400139def teardown_module():
140 """
141 this is like a "finally"; the name of this function is pytest magic
142 safer to put down here since certain failures above can lead to pytest never returning
143 for example if an exception gets raised before stop is called in any test function above, pytest will hang forever
144 """
145 with suppress(Exception):
146 gen_xapp.stop()
147 with suppress(Exception):
148 rmr_xapp.stop()
Tommy Carpenter09894e32020-04-02 19:45:19 -0400149 with suppress(Exception):
150 rmr_xapp_health.stop()
yc999.jang750eb5b2021-06-10 19:04:18 +0900151 with suppress(Exception):
152 rnib_xapp.stop()