d3c2483ac774d3999958ffb0b30e4f49fedf5ee8
[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 import logging
31
32 LOG = logging.getLogger("api.openstack.base")
33
34
35 class BaseOpenstackDummy(Resource):
36 """
37 This class is the base class for all openstack entrypoints of son-emu.
38 """
39
40 def __init__(self, listenip, port):
41 self.ip = listenip
42 self.port = port
43 self.compute = None
44 self.manage = None
45 self.playbook_file = '/tmp/son-emu-requests.log'
46 with open(self.playbook_file, 'w'):
47 pass
48
49 # setup Flask
50 self.app = Flask(__name__)
51 self.api = Api(self.app)
52
53 def _start_flask(self):
54 LOG.info("Starting %s endpoint @ http://%s:%d" % (__name__, self.ip, self.port))
55 if self.app is not None:
56 self.app.before_request(self.dump_playbook)
57 self.app.run(self.ip, self.port, debug=True, use_reloader=False)
58
59 def dump_playbook(self):
60 with self.manage.lock:
61 with open(self.playbook_file, 'a') as logfile:
62 if len(request.data) > 0:
63 data = "# %s API\n" % str(self.__class__).split('.')[-1].rstrip('\'>')
64 data += "curl -X {type} -H \"Content-type: application/json\" -d '{data}' {url}".format(type=request.method,
65 data=request.data,
66 url=request.url)
67 logfile.write(data + "\n")