93ad2bd0106a9c19afec48305d98f20d1e551607
[osm/vim-emu.git] / src / emuvim / api / openstack / openstack_dummies / base_openstack_dummy.py
1 """
2 Copyright (c) 2017 SONATA-NFV and Paderborn University
3 ALL RIGHTS RESERVED.
4
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
17 Neither the name of the SONATA-NFV, Paderborn University
18 nor the names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior written
20 permission.
21
22 This work has been performed in the framework of the SONATA project,
23 funded by the European Commission under Grant number 671517 through
24 the Horizon 2020 and 5G-PPP programmes. The authors would like to
25 acknowledge the contributions of their colleagues of the SONATA
26 partner consortium (www.sonata-nfv.eu).
27 """
28 from flask import Flask, request
29 from flask_restful import Api, Resource
30 from gevent.pywsgi import WSGIServer
31 import logging
32
33 LOG = logging.getLogger("api.openstack.base")
34
35
36 class BaseOpenstackDummy(Resource):
37 """
38 This class is the base class for all openstack entrypoints of son-emu.
39 """
40
41 def __init__(self, listenip, port):
42 self.ip = listenip
43 self.port = port
44 self.compute = None
45 self.manage = None
46 self.http_server = None
47 self.server_thread = None
48 self.playbook_file = '/tmp/son-emu-requests.log'
49 with open(self.playbook_file, 'w'):
50 pass
51
52 # setup Flask
53 self.app = Flask(__name__)
54 self.api = Api(self.app)
55
56 def stop(self):
57 if self.http_server:
58 self.http_server.stop(timeout=1.0)
59
60 def _start_flask(self):
61 LOG.info("Starting %s endpoint @ http://%s:%d" % (
62 self.__class__.__name__, self.ip, self.port))
63 self.http_server = WSGIServer(
64 (self.ip, self.port),
65 self.app,
66 log=open("/dev/null", "w") # don't show http logs
67 )
68 self.http_server.serve_forever(stop_timeout=1.0)
69
70 def dump_playbook(self):
71 with self.manage.lock:
72 with open(self.playbook_file, 'a') as logfile:
73 if len(request.data) > 0:
74 data = "# %s API\n" % str(self.__class__).split('.')[-1].rstrip('\'>')
75 data += "curl -X {type} -H \"Content-type: application/json\" -d '{data}' {url}".format(type=request.method,
76 data=request.data,
77 url=request.url)
78 logfile.write(data + "\n")