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