alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 1 | # org.onap.dcae |
| 2 | # ================================================================================ |
Alex Shatov | 2322ef8 | 2018-01-25 17:40:39 -0500 | [diff] [blame] | 3 | # Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 4 | # ================================================================================ |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # ============LICENSE_END========================================================= |
| 17 | # |
| 18 | # ECOMP is a trademark and service mark of AT&T Intellectual Property. |
| 19 | |
Alex Shatov | f53e5e7 | 2018-01-11 11:15:56 -0500 | [diff] [blame] | 20 | """web-service for policy_handler""" |
| 21 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 22 | import logging |
| 23 | import json |
| 24 | from datetime import datetime |
| 25 | import cherrypy |
| 26 | |
| 27 | from .config import Config |
| 28 | from .onap.audit import Audit |
| 29 | from .policy_rest import PolicyRest |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 30 | from .policy_receiver import PolicyReceiver |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 31 | |
| 32 | class PolicyWeb(object): |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 33 | """run REST API of policy-handler""" |
| 34 | logger = logging.getLogger("policy_handler.policy_web") |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 35 | |
| 36 | @staticmethod |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 37 | def run_forever(audit): |
| 38 | """run the web-server of the policy-handler forever""" |
| 39 | PolicyWeb.logger.info("policy_handler web-service at port(%d)...", Config.wservice_port) |
| 40 | cherrypy.config.update({"server.socket_host": "0.0.0.0", |
| 41 | 'server.socket_port': Config.wservice_port}) |
| 42 | cherrypy.tree.mount(_PolicyWeb(), '/') |
| 43 | audit.info("running policy_handler web-service at port({0})".format(Config.wservice_port)) |
| 44 | cherrypy.engine.start() |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 45 | |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 46 | class _PolicyWeb(object): |
| 47 | """REST API of policy-handler""" |
| 48 | |
| 49 | @staticmethod |
| 50 | def _get_request_info(request): |
| 51 | """returns info about the http request""" |
| 52 | return "{0} {1}{2}".format(request.method, request.script_name, request.path_info) |
| 53 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 54 | @cherrypy.expose |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 55 | @cherrypy.popargs('policy_id') |
| 56 | @cherrypy.tools.json_out() |
| 57 | def policy_latest(self, policy_id): |
| 58 | """retireves the latest policy identified by policy_id""" |
| 59 | req_info = _PolicyWeb._get_request_info(cherrypy.request) |
| 60 | audit = Audit(req_message=req_info, headers=cherrypy.request.headers) |
| 61 | PolicyWeb.logger.info("%s policy_id=%s headers=%s", \ |
| 62 | req_info, policy_id, json.dumps(cherrypy.request.headers)) |
| 63 | |
Alex Shatov | 2322ef8 | 2018-01-25 17:40:39 -0500 | [diff] [blame] | 64 | latest_policy = PolicyRest.get_latest_policy((audit, policy_id, None, None)) or {} |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 65 | |
Alex Shatov | 2322ef8 | 2018-01-25 17:40:39 -0500 | [diff] [blame] | 66 | PolicyWeb.logger.info("res %s policy_id=%s latest_policy=%s", |
| 67 | req_info, policy_id, json.dumps(latest_policy)) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 68 | |
Alex Shatov | 2322ef8 | 2018-01-25 17:40:39 -0500 | [diff] [blame] | 69 | success, http_status_code, _ = audit.audit_done(result=json.dumps(latest_policy)) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 70 | if not success: |
Alex Shatov | 2322ef8 | 2018-01-25 17:40:39 -0500 | [diff] [blame] | 71 | cherrypy.response.status = http_status_code |
| 72 | |
| 73 | return latest_policy |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 74 | |
| 75 | def _get_all_policies_latest(self): |
| 76 | """retireves all the latest policies on GET /policies_latest""" |
| 77 | req_info = _PolicyWeb._get_request_info(cherrypy.request) |
| 78 | audit = Audit(req_message=req_info, headers=cherrypy.request.headers) |
| 79 | |
| 80 | PolicyWeb.logger.info("%s", req_info) |
| 81 | |
Alex Shatov | ac779d3 | 2018-02-01 14:16:56 -0500 | [diff] [blame^] | 82 | result = PolicyRest.get_latest_policies(audit) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 83 | |
Alex Shatov | ac779d3 | 2018-02-01 14:16:56 -0500 | [diff] [blame^] | 84 | PolicyWeb.logger.info("result %s: %s", req_info, json.dumps(result)) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 85 | |
Alex Shatov | ac779d3 | 2018-02-01 14:16:56 -0500 | [diff] [blame^] | 86 | success, http_status_code, _ = audit.audit_done(result=json.dumps(result)) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 87 | if not success: |
Alex Shatov | 2322ef8 | 2018-01-25 17:40:39 -0500 | [diff] [blame] | 88 | cherrypy.response.status = http_status_code |
| 89 | |
Alex Shatov | ac779d3 | 2018-02-01 14:16:56 -0500 | [diff] [blame^] | 90 | return result |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 91 | |
| 92 | @cherrypy.expose |
| 93 | @cherrypy.tools.json_out() |
| 94 | @cherrypy.tools.json_in() |
| 95 | def policies_latest(self): |
| 96 | """ |
| 97 | on :GET: retrieves all the latest policies from policy-engine that are |
| 98 | in the scope of the policy-handler. |
| 99 | |
| 100 | on :POST: expects to receive the params that mimic the /getConfig of policy-engine |
| 101 | and retrieves the matching policies from policy-engine and picks the latest on each policy. |
| 102 | |
| 103 | sample request - policies filter |
| 104 | |
| 105 | { |
| 106 | "configAttributes": { "key1":"value1" }, |
| 107 | "configName": "alex_config_name", |
| 108 | "ecompName": "DCAE", |
| 109 | "policyName": "DCAE_alex.Config_alex_.*", |
| 110 | "unique": false |
| 111 | } |
| 112 | |
| 113 | sample response |
| 114 | |
| 115 | { |
| 116 | "DCAE_alex.Config_alex_priority": { |
| 117 | "policy_body": { |
| 118 | "policyName": "DCAE_alex.Config_alex_priority.3.xml", |
| 119 | "policyConfigMessage": "Config Retrieved! ", |
| 120 | "responseAttributes": {}, |
| 121 | "policyConfigStatus": "CONFIG_RETRIEVED", |
| 122 | "type": "JSON", |
| 123 | "matchingConditions": { |
| 124 | "priority": "10", |
| 125 | "key1": "value1", |
| 126 | "ECOMPName": "DCAE", |
| 127 | "ConfigName": "alex_config_name" |
| 128 | }, |
| 129 | "property": null, |
| 130 | "config": { |
| 131 | "foo": "bar", |
Alex Shatov | 2322ef8 | 2018-01-25 17:40:39 -0500 | [diff] [blame] | 132 | "foo_updated": "2018-10-06T16:54:31.696Z" |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 133 | }, |
| 134 | "policyVersion": "3" |
| 135 | }, |
| 136 | "policy_id": "DCAE_alex.Config_alex_priority" |
| 137 | } |
| 138 | } |
| 139 | """ |
| 140 | if cherrypy.request.method == "GET": |
| 141 | return self._get_all_policies_latest() |
| 142 | |
| 143 | if cherrypy.request.method != "POST": |
| 144 | raise cherrypy.HTTPError(404, "unexpected method {0}".format(cherrypy.request.method)) |
| 145 | |
| 146 | policy_filter = cherrypy.request.json or {} |
| 147 | str_policy_filter = json.dumps(policy_filter) |
| 148 | |
| 149 | req_info = _PolicyWeb._get_request_info(cherrypy.request) |
| 150 | audit = Audit(req_message="{0}: {1}".format(req_info, str_policy_filter), \ |
| 151 | headers=cherrypy.request.headers) |
| 152 | PolicyWeb.logger.info("%s: policy_filter=%s headers=%s", \ |
| 153 | req_info, str_policy_filter, json.dumps(cherrypy.request.headers)) |
| 154 | |
Alex Shatov | ac779d3 | 2018-02-01 14:16:56 -0500 | [diff] [blame^] | 155 | result = PolicyRest.get_latest_policies(audit, policy_filter=policy_filter) or {} |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 156 | |
Alex Shatov | ac779d3 | 2018-02-01 14:16:56 -0500 | [diff] [blame^] | 157 | PolicyWeb.logger.info("result %s: policy_filter=%s result=%s", \ |
| 158 | req_info, str_policy_filter, json.dumps(result)) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 159 | |
Alex Shatov | ac779d3 | 2018-02-01 14:16:56 -0500 | [diff] [blame^] | 160 | success, http_status_code, _ = audit.audit_done(result=json.dumps(result)) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 161 | if not success: |
Alex Shatov | 2322ef8 | 2018-01-25 17:40:39 -0500 | [diff] [blame] | 162 | cherrypy.response.status = http_status_code |
| 163 | |
Alex Shatov | ac779d3 | 2018-02-01 14:16:56 -0500 | [diff] [blame^] | 164 | return result |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 165 | |
| 166 | @cherrypy.expose |
| 167 | @cherrypy.tools.json_out() |
| 168 | def catch_up(self): |
| 169 | """catch up with all DCAE policies""" |
| 170 | started = str(datetime.now()) |
| 171 | req_info = _PolicyWeb._get_request_info(cherrypy.request) |
| 172 | audit = Audit(req_message=req_info, headers=cherrypy.request.headers) |
| 173 | |
| 174 | PolicyWeb.logger.info("%s", req_info) |
| 175 | PolicyReceiver.catch_up(audit) |
| 176 | |
| 177 | res = {"catch-up requested": started} |
| 178 | PolicyWeb.logger.info("requested %s: %s", req_info, json.dumps(res)) |
| 179 | audit.info_requested(started) |
| 180 | return res |
| 181 | |
| 182 | @cherrypy.expose |
| 183 | def shutdown(self): |
| 184 | """Shutdown the policy-handler""" |
| 185 | req_info = _PolicyWeb._get_request_info(cherrypy.request) |
| 186 | audit = Audit(req_message=req_info, headers=cherrypy.request.headers) |
| 187 | |
| 188 | PolicyWeb.logger.info("%s: --- stopping REST API of policy-handler ---", req_info) |
| 189 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 190 | cherrypy.engine.exit() |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 191 | |
| 192 | PolicyReceiver.shutdown(audit) |
| 193 | |
| 194 | health = json.dumps(Audit.health()) |
| 195 | audit.info("policy_handler health: {0}".format(health)) |
| 196 | PolicyWeb.logger.info("policy_handler health: %s", health) |
| 197 | PolicyWeb.logger.info("%s: --------- the end -----------", req_info) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 198 | res = str(datetime.now()) |
| 199 | audit.info_requested(res) |
| 200 | return "goodbye! shutdown requested {0}".format(res) |
| 201 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 202 | @cherrypy.expose |
| 203 | @cherrypy.tools.json_out() |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 204 | def healthcheck(self): |
| 205 | """returns the healthcheck results""" |
| 206 | req_info = _PolicyWeb._get_request_info(cherrypy.request) |
| 207 | audit = Audit(req_message=req_info, headers=cherrypy.request.headers) |
| 208 | |
| 209 | PolicyWeb.logger.info("%s", req_info) |
| 210 | |
| 211 | res = Audit.health() |
| 212 | |
| 213 | PolicyWeb.logger.info("healthcheck %s: res=%s", req_info, json.dumps(res)) |
| 214 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 215 | audit.audit_done(result=json.dumps(res)) |
| 216 | return res |