cleanup logging
[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 import logging
29 import threading
30 from flask import Flask
31 from flask_restful import Api
32 #from gevent.wsgi import WSGIServer
33
34 # need to import total module to set its global variable dcs
35 import compute
36 from compute import dcs, ComputeList, Compute, ComputeResources, DatacenterList, DatacenterStatus
37
38 # need to import total module to set its global variable net
39 import network
40 from network import NetworkAction
41
42 import monitor
43 from monitor import MonitorInterfaceAction, MonitorFlowAction, MonitorLinkAction, MonitorSkewAction
44
45 logging.basicConfig(level=logging.INFO)
46
47
48 class RestApiEndpoint(object):
49 """
50 Simple API endpoint that offers a REST
51 interface. This interface will be used by the
52 default command line client.
53 """
54
55 def __init__(self, listenip, port):
56 self.ip = listenip
57 self.port = port
58
59 # setup Flask
60 self.app = Flask(__name__)
61 self.api = Api(self.app)
62
63 # setup endpoints
64
65 # compute related actions (start/stop VNFs, get info)
66 self.api.add_resource(Compute, "/restapi/compute/<dc_label>/<compute_name>")
67 self.api.add_resource(ComputeList,
68 "/restapi/compute",
69 "/restapi/compute/<dc_label>")
70 self.api.add_resource(ComputeResources, "/restapi/compute/resources/<dc_label>/<compute_name>")
71
72 self.api.add_resource(DatacenterStatus, "/restapi/datacenter/<dc_label>")
73 self.api.add_resource(DatacenterList, "/restapi/datacenter")
74
75
76 # network related actions (setup chaining between VNFs)
77 self.api.add_resource(NetworkAction,
78 "/restapi/network")
79
80
81 # monitoring related actions
82 # export a network interface traffic rate counter
83 self.api.add_resource(MonitorInterfaceAction,
84 "/restapi/monitor/interface")
85 # export flow traffic counter, of a manually pre-installed flow entry, specified by its cookie
86 self.api.add_resource(MonitorFlowAction,
87 "/restapi/monitor/flow")
88 # install monitoring of a specific flow on a pre-existing link in the service.
89 # the traffic counters of the newly installed monitor flow are exported
90 self.api.add_resource(MonitorLinkAction,
91 "/restapi/monitor/link")
92 # install skewness monitor of resource usage disribution
93 # the skewness metric is exported
94 self.api.add_resource(MonitorSkewAction,
95 "/restapi/monitor/skewness")
96
97 logging.debug("Created API endpoint %s(%s:%d)" % (self.__class__.__name__, self.ip, self.port))
98
99 def connectDatacenter(self, dc):
100 compute.dcs[dc.label] = dc
101 logging.info(
102 "Connected DC(%s) to API endpoint %s(%s:%d)" % (dc.label, self.__class__.__name__, self.ip, self.port))
103
104 def connectDCNetwork(self, DCnetwork):
105 network.net = DCnetwork
106 monitor.net = DCnetwork
107
108 logging.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
109 self.__class__.__name__, self.ip, self.port))
110
111 def start(self):
112 thread = threading.Thread(target=self._start_flask, args=())
113 thread.daemon = True
114 thread.start()
115 logging.info("Started API endpoint @ http://%s:%d" % (self.ip, self.port))
116
117 def _start_flask(self):
118 self.app.run(self.ip, self.port, debug=True, use_reloader=False)
119 #this should be a more production-fit http-server
120 #http_server = WSGIServer((self.ip, self.port), self.app)
121 #http_server.serve_forever()