blob: 8fb02ccb345e19c8231dc1eef474eaeac42c9a77 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- coding: utf-8 -*-
2
3##
tierno92021022018-09-12 16:29:23 +02004# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
tierno7edb6752016-03-21 17:37:52 +01005# 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
tierno72774862020-05-04 11:44:15 +000030from osm_ro_plugin import vimconn
tierno7edb6752016-03-21 17:37:52 +010031import requests
32import json
33import yaml
tiernoae4a8d12016-07-08 12:30:39 +020034import logging
tierno66eba6e2017-11-10 17:09:18 +010035import math
tierno7d782ef2019-10-04 12:56:31 +000036from osm_ro.openmano_schemas import id_schema, name_schema, nameshort_schema, description_schema, \
tierno7edb6752016-03-21 17:37:52 +010037 vlan1000_schema, integer0_schema
38from jsonschema import validate as js_v, exceptions as js_e
tierno7d782ef2019-10-04 12:56:31 +000039from urllib.parse import quote
tierno7edb6752016-03-21 17:37:52 +010040
41'''contain the openvim virtual machine status to openmano status'''
42vmStatus2manoFormat={'ACTIVE':'ACTIVE',
43 'PAUSED':'PAUSED',
44 'SUSPENDED': 'SUSPENDED',
45 'INACTIVE':'INACTIVE',
46 'CREATING':'BUILD',
47 'ERROR':'ERROR','DELETED':'DELETED'
48 }
49netStatus2manoFormat={'ACTIVE':'ACTIVE','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED', 'DOWN':'DOWN'
50 }
51
52
53host_schema = {
54 "type":"object",
55 "properties":{
56 "id": id_schema,
57 "name": name_schema,
58 },
59 "required": ["id"]
60}
61image_schema = {
62 "type":"object",
63 "properties":{
64 "id": id_schema,
65 "name": name_schema,
66 },
67 "required": ["id","name"]
68}
69server_schema = {
70 "type":"object",
71 "properties":{
72 "id":id_schema,
73 "name": name_schema,
74 },
75 "required": ["id","name"]
76}
77new_host_response_schema = {
78 "title":"host response information schema",
79 "$schema": "http://json-schema.org/draft-04/schema#",
80 "type":"object",
81 "properties":{
82 "host": host_schema
83 },
84 "required": ["host"],
85 "additionalProperties": False
86}
87
88get_images_response_schema = {
89 "title":"openvim images response information schema",
90 "$schema": "http://json-schema.org/draft-04/schema#",
91 "type":"object",
92 "properties":{
93 "images":{
94 "type":"array",
95 "items": image_schema,
96 }
97 },
98 "required": ["images"],
99 "additionalProperties": False
100}
101
102get_hosts_response_schema = {
103 "title":"openvim hosts response information schema",
104 "$schema": "http://json-schema.org/draft-04/schema#",
105 "type":"object",
106 "properties":{
107 "hosts":{
108 "type":"array",
109 "items": host_schema,
110 }
111 },
112 "required": ["hosts"],
113 "additionalProperties": False
114}
115
116get_host_detail_response_schema = new_host_response_schema # TODO: Content is not parsed yet
117
118get_server_response_schema = {
119 "title":"openvim server response information schema",
120 "$schema": "http://json-schema.org/draft-04/schema#",
121 "type":"object",
122 "properties":{
123 "servers":{
124 "type":"array",
125 "items": server_schema,
126 }
127 },
128 "required": ["servers"],
129 "additionalProperties": False
130}
131
132new_tenant_response_schema = {
133 "title":"tenant response information schema",
134 "$schema": "http://json-schema.org/draft-04/schema#",
135 "type":"object",
136 "properties":{
137 "tenant":{
138 "type":"object",
139 "properties":{
140 "id": id_schema,
141 "name": nameshort_schema,
142 "description":description_schema,
143 "enabled":{"type" : "boolean"}
144 },
145 "required": ["id"]
146 }
147 },
148 "required": ["tenant"],
149 "additionalProperties": False
150}
151
152new_network_response_schema = {
153 "title":"network response information schema",
154 "$schema": "http://json-schema.org/draft-04/schema#",
155 "type":"object",
156 "properties":{
157 "network":{
158 "type":"object",
159 "properties":{
160 "id":id_schema,
161 "name":name_schema,
162 "type":{"type":"string", "enum":["bridge_man","bridge_data","data", "ptp"]},
163 "shared":{"type":"boolean"},
164 "tenant_id":id_schema,
165 "admin_state_up":{"type":"boolean"},
166 "vlan":vlan1000_schema
167 },
168 "required": ["id"]
169 }
170 },
171 "required": ["network"],
172 "additionalProperties": False
173}
174
175
176# get_network_response_schema = {
177# "title":"get network response information schema",
178# "$schema": "http://json-schema.org/draft-04/schema#",
179# "type":"object",
180# "properties":{
181# "network":{
182# "type":"object",
183# "properties":{
184# "id":id_schema,
185# "name":name_schema,
186# "type":{"type":"string", "enum":["bridge_man","bridge_data","data", "ptp"]},
187# "shared":{"type":"boolean"},
188# "tenant_id":id_schema,
189# "admin_state_up":{"type":"boolean"},
190# "vlan":vlan1000_schema
191# },
192# "required": ["id"]
193# }
194# },
195# "required": ["network"],
196# "additionalProperties": False
197# }
198
199
200new_port_response_schema = {
201 "title":"port response information schema",
202 "$schema": "http://json-schema.org/draft-04/schema#",
203 "type":"object",
204 "properties":{
205 "port":{
206 "type":"object",
207 "properties":{
208 "id":id_schema,
209 },
210 "required": ["id"]
211 }
212 },
213 "required": ["port"],
214 "additionalProperties": False
215}
216
217get_flavor_response_schema = {
218 "title":"openvim flavors response information schema",
219 "$schema": "http://json-schema.org/draft-04/schema#",
220 "type":"object",
221 "properties":{
222 "flavor":{
223 "type":"object",
224 "properties":{
225 "id": id_schema,
226 "name": name_schema,
227 "extended": {"type":"object"},
228 },
229 "required": ["id", "name"],
230 }
231 },
232 "required": ["flavor"],
233 "additionalProperties": False
234}
235
236new_flavor_response_schema = {
237 "title":"flavor response information schema",
238 "$schema": "http://json-schema.org/draft-04/schema#",
239 "type":"object",
240 "properties":{
241 "flavor":{
242 "type":"object",
243 "properties":{
244 "id":id_schema,
245 },
246 "required": ["id"]
247 }
248 },
249 "required": ["flavor"],
250 "additionalProperties": False
251}
252
tiernoae4a8d12016-07-08 12:30:39 +0200253get_image_response_schema = {
254 "title":"openvim images response information schema",
255 "$schema": "http://json-schema.org/draft-04/schema#",
256 "type":"object",
257 "properties":{
258 "image":{
259 "type":"object",
260 "properties":{
261 "id": id_schema,
262 "name": name_schema,
263 },
264 "required": ["id", "name"],
265 }
266 },
267 "required": ["flavor"],
268 "additionalProperties": False
269}
tierno7edb6752016-03-21 17:37:52 +0100270new_image_response_schema = {
271 "title":"image response information schema",
272 "$schema": "http://json-schema.org/draft-04/schema#",
273 "type":"object",
274 "properties":{
275 "image":{
276 "type":"object",
277 "properties":{
278 "id":id_schema,
279 },
280 "required": ["id"]
281 }
282 },
283 "required": ["image"],
284 "additionalProperties": False
285}
286
287new_vminstance_response_schema = {
288 "title":"server response information schema",
289 "$schema": "http://json-schema.org/draft-04/schema#",
290 "type":"object",
291 "properties":{
292 "server":{
293 "type":"object",
294 "properties":{
295 "id":id_schema,
296 },
297 "required": ["id"]
298 }
299 },
300 "required": ["server"],
301 "additionalProperties": False
302}
303
304get_processor_rankings_response_schema = {
305 "title":"processor rankings information schema",
306 "$schema": "http://json-schema.org/draft-04/schema#",
307 "type":"object",
308 "properties":{
309 "rankings":{
310 "type":"array",
311 "items":{
312 "type":"object",
313 "properties":{
314 "model": description_schema,
315 "value": integer0_schema
316 },
317 "additionalProperties": False,
318 "required": ["model","value"]
319 }
320 },
321 "additionalProperties": False,
322 "required": ["rankings"]
323 }
324}
325
tierno031ded62019-11-14 14:06:31 +0000326
tierno72774862020-05-04 11:44:15 +0000327class vimconnector(vimconn.VimConnector):
tiernob3d36742017-03-03 23:51:05 +0100328 def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None,
329 log_level="DEBUG", config={}, persistent_info={}):
tierno72774862020-05-04 11:44:15 +0000330 vimconn.VimConnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level, config)
tierno392f2852016-05-13 12:28:55 +0200331 self.tenant = None
332 self.headers_req = {'content-type': 'application/json'}
sousaedu3f8f2f42021-02-09 15:30:01 +0100333 self.logger = logging.getLogger('ro.vim.openvim')
tiernob3d36742017-03-03 23:51:05 +0100334 self.persistent_info = persistent_info
tierno392f2852016-05-13 12:28:55 +0200335 if tenant_id:
336 self.tenant = tenant_id
337
338 def __setitem__(self,index, value):
339 '''Set individuals parameters
340 Throw TypeError, KeyError
341 '''
342 if index=='tenant_id':
343 self.tenant = value
344 elif index=='tenant_name':
345 self.tenant = None
tierno72774862020-05-04 11:44:15 +0000346 vimconn.VimConnector.__setitem__(self,index, value)
tierno392f2852016-05-13 12:28:55 +0200347
348 def _get_my_tenant(self):
349 '''Obtain uuid of my tenant from name
350 '''
351 if self.tenant:
352 return self.tenant
353
tiernobe41e222016-09-02 15:16:13 +0200354 url = self.url+'/tenants?name='+ quote(self.tenant_name)
tiernoae4a8d12016-07-08 12:30:39 +0200355 self.logger.info("Getting VIM tenant_id GET %s", url)
356 vim_response = requests.get(url, headers = self.headers_req)
357 self._check_http_request_response(vim_response)
358 try:
359 tenant_list = vim_response.json()["tenants"]
360 if len(tenant_list) == 0:
tierno72774862020-05-04 11:44:15 +0000361 raise vimconn.VimConnNotFoundException("No tenant found for name '{}'".format(self.tenant_name))
tiernoae4a8d12016-07-08 12:30:39 +0200362 elif len(tenant_list) > 1:
tierno72774862020-05-04 11:44:15 +0000363 raise vimconn.VimConnConflictException ("More that one tenant found for name '{}'".format(self.tenant_name))
tiernoae4a8d12016-07-08 12:30:39 +0200364 self.tenant = tenant_list[0]["id"]
365 return self.tenant
366 except Exception as e:
tierno72774862020-05-04 11:44:15 +0000367 raise vimconn.VimConnUnexpectedResponse("Get VIM tenant {} '{}'".format(type(e).__name__, str(e)))
tierno392f2852016-05-13 12:28:55 +0200368
tierno7edb6752016-03-21 17:37:52 +0100369 def _format_jsonerror(self,http_response):
tiernoae4a8d12016-07-08 12:30:39 +0200370 #DEPRECATED, to delete in the future
tierno7edb6752016-03-21 17:37:52 +0100371 try:
372 data = http_response.json()
373 return data["error"]["description"]
374 except:
375 return http_response.text
376
377 def _format_in(self, http_response, schema):
tiernoae4a8d12016-07-08 12:30:39 +0200378 #DEPRECATED, to delete in the future
tierno7edb6752016-03-21 17:37:52 +0100379 try:
380 client_data = http_response.json()
381 js_v(client_data, schema)
382 #print "Input data: ", str(client_data)
383 return True, client_data
venkatamahesh6ecca182017-01-27 23:04:40 +0530384 except js_e.ValidationError as exc:
tierno7d782ef2019-10-04 12:56:31 +0000385 print("validate_in error, jsonschema exception ", exc.message, "at", exc.path)
tierno7edb6752016-03-21 17:37:52 +0100386 return False, ("validate_in error, jsonschema exception ", exc.message, "at", exc.path)
387
388 def _remove_extra_items(self, data, schema):
389 deleted=[]
390 if type(data) is tuple or type(data) is list:
391 for d in data:
392 a= self._remove_extra_items(d, schema['items'])
393 if a is not None: deleted.append(a)
394 elif type(data) is dict:
tierno031ded62019-11-14 14:06:31 +0000395 to_delete = []
tierno7edb6752016-03-21 17:37:52 +0100396 for k in data.keys():
397 if 'properties' not in schema or k not in schema['properties'].keys():
tierno031ded62019-11-14 14:06:31 +0000398 to_delete.append(k)
tierno7edb6752016-03-21 17:37:52 +0100399 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})
tierno031ded62019-11-14 14:06:31 +0000403 for k in to_delete:
404 del data[k]
tierno7edb6752016-03-21 17:37:52 +0100405 if len(deleted) == 0: return None
406 elif len(deleted) == 1: return deleted[0]
407 else: return deleted
tierno7edb6752016-03-21 17:37:52 +0100408
tiernoae4a8d12016-07-08 12:30:39 +0200409 def _format_request_exception(self, request_exception):
410 '''Transform a request exception into a vimconn exception'''
411 if isinstance(request_exception, js_e.ValidationError):
tierno72774862020-05-04 11:44:15 +0000412 raise vimconn.VimConnUnexpectedResponse("jsonschema exception '{}' at '{}'".format(request_exception.message, request_exception.path))
tiernoae4a8d12016-07-08 12:30:39 +0200413 elif isinstance(request_exception, requests.exceptions.HTTPError):
tierno72774862020-05-04 11:44:15 +0000414 raise vimconn.VimConnUnexpectedResponse(type(request_exception).__name__ + ": " + str(request_exception))
tierno7edb6752016-03-21 17:37:52 +0100415 else:
tierno72774862020-05-04 11:44:15 +0000416 raise vimconn.VimConnConnectionException(type(request_exception).__name__ + ": " + str(request_exception))
tiernoae4a8d12016-07-08 12:30:39 +0200417
418 def _check_http_request_response(self, request_response):
419 '''Raise a vimconn exception if the response is not Ok'''
420 if request_response.status_code >= 200 and request_response.status_code < 300:
421 return
422 if request_response.status_code == vimconn.HTTP_Unauthorized:
tierno72774862020-05-04 11:44:15 +0000423 raise vimconn.VimConnAuthException(request_response.text)
tiernoae4a8d12016-07-08 12:30:39 +0200424 elif request_response.status_code == vimconn.HTTP_Not_Found:
tierno72774862020-05-04 11:44:15 +0000425 raise vimconn.VimConnNotFoundException(request_response.text)
tiernoae4a8d12016-07-08 12:30:39 +0200426 elif request_response.status_code == vimconn.HTTP_Conflict:
tierno72774862020-05-04 11:44:15 +0000427 raise vimconn.VimConnConflictException(request_response.text)
tiernoae4a8d12016-07-08 12:30:39 +0200428 else:
tierno72774862020-05-04 11:44:15 +0000429 raise vimconn.VimConnUnexpectedResponse("VIM HTTP_response {}, {}".format(request_response.status_code, str(request_response.text)))
tiernoae4a8d12016-07-08 12:30:39 +0200430
tierno7edb6752016-03-21 17:37:52 +0100431 def new_tenant(self,tenant_name,tenant_description):
tiernoae4a8d12016-07-08 12:30:39 +0200432 '''Adds a new tenant to VIM with this name and description, returns the tenant identifier'''
433 #print "VIMConnector: Adding a new tenant to VIM"
tierno7edb6752016-03-21 17:37:52 +0100434 payload_dict = {"tenant": {"name":tenant_name,"description": tenant_description, "enabled": True}}
435 payload_req = json.dumps(payload_dict)
tierno7edb6752016-03-21 17:37:52 +0100436 try:
tiernoae4a8d12016-07-08 12:30:39 +0200437 url = self.url_admin+'/tenants'
438 self.logger.info("Adding a new tenant %s", url)
439 vim_response = requests.post(url, headers = self.headers_req, data=payload_req)
440 self._check_http_request_response(vim_response)
441 self.logger.debug(vim_response.text)
tierno7edb6752016-03-21 17:37:52 +0100442 #print json.dumps(vim_response.json(), indent=4)
tiernoae4a8d12016-07-08 12:30:39 +0200443 response = vim_response.json()
444 js_v(response, new_tenant_response_schema)
445 #r = self._remove_extra_items(response, new_tenant_response_schema)
446 #if r is not None:
447 # self.logger.warn("Warning: remove extra items %s", str(r))
448 tenant_id = response['tenant']['id']
449 return tenant_id
450 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
451 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100452
tiernoae4a8d12016-07-08 12:30:39 +0200453 def delete_tenant(self,tenant_id):
454 '''Delete a tenant from VIM. Returns the old tenant identifier'''
tierno7edb6752016-03-21 17:37:52 +0100455 try:
tiernoae4a8d12016-07-08 12:30:39 +0200456 url = self.url_admin+'/tenants/'+tenant_id
457 self.logger.info("Delete a tenant DELETE %s", url)
458 vim_response = requests.delete(url, headers = self.headers_req)
459 self._check_http_request_response(vim_response)
460 self.logger.debug(vim_response.text)
tierno7edb6752016-03-21 17:37:52 +0100461 #print json.dumps(vim_response.json(), indent=4)
tiernoae4a8d12016-07-08 12:30:39 +0200462 return tenant_id
463 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
464 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100465
466 def get_tenant_list(self, filter_dict={}):
467 '''Obtain tenants of VIM
tiernoae4a8d12016-07-08 12:30:39 +0200468 filter_dict can contain the following keys:
469 name: filter by tenant name
470 id: filter by tenant uuid/id
471 <other VIM specific>
472 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
tierno7edb6752016-03-21 17:37:52 +0100473 '''
tierno7edb6752016-03-21 17:37:52 +0100474 filterquery=[]
475 filterquery_text=''
tierno7d782ef2019-10-04 12:56:31 +0000476 for k,v in filter_dict.items():
tierno7edb6752016-03-21 17:37:52 +0100477 filterquery.append(str(k)+'='+str(v))
478 if len(filterquery)>0:
479 filterquery_text='?'+ '&'.join(filterquery)
tierno7edb6752016-03-21 17:37:52 +0100480 try:
tiernoae4a8d12016-07-08 12:30:39 +0200481 url = self.url+'/tenants'+filterquery_text
482 self.logger.info("get_tenant_list GET %s", url)
483 vim_response = requests.get(url, headers = self.headers_req)
484 self._check_http_request_response(vim_response)
485 self.logger.debug(vim_response.text)
tierno7edb6752016-03-21 17:37:52 +0100486 #print json.dumps(vim_response.json(), indent=4)
tiernoae4a8d12016-07-08 12:30:39 +0200487 return vim_response.json()["tenants"]
488 except requests.exceptions.RequestException as e:
489 self._format_request_exception(e)
490
kbsuba85c54d2019-10-17 16:30:32 +0000491 def new_network(self,net_name, net_type, ip_profile=None, shared=False, provider_network_profile=None): #, **vim_specific):
garciadeblasebd66722019-01-31 16:01:31 +0000492 """Adds a tenant network to VIM
493 Params:
494 'net_name': name of the network
495 'net_type': one of:
496 'bridge': overlay isolated network
497 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
498 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
499 'ip_profile': is a dict containing the IP parameters of the network
500 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
501 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
502 'gateway_address': (Optional) ip_schema, that is X.X.X.X
503 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
504 'dhcp_enabled': True or False
505 'dhcp_start_address': ip_schema, first IP to grant
506 'dhcp_count': number of IPs to grant.
507 'shared': if this network can be seen/use by other tenants/organization
kbsuba85c54d2019-10-17 16:30:32 +0000508 'provider_network_profile': (optional) contains {segmentation-id: vlan, provider-network: vim_netowrk}
garciadeblasebd66722019-01-31 16:01:31 +0000509 Returns a tuple with the network identifier and created_items, or raises an exception on error
510 created_items can be None or a dictionary where this method can include key-values that will be passed to
511 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
512 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
513 as not present.
514 """
tiernoae4a8d12016-07-08 12:30:39 +0200515 try:
kbsuba85c54d2019-10-17 16:30:32 +0000516 vlan = None
517 if provider_network_profile:
518 vlan = provider_network_profile.get("segmentation-id")
garciadeblasebd66722019-01-31 16:01:31 +0000519 created_items = {}
tiernoae4a8d12016-07-08 12:30:39 +0200520 self._get_my_tenant()
521 if net_type=="bridge":
522 net_type="bridge_data"
523 payload_req = {"name": net_name, "type": net_type, "tenant_id": self.tenant, "shared": shared}
tiernoa7d34d02017-02-23 14:42:07 +0100524 if vlan:
525 payload_req["provider:vlan"] = vlan
526 # payload_req.update(vim_specific)
tiernoae4a8d12016-07-08 12:30:39 +0200527 url = self.url+'/networks'
528 self.logger.info("Adding a new network POST: %s DATA: %s", url, str(payload_req))
529 vim_response = requests.post(url, headers = self.headers_req, data=json.dumps({"network": payload_req}) )
530 self._check_http_request_response(vim_response)
531 self.logger.debug(vim_response.text)
532 #print json.dumps(vim_response.json(), indent=4)
533 response = vim_response.json()
534 js_v(response, new_network_response_schema)
535 #r = self._remove_extra_items(response, new_network_response_schema)
536 #if r is not None:
537 # self.logger.warn("Warning: remove extra items %s", str(r))
538 network_id = response['network']['id']
garciadeblasebd66722019-01-31 16:01:31 +0000539 return network_id, created_items
tiernoae4a8d12016-07-08 12:30:39 +0200540 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
541 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100542
543 def get_network_list(self, filter_dict={}):
544 '''Obtain tenant networks of VIM
545 Filter_dict can be:
546 name: network name
547 id: network uuid
tiernoae4a8d12016-07-08 12:30:39 +0200548 public: boolean
tierno7edb6752016-03-21 17:37:52 +0100549 tenant_id: tenant
550 admin_state_up: boolean
551 status: 'ACTIVE'
552 Returns the network list of dictionaries
553 '''
tierno7edb6752016-03-21 17:37:52 +0100554 try:
tiernoae4a8d12016-07-08 12:30:39 +0200555 if 'tenant_id' not in filter_dict:
556 filter_dict["tenant_id"] = self._get_my_tenant()
557 elif not filter_dict["tenant_id"]:
558 del filter_dict["tenant_id"]
559 filterquery=[]
560 filterquery_text=''
tierno7d782ef2019-10-04 12:56:31 +0000561 for k,v in filter_dict.items():
tiernoae4a8d12016-07-08 12:30:39 +0200562 filterquery.append(str(k)+'='+str(v))
563 if len(filterquery)>0:
564 filterquery_text='?'+ '&'.join(filterquery)
565 url = self.url+'/networks'+filterquery_text
566 self.logger.info("Getting network list GET %s", url)
567 vim_response = requests.get(url, headers = self.headers_req)
568 self._check_http_request_response(vim_response)
569 self.logger.debug(vim_response.text)
tierno7edb6752016-03-21 17:37:52 +0100570 #print json.dumps(vim_response.json(), indent=4)
tiernoae4a8d12016-07-08 12:30:39 +0200571 response = vim_response.json()
572 return response['networks']
573 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
574 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100575
tiernoae4a8d12016-07-08 12:30:39 +0200576 def get_network(self, net_id):
577 '''Obtain network details of network id'''
578 try:
579 url = self.url+'/networks/'+net_id
580 self.logger.info("Getting network GET %s", url)
581 vim_response = requests.get(url, headers = self.headers_req)
582 self._check_http_request_response(vim_response)
583 self.logger.debug(vim_response.text)
584 #print json.dumps(vim_response.json(), indent=4)
585 response = vim_response.json()
586 return response['network']
587 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
588 self._format_request_exception(e)
589
garciadeblasebd66722019-01-31 16:01:31 +0000590 def delete_network(self, net_id, created_items=None):
591 """
592 Removes a tenant network from VIM and its associated elements
593 :param net_id: VIM identifier of the network, provided by method new_network
594 :param created_items: dictionary with extra items to be deleted. provided by method new_network
595 Returns the network identifier or raises an exception upon error or when network is not found
596 """
tierno392f2852016-05-13 12:28:55 +0200597 try:
598 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +0200599 url = self.url+'/networks/'+net_id
600 self.logger.info("Deleting VIM network DELETE %s", url)
601 vim_response = requests.delete(url, headers=self.headers_req)
602 self._check_http_request_response(vim_response)
603 #self.logger.debug(vim_response.text)
604 #print json.dumps(vim_response.json(), indent=4)
605 return net_id
606 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
607 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100608
tiernoae4a8d12016-07-08 12:30:39 +0200609 def get_flavor(self, flavor_id):
610 '''Obtain flavor details from the VIM'''
tierno392f2852016-05-13 12:28:55 +0200611 try:
612 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +0200613 url = self.url+'/'+self.tenant+'/flavors/'+flavor_id
614 self.logger.info("Getting flavor GET %s", url)
615 vim_response = requests.get(url, headers = self.headers_req)
616 self._check_http_request_response(vim_response)
617 self.logger.debug(vim_response.text)
tierno7edb6752016-03-21 17:37:52 +0100618 #print json.dumps(vim_response.json(), indent=4)
tiernoae4a8d12016-07-08 12:30:39 +0200619 response = vim_response.json()
620 js_v(response, get_flavor_response_schema)
621 r = self._remove_extra_items(response, get_flavor_response_schema)
622 if r is not None:
623 self.logger.warn("Warning: remove extra items %s", str(r))
624 return response['flavor']
625 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
626 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100627
tiernoae4a8d12016-07-08 12:30:39 +0200628 def new_flavor(self, flavor_data):
tierno7edb6752016-03-21 17:37:52 +0100629 '''Adds a tenant flavor to VIM'''
630 '''Returns the flavor identifier'''
tierno392f2852016-05-13 12:28:55 +0200631 try:
tierno7432e7c2017-01-04 16:54:37 +0100632 new_flavor_dict = flavor_data.copy()
tierno714bc902017-06-06 18:25:15 +0200633 for device in new_flavor_dict.get('extended', {}).get('devices', ()):
634 if 'image name' in device:
635 del device['image name']
tierno9f2900c2018-07-13 15:25:24 +0200636 if 'name' in device:
637 del device['name']
tierno66eba6e2017-11-10 17:09:18 +0100638 numas = new_flavor_dict.get('extended', {}).get('numas')
639 if numas:
640 numa = numas[0]
641 # translate memory, cpus to EPA
642 if "cores" not in numa and "threads" not in numa and "paired-threads" not in numa:
643 numa["paired-threads"] = new_flavor_dict["vcpus"]
644 if "memory" not in numa:
tierno7d782ef2019-10-04 12:56:31 +0000645 numa["memory"] = int(math.ceil(new_flavor_dict["ram"] / 1024.0))
tierno66eba6e2017-11-10 17:09:18 +0100646 for iface in numa.get("interfaces", ()):
647 if not iface.get("bandwidth"):
648 iface["bandwidth"] = "1 Mbps"
649
tierno7432e7c2017-01-04 16:54:37 +0100650 new_flavor_dict["name"] = flavor_data["name"][:64]
tierno392f2852016-05-13 12:28:55 +0200651 self._get_my_tenant()
tierno7432e7c2017-01-04 16:54:37 +0100652 payload_req = json.dumps({'flavor': new_flavor_dict})
tiernoae4a8d12016-07-08 12:30:39 +0200653 url = self.url+'/'+self.tenant+'/flavors'
654 self.logger.info("Adding a new VIM flavor POST %s", url)
tierno392f2852016-05-13 12:28:55 +0200655 vim_response = requests.post(url, headers = self.headers_req, data=payload_req)
tiernoae4a8d12016-07-08 12:30:39 +0200656 self._check_http_request_response(vim_response)
657 self.logger.debug(vim_response.text)
tierno7edb6752016-03-21 17:37:52 +0100658 #print json.dumps(vim_response.json(), indent=4)
tiernoae4a8d12016-07-08 12:30:39 +0200659 response = vim_response.json()
660 js_v(response, new_flavor_response_schema)
661 r = self._remove_extra_items(response, new_flavor_response_schema)
662 if r is not None:
663 self.logger.warn("Warning: remove extra items %s", str(r))
664 flavor_id = response['flavor']['id']
665 return flavor_id
666 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
667 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100668
tiernoae4a8d12016-07-08 12:30:39 +0200669 def delete_flavor(self,flavor_id):
670 '''Deletes a tenant flavor from VIM'''
671 '''Returns the old flavor_id'''
tierno392f2852016-05-13 12:28:55 +0200672 try:
673 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +0200674 url = self.url+'/'+self.tenant+'/flavors/'+flavor_id
675 self.logger.info("Deleting VIM flavor DELETE %s", url)
676 vim_response = requests.delete(url, headers=self.headers_req)
677 self._check_http_request_response(vim_response)
678 #self.logger.debug(vim_response.text)
679 #print json.dumps(vim_response.json(), indent=4)
680 return flavor_id
681 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
682 self._format_request_exception(e)
683
684 def get_image(self, image_id):
685 '''Obtain image details from the VIM'''
tierno7edb6752016-03-21 17:37:52 +0100686 try:
tiernoae4a8d12016-07-08 12:30:39 +0200687 self._get_my_tenant()
688 url = self.url+'/'+self.tenant+'/images/'+image_id
689 self.logger.info("Getting image GET %s", url)
690 vim_response = requests.get(url, headers = self.headers_req)
691 self._check_http_request_response(vim_response)
692 self.logger.debug(vim_response.text)
693 #print json.dumps(vim_response.json(), indent=4)
694 response = vim_response.json()
695 js_v(response, get_image_response_schema)
696 r = self._remove_extra_items(response, get_image_response_schema)
697 if r is not None:
698 self.logger.warn("Warning: remove extra items %s", str(r))
699 return response['image']
700 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
701 self._format_request_exception(e)
702
703 def new_image(self,image_dict):
704 ''' Adds a tenant image to VIM, returns image_id'''
705 try:
706 self._get_my_tenant()
tierno7432e7c2017-01-04 16:54:37 +0100707 new_image_dict={'name': image_dict['name'][:64]}
tiernoae4a8d12016-07-08 12:30:39 +0200708 if image_dict.get('description'):
709 new_image_dict['description'] = image_dict['description']
710 if image_dict.get('metadata'):
tierno7d782ef2019-10-04 12:56:31 +0000711 new_image_dict['metadata'] = yaml.load(image_dict['metadata'], Loader=yaml.SafeLoader)
tiernoae4a8d12016-07-08 12:30:39 +0200712 if image_dict.get('location'):
713 new_image_dict['path'] = image_dict['location']
714 payload_req = json.dumps({"image":new_image_dict})
715 url=self.url + '/' + self.tenant + '/images'
716 self.logger.info("Adding a new VIM image POST %s", url)
717 vim_response = requests.post(url, headers = self.headers_req, data=payload_req)
718 self._check_http_request_response(vim_response)
719 self.logger.debug(vim_response.text)
720 #print json.dumps(vim_response.json(), indent=4)
721 response = vim_response.json()
722 js_v(response, new_image_response_schema)
723 r = self._remove_extra_items(response, new_image_response_schema)
724 if r is not None:
725 self.logger.warn("Warning: remove extra items %s", str(r))
726 image_id = response['image']['id']
727 return image_id
728 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
729 self._format_request_exception(e)
730
731 def delete_image(self, image_id):
732 '''Deletes a tenant image from VIM'''
733 '''Returns the deleted image_id'''
734 try:
735 self._get_my_tenant()
736 url = self.url + '/'+ self.tenant +'/images/'+image_id
737 self.logger.info("Deleting VIM image DELETE %s", url)
738 vim_response = requests.delete(url, headers=self.headers_req)
739 self._check_http_request_response(vim_response)
740 #self.logger.debug(vim_response.text)
741 #print json.dumps(vim_response.json(), indent=4)
742 return image_id
743 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
744 self._format_request_exception(e)
745
tiernoae4a8d12016-07-08 12:30:39 +0200746 def get_image_id_from_path(self, path):
garciadeblasb69fa9f2016-09-28 12:04:10 +0200747 '''Get the image id from image path in the VIM database. Returns the image_id'''
tiernoae4a8d12016-07-08 12:30:39 +0200748 try:
749 self._get_my_tenant()
tiernobe41e222016-09-02 15:16:13 +0200750 url=self.url + '/' + self.tenant + '/images?path='+quote(path)
tiernoae4a8d12016-07-08 12:30:39 +0200751 self.logger.info("Getting images GET %s", url)
752 vim_response = requests.get(url)
753 self._check_http_request_response(vim_response)
754 self.logger.debug(vim_response.text)
755 #print json.dumps(vim_response.json(), indent=4)
756 response = vim_response.json()
757 js_v(response, get_images_response_schema)
758 #r = self._remove_extra_items(response, get_images_response_schema)
759 #if r is not None:
760 # self.logger.warn("Warning: remove extra items %s", str(r))
761 if len(response['images'])==0:
tierno72774862020-05-04 11:44:15 +0000762 raise vimconn.VimConnNotFoundException("Image not found at VIM with path '{}'".format(path))
tiernoae4a8d12016-07-08 12:30:39 +0200763 elif len(response['images'])>1:
tierno72774862020-05-04 11:44:15 +0000764 raise vimconn.VimConnConflictException("More than one image found at VIM with path '{}'".format(path))
tiernoae4a8d12016-07-08 12:30:39 +0200765 return response['images'][0]['id']
766 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
767 self._format_request_exception(e)
768
garciadeblasb69fa9f2016-09-28 12:04:10 +0200769 def get_image_list(self, filter_dict={}):
770 '''Obtain tenant images from VIM
771 Filter_dict can be:
772 name: image name
773 id: image uuid
774 checksum: image checksum
775 location: image path
776 Returns the image list of dictionaries:
777 [{<the fields at Filter_dict plus some VIM specific>}, ...]
778 List can be empty
779 '''
780 try:
781 self._get_my_tenant()
782 filterquery=[]
783 filterquery_text=''
tierno7d782ef2019-10-04 12:56:31 +0000784 for k,v in filter_dict.items():
garciadeblasb69fa9f2016-09-28 12:04:10 +0200785 filterquery.append(str(k)+'='+str(v))
786 if len(filterquery)>0:
787 filterquery_text='?'+ '&'.join(filterquery)
788 url = self.url+'/'+self.tenant+'/images'+filterquery_text
789 self.logger.info("Getting image list GET %s", url)
790 vim_response = requests.get(url, headers = self.headers_req)
791 self._check_http_request_response(vim_response)
792 self.logger.debug(vim_response.text)
793 #print json.dumps(vim_response.json(), indent=4)
794 response = vim_response.json()
795 return response['images']
796 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
797 self._format_request_exception(e)
798
tiernoae4a8d12016-07-08 12:30:39 +0200799 def new_vminstancefromJSON(self, vm_data):
tierno7edb6752016-03-21 17:37:52 +0100800 '''Adds a VM instance to VIM'''
801 '''Returns the instance identifier'''
tierno392f2852016-05-13 12:28:55 +0200802 try:
803 self._get_my_tenant()
804 except Exception as e:
805 return -vimconn.HTTP_Not_Found, str(e)
tierno7d782ef2019-10-04 12:56:31 +0000806 print("VIMConnector: Adding a new VM instance from JSON to VIM")
tierno7edb6752016-03-21 17:37:52 +0100807 payload_req = vm_data
808 try:
tierno392f2852016-05-13 12:28:55 +0200809 vim_response = requests.post(self.url+'/'+self.tenant+'/servers', headers = self.headers_req, data=payload_req)
venkatamahesh6ecca182017-01-27 23:04:40 +0530810 except requests.exceptions.RequestException as e:
tierno7d782ef2019-10-04 12:56:31 +0000811 print( "new_vminstancefromJSON Exception: ", e.args)
tierno7edb6752016-03-21 17:37:52 +0100812 return -vimconn.HTTP_Not_Found, str(e.args[0])
tierno7d782ef2019-10-04 12:56:31 +0000813 # print vim_response
tierno7edb6752016-03-21 17:37:52 +0100814 #print vim_response.status_code
815 if vim_response.status_code == 200:
816 #print vim_response.json()
817 #print json.dumps(vim_response.json(), indent=4)
818 res,http_content = self._format_in(vim_response, new_image_response_schema)
819 #print http_content
820 if res:
821 r = self._remove_extra_items(http_content, new_image_response_schema)
tierno7d782ef2019-10-04 12:56:31 +0000822 if r is not None: print("Warning: remove extra items ", r)
tierno7edb6752016-03-21 17:37:52 +0100823 #print http_content
824 vminstance_id = http_content['server']['id']
tierno7d782ef2019-10-04 12:56:31 +0000825 print( "Tenant image id: ",vminstance_id)
tierno7edb6752016-03-21 17:37:52 +0100826 return vim_response.status_code,vminstance_id
827 else: return -vimconn.HTTP_Bad_Request,http_content
828 else:
829 #print vim_response.text
830 jsonerror = self._format_jsonerror(vim_response)
tierno7d782ef2019-10-04 12:56:31 +0000831 text = 'Error in VIM "{}": not possible to add new vm instance. HTTP Response: {}. Error: {}'.format(
832 self.url, vim_response.status_code, jsonerror)
tierno7edb6752016-03-21 17:37:52 +0100833 #print text
834 return -vim_response.status_code,text
835
tierno5a3273c2017-08-29 11:43:46 +0200836 def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None,
837 availability_zone_index=None, availability_zone_list=None):
tierno66eba6e2017-11-10 17:09:18 +0100838 """Adds a VM instance to VIM
tierno7edb6752016-03-21 17:37:52 +0100839 Params:
840 start: indicates if VM must start or boot in pause mode. Ignored
841 image_id,flavor_id: image and flavor uuid
842 net_list: list of interfaces, each one is a dictionary with:
843 name:
844 net_id: network uuid to connect
845 vpci: virtual vcpi to assign
garciadeblasc4f4d732018-10-25 18:17:24 +0200846 model: interface model, virtio, e1000, ...
tierno7edb6752016-03-21 17:37:52 +0100847 mac_address:
848 use: 'data', 'bridge', 'mgmt'
tierno66eba6e2017-11-10 17:09:18 +0100849 type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared'
tierno7edb6752016-03-21 17:37:52 +0100850 vim_id: filled/added by this function
851 #TODO ip, security groups
tierno98e909c2017-10-14 13:27:03 +0200852 Returns a tuple with the instance identifier and created_items or raises an exception on error
853 created_items can be None or a dictionary where this method can include key-values that will be passed to
854 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
855 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
856 as not present.
tierno66eba6e2017-11-10 17:09:18 +0100857 """
tiernofa51c202017-01-27 14:58:17 +0100858 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 +0200859 try:
860 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +0200861# net_list = []
862# for k,v in net_dict.items():
863# print k,v
864# net_list.append('{"name":"' + k + '", "uuid":"' + v + '"}')
865# net_list_string = ', '.join(net_list)
866 virtio_net_list=[]
867 for net in net_list:
tierno7edb6752016-03-21 17:37:52 +0100868 if not net.get("net_id"):
869 continue
tierno66eba6e2017-11-10 17:09:18 +0100870 net_dict = {'uuid': net["net_id"]}
871 if net.get("type"):
872 if net["type"] == "SR-IOV":
873 net_dict["type"] = "VF"
874 elif net["type"] == "PCI-PASSTHROUGH":
875 net_dict["type"] = "PF"
876 else:
877 net_dict["type"] = net["type"]
878 if net.get("name"):
879 net_dict["name"] = net["name"]
880 if net.get("vpci"):
881 net_dict["vpci"] = net["vpci"]
882 if net.get("model"):
garciadeblas31e141b2018-10-25 18:33:19 +0200883 if net["model"] == "VIRTIO" or net["model"] == "paravirt":
tierno66eba6e2017-11-10 17:09:18 +0100884 net_dict["model"] = "virtio"
885 else:
886 net_dict["model"] = net["model"]
887 if net.get("mac_address"):
888 net_dict["mac_address"] = net["mac_address"]
tierno41a69812018-02-16 14:34:33 +0100889 if net.get("ip_address"):
890 net_dict["ip_address"] = net["ip_address"]
tiernoae4a8d12016-07-08 12:30:39 +0200891 virtio_net_list.append(net_dict)
tierno7432e7c2017-01-04 16:54:37 +0100892 payload_dict={ "name": name[:64],
tiernoae4a8d12016-07-08 12:30:39 +0200893 "description": description,
894 "imageRef": image_id,
895 "flavorRef": flavor_id,
896 "networks": virtio_net_list
897 }
898 if start != None:
899 payload_dict["start"] = start
900 payload_req = json.dumps({"server": payload_dict})
901 url = self.url+'/'+self.tenant+'/servers'
902 self.logger.info("Adding a new vm POST %s DATA %s", url, payload_req)
903 vim_response = requests.post(url, headers = self.headers_req, data=payload_req)
904 self._check_http_request_response(vim_response)
905 self.logger.debug(vim_response.text)
906 #print json.dumps(vim_response.json(), indent=4)
907 response = vim_response.json()
908 js_v(response, new_vminstance_response_schema)
909 #r = self._remove_extra_items(response, new_vminstance_response_schema)
910 #if r is not None:
911 # self.logger.warn("Warning: remove extra items %s", str(r))
912 vminstance_id = response['server']['id']
913
914 #connect data plane interfaces to network
915 for net in net_list:
916 if net["type"]=="virtual":
917 if not net.get("net_id"):
918 continue
919 for iface in response['server']['networks']:
920 if "name" in net:
921 if net["name"]==iface["name"]:
922 net["vim_id"] = iface['iface_id']
923 break
924 elif "net_id" in net:
925 if net["net_id"]==iface["net_id"]:
926 net["vim_id"] = iface['iface_id']
927 break
928 else: #dataplane
929 for numa in response['server'].get('extended',{}).get('numas',() ):
930 for iface in numa.get('interfaces',() ):
931 if net['name'] == iface['name']:
932 net['vim_id'] = iface['iface_id']
933 #Code bellow is not needed, current openvim connect dataplane interfaces
934 #if net.get("net_id"):
935 ##connect dataplane interface
936 # result, port_id = self.connect_port_network(iface['iface_id'], net["net_id"])
937 # if result < 0:
938 # error_text = "Error attaching port %s to network %s: %s." % (iface['iface_id'], net["net_id"], port_id)
939 # print "new_vminstance: " + error_text
940 # self.delete_vminstance(vminstance_id)
941 # return result, error_text
942 break
tierno7edb6752016-03-21 17:37:52 +0100943
tierno98e909c2017-10-14 13:27:03 +0200944 return vminstance_id, None
tiernoae4a8d12016-07-08 12:30:39 +0200945 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
946 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100947
tiernoae4a8d12016-07-08 12:30:39 +0200948 def get_vminstance(self, vm_id):
tierno7edb6752016-03-21 17:37:52 +0100949 '''Returns the VM instance information from VIM'''
tierno392f2852016-05-13 12:28:55 +0200950 try:
951 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +0200952 url = self.url+'/'+self.tenant+'/servers/'+vm_id
953 self.logger.info("Getting vm GET %s", url)
tierno392f2852016-05-13 12:28:55 +0200954 vim_response = requests.get(url, headers = self.headers_req)
tiernoae4a8d12016-07-08 12:30:39 +0200955 vim_response = requests.get(url, headers = self.headers_req)
956 self._check_http_request_response(vim_response)
957 self.logger.debug(vim_response.text)
958 #print json.dumps(vim_response.json(), indent=4)
959 response = vim_response.json()
960 js_v(response, new_vminstance_response_schema)
961 #r = self._remove_extra_items(response, new_vminstance_response_schema)
962 #if r is not None:
963 # self.logger.warn("Warning: remove extra items %s", str(r))
964 return response['server']
965 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
966 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100967
tierno98e909c2017-10-14 13:27:03 +0200968 def delete_vminstance(self, vm_id, created_items=None):
tiernoae4a8d12016-07-08 12:30:39 +0200969 '''Removes a VM instance from VIM, returns the deleted vm_id'''
tierno392f2852016-05-13 12:28:55 +0200970 try:
971 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +0200972 url = self.url+'/'+self.tenant+'/servers/'+vm_id
973 self.logger.info("Deleting VIM vm DELETE %s", url)
974 vim_response = requests.delete(url, headers=self.headers_req)
975 self._check_http_request_response(vim_response)
976 #self.logger.debug(vim_response.text)
977 #print json.dumps(vim_response.json(), indent=4)
978 return vm_id
979 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
980 self._format_request_exception(e)
981
982 def refresh_vms_status(self, vm_list):
983 '''Refreshes the status of the virtual machines'''
tierno7edb6752016-03-21 17:37:52 +0100984 try:
tiernoae4a8d12016-07-08 12:30:39 +0200985 self._get_my_tenant()
986 except requests.exceptions.RequestException as e:
987 self._format_request_exception(e)
988 vm_dict={}
989 for vm_id in vm_list:
990 vm={}
991 #print "VIMConnector refresh_tenant_vms and nets: Getting tenant VM instance information from VIM"
992 try:
tierno41a69812018-02-16 14:34:33 +0100993 url = self.url + '/' + self.tenant + '/servers/' + vm_id
tiernoae4a8d12016-07-08 12:30:39 +0200994 self.logger.info("Getting vm GET %s", url)
995 vim_response = requests.get(url, headers = self.headers_req)
996 self._check_http_request_response(vim_response)
997 response = vim_response.json()
998 js_v(response, new_vminstance_response_schema)
999 if response['server']['status'] in vmStatus2manoFormat:
1000 vm['status'] = vmStatus2manoFormat[ response['server']['status'] ]
1001 else:
1002 vm['status'] = "OTHER"
1003 vm['error_msg'] = "VIM status reported " + response['server']['status']
1004 if response['server'].get('last_error'):
1005 vm['error_msg'] = response['server']['last_error']
1006 vm["vim_info"] = yaml.safe_dump(response['server'])
1007 #get interfaces info
1008 try:
1009 management_ip = False
tierno41a69812018-02-16 14:34:33 +01001010 url2 = self.url + '/ports?device_id=' + quote(vm_id)
tiernoae4a8d12016-07-08 12:30:39 +02001011 self.logger.info("Getting PORTS GET %s", url2)
1012 vim_response2 = requests.get(url2, headers = self.headers_req)
1013 self._check_http_request_response(vim_response2)
1014 client_data = vim_response2.json()
1015 if isinstance(client_data.get("ports"), list):
1016 vm["interfaces"]=[]
1017 for port in client_data.get("ports"):
1018 interface={}
tierno41a69812018-02-16 14:34:33 +01001019 interface['vim_info'] = yaml.safe_dump(port)
tiernoae4a8d12016-07-08 12:30:39 +02001020 interface["mac_address"] = port.get("mac_address")
tierno8e995ce2016-09-22 08:13:00 +00001021 interface["vim_net_id"] = port.get("network_id")
tiernoae4a8d12016-07-08 12:30:39 +02001022 interface["vim_interface_id"] = port["id"]
1023 interface["ip_address"] = port.get("ip_address")
1024 if interface["ip_address"]:
1025 management_ip = True
1026 if interface["ip_address"] == "0.0.0.0":
1027 interface["ip_address"] = None
1028 vm["interfaces"].append(interface)
1029
1030 except Exception as e:
1031 self.logger.error("refresh_vms_and_nets. Port get %s: %s", type(e).__name__, str(e))
tierno7edb6752016-03-21 17:37:52 +01001032
tiernoae4a8d12016-07-08 12:30:39 +02001033 if vm['status'] == "ACTIVE" and not management_ip:
1034 vm['status'] = "ACTIVE:NoMgmtIP"
1035
tierno72774862020-05-04 11:44:15 +00001036 except vimconn.VimConnNotFoundException as e:
tiernoae4a8d12016-07-08 12:30:39 +02001037 self.logger.error("Exception getting vm status: %s", str(e))
1038 vm['status'] = "DELETED"
1039 vm['error_msg'] = str(e)
tierno72774862020-05-04 11:44:15 +00001040 except (requests.exceptions.RequestException, js_e.ValidationError, vimconn.VimConnException) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001041 self.logger.error("Exception getting vm status: %s", str(e))
1042 vm['status'] = "VIM_ERROR"
1043 vm['error_msg'] = str(e)
1044 vm_dict[vm_id] = vm
1045 return vm_dict
tierno7edb6752016-03-21 17:37:52 +01001046
tiernoae4a8d12016-07-08 12:30:39 +02001047 def refresh_nets_status(self, net_list):
1048 '''Get the status of the networks
1049 Params: the list of network identifiers
1050 Returns a dictionary with:
1051 net_id: #VIM id of this network
1052 status: #Mandatory. Text with one of:
1053 # DELETED (not found at vim)
1054 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1055 # OTHER (Vim reported other status not understood)
1056 # ERROR (VIM indicates an ERROR status)
1057 # ACTIVE, INACTIVE, DOWN (admin down),
1058 # BUILD (on building process)
1059 #
1060 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1061 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1062
tierno7edb6752016-03-21 17:37:52 +01001063 '''
tierno392f2852016-05-13 12:28:55 +02001064 try:
1065 self._get_my_tenant()
tiernoae4a8d12016-07-08 12:30:39 +02001066 except requests.exceptions.RequestException as e:
1067 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001068
tiernoae4a8d12016-07-08 12:30:39 +02001069 net_dict={}
1070 for net_id in net_list:
1071 net = {}
1072 #print "VIMConnector refresh_tenant_vms_and_nets: Getting tenant network from VIM (tenant: " + str(self.tenant) + "): "
tierno7edb6752016-03-21 17:37:52 +01001073 try:
tiernoae4a8d12016-07-08 12:30:39 +02001074 net_vim = self.get_network(net_id)
1075 if net_vim['status'] in netStatus2manoFormat:
1076 net["status"] = netStatus2manoFormat[ net_vim['status'] ]
tierno7edb6752016-03-21 17:37:52 +01001077 else:
tiernoae4a8d12016-07-08 12:30:39 +02001078 net["status"] = "OTHER"
1079 net["error_msg"] = "VIM status reported " + net_vim['status']
1080
1081 if net["status"] == "ACTIVE" and not net_vim['admin_state_up']:
1082 net["status"] = "DOWN"
1083 if net_vim.get('last_error'):
1084 net['error_msg'] = net_vim['last_error']
1085 net["vim_info"] = yaml.safe_dump(net_vim)
tierno72774862020-05-04 11:44:15 +00001086 except vimconn.VimConnNotFoundException as e:
tiernoae4a8d12016-07-08 12:30:39 +02001087 self.logger.error("Exception getting net status: %s", str(e))
1088 net['status'] = "DELETED"
1089 net['error_msg'] = str(e)
tierno72774862020-05-04 11:44:15 +00001090 except (requests.exceptions.RequestException, js_e.ValidationError, vimconn.VimConnException) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001091 self.logger.error("Exception getting net status: %s", str(e))
1092 net['status'] = "VIM_ERROR"
1093 net['error_msg'] = str(e)
1094 net_dict[net_id] = net
1095 return net_dict
tierno7edb6752016-03-21 17:37:52 +01001096
tierno98e909c2017-10-14 13:27:03 +02001097 def action_vminstance(self, vm_id, action_dict, created_items={}):
tierno7edb6752016-03-21 17:37:52 +01001098 '''Send and action over a VM instance from VIM'''
1099 '''Returns the status'''
tierno392f2852016-05-13 12:28:55 +02001100 try:
1101 self._get_my_tenant()
tierno7edb6752016-03-21 17:37:52 +01001102 if "console" in action_dict:
tierno72774862020-05-04 11:44:15 +00001103 raise vimconn.VimConnException("getting console is not available at openvim", http_code=vimconn.HTTP_Service_Unavailable)
tiernoae4a8d12016-07-08 12:30:39 +02001104 url = self.url+'/'+self.tenant+'/servers/'+vm_id+"/action"
1105 self.logger.info("Action over VM instance POST %s", url)
1106 vim_response = requests.post(url, headers = self.headers_req, data=json.dumps(action_dict) )
1107 self._check_http_request_response(vim_response)
tierno98e909c2017-10-14 13:27:03 +02001108 return None
tiernoae4a8d12016-07-08 12:30:39 +02001109 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
1110 self._format_request_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001111
tiernoae4a8d12016-07-08 12:30:39 +02001112#NOT USED METHODS in current version
1113
tierno7edb6752016-03-21 17:37:52 +01001114 def host_vim2gui(self, host, server_dict):
1115 '''Transform host dictionary from VIM format to GUI format,
1116 and append to the server_dict
1117 '''
1118 if type(server_dict) is not dict:
tierno7d782ef2019-10-04 12:56:31 +00001119 print( 'vimconnector.host_vim2gui() ERROR, param server_dict must be a dictionary')
tierno7edb6752016-03-21 17:37:52 +01001120 return
1121 RAD={}
1122 occupation={}
1123 for numa in host['host']['numas']:
1124 RAD_item={}
1125 occupation_item={}
1126 #memory
1127 RAD_item['memory']={'size': str(numa['memory'])+'GB', 'eligible': str(numa['hugepages'])+'GB'}
1128 occupation_item['memory']= str(numa['hugepages_consumed'])+'GB'
1129 #cpus
1130 RAD_item['cpus']={}
1131 RAD_item['cpus']['cores'] = []
1132 RAD_item['cpus']['eligible_cores'] = []
1133 occupation_item['cores']=[]
tierno7d782ef2019-10-04 12:56:31 +00001134 for _ in range(0, len(numa['cores']) // 2):
tierno7edb6752016-03-21 17:37:52 +01001135 RAD_item['cpus']['cores'].append( [] )
1136 for core in numa['cores']:
1137 RAD_item['cpus']['cores'][core['core_id']].append(core['thread_id'])
1138 if not 'status' in core: RAD_item['cpus']['eligible_cores'].append(core['thread_id'])
1139 if 'instance_id' in core: occupation_item['cores'].append(core['thread_id'])
1140 #ports
1141 RAD_item['ports']={}
1142 occupation_item['ports']={}
1143 for iface in numa['interfaces']:
1144 RAD_item['ports'][ iface['pci'] ] = 'speed:'+str(iface['Mbps'])+'M'
tierno7d782ef2019-10-04 12:56:31 +00001145 occupation_item['ports'][ iface['pci'] ] = { 'occupied': str(100*iface['Mbps_consumed'] // iface['Mbps']) + "%" }
tierno7edb6752016-03-21 17:37:52 +01001146
1147 RAD[ numa['numa_socket'] ] = RAD_item
1148 occupation[ numa['numa_socket'] ] = occupation_item
1149 server_dict[ host['host']['name'] ] = {'RAD':RAD, 'occupation':occupation}
1150
1151 def get_hosts_info(self):
1152 '''Get the information of deployed hosts
1153 Returns the hosts content'''
1154 #obtain hosts list
1155 url=self.url+'/hosts'
1156 try:
1157 vim_response = requests.get(url)
venkatamahesh6ecca182017-01-27 23:04:40 +05301158 except requests.exceptions.RequestException as e:
tierno7d782ef2019-10-04 12:56:31 +00001159 print( "get_hosts_info Exception: ", e.args)
tierno7edb6752016-03-21 17:37:52 +01001160 return -vimconn.HTTP_Not_Found, str(e.args[0])
tierno7d782ef2019-10-04 12:56:31 +00001161 print("vim get", url, "response:", vim_response.status_code, vim_response.json())
tierno7edb6752016-03-21 17:37:52 +01001162 #print vim_response.status_code
1163 #print json.dumps(vim_response.json(), indent=4)
1164 if vim_response.status_code != 200:
tierno7d782ef2019-10-04 12:56:31 +00001165 # TODO: get error
1166 print('vimconnector.get_hosts_info error getting host list {} {}'.format(vim_response.status_code, vim_response.json()))
tierno7edb6752016-03-21 17:37:52 +01001167 return -vim_response.status_code, "Error getting host list"
1168
1169 res,hosts = self._format_in(vim_response, get_hosts_response_schema)
1170
1171 if res==False:
tierno7d782ef2019-10-04 12:56:31 +00001172 print("vimconnector.get_hosts_info error parsing GET HOSTS vim response", hosts)
tierno7edb6752016-03-21 17:37:52 +01001173 return vimconn.HTTP_Internal_Server_Error, hosts
1174 #obtain hosts details
1175 hosts_dict={}
1176 for host in hosts['hosts']:
1177 url=self.url+'/hosts/'+host['id']
1178 try:
1179 vim_response = requests.get(url)
venkatamahesh6ecca182017-01-27 23:04:40 +05301180 except requests.exceptions.RequestException as e:
tierno7d782ef2019-10-04 12:56:31 +00001181 print( "get_hosts_info Exception: ", e.args)
tierno7edb6752016-03-21 17:37:52 +01001182 return -vimconn.HTTP_Not_Found, str(e.args[0])
tierno7d782ef2019-10-04 12:56:31 +00001183 print("vim get", url, "response:", vim_response.status_code, vim_response.json())
tierno7edb6752016-03-21 17:37:52 +01001184 if vim_response.status_code != 200:
tierno7d782ef2019-10-04 12:56:31 +00001185 print('vimconnector.get_hosts_info error getting detailed host {} {}'.format(vim_response.status_code, vim_response.json()))
tierno7edb6752016-03-21 17:37:52 +01001186 continue
1187 res,host_detail = self._format_in(vim_response, get_host_detail_response_schema)
1188 if res==False:
tierno7d782ef2019-10-04 12:56:31 +00001189 print ("vimconnector.get_hosts_info error parsing GET HOSTS/{} vim response {}".format(host['id']), host_detail)
tierno7edb6752016-03-21 17:37:52 +01001190 continue
1191 #print 'host id '+host['id'], json.dumps(host_detail, indent=4)
1192 self.host_vim2gui(host_detail, hosts_dict)
1193 return 200, hosts_dict
1194
1195 def get_hosts(self, vim_tenant):
1196 '''Get the hosts and deployed instances
1197 Returns the hosts content'''
1198 #obtain hosts list
1199 url=self.url+'/hosts'
1200 try:
1201 vim_response = requests.get(url)
venkatamahesh6ecca182017-01-27 23:04:40 +05301202 except requests.exceptions.RequestException as e:
tierno7d782ef2019-10-04 12:56:31 +00001203 print("get_hosts Exception: ", e.args)
tierno7edb6752016-03-21 17:37:52 +01001204 return -vimconn.HTTP_Not_Found, str(e.args[0])
tierno7d782ef2019-10-04 12:56:31 +00001205 print("vim get", url, "response:", vim_response.status_code, vim_response.json())
tierno7edb6752016-03-21 17:37:52 +01001206 #print vim_response.status_code
1207 #print json.dumps(vim_response.json(), indent=4)
1208 if vim_response.status_code != 200:
1209 #TODO: get error
tierno7d782ef2019-10-04 12:56:31 +00001210 print('vimconnector.get_hosts error getting host list {} {}'.format(vim_response.status_code, vim_response.json()))
tierno7edb6752016-03-21 17:37:52 +01001211 return -vim_response.status_code, "Error getting host list"
1212
1213 res,hosts = self._format_in(vim_response, get_hosts_response_schema)
1214
1215 if res==False:
tierno7d782ef2019-10-04 12:56:31 +00001216 print("vimconnector.get_host error parsing GET HOSTS vim response", hosts)
tierno7edb6752016-03-21 17:37:52 +01001217 return vimconn.HTTP_Internal_Server_Error, hosts
1218 #obtain instances from hosts
1219 for host in hosts['hosts']:
1220 url=self.url+'/' + vim_tenant + '/servers?hostId='+host['id']
1221 try:
1222 vim_response = requests.get(url)
venkatamahesh6ecca182017-01-27 23:04:40 +05301223 except requests.exceptions.RequestException as e:
tierno7d782ef2019-10-04 12:56:31 +00001224 print("get_hosts Exception: ", e.args)
tierno7edb6752016-03-21 17:37:52 +01001225 return -vimconn.HTTP_Not_Found, str(e.args[0])
tierno7d782ef2019-10-04 12:56:31 +00001226 print("vim get", url, "response:", vim_response.status_code, vim_response.json())
tierno7edb6752016-03-21 17:37:52 +01001227 if vim_response.status_code != 200:
tierno7d782ef2019-10-04 12:56:31 +00001228 print('vimconnector.get_hosts error getting instances at host {} {}'.format(vim_response.status_code, vim_response.json()))
tierno7edb6752016-03-21 17:37:52 +01001229 continue
1230 res,servers = self._format_in(vim_response, get_server_response_schema)
1231 if res==False:
tierno7d782ef2019-10-04 12:56:31 +00001232 print("vimconnector.get_host error parsing GET SERVERS/{} vim response {}".format(host['id']), servers)
tierno7edb6752016-03-21 17:37:52 +01001233 continue
1234 #print 'host id '+host['id'], json.dumps(host_detail, indent=4)
1235 host['instances'] = servers['servers']
1236 return 200, hosts['hosts']
1237
1238 def get_processor_rankings(self):
1239 '''Get the processor rankings in the VIM database'''
1240 url=self.url+'/processor_ranking'
1241 try:
1242 vim_response = requests.get(url)
venkatamahesh6ecca182017-01-27 23:04:40 +05301243 except requests.exceptions.RequestException as e:
tierno7d782ef2019-10-04 12:56:31 +00001244 print("get_processor_rankings Exception: ", e.args)
tierno7edb6752016-03-21 17:37:52 +01001245 return -vimconn.HTTP_Not_Found, str(e.args[0])
tierno7d782ef2019-10-04 12:56:31 +00001246 print("vim get", url, "response:", vim_response.status_code, vim_response.json())
tierno7edb6752016-03-21 17:37:52 +01001247 #print vim_response.status_code
1248 #print json.dumps(vim_response.json(), indent=4)
1249 if vim_response.status_code != 200:
1250 #TODO: get error
tierno7d782ef2019-10-04 12:56:31 +00001251 print('vimconnector.get_processor_rankings error getting processor rankings {} {}'.format(vim_response.status_code, vim_response.json()))
tierno7edb6752016-03-21 17:37:52 +01001252 return -vim_response.status_code, "Error getting processor rankings"
1253
1254 res,rankings = self._format_in(vim_response, get_processor_rankings_response_schema)
1255 return res, rankings['rankings']
1256
tiernoae4a8d12016-07-08 12:30:39 +02001257 def new_host(self, host_data):
1258 '''Adds a new host to VIM'''
1259 '''Returns status code of the VIM response'''
1260 payload_req = host_data
tierno392f2852016-05-13 12:28:55 +02001261 try:
tiernoae4a8d12016-07-08 12:30:39 +02001262 url = self.url_admin+'/hosts'
1263 self.logger.info("Adding a new host POST %s", url)
1264 vim_response = requests.post(url, headers = self.headers_req, data=payload_req)
1265 self._check_http_request_response(vim_response)
1266 self.logger.debug(vim_response.text)
1267 #print json.dumps(vim_response.json(), indent=4)
1268 response = vim_response.json()
1269 js_v(response, new_host_response_schema)
1270 r = self._remove_extra_items(response, new_host_response_schema)
1271 if r is not None:
1272 self.logger.warn("Warning: remove extra items %s", str(r))
1273 host_id = response['host']['id']
1274 return host_id
1275 except (requests.exceptions.RequestException, js_e.ValidationError) as e:
1276 self._format_request_exception(e)
1277
1278 def new_external_port(self, port_data):
1279 '''Adds a external port to VIM'''
1280 '''Returns the port identifier'''
1281 #TODO change to logging exception code policies
tierno7d782ef2019-10-04 12:56:31 +00001282 print( "VIMConnector: Adding a new external port")
tiernoae4a8d12016-07-08 12:30:39 +02001283 payload_req = port_data
tierno7edb6752016-03-21 17:37:52 +01001284 try:
tiernoae4a8d12016-07-08 12:30:39 +02001285 vim_response = requests.post(self.url_admin+'/ports', headers = self.headers_req, data=payload_req)
venkatamahesh6ecca182017-01-27 23:04:40 +05301286 except requests.exceptions.RequestException as e:
tiernoae4a8d12016-07-08 12:30:39 +02001287 self.logger.error("new_external_port Exception: ", str(e))
tierno7edb6752016-03-21 17:37:52 +01001288 return -vimconn.HTTP_Not_Found, str(e.args[0])
tierno7d782ef2019-10-04 12:56:31 +00001289 print( vim_response)
tierno7edb6752016-03-21 17:37:52 +01001290 #print vim_response.status_code
tiernoae4a8d12016-07-08 12:30:39 +02001291 if vim_response.status_code == 200:
1292 #print vim_response.json()
tierno7edb6752016-03-21 17:37:52 +01001293 #print json.dumps(vim_response.json(), indent=4)
tiernoae4a8d12016-07-08 12:30:39 +02001294 res, http_content = self._format_in(vim_response, new_port_response_schema)
1295 #print http_content
1296 if res:
1297 r = self._remove_extra_items(http_content, new_port_response_schema)
tierno7d782ef2019-10-04 12:56:31 +00001298 if r is not None: print("Warning: remove extra items ", r)
tiernoae4a8d12016-07-08 12:30:39 +02001299 #print http_content
1300 port_id = http_content['port']['id']
tierno7d782ef2019-10-04 12:56:31 +00001301 print("Port id: ",port_id)
tiernoae4a8d12016-07-08 12:30:39 +02001302 return vim_response.status_code,port_id
1303 else: return -vimconn.HTTP_Bad_Request,http_content
1304 else:
1305 #print vim_response.text
1306 jsonerror = self._format_jsonerror(vim_response)
tierno7d782ef2019-10-04 12:56:31 +00001307 text = 'Error in VIM "{}": not possible to add new external port. HTTP Response: {}. Error: {}'.format(
1308 self.url_admin, vim_response.status_code, jsonerror)
tiernoae4a8d12016-07-08 12:30:39 +02001309 #print text
1310 return -vim_response.status_code,text
tierno7edb6752016-03-21 17:37:52 +01001311
tiernoae4a8d12016-07-08 12:30:39 +02001312 def new_external_network(self,net_name,net_type):
1313 '''Adds a external network to VIM (shared)'''
1314 '''Returns the network identifier'''
1315 #TODO change to logging exception code policies
tierno7d782ef2019-10-04 12:56:31 +00001316 print("VIMConnector: Adding external shared network to VIM (type " + net_type + "): "+ net_name)
tiernoae4a8d12016-07-08 12:30:39 +02001317
1318 payload_req = '{"network":{"name": "' + net_name + '","shared":true,"type": "' + net_type + '"}}'
1319 try:
1320 vim_response = requests.post(self.url+'/networks', headers = self.headers_req, data=payload_req)
venkatamahesh6ecca182017-01-27 23:04:40 +05301321 except requests.exceptions.RequestException as e:
tiernoae4a8d12016-07-08 12:30:39 +02001322 self.logger.error( "new_external_network Exception: ", e.args)
1323 return -vimconn.HTTP_Not_Found, str(e.args[0])
tierno7d782ef2019-10-04 12:56:31 +00001324 print(vim_response)
tiernoae4a8d12016-07-08 12:30:39 +02001325 #print vim_response.status_code
1326 if vim_response.status_code == 200:
1327 #print vim_response.json()
1328 #print json.dumps(vim_response.json(), indent=4)
1329 res,http_content = self._format_in(vim_response, new_network_response_schema)
1330 #print http_content
1331 if res:
1332 r = self._remove_extra_items(http_content, new_network_response_schema)
tierno7d782ef2019-10-04 12:56:31 +00001333 if r is not None: print("Warning: remove extra items ", r)
tiernoae4a8d12016-07-08 12:30:39 +02001334 #print http_content
1335 network_id = http_content['network']['id']
tierno7d782ef2019-10-04 12:56:31 +00001336 print( "Network id: ",network_id)
tiernoae4a8d12016-07-08 12:30:39 +02001337 return vim_response.status_code,network_id
1338 else: return -vimconn.HTTP_Bad_Request,http_content
1339 else:
1340 #print vim_response.text
1341 jsonerror = self._format_jsonerror(vim_response)
tierno7d782ef2019-10-04 12:56:31 +00001342 text = 'Error in VIM "{}": not possible to add new external network. HTTP Response: {}. Error: {}'.format(
1343 self.url, vim_response.status_code, jsonerror)
tiernoae4a8d12016-07-08 12:30:39 +02001344 #print text
1345 return -vim_response.status_code,text
1346
1347 def connect_port_network(self, port_id, network_id, admin=False):
1348 '''Connects a external port to a network'''
1349 '''Returns status code of the VIM response'''
1350 #TODO change to logging exception code policies
tierno7d782ef2019-10-04 12:56:31 +00001351 print("VIMConnector: Connecting external port to network")
tiernoae4a8d12016-07-08 12:30:39 +02001352
1353 payload_req = '{"port":{"network_id":"' + network_id + '"}}'
1354 if admin:
1355 if self.url_admin==None:
1356 return -vimconn.HTTP_Unauthorized, "datacenter cannot contain admin URL"
1357 url= self.url_admin
1358 else:
1359 url= self.url
1360 try:
1361 vim_response = requests.put(url +'/ports/'+port_id, headers = self.headers_req, data=payload_req)
venkatamahesh6ecca182017-01-27 23:04:40 +05301362 except requests.exceptions.RequestException as e:
tierno7d782ef2019-10-04 12:56:31 +00001363 print("connect_port_network Exception: ", e.args)
tiernoae4a8d12016-07-08 12:30:39 +02001364 return -vimconn.HTTP_Not_Found, str(e.args[0])
tierno7d782ef2019-10-04 12:56:31 +00001365 print(vim_response)
tiernoae4a8d12016-07-08 12:30:39 +02001366 #print vim_response.status_code
1367 if vim_response.status_code == 200:
1368 #print vim_response.json()
1369 #print json.dumps(vim_response.json(), indent=4)
1370 res,http_content = self._format_in(vim_response, new_port_response_schema)
1371 #print http_content
1372 if res:
1373 r = self._remove_extra_items(http_content, new_port_response_schema)
tierno7d782ef2019-10-04 12:56:31 +00001374 if r is not None: print("Warning: remove extra items ", r)
tiernoae4a8d12016-07-08 12:30:39 +02001375 #print http_content
1376 port_id = http_content['port']['id']
tierno7d782ef2019-10-04 12:56:31 +00001377 print("Port id: ",port_id)
tiernoae4a8d12016-07-08 12:30:39 +02001378 return vim_response.status_code,port_id
1379 else: return -vimconn.HTTP_Bad_Request,http_content
1380 else:
tierno7d782ef2019-10-04 12:56:31 +00001381 print(vim_response.text)
tiernoae4a8d12016-07-08 12:30:39 +02001382 jsonerror = self._format_jsonerror(vim_response)
tierno7d782ef2019-10-04 12:56:31 +00001383 text = 'Error in VIM "{}": not possible to connect external port to network. HTTP Response: {}.' \
1384 ' Error: {}'.format(self.url_admin, vim_response.status_code, jsonerror)
1385 print(text)
tiernoae4a8d12016-07-08 12:30:39 +02001386 return -vim_response.status_code,text
tierno7edb6752016-03-21 17:37:52 +01001387
1388