change compute api start/stop to put/delete
[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
38 class Compute(Resource):
39 """
40 Start a new compute instance: A docker container (note: zerorpc does not support keyword arguments)
41 :param dc_label: name of the DC
42 :param compute_name: compute container name
43 :param image: image name
44 :param command: command to execute
45 :param network: list of all interface of the vnf, with their parameters (id=id1,ip=x.x.x.x/x),...
46 example networks list({"id":"input","ip": "10.0.0.254/8"}, {"id":"output","ip": "11.0.0.254/24"})
47 :return: docker inspect dict of deployed docker
48 """
49 global dcs
50
51 def put(self, dc_label, compute_name):
52
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 try:
66 logging.debug("API CALL: compute start")
67 c = dcs.get(dc_label).startCompute(
68 compute_name, image=image, command=command, network=nw_list)
69 # return docker inspect dict
70 return c.getStatus(), 200
71 except Exception as ex:
72 logging.exception("API error.")
73 return ex.message, 500
74
75 def get(self, dc_label, compute_name):
76
77 logging.debug("API CALL: compute status")
78
79 try:
80 return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200
81 except Exception as ex:
82 logging.exception("API error.")
83 return ex.message, 500
84
85 def delete(self, dc_label, compute_name):
86 logging.debug("API CALL: compute stop")
87 try:
88 return dcs.get(dc_label).stopCompute(compute_name), 200
89 except Exception as ex:
90 logging.exception("API error.")
91 return ex.message, 500
92
93 def _parse_network(self, network_str):
94 '''
95 parse the options for all network interfaces of the vnf
96 :param network_str: (id=x,ip=x.x.x.x/x), ...
97 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
98 '''
99 nw_list = list()
100
101 # TODO make this more robust with regex check
102 if network_str is None:
103 return nw_list
104
105 networks = network_str[1:-1].split('),(')
106 for nw in networks:
107 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
108 nw_list.append(nw_dict)
109
110 return nw_list
111
112
113 class ComputeList(Resource):
114 global dcs
115
116 def get(self, dc_label):
117 logging.debug("API CALL: compute list")
118 try:
119 if dc_label == 'None':
120 # return list with all compute nodes in all DCs
121 all_containers = []
122 for dc in dcs.itervalues():
123 all_containers += dc.listCompute()
124 return [(c.name, c.getStatus()) for c in all_containers], 200
125 else:
126 # return list of compute nodes for specified DC
127 return [(c.name, c.getStatus())
128 for c in dcs.get(dc_label).listCompute()], 200
129 except Exception as ex:
130 logging.exception("API error.")
131 return ex.message, 500
132
133
134 class DatacenterList(Resource):
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
146 class DatacenterStatus(Resource):
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