2 Copyright (c) 2015 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 [, ANY ADDITIONAL AFFILIATION]
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).
31 from flask
import Flask
32 from flask_restful
import Api
33 #from gevent.wsgi import WSGIServer
35 # need to import total module to set its global variable dcs
37 from compute
import dcs
, ComputeList
, Compute
, ComputeResources
, DatacenterList
, DatacenterStatus
39 # need to import total module to set its global variable net
41 from network
import NetworkAction
, DrawD3jsgraph
44 from monitor
import MonitorInterfaceAction
, MonitorFlowAction
, MonitorLinkAction
, MonitorSkewAction
49 logging
.basicConfig(level
=logging
.INFO
)
52 class RestApiEndpoint(object):
54 Simple API endpoint that offers a REST
55 interface. This interface will be used by the
56 default command line client.
59 def __init__(self
, listenip
, port
):
64 # find directory of dashboard files
65 dashboard_file
= pkg_resources
.resource_filename('emuvim.dashboard', "index.html")
66 dashboard_dir
= path
.dirname(dashboard_file
)
67 logging
.info("Started emu dashboard: {0}".format(dashboard_dir
))
69 self
.app
= Flask(__name__
, static_folder
=dashboard_dir
, static_url_path
='/dashboard')
70 self
.api
= Api(self
.app
)
74 # compute related actions (start/stop VNFs, get info)
75 self
.api
.add_resource(Compute
, "/restapi/compute/<dc_label>/<compute_name>")
76 self
.api
.add_resource(ComputeList
,
78 "/restapi/compute/<dc_label>")
79 self
.api
.add_resource(ComputeResources
, "/restapi/compute/resources/<dc_label>/<compute_name>")
81 self
.api
.add_resource(DatacenterStatus
, "/restapi/datacenter/<dc_label>")
82 self
.api
.add_resource(DatacenterList
, "/restapi/datacenter")
85 # network related actions (setup chaining between VNFs)
86 self
.api
.add_resource(NetworkAction
,
88 self
.api
.add_resource(DrawD3jsgraph
,
89 "/restapi/network/d3jsgraph")
91 # monitoring related actions
92 # export a network interface traffic rate counter
93 self
.api
.add_resource(MonitorInterfaceAction
,
94 "/restapi/monitor/interface")
95 # export flow traffic counter, of a manually pre-installed flow entry, specified by its cookie
96 self
.api
.add_resource(MonitorFlowAction
,
97 "/restapi/monitor/flow")
98 # install monitoring of a specific flow on a pre-existing link in the service.
99 # the traffic counters of the newly installed monitor flow are exported
100 self
.api
.add_resource(MonitorLinkAction
,
101 "/restapi/monitor/link")
102 # install skewness monitor of resource usage disribution
103 # the skewness metric is exported
104 self
.api
.add_resource(MonitorSkewAction
,
105 "/restapi/monitor/skewness")
108 logging
.debug("Created API endpoint %s(%s:%d)" % (self
.__class
__.__name
__, self
.ip
, self
.port
))
111 def connectDatacenter(self
, dc
):
112 compute
.dcs
[dc
.label
] = dc
114 "Connected DC(%s) to API endpoint %s(%s:%d)" % (dc
.label
, self
.__class
__.__name
__, self
.ip
, self
.port
))
116 def connectDCNetwork(self
, DCnetwork
):
117 network
.net
= DCnetwork
118 monitor
.net
= DCnetwork
120 logging
.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
121 self
.__class
__.__name
__, self
.ip
, self
.port
))
124 thread
= threading
.Thread(target
=self
._start
_flask
, args
=())
127 logging
.info("Started API endpoint @ http://%s:%d" % (self
.ip
, self
.port
))
129 def _start_flask(self
):
130 self
.app
.run(self
.ip
, self
.port
, debug
=True, use_reloader
=False)
131 #this should be a more production-fit http-server
132 #http_server = WSGIServer((self.ip, self.port), self.app)
133 #http_server.serve_forever()