summaryrefslogtreecommitdiffstats
path: root/policyhandler/web_server.py
blob: 9a5ee192424c1c8435145e5dbb2d01c30a353616 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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