blob: 9f0516b3cc8355aeb52230508b78300a7333c3b7 [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
hadik3r237d3f52016-06-27 17:57:49 +020035dcs = {}
36
hadik3r237d3f52016-06-27 17:57:49 +020037class 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),...
stevenvanrossem73efd192016-06-29 01:44:07 +020045 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
hadik3r237d3f52016-06-27 17:57:49 +020047 """
48 global dcs
49
50 def put(self, dc_label, compute_name):
51 logging.debug("API CALL: compute start")
52 try:
stevenvanrossemff6b4042016-07-14 20:51:37 +020053 #check if json data is a dict
54 data = request.json
stevenvanrossem1027edc2016-07-14 22:02:02 +020055 if data is None:
56 data = {}
57 elif type(data) is not dict:
stevenvanrossemff6b4042016-07-14 20:51:37 +020058 data = json.loads(request.json)
hadik3r237d3f52016-06-27 17:57:49 +020059
stevenvanrossemff6b4042016-07-14 20:51:37 +020060 network = data.get("network")
61 nw_list = self._parse_network(network)
62 image = data.get("image")
63 command = data.get("docker_command")
64
hadik3r237d3f52016-06-27 17:57:49 +020065 c = dcs.get(dc_label).startCompute(
stevenvanrossemff6b4042016-07-14 20:51:37 +020066 compute_name, image= image, command= command, network= nw_list)
hadik3r237d3f52016-06-27 17:57:49 +020067 # return docker inspect dict
stevenvanrossem73efd192016-06-29 01:44:07 +020068 return c.getStatus(), 200
hadik3r237d3f52016-06-27 17:57:49 +020069 except Exception as ex:
70 logging.exception("API error.")
71 return ex.message, 500
72
stevenvanrossemff6b4042016-07-14 20:51:37 +020073 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
stevenvanrossem9c8a4122016-07-16 03:23:13 +020081 # TODO make this more robust with regex check
82 if network_str is None :
stevenvanrossemff6b4042016-07-14 20:51:37 +020083 return nw_list
84
85 networks = network_str[1:-1].split('),(')
86 for nw in networks:
87 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
88 nw_list.append(nw_dict)
89
90 return nw_list
91
hadik3r237d3f52016-06-27 17:57:49 +020092class ComputeStop(Resource):
93
94 global dcs
95
96 def get(self, dc_label, compute_name):
97 logging.debug("API CALL: compute stop")
98 try:
99 return dcs.get(dc_label).stopCompute(compute_name), 200
100 except Exception as ex:
101 logging.exception("API error.")
102 return ex.message,500
103
104
105class ComputeList(Resource):
106
107 global dcs
108
109 def get(self, dc_label):
110 logging.debug("API CALL: compute list")
111 try:
112 if dc_label == 'None':
113 # return list with all compute nodes in all DCs
114 all_containers = []
115 for dc in dcs.itervalues():
116 all_containers += dc.listCompute()
117 return [(c.name, c.getStatus()) for c in all_containers], 200
118 else:
119 # return list of compute nodes for specified DC
120 return [(c.name, c.getStatus())
121 for c in dcs.get(dc_label).listCompute()], 200
122 except Exception as ex:
123 logging.exception("API error.")
124 return ex.message, 500
125
126
127class ComputeStatus(Resource):
128
129 global dcs
130
131 def get(self, dc_label, compute_name):
132
133 logging.debug("API CALL: compute list")
134
135 try:
136 return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200
137 except Exception as ex:
138 logging.exception("API error.")
139 return ex.message, 500
140
141class DatacenterList(Resource):
142
143 global dcs
144
145 def get(self):
146 logging.debug("API CALL: datacenter list")
147 try:
148 return [d.getStatus() for d in dcs.itervalues()], 200
149 except Exception as ex:
150 logging.exception("API error.")
151 return ex.message, 500
152
153class DatacenterStatus(Resource):
154
155 global dcs
156
157 def get(self, dc_label):
158 logging.debug("API CALL: datacenter status")
159 try:
160 return dcs.get(dc_label).getStatus(), 200
161 except Exception as ex:
162 logging.exception("API error.")
163 return ex.message, 500
stevenvanrossemff6b4042016-07-14 20:51:37 +0200164
165