91a93de5c1c73efdd75f759d7aaab66057b4f526
[integration/csit.git] / tests / dcaegen2 / prh-testcases / resources / simulator / AAI.py
1 import logging
2 import sys
3 import re
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('AAI-simulator-logger')
17
18 pnfs = 'Empty'
19 pnf_entry = 'Empty'
20
21 class AAISetup(BaseHTTPRequestHandler):
22
23     def do_PUT(self):
24         logger.info('AAI SIM Setup Put execution')
25         if re.search('/set_pnf', self.path):
26             global pnfs
27             content_length = int(self.headers['Content-Length'])
28             pnfs = self.rfile.read(content_length)
29             logger.info('Execution status 200')
30             httpServerLib.header_200_and_json(self)
31
32         if re.search('/set_pnf_entry',self.path):
33             logger.info('Execution status 200')
34             httpServerLib.header_200_and_json(self)
35
36         return
37
38     def do_POST(self):
39         logger.info('AAI SIM Setup Post execution')
40         if re.search('/reset', self.path):
41             global pnfs
42             pnfs = 'Empty'
43             logger.info('Execution status 200')
44             httpServerLib.header_200_and_json(self)
45
46         return
47
48
49 class AAIHandler(BaseHTTPRequestHandler):
50
51     def do_PATCH(self):
52         logger.info('AAI SIM Patch execution')
53         pnfs_name = '/aai/v12/network/pnfs/pnf/' + pnfs.decode()
54         if re.search('wrong_aai_record', self.path):
55             self.send_response(400)
56             logger.info('Execution status 400')
57             self.end_headers()
58         elif re.search(pnfs_name, self.path):
59             self.send_response(200)
60             logger.info('Execution status 200')
61             self.end_headers()
62             
63         return
64
65
66 def _main_(handler_class=AAIHandler, protocol="HTTP/1.0"):
67     handler_class.protocol_version = protocol
68     httpServerLib.start_http_endpoint(3333, AAIHandler)
69     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")
70     httpServerLib.start_http_endpoint(3335, AAISetup)
71     while 1:
72         time.sleep(10)
73
74
75 if __name__ == '__main__':
76     _main_()