| peusterm | 79ef6ae | 2016-07-08 13:53:57 +0200 | [diff] [blame] | 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 | """ |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 28 | import logging |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 29 | from flask_restful import Resource |
| 30 | from flask import request |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 31 | import json |
| 32 | |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 33 | logging.basicConfig(level=logging.INFO) |
| 34 | |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 35 | dcs = {} |
| 36 | |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 37 | |
| 38 | class Compute(Resource): |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 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),... |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 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 |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 48 | """ |
| 49 | global dcs |
| 50 | |
| stevenvanrossem | b3f3417 | 2016-11-16 23:30:57 +0100 | [diff] [blame] | 51 | def put(self, dc_label, compute_name, resource=None, value=None): |
| 52 | # check if resource update |
| 53 | if resource and value: |
| 54 | c = self._update_resource(dc_label, compute_name, resource, value) |
| 55 | return c.getStatus(), 200 |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 56 | |
| stevenvanrossem | b3f3417 | 2016-11-16 23:30:57 +0100 | [diff] [blame] | 57 | # deploy new container |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 58 | # check if json data is a dict |
| 59 | data = request.json |
| 60 | if data is None: |
| 61 | data = {} |
| 62 | elif type(data) is not dict: |
| 63 | data = json.loads(request.json) |
| 64 | |
| 65 | network = data.get("network") |
| 66 | nw_list = self._parse_network(network) |
| 67 | image = data.get("image") |
| 68 | command = data.get("docker_command") |
| 69 | |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 70 | try: |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 71 | logging.debug("API CALL: compute start") |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 72 | c = dcs.get(dc_label).startCompute( |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 73 | compute_name, image=image, command=command, network=nw_list) |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 74 | # return docker inspect dict |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 75 | return c.getStatus(), 200 |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 76 | except Exception as ex: |
| 77 | logging.exception("API error.") |
| 78 | return ex.message, 500 |
| 79 | |
| stevenvanrossem | b3f3417 | 2016-11-16 23:30:57 +0100 | [diff] [blame] | 80 | def _update_resource(self, dc_label, compute_name, resource, value): |
| 81 | #check if container exists |
| 82 | d = dcs.get(dc_label).net.getNodeByName(compute_name) |
| 83 | if resource == 'cpu': |
| 84 | cpu_period = int(dcs.get(dc_label).net.cpu_period) |
| 85 | cpu_quota = int(cpu_period * float(value)) |
| 86 | #put default values back |
| 87 | if float(value) <= 0: |
| 88 | cpu_period = 100000 |
| 89 | cpu_quota = -1 |
| 90 | d.updateCpuLimit(cpu_period=cpu_period, cpu_quota=cpu_quota) |
| 91 | return d |
| 92 | |
| 93 | |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 94 | def get(self, dc_label, compute_name): |
| 95 | |
| 96 | logging.debug("API CALL: compute status") |
| 97 | |
| 98 | try: |
| 99 | return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200 |
| 100 | except Exception as ex: |
| 101 | logging.exception("API error.") |
| 102 | return ex.message, 500 |
| 103 | |
| 104 | def delete(self, dc_label, compute_name): |
| 105 | logging.debug("API CALL: compute stop") |
| 106 | try: |
| 107 | return dcs.get(dc_label).stopCompute(compute_name), 200 |
| 108 | except Exception as ex: |
| 109 | logging.exception("API error.") |
| 110 | return ex.message, 500 |
| 111 | |
| stevenvanrossem | ff6b404 | 2016-07-14 20:51:37 +0200 | [diff] [blame] | 112 | def _parse_network(self, network_str): |
| 113 | ''' |
| 114 | parse the options for all network interfaces of the vnf |
| 115 | :param network_str: (id=x,ip=x.x.x.x/x), ... |
| 116 | :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...] |
| 117 | ''' |
| 118 | nw_list = list() |
| 119 | |
| stevenvanrossem | 9c8a412 | 2016-07-16 03:23:13 +0200 | [diff] [blame] | 120 | # TODO make this more robust with regex check |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 121 | if network_str is None: |
| stevenvanrossem | ff6b404 | 2016-07-14 20:51:37 +0200 | [diff] [blame] | 122 | return nw_list |
| 123 | |
| 124 | networks = network_str[1:-1].split('),(') |
| 125 | for nw in networks: |
| 126 | nw_dict = dict(tuple(e.split('=')) for e in nw.split(',')) |
| 127 | nw_list.append(nw_dict) |
| 128 | |
| 129 | return nw_list |
| 130 | |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 131 | |
| 132 | class ComputeList(Resource): |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 133 | global dcs |
| 134 | |
| stevenvanrossem | 65819b8 | 2016-08-05 18:21:47 +0200 | [diff] [blame] | 135 | def get(self, dc_label=None): |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 136 | logging.debug("API CALL: compute list") |
| 137 | try: |
| stevenvanrossem | 3456644 | 2016-08-10 00:13:24 +0200 | [diff] [blame] | 138 | if dc_label is None or dc_label == 'None': |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 139 | # return list with all compute nodes in all DCs |
| 140 | all_containers = [] |
| 141 | for dc in dcs.itervalues(): |
| 142 | all_containers += dc.listCompute() |
| 143 | return [(c.name, c.getStatus()) for c in all_containers], 200 |
| 144 | else: |
| 145 | # return list of compute nodes for specified DC |
| 146 | return [(c.name, c.getStatus()) |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 147 | for c in dcs.get(dc_label).listCompute()], 200 |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 148 | except Exception as ex: |
| 149 | logging.exception("API error.") |
| 150 | return ex.message, 500 |
| 151 | |
| 152 | |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 153 | class DatacenterList(Resource): |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 154 | global dcs |
| 155 | |
| 156 | def get(self): |
| 157 | logging.debug("API CALL: datacenter list") |
| 158 | try: |
| 159 | return [d.getStatus() for d in dcs.itervalues()], 200 |
| 160 | except Exception as ex: |
| 161 | logging.exception("API error.") |
| 162 | return ex.message, 500 |
| 163 | |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 164 | |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 165 | class DatacenterStatus(Resource): |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 166 | global dcs |
| 167 | |
| 168 | def get(self, dc_label): |
| 169 | logging.debug("API CALL: datacenter status") |
| 170 | try: |
| 171 | return dcs.get(dc_label).getStatus(), 200 |
| 172 | except Exception as ex: |
| 173 | logging.exception("API error.") |
| 174 | return ex.message, 500 |