Merge "PRH simulators refactor"
diff --git a/tests/dcaegen2/prh-testcases/resources/simulator/AAI.py b/tests/dcaegen2/prh-testcases/resources/simulator/AAI.py
index baa8165..010c523 100644
--- a/tests/dcaegen2/prh-testcases/resources/simulator/AAI.py
+++ b/tests/dcaegen2/prh-testcases/resources/simulator/AAI.py
@@ -4,6 +4,7 @@
 import re
 import time
 from http.server import BaseHTTPRequestHandler
+from os.path import basename
 import httpServerLib
 
 ch = logging.StreamHandler(sys.stdout)
@@ -16,72 +17,125 @@
 
 logger = logging.getLogger('AAI-simulator-logger')
 
-pnf_name = 'Empty'
-pnf_entry = {}
+AAI_RESOURCE_NOT_FOUND = b'{}'
 
-
-def _mark_response_as_http_ok(http_endpoint):
-    logger.info('Execution status 200')
-    httpServerLib.header_200_and_json(http_endpoint)
-
+pnf_entries = {}
+patched_pnf = AAI_RESOURCE_NOT_FOUND
+created_logical_link = AAI_RESOURCE_NOT_FOUND
 
 class AAISetup(BaseHTTPRequestHandler):
 
+    def do_GET(self):
+        try:
+            if re.search('/setup/patched_pnf', self.path):
+                httpServerLib.set_response_200_ok(self, payload = patched_pnf)
+                logger.debug('AAISetup GET /setup/patched_pnf -> 200 OK')
+            elif re.search('/setup/created_logical_link', self.path):
+                httpServerLib.set_response_200_ok(self, payload = created_logical_link)
+                logger.debug('AAISetup GET /setup/created_logical_link -> 200 OK')
+            else:
+                httpServerLib.set_response_404_not_found(self)
+                logger.info('AAISetup GET ' + self.path + ' -> 404 Not found')
+        except Exception as e:
+            logger.error(e)
+            httpServerLib.set_response_500_server_error(self)
+
     def do_PUT(self):
-        logger.info('AAI SIM Setup Put execution')
-        if re.search('/set_pnf$', self.path): # to avoid regex collisions '$' must be added
-            global pnf_name
-            content_length = self._get_content_length()
-            pnf_name = self.rfile.read(content_length).decode()
-            _mark_response_as_http_ok(self)
+        try:
+            if re.search('/setup/add_pnf_entry', self.path):
+                pnf_entry = httpServerLib.get_payload(self)
+                pnf_name = json.loads(pnf_entry).get("pnf-name")
+                if pnf_name == None:
+                    raise Exception("Invalid PNF entry, could not extract `pnf-name`")
 
-        if re.search('/set_pnf_entry',self.path):
-            global pnf_entry
-            content_length = self._get_content_length()
-            pnf_entry = json.loads(self.rfile.read(content_length))
-            _mark_response_as_http_ok(self)
+                global pnf_entries
+                pnf_entries[pnf_name] = pnf_entry
 
-        return
+                httpServerLib.set_response_200_ok(self)
+                logger.debug('AAISetup PUT /setup/add_pnf_entry [' + pnf_name + '] -> 200 OK')
+            elif re.search('/set_pnf', self.path):
+                pnf_name = httpServerLib.get_payload(self).decode()
+                pnf_entries[pnf_name] = AAI_RESOURCE_NOT_FOUND
+                httpServerLib.set_response_200_ok(self)
+            else:
+                httpServerLib.set_response_404_not_found(self)
+                logger.info('AAISetup PUT ' + self.path + ' -> 404 Not found')
+        except Exception as e:
+            logger.error(e)
+            httpServerLib.set_response_500_server_error(self)
 
     def do_POST(self):
-        logger.info('AAI SIM Setup Post execution')
-        if re.search('/reset', self.path):
-            global pnf_name
-            pnf_name = 'Empty'
-            _mark_response_as_http_ok(self)
+        try:
+            if re.search('/reset', self.path):
+                global pnf_entries
+                global patched_pnf
+                global created_logical_link
+                pnf_entries = {}
+                patched_pnf = AAI_RESOURCE_NOT_FOUND
+                created_logical_link = AAI_RESOURCE_NOT_FOUND
 
