blob: 32a69c9922b4734f0a1201bcd097b6a2e8aad64c [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- coding: utf-8 -*-
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5# This file is part of openmano
6# All Rights Reserved.
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License. You may obtain
10# a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17# License for the specific language governing permissions and limitations
18# under the License.
19#
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact with: nfvlabs@tid.es
22##
23
24'''
25vimconnector implements all the methods to interact with openvim using the openvim API.
tierno7edb6752016-03-21 17:37:52 +010026'''
27__author__="Alfonso Tierno, Gerardo Garcia"
28__date__ ="$26-aug-2014 11:09:29$"
29
Adam Israel8e3ce872018-01-08 18:43:40 +000030import vimconn
tierno7edb6752016-03-21 17:37:52 +010031import requests
32import json
33import yaml
tiernoae4a8d12016-07-08 12:30:39 +020034import logging
Adam Israel8e3ce872018-01-08 18:43:40 +000035from openmano_schemas import id_schema, name_schema, nameshort_schema, description_schema, \
tierno7edb6752016-03-21 17:37:52 +010036 vlan1000_schema, integer0_schema
37from jsonschema import validate as js_v, exceptions as js_e
Adam Israel8e3ce872018-01-08 18:43:40 +000038try:
39 from urllib import quote # Python 2.X
40except ImportError:
41 from urllib.parse import quote # Python 3+
42
tierno7edb6752016-03-21 17:37:52 +010043'''contain the openvim virtual machine status to openmano status'''
44vmStatus2manoFormat={'ACTIVE':'ACTIVE',
45 'PAUSED':'PAUSED',
46 'SUSPENDED': 'SUSPENDED',
47 'INACTIVE':'INACTIVE',
48 'CREATING':'BUILD',
49 'ERROR':'ERROR','DELETED':'DELETED'
50 }
51netStatus2manoFormat={'ACTIVE':'ACTIVE','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED', 'DOWN':'DOWN'
52 }
53
54
55host_schema = {
56 "type":"object",
57 "properties":{
58 "id": id_schema,
59 "name": name_schema,
60 },
61 "required": ["id"]
62}
63image_schema = {
64 "type":"object",
65 "properties":{
66 "id": id_schema,
67 "name": name_schema,
68 },
69 "required": ["id","name"]
70}
71server_schema = {
72 "type":"object",
73 "properties":{
74 "id":id_schema,
75 "name": name_schema,
76 },
77 "required": ["id","name"]
78}
79new_host_response_schema = {
80 "title":"host response information schema",
81 "$schema": "http://json-schema.org/draft-04/schema#",
82 "type":"object",
83 "properties":{
84 "host": host_schema
85 },
86 "required": ["host"],
87 "additionalProperties": False
88}
89
90get_images_response_schema = {
91 "title":"openvim images response information schema",
92 "$schema": "http://json-schema.org/draft-04/schema#",
93 "type":"object",
94 "properties":{
95 "images":{
96 "type":"array",
97 "items": image_schema,
98 }
99 },
100 "required": ["images"],
101 "additionalProperties": False
102}
103
104get_hosts_response_schema = {
105 "title":"openvim hosts response information schema",
106 "$schema": "http://json-schema.org/draft-04/schema#",
107 "type":"object",
108 "properties":{
109 "hosts":{
110 "type":"array",
111 "items": host_schema,
112 }
113 },
114 "required": ["hosts"],
115 "additionalProperties": False
116}
117
118get_host_detail_response_schema = new_host_response_schema # TODO: Content is not parsed yet
119
120get_server_response_schema = {
121 "title":"openvim server response information schema",
122 "$schema": "http://json-schema.org/draft-04/schema#",
123 "type":"object",
124 "properties":{
125 "servers":{
126 "type":"array",
127 "items": server_schema,
128 }
129 },
130 "required": ["servers"],
131 "additionalProperties": False
132}
133
134new_tenant_response_schema = {
135 "title":"tenant response information schema",
136 "$schema": "http://json-schema.org/draft-04/schema#",
137 "type":"object",
138 "properties":{
139 "tenant":{
140 "type":"object",
141 "properties":{
142 "id": id_schema,
143 "name": nameshort_schema,
144 "description":description_schema,
145 "enabled":{"type" : "boolean"}
146 },
147 "required": ["id"]
148 }
149 },
150 "required": ["tenant"],
151 "additionalProperties": False
152}
153
154new_network_response_schema = {
155 "title":"network response information schema",
156 "$schema": "http://json-schema.org/draft-04/schema#",
157 "type":"object",
158 "properties":{
159 "network":{
160 "type":"object",
161 "properties":{
162 "id":id_schema,
163 "name":name_schema,
164 "type":{"type":"string", "enum":["bridge_man","bridge_data","data", "ptp"]},
165 "shared":{"type":"boolean"},
166 "tenant_id":id_schema,
167 "admin_state_up":{"type":"boolean"},
168 "vlan":vlan1000_schema
169 },
170 "required": ["id"]
171 }
172 },
173 "required": ["network"],
174 "additionalProperties": False
175}
176
177
178# get_network_response_schema = {
179# "title":"get network response information schema",
180# "$schema": "http://json-schema.org/draft-04/schema#",
181# "type":"object",
182# "properties":{
183# "network":{
184# "type":"object",
185# "properties":{
186# "id":id_schema,
187# "name":name_schema,
188# "type":{"type":"string", "enum":["bridge_man","bridge_data","data", "ptp"]},
189# "shared":{"type":"boolean"},
190# "tenant_id":id_schema,
191# "admin_state_up":{"type":"boolean"},
192# "vlan":vlan1000_schema
193# },
194# "required": ["id"]
195# }
196# },
197# "required": ["network"],
198# "additionalProperties": False
199# }
200
201
202new_port_response_schema = {
203 "title":"port response information schema",
204 "$schema": "http://json-schema.org/draft-04/schema#",
205 "type":"object",
206 "properties":{
207 "port":{
208 "type":"object",
209 "properties":{
210 "id":id_schema,
211 },
212 "required": ["id"]
213 }
214 },
215 "required": ["port"],
216 "additionalProperties": False
217}
218
219get_flavor_response_schema = {
220 "title":"openvim flavors response information schema",
221 "$schema": "http://json-schema.org/draft-04/schema#",
222 "type":"object",
223 "properties":{
224 "flavor":{
225 "type":"object",
226 "properties":{
227 "id": id_schema,
228 "name": name_schema,
229 "extended": {"type":"object"},
230 },
231 "required": ["id", "name"],
232 }
233 },
234 "required": ["flavor"],
235 "additionalProperties": False
236}
237
238new_flavor_response_schema = {
239 "title":"flavor response information schema",
240 "$schema": "http://json-schema.org/draft-04/schema#",
241 "type":"object",
242 "properties":{
243 "flavor":{
244 "type":"object",
245 "properties":{
246 "id":id_schema,
247 },
248 "required": ["id"]
249 }
250 },
251 "required": ["flavor"],
252 "additionalProperties": False
253}
254
tiernoae4a8d12016-07-08 12:30:39 +0200255get_image_response_schema = {
256 "title":"openvim images response information schema",
257 "$schema": "http://json-schema.org/draft-04/schema#",
258 "type":"object",
259 "properties":{
260 "image":{
261 "type":"object",
262 "properties":{
263 "id": id_schema,
264 "name": name_schema,
265 },
266 "required": ["id", "name"],
267 }
268 },
269 "required": ["flavor"],
270 "additionalProperties": False
271}
tierno7edb6752016-03-21 17:37:52 +0100272new_image_response_schema = {
273 "title":"image response information schema",
274 "$schema": "http://json-schema.org/draft-04/schema#",
275 "type":"object",
276 "properties":{
277 "image":{
278 "type":"object",
279 "properties":{
280 "id":id_schema,
281 },
282 "required": ["id"]
283 }
284 },
285 "required": ["image"],
286 "additionalProperties": False
287}
288
289new_vminstance_response_schema = {
290 "title":"server response information schema",
291 "$schema": "http://json-schema.org/draft-04/schema#",
292 "type":"object",
293 "properties":{
294 "server":{
295 "type":"object",
296 "properties":{
297 "id":id_schema,
298 },
299 "required": ["id"]
300 }
301 },
302 "required": ["server"],
303 "additionalProperties": False
304}
305
306get_processor_rankings_response_schema = {
307 "title":"processor rankings information schema",
308 "$schema": "http://json-schema.org/draft-04/schema#",
309 "type":"object",
310 "properties":{
311 "rankings":{
312 "type":"array",
313 "items":{
314 "type":"object",
315 "properties":{
316 "model": description_schema,
317 "value": integer0_schema
318 },
319 "additionalProperties": False,
320 "required": ["model","value"]
321 }
322 },
323 "additionalProperties": False,
324 "required": ["rankings"]
325 }
326}
327
328class vimconnector(vimconn.vimconnector):
tiernob3d36742017-03-03 23:51:05 +0100329 def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None,
330 log_level="DEBUG", config={}, persistent_info={}):
tiernoae4a8d12016-07-08 12:30:39 +0200331 vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level, config)
tierno392f2852016-05-13 12:28:55 +0200332 self.tenant = None
333 self.headers_req = {'content-type': 'application/json'}
tierno73ad9e42016-09-12 18:11:11 +0200334 self.logger = logging.getLogger('openmano.vim.openvim')
tiernob3d36742017-03-03 23:51:05 +0100335 self.persistent_info = persistent_info
tierno392f2852016-05-13 12:28:55 +0200336 if tenant_id:
337 self.tenant = tenant_id
338
339 def __setitem__(self,index, value):
Adam Israel8e3ce872018-01-08 18:43:40 +0000340 '''Set individuals parameters
tierno392f2852016-05-13 12:28:55 +0200341 Throw TypeError, KeyError
342 '''
343 if index=='tenant_id':
344 self.tenant = value
345 elif index=='tenant_name':
346 self.tenant = None
Adam Israel8e3ce872018-01-08 18:43:40 +0000347 vimconn.vimconnector.__setitem__(self,index, value)
tierno392f2852016-05-13 12:28:55 +0200348
349 def _get_my_tenant(self):
350 '''Obtain uuid of my tenant from name
351 '''
352 if self.tenant:
353 return self.tenant
354
tiernobe41e222016-09-02 15:16:13 +0200355 url = self.url+'/tenants?name='+ quote(self.tenant_name)
tiernoae4a8d12016-07-08 12:30:39 +0200356 self.logger.info("Getting VIM tenant_id GET %s", url)
357 vim_response = requests.get(url, headers = self.headers_req)
358 self._check_http_request_response(vim_response)
359 try:
360 tenant_list = vim_response.json()["tenants"]
361 if len(tenant_list) == 0:
362 raise vimconn.vimconnNotFoundException("No tenant found for name '%s'" % str(self.tenant_name))
363 elif len(tenant_list) > 1:
364 raise vimconn.vimconnConflictException ("More that one tenant found for name '%s'" % str(self.tenant_name))
365 self.tenant = tenant_list[0]["id"]
366 return self.tenant
367 except Exception as e:
368 raise vimconn.vimconnUnexpectedResponse("Get VIM tenant {} '{}'".format(type(e).__name__, str(e)))
tierno392f2852016-05-13 12:28:55 +0200369
tierno7edb6752016-03-21 17:37:52 +0100370 def _format_jsonerror(self,http_response):
tiernoae4a8d12016-07-08 12:30:39 +0200371 #DEPRECATED, to delete in the future
tierno7edb6752016-03-21 17:37:52 +0100372 try:
373 data = http_response.json()
374 return data["error"]["description"]
375 except:
376 return http_response.text
377
378 def _format_in(self, http_response, schema):
tiernoae4a8d12016-07-08 12:30:39 +0200379 #DEPRECATED, to delete in the future
tierno7edb6752016-03-21 17:37:52 +0100380 try:
381 client_data = http_response.json()
382 js_v(client_data, schema)
Adam Israel8e3ce872018-01-08 18:43:40 +0000383 #print("Input data: ", str(client_data))
tierno7edb6752016-03-21 17:37:52 +0100384 return True, client_data
venkatamahesh6ecca182017-01-27 23:04:40 +0530385 except js_e.ValidationError as exc:
Marco Ceppic4629bd2017-10-26 14:35:46 +0200386 print("validate_in error, jsonschema exception ", exc.message, "at", exc.path)
tierno7edb6752016-03-21 17:37:52 +0100387 return False, ("validate_in error, jsonschema exception ", exc.message, "at", exc.path)
Adam Israel8e3ce872018-01-08 18:43:40 +0000388
tierno7edb6752016-03-21 17:37:52 +0100389 def _remove_extra_items(self, data, schema):
390 deleted=[]
391 if type(data) is tuple or type(data) is list:
392 for d in data:
393 a= self._remove_extra_items(d, schema['items'])
394 if a is not None: deleted.append(a)
395 elif type(data) is dict:
Adam Israel8e3ce872018-01-08 18:43:40 +0000396 for k in data.keys():
397 if 'properties' not in schema or k not in schema['properties'].keys():
tierno7edb6752016-03-21 17:37:52 +0100398 del data[k]
399 deleted.append(k)
400 else:
401 a = self._remove_extra_items(data[k], schema['properties'][k])
402 if a is not None: deleted.append({k:a})
403 if len(deleted) == 0: return None
404 elif len(deleted) == 1: return deleted[0]
405 else: return deleted
Adam Israel8e3ce872018-01-08 18:43:40 +0000406
tiernoae4a8d12016-07-08 12:30:39 +0200407 def _format_request_exception(self, request_exception):
408 '''Transform a request exception into a vimconn exception'''
409 if isinstance(request_exception, js_e.ValidationError):
Adam Israel8e3ce872018-01-08 18:43:40 +0000410 raise vimconn.vimconnUnexpectedResponse("jsonschema exception '{}' at '{}'".format(request_exception.message, request_exception.path))
tiernoae4a8d12016-07-08 12:30:39 +0200411 elif isinstance(request_exception, requests.exceptions.HTTPError):
412 raise vimconn.vimconnUnexpectedResponse(type(request_exception).__name__ + ": " + str(request_exception))
tierno7edb6752016-03-21 17:37:52 +0100413 else:
tiernoae4a8d12016-07-08 12:30:39 +0200414 raise vimconn.vimconnConnectionException(type(request_exception).__name__ + ": " + str(request_exception))
415
416 def _check_http_request_response(self, request_response):
417 '''Raise a vimconn exception if the response is not Ok'''
418 if request_response.status_code >= 200 and request_response.status_code < 300:
419 return
420 if request_response.status_code == vimconn.HTTP_Unauthorized:
421 raise vimconn.vimconnAuthException(request_response.text)
422 elif request_response.status_code == vimconn.HTTP_Not_Found:
423 raise vimconn.vimconnNotFoundException(request_response.text)
424 elif request_response.status_code == vimconn.HTTP_Conflict:
425 raise vimconn.vimconnConflictException(request_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000426 else:
tiernoae4a8d12016-07-08 12:30:39 +0200427 raise vimconn.vimconnUnexpectedResponse("VIM HTTP_response {}, {}".format(request_response.status_code, str(request_response.text)))
428
tierno7edb6752016-03-21 17:37:52 +0100429 def new_tenant(self,tenant_name,tenant_description):
tiernoae4a8d12016-07-08 12:30:39 +0200430 '''Adds a new tenant to VIM with this name and description, returns the tenant identifier'''
Adam Israel8e3ce872018-01-08 18:43:40 +0000431 #print("VIMConnector: Adding a new tenant to VIM")
tierno7edb6752016-03-21 17:37:52 +0100432 payload_dict = {"tenant": {"name":tenant_name,"description": tenant_description, "enabled": True}}
433 payload_req = json.dumps(payload_dict)
tierno7edb6752016-03-21 17:37:52 +0100434 try:
tiernoae4a8d12016-07-08 12:30:39 +0200435 url = self.url_admin+'/tenants'
436 self.logger.info("Adding a new tenant %s", url)
437 vim_response = requests.post(url, headers = self.headers_req, data=payload_req)
438 self._check_http_request_response(vim_response)
439 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000440 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200441 response = vim_response.json()
442 js_v(response, new_tenant_response_schema)
443 #r = self._remove_extra_items(response, new_tenant_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +0000444 #if r is not None:
tiernoae4a8d12016-07-08 12:30:39 +0200445 # self.logger.warn("Warning: remove extra items %s", str(r))
446 tenant_id = response['tenant']['id']
447 return tenant_id
448 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
449 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100450
tiernoae4a8d12016-07-08 12:30:39 +0200451 def delete_tenant(self,tenant_id):
452 '''Delete a tenant from VIM. Returns the old tenant identifier'''
tierno7edb6752016-03-21 17:37:52 +0100453 try:
tiernoae4a8d12016-07-08 12:30:39 +0200454 url = self.url_admin+'/tenants/'+tenant_id
455 self.logger.info("Delete a tenant DELETE %s", url)
456 vim_response = requests.delete(url, headers = self.headers_req)
457 self._check_http_request_response(vim_response)
458 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000459 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200460 return tenant_id
461 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
462 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100463
464 def get_tenant_list(self, filter_dict={}):
465 '''Obtain tenants of VIM
tiernoae4a8d12016-07-08 12:30:39 +0200466 filter_dict can contain the following keys:
467 name: filter by tenant name
468 id: filter by tenant uuid/id
469 <other VIM specific>
470 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
tierno7edb6752016-03-21 17:37:52 +0100471 '''
tierno7edb6752016-03-21 17:37:52 +0100472 filterquery=[]
473 filterquery_text=''
Adam Israel8e3ce872018-01-08 18:43:40 +0000474 for k,v in filter_dict.iteritems():
tierno7edb6752016-03-21 17:37:52 +0100475 filterquery.append(str(k)+'='+str(v))
476 if len(filterquery)>0:
477 filterquery_text='?'+ '&'.join(filterquery)
tierno7edb6752016-03-21 17:37:52 +0100478 try:
tiernoae4a8d12016-07-08 12:30:39 +0200479 url = self.url+'/tenants'+filterquery_text
480 self.logger.info("get_tenant_list GET %s", url)
481 vim_response = requests.get(url, headers = self.headers_req)
482 self._check_http_request_response(vim_response)
483 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000484 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200485 return vim_response.json()["tenants"]
486 except requests.exceptions.RequestException as e:
487 self._format_request_exception(e)
488
tiernoa7d34d02017-02-23 14:42:07 +0100489 def new_network(self,net_name, net_type, ip_profile=None, shared=False, vlan=None): #, **vim_specific):
tiernoae4a8d12016-07-08 12:30:39 +0200490 '''Adds a tenant network to VIM'''
491 '''Returns the network identifier'''
492 try:
493 self._get_my_tenant()
494 if net_type=="bridge":
495 net_type="bridge_data"
496 payload_req = {"name": net_name, "type": net_type, "tenant_id": self.tenant, "shared": shared}
tiernoa7d34d02017-02-23 14:42:07 +0100497 if vlan:
498 payload_req["provider:vlan"] = vlan
499 # payload_req.update(vim_specific)
tiernoae4a8d12016-07-08 12:30:39 +0200500 url = self.url+'/networks'
501 self.logger.info("Adding a new network POST: %s DATA: %s", url, str(payload_req))
502 vim_response = requests.post(url, headers = self.headers_req, data=json.dumps({"network": payload_req}) )
503 self._check_http_request_response(vim_response)
504 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000505 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200506 response = vim_response.json()
507 js_v(response, new_network_response_schema)
508 #r = self._remove_extra_items(response, new_network_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +0000509 #if r is not None:
tiernoae4a8d12016-07-08 12:30:39 +0200510 # self.logger.warn("Warning: remove extra items %s", str(r))
511 network_id = response['network']['id']
512 return network_id
513 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
514 self._format_request_exception(e)
Adam Israel8e3ce872018-01-08 18:43:40 +0000515
tierno7edb6752016-03-21 17:37:52 +0100516 def get_network_list(self, filter_dict={}):
517 '''Obtain tenant networks of VIM
518 Filter_dict can be:
519 name: network name
520 id: network uuid
tiernoae4a8d12016-07-08 12:30:39 +0200521 public: boolean
tierno7edb6752016-03-21 17:37:52 +0100522 tenant_id: tenant
523 admin_state_up: boolean
524 status: 'ACTIVE'
525 Returns the network list of dictionaries
526 '''
tierno7edb6752016-03-21 17:37:52 +0100527 try:
tiernoae4a8d12016-07-08 12:30:39 +0200528 if 'tenant_id' not in filter_dict:
529 filter_dict["tenant_id"] = self._get_my_tenant()
530 elif not filter_dict["tenant_id"]:
531 del filter_dict["tenant_id"]
532 filterquery=[]
533 filterquery_text=''
Adam Israel8e3ce872018-01-08 18:43:40 +0000534 for k,v in filter_dict.iteritems():
tiernoae4a8d12016-07-08 12:30:39 +0200535 filterquery.append(str(k)+'='+str(v))
536 if len(filterquery)>0:
537 filterquery_text='?'+ '&'.join(filterquery)
538 url = self.url+'/networks'+filterquery_text
539 self.logger.info("Getting network list GET %s", url)
540 vim_response = requests.get(url, headers = self.headers_req)
541 self._check_http_request_response(vim_response)
542 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000543 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200544 response = vim_response.json()
545 return response['networks']
546 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
547 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100548
tiernoae4a8d12016-07-08 12:30:39 +0200549 def get_network(self, net_id):
550 '''Obtain network details of network id'''
551 try:
552 url = self.url+'/networks/'+net_id
553 self.logger.info("Getting network GET %s", url)
554 vim_response = requests.get(url, headers = self.headers_req)
555 self._check_http_request_response(vim_response)
556 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000557 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200558 response = vim_response.json()
559 return response['network']
560 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
561 self._format_request_exception(e)
Adam Israel8e3ce872018-01-08 18:43:40 +0000562
tiernoae4a8d12016-07-08 12:30:39 +0200563 def delete_network(self, net_id):
tierno7edb6752016-03-21 17:37:52 +0100564 '''Deletes a tenant network from VIM'''
565 '''Returns the network identifier'''
tierno392f2852016-05-13 12:28:55 +0200566 try:
567 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +0200568 url = self.url+'/networks/'+net_id
569 self.logger.info("Deleting VIM network DELETE %s", url)
570 vim_response = requests.delete(url, headers=self.headers_req)
571 self._check_http_request_response(vim_response)
572 #self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000573 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200574 return net_id
575 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
576 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100577
tiernoae4a8d12016-07-08 12:30:39 +0200578 def get_flavor(self, flavor_id):
579 '''Obtain flavor details from the VIM'''
tierno392f2852016-05-13 12:28:55 +0200580 try:
581 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +0200582 url = self.url+'/'+self.tenant+'/flavors/'+flavor_id
583 self.logger.info("Getting flavor GET %s", url)
584 vim_response = requests.get(url, headers = self.headers_req)
585 self._check_http_request_response(vim_response)
586 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000587 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200588 response = vim_response.json()
589 js_v(response, get_flavor_response_schema)
590 r = self._remove_extra_items(response, get_flavor_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +0000591 if r is not None:
tiernoae4a8d12016-07-08 12:30:39 +0200592 self.logger.warn("Warning: remove extra items %s", str(r))
593 return response['flavor']
594 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
595 self._format_request_exception(e)
Adam Israel8e3ce872018-01-08 18:43:40 +0000596
tiernoae4a8d12016-07-08 12:30:39 +0200597 def new_flavor(self, flavor_data):
tierno7edb6752016-03-21 17:37:52 +0100598 '''Adds a tenant flavor to VIM'''
599 '''Returns the flavor identifier'''
tierno392f2852016-05-13 12:28:55 +0200600 try:
tierno7432e7c2017-01-04 16:54:37 +0100601 new_flavor_dict = flavor_data.copy()
tierno714bc902017-06-06 18:25:15 +0200602 for device in new_flavor_dict.get('extended', {}).get('devices', ()):
603 if 'image name' in device:
604 del device['image name']
tierno7432e7c2017-01-04 16:54:37 +0100605 new_flavor_dict["name"] = flavor_data["name"][:64]
tierno392f2852016-05-13 12:28:55 +0200606 self._get_my_tenant()
tierno7432e7c2017-01-04 16:54:37 +0100607 payload_req = json.dumps({'flavor': new_flavor_dict})
tiernoae4a8d12016-07-08 12:30:39 +0200608 url = self.url+'/'+self.tenant+'/flavors'
609 self.logger.info("Adding a new VIM flavor POST %s", url)
tierno392f2852016-05-13 12:28:55 +0200610 vim_response = requests.post(url, headers = self.headers_req, data=payload_req)
tiernoae4a8d12016-07-08 12:30:39 +0200611 self._check_http_request_response(vim_response)
612 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000613 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200614 response = vim_response.json()
615 js_v(response, new_flavor_response_schema)
616 r = self._remove_extra_items(response, new_flavor_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +0000617 if r is not None:
tiernoae4a8d12016-07-08 12:30:39 +0200618 self.logger.warn("Warning: remove extra items %s", str(r))
619 flavor_id = response['flavor']['id']
620 return flavor_id
621 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
622 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100623
tiernoae4a8d12016-07-08 12:30:39 +0200624 def delete_flavor(self,flavor_id):
625 '''Deletes a tenant flavor from VIM'''
626 '''Returns the old flavor_id'''
tierno392f2852016-05-13 12:28:55 +0200627 try:
628 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +0200629 url = self.url+'/'+self.tenant+'/flavors/'+flavor_id
630 self.logger.info("Deleting VIM flavor DELETE %s", url)
631 vim_response = requests.delete(url, headers=self.headers_req)
632 self._check_http_request_response(vim_response)
633 #self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000634 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200635 return flavor_id
636 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
637 self._format_request_exception(e)
638
639 def get_image(self, image_id):
640 '''Obtain image details from the VIM'''
tierno7edb6752016-03-21 17:37:52 +0100641 try:
tiernoae4a8d12016-07-08 12:30:39 +0200642 self._get_my_tenant()
643 url = self.url+'/'+self.tenant+'/images/'+image_id
644 self.logger.info("Getting image GET %s", url)
645 vim_response = requests.get(url, headers = self.headers_req)
646 self._check_http_request_response(vim_response)
647 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000648 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200649 response = vim_response.json()
650 js_v(response, get_image_response_schema)
651 r = self._remove_extra_items(response, get_image_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +0000652 if r is not None:
tiernoae4a8d12016-07-08 12:30:39 +0200653 self.logger.warn("Warning: remove extra items %s", str(r))
654 return response['image']
655 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
656 self._format_request_exception(e)
657
658 def new_image(self,image_dict):
659 ''' Adds a tenant image to VIM, returns image_id'''
660 try:
661 self._get_my_tenant()
tierno7432e7c2017-01-04 16:54:37 +0100662 new_image_dict={'name': image_dict['name'][:64]}
tiernoae4a8d12016-07-08 12:30:39 +0200663 if image_dict.get('description'):
664 new_image_dict['description'] = image_dict['description']
665 if image_dict.get('metadata'):
666 new_image_dict['metadata'] = yaml.load(image_dict['metadata'])
667 if image_dict.get('location'):
668 new_image_dict['path'] = image_dict['location']
669 payload_req = json.dumps({"image":new_image_dict})
670 url=self.url + '/' + self.tenant + '/images'
671 self.logger.info("Adding a new VIM image POST %s", url)
672 vim_response = requests.post(url, headers = self.headers_req, data=payload_req)
673 self._check_http_request_response(vim_response)
674 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000675 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200676 response = vim_response.json()
677 js_v(response, new_image_response_schema)
678 r = self._remove_extra_items(response, new_image_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +0000679 if r is not None:
tiernoae4a8d12016-07-08 12:30:39 +0200680 self.logger.warn("Warning: remove extra items %s", str(r))
681 image_id = response['image']['id']
682 return image_id
683 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
684 self._format_request_exception(e)
Adam Israel8e3ce872018-01-08 18:43:40 +0000685
tiernoae4a8d12016-07-08 12:30:39 +0200686 def delete_image(self, image_id):
687 '''Deletes a tenant image from VIM'''
688 '''Returns the deleted image_id'''
689 try:
690 self._get_my_tenant()
691 url = self.url + '/'+ self.tenant +'/images/'+image_id
692 self.logger.info("Deleting VIM image DELETE %s", url)
693 vim_response = requests.delete(url, headers=self.headers_req)
694 self._check_http_request_response(vim_response)
695 #self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000696 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200697 return image_id
698 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
699 self._format_request_exception(e)
700
tiernoae4a8d12016-07-08 12:30:39 +0200701 def get_image_id_from_path(self, path):
garciadeblasb69fa9f2016-09-28 12:04:10 +0200702 '''Get the image id from image path in the VIM database. Returns the image_id'''
tiernoae4a8d12016-07-08 12:30:39 +0200703 try:
704 self._get_my_tenant()
tiernobe41e222016-09-02 15:16:13 +0200705 url=self.url + '/' + self.tenant + '/images?path='+quote(path)
tiernoae4a8d12016-07-08 12:30:39 +0200706 self.logger.info("Getting images GET %s", url)
707 vim_response = requests.get(url)
708 self._check_http_request_response(vim_response)
709 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000710 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200711 response = vim_response.json()
712 js_v(response, get_images_response_schema)
713 #r = self._remove_extra_items(response, get_images_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +0000714 #if r is not None:
tiernoae4a8d12016-07-08 12:30:39 +0200715 # self.logger.warn("Warning: remove extra items %s", str(r))
716 if len(response['images'])==0:
717 raise vimconn.vimconnNotFoundException("Image not found at VIM with path '%s'", path)
718 elif len(response['images'])>1:
719 raise vimconn.vimconnConflictException("More than one image found at VIM with path '%s'", path)
720 return response['images'][0]['id']
721 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
722 self._format_request_exception(e)
723
garciadeblasb69fa9f2016-09-28 12:04:10 +0200724 def get_image_list(self, filter_dict={}):
725 '''Obtain tenant images from VIM
726 Filter_dict can be:
727 name: image name
728 id: image uuid
729 checksum: image checksum
730 location: image path
731 Returns the image list of dictionaries:
732 [{<the fields at Filter_dict plus some VIM specific>}, ...]
733 List can be empty
734 '''
735 try:
736 self._get_my_tenant()
737 filterquery=[]
738 filterquery_text=''
Adam Israel8e3ce872018-01-08 18:43:40 +0000739 for k,v in filter_dict.iteritems():
garciadeblasb69fa9f2016-09-28 12:04:10 +0200740 filterquery.append(str(k)+'='+str(v))
741 if len(filterquery)>0:
742 filterquery_text='?'+ '&'.join(filterquery)
743 url = self.url+'/'+self.tenant+'/images'+filterquery_text
744 self.logger.info("Getting image list GET %s", url)
745 vim_response = requests.get(url, headers = self.headers_req)
746 self._check_http_request_response(vim_response)
747 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000748 #print(json.dumps(vim_response.json(), indent=4))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200749 response = vim_response.json()
750 return response['images']
751 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
752 self._format_request_exception(e)
753
tiernoae4a8d12016-07-08 12:30:39 +0200754 def new_vminstancefromJSON(self, vm_data):
tierno7edb6752016-03-21 17:37:52 +0100755 '''Adds a VM instance to VIM'''
756 '''Returns the instance identifier'''
tierno392f2852016-05-13 12:28:55 +0200757 try:
758 self._get_my_tenant()
759 except Exception as e:
760 return -vimconn.HTTP_Not_Found, str(e)
Marco Ceppic4629bd2017-10-26 14:35:46 +0200761 print("VIMConnector: Adding a new VM instance from JSON to VIM")
tierno7edb6752016-03-21 17:37:52 +0100762 payload_req = vm_data
763 try:
tierno392f2852016-05-13 12:28:55 +0200764 vim_response = requests.post(self.url+'/'+self.tenant+'/servers', headers = self.headers_req, data=payload_req)
venkatamahesh6ecca182017-01-27 23:04:40 +0530765 except requests.exceptions.RequestException as e:
Marco Ceppic4629bd2017-10-26 14:35:46 +0200766 print("new_vminstancefromJSON Exception: ", e.args)
tierno7edb6752016-03-21 17:37:52 +0100767 return -vimconn.HTTP_Not_Found, str(e.args[0])
Marco Ceppic4629bd2017-10-26 14:35:46 +0200768 print(vim_response)
Adam Israel8e3ce872018-01-08 18:43:40 +0000769 #print(vim_response.status_code)
tierno7edb6752016-03-21 17:37:52 +0100770 if vim_response.status_code == 200:
Adam Israel8e3ce872018-01-08 18:43:40 +0000771 #print(vim_response.json())
772 #print(json.dumps(vim_response.json(), indent=4))
tierno7edb6752016-03-21 17:37:52 +0100773 res,http_content = self._format_in(vim_response, new_image_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +0000774 #print(http_content)
tierno7edb6752016-03-21 17:37:52 +0100775 if res:
776 r = self._remove_extra_items(http_content, new_image_response_schema)
Marco Ceppic4629bd2017-10-26 14:35:46 +0200777 if r is not None: print("Warning: remove extra items ", r)
Adam Israel8e3ce872018-01-08 18:43:40 +0000778 #print(http_content)
tierno7edb6752016-03-21 17:37:52 +0100779 vminstance_id = http_content['server']['id']
Marco Ceppic4629bd2017-10-26 14:35:46 +0200780 print("Tenant image id: ",vminstance_id)
tierno7edb6752016-03-21 17:37:52 +0100781 return vim_response.status_code,vminstance_id
782 else: return -vimconn.HTTP_Bad_Request,http_content
783 else:
Adam Israel8e3ce872018-01-08 18:43:40 +0000784 #print(vim_response.text)
tierno7edb6752016-03-21 17:37:52 +0100785 jsonerror = self._format_jsonerror(vim_response)
786 text = 'Error in VIM "%s": not possible to add new vm instance. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
Adam Israel8e3ce872018-01-08 18:43:40 +0000787 #print(text)
tierno7edb6752016-03-21 17:37:52 +0100788 return -vim_response.status_code,text
789
tierno5a3273c2017-08-29 11:43:46 +0200790 def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None,
791 availability_zone_index=None, availability_zone_list=None):
tierno7edb6752016-03-21 17:37:52 +0100792 '''Adds a VM instance to VIM
793 Params:
794 start: indicates if VM must start or boot in pause mode. Ignored
795 image_id,flavor_id: image and flavor uuid
796 net_list: list of interfaces, each one is a dictionary with:
797 name:
798 net_id: network uuid to connect
799 vpci: virtual vcpi to assign
800 model: interface model, virtio, e2000, ...
Adam Israel8e3ce872018-01-08 18:43:40 +0000801 mac_address:
tierno7edb6752016-03-21 17:37:52 +0100802 use: 'data', 'bridge', 'mgmt'
803 type: 'virtual', 'PF', 'VF', 'VFnotShared'
804 vim_id: filled/added by this function
805 #TODO ip, security groups
tiernoae4a8d12016-07-08 12:30:39 +0200806 Returns the instance identifier
tierno7edb6752016-03-21 17:37:52 +0100807 '''
tiernofa51c202017-01-27 14:58:17 +0100808 self.logger.debug("new_vminstance input: image='%s' flavor='%s' nics='%s'", image_id, flavor_id, str(net_list))
tierno392f2852016-05-13 12:28:55 +0200809 try:
810 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +0200811# net_list = []
812# for k,v in net_dict.items():
Adam Israel8e3ce872018-01-08 18:43:40 +0000813# print(k,v)
tiernoae4a8d12016-07-08 12:30:39 +0200814# net_list.append('{"name":"' + k + '", "uuid":"' + v + '"}')
Adam Israel8e3ce872018-01-08 18:43:40 +0000815# net_list_string = ', '.join(net_list)
tiernoae4a8d12016-07-08 12:30:39 +0200816 virtio_net_list=[]
817 for net in net_list:
tierno7edb6752016-03-21 17:37:52 +0100818 if not net.get("net_id"):
819 continue
tiernoae4a8d12016-07-08 12:30:39 +0200820 net_dict={'uuid': net["net_id"]}
821 if net.get("type"): net_dict["type"] = net["type"]
822 if net.get("name"): net_dict["name"] = net["name"]
823 if net.get("vpci"): net_dict["vpci"] = net["vpci"]
824 if net.get("model"): net_dict["model"] = net["model"]
825 if net.get("mac_address"): net_dict["mac_address"] = net["mac_address"]
826 virtio_net_list.append(net_dict)
tierno7432e7c2017-01-04 16:54:37 +0100827 payload_dict={ "name": name[:64],
tiernoae4a8d12016-07-08 12:30:39 +0200828 "description": description,
829 "imageRef": image_id,
830 "flavorRef": flavor_id,
831 "networks": virtio_net_list
832 }
833 if start != None:
834 payload_dict["start"] = start
835 payload_req = json.dumps({"server": payload_dict})
836 url = self.url+'/'+self.tenant+'/servers'
837 self.logger.info("Adding a new vm POST %s DATA %s", url, payload_req)
838 vim_response = requests.post(url, headers = self.headers_req, data=payload_req)
839 self._check_http_request_response(vim_response)
840 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000841 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200842 response = vim_response.json()
843 js_v(response, new_vminstance_response_schema)
844 #r = self._remove_extra_items(response, new_vminstance_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +0000845 #if r is not None:
tiernoae4a8d12016-07-08 12:30:39 +0200846 # self.logger.warn("Warning: remove extra items %s", str(r))
847 vminstance_id = response['server']['id']
848
849 #connect data plane interfaces to network
850 for net in net_list:
851 if net["type"]=="virtual":
852 if not net.get("net_id"):
853 continue
854 for iface in response['server']['networks']:
855 if "name" in net:
856 if net["name"]==iface["name"]:
857 net["vim_id"] = iface['iface_id']
858 break
859 elif "net_id" in net:
860 if net["net_id"]==iface["net_id"]:
861 net["vim_id"] = iface['iface_id']
862 break
863 else: #dataplane
864 for numa in response['server'].get('extended',{}).get('numas',() ):
865 for iface in numa.get('interfaces',() ):
866 if net['name'] == iface['name']:
867 net['vim_id'] = iface['iface_id']
Adam Israel8e3ce872018-01-08 18:43:40 +0000868 #Code bellow is not needed, current openvim connect dataplane interfaces
tiernoae4a8d12016-07-08 12:30:39 +0200869 #if net.get("net_id"):
870 ##connect dataplane interface
871 # result, port_id = self.connect_port_network(iface['iface_id'], net["net_id"])
872 # if result < 0:
873 # error_text = "Error attaching port %s to network %s: %s." % (iface['iface_id'], net["net_id"], port_id)
Adam Israel8e3ce872018-01-08 18:43:40 +0000874 # print("new_vminstance: " + error_text)
tiernoae4a8d12016-07-08 12:30:39 +0200875 # self.delete_vminstance(vminstance_id)
876 # return result, error_text
877 break
Adam Israel8e3ce872018-01-08 18:43:40 +0000878
tiernoae4a8d12016-07-08 12:30:39 +0200879 return vminstance_id
880 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
881 self._format_request_exception(e)
Adam Israel8e3ce872018-01-08 18:43:40 +0000882
tiernoae4a8d12016-07-08 12:30:39 +0200883 def get_vminstance(self, vm_id):
tierno7edb6752016-03-21 17:37:52 +0100884 '''Returns the VM instance information from VIM'''
tierno392f2852016-05-13 12:28:55 +0200885 try:
886 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +0200887 url = self.url+'/'+self.tenant+'/servers/'+vm_id
888 self.logger.info("Getting vm GET %s", url)
tierno392f2852016-05-13 12:28:55 +0200889 vim_response = requests.get(url, headers = self.headers_req)
tiernoae4a8d12016-07-08 12:30:39 +0200890 vim_response = requests.get(url, headers = self.headers_req)
891 self._check_http_request_response(vim_response)
892 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000893 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200894 response = vim_response.json()
895 js_v(response, new_vminstance_response_schema)
896 #r = self._remove_extra_items(response, new_vminstance_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +0000897 #if r is not None:
tiernoae4a8d12016-07-08 12:30:39 +0200898 # self.logger.warn("Warning: remove extra items %s", str(r))
899 return response['server']
900 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
901 self._format_request_exception(e)
Adam Israel8e3ce872018-01-08 18:43:40 +0000902
tiernoae4a8d12016-07-08 12:30:39 +0200903 def delete_vminstance(self, vm_id):
904 '''Removes a VM instance from VIM, returns the deleted vm_id'''
tierno392f2852016-05-13 12:28:55 +0200905 try:
906 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +0200907 url = self.url+'/'+self.tenant+'/servers/'+vm_id
908 self.logger.info("Deleting VIM vm DELETE %s", url)
909 vim_response = requests.delete(url, headers=self.headers_req)
910 self._check_http_request_response(vim_response)
911 #self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +0000912 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +0200913 return vm_id
914 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
915 self._format_request_exception(e)
916
917 def refresh_vms_status(self, vm_list):
918 '''Refreshes the status of the virtual machines'''
tierno7edb6752016-03-21 17:37:52 +0100919 try:
tiernoae4a8d12016-07-08 12:30:39 +0200920 self._get_my_tenant()
921 except requests.exceptions.RequestException as e:
922 self._format_request_exception(e)
923 vm_dict={}
924 for vm_id in vm_list:
925 vm={}
Adam Israel8e3ce872018-01-08 18:43:40 +0000926 #print("VIMConnector refresh_tenant_vms and nets: Getting tenant VM instance information from VIM")
tiernoae4a8d12016-07-08 12:30:39 +0200927 try:
928 url = self.url+'/'+self.tenant+'/servers/'+ vm_id
929 self.logger.info("Getting vm GET %s", url)
930 vim_response = requests.get(url, headers = self.headers_req)
931 self._check_http_request_response(vim_response)
932 response = vim_response.json()
933 js_v(response, new_vminstance_response_schema)
934 if response['server']['status'] in vmStatus2manoFormat:
935 vm['status'] = vmStatus2manoFormat[ response['server']['status'] ]
936 else:
937 vm['status'] = "OTHER"
938 vm['error_msg'] = "VIM status reported " + response['server']['status']
939 if response['server'].get('last_error'):
940 vm['error_msg'] = response['server']['last_error']
941 vm["vim_info"] = yaml.safe_dump(response['server'])
942 #get interfaces info
943 try:
944 management_ip = False
tiernobe41e222016-09-02 15:16:13 +0200945 url2 = self.url+'/ports?device_id='+ quote(vm_id)
tiernoae4a8d12016-07-08 12:30:39 +0200946 self.logger.info("Getting PORTS GET %s", url2)
947 vim_response2 = requests.get(url2, headers = self.headers_req)
948 self._check_http_request_response(vim_response2)
949 client_data = vim_response2.json()
950 if isinstance(client_data.get("ports"), list):
951 vm["interfaces"]=[]
952 for port in client_data.get("ports"):
953 interface={}
954 interface['vim_info'] = yaml.safe_dump(port)
955 interface["mac_address"] = port.get("mac_address")
tierno8e995ce2016-09-22 08:13:00 +0000956 interface["vim_net_id"] = port.get("network_id")
tiernoae4a8d12016-07-08 12:30:39 +0200957 interface["vim_interface_id"] = port["id"]
958 interface["ip_address"] = port.get("ip_address")
959 if interface["ip_address"]:
960 management_ip = True
961 if interface["ip_address"] == "0.0.0.0":
962 interface["ip_address"] = None
963 vm["interfaces"].append(interface)
Adam Israel8e3ce872018-01-08 18:43:40 +0000964
tiernoae4a8d12016-07-08 12:30:39 +0200965 except Exception as e:
966 self.logger.error("refresh_vms_and_nets. Port get %s: %s", type(e).__name__, str(e))
tierno7edb6752016-03-21 17:37:52 +0100967
tiernoae4a8d12016-07-08 12:30:39 +0200968 if vm['status'] == "ACTIVE" and not management_ip:
969 vm['status'] = "ACTIVE:NoMgmtIP"
Adam Israel8e3ce872018-01-08 18:43:40 +0000970
tiernoae4a8d12016-07-08 12:30:39 +0200971 except vimconn.vimconnNotFoundException as e:
972 self.logger.error("Exception getting vm status: %s", str(e))
973 vm['status'] = "DELETED"
974 vm['error_msg'] = str(e)
975 except (requests.exceptions.RequestException, js_e.ValidationError, vimconn.vimconnException) as e:
976 self.logger.error("Exception getting vm status: %s", str(e))
977 vm['status'] = "VIM_ERROR"
978 vm['error_msg'] = str(e)
979 vm_dict[vm_id] = vm
980 return vm_dict
tierno7edb6752016-03-21 17:37:52 +0100981
tiernoae4a8d12016-07-08 12:30:39 +0200982 def refresh_nets_status(self, net_list):
983 '''Get the status of the networks
984 Params: the list of network identifiers
985 Returns a dictionary with:
986 net_id: #VIM id of this network
987 status: #Mandatory. Text with one of:
988 # DELETED (not found at vim)
Adam Israel8e3ce872018-01-08 18:43:40 +0000989 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
tiernoae4a8d12016-07-08 12:30:39 +0200990 # OTHER (Vim reported other status not understood)
991 # ERROR (VIM indicates an ERROR status)
Adam Israel8e3ce872018-01-08 18:43:40 +0000992 # ACTIVE, INACTIVE, DOWN (admin down),
tiernoae4a8d12016-07-08 12:30:39 +0200993 # BUILD (on building process)
994 #
Adam Israel8e3ce872018-01-08 18:43:40 +0000995 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
tiernoae4a8d12016-07-08 12:30:39 +0200996 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
997
tierno7edb6752016-03-21 17:37:52 +0100998 '''
tierno392f2852016-05-13 12:28:55 +0200999 try:
1000 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +02001001 except requests.exceptions.RequestException as e:
1002 self._format_request_exception(e)
Adam Israel8e3ce872018-01-08 18:43:40 +00001003
tiernoae4a8d12016-07-08 12:30:39 +02001004 net_dict={}
1005 for net_id in net_list:
1006 net = {}
Adam Israel8e3ce872018-01-08 18:43:40 +00001007 #print("VIMConnector refresh_tenant_vms_and_nets: Getting tenant network from VIM (tenant: " + str(self.tenant) + "): ")
tierno7edb6752016-03-21 17:37:52 +01001008 try:
tiernoae4a8d12016-07-08 12:30:39 +02001009 net_vim = self.get_network(net_id)
1010 if net_vim['status'] in netStatus2manoFormat:
1011 net["status"] = netStatus2manoFormat[ net_vim['status'] ]
tierno7edb6752016-03-21 17:37:52 +01001012 else:
tiernoae4a8d12016-07-08 12:30:39 +02001013 net["status"] = "OTHER"
1014 net["error_msg"] = "VIM status reported " + net_vim['status']
Adam Israel8e3ce872018-01-08 18:43:40 +00001015
tiernoae4a8d12016-07-08 12:30:39 +02001016 if net["status"] == "ACTIVE" and not net_vim['admin_state_up']:
1017 net["status"] = "DOWN"
1018 if net_vim.get('last_error'):
1019 net['error_msg'] = net_vim['last_error']
1020 net["vim_info"] = yaml.safe_dump(net_vim)
1021 except vimconn.vimconnNotFoundException as e:
1022 self.logger.error("Exception getting net status: %s", str(e))
1023 net['status'] = "DELETED"
1024 net['error_msg'] = str(e)
1025 except (requests.exceptions.RequestException, js_e.ValidationError, vimconn.vimconnException) as e:
1026 self.logger.error("Exception getting net status: %s", str(e))
1027 net['status'] = "VIM_ERROR"
1028 net['error_msg'] = str(e)
1029 net_dict[net_id] = net
1030 return net_dict
Adam Israel8e3ce872018-01-08 18:43:40 +00001031
tiernoae4a8d12016-07-08 12:30:39 +02001032 def action_vminstance(self, vm_id, action_dict):
tierno7edb6752016-03-21 17:37:52 +01001033 '''Send and action over a VM instance from VIM'''
1034 '''Returns the status'''
tierno392f2852016-05-13 12:28:55 +02001035 try:
1036 self._get_my_tenant()
tierno7edb6752016-03-21 17:37:52 +01001037 if "console" in action_dict:
tiernoae4a8d12016-07-08 12:30:39 +02001038 raise vimconn.vimconnException("getting console is not available at openvim", http_code=vimconn.HTTP_Service_Unavailable)
1039 url = self.url+'/'+self.tenant+'/servers/'+vm_id+"/action"
1040 self.logger.info("Action over VM instance POST %s", url)
1041 vim_response = requests.post(url, headers = self.headers_req, data=json.dumps(action_dict) )
1042 self._check_http_request_response(vim_response)
1043 return vm_id
1044 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
1045 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001046
Adam Israel8e3ce872018-01-08 18:43:40 +00001047#NOT USED METHODS in current version
1048
tierno7edb6752016-03-21 17:37:52 +01001049 def host_vim2gui(self, host, server_dict):
1050 '''Transform host dictionary from VIM format to GUI format,
1051 and append to the server_dict
1052 '''
Adam Israel8e3ce872018-01-08 18:43:40 +00001053 if type(server_dict) is not dict:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001054 print('vimconnector.host_vim2gui() ERROR, param server_dict must be a dictionary')
tierno7edb6752016-03-21 17:37:52 +01001055 return
1056 RAD={}
1057 occupation={}
1058 for numa in host['host']['numas']:
1059 RAD_item={}
1060 occupation_item={}
1061 #memory
1062 RAD_item['memory']={'size': str(numa['memory'])+'GB', 'eligible': str(numa['hugepages'])+'GB'}
1063 occupation_item['memory']= str(numa['hugepages_consumed'])+'GB'
1064 #cpus
1065 RAD_item['cpus']={}
1066 RAD_item['cpus']['cores'] = []
1067 RAD_item['cpus']['eligible_cores'] = []
1068 occupation_item['cores']=[]
1069 for _ in range(0, len(numa['cores']) / 2):
1070 RAD_item['cpus']['cores'].append( [] )
1071 for core in numa['cores']:
1072 RAD_item['cpus']['cores'][core['core_id']].append(core['thread_id'])
1073 if not 'status' in core: RAD_item['cpus']['eligible_cores'].append(core['thread_id'])
1074 if 'instance_id' in core: occupation_item['cores'].append(core['thread_id'])
1075 #ports
1076 RAD_item['ports']={}
1077 occupation_item['ports']={}
1078 for iface in numa['interfaces']:
1079 RAD_item['ports'][ iface['pci'] ] = 'speed:'+str(iface['Mbps'])+'M'
1080 occupation_item['ports'][ iface['pci'] ] = { 'occupied': str(100*iface['Mbps_consumed'] / iface['Mbps']) + "%" }
Adam Israel8e3ce872018-01-08 18:43:40 +00001081
tierno7edb6752016-03-21 17:37:52 +01001082 RAD[ numa['numa_socket'] ] = RAD_item
1083 occupation[ numa['numa_socket'] ] = occupation_item
1084 server_dict[ host['host']['name'] ] = {'RAD':RAD, 'occupation':occupation}
1085
1086 def get_hosts_info(self):
1087 '''Get the information of deployed hosts
1088 Returns the hosts content'''
1089 #obtain hosts list
1090 url=self.url+'/hosts'
1091 try:
1092 vim_response = requests.get(url)
venkatamahesh6ecca182017-01-27 23:04:40 +05301093 except requests.exceptions.RequestException as e:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001094 print("get_hosts_info Exception: ", e.args)
tierno7edb6752016-03-21 17:37:52 +01001095 return -vimconn.HTTP_Not_Found, str(e.args[0])
Marco Ceppic4629bd2017-10-26 14:35:46 +02001096 print("vim get", url, "response:", vim_response.status_code, vim_response.json())
Adam Israel8e3ce872018-01-08 18:43:40 +00001097 #print(vim_response.status_code)
1098 #print(json.dumps(vim_response.json(), indent=4))
tierno7edb6752016-03-21 17:37:52 +01001099 if vim_response.status_code != 200:
1100 #TODO: get error
Marco Ceppic4629bd2017-10-26 14:35:46 +02001101 print('vimconnector.get_hosts_info error getting host list %d %s' %(vim_response.status_code, vim_response.json()))
tierno7edb6752016-03-21 17:37:52 +01001102 return -vim_response.status_code, "Error getting host list"
Adam Israel8e3ce872018-01-08 18:43:40 +00001103
tierno7edb6752016-03-21 17:37:52 +01001104 res,hosts = self._format_in(vim_response, get_hosts_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +00001105
tierno7edb6752016-03-21 17:37:52 +01001106 if res==False:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001107 print("vimconnector.get_hosts_info error parsing GET HOSTS vim response", hosts)
tierno7edb6752016-03-21 17:37:52 +01001108 return vimconn.HTTP_Internal_Server_Error, hosts
1109 #obtain hosts details
1110 hosts_dict={}
1111 for host in hosts['hosts']:
1112 url=self.url+'/hosts/'+host['id']
1113 try:
1114 vim_response = requests.get(url)
venkatamahesh6ecca182017-01-27 23:04:40 +05301115 except requests.exceptions.RequestException as e:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001116 print("get_hosts_info Exception: ", e.args)
tierno7edb6752016-03-21 17:37:52 +01001117 return -vimconn.HTTP_Not_Found, str(e.args[0])
Marco Ceppic4629bd2017-10-26 14:35:46 +02001118 print("vim get", url, "response:", vim_response.status_code, vim_response.json())
tierno7edb6752016-03-21 17:37:52 +01001119 if vim_response.status_code != 200:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001120 print('vimconnector.get_hosts_info error getting detailed host %d %s' %(vim_response.status_code, vim_response.json()))
tierno7edb6752016-03-21 17:37:52 +01001121 continue
1122 res,host_detail = self._format_in(vim_response, get_host_detail_response_schema)
1123 if res==False:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001124 print("vimconnector.get_hosts_info error parsing GET HOSTS/%s vim response" % host['id'], host_detail)
tierno7edb6752016-03-21 17:37:52 +01001125 continue
Adam Israel8e3ce872018-01-08 18:43:40 +00001126 #print('host id '+host['id'], json.dumps(host_detail, indent=4))
tierno7edb6752016-03-21 17:37:52 +01001127 self.host_vim2gui(host_detail, hosts_dict)
1128 return 200, hosts_dict
1129
1130 def get_hosts(self, vim_tenant):
1131 '''Get the hosts and deployed instances
1132 Returns the hosts content'''
1133 #obtain hosts list
1134 url=self.url+'/hosts'
1135 try:
1136 vim_response = requests.get(url)
venkatamahesh6ecca182017-01-27 23:04:40 +05301137 except requests.exceptions.RequestException as e:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001138 print("get_hosts Exception: ", e.args)
tierno7edb6752016-03-21 17:37:52 +01001139 return -vimconn.HTTP_Not_Found, str(e.args[0])
Marco Ceppic4629bd2017-10-26 14:35:46 +02001140 print("vim get", url, "response:", vim_response.status_code, vim_response.json())
Adam Israel8e3ce872018-01-08 18:43:40 +00001141 #print(vim_response.status_code)
1142 #print(json.dumps(vim_response.json(), indent=4))
tierno7edb6752016-03-21 17:37:52 +01001143 if vim_response.status_code != 200:
1144 #TODO: get error
Marco Ceppic4629bd2017-10-26 14:35:46 +02001145 print('vimconnector.get_hosts error getting host list %d %s' %(vim_response.status_code, vim_response.json()))
tierno7edb6752016-03-21 17:37:52 +01001146 return -vim_response.status_code, "Error getting host list"
Adam Israel8e3ce872018-01-08 18:43:40 +00001147
tierno7edb6752016-03-21 17:37:52 +01001148 res,hosts = self._format_in(vim_response, get_hosts_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +00001149
tierno7edb6752016-03-21 17:37:52 +01001150 if res==False:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001151 print("vimconnector.get_host error parsing GET HOSTS vim response", hosts)
tierno7edb6752016-03-21 17:37:52 +01001152 return vimconn.HTTP_Internal_Server_Error, hosts
1153 #obtain instances from hosts
1154 for host in hosts['hosts']:
1155 url=self.url+'/' + vim_tenant + '/servers?hostId='+host['id']
1156 try:
1157 vim_response = requests.get(url)
venkatamahesh6ecca182017-01-27 23:04:40 +05301158 except requests.exceptions.RequestException as e:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001159 print("get_hosts Exception: ", e.args)
tierno7edb6752016-03-21 17:37:52 +01001160 return -vimconn.HTTP_Not_Found, str(e.args[0])
Marco Ceppic4629bd2017-10-26 14:35:46 +02001161 print("vim get", url, "response:", vim_response.status_code, vim_response.json())
tierno7edb6752016-03-21 17:37:52 +01001162 if vim_response.status_code != 200:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001163 print('vimconnector.get_hosts error getting instances at host %d %s' %(vim_response.status_code, vim_response.json()))
tierno7edb6752016-03-21 17:37:52 +01001164 continue
1165 res,servers = self._format_in(vim_response, get_server_response_schema)
1166 if res==False:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001167 print("vimconnector.get_host error parsing GET SERVERS/%s vim response" % host['id'], servers)
tierno7edb6752016-03-21 17:37:52 +01001168 continue
Adam Israel8e3ce872018-01-08 18:43:40 +00001169 #print('host id '+host['id'], json.dumps(host_detail, indent=4))
tierno7edb6752016-03-21 17:37:52 +01001170 host['instances'] = servers['servers']
1171 return 200, hosts['hosts']
1172
1173 def get_processor_rankings(self):
1174 '''Get the processor rankings in the VIM database'''
1175 url=self.url+'/processor_ranking'
1176 try:
1177 vim_response = requests.get(url)
venkatamahesh6ecca182017-01-27 23:04:40 +05301178 except requests.exceptions.RequestException as e:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001179 print("get_processor_rankings Exception: ", e.args)
tierno7edb6752016-03-21 17:37:52 +01001180 return -vimconn.HTTP_Not_Found, str(e.args[0])
Marco Ceppic4629bd2017-10-26 14:35:46 +02001181 print("vim get", url, "response:", vim_response.status_code, vim_response.json())
Adam Israel8e3ce872018-01-08 18:43:40 +00001182 #print(vim_response.status_code)
1183 #print(json.dumps(vim_response.json(), indent=4))
tierno7edb6752016-03-21 17:37:52 +01001184 if vim_response.status_code != 200:
1185 #TODO: get error
Marco Ceppic4629bd2017-10-26 14:35:46 +02001186 print('vimconnector.get_processor_rankings error getting processor rankings %d %s' %(vim_response.status_code, vim_response.json()))
tierno7edb6752016-03-21 17:37:52 +01001187 return -vim_response.status_code, "Error getting processor rankings"
Adam Israel8e3ce872018-01-08 18:43:40 +00001188
tierno7edb6752016-03-21 17:37:52 +01001189 res,rankings = self._format_in(vim_response, get_processor_rankings_response_schema)
1190 return res, rankings['rankings']
Adam Israel8e3ce872018-01-08 18:43:40 +00001191
tiernoae4a8d12016-07-08 12:30:39 +02001192 def new_host(self, host_data):
1193 '''Adds a new host to VIM'''
1194 '''Returns status code of the VIM response'''
1195 payload_req = host_data
tierno392f2852016-05-13 12:28:55 +02001196 try:
tiernoae4a8d12016-07-08 12:30:39 +02001197 url = self.url_admin+'/hosts'
1198 self.logger.info("Adding a new host POST %s", url)
1199 vim_response = requests.post(url, headers = self.headers_req, data=payload_req)
1200 self._check_http_request_response(vim_response)
1201 self.logger.debug(vim_response.text)
Adam Israel8e3ce872018-01-08 18:43:40 +00001202 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +02001203 response = vim_response.json()
1204 js_v(response, new_host_response_schema)
1205 r = self._remove_extra_items(response, new_host_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +00001206 if r is not None:
tiernoae4a8d12016-07-08 12:30:39 +02001207 self.logger.warn("Warning: remove extra items %s", str(r))
1208 host_id = response['host']['id']
1209 return host_id
1210 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
1211 self._format_request_exception(e)
Adam Israel8e3ce872018-01-08 18:43:40 +00001212
tiernoae4a8d12016-07-08 12:30:39 +02001213 def new_external_port(self, port_data):
1214 '''Adds a external port to VIM'''
1215 '''Returns the port identifier'''
1216 #TODO change to logging exception code policies
Marco Ceppic4629bd2017-10-26 14:35:46 +02001217 print("VIMConnector: Adding a new external port")
tiernoae4a8d12016-07-08 12:30:39 +02001218 payload_req = port_data
tierno7edb6752016-03-21 17:37:52 +01001219 try:
tiernoae4a8d12016-07-08 12:30:39 +02001220 vim_response = requests.post(self.url_admin+'/ports', headers = self.headers_req, data=payload_req)
venkatamahesh6ecca182017-01-27 23:04:40 +05301221 except requests.exceptions.RequestException as e:
tiernoae4a8d12016-07-08 12:30:39 +02001222 self.logger.error("new_external_port Exception: ", str(e))
tierno7edb6752016-03-21 17:37:52 +01001223 return -vimconn.HTTP_Not_Found, str(e.args[0])
Marco Ceppic4629bd2017-10-26 14:35:46 +02001224 print(vim_response)
Adam Israel8e3ce872018-01-08 18:43:40 +00001225 #print(vim_response.status_code)
tiernoae4a8d12016-07-08 12:30:39 +02001226 if vim_response.status_code == 200:
Adam Israel8e3ce872018-01-08 18:43:40 +00001227 #print(vim_response.json())
1228 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +02001229 res, http_content = self._format_in(vim_response, new_port_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +00001230 #print(http_content)
tiernoae4a8d12016-07-08 12:30:39 +02001231 if res:
1232 r = self._remove_extra_items(http_content, new_port_response_schema)
Marco Ceppic4629bd2017-10-26 14:35:46 +02001233 if r is not None: print("Warning: remove extra items ", r)
Adam Israel8e3ce872018-01-08 18:43:40 +00001234 #print(http_content)
tiernoae4a8d12016-07-08 12:30:39 +02001235 port_id = http_content['port']['id']
Marco Ceppic4629bd2017-10-26 14:35:46 +02001236 print("Port id: ",port_id)
tiernoae4a8d12016-07-08 12:30:39 +02001237 return vim_response.status_code,port_id
1238 else: return -vimconn.HTTP_Bad_Request,http_content
1239 else:
Adam Israel8e3ce872018-01-08 18:43:40 +00001240 #print(vim_response.text)
tiernoae4a8d12016-07-08 12:30:39 +02001241 jsonerror = self._format_jsonerror(vim_response)
1242 text = 'Error in VIM "%s": not possible to add new external port. HTTP Response: %d. Error: %s' % (self.url_admin, vim_response.status_code, jsonerror)
Adam Israel8e3ce872018-01-08 18:43:40 +00001243 #print(text)
tiernoae4a8d12016-07-08 12:30:39 +02001244 return -vim_response.status_code,text
Adam Israel8e3ce872018-01-08 18:43:40 +00001245
tiernoae4a8d12016-07-08 12:30:39 +02001246 def new_external_network(self,net_name,net_type):
1247 '''Adds a external network to VIM (shared)'''
1248 '''Returns the network identifier'''
1249 #TODO change to logging exception code policies
Marco Ceppic4629bd2017-10-26 14:35:46 +02001250 print("VIMConnector: Adding external shared network to VIM (type " + net_type + "): "+ net_name)
Adam Israel8e3ce872018-01-08 18:43:40 +00001251
tiernoae4a8d12016-07-08 12:30:39 +02001252 payload_req = '{"network":{"name": "' + net_name + '","shared":true,"type": "' + net_type + '"}}'
1253 try:
1254 vim_response = requests.post(self.url+'/networks', headers = self.headers_req, data=payload_req)
venkatamahesh6ecca182017-01-27 23:04:40 +05301255 except requests.exceptions.RequestException as e:
tiernoae4a8d12016-07-08 12:30:39 +02001256 self.logger.error( "new_external_network Exception: ", e.args)
1257 return -vimconn.HTTP_Not_Found, str(e.args[0])
Marco Ceppic4629bd2017-10-26 14:35:46 +02001258 print(vim_response)
Adam Israel8e3ce872018-01-08 18:43:40 +00001259 #print(vim_response.status_code)
tiernoae4a8d12016-07-08 12:30:39 +02001260 if vim_response.status_code == 200:
Adam Israel8e3ce872018-01-08 18:43:40 +00001261 #print(vim_response.json())
1262 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +02001263 res,http_content = self._format_in(vim_response, new_network_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +00001264 #print(http_content)
tiernoae4a8d12016-07-08 12:30:39 +02001265 if res:
1266 r = self._remove_extra_items(http_content, new_network_response_schema)
Marco Ceppic4629bd2017-10-26 14:35:46 +02001267 if r is not None: print("Warning: remove extra items ", r)
Adam Israel8e3ce872018-01-08 18:43:40 +00001268 #print(http_content)
tiernoae4a8d12016-07-08 12:30:39 +02001269 network_id = http_content['network']['id']
Marco Ceppic4629bd2017-10-26 14:35:46 +02001270 print("Network id: ",network_id)
tiernoae4a8d12016-07-08 12:30:39 +02001271 return vim_response.status_code,network_id
1272 else: return -vimconn.HTTP_Bad_Request,http_content
1273 else:
Adam Israel8e3ce872018-01-08 18:43:40 +00001274 #print(vim_response.text)
tiernoae4a8d12016-07-08 12:30:39 +02001275 jsonerror = self._format_jsonerror(vim_response)
1276 text = 'Error in VIM "%s": not possible to add new external network. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
Adam Israel8e3ce872018-01-08 18:43:40 +00001277 #print(text)
tiernoae4a8d12016-07-08 12:30:39 +02001278 return -vim_response.status_code,text
Adam Israel8e3ce872018-01-08 18:43:40 +00001279
tiernoae4a8d12016-07-08 12:30:39 +02001280 def connect_port_network(self, port_id, network_id, admin=False):
1281 '''Connects a external port to a network'''
1282 '''Returns status code of the VIM response'''
1283 #TODO change to logging exception code policies
Marco Ceppic4629bd2017-10-26 14:35:46 +02001284 print("VIMConnector: Connecting external port to network")
Adam Israel8e3ce872018-01-08 18:43:40 +00001285
tiernoae4a8d12016-07-08 12:30:39 +02001286 payload_req = '{"port":{"network_id":"' + network_id + '"}}'
1287 if admin:
1288 if self.url_admin==None:
1289 return -vimconn.HTTP_Unauthorized, "datacenter cannot contain admin URL"
1290 url= self.url_admin
1291 else:
1292 url= self.url
1293 try:
1294 vim_response = requests.put(url +'/ports/'+port_id, headers = self.headers_req, data=payload_req)
venkatamahesh6ecca182017-01-27 23:04:40 +05301295 except requests.exceptions.RequestException as e:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001296 print("connect_port_network Exception: ", e.args)
tiernoae4a8d12016-07-08 12:30:39 +02001297 return -vimconn.HTTP_Not_Found, str(e.args[0])
Marco Ceppic4629bd2017-10-26 14:35:46 +02001298 print(vim_response)
Adam Israel8e3ce872018-01-08 18:43:40 +00001299 #print(vim_response.status_code)
tiernoae4a8d12016-07-08 12:30:39 +02001300 if vim_response.status_code == 200:
Adam Israel8e3ce872018-01-08 18:43:40 +00001301 #print(vim_response.json())
1302 #print(json.dumps(vim_response.json(), indent=4))
tiernoae4a8d12016-07-08 12:30:39 +02001303 res,http_content = self._format_in(vim_response, new_port_response_schema)
Adam Israel8e3ce872018-01-08 18:43:40 +00001304 #print(http_content)
tiernoae4a8d12016-07-08 12:30:39 +02001305 if res:
1306 r = self._remove_extra_items(http_content, new_port_response_schema)
Marco Ceppic4629bd2017-10-26 14:35:46 +02001307 if r is not None: print("Warning: remove extra items ", r)
Adam Israel8e3ce872018-01-08 18:43:40 +00001308 #print(http_content)
tiernoae4a8d12016-07-08 12:30:39 +02001309 port_id = http_content['port']['id']
Marco Ceppic4629bd2017-10-26 14:35:46 +02001310 print("Port id: ",port_id)
tiernoae4a8d12016-07-08 12:30:39 +02001311 return vim_response.status_code,port_id
1312 else: return -vimconn.HTTP_Bad_Request,http_content
1313 else:
Marco Ceppic4629bd2017-10-26 14:35:46 +02001314 print(vim_response.text)
tiernoae4a8d12016-07-08 12:30:39 +02001315 jsonerror = self._format_jsonerror(vim_response)
1316 text = 'Error in VIM "%s": not possible to connect external port to network. HTTP Response: %d. Error: %s' % (self.url_admin, vim_response.status_code, jsonerror)
Marco Ceppic4629bd2017-10-26 14:35:46 +02001317 print(text)
tiernoae4a8d12016-07-08 12:30:39 +02001318 return -vim_response.status_code,text