Adding policy manager and a1 sdnc vth
[it/otf.git] / a1-sdnc-vth / app / helpers / action_helper.py
diff --git a/a1-sdnc-vth/app/helpers/action_helper.py b/a1-sdnc-vth/app/helpers/action_helper.py
new file mode 100644 (file)
index 0000000..a4f7a3a
--- /dev/null
@@ -0,0 +1,42 @@
+import json\r
+import ast\r
+from app.helpers import response_helper as ResponseHelper\r
+from flask import current_app, jsonify\r
+from app.errors.bad_request_exception import BadRequestException\r
+import requests\r
+\r
+\r
+def execute_action(request, response_dict, config):\r
+    headers = ResponseHelper.create_headers();\r
+    request_data = request.json\r
+    action_request = request_data.get("action").lower()\r
+    \r
+    creds = ResponseHelper.get_credentials(request_data, config)\r
+    proxies = ResponseHelper.get_proxies(config)\r
+    url = ResponseHelper.create_url(config=config, uri_path="/restconf/operations/A1-ADAPTER-API:"+action_request)\r
+#    ret_url = request.args.get('retURL')\r
+\r
+    json_req = ast.literal_eval(request_data["action_data"]["jsonBody"])\r
+    current_app.logger.info("Requesting Url: {}, body: {}, auth: {}, proxies: {}".format(url, json_req, creds, proxies))\r
+    try:\r
+        res = requests.post(url, proxies=proxies, auth=creds, headers=headers, json=json_req)\r
+        response = {\r
+                "status_code":res.status_code,\r
+                "result": res.json()\r
+                }\r
+    except(json.decoder.JSONDecodeError):\r
+        response = {\r
+                "status_code":res.status_code,\r
+                "result": res.reason\r
+                }\r
+    except requests.exceptions.RequestException:\r
+        response = {\r
+                "status_code":504,\r
+                "result": "Something Happned"\r
+                }\r
+    finally:\r
+        response_dict['vthResponse']['resultData'] = response\r
+#        if ret_url is not None:\r
+#            ResponseHelper.sendCallback(ret_url,response_dict)\r
+#            return '',200\r
+        return response_dict\r