From 9d980ce62d1f12d1e3fba48efb50398b8b95272a Mon Sep 17 00:00:00 2001 From: alex_sh Date: Wed, 23 Aug 2017 17:30:56 -0400 Subject: policy-handler seed code Change-Id: I35cd80b6e082f4b84740bab752774e8abc40ca35 Issue-Id: DCAEGEN2-46 Signed-off-by: Alex Shatov --- policyhandler/web_server.py | 104 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 policyhandler/web_server.py (limited to 'policyhandler/web_server.py') diff --git a/policyhandler/web_server.py b/policyhandler/web_server.py new file mode 100644 index 0000000..9a5ee19 --- /dev/null +++ b/policyhandler/web_server.py @@ -0,0 +1,104 @@ +"""web-service for policy_handler""" + +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2017 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. + +import logging +import json +from datetime import datetime +import cherrypy + +from .config import Config +from .onap.audit import Audit +from .policy_rest import PolicyRest +from .policy_engine import PolicyEngineClient + +class PolicyWeb(object): + """Main static class for REST API of policy-handler""" + logger = logging.getLogger("policy_handler.web_cherrypy") + + @staticmethod + def run(): + """run forever the web-server of the policy-handler""" + PolicyWeb.logger.info("policy_handler web-service at port(%d)...", \ + Config.wservice_port) + cherrypy.config.update({"server.socket_host": "0.0.0.0", \ + 'server.socket_port': Config.wservice_port}) + cherrypy.tree.mount(PolicyLatest(), '/policy_latest') + cherrypy.tree.mount(PoliciesLatest(), '/policies_latest') + cherrypy.tree.mount(PoliciesCatchUp(), '/catch_up') + cherrypy.quickstart(Shutdown(), '/shutdown') + +class Shutdown(object): + """Shutdown the policy-handler""" + @cherrypy.expose + def index(self): + """shutdown event""" + audit = Audit(req_message="get /shutdown", headers=cherrypy.request.headers) + PolicyWeb.logger.info("--------- stopping REST API of policy-handler -----------") + cherrypy.engine.exit() + PolicyEngineClient.shutdown(audit) + PolicyWeb.logger.info("--------- the end -----------") + res = str(datetime.now()) + audit.info_requested(res) + return "goodbye! shutdown requested {0}".format(res) + +class PoliciesLatest(object): + """REST API of the policy-hanlder""" + + @cherrypy.expose + @cherrypy.tools.json_out() + def index(self): + """find the latest policy by policy_id or all latest policies""" + audit = Audit(req_message="get /policies_latest", headers=cherrypy.request.headers) + res = PolicyRest.get_latest_policies(audit) or {} + PolicyWeb.logger.info("PoliciesLatest: %s", json.dumps(res)) + audit.audit_done(result=json.dumps(res)) + return res + +@cherrypy.popargs('policy_id') +class PolicyLatest(object): + """REST API of the policy-hanlder""" + + @cherrypy.expose + @cherrypy.tools.json_out() + def index(self, policy_id): + """find the latest policy by policy_id or all latest policies""" + audit = Audit(req_message="get /policy_latest/{0}".format(policy_id or ""), \ + headers=cherrypy.request.headers) + PolicyWeb.logger.info("PolicyLatest policy_id=%s headers=%s", \ + policy_id, json.dumps(cherrypy.request.headers)) + res = PolicyRest.get_latest_policy((audit, policy_id)) or {} + PolicyWeb.logger.info("PolicyLatest policy_id=%s res=%s", policy_id, json.dumps(res)) + audit.audit_done(result=json.dumps(res)) + return res + +class PoliciesCatchUp(object): + """catch up with all DCAE policies""" + @cherrypy.expose + @cherrypy.tools.json_out() + def index(self): + """catch up with all policies""" + started = str(datetime.now()) + audit = Audit(req_message="get /catch_up", headers=cherrypy.request.headers) + PolicyEngineClient.catch_up(audit) + res = {"catch-up requested": started} + PolicyWeb.logger.info("PoliciesCatchUp: %s", json.dumps(res)) + audit.info_requested(started) + return res -- cgit 1.2.3-korg