mec_http_server.py 28 KB
Newer Older
Yann Garcia's avatar
Yann Garcia committed
1
2
3
4
5
6
7
8
import http.server
import json
from mec_database import mec_database

# This class will handles any incoming request from the browser
class myHandler(http.server.BaseHTTPRequestHandler):
    __db__ = mec_database()

Yann Garcia's avatar
Yann Garcia committed
9
10
11
12
    def __init__(self, p_request, p_client_address, p_server):
        super(http.server.BaseHTTPRequestHandler, self).__init__(p_request, p_client_address, p_server)
        self.wfile._wbufsize = -1;

Yann Garcia's avatar
Yann Garcia committed
13
14
    # Handler for the GET requests
    def do_GET(self):
Yann Garcia's avatar
Yann Garcia committed
15
        print('>>> do_GET: ', self.path)
Yann Garcia's avatar
Yann Garcia committed
16
17
18
19
20
        self.protocol_version = self.request_version
        # Check HTTP end_headers
        if self.__check_http_headers__() == False:
            resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"N/A\",\t\"status\": 400,\t\"detail\": \"Wrong headers\",\t\"instance\": \"N/A\"}}"
            self.send_response(400, 'Bad Request')
Yann Garcia's avatar
Yann Garcia committed
21
            self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
Yann Garcia's avatar
Yann Garcia committed
22
            self.send_header('Authorization', self.headers.get('Authorization'))
Yann Garcia's avatar
Yann Garcia committed
23
            self.send_header('Connection', 'keep-alive')
Yann Garcia's avatar
Yann Garcia committed
24
            self.send_header('Content-Type', 'application/problem+json')
Yann Garcia's avatar
Yann Garcia committed
25
26
27
28
29
30
31
            l = len(resp)
            self.send_header('Content-Length', str(l))
            self.end_headers()
            self.wfile.write(bytes(resp, 'utf-8'))
            return

        resp, content_type = self.__process__()
Yann Garcia's avatar
Yann Garcia committed
32
        print('do_GET: resp= ', resp, ', Content_Type= ', content_type)
Yann Garcia's avatar
Yann Garcia committed
33
34
        if (resp == None):
            self.send_response(404, 'Not Found')
Yann Garcia's avatar
Yann Garcia committed
35
            self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
Yann Garcia's avatar
Yann Garcia committed
36
            self.send_header('Authorization', self.headers.get('Authorization'))
Yann Garcia's avatar
Yann Garcia committed
37
            self.send_header('Connection', 'keep-alive')
Yann Garcia's avatar
Yann Garcia committed
38
39
40
41
42
43
44
45
46
47
48
49
            self.send_header('Content-Type', content_type)
            self.send_header('Content-Length', '0')
            self.end_headers()
        else:
            if resp.find('Not Found') != -1:
                self.send_response(404, 'Not Found')
            elif resp.find('Forbidden') != -1:
                self.send_response(403, 'Forbidden')
            elif resp.find('Bad Request') != -1:
                self.send_response(400, 'Bad Request')
            else:
                self.send_response(200, 'OK')
Yann Garcia's avatar
Yann Garcia committed
50
            self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
Yann Garcia's avatar
Yann Garcia committed
51
            self.send_header('Authorization', self.headers.get('Authorization'))
Yann Garcia's avatar
Yann Garcia committed
52
            self.send_header('Connection', 'keep-alive')
Yann Garcia's avatar
Yann Garcia committed
53
54
55
            self.send_header('Content-Type', content_type)
            l = len(resp)
            self.send_header('Content-Length', str(l))
Yann Garcia's avatar
Yann Garcia committed
56
57
58
59
60
61
62
63
64
            #self.end_headers() # Unable to bufferize wfile, so calling end_headers() headers and body are sent in 2 different TCP packets
            self._headers_buffer.append(b"\r\n")
            self._headers_buffer.append(bytes(resp, 'utf-8'))
            self.wfile.write(b"".join(self._headers_buffer))
            self._headers_buffer = []
            #self.wfile.write(bytes(resp, 'utf-8'))
            #self.wfile.flush()
        print("<<< do_GET")
        return
