1 # -------------------------------------------------------------------------
2 # Copyright (c) 2018 AT&T Intellectual Property
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # -------------------------------------------------------------------------
20 Simulators for dependencies of OSDF (e.g. HAS-API, Policy, SO-callback, etc.)
25 from flask import Flask, jsonify, request
27 from osdf.utils.interfaces import json_from_file
32 @app.route("/simulated/ERROR/<component>", methods=["GET", "POST"])
33 @app.route("/simulated/unhealthy/<component>", methods=["GET", "POST"])
34 def error_for_component(component):
35 """Send an HTTP error for component"""
36 return jsonify({"error": "{} error".format(component)}), 503
39 @app.route("/simulated/healthy/<component>", methods=["GET", "POST"])
40 def healthy_status_for_component(component):
41 """Send a health-OK response for component"""
42 return jsonify({"success": "Passed Health Check for Component {}".format(component)})
45 @app.route("/simulated/success/<component>", methods=["GET", "POST"])
46 def successful_call_for_component(component):
47 """Send a message about successful call to component"""
48 return jsonify({"success": "made a call to Component: {}".format(component)})
51 @app.route("/simulated/oof/has-api/<path:mainpath>", methods=["GET", "POST"])
52 def has_api_calls(mainpath):
53 data, status = get_payload_for_simulated_component('has-api', mainpath)
56 return jsonify(data), 503
59 def get_payload_for_simulated_component(component, mainpath):
61 Get the payload for the given path for the given component
62 :param component: Component we are using (e.g. HAS-API, Policy, SO-callback, etc.)
63 :param mainpath: path within the URL (e.g. /main/X1/y1/)
64 :return: Content if file exists, or else 503 error
66 file_name = "{}/response-payloads/{}".format(component, mainpath)
67 data = json_from_file(file_name)
69 return {"Error": "Unable to read File {}".format(file_name)}, 503
73 @app.route("/simulated/policy/<sub_component>/getConfig", methods=["POST"])
74 def get_policies(sub_component):
76 Get all policies for this folder
77 :param sub_component: The folder we are interested in (e.g. "pdp-has-vcpe-good", "pdp-has-vcpe-bad")
78 :return: A list of policies as a json object (each element is one policy)
80 main_dir = "policy/response-payloads/" + sub_component
81 files = glob.glob("{}/*.json".format(main_dir))
85 "policyConfigMessage": "Config Retrieved! ",
86 "policyConfigStatus": "CONFIG_RETRIEVED",
88 "config": json.dumps(json_from_file(x)),
89 "policyName": os.path.basename(x),
90 "policyType": "MicroService",
93 return jsonify(list_json)
96 @app.route("/simulated/configdb/getCellList/<network_id>/<ts>", methods=["GET"])
97 def get_cell_list(network_id, ts):
98 data, status = get_payload_for_simulated_component('configdb',
99 'getCellList-' + network_id + '.json')
102 return jsonify(data), 503
105 @app.route("/simulated/configdb/getNbrList/<cell_id>/<ts>", methods=["GET"])
106 def get_nbr_list(cell_id, ts):
107 data, status = get_payload_for_simulated_component('configdb', 'getNbrList-' + cell_id + '.json')
110 return jsonify(data), 503
113 if __name__ == "__main__":
114 app.run(debug=True, host='0.0.0.0')