aboutsummaryrefslogtreecommitdiffstats
path: root/tests/dcaegen2/bbs-testcases/resources/simulator/AAI.py
blob: ba32582d6b37faf08c47370f72bc934b712b588d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import re
import time
from http.server import BaseHTTPRequestHandler
import httpServerLib

import json

pnfs = []
services = []
pnf_not_found = '[{}]'

class AAISetup(BaseHTTPRequestHandler):

    def do_PUT(self):
        global pnfs
        global services
        global pnf_not_found
        if re.search('/set_pnfs', self.path):
            content_length = int(self.headers['Content-Length'])
            pnfs = self.rfile.read(content_length)
            pnfs = pnfs.decode()
            httpServerLib.header_200_and_json(self)
        elif re.search('/set_services', self.path):
            content_length = int(self.headers['Content-Length'])
            services = self.rfile.read(content_length)
            services = services.decode()
            httpServerLib.header_200_and_json(self)
        elif re.search('/set_pnf_not_found', self.path):
            content_length = int(self.headers['Content-Length'])
            pnf_not_found = self.rfile.read(content_length)
            pnf_not_found = pnf_not_found.decode()
            httpServerLib.header_200_and_json(self)

        return

    def do_POST(self):
        if re.search('/reset', self.path):
            httpServerLib.header_200_and_json(self)

        return


class AAIHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        global pnfs
        global services
        global pnf_not_found
        pnf_path = '/aai/v14/network/pnfs/pnf/'
        service_path = '/aai/v14/nodes/service-instances/service-instance/'
        found_resource = None
        if re.search(pnf_path, self.path):
            try:
                python_pnfs = json.loads(pnfs)
            except AttributeError:
                python_pnfs = []
            for pnf_instance in python_pnfs:
                try:
                    pnf_name = pnf_path + pnf_instance.get("pnf-name")
                except AttributeError:
                    pnf_name = "PNF not found"
                if re.search(pnf_name, self.path):
                    found_resource = pnf_instance
                    break
        elif re.search(service_path, self.path):
            try:
                python_services = json.loads(services)
            except AttributeError:
                python_services = []
            for service_instance in python_services:
                try:
                    service_name = service_path + service_instance.get("service-instance-id")
                except AttributeError:
                    pnf_name = "Service not found"
                if re.search(service_name, self.path):
                    found_resource = service_instance
                    break

        if found_resource is not None:
            # Prepare the response for DMaaP (byte encoded JSON Object)
            found_resource = json.dumps(found_resource)
            found_resource = found_resource.encode()
            httpServerLib.header_200_and_json(self)
            self.wfile.write(found_resource)
        else:
            # Send a 404 message for not found
            pnf_resp = pnf_not_found
            # We have to replace the request line data
            err_pnf_template = "nodes/pnfs/pnf/Wrong-PNF-Name"
            pnf_resp = pnf_resp.replace(err_pnf_template, self.path)
            pnf_resp = pnf_resp.encode()
            httpServerLib.header_404_and_json(self)
            self.wfile.write(pnf_resp)

            
        return

def _main_(handler_class=AAIHandler, protocol="HTTP/1.0"):
    handler_class.protocol_version = protocol
    httpServerLib.start_http_endpoint(3333, AAIHandler)
    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")
    httpServerLib.start_http_endpoint(3335, AAISetup)
    while 1:
        time.sleep(10)


if __name__ == '__main__':
    _main_()