Use "builder image" more appropriately
[ric-plt/a1.git] / integration_tests / receiver.py
1 # ==================================================================================
2 #       Copyright (c) 2019 Nokia
3 #       Copyright (c) 2018-2019 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 # ==================================================================================
17 """
18 Test receiver
19 """
20
21 import time
22 from rmr import rmr
23 import json
24 import os
25
26 PORT = os.environ.get("TEST_RCV_PORT", "4560")
27 RETURN_MINT = int(os.environ.get("TEST_RCV_RETURN_MINT", 20001))
28 DELAY = int(os.environ.get("TEST_RCV_SEC_DELAY", 0))
29 PAYLOAD_RETURNED = json.loads(
30     os.environ.get("TEST_RCV_RETURN_PAYLOAD", '{"ACK_FROM": "ADMISSION_CONTROL", "status": "SUCCESS"}')
31 )
32
33 # TODO: should these be made constants?
34 mrc = rmr.rmr_init(PORT.encode("utf-8"), rmr.RMR_MAX_RCV_BYTES, 0x00)
35
36 while rmr.rmr_ready(mrc) == 0:
37     time.sleep(1)
38     print("not yet ready")
39
40 print("listening")
41 sbuf = None
42 while True:
43     sbuf = rmr.rmr_torcv_msg(mrc, sbuf, 1000)
44     summary = rmr.message_summary(sbuf)
45     if summary["message state"] == 12 and summary["message status"] == "RMR_ERR_TIMEOUT":
46         # print("Nothing received yet")
47         time.sleep(1)
48     else:
49         print("Message received!: {}".format(summary))
50
51         val = json.dumps(PAYLOAD_RETURNED).encode("utf-8")
52         rmr.set_payload_and_length(val, sbuf)
53         sbuf.contents.mtype = RETURN_MINT
54         print("Pre reply summary: {}".format(rmr.message_summary(sbuf)))
55         time.sleep(DELAY)
56         sbuf = rmr.rmr_rts_msg(mrc, sbuf)
57         print("Post reply summary: {}".format(rmr.message_summary(sbuf)))