da113e683ba1422a98ad69322131c065e1cf5a30
[integration/csit.git] / tests / dcaegen2 / prh-testcases / resources / simulator / DMaaP.py
1 import logging
2 import re
3 import sys
4 import time
5 from http.server import BaseHTTPRequestHandler
6 import httpServerLib
7
8 ch = logging.StreamHandler(sys.stdout)
9 handlers = [ch]
10 logging.basicConfig(
11     level=logging.DEBUG,
12     format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
13     handlers=handlers
14 )
15
16 logger = logging.getLogger('DMaaP-simulator-logger')
17
18 posted_event_from_prh = b'Empty'
19 received_event_to_get_method = b'Empty'
20
21
22 class DmaapSetup(BaseHTTPRequestHandler):
23
24     def do_PUT(self):
25         logger.info('DMaaP SIM Setup Put execution')
26         if re.search('/set_get_event', self.path):
27             global received_event_to_get_method
28             content_length = int(self.headers['Content-Length'])
29             received_event_to_get_method = self.rfile.read(content_length)
30             httpServerLib.header_200_and_json(self)
31
32         return
33
34     def do_GET(self):
35         logger.info('DMaaP SIM Setup Get execution')
36         if re.search('/events/pnfReady', self.path):
37             httpServerLib.header_200_and_json(self)
38             self.wfile.write(posted_event_from_prh)
39
40         return
41
42     def do_POST(self):
43         logger.info('DMaaP SIM Setup Post execution')
44         if re.search('/reset', self.path):
45             global posted_event_from_prh
46             global received_event_to_get_method
47             posted_event_from_prh = b'Empty'
48             received_event_to_get_method = b'Empty'
49             httpServerLib.header_200_and_json(self)
50
51         return
52
53
54 class DMaaPHandler(BaseHTTPRequestHandler):
55
56     def do_POST(self):
57         logger.info('DMaaP SIM Post execution')
58         if re.search('/events/unauthenticated.PNF_READY', self.path):
59             global posted_event_from_prh
60             content_length = int(self.headers['Content-Length'])
61             posted_event_from_prh = self.rfile.read(content_length)
62             httpServerLib.header_200_and_json(self)
63
64         return
65
66     def do_GET(self):
67         logger.info('DMaaP SIM Get execution')
68         if re.search('/events/unauthenticated.VES_PNFREG_OUTPUT/OpenDcae-c12/c12', self.path):
69             httpServerLib.header_200_and_json(self)
70             self.wfile.write(received_event_to_get_method)
71
72         return
73
74
75 def _main_(handler_class=DMaaPHandler, protocol="HTTP/1.0"):
76     handler_class.protocol_version = protocol
77     httpServerLib.start_http_endpoint(2222, DMaaPHandler)
78     httpServerLib.start_https_endpoint(2223, DMaaPHandler, keyfile="certs/org.onap.dmaap-bc.key", certfile="certs/dmaap_bc_topic_mgr_dmaap_bc.onap.org.cer", ca_certs="certs/ca_local_0.cer")
79     httpServerLib.start_http_endpoint(2224, DmaapSetup)
80     while 1:
81         time.sleep(10)
82
83
84 if __name__ == '__main__':
85     _main_()