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.)
26 from flask import Flask, jsonify, request
28 from osdf.utils.interfaces import json_from_file
33 @app.route("/simulated/ERROR/<component>", methods=["GET", "POST"])
34 @app.route("/simulated/unhealthy/<component>", methods=["GET", "POST"])
35 def error_for_component(component):
36 """Send an HTTP error for component"""
37 return jsonify({"error": "{} error".format(component)}), 503
40 @app.route("/simulated/healthy/<component>", methods=["GET", "POST"])
41 def healthy_status_for_component(component):
42 """Send a health-OK response for component"""
43 return jsonify({"success": "Passed Health Check for Component {}".format(component)})
46 @app.route("/simulated/success/<component>", methods=["GET", "POST"])
47 def successful_call_for_component(component):
48 """Send a message about successful call to component"""
49 return jsonify({"success": "made a call to Component: {}".format(component)})
52 @app.route("/simulated/oof/has-api/<path:mainpath>", methods=["GET", "POST"])
53 def has_api_calls(mainpath):
54 data, status = get_payload_for_simulated_component('has-api', mainpath)
57 return jsonify(data), 503
60 def get_payload_for_simulated_component(component, mainpath):
62 Get the payload for the given path for the given component
63 :param component: Component we are using (e.g. HAS-API, Policy, SO-callback, etc.)
64 :param mainpath: path within the URL (e.g. /main/X1/y1/)
65 :return: Content if file exists, or else 503 error
67 file_name = "{}/response-payloads/{}".format(component, mainpath)
68 data = json_from_file(file_name)
70 return {"Error": "Unable to read File {}".format(file_name)}, 503
74 @app.route("/simulated/policy/<sub_component>/getConfig", methods=["POST"])
75 def get_policies(sub_component):
77 Get all policies for this folder
78 :param sub_component: The folder we are interested in (e.g. "pdp-has-vcpe-good", "pdp-has-vcpe-bad")
79 :return: A list of policies as a json object (each element is one policy)
81 main_dir = "policy/response-payloads/" + sub_component
82 files = glob.glob("{}/*.json".format(main_dir))
86 "policyConfigMessage": "Config Retrieved! ",
87 "policyConfigStatus": "CONFIG_RETRIEVED",
89 "config": json.dumps(json_from_file(x)),
90 "policyName": os.path.basename(x),
91 "policyType": "MicroService",
94 return jsonify(list_json)
97 @app.route("/simulated/policy/pdpx/decision/v1", methods=["POST"])
98 def get_pdx_policies():
102 return jsonify(json_from_file("policy/response-payloads/policy_response.json"))
105 @app.route("/simulated/configdb/getCellList/<network_id>/<ts>", methods=["GET"])
106 def get_cell_list(network_id, ts):
107 data, status = get_payload_for_simulated_component('configdb',
108 'getCellList-' + network_id + '.json')
111 return jsonify(data), 503
114 @app.route("/simulated/configdb/getNbrList/<cell_id>/<ts>", methods=["GET"])
115 def get_nbr_list(cell_id, ts):
116 data, status = get_payload_for_simulated_component('configdb', 'getNbrList-' + cell_id + '.json')
119 return jsonify(data), 503
121 @app.route("/simulated/aai/v23/nodes/service-instances/service-instance/<service_id>", methods=["GET"])
122 def get_aai_instances(service_id):
123 data, status = get_payload_for_simulated_component('aai', 'nsi_instance.json')
126 return jsonify(data), 503
128 @app.route("/simulated/aai/v23/dsl", methods=["PUT"])
131 nssi_query = {"dsl": "service-instance*('service-instance-id','9629e36c-a3d9-4aed-8368-f72b8be1cd34')('workload-context', 'CN') > "
132 "service-instance*('service-role','nsi')"}
134 nssi_with_nsi_query = {"dsl": "service-instance*('service-instance-id','9629e36c-a3d9-4aed-8368-f72b8be1cd34')('workload-context', 'CN') > "
135 "service-instance*('service-role','nsi')('service-instance-id',"
136 "'660ca85c-1a0f-4521-a559-65f23e794699')"}
139 "nssi_query": nssi_query,
140 "nssi_with_nsi": nssi_with_nsi_query
148 request_body = request.get_json()
150 for query_type, query in queries.items():
151 if request_body == query:
152 service_count = count[query_type]
153 return {'results': [{'service-instance': service_count}]}
156 if __name__ == "__main__":
157 app.run(debug=True, host='0.0.0.0')