From 2322ef8736e839d62930d9b6c847ce818261c26c Mon Sep 17 00:00:00 2001 From: Alex Shatov Date: Thu, 25 Jan 2018 17:40:39 -0500 Subject: on not finding the policy return 404 and json * json of what is returned from policy-engine instead of 404 html Change-Id: I44fb7e113e23f68cac44e6ad21c1bc9b37cf3d58 Issue-ID: DCAEGEN2-249 Signed-off-by: Alex Shatov --- policyhandler/policy_rest.py | 29 +++++++++++++++++++++++++---- policyhandler/web_server.py | 26 +++++++++++++++----------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/policyhandler/policy_rest.py b/policyhandler/policy_rest.py index 57dcfae..800c564 100644 --- a/policyhandler/policy_rest.py +++ b/policyhandler/policy_rest.py @@ -1,6 +1,6 @@ # org.onap.dcae # ================================================================================ -# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -38,8 +38,11 @@ class PolicyRest(object): POLICY_GET_CONFIG = 'getConfig' POLICY_CONFIG_STATUS = "policyConfigStatus" CONFIG_RETRIEVED = "CONFIG_RETRIEVED" + CONFIG_NOT_FOUND = "CONFIG_NOT_FOUND" POLICY_CONFIG_MESSAGE = "policyConfigMessage" NO_RESPONSE_RECEIVED = "No Response Received" + POLICY_ENGINE_STATUS_CODE_ERROR = 400 + PE_DATA_NOT_FOUND = "PE300 - Data Issue: Incorrect Params passed: Decision not a Permit." MIN_VERSION_EXPECTED = "min_version_expected" IGNORE_POLICY_NAMES = "ignore_policy_names" @@ -131,10 +134,10 @@ class PolicyRest(object): res_data = res.json() if res_data and isinstance(res_data, list) and len(res_data) == 1: - result = res_data[0] - if result and not result.get(POLICY_NAME): + rslt = res_data[0] + if rslt and not rslt.get(POLICY_NAME): res_data = None - if result.get(PolicyRest.POLICY_CONFIG_MESSAGE) == PolicyRest.NO_RESPONSE_RECEIVED: + if rslt.get(PolicyRest.POLICY_CONFIG_MESSAGE) == PolicyRest.NO_RESPONSE_RECEIVED: error_code = AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value error_msg = "unexpected {0}".format(log_line) @@ -144,6 +147,24 @@ class PolicyRest(object): sub_aud.metrics(error_msg) return (error_code, None) + elif res.status_code == PolicyRest.POLICY_ENGINE_STATUS_CODE_ERROR: + try: + rslt = res.json() + if rslt and isinstance(rslt, list) and len(rslt) == 1: + rslt = rslt[0] + if rslt and not rslt.get(POLICY_NAME) \ + and rslt.get(PolicyRest.POLICY_CONFIG_STATUS) == PolicyRest.CONFIG_NOT_FOUND \ + and rslt.get(PolicyRest.POLICY_CONFIG_MESSAGE) == PolicyRest.PE_DATA_NOT_FOUND: + status_code = AuditHttpCode.DATA_NOT_FOUND_ERROR.value + info_msg = "not found {0}".format(log_line) + + PolicyRest._logger.info(info_msg) + sub_aud.set_http_status_code(status_code) + sub_aud.metrics(info_msg) + return (status_code, None) + except ValueError: + pass + sub_aud.set_http_status_code(res.status_code) sub_aud.metrics(log_line) PolicyRest._logger.info(log_line) diff --git a/policyhandler/web_server.py b/policyhandler/web_server.py index 4a6dee2..5b3227d 100644 --- a/policyhandler/web_server.py +++ b/policyhandler/web_server.py @@ -1,6 +1,6 @@ # org.onap.dcae # ================================================================================ -# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -61,14 +61,16 @@ class _PolicyWeb(object): PolicyWeb.logger.info("%s policy_id=%s headers=%s", \ req_info, policy_id, json.dumps(cherrypy.request.headers)) - res = PolicyRest.get_latest_policy((audit, policy_id, None, None)) or {} + latest_policy = PolicyRest.get_latest_policy((audit, policy_id, None, None)) or {} - PolicyWeb.logger.info("res %s policy_id=%s res=%s", req_info, policy_id, json.dumps(res)) + PolicyWeb.logger.info("res %s policy_id=%s latest_policy=%s", + req_info, policy_id, json.dumps(latest_policy)) - success, http_status_code, response_description = audit.audit_done(result=json.dumps(res)) + success, http_status_code, _ = audit.audit_done(result=json.dumps(latest_policy)) if not success: - raise cherrypy.HTTPError(http_status_code, response_description) - return res + cherrypy.response.status = http_status_code + + return latest_policy def _get_all_policies_latest(self): """retireves all the latest policies on GET /policies_latest""" @@ -82,9 +84,10 @@ class _PolicyWeb(object): res = {"valid_policies": valid_policies, "errored_policies": errored_policies} PolicyWeb.logger.info("result %s: %s", req_info, json.dumps(res)) - success, http_status_code, response_description = audit.audit_done(result=json.dumps(res)) + success, http_status_code, _ = audit.audit_done(result=json.dumps(res)) if not success: - raise cherrypy.HTTPError(http_status_code, response_description) + cherrypy.response.status = http_status_code + return res @cherrypy.expose @@ -127,7 +130,7 @@ class _PolicyWeb(object): "property": null, "config": { "foo": "bar", - "foo_updated": "2017-10-06T16:54:31.696Z" + "foo_updated": "2018-10-06T16:54:31.696Z" }, "policyVersion": "3" }, @@ -155,9 +158,10 @@ class _PolicyWeb(object): PolicyWeb.logger.info("result %s: policy_filter=%s res=%s", \ req_info, str_policy_filter, json.dumps(res)) - success, http_status_code, response_description = audit.audit_done(result=json.dumps(res)) + success, http_status_code, _ = audit.audit_done(result=json.dumps(res)) if not success: - raise cherrypy.HTTPError(http_status_code, response_description) + cherrypy.response.status = http_status_code + return res @cherrypy.expose -- cgit 1.2.3-korg