Yann Garcia's avatar
Yann Garcia committed
65
66
67
68
69
70
71
72
73
74
75
76
77
    # End of do_GET

    # Handler for the POST requests
    def do_POST(self):
        print(">>> do_POST: ", self.path)
        self.protocol_version = self.request_version
        # Check HTTP end_headers
        if self.__check_http_headers__() == False:
            resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"N/A\",\t\"status\": 400,\t\"detail\": \"Wrong headers\",\t\"instance\": \"N/A\"}}"
            self.protocol_version = self.request_version
            self.send_response(400, 'Bad Request')
            self.send_header('Host', self.headers.get('Host'))
            self.send_header('Authorization', self.headers.get('Authorization'))
Yann Garcia's avatar
Yann Garcia committed
78
            self.send_header('Connection', 'keep-alive')
Yann Garcia's avatar
Yann Garcia committed
79
            self.send_header('Content-Type', 'application/problem+json')
Yann Garcia's avatar
Yann Garcia committed
80
81
82
83
84
85
86
            l = len(resp)
            self.send_header('Content-Length', str(l))
            self.end_headers()
            self.wfile.write(bytes(resp, 'utf-8'))
            return

        resp, content_type = self.__process__()
Yann Garcia's avatar
Yann Garcia committed
87
        print('do_POST: resp= ', resp, ', Content_Type= ', content_type)
Yann Garcia's avatar
Yann Garcia committed
88
89
        if (resp == None):
            self.send_response(404, 'Not Found')
Yann Garcia's avatar
Yann Garcia committed
90
            self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
Yann Garcia's avatar
Yann Garcia committed
91
            self.send_header('Authorization', self.headers.get('Authorization'))
Yann Garcia's avatar
Yann Garcia committed
92
            self.send_header('Connection', 'keep-alive')
Yann Garcia's avatar
Yann Garcia committed
93
94
95
96
97
98
            self.send_header('Content-Type', content_type)
            self.send_header('Content-Length', '0')
            self.end_headers()
        else:
            if resp.find('Not Found') != -1:
                self.send_response(404, 'Not Found')
Yann Garcia's avatar
Yann Garcia committed
99
                resp = ""
Yann Garcia's avatar
Yann Garcia committed
100
101
            elif resp.find('Forbidden') != -1:
                self.send_response(403, 'Forbidden')
Yann Garcia's avatar
Yann Garcia committed
102
                resp = ""
Yann Garcia's avatar
Yann Garcia committed
103
            elif resp.find('Bad Request') != -1 or resp.find('Wrong parameters') != -1:
Yann Garcia's avatar
Yann Garcia committed
104
                self.send_response(400, 'Bad Request')
Yann Garcia's avatar
Yann Garcia committed
105
106
107
108
            elif resp.find('Precondition Failed') != -1:
                self.send_response(412, 'Precondition Failed')
                resp = "{\"problemDetails\": {\t\"type\": \"Precondition Failed\",\t\"title\": \"N/A\",\t\"status\": 412,\t\"detail\": \"Wrong preconditions\",\t\"instance\": \"N/A\"}}"
                content_type = 'application/problem+json'
Yann Garcia's avatar
Yann Garcia committed
109
110
            elif resp.find('userTrackingSubscription') != -1:
                self.send_response(201, 'Created')
Yann Garcia's avatar
Yann Garcia committed
111
                resp = ""
Yann Garcia's avatar
Yann Garcia committed
112
113
            elif resp.find('CellChangeSubscription') != -1:
                self.send_response(201, 'Created')
Yann Garcia's avatar
Yann Garcia committed
114
115
            else:
                self.send_response(200, 'OK')
Yann Garcia's avatar
Yann Garcia committed
116
            self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
