Add rest api
[osm/vim-emu.git] / src / emuvim / api / rest / compute.py
1 import logging
2 import threading
3 from flask import Flask, request
4 from flask_restful import Resource,Api
5 import json
6
7
8
9 logging.basicConfig(level=logging.INFO)
10
11
12 dcs = {}
13
14 class RestApiEndpoint(object):
15
16 """
17 Simple API endpoint that offers a REST
18 interface. This interface will be used by the
19 default command line client.
20 """
21 global dcs
22
23 def __init__(self, listenip, port):
24 self.ip = listenip
25 self.port = port
26
27 # setup Flask
28 self.app = Flask(__name__)
29 self.api = Api(self.app)
30
31 # setup endpoints
32 self.api.add_resource(ComputeList, "/restapi/compute/<dc_label>")
33 self.api.add_resource(ComputeStart, "/restapi/compute/<dc_label>/<compute_name>/start")
34 self.api.add_resource(ComputeStop, "/restapi/compute/<dc_label>/<compute_name>/stop")
35 self.api.add_resource(ComputeStatus, "/restapi/compute/<dc_label>/<compute_name>")
36 self.api.add_resource(DatacenterList, "/restapi/datacenter")
37 self.api.add_resource(DatacenterStatus, "/restapi/datacenter/<dc_label>")
38
39 logging.debug("Created API endpoint %s(%s:%d)" % (self.__class__.__name__, self.ip, self.port))
40
41
42 def connectDatacenter(self, dc):
43 dcs[dc.label] = dc
44 logging.info("Connected DC(%s) to API endpoint %s(%s:%d)" % (dc.label, self.__class__.__name__, self.ip, self.port))
45
46 def start(self):
47 thread = threading.Thread(target= self._start_flask, args=())
48 thread.daemon = True
49 thread.start()
50 logging.info("Started API endpoint @ http://%s:%d" % (self.ip, self.port))
51
52
53 def _start_flask(self):
54 self.app.run(self.ip, self.port, debug=True, use_reloader=False)
55
56
57 class ComputeStart(Resource):
58 """
59 Start a new compute instance: A docker container (note: zerorpc does not support keyword arguments)
60 :param dc_label: name of the DC
61 :param compute_name: compute container name
62 :param image: image name
63 :param command: command to execute
64 :param network: list of all interface of the vnf, with their parameters (id=id1,ip=x.x.x.x/x),...
65 :return: networks list({"id":"input","ip": "10.0.0.254/8"}, {"id":"output","ip": "11.0.0.254/24"})
66 """
67 global dcs
68
69 def put(self, dc_label, compute_name):
70 logging.debug("API CALL: compute start")
71 try:
72
73 image = json.loads(request.json).get("image")
74 network = json.loads(request.json).get("network")
75 command = json.loads(request.json).get("docker_command")
76 c = dcs.get(dc_label).startCompute(
77 compute_name, image= image, command= command, network= network)
78 # return docker inspect dict
79 return c. getStatus(), 200
80 except Exception as ex:
81 logging.exception("API error.")
82 return ex.message, 500
83
84 class ComputeStop(Resource):
85
86 global dcs
87
88 def get(self, dc_label, compute_name):
89 logging.debug("API CALL: compute stop")
90 try:
91 return dcs.get(dc_label).stopCompute(compute_name), 200
92 except Exception as ex:
93 logging.exception("API error.")
94 return ex.message,500
95
96
97 class ComputeList(Resource):
98
99 global dcs
100
101 def get(self, dc_label):
102 logging.debug("API CALL: compute list")
103 try:
104 if dc_label == 'None':
105 # return list with all compute nodes in all DCs
106 all_containers = []
107 for dc in dcs.itervalues():
108 all_containers += dc.listCompute()
109 return [(c.name, c.getStatus()) for c in all_containers], 200
110 else:
111 # return list of compute nodes for specified DC
112 return [(c.name, c.getStatus())
113 for c in dcs.get(dc_label).listCompute()], 200
114 except Exception as ex:
115 logging.exception("API error.")
116 return ex.message, 500
117
118
119 class ComputeStatus(Resource):
120
121 global dcs
122
123 def get(self, dc_label, compute_name):
124
125 logging.debug("API CALL: compute list")
126
127 try:
128 return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200
129 except Exception as ex:
130 logging.exception("API error.")
131 return ex.message, 500
132
133 class DatacenterList(Resource):
134
135 global dcs
136
137 def get(self):
138 logging.debug("API CALL: datacenter list")
139 try:
140 return [d.getStatus() for d in dcs.itervalues()], 200
141 except Exception as ex:
142 logging.exception("API error.")
143 return ex.message, 500
144
145 class DatacenterStatus(Resource):
146
147 global dcs
148
149 def get(self, dc_label):
150 logging.debug("API CALL: datacenter status")
151 try:
152 return dcs.get(dc_label).getStatus(), 200
153 except Exception as ex:
154 logging.exception("API error.")
155 return ex.message, 500