-        return
-
-    def _get_content_length(self):
-        return int(self.headers['Content-Length'])
-
+                httpServerLib.set_response_200_ok(self)
+                logger.debug('AAISetup POST /reset -> 200 OK')
+            else:
+                httpServerLib.set_response_404_not_found(self)
+                logger.info('AAISetup POST ' + self.path + ' -> 404 Not found')
+        except Exception as e:
+            logger.error(e)
+            httpServerLib.set_response_500_server_error(self)
 
 class AAIHandler(BaseHTTPRequestHandler):
 
     def do_GET(self):
-        logger.info('AAI SIM Get execution')
-        full_request_path = '/aai/v12/network/pnfs/pnf/' + pnf_name
-        if re.search(full_request_path, self.path):
-            _mark_response_as_http_ok(self)
-            body = json.dumps(pnf_entry)
-            logger.info('AAI SIM Get json prepared')
-            self.wfile.write(body.encode())
-        return
-
+        try:
+            if re.search('/aai/v12/network/pnfs/pnf/[^/]*$', self.path):
+                pnf_name = basename(self.path)
+                if pnf_name in pnf_entries:
+                    httpServerLib.set_response_200_ok(self, payload = pnf_entries[pnf_name])
+                    logger.debug('AAIHandler GET /aai/v12/network/pnfs/pnf/' + pnf_name + ' -> 200 OK')
+                else:
+                    httpServerLib.set_response_404_not_found(self)
+                    logger.info('AAIHandler GET /aai/v12/network/pnfs/pnf/' + pnf_name + ' -> 404 Not found, actual entries: ' + str(pnf_entries.keys()))
+            else:
+                httpServerLib.set_response_404_not_found(self)
+                logger.info('AAIHandler GET ' + self.path + ' -> 404 Not found')
+        except Exception as e:
+            logger.error(e)
+            httpServerLib.set_response_500_server_error(self)
 
     def do_PATCH(self):
-        logger.info('AAI SIM Patch execution')
-        pnfs_name = '/aai/v12/network/pnfs/pnf/' + pnf_name
-        if re.search('wrong_aai_record', self.path):
-            self.send_response(400)
-            logger.info('Execution status 400')
-            self.end_headers()
-        elif re.search(pnfs_name, self.path):
-            self.send_response(200)
-            logger.info('Execution status 200')
-            self.end_headers()
-            
-        return
+        try:
+            if re.search('/aai/v12/network/pnfs/pnf/[^/]*$', self.path):
+                pnf_name = basename(self.path)
+                if pnf_name in pnf_entries:
+                    global patched_pnf
+                    patched_pnf = httpServerLib.get_payload(self)
+
+                    httpServerLib.set_response_200_ok(self)
+                    logger.debug('AAIHandler PATCH /aai/v12/network/pnfs/pnf/' + pnf_name + ' -> 200 OK')
+                else:
+                    httpServerLib.set_response_404_not_found(self)
+                    logger.info('AAIHandler PATCH /aai/v12/network/pnfs/pnf/' + pnf_name + ' -> 404 Not found, actual entries: ' + str(pnf_entries.keys()))
+            else:
+                httpServerLib.set_response_404_not_found(self)
+                logger.info('AAIHandler PATCH ' + self.path + ' -> 404 Not found')
+        except Exception as e:
+            logger.error(e)
+            httpServerLib.set_response_500_server_error(self)
+
+    def do_PUT(self):
+        try:
+            if re.search('/aai/v12/network/logical-links/logical-link/[^/]*$', self.path):
+                global created_logical_link
+                created_logical_link = httpServerLib.get_payload(self)
+
+                httpServerLib.set_response_200_ok(self)
+                logger.debug('AAIHandler PUT /aai/v12/network/logical-links/logical-link/' + created_logical_link + ' -> 200 OK')
+            else:
+                httpServerLib.set_response_404_not_found(self)
+                logger.info('AAIHandler PUT ' + self.path + ' -> 404 Not found')
+        except Exception as e:
+            logger.error(e)
+            httpServerLib.set_response_500_server_error(self)
 
 
 def _main_(handler_class=AAIHandler, protocol="HTTP/1.0"):