Yann Garcia's avatar
Yann Garcia committed
117
            self.send_header('Authorization', self.headers.get('Authorization'))
Yann Garcia's avatar
Yann Garcia committed
118
            self.send_header('Connection', 'keep-alive')
Yann Garcia's avatar
Yann Garcia committed
119
120
121
            self.send_header('Content-Type', content_type)
            l = len(resp)
            self.send_header('Content-Length', str(l))
Yann Garcia's avatar
Yann Garcia committed
122
123
124
125
126
127
128
129
130
131
132
            #self.end_headers() # Unable to bufferize wfile, so calling end_headers() headers and body are sent in 2 different TCP packets
            self._headers_buffer.append(b"\r\n")
            self._headers_buffer.append(bytes(resp, 'utf-8'))
            self.wfile.write(b"".join(self._headers_buffer))
            self._headers_buffer = []
            #self.wfile.write(bytes(resp, 'utf-8'))
            #self.wfile.flush()
        print("<<< do_POST")
        return
    # End of do_POST

Yann Garcia's avatar
Yann Garcia committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    # Handler for the PUT requests
    def do_PUT(self):
        print(">>> do_PUT: ", self.path)
        self.protocol_version = self.request_version
        # Check HTTP end_headers
        if self.__check_http_headers__() == False:
            resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"N/A\",\t\"status\": 400,\t\"detail\": \"Wrong headers\",\t\"instance\": \"N/A\"}}"
            self.protocol_version = self.request_version
            self.send_response(400, 'Bad Request')
            self.send_header('Host', self.headers.get('Host'))
            self.send_header('Authorization', self.headers.get('Authorization'))
            self.send_header('Connection', 'keep-alive')
            self.send_header('Content-Type', 'application/problem+json')
            l = len(resp)
            self.send_header('Content-Length', str(l))
            self.end_headers()
            self.wfile.write(bytes(resp, 'utf-8'))
            return

        resp, content_type = self.__process__()
        print('do_PUT: resp= ', resp, ', Content_Type= ', content_type)
        if (resp == None):
            self.send_response(404, 'Not Found')
            self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
            self.send_header('Authorization', self.headers.get('Authorization'))
            self.send_header('Connection', 'keep-alive')
            self.send_header('Content-Type', content_type)
            self.send_header('Content-Length', '0')
            self.end_headers()
        else:
            if resp.find('Not Found') != -1:
                self.send_response(404, 'Not Found')
                resp = ""
            elif resp.find('Forbidden') != -1:
                self.send_response(403, 'Forbidden')
                resp = ""
            elif resp.find('Bad Request') != -1:
                self.send_response(400, 'Bad Request')
                resp = ""
            elif resp.find('Precondition Failed') != -1:
                self.send_response(412, 'Precondition Failed')
                resp = "{\"problemDetails\": {\t\"type\": \"Precondition Failed\",\t\"title\": \"N/A\",\t\"status\": 412,\t\"detail\": \"Wrong preconditions\",\t\"instance\": \"N/A\"}}"
                content_type = 'application/problem+json'
            else:
                self.send_response(200, 'OK')
            self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
            self.send_header('Authorization', self.headers.get('Authorization'))
            self.send_header('Connection', 'keep-alive')
            self.send_header('Content-Type', content_type)
            l = len(resp)
            self.send_header('Content-Length', str(l))
            #self.end_headers() # Unable to bufferize wfile, so calling end_headers() headers and body are sent in 2 different TCP packets
            self._headers_buffer.append(b"\r\n")
            self._headers_buffer.append(bytes(resp, 'utf-8'))
            self.wfile.write(b"".join(self._headers_buffer))
            self._headers_buffer = []
            #self.wfile.write(bytes(resp, 'utf-8'))
            #self.wfile.flush()
        print("<<< do_PUT")
        return
    # End of do_POST

