Merge "KVMFORNFV: Implementing test suites for kvmfornfv ovs+dpdk+barometer scenarios."
[yardstick.git] / api / server.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
10
11 import inspect
12 import logging
13 from six.moves import filter
14
15 from flasgger import Swagger
16 from flask import Flask
17 from flask_restful import Api
18
19 from api.database import Base
20 from api.database import db_session
21 from api.database import engine
22 from api.database import models
23 from api.urls import urlpatterns
24 from yardstick import _init_logging
25
26 logger = logging.getLogger(__name__)
27
28 app = Flask(__name__)
29
30 Swagger(app)
31
32 api = Api(app)
33
34
35 @app.teardown_request
36 def shutdown_session(exception=None):
37     db_session.remove()
38
39
40 for u in urlpatterns:
41     api.add_resource(u.resource, u.url, endpoint=u.endpoint)
42
43
44 def init_db():
45     def func(a):
46         try:
47             if issubclass(a[1], Base):
48                 return True
49         except TypeError:
50             pass
51         return False
52
53     subclses = filter(func, inspect.getmembers(models, inspect.isclass))
54     logger.debug('Import models: %s', [a[1] for a in subclses])
55     Base.metadata.create_all(bind=engine)
56
57
58 def app_wrapper(*args, **kwargs):
59     init_db()
60     return app(*args, **kwargs)
61
62
63 if __name__ == '__main__':
64     _init_logging()
65     logger.setLevel(logging.DEBUG)
66     logger.info('Starting server')
67     init_db()
68     app.run(host='0.0.0.0')