| 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 | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 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),... |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 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 |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 47 | """ |
| 48 | global dcs |
| 49 | |
| 50 | def put(self, dc_label, compute_name): |
| 51 | logging.debug("API CALL: compute start") |
| 52 | try: |
| 53 | |
| 54 | image = json.loads(request.json).get("image") |
| 55 | network = json.loads(request.json).get("network") |
| 56 | command = json.loads(request.json).get("docker_command") |
| 57 | c = dcs.get(dc_label).startCompute( |
| 58 | compute_name, image= image, command= command, network= network) |
| 59 | # return docker inspect dict |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 60 | return c.getStatus(), 200 |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 61 | except Exception as ex: |
| 62 | logging.exception("API error.") |
| 63 | return ex.message, 500 |
| 64 | |
| 65 | class ComputeStop(Resource): |
| 66 | |
| 67 | global dcs |
| 68 | |
| 69 | def get(self, dc_label, compute_name): |
| 70 | logging.debug("API CALL: compute stop") |
| 71 | try: |
| 72 | return dcs.get(dc_label).stopCompute(compute_name), 200 |
| 73 | except Exception as ex: |
| 74 | logging.exception("API error.") |
| 75 | return ex.message,500 |
| 76 | |
| 77 | |
| 78 | class ComputeList(Resource): |
| 79 | |
| 80 | global dcs |
| 81 | |
| 82 | def get(self, dc_label): |
| 83 | logging.debug("API CALL: compute list") |
| 84 | try: |
| 85 | if dc_label == 'None': |
| 86 | # return list with all compute nodes in all DCs |
| 87 | all_containers = [] |
| 88 | for dc in dcs.itervalues(): |
| 89 | all_containers += dc.listCompute() |
| 90 | return [(c.name, c.getStatus()) for c in all_containers], 200 |
| 91 | else: |
| 92 | # return list of compute nodes for specified DC |
| 93 | return [(c.name, c.getStatus()) |
| 94 | for c in dcs.get(dc_label).listCompute()], 200 |
| 95 | except Exception as ex: |
| 96 | logging.exception("API error.") |
| 97 | return ex.message, 500 |
| 98 | |
| 99 | |
| 100 | class ComputeStatus(Resource): |
| 101 | |
| 102 | global dcs |
| 103 | |
| 104 | def get(self, dc_label, compute_name): |
| 105 | |
| 106 | logging.debug("API CALL: compute list") |
| 107 | |
| 108 | try: |
| 109 | return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200 |
| 110 | except Exception as ex: |
| 111 | logging.exception("API error.") |
| 112 | return ex.message, 500 |
| 113 | |
| 114 | class DatacenterList(Resource): |
| 115 | |
| 116 | global dcs |
| 117 | |
| 118 | def get(self): |
| 119 | logging.debug("API CALL: datacenter list") |
| 120 | try: |
| 121 | return [d.getStatus() for d in dcs.itervalues()], 200 |
| 122 | except Exception as ex: |
| 123 | logging.exception("API error.") |
| 124 | return ex.message, 500 |
| 125 | |
| 126 | class DatacenterStatus(Resource): |
| 127 | |
| 128 | global dcs |
| 129 | |
| 130 | def get(self, dc_label): |
| 131 | logging.debug("API CALL: datacenter status") |
| 132 | try: |
| 133 | return dcs.get(dc_label).getStatus(), 200 |
| 134 | except Exception as ex: |
| 135 | logging.exception("API error.") |
| 136 | return ex.message, 500 |