6 from http.server import BaseHTTPRequestHandler
9 ch = logging.StreamHandler(sys.stdout)
13 format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
17 logger = logging.getLogger('AAI-simulator-logger')
23 def _mark_response_as_http_ok(http_endpoint):
24 logger.info('Execution status 200')
25 httpServerLib.header_200_and_json(http_endpoint)
28 class AAISetup(BaseHTTPRequestHandler):
31 logger.info('AAI SIM Setup Put execution')
32 if re.search('/set_pnf$', self.path): # to avoid regex collisions '$' must be added
34 content_length = self._get_content_length()
35 pnf_name = self.rfile.read(content_length).decode()
36 _mark_response_as_http_ok(self)
38 if re.search('/set_pnf_entry',self.path):
40 content_length = self._get_content_length()
41 pnf_entry = json.loads(self.rfile.read(content_length))
42 _mark_response_as_http_ok(self)
47 logger.info('AAI SIM Setup Post execution')
48 if re.search('/reset', self.path):
51 _mark_response_as_http_ok(self)
55 def _get_content_length(self):
56 return int(self.headers['Content-Length'])
59 class AAIHandler(BaseHTTPRequestHandler):
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())
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')
79 elif re.search(pnfs_name, self.path):
80 self.send_response(200)
81 logger.info('Execution status 200')
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)
96 if __name__ == '__main__':