75294c66a5e57c2413f1b054f13fbc45c8ec47c5
[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 import json
23 import os
24 from rmr import rmr
25
26 PORT = os.environ.get("TEST_RCV_PORT", "4560")
27 DELAY = int(os.environ.get("TEST_RCV_SEC_DELAY", 0))
28 HANDLER_ID = os.environ.get("HANDLER_ID", "test_receiver")
29
30 # TODO: should these be made constants?
31 mrc = rmr.rmr_init(PORT.encode("utf-8"), rmr.RMR_MAX_RCV_BYTES, 0x00)
32
33 while rmr.rmr_ready(mrc) == 0:
34     time.sleep(1)
35     print("not yet ready")
36
37 print("listening ON {}".format(PORT))
38 while True:
39     sbuf = rmr.rmr_alloc_msg(mrc, 4096)
40     sbuf = rmr.rmr_torcv_msg(mrc, sbuf, 1000)
41     summary = rmr.message_summary(sbuf)
42     if summary["message state"] == 12 and summary["message status"] == "RMR_ERR_TIMEOUT":
43         # print("Nothing received yet")
44         time.sleep(1)
45     else:
46         print("Message received!: {}".format(summary))
47
48         received_payload = json.loads(summary["payload"])
49
50         op = received_payload["operation"]
51         send_payload_status = "ERROR"
52         if op == "CREATE":
53             send_payload_status = "OK"
54         elif op == "DELETE":
55             send_payload_status = "DELETED"
56
57         payload = {
58             "policy_type_id": received_payload["policy_type_id"],
59             "policy_instance_id": received_payload["policy_instance_id"],
60             "handler_id": HANDLER_ID,
61             "status": send_payload_status,
62         }
63
64         val = json.dumps(payload).encode("utf-8")
65         rmr.set_payload_and_length(val, sbuf)
66         sbuf.contents.mtype = 21024
67         print("Pre reply summary: {}".format(rmr.message_summary(sbuf)))
68         time.sleep(DELAY)
69
70         # try up to 5 times to send back the ack
71         for _ in range(5):
72             sbuf = rmr.rmr_rts_msg(mrc, sbuf)
73             post_reply_summary = rmr.message_summary(sbuf)
74             print("Post reply summary: {}".format(post_reply_summary))
75             if post_reply_summary["message state"] == 10 and post_reply_summary["message status"] == "RMR_ERR_RETRY":
76                 time.sleep(1)
77             else:
78                 break