1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
25 vimconnector implements all the methods to interact with openvim using the openvim API.
27 For interacting with Openstack refer to osconnector.
29 __author__
="Alfonso Tierno, Gerardo Garcia"
30 __date__
="$26-aug-2014 11:09:29$"
36 from openmano_schemas
import id_schema
, name_schema
, nameshort_schema
, description_schema
, \
37 vlan1000_schema
, integer0_schema
38 from jsonschema
import validate
as js_v
, exceptions
as js_e
40 '''contain the openvim virtual machine status to openmano status'''
41 vmStatus2manoFormat
={'ACTIVE':'ACTIVE',
43 'SUSPENDED': 'SUSPENDED',
44 'INACTIVE':'INACTIVE',
46 'ERROR':'ERROR','DELETED':'DELETED'
48 netStatus2manoFormat
={'ACTIVE':'ACTIVE','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED', 'DOWN':'DOWN'
66 "required": ["id","name"]
74 "required": ["id","name"]
76 new_host_response_schema
= {
77 "title":"host response information schema",
78 "$schema": "http://json-schema.org/draft-04/schema#",
84 "additionalProperties": False
87 get_images_response_schema
= {
88 "title":"openvim images response information schema",
89 "$schema": "http://json-schema.org/draft-04/schema#",
94 "items": image_schema
,
97 "required": ["images"],
98 "additionalProperties": False
101 get_hosts_response_schema
= {
102 "title":"openvim hosts response information schema",
103 "$schema": "http://json-schema.org/draft-04/schema#",
108 "items": host_schema
,
111 "required": ["hosts"],
112 "additionalProperties": False
115 get_host_detail_response_schema
= new_host_response_schema
# TODO: Content is not parsed yet
117 get_server_response_schema
= {
118 "title":"openvim server response information schema",
119 "$schema": "http://json-schema.org/draft-04/schema#",
124 "items": server_schema
,
127 "required": ["servers"],
128 "additionalProperties": False
131 new_tenant_response_schema
= {
132 "title":"tenant response information schema",
133 "$schema": "http://json-schema.org/draft-04/schema#",
140 "name": nameshort_schema
,
141 "description":description_schema
,
142 "enabled":{"type" : "boolean"}
147 "required": ["tenant"],
148 "additionalProperties": False
151 new_network_response_schema
= {
152 "title":"network response information schema",
153 "$schema": "http://json-schema.org/draft-04/schema#",
161 "type":{"type":"string", "enum":["bridge_man","bridge_data","data", "ptp"]},
162 "shared":{"type":"boolean"},
163 "tenant_id":id_schema
,
164 "admin_state_up":{"type":"boolean"},
165 "vlan":vlan1000_schema
170 "required": ["network"],
171 "additionalProperties": False
175 # get_network_response_schema = {
176 # "title":"get network response information schema",
177 # "$schema": "http://json-schema.org/draft-04/schema#",
184 # "name":name_schema,
185 # "type":{"type":"string", "enum":["bridge_man","bridge_data","data", "ptp"]},
186 # "shared":{"type":"boolean"},
187 # "tenant_id":id_schema,
188 # "admin_state_up":{"type":"boolean"},
189 # "vlan":vlan1000_schema
194 # "required": ["network"],
195 # "additionalProperties": False
199 new_port_response_schema
= {
200 "title":"port response information schema",
201 "$schema": "http://json-schema.org/draft-04/schema#",
212 "required": ["port"],
213 "additionalProperties": False
216 get_flavor_response_schema
= {
217 "title":"openvim flavors response information schema",
218 "$schema": "http://json-schema.org/draft-04/schema#",
226 "extended": {"type":"object"},
228 "required": ["id", "name"],
231 "required": ["flavor"],
232 "additionalProperties": False
235 new_flavor_response_schema
= {
236 "title":"flavor response information schema",
237 "$schema": "http://json-schema.org/draft-04/schema#",
248 "required": ["flavor"],
249 "additionalProperties": False
252 new_image_response_schema
= {
253 "title":"image response information schema",
254 "$schema": "http://json-schema.org/draft-04/schema#",
265 "required": ["image"],
266 "additionalProperties": False
269 new_vminstance_response_schema
= {
270 "title":"server response information schema",
271 "$schema": "http://json-schema.org/draft-04/schema#",
282 "required": ["server"],
283 "additionalProperties": False
286 get_processor_rankings_response_schema
= {
287 "title":"processor rankings information schema",
288 "$schema": "http://json-schema.org/draft-04/schema#",
296 "model": description_schema
,
297 "value": integer0_schema
299 "additionalProperties": False,
300 "required": ["model","value"]
303 "additionalProperties": False,
304 "required": ["rankings"]
308 class vimconnector(vimconn
.vimconnector
):
309 def __init__(self
, uuid
, name
, tenant_id
, tenant_name
, url
, url_admin
=None, user
=None, passwd
=None,debug
=True,config
={}):
310 vimconn
.vimconnector
.__init
__(self
, uuid
, name
, tenant_id
, tenant_name
, url
, url_admin
, user
, passwd
, debug
, config
)
312 self
.headers_req
= {'content-type': 'application/json'}
314 self
.tenant
= tenant_id
316 def __setitem__(self
,index
, value
):
317 '''Set individuals parameters
318 Throw TypeError, KeyError
320 if index
=='tenant_id':
322 elif index
=='tenant_name':
324 vimconn
.vimconnector
.__setitem
__(self
,index
, value
)
326 def _get_my_tenant(self
):
327 '''Obtain uuid of my tenant from name
332 vim_response
= requests
.get(self
.url
+'/tenants?name='+ self
.tenant_name
, headers
= self
.headers_req
)
333 if vim_response
.status_code
!= 200:
334 raise vimconn
.vimconnectorException ("_get_my_tenant response " + str(vim_response
.status_code
))
335 tenant_list
= vim_response
.json()["tenants"]
336 if len(tenant_list
) == 0:
337 raise vimconn
.vimconnectorException ("No tenant found for name '%s'" % str(self
.tenant_name
))
338 elif len(tenant_list
) > 1:
339 raise vimconn
.vimconnectorException ("More that one tenant found for name '%s'" % str(self
.tenant_name
))
340 self
.tenant
= tenant_list
[0]["id"]
343 def _format_jsonerror(self
,http_response
):
345 data
= http_response
.json()
346 return data
["error"]["description"]
348 return http_response
.text
350 def _format_in(self
, http_response
, schema
):
352 client_data
= http_response
.json()
353 js_v(client_data
, schema
)
354 #print "Input data: ", str(client_data)
355 return True, client_data
356 except js_e
.ValidationError
, exc
:
357 print "validate_in error, jsonschema exception ", exc
.message
, "at", exc
.path
358 return False, ("validate_in error, jsonschema exception ", exc
.message
, "at", exc
.path
)
360 def _remove_extra_items(self
, data
, schema
):
362 if type(data
) is tuple or type(data
) is list:
364 a
= self
._remove
_extra
_items
(d
, schema
['items'])
365 if a
is not None: deleted
.append(a
)
366 elif type(data
) is dict:
367 for k
in data
.keys():
368 if 'properties' not in schema
or k
not in schema
['properties'].keys():
372 a
= self
._remove
_extra
_items
(data
[k
], schema
['properties'][k
])
373 if a
is not None: deleted
.append({k
:a
})
374 if len(deleted
) == 0: return None
375 elif len(deleted
) == 1: return deleted
[0]
378 def new_host(self
, host_data
):
379 '''Adds a new host to VIM'''
380 '''Returns status code of the VIM response'''
381 print "VIMConnector: Adding a new host"
382 payload_req
= host_data
384 vim_response
= requests
.post(self
.url_admin
+'/hosts', headers
= self
.headers_req
, data
=payload_req
)
385 except requests
.exceptions
.RequestException
, e
:
386 print "new_host Exception: ", e
.args
387 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
389 #print vim_response.status_code
390 if vim_response
.status_code
== 200:
391 #print vim_response.json()
392 #print json.dumps(vim_response.json(), indent=4)
393 res
,http_content
= self
._format
_in
(vim_response
, new_host_response_schema
)
396 r
= self
._remove
_extra
_items
(http_content
, new_host_response_schema
)
397 if r
is not None: print "Warning: remove extra items ", r
399 host_id
= http_content
['host']['id']
400 #print "Host id: ",host_id
401 return vim_response
.status_code
,host_id
402 else: return -vimconn
.HTTP_Bad_Request
,http_content
404 #print vim_response.text
405 jsonerror
= self
.__format
_jsonerror
(vim_response
)
406 text
= 'Error in VIM "%s": not possible to add new host. HTTP Response: %d. Error: %s' % (self
.url_admin
, vim_response
.status_code
, jsonerror
)
408 return -vim_response
.status_code
,text
410 def new_external_port(self
, port_data
):
411 '''Adds a external port to VIM'''
412 '''Returns the port identifier'''
413 print "VIMConnector: Adding a new external port"
414 payload_req
= port_data
416 vim_response
= requests
.post(self
.url_admin
+'/ports', headers
= self
.headers_req
, data
=payload_req
)
417 except requests
.exceptions
.RequestException
, e
:
418 print "new_external_port Exception: ", e
.args
419 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
421 #print vim_response.status_code
422 if vim_response
.status_code
== 200:
423 #print vim_response.json()
424 #print json.dumps(vim_response.json(), indent=4)
425 res
, http_content
= self
.__format
_in
(vim_response
, new_port_response_schema
)
428 r
= self
._remove
_extra
_items
(http_content
, new_port_response_schema
)
429 if r
is not None: print "Warning: remove extra items ", r
431 port_id
= http_content
['port']['id']
432 print "Port id: ",port_id
433 return vim_response
.status_code
,port_id
434 else: return -vimconn
.HTTP_Bad_Request
,http_content
436 #print vim_response.text
437 jsonerror
= self
._format
_jsonerror
(vim_response
)
438 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
)
440 return -vim_response
.status_code
,text
442 def new_external_network(self
,net_name
,net_type
):
443 '''Adds a external network to VIM (shared)'''
444 '''Returns the network identifier'''
445 print "VIMConnector: Adding external shared network to VIM (type " + net_type
+ "): "+ net_name
447 payload_req
= '{"network":{"name": "' + net_name
+ '","shared":true,"type": "' + net_type
+ '"}}'
449 vim_response
= requests
.post(self
.url
+'/networks', headers
= self
.headers_req
, data
=payload_req
)
450 except requests
.exceptions
.RequestException
, e
:
451 print "new_external_network Exception: ", e
.args
452 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
454 #print vim_response.status_code
455 if vim_response
.status_code
== 200:
456 #print vim_response.json()
457 #print json.dumps(vim_response.json(), indent=4)
458 res
,http_content
= self
._format
_in
(vim_response
, new_network_response_schema
)
461 r
= self
._remove
_extra
_items
(http_content
, new_network_response_schema
)
462 if r
is not None: print "Warning: remove extra items ", r
464 network_id
= http_content
['network']['id']
465 print "Network id: ",network_id
466 return vim_response
.status_code
,network_id
467 else: return -vimconn
.HTTP_Bad_Request
,http_content
469 #print vim_response.text
470 jsonerror
= self
._format
_jsonerror
(vim_response
)
471 text
= 'Error in VIM "%s": not possible to add new external network. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
473 return -vim_response
.status_code
,text
475 def connect_port_network(self
, port_id
, network_id
, admin
=False):
476 '''Connects a external port to a network'''
477 '''Returns status code of the VIM response'''
478 print "VIMConnector: Connecting external port to network"
480 payload_req
= '{"port":{"network_id":"' + network_id
+ '"}}'
482 if self
.url_admin
==None:
483 return -vimconn
.HTTP_Unauthorized
, "datacenter cannot contain admin URL"
488 vim_response
= requests
.put(url
+'/ports/'+port_id
, headers
= self
.headers_req
, data
=payload_req
)
489 except requests
.exceptions
.RequestException
, e
:
490 print "connect_port_network Exception: ", e
.args
491 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
493 #print vim_response.status_code
494 if vim_response
.status_code
== 200:
495 #print vim_response.json()
496 #print json.dumps(vim_response.json(), indent=4)
497 res
,http_content
= self
._format
_in
(vim_response
, new_port_response_schema
)
500 r
= self
._remove
_extra
_items
(http_content
, new_port_response_schema
)
501 if r
is not None: print "Warning: remove extra items ", r
503 port_id
= http_content
['port']['id']
504 print "Port id: ",port_id
505 return vim_response
.status_code
,port_id
506 else: return -vimconn
.HTTP_Bad_Request
,http_content
508 print vim_response
.text
509 jsonerror
= self
._format
_jsonerror
(vim_response
)
510 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
)
512 return -vim_response
.status_code
,text
514 def new_tenant(self
,tenant_name
,tenant_description
):
515 '''Adds a new tenant to VIM'''
516 '''Returns the tenant identifier'''
517 print "VIMConnector: Adding a new tenant to VIM"
518 payload_dict
= {"tenant": {"name":tenant_name
,"description": tenant_description
, "enabled": True}}
519 payload_req
= json
.dumps(payload_dict
)
523 vim_response
= requests
.post(self
.url
+'/tenants', headers
= self
.headers_req
, data
=payload_req
)
524 except requests
.exceptions
.RequestException
, e
:
525 print "new_tenant Exception: ", e
.args
526 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
528 if vim_response
.status_code
== 200:
529 #print vim_response.json()
530 #print json.dumps(vim_response.json(), indent=4)
531 res
,http_content
= self
._format
_in
(vim_response
, new_tenant_response_schema
)
534 r
= self
._remove
_extra
_items
(http_content
, new_tenant_response_schema
)
535 if r
is not None: print "Warning: remove extra items ", r
537 tenant_id
= http_content
['tenant']['id']
538 #print "Tenant id: ",tenant_id
539 return vim_response
.status_code
,tenant_id
540 else: return -vimconn
.HTTP_Bad_Request
,http_content
542 #print vim_response.text
543 jsonerror
= self
._format
_jsonerror
(vim_response
)
544 text
= 'Error in VIM "%s": not possible to add new tenant. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
546 return -vim_response
.status_code
,text
548 def delete_tenant(self
,tenant_id
,):
549 '''Delete a tenant from VIM'''
550 '''Returns the tenant identifier'''
551 print "VIMConnector: Deleting a tenant from VIM"
553 vim_response
= requests
.delete(self
.url
+'/tenants/'+tenant_id
, headers
= self
.headers_req
)
554 except requests
.exceptions
.RequestException
, e
:
555 print "delete_tenant Exception: ", e
.args
556 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
558 if vim_response
.status_code
== 200:
559 return vim_response
.status_code
,tenant_id
561 #print vim_response.text
562 jsonerror
= self
._format
_jsonerror
(vim_response
)
563 text
= 'Error in VIM "%s": not possible to delete tenant. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
565 return -vim_response
.status_code
,text
567 def new_tenant_network(self
,net_name
,net_type
,public
=False, **vim_specific
):
568 '''Adds a tenant network to VIM'''
569 '''Returns the network identifier'''
571 self
._get
_my
_tenant
()
572 except Exception as e
:
573 return -vimconn
.HTTP_Not_Found
, str(e
)
574 print "vim_specific", vim_specific
575 if net_type
=="bridge":
576 net_type
="bridge_data"
577 print "VIMConnector: Adding a new tenant network to VIM (tenant: " + str(self
.tenant
) + ", type: " + net_type
+ "): "+ net_name
579 payload_req
= {"name": net_name
, "type": net_type
, "tenant_id": self
.tenant
, "shared": public
}
580 payload_req
.update(vim_specific
)
582 vim_response
= requests
.post(self
.url
+'/networks', headers
= self
.headers_req
, data
=json
.dumps({"network": payload_req
}) )
583 except requests
.exceptions
.RequestException
, e
:
584 print "new_tenant_network Exception: ", e
.args
585 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
587 #print vim_response.status_code
588 if vim_response
.status_code
== 200:
589 #print vim_response.json()
590 #print json.dumps(vim_response.json(), indent=4)
591 res
,http_content
= self
._format
_in
(vim_response
, new_network_response_schema
)
594 r
= self
._remove
_extra
_items
(http_content
, new_network_response_schema
)
595 if r
is not None: print "Warning: remove extra items ", r
597 network_id
= http_content
['network']['id']
598 print "Tenant Network id: ",network_id
599 return vim_response
.status_code
,network_id
600 else: return -vimconn
.HTTP_Bad_Request
,http_content
602 #print vim_response.text
603 jsonerror
= self
._format
_jsonerror
(vim_response
)
604 text
= 'Error in VIM "%s": not possible to add new tenant network. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
606 return -vim_response
.status_code
,text
608 def get_tenant_list(self
, filter_dict
={}):
609 '''Obtain tenants of VIM
613 Returns the network list of dictionaries
615 print "VIMConnector.get_tenant_list: Getting tenants from VIM (filter: " + str(filter_dict
) + "): "
618 for k
,v
in filter_dict
.iteritems():
619 filterquery
.append(str(k
)+'='+str(v
))
620 if len(filterquery
)>0:
621 filterquery_text
='?'+ '&'.join(filterquery
)
623 print self
.url
+'/tenants'+filterquery_text
624 vim_response
= requests
.get(self
.url
+'/tenants'+filterquery_text
, headers
= self
.headers_req
)
625 except requests
.exceptions
.RequestException
, e
:
626 print "get_tenant_list Exception: ", e
.args
627 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
629 #print vim_response.status_code
630 if vim_response
.status_code
== 200:
631 #print vim_response.json()
632 #print json.dumps(vim_response.json(), indent=4)
633 #TODO: parse input datares,http_content = self._format_in(vim_response, new_network_response_schema)
635 return vim_response
.status_code
, vim_response
.json()["tenants"]
637 #print vim_response.text
638 jsonerror
= self
._format
_jsonerror
(vim_response
)
639 text
= 'Error in VIM "%s": not possible to get tenant list. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
641 return -vim_response
.status_code
,text
643 def get_network_list(self
, filter_dict
={}):
644 '''Obtain tenant networks of VIM
650 admin_state_up: boolean
652 Returns the network list of dictionaries
655 filter_dict
["tenant_id"] = self
._get
_my
_tenant
()
656 print "VIMConnector.get_network_list: Getting tenant network from VIM (filter: " + str(filter_dict
) + "): "
659 for k
,v
in filter_dict
.iteritems():
660 filterquery
.append(str(k
)+'='+str(v
))
661 if len(filterquery
)>0:
662 filterquery_text
='?'+ '&'.join(filterquery
)
664 print self
.url
+'/networks'+filterquery_text
665 vim_response
= requests
.get(self
.url
+'/networks'+filterquery_text
, headers
= self
.headers_req
)
666 except requests
.exceptions
.RequestException
, e
:
667 print "get_network_list Exception: ", e
.args
668 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
670 #print vim_response.status_code
671 if vim_response
.status_code
== 200:
672 #print vim_response.json()
673 #print json.dumps(vim_response.json(), indent=4)
674 #TODO: parse input datares,http_content = self._format_in(vim_response, new_network_response_schema)
676 return vim_response
.status_code
, vim_response
.json()["networks"]
678 #print vim_response.text
679 jsonerror
= self
._format
_jsonerror
(vim_response
)
680 text
= 'Error in VIM "%s": not possible to get network list. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
682 return -vim_response
.status_code
,text
684 def get_tenant_network(self
, net_id
, tenant_id
=None):
685 '''Obtain tenant networks of VIM'''
686 '''Returns the network information from a network id'''
688 print "VIMconnector.get_tenant_network(): Getting tenant network %s from VIM" % net_id
689 filter_dict
={"id": net_id
}
691 filter_dict
["tenant_id"] = tenant_id
692 r
, net_list
= self
.get_network_list(filter_dict
)
696 return -vimconn
.HTTP_Not_Found
, "Network '%s' not found" % net_id
697 elif len(net_list
)>1:
698 return -vimconn
.HTTP_Conflict
, "Found more than one network with this criteria"
699 return 1, net_list
[0]
701 def delete_tenant_network(self
, net_id
):
702 '''Deletes a tenant network from VIM'''
703 '''Returns the network identifier'''
705 self
._get
_my
_tenant
()
706 except Exception as e
:
707 return -vimconn
.HTTP_Not_Found
, str(e
)
708 print "VIMConnector: Deleting a new tenant network from VIM tenant: " + self
.tenant
+ ", id: " + net_id
711 vim_response
= requests
.delete(self
.url
+'/networks/'+net_id
, headers
=self
.headers_req
)
712 except requests
.exceptions
.RequestException
, e
:
713 print "delete_tenant_network Exception: ", e
.args
714 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
717 #print vim_response.status_code
718 if vim_response
.status_code
== 200:
719 return vim_response
.status_code
,net_id
721 #print vim_response.text
722 jsonerror
= self
._format
_jsonerror
(vim_response
)
723 text
= 'Error in VIM "%s": not possible to delete tenant network. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
725 return -vim_response
.status_code
,text
727 def refresh_tenant_network(self
, net_id
):
728 '''Refreshes the status of the tenant network'''
729 '''Returns: 0 if no error,
733 def get_tenant_flavor(self
, flavor_id
):
734 '''Obtain flavor details from the VIM
735 Returns the flavor dict details
738 self
._get
_my
_tenant
()
739 except Exception as e
:
740 return -vimconn
.HTTP_Not_Found
, str(e
)
741 print "VIMConnector: Getting flavor from VIM"
742 #print "VIM URL:",self.url
743 #print "Tenant id:",self.tenant
744 #print "Flavor:",flavor_data
746 vim_response
= requests
.get(self
.url
+'/'+self
.tenant
+'/flavors/'+flavor_id
, headers
= self
.headers_req
)
747 except requests
.exceptions
.RequestException
, e
:
748 print "get_tenant_flavor Exception: ", e
.args
749 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
751 #print vim_response.status_code
752 if vim_response
.status_code
== 200:
753 #print vim_response.json()
754 #print json.dumps(vim_response.json(), indent=4)
755 res
,http_content
= self
._format
_in
(vim_response
, get_flavor_response_schema
)
758 r
= self
._remove
_extra
_items
(http_content
, get_flavor_response_schema
)
759 if r
is not None: print "Warning: remove extra items ", r
761 flavor_id
= http_content
['flavor']['id']
762 print "Flavor id: ",flavor_id
763 return vim_response
.status_code
,flavor_id
764 else: return -vimconn
.HTTP_Bad_Request
,http_content
767 #print vim_response.text
768 jsonerror
= self
._format
_jsonerror
(vim_response
)
769 text
= 'Error in VIM "%s": not possible to get flavor. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
771 return -vim_response
.status_code
,text
773 def new_tenant_flavor(self
, flavor_data
):
774 '''Adds a tenant flavor to VIM'''
775 '''Returns the flavor identifier'''
777 self
._get
_my
_tenant
()
778 except Exception as e
:
779 return -vimconn
.HTTP_Not_Found
, str(e
)
780 print "VIMConnector: Adding a new flavor to VIM"
781 #print "VIM URL:",self.url
782 #print "Tenant id:",self.tenant
783 #print "Flavor:",flavor_data
784 payload_req
= json
.dumps({'flavor': flavor_data
})
786 vim_response
= requests
.post(self
.url
+'/'+self
.tenant
+'/flavors', headers
= self
.headers_req
, data
=payload_req
)
787 except requests
.exceptions
.RequestException
, e
:
788 print "new_tenant_flavor Exception: ", e
.args
789 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
791 #print vim_response.status_code
792 if vim_response
.status_code
== 200:
793 #print vim_response.json()
794 #print json.dumps(vim_response.json(), indent=4)
795 res
,http_content
= self
._format
_in
(vim_response
, new_flavor_response_schema
)
798 r
= self
._remove
_extra
_items
(http_content
, new_flavor_response_schema
)
799 if r
is not None: print "Warning: remove extra items ", r
801 flavor_id
= http_content
['flavor']['id']
802 print "Flavor id: ",flavor_id
803 return vim_response
.status_code
,flavor_id
804 else: return -vimconn
.HTTP_Bad_Request
,http_content
807 #print vim_response.text
808 jsonerror
= self
._format
_jsonerror
(vim_response
)
809 text
= 'Error in VIM "%s": not possible to add new flavor. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
811 return -vim_response
.status_code
,text
813 def delete_tenant_flavor(self
,flavor_id
):
814 '''Deletes a tenant flavor from VIM'''
815 '''Returns the HTTP response code and a message indicating details of the success or fail'''
817 self
._get
_my
_tenant
()
818 except Exception as e
:
819 return -vimconn
.HTTP_Not_Found
, str(e
)
820 print "VIMConnector: Deleting a flavor from VIM"
821 print "VIM URL:",self
.url
822 print "Tenant id:",self
.tenant
823 print "Flavor id:",flavor_id
824 #payload_req = flavor_data
826 vim_response
= requests
.delete(self
.url
+'/'+self
.tenant
+'/flavors/'+flavor_id
)
827 except requests
.exceptions
.RequestException
, e
:
828 print "delete_tenant_flavor Exception: ", e
.args
829 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
831 print vim_response
.status_code
832 if vim_response
.status_code
== 200:
833 result
= vim_response
.json()["result"]
836 #print vim_response.text
837 jsonerror
= self
._format
_jsonerror
(vim_response
)
838 text
= 'Error in VIM "%s": not possible to delete flavor. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
840 return -vim_response
.status_code
,text
842 def new_tenant_image(self
,image_dict
):
844 Adds a tenant image to VIM
846 200, image-id if the image is created
847 <0, message if there is an error
850 self
._get
_my
_tenant
()
851 except Exception as e
:
852 return -vimconn
.HTTP_Not_Found
, str(e
)
853 print "VIMConnector: Adding a new image to VIM", image_dict
['location']
854 new_image_dict
={'name': image_dict
['name']}
855 if 'description' in image_dict
and image_dict
['description'] != None:
856 new_image_dict
['description'] = image_dict
['description']
857 if 'metadata' in image_dict
and image_dict
['metadata'] != None:
858 new_image_dict
['metadata'] = yaml
.load(image_dict
['metadata'])
859 if 'location' in image_dict
and image_dict
['location'] != None:
860 new_image_dict
['path'] = image_dict
['location']
861 payload_req
= json
.dumps({"image":new_image_dict
})
862 url
=self
.url
+ '/' + self
.tenant
+ '/images'
864 vim_response
= requests
.post(url
, headers
= self
.headers_req
, data
=payload_req
)
865 except requests
.exceptions
.RequestException
, e
:
866 print "new_tenant_image Exception: ", e
.args
867 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
869 #print vim_response.status_code
870 if vim_response
.status_code
== 200:
871 #print vim_response.json()
872 #print json.dumps(vim_response.json(), indent=4)
873 res
,http_content
= self
._format
_in
(vim_response
, new_image_response_schema
)
876 r
= self
._remove
_extra
_items
(http_content
, new_image_response_schema
)
877 if r
is not None: print "Warning: remove extra items ", r
879 image_id
= http_content
['image']['id']
880 print "Image id: ",image_id
881 return vim_response
.status_code
,image_id
882 else: return -vimconn
.HTTP_Bad_Request
,http_content
884 #print vim_response.text
885 jsonerror
= self
._format
_jsonerror
(vim_response
)
886 text
= 'Error in VIM "%s": not possible to add new image. HTTP Response: %d. Error: %s' % (url
, vim_response
.status_code
, jsonerror
)
888 return -vim_response
.status_code
,text
890 def delete_tenant_image(self
, image_id
):
891 '''Deletes a tenant image from VIM'''
892 '''Returns the HTTP response code and a message indicating details of the success or fail'''
894 self
._get
_my
_tenant
()
895 except Exception as e
:
896 return -vimconn
.HTTP_Not_Found
, str(e
)
897 print "VIMConnector: Deleting an image from VIM"
898 #payload_req = flavor_data
899 url
=self
.url
+ '/'+ self
.tenant
+'/images/'+image_id
901 vim_response
= requests
.delete(url
)
902 except requests
.exceptions
.RequestException
, e
:
903 print "delete_tenant_image Exception url '%s': " % url
, e
.args
904 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
906 print vim_response
.status_code
907 if vim_response
.status_code
== 200:
908 result
= vim_response
.json()["result"]
911 #print vim_response.text
912 jsonerror
= self
._format
_jsonerror
(vim_response
)
913 text
= 'Error in VIM "%s": not possible to delete image. HTTP Response: %d. Error: %s' % (url
, vim_response
.status_code
, jsonerror
)
915 return -vim_response
.status_code
,text
917 def new_tenant_vminstancefromJSON(self
, vm_data
):
918 '''Adds a VM instance to VIM'''
919 '''Returns the instance identifier'''
921 self
._get
_my
_tenant
()
922 except Exception as e
:
923 return -vimconn
.HTTP_Not_Found
, str(e
)
924 print "VIMConnector: Adding a new VM instance from JSON to VIM"
925 payload_req
= vm_data
927 vim_response
= requests
.post(self
.url
+'/'+self
.tenant
+'/servers', headers
= self
.headers_req
, data
=payload_req
)
928 except requests
.exceptions
.RequestException
, e
:
929 print "new_tenant_vminstancefromJSON Exception: ", e
.args
930 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
932 #print vim_response.status_code
933 if vim_response
.status_code
== 200:
934 #print vim_response.json()
935 #print json.dumps(vim_response.json(), indent=4)
936 res
,http_content
= self
._format
_in
(vim_response
, new_image_response_schema
)
939 r
= self
._remove
_extra
_items
(http_content
, new_image_response_schema
)
940 if r
is not None: print "Warning: remove extra items ", r
942 vminstance_id
= http_content
['server']['id']
943 print "Tenant image id: ",vminstance_id
944 return vim_response
.status_code
,vminstance_id
945 else: return -vimconn
.HTTP_Bad_Request
,http_content
947 #print vim_response.text
948 jsonerror
= self
._format
_jsonerror
(vim_response
)
949 text
= 'Error in VIM "%s": not possible to add new vm instance. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
951 return -vim_response
.status_code
,text
953 def new_tenant_vminstance(self
,name
,description
,start
,image_id
,flavor_id
,net_list
):
954 '''Adds a VM instance to VIM
956 start: indicates if VM must start or boot in pause mode. Ignored
957 image_id,flavor_id: image and flavor uuid
958 net_list: list of interfaces, each one is a dictionary with:
960 net_id: network uuid to connect
961 vpci: virtual vcpi to assign
962 model: interface model, virtio, e2000, ...
964 use: 'data', 'bridge', 'mgmt'
965 type: 'virtual', 'PF', 'VF', 'VFnotShared'
966 vim_id: filled/added by this function
967 #TODO ip, security groups
968 Returns >=0, the instance identifier
972 self
._get
_my
_tenant
()
973 except Exception as e
:
974 return -vimconn
.HTTP_Not_Found
, str(e
)
975 print "VIMConnector: Adding a new VM instance to VIM"
978 # for k,v in net_dict.items():
980 # net_list.append('{"name":"' + k + '", "uuid":"' + v + '"}')
981 # net_list_string = ', '.join(net_list)
984 if not net
.get("net_id"):
986 net_dict
={'uuid': net
["net_id"]}
987 if net
.get("type"): net_dict
["type"] = net
["type"]
988 if net
.get("name"): net_dict
["name"] = net
["name"]
989 if net
.get("vpci"): net_dict
["vpci"] = net
["vpci"]
990 if net
.get("model"): net_dict
["model"] = net
["model"]
991 if net
.get("mac_address"): net_dict
["mac_address"] = net
["mac_address"]
992 virtio_net_list
.append(net_dict
)
993 payload_dict
={ "name": name
,
994 "description": description
,
995 "imageRef": image_id
,
996 "flavorRef": flavor_id
,
997 "networks": virtio_net_list
1000 payload_dict
["start"] = start
1001 payload_req
= json
.dumps({"server": payload_dict
})
1002 print self
.url
+'/'+self
.tenant
+'/servers'+payload_req
1004 vim_response
= requests
.post(self
.url
+'/'+self
.tenant
+'/servers', headers
= self
.headers_req
, data
=payload_req
)
1005 except requests
.exceptions
.RequestException
, e
:
1006 print "new_tenant_vminstance Exception: ", e
.args
1007 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
1009 #print vim_response.status_code
1010 if vim_response
.status_code
!= 200:
1011 print vim_response
.text
1012 jsonerror
= self
._format
_jsonerror
(vim_response
)
1013 text
= 'Error in VIM "%s": not possible to add new vm instance. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
1015 return -vim_response
.status_code
,text
1017 print vim_response
.json()
1018 print json
.dumps(vim_response
.json(), indent
=4)
1019 res
,http_content
= self
._format
_in
(vim_response
, new_vminstance_response_schema
)
1022 return -vimconn
.HTTP_Bad_Request
,http_content
1023 #r = self._remove_extra_items(http_content, new_vminstance_response_schema)
1024 #if r is not None: print "Warning: remove extra items ", r
1025 vminstance_id
= http_content
['server']['id']
1026 print json
.dumps(http_content
, indent
=4)
1027 #connect data plane interfaces to network
1028 for net
in net_list
:
1029 if net
["type"]=="virtual":
1030 if not net
.get("net_id"):
1032 for iface
in http_content
['server']['networks']:
1034 if net
["name"]==iface
["name"]:
1035 net
["vim_id"] = iface
['iface_id']
1037 elif "net_id" in net
:
1038 if net
["net_id"]==iface
["net_id"]:
1039 net
["vim_id"] = iface
['iface_id']
1042 for numa
in http_content
['server'].get('extended',{}).get('numas',() ):
1043 for iface
in numa
.get('interfaces',() ):
1044 if net
['name'] == iface
['name']:
1045 net
['vim_id'] = iface
['iface_id']
1046 #Code bellow is not needed, current openvim connect dataplane interfaces
1047 #if net.get("net_id"):
1048 ##connect dataplane interface
1049 # result, port_id = self.connect_port_network(iface['iface_id'], net["net_id"])
1051 # error_text = "Error attaching port %s to network %s: %s." % (iface['iface_id'], net["net_id"], port_id)
1052 # print "new_tenant_vminstance: " + error_text
1053 # self.delete_tenant_vminstance(vminstance_id)
1054 # return result, error_text
1057 print "VM instance id: ",vminstance_id
1058 return vim_response
.status_code
,vminstance_id
1060 def get_tenant_vminstance(self
,vm_id
):
1061 '''Returns the VM instance information from VIM'''
1063 self
._get
_my
_tenant
()
1064 except Exception as e
:
1065 return -vimconn
.HTTP_Not_Found
, str(e
)
1066 print "VIMConnector: Getting tenant VM instance information from VIM"
1068 url
= self
.url
+'/'+self
.tenant
+'/servers/'+vm_id
1071 vim_response
= requests
.get(url
, headers
= self
.headers_req
)
1072 except requests
.exceptions
.RequestException
, e
:
1073 print "get_tenant_vminstance Exception: ", e
.args
1074 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
1076 #print vim_response.status_code
1077 if vim_response
.status_code
== 200:
1078 print vim_response
.json()
1079 print json
.dumps(vim_response
.json(), indent
=4)
1080 res
,http_content
= self
._format
_in
(vim_response
, new_vminstance_response_schema
)
1083 print json
.dumps(http_content
, indent
=4)
1084 return vim_response
.status_code
,http_content
1085 else: return -vimconn
.HTTP_Bad_Request
,http_content
1087 print vim_response
.text
1088 jsonerror
= self
._format
_jsonerror
(vim_response
)
1089 text
= 'Error in VIM "%s": not possible to get vm instance. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
1091 return -vim_response
.status_code
,text
1093 def delete_tenant_vminstance(self
, vm_id
):
1094 '''Removes a VM instance from VIM'''
1095 '''Returns the instance identifier'''
1097 self
._get
_my
_tenant
()
1098 except Exception as e
:
1099 return -vimconn
.HTTP_Not_Found
, str(e
)
1100 print "VIMConnector: Delete a VM instance from VIM " + vm_id
1103 vim_response
= requests
.delete(self
.url
+'/'+self
.tenant
+'/servers/'+vm_id
, headers
= self
.headers_req
)
1104 except requests
.exceptions
.RequestException
, e
:
1105 print "delete_tenant_vminstance Exception: ", e
.args
1106 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
1108 #print vim_response.status_code
1109 if vim_response
.status_code
== 200:
1110 print json
.dumps(vim_response
.json(), indent
=4)
1111 return vim_response
.status_code
, vm_id
1113 #print vim_response.text
1114 jsonerror
= self
._format
_jsonerror
(vim_response
)
1115 text
= 'Error in VIM "%s": not possible to delete vm instance. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
1117 return -vim_response
.status_code
, text
1119 def refresh_tenant_vms_and_nets(self
, vmDict
, netDict
):
1120 '''Refreshes the status of the dictionaries of VM instances and nets passed as arguments. It modifies the dictionaries'''
1122 - result: 0 if all elements could be refreshed (even if its status didn't change)
1123 n>0, the number of elements that couldn't be refreshed,
1124 <0 if error (foreseen)
1125 - error_msg: text with reference to possible errors
1128 self
._get
_my
_tenant
()
1129 except Exception as e
:
1130 return -vimconn
.HTTP_Not_Found
, str(e
)
1132 #nets_refreshed = []
1133 vms_unrefreshed
= []
1134 nets_unrefreshed
= []
1135 for vm_id
in vmDict
:
1136 vmDict
[vm_id
]={'error_msg':None, 'vim_info':None}
1137 print "VIMConnector refresh_tenant_vms and nets: Getting tenant VM instance information from VIM"
1139 url
= self
.url
+'/'+self
.tenant
+'/servers/'+ vm_id
1142 vim_response
= requests
.get(url
, headers
= self
.headers_req
)
1143 except requests
.exceptions
.RequestException
, e
:
1144 print "VIMConnector refresh_tenant_elements. Exception: ", e
.args
1145 vmDict
[vm_id
]['status'] = "VIM_ERROR"
1146 vmDict
[vm_id
]['error_msg'] = str(e
)
1147 vms_unrefreshed
.append(vm_id
)
1150 #print vim_response.status_code
1151 if vim_response
.status_code
== 200:
1152 #print vim_response.json()
1153 #print json.dumps(vim_response.json(), indent=4)
1154 management_ip
= False
1155 res
,http_content
= self
._format
_in
(vim_response
, new_vminstance_response_schema
)
1158 vmDict
[vm_id
]['status'] = vmStatus2manoFormat
[ http_content
['server']['status'] ]
1159 if http_content
['server'].get('last_error'):
1160 vmDict
[vm_id
]['error_msg'] = http_content
['server']['last_error']
1161 vmDict
[vm_id
]["vim_info"] = yaml
.safe_dump(http_content
['server'])
1162 vmDict
[vm_id
]["interfaces"]=[]
1163 #get interfaces info
1164 url2
= self
.url
+'/ports?device_id='+ vm_id
1166 vim_response2
= requests
.get(url2
, headers
= self
.headers_req
)
1167 if vim_response
.status_code
== 200:
1168 client_data
= vim_response2
.json()
1169 for port
in client_data
.get("ports"):
1170 print "VACAport", port
1172 interface
['vim_info'] = yaml
.safe_dump(port
)
1173 interface
["mac_address"] = port
.get("mac_address")
1174 interface
["vim_net_id"] = port
["network_id"]
1175 interface
["vim_interface_id"] = port
["id"]
1176 interface
["ip_address"] = port
.get("ip_address")
1177 if interface
["ip_address"]:
1178 management_ip
= True
1179 if interface
["ip_address"] == "0.0.0.0":
1180 interface
["ip_address"] = None
1181 vmDict
[vm_id
]["interfaces"].append(interface
)
1183 except Exception as e
:
1184 print "VIMConnector refresh_tenant_elements. Port get %s: %s", (type(e
).__name
__, (str(e
) if len(e
.args
)==0 else str(e
.args
[0])))
1186 if vmDict
[vm_id
]['status'] == "ACTIVE" and not management_ip
:
1187 vmDict
[vm_id
]['status'] = "ACTIVE:NoMgmtIP"
1189 except Exception as e
:
1190 vmDict
[vm_id
]['status'] = "VIM_ERROR"
1191 vmDict
[vm_id
]['error_msg'] = str(e
)
1192 vms_unrefreshed
.append(vm_id
)
1194 vmDict
[vm_id
]['status'] = "VIM_ERROR"
1195 vmDict
[vm_id
]['error_msg'] = str(http_content
)
1196 vms_unrefreshed
.append(vm_id
)
1198 #print vim_response.text
1199 jsonerror
= self
._format
_jsonerror
(vim_response
)
1200 print 'VIMConnector refresh_tenant_vms_and_nets. Error in VIM "%s": not possible to get VM instance. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
1201 if vim_response
.status_code
== 404: # HTTP_Not_Found
1202 vmDict
[vm_id
]['status'] = "DELETED"
1204 vmDict
[vm_id
]['status'] = "VIM_ERROR"
1205 vmDict
[vm_id
]['error_msg'] = jsonerror
1206 vms_unrefreshed
.append(vm_id
)
1208 #print "VMs refreshed: %s" % str(vms_refreshed)
1209 for net_id
in netDict
:
1210 netDict
[net_id
] = {'error_msg':None, 'vim_info':None}
1211 print "VIMConnector refresh_tenant_vms_and_nets: Getting tenant network from VIM (tenant: " + str(self
.tenant
) + "): "
1212 r
,c
= self
.get_tenant_network(net_id
)
1214 print "VIMconnector refresh_tenant_network. Error getting net_id '%s' status: %s" % (net_id
, c
)
1215 if r
==-vimconn
.HTTP_Not_Found
:
1216 netDict
[net_id
]['status'] = "DELETED" #TODO check exit status
1218 netDict
[net_id
]['status'] = "VIM_ERROR"
1219 netDict
[net_id
]['error_msg'] = c
1220 nets_unrefreshed
.append(net_id
)
1223 net_status
= netStatus2manoFormat
[ c
['status'] ]
1224 if net_status
== "ACTIVE" and not c
['admin_state_up']:
1226 netDict
[net_id
]['status'] = net_status
1227 if c
.get('last_error'):
1228 netDict
[net_id
]['error_msg'] = c
['last_error']
1229 netDict
[net_id
]["vim_info"] = yaml
.safe_dump(c
)
1230 except Exception as e
:
1231 netDict
[net_id
]['status'] = "VIM_ERROR"
1232 netDict
[net_id
]['error_msg'] = str(e
)
1233 nets_unrefreshed
.append(net_id
)
1235 #print "Nets refreshed: %s" % str(nets_refreshed)
1238 if len(vms_unrefreshed
)+len(nets_unrefreshed
)>0:
1239 error_msg
+= "VMs unrefreshed: " + str(vms_unrefreshed
) + "; nets unrefreshed: " + str(nets_unrefreshed
)
1242 #return len(vms_unrefreshed)+len(nets_unrefreshed), error_msg, vms_refreshed, nets_refreshed
1243 return len(vms_unrefreshed
)+len(nets_unrefreshed
), error_msg
1245 def action_tenant_vminstance(self
, vm_id
, action_dict
):
1246 '''Send and action over a VM instance from VIM'''
1247 '''Returns the status'''
1249 self
._get
_my
_tenant
()
1250 except Exception as e
:
1251 return -vimconn
.HTTP_Not_Found
, str(e
)
1252 print "VIMConnector: Action over VM instance from VIM " + vm_id
1255 if "console" in action_dict
:
1256 return -vimconn
.HTTP_Service_Unavailable
, "getting console is not available at openvim"
1258 vim_response
= requests
.post(self
.url
+'/'+self
.tenant
+'/servers/'+vm_id
+"/action", headers
= self
.headers_req
, data
=json
.dumps(action_dict
) )
1259 except requests
.exceptions
.RequestException
, e
:
1260 print "action_tenant_vminstance Exception: ", e
.args
1261 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
1263 #print vim_response.status_code
1264 if vim_response
.status_code
== 200:
1265 #print "vimconnector.action_tenant_vminstance():", json.dumps(vim_response.json(), indent=4)
1266 return vim_response
.status_code
, vm_id
1268 #print vim_response.text
1269 jsonerror
= self
._format
_jsonerror
(vim_response
)
1270 text
= 'Error in VIM "%s": action over vm instance. HTTP Response: %d. Error: %s' % (self
.url
, vim_response
.status_code
, jsonerror
)
1272 return vim_response
.status_code
, text
1274 def host_vim2gui(self
, host
, server_dict
):
1275 '''Transform host dictionary from VIM format to GUI format,
1276 and append to the server_dict
1278 if type(server_dict
) is not dict:
1279 print 'vimconnector.host_vim2gui() ERROR, param server_dict must be a dictionary'
1283 for numa
in host
['host']['numas']:
1287 RAD_item
['memory']={'size': str(numa
['memory'])+'GB', 'eligible': str(numa
['hugepages'])+'GB'}
1288 occupation_item
['memory']= str(numa
['hugepages_consumed'])+'GB'
1291 RAD_item
['cpus']['cores'] = []
1292 RAD_item
['cpus']['eligible_cores'] = []
1293 occupation_item
['cores']=[]
1294 for _
in range(0, len(numa
['cores']) / 2):
1295 RAD_item
['cpus']['cores'].append( [] )
1296 for core
in numa
['cores']:
1297 RAD_item
['cpus']['cores'][core
['core_id']].append(core
['thread_id'])
1298 if not 'status' in core
: RAD_item
['cpus']['eligible_cores'].append(core
['thread_id'])
1299 if 'instance_id' in core
: occupation_item
['cores'].append(core
['thread_id'])
1301 RAD_item
['ports']={}
1302 occupation_item
['ports']={}
1303 for iface
in numa
['interfaces']:
1304 RAD_item
['ports'][ iface
['pci'] ] = 'speed:'+str(iface
['Mbps'])+'M'
1305 occupation_item
['ports'][ iface
['pci'] ] = { 'occupied': str(100*iface
['Mbps_consumed'] / iface
['Mbps']) + "%" }
1307 RAD
[ numa
['numa_socket'] ] = RAD_item
1308 occupation
[ numa
['numa_socket'] ] = occupation_item
1309 server_dict
[ host
['host']['name'] ] = {'RAD':RAD
, 'occupation':occupation
}
1311 def get_hosts_info(self
):
1312 '''Get the information of deployed hosts
1313 Returns the hosts content'''
1315 url
=self
.url
+'/hosts'
1317 vim_response
= requests
.get(url
)
1318 except requests
.exceptions
.RequestException
, e
:
1319 print "get_hosts_info Exception: ", e
.args
1320 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
1321 print "vim get", url
, "response:", vim_response
.status_code
, vim_response
.json()
1322 #print vim_response.status_code
1323 #print json.dumps(vim_response.json(), indent=4)
1324 if vim_response
.status_code
!= 200:
1326 print 'vimconnector.get_hosts_info error getting host list %d %s' %(vim_response
.status_code
, vim_response
.json())
1327 return -vim_response
.status_code
, "Error getting host list"
1329 res
,hosts
= self
._format
_in
(vim_response
, get_hosts_response_schema
)
1332 print "vimconnector.get_hosts_info error parsing GET HOSTS vim response", hosts
1333 return vimconn
.HTTP_Internal_Server_Error
, hosts
1334 #obtain hosts details
1336 for host
in hosts
['hosts']:
1337 url
=self
.url
+'/hosts/'+host
['id']
1339 vim_response
= requests
.get(url
)
1340 except requests
.exceptions
.RequestException
, e
:
1341 print "get_hosts_info Exception: ", e
.args
1342 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
1343 print "vim get", url
, "response:", vim_response
.status_code
, vim_response
.json()
1344 if vim_response
.status_code
!= 200:
1345 print 'vimconnector.get_hosts_info error getting detailed host %d %s' %(vim_response
.status_code
, vim_response
.json())
1347 res
,host_detail
= self
._format
_in
(vim_response
, get_host_detail_response_schema
)
1349 print "vimconnector.get_hosts_info error parsing GET HOSTS/%s vim response" % host
['id'], host_detail
1351 #print 'host id '+host['id'], json.dumps(host_detail, indent=4)
1352 self
.host_vim2gui(host_detail
, hosts_dict
)
1353 return 200, hosts_dict
1355 def get_hosts(self
, vim_tenant
):
1356 '''Get the hosts and deployed instances
1357 Returns the hosts content'''
1359 url
=self
.url
+'/hosts'
1361 vim_response
= requests
.get(url
)
1362 except requests
.exceptions
.RequestException
, e
:
1363 print "get_hosts Exception: ", e
.args
1364 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
1365 print "vim get", url
, "response:", vim_response
.status_code
, vim_response
.json()
1366 #print vim_response.status_code
1367 #print json.dumps(vim_response.json(), indent=4)
1368 if vim_response
.status_code
!= 200:
1370 print 'vimconnector.get_hosts error getting host list %d %s' %(vim_response
.status_code
, vim_response
.json())
1371 return -vim_response
.status_code
, "Error getting host list"
1373 res
,hosts
= self
._format
_in
(vim_response
, get_hosts_response_schema
)
1376 print "vimconnector.get_host error parsing GET HOSTS vim response", hosts
1377 return vimconn
.HTTP_Internal_Server_Error
, hosts
1378 #obtain instances from hosts
1379 for host
in hosts
['hosts']:
1380 url
=self
.url
+'/' + vim_tenant
+ '/servers?hostId='+host
['id']
1382 vim_response
= requests
.get(url
)
1383 except requests
.exceptions
.RequestException
, e
:
1384 print "get_hosts Exception: ", e
.args
1385 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
1386 print "vim get", url
, "response:", vim_response
.status_code
, vim_response
.json()
1387 if vim_response
.status_code
!= 200:
1388 print 'vimconnector.get_hosts error getting instances at host %d %s' %(vim_response
.status_code
, vim_response
.json())
1390 res
,servers
= self
._format
_in
(vim_response
, get_server_response_schema
)
1392 print "vimconnector.get_host error parsing GET SERVERS/%s vim response" % host
['id'], servers
1394 #print 'host id '+host['id'], json.dumps(host_detail, indent=4)
1395 host
['instances'] = servers
['servers']
1396 return 200, hosts
['hosts']
1398 def get_processor_rankings(self
):
1399 '''Get the processor rankings in the VIM database'''
1400 url
=self
.url
+'/processor_ranking'
1402 vim_response
= requests
.get(url
)
1403 except requests
.exceptions
.RequestException
, e
:
1404 print "get_processor_rankings Exception: ", e
.args
1405 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
1406 print "vim get", url
, "response:", vim_response
.status_code
, vim_response
.json()
1407 #print vim_response.status_code
1408 #print json.dumps(vim_response.json(), indent=4)
1409 if vim_response
.status_code
!= 200:
1411 print 'vimconnector.get_processor_rankings error getting processor rankings %d %s' %(vim_response
.status_code
, vim_response
.json())
1412 return -vim_response
.status_code
, "Error getting processor rankings"
1414 res
,rankings
= self
._format
_in
(vim_response
, get_processor_rankings_response_schema
)
1415 return res
, rankings
['rankings']
1417 def get_image_id_from_path(self
, path
):
1418 '''Get the image id from image path in the VIM database'''
1420 0,"Image not found" if there are no images with that path
1421 1,image-id if there is one image with that path
1422 <0,message if there was an error (Image not found, error contacting VIM, more than 1 image with that path, etc.)
1425 self
._get
_my
_tenant
()
1426 except Exception as e
:
1427 return -vimconn
.HTTP_Not_Found
, str(e
)
1428 url
=self
.url
+ '/' + self
.tenant
+ '/images?path='+path
1430 vim_response
= requests
.get(url
)
1431 except requests
.exceptions
.RequestException
, e
:
1432 print "get_image_id_from_path url='%s'Exception: '%s'" % (url
, str(e
.args
))
1433 return -vimconn
.HTTP_Not_Found
, str(e
.args
[0])
1434 print "vim get_image_id_from_path", url
, "response:", vim_response
.status_code
, vim_response
.json()
1435 #print vim_response.status_code
1436 #print json.dumps(vim_response.json(), indent=4)
1437 if vim_response
.status_code
!= 200:
1439 print 'vimconnector.get_image_id_from_path error getting image id from path. Error code: %d Description: %s' %(vim_response
.status_code
, vim_response
.json())
1440 return -vim_response
.status_code
, "Error getting image id from path"
1442 res
,image
= self
._format
_in
(vim_response
, get_images_response_schema
)
1444 print "vimconnector.get_image_id_from_path error"
1445 return -vimconn
.HTTP_Bad_Request
, image
1446 if len(image
['images'])==0:
1447 return 0,"Image not found"
1448 elif len(image
['images'])>1:
1449 print "vimconnector.get_image_id_from_path error. More than one images with the path %s." %(path)
1450 return -vimconn
.HTTP_Internal_Server_Error
,"More than one images with that path"
1451 return 1, image
['images'][0]['id']