Functest scripts, simulators, and payloads
[optf/osdf.git] / test / functest / simulators / oof_dependencies_simulators.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2018 AT&T Intellectual Property
3 #
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
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16 # -------------------------------------------------------------------------
17 #
18
19 """
20 Simulators for dependencies of OSDF (e.g. HAS-API, Policy, SO-callback, etc.)
21 """
22 import glob
23
24 from osdf.utils.interfaces import json_from_file
25 from flask import Flask, jsonify, request
26
27 app = Flask(__name__)
28
29
30 @app.route("/simulated/ERROR/<component>", methods=["GET", "POST"])
31 @app.route("/simulated/unhealthy/<component>", methods=["GET", "POST"])
32 def error_for_component(component):
33     """Send an HTTP error for component"""
34     return jsonify({"error": "{} error".format(component)}), 503
35
36
37 @app.route("/simulated/healthy/<component>", methods=["GET", "POST"])
38 def healthy_status_for_component(component):
39     """Send a health-OK response for component"""
40     return jsonify({"success": "Passed Health Check for Component {}".format(component)})
41
42
43 @app.route("/simulated/success/<component>", methods=["GET", "POST"])
44 def successful_call_for_component(component):
45     """Send a message about successful call to component"""
46     return jsonify({"success": "made a call to Component: {}".format(component)})
47
48
49 @app.route("/simulated/oof/has-api/<path:mainpath>", methods=["GET", "POST"])
50 def has_api_calls(mainpath):
51     data, status = get_payload_for_simulated_component('has-api', mainpath)
52     if not status:
53         return jsonify(data)
54     return jsonify(data), 503
55
56
57 def get_payload_for_simulated_component(component, mainpath):
58     """
59     Get the payload for the given path for the given component
60     :param component: Component we are using (e.g. HAS-API, Policy, SO-callback, etc.)
61     :param mainpath: path within the URL (e.g. /main/X1/y1/)
62     :return: Content if file exists, or else 503 error
63     """
64     file_name = "{}/response-payloads/{}".format(component, mainpath)
65     data = json_from_file(file_name)
66     if not data:
67         return {"Error": "Unable to read File {}".format(file_name)}, 503
68     return data, None
69
70
71 @app.route("/simulated/policy/<sub_component>/getConfig", methods=["POST"])
72 def get_policies(sub_component):
73     """
74     Get all policies for this folder
75     :param sub_component: The folder we are interested in (e.g. "pdp-has-vcpe-good", "pdp-has-vcpe-bad")
76     :return: A list of policies as a json object (each element is one policy)
77     """
78     main_dir = "policy/response-payloads/" + sub_component
79     files = glob.glob("{}/*.json".format(main_dir))
80     return jsonify([json_from_file(x) for x in files])
81
82
83 if __name__ == "__main__":
84     app.run(debug=True)