cpu resource control via rest api + unittest including ELAN test
[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
33 # need to import total module to set its global variable dcs
34 import compute
35 from compute import dcs, ComputeList, Compute, DatacenterList, DatacenterStatus
36
37 # need to import total module to set its global variable net
38 import network
39 from network import NetworkAction
40
41 import monitor
42 from monitor import MonitorInterfaceAction, MonitorFlowAction
43
44 logging.basicConfig(level=logging.INFO)
45
46
47 class RestApiEndpoint(object):
48 """
49 Simple API endpoint that offers a REST
50 interface. This interface will be used by the
51 default command line client.
52 """
53
54 def __init__(self, listenip, port):
55 self.ip = listenip
56 self.port = port
57
58 # setup Flask
59 self.app = Flask(__name__)
60 self.api = Api(self.app)
61
62 # setup endpoints
63
64 self.api.add_resource(Compute,
65 "/restapi/compute/<dc_label>/<compute_name>",
66 "/restapi/compute/<dc_label>/<compute_name>/<resource>/<value>")
67 self.api.add_resource(ComputeList,
68 "/restapi/compute",
69 "/restapi/compute/<dc_label>")
70
71 self.api.add_resource(DatacenterStatus, "/restapi/datacenter/<dc_label>")
72 self.api.add_resource(DatacenterList, "/restapi/datacenter")
73
74 self.api.add_resource(NetworkAction, "/restapi/network/<vnf_src_name>/<vnf_dst_name>", )
75
76 self.api.add_resource(MonitorInterfaceAction,
77 "/restapi/monitor/<vnf_name>/<metric>",
78 "/restapi/monitor/<vnf_name>/<vnf_interface>/<metric>")
79 self.api.add_resource(MonitorFlowAction,
80 "/restapi/flowmon/<vnf_name>/<metric>/<cookie>",
81 "/restapi/flowmon/<vnf_name>/<vnf_interface>/<metric>/<cookie>")
82
83 logging.debug("Created API endpoint %s(%s:%d)" % (self.__class__.__name__, self.ip, self.port))
84
85 def connectDatacenter(self, dc):
86 compute.dcs[dc.label] = dc
87 logging.info(
88 "Connected DC(%s) to API endpoint %s(%s:%d)" % (dc.label, self.__class__.__name__, self.ip, self.port))
89
90 def connectDCNetwork(self, DCnetwork):
91 network.net = DCnetwork
92 monitor.net = DCnetwork
93
94 logging.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
95 self.__class__.__name__, self.ip, self.port))
96
97 def start(self):
98 thread = threading.Thread(target=self._start_flask, args=())
99 thread.daemon = True
100 thread.start()
101 logging.info("Started API endpoint @ http://%s:%d" % (self.ip, self.port))
102
103 def _start_flask(self):
104 self.app.run(self.ip, self.port, debug=True, use_reloader=False)