Yann Garcia's avatar
Yann Garcia committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    # Handler for the DELETE requests
    def do_DELETE(self):
        print('>>> do_DELETE: ', self.path)
        self.protocol_version = self.request_version
        # Check HTTP end_headers
        if self.__check_http_headers__() == False:
            resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"N/A\",\t\"status\": 400,\t\"detail\": \"Wrong headers\",\t\"instance\": \"N/A\"}}"
            self.send_response(400, 'Bad Request')
            self.send_header('Host', self.headers.get('Host'))
            self.send_header('Authorization', self.headers.get('Authorization'))
            self.send_header('Connection', 'keep-alive')
            self.send_header('Content-Type', 'application/problem+json')
            l = len(resp)
            self.send_header('Content-Length', str(l))
Yann Garcia's avatar
Yann Garcia committed
209
210
            self.end_headers()
            self.wfile.write(bytes(resp, 'utf-8'))
Yann Garcia's avatar
Yann Garcia committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
            return

        resp, content_type = self.__process__()
        print('do_DELETE: resp= ', resp, ', Content_Type= ', content_type)
        if (resp == None):
            self.send_response(404, 'Not Found')
            self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
            self.send_header('Authorization', self.headers.get('Authorization'))
            self.send_header('Connection', 'keep-alive')
            self.send_header('Content-Type', content_type)
            self.send_header('Content-Length', '0')
            self.end_headers()
        else:
            if resp.find('Not Found') != -1:
                self.send_response(404, 'Not Found')
            elif resp.find('Forbidden') != -1:
                self.send_response(403, 'Forbidden')
            elif resp.find('Bad Request') != -1:
                self.send_response(400, 'Bad Request')
            else:
                self.send_response(200, 'OK')
            self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
            self.send_header('Authorization', self.headers.get('Authorization'))
            self.send_header('Connection', 'keep-alive')
            self.send_header('Content-Type', content_type)
            l = len(resp)
            self.send_header('Content-Length', str(l))
            #self.end_headers()
            self._headers_buffer.append(b"\r\n")
            self._headers_buffer.append(bytes(resp, 'utf-8'))
            self.wfile.write(b"".join(self._headers_buffer))
            self._headers_buffer = []
            #self.wfile.write(bytes(resp, 'utf-8'))
            #self.wfile.flush()
        print("<<< do_DELETE")
        return
    # End of do_DELETE
Yann Garcia's avatar
Yann Garcia committed
248
249
250

    # Check HTTP headers
    def __check_http_headers__(self):
Yann Garcia's avatar
Yann Garcia committed
251
        print('>>> __check_http_headers__')
Yann Garcia's avatar
Yann Garcia committed
252
253
254
        result = True
        host = self.headers.get('Host')
        if host == None:
Yann Garcia's avatar
Yann Garcia committed
255
            print("__check_http_headers__: Failed to parse Host header")
Yann Garcia's avatar
Yann Garcia committed
256
257
            result = False
        content_type = self.headers.get('Content-Type')
Yann Garcia's avatar
Yann Garcia committed
258
        if content_type != 'application/json':
Yann Garcia's avatar
Yann Garcia committed
259
260
261
262
263
264
265
266
267
268
269
270
            print("__check_http_headers__: Failed to parse 'Content-Type' header")
            result = False
        auth = self.headers.get('Authorization')
        if auth == None:
            print("__check_http_headers__: Failed to parse 'Authorization' header")
            result = False
        return result
    # End of __check_http_headers__

    # Process the request
    def __process__(self):
        s = self.path.split('/')
Yann Garcia's avatar
Yann Garcia committed
271
        print('>>> __process__: ', s)
Yann Garcia's avatar
Yann Garcia committed
272
        if s[1] != 'exampleAPI':
Yann Garcia's avatar
Yann Garcia committed
273
            return None, 'application/json'
