RIC-642 related changes: REST subscription, rnib enhancements, symptomdata, rest...
[ric-plt/xapp-frame-py.git] / examples / restserversimu.py
1 import sys
2 import time
3 import json
4 import argparse
5
6 sys.path.insert(0, './')
7
8 import ricxappframe.xapp_rest as ricrest
9
10 def respPostHandler(name, path, data, ctype):
11     print(name)
12     print(path)
13     print(data)
14     response = ricrest.initResponse()
15     response['payload'] = ('{ "SubscriptionResponse": {'
16         '"SubscriptionId": "testing",'
17         '"SubscriptionInstances": [{'
18             '"XappEventInstanceID": "16253",'
19             '"E2EventInstanceID": "1241"'
20             '}]'
21         '}'
22     '}')
23     return response
24
25
26 def respSymptomGetHandler(name, path, data, ctype):
27     print(name)
28     print(path)
29     response = ricrest.initResponse()
30     response['payload'] = ('[{"service" : "xapp-test"}]')
31     print(json.loads(response['payload']))
32     return response
33
34 def respGetHandler(name, path, data, ctype):
35     print(name)
36     print(path)
37     response = ricrest.initResponse()
38     response['payload'] = ('{ "SubscriptionList": [{'
39             '"SubscriptionId": "12345",'
40             '"Meid": "gnb123456",'
41                         '"ClientEndpoint": ["127.0.0.1:4056"],'
42             '"SubscriptionInstances": [{'
43                 '"XappEventInstanceID": "16253",'
44                 '"E2EventInstanceID": "1241"'
45                 '}]'
46             '}]'
47         '}')
48     return response
49
50 def respDeleteHandler(name, path, data, ctype):
51     print(name)
52     print(path)
53     response = ricrest.initResponse()
54     response['payload'] = ('{}')
55     return response
56
57 if __name__ == "__main__":
58     parser = argparse.ArgumentParser()
59     parser.add_argument('-port', dest='port', help='HTTP rest server listen port', required=False, type=int)
60     parser.add_argument('-address', dest='address', help='HTTP rest IP listen address, default all interfaces', required=False, type=str)
61     
62     args = parser.parse_args()
63     
64     if args.port is None:
65         args.port = 8088 
66     if args.address is None:
67         args.address = "0.0.0.0"
68
69     # create the thread HTTP server
70     server = ricrest.ThreadedHTTPServer(args.address, args.port)
71     # trick to get the own handler with defined 
72     server.handler.add_handler(server.handler, "GET", "response", "/ric/v1/subscriptions", respGetHandler)
73     server.handler.add_handler(server.handler, "DELETE", "delete", "/ric/v1/subscriptions/", respDeleteHandler)
74     server.handler.add_handler(server.handler, "GET", "lwsdget", "/ric/v1/lwsd", respSymptomGetHandler)
75     server.handler.add_handler(server.handler, "POST", "lwsdpost", "/ric/v1/lwsd", respSymptomGetHandler)
76     server.handler.add_handler(server.handler, "POST", "responsepost", "/ric/v1", respPostHandler)
77     # for symptomdata subscription
78
79     server.start()
80     while True:
81         time.sleep(60)
82     server.stop()