2 Copyright (c) 2017 SONATA-NFV and Paderborn University
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
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
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).
28 from flask
import Flask
, request
29 from flask_restful
import Api
, Resource
30 from gevent
.pywsgi
import WSGIServer
33 LOG
= logging
.getLogger("api.openstack.base")
36 class BaseOpenstackDummy(Resource
):
38 This class is the base class for all openstack entrypoints of son-emu.
41 def __init__(self
, listenip
, port
):
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'):
53 self
.app
= Flask(__name__
)
54 self
.api
= Api(self
.app
)
58 self
.http_server
.stop(timeout
=1.0)
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(
66 log
=open("/dev/null", "w") # don't show http logs
68 self
.http_server
.serve_forever(stop_timeout
=1.0)
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
,
78 logfile
.write(data
+ "\n")