Yann Garcia's avatar
Yann Garcia committed
274
275
276
277
278
279
280
281
282
        # TODO Check HTTP headers
        if s[2] == 'location' and ((s[3] == 'v1') or (s[3] == 'v2')):
            return self.__process__location__api__(s)
        elif s[2] == 'ui' and ((s[3] == 'v1') or (s[3] == 'v2')):
            return self.__process__ue__identity__api__(s)
        elif s[2] == 'rni' and ((s[3] == 'v1') or (s[3] == 'v2')):
            return self.__process__rnis__api__(s)
        elif s[2] == 'bwm' and ((s[3] == 'v1') or (s[3] == 'v2')):
            return self.__process__bwm__api__(s)
Yann Garcia's avatar
Yann Garcia committed
283
284
        elif s[2] == 'mec_app_support' and ((s[3] == 'v1') or (s[3] == 'v2')):
            return self.__process__mp1__api__(s)
Yann Garcia's avatar
Yann Garcia committed
285
        else:
Yann Garcia's avatar
Yann Garcia committed
286
            return None, 'application/json'
Yann Garcia's avatar
Yann Garcia committed
287
288
289
290
291
292
    # End of __process__

    # Process the LocationAPI request
    def __process__location__api__(self, p_split):
        print (">>> __process__location__api__: ", p_split)
        resp = None
Yann Garcia's avatar
Yann Garcia committed
293
        content_type = 'application/json'
Yann Garcia's avatar
Yann Garcia committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
        if p_split[4].startswith('users'): # E.g. users?zoneId=zone01
            s = p_split[4].split('?')
            if s.__len__() == 1: # Users list requested
                resp = self.__db__.getSubscriberList(self.path)
            elif s.__len__() == 2: # E.g. zoneId=zone01
                s = s[1].split('=')
                if s[0] == 'zoneId': # Users list into a specific zoneId requested
                    resp = self.__db__.getSubscriberFromZoneId(s[1], self.path)
                elif s[0] == 'address':
                    resp = self.__db__.getSubscriberFromAddress(s[1])
        elif p_split[4] == 'zones':
            if p_split.__len__() == 5:
                s = p_split[4].split('?')
                print("__process__location__api__: ", s)
                if s.__len__() == 1: # A list requested
                    if s[0] == 'zones':
                        resp = self.__db__.getZoneList(self.path)
                    else:
                        resp = "{\"Unsupported zone request\"}"
                elif s.__len__() == 2: # E.g. ?
                    resp = "{\"Unsupported zone request\"}"
                else:
                    resp = "{\"Unsupported " + p_split[4] + " request\"}"
            elif p_split.__len__() == 7:
                if p_split[6] == 'accessPoints':
                    resp = self.__db__.getAccessPointList(p_split[5], self.path)
                else:
                    resp = "{\"Unsupported " + p_split[5] + " request\"}"
            else:
                resp = "{\"Unsupported " + p_split[4] + " request\"}"
Yann Garcia's avatar
Yann Garcia committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
        elif p_split[4] == 'subscriptions':
            if p_split.__len__() == 6:
                # Extract the body if any
                body = None
                content_len = int(self.headers.get('Content-Length'))
                if (content_len != 0):
                    body = self.rfile.read(content_len)
                    json_msg = self.__decode__json__body__(body)
                    if (json_msg == None):
                        resp = "{\"problemDetails\": {\t\"type\": \"Body processing not supported\",\t\"title\": \"UElocationAPI\",\t\"status\": 400,\t\"detail\": \"Unknown request\",\t\"instance\": \"string\"}}"
                    else:
                        resp = self.__db__.registerSubscription(json_msg, self.path)
            elif p_split.__len__() == 7:
                resp = self.__db__.unregisterSubscription(p_split[6])
Yann Garcia's avatar
Yann Garcia committed
338
339
340
341
342
343
344
345
346
347
348
        else:
            resp = "{\"Unsupported " + p_split[4] + " request\"}"

        print ("<<< __process__location__api__: ", resp, ", ", content_type)
        return resp, content_type
    # End of __process__location__api__

    # Process the ue_identityAPI request
    def __process__ue__identity__api__(self, p_split):
        print (">>> __process__ue__identity__api__: ", p_split)
        resp = None
