3 from http.server import BaseHTTPRequestHandler
10 pnf_not_found = '[{}]'
12 class AAISetup(BaseHTTPRequestHandler):
18 if re.search('/set_pnfs', self.path):
19 content_length = int(self.headers['Content-Length'])
20 pnfs = self.rfile.read(content_length)
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)
37 if re.search('/reset', self.path):
38 httpServerLib.header_200_and_json(self)
43 class AAIHandler(BaseHTTPRequestHandler):
49 pnf_path = '/aai/v14/network/pnfs/pnf/'
50 service_path = '/aai/v14/nodes/service-instances/service-instance/'
52 if re.search(pnf_path, self.path):
54 python_pnfs = json.loads(pnfs)
55 except AttributeError:
57 for pnf_instance in python_pnfs:
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
65 elif re.search(service_path, self.path):
67 python_services = json.loads(services)
68 except AttributeError:
70 for service_instance in python_services:
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
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)
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)
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)
107 if __name__ == '__main__':