blob: 17c06b4020e743f31ef9d92aaf7526ec6a37982f [file] [log] [blame]
alex_sh9d980ce2017-08-23 17:30:56 -04001# org.onap.dcae
2# ================================================================================
Alex Shatov2322ef82018-01-25 17:40:39 -05003# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
alex_sh9d980ce2017-08-23 17:30:56 -04004# ================================================================================
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 Shatovf53e5e72018-01-11 11:15:56 -050020"""web-service for policy_handler"""
21
alex_sh9d980ce2017-08-23 17:30:56 -040022import logging
23import json
24from datetime import datetime
25import cherrypy
26
27from .config import Config
28from .onap.audit import Audit
29from .policy_rest import PolicyRest
Alex Shatov1369bea2018-01-10 11:00:50 -050030from .policy_receiver import PolicyReceiver
alex_sh9d980ce2017-08-23 17:30:56 -040031
32class PolicyWeb(object):
Alex Shatov1369bea2018-01-10 11:00:50 -050033 """run REST API of policy-handler"""
34 logger = logging.getLogger("policy_handler.policy_web")
alex_sh9d980ce2017-08-23 17:30:56 -040035
36 @staticmethod
Alex Shatov1369bea2018-01-10 11:00:50 -050037 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_sh9d980ce2017-08-23 17:30:56 -040045
Alex Shatov1369bea2018-01-10 11:00:50 -050046class _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_sh9d980ce2017-08-23 17:30:56 -040054 @cherrypy.expose
Alex Shatov1369bea2018-01-10 11:00:50 -050055 @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 Shatov2322ef82018-01-25 17:40:39 -050064 latest_policy = PolicyRest.get_latest_policy((audit, policy_id, None, None)) or {}
Alex Shatov1369bea2018-01-10 11:00:50 -050065
Alex Shatov2322ef82018-01-25 17:40:39 -050066 PolicyWeb.logger.info("res %s policy_id=%s latest_policy=%s",
67 req_info, policy_id, json.dumps(latest_policy))
Alex Shatov1369bea2018-01-10 11:00:50 -050068
Alex Shatov2322ef82018-01-25 17:40:39 -050069 success, http_status_code, _ = audit.audit_done(result=json.dumps(latest_policy))
Alex Shatov1369bea2018-01-10 11:00:50 -050070 if not success:
Alex Shatov2322ef82018-01-25 17:40:39 -050071 cherrypy.response.status = http_status_code
72
73 return latest_policy
Alex Shatov1369bea2018-01-10 11:00:50 -050074
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 Shatovac779d32018-02-01 14:16:56 -050082 result = PolicyRest.get_latest_policies(audit)
Alex Shatov1369bea2018-01-10 11:00:50 -050083
Alex Shatovac779d32018-02-01 14:16:56 -050084 PolicyWeb.logger.info("result %s: %s", req_info, json.dumps(result))
Alex Shatov1369bea2018-01-10 11:00:50 -050085
Alex Shatovac779d32018-02-01 14:16:56 -050086 success, http_status_code, _ = audit.audit_done(result=json.dumps(result))
Alex Shatov1369bea2018-01-10 11:00:50 -050087 if not success:
Alex Shatov2322ef82018-01-25 17:40:39 -050088 cherrypy.response.status = http_status_code
89
Alex Shatovac779d32018-02-01 14:16:56 -050090 return result
Alex Shatov1369bea2018-01-10 11:00:50 -050091
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 Shatov2322ef82018-01-25 17:40:39 -0500132 "foo_updated": "2018-10-06T16:54:31.696Z"
Alex Shatov1369bea2018-01-10 11:00:50 -0500133 },
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 Shatovac779d32018-02-01 14:16:56 -0500155 result = PolicyRest.get_latest_policies(audit, policy_filter=policy_filter) or {}
Alex Shatov1369bea2018-01-10 11:00:50 -0500156
Alex Shatovac779d32018-02-01 14:16:56 -0500157 PolicyWeb.logger.info("result %s: policy_filter=%s result=%s", \
158 req_info, str_policy_filter, json.dumps(result))
Alex Shatov1369bea2018-01-10 11:00:50 -0500159
Alex Shatovac779d32018-02-01 14:16:56 -0500160 success, http_status_code, _ = audit.audit_done(result=json.dumps(result))
Alex Shatov1369bea2018-01-10 11:00:50 -0500161 if not success:
Alex Shatov2322ef82018-01-25 17:40:39 -0500162 cherrypy.response.status = http_status_code
163
Alex Shatovac779d32018-02-01 14:16:56 -0500164 return result
Alex Shatov1369bea2018-01-10 11:00:50 -0500165
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_sh9d980ce2017-08-23 17:30:56 -0400190 cherrypy.engine.exit()
Alex Shatov1369bea2018-01-10 11:00:50 -0500191
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_sh9d980ce2017-08-23 17:30:56 -0400198 res = str(datetime.now())
199 audit.info_requested(res)
200 return "goodbye! shutdown requested {0}".format(res)
201
alex_sh9d980ce2017-08-23 17:30:56 -0400202 @cherrypy.expose
203 @cherrypy.tools.json_out()
Alex Shatov1369bea2018-01-10 11:00:50 -0500204 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_sh9d980ce2017-08-23 17:30:56 -0400215 audit.audit_done(result=json.dumps(res))
216 return res