Yann Garcia's avatar
Yann Garcia committed
349
        content_type = 'application/json'
Yann Garcia's avatar
Yann Garcia committed
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
        if p_split[4] == 'appInst98': # Used for unknown application, e.g. TC_MEC_PLAT_UETAG_003_BI
            resp = "{\"problemDetails\": {\t\"type\": \"Not Authorized\",\t\"title\": \"UEidentityAPI\",\t\"status\": 403,\t\"detail\": \"Forbidden\",\t\"instance\": \"AppInst98\"}}"
        else:
            if p_split[5].startswith('ue_identity_tag_info'): # E.g. ue_identity_tag_info?ueIdentityTag=UeTagA
                s = p_split[5].split('?')
                if s[0] != 'ue_identity_tag_info':
                    resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"UEidentityAPI\",\t\"status\": 400,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
                elif s.__len__() == 1: # ue_identity_tag_info, POST
                    # Register/Unregister operation
                    body = None
                    # Extract the body if any
                    content_len = int(self.headers.get('Content-Length'))
                    if (content_len != 0):
                        body = self.rfile.read(content_len)
                        json_msg = self.__decode__json__body__(body)
                        if (json_msg == None):
                            resp = "{\"problemDetails\": {\t\"type\": \"Body processing not supported\",\t\"title\": \"UEidentityAPI\",\t\"status\": 400,\t\"detail\": \"Unknown request\",\t\"instance\": \"string\"}}"
                        else:
                            resp = self.__db__.registerUEidentity(p_split[4], json_msg)
                elif s.__len__() == 2: # E.g. ueIdentityTag=UeTagA
                    s = s[1].split('=')
                    if s[0] == 'ueIdentityTag': # Users list into a specific zoneId requested
                        resp = self.__db__.getUEidentityTagInfo(p_split[4], s[1])
                        if (resp == None):
                            resp = "{\"problemDetails\": {\t\"type\": \"Not Found\",\t\"title\": \"UEidentityAPI\",\t\"status\": 404,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
Yann Garcia's avatar
Yann Garcia committed
375
                            content_type = 'application/problem+json'
Yann Garcia's avatar
Yann Garcia committed
376
377
        if (resp == None):
            resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"UEidentityAPI\",\t\"status\": 400,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
Yann Garcia's avatar
Yann Garcia committed
378
        if resp.find('problemDetails') != -1:
Yann Garcia's avatar
Yann Garcia committed
379
            content_type = 'application/problem+json'
Yann Garcia's avatar
Yann Garcia committed
380
381
382
383
384
385
386

        print ("<<< __process__ue__identity__api__: ", resp, ", ", content_type)
        return resp, content_type
    # End of __process__ue__identity__api__

    # Process the RnisAPI request
    def __process__rnis__api__(self, p_split):
Yann Garcia's avatar
Yann Garcia committed
387
        print ('>>> __process__rnis__api__: ', p_split)
Yann Garcia's avatar
Yann Garcia committed
388
        resp = None
Yann Garcia's avatar
Yann Garcia committed
389
        content_type = 'application/json'
Yann Garcia's avatar
Yann Garcia committed
390
        if p_split[4].startswith('subscriptions'):
Yann Garcia's avatar
Yann Garcia committed
391
392
            if p_split.__len__() == 5:
                s = p_split[4].split('?')
Yann Garcia's avatar
Yann Garcia committed
393
394
395
396
397
398
399
400
401
402
403
404
                if s.__len__() == 1: # Chek message body
                    # Register/Unregister operation
                    body = None
                    # Extract the body if any
                    content_len = int(self.headers.get('Content-Length'))
                    if (content_len != 0):
                        body = self.rfile.read(content_len)
                        json_msg = self.__decode__json__body__(body)
                        if (json_msg == None):
                            resp = "{\"problemDetails\": {\t\"type\": \"Body processing not supported\",\t\"title\": \"UEidentityAPI\",\t\"status\": 400,\t\"detail\": \"Unknown request\",\t\"instance\": \"string\"}}"
                        else:
                            resp = self.__db__.registerRniSubscription(json_msg)
