[DCAEGEN2] Update son-handler CSIT with CPS
[integration/csit.git] / scripts / dcaegen2-services-son-handler / sonhandler / configdb-oof-sim.py
1 import flask
2 import json
3 from flask import request
4 import requests
5 import threading
6 import time
7
8 app = flask.Flask(__name__)
9 app.config["DEBUG"] = True
10
11 def get_neighbour_cell_list_for_cell_id():
12     with open('cell_list.json') as cell_list:
13         data = json.load(cell_list)
14     if not data:
15         return {"Error": "Unable to read file"}, 503
16     return data, None
17
18 def get_pci_for_cell_id():
19     with open('pci_value.json') as pci_value:
20         data = json.load(pci_value)
21     if not data:
22         return {"Error": "Unable to read file"}, 503
23     return data, None
24
25 def get_cell_data_for_cell_id():
26     with open('cell_data.json') as cell_data:
27         data = json.load(cell_data)
28     if not data:
29         return {"Error": "Unable to read file"}, 503
30     return data, None
31
32 def get_oof_sync_response():
33     with open('oof_syn_response.json') as syncRes:
34         data = json.load(syncRes)
35     if not data:
36         return {"Error": "Unale to read file"}, 503
37     return data, None
38
39 def get_oof_async_response(callback_url, transaction_id):
40     time.sleep(10)
41     with open('oof_async_response.json') as asyncRes:
42         data = json.load(asyncRes)
43         data['transactionId'] = transaction_id
44     if not data:
45         return {"Error": "Unable to read file"}, 503
46     res = requests.post(callback_url, json=data)
47     print('response from server:',res.text)
48     return res
49
50 @app.route("/api/sdnc-config-db/v3/getNbrList/<cell_id>/<ts>", methods=["GET"])
51 def get_neighbour_list(cell_id, ts):
52     data, status = get_neighbour_cell_list_for_cell_id()
53     if not status:
54         return data
55     return data, 503
56
57 @app.route("/api/sdnc-config-db/v3/getPCI/<cell_id>/<ts>", methods=["GET"])
58 def get_pci(cell_id, ts):
59     data, status = get_pci_for_cell_id()
60     if not status:
61         return data
62     return data, 503
63 @app.route("/api/sdnc-config-db/v3/getPnfId/<cell_id>/<ts>", methods=["GET"])
64 def get_pnf_id(cell_id, ts):
65     data, status = get_pci_for_cell_id()
66     data['value'] = 'ncserver5'
67     if not status:
68         return data
69     return data, 503
70
71 @app.route("/api/sdnc-config-db/v3/getCell/<cell_id>", methods=["GET"])
72 def get_cell_data(cell_id):
73     data, status = get_cell_data_for_cell_id()
74     if not status:
75         return data
76     return data, 503
77
78 @app.route("/api/oof/v1/pci",methods=["POST"])
79 def oof_optimizatio_result():
80     content = request.get_json()
81     callback_url = content['requestInfo']['callbackUrl']
82     transaction_id = content['requestInfo']['transactionId']
83     try:
84         task = threading.Thread(target=get_oof_async_response, args=(callback_url,transaction_id,))
85         task.daemon = True
86         task.start()
87     except:
88         print("Error: Unable to start thread")
89
90     data, status = get_oof_sync_response()
91
92     if not status:
93         return data, 202
94     return data, 503
95
96
97 app.run(host='0.0.0.0')
98 app.run(debug=True)