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 NFVO engine, implementing all the methods for the creation, deletion and management of vnfs, scenarios and instances
27 __author__
="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
28 __date__
="$16-sep-2014 22:05:01$"
35 from db_base
import HTTP_Unauthorized
, HTTP_Bad_Request
, HTTP_Internal_Server_Error
, HTTP_Not_Found
,\
36 HTTP_Conflict
, HTTP_Method_Not_Allowed
37 import console_proxy_thread
as cli
41 from uuid
import uuid4
42 from db_base
import db_base_Exception
45 from threading
import Lock
47 from lib_osm_openvim
import ovim
as ovim_module
48 from lib_osm_openvim
.ovim
import ovimException
50 import osm_im
.vnfd
as vnfd_catalog
51 import osm_im
.nsd
as nsd_catalog
53 from pyangbind
.lib
.serialise
import pybindJSONDecoder
54 from itertools
import chain
57 global vimconn_imported
59 global default_volume_size
60 default_volume_size
= '5' #size in GB
65 vimconn_imported
= {} # dictionary with VIM type as key, loaded module as value
66 vim_threads
= {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
67 vim_persistent_info
= {}
68 logger
= logging
.getLogger('openmano.nfvo')
70 global_instance_tasks
= {}
75 class NfvoException(Exception):
76 def __init__(self
, message
, http_code
):
77 self
.http_code
= http_code
78 Exception.__init
__(self
, message
)
84 if task_id
<= last_task_id
:
85 task_id
= last_task_id
+ 0.000001
86 last_task_id
= task_id
87 return "TASK.{:.6f}".format(task_id
)
90 def new_task(name
, params
, depends
=None):
91 task_id
= get_task_id()
92 task
= {"status": "enqueued", "id": task_id
, "name": name
, "params": params
}
94 task
["depends"] = depends
99 return True if id[:5] == "TASK." else False
102 def get_non_used_vim_name(datacenter_name
, datacenter_id
, tenant_name
, tenant_id
):
103 name
= datacenter_name
[:16]
104 if name
not in vim_threads
["names"]:
105 vim_threads
["names"].append(name
)
107 name
= datacenter_name
[:16] + "." + tenant_name
[:16]
108 if name
not in vim_threads
["names"]:
109 vim_threads
["names"].append(name
)
111 name
= datacenter_id
+ "-" + tenant_id
112 vim_threads
["names"].append(name
)
116 def start_service(mydb
):
117 global db
, global_config
118 db
= nfvo_db
.nfvo_db()
119 db
.connect(global_config
['db_host'], global_config
['db_user'], global_config
['db_passwd'], global_config
['db_name'])
122 # Initialize openvim for SDN control
123 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
124 # TODO: review ovim.py to delete not needed configuration
125 ovim_configuration
= {
126 'logger_name': 'openmano.ovim',
127 'network_vlan_range_start': 1000,
128 'network_vlan_range_end': 4096,
129 'db_name': global_config
["db_ovim_name"],
130 'db_host': global_config
["db_ovim_host"],
131 'db_user': global_config
["db_ovim_user"],
132 'db_passwd': global_config
["db_ovim_passwd"],
135 'network_type': 'bridge',
136 #TODO: log_level_of should not be needed. To be modified in ovim
137 'log_level_of': 'DEBUG'
140 ovim
= ovim_module
.ovim(ovim_configuration
)
143 from_
= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
144 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
145 select_
= ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
146 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
147 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
148 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
149 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
)
151 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
152 'datacenter_id': vim
.get('datacenter_id')}
154 extra
.update(yaml
.load(vim
["config"]))
155 if vim
.get('dt_config'):
156 extra
.update(yaml
.load(vim
["dt_config"]))
157 if vim
["type"] not in vimconn_imported
:
160 module
= "vimconn_" + vim
["type"]
161 pkg
= __import__("osm_ro." + module
)
162 vim_conn
= getattr(pkg
, module
)
163 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
164 # vim_conn = imp.load_module(vim["type"], *module_info)
165 vimconn_imported
[vim
["type"]] = vim_conn
166 except (IOError, ImportError) as e
:
167 # if module_info and module_info[0]:
168 # file.close(module_info[0])
169 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
170 vim
["type"], module
, type(e
).__name
__, str(e
)), HTTP_Bad_Request
)
172 thread_id
= vim
['datacenter_tenant_id']
173 vim_persistent_info
[thread_id
] = {}
176 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
177 myvim
= vimconn_imported
[ vim
["type"] ].vimconnector(
178 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
179 tenant_id
=vim
['vim_tenant_id'], tenant_name
=vim
['vim_tenant_name'],
180 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
181 user
=vim
['user'], passwd
=vim
['passwd'],
182 config
=extra
, persistent_info
=vim_persistent_info
[thread_id
]
184 except Exception as e
:
185 raise NfvoException("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, e
),
186 HTTP_Internal_Server_Error
)
187 thread_name
= get_non_used_vim_name(vim
['datacenter_name'], vim
['vim_tenant_id'], vim
['vim_tenant_name'],
188 vim
['vim_tenant_id'])
189 new_thread
= vim_thread
.vim_thread(myvim
, task_lock
, thread_name
, vim
['datacenter_name'],
190 vim
['datacenter_tenant_id'], db
=db
, db_lock
=db_lock
, ovim
=ovim
)
192 vim_threads
["running"][thread_id
] = new_thread
193 except db_base_Exception
as e
:
194 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
195 except ovim_module
.ovimException
as e
:
197 if message
[:22] == "DATABASE wrong version":
198 message
= "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
199 "at host {dbhost}".format(
200 msg
=message
[22:-3], dbname
=global_config
["db_ovim_name"],
201 dbuser
=global_config
["db_ovim_user"], dbpass
=global_config
["db_ovim_passwd"],
202 ver
=message
[-3:-1], dbhost
=global_config
["db_ovim_host"])
203 raise NfvoException(message
, HTTP_Bad_Request
)
207 global ovim
, global_config
210 for thread_id
,thread
in vim_threads
["running"].items():
211 thread
.insert_task(new_task("exit", None))
212 vim_threads
["deleting"][thread_id
] = thread
213 vim_threads
["running"] = {}
214 if global_config
and global_config
.get("console_thread"):
215 for thread
in global_config
["console_thread"]:
216 thread
.terminate
= True
219 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config
["version"],
220 global_config
["version_date"] ))
223 def get_flavorlist(mydb
, vnf_id
, nfvo_tenant
=None):
225 return result, content:
226 <0, error_text upon error
227 nb_records, flavor_list on success
230 WHERE_dict
['vnf_id'] = vnf_id
231 if nfvo_tenant
is not None:
232 WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
234 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
235 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
236 flavors
= mydb
.get_rows(FROM
='vms join flavors on vms.flavor_id=flavors.uuid',SELECT
=('flavor_id',),WHERE
=WHERE_dict
)
237 #print "get_flavor_list result:", result
238 #print "get_flavor_list content:", content
240 for flavor
in flavors
:
241 flavorList
.append(flavor
['flavor_id'])
245 def get_imagelist(mydb
, vnf_id
, nfvo_tenant
=None):
247 return result, content:
248 <0, error_text upon error
249 nb_records, flavor_list on success
252 WHERE_dict
['vnf_id'] = vnf_id
253 if nfvo_tenant
is not None:
254 WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
256 #result, content = mydb.get_table(FROM='vms join vnfs on vms-vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
257 images
= mydb
.get_rows(FROM
='vms join images on vms.image_id=images.uuid',SELECT
=('image_id',),WHERE
=WHERE_dict
)
260 imageList
.append(image
['image_id'])
264 def get_vim(mydb
, nfvo_tenant
=None, datacenter_id
=None, datacenter_name
=None, datacenter_tenant_id
=None,
265 vim_tenant
=None, vim_tenant_name
=None, vim_user
=None, vim_passwd
=None):
266 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
267 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
268 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
269 raise exception upon error
272 if nfvo_tenant
is not None: WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
273 if datacenter_id
is not None: WHERE_dict
['d.uuid'] = datacenter_id
274 if datacenter_tenant_id
is not None: WHERE_dict
['datacenter_tenant_id'] = datacenter_tenant_id
275 if datacenter_name
is not None: WHERE_dict
['d.name'] = datacenter_name
276 if vim_tenant
is not None: WHERE_dict
['dt.vim_tenant_id'] = vim_tenant
277 if vim_tenant_name
is not None: WHERE_dict
['vim_tenant_name'] = vim_tenant_name
278 if nfvo_tenant
or vim_tenant
or vim_tenant_name
or datacenter_tenant_id
:
279 from_
= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
280 select_
= ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
281 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
282 'user','passwd', 'dt.config as dt_config')
284 from_
= 'datacenters as d'
285 select_
= ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
287 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
, WHERE
=WHERE_dict
)
290 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
291 'datacenter_id': vim
.get('datacenter_id')}
293 extra
.update(yaml
.load(vim
["config"]))
294 if vim
.get('dt_config'):
295 extra
.update(yaml
.load(vim
["dt_config"]))
296 if vim
["type"] not in vimconn_imported
:
299 module
= "vimconn_" + vim
["type"]
300 pkg
= __import__("osm_ro." + module
)
301 vim_conn
= getattr(pkg
, module
)
302 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
303 # vim_conn = imp.load_module(vim["type"], *module_info)
304 vimconn_imported
[vim
["type"]] = vim_conn
305 except (IOError, ImportError) as e
:
306 # if module_info and module_info[0]:
307 # file.close(module_info[0])
308 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
309 vim
["type"], module
, type(e
).__name
__, str(e
)), HTTP_Bad_Request
)
312 if 'datacenter_tenant_id' in vim
:
313 thread_id
= vim
["datacenter_tenant_id"]
314 if thread_id
not in vim_persistent_info
:
315 vim_persistent_info
[thread_id
] = {}
316 persistent_info
= vim_persistent_info
[thread_id
]
320 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
321 vim_dict
[ vim
['datacenter_id'] ] = vimconn_imported
[ vim
["type"] ].vimconnector(
322 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
323 tenant_id
=vim
.get('vim_tenant_id',vim_tenant
),
324 tenant_name
=vim
.get('vim_tenant_name',vim_tenant_name
),
325 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
326 user
=vim
.get('user',vim_user
), passwd
=vim
.get('passwd',vim_passwd
),
327 config
=extra
, persistent_info
=persistent_info
329 except Exception as e
:
330 raise NfvoException("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, str(e
)), HTTP_Internal_Server_Error
)
332 except db_base_Exception
as e
:
333 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
336 def rollback(mydb
, vims
, rollback_list
):
338 #delete things by reverse order
339 for i
in range(len(rollback_list
)-1, -1, -1):
340 item
= rollback_list
[i
]
341 if item
["where"]=="vim":
342 if item
["vim_id"] not in vims
:
344 if is_task_id(item
["uuid"]):
346 vim
= vims
[item
["vim_id"]]
348 if item
["what"]=="image":
349 vim
.delete_image(item
["uuid"])
350 mydb
.delete_row(FROM
="datacenters_images", WHERE
={"datacenter_id": vim
["id"], "vim_id":item
["uuid"]})
351 elif item
["what"]=="flavor":
352 vim
.delete_flavor(item
["uuid"])
353 mydb
.delete_row(FROM
="datacenters_flavors", WHERE
={"datacenter_id": vim
["id"], "vim_id":item
["uuid"]})
354 elif item
["what"]=="network":
355 vim
.delete_network(item
["uuid"])
356 elif item
["what"]=="vm":
357 vim
.delete_vminstance(item
["uuid"])
358 except vimconn
.vimconnException
as e
:
359 logger
.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item
['what'], item
["uuid"], str(e
))
360 undeleted_items
.append("{} {} from VIM {}".format(item
['what'], item
["uuid"], vim
["name"]))
361 except db_base_Exception
as e
:
362 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item
['what'], item
["uuid"], str(e
))
366 if item
["what"]=="image":
367 mydb
.delete_row(FROM
="images", WHERE
={"uuid": item
["uuid"]})
368 elif item
["what"]=="flavor":
369 mydb
.delete_row(FROM
="flavors", WHERE
={"uuid": item
["uuid"]})
370 except db_base_Exception
as e
:
371 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item
['what'], item
["uuid"], str(e
))
372 undeleted_items
.append("{} '{}'".format(item
['what'], item
["uuid"]))
373 if len(undeleted_items
)==0:
374 return True," Rollback successful."
376 return False," Rollback fails to delete: " + str(undeleted_items
)
379 def check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1):
381 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
383 for vnfc
in vnf_descriptor
["vnf"]["VNFC"]:
385 #dataplane interfaces
386 for numa
in vnfc
.get("numas",() ):
387 for interface
in numa
.get("interfaces",()):
388 if interface
["name"] in name_dict
:
390 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
391 vnfc
["name"], interface
["name"]),
393 name_dict
[ interface
["name"] ] = "underlay"
395 for interface
in vnfc
.get("bridge-ifaces",() ):
396 if interface
["name"] in name_dict
:
398 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
399 vnfc
["name"], interface
["name"]),
401 name_dict
[ interface
["name"] ] = "overlay"
402 vnfc_interfaces
[ vnfc
["name"] ] = name_dict
403 # check bood-data info
404 # if "boot-data" in vnfc:
405 # # check that user-data is incompatible with users and config-files
406 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
407 # raise NfvoException(
408 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
411 #check if the info in external_connections matches with the one in the vnfcs
413 for external_connection
in vnf_descriptor
["vnf"].get("external-connections",() ):
414 if external_connection
["name"] in name_list
:
416 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
417 external_connection
["name"]),
419 name_list
.append(external_connection
["name"])
420 if external_connection
["VNFC"] not in vnfc_interfaces
:
422 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
423 external_connection
["name"], external_connection
["VNFC"]),
426 if external_connection
["local_iface_name"] not in vnfc_interfaces
[ external_connection
["VNFC"] ]:
428 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
429 external_connection
["name"],
430 external_connection
["local_iface_name"]),
433 #check if the info in internal_connections matches with the one in the vnfcs
435 for internal_connection
in vnf_descriptor
["vnf"].get("internal-connections",() ):
436 if internal_connection
["name"] in name_list
:
438 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
439 internal_connection
["name"]),
441 name_list
.append(internal_connection
["name"])
442 #We should check that internal-connections of type "ptp" have only 2 elements
444 if len(internal_connection
["elements"])>2 and (internal_connection
.get("type") == "ptp" or internal_connection
.get("type") == "e-line"):
446 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
447 internal_connection
["name"],
448 'ptp' if vnf_descriptor_version
==1 else 'e-line',
449 'data' if vnf_descriptor_version
==1 else "e-lan"),
451 for port
in internal_connection
["elements"]:
453 iface
= port
["local_iface_name"]
454 if vnf
not in vnfc_interfaces
:
456 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
457 internal_connection
["name"], vnf
),
459 if iface
not in vnfc_interfaces
[ vnf
]:
461 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
462 internal_connection
["name"], iface
),
464 return -HTTP_Bad_Request
,
465 if vnf_descriptor_version
==1 and "type" not in internal_connection
:
466 if vnfc_interfaces
[vnf
][iface
] == "overlay":
467 internal_connection
["type"] = "bridge"
469 internal_connection
["type"] = "data"
470 if vnf_descriptor_version
==2 and "implementation" not in internal_connection
:
471 if vnfc_interfaces
[vnf
][iface
] == "overlay":
472 internal_connection
["implementation"] = "overlay"
474 internal_connection
["implementation"] = "underlay"
475 if (internal_connection
.get("type") == "data" or internal_connection
.get("type") == "ptp" or \
476 internal_connection
.get("implementation") == "underlay") and vnfc_interfaces
[vnf
][iface
] == "overlay":
478 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
479 internal_connection
["name"],
480 iface
, 'bridge' if vnf_descriptor_version
==1 else 'overlay',
481 'data' if vnf_descriptor_version
==1 else 'underlay'),
483 if (internal_connection
.get("type") == "bridge" or internal_connection
.get("implementation") == "overlay") and \
484 vnfc_interfaces
[vnf
][iface
] == "underlay":
486 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
487 internal_connection
["name"], iface
,
488 'data' if vnf_descriptor_version
==1 else 'underlay',
489 'bridge' if vnf_descriptor_version
==1 else 'overlay'),
493 def create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=None):
495 if only_create_at_vim
:
496 image_mano_id
= image_dict
['uuid']
497 if return_on_error
== None:
498 return_on_error
= True
500 if image_dict
['location']:
501 images
= mydb
.get_rows(FROM
="images", WHERE
={'location':image_dict
['location'], 'metadata':image_dict
['metadata']})
503 images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name':image_dict
['universal_name'], 'checksum':image_dict
['checksum']})
505 image_mano_id
= images
[0]['uuid']
507 #create image in MANO DB
508 temp_image_dict
={'name':image_dict
['name'], 'description':image_dict
.get('description',None),
509 'location':image_dict
['location'], 'metadata':image_dict
.get('metadata',None),
510 'universal_name':image_dict
['universal_name'] , 'checksum':image_dict
['checksum']
512 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
513 image_mano_id
= mydb
.new_row('images', temp_image_dict
, add_uuid
=True)
514 rollback_list
.append({"where":"mano", "what":"image","uuid":image_mano_id
})
515 #create image at every vim
516 for vim_id
,vim
in vims
.iteritems():
517 image_created
="false"
519 image_db
= mydb
.get_rows(FROM
="datacenters_images", WHERE
={'datacenter_id':vim_id
, 'image_id':image_mano_id
})
520 #look at VIM if this image exist
522 if image_dict
['location'] is not None:
523 image_vim_id
= vim
.get_image_id_from_path(image_dict
['location'])
526 filter_dict
['name'] = image_dict
['universal_name']
527 if image_dict
.get('checksum') != None:
528 filter_dict
['checksum'] = image_dict
['checksum']
529 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
530 vim_images
= vim
.get_image_list(filter_dict
)
531 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
532 if len(vim_images
) > 1:
533 raise vimconn
.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict
)), HTTP_Conflict
)
534 elif len(vim_images
) == 0:
535 raise vimconn
.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict
)))
537 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
538 image_vim_id
= vim_images
[0]['id']
540 except vimconn
.vimconnNotFoundException
as e
:
541 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
543 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
544 if image_dict
['location']:
545 image_vim_id
= vim
.new_image(image_dict
)
546 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"image","uuid":image_vim_id
})
549 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
550 raise vimconn
.vimconnException(str(e
))
551 except vimconn
.vimconnException
as e
:
553 logger
.error("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
556 logger
.warn("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
558 except vimconn
.vimconnException
as e
:
560 logger
.error("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
562 logger
.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
565 #if we reach here, the image has been created or existed
567 #add new vim_id at datacenters_images
568 mydb
.new_row('datacenters_images', {'datacenter_id':vim_id
, 'image_id':image_mano_id
, 'vim_id': image_vim_id
, 'created':image_created
})
569 elif image_db
[0]["vim_id"]!=image_vim_id
:
570 #modify existing vim_id at datacenters_images
571 mydb
.update_rows('datacenters_images', UPDATE
={'vim_id':image_vim_id
}, WHERE
={'datacenter_id':vim_id
, 'image_id':image_mano_id
})
573 return image_vim_id
if only_create_at_vim
else image_mano_id
576 def create_or_use_flavor(mydb
, vims
, flavor_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
= None):
577 temp_flavor_dict
= {'disk':flavor_dict
.get('disk',1),
578 'ram':flavor_dict
.get('ram'),
579 'vcpus':flavor_dict
.get('vcpus'),
581 if 'extended' in flavor_dict
and flavor_dict
['extended']==None:
582 del flavor_dict
['extended']
583 if 'extended' in flavor_dict
:
584 temp_flavor_dict
['extended']=yaml
.safe_dump(flavor_dict
['extended'],default_flow_style
=True,width
=256)
586 #look if flavor exist
587 if only_create_at_vim
:
588 flavor_mano_id
= flavor_dict
['uuid']
589 if return_on_error
== None:
590 return_on_error
= True
592 flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
594 flavor_mano_id
= flavors
[0]['uuid']
597 #create one by one the images of aditional disks
598 dev_image_list
=[] #list of images
599 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None:
601 for device
in flavor_dict
['extended'].get('devices',[]):
602 if "image" not in device
and "image name" not in device
:
605 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
606 image_dict
['universal_name']=device
.get('image name')
607 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
608 image_dict
['location']=device
.get('image')
609 #image_dict['new_location']=vnfc.get('image location')
610 image_dict
['checksum']=device
.get('image checksum')
611 image_metadata_dict
= device
.get('image metadata', None)
612 image_metadata_str
= None
613 if image_metadata_dict
!= None:
614 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
615 image_dict
['metadata']=image_metadata_str
616 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
617 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
618 dev_image_list
.append(image_id
)
620 temp_flavor_dict
['name'] = flavor_dict
['name']
621 temp_flavor_dict
['description'] = flavor_dict
.get('description',None)
622 content
= mydb
.new_row('flavors', temp_flavor_dict
, add_uuid
=True)
623 flavor_mano_id
= content
624 rollback_list
.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id
})
625 #create flavor at every vim
626 if 'uuid' in flavor_dict
:
627 del flavor_dict
['uuid']
629 for vim_id
,vim
in vims
.items():
630 flavor_created
="false"
632 flavor_db
= mydb
.get_rows(FROM
="datacenters_flavors", WHERE
={'datacenter_id':vim_id
, 'flavor_id':flavor_mano_id
})
633 #look at VIM if this flavor exist SKIPPED
634 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
636 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
640 # Create the flavor in VIM
641 # Translate images at devices from MANO id to VIM id
643 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None and "devices" in flavor_dict
['extended']:
644 # make a copy of original devices
647 for device
in flavor_dict
["extended"].get("devices",[]):
650 devices_original
.append(dev
)
651 if 'image' in device
:
653 if 'image metadata' in device
:
654 del device
['image metadata']
655 if 'image checksum' in device
:
656 del device
['image checksum']
658 for index
in range(0,len(devices_original
)) :
659 device
=devices_original
[index
]
660 if "image" not in device
and "image name" not in device
:
662 disk_list
.append({'size': device
.get('size', default_volume_size
)})
665 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
666 image_dict
['universal_name']=device
.get('image name')
667 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
668 image_dict
['location']=device
.get('image')
669 # image_dict['new_location']=device.get('image location')
670 image_dict
['checksum']=device
.get('image checksum')
671 image_metadata_dict
= device
.get('image metadata', None)
672 image_metadata_str
= None
673 if image_metadata_dict
!= None:
674 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
675 image_dict
['metadata']=image_metadata_str
676 image_mano_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=return_on_error
)
677 image_dict
["uuid"]=image_mano_id
678 image_vim_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=True, return_on_error
=return_on_error
)
680 #save disk information (image must be based on and size
681 disk_list
.append({'image_id': image_vim_id
, 'size': device
.get('size', default_volume_size
)})
683 flavor_dict
["extended"]["devices"][index
]['imageRef']=image_vim_id
686 #check that this vim_id exist in VIM, if not create
687 flavor_vim_id
=flavor_db
[0]["vim_id"]
689 vim
.get_flavor(flavor_vim_id
)
690 continue #flavor exist
691 except vimconn
.vimconnException
:
693 #create flavor at vim
694 logger
.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim
["name"])
697 flavor_vim_id
=vim
.get_flavor_id_from_data(flavor_dict
)
698 flavor_create
="false"
699 except vimconn
.vimconnException
as e
:
702 if not flavor_vim_id
:
703 flavor_vim_id
= vim
.new_flavor(flavor_dict
)
704 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"flavor","uuid":flavor_vim_id
})
705 flavor_created
="true"
706 except vimconn
.vimconnException
as e
:
708 logger
.error("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
710 logger
.warn("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
713 #if reach here the flavor has been create or exist
714 if len(flavor_db
)==0:
715 #add new vim_id at datacenters_flavors
716 extended_devices_yaml
= None
717 if len(disk_list
) > 0:
718 extended_devices
= dict()
719 extended_devices
['disks'] = disk_list
720 extended_devices_yaml
= yaml
.safe_dump(extended_devices
,default_flow_style
=True,width
=256)
721 mydb
.new_row('datacenters_flavors',
722 {'datacenter_id':vim_id
, 'flavor_id':flavor_mano_id
, 'vim_id': flavor_vim_id
,
723 'created':flavor_created
,'extended': extended_devices_yaml
})
724 elif flavor_db
[0]["vim_id"]!=flavor_vim_id
:
725 #modify existing vim_id at datacenters_flavors
726 mydb
.update_rows('datacenters_flavors', UPDATE
={'vim_id':flavor_vim_id
}, WHERE
={'datacenter_id':vim_id
, 'flavor_id':flavor_mano_id
})
728 return flavor_vim_id
if only_create_at_vim
else flavor_mano_id
731 def get_str(obj
, field
, length
):
733 Obtain the str value,
738 value
= obj
.get(field
)
739 if value
is not None:
740 value
= str(value
)[:length
]
743 def _lookfor_or_create_image(db_image
, mydb
, descriptor
):
745 fill image content at db_image dictionary. Check if the image with this image and checksum exist
746 :param db_image: dictionary to insert data
747 :param mydb: database connector
748 :param descriptor: yang descriptor
749 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
752 db_image
["name"] = get_str(descriptor
, "image", 255)
753 db_image
["checksum"] = get_str(descriptor
, "image-checksum", 32)
754 if not db_image
["checksum"]: # Ensure that if empty string, None is stored
755 db_image
["checksum"] = None
756 if db_image
["name"].startswith("/"):
757 db_image
["location"] = db_image
["name"]
758 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'location': db_image
["location"]})
760 db_image
["universal_name"] = db_image
["name"]
761 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name': db_image
['universal_name'],
762 'checksum': db_image
['checksum']})
764 return existing_images
[0]["uuid"]
766 image_uuid
= str(uuid4())
767 db_image
["uuid"] = image_uuid
770 def new_vnfd_v3(mydb
, tenant_id
, vnf_descriptor
):
772 Parses an OSM IM vnfd_catalog and insert at DB
775 :param vnf_descriptor:
776 :return: The list of cretated vnf ids
779 myvnfd
= vnfd_catalog
.vnfd()
781 pybindJSONDecoder
.load_ietf_json(vnf_descriptor
, None, None, obj
=myvnfd
)
782 except Exception as e
:
783 raise NfvoException("Invalid yang descriptor format " + str(e
), HTTP_Bad_Request
)
793 for rift_vnfd
in myvnfd
.vnfd_catalog
.vnfd
.itervalues():
794 vnfd
= rift_vnfd
.get()
797 vnf_uuid
= str(uuid4())
798 uuid_list
.append(vnf_uuid
)
799 vnfd_uuid_list
.append(vnf_uuid
)
802 "osm_id": get_str(vnfd
, "id", 255),
803 "name": get_str(vnfd
, "name", 255),
804 "description": get_str(vnfd
, "description", 255),
805 "tenant_id": tenant_id
,
806 "vendor": get_str(vnfd
, "vendor", 255),
807 "short_name": get_str(vnfd
, "short-name", 255),
808 "descriptor": str(vnf_descriptor
)[:60000]
811 # table nets (internal-vld)
812 net_id2uuid
= {} # for mapping interface with network
813 for vld
in vnfd
.get("internal-vld").itervalues():
814 net_uuid
= str(uuid4())
815 uuid_list
.append(net_uuid
)
817 "name": get_str(vld
, "name", 255),
820 "description": get_str(vld
, "description", 255),
821 "type": "bridge", # TODO adjust depending on connection point type
823 net_id2uuid
[vld
.get("id")] = net_uuid
824 db_nets
.append(db_net
)
828 vdu_id2db_table_index
= {}
829 for vdu
in vnfd
.get("vdu").itervalues():
830 vm_uuid
= str(uuid4())
831 uuid_list
.append(vm_uuid
)
834 "osm_id": get_str(vdu
, "id", 255),
835 "name": get_str(vdu
, "name", 255),
836 "description": get_str(vdu
, "description", 255),
839 vdu_id2uuid
[db_vm
["osm_id"]] = vm_uuid
840 vdu_id2db_table_index
[db_vm
["osm_id"]] = db_vms_index
842 db_vm
["count"] = int(vdu
["count"])
845 image_present
= False
849 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, vdu
)
851 image_uuid
= db_image
["uuid"]
852 db_images
.append(db_image
)
853 db_vm
["image_id"] = image_uuid
857 if vdu
.get("volumes"):
858 for volume_key
in sorted(vdu
["volumes"]):
859 volume
= vdu
["volumes"][volume_key
]
860 if not image_present
:
861 # Convert the first volume to vnfc.image
864 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, volume
)
866 image_uuid
= db_image
["uuid"]
867 db_images
.append(db_image
)
868 db_vm
["image_id"] = image_uuid
870 # Add Openmano devices
872 device
["type"] = str(volume
.get("device-type"))
873 if volume
.get("size"):
874 device
["size"] = int(volume
["size"])
875 if volume
.get("image"):
876 device
["image name"] = str(volume
["image"])
877 if volume
.get("image-checksum"):
878 device
["image checksum"] = str(volume
["image-checksum"])
879 devices
.append(device
)
883 "name": get_str(vdu
, "name", 250) + "-flv",
884 "vcpus": int(vdu
["vm-flavor"].get("vcpu-count", 1)),
885 "ram": int(vdu
["vm-flavor"].get("memory-mb", 1)),
886 "disk": int(vdu
["vm-flavor"].get("storage-gb", 1)),
892 extended
["devices"] = devices
893 if vdu
.get("guest-epa"): # TODO or dedicated_int:
895 if vdu
["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
896 numa_node_policy
= vdu
["guest-epa"].get("numa-node-policy")
897 if numa_node_policy
.get("node"):
898 numa_node
= numa_node_policy
.node
[0]
899 if numa_node
.get("num-cores"):
900 numa
["cores"] = numa_node
["num-cores"]
902 if numa_node
.get("paired-threads"):
903 if numa_node
["paired-threads"].get("num-paired-threads"):
904 numa
["paired-threads"] = numa_node
["paired-threads"]["num-paired-threads"]
906 if len(numa_node
["paired-threads"].get("paired-thread-ids")) > 0:
907 numa
["paired-threads-id"] = []
908 for pair
in numa_node
["paired-threads"]["paired-thread-ids"].itervalues
:
909 numa
["paired-threads-id"].append(
910 (str(pair
["thread-a"]), str(pair
["thread-b"]))
912 if numa_node
.get("num-threads"):
913 numa
["threads"] = numa_node
["num-threads"]
915 if numa_node
.get("memory-mb"):
916 numa
["memory"] = max(int(numa_node
["memory-mb"] / 1024), 1)
917 if vdu
["guest-epa"].get("mempage-size"):
918 if vdu
["guest-epa"]["mempage-size"] != "SMALL":
919 numa
["memory"] = max(int(db_flavor
["ram"] / 1024), 1)
920 if vdu
["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set
:
921 if vdu
["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
922 if vdu
["guest-epa"].get("cpu-thread-pinning-policy") and \
923 vdu
["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
924 numa
["cores"] = max(db_flavor
["vcpus"], 1)
926 numa
["threads"] = max(db_flavor
["vcpus"], 1)
928 extended
["numas"] = [numa
]
930 extended_text
= yaml
.safe_dump(extended
, default_flow_style
=True, width
=256)
931 db_flavor
["extended"] = extended_text
932 # look if flavor exist
934 temp_flavor_dict
= {'disk': db_flavor
.get('disk', 1),
935 'ram': db_flavor
.get('ram'),
936 'vcpus': db_flavor
.get('vcpus'),
937 'extended': db_flavor
.get('extended')
939 existing_flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
941 flavor_uuid
= existing_flavors
[0]["uuid"]
943 flavor_uuid
= str(uuid4())
944 uuid_list
.append(flavor_uuid
)
945 db_flavor
["uuid"] = flavor_uuid
946 db_flavors
.append(db_flavor
)
947 db_vm
["flavor_id"] = flavor_uuid
951 if vdu
.get("cloud-init"):
952 boot_data
["user-data"] = vdu
["cloud-init"]
953 elif vdu
.get("cloud-init-file"):
954 # TODO Where this file content is present???
955 # boot_data["user-data"] = rift_vnfd.files[vdu["cloud-init-file"]]
956 boot_data
["user-data"] = str(vdu
["cloud-init-file"])
958 if vdu
.get("supplemental-boot-data"):
959 if vdu
["supplemental-boot-data"].get('boot-data-drive'):
960 boot_data
['boot-data-drive'] = True
961 if vdu
["supplemental-boot-data"].get('config-file'):
962 om_cfgfile_list
= list()
963 for custom_config_file
in vdu
["supplemental-boot-data"]['config-file'].itervalues():
964 # TODO Where this file content is present???
965 cfg_source
= str(custom_config_file
["source"])
966 om_cfgfile_list
.append({"dest": custom_config_file
["dest"],
967 "content": cfg_source
})
968 boot_data
['config-files'] = om_cfgfile_list
970 db_vm
["boot_data"] = boot_data
975 # table interfaces (internal/external interfaces)
976 cp_name2iface_uuid
= {}
978 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
979 for iface
in vdu
.get("interface").itervalues():
980 iface_uuid
= str(uuid4())
981 uuid_list
.append(iface_uuid
)
984 "internal_name": get_str(iface
, "name", 255),
987 if iface
.get("virtual-interface").get("vpci"):
988 db_interface
["vpci"] = get_str(iface
.get("virtual-interface"), "vpci", 12)
990 if iface
.get("virtual-interface").get("bandwidth"):
991 bps
= int(iface
.get("virtual-interface").get("bandwidth"))
992 db_interface
["bw"] = bps
/1000
994 if iface
.get("virtual-interface").get("type") == "OM-MGMT":
995 db_interface
["type"] = "mgmt"
996 elif iface
.get("virtual-interface").get("type") in ("VIRTIO", "E1000"):
997 db_interface
["type"] = "bridge"
998 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
999 elif iface
.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1000 db_interface
["type"] = "data"
1001 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
1003 raise ValueError("Interface type {} not supported".format(iface
.get("virtual-interface").get("type")))
1005 if iface
.get("external-connection-point-ref"):
1007 cp
= vnfd
.get("connection-point")[iface
.get("external-connection-point-ref")]
1008 db_interface
["external_name"] = get_str(cp
, "name", 255)
1009 cp_name2iface_uuid
[db_interface
["external_name"]] = iface_uuid
1010 cp_name2vm_uuid
[db_interface
["external_name"]] = vm_uuid
1011 # TODO add port-security-enable
1012 # if cp.get("port-security-enabled") == False:
1013 # elif cp.get("port-security-enabled") == True:
1016 "Error wrong reference at vnfd['{vnf}'] vdu['{vdu}']:internal-interface['{iface}']:"
1017 "vnfd-connection-point-ref '{cp}' is not present at connection-point".format(
1018 vnf
=vnfd
["id"], vdu
=vdu
["id"], iface
=iface
["name"],
1019 cp
=iface
.get("vnfd-connection-point-ref"))
1021 elif iface
.get("internal-connection-point-ref"):
1023 for vld
in vnfd
.get("internal-vld").itervalues():
1024 for cp
in vld
.get("internal-connection-point").itervalues():
1025 if cp
.get("id-ref") == iface
.get("internal-connection-point-ref"):
1026 db_interface
["net_id"] = net_id2uuid
[vld
.get("id")]
1030 "Error at vnfd['{vnf}'] vdu['{vdu}']:internal-interface['{iface}']:"
1031 "vdu-internal-connection-point-ref '{cp}' is not referenced by any internal-vld".format(
1032 vnf
=vnfd
["id"], vdu
=vdu
["id"], iface
=iface
["name"],
1033 cp
=iface
.get("vdu-internal-connection-point-ref"))
1035 if iface
.get("position") is not None:
1036 db_interface
["created_at"] = int(iface
.get("position")) - 1000
1037 db_interfaces
.append(db_interface
)
1039 # VNF affinity and antiaffinity
1040 for pg
in vnfd
.get("placement-groups").itervalues():
1041 pg_name
= get_str(pg
, "name", 255)
1042 for vdu
in pg
.get("member-vdus").itervalues():
1043 vdu_id
= get_str(vdu
, "member-vdu-ref", 255)
1044 if vdu_id
not in vdu_id2db_table_index
:
1046 "Error at 'vnfd'['{vnf}']:'placement-groups'['{pg}']:'member-vdus':'{vdu}' references a non existing vdu".format(
1047 vnf
=vnfd
["id"], pg
=pg_name
, vdu
=vdu_id
))
1048 db_vms
[vdu_id2db_table_index
[vdu_id
]]["availability_zone"] = pg_name
1049 # TODO consider the case of isolation and not colocation
1050 # if pg.get("strategy") == "ISOLATION":
1052 # VNF mgmt configuration
1054 if vnfd
["mgmt-interface"].get("vdu-id"):
1055 if vnfd
["mgmt-interface"]["vdu-id"] not in vdu_id2uuid
:
1057 "Error at vnfd['{vnf}']:'mgmt-interface':'vdu-id':{vdu} reference a non existing vdu".format(
1058 vnf
=vnfd
["id"], vdu
=vnfd
["mgmt-interface"]["vdu-id"]))
1059 mgmt_access
["vm_id"] = vdu_id2uuid
[vnfd
["mgmt-interface"]["vdu-id"]]
1060 if vnfd
["mgmt-interface"].get("ip-address"):
1061 mgmt_access
["ip-address"] = str(vnfd
["mgmt-interface"].get("ip-address"))
1062 if vnfd
["mgmt-interface"].get("cp"):
1063 if vnfd
["mgmt-interface"]["cp"] not in cp_name2iface_uuid
:
1065 "Error at vnfd['{vnf}']:'mgmt-interface':'cp':{cp} reference a non existing connection-point".
1066 format(vnf
=vnfd
["id"], cp
=vnfd
["mgmt-interface"]["cp"]))
1067 mgmt_access
["vm_id"] = cp_name2vm_uuid
[vnfd
["mgmt-interface"]["cp"]]
1068 mgmt_access
["interface_id"] = cp_name2iface_uuid
[vnfd
["mgmt-interface"]["cp"]]
1069 default_user
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1072 mgmt_access
["default_user"] = default_user
1074 db_vnf
["mgmt_access"] = yaml
.safe_dump(mgmt_access
, default_flow_style
=True, width
=256)
1076 db_vnfs
.append(db_vnf
)
1080 {"images": db_images
},
1081 {"flavors": db_flavors
},
1083 {"interfaces": db_interfaces
},
1086 logger
.debug("create_vnf Deployment done vnfDict: %s",
1087 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
1088 mydb
.new_rows(db_tables
, uuid_list
)
1089 return vnfd_uuid_list
1090 except Exception as e
:
1091 logger
.error("Exception {}".format(e
))
1092 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
1095 def new_vnf(mydb
, tenant_id
, vnf_descriptor
):
1096 global global_config
1098 # Step 1. Check the VNF descriptor
1099 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1)
1100 # Step 2. Check tenant exist
1102 if tenant_id
!= "any":
1103 check_tenant(mydb
, tenant_id
)
1104 if "tenant_id" in vnf_descriptor
["vnf"]:
1105 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1106 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1109 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1110 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1111 if global_config
["auto_push_VNF_to_VIMs"]:
1112 vims
= get_vim(mydb
, tenant_id
)
1114 # Step 4. Review the descriptor and add missing fields
1115 #print vnf_descriptor
1116 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1117 vnf_name
= vnf_descriptor
['vnf']['name']
1118 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1119 if "physical" in vnf_descriptor
['vnf']:
1120 del vnf_descriptor
['vnf']['physical']
1121 #print vnf_descriptor
1123 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1124 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1125 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1127 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1128 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1129 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1131 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1132 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1134 VNFCitem
["name"] = vnfc
['name']
1135 VNFCitem
["availability_zone"] = vnfc
.get('availability_zone')
1136 VNFCitem
["description"] = vnfc
.get("description", 'VM %s of the VNF %s' %(vnfc
['name'],vnf_name
))
1138 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1141 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1142 myflavorDict
["description"] = VNFCitem
["description"]
1143 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1144 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1145 myflavorDict
["disk"] = vnfc
.get("disk", 1)
1146 myflavorDict
["extended"] = {}
1148 devices
= vnfc
.get("devices")
1150 myflavorDict
["extended"]["devices"] = devices
1153 # Mapping from processor models to rankings should be available somehow in the NFVO. They could be taken from VIM or directly from a new database table
1154 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1156 # Previous code has been commented
1157 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1158 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1159 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1160 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1162 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1164 # print "Error creating flavor: unknown processor model. Rollback successful."
1165 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1167 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1168 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1170 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1171 myflavorDict
['extended']['numas'] = vnfc
['numas']
1175 # Step 6.2 New flavors are created in the VIM
1176 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1178 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1179 VNFCitem
["flavor_id"] = flavor_id
1180 VNFCDict
[vnfc
['name']] = VNFCitem
1182 logger
.debug("Creating new images in the VIM for each VNFC")
1183 # Step 6.3 New images are created in the VIM
1184 #For each VNFC, we must create the appropriate image.
1185 #This "for" loop might be integrated with the previous one
1186 #In case this integration is made, the VNFCDict might become a VNFClist.
1187 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1188 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1190 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1191 image_dict
['universal_name']=vnfc
.get('image name')
1192 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1193 image_dict
['location']=vnfc
.get('VNFC image')
1194 #image_dict['new_location']=vnfc.get('image location')
1195 image_dict
['checksum']=vnfc
.get('image checksum')
1196 image_metadata_dict
= vnfc
.get('image metadata', None)
1197 image_metadata_str
= None
1198 if image_metadata_dict
is not None:
1199 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1200 image_dict
['metadata']=image_metadata_str
1201 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1202 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1203 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1204 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1205 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1206 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1207 if vnfc
.get("boot-data"):
1208 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1211 # Step 7. Storing the VNF descriptor in the repository
1212 if "descriptor" not in vnf_descriptor
["vnf"]:
1213 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1215 # Step 8. Adding the VNF to the NFVO DB
1216 vnf_id
= mydb
.new_vnf_as_a_whole(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1218 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1219 _
, message
= rollback(mydb
, vims
, rollback_list
)
1220 if isinstance(e
, db_base_Exception
):
1221 error_text
= "Exception at database"
1222 elif isinstance(e
, KeyError):
1223 error_text
= "KeyError exception "
1224 e
.http_code
= HTTP_Internal_Server_Error
1226 error_text
= "Exception at VIM"
1227 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1228 #logger.error("start_scenario %s", error_text)
1229 raise NfvoException(error_text
, e
.http_code
)
1232 def new_vnf_v02(mydb
, tenant_id
, vnf_descriptor
):
1233 global global_config
1235 # Step 1. Check the VNF descriptor
1236 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=2)
1237 # Step 2. Check tenant exist
1239 if tenant_id
!= "any":
1240 check_tenant(mydb
, tenant_id
)
1241 if "tenant_id" in vnf_descriptor
["vnf"]:
1242 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1243 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1246 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1247 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1248 if global_config
["auto_push_VNF_to_VIMs"]:
1249 vims
= get_vim(mydb
, tenant_id
)
1251 # Step 4. Review the descriptor and add missing fields
1252 #print vnf_descriptor
1253 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1254 vnf_name
= vnf_descriptor
['vnf']['name']
1255 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1256 if "physical" in vnf_descriptor
['vnf']:
1257 del vnf_descriptor
['vnf']['physical']
1258 #print vnf_descriptor
1260 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1261 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1262 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1264 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1265 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1266 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1268 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1269 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1271 VNFCitem
["name"] = vnfc
['name']
1272 VNFCitem
["description"] = vnfc
.get("description", 'VM %s of the VNF %s' %(vnfc
['name'],vnf_name
))
1274 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1277 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1278 myflavorDict
["description"] = VNFCitem
["description"]
1279 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1280 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1281 myflavorDict
["disk"] = vnfc
.get("disk", 1)
1282 myflavorDict
["extended"] = {}
1284 devices
= vnfc
.get("devices")
1286 myflavorDict
["extended"]["devices"] = devices
1289 # Mapping from processor models to rankings should be available somehow in the NFVO. They could be taken from VIM or directly from a new database table
1290 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1292 # Previous code has been commented
1293 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1294 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1295 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1296 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1298 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1300 # print "Error creating flavor: unknown processor model. Rollback successful."
1301 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1303 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1304 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1306 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1307 myflavorDict
['extended']['numas'] = vnfc
['numas']
1311 # Step 6.2 New flavors are created in the VIM
1312 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1314 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1315 VNFCitem
["flavor_id"] = flavor_id
1316 VNFCDict
[vnfc
['name']] = VNFCitem
1318 logger
.debug("Creating new images in the VIM for each VNFC")
1319 # Step 6.3 New images are created in the VIM
1320 #For each VNFC, we must create the appropriate image.
1321 #This "for" loop might be integrated with the previous one
1322 #In case this integration is made, the VNFCDict might become a VNFClist.
1323 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1324 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1326 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1327 image_dict
['universal_name']=vnfc
.get('image name')
1328 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1329 image_dict
['location']=vnfc
.get('VNFC image')
1330 #image_dict['new_location']=vnfc.get('image location')
1331 image_dict
['checksum']=vnfc
.get('image checksum')
1332 image_metadata_dict
= vnfc
.get('image metadata', None)
1333 image_metadata_str
= None
1334 if image_metadata_dict
is not None:
1335 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1336 image_dict
['metadata']=image_metadata_str
1337 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1338 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1339 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1340 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1341 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1342 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1343 if vnfc
.get("boot-data"):
1344 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1346 # Step 7. Storing the VNF descriptor in the repository
1347 if "descriptor" not in vnf_descriptor
["vnf"]:
1348 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1350 # Step 8. Adding the VNF to the NFVO DB
1351 vnf_id
= mydb
.new_vnf_as_a_whole2(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1353 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1354 _
, message
= rollback(mydb
, vims
, rollback_list
)
1355 if isinstance(e
, db_base_Exception
):
1356 error_text
= "Exception at database"
1357 elif isinstance(e
, KeyError):
1358 error_text
= "KeyError exception "
1359 e
.http_code
= HTTP_Internal_Server_Error
1361 error_text
= "Exception at VIM"
1362 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1363 #logger.error("start_scenario %s", error_text)
1364 raise NfvoException(error_text
, e
.http_code
)
1367 def get_vnf_id(mydb
, tenant_id
, vnf_id
):
1368 #check valid tenant_id
1369 check_tenant(mydb
, tenant_id
)
1372 if tenant_id
!= "any":
1373 where_or
["tenant_id"] = tenant_id
1374 where_or
["public"] = True
1375 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1377 vnf_id
= vnf
["uuid"]
1378 filter_keys
= ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
1379 filtered_content
= dict( (k
,v
) for k
,v
in vnf
.iteritems() if k
in filter_keys
)
1380 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1381 data
={'vnf' : filtered_content
}
1383 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id',
1384 SELECT
=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1386 WHERE
={'vnfs.uuid': vnf_id
} )
1388 raise NfvoException("vnf '{}' not found".format(vnf_id
), HTTP_Not_Found
)
1389 # change boot_data into boot-data
1391 if vm
.get("boot_data"):
1392 vm
["boot-data"] = yaml
.safe_load(vm
["boot_data"])
1395 data
['vnf']['VNFC'] = content
1396 #TODO: GET all the information from a VNFC and include it in the output.
1399 content
= mydb
.get_rows(FROM
='vnfs join nets on vnfs.uuid=nets.vnf_id',
1400 SELECT
=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1401 WHERE
={'vnfs.uuid': vnf_id
} )
1402 data
['vnf']['nets'] = content
1404 #GET ip-profile for each net
1405 for net
in data
['vnf']['nets']:
1406 ipprofiles
= mydb
.get_rows(FROM
='ip_profiles',
1407 SELECT
=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1408 WHERE
={'net_id': net
["uuid"]} )
1409 if len(ipprofiles
)==1:
1410 net
["ip_profile"] = ipprofiles
[0]
1411 elif len(ipprofiles
)>1:
1412 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net
['uuid']), HTTP_Bad_Request
)
1415 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
1417 #GET External Interfaces
1418 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
1419 SELECT
=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1420 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
1421 WHERE
={'vnfs.uuid': vnf_id
},
1422 WHERE_NOT
={'interfaces.external_name': None} )
1424 data
['vnf']['external-connections'] = content
1429 def delete_vnf(mydb
,tenant_id
,vnf_id
,datacenter
=None,vim_tenant
=None):
1430 # Check tenant exist
1431 if tenant_id
!= "any":
1432 check_tenant(mydb
, tenant_id
)
1433 # Get the URL of the VIM from the nfvo_tenant and the datacenter
1434 vims
= get_vim(mydb
, tenant_id
)
1438 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1440 if tenant_id
!= "any":
1441 where_or
["tenant_id"] = tenant_id
1442 where_or
["public"] = True
1443 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1444 vnf_id
= vnf
["uuid"]
1446 # "Getting the list of flavors and tenants of the VNF"
1447 flavorList
= get_flavorlist(mydb
, vnf_id
)
1448 if len(flavorList
)==0:
1449 logger
.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id
)
1451 imageList
= get_imagelist(mydb
, vnf_id
)
1452 if len(imageList
)==0:
1453 logger
.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id
)
1455 deleted
= mydb
.delete_row_by_id('vnfs', vnf_id
)
1457 raise NfvoException("vnf '{}' not found".format(vnf_id
), HTTP_Not_Found
)
1460 for flavor
in flavorList
:
1461 #check if flavor is used by other vnf
1463 c
= mydb
.get_rows(FROM
='vms', WHERE
={'flavor_id':flavor
} )
1465 logger
.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor
)
1467 #flavor not used, must be deleted
1469 c
= mydb
.get_rows(FROM
='datacenters_flavors', WHERE
={'flavor_id':flavor
})
1470 for flavor_vim
in c
:
1471 if flavor_vim
["datacenter_id"] not in vims
:
1473 if flavor_vim
['created']=='false': #skip this flavor because not created by openmano
1475 myvim
=vims
[ flavor_vim
["datacenter_id"] ]
1477 myvim
.delete_flavor(flavor_vim
["vim_id"])
1478 except vimconn
.vimconnNotFoundException
as e
:
1479 logger
.warn("VIM flavor %s not exist at datacenter %s", flavor_vim
["vim_id"], flavor_vim
["datacenter_id"] )
1480 except vimconn
.vimconnException
as e
:
1481 logger
.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1482 flavor_vim
["vim_id"], flavor_vim
["datacenter_id"], type(e
).__name
__, str(e
))
1483 undeletedItems
.append("flavor {} from VIM {}".format(flavor_vim
["vim_id"], flavor_vim
["datacenter_id"] ))
1484 #delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1485 mydb
.delete_row_by_id('flavors', flavor
)
1486 except db_base_Exception
as e
:
1487 logger
.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor
, str(e
))
1488 undeletedItems
.append("flavor %s" % flavor
)
1491 for image
in imageList
:
1493 #check if image is used by other vnf
1494 c
= mydb
.get_rows(FROM
='vms', WHERE
={'image_id':image
} )
1496 logger
.debug("Image '%s' not deleted because it is being used by another VNF", image
)
1498 #image not used, must be deleted
1500 c
= mydb
.get_rows(FROM
='datacenters_images', WHERE
={'image_id':image
})
1502 if image_vim
["datacenter_id"] not in vims
:
1504 if image_vim
['created']=='false': #skip this image because not created by openmano
1506 myvim
=vims
[ image_vim
["datacenter_id"] ]
1508 myvim
.delete_image(image_vim
["vim_id"])
1509 except vimconn
.vimconnNotFoundException
as e
:
1510 logger
.warn("VIM image %s not exist at datacenter %s", image_vim
["vim_id"], image_vim
["datacenter_id"] )
1511 except vimconn
.vimconnException
as e
:
1512 logger
.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1513 image_vim
["vim_id"], image_vim
["datacenter_id"], type(e
).__name
__, str(e
))
1514 undeletedItems
.append("image {} from VIM {}".format(image_vim
["vim_id"], image_vim
["datacenter_id"] ))
1515 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1516 mydb
.delete_row_by_id('images', image
)
1517 except db_base_Exception
as e
:
1518 logger
.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image
, str(e
))
1519 undeletedItems
.append("image %s" % image
)
1521 return vnf_id
+ " " + vnf
["name"]
1523 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
1526 def get_hosts_info(mydb
, nfvo_tenant_id
, datacenter_name
=None):
1527 result
, vims
= get_vim(mydb
, nfvo_tenant_id
, None, datacenter_name
)
1531 return -HTTP_Not_Found
, "datacenter '%s' not found" % datacenter_name
1532 myvim
= vims
.values()[0]
1533 result
,servers
= myvim
.get_hosts_info()
1535 return result
, servers
1536 topology
= {'name':myvim
['name'] , 'servers': servers
}
1537 return result
, topology
1540 def get_hosts(mydb
, nfvo_tenant_id
):
1541 vims
= get_vim(mydb
, nfvo_tenant_id
)
1543 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id
)), HTTP_Not_Found
)
1545 #print "nfvo.datacenter_action() error. Several datacenters found"
1546 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
1547 myvim
= vims
.values()[0]
1549 hosts
= myvim
.get_hosts()
1550 logger
.debug('VIM hosts response: '+ yaml
.safe_dump(hosts
, indent
=4, default_flow_style
=False))
1552 datacenter
= {'Datacenters': [ {'name':myvim
['name'],'servers':[]} ] }
1554 server
={'name':host
['name'], 'vms':[]}
1555 for vm
in host
['instances']:
1556 #get internal name and model
1558 c
= mydb
.get_rows(SELECT
=('name',), FROM
='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1559 WHERE
={'vim_vm_id':vm
['id']} )
1561 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm
['id']))
1563 server
['vms'].append( {'name':vm
['name'] , 'model':c
[0]['name']} )
1565 except db_base_Exception
as e
:
1566 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm
['id'], str(e
)))
1567 datacenter
['Datacenters'][0]['servers'].append(server
)
1568 #return -400, "en construccion"
1570 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1572 except vimconn
.vimconnException
as e
:
1573 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e
)), e
.http_code
)
1576 def new_scenario(mydb
, tenant_id
, topo
):
1578 # result, vims = get_vim(mydb, tenant_id)
1580 # return result, vims
1582 if tenant_id
!= "any":
1583 check_tenant(mydb
, tenant_id
)
1584 if "tenant_id" in topo
:
1585 if topo
["tenant_id"] != tenant_id
:
1586 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo
["tenant_id"], tenant_id
),
1591 #1.1: get VNFs and external_networks (other_nets).
1593 other_nets
={} #external_networks, bridge_networks and data_networkds
1594 nodes
= topo
['topology']['nodes']
1595 for k
in nodes
.keys():
1596 if nodes
[k
]['type'] == 'VNF':
1598 vnfs
[k
]['ifaces'] = {}
1599 elif nodes
[k
]['type'] == 'other_network' or nodes
[k
]['type'] == 'external_network':
1600 other_nets
[k
] = nodes
[k
]
1601 other_nets
[k
]['external']=True
1602 elif nodes
[k
]['type'] == 'network':
1603 other_nets
[k
] = nodes
[k
]
1604 other_nets
[k
]['external']=False
1607 #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1608 for name
,vnf
in vnfs
.items():
1610 where_or
={"tenant_id": tenant_id
, 'public': "true"}
1612 error_pos
= "'topology':'nodes':'" + name
+ "'"
1614 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
1615 where
['uuid'] = vnf
['vnf_id']
1616 if 'VNF model' in vnf
:
1617 error_text
+= " 'VNF model' " + vnf
['VNF model']
1618 where
['name'] = vnf
['VNF model']
1620 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos
, HTTP_Bad_Request
)
1622 vnf_db
= mydb
.get_rows(SELECT
=('uuid','name','description'),
1628 raise NfvoException("unknown" + error_text
+ " at " + error_pos
, HTTP_Not_Found
)
1630 raise NfvoException("more than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", HTTP_Conflict
)
1631 vnf
['uuid']=vnf_db
[0]['uuid']
1632 vnf
['description']=vnf_db
[0]['description']
1633 #get external interfaces
1634 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1635 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1636 WHERE
={'vnfs.uuid':vnf
['uuid']}, WHERE_NOT
={'external_name':None} )
1637 for ext_iface
in ext_ifaces
:
1638 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type':ext_iface
['type']}
1640 #1.4 get list of connections
1641 conections
= topo
['topology']['connections']
1642 conections_list
= []
1643 conections_list_name
= []
1644 for k
in conections
.keys():
1645 if type(conections
[k
]['nodes'])==dict: #dict with node:iface pairs
1646 ifaces_list
= conections
[k
]['nodes'].items()
1647 elif type(conections
[k
]['nodes'])==list: #list with dictionary
1649 conection_pair_list
= map(lambda x
: x
.items(), conections
[k
]['nodes'] )
1650 for k2
in conection_pair_list
:
1653 con_type
= conections
[k
].get("type", "link")
1654 if con_type
!= "link":
1656 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k
)), HTTP_Bad_Request
)
1657 other_nets
[k
] = {'external': False}
1658 if conections
[k
].get("graph"):
1659 other_nets
[k
]["graph"] = conections
[k
]["graph"]
1660 ifaces_list
.append( (k
, None) )
1663 if con_type
== "external_network":
1664 other_nets
[k
]['external'] = True
1665 if conections
[k
].get("model"):
1666 other_nets
[k
]["model"] = conections
[k
]["model"]
1668 other_nets
[k
]["model"] = k
1669 if con_type
== "dataplane_net" or con_type
== "bridge_net":
1670 other_nets
[k
]["model"] = con_type
1672 conections_list_name
.append(k
)
1673 conections_list
.append(set(ifaces_list
)) #from list to set to operate as a set (this conversion removes elements that are repeated in a list)
1674 #print set(ifaces_list)
1675 #check valid VNF and iface names
1676 for iface
in ifaces_list
:
1677 if iface
[0] not in vnfs
and iface
[0] not in other_nets
:
1678 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1679 str(k
), iface
[0]), HTTP_Not_Found
)
1680 if iface
[0] in vnfs
and iface
[1] not in vnfs
[ iface
[0] ]['ifaces']:
1681 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1682 str(k
), iface
[0], iface
[1]), HTTP_Not_Found
)
1684 #1.5 unify connections from the pair list to a consolidated list
1686 while index
< len(conections_list
):
1688 while index2
< len(conections_list
):
1689 if len(conections_list
[index
] & conections_list
[index2
])>0: #common interface, join nets
1690 conections_list
[index
] |
= conections_list
[index2
]
1691 del conections_list
[index2
]
1692 del conections_list_name
[index2
]
1695 conections_list
[index
] = list(conections_list
[index
]) # from set to list again
1697 #for k in conections_list:
1702 #1.6 Delete non external nets
1703 # for k in other_nets.keys():
1704 # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1705 # for con in conections_list:
1707 # for index in range(0,len(con)):
1708 # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1709 # for index in delete_indexes:
1712 #1.7: Check external_ports are present at database table datacenter_nets
1713 for k
,net
in other_nets
.items():
1714 error_pos
= "'topology':'nodes':'" + k
+ "'"
1715 if net
['external']==False:
1716 if 'name' not in net
:
1718 if 'model' not in net
:
1719 raise NfvoException("needed a 'model' at " + error_pos
, HTTP_Bad_Request
)
1720 if net
['model']=='bridge_net':
1721 net
['type']='bridge';
1722 elif net
['model']=='dataplane_net':
1725 raise NfvoException("unknown 'model' '"+ net
['model'] +"' at " + error_pos
, HTTP_Not_Found
)
1727 #IF we do not want to check that external network exist at datacenter
1732 # if 'net_id' in net:
1733 # error_text += " 'net_id' " + net['net_id']
1734 # WHERE_['uuid'] = net['net_id']
1735 # if 'model' in net:
1736 # error_text += " 'model' " + net['model']
1737 # WHERE_['name'] = net['model']
1738 # if len(WHERE_) == 0:
1739 # return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1740 # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1741 # FROM='datacenter_nets', WHERE=WHERE_ )
1743 # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1745 # print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1746 # return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1748 # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1749 # return -HTTP_Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
1750 # other_nets[k].update(net_db[0])
1753 net_nb
=0 #Number of nets
1754 for con
in conections_list
:
1755 #check if this is connected to a external net
1759 for index
in range(0,len(con
)):
1760 #check if this is connected to a external net
1761 for net_key
in other_nets
.keys():
1762 if con
[index
][0]==net_key
:
1763 if other_net_index
>=0:
1764 error_text
="There is some interface connected both to net '%s' and net '%s'" % (con
[other_net_index
][0], net_key
)
1765 #print "nfvo.new_scenario " + error_text
1766 raise NfvoException(error_text
, HTTP_Bad_Request
)
1768 other_net_index
= index
1769 net_target
= net_key
1771 #print "other_net_index", other_net_index
1773 if other_net_index
>=0:
1774 del con
[other_net_index
]
1775 #IF we do not want to check that external network exist at datacenter
1776 if other_nets
[net_target
]['external'] :
1777 if "name" not in other_nets
[net_target
]:
1778 other_nets
[net_target
]['name'] = other_nets
[net_target
]['model']
1779 if other_nets
[net_target
]["type"] == "external_network":
1780 if vnfs
[ con
[0][0] ]['ifaces'][ con
[0][1] ]["type"] == "data":
1781 other_nets
[net_target
]["type"] = "data"
1783 other_nets
[net_target
]["type"] = "bridge"
1785 # if other_nets[net_target]['external'] :
1786 # type_='data' if len(con)>1 else 'ptp' #an external net is connected to a external port, so it is ptp if only one connection is done to this net
1787 # if type_=='data' and other_nets[net_target]['type']=="ptp":
1788 # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
1789 # print "nfvo.new_scenario " + error_text
1790 # return -HTTP_Bad_Request, error_text
1793 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
1796 net_type_bridge
=False
1798 net_target
= "__-__net"+str(net_nb
)
1799 net_list
[net_target
] = {'name': conections_list_name
[net_nb
], #"net-"+str(net_nb),
1800 'description':"net-%s in scenario %s" %(net_nb
,topo
['name']),
1803 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
1804 iface_type
= vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['type']
1805 if iface_type
=='mgmt' or iface_type
=='bridge':
1806 net_type_bridge
= True
1808 net_type_data
= True
1809 if net_type_bridge
and net_type_data
:
1810 error_text
= "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface
[0], iface
[1])
1811 #print "nfvo.new_scenario " + error_text
1812 raise NfvoException(error_text
, HTTP_Bad_Request
)
1813 elif net_type_bridge
:
1816 type_
='data' if len(con
)>2 else 'ptp'
1817 net_list
[net_target
]['type'] = type_
1820 error_text
= "Error connection node %s : %s does not match any VNF or interface" % (iface
[0], iface
[1])
1821 #print "nfvo.new_scenario " + error_text
1823 raise NfvoException(error_text
, HTTP_Bad_Request
)
1825 #1.8: Connect to management net all not already connected interfaces of type 'mgmt'
1826 #1.8.1 obtain management net
1827 mgmt_net
= mydb
.get_rows(SELECT
=('uuid','name','description','type','shared'),
1828 FROM
='datacenter_nets', WHERE
={'name':'mgmt'} )
1829 #1.8.2 check all interfaces from all vnfs
1831 add_mgmt_net
= False
1832 for vnf
in vnfs
.values():
1833 for iface
in vnf
['ifaces'].values():
1834 if iface
['type']=='mgmt' and 'net_key' not in iface
:
1835 #iface not connected
1836 iface
['net_key'] = 'mgmt'
1838 if add_mgmt_net
and 'mgmt' not in net_list
:
1839 net_list
['mgmt']=mgmt_net
[0]
1840 net_list
['mgmt']['external']=True
1841 net_list
['mgmt']['graph']={'visible':False}
1843 net_list
.update(other_nets
)
1845 #print 'net_list', net_list
1850 #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
1851 c
= mydb
.new_scenario( { 'vnfs':vnfs
, 'nets':net_list
,
1852 'tenant_id':tenant_id
, 'name':topo
['name'],
1853 'description':topo
.get('description',topo
['name']),
1854 'public': topo
.get('public', False)
1860 def new_scenario_v02(mydb
, tenant_id
, scenario_dict
, version
):
1861 """ This creates a new scenario for version 0.2 and 0.3"""
1862 scenario
= scenario_dict
["scenario"]
1863 if tenant_id
!= "any":
1864 check_tenant(mydb
, tenant_id
)
1865 if "tenant_id" in scenario
:
1866 if scenario
["tenant_id"] != tenant_id
:
1867 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
1868 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
1869 scenario
["tenant_id"], tenant_id
), HTTP_Unauthorized
)
1873 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
1874 for name
,vnf
in scenario
["vnfs"].iteritems():
1876 where_or
={"tenant_id": tenant_id
, 'public': "true"}
1878 error_pos
= "'scenario':'vnfs':'" + name
+ "'"
1880 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
1881 where
['uuid'] = vnf
['vnf_id']
1882 if 'vnf_name' in vnf
:
1883 error_text
+= " 'vnf_name' " + vnf
['vnf_name']
1884 where
['name'] = vnf
['vnf_name']
1886 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos
, HTTP_Bad_Request
)
1887 vnf_db
= mydb
.get_rows(SELECT
=('uuid', 'name', 'description'),
1892 if len(vnf_db
) == 0:
1893 raise NfvoException("Unknown" + error_text
+ " at " + error_pos
, HTTP_Not_Found
)
1894 elif len(vnf_db
) > 1:
1895 raise NfvoException("More than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", HTTP_Conflict
)
1896 vnf
['uuid'] = vnf_db
[0]['uuid']
1897 vnf
['description'] = vnf_db
[0]['description']
1899 # get external interfaces
1900 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
1901 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1902 WHERE
={'vnfs.uuid':vnf
['uuid']}, WHERE_NOT
={'external_name': None} )
1903 for ext_iface
in ext_ifaces
:
1904 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type': ext_iface
['type']}
1905 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
1907 # 2: Insert net_key and ip_address at every vnf interface
1908 for net_name
, net
in scenario
["networks"].items():
1909 net_type_bridge
= False
1910 net_type_data
= False
1911 for iface_dict
in net
["interfaces"]:
1912 if version
== "0.2":
1913 temp_dict
= iface_dict
1915 elif version
== "0.3":
1916 temp_dict
= {iface_dict
["vnf"] : iface_dict
["vnf_interface"]}
1917 ip_address
= iface_dict
.get('ip_address', None)
1918 for vnf
, iface
in temp_dict
.items():
1919 if vnf
not in scenario
["vnfs"]:
1920 error_text
= "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
1922 # logger.debug("nfvo.new_scenario_v02 " + error_text)
1923 raise NfvoException(error_text
, HTTP_Not_Found
)
1924 if iface
not in scenario
["vnfs"][vnf
]['ifaces']:
1925 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
1926 .format(net_name
, iface
)
1927 # logger.debug("nfvo.new_scenario_v02 " + error_text)
1928 raise NfvoException(error_text
, HTTP_Bad_Request
)
1929 if "net_key" in scenario
["vnfs"][vnf
]['ifaces'][iface
]:
1930 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
1931 "'{}'".format(net_name
, iface
,scenario
["vnfs"][vnf
]['ifaces'][iface
]['net_key'])
1932 # logger.debug("nfvo.new_scenario_v02 " + error_text)
1933 raise NfvoException(error_text
, HTTP_Bad_Request
)
1934 scenario
["vnfs"][vnf
]['ifaces'][ iface
]['net_key'] = net_name
1935 scenario
["vnfs"][vnf
]['ifaces'][iface
]['ip_address'] = ip_address
1936 iface_type
= scenario
["vnfs"][vnf
]['ifaces'][iface
]['type']
1937 if iface_type
== 'mgmt' or iface_type
== 'bridge':
1938 net_type_bridge
= True
1940 net_type_data
= True
1942 if net_type_bridge
and net_type_data
:
1943 error_text
= "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
1945 # logger.debug("nfvo.new_scenario " + error_text)
1946 raise NfvoException(error_text
, HTTP_Bad_Request
)
1947 elif net_type_bridge
:
1950 type_
= 'data' if len(net
["interfaces"]) > 2 else 'ptp'
1952 if net
.get("implementation"): # for v0.3
1953 if type_
== "bridge" and net
["implementation"] == "underlay":
1954 error_text
= "Error connecting interfaces of data type to a network declared as 'underlay' at "\
1955 "'network':'{}'".format(net_name
)
1956 # logger.debug(error_text)
1957 raise NfvoException(error_text
, HTTP_Bad_Request
)
1958 elif type_
!= "bridge" and net
["implementation"] == "overlay":
1959 error_text
= "Error connecting interfaces of data type to a network declared as 'overlay' at "\
1960 "'network':'{}'".format(net_name
)
1961 # logger.debug(error_text)
1962 raise NfvoException(error_text
, HTTP_Bad_Request
)
1963 net
.pop("implementation")
1964 if "type" in net
and version
== "0.3": # for v0.3
1965 if type_
== "data" and net
["type"] == "e-line":
1966 error_text
= "Error connecting more than 2 interfaces of data type to a network declared as type "\
1967 "'e-line' at 'network':'{}'".format(net_name
)
1968 # logger.debug(error_text)
1969 raise NfvoException(error_text
, HTTP_Bad_Request
)
1970 elif type_
== "ptp" and net
["type"] == "e-lan":
1974 net
['name'] = net_name
1975 net
['external'] = net
.get('external', False)
1977 # 3: insert at database
1978 scenario
["nets"] = scenario
["networks"]
1979 scenario
['tenant_id'] = tenant_id
1980 scenario_id
= mydb
.new_scenario(scenario
)
1984 def new_nsd_v3(mydb
, tenant_id
, nsd_descriptor
):
1986 Parses an OSM IM nsd_catalog and insert at DB
1989 :param nsd_descriptor:
1990 :return: The list of cretated NSD ids
1993 mynsd
= nsd_catalog
.nsd()
1995 pybindJSONDecoder
.load_ietf_json(nsd_descriptor
, None, None, obj
=mynsd
)
1996 except Exception as e
:
1997 raise NfvoException("Invalid yang descriptor format " + str(e
), HTTP_Bad_Request
)
2001 db_sce_interfaces
= []
2003 db_ip_profiles_index
= 0
2006 for rift_nsd
in mynsd
.nsd_catalog
.nsd
.itervalues():
2007 nsd
= rift_nsd
.get()
2010 scenario_uuid
= str(uuid4())
2011 uuid_list
.append(scenario_uuid
)
2012 nsd_uuid_list
.append(scenario_uuid
)
2014 "uuid": scenario_uuid
,
2015 "osm_id": get_str(nsd
, "id", 255),
2016 "name": get_str(nsd
, "name", 255),
2017 "description": get_str(nsd
, "description", 255),
2018 "tenant_id": tenant_id
,
2019 "vendor": get_str(nsd
, "vendor", 255),
2020 "short_name": get_str(nsd
, "short-name", 255),
2021 "descriptor": str(nsd_descriptor
)[:60000],
2023 db_scenarios
.append(db_scenario
)
2025 # table sce_vnfs (constituent-vnfd)
2026 vnf_index2scevnf_uuid
= {}
2027 vnf_index2vnf_uuid
= {}
2028 for vnf
in nsd
.get("constituent-vnfd").itervalues():
2029 existing_vnf
= mydb
.get_rows(FROM
="vnfs", WHERE
={'osm_id': str(vnf
["vnfd-id-ref"])[:255],
2030 'tenant_id': tenant_id
})
2031 if not existing_vnf
:
2032 raise KeyError("Error at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':'{}' references a "
2033 "non existing VNFD in the catalog".format(str(nsd
["id"]),
2034 str(vnf
["vnfd-id-ref"])[:255]))
2035 sce_vnf_uuid
= str(uuid4())
2036 uuid_list
.append(sce_vnf_uuid
)
2038 "uuid": sce_vnf_uuid
,
2039 "scenario_id": scenario_uuid
,
2040 "name": existing_vnf
[0]["name"][:200] + "." + get_str(vnf
, "member-vnf-index", 5),
2041 "vnf_id": existing_vnf
[0]["uuid"],
2042 "member_vnf_index": int(vnf
["member-vnf-index"]),
2043 # TODO 'start-by-default': True
2045 vnf_index2scevnf_uuid
[int(vnf
['member-vnf-index'])] = sce_vnf_uuid
2046 vnf_index2vnf_uuid
[int(vnf
['member-vnf-index'])] = existing_vnf
[0]["uuid"]
2047 db_sce_vnfs
.append(db_sce_vnf
)
2049 # table ip_profiles (ip-profiles)
2050 ip_profile_name2db_table_index
= {}
2051 for ip_profile
in nsd
.get("ip-profiles").itervalues():
2053 "ip_version": str(ip_profile
["ip-profile-params"].get("ip-version", "ipv4")),
2054 "subnet_address": str(ip_profile
["ip-profile-params"].get("subnet-address")),
2055 "gateway_address": str(ip_profile
["ip-profile-params"].get("gateway-address")),
2056 "dhcp_enabled": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2057 "dhcp_start_address": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("start-address")),
2058 "dhcp_count": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("count")),
2061 for dns
in ip_profile
["ip-profile-params"]["dns-server"].itervalues():
2062 dns_list
.append(str(dns
.get("address")))
2063 db_ip_profile
["dns_address"] = ";".join(dns_list
)
2064 if ip_profile
["ip-profile-params"].get('security-group'):
2065 db_ip_profile
["security_group"] = ip_profile
["ip-profile-params"]['security-group']
2066 ip_profile_name2db_table_index
[str(ip_profile
["name"])] = db_ip_profiles_index
2067 db_ip_profiles_index
+= 1
2068 db_ip_profiles
.append(db_ip_profile
)
2070 # table sce_nets (internal-vld)
2071 for vld
in nsd
.get("vld").itervalues():
2072 sce_net_uuid
= str(uuid4())
2073 uuid_list
.append(sce_net_uuid
)
2075 "uuid": sce_net_uuid
,
2076 "name": get_str(vld
, "name", 255),
2077 "scenario_id": scenario_uuid
,
2079 "multipoint": not vld
.get("type") == "ELINE",
2081 "description": get_str(vld
, "description", 255),
2083 # guess type of network
2084 if vld
.get("mgmt-network"):
2085 db_sce_net
["type"] = "bridge"
2086 db_sce_net
["external"] = True
2087 elif vld
.get("provider-network").get("overlay-type") == "VLAN":
2088 db_sce_net
["type"] = "data"
2090 db_sce_net
["type"] = "bridge"
2091 db_sce_nets
.append(db_sce_net
)
2093 # ip-profile, link db_ip_profile with db_sce_net
2094 if vld
.get("ip-profile-ref"):
2095 ip_profile_name
= vld
.get("ip-profile-ref")
2096 if ip_profile_name
not in ip_profile_name2db_table_index
:
2097 raise KeyError("Error at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}' references a non existing "
2098 "'ip_profiles'".format(
2099 str(nsd
["id"]), str(vld
["id"]), str(vld
["ip-profile-ref"])))
2100 db_ip_profiles
[ip_profile_name2db_table_index
[ip_profile_name
]]["sce_net_id"] = sce_net_uuid
2102 # table sce_interfaces (vld:vnfd-connection-point-ref)
2103 for iface
in vld
.get("vnfd-connection-point-ref").itervalues():
2104 vnf_index
= int(iface
['member-vnf-index-ref'])
2105 # check correct parameters
2106 if vnf_index
not in vnf_index2vnf_uuid
:
2107 raise KeyError("Error at 'nsd[{}]':'vld[{}]':'vnfd-connection-point-ref':'member-vnf-index-ref'"
2108 ":'{}' references a non existing index at 'nsd':'constituent-vnfd'".format(
2109 str(nsd
["id"]), str(vld
["id"]), str(iface
["member-vnf-index-ref"])))
2111 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2112 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2113 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2114 'external_name': get_str(iface
, "vnfd-connection-point-ref",
2116 if not existing_ifaces
:
2117 raise KeyError("Error at 'nsd[{}]':'vld[{}]':'vnfd-connection-point-ref':'vnfd-connection-point"
2118 "-ref':'{}' references a non existing interface at VNFD '{}'".format(
2119 str(nsd
["id"]), str(vld
["id"]), str(iface
["vnfd-connection-point-ref"]),
2120 str(iface
.get("vnfd-id-ref"))[:255]))
2122 interface_uuid
= existing_ifaces
[0]["uuid"]
2123 sce_interface_uuid
= str(uuid4())
2124 uuid_list
.append(sce_net_uuid
)
2125 db_sce_interface
= {
2126 "uuid": sce_interface_uuid
,
2127 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2128 "sce_net_id": sce_net_uuid
,
2129 "interface_id": interface_uuid
,
2130 # "ip_address": #TODO
2132 db_sce_interfaces
.append(db_sce_interface
)
2135 {"scenarios": db_scenarios
},
2136 {"sce_nets": db_sce_nets
},
2137 {"ip_profiles": db_ip_profiles
},
2138 {"sce_vnfs": db_sce_vnfs
},
2139 {"sce_interfaces": db_sce_interfaces
},
2142 logger
.debug("create_vnf Deployment done vnfDict: %s",
2143 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
2144 mydb
.new_rows(db_tables
, uuid_list
)
2145 return nsd_uuid_list
2146 except Exception as e
:
2147 logger
.error("Exception {}".format(e
))
2148 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
2151 def edit_scenario(mydb
, tenant_id
, scenario_id
, data
):
2152 data
["uuid"] = scenario_id
2153 data
["tenant_id"] = tenant_id
2154 c
= mydb
.edit_scenario( data
)
2158 def start_scenario(mydb
, tenant_id
, scenario_id
, instance_scenario_name
, instance_scenario_description
, datacenter
=None,vim_tenant
=None, startvms
=True):
2159 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2160 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
, vim_tenant
=vim_tenant
)
2161 vims
= {datacenter_id
: myvim
}
2162 myvim_tenant
= myvim
['tenant_id']
2163 datacenter_name
= myvim
['name']
2167 #print "Checking that the scenario_id exists and getting the scenario dictionary"
2168 scenarioDict
= mydb
.get_scenario(scenario_id
, tenant_id
, datacenter_id
)
2169 scenarioDict
['datacenter2tenant'] = { datacenter_id
: myvim
['config']['datacenter_tenant_id'] }
2170 scenarioDict
['datacenter_id'] = datacenter_id
2171 #print '================scenarioDict======================='
2172 #print json.dumps(scenarioDict, indent=4)
2173 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
2175 logger
.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict
['name'],len(scenarioDict
['vnfs']))
2176 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2178 auxNetDict
= {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2179 auxNetDict
['scenario'] = {}
2181 logger
.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2182 for sce_net
in scenarioDict
['nets']:
2183 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
2185 myNetName
= "%s.%s" % (instance_scenario_name
, sce_net
['name'])
2186 myNetName
= myNetName
[0:255] #limit length
2187 myNetType
= sce_net
['type']
2189 myNetDict
["name"] = myNetName
2190 myNetDict
["type"] = myNetType
2191 myNetDict
["tenant_id"] = myvim_tenant
2192 myNetIPProfile
= sce_net
.get('ip_profile', None)
2194 #We should use the dictionary as input parameter for new_network
2196 if not sce_net
["external"]:
2197 network_id
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
)
2198 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2199 sce_net
['vim_id'] = network_id
2200 auxNetDict
['scenario'][sce_net
['uuid']] = network_id
2201 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2202 sce_net
["created"] = True
2204 if sce_net
['vim_id'] == None:
2205 error_text
= "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name
, sce_net
['name'])
2206 _
, message
= rollback(mydb
, vims
, rollbackList
)
2207 logger
.error("nfvo.start_scenario: %s", error_text
)
2208 raise NfvoException(error_text
, HTTP_Bad_Request
)
2209 logger
.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict
['name'],sce_net
['vim_id'])
2210 auxNetDict
['scenario'][sce_net
['uuid']] = sce_net
['vim_id']
2212 logger
.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2213 #For each vnf net, we create it and we add it to instanceNetlist.
2215 for sce_vnf
in scenarioDict
['vnfs']:
2216 for net
in sce_vnf
['nets']:
2217 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
2219 myNetName
= "%s.%s" % (instance_scenario_name
,net
['name'])
2220 myNetName
= myNetName
[0:255] #limit length
2221 myNetType
= net
['type']
2223 myNetDict
["name"] = myNetName
2224 myNetDict
["type"] = myNetType
2225 myNetDict
["tenant_id"] = myvim_tenant
2226 myNetIPProfile
= net
.get('ip_profile', None)
2229 #We should use the dictionary as input parameter for new_network
2230 network_id
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
)
2231 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2232 net
['vim_id'] = network_id
2233 if sce_vnf
['uuid'] not in auxNetDict
:
2234 auxNetDict
[sce_vnf
['uuid']] = {}
2235 auxNetDict
[sce_vnf
['uuid']][net
['uuid']] = network_id
2236 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2237 net
["created"] = True
2239 #print "auxNetDict:"
2240 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
2242 logger
.debug("start_scenario 3. Creating new vm instances in the VIM")
2243 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2245 for sce_vnf
in scenarioDict
['vnfs']:
2246 vnf_availability_zones
= []
2247 for vm
in sce_vnf
['vms']:
2248 vm_av
= vm
.get('availability_zone')
2249 if vm_av
and vm_av
not in vnf_availability_zones
:
2250 vnf_availability_zones
.append(vm_av
)
2252 # check if there is enough availability zones available at vim level.
2253 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
2254 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
2255 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request
)
2257 for vm
in sce_vnf
['vms']:
2260 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
2261 myVMDict
['name'] = "{}.{}.{}".format(instance_scenario_name
,sce_vnf
['name'],chr(96+i
))
2262 #myVMDict['description'] = vm['description']
2263 myVMDict
['description'] = myVMDict
['name'][0:99]
2265 myVMDict
['start'] = "no"
2266 myVMDict
['name'] = myVMDict
['name'][0:255] #limit name length
2267 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
2269 #create image at vim in case it not exist
2270 image_dict
= mydb
.get_table_by_uuid_name("images", vm
['image_id'])
2271 image_id
= create_or_use_image(mydb
, vims
, image_dict
, [], True)
2272 vm
['vim_image_id'] = image_id
2274 #create flavor at vim in case it not exist
2275 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
2276 if flavor_dict
['extended']!=None:
2277 flavor_dict
['extended']= yaml
.load(flavor_dict
['extended'])
2278 flavor_id
= create_or_use_flavor(mydb
, vims
, flavor_dict
, [], True)
2279 vm
['vim_flavor_id'] = flavor_id
2282 myVMDict
['imageRef'] = vm
['vim_image_id']
2283 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
2284 myVMDict
['networks'] = []
2285 for iface
in vm
['interfaces']:
2287 if iface
['type']=="data":
2288 netDict
['type'] = iface
['model']
2289 elif "model" in iface
and iface
["model"]!=None:
2290 netDict
['model']=iface
['model']
2291 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2292 #discover type of interface looking at flavor
2293 for numa
in flavor_dict
.get('extended',{}).get('numas',[]):
2294 for flavor_iface
in numa
.get('interfaces',[]):
2295 if flavor_iface
.get('name') == iface
['internal_name']:
2296 if flavor_iface
['dedicated'] == 'yes':
2297 netDict
['type']="PF" #passthrough
2298 elif flavor_iface
['dedicated'] == 'no':
2299 netDict
['type']="VF" #siov
2300 elif flavor_iface
['dedicated'] == 'yes:sriov':
2301 netDict
['type']="VFnotShared" #sriov but only one sriov on the PF
2302 netDict
["mac_address"] = flavor_iface
.get("mac_address")
2304 netDict
["use"]=iface
['type']
2305 if netDict
["use"]=="data" and not netDict
.get("type"):
2306 #print "netDict", netDict
2307 #print "iface", iface
2308 e_text
= "Cannot determine the interface type PF or VF of VNF '%s' VM '%s' iface '%s'" %(sce_vnf
['name'], vm
['name'], iface
['internal_name'])
2309 if flavor_dict
.get('extended')==None:
2310 raise NfvoException(e_text
+ "After database migration some information is not available. \
2311 Try to delete and create the scenarios and VNFs again", HTTP_Conflict
)
2313 raise NfvoException(e_text
, HTTP_Internal_Server_Error
)
2314 if netDict
["use"]=="mgmt" or netDict
["use"]=="bridge":
2315 netDict
["type"]="virtual"
2316 if "vpci" in iface
and iface
["vpci"] is not None:
2317 netDict
['vpci'] = iface
['vpci']
2318 if "mac" in iface
and iface
["mac"] is not None:
2319 netDict
['mac_address'] = iface
['mac']
2320 if "port-security" in iface
and iface
["port-security"] is not None:
2321 netDict
['port_security'] = iface
['port-security']
2322 if "floating-ip" in iface
and iface
["floating-ip"] is not None:
2323 netDict
['floating_ip'] = iface
['floating-ip']
2324 netDict
['name'] = iface
['internal_name']
2325 if iface
['net_id'] is None:
2326 for vnf_iface
in sce_vnf
["interfaces"]:
2329 if vnf_iface
['interface_id']==iface
['uuid']:
2330 netDict
['net_id'] = auxNetDict
['scenario'][ vnf_iface
['sce_net_id'] ]
2333 netDict
['net_id'] = auxNetDict
[ sce_vnf
['uuid'] ][ iface
['net_id'] ]
2334 #skip bridge ifaces not connected to any net
2335 #if 'net_id' not in netDict or netDict['net_id']==None:
2337 myVMDict
['networks'].append(netDict
)
2338 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2339 #print myVMDict['name']
2340 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2341 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2342 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2344 if 'availability_zone' in myVMDict
:
2345 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
2349 vm_id
= myvim
.new_vminstance(myVMDict
['name'], myVMDict
['description'], myVMDict
.get('start', None),
2350 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'],
2351 availability_zone_index
=av_index
,
2352 availability_zone_list
=vnf_availability_zones
)
2353 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2354 vm
['vim_id'] = vm_id
2355 rollbackList
.append({'what':'vm','where':'vim','vim_id':datacenter_id
,'uuid':vm_id
})
2356 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2357 for net
in myVMDict
['networks']:
2359 for iface
in vm
['interfaces']:
2360 if net
["name"]==iface
["internal_name"]:
2361 iface
["vim_id"]=net
["vim_id"]
2364 logger
.debug("start scenario Deployment done")
2365 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2366 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
2367 instance_id
= mydb
.new_instance_scenario_as_a_whole(tenant_id
,instance_scenario_name
, instance_scenario_description
, scenarioDict
)
2368 return mydb
.get_instance_scenario(instance_id
)
2370 except (db_base_Exception
, vimconn
.vimconnException
) as e
:
2371 _
, message
= rollback(mydb
, vims
, rollbackList
)
2372 if isinstance(e
, db_base_Exception
):
2373 error_text
= "Exception at database"
2375 error_text
= "Exception at VIM"
2376 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
2377 #logger.error("start_scenario %s", error_text)
2378 raise NfvoException(error_text
, e
.http_code
)
2381 def unify_cloud_config(cloud_config_preserve
, cloud_config
):
2382 """ join the cloud config information into cloud_config_preserve.
2383 In case of conflict cloud_config_preserve preserves
2386 if not cloud_config_preserve
and not cloud_config
:
2389 new_cloud_config
= {"key-pairs":[], "users":[]}
2391 if cloud_config_preserve
:
2392 for key
in cloud_config_preserve
.get("key-pairs", () ):
2393 if key
not in new_cloud_config
["key-pairs"]:
2394 new_cloud_config
["key-pairs"].append(key
)
2396 for key
in cloud_config
.get("key-pairs", () ):
2397 if key
not in new_cloud_config
["key-pairs"]:
2398 new_cloud_config
["key-pairs"].append(key
)
2399 if not new_cloud_config
["key-pairs"]:
2400 del new_cloud_config
["key-pairs"]
2404 new_cloud_config
["users"] += cloud_config
.get("users", () )
2405 if cloud_config_preserve
:
2406 new_cloud_config
["users"] += cloud_config_preserve
.get("users", () )
2407 index_to_delete
= []
2408 users
= new_cloud_config
.get("users", [])
2409 for index0
in range(0,len(users
)):
2410 if index0
in index_to_delete
:
2412 for index1
in range(index0
+1,len(users
)):
2413 if index1
in index_to_delete
:
2415 if users
[index0
]["name"] == users
[index1
]["name"]:
2416 index_to_delete
.append(index1
)
2417 for key
in users
[index1
].get("key-pairs",()):
2418 if "key-pairs" not in users
[index0
]:
2419 users
[index0
]["key-pairs"] = [key
]
2420 elif key
not in users
[index0
]["key-pairs"]:
2421 users
[index0
]["key-pairs"].append(key
)
2422 index_to_delete
.sort(reverse
=True)
2423 for index
in index_to_delete
:
2425 if not new_cloud_config
["users"]:
2426 del new_cloud_config
["users"]
2429 if cloud_config
and cloud_config
.get("boot-data-drive") != None:
2430 new_cloud_config
["boot-data-drive"] = cloud_config
["boot-data-drive"]
2431 if cloud_config_preserve
and cloud_config_preserve
.get("boot-data-drive") != None:
2432 new_cloud_config
["boot-data-drive"] = cloud_config_preserve
["boot-data-drive"]
2435 new_cloud_config
["user-data"] = []
2436 if cloud_config
and cloud_config
.get("user-data"):
2437 if isinstance(cloud_config
["user-data"], list):
2438 new_cloud_config
["user-data"] += cloud_config
["user-data"]
2440 new_cloud_config
["user-data"].append(cloud_config
["user-data"])
2441 if cloud_config_preserve
and cloud_config_preserve
.get("user-data"):
2442 if isinstance(cloud_config_preserve
["user-data"], list):
2443 new_cloud_config
["user-data"] += cloud_config_preserve
["user-data"]
2445 new_cloud_config
["user-data"].append(cloud_config_preserve
["user-data"])
2446 if not new_cloud_config
["user-data"]:
2447 del new_cloud_config
["user-data"]
2450 new_cloud_config
["config-files"] = []
2451 if cloud_config
and cloud_config
.get("config-files") != None:
2452 new_cloud_config
["config-files"] += cloud_config
["config-files"]
2453 if cloud_config_preserve
:
2454 for file in cloud_config_preserve
.get("config-files", ()):
2455 for index
in range(0, len(new_cloud_config
["config-files"])):
2456 if new_cloud_config
["config-files"][index
]["dest"] == file["dest"]:
2457 new_cloud_config
["config-files"][index
] = file
2460 new_cloud_config
["config-files"].append(file)
2461 if not new_cloud_config
["config-files"]:
2462 del new_cloud_config
["config-files"]
2463 return new_cloud_config
2466 def get_vim_thread(mydb
, tenant_id
, datacenter_id_name
=None, datacenter_tenant_id
=None):
2467 datacenter_id
= None
2468 datacenter_name
= None
2471 if datacenter_tenant_id
:
2472 thread_id
= datacenter_tenant_id
2473 thread
= vim_threads
["running"].get(datacenter_tenant_id
)
2475 where_
={"td.nfvo_tenant_id": tenant_id
}
2476 if datacenter_id_name
:
2477 if utils
.check_valid_uuid(datacenter_id_name
):
2478 datacenter_id
= datacenter_id_name
2479 where_
["dt.datacenter_id"] = datacenter_id
2481 datacenter_name
= datacenter_id_name
2482 where_
["d.name"] = datacenter_name
2483 if datacenter_tenant_id
:
2484 where_
["dt.uuid"] = datacenter_tenant_id
2485 datacenters
= mydb
.get_rows(
2486 SELECT
=("dt.uuid as datacenter_tenant_id",),
2487 FROM
="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2488 "join datacenters as d on d.uuid=dt.datacenter_id",
2490 if len(datacenters
) > 1:
2491 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
2493 thread_id
= datacenters
[0]["datacenter_tenant_id"]
2494 thread
= vim_threads
["running"].get(thread_id
)
2496 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), HTTP_Not_Found
)
2497 return thread_id
, thread
2498 except db_base_Exception
as e
:
2499 raise NfvoException("{} {}".format(type(e
).__name
__ , str(e
)), e
.http_code
)
2502 def get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter_id_name
=None, **extra_filter
):
2503 datacenter_id
= None
2504 datacenter_name
= None
2505 if datacenter_id_name
:
2506 if utils
.check_valid_uuid(datacenter_id_name
):
2507 datacenter_id
= datacenter_id_name
2509 datacenter_name
= datacenter_id_name
2510 vims
= get_vim(mydb
, tenant_id
, datacenter_id
, datacenter_name
, **extra_filter
)
2512 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), HTTP_Not_Found
)
2514 #print "nfvo.datacenter_action() error. Several datacenters found"
2515 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
2516 return vims
.keys()[0], vims
.values()[0]
2520 '''Takes dict d and updates it with the values in dict u.'''
2521 '''It merges all depth levels'''
2522 for k
, v
in u
.iteritems():
2523 if isinstance(v
, collections
.Mapping
):
2524 r
= update(d
.get(k
, {}), v
)
2531 def create_instance(mydb
, tenant_id
, instance_dict
):
2532 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2533 # logger.debug("Creating instance...")
2534 scenario
= instance_dict
["scenario"]
2536 #find main datacenter
2538 myvim_threads_id
= {}
2541 datacenter
= instance_dict
.get("datacenter")
2542 default_datacenter_id
, vim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
2543 myvims
[default_datacenter_id
] = vim
2544 myvim_threads_id
[default_datacenter_id
], _
= get_vim_thread(mydb
, tenant_id
, default_datacenter_id
)
2545 tasks_to_launch
[myvim_threads_id
[default_datacenter_id
]] = []
2546 #myvim_tenant = myvim['tenant_id']
2547 # default_datacenter_name = vim['name']
2550 #print "Checking that the scenario exists and getting the scenario dictionary"
2551 scenarioDict
= mydb
.get_scenario(scenario
, tenant_id
, default_datacenter_id
)
2553 #logger.debug(">>>>>>> Dictionaries before merging")
2554 #logger.debug(">>>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
2555 #logger.debug(">>>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
2558 instance_name
= instance_dict
["name"]
2559 instance_uuid
= str(uuid4())
2560 uuid_list
.append(instance_uuid
)
2561 db_instance_scenario
= {
2562 "uuid": instance_uuid
,
2563 "name": instance_name
,
2564 "tenant_id": tenant_id
,
2565 "scenario_id": scenarioDict
['uuid'],
2566 "datacenter_id": default_datacenter_id
,
2567 # filled bellow 'datacenter_tenant_id'
2568 "description": instance_dict
.get("description"),
2571 if scenarioDict
.get("cloud-config"):
2572 db_instance_scenario
["cloud_config"] = yaml
.safe_dump(scenarioDict
["cloud-config"],
2573 default_flow_style
=True, width
=256)
2575 vnf_net2instance
= {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2576 sce_net2instance
= {}
2577 auxNetDict
= {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2578 auxNetDict
['scenario'] = {}
2580 logger
.debug("Creating instance from scenario-dict:\n%s", yaml
.safe_dump(scenarioDict
, indent
=4, default_flow_style
=False)) #TODO remove
2582 # 0 check correct parameters
2583 for net_name
, net_instance_desc
in instance_dict
.get("networks",{}).iteritems():
2585 for scenario_net
in scenarioDict
['nets']:
2586 if net_name
== scenario_net
["name"]:
2590 raise NfvoException("Invalid scenario network name '{}' at instance:networks".format(net_name
), HTTP_Bad_Request
)
2591 if "sites" not in net_instance_desc
:
2592 net_instance_desc
["sites"] = [ {} ]
2593 site_without_datacenter_field
= False
2594 for site
in net_instance_desc
["sites"]:
2595 if site
.get("datacenter"):
2596 if site
["datacenter"] not in myvims
:
2597 #Add this datacenter to myvims
2598 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, site
["datacenter"])
2600 myvim_threads_id
[d
],_
= get_vim_thread(mydb
, tenant_id
, site
["datacenter"])
2601 tasks_to_launch
[myvim_threads_id
[d
]] = []
2602 site
["datacenter"] = d
#change name to id
2604 if site_without_datacenter_field
:
2605 raise NfvoException("Found more than one entries without datacenter field at instance:networks:{}:sites".format(net_name
), HTTP_Bad_Request
)
2606 site_without_datacenter_field
= True
2607 site
["datacenter"] = default_datacenter_id
#change name to id
2609 for vnf_name
, vnf_instance_desc
in instance_dict
.get("vnfs",{}).iteritems():
2611 for scenario_vnf
in scenarioDict
['vnfs']:
2612 if vnf_name
== scenario_vnf
['name']:
2616 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_instance_desc
), HTTP_Bad_Request
)
2617 if "datacenter" in vnf_instance_desc
:
2618 # Add this datacenter to myvims
2619 if vnf_instance_desc
["datacenter"] not in myvims
:
2620 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
2622 myvim_threads_id
[d
],_
= get_vim_thread(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
2623 tasks_to_launch
[myvim_threads_id
[d
]] = []
2624 scenario_vnf
["datacenter"] = vnf_instance_desc
["datacenter"]
2626 #0.1 parse cloud-config parameters
2627 cloud_config
= unify_cloud_config(instance_dict
.get("cloud-config"), scenarioDict
.get("cloud-config"))
2629 #0.2 merge instance information into scenario
2630 #Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
2631 #However, this is not possible yet.
2632 for net_name
, net_instance_desc
in instance_dict
.get("networks",{}).iteritems():
2633 for scenario_net
in scenarioDict
['nets']:
2634 if net_name
== scenario_net
["name"]:
2635 if 'ip-profile' in net_instance_desc
:
2636 # translate from input format to database format
2637 ipprofile_in
= net_instance_desc
['ip-profile']
2639 ipprofile_db
['subnet_address'] = ipprofile_in
.get('subnet-address')
2640 ipprofile_db
['ip_version'] = ipprofile_in
.get('ip-version', 'IPv4')
2641 ipprofile_db
['gateway_address'] = ipprofile_in
.get('gateway-address')
2642 ipprofile_db
['dns_address'] = ipprofile_in
.get('dns-address')
2643 if isinstance(ipprofile_db
['dns_address'], (list, tuple)):
2644 ipprofile_db
['dns_address'] = ";".join(ipprofile_db
['dns_address'])
2645 if 'dhcp' in ipprofile_in
:
2646 ipprofile_db
['dhcp_start_address'] = ipprofile_in
['dhcp'].get('start-address')
2647 ipprofile_db
['dhcp_enabled'] = ipprofile_in
['dhcp'].get('enabled', True)
2648 ipprofile_db
['dhcp_count'] = ipprofile_in
['dhcp'].get('count' )
2649 if 'ip_profile' not in scenario_net
:
2650 scenario_net
['ip_profile'] = ipprofile_db
2652 update(scenario_net
['ip_profile'], ipprofile_db
)
2653 for interface
in net_instance_desc
.get('interfaces', () ):
2654 if 'ip_address' in interface
:
2655 for vnf
in scenarioDict
['vnfs']:
2656 if interface
['vnf'] == vnf
['name']:
2657 for vnf_interface
in vnf
['interfaces']:
2658 if interface
['vnf_interface'] == vnf_interface
['external_name']:
2659 vnf_interface
['ip_address']=interface
['ip_address']
2661 #logger.debug(">>>>>>>> Merged dictionary")
2662 logger
.debug("Creating instance scenario-dict MERGED:\n%s", yaml
.safe_dump(scenarioDict
, indent
=4, default_flow_style
=False))
2664 # 1. Creating new nets (sce_nets) in the VIM"
2665 db_instance_nets
= []
2666 for sce_net
in scenarioDict
['nets']:
2667 descriptor_net
= instance_dict
.get("networks",{}).get(sce_net
["name"],{})
2668 net_name
= descriptor_net
.get("vim-network-name")
2669 sce_net2instance
[sce_net
['uuid']] = {}
2670 auxNetDict
['scenario'][sce_net
['uuid']] = {}
2672 sites
= descriptor_net
.get("sites", [ {} ])
2674 if site
.get("datacenter"):
2675 vim
= myvims
[ site
["datacenter"] ]
2676 datacenter_id
= site
["datacenter"]
2677 myvim_thread_id
= myvim_threads_id
[ site
["datacenter"] ]
2679 vim
= myvims
[ default_datacenter_id
]
2680 datacenter_id
= default_datacenter_id
2681 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
2682 net_type
= sce_net
['type']
2683 lookfor_filter
= {'admin_state_up': True, 'status': 'ACTIVE'} #'shared': True
2686 if sce_net
["external"]:
2687 net_name
= sce_net
["name"]
2689 net_name
= "{}.{}".format(instance_name
, sce_net
["name"])
2690 net_name
= net_name
[:255] # limit length
2692 if "netmap-use" in site
or "netmap-create" in site
:
2693 create_network
= False
2694 lookfor_network
= False
2695 if "netmap-use" in site
:
2696 lookfor_network
= True
2697 if utils
.check_valid_uuid(site
["netmap-use"]):
2698 filter_text
= "scenario id '%s'" % site
["netmap-use"]
2699 lookfor_filter
["id"] = site
["netmap-use"]
2701 filter_text
= "scenario name '%s'" % site
["netmap-use"]
2702 lookfor_filter
["name"] = site
["netmap-use"]
2703 if "netmap-create" in site
:
2704 create_network
= True
2705 net_vim_name
= net_name
2706 if site
["netmap-create"]:
2707 net_vim_name
= site
["netmap-create"]
2708 elif sce_net
["external"]:
2709 if sce_net
['vim_id'] != None:
2710 #there is a netmap at datacenter_nets database #TODO REVISE!!!!
2711 create_network
= False
2712 lookfor_network
= True
2713 lookfor_filter
["id"] = sce_net
['vim_id']
2714 filter_text
= "vim_id '%s' datacenter_netmap name '%s'. Try to reload vims with datacenter-net-update" % (sce_net
['vim_id'], sce_net
["name"])
2715 #look for network at datacenter and return error
2717 #There is not a netmap, look at datacenter for a net with this name and create if not found
2718 create_network
= True
2719 lookfor_network
= True
2720 lookfor_filter
["name"] = sce_net
["name"]
2721 net_vim_name
= sce_net
["name"]
2722 filter_text
= "scenario name '%s'" % sce_net
["name"]
2724 net_vim_name
= net_name
2725 create_network
= True
2726 lookfor_network
= False
2731 vim_nets
= vim
.get_network_list(filter_dict
=lookfor_filter
)
2732 if len(vim_nets
) > 1:
2733 raise NfvoException("More than one candidate VIM network found for " + filter_text
, HTTP_Bad_Request
)
2734 elif len(vim_nets
) == 0:
2735 if not create_network
:
2736 raise NfvoException("No candidate VIM network found for " + filter_text
, HTTP_Bad_Request
)
2738 vim_id
= vim_nets
[0]['id']
2739 auxNetDict
['scenario'][sce_net
['uuid']][datacenter_id
] = vim_nets
[0]['id']
2740 create_network
= False
2742 #if network is not external
2743 task
= new_task("new-net", (net_vim_name
, net_type
, sce_net
.get('ip_profile',None)))
2744 task_id
= task
["id"]
2745 instance_tasks
[task_id
] = task
2746 tasks_to_launch
[myvim_thread_id
].append(task
)
2747 #network_id = vim.new_network(net_vim_name, net_type, sce_net.get('ip_profile',None))
2749 auxNetDict
['scenario'][sce_net
['uuid']][datacenter_id
] = task_id
2750 rollbackList
.append({'what':'network', 'where':'vim', 'vim_id':datacenter_id
, 'uuid':task_id
})
2751 sce_net
["created"] = True
2753 # fill database content
2754 net_uuid
= str(uuid4())
2755 uuid_list
.append(net_uuid
)
2756 sce_net2instance
[sce_net
['uuid']][datacenter_id
] = net_uuid
2759 'vim_net_id': vim_id
,
2760 "instance_scenario_id": instance_uuid
,
2761 "sce_net_id": sce_net
["uuid"],
2762 "created": create_network
,
2763 'datacenter_id': datacenter_id
,
2764 'datacenter_tenant_id': myvim_thread_id
,
2765 'status': 'BUILD' if create_network
else "ACTIVE"
2767 db_instance_nets
.append(db_net
)
2768 if 'ip_profile' in sce_net
:
2770 'instance_net_id': net_uuid
,
2771 'ip_version': sce_net
['ip_profile']['ip_version'],
2772 'subnet_address': sce_net
['ip_profile']['subnet_address'],
2773 'gateway_address': sce_net
['ip_profile']['gateway_address'],
2774 'dns_address': sce_net
['ip_profile']['dns_address'],
2775 'dhcp_enabled': sce_net
['ip_profile']['dhcp_enabled'],
2776 'dhcp_start_address': sce_net
['ip_profile']['dhcp_start_address'],
2777 'dhcp_count': sce_net
['ip_profile']['dhcp_count'],
2779 db_ip_profiles
.append(db_ip_profile
)
2781 # 2. Creating new nets (vnf internal nets) in the VIM"
2782 # For each vnf net, we create it and we add it to instanceNetlist.
2783 for sce_vnf
in scenarioDict
['vnfs']:
2784 for net
in sce_vnf
['nets']:
2785 if sce_vnf
.get("datacenter"):
2786 vim
= myvims
[ sce_vnf
["datacenter"] ]
2787 datacenter_id
= sce_vnf
["datacenter"]
2788 myvim_thread_id
= myvim_threads_id
[ sce_vnf
["datacenter"]]
2790 vim
= myvims
[ default_datacenter_id
]
2791 datacenter_id
= default_datacenter_id
2792 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
2793 descriptor_net
= instance_dict
.get("vnfs",{}).get(sce_vnf
["name"],{})
2794 net_name
= descriptor_net
.get("name")
2796 net_name
= "%s.%s" %(instance_name
, net
["name"])
2797 net_name
= net_name
[:255] #limit length
2798 net_type
= net
['type']
2799 task
= new_task("new-net", (net_name
, net_type
, net
.get('ip_profile',None)))
2800 task_id
= task
["id"]
2801 instance_tasks
[task_id
] = task
2802 tasks_to_launch
[myvim_thread_id
].append(task
)
2803 # network_id = vim.new_network(net_name, net_type, net.get('ip_profile',None))
2805 if sce_vnf
['uuid'] not in vnf_net2instance
:
2806 vnf_net2instance
[sce_vnf
['uuid']] = {}
2807 vnf_net2instance
[sce_vnf
['uuid']][net
['uuid']] = task_id
2808 if sce_vnf
['uuid'] not in auxNetDict
:
2809 auxNetDict
[sce_vnf
['uuid']] = {}
2810 auxNetDict
[sce_vnf
['uuid']][net
['uuid']] = task_id
2811 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':task_id
})
2812 net
["created"] = True
2814 # fill database content
2815 net_uuid
= str(uuid4())
2816 uuid_list
.append(net_uuid
)
2817 vnf_net2instance
[sce_vnf
['uuid']][net
['uuid']] = net_uuid
2820 'vim_net_id': vim_id
,
2821 "instance_scenario_id": instance_uuid
,
2822 "net_id": net
["uuid"],
2824 'datacenter_id': datacenter_id
,
2825 'datacenter_tenant_id': myvim_thread_id
,
2827 db_instance_nets
.append(db_net
)
2828 if 'ip_profile' in net
:
2830 'instance_net_id': net_uuid
,
2831 'ip_version': net
['ip_profile']['ip_version'],
2832 'subnet_address': net
['ip_profile']['subnet_address'],
2833 'gateway_address': net
['ip_profile']['gateway_address'],
2834 'dns_address': net
['ip_profile']['dns_address'],
2835 'dhcp_enabled': net
['ip_profile']['dhcp_enabled'],
2836 'dhcp_start_address': net
['ip_profile']['dhcp_start_address'],
2837 'dhcp_count': net
['ip_profile']['dhcp_count'],
2839 db_ip_profiles
.append(db_ip_profile
)
2841 #print "vnf_net2instance:"
2842 #print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
2844 # 3. Creating new vm instances in the VIM
2845 db_instance_vnfs
= []
2846 db_instance_vms
= []
2847 db_instance_interfaces
= []
2848 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2849 sce_vnf_list
= sorted(scenarioDict
['vnfs'], key
=lambda k
: k
['name'])
2850 #for sce_vnf in scenarioDict['vnfs']:
2851 for sce_vnf
in sce_vnf_list
:
2852 vnf_availability_zones
= []
2853 for vm
in sce_vnf
['vms']:
2854 vm_av
= vm
.get('availability_zone')
2855 if vm_av
and vm_av
not in vnf_availability_zones
:
2856 vnf_availability_zones
.append(vm_av
)
2858 # check if there is enough availability zones available at vim level.
2859 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
2860 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
2861 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request
)
2863 if sce_vnf
.get("datacenter"):
2864 vim
= myvims
[ sce_vnf
["datacenter"] ]
2865 myvim_thread_id
= myvim_threads_id
[ sce_vnf
["datacenter"] ]
2866 datacenter_id
= sce_vnf
["datacenter"]
2868 vim
= myvims
[ default_datacenter_id
]
2869 myvim_thread_id
= myvim_threads_id
[ default_datacenter_id
]
2870 datacenter_id
= default_datacenter_id
2871 sce_vnf
["datacenter_id"] = datacenter_id
2874 vnf_uuid
= str(uuid4())
2875 uuid_list
.append(vnf_uuid
)
2878 'instance_scenario_id': instance_uuid
,
2879 'vnf_id': sce_vnf
['vnf_id'],
2880 'sce_vnf_id': sce_vnf
['uuid'],
2881 'datacenter_id': datacenter_id
,
2882 'datacenter_tenant_id': myvim_thread_id
,
2884 db_instance_vnfs
.append(db_instance_vnf
)
2886 for vm
in sce_vnf
['vms']:
2888 myVMDict
['name'] = "{}.{}.{}".format(instance_name
[:64], sce_vnf
['name'][:64], vm
["name"][:64])
2889 myVMDict
['description'] = myVMDict
['name'][0:99]
2891 # myVMDict['start'] = "no"
2892 myVMDict
['name'] = myVMDict
['name'][0:255] #limit name length
2893 #create image at vim in case it not exist
2894 image_dict
= mydb
.get_table_by_uuid_name("images", vm
['image_id'])
2895 image_id
= create_or_use_image(mydb
, {datacenter_id
: vim
}, image_dict
, [], True)
2896 vm
['vim_image_id'] = image_id
2898 #create flavor at vim in case it not exist
2899 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
2900 if flavor_dict
['extended']!=None:
2901 flavor_dict
['extended']= yaml
.load(flavor_dict
['extended'])
2902 flavor_id
= create_or_use_flavor(mydb
, {datacenter_id
: vim
}, flavor_dict
, rollbackList
, True)
2904 #Obtain information for additional disks
2905 extended_flavor_dict
= mydb
.get_rows(FROM
='datacenters_flavors', SELECT
=('extended',), WHERE
={'vim_id': flavor_id
})
2906 if not extended_flavor_dict
:
2907 raise NfvoException("flavor '{}' not found".format(flavor_id
), HTTP_Not_Found
)
2910 #extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
2911 myVMDict
['disks'] = None
2912 extended_info
= extended_flavor_dict
[0]['extended']
2913 if extended_info
!= None:
2914 extended_flavor_dict_yaml
= yaml
.load(extended_info
)
2915 if 'disks' in extended_flavor_dict_yaml
:
2916 myVMDict
['disks'] = extended_flavor_dict_yaml
['disks']
2918 vm
['vim_flavor_id'] = flavor_id
2919 myVMDict
['imageRef'] = vm
['vim_image_id']
2920 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
2921 myVMDict
['availability_zone'] = vm
.get('availability_zone')
2922 myVMDict
['networks'] = []
2924 #TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
2926 for iface
in vm
['interfaces']:
2928 if iface
['type']=="data":
2929 netDict
['type'] = iface
['model']
2930 elif "model" in iface
and iface
["model"]!=None:
2931 netDict
['model']=iface
['model']
2932 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2933 #discover type of interface looking at flavor
2934 for numa
in flavor_dict
.get('extended',{}).get('numas',[]):
2935 for flavor_iface
in numa
.get('interfaces',[]):
2936 if flavor_iface
.get('name') == iface
['internal_name']:
2937 if flavor_iface
['dedicated'] == 'yes':
2938 netDict
['type']="PF" #passthrough
2939 elif flavor_iface
['dedicated'] == 'no':
2940 netDict
['type']="VF" #siov
2941 elif flavor_iface
['dedicated'] == 'yes:sriov':
2942 netDict
['type']="VFnotShared" #sriov but only one sriov on the PF
2943 netDict
["mac_address"] = flavor_iface
.get("mac_address")
2945 netDict
["use"]=iface
['type']
2946 if netDict
["use"]=="data" and not netDict
.get("type"):
2947 #print "netDict", netDict
2948 #print "iface", iface
2949 e_text
= "Cannot determine the interface type PF or VF of VNF '%s' VM '%s' iface '%s'" %(sce_vnf
['name'], vm
['name'], iface
['internal_name'])
2950 if flavor_dict
.get('extended')==None:
2951 raise NfvoException(e_text
+ "After database migration some information is not available. \
2952 Try to delete and create the scenarios and VNFs again", HTTP_Conflict
)
2954 raise NfvoException(e_text
, HTTP_Internal_Server_Error
)
2955 if netDict
["use"]=="mgmt" or netDict
["use"]=="bridge":
2956 netDict
["type"]="virtual"
2957 if "vpci" in iface
and iface
["vpci"] is not None:
2958 netDict
['vpci'] = iface
['vpci']
2959 if "mac" in iface
and iface
["mac"] is not None:
2960 netDict
['mac_address'] = iface
['mac']
2961 if "port-security" in iface
and iface
["port-security"] is not None:
2962 netDict
['port_security'] = iface
['port-security']
2963 if "floating-ip" in iface
and iface
["floating-ip"] is not None:
2964 netDict
['floating_ip'] = iface
['floating-ip']
2965 netDict
['name'] = iface
['internal_name']
2966 if iface
['net_id'] is None:
2967 for vnf_iface
in sce_vnf
["interfaces"]:
2970 if vnf_iface
['interface_id']==iface
['uuid']:
2971 netDict
['net_id'] = auxNetDict
['scenario'][ vnf_iface
['sce_net_id'] ][datacenter_id
]
2972 instance_net_id
= sce_net2instance
[ vnf_iface
['sce_net_id'] ][datacenter_id
]
2975 netDict
['net_id'] = auxNetDict
[ sce_vnf
['uuid'] ][ iface
['net_id'] ]
2976 instance_net_id
= vnf_net2instance
[ sce_vnf
['uuid'] ][ iface
['net_id'] ]
2977 if netDict
.get('net_id') and is_task_id(netDict
['net_id']):
2978 task_depends
[netDict
['net_id']] = instance_tasks
[netDict
['net_id']]
2979 #skip bridge ifaces not connected to any net
2980 #if 'net_id' not in netDict or netDict['net_id']==None:
2982 myVMDict
['networks'].append(netDict
)
2985 # 'instance_vm_id': instance_vm_uuid,
2986 "instance_net_id": instance_net_id
,
2987 'interface_id': iface
['uuid'],
2988 # 'vim_interface_id': ,
2989 'type': 'external' if iface
['external_name'] is not None else 'internal',
2990 'ip_address': iface
.get('ip_address'),
2991 'floating_ip': int(iface
.get('floating-ip', False)),
2992 'port_security': int(iface
.get('port-security', True))
2994 db_vm_ifaces
.append(db_vm_iface
)
2995 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2996 # print myVMDict['name']
2997 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2998 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2999 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3000 if vm
.get("boot_data"):
3001 cloud_config_vm
= unify_cloud_config(vm
["boot_data"], cloud_config
)
3003 cloud_config_vm
= cloud_config
3004 if myVMDict
.get('availability_zone'):
3005 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
3008 for vm_index
in range(0, vm
.get('count', 1)):
3010 if vm
.get('count', 1) > 1:
3011 vm_index_name
+= "." + chr(97 + vm_index
)
3012 task
= new_task("new-vm", (myVMDict
['name']+vm_index_name
, myVMDict
['description'],
3013 myVMDict
.get('start', None), myVMDict
['imageRef'],
3014 myVMDict
['flavorRef'], myVMDict
['networks'],
3015 cloud_config_vm
, myVMDict
['disks'], av_index
,
3016 vnf_availability_zones
), depends
=task_depends
)
3017 instance_tasks
[task
["id"]] = task
3018 tasks_to_launch
[myvim_thread_id
].append(task
)
3020 vm
['vim_id'] = vm_id
3021 rollbackList
.append({'what':'vm','where':'vim','vim_id':datacenter_id
,'uuid':vm_id
})
3022 # put interface uuid back to scenario[vnfs][vms[[interfaces]
3023 for net
in myVMDict
['networks']:
3025 for iface
in vm
['interfaces']:
3026 if net
["name"]==iface
["internal_name"]:
3027 iface
["vim_id"]=net
["vim_id"]
3029 vm_uuid
= str(uuid4())
3030 uuid_list
.append(vm_uuid
)
3033 'instance_vnf_id': vnf_uuid
,
3035 "vm_id": vm
["uuid"],
3038 db_instance_vms
.append(db_vm
)
3039 for db_vm_iface
in db_vm_ifaces
:
3040 iface_uuid
= str(uuid4())
3041 uuid_list
.append(iface_uuid
)
3042 db_vm_iface_instance
= {
3044 "instance_vm_id": vm_uuid
3046 db_vm_iface_instance
.update(db_vm_iface
)
3047 if db_vm_iface_instance
.get("ip_address"): # increment ip_address
3048 ip
= db_vm_iface_instance
.get("ip_address")
3053 ip
= ip
[i
:] + str(int(ip
[:i
]) +1)
3054 db_vm_iface_instance
["ip_address"] = ip
3056 db_vm_iface_instance
["ip_address"] = None
3057 db_instance_interfaces
.append(db_vm_iface_instance
)
3059 scenarioDict
["datacenter2tenant"] = myvim_threads_id
3061 db_instance_scenario
['datacenter_tenant_id'] = myvim_threads_id
[default_datacenter_id
]
3062 db_instance_scenario
['datacenter_id'] = default_datacenter_id
3064 {"instance_scenarios": db_instance_scenario
},
3065 {"instance_vnfs": db_instance_vnfs
},
3066 {"instance_nets": db_instance_nets
},
3067 {"ip_profiles": db_ip_profiles
},
3068 {"instance_vms": db_instance_vms
},
3069 {"instance_interfaces": db_instance_interfaces
},
3072 logger
.debug("create_instance Deployment done scenarioDict: %s",
3073 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
3074 mydb
.new_rows(db_tables
, uuid_list
)
3075 for myvim_thread_id
,task_list
in tasks_to_launch
.items():
3076 for task
in task_list
:
3077 vim_threads
["running"][myvim_thread_id
].insert_task(task
)
3079 global_instance_tasks
[instance_uuid
] = instance_tasks
3080 # Update database with those ended instance_tasks
3081 # for task in instance_tasks.values():
3082 # if task["status"] == "ok":
3083 # if task["name"] == "new-vm":
3084 # mydb.update_rows("instance_vms", UPDATE={"vim_vm_id": task["result"]},
3085 # WHERE={"vim_vm_id": task["id"]})
3086 # elif task["name"] == "new-net":
3087 # mydb.update_rows("instance_nets", UPDATE={"vim_net_id": task["result"]},
3088 # WHERE={"vim_net_id": task["id"]})
3089 return mydb
.get_instance_scenario(instance_uuid
)
3090 except (NfvoException
, vimconn
.vimconnException
,db_base_Exception
) as e
:
3091 message
= rollback(mydb
, myvims
, rollbackList
)
3092 if isinstance(e
, db_base_Exception
):
3093 error_text
= "database Exception"
3094 elif isinstance(e
, vimconn
.vimconnException
):
3095 error_text
= "VIM Exception"
3097 error_text
= "Exception"
3098 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
3099 #logger.error("create_instance: %s", error_text)
3100 raise NfvoException(error_text
, e
.http_code
)
3103 def delete_instance(mydb
, tenant_id
, instance_id
):
3104 #print "Checking that the instance_id exists and getting the instance dictionary"
3105 instanceDict
= mydb
.get_instance_scenario(instance_id
, tenant_id
)
3106 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
3107 tenant_id
= instanceDict
["tenant_id"]
3108 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
3110 #1. Delete from Database
3111 message
= mydb
.delete_instance_scenario(instance_id
, tenant_id
)
3120 for sce_vnf
in instanceDict
['vnfs']:
3121 datacenter_key
= (sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
3122 if datacenter_key
not in myvims
:
3124 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
3125 except NfvoException
as e
:
3126 logger
.error(str(e
))
3128 myvim_threads
[datacenter_key
] = myvim_thread
3129 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sce_vnf
["datacenter_id"],
3130 datacenter_tenant_id
=sce_vnf
["datacenter_tenant_id"])
3132 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf
["datacenter_id"],
3133 sce_vnf
["datacenter_tenant_id"]))
3134 myvims
[datacenter_key
] = None
3136 myvims
[datacenter_key
] = vims
.values()[0]
3137 myvim
= myvims
[datacenter_key
]
3138 myvim_thread
= myvim_threads
[datacenter_key
]
3139 for vm
in sce_vnf
['vms']:
3141 error_msg
+= "\n VM id={} cannot be deleted because datacenter={} not found".format(vm
['vim_vm_id'], sce_vnf
["datacenter_id"])
3145 if is_task_id(vm
['vim_vm_id']):
3146 task_id
= vm
['vim_vm_id']
3147 old_task
= global_instance_tasks
[instance_id
].get(task_id
)
3149 error_msg
+= "\n VM was scheduled for create, but task {} is not found".format(task_id
)
3152 if old_task
["status"] == "enqueued":
3153 old_task
["status"] = "deleted"
3154 elif old_task
["status"] == "error":
3156 elif old_task
["status"] == "processing":
3157 task
= new_task("del-vm", (task_id
, vm
["interfaces"]), depends
={task_id
: old_task
})
3159 task
= new_task("del-vm", (old_task
["result"], vm
["interfaces"]))
3161 task
= new_task("del-vm", (vm
['vim_vm_id'], vm
["interfaces"]) )
3163 myvim_thread
.insert_task(task
)
3164 except vimconn
.vimconnNotFoundException
as e
:
3165 error_msg
+="\n VM VIM_id={} not found at datacenter={}".format(vm
['vim_vm_id'], sce_vnf
["datacenter_id"])
3166 logger
.warn("VM instance '%s'uuid '%s', VIM id '%s', from VNF_id '%s' not found",
3167 vm
['name'], vm
['uuid'], vm
['vim_vm_id'], sce_vnf
['vnf_id'])
3168 except vimconn
.vimconnException
as e
:
3169 error_msg
+="\n VM VIM_id={} at datacenter={} Error: {} {}".format(vm
['vim_vm_id'], sce_vnf
["datacenter_id"], e
.http_code
, str(e
))
3170 logger
.error("Error %d deleting VM instance '%s'uuid '%s', VIM_id '%s', from VNF_id '%s': %s",
3171 e
.http_code
, vm
['name'], vm
['uuid'], vm
['vim_vm_id'], sce_vnf
['vnf_id'], str(e
))
3175 for net
in instanceDict
['nets']:
3176 if not net
['created']:
3177 continue #skip not created nets
3178 datacenter_key
= (net
["datacenter_id"], net
["datacenter_tenant_id"])
3179 if datacenter_key
not in myvims
:
3181 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
3182 except NfvoException
as e
:
3183 logger
.error(str(e
))
3185 myvim_threads
[datacenter_key
] = myvim_thread
3186 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=net
["datacenter_id"],
3187 datacenter_tenant_id
=net
["datacenter_tenant_id"])
3189 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net
["datacenter_id"], net
["datacenter_tenant_id"]))
3190 myvims
[datacenter_key
] = None
3192 myvims
[datacenter_key
] = vims
.values()[0]
3193 myvim
= myvims
[datacenter_key
]
3194 myvim_thread
= myvim_threads
[datacenter_key
]
3197 error_msg
+= "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net
['vim_net_id'], net
["datacenter_id"])
3201 if is_task_id(net
['vim_net_id']):
3202 task_id
= net
['vim_net_id']
3203 old_task
= global_instance_tasks
[instance_id
].get(task_id
)
3205 error_msg
+= "\n NET was scheduled for create, but task {} is not found".format(task_id
)
3208 if old_task
["status"] == "enqueued":
3209 old_task
["status"] = "deleted"
3210 elif old_task
["status"] == "error":
3212 elif old_task
["status"] == "processing":
3213 task
= new_task("del-net", task_id
, depends
={task_id
: old_task
})
3215 task
= new_task("del-net", old_task
["result"])
3217 task
= new_task("del-net", (net
['vim_net_id'], net
['sdn_net_id']))
3219 myvim_thread
.insert_task(task
)
3220 except vimconn
.vimconnNotFoundException
as e
:
3221 error_msg
+= "\n NET VIM_id={} not found at datacenter={}".format(net
['vim_net_id'], net
["datacenter_id"])
3222 logger
.warn("NET '%s', VIM_id '%s', from VNF_net_id '%s' not found",
3223 net
['uuid'], net
['vim_net_id'], str(net
['vnf_net_id']))
3224 except vimconn
.vimconnException
as e
:
3225 error_msg
+= "\n NET VIM_id={} at datacenter={} Error: {} {}".format(net
['vim_net_id'],
3226 net
["datacenter_id"],
3227 e
.http_code
, str(e
))
3228 logger
.error("Error %d deleting NET '%s', VIM_id '%s', from VNF_net_id '%s': %s",
3229 e
.http_code
, net
['uuid'], net
['vim_net_id'], str(net
['vnf_net_id']), str(e
))
3230 if len(error_msg
) > 0:
3231 return 'instance ' + message
+ ' deleted but some elements could not be deleted, or already deleted (error: 404) from VIM: ' + error_msg
3233 return 'instance ' + message
+ ' deleted'
3236 def refresh_instance(mydb
, nfvo_tenant
, instanceDict
, datacenter
=None, vim_tenant
=None):
3237 '''Refreshes a scenario instance. It modifies instanceDict'''
3239 - result: <0 if there is any unexpected error, n>=0 if no errors where n is the number of vms and nets that couldn't be updated in the database
3242 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
3243 # #print "nfvo.refresh_instance begins"
3244 # #print json.dumps(instanceDict, indent=4)
3246 # #print "Getting the VIM URL and the VIM tenant_id"
3249 # # 1. Getting VIM vm and net list
3250 # vms_updated = [] #List of VM instance uuids in openmano that were updated
3253 # for sce_vnf in instanceDict['vnfs']:
3254 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
3255 # if datacenter_key not in vm_list:
3256 # vm_list[datacenter_key] = []
3257 # if datacenter_key not in myvims:
3258 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
3259 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
3260 # if len(vims) == 0:
3261 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
3262 # myvims[datacenter_key] = None
3264 # myvims[datacenter_key] = vims.values()[0]
3265 # for vm in sce_vnf['vms']:
3266 # vm_list[datacenter_key].append(vm['vim_vm_id'])
3267 # vms_notupdated.append(vm["uuid"])
3269 # nets_updated = [] #List of VM instance uuids in openmano that were updated
3270 # nets_notupdated=[]
3272 # for net in instanceDict['nets']:
3273 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
3274 # if datacenter_key not in net_list:
3275 # net_list[datacenter_key] = []
3276 # if datacenter_key not in myvims:
3277 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
3278 # datacenter_tenant_id=net["datacenter_tenant_id"])
3279 # if len(vims) == 0:
3280 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
3281 # myvims[datacenter_key] = None
3283 # myvims[datacenter_key] = vims.values()[0]
3285 # net_list[datacenter_key].append(net['vim_net_id'])
3286 # nets_notupdated.append(net["uuid"])
3288 # # 1. Getting the status of all VMs
3290 # for datacenter_key in myvims:
3291 # if not vm_list.get(datacenter_key):
3295 # if not myvims[datacenter_key]:
3296 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
3299 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
3301 # except vimconn.vimconnException as e:
3302 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
3303 # failed_message = str(e)
3305 # for vm in vm_list[datacenter_key]:
3306 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
3308 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
3309 # for sce_vnf in instanceDict['vnfs']:
3310 # for vm in sce_vnf['vms']:
3311 # vm_id = vm['vim_vm_id']
3312 # interfaces = vm_dict[vm_id].pop('interfaces', [])
3313 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
3314 # has_mgmt_iface = False
3315 # for iface in vm["interfaces"]:
3316 # if iface["type"]=="mgmt":
3317 # has_mgmt_iface = True
3318 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
3319 # vm_dict[vm_id]['status'] = "ACTIVE"
3320 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
3321 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
3322 # if vm['status'] != vm_dict[vm_id]['status'] or vm.get('error_msg')!=vm_dict[vm_id].get('error_msg') or vm.get('vim_info')!=vm_dict[vm_id].get('vim_info'):
3323 # vm['status'] = vm_dict[vm_id]['status']
3324 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
3325 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
3326 # # 2.1. Update in openmano DB the VMs whose status changed
3328 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
3329 # vms_notupdated.remove(vm["uuid"])
3331 # vms_updated.append(vm["uuid"])
3332 # except db_base_Exception as e:
3333 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
3334 # # 2.2. Update in openmano DB the interface VMs
3335 # for interface in interfaces:
3336 # #translate from vim_net_id to instance_net_id
3337 # network_id_list=[]
3338 # for net in instanceDict['nets']:
3339 # if net["vim_net_id"] == interface["vim_net_id"]:
3340 # network_id_list.append(net["uuid"])
3341 # if not network_id_list:
3343 # del interface["vim_net_id"]
3345 # for network_id in network_id_list:
3346 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
3347 # except db_base_Exception as e:
3348 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
3350 # # 3. Getting the status of all nets
3352 # for datacenter_key in myvims:
3353 # if not net_list.get(datacenter_key):
3356 # failed_message = ""
3357 # if not myvims[datacenter_key]:
3358 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
3361 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
3363 # except vimconn.vimconnException as e:
3364 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
3365 # failed_message = str(e)
3367 # for net in net_list[datacenter_key]:
3368 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
3370 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
3371 # # TODO: update nets inside a vnf
3372 # for net in instanceDict['nets']:
3373 # net_id = net['vim_net_id']
3374 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
3375 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
3376 # if net['status'] != net_dict[net_id]['status'] or net.get('error_msg')!=net_dict[net_id].get('error_msg') or net.get('vim_info')!=net_dict[net_id].get('vim_info'):
3377 # net['status'] = net_dict[net_id]['status']
3378 # net['error_msg'] = net_dict[net_id].get('error_msg')
3379 # net['vim_info'] = net_dict[net_id].get('vim_info')
3380 # # 5.1. Update in openmano DB the nets whose status changed
3382 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
3383 # nets_notupdated.remove(net["uuid"])
3385 # nets_updated.append(net["uuid"])
3386 # except db_base_Exception as e:
3387 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
3389 # # Returns appropriate output
3390 # #print "nfvo.refresh_instance finishes"
3391 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
3392 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
3393 instance_id
= instanceDict
['uuid']
3394 # if len(vms_notupdated)+len(nets_notupdated)>0:
3395 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
3396 # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
3398 return 0, 'Scenario instance ' + instance_id
+ ' refreshed.'
3401 def instance_action(mydb
,nfvo_tenant
,instance_id
, action_dict
):
3402 #print "Checking that the instance_id exists and getting the instance dictionary"
3403 instanceDict
= mydb
.get_instance_scenario(instance_id
, nfvo_tenant
)
3404 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
3406 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
3407 vims
= get_vim(mydb
, nfvo_tenant
, instanceDict
['datacenter_id'])
3409 raise NfvoException("datacenter '{}' not found".format(str(instanceDict
['datacenter_id'])), HTTP_Not_Found
)
3410 myvim
= vims
.values()[0]
3413 input_vnfs
= action_dict
.pop("vnfs", [])
3414 input_vms
= action_dict
.pop("vms", [])
3415 action_over_all
= True if len(input_vnfs
)==0 and len (input_vms
)==0 else False
3419 for sce_vnf
in instanceDict
['vnfs']:
3420 for vm
in sce_vnf
['vms']:
3421 if not action_over_all
:
3422 if sce_vnf
['uuid'] not in input_vnfs
and sce_vnf
['vnf_name'] not in input_vnfs
and \
3423 vm
['uuid'] not in input_vms
and vm
['name'] not in input_vms
:
3426 data
= myvim
.action_vminstance(vm
['vim_vm_id'], action_dict
)
3427 if "console" in action_dict
:
3428 if not global_config
["http_console_proxy"]:
3429 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
3430 "description": "{protocol}//{ip}:{port}/{suffix}".format(
3431 protocol
=data
["protocol"],
3432 ip
= data
["server"],
3433 port
= data
["port"],
3434 suffix
= data
["suffix"]),
3438 elif data
["server"]=="127.0.0.1" or data
["server"]=="localhost":
3439 vm_result
[ vm
['uuid'] ] = {"vim_result": -HTTP_Unauthorized
,
3440 "description": "this console is only reachable by local interface",
3445 #print "console data", data
3447 console_thread
= create_or_use_console_proxy_thread(data
["server"], data
["port"])
3448 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
3449 "description": "{protocol}//{ip}:{port}/{suffix}".format(
3450 protocol
=data
["protocol"],
3451 ip
= global_config
["http_console_host"],
3452 port
= console_thread
.port
,
3453 suffix
= data
["suffix"]),
3457 except NfvoException
as e
:
3458 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
3462 vm_result
[ vm
['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm
['name']}
3464 except vimconn
.vimconnException
as e
:
3465 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
3468 if vm_ok
==0: #all goes wrong
3474 def create_or_use_console_proxy_thread(console_server
, console_port
):
3475 #look for a non-used port
3476 console_thread_key
= console_server
+ ":" + str(console_port
)
3477 if console_thread_key
in global_config
["console_thread"]:
3478 #global_config["console_thread"][console_thread_key].start_timeout()
3479 return global_config
["console_thread"][console_thread_key
]
3481 for port
in global_config
["console_port_iterator"]():
3482 #print "create_or_use_console_proxy_thread() port:", port
3483 if port
in global_config
["console_ports"]:
3486 clithread
= cli
.ConsoleProxyThread(global_config
['http_host'], port
, console_server
, console_port
)
3488 global_config
["console_thread"][console_thread_key
] = clithread
3489 global_config
["console_ports"][port
] = console_thread_key
3491 except cli
.ConsoleProxyExceptionPortUsed
as e
:
3492 #port used, try with onoher
3494 except cli
.ConsoleProxyException
as e
:
3495 raise NfvoException(str(e
), HTTP_Bad_Request
)
3496 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict
)
3499 def check_tenant(mydb
, tenant_id
):
3500 '''check that tenant exists at database'''
3501 tenant
= mydb
.get_rows(FROM
='nfvo_tenants', SELECT
=('uuid',), WHERE
={'uuid': tenant_id
})
3503 raise NfvoException("tenant '{}' not found".format(tenant_id
), HTTP_Not_Found
)
3507 def new_tenant(mydb
, tenant_dict
):
3508 tenant_id
= mydb
.new_row("nfvo_tenants", tenant_dict
, add_uuid
=True)
3512 def delete_tenant(mydb
, tenant
):
3513 #get nfvo_tenant info
3515 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant
, 'tenant')
3516 mydb
.delete_row_by_id("nfvo_tenants", tenant_dict
['uuid'])
3517 return tenant_dict
['uuid'] + " " + tenant_dict
["name"]
3520 def new_datacenter(mydb
, datacenter_descriptor
):
3521 if "config" in datacenter_descriptor
:
3522 datacenter_descriptor
["config"]=yaml
.safe_dump(datacenter_descriptor
["config"],default_flow_style
=True,width
=256)
3523 #Check that datacenter-type is correct
3524 datacenter_type
= datacenter_descriptor
.get("type", "openvim");
3527 module
= "vimconn_" + datacenter_type
3528 pkg
= __import__("osm_ro." + module
)
3529 vim_conn
= getattr(pkg
, module
)
3530 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
3531 except (IOError, ImportError):
3532 # if module_info and module_info[0]:
3533 # file.close(module_info[0])
3534 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}'.py not installed".format(datacenter_type
, module
), HTTP_Bad_Request
)
3536 datacenter_id
= mydb
.new_row("datacenters", datacenter_descriptor
, add_uuid
=True)
3537 return datacenter_id
3540 def edit_datacenter(mydb
, datacenter_id_name
, datacenter_descriptor
):
3541 # obtain data, check that only one exist
3542 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id_name
)
3545 datacenter_id
= datacenter
['uuid']
3546 where
={'uuid': datacenter
['uuid']}
3547 remove_port_mapping
= False
3548 if "config" in datacenter_descriptor
:
3549 if datacenter_descriptor
['config'] != None:
3551 new_config_dict
= datacenter_descriptor
["config"]
3554 for k
in new_config_dict
:
3555 if new_config_dict
[k
] == None:
3557 if k
== 'sdn-controller':
3558 remove_port_mapping
= True
3560 config_text
= datacenter
.get("config")
3563 config_dict
= yaml
.load(config_text
)
3564 config_dict
.update(new_config_dict
)
3568 except Exception as e
:
3569 raise NfvoException("Bad format at datacenter:config " + str(e
), HTTP_Bad_Request
)
3571 datacenter_descriptor
["config"] = yaml
.safe_dump(config_dict
, default_flow_style
=True, width
=256)
3573 datacenter_descriptor
["config"] = None
3574 if remove_port_mapping
:
3576 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_id
)
3577 except ovimException
as e
:
3578 logger
.error("Error deleting datacenter-port-mapping " + str(e
))
3580 mydb
.update_rows('datacenters', datacenter_descriptor
, where
)
3581 return datacenter_id
3584 def delete_datacenter(mydb
, datacenter
):
3585 #get nfvo_tenant info
3586 datacenter_dict
= mydb
.get_table_by_uuid_name('datacenters', datacenter
, 'datacenter')
3587 mydb
.delete_row_by_id("datacenters", datacenter_dict
['uuid'])
3589 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_dict
['uuid'])
3590 except ovimException
as e
:
3591 logger
.error("Error deleting datacenter-port-mapping " + str(e
))
3592 return datacenter_dict
['uuid'] + " " + datacenter_dict
['name']
3595 def associate_datacenter_to_tenant(mydb
, nfvo_tenant
, datacenter
, vim_tenant_id
=None, vim_tenant_name
=None, vim_username
=None, vim_password
=None, config
=None):
3596 #get datacenter info
3597 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, None, datacenter
, vim_user
=vim_username
, vim_passwd
=vim_password
)
3598 datacenter_name
= myvim
["name"]
3600 create_vim_tenant
= True if not vim_tenant_id
and not vim_tenant_name
else False
3602 # get nfvo_tenant info
3603 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant
)
3604 if vim_tenant_name
==None:
3605 vim_tenant_name
=tenant_dict
['name']
3607 #check that this association does not exist before
3608 tenants_datacenter_dict
={"nfvo_tenant_id":tenant_dict
['uuid'], "datacenter_id":datacenter_id
}
3609 tenants_datacenters
= mydb
.get_rows(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
3610 if len(tenants_datacenters
)>0:
3611 raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id
, tenant_dict
['uuid']), HTTP_Conflict
)
3613 vim_tenant_id_exist_atdb
=False
3614 if not create_vim_tenant
:
3615 where_
={"datacenter_id": datacenter_id
}
3616 if vim_tenant_id
!=None:
3617 where_
["vim_tenant_id"] = vim_tenant_id
3618 if vim_tenant_name
!=None:
3619 where_
["vim_tenant_name"] = vim_tenant_name
3620 #check if vim_tenant_id is already at database
3621 datacenter_tenants_dict
= mydb
.get_rows(FROM
='datacenter_tenants', WHERE
=where_
)
3622 if len(datacenter_tenants_dict
)>=1:
3623 datacenter_tenants_dict
= datacenter_tenants_dict
[0]
3624 vim_tenant_id_exist_atdb
=True
3625 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
3627 datacenter_tenants_dict
= {}
3628 #insert at table datacenter_tenants
3629 else: #if vim_tenant_id==None:
3630 #create tenant at VIM if not provided
3632 vim_tenant_id
= myvim
.new_tenant(vim_tenant_name
, "created by openmano for datacenter "+datacenter_name
)
3633 except vimconn
.vimconnException
as e
:
3634 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id
, str(e
)), HTTP_Internal_Server_Error
)
3635 datacenter_tenants_dict
= {}
3636 datacenter_tenants_dict
["created"]="true"
3638 #fill datacenter_tenants table
3639 if not vim_tenant_id_exist_atdb
:
3640 datacenter_tenants_dict
["vim_tenant_id"] = vim_tenant_id
3641 datacenter_tenants_dict
["vim_tenant_name"] = vim_tenant_name
3642 datacenter_tenants_dict
["user"] = vim_username
3643 datacenter_tenants_dict
["passwd"] = vim_password
3644 datacenter_tenants_dict
["datacenter_id"] = datacenter_id
3646 datacenter_tenants_dict
["config"] = yaml
.safe_dump(config
, default_flow_style
=True, width
=256)
3647 id_
= mydb
.new_row('datacenter_tenants', datacenter_tenants_dict
, add_uuid
=True)
3648 datacenter_tenants_dict
["uuid"] = id_
3650 #fill tenants_datacenters table
3651 datacenter_tenant_id
= datacenter_tenants_dict
["uuid"]
3652 tenants_datacenter_dict
["datacenter_tenant_id"] = datacenter_tenant_id
3653 mydb
.new_row('tenants_datacenters', tenants_datacenter_dict
)
3655 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_dict
['uuid'], datacenter_id
) # reload data
3656 thread_name
= get_non_used_vim_name(datacenter_name
, datacenter_id
, tenant_dict
['name'], tenant_dict
['uuid'])
3657 new_thread
= vim_thread
.vim_thread(myvim
, task_lock
, thread_name
, datacenter_name
, datacenter_tenant_id
,
3658 db
=db
, db_lock
=db_lock
, ovim
=ovim
)
3660 thread_id
= datacenter_tenants_dict
["uuid"]
3661 vim_threads
["running"][thread_id
] = new_thread
3662 return datacenter_id
3665 def edit_datacenter_to_tenant(mydb
, nfvo_tenant
, datacenter_id
, vim_tenant_id
=None, vim_tenant_name
=None,
3666 vim_username
=None, vim_password
=None, config
=None):
3667 #Obtain the data of this datacenter_tenant_id
3668 vim_data
= mydb
.get_rows(
3669 SELECT
=("datacenter_tenants.vim_tenant_name", "datacenter_tenants.vim_tenant_id", "datacenter_tenants.user",
3670 "datacenter_tenants.passwd", "datacenter_tenants.config"),
3671 FROM
="datacenter_tenants JOIN tenants_datacenters ON datacenter_tenants.uuid=tenants_datacenters.datacenter_tenant_id",
3672 WHERE
={"tenants_datacenters.nfvo_tenant_id": nfvo_tenant
,
3673 "tenants_datacenters.datacenter_id": datacenter_id
})
3675 logger
.debug(str(vim_data
))
3676 if len(vim_data
) < 1:
3677 raise NfvoException("Datacenter {} is not attached for tenant {}".format(datacenter_id
, nfvo_tenant
), HTTP_Conflict
)
3681 v
['config'] = yaml
.load(v
['config'])
3684 v
['vim_tenant_id'] = vim_tenant_id
3686 v
['vim_tenant_name'] = vim_tenant_name
3688 v
['user'] = vim_username
3690 v
['passwd'] = vim_password
3694 v
['config'].update(config
)
3696 logger
.debug(str(v
))
3697 deassociate_datacenter_to_tenant(mydb
, nfvo_tenant
, datacenter_id
, vim_tenant_id
=v
['vim_tenant_id'])
3698 associate_datacenter_to_tenant(mydb
, nfvo_tenant
, datacenter_id
, vim_tenant_id
=v
['vim_tenant_id'], vim_tenant_name
=v
['vim_tenant_name'],
3699 vim_username
=v
['user'], vim_password
=v
['passwd'], config
=v
['config'])
3701 return datacenter_id
3703 def deassociate_datacenter_to_tenant(mydb
, tenant_id
, datacenter
, vim_tenant_id
=None):
3704 #get datacenter info
3705 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3707 #get nfvo_tenant info
3708 if not tenant_id
or tenant_id
=="any":
3711 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant_id
)
3712 tenant_uuid
= tenant_dict
['uuid']
3714 #check that this association exist before
3715 tenants_datacenter_dict
={"datacenter_id":datacenter_id
}
3717 tenants_datacenter_dict
["nfvo_tenant_id"] = tenant_uuid
3718 tenant_datacenter_list
= mydb
.get_rows(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
3719 if len(tenant_datacenter_list
)==0 and tenant_uuid
:
3720 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id
, tenant_dict
['uuid']), HTTP_Not_Found
)
3722 #delete this association
3723 mydb
.delete_row(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
3725 #get vim_tenant info and deletes
3727 for tenant_datacenter_item
in tenant_datacenter_list
:
3728 vim_tenant_dict
= mydb
.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
3729 #try to delete vim:tenant
3731 mydb
.delete_row_by_id('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
3732 if vim_tenant_dict
['created']=='true':
3733 #delete tenant at VIM if created by NFVO
3735 myvim
.delete_tenant(vim_tenant_dict
['vim_tenant_id'])
3736 except vimconn
.vimconnException
as e
:
3737 warning
= "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict
['vim_tenant_id'], str(e
))
3738 logger
.warn(warning
)
3739 except db_base_Exception
as e
:
3740 logger
.error("Cannot delete datacenter_tenants " + str(e
))
3741 pass # the error will be caused because dependencies, vim_tenant can not be deleted
3742 thread_id
= tenant_datacenter_item
["datacenter_tenant_id"]
3743 thread
= vim_threads
["running"][thread_id
]
3744 thread
.insert_task(new_task("exit", None))
3745 vim_threads
["deleting"][thread_id
] = thread
3746 return "datacenter {} detached. {}".format(datacenter_id
, warning
)
3749 def datacenter_action(mydb
, tenant_id
, datacenter
, action_dict
):
3751 #get datacenter info
3752 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3754 if 'net-update' in action_dict
:
3756 nets
= myvim
.get_network_list(filter_dict
={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
3758 except vimconn
.vimconnException
as e
:
3759 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
3760 raise NfvoException(str(e
), HTTP_Internal_Server_Error
)
3761 #update nets Change from VIM format to NFVO format
3764 net_nfvo
={'datacenter_id': datacenter_id
}
3765 net_nfvo
['name'] = net
['name']
3766 #net_nfvo['description']= net['name']
3767 net_nfvo
['vim_net_id'] = net
['id']
3768 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
3769 net_nfvo
['shared'] = net
['shared']
3770 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
3771 net_list
.append(net_nfvo
)
3772 inserted
, deleted
= mydb
.update_datacenter_nets(datacenter_id
, net_list
)
3773 logger
.info("Inserted %d nets, deleted %d old nets", inserted
, deleted
)
3775 elif 'net-edit' in action_dict
:
3776 net
= action_dict
['net-edit'].pop('net')
3777 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
3778 result
= mydb
.update_rows('datacenter_nets', action_dict
['net-edit'],
3779 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
3781 elif 'net-delete' in action_dict
:
3782 net
= action_dict
['net-deelte'].get('net')
3783 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
3784 result
= mydb
.delete_row(FROM
='datacenter_nets',
3785 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
3789 raise NfvoException("Unknown action " + str(action_dict
), HTTP_Bad_Request
)
3792 def datacenter_edit_netmap(mydb
, tenant_id
, datacenter
, netmap
, action_dict
):
3793 #get datacenter info
3794 datacenter_id
, _
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3796 what
= 'uuid' if utils
.check_valid_uuid(netmap
) else 'name'
3797 result
= mydb
.update_rows('datacenter_nets', action_dict
['netmap'],
3798 WHERE
={'datacenter_id':datacenter_id
, what
: netmap
})
3802 def datacenter_new_netmap(mydb
, tenant_id
, datacenter
, action_dict
=None):
3803 #get datacenter info
3804 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3807 action_dict
= action_dict
["netmap"]
3808 if 'vim_id' in action_dict
:
3809 filter_dict
["id"] = action_dict
['vim_id']
3810 if 'vim_name' in action_dict
:
3811 filter_dict
["name"] = action_dict
['vim_name']
3813 filter_dict
["shared"] = True
3816 vim_nets
= myvim
.get_network_list(filter_dict
=filter_dict
)
3817 except vimconn
.vimconnException
as e
:
3818 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
3819 raise NfvoException(str(e
), HTTP_Internal_Server_Error
)
3820 if len(vim_nets
)>1 and action_dict
:
3821 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict
)
3822 elif len(vim_nets
)==0: # and action_dict:
3823 raise NfvoException("Not found a network at VIM with " + str(filter_dict
), HTTP_Not_Found
)
3825 for net
in vim_nets
:
3826 net_nfvo
={'datacenter_id': datacenter_id
}
3827 if action_dict
and "name" in action_dict
:
3828 net_nfvo
['name'] = action_dict
['name']
3830 net_nfvo
['name'] = net
['name']
3831 #net_nfvo['description']= net['name']
3832 net_nfvo
['vim_net_id'] = net
['id']
3833 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
3834 net_nfvo
['shared'] = net
['shared']
3835 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
3837 net_id
= mydb
.new_row("datacenter_nets", net_nfvo
, add_uuid
=True)
3838 net_nfvo
["status"] = "OK"
3839 net_nfvo
["uuid"] = net_id
3840 except db_base_Exception
as e
:
3844 net_nfvo
["status"] = "FAIL: " + str(e
)
3845 net_list
.append(net_nfvo
)
3848 def get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
):
3849 # obtain all network data
3851 if utils
.check_valid_uuid(network_id
):
3852 filter_dict
= {"id": network_id
}
3854 filter_dict
= {"name": network_id
}
3856 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3857 network
= myvim
.get_network_list(filter_dict
=filter_dict
)
3858 except vimconn
.vimconnException
as e
:
3859 raise NfvoException("Not possible to get_sdn_net_id from VIM: {}".format(str(e
)), e
.http_code
)
3861 # ensure the network is defined
3862 if len(network
) == 0:
3863 raise NfvoException("Network {} is not present in the system".format(network_id
),
3866 # ensure there is only one network with the provided name
3867 if len(network
) > 1:
3868 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id
), HTTP_Bad_Request
)
3870 # ensure it is a dataplane network
3871 if network
[0]['type'] != 'data':
3874 # ensure we use the id
3875 network_id
= network
[0]['id']
3877 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
3878 # and with instance_scenario_id==NULL
3879 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
3880 search_dict
= {'vim_net_id': network_id
}
3883 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
3884 result
= mydb
.get_rows(SELECT
=('sdn_net_id',), FROM
='instance_nets', WHERE
=search_dict
)
3885 except db_base_Exception
as e
:
3886 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
3887 network_id
) + str(e
), HTTP_Internal_Server_Error
)
3891 if net
['sdn_net_id'] != None:
3893 sdn_net_id
= net
['sdn_net_id']
3895 if sdn_net_counter
== 0:
3897 elif sdn_net_counter
== 1:
3900 raise NfvoException("More than one SDN network is associated to vim network {}".format(
3901 network_id
), HTTP_Internal_Server_Error
)
3903 def get_sdn_controller_id(mydb
, datacenter
):
3904 # Obtain sdn controller id
3905 config
= mydb
.get_rows(SELECT
=('config',), FROM
='datacenters', WHERE
={'uuid': datacenter
})[0].get('config', '{}')
3909 return yaml
.load(config
).get('sdn-controller')
3911 def vim_net_sdn_attach(mydb
, tenant_id
, datacenter
, network_id
, descriptor
):
3913 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
3914 if not sdn_network_id
:
3915 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
), HTTP_Internal_Server_Error
)
3917 #Obtain sdn controller id
3918 controller_id
= get_sdn_controller_id(mydb
, datacenter
)
3919 if not controller_id
:
3920 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter
), HTTP_Internal_Server_Error
)
3922 #Obtain sdn controller info
3923 sdn_controller
= ovim
.show_of_controller(controller_id
)
3926 'name': 'external_port',
3927 'net_id': sdn_network_id
,
3928 'ofc_id': controller_id
,
3929 'switch_dpid': sdn_controller
['dpid'],
3930 'switch_port': descriptor
['port']
3933 if 'vlan' in descriptor
:
3934 port_data
['vlan'] = descriptor
['vlan']
3935 if 'mac' in descriptor
:
3936 port_data
['mac'] = descriptor
['mac']
3938 result
= ovim
.new_port(port_data
)
3939 except ovimException
as e
:
3940 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
3941 sdn_network_id
, network_id
) + str(e
), HTTP_Internal_Server_Error
)
3942 except db_base_Exception
as e
:
3943 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
3944 network_id
) + str(e
), HTTP_Internal_Server_Error
)
3946 return 'Port uuid: '+ result
3948 def vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, network_id
, port_id
=None):
3950 filter = {'uuid': port_id
}
3952 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
3953 if not sdn_network_id
:
3954 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
),
3955 HTTP_Internal_Server_Error
)
3956 #in case no port_id is specified only ports marked as 'external_port' will be detached
3957 filter = {'name': 'external_port', 'net_id': sdn_network_id
}
3960 port_list
= ovim
.get_ports(columns
={'uuid'}, filter=filter)
3961 except ovimException
as e
:
3962 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
3963 HTTP_Internal_Server_Error
)
3965 if len(port_list
) == 0:
3966 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id
),
3970 for port
in port_list
:
3972 port_uuid_list
.append(port
['uuid'])
3973 ovim
.delete_port(port
['uuid'])
3974 except ovimException
as e
:
3975 raise NfvoException("ovimException deleting port {} for net {}. ".format(port
['uuid'], network_id
) + str(e
), HTTP_Internal_Server_Error
)
3977 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list
))
3979 def vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
):
3980 #get datacenter info
3981 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3984 if utils
.check_valid_uuid(name
):
3985 filter_dict
["id"] = name
3987 filter_dict
["name"] = name
3989 if item
=="networks":
3990 #filter_dict['tenant_id'] = myvim['tenant_id']
3991 content
= myvim
.get_network_list(filter_dict
=filter_dict
)
3993 if len(content
) == 0:
3994 raise NfvoException("Network {} is not present in the system. ".format(name
),
3997 #Update the networks with the attached ports
3999 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, net
['id'])
4000 if sdn_network_id
!= None:
4002 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
4003 port_list
= ovim
.get_ports(columns
={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id
})
4004 except ovimException
as e
:
4005 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
), HTTP_Internal_Server_Error
)
4006 #Remove field name and if port name is external_port save it as 'type'
4007 for port
in port_list
:
4008 if port
['name'] == 'external_port':
4009 port
['type'] = "External"
4011 net
['sdn_network_id'] = sdn_network_id
4012 net
['sdn_attached_ports'] = port_list
4014 elif item
=="tenants":
4015 content
= myvim
.get_tenant_list(filter_dict
=filter_dict
)
4016 elif item
== "images":
4018 content
= myvim
.get_image_list(filter_dict
=filter_dict
)
4020 raise NfvoException(item
+ "?", HTTP_Method_Not_Allowed
)
4021 logger
.debug("vim_action response %s", content
) #update nets Change from VIM format to NFVO format
4022 if name
and len(content
)==1:
4023 return {item
[:-1]: content
[0]}
4024 elif name
and len(content
)==0:
4025 raise NfvoException("No {} found with ".format(item
[:-1]) + " and ".join(map(lambda x
: str(x
[0])+": "+str(x
[1]), filter_dict
.iteritems())),
4028 return {item
: content
}
4029 except vimconn
.vimconnException
as e
:
4030 print "vim_action Not possible to get_%s_list from VIM: %s " % (item
, str(e
))
4031 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item
, str(e
)), e
.http_code
)
4034 def vim_action_delete(mydb
, tenant_id
, datacenter
, item
, name
):
4035 #get datacenter info
4036 if tenant_id
== "any":
4039 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4041 content
= vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
)
4042 logger
.debug("vim_action_delete vim response: " + str(content
))
4043 items
= content
.values()[0]
4044 if type(items
)==list and len(items
)==0:
4045 raise NfvoException("Not found " + item
, HTTP_Not_Found
)
4046 elif type(items
)==list and len(items
)>1:
4047 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item
), HTTP_Not_Found
)
4048 else: # it is a dict
4049 item_id
= items
["id"]
4050 item_name
= str(items
.get("name"))
4053 if item
=="networks":
4054 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
4055 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, item_id
)
4056 if sdn_network_id
!= None:
4057 #Delete any port attachment to this network
4059 port_list
= ovim
.get_ports(columns
={'uuid'}, filter={'net_id': sdn_network_id
})
4060 except ovimException
as e
:
4061 raise NfvoException(
4062 "ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
4063 HTTP_Internal_Server_Error
)
4065 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
4066 for port
in port_list
:
4067 vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, item_id
, port
['uuid'])
4069 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
4071 mydb
.delete_row(FROM
='instance_nets', WHERE
={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id
, 'vim_net_id': item_id
})
4072 except db_base_Exception
as e
:
4073 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence
) +
4074 str(e
), HTTP_Internal_Server_Error
)
4076 #Delete the SDN network
4078 ovim
.delete_network(sdn_network_id
)
4079 except ovimException
as e
:
4080 logger
.error("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
), exc_info
=True)
4081 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
),
4082 HTTP_Internal_Server_Error
)
4084 content
= myvim
.delete_network(item_id
)
4085 elif item
=="tenants":
4086 content
= myvim
.delete_tenant(item_id
)
4087 elif item
== "images":
4088 content
= myvim
.delete_image(item_id
)
4090 raise NfvoException(item
+ "?", HTTP_Method_Not_Allowed
)
4091 except vimconn
.vimconnException
as e
:
4092 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
4093 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item
, name
, str(e
)), e
.http_code
)
4095 return "{} {} {} deleted".format(item
[:-1], item_id
,item_name
)
4098 def vim_action_create(mydb
, tenant_id
, datacenter
, item
, descriptor
):
4099 #get datacenter info
4100 logger
.debug("vim_action_create descriptor %s", str(descriptor
))
4101 if tenant_id
== "any":
4103 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4105 if item
=="networks":
4106 net
= descriptor
["network"]
4107 net_name
= net
.pop("name")
4108 net_type
= net
.pop("type", "bridge")
4109 net_public
= net
.pop("shared", False)
4110 net_ipprofile
= net
.pop("ip_profile", None)
4111 net_vlan
= net
.pop("vlan", None)
4112 content
= myvim
.new_network(net_name
, net_type
, net_ipprofile
, shared
=net_public
, vlan
=net_vlan
) #, **net)
4114 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
4115 if get_sdn_controller_id(mydb
, datacenter
) != None and (net_type
== 'data' or net_type
== 'ptp'):
4118 sdn_network
['vlan'] = net_vlan
4119 sdn_network
['type'] = net_type
4120 sdn_network
['name'] = net_name
4121 ovim_content
= ovim
.new_network(sdn_network
)
4122 except ovimException
as e
:
4123 self
.logger
.error("ovimException creating SDN network={} ".format(
4124 sdn_network
) + str(e
), exc_info
=True)
4125 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network
) + str(e
),
4126 HTTP_Internal_Server_Error
)
4128 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
4129 # use instance_scenario_id=None to distinguish from real instaces of nets
4130 correspondence
= {'instance_scenario_id': None, 'sdn_net_id': ovim_content
, 'vim_net_id': content
}
4131 #obtain datacenter_tenant_id
4132 correspondence
['datacenter_tenant_id'] = mydb
.get_rows(SELECT
=('uuid',), FROM
='datacenter_tenants', WHERE
={'datacenter_id': datacenter
})[0]['uuid']
4135 mydb
.new_row('instance_nets', correspondence
, add_uuid
=True)
4136 except db_base_Exception
as e
:
4137 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: ".format(correspondence
) +
4138 str(e
), HTTP_Internal_Server_Error
)
4139 elif item
=="tenants":
4140 tenant
= descriptor
["tenant"]
4141 content
= myvim
.new_tenant(tenant
["name"], tenant
.get("description"))
4143 raise NfvoException(item
+ "?", HTTP_Method_Not_Allowed
)
4144 except vimconn
.vimconnException
as e
:
4145 raise NfvoException("Not possible to create {} at VIM: {}".format(item
, str(e
)), e
.http_code
)
4147 return vim_action_get(mydb
, tenant_id
, datacenter
, item
, content
)
4149 def sdn_controller_create(mydb
, tenant_id
, sdn_controller
):
4150 data
= ovim
.new_of_controller(sdn_controller
)
4151 logger
.debug('New SDN controller created with uuid {}'.format(data
))
4154 def sdn_controller_update(mydb
, tenant_id
, controller_id
, sdn_controller
):
4155 data
= ovim
.edit_of_controller(controller_id
, sdn_controller
)
4156 msg
= 'SDN controller {} updated'.format(data
)
4160 def sdn_controller_list(mydb
, tenant_id
, controller_id
=None):
4161 if controller_id
== None:
4162 data
= ovim
.get_of_controllers()
4164 data
= ovim
.show_of_controller(controller_id
)
4166 msg
= 'SDN controller list:\n {}'.format(data
)
4170 def sdn_controller_delete(mydb
, tenant_id
, controller_id
):
4171 select_
= ('uuid', 'config')
4172 datacenters
= mydb
.get_rows(FROM
='datacenters', SELECT
=select_
)
4173 for datacenter
in datacenters
:
4174 if datacenter
['config']:
4175 config
= yaml
.load(datacenter
['config'])
4176 if 'sdn-controller' in config
and config
['sdn-controller'] == controller_id
:
4177 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id
, datacenter
['uuid']), HTTP_Conflict
)
4179 data
= ovim
.delete_of_controller(controller_id
)
4180 msg
= 'SDN controller {} deleted'.format(data
)
4184 def datacenter_sdn_port_mapping_set(mydb
, tenant_id
, datacenter_id
, sdn_port_mapping
):
4185 controller
= mydb
.get_rows(FROM
="datacenters", SELECT
=("config",), WHERE
={"uuid":datacenter_id
})
4186 if len(controller
) < 1:
4187 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id
), HTTP_Not_Found
)
4190 sdn_controller_id
= yaml
.load(controller
[0]["config"])["sdn-controller"]
4192 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id
), HTTP_Bad_Request
)
4194 sdn_controller
= ovim
.show_of_controller(sdn_controller_id
)
4195 switch_dpid
= sdn_controller
["dpid"]
4198 for compute_node
in sdn_port_mapping
:
4199 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
4201 element
["compute_node"] = compute_node
["compute_node"]
4202 for port
in compute_node
["ports"]:
4203 element
["pci"] = port
.get("pci")
4204 element
["switch_port"] = port
.get("switch_port")
4205 element
["switch_mac"] = port
.get("switch_mac")
4206 if not element
["pci"] or not (element
["switch_port"] or element
["switch_mac"]):
4207 raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'"
4208 " or 'switch_mac'", HTTP_Bad_Request
)
4209 maps
.append(dict(element
))
4211 return ovim
.set_of_port_mapping(maps
, ofc_id
=sdn_controller_id
, switch_dpid
=switch_dpid
, region
=datacenter_id
)
4213 def datacenter_sdn_port_mapping_list(mydb
, tenant_id
, datacenter_id
):
4214 maps
= ovim
.get_of_port_mappings(db_filter
={"region": datacenter_id
})
4217 "sdn-controller": None,
4218 "datacenter-id": datacenter_id
,
4220 "ports_mapping": list()
4223 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id
)
4224 if datacenter
['config']:
4225 config
= yaml
.load(datacenter
['config'])
4226 if 'sdn-controller' in config
:
4227 controller_id
= config
['sdn-controller']
4228 sdn_controller
= sdn_controller_list(mydb
, tenant_id
, controller_id
)
4229 result
["sdn-controller"] = controller_id
4230 result
["dpid"] = sdn_controller
["dpid"]
4232 if result
["sdn-controller"] == None:
4233 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id
), HTTP_Bad_Request
)
4234 if result
["dpid"] == None:
4235 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result
["sdn-controller"]),
4236 HTTP_Internal_Server_Error
)
4241 ports_correspondence_dict
= dict()
4243 if result
["sdn-controller"] != link
["ofc_id"]:
4244 raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error
)
4245 if result
["dpid"] != link
["switch_dpid"]:
4246 raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error
)
4248 element
["pci"] = link
["pci"]
4249 if link
["switch_port"]:
4250 element
["switch_port"] = link
["switch_port"]
4251 if link
["switch_mac"]:
4252 element
["switch_mac"] = link
["switch_mac"]
4254 if not link
["compute_node"] in ports_correspondence_dict
:
4256 content
["compute_node"] = link
["compute_node"]
4257 content
["ports"] = list()
4258 ports_correspondence_dict
[link
["compute_node"]] = content
4260 ports_correspondence_dict
[link
["compute_node"]]["ports"].append(element
)
4262 for key
in sorted(ports_correspondence_dict
):
4263 result
["ports_mapping"].append(ports_correspondence_dict
[key
])
4267 def datacenter_sdn_port_mapping_delete(mydb
, tenant_id
, datacenter_id
):
4268 return ovim
.clear_of_port_mapping(db_filter
={"region":datacenter_id
})