Yann Garcia's avatar
Yann Garcia committed
405
406
407
408
409
410
411
                elif s[0] != 'subscriptions':
                    resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"RnisAPI\",\t\"status\": 400,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
                else:
                    s = s[1].split('=')
                    print('__process__rnis__api__: ', s)
                    if s[0] != 'subscription_type':
                        resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"RnisAPI\",\t\"status\": 400,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
Yann Garcia's avatar
Yann Garcia committed
412
413
                    elif s[1] == 'wrongSubscriptionType':
                        resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"RnisAPI\",\t\"status\": 400,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
Yann Garcia's avatar
Yann Garcia committed
414
415
416
                    else:
                        resp = self.__db__.getSubscriptionLinkList(self.path)
            elif p_split.__len__() == 6:
Yann Garcia's avatar
Yann Garcia committed
417
418
                if p_split[5] == '':
                    resp = self.__db__.getSubscriptionLinkList(self.path)
Yann Garcia's avatar
Yann Garcia committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
                elif s[0] != 'subscriptions':
                    resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"RnisAPI\",\t\"status\": 400,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
                else:
                    s = s[1].split('=')
                    print('__process__rnis__api__: ', s)
                    if s[0] != 'subscription_type':
                        resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"RnisAPI\",\t\"status\": 400,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
                    elif s[1] == 'wrongSubscriptionType':
                        resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"RnisAPI\",\t\"status\": 400,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
                    else:
                        resp = self.__db__.getSubscriptionLinkList(self.path)
        elif p_split[4].startswith('queries'):
            if p_split.__len__() == 5:
                s = p_split[4].split('?')
                if s.__len__() == 1: # Chek message body
                    pass
                else:
                    if s[0] != 'queries':
                        resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"RnisAPI\",\t\"status\": 400,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
                    else:
                        s = s[1].split('=')
                        print('__process__rnis__api__ (queries): ', s)
                        if s[0] == 'cell_id':
                            resp = self.__db__.getRabInfo(s[1])
                        elif s[0] == 'plmn_info':
                            resp = self.__db__.getPlmnInfo(s[1])
                        else:
                            resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"RnisAPI\",\t\"status\": 400,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
Yann Garcia's avatar
Yann Garcia committed
447
448
        if (resp == None):
            resp = "{\"problemDetails\": {\t\"type\": \"Not supported\",\t\"title\": \"RnisAPI\",\t\"status\": 400,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
Yann Garcia's avatar
Yann Garcia committed
449
        if resp.find('problemDetails') != -1:
Yann Garcia's avatar
Yann Garcia committed
450
            content_type = 'application/problem+json'
Yann Garcia's avatar
Yann Garcia committed
451
452
453
454
455
456
457
458
459

        print ("<<< __process__rnis__api__: ", resp, ", ", content_type)
        return resp, content_type
    # End of __process__rnis__api__

    # Process the BwManagementAPI request
    def __process__bwm__api__(self, p_split):
        print (">>> __process__bwm__api__: ", p_split)
        resp = None
Yann Garcia's avatar
Yann Garcia committed
460
        content_type = 'application/json'
Yann Garcia's avatar
Yann Garcia committed
461
462
463
464
465
466
467
468
469
470
471
472
        if p_split[4].startswith('bw_allocations'): # E.g. bw_allocation?app_instance_id=InstApp01
            s = p_split[4].split('?')
            if s.__len__() == 1: # ue_identity_tag_info, POST
                # Register/Unregister operation
                body = None
                # Extract the body if any
                content_len = int(self.headers.get('Content-Length'))
                if (content_len != 0):
                    body = self.rfile.read(content_len)
                    json_msg = self.__decode__json__body__(body)
                    if (json_msg == None):
                        resp = "{\"problemDetails\": {\t\"type\": \"Body processing not supported\",\t\"title\": \"BwManagementAPI\",\t\"status\": 400,\t\"detail\": \"Unknown request\",\t\"instance\": \"string\"}}"
