3 from flask
import Flask
4 from flask_restful
import Api
6 # need to import total module to set its global variable dcs
8 from compute
import dcs
, ComputeList
, ComputeStart
, ComputeStatus
, ComputeStop
, DatacenterList
, DatacenterStatus
10 # need to import total module to set its global variable net
12 from network
import NetworkAction
15 from monitor
import MonitorInterfaceAction
, MonitorFlowAction
17 logging
.basicConfig(level
=logging
.INFO
)
21 class RestApiEndpoint(object):
24 Simple API endpoint that offers a REST
25 interface. This interface will be used by the
26 default command line client.
29 def __init__(self
, listenip
, port
):
34 self
.app
= Flask(__name__
)
35 self
.api
= Api(self
.app
)
38 self
.api
.add_resource(ComputeList
, "/restapi/compute/<dc_label>")
39 self
.api
.add_resource(ComputeStart
, "/restapi/compute/<dc_label>/<compute_name>/start")
40 self
.api
.add_resource(ComputeStop
, "/restapi/compute/<dc_label>/<compute_name>/stop")
41 self
.api
.add_resource(ComputeStatus
, "/restapi/compute/<dc_label>/<compute_name>")
42 self
.api
.add_resource(DatacenterList
, "/restapi/datacenter")
43 self
.api
.add_resource(DatacenterStatus
, "/restapi/datacenter/<dc_label>")
45 self
.api
.add_resource(NetworkAction
, "/restapi/network/<vnf_src_name>/<vnf_dst_name>")
47 self
.api
.add_resource(MonitorInterfaceAction
, "/restapi/monitor/<vnf_name>/<vnf_interface>/<metric>")
48 self
.api
.add_resource(MonitorFlowAction
, "/restapi/monitor/<vnf_name>/<vnf_interface>/<metric>/<cookie>")
50 logging
.debug("Created API endpoint %s(%s:%d)" % (self
.__class
__.__name
__, self
.ip
, self
.port
))
53 def connectDatacenter(self
, dc
):
54 compute
.dcs
[dc
.label
] = dc
55 logging
.info("Connected DC(%s) to API endpoint %s(%s:%d)" % (dc
.label
, self
.__class
__.__name
__, self
.ip
, self
.port
))
57 def connectDCNetwork(self
, DCnetwork
):
59 network
.net
= DCnetwork
60 monitor
.net
= DCnetwork
62 logging
.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
63 self
.__class
__.__name
__, self
.ip
, self
.port
))
66 thread
= threading
.Thread(target
= self
._start
_flask
, args
=())
69 logging
.info("Started API endpoint @ http://%s:%d" % (self
.ip
, self
.port
))
72 def _start_flask(self
):
73 self
.app
.run(self
.ip
, self
.port
, debug
=True, use_reloader
=False)