baa81651ca96e708e78e135b50dad50591f805bd
[integration/csit.git] / tests / dcaegen2 / prh-testcases / resources / simulator / AAI.py
1 import logging
2 import json
3 import sys
4 import re
5 import time
6 from http.server import BaseHTTPRequestHandler
7 import httpServerLib
8
9 ch = logging.StreamHandler(sys.stdout)
10 handlers = [ch]
11 logging.basicConfig(
12     level=logging.DEBUG,
13     format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
14     handlers=handlers
15 )
16
17 logger = logging.getLogger('AAI-simulator-logger')
18
19 pnf_name = 'Empty'
20 pnf_entry = {}
21
22
23 def _mark_response_as_http_ok(http_endpoint):
24     logger.info('Execution status 200')
25     httpServerLib.header_200_and_json(http_endpoint)
26
27
28 class AAISetup(BaseHTTPRequestHandler):
29
30     def do_PUT(self):
31         logger.info('AAI SIM Setup Put execution')
32         if re.search('/set_pnf$', self.path): # to avoid regex collisions '$' must be added
33             global pnf_name
34             content_length = self._get_content_length()
35             pnf_name = self.rfile.read(content_length).decode()
36             _mark_response_as_http_ok(self)
37
38         if re.search('/set_pnf_entry',self.path):
39             global pnf_entry
40             content_length = self._get_content_length()
41             pnf_entry = json.loads(self.rfile.read(content_length))
42             _mark_response_as_http_ok(self)
43
44         return
45
46     def do_POST(self):
47         logger.info('AAI SIM Setup Post execution')
48         if re.search('/reset', self.path):
49             global pnf_name
50             pnf_name = 'Empty'
51             _mark_response_as_http_ok(self)
52
53         return
54
55     def _get_content_length(self):
56         return int(self.headers['Content-Length'])
57
58
59 class AAIHandler(BaseHTTPRequestHandler):
60
61     def do_GET(self):
62         logger.info('AAI SIM Get execution')
63         full_request_path = '/aai/v12/network/pnfs/pnf/' + pnf_name
64         if re.search(full_request_path, self.path):
65             _mark_response_as_http_ok(self)
66             body = json.dumps(pnf_entry)
67             logger.info('AAI SIM Get json prepared')
68             self.wfile.write(body.encode())
69         return
70
71
72     def do_PATCH(self):
73         logger.info('AAI SIM Patch execution')
74         pnfs_name = '/aai/v12/network/pnfs/pnf/' + pnf_name
75         if re.search('wrong_aai_record', self.path):
76             self.send_response(400)
77             logger.info('Execution status 400')
78             self.end_headers()
79         elif re.search(pnfs_name, self.path):
80             self.send_response(200)
81             logger.info('Execution status 200')
82             self.end_headers()
83             
84         return
85
86
87 def _main_(handler_class=AAIHandler, protocol="HTTP/1.0"):
88     handler_class.protocol_version = protocol
89     httpServerLib.start_http_endpoint(3333, AAIHandler)
90     httpServerLib.start_https_endpoint(3334, AAIHandler, keyfile="certs/org.onap.aai.key", certfile="certs/aai_aai.onap.org.cer", ca_certs="certs/ca_local_0.cer")
91     httpServerLib.start_http_endpoint(3335, AAISetup)
92     while 1:
93         time.sleep(10)
94
95
96 if __name__ == '__main__':
97     _main_()