Merge "Update certificates in PM Mapper CSIT's"
[integration/csit.git] / tests / dcaegen2 / bbs-testcases / resources / simulator / AAI.py
1 import re
2 import time
3 from http.server import BaseHTTPRequestHandler
4 import httpServerLib
5
6 import json
7
8 pnfs = []
9 services = []
10 pnf_not_found = '[{}]'
11
12 class AAISetup(BaseHTTPRequestHandler):
13
14     def do_PUT(self):
15         global pnfs
16         global services
17         global pnf_not_found
18         if re.search('/set_pnfs', self.path):
19             content_length = int(self.headers['Content-Length'])
20             pnfs = self.rfile.read(content_length)
21             pnfs = pnfs.decode()
22             httpServerLib.header_200_and_json(self)
23         elif re.search('/set_services', self.path):
24             content_length = int(self.headers['Content-Length'])
25             services = self.rfile.read(content_length)
26             services = services.decode()
27             httpServerLib.header_200_and_json(self)
28         elif re.search('/set_pnf_not_found', self.path):
29             content_length = int(self.headers['Content-Length'])
30             pnf_not_found = self.rfile.read(content_length)
31             pnf_not_found = pnf_not_found.decode()
32             httpServerLib.header_200_and_json(self)
33
34         return
35
36     def do_POST(self):
37         if re.search('/reset', self.path):
38             httpServerLib.header_200_and_json(self)
39
40         return
41
42
43 class AAIHandler(BaseHTTPRequestHandler):
44
45     def do_GET(self):
46         global pnfs
47         global services
48         global pnf_not_found
49         pnf_path = '/aai/v14/network/pnfs/pnf/'
50         service_path = '/aai/v14/nodes/service-instances/service-instance/'
51         found_resource = None
52         if re.search(pnf_path, self.path):
53             try:
54                 python_pnfs = json.loads(pnfs)
55             except AttributeError:
56                 python_pnfs = []
57             for pnf_instance in python_pnfs:
58                 try:
59                     pnf_name = pnf_path + pnf_instance.get("pnf-name")
60                 except AttributeError:
61                     pnf_name = "PNF not found"
62                 if re.search(pnf_name, self.path):
63                     found_resource = pnf_instance
64                     break
65         elif re.search(service_path, self.path):
66             try:
67                 python_services = json.loads(services)
68             except AttributeError:
69                 python_services = []
70             for service_instance in python_services:
71                 try:
72                     service_name = service_path + service_instance.get("service-instance-id")
73                 except AttributeError:
74                     pnf_name = "Service not found"
75                 if re.search(service_name, self.path):
76                     found_resource = service_instance
77                     break
78
79         if found_resource is not None:
80             # Prepare the response for DMaaP (byte encoded JSON Object)
81             found_resource = json.dumps(found_resource)
82             found_resource = found_resource.encode()
83             httpServerLib.header_200_and_json(self)
84             self.wfile.write(found_resource)
85         else:
86             # Send a 404 message for not found
87             pnf_resp = pnf_not_found
88             # We have to replace the request line data
89             err_pnf_template = "nodes/pnfs/pnf/Wrong-PNF-Name"
90             pnf_resp = pnf_resp.replace(err_pnf_template, self.path)
91             pnf_resp = pnf_resp.encode()
92             httpServerLib.header_404_and_json(self)
93             self.wfile.write(pnf_resp)
94
95             
96         return
97
98 def _main_(handler_class=AAIHandler, protocol="HTTP/1.0"):
99     handler_class.protocol_version = protocol
100     httpServerLib.start_http_endpoint(3333, AAIHandler)
101     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")
102     httpServerLib.start_http_endpoint(3335, AAISetup)
103     while 1:
104         time.sleep(10)
105
106
107 if __name__ == '__main__':
108     _main_()