3cb737e9924011d31420dcdcf1d1b458616803d4
[osm/vim-emu.git] / src / emuvim / api / rest / rest_api_endpoint.py
1 """
2 Copyright (c) 2015 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 [, 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
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
29 import logging
30 import threading
31 from flask import Flask
32 from flask_restful import Api
33 from gevent.wsgi import WSGIServer
34
35 # need to import total module to set its global variable dcs
36 import compute
37 from compute import dcs, ComputeList, Compute, ComputeResources, DatacenterList, DatacenterStatus
38
39 # need to import total module to set its global variable net
40 import network
41 from network import NetworkAction, DrawD3jsgraph
42
43 import monitor
44 from monitor import MonitorInterfaceAction, MonitorFlowAction, MonitorLinkAction, MonitorSkewAction, MonitorTerminal
45
46 import pkg_resources
47 from os import path
48
49 logging.basicConfig()
50
51
52 class RestApiEndpoint(object):
53 """
54 Simple API endpoint that offers a REST
55 interface. This interface will be used by the
56 default command line client.
57 """
58
59 def __init__(self, listenip, port, DCnetwork=None):
60 self.ip = listenip
61 self.port = port
62
63 # connect this DC network to the rest api endpoint (needed for the networking and monitoring api)
64 self.connectDCNetwork(DCnetwork)
65
66 # setup Flask
67 # find directory of dashboard files
68 dashboard_file = pkg_resources.resource_filename('emuvim.dashboard', "index.html")
69 dashboard_dir = path.dirname(dashboard_file)
70 logging.info("Started emu dashboard: {0}".format(dashboard_dir))
71
72 self.app = Flask(__name__, static_folder=dashboard_dir, static_url_path='/dashboard')
73 self.api = Api(self.app)
74
75 # setup endpoints
76
77 # compute related actions (start/stop VNFs, get info)
78 self.api.add_resource(Compute, "/restapi/compute/<dc_label>/<compute_name>")
79 self.api.add_resource(ComputeList,
80 "/restapi/compute",
81 "/restapi/compute/<dc_label>")
82 self.api.add_resource(ComputeResources, "/restapi/compute/resources/<dc_label>/<compute_name>")
83
84 self.api.add_resource(DatacenterStatus, "/restapi/datacenter/<dc_label>")
85 self.api.add_resource(DatacenterList, "/restapi/datacenter")
86
87
88 # network related actions (setup chaining between VNFs)
89 self.api.add_resource(NetworkAction,
90 "/restapi/network")
91 self.api.add_resource(DrawD3jsgraph,
92 "/restapi/network/d3jsgraph")
93
94 # monitoring related actions
95 # export a network interface traffic rate counter
96 self.api.add_resource(MonitorInterfaceAction,
97 "/restapi/monitor/interface")
98 # export flow traffic counter, of a manually pre-installed flow entry, specified by its cookie
99 self.api.add_resource(MonitorFlowAction,
100 "/restapi/monitor/flow")
101 # install monitoring of a specific flow on a pre-existing link in the service.
102 # the traffic counters of the newly installed monitor flow are exported
103 self.api.add_resource(MonitorLinkAction,
104 "/restapi/monitor/link")
105 # install skewness monitor of resource usage disribution
106 # the skewness metric is exported
107 self.api.add_resource(MonitorSkewAction,
108 "/restapi/monitor/skewness")
109 # start a terminal window for the specified vnfs
110 self.api.add_resource(MonitorTerminal,
111 "/restapi/monitor/term")
112
113
114 logging.debug("Created API endpoint %s(%s:%d)" % (self.__class__.__name__, self.ip, self.port))
115
116
117 def connectDatacenter(self, dc):
118 compute.dcs[dc.label] = dc
119 logging.info(
120 "Connected DC(%s) to API endpoint %s(%s:%d)" % (dc.label, self.__class__.__name__, self.ip, self.port))
121
122 def connectDCNetwork(self, DCnetwork):
123 network.net = DCnetwork
124 monitor.net = DCnetwork
125
126 logging.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
127 self.__class__.__name__, self.ip, self.port))
128
129 def start(self):
130 self.thread = threading.Thread(target=self._start_flask, args=())
131 self.thread.daemon = True
132 self.thread.start()
133 logging.info("Started API endpoint @ http://%s:%d" % (self.ip, self.port))
134
135 def stop(self):
136 if self.http_server:
137 self.http_server.close()
138
139 def _start_flask(self):
140 #self.app.run(self.ip, self.port, debug=False, use_reloader=False)
141 #this should be a more production-fit http-server
142 #self.app.logger.setLevel(logging.ERROR)
143 self.http_server = WSGIServer((self.ip, self.port),
144 self.app,
145 log=open("/dev/null", "w") # This disables HTTP request logs to not mess up the CLI when e.g. the auto-updated dashboard is used
146 )
147 self.http_server.serve_forever()