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