Fix cloud-init insertion with v3 IM
[osm/RO.git] / osm_ro / vimconn.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
24 """
25 vimconn implement an Abstract class for the vim connector plugins
26 with the definition of the method to be implemented.
27 """
28 __author__="Alfonso Tierno, Igor D.C."
29 __date__ ="$14-aug-2017 23:59:59$"
30
31 import logging
32 import paramiko
33 import socket
34 import StringIO
35 import yaml
36 import sys
37 from email.mime.multipart import MIMEMultipart
38 from email.mime.text import MIMEText
39
40 #Error variables
41 HTTP_Bad_Request = 400
42 HTTP_Unauthorized = 401
43 HTTP_Not_Found = 404
44 HTTP_Method_Not_Allowed = 405
45 HTTP_Request_Timeout = 408
46 HTTP_Conflict = 409
47 HTTP_Not_Implemented = 501
48 HTTP_Service_Unavailable = 503
49 HTTP_Internal_Server_Error = 500
50
51 class vimconnException(Exception):
52 """Common and base class Exception for all vimconnector exceptions"""
53 def __init__(self, message, http_code=HTTP_Bad_Request):
54 Exception.__init__(self, message)
55 self.http_code = http_code
56
57 class vimconnConnectionException(vimconnException):
58 """Connectivity error with the VIM"""
59 def __init__(self, message, http_code=HTTP_Service_Unavailable):
60 vimconnException.__init__(self, message, http_code)
61
62 class vimconnUnexpectedResponse(vimconnException):
63 """Get an wrong response from VIM"""
64 def __init__(self, message, http_code=HTTP_Service_Unavailable):
65 vimconnException.__init__(self, message, http_code)
66
67 class vimconnAuthException(vimconnException):
68 """Invalid credentials or authorization to perform this action over the VIM"""
69 def __init__(self, message, http_code=HTTP_Unauthorized):
70 vimconnException.__init__(self, message, http_code)
71
72 class vimconnNotFoundException(vimconnException):
73 """The item is not found at VIM"""
74 def __init__(self, message, http_code=HTTP_Not_Found):
75 vimconnException.__init__(self, message, http_code)
76
77 class vimconnConflictException(vimconnException):
78 """There is a conflict, e.g. more item found than one"""
79 def __init__(self, message, http_code=HTTP_Conflict):
80 vimconnException.__init__(self, message, http_code)
81
82 class vimconnNotSupportedException(vimconnException):
83 """The request is not supported by connector"""
84 def __init__(self, message, http_code=HTTP_Service_Unavailable):
85 vimconnException.__init__(self, message, http_code)
86
87 class vimconnNotImplemented(vimconnException):
88 """The method is not implemented by the connected"""
89 def __init__(self, message, http_code=HTTP_Not_Implemented):
90 vimconnException.__init__(self, message, http_code)
91
92
93 class vimconnector():
94 """Abstract base class for all the VIM connector plugins
95 These plugins must implement a vimconnector class derived from this
96 and all these privated methods
97 """
98 def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None, log_level=None,
99 config={}, persitent_info={}):
100 """Constructor of VIM
101 Params:
102 'uuid': id asigned to this VIM
103 'name': name assigned to this VIM, can be used for logging
104 'tenant_id', 'tenant_name': (only one of them is mandatory) VIM tenant to be used
105 'url_admin': (optional), url used for administrative tasks
106 'user', 'passwd': credentials of the VIM user
107 'log_level': provider if it should use a different log_level than the general one
108 'config': dictionary with extra VIM information. This contains a consolidate version of general VIM config
109 at creation and particular VIM config at teh attachment
110 'persistent_info': dict where the class can store information that will be available among class
111 destroy/creation cycles. This info is unique per VIM/credential. At first call it will contain an
112 empty dict. Useful to store login/tokens information for speed up communication
113
114 Returns: Raise an exception is some needed parameter is missing, but it must not do any connectivity
115 check against the VIM
116 """
117 self.id = uuid
118 self.name = name
119 self.url = url
120 self.url_admin = url_admin
121 self.tenant_id = tenant_id
122 self.tenant_name = tenant_name
123 self.user = user
124 self.passwd = passwd
125 self.config = config
126 self.availability_zone = None
127 self.logger = logging.getLogger('openmano.vim')
128 if log_level:
129 self.logger.setLevel( getattr(logging, log_level) )
130 if not self.url_admin: #try to use normal url
131 self.url_admin = self.url
132
133 def __getitem__(self,index):
134 if index=='tenant_id':
135 return self.tenant_id
136 if index=='tenant_name':
137 return self.tenant_name
138 elif index=='id':
139 return self.id
140 elif index=='name':
141 return self.name
142 elif index=='user':
143 return self.user
144 elif index=='passwd':
145 return self.passwd
146 elif index=='url':
147 return self.url
148 elif index=='url_admin':
149 return self.url_admin
150 elif index=="config":
151 return self.config
152 else:
153 raise KeyError("Invalid key '%s'" %str(index))
154
155 def __setitem__(self,index, value):
156 if index=='tenant_id':
157 self.tenant_id = value
158 if index=='tenant_name':
159 self.tenant_name = value
160 elif index=='id':
161 self.id = value
162 elif index=='name':
163 self.name = value
164 elif index=='user':
165 self.user = value
166 elif index=='passwd':
167 self.passwd = value
168 elif index=='url':
169 self.url = value
170 elif index=='url_admin':
171 self.url_admin = value
172 else:
173 raise KeyError("Invalid key '%s'" %str(index))
174
175 @staticmethod
176 def _create_mimemultipart(content_list):
177 """Creates a MIMEmultipart text combining the content_list
178 :param content_list: list of text scripts to be combined
179 :return: str of the created MIMEmultipart. If the list is empty returns None, if the list contains only one
180 element MIMEmultipart is not created and this content is returned
181 """
182 if not content_list:
183 return None
184 elif len(content_list) == 1:
185 return content_list[0]
186 combined_message = MIMEMultipart()
187 for content in content_list:
188 if content.startswith('#include'):
189 format = 'text/x-include-url'
190 elif content.startswith('#include-once'):
191 format = 'text/x-include-once-url'
192 elif content.startswith('#!'):
193 format = 'text/x-shellscript'
194 elif content.startswith('#cloud-config'):
195 format = 'text/cloud-config'
196 elif content.startswith('#cloud-config-archive'):
197 format = 'text/cloud-config-archive'
198 elif content.startswith('#upstart-job'):
199 format = 'text/upstart-job'
200 elif content.startswith('#part-handler'):
201 format = 'text/part-handler'
202 elif content.startswith('#cloud-boothook'):
203 format = 'text/cloud-boothook'
204 else: # by default
205 format = 'text/x-shellscript'
206 sub_message = MIMEText(content, format, sys.getdefaultencoding())
207 combined_message.attach(sub_message)
208 return combined_message.as_string()
209
210 def _create_user_data(self, cloud_config):
211 """
212 Creates a script user database on cloud_config info
213 :param cloud_config: dictionary with
214 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
215 'users': (optional) list of users to be inserted, each item is a dict with:
216 'name': (mandatory) user name,
217 'key-pairs': (optional) list of strings with the public key to be inserted to the user
218 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
219 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
220 'config-files': (optional). List of files to be transferred. Each item is a dict with:
221 'dest': (mandatory) string with the destination absolute path
222 'encoding': (optional, by default text). Can be one of:
223 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
224 'content' (mandatory): string with the content of the file
225 'permissions': (optional) string with file permissions, typically octal notation '0644'
226 'owner': (optional) file owner, string with the format 'owner:group'
227 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
228 :return: config_drive, userdata. The first is a boolean or None, the second a string or None
229 """
230 config_drive = None
231 userdata = None
232 userdata_list = []
233 if isinstance(cloud_config, dict):
234 if cloud_config.get("user-data"):
235 if isinstance(cloud_config["user-data"], str):
236 userdata_list.append(cloud_config["user-data"])
237 else:
238 for u in cloud_config["user-data"]:
239 userdata_list.append(u)
240 if cloud_config.get("boot-data-drive") != None:
241 config_drive = cloud_config["boot-data-drive"]
242 if cloud_config.get("config-files") or cloud_config.get("users") or cloud_config.get("key-pairs"):
243 userdata_dict = {}
244 # default user
245 if cloud_config.get("key-pairs"):
246 userdata_dict["ssh-authorized-keys"] = cloud_config["key-pairs"]
247 userdata_dict["users"] = [{"default": None, "ssh-authorized-keys": cloud_config["key-pairs"]}]
248 if cloud_config.get("users"):
249 if "users" not in userdata_dict:
250 userdata_dict["users"] = ["default"]
251 for user in cloud_config["users"]:
252 user_info = {
253 "name": user["name"],
254 "sudo": "ALL = (ALL)NOPASSWD:ALL"
255 }
256 if "user-info" in user:
257 user_info["gecos"] = user["user-info"]
258 if user.get("key-pairs"):
259 user_info["ssh-authorized-keys"] = user["key-pairs"]
260 userdata_dict["users"].append(user_info)
261
262 if cloud_config.get("config-files"):
263 userdata_dict["write_files"] = []
264 for file in cloud_config["config-files"]:
265 file_info = {
266 "path": file["dest"],
267 "content": file["content"]
268 }
269 if file.get("encoding"):
270 file_info["encoding"] = file["encoding"]
271 if file.get("permissions"):
272 file_info["permissions"] = file["permissions"]
273 if file.get("owner"):
274 file_info["owner"] = file["owner"]
275 userdata_dict["write_files"].append(file_info)
276 userdata_list.append("#cloud-config\n" + yaml.safe_dump(userdata_dict, indent=4,
277 default_flow_style=False))
278 userdata = self._create_mimemultipart(userdata_list)
279 self.logger.debug("userdata: %s", userdata)
280 elif isinstance(cloud_config, str):
281 userdata = cloud_config
282 return config_drive, userdata
283
284 def check_vim_connectivity(self):
285 """Checks VIM can be reached and user credentials are ok.
286 Returns None if success or raised vimconnConnectionException, vimconnAuthException, ...
287 """
288 raise vimconnNotImplemented( "Should have implemented this" )
289
290 def new_tenant(self,tenant_name,tenant_description):
291 """Adds a new tenant to VIM with this name and description, this is done using admin_url if provided
292 "tenant_name": string max lenght 64
293 "tenant_description": string max length 256
294 returns the tenant identifier or raise exception
295 """
296 raise vimconnNotImplemented( "Should have implemented this" )
297
298 def delete_tenant(self,tenant_id,):
299 """Delete a tenant from VIM
300 tenant_id: returned VIM tenant_id on "new_tenant"
301 Returns None on success. Raises and exception of failure. If tenant is not found raises vimconnNotFoundException
302 """
303 raise vimconnNotImplemented( "Should have implemented this" )
304
305 def get_tenant_list(self, filter_dict={}):
306 """Obtain tenants of VIM
307 filter_dict dictionary that can contain the following keys:
308 name: filter by tenant name
309 id: filter by tenant uuid/id
310 <other VIM specific>
311 Returns the tenant list of dictionaries, and empty list if no tenant match all the filers:
312 [{'name':'<name>, 'id':'<id>, ...}, ...]
313 """
314 raise vimconnNotImplemented( "Should have implemented this" )
315
316 def new_network(self, net_name, net_type, ip_profile=None, shared=False, vlan=None):
317 """Adds a tenant network to VIM
318 Params:
319 'net_name': name of the network
320 'net_type': one of:
321 'bridge': overlay isolated network
322 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
323 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
324 'ip_profile': is a dict containing the IP parameters of the network (Currently only IPv4 is implemented)
325 'ip-version': can be one of ["IPv4","IPv6"]
326 'subnet-address': ip_prefix_schema, that is X.X.X.X/Y
327 'gateway-address': (Optional) ip_schema, that is X.X.X.X
328 'dns-address': (Optional) ip_schema,
329 'dhcp': (Optional) dict containing
330 'enabled': {"type": "boolean"},
331 'start-address': ip_schema, first IP to grant
332 'count': number of IPs to grant.
333 'shared': if this network can be seen/use by other tenants/organization
334 'vlan': in case of a data or ptp net_type, the intended vlan tag to be used for the network
335 Returns the network identifier on success or raises and exception on failure
336 """
337 raise vimconnNotImplemented( "Should have implemented this" )
338
339 def get_network_list(self, filter_dict={}):
340 """Obtain tenant networks of VIM
341 Params:
342 'filter_dict' (optional) contains entries to return only networks that matches ALL entries:
343 name: string => returns only networks with this name
344 id: string => returns networks with this VIM id, this imply returns one network at most
345 shared: boolean >= returns only networks that are (or are not) shared
346 tenant_id: sting => returns only networks that belong to this tenant/project
347 ,#(not used yet) admin_state_up: boolean => returns only networks that are (or are not) in admin state active
348 #(not used yet) status: 'ACTIVE','ERROR',... => filter networks that are on this status
349 Returns the network list of dictionaries. each dictionary contains:
350 'id': (mandatory) VIM network id
351 'name': (mandatory) VIM network name
352 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
353 'network_type': (optional) can be 'vxlan', 'vlan' or 'flat'
354 'segmentation_id': (optional) in case network_type is vlan or vxlan this field contains the segmentation id
355 'error_msg': (optional) text that explains the ERROR status
356 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
357 List can be empty if no network map the filter_dict. Raise an exception only upon VIM connectivity,
358 authorization, or some other unspecific error
359 """
360 raise vimconnNotImplemented( "Should have implemented this" )
361
362 def get_network(self, net_id):
363 """Obtain network details from the 'net_id' VIM network
364 Return a dict that contains:
365 'id': (mandatory) VIM network id, that is, net_id
366 'name': (mandatory) VIM network name
367 'status': (mandatory) can be 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
368 'error_msg': (optional) text that explains the ERROR status
369 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
370 Raises an exception upon error or when network is not found
371 """
372 raise vimconnNotImplemented( "Should have implemented this" )
373
374 def delete_network(self, net_id):
375 """Deletes a tenant network from VIM
376 Returns the network identifier or raises an exception upon error or when network is not found
377 """
378 raise vimconnNotImplemented( "Should have implemented this" )
379
380 def refresh_nets_status(self, net_list):
381 """Get the status of the networks
382 Params:
383 'net_list': a list with the VIM network id to be get the status
384 Returns a dictionary with:
385 'net_id': #VIM id of this network
386 status: #Mandatory. Text with one of:
387 # DELETED (not found at vim)
388 # VIM_ERROR (Cannot connect to VIM, authentication problems, VIM response error, ...)
389 # OTHER (Vim reported other status not understood)
390 # ERROR (VIM indicates an ERROR status)
391 # ACTIVE, INACTIVE, DOWN (admin down),
392 # BUILD (on building process)
393 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
394 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
395 'net_id2': ...
396 """
397 raise vimconnNotImplemented( "Should have implemented this" )
398
399 def get_flavor(self, flavor_id):
400 """Obtain flavor details from the VIM
401 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific }
402 Raises an exception upon error or if not found
403 """
404 raise vimconnNotImplemented( "Should have implemented this" )
405
406 def get_flavor_id_from_data(self, flavor_dict):
407 """Obtain flavor id that match the flavor description
408 Params:
409 'flavor_dict': dictionary that contains:
410 'disk': main hard disk in GB
411 'ram': meomry in MB
412 'vcpus': number of virtual cpus
413 #TODO: complete parameters for EPA
414 Returns the flavor_id or raises a vimconnNotFoundException
415 """
416 raise vimconnNotImplemented( "Should have implemented this" )
417
418 def new_flavor(self, flavor_data):
419 """Adds a tenant flavor to VIM
420 flavor_data contains a dictionary with information, keys:
421 name: flavor name
422 ram: memory (cloud type) in MBytes
423 vpcus: cpus (cloud type)
424 extended: EPA parameters
425 - numas: #items requested in same NUMA
426 memory: number of 1G huge pages memory
427 paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual threads
428 interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
429 - name: interface name
430 dedicated: yes|no|yes:sriov; for PT, SRIOV or only one SRIOV for the physical NIC
431 bandwidth: X Gbps; requested guarantee bandwidth
432 vpci: requested virtual PCI address
433 disk: disk size
434 is_public:
435 #TODO to concrete
436 Returns the flavor identifier"""
437 raise vimconnNotImplemented( "Should have implemented this" )
438
439 def delete_flavor(self, flavor_id):
440 """Deletes a tenant flavor from VIM identify by its id
441 Returns the used id or raise an exception"""
442 raise vimconnNotImplemented( "Should have implemented this" )
443
444 def new_image(self, image_dict):
445 """ Adds a tenant image to VIM
446 Returns the image id or raises an exception if failed
447 """
448 raise vimconnNotImplemented( "Should have implemented this" )
449
450 def delete_image(self, image_id):
451 """Deletes a tenant image from VIM
452 Returns the image_id if image is deleted or raises an exception on error"""
453 raise vimconnNotImplemented( "Should have implemented this" )
454
455 def get_image_id_from_path(self, path):
456 """Get the image id from image path in the VIM database.
457 Returns the image_id or raises a vimconnNotFoundException
458 """
459 raise vimconnNotImplemented( "Should have implemented this" )
460
461 def get_image_list(self, filter_dict={}):
462 """Obtain tenant images from VIM
463 Filter_dict can be:
464 name: image name
465 id: image uuid
466 checksum: image checksum
467 location: image path
468 Returns the image list of dictionaries:
469 [{<the fields at Filter_dict plus some VIM specific>}, ...]
470 List can be empty
471 """
472 raise vimconnNotImplemented( "Should have implemented this" )
473
474 def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None,
475 availability_zone_index=None, availability_zone_list=None):
476 """Adds a VM instance to VIM
477 Params:
478 'start': (boolean) indicates if VM must start or created in pause mode.
479 'image_id','flavor_id': image and flavor VIM id to use for the VM
480 'net_list': list of interfaces, each one is a dictionary with:
481 'name': (optional) name for the interface.
482 'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual
483 'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM capabilities
484 'model': (optional and only have sense for type==virtual) interface model: virtio, e2000, ...
485 'mac_address': (optional) mac address to assign to this interface
486 #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not provided,
487 the VLAN tag to be used. In case net_id is provided, the internal network vlan is used for tagging VF
488 'type': (mandatory) can be one of:
489 'virtual', in this case always connected to a network of type 'net_type=bridge'
490 'PF' (passthrough): depending on VIM capabilities it can be connected to a data/ptp network ot it
491 can created unconnected
492 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
493 'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs
494 are allocated on the same physical NIC
495 'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS
496 'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing
497 or True, it must apply the default VIM behaviour
498 After execution the method will add the key:
499 'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this
500 interface. 'net_list' is modified
501 'cloud_config': (optional) dictionary with:
502 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
503 'users': (optional) list of users to be inserted, each item is a dict with:
504 'name': (mandatory) user name,
505 'key-pairs': (optional) list of strings with the public key to be inserted to the user
506 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
507 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
508 'config-files': (optional). List of files to be transferred. Each item is a dict with:
509 'dest': (mandatory) string with the destination absolute path
510 'encoding': (optional, by default text). Can be one of:
511 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
512 'content' (mandatory): string with the content of the file
513 'permissions': (optional) string with file permissions, typically octal notation '0644'
514 'owner': (optional) file owner, string with the format 'owner:group'
515 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
516 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
517 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
518 'size': (mandatory) string with the size of the disk in GB
519 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
520 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
521 availability_zone_index is None
522 Returns the instance identifier or raises an exception on error
523 """
524 raise vimconnNotImplemented( "Should have implemented this" )
525
526 def get_vminstance(self,vm_id):
527 """Returns the VM instance information from VIM"""
528 raise vimconnNotImplemented( "Should have implemented this" )
529
530 def delete_vminstance(self, vm_id):
531 """Removes a VM instance from VIM
532 Returns the instance identifier"""
533 raise vimconnNotImplemented( "Should have implemented this" )
534
535 def refresh_vms_status(self, vm_list):
536 """Get the status of the virtual machines and their interfaces/ports
537 Params: the list of VM identifiers
538 Returns a dictionary with:
539 vm_id: #VIM id of this Virtual Machine
540 status: #Mandatory. Text with one of:
541 # DELETED (not found at vim)
542 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
543 # OTHER (Vim reported other status not understood)
544 # ERROR (VIM indicates an ERROR status)
545 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
546 # BUILD (on building process), ERROR
547 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
548 #
549 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
550 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
551 interfaces: list with interface info. Each item a dictionary with:
552 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
553 mac_address: #Text format XX:XX:XX:XX:XX:XX
554 vim_net_id: #network id where this interface is connected, if provided at creation
555 vim_interface_id: #interface/port VIM id
556 ip_address: #null, or text with IPv4, IPv6 address
557 compute_node: #identification of compute node where PF,VF interface is allocated
558 pci: #PCI address of the NIC that hosts the PF,VF
559 vlan: #physical VLAN used for VF
560 """
561 raise vimconnNotImplemented( "Should have implemented this" )
562
563 def action_vminstance(self, vm_id, action_dict):
564 """Send and action over a VM instance from VIM
565 Returns the vm_id if the action was successfully sent to the VIM"""
566 raise vimconnNotImplemented( "Should have implemented this" )
567
568 def get_vminstance_console(self, vm_id, console_type="vnc"):
569 """
570 Get a console for the virtual machine
571 Params:
572 vm_id: uuid of the VM
573 console_type, can be:
574 "novnc" (by default), "xvpvnc" for VNC types,
575 "rdp-html5" for RDP types, "spice-html5" for SPICE types
576 Returns dict with the console parameters:
577 protocol: ssh, ftp, http, https, ...
578 server: usually ip address
579 port: the http, ssh, ... port
580 suffix: extra text, e.g. the http path and query string
581 """
582 raise vimconnNotImplemented( "Should have implemented this" )
583
584 def new_classification(self, name, ctype, definition):
585 """Creates a traffic classification in the VIM
586 Params:
587 'name': name of this classification
588 'ctype': type of this classification
589 'definition': definition of this classification (type-dependent free-form text)
590 Returns the VIM's classification ID on success or raises an exception on failure
591 """
592 raise vimconnNotImplemented( "SFC support not implemented" )
593
594 def get_classification(self, classification_id):
595 """Obtain classification details of the VIM's classification with ID='classification_id'
596 Return a dict that contains:
597 'id': VIM's classification ID (same as classification_id)
598 'name': VIM's classification name
599 'type': type of this classification
600 'definition': definition of the classification
601 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
602 'error_msg': (optional) text that explains the ERROR status
603 other VIM specific fields: (optional) whenever possible
604 Raises an exception upon error or when classification is not found
605 """
606 raise vimconnNotImplemented( "SFC support not implemented" )
607
608 def get_classification_list(self, filter_dict={}):
609 """Obtain classifications from the VIM
610 Params:
611 'filter_dict' (optional): contains the entries to filter the classifications on and only return those that match ALL:
612 id: string => returns classifications with this VIM's classification ID, which implies a return of one classification at most
613 name: string => returns only classifications with this name
614 type: string => returns classifications of this type
615 definition: string => returns classifications that have this definition
616 tenant_id: string => returns only classifications that belong to this tenant/project
617 Returns a list of classification dictionaries, each dictionary contains:
618 'id': (mandatory) VIM's classification ID
619 'name': (mandatory) VIM's classification name
620 'type': type of this classification
621 'definition': definition of the classification
622 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
623 List can be empty if no classification matches the filter_dict. Raise an exception only upon VIM connectivity,
624 authorization, or some other unspecific error
625 """
626 raise vimconnNotImplemented( "SFC support not implemented" )
627
628 def delete_classification(self, classification_id):
629 """Deletes a classification from the VIM
630 Returns the classification ID (classification_id) or raises an exception upon error or when classification is not found
631 """
632 raise vimconnNotImplemented( "SFC support not implemented" )
633
634 def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True):
635 """Creates a service function instance in the VIM
636 Params:
637 'name': name of this service function instance
638 'ingress_ports': set of ingress ports (VIM's port IDs)
639 'egress_ports': set of egress ports (VIM's port IDs)
640 'sfc_encap': boolean stating whether this specific instance supports IETF SFC Encapsulation
641 Returns the VIM's service function instance ID on success or raises an exception on failure
642 """
643 raise vimconnNotImplemented( "SFC support not implemented" )
644
645 def get_sfi(self, sfi_id):
646 """Obtain service function instance details of the VIM's service function instance with ID='sfi_id'
647 Return a dict that contains:
648 'id': VIM's sfi ID (same as sfi_id)
649 'name': VIM's sfi name
650 'ingress_ports': set of ingress ports (VIM's port IDs)
651 'egress_ports': set of egress ports (VIM's port IDs)
652 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
653 'error_msg': (optional) text that explains the ERROR status
654 other VIM specific fields: (optional) whenever possible
655 Raises an exception upon error or when service function instance is not found
656 """
657 raise vimconnNotImplemented( "SFC support not implemented" )
658
659 def get_sfi_list(self, filter_dict={}):
660 """Obtain service function instances from the VIM
661 Params:
662 'filter_dict' (optional): contains the entries to filter the sfis on and only return those that match ALL:
663 id: string => returns sfis with this VIM's sfi ID, which implies a return of one sfi at most
664 name: string => returns only service function instances with this name
665 tenant_id: string => returns only service function instances that belong to this tenant/project
666 Returns a list of service function instance dictionaries, each dictionary contains:
667 'id': (mandatory) VIM's sfi ID
668 'name': (mandatory) VIM's sfi name
669 'ingress_ports': set of ingress ports (VIM's port IDs)
670 'egress_ports': set of egress ports (VIM's port IDs)
671 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
672 List can be empty if no sfi matches the filter_dict. Raise an exception only upon VIM connectivity,
673 authorization, or some other unspecific error
674 """
675 raise vimconnNotImplemented( "SFC support not implemented" )
676
677 def delete_sfi(self, sfi_id):
678 """Deletes a service function instance from the VIM
679 Returns the service function instance ID (sfi_id) or raises an exception upon error or when sfi is not found
680 """
681 raise vimconnNotImplemented( "SFC support not implemented" )
682
683 def new_sf(self, name, sfis, sfc_encap=True):
684 """Creates (an abstract) service function in the VIM
685 Params:
686 'name': name of this service function
687 'sfis': set of service function instances of this (abstract) service function
688 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
689 Returns the VIM's service function ID on success or raises an exception on failure
690 """
691 raise vimconnNotImplemented( "SFC support not implemented" )
692
693 def get_sf(self, sf_id):
694 """Obtain service function details of the VIM's service function with ID='sf_id'
695 Return a dict that contains:
696 'id': VIM's sf ID (same as sf_id)
697 'name': VIM's sf name
698 'sfis': VIM's sf's set of VIM's service function instance IDs
699 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
700 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
701 'error_msg': (optional) text that explains the ERROR status
702 other VIM specific fields: (optional) whenever possible
703 Raises an exception upon error or when sf is not found
704 """
705
706 def get_sf_list(self, filter_dict={}):
707 """Obtain service functions from the VIM
708 Params:
709 'filter_dict' (optional): contains the entries to filter the sfs on and only return those that match ALL:
710 id: string => returns sfs with this VIM's sf ID, which implies a return of one sf at most
711 name: string => returns only service functions with this name
712 tenant_id: string => returns only service functions that belong to this tenant/project
713 Returns a list of service function dictionaries, each dictionary contains:
714 'id': (mandatory) VIM's sf ID
715 'name': (mandatory) VIM's sf name
716 'sfis': VIM's sf's set of VIM's service function instance IDs
717 'sfc_encap': boolean stating whether this service function supports IETF SFC Encapsulation
718 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
719 List can be empty if no sf matches the filter_dict. Raise an exception only upon VIM connectivity,
720 authorization, or some other unspecific error
721 """
722 raise vimconnNotImplemented( "SFC support not implemented" )
723
724 def delete_sf(self, sf_id):
725 """Deletes (an abstract) service function from the VIM
726 Returns the service function ID (sf_id) or raises an exception upon error or when sf is not found
727 """
728 raise vimconnNotImplemented( "SFC support not implemented" )
729
730
731 def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None):
732 """Creates a service function path
733 Params:
734 'name': name of this service function path
735 'classifications': set of traffic classifications that should be matched on to get into this sfp
736 'sfs': list of every service function that constitutes this path , from first to last
737 'sfc_encap': whether this is an SFC-Encapsulated chain (i.e using NSH), True by default
738 'spi': (optional) the Service Function Path identifier (SPI: Service Path Identifier) for this path
739 Returns the VIM's sfp ID on success or raises an exception on failure
740 """
741 raise vimconnNotImplemented( "SFC support not implemented" )
742
743 def get_sfp(self, sfp_id):
744 """Obtain service function path details of the VIM's sfp with ID='sfp_id'
745 Return a dict that contains:
746 'id': VIM's sfp ID (same as sfp_id)
747 'name': VIM's sfp name
748 'classifications': VIM's sfp's list of VIM's classification IDs
749 'sfs': VIM's sfp's list of VIM's service function IDs
750 'status': 'ACTIVE', 'INACTIVE', 'DOWN', 'BUILD', 'ERROR', 'VIM_ERROR', 'OTHER'
751 'error_msg': (optional) text that explains the ERROR status
752 other VIM specific fields: (optional) whenever possible
753 Raises an exception upon error or when sfp is not found
754 """
755 raise vimconnNotImplemented( "SFC support not implemented" )
756
757 def get_sfp_list(self, filter_dict={}):
758 """Obtain service function paths from VIM
759 Params:
760 'filter_dict' (optional): contains the entries to filter the sfps on, and only return those that match ALL:
761 id: string => returns sfps with this VIM's sfp ID , which implies a return of one sfp at most
762 name: string => returns only sfps with this name
763 tenant_id: string => returns only sfps that belong to this tenant/project
764 Returns a list of service function path dictionaries, each dictionary contains:
765 'id': (mandatory) VIM's sfp ID
766 'name': (mandatory) VIM's sfp name
767 'classifications': VIM's sfp's list of VIM's classification IDs
768 'sfs': VIM's sfp's list of VIM's service function IDs
769 other VIM specific fields: (optional) whenever possible using the same naming of filter_dict param
770 List can be empty if no sfp matches the filter_dict. Raise an exception only upon VIM connectivity,
771 authorization, or some other unspecific error
772 """
773 raise vimconnNotImplemented( "SFC support not implemented" )
774
775 def delete_sfp(self, sfp_id):
776 """Deletes a service function path from the VIM
777 Returns the sfp ID (sfp_id) or raises an exception upon error or when sf is not found
778 """
779 raise vimconnNotImplemented( "SFC support not implemented" )
780
781 def inject_user_key(self, ip_addr=None, user=None, key=None, ro_key=None, password=None):
782 """
783 Inject a ssh public key in a VM
784 Params:
785 ip_addr: ip address of the VM
786 user: username (default-user) to enter in the VM
787 key: public key to be injected in the VM
788 ro_key: private key of the RO, used to enter in the VM if the password is not provided
789 password: password of the user to enter in the VM
790 The function doesn't return a value:
791 """
792 if not ip_addr or not user:
793 raise vimconnNotSupportedException("All parameters should be different from 'None'")
794 elif not ro_key and not password:
795 raise vimconnNotSupportedException("All parameters should be different from 'None'")
796 else:
797 commands = {'mkdir -p ~/.ssh/', 'echo "%s" >> ~/.ssh/authorized_keys' % key,
798 'chmod 644 ~/.ssh/authorized_keys', 'chmod 700 ~/.ssh/'}
799 client = paramiko.SSHClient()
800 try:
801 if ro_key:
802 pkey = paramiko.RSAKey.from_private_key(StringIO.StringIO(ro_key))
803 else:
804 pkey = None
805 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
806 client.connect(ip_addr, username=user, password=password, pkey=pkey, timeout=10)
807 for command in commands:
808 (i, o, e) = client.exec_command(command, timeout=10)
809 returncode = o.channel.recv_exit_status()
810 output = o.read()
811 outerror = e.read()
812 if returncode != 0:
813 text = "run_command='{}' Error='{}'".format(command, outerror)
814 raise vimconnUnexpectedResponse("Cannot inject ssh key in VM: '{}'".format(text))
815 return
816 except (socket.error, paramiko.AuthenticationException, paramiko.SSHException) as message:
817 raise vimconnUnexpectedResponse(
818 "Cannot inject ssh key in VM: '{}' - {}".format(ip_addr, str(message)))
819 return
820
821
822 #NOT USED METHODS in current version
823
824 def host_vim2gui(self, host, server_dict):
825 """Transform host dictionary from VIM format to GUI format,
826 and append to the server_dict
827 """
828 raise vimconnNotImplemented( "Should have implemented this" )
829
830 def get_hosts_info(self):
831 """Get the information of deployed hosts
832 Returns the hosts content"""
833 raise vimconnNotImplemented( "Should have implemented this" )
834
835 def get_hosts(self, vim_tenant):
836 """Get the hosts and deployed instances
837 Returns the hosts content"""
838 raise vimconnNotImplemented( "Should have implemented this" )
839
840 def get_processor_rankings(self):
841 """Get the processor rankings in the VIM database"""
842 raise vimconnNotImplemented( "Should have implemented this" )
843
844 def new_host(self, host_data):
845 """Adds a new host to VIM"""
846 """Returns status code of the VIM response"""
847 raise vimconnNotImplemented( "Should have implemented this" )
848
849 def new_external_port(self, port_data):
850 """Adds a external port to VIM"""
851 """Returns the port identifier"""
852 raise vimconnNotImplemented( "Should have implemented this" )
853
854 def new_external_network(self,net_name,net_type):
855 """Adds a external network to VIM (shared)"""
856 """Returns the network identifier"""
857 raise vimconnNotImplemented( "Should have implemented this" )
858
859 def connect_port_network(self, port_id, network_id, admin=False):
860 """Connects a external port to a network"""
861 """Returns status code of the VIM response"""
862 raise vimconnNotImplemented( "Should have implemented this" )
863
864 def new_vminstancefromJSON(self, vm_data):
865 """Adds a VM instance to VIM"""
866 """Returns the instance identifier"""
867 raise vimconnNotImplemented( "Should have implemented this" )
868