diff --git a/tests/dcaegen2/prh-testcases/resources/simulator/DMaaP.py b/tests/dcaegen2/prh-testcases/resources/simulator/DMaaP.py
index da113e6..7f590a5 100644
--- a/tests/dcaegen2/prh-testcases/resources/simulator/DMaaP.py
+++ b/tests/dcaegen2/prh-testcases/resources/simulator/DMaaP.py
@@ -15,62 +15,85 @@
 
 logger = logging.getLogger('DMaaP-simulator-logger')
 
-posted_event_from_prh = b'Empty'
-received_event_to_get_method = b'Empty'
+DMAAP_EMPTY = b'[]'
 
+ves_event = DMAAP_EMPTY
+captured_prh_event = DMAAP_EMPTY
 
 class DmaapSetup(BaseHTTPRequestHandler):
 
-    def do_PUT(self):
-        logger.info('DMaaP SIM Setup Put execution')
-        if re.search('/set_get_event', self.path):
-            global received_event_to_get_method
-            content_length = int(self.headers['Content-Length'])
-            received_event_to_get_method = self.rfile.read(content_length)
-            httpServerLib.header_200_and_json(self)
-
-        return
-
     def do_GET(self):
-        logger.info('DMaaP SIM Setup Get execution')
-        if re.search('/events/pnfReady', self.path):
-            httpServerLib.header_200_and_json(self)
-            self.wfile.write(posted_event_from_prh)
+        try:
+            if re.search('/setup/pnf_ready', self.path) or re.search('events/pnfReady', self.path):
+                global captured_prh_event
+                httpServerLib.set_response_200_ok(self, payload = captured_prh_event)
+                logger.debug('DmaapSetup GET /setup/pnf_ready -> 200 OK')
+            else:
+                httpServerLib.set_response_404_not_found(self)
+                logger.info('DmaapSetup GET ' + self.path + ' -> 404 Not found')
+        except Exception as e:
+            logger.error(e)
+            httpServerLib.set_response_500_server_error(self)
 
-        return
+    def do_PUT(self):
+        try:
+            if re.search('/setup/ves_event', self.path) or re.search('/set_get_event', self.path):
+                global ves_event
+                ves_event = httpServerLib.get_payload(self)
+                httpServerLib.set_response_200_ok(self)
+                logger.debug('DmaapSetup PUT /setup/ves_event -> 200 OK')
+            else:
+                httpServerLib.set_response_404_not_found(self)
+                logger.info('DmaapSetup PUT ' + self.path + ' -> 404 Not found')
+        except Exception as e:
+            logger.error(e)
+            httpServerLib.set_response_500_server_error(self)
 
     def do_POST(self):
-        logger.info('DMaaP SIM Setup Post execution')
-        if re.search('/reset', self.path):
-            global posted_event_from_prh
-            global received_event_to_get_method
-            posted_event_from_prh = b'Empty'
-            received_event_to_get_method = b'Empty'
-            httpServerLib.header_200_and_json(self)
-
-        return
-
+        try:
+            if re.search('/reset', self.path):
+                global ves_event
+                global captured_prh_event
+                ves_event = DMAAP_EMPTY
+                captured_prh_event = DMAAP_EMPTY
+                httpServerLib.set_response_200_ok(self)
+                logger.debug('DmaapSetup POST /reset -> 200 OK')
+            else:
+                httpServerLib.set_response_404_not_found(self)
+                logger.info('DmaapSetup POST ' + self.path + ' -> 404 Not found')
+        except Exception as e:
+            logger.error(e)
+            httpServerLib.set_response_500_server_error(self)
 
 class DMaaPHandler(BaseHTTPRequestHandler):
 
     def do_POST(self):
