Merge pull request #136 from stevenvanrossem/master
[osm/vim-emu.git] / src / emuvim / api / rest / compute.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 from flask_restful import Resource
30 from flask import request
31 import json
32
33 logging.basicConfig(level=logging.INFO)
34
35 dcs = {}
36
37 class ComputeStart(Resource):
38 """
39 Start a new compute instance: A docker container (note: zerorpc does not support keyword arguments)
40 :param dc_label: name of the DC
41 :param compute_name: compute container name
42 :param image: image name
43 :param command: command to execute
44 :param network: list of all interface of the vnf, with their parameters (id=id1,ip=x.x.x.x/x),...
45 example networks list({"id":"input","ip": "10.0.0.254/8"}, {"id":"output","ip": "11.0.0.254/24"})
46 :return: docker inspect dict of deployed docker
47 """
48 global dcs
49
50 def put(self, dc_label, compute_name):
51 logging.debug("API CALL: compute start")
52 try:
53 #check if json data is a dict
54 data = request.json
55 if data is None:
56 data = {}
57 elif type(data) is not dict:
58 data = json.loads(request.json)
59
60 network = data.get("network")
61 nw_list = self._parse_network(network)
62 image = data.get("image")
63 command = data.get("docker_command")
64
65 c = dcs.get(dc_label).startCompute(
66 compute_name, image= image, command= command, network= nw_list)
67 # return docker inspect dict
68 return c.getStatus(), 200
69 except Exception as ex:
70 logging.exception("API error.")
71 return ex.message, 500
72
73 def _parse_network(self, network_str):
74 '''
75 parse the options for all network interfaces of the vnf
76 :param network_str: (id=x,ip=x.x.x.x/x), ...
77 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
78 '''
79 nw_list = list()
80
81 if network_str is None or '),(' not in network_str :
82 return nw_list
83
84 networks = network_str[1:-1].split('),(')
85 for nw in networks:
86 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
87 nw_list.append(nw_dict)
88
89 return nw_list
90
91 class ComputeStop(Resource):
92
93 global dcs
94
95 def get(self, dc_label, compute_name):
96 logging.debug("API CALL: compute stop")
97 try:
98 return dcs.get(dc_label).stopCompute(compute_name), 200
99 except Exception as ex:
100 logging.exception("API error.")
101 return ex.message,500
102
103
104 class ComputeList(Resource):
105
106 global dcs
107
108 def get(self, dc_label):
109 logging.debug("API CALL: compute list")
110 try:
111 if dc_label == 'None':
112 # return list with all compute nodes in all DCs
113 all_containers = []
114 for dc in dcs.itervalues():
115 all_containers += dc.listCompute()
116 return [(c.name, c.getStatus()) for c in all_containers], 200
117 else:
118 # return list of compute nodes for specified DC
119 return [(c.name, c.getStatus())
120 for c in dcs.get(dc_label).listCompute()], 200
121 except Exception as ex:
122 logging.exception("API error.")
123 return ex.message, 500
124
125
126 class ComputeStatus(Resource):
127
128 global dcs
129
130 def get(self, dc_label, compute_name):
131
132 logging.debug("API CALL: compute list")
133
134 try:
135 return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200
136 except Exception as ex:
137 logging.exception("API error.")
138 return ex.message, 500
139
140 class DatacenterList(Resource):
141
142 global dcs
143
144 def get(self):
145 logging.debug("API CALL: datacenter list")
146 try:
147 return [d.getStatus() for d in dcs.itervalues()], 200
148 except Exception as ex:
149 logging.exception("API error.")
150 return ex.message, 500
151
152 class DatacenterStatus(Resource):
153
154 global dcs
155
156 def get(self, dc_label):
157 logging.debug("API CALL: datacenter status")
158 try:
159 return dcs.get(dc_label).getStatus(), 200
160 except Exception as ex:
161 logging.exception("API error.")
162 return ex.message, 500
163
164