Add pnf_entry endpoints
[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 pnfs = '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):
33             global pnfs
34             content_length = self._get_content_length()
35             pnfs = self.rfile.read(content_length)
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 pnfs
50             pnfs = '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         if re.search('/get_pnf_entry', self.path):
64             _mark_response_as_http_ok(self)
65             body = json.dumps(pnf_entry)
66             self.wfile.write(body.encode())
67
68         return
69
70
71     def do_PATCH(self):
72         logger.info('AAI SIM Patch execution')
73         pnfs_name = '/aai/v12/network/pnfs/pnf/' + pnfs.decode()
74         if re.search('wrong_aai_record', self.path):
75             self.send_response(400)
76             logger.info('Execution status 400')
77             self.end_headers()
78         elif re.search(pnfs_name, self.path):
79             self.send_response(200)
80             logger.info('Execution status 200')
81             self.end_headers()
82             
83         return
84
85
86 def _main_(handler_class=AAIHandler, protocol="HTTP/1.0"):
87     handler_class.protocol_version = protocol
88     httpServerLib.start_http_endpoint(3333, AAIHandler)
89     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")
90     httpServerLib.start_http_endpoint(3335, AAISetup)
91     while 1:
92         time.sleep(10)
93
94
95 if __name__ == '__main__':
96     _main_()