blob: 22e9d6df13c9a0a17bbf3cf8bb6d1cd62635230d [file] [log] [blame]
peusterm79ef6ae2016-07-08 13:53:57 +02001"""
2Copyright (c) 2015 SONATA-NFV and Paderborn University
3ALL RIGHTS RESERVED.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18nor the names of its contributors may be used to endorse or promote
19products derived from this software without specific prior written
20permission.
21
22This work has been performed in the framework of the SONATA project,
23funded by the European Commission under Grant number 671517 through
24the Horizon 2020 and 5G-PPP programmes. The authors would like to
25acknowledge the contributions of their colleagues of the SONATA
26partner consortium (www.sonata-nfv.eu).
27"""
hadik3r237d3f52016-06-27 17:57:49 +020028import logging
stevenvanrossem73efd192016-06-29 01:44:07 +020029from flask_restful import Resource
30from flask import request
hadik3r237d3f52016-06-27 17:57:49 +020031import json
32
hadik3r237d3f52016-06-27 17:57:49 +020033logging.basicConfig(level=logging.INFO)
34
peustermdfc14602017-01-13 08:22:45 +010035CORS_HEADER = {'Access-Control-Allow-Origin': '*'}
36
hadik3r237d3f52016-06-27 17:57:49 +020037dcs = {}
38
hadik3ra9dd9012016-08-09 10:51:13 +020039
40class Compute(Resource):
hadik3r237d3f52016-06-27 17:57:49 +020041 """
42 Start a new compute instance: A docker container (note: zerorpc does not support keyword arguments)
43 :param dc_label: name of the DC
44 :param compute_name: compute container name
45 :param image: image name
46 :param command: command to execute
47 :param network: list of all interface of the vnf, with their parameters (id=id1,ip=x.x.x.x/x),...
stevenvanrossem73efd192016-06-29 01:44:07 +020048 example networks list({"id":"input","ip": "10.0.0.254/8"}, {"id":"output","ip": "11.0.0.254/24"})
49 :return: docker inspect dict of deployed docker
hadik3r237d3f52016-06-27 17:57:49 +020050 """
51 global dcs
52
53 def put(self, dc_label, compute_name):
hadik3ra9dd9012016-08-09 10:51:13 +020054
55 # check if json data is a dict
56 data = request.json
57 if data is None:
58 data = {}
59 elif type(data) is not dict:
60 data = json.loads(request.json)
61
62 network = data.get("network")
63 nw_list = self._parse_network(network)
64 image = data.get("image")
65 command = data.get("docker_command")
66
hadik3r237d3f52016-06-27 17:57:49 +020067 try:
hadik3ra9dd9012016-08-09 10:51:13 +020068 logging.debug("API CALL: compute start")
hadik3r237d3f52016-06-27 17:57:49 +020069 c = dcs.get(dc_label).startCompute(
hadik3ra9dd9012016-08-09 10:51:13 +020070 compute_name, image=image, command=command, network=nw_list)
hadik3r237d3f52016-06-27 17:57:49 +020071 # return docker inspect dict
peustermdfc14602017-01-13 08:22:45 +010072 return c.getStatus(), 200, CORS_HEADER
hadik3r237d3f52016-06-27 17:57:49 +020073 except Exception as ex:
74 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +010075 return ex.message, 500, CORS_HEADER
hadik3r237d3f52016-06-27 17:57:49 +020076
hadik3ra9dd9012016-08-09 10:51:13 +020077 def get(self, dc_label, compute_name):
78
79 logging.debug("API CALL: compute status")
80
81 try:
peustermdfc14602017-01-13 08:22:45 +010082 return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200, CORS_HEADER
hadik3ra9dd9012016-08-09 10:51:13 +020083 except Exception as ex:
84 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +010085 return ex.message, 500, CORS_HEADER
hadik3ra9dd9012016-08-09 10:51:13 +020086
87 def delete(self, dc_label, compute_name):
88 logging.debug("API CALL: compute stop")
89 try:
peustermdfc14602017-01-13 08:22:45 +010090 return dcs.get(dc_label).stopCompute(compute_name), 200, CORS_HEADER
hadik3ra9dd9012016-08-09 10:51:13 +020091 except Exception as ex:
92 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +010093 return ex.message, 500, CORS_HEADER
hadik3ra9dd9012016-08-09 10:51:13 +020094
stevenvanrossemff6b4042016-07-14 20:51:37 +020095 def _parse_network(self, network_str):
96 '''
97 parse the options for all network interfaces of the vnf
98 :param network_str: (id=x,ip=x.x.x.x/x), ...
99 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
100 '''
101 nw_list = list()
102
stevenvanrossem9c8a4122016-07-16 03:23:13 +0200103 # TODO make this more robust with regex check
hadik3ra9dd9012016-08-09 10:51:13 +0200104 if network_str is None:
stevenvanrossemff6b4042016-07-14 20:51:37 +0200105 return nw_list
106
107 networks = network_str[1:-1].split('),(')
108 for nw in networks:
109 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
110 nw_list.append(nw_dict)
111
112 return nw_list
113
hadik3r237d3f52016-06-27 17:57:49 +0200114
115class ComputeList(Resource):
hadik3r237d3f52016-06-27 17:57:49 +0200116 global dcs
117
stevenvanrossem65819b82016-08-05 18:21:47 +0200118 def get(self, dc_label=None):
hadik3r237d3f52016-06-27 17:57:49 +0200119 logging.debug("API CALL: compute list")
120 try:
stevenvanrossem34566442016-08-10 00:13:24 +0200121 if dc_label is None or dc_label == 'None':
hadik3r237d3f52016-06-27 17:57:49 +0200122 # return list with all compute nodes in all DCs
123 all_containers = []
124 for dc in dcs.itervalues():
125 all_containers += dc.listCompute()
peustermdfc14602017-01-13 08:22:45 +0100126 return [(c.name, c.getStatus()) for c in all_containers], 200, CORS_HEADER
hadik3r237d3f52016-06-27 17:57:49 +0200127 else:
128 # return list of compute nodes for specified DC
129 return [(c.name, c.getStatus())
peustermdfc14602017-01-13 08:22:45 +0100130 for c in dcs.get(dc_label).listCompute()], 200, CORS_HEADER
hadik3r237d3f52016-06-27 17:57:49 +0200131 except Exception as ex:
132 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +0100133 return ex.message, 500, CORS_HEADER
hadik3r237d3f52016-06-27 17:57:49 +0200134
135
hadik3r237d3f52016-06-27 17:57:49 +0200136class DatacenterList(Resource):
hadik3r237d3f52016-06-27 17:57:49 +0200137 global dcs
138
139 def get(self):
140 logging.debug("API CALL: datacenter list")
141 try:
peustermdfc14602017-01-13 08:22:45 +0100142 return [d.getStatus() for d in dcs.itervalues()], 200, CORS_HEADER
hadik3r237d3f52016-06-27 17:57:49 +0200143 except Exception as ex:
144 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +0100145 return ex.message, 500, CORS_HEADER
hadik3r237d3f52016-06-27 17:57:49 +0200146
hadik3r237d3f52016-06-27 17:57:49 +0200147
hadik3ra9dd9012016-08-09 10:51:13 +0200148class DatacenterStatus(Resource):
hadik3r237d3f52016-06-27 17:57:49 +0200149 global dcs
150
151 def get(self, dc_label):
152 logging.debug("API CALL: datacenter status")
153 try:
peustermdfc14602017-01-13 08:22:45 +0100154 return dcs.get(dc_label).getStatus(), 200, CORS_HEADER
hadik3r237d3f52016-06-27 17:57:49 +0200155 except Exception as ex:
156 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +0100157 return ex.message, 500, CORS_HEADER