blob: f8da42843e02a7a52686ab687201499c539c2612 [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
55 if type(data) is not dict:
56 data = json.loads(request.json)
hadik3r237d3f52016-06-27 17:57:49 +020057
stevenvanrossemff6b4042016-07-14 20:51:37 +020058 network = data.get("network")
59 nw_list = self._parse_network(network)
60 image = data.get("image")
61 command = data.get("docker_command")
62
hadik3r237d3f52016-06-27 17:57:49 +020063 c = dcs.get(dc_label).startCompute(
stevenvanrossemff6b4042016-07-14 20:51:37 +020064 compute_name, image= image, command= command, network= nw_list)
hadik3r237d3f52016-06-27 17:57:49 +020065 # return docker inspect dict
stevenvanrossem73efd192016-06-29 01:44:07 +020066 return c.getStatus(), 200
hadik3r237d3f52016-06-27 17:57:49 +020067 except Exception as ex:
68 logging.exception("API error.")
69 return ex.message, 500
70
stevenvanrossemff6b4042016-07-14 20:51:37 +020071 def _parse_network(self, network_str):
72 '''
73 parse the options for all network interfaces of the vnf
74 :param network_str: (id=x,ip=x.x.x.x/x), ...
75 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
76 '''
77 nw_list = list()
78
79 if network_str is None or '),(' not in network_str :
80 return nw_list
81
82 networks = network_str[1:-1].split('),(')
83 for nw in networks:
84 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
85 nw_list.append(nw_dict)
86
87 return nw_list
88
hadik3r237d3f52016-06-27 17:57:49 +020089class ComputeStop(Resource):
90
91 global dcs
92
93 def get(self, dc_label, compute_name):
94 logging.debug("API CALL: compute stop")
95 try:
96 return dcs.get(dc_label).stopCompute(compute_name), 200
97 except Exception as ex:
98 logging.exception("API error.")
99 return ex.message,500
100
101
102class ComputeList(Resource):
103
104 global dcs
105
106 def get(self, dc_label):
107 logging.debug("API CALL: compute list")
108 try:
109 if dc_label == 'None':
110 # return list with all compute nodes in all DCs
111 all_containers = []
112 for dc in dcs.itervalues():
113 all_containers += dc.listCompute()
114 return [(c.name, c.getStatus()) for c in all_containers], 200
115 else:
116 # return list of compute nodes for specified DC
117 return [(c.name, c.getStatus())
118 for c in dcs.get(dc_label).listCompute()], 200
119 except Exception as ex:
120 logging.exception("API error.")
121 return ex.message, 500
122
123
124class ComputeStatus(Resource):
125
126 global dcs
127
128 def get(self, dc_label, compute_name):
129
130 logging.debug("API CALL: compute list")
131
132 try:
133 return dcs.get(dc_label).containers.get(compute_name).getStatus(), 200
134 except Exception as ex:
135 logging.exception("API error.")
136 return ex.message, 500
137
138class DatacenterList(Resource):
139
140 global dcs
141
142 def get(self):
143 logging.debug("API CALL: datacenter list")
144 try:
145 return [d.getStatus() for d in dcs.itervalues()], 200
146 except Exception as ex:
147 logging.exception("API error.")
148 return ex.message, 500
149
150class DatacenterStatus(Resource):
151
152 global dcs
153
154 def get(self, dc_label):
155 logging.debug("API CALL: datacenter status")
156 try:
157 return dcs.get(dc_label).getStatus(), 200
158 except Exception as ex:
159 logging.exception("API error.")
160 return ex.message, 500
stevenvanrossemff6b4042016-07-14 20:51:37 +0200161
162