-        logger.info('DMaaP SIM Post execution')
-        if re.search('/events/unauthenticated.PNF_READY', self.path):
-            global posted_event_from_prh
-            content_length = int(self.headers['Content-Length'])
-            posted_event_from_prh = self.rfile.read(content_length)
-            httpServerLib.header_200_and_json(self)
-
-        return
+        try:
+            if re.search('/events/unauthenticated.PNF_READY', self.path):
+                global captured_prh_event
+                captured_prh_event = httpServerLib.get_payload(self)
+                httpServerLib.set_response_200_ok(self)
+                logger.debug('DMaaPHandler POST /events/unauthenticated.PNF_READY -> 200')
+            else:
+                httpServerLib.set_response_404_not_found(self)
+                logger.info('DMaaPHandler POST ' + self.path + ' -> 404 Not found')
+        except Exception as e:
+            logger.error(e)
+            httpServerLib.set_response_500_server_error(self)
 
     def do_GET(self):
-        logger.info('DMaaP SIM Get execution')
-        if re.search('/events/unauthenticated.VES_PNFREG_OUTPUT/OpenDcae-c12/c12', self.path):
-            httpServerLib.header_200_and_json(self)
-            self.wfile.write(received_event_to_get_method)
-
-        return
-
+        try:
+            if re.search('/events/unauthenticated.VES_PNFREG_OUTPUT/OpenDcae-c12/c12', self.path):
+                global ves_event
+                httpServerLib.set_response_200_ok(self, payload = ves_event)
+                ves_event = DMAAP_EMPTY
+                logger.debug('DMaaPHandler GET /events/unauthenticated.VES_PNFREG_OUTPUT/OpenDcae-c12/c12 -> 200')
+            else:
+                httpServerLib.set_response_404_not_found(self)
+                logger.info('DMaaPHandler GET ' + self.path + ' -> 404 Not found')
+        except Exception as e:
+            logger.error(e)
+            httpServerLib.set_response_500_server_error(self)
 
 def _main_(handler_class=DMaaPHandler, protocol="HTTP/1.0"):
     handler_class.protocol_version = protocol
@@ -80,6 +103,5 @@
     while 1:
         time.sleep(10)
 
-
 if __name__ == '__main__':
-    _main_()
+    _main_()
\ No newline at end of file
diff --git a/tests/dcaegen2/prh-testcases/resources/simulator/httpServerLib.py b/tests/dcaegen2/prh-testcases/resources/simulator/httpServerLib.py
index 7b54539..e91927e 100644
--- a/tests/dcaegen2/prh-testcases/resources/simulator/httpServerLib.py
+++ b/tests/dcaegen2/prh-testcases/resources/simulator/httpServerLib.py
@@ -2,32 +2,44 @@
 import ssl
 from http.server import HTTPServer
 
-
-def header_200_and_json(self):
+def set_response_200_ok(self, payload = None):
     self.send_response(200)
     self.send_header('Content-Type', 'application/json')
     self.end_headers()
+    if payload != None:
+        self.wfile.write(payload)
 
+def set_response_404_not_found(self):
+    self.send_response(404)
+    self.end_headers()
+
+def set_response_500_server_error(self):
+    self.send_response(500)
+    self.end_headers()
+
+def get_payload(self):
+    if self.headers['Content-Length'] == None:
+        raise Exception('Invalid payload, Content-Length not defined')
+
+    content_length = int(self.headers['Content-Length'])
+    return self.rfile.read(content_length)
 
 def start_http_endpoint(port, handler_class):
     _thread.start_new_thread(init_http_endpoints, (port, handler_class))
 
-
 def start_https_endpoint(port, handler_class, keyfile, certfile, ca_certs):
     _thread.start_new_thread(init_https_endpoints, (port, handler_class, keyfile, certfile, ca_certs))
 
-
 def init_http_endpoints(port, handler_class, server_class=HTTPServer):
     server = server_class(('', port), handler_class)
     sa = server.socket.getsockname()
     print("Serving HTTP on", sa[0], "port", sa[1], "for", handler_class, "...")
     server.serve_forever()
 
-
 def init_https_endpoints(port, handler_class, keyfile, certfile, ca_certs, server_class=HTTPServer):
     server = server_class(('', port), handler_class)
     server.socket = ssl.wrap_socket(server.socket, keyfile=keyfile, certfile=certfile,
                                     ca_certs=ca_certs, server_side=True)
     sa = server.socket.getsockname()
     print("Serving HTTPS on", sa[0], "port", sa[1], "for", handler_class, "...")
-    server.serve_forever()
+    server.serve_forever()
\ No newline at end of file