Merge "set marketplace port to 8080 for NFVO"
[functest.git] / functest / api / server.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """
11 Used to launch Functest RestApi
12
13 """
14
15 import inspect
16 import logging
17 import socket
18 from urlparse import urljoin
19 import pkg_resources
20
21 from flask import Flask
22 from flask_restful import Api
23
24 from functest.api.base import ApiResource
25 from functest.api.common import api_utils
26 from functest.api.database.db import BASE
27 from functest.api.database.db import DB_SESSION
28 from functest.api.database.db import ENGINE
29 from functest.api.database.v1 import models
30 from functest.api.urls import URLPATTERNS
31
32
33 LOGGER = logging.getLogger(__name__)
34
35 APP = Flask(__name__)
36 API = Api(APP)
37
38
39 @APP.teardown_request
40 def shutdown_session(exception=None):  # pylint: disable=unused-argument
41     """
42     To be called at the end of each request whether it is successful
43     or an exception is raised
44     """
45     DB_SESSION.remove()
46
47
48 def get_resource(resource_name):
49     """ Obtain the required resource according to resource name """
50     name = ''.join(resource_name.split('_'))
51     return next((r for r in api_utils.itersubclasses(ApiResource)
52                  if r.__name__.lower() == name))
53
54
55 def get_endpoint(url):
56     """ Obtain the endpoint of url """
57     address = socket.gethostbyname(socket.gethostname())
58     return urljoin('http://{}:5000'.format(address), url)
59
60
61 def api_add_resource():
62     """
63     The resource has multiple URLs and you can pass multiple URLs to the
64     add_resource() method on the Api object. Each one will be routed to
65     your Resource
66     """
67     for url_pattern in URLPATTERNS:
68         try:
69             API.add_resource(
70                 get_resource(url_pattern.target), url_pattern.url,
71                 endpoint=get_endpoint(url_pattern.url))
72         except StopIteration:
73             LOGGER.error('url resource not found: %s', url_pattern.url)
74
75
76 def init_db():
77     """
78     Import all modules here that might define models so that
79     they will be registered properly on the metadata, and then
80     create a database
81     """
82     def func(subcls):
83         """ To check the subclasses of BASE"""
84         try:
85             if issubclass(subcls[1], BASE):
86                 return True
87         except TypeError:
88             pass
89         return False
90     # pylint: disable=bad-builtin
91     subclses = filter(func, inspect.getmembers(models, inspect.isclass))
92     LOGGER.debug('Import models: %s', [subcls[1] for subcls in subclses])
93     BASE.metadata.create_all(bind=ENGINE)
94
95
96 def main():
97     """Entry point"""
98     logging.config.fileConfig(pkg_resources.resource_filename(
99         'functest', 'ci/logging.ini'))
100     LOGGER.info('Starting Functest server')
101     api_add_resource()
102     init_db()
103     APP.run(host='0.0.0.0')