Yann Garcia's avatar
Yann Garcia committed
473
                        content_type = 'application/problem+json'
Yann Garcia's avatar
Yann Garcia committed
474
475
476
477
478
479
480
481
                    else:
                        resp = self.__db__.bwAllocation(json_msg)
            elif s.__len__() == 2: # E.g. app_instance_id=InstApp01
                s = s[1].split('=')
                if s[0] == 'app_instance_id':
                    resp = self.__db__.getBwAllocationAppInst(p_split[4], s[1])
        if (resp == None):
            resp = "{\"problemDetails\": {\t\"type\": \"Not supported\",\t\"title\": \"BwManagementAPI\",\t\"status\": 400,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
Yann Garcia's avatar
Yann Garcia committed
482
            content_type = 'application/problem+json'
Yann Garcia's avatar
Yann Garcia committed
483
484
485
486
487

        print ("<<< __process__bwm__api__: ", resp, ", ", content_type)
        return resp, content_type
    # End of __process__bwm__api__

Yann Garcia's avatar
Yann Garcia committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
    # Process the MP1 request
    def __process__mp1__api__(self, p_split):
        print (">>> __process__mp1__api__: ", p_split)
        resp = None
        content_type = 'application/json'
        if p_split[4].startswith('transports'): # E.g. /mp1/v1/transports
            resp = self.__db__.getMp1TransportInfoList()
        elif p_split[4].startswith('applications'):
            if p_split[6].startswith('dns_rules'): # E.g. /mp1/v1/applications/appInstId01/dns_rules
                pass
            elif p_split[6].startswith('traffic_rules'):  # E.g. GET/PUT /mp1/v1/applications/appInstId01/traffic_rules...
                if length(p_split) == 7: # E.g. GET /mp1/v1/applications/appInstId01/traffic_rules
                    resp = self.__db__.getMp1TrafficRulesList()
                else: # E.g. GET/PUT /mp1/v1/applications/appInstId01/traffic_rules/{trafficRuleId}
                    resp = self.__db__.getMp1TrafficRule(p_split[7])
        if (resp == None):
            resp = "{\"problemDetails\": {\t\"type\": \"Not supported\",\t\"title\": \"Mp1\",\t\"status\": 400,\t\"detail\": \"Wrong parameters\",\t\"instance\": \"string\"}}"
            content_type = 'application/problem+json'
        print ("<<< __process__mp1__api__: ", resp, ", ", content_type)
        return resp, content_type
    # End of __process__bwm__api__

Yann Garcia's avatar
Yann Garcia committed
510
511
    # Decode a Json HTTP body message
    def __decode__json__body__(self, p_body):
Yann Garcia's avatar
Yann Garcia committed
512
        print(">>> __decode__json__body__: ", p_body)
Yann Garcia's avatar
Yann Garcia committed
513
514
        json_msg = json.loads(p_body)
        print("__decode__json__body__: ", type(json_msg))
Yann Garcia's avatar
Yann Garcia committed
515
516
517
518
519
        print("__decode__json__body__: ", json_msg.keys())
        if ("ueIdentityTags" in json_msg.keys()) == True:
            return json_msg.get("ueIdentityTags")[0]
        elif ("userTrackingSubscription" in json_msg.keys()) == True:
            return json_msg.get("userTrackingSubscription")
Yann Garcia's avatar
Yann Garcia committed
520
521
        elif ("CellChangeSubscription" in json_msg.keys()) == True:
            return json_msg.get("CellChangeSubscription")
Yann Garcia's avatar
Yann Garcia committed
522
        return None
Yann Garcia's avatar
Yann Garcia committed
523
524
525
    # End of __decode__json__body__

# End of class SimpleHTTPRequestHandler