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')
74 class NfvoException(Exception):
75 def __init__(self
, message
, http_code
):
76 self
.http_code
= http_code
77 Exception.__init
__(self
, message
)
83 if task_id
<= last_task_id
:
84 task_id
= last_task_id
+ 0.000001
85 last_task_id
= task_id
86 return "ACTION-{:.6f}".format(task_id
)
87 # return (t.strftime("%Y%m%dT%H%M%S.{}%Z", t.localtime(task_id))).format(int((task_id % 1)*1e6))
90 def new_task(name
, params
, depends
=None):
92 task_id
= get_task_id()
93 task
= {"status": "enqueued", "id": task_id
, "name": name
, "params": params
}
95 task
["depends"] = depends
100 return True if id[:5] == "TASK-" else False
103 def get_non_used_vim_name(datacenter_name
, datacenter_id
, tenant_name
, tenant_id
):
104 name
= datacenter_name
[:16]
105 if name
not in vim_threads
["names"]:
106 vim_threads
["names"].append(name
)
108 name
= datacenter_name
[:16] + "." + tenant_name
[:16]
109 if name
not in vim_threads
["names"]:
110 vim_threads
["names"].append(name
)
112 name
= datacenter_id
+ "-" + tenant_id
113 vim_threads
["names"].append(name
)
117 def start_service(mydb
):
118 global db
, global_config
119 db
= nfvo_db
.nfvo_db()
120 db
.connect(global_config
['db_host'], global_config
['db_user'], global_config
['db_passwd'], global_config
['db_name'])
123 # Initialize openvim for SDN control
124 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
125 # TODO: review ovim.py to delete not needed configuration
126 ovim_configuration
= {
127 'logger_name': 'openmano.ovim',
128 'network_vlan_range_start': 1000,
129 'network_vlan_range_end': 4096,
130 'db_name': global_config
["db_ovim_name"],
131 'db_host': global_config
["db_ovim_host"],
132 'db_user': global_config
["db_ovim_user"],
133 'db_passwd': global_config
["db_ovim_passwd"],
136 'network_type': 'bridge',
137 #TODO: log_level_of should not be needed. To be modified in ovim
138 'log_level_of': 'DEBUG'
141 ovim
= ovim_module
.ovim(ovim_configuration
)
144 from_
= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
145 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
146 select_
= ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
147 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
148 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
149 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
150 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
)
152 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
153 'datacenter_id': vim
.get('datacenter_id')}
155 extra
.update(yaml
.load(vim
["config"]))
156 if vim
.get('dt_config'):
157 extra
.update(yaml
.load(vim
["dt_config"]))
158 if vim
["type"] not in vimconn_imported
:
161 module
= "vimconn_" + vim
["type"]
162 pkg
= __import__("osm_ro." + module
)
163 vim_conn
= getattr(pkg
, module
)
164 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
165 # vim_conn = imp.load_module(vim["type"], *module_info)
166 vimconn_imported
[vim
["type"]] = vim_conn
167 except (IOError, ImportError) as e
:
168 # if module_info and module_info[0]:
169 # file.close(module_info[0])
170 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
171 vim
["type"], module
, type(e
).__name
__, str(e
)), HTTP_Bad_Request
)
173 thread_id
= vim
['datacenter_tenant_id']
174 vim_persistent_info
[thread_id
] = {}
177 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
178 myvim
= vimconn_imported
[ vim
["type"] ].vimconnector(
179 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
180 tenant_id
=vim
['vim_tenant_id'], tenant_name
=vim
['vim_tenant_name'],
181 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
182 user
=vim
['user'], passwd
=vim
['passwd'],
183 config
=extra
, persistent_info
=vim_persistent_info
[thread_id
]
185 except Exception as e
:
186 raise NfvoException("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, e
),
187 HTTP_Internal_Server_Error
)
188 thread_name
= get_non_used_vim_name(vim
['datacenter_name'], vim
['vim_tenant_id'], vim
['vim_tenant_name'],
189 vim
['vim_tenant_id'])
190 new_thread
= vim_thread
.vim_thread(myvim
, task_lock
, thread_name
, vim
['datacenter_name'],
191 vim
['datacenter_tenant_id'], db
=db
, db_lock
=db_lock
, ovim
=ovim
)
193 vim_threads
["running"][thread_id
] = new_thread
194 except db_base_Exception
as e
:
195 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
196 except ovim_module
.ovimException
as e
:
198 if message
[:22] == "DATABASE wrong version":
199 message
= "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
200 "at host {dbhost}".format(
201 msg
=message
[22:-3], dbname
=global_config
["db_ovim_name"],
202 dbuser
=global_config
["db_ovim_user"], dbpass
=global_config
["db_ovim_passwd"],
203 ver
=message
[-3:-1], dbhost
=global_config
["db_ovim_host"])
204 raise NfvoException(message
, HTTP_Bad_Request
)
208 global ovim
, global_config
211 for thread_id
,thread
in vim_threads
["running"].items():
212 thread
.insert_task("exit")
213 vim_threads
["deleting"][thread_id
] = thread
214 vim_threads
["running"] = {}
215 if global_config
and global_config
.get("console_thread"):
216 for thread
in global_config
["console_thread"]:
217 thread
.terminate
= True
220 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config
["version"],
221 global_config
["version_date"] ))
224 def get_flavorlist(mydb
, vnf_id
, nfvo_tenant
=None):
226 return result, content:
227 <0, error_text upon error
228 nb_records, flavor_list on success
231 WHERE_dict
['vnf_id'] = vnf_id
232 if nfvo_tenant
is not None:
233 WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
235 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
236 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
237 flavors
= mydb
.get_rows(FROM
='vms join flavors on vms.flavor_id=flavors.uuid',SELECT
=('flavor_id',),WHERE
=WHERE_dict
)
238 #print "get_flavor_list result:", result
239 #print "get_flavor_list content:", content
241 for flavor
in flavors
:
242 flavorList
.append(flavor
['flavor_id'])
246 def get_imagelist(mydb
, vnf_id
, nfvo_tenant
=None):
248 return result, content:
249 <0, error_text upon error
250 nb_records, flavor_list on success
253 WHERE_dict
['vnf_id'] = vnf_id
254 if nfvo_tenant
is not None:
255 WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
257 #result, content = mydb.get_table(FROM='vms join vnfs on vms-vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
258 images
= mydb
.get_rows(FROM
='vms join images on vms.image_id=images.uuid',SELECT
=('image_id',),WHERE
=WHERE_dict
)
261 imageList
.append(image
['image_id'])
265 def get_vim(mydb
, nfvo_tenant
=None, datacenter_id
=None, datacenter_name
=None, datacenter_tenant_id
=None,
266 vim_tenant
=None, vim_tenant_name
=None, vim_user
=None, vim_passwd
=None):
267 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
268 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
269 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
270 raise exception upon error
273 if nfvo_tenant
is not None: WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
274 if datacenter_id
is not None: WHERE_dict
['d.uuid'] = datacenter_id
275 if datacenter_tenant_id
is not None: WHERE_dict
['datacenter_tenant_id'] = datacenter_tenant_id
276 if datacenter_name
is not None: WHERE_dict
['d.name'] = datacenter_name
277 if vim_tenant
is not None: WHERE_dict
['dt.vim_tenant_id'] = vim_tenant
278 if vim_tenant_name
is not None: WHERE_dict
['vim_tenant_name'] = vim_tenant_name
279 if nfvo_tenant
or vim_tenant
or vim_tenant_name
or datacenter_tenant_id
:
280 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'
281 select_
= ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
282 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
283 'user','passwd', 'dt.config as dt_config')
285 from_
= 'datacenters as d'
286 select_
= ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
288 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
, WHERE
=WHERE_dict
)
291 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
292 'datacenter_id': vim
.get('datacenter_id')}
294 extra
.update(yaml
.load(vim
["config"]))
295 if vim
.get('dt_config'):
296 extra
.update(yaml
.load(vim
["dt_config"]))
297 if vim
["type"] not in vimconn_imported
:
300 module
= "vimconn_" + vim
["type"]
301 pkg
= __import__("osm_ro." + module
)
302 vim_conn
= getattr(pkg
, module
)
303 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
304 # vim_conn = imp.load_module(vim["type"], *module_info)
305 vimconn_imported
[vim
["type"]] = vim_conn
306 except (IOError, ImportError) as e
:
307 # if module_info and module_info[0]:
308 # file.close(module_info[0])
309 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
310 vim
["type"], module
, type(e
).__name
__, str(e
)), HTTP_Bad_Request
)
313 if 'datacenter_tenant_id' in vim
:
314 thread_id
= vim
["datacenter_tenant_id"]
315 if thread_id
not in vim_persistent_info
:
316 vim_persistent_info
[thread_id
] = {}
317 persistent_info
= vim_persistent_info
[thread_id
]
321 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
322 vim_dict
[ vim
['datacenter_id'] ] = vimconn_imported
[ vim
["type"] ].vimconnector(
323 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
324 tenant_id
=vim
.get('vim_tenant_id',vim_tenant
),
325 tenant_name
=vim
.get('vim_tenant_name',vim_tenant_name
),
326 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
327 user
=vim
.get('user',vim_user
), passwd
=vim
.get('passwd',vim_passwd
),
328 config
=extra
, persistent_info
=persistent_info
330 except Exception as e
:
331 raise NfvoException("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, str(e
)), HTTP_Internal_Server_Error
)
333 except db_base_Exception
as e
:
334 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
337 def rollback(mydb
, vims
, rollback_list
):
339 #delete things by reverse order
340 for i
in range(len(rollback_list
)-1, -1, -1):
341 item
= rollback_list
[i
]
342 if item
["where"]=="vim":
343 if item
["vim_id"] not in vims
:
345 if is_task_id(item
["uuid"]):
347 vim
= vims
[item
["vim_id"]]
349 if item
["what"]=="image":
350 vim
.delete_image(item
["uuid"])
351 mydb
.delete_row(FROM
="datacenters_images", WHERE
={"datacenter_vim_id": vim
["id"], "vim_id":item
["uuid"]})
352 elif item
["what"]=="flavor":
353 vim
.delete_flavor(item
["uuid"])
354 mydb
.delete_row(FROM
="datacenters_flavors", WHERE
={"datacenter_id": vim
["id"], "vim_id":item
["uuid"]})
355 elif item
["what"]=="network":
356 vim
.delete_network(item
["uuid"])
357 elif item
["what"]=="vm":
358 vim
.delete_vminstance(item
["uuid"])
359 except vimconn
.vimconnException
as e
:
360 logger
.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item
['what'], item
["uuid"], str(e
))
361 undeleted_items
.append("{} {} from VIM {}".format(item
['what'], item
["uuid"], vim
["name"]))
362 except db_base_Exception
as e
:
363 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item
['what'], item
["uuid"], str(e
))
367 if item
["what"]=="image":
368 mydb
.delete_row(FROM
="images", WHERE
={"uuid": item
["uuid"]})
369 elif item
["what"]=="flavor":
370 mydb
.delete_row(FROM
="flavors", WHERE
={"uuid": item
["uuid"]})
371 except db_base_Exception
as e
:
372 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item
['what'], item
["uuid"], str(e
))
373 undeleted_items
.append("{} '{}'".format(item
['what'], item
["uuid"]))
374 if len(undeleted_items
)==0:
375 return True," Rollback successful."
377 return False," Rollback fails to delete: " + str(undeleted_items
)
380 def check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1):
382 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
384 for vnfc
in vnf_descriptor
["vnf"]["VNFC"]:
386 #dataplane interfaces
387 for numa
in vnfc
.get("numas",() ):
388 for interface
in numa
.get("interfaces",()):
389 if interface
["name"] in name_dict
:
391 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
392 vnfc
["name"], interface
["name"]),
394 name_dict
[ interface
["name"] ] = "underlay"
396 for interface
in vnfc
.get("bridge-ifaces",() ):
397 if interface
["name"] in name_dict
:
399 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
400 vnfc
["name"], interface
["name"]),
402 name_dict
[ interface
["name"] ] = "overlay"
403 vnfc_interfaces
[ vnfc
["name"] ] = name_dict
404 # check bood-data info
405 # if "boot-data" in vnfc:
406 # # check that user-data is incompatible with users and config-files
407 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
408 # raise NfvoException(
409 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
412 #check if the info in external_connections matches with the one in the vnfcs
414 for external_connection
in vnf_descriptor
["vnf"].get("external-connections",() ):
415 if external_connection
["name"] in name_list
:
417 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
418 external_connection
["name"]),
420 name_list
.append(external_connection
["name"])
421 if external_connection
["VNFC"] not in vnfc_interfaces
:
423 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
424 external_connection
["name"], external_connection
["VNFC"]),
427 if external_connection
["local_iface_name"] not in vnfc_interfaces
[ external_connection
["VNFC"] ]:
429 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
430 external_connection
["name"],
431 external_connection
["local_iface_name"]),
434 #check if the info in internal_connections matches with the one in the vnfcs
436 for internal_connection
in vnf_descriptor
["vnf"].get("internal-connections",() ):
437 if internal_connection
["name"] in name_list
:
439 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
440 internal_connection
["name"]),
442 name_list
.append(internal_connection
["name"])
443 #We should check that internal-connections of type "ptp" have only 2 elements
445 if len(internal_connection
["elements"])>2 and (internal_connection
.get("type") == "ptp" or internal_connection
.get("type") == "e-line"):
447 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
448 internal_connection
["name"],
449 'ptp' if vnf_descriptor_version
==1 else 'e-line',
450 'data' if vnf_descriptor_version
==1 else "e-lan"),
452 for port
in internal_connection
["elements"]:
454 iface
= port
["local_iface_name"]
455 if vnf
not in vnfc_interfaces
:
457 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
458 internal_connection
["name"], vnf
),
460 if iface
not in vnfc_interfaces
[ vnf
]:
462 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
463 internal_connection
["name"], iface
),
465 return -HTTP_Bad_Request
,
466 if vnf_descriptor_version
==1 and "type" not in internal_connection
:
467 if vnfc_interfaces
[vnf
][iface
] == "overlay":
468 internal_connection
["type"] = "bridge"
470 internal_connection
["type"] = "data"
471 if vnf_descriptor_version
==2 and "implementation" not in internal_connection
:
472 if vnfc_interfaces
[vnf
][iface
] == "overlay":
473 internal_connection
["implementation"] = "overlay"
475 internal_connection
["implementation"] = "underlay"
476 if (internal_connection
.get("type") == "data" or internal_connection
.get("type") == "ptp" or \
477 internal_connection
.get("implementation") == "underlay") and vnfc_interfaces
[vnf
][iface
] == "overlay":
479 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
480 internal_connection
["name"],
481 iface
, 'bridge' if vnf_descriptor_version
==1 else 'overlay',
482 'data' if vnf_descriptor_version
==1 else 'underlay'),
484 if (internal_connection
.get("type") == "bridge" or internal_connection
.get("implementation") == "overlay") and \
485 vnfc_interfaces
[vnf
][iface
] == "underlay":
487 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
488 internal_connection
["name"], iface
,
489 'data' if vnf_descriptor_version
==1 else 'underlay',
490 'bridge' if vnf_descriptor_version
==1 else 'overlay'),
494 def create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=None):
496 if only_create_at_vim
:
497 image_mano_id
= image_dict
['uuid']
498 if return_on_error
== None:
499 return_on_error
= True
501 if image_dict
['location']:
502 images
= mydb
.get_rows(FROM
="images", WHERE
={'location':image_dict
['location'], 'metadata':image_dict
['metadata']})
504 images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name':image_dict
['universal_name'], 'checksum':image_dict
['checksum']})
506 image_mano_id
= images
[0]['uuid']
508 #create image in MANO DB
509 temp_image_dict
={'name':image_dict
['name'], 'description':image_dict
.get('description',None),
510 'location':image_dict
['location'], 'metadata':image_dict
.get('metadata',None),
511 'universal_name':image_dict
['universal_name'] , 'checksum':image_dict
['checksum']
513 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
514 image_mano_id
= mydb
.new_row('images', temp_image_dict
, add_uuid
=True)
515 rollback_list
.append({"where":"mano", "what":"image","uuid":image_mano_id
})
516 #create image at every vim
517 for vim_id
,vim
in vims
.iteritems():
518 datacenter_vim_id
= vim
["config"]["datacenter_tenant_id"]
519 image_created
="false"
521 image_db
= mydb
.get_rows(FROM
="datacenters_images",
522 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'image_id': image_mano_id
})
523 #look at VIM if this image exist
525 if image_dict
['location'] is not None:
526 image_vim_id
= vim
.get_image_id_from_path(image_dict
['location'])
529 filter_dict
['name'] = image_dict
['universal_name']
530 if image_dict
.get('checksum') != None:
531 filter_dict
['checksum'] = image_dict
['checksum']
532 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
533 vim_images
= vim
.get_image_list(filter_dict
)
534 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
535 if len(vim_images
) > 1:
536 raise vimconn
.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict
)), HTTP_Conflict
)
537 elif len(vim_images
) == 0:
538 raise vimconn
.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict
)))
540 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
541 image_vim_id
= vim_images
[0]['id']
543 except vimconn
.vimconnNotFoundException
as e
:
544 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
546 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
547 if image_dict
['location']:
548 image_vim_id
= vim
.new_image(image_dict
)
549 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"image","uuid":image_vim_id
})
552 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
553 raise vimconn
.vimconnException(str(e
))
554 except vimconn
.vimconnException
as e
:
556 logger
.error("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
559 logger
.warn("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
561 except vimconn
.vimconnException
as e
:
563 logger
.error("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
565 logger
.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
568 #if we reach here, the image has been created or existed
570 #add new vim_id at datacenters_images
571 mydb
.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id
,
572 'image_id':image_mano_id
,
573 'vim_id': image_vim_id
,
574 'created':image_created
})
575 elif image_db
[0]["vim_id"]!=image_vim_id
:
576 #modify existing vim_id at datacenters_images
577 mydb
.update_rows('datacenters_images', UPDATE
={'vim_id':image_vim_id
}, WHERE
={'datacenter_vim_id':vim_id
, 'image_id':image_mano_id
})
579 return image_vim_id
if only_create_at_vim
else image_mano_id
582 def create_or_use_flavor(mydb
, vims
, flavor_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
= None):
583 temp_flavor_dict
= {'disk':flavor_dict
.get('disk',1),
584 'ram':flavor_dict
.get('ram'),
585 'vcpus':flavor_dict
.get('vcpus'),
587 if 'extended' in flavor_dict
and flavor_dict
['extended']==None:
588 del flavor_dict
['extended']
589 if 'extended' in flavor_dict
:
590 temp_flavor_dict
['extended']=yaml
.safe_dump(flavor_dict
['extended'],default_flow_style
=True,width
=256)
592 #look if flavor exist
593 if only_create_at_vim
:
594 flavor_mano_id
= flavor_dict
['uuid']
595 if return_on_error
== None:
596 return_on_error
= True
598 flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
600 flavor_mano_id
= flavors
[0]['uuid']
603 #create one by one the images of aditional disks
604 dev_image_list
=[] #list of images
605 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None:
607 for device
in flavor_dict
['extended'].get('devices',[]):
608 if "image" not in device
and "image name" not in device
:
611 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
612 image_dict
['universal_name']=device
.get('image name')
613 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
614 image_dict
['location']=device
.get('image')
615 #image_dict['new_location']=vnfc.get('image location')
616 image_dict
['checksum']=device
.get('image checksum')
617 image_metadata_dict
= device
.get('image metadata', None)
618 image_metadata_str
= None
619 if image_metadata_dict
!= None:
620 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
621 image_dict
['metadata']=image_metadata_str
622 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
623 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
624 dev_image_list
.append(image_id
)
626 temp_flavor_dict
['name'] = flavor_dict
['name']
627 temp_flavor_dict
['description'] = flavor_dict
.get('description',None)
628 content
= mydb
.new_row('flavors', temp_flavor_dict
, add_uuid
=True)
629 flavor_mano_id
= content
630 rollback_list
.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id
})
631 #create flavor at every vim
632 if 'uuid' in flavor_dict
:
633 del flavor_dict
['uuid']
635 for vim_id
,vim
in vims
.items():
636 datacenter_vim_id
= vim
["config"]["datacenter_tenant_id"]
637 flavor_created
="false"
639 flavor_db
= mydb
.get_rows(FROM
="datacenters_flavors",
640 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
})
641 #look at VIM if this flavor exist SKIPPED
642 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
644 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
648 # Create the flavor in VIM
649 # Translate images at devices from MANO id to VIM id
651 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None and "devices" in flavor_dict
['extended']:
652 # make a copy of original devices
655 for device
in flavor_dict
["extended"].get("devices",[]):
658 devices_original
.append(dev
)
659 if 'image' in device
:
661 if 'image metadata' in device
:
662 del device
['image metadata']
663 if 'image checksum' in device
:
664 del device
['image checksum']
666 for index
in range(0,len(devices_original
)) :
667 device
=devices_original
[index
]
668 if "image" not in device
and "image name" not in device
:
670 disk_list
.append({'size': device
.get('size', default_volume_size
)})
673 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
674 image_dict
['universal_name']=device
.get('image name')
675 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
676 image_dict
['location']=device
.get('image')
677 # image_dict['new_location']=device.get('image location')
678 image_dict
['checksum']=device
.get('image checksum')
679 image_metadata_dict
= device
.get('image metadata', None)
680 image_metadata_str
= None
681 if image_metadata_dict
!= None:
682 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
683 image_dict
['metadata']=image_metadata_str
684 image_mano_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=return_on_error
)
685 image_dict
["uuid"]=image_mano_id
686 image_vim_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=True, return_on_error
=return_on_error
)
688 #save disk information (image must be based on and size
689 disk_list
.append({'image_id': image_vim_id
, 'size': device
.get('size', default_volume_size
)})
691 flavor_dict
["extended"]["devices"][index
]['imageRef']=image_vim_id
694 #check that this vim_id exist in VIM, if not create
695 flavor_vim_id
=flavor_db
[0]["vim_id"]
697 vim
.get_flavor(flavor_vim_id
)
698 continue #flavor exist
699 except vimconn
.vimconnException
:
701 #create flavor at vim
702 logger
.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim
["name"])
705 flavor_vim_id
=vim
.get_flavor_id_from_data(flavor_dict
)
706 flavor_create
="false"
707 except vimconn
.vimconnException
as e
:
710 if not flavor_vim_id
:
711 flavor_vim_id
= vim
.new_flavor(flavor_dict
)
712 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"flavor","uuid":flavor_vim_id
})
713 flavor_created
="true"
714 except vimconn
.vimconnException
as e
:
716 logger
.error("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
718 logger
.warn("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
721 #if reach here the flavor has been create or exist
722 if len(flavor_db
)==0:
723 #add new vim_id at datacenters_flavors
724 extended_devices_yaml
= None
725 if len(disk_list
) > 0:
726 extended_devices
= dict()
727 extended_devices
['disks'] = disk_list
728 extended_devices_yaml
= yaml
.safe_dump(extended_devices
,default_flow_style
=True,width
=256)
729 mydb
.new_row('datacenters_flavors',
730 {'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
, 'vim_id': flavor_vim_id
,
731 'created': flavor_created
, 'extended': extended_devices_yaml
})
732 elif flavor_db
[0]["vim_id"]!=flavor_vim_id
:
733 #modify existing vim_id at datacenters_flavors
734 mydb
.update_rows('datacenters_flavors', UPDATE
={'vim_id':flavor_vim_id
},
735 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
})
737 return flavor_vim_id
if only_create_at_vim
else flavor_mano_id
740 def get_str(obj
, field
, length
):
742 Obtain the str value,
747 value
= obj
.get(field
)
748 if value
is not None:
749 value
= str(value
)[:length
]
752 def _lookfor_or_create_image(db_image
, mydb
, descriptor
):
754 fill image content at db_image dictionary. Check if the image with this image and checksum exist
755 :param db_image: dictionary to insert data
756 :param mydb: database connector
757 :param descriptor: yang descriptor
758 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
761 db_image
["name"] = get_str(descriptor
, "image", 255)
762 db_image
["checksum"] = get_str(descriptor
, "image-checksum", 32)
763 if not db_image
["checksum"]: # Ensure that if empty string, None is stored
764 db_image
["checksum"] = None
765 if db_image
["name"].startswith("/"):
766 db_image
["location"] = db_image
["name"]
767 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'location': db_image
["location"]})
769 db_image
["universal_name"] = db_image
["name"]
770 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name': db_image
['universal_name'],
771 'checksum': db_image
['checksum']})
773 return existing_images
[0]["uuid"]
775 image_uuid
= str(uuid4())
776 db_image
["uuid"] = image_uuid
779 def new_vnfd_v3(mydb
, tenant_id
, vnf_descriptor
):
781 Parses an OSM IM vnfd_catalog and insert at DB
784 :param vnf_descriptor:
785 :return: The list of cretated vnf ids
788 myvnfd
= vnfd_catalog
.vnfd()
790 pybindJSONDecoder
.load_ietf_json(vnf_descriptor
, None, None, obj
=myvnfd
)
791 except Exception as e
:
792 raise NfvoException("Invalid yang descriptor format " + str(e
), HTTP_Bad_Request
)
802 for rift_vnfd
in myvnfd
.vnfd_catalog
.vnfd
.itervalues():
803 vnfd
= rift_vnfd
.get()
806 vnf_uuid
= str(uuid4())
807 uuid_list
.append(vnf_uuid
)
808 vnfd_uuid_list
.append(vnf_uuid
)
811 "osm_id": get_str(vnfd
, "id", 255),
812 "name": get_str(vnfd
, "name", 255),
813 "description": get_str(vnfd
, "description", 255),
814 "tenant_id": tenant_id
,
815 "vendor": get_str(vnfd
, "vendor", 255),
816 "short_name": get_str(vnfd
, "short-name", 255),
817 "descriptor": str(vnf_descriptor
)[:60000]
820 # table nets (internal-vld)
821 net_id2uuid
= {} # for mapping interface with network
822 for vld
in vnfd
.get("internal-vld").itervalues():
823 net_uuid
= str(uuid4())
824 uuid_list
.append(net_uuid
)
826 "name": get_str(vld
, "name", 255),
829 "description": get_str(vld
, "description", 255),
830 "type": "bridge", # TODO adjust depending on connection point type
832 net_id2uuid
[vld
.get("id")] = net_uuid
833 db_nets
.append(db_net
)
837 vdu_id2db_table_index
= {}
838 for vdu
in vnfd
.get("vdu").itervalues():
839 vm_uuid
= str(uuid4())
840 uuid_list
.append(vm_uuid
)
843 "osm_id": get_str(vdu
, "id", 255),
844 "name": get_str(vdu
, "name", 255),
845 "description": get_str(vdu
, "description", 255),
848 vdu_id2uuid
[db_vm
["osm_id"]] = vm_uuid
849 vdu_id2db_table_index
[db_vm
["osm_id"]] = db_vms_index
851 db_vm
["count"] = int(vdu
["count"])
854 image_present
= False
858 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, vdu
)
860 image_uuid
= db_image
["uuid"]
861 db_images
.append(db_image
)
862 db_vm
["image_id"] = image_uuid
866 if vdu
.get("volumes"):
867 for volume_key
in sorted(vdu
["volumes"]):
868 volume
= vdu
["volumes"][volume_key
]
869 if not image_present
:
870 # Convert the first volume to vnfc.image
873 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, volume
)
875 image_uuid
= db_image
["uuid"]
876 db_images
.append(db_image
)
877 db_vm
["image_id"] = image_uuid
879 # Add Openmano devices
881 device
["type"] = str(volume
.get("device-type"))
882 if volume
.get("size"):
883 device
["size"] = int(volume
["size"])
884 if volume
.get("image"):
885 device
["image name"] = str(volume
["image"])
886 if volume
.get("image-checksum"):
887 device
["image checksum"] = str(volume
["image-checksum"])
888 devices
.append(device
)
892 "name": get_str(vdu
, "name", 250) + "-flv",
893 "vcpus": int(vdu
["vm-flavor"].get("vcpu-count", 1)),
894 "ram": int(vdu
["vm-flavor"].get("memory-mb", 1)),
895 "disk": int(vdu
["vm-flavor"].get("storage-gb", 1)),
901 extended
["devices"] = devices
902 if vdu
.get("guest-epa"): # TODO or dedicated_int:
904 if vdu
["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
905 numa_node_policy
= vdu
["guest-epa"].get("numa-node-policy")
906 if numa_node_policy
.get("node"):
907 numa_node
= numa_node_policy
.node
[0]
908 if numa_node
.get("num-cores"):
909 numa
["cores"] = numa_node
["num-cores"]
911 if numa_node
.get("paired-threads"):
912 if numa_node
["paired-threads"].get("num-paired-threads"):
913 numa
["paired-threads"] = numa_node
["paired-threads"]["num-paired-threads"]
915 if len(numa_node
["paired-threads"].get("paired-thread-ids")) > 0:
916 numa
["paired-threads-id"] = []
917 for pair
in numa_node
["paired-threads"]["paired-thread-ids"].itervalues
:
918 numa
["paired-threads-id"].append(
919 (str(pair
["thread-a"]), str(pair
["thread-b"]))
921 if numa_node
.get("num-threads"):
922 numa
["threads"] = numa_node
["num-threads"]
924 if numa_node
.get("memory-mb"):
925 numa
["memory"] = max(int(numa_node
["memory-mb"] / 1024), 1)
926 if vdu
["guest-epa"].get("mempage-size"):
927 if vdu
["guest-epa"]["mempage-size"] != "SMALL":
928 numa
["memory"] = max(int(db_flavor
["ram"] / 1024), 1)
929 if vdu
["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set
:
930 if vdu
["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
931 if vdu
["guest-epa"].get("cpu-thread-pinning-policy") and \
932 vdu
["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
933 numa
["cores"] = max(db_flavor
["vcpus"], 1)
935 numa
["threads"] = max(db_flavor
["vcpus"], 1)
937 extended
["numas"] = [numa
]
939 extended_text
= yaml
.safe_dump(extended
, default_flow_style
=True, width
=256)
940 db_flavor
["extended"] = extended_text
941 # look if flavor exist
943 temp_flavor_dict
= {'disk': db_flavor
.get('disk', 1),
944 'ram': db_flavor
.get('ram'),
945 'vcpus': db_flavor
.get('vcpus'),
946 'extended': db_flavor
.get('extended')
948 existing_flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
950 flavor_uuid
= existing_flavors
[0]["uuid"]
952 flavor_uuid
= str(uuid4())
953 uuid_list
.append(flavor_uuid
)
954 db_flavor
["uuid"] = flavor_uuid
955 db_flavors
.append(db_flavor
)
956 db_vm
["flavor_id"] = flavor_uuid
960 if vdu
.get("cloud-init"):
961 boot_data
["user-data"] = vdu
["cloud-init"]
962 elif vdu
.get("cloud-init-file"):
963 # TODO Where this file content is present???
964 # boot_data["user-data"] = rift_vnfd.files[vdu["cloud-init-file"]]
965 boot_data
["user-data"] = str(vdu
["cloud-init-file"])
967 if vdu
.get("supplemental-boot-data"):
968 if vdu
["supplemental-boot-data"].get('boot-data-drive'):
969 boot_data
['boot-data-drive'] = True
970 if vdu
["supplemental-boot-data"].get('config-file'):
971 om_cfgfile_list
= list()
972 for custom_config_file
in vdu
["supplemental-boot-data"]['config-file'].itervalues():
973 # TODO Where this file content is present???
974 cfg_source
= str(custom_config_file
["source"])
975 om_cfgfile_list
.append({"dest": custom_config_file
["dest"],
976 "content": cfg_source
})
977 boot_data
['config-files'] = om_cfgfile_list
979 db_vm
["boot_data"] = boot_data
984 # table interfaces (internal/external interfaces)
985 cp_name2iface_uuid
= {}
987 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
988 for iface
in vdu
.get("interface").itervalues():
989 iface_uuid
= str(uuid4())
990 uuid_list
.append(iface_uuid
)
993 "internal_name": get_str(iface
, "name", 255),
996 if iface
.get("virtual-interface").get("vpci"):
997 db_interface
["vpci"] = get_str(iface
.get("virtual-interface"), "vpci", 12)
999 if iface
.get("virtual-interface").get("bandwidth"):
1000 bps
= int(iface
.get("virtual-interface").get("bandwidth"))
1001 db_interface
["bw"] = bps
/1000
1003 if iface
.get("virtual-interface").get("type") == "OM-MGMT":
1004 db_interface
["type"] = "mgmt"
1005 elif iface
.get("virtual-interface").get("type") in ("VIRTIO", "E1000"):
1006 db_interface
["type"] = "bridge"
1007 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
1008 elif iface
.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1009 db_interface
["type"] = "data"
1010 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
1012 raise ValueError("Interface type {} not supported".format(iface
.get("virtual-interface").get("type")))
1014 if iface
.get("external-connection-point-ref"):
1016 cp
= vnfd
.get("connection-point")[iface
.get("external-connection-point-ref")]
1017 db_interface
["external_name"] = get_str(cp
, "name", 255)
1018 cp_name2iface_uuid
[db_interface
["external_name"]] = iface_uuid
1019 cp_name2vm_uuid
[db_interface
["external_name"]] = vm_uuid
1020 # TODO add port-security-enable
1021 # if cp.get("port-security-enabled") == False:
1022 # elif cp.get("port-security-enabled") == True:
1025 "Error wrong reference at vnfd['{vnf}'] vdu['{vdu}']:internal-interface['{iface}']:"
1026 "vnfd-connection-point-ref '{cp}' is not present at connection-point".format(
1027 vnf
=vnfd
["id"], vdu
=vdu
["id"], iface
=iface
["name"],
1028 cp
=iface
.get("vnfd-connection-point-ref"))
1030 elif iface
.get("internal-connection-point-ref"):
1032 for vld
in vnfd
.get("internal-vld").itervalues():
1033 for cp
in vld
.get("internal-connection-point").itervalues():
1034 if cp
.get("id-ref") == iface
.get("internal-connection-point-ref"):
1035 db_interface
["net_id"] = net_id2uuid
[vld
.get("id")]
1039 "Error at vnfd['{vnf}'] vdu['{vdu}']:internal-interface['{iface}']:"
1040 "vdu-internal-connection-point-ref '{cp}' is not referenced by any internal-vld".format(
1041 vnf
=vnfd
["id"], vdu
=vdu
["id"], iface
=iface
["name"],
1042 cp
=iface
.get("vdu-internal-connection-point-ref"))
1044 if iface
.get("position") is not None:
1045 db_interface
["created_at"] = int(iface
.get("position")) - 1000
1046 db_interfaces
.append(db_interface
)
1048 # VNF affinity and antiaffinity
1049 for pg
in vnfd
.get("placement-groups").itervalues():
1050 pg_name
= get_str(pg
, "name", 255)
1051 for vdu
in pg
.get("member-vdus").itervalues():
1052 vdu_id
= get_str(vdu
, "member-vdu-ref", 255)
1053 if vdu_id
not in vdu_id2db_table_index
:
1055 "Error at 'vnfd'['{vnf}']:'placement-groups'['{pg}']:'member-vdus':'{vdu}' references a non existing vdu".format(
1056 vnf
=vnfd
["id"], pg
=pg_name
, vdu
=vdu_id
))
1057 db_vms
[vdu_id2db_table_index
[vdu_id
]]["availability_zone"] = pg_name
1058 # TODO consider the case of isolation and not colocation
1059 # if pg.get("strategy") == "ISOLATION":
1061 # VNF mgmt configuration
1063 if vnfd
["mgmt-interface"].get("vdu-id"):
1064 if vnfd
["mgmt-interface"]["vdu-id"] not in vdu_id2uuid
:
1066 "Error at vnfd['{vnf}']:'mgmt-interface':'vdu-id':{vdu} reference a non existing vdu".format(
1067 vnf
=vnfd
["id"], vdu
=vnfd
["mgmt-interface"]["vdu-id"]))
1068 mgmt_access
["vm_id"] = vdu_id2uuid
[vnfd
["mgmt-interface"]["vdu-id"]]
1069 if vnfd
["mgmt-interface"].get("ip-address"):
1070 mgmt_access
["ip-address"] = str(vnfd
["mgmt-interface"].get("ip-address"))
1071 if vnfd
["mgmt-interface"].get("cp"):
1072 if vnfd
["mgmt-interface"]["cp"] not in cp_name2iface_uuid
:
1074 "Error at vnfd['{vnf}']:'mgmt-interface':'cp':{cp} reference a non existing connection-point".
1075 format(vnf
=vnfd
["id"], cp
=vnfd
["mgmt-interface"]["cp"]))
1076 mgmt_access
["vm_id"] = cp_name2vm_uuid
[vnfd
["mgmt-interface"]["cp"]]
1077 mgmt_access
["interface_id"] = cp_name2iface_uuid
[vnfd
["mgmt-interface"]["cp"]]
1078 default_user
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1081 mgmt_access
["default_user"] = default_user
1083 db_vnf
["mgmt_access"] = yaml
.safe_dump(mgmt_access
, default_flow_style
=True, width
=256)
1085 db_vnfs
.append(db_vnf
)
1089 {"images": db_images
},
1090 {"flavors": db_flavors
},
1092 {"interfaces": db_interfaces
},
1095 logger
.debug("create_vnf Deployment done vnfDict: %s",
1096 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
1097 mydb
.new_rows(db_tables
, uuid_list
)
1098 return vnfd_uuid_list
1099 except Exception as e
:
1100 logger
.error("Exception {}".format(e
))
1101 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
1104 def new_vnf(mydb
, tenant_id
, vnf_descriptor
):
1105 global global_config
1107 # Step 1. Check the VNF descriptor
1108 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1)
1109 # Step 2. Check tenant exist
1111 if tenant_id
!= "any":
1112 check_tenant(mydb
, tenant_id
)
1113 if "tenant_id" in vnf_descriptor
["vnf"]:
1114 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1115 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1118 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1119 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1120 if global_config
["auto_push_VNF_to_VIMs"]:
1121 vims
= get_vim(mydb
, tenant_id
)
1123 # Step 4. Review the descriptor and add missing fields
1124 #print vnf_descriptor
1125 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1126 vnf_name
= vnf_descriptor
['vnf']['name']
1127 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1128 if "physical" in vnf_descriptor
['vnf']:
1129 del vnf_descriptor
['vnf']['physical']
1130 #print vnf_descriptor
1132 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1133 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1134 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1136 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1137 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1138 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1140 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1141 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1143 VNFCitem
["name"] = vnfc
['name']
1144 VNFCitem
["availability_zone"] = vnfc
.get('availability_zone')
1145 VNFCitem
["description"] = vnfc
.get("description", 'VM %s of the VNF %s' %(vnfc
['name'],vnf_name
))
1147 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1150 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1151 myflavorDict
["description"] = VNFCitem
["description"]
1152 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1153 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1154 myflavorDict
["disk"] = vnfc
.get("disk", 1)
1155 myflavorDict
["extended"] = {}
1157 devices
= vnfc
.get("devices")
1159 myflavorDict
["extended"]["devices"] = devices
1162 # 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
1163 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1165 # Previous code has been commented
1166 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1167 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1168 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1169 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1171 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1173 # print "Error creating flavor: unknown processor model. Rollback successful."
1174 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1176 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1177 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1179 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1180 myflavorDict
['extended']['numas'] = vnfc
['numas']
1184 # Step 6.2 New flavors are created in the VIM
1185 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1187 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1188 VNFCitem
["flavor_id"] = flavor_id
1189 VNFCDict
[vnfc
['name']] = VNFCitem
1191 logger
.debug("Creating new images in the VIM for each VNFC")
1192 # Step 6.3 New images are created in the VIM
1193 #For each VNFC, we must create the appropriate image.
1194 #This "for" loop might be integrated with the previous one
1195 #In case this integration is made, the VNFCDict might become a VNFClist.
1196 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1197 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1199 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1200 image_dict
['universal_name']=vnfc
.get('image name')
1201 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1202 image_dict
['location']=vnfc
.get('VNFC image')
1203 #image_dict['new_location']=vnfc.get('image location')
1204 image_dict
['checksum']=vnfc
.get('image checksum')
1205 image_metadata_dict
= vnfc
.get('image metadata', None)
1206 image_metadata_str
= None
1207 if image_metadata_dict
is not None:
1208 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1209 image_dict
['metadata']=image_metadata_str
1210 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1211 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1212 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1213 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1214 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1215 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1216 if vnfc
.get("boot-data"):
1217 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1220 # Step 7. Storing the VNF descriptor in the repository
1221 if "descriptor" not in vnf_descriptor
["vnf"]:
1222 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1224 # Step 8. Adding the VNF to the NFVO DB
1225 vnf_id
= mydb
.new_vnf_as_a_whole(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1227 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1228 _
, message
= rollback(mydb
, vims
, rollback_list
)
1229 if isinstance(e
, db_base_Exception
):
1230 error_text
= "Exception at database"
1231 elif isinstance(e
, KeyError):
1232 error_text
= "KeyError exception "
1233 e
.http_code
= HTTP_Internal_Server_Error
1235 error_text
= "Exception at VIM"
1236 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1237 #logger.error("start_scenario %s", error_text)
1238 raise NfvoException(error_text
, e
.http_code
)
1241 def new_vnf_v02(mydb
, tenant_id
, vnf_descriptor
):
1242 global global_config
1244 # Step 1. Check the VNF descriptor
1245 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=2)
1246 # Step 2. Check tenant exist
1248 if tenant_id
!= "any":
1249 check_tenant(mydb
, tenant_id
)
1250 if "tenant_id" in vnf_descriptor
["vnf"]:
1251 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1252 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1255 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1256 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1257 if global_config
["auto_push_VNF_to_VIMs"]:
1258 vims
= get_vim(mydb
, tenant_id
)
1260 # Step 4. Review the descriptor and add missing fields
1261 #print vnf_descriptor
1262 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1263 vnf_name
= vnf_descriptor
['vnf']['name']
1264 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1265 if "physical" in vnf_descriptor
['vnf']:
1266 del vnf_descriptor
['vnf']['physical']
1267 #print vnf_descriptor
1269 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1270 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1271 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1273 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1274 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1275 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1277 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1278 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1280 VNFCitem
["name"] = vnfc
['name']
1281 VNFCitem
["description"] = vnfc
.get("description", 'VM %s of the VNF %s' %(vnfc
['name'],vnf_name
))
1283 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1286 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1287 myflavorDict
["description"] = VNFCitem
["description"]
1288 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1289 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1290 myflavorDict
["disk"] = vnfc
.get("disk", 1)
1291 myflavorDict
["extended"] = {}
1293 devices
= vnfc
.get("devices")
1295 myflavorDict
["extended"]["devices"] = devices
1298 # 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
1299 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1301 # Previous code has been commented
1302 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1303 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1304 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1305 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1307 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1309 # print "Error creating flavor: unknown processor model. Rollback successful."
1310 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1312 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1313 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1315 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1316 myflavorDict
['extended']['numas'] = vnfc
['numas']
1320 # Step 6.2 New flavors are created in the VIM
1321 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1323 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1324 VNFCitem
["flavor_id"] = flavor_id
1325 VNFCDict
[vnfc
['name']] = VNFCitem
1327 logger
.debug("Creating new images in the VIM for each VNFC")
1328 # Step 6.3 New images are created in the VIM
1329 #For each VNFC, we must create the appropriate image.
1330 #This "for" loop might be integrated with the previous one
1331 #In case this integration is made, the VNFCDict might become a VNFClist.
1332 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1333 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1335 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1336 image_dict
['universal_name']=vnfc
.get('image name')
1337 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1338 image_dict
['location']=vnfc
.get('VNFC image')
1339 #image_dict['new_location']=vnfc.get('image location')
1340 image_dict
['checksum']=vnfc
.get('image checksum')
1341 image_metadata_dict
= vnfc
.get('image metadata', None)
1342 image_metadata_str
= None
1343 if image_metadata_dict
is not None:
1344 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1345 image_dict
['metadata']=image_metadata_str
1346 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1347 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1348 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1349 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1350 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1351 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1352 if vnfc
.get("boot-data"):
1353 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1355 # Step 7. Storing the VNF descriptor in the repository
1356 if "descriptor" not in vnf_descriptor
["vnf"]:
1357 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1359 # Step 8. Adding the VNF to the NFVO DB
1360 vnf_id
= mydb
.new_vnf_as_a_whole2(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1362 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1363 _
, message
= rollback(mydb
, vims
, rollback_list
)
1364 if isinstance(e
, db_base_Exception
):
1365 error_text
= "Exception at database"
1366 elif isinstance(e
, KeyError):
1367 error_text
= "KeyError exception "
1368 e
.http_code
= HTTP_Internal_Server_Error
1370 error_text
= "Exception at VIM"
1371 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1372 #logger.error("start_scenario %s", error_text)
1373 raise NfvoException(error_text
, e
.http_code
)
1376 def get_vnf_id(mydb
, tenant_id
, vnf_id
):
1377 #check valid tenant_id
1378 check_tenant(mydb
, tenant_id
)
1381 if tenant_id
!= "any":
1382 where_or
["tenant_id"] = tenant_id
1383 where_or
["public"] = True
1384 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1386 vnf_id
= vnf
["uuid"]
1387 filter_keys
= ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
1388 filtered_content
= dict( (k
,v
) for k
,v
in vnf
.iteritems() if k
in filter_keys
)
1389 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1390 data
={'vnf' : filtered_content
}
1392 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id',
1393 SELECT
=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1395 WHERE
={'vnfs.uuid': vnf_id
} )
1397 raise NfvoException("vnf '{}' not found".format(vnf_id
), HTTP_Not_Found
)
1398 # change boot_data into boot-data
1400 if vm
.get("boot_data"):
1401 vm
["boot-data"] = yaml
.safe_load(vm
["boot_data"])
1404 data
['vnf']['VNFC'] = content
1405 #TODO: GET all the information from a VNFC and include it in the output.
1408 content
= mydb
.get_rows(FROM
='vnfs join nets on vnfs.uuid=nets.vnf_id',
1409 SELECT
=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1410 WHERE
={'vnfs.uuid': vnf_id
} )
1411 data
['vnf']['nets'] = content
1413 #GET ip-profile for each net
1414 for net
in data
['vnf']['nets']:
1415 ipprofiles
= mydb
.get_rows(FROM
='ip_profiles',
1416 SELECT
=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1417 WHERE
={'net_id': net
["uuid"]} )
1418 if len(ipprofiles
)==1:
1419 net
["ip_profile"] = ipprofiles
[0]
1420 elif len(ipprofiles
)>1:
1421 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net
['uuid']), HTTP_Bad_Request
)
1424 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
1426 #GET External Interfaces
1427 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
1428 SELECT
=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1429 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
1430 WHERE
={'vnfs.uuid': vnf_id
},
1431 WHERE_NOT
={'interfaces.external_name': None} )
1433 data
['vnf']['external-connections'] = content
1438 def delete_vnf(mydb
,tenant_id
,vnf_id
,datacenter
=None,vim_tenant
=None):
1439 # Check tenant exist
1440 if tenant_id
!= "any":
1441 check_tenant(mydb
, tenant_id
)
1442 # Get the URL of the VIM from the nfvo_tenant and the datacenter
1443 vims
= get_vim(mydb
, tenant_id
)
1447 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1449 if tenant_id
!= "any":
1450 where_or
["tenant_id"] = tenant_id
1451 where_or
["public"] = True
1452 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1453 vnf_id
= vnf
["uuid"]
1455 # "Getting the list of flavors and tenants of the VNF"
1456 flavorList
= get_flavorlist(mydb
, vnf_id
)
1457 if len(flavorList
)==0:
1458 logger
.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id
)
1460 imageList
= get_imagelist(mydb
, vnf_id
)
1461 if len(imageList
)==0:
1462 logger
.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id
)
1464 deleted
= mydb
.delete_row_by_id('vnfs', vnf_id
)
1466 raise NfvoException("vnf '{}' not found".format(vnf_id
), HTTP_Not_Found
)
1469 for flavor
in flavorList
:
1470 #check if flavor is used by other vnf
1472 c
= mydb
.get_rows(FROM
='vms', WHERE
={'flavor_id':flavor
} )
1474 logger
.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor
)
1476 #flavor not used, must be deleted
1478 c
= mydb
.get_rows(FROM
='datacenters_flavors', WHERE
={'flavor_id':flavor
})
1479 for flavor_vim
in c
:
1480 if flavor_vim
["datacenter_vim_id"] not in vims
: # TODO change to datacenter_tenant_id
1482 if flavor_vim
['created']=='false': #skip this flavor because not created by openmano
1484 myvim
=vims
[ flavor_vim
["datacenter_id"] ]
1486 myvim
.delete_flavor(flavor_vim
["vim_id"])
1487 except vimconn
.vimconnNotFoundException
as e
:
1488 logger
.warn("VIM flavor %s not exist at datacenter %s", flavor_vim
["vim_id"], flavor_vim
["datacenter_id"] )
1489 except vimconn
.vimconnException
as e
:
1490 logger
.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1491 flavor_vim
["vim_id"], flavor_vim
["datacenter_id"], type(e
).__name
__, str(e
))
1492 undeletedItems
.append("flavor {} from VIM {}".format(flavor_vim
["vim_id"], flavor_vim
["datacenter_id"] ))
1493 #delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1494 mydb
.delete_row_by_id('flavors', flavor
)
1495 except db_base_Exception
as e
:
1496 logger
.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor
, str(e
))
1497 undeletedItems
.append("flavor %s" % flavor
)
1500 for image
in imageList
:
1502 #check if image is used by other vnf
1503 c
= mydb
.get_rows(FROM
='vms', WHERE
={'image_id':image
} )
1505 logger
.debug("Image '%s' not deleted because it is being used by another VNF", image
)
1507 #image not used, must be deleted
1509 c
= mydb
.get_rows(FROM
='datacenters_images', WHERE
={'image_id':image
})
1511 if image_vim
["datacenter_vim_id"] not in vims
: # TODO change to datacenter_tenant_id
1513 if image_vim
['created']=='false': #skip this image because not created by openmano
1515 myvim
=vims
[ image_vim
["datacenter_id"] ]
1517 myvim
.delete_image(image_vim
["vim_id"])
1518 except vimconn
.vimconnNotFoundException
as e
:
1519 logger
.warn("VIM image %s not exist at datacenter %s", image_vim
["vim_id"], image_vim
["datacenter_id"] )
1520 except vimconn
.vimconnException
as e
:
1521 logger
.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1522 image_vim
["vim_id"], image_vim
["datacenter_id"], type(e
).__name
__, str(e
))
1523 undeletedItems
.append("image {} from VIM {}".format(image_vim
["vim_id"], image_vim
["datacenter_id"] ))
1524 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1525 mydb
.delete_row_by_id('images', image
)
1526 except db_base_Exception
as e
:
1527 logger
.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image
, str(e
))
1528 undeletedItems
.append("image %s" % image
)
1530 return vnf_id
+ " " + vnf
["name"]
1532 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
1535 def get_hosts_info(mydb
, nfvo_tenant_id
, datacenter_name
=None):
1536 result
, vims
= get_vim(mydb
, nfvo_tenant_id
, None, datacenter_name
)
1540 return -HTTP_Not_Found
, "datacenter '%s' not found" % datacenter_name
1541 myvim
= vims
.values()[0]
1542 result
,servers
= myvim
.get_hosts_info()
1544 return result
, servers
1545 topology
= {'name':myvim
['name'] , 'servers': servers
}
1546 return result
, topology
1549 def get_hosts(mydb
, nfvo_tenant_id
):
1550 vims
= get_vim(mydb
, nfvo_tenant_id
)
1552 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id
)), HTTP_Not_Found
)
1554 #print "nfvo.datacenter_action() error. Several datacenters found"
1555 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
1556 myvim
= vims
.values()[0]
1558 hosts
= myvim
.get_hosts()
1559 logger
.debug('VIM hosts response: '+ yaml
.safe_dump(hosts
, indent
=4, default_flow_style
=False))
1561 datacenter
= {'Datacenters': [ {'name':myvim
['name'],'servers':[]} ] }
1563 server
={'name':host
['name'], 'vms':[]}
1564 for vm
in host
['instances']:
1565 #get internal name and model
1567 c
= mydb
.get_rows(SELECT
=('name',), FROM
='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1568 WHERE
={'vim_vm_id':vm
['id']} )
1570 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm
['id']))
1572 server
['vms'].append( {'name':vm
['name'] , 'model':c
[0]['name']} )
1574 except db_base_Exception
as e
:
1575 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm
['id'], str(e
)))
1576 datacenter
['Datacenters'][0]['servers'].append(server
)
1577 #return -400, "en construccion"
1579 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1581 except vimconn
.vimconnException
as e
:
1582 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e
)), e
.http_code
)
1585 def new_scenario(mydb
, tenant_id
, topo
):
1587 # result, vims = get_vim(mydb, tenant_id)
1589 # return result, vims
1591 if tenant_id
!= "any":
1592 check_tenant(mydb
, tenant_id
)
1593 if "tenant_id" in topo
:
1594 if topo
["tenant_id"] != tenant_id
:
1595 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo
["tenant_id"], tenant_id
),
1600 #1.1: get VNFs and external_networks (other_nets).
1602 other_nets
={} #external_networks, bridge_networks and data_networkds
1603 nodes
= topo
['topology']['nodes']
1604 for k
in nodes
.keys():
1605 if nodes
[k
]['type'] == 'VNF':
1607 vnfs
[k
]['ifaces'] = {}
1608 elif nodes
[k
]['type'] == 'other_network' or nodes
[k
]['type'] == 'external_network':
1609 other_nets
[k
] = nodes
[k
]
1610 other_nets
[k
]['external']=True
1611 elif nodes
[k
]['type'] == 'network':
1612 other_nets
[k
] = nodes
[k
]
1613 other_nets
[k
]['external']=False
1616 #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1617 for name
,vnf
in vnfs
.items():
1619 where_or
={"tenant_id": tenant_id
, 'public': "true"}
1621 error_pos
= "'topology':'nodes':'" + name
+ "'"
1623 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
1624 where
['uuid'] = vnf
['vnf_id']
1625 if 'VNF model' in vnf
:
1626 error_text
+= " 'VNF model' " + vnf
['VNF model']
1627 where
['name'] = vnf
['VNF model']
1629 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos
, HTTP_Bad_Request
)
1631 vnf_db
= mydb
.get_rows(SELECT
=('uuid','name','description'),
1637 raise NfvoException("unknown" + error_text
+ " at " + error_pos
, HTTP_Not_Found
)
1639 raise NfvoException("more than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", HTTP_Conflict
)
1640 vnf
['uuid']=vnf_db
[0]['uuid']
1641 vnf
['description']=vnf_db
[0]['description']
1642 #get external interfaces
1643 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1644 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1645 WHERE
={'vnfs.uuid':vnf
['uuid']}, WHERE_NOT
={'external_name':None} )
1646 for ext_iface
in ext_ifaces
:
1647 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type':ext_iface
['type']}
1649 #1.4 get list of connections
1650 conections
= topo
['topology']['connections']
1651 conections_list
= []
1652 conections_list_name
= []
1653 for k
in conections
.keys():
1654 if type(conections
[k
]['nodes'])==dict: #dict with node:iface pairs
1655 ifaces_list
= conections
[k
]['nodes'].items()
1656 elif type(conections
[k
]['nodes'])==list: #list with dictionary
1658 conection_pair_list
= map(lambda x
: x
.items(), conections
[k
]['nodes'] )
1659 for k2
in conection_pair_list
:
1662 con_type
= conections
[k
].get("type", "link")
1663 if con_type
!= "link":
1665 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k
)), HTTP_Bad_Request
)
1666 other_nets
[k
] = {'external': False}
1667 if conections
[k
].get("graph"):
1668 other_nets
[k
]["graph"] = conections
[k
]["graph"]
1669 ifaces_list
.append( (k
, None) )
1672 if con_type
== "external_network":
1673 other_nets
[k
]['external'] = True
1674 if conections
[k
].get("model"):
1675 other_nets
[k
]["model"] = conections
[k
]["model"]
1677 other_nets
[k
]["model"] = k
1678 if con_type
== "dataplane_net" or con_type
== "bridge_net":
1679 other_nets
[k
]["model"] = con_type
1681 conections_list_name
.append(k
)
1682 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)
1683 #print set(ifaces_list)
1684 #check valid VNF and iface names
1685 for iface
in ifaces_list
:
1686 if iface
[0] not in vnfs
and iface
[0] not in other_nets
:
1687 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1688 str(k
), iface
[0]), HTTP_Not_Found
)
1689 if iface
[0] in vnfs
and iface
[1] not in vnfs
[ iface
[0] ]['ifaces']:
1690 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1691 str(k
), iface
[0], iface
[1]), HTTP_Not_Found
)
1693 #1.5 unify connections from the pair list to a consolidated list
1695 while index
< len(conections_list
):
1697 while index2
< len(conections_list
):
1698 if len(conections_list
[index
] & conections_list
[index2
])>0: #common interface, join nets
1699 conections_list
[index
] |
= conections_list
[index2
]
1700 del conections_list
[index2
]
1701 del conections_list_name
[index2
]
1704 conections_list
[index
] = list(conections_list
[index
]) # from set to list again
1706 #for k in conections_list:
1711 #1.6 Delete non external nets
1712 # for k in other_nets.keys():
1713 # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1714 # for con in conections_list:
1716 # for index in range(0,len(con)):
1717 # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1718 # for index in delete_indexes:
1721 #1.7: Check external_ports are present at database table datacenter_nets
1722 for k
,net
in other_nets
.items():
1723 error_pos
= "'topology':'nodes':'" + k
+ "'"
1724 if net
['external']==False:
1725 if 'name' not in net
:
1727 if 'model' not in net
:
1728 raise NfvoException("needed a 'model' at " + error_pos
, HTTP_Bad_Request
)
1729 if net
['model']=='bridge_net':
1730 net
['type']='bridge';
1731 elif net
['model']=='dataplane_net':
1734 raise NfvoException("unknown 'model' '"+ net
['model'] +"' at " + error_pos
, HTTP_Not_Found
)
1736 #IF we do not want to check that external network exist at datacenter
1741 # if 'net_id' in net:
1742 # error_text += " 'net_id' " + net['net_id']
1743 # WHERE_['uuid'] = net['net_id']
1744 # if 'model' in net:
1745 # error_text += " 'model' " + net['model']
1746 # WHERE_['name'] = net['model']
1747 # if len(WHERE_) == 0:
1748 # return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1749 # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1750 # FROM='datacenter_nets', WHERE=WHERE_ )
1752 # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1754 # print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1755 # return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1757 # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1758 # return -HTTP_Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
1759 # other_nets[k].update(net_db[0])
1762 net_nb
=0 #Number of nets
1763 for con
in conections_list
:
1764 #check if this is connected to a external net
1768 for index
in range(0,len(con
)):
1769 #check if this is connected to a external net
1770 for net_key
in other_nets
.keys():
1771 if con
[index
][0]==net_key
:
1772 if other_net_index
>=0:
1773 error_text
="There is some interface connected both to net '%s' and net '%s'" % (con
[other_net_index
][0], net_key
)
1774 #print "nfvo.new_scenario " + error_text
1775 raise NfvoException(error_text
, HTTP_Bad_Request
)
1777 other_net_index
= index
1778 net_target
= net_key
1780 #print "other_net_index", other_net_index
1782 if other_net_index
>=0:
1783 del con
[other_net_index
]
1784 #IF we do not want to check that external network exist at datacenter
1785 if other_nets
[net_target
]['external'] :
1786 if "name" not in other_nets
[net_target
]:
1787 other_nets
[net_target
]['name'] = other_nets
[net_target
]['model']
1788 if other_nets
[net_target
]["type"] == "external_network":
1789 if vnfs
[ con
[0][0] ]['ifaces'][ con
[0][1] ]["type"] == "data":
1790 other_nets
[net_target
]["type"] = "data"
1792 other_nets
[net_target
]["type"] = "bridge"
1794 # if other_nets[net_target]['external'] :
1795 # 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
1796 # if type_=='data' and other_nets[net_target]['type']=="ptp":
1797 # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
1798 # print "nfvo.new_scenario " + error_text
1799 # return -HTTP_Bad_Request, error_text
1802 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
1805 net_type_bridge
=False
1807 net_target
= "__-__net"+str(net_nb
)
1808 net_list
[net_target
] = {'name': conections_list_name
[net_nb
], #"net-"+str(net_nb),
1809 'description':"net-%s in scenario %s" %(net_nb
,topo
['name']),
1812 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
1813 iface_type
= vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['type']
1814 if iface_type
=='mgmt' or iface_type
=='bridge':
1815 net_type_bridge
= True
1817 net_type_data
= True
1818 if net_type_bridge
and net_type_data
:
1819 error_text
= "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface
[0], iface
[1])
1820 #print "nfvo.new_scenario " + error_text
1821 raise NfvoException(error_text
, HTTP_Bad_Request
)
1822 elif net_type_bridge
:
1825 type_
='data' if len(con
)>2 else 'ptp'
1826 net_list
[net_target
]['type'] = type_
1829 error_text
= "Error connection node %s : %s does not match any VNF or interface" % (iface
[0], iface
[1])
1830 #print "nfvo.new_scenario " + error_text
1832 raise NfvoException(error_text
, HTTP_Bad_Request
)
1834 #1.8: Connect to management net all not already connected interfaces of type 'mgmt'
1835 #1.8.1 obtain management net
1836 mgmt_net
= mydb
.get_rows(SELECT
=('uuid','name','description','type','shared'),
1837 FROM
='datacenter_nets', WHERE
={'name':'mgmt'} )
1838 #1.8.2 check all interfaces from all vnfs
1840 add_mgmt_net
= False
1841 for vnf
in vnfs
.values():
1842 for iface
in vnf
['ifaces'].values():
1843 if iface
['type']=='mgmt' and 'net_key' not in iface
:
1844 #iface not connected
1845 iface
['net_key'] = 'mgmt'
1847 if add_mgmt_net
and 'mgmt' not in net_list
:
1848 net_list
['mgmt']=mgmt_net
[0]
1849 net_list
['mgmt']['external']=True
1850 net_list
['mgmt']['graph']={'visible':False}
1852 net_list
.update(other_nets
)
1854 #print 'net_list', net_list
1859 #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
1860 c
= mydb
.new_scenario( { 'vnfs':vnfs
, 'nets':net_list
,
1861 'tenant_id':tenant_id
, 'name':topo
['name'],
1862 'description':topo
.get('description',topo
['name']),
1863 'public': topo
.get('public', False)
1869 def new_scenario_v02(mydb
, tenant_id
, scenario_dict
, version
):
1870 """ This creates a new scenario for version 0.2 and 0.3"""
1871 scenario
= scenario_dict
["scenario"]
1872 if tenant_id
!= "any":
1873 check_tenant(mydb
, tenant_id
)
1874 if "tenant_id" in scenario
:
1875 if scenario
["tenant_id"] != tenant_id
:
1876 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
1877 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
1878 scenario
["tenant_id"], tenant_id
), HTTP_Unauthorized
)
1882 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
1883 for name
,vnf
in scenario
["vnfs"].iteritems():
1885 where_or
={"tenant_id": tenant_id
, 'public': "true"}
1887 error_pos
= "'scenario':'vnfs':'" + name
+ "'"
1889 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
1890 where
['uuid'] = vnf
['vnf_id']
1891 if 'vnf_name' in vnf
:
1892 error_text
+= " 'vnf_name' " + vnf
['vnf_name']
1893 where
['name'] = vnf
['vnf_name']
1895 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos
, HTTP_Bad_Request
)
1896 vnf_db
= mydb
.get_rows(SELECT
=('uuid', 'name', 'description'),
1901 if len(vnf_db
) == 0:
1902 raise NfvoException("Unknown" + error_text
+ " at " + error_pos
, HTTP_Not_Found
)
1903 elif len(vnf_db
) > 1:
1904 raise NfvoException("More than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", HTTP_Conflict
)
1905 vnf
['uuid'] = vnf_db
[0]['uuid']
1906 vnf
['description'] = vnf_db
[0]['description']
1908 # get external interfaces
1909 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
1910 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1911 WHERE
={'vnfs.uuid':vnf
['uuid']}, WHERE_NOT
={'external_name': None} )
1912 for ext_iface
in ext_ifaces
:
1913 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type': ext_iface
['type']}
1914 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
1916 # 2: Insert net_key and ip_address at every vnf interface
1917 for net_name
, net
in scenario
["networks"].items():
1918 net_type_bridge
= False
1919 net_type_data
= False
1920 for iface_dict
in net
["interfaces"]:
1921 if version
== "0.2":
1922 temp_dict
= iface_dict
1924 elif version
== "0.3":
1925 temp_dict
= {iface_dict
["vnf"] : iface_dict
["vnf_interface"]}
1926 ip_address
= iface_dict
.get('ip_address', None)
1927 for vnf
, iface
in temp_dict
.items():
1928 if vnf
not in scenario
["vnfs"]:
1929 error_text
= "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
1931 # logger.debug("nfvo.new_scenario_v02 " + error_text)
1932 raise NfvoException(error_text
, HTTP_Not_Found
)
1933 if iface
not in scenario
["vnfs"][vnf
]['ifaces']:
1934 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
1935 .format(net_name
, iface
)
1936 # logger.debug("nfvo.new_scenario_v02 " + error_text)
1937 raise NfvoException(error_text
, HTTP_Bad_Request
)
1938 if "net_key" in scenario
["vnfs"][vnf
]['ifaces'][iface
]:
1939 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
1940 "'{}'".format(net_name
, iface
,scenario
["vnfs"][vnf
]['ifaces'][iface
]['net_key'])
1941 # logger.debug("nfvo.new_scenario_v02 " + error_text)
1942 raise NfvoException(error_text
, HTTP_Bad_Request
)
1943 scenario
["vnfs"][vnf
]['ifaces'][ iface
]['net_key'] = net_name
1944 scenario
["vnfs"][vnf
]['ifaces'][iface
]['ip_address'] = ip_address
1945 iface_type
= scenario
["vnfs"][vnf
]['ifaces'][iface
]['type']
1946 if iface_type
== 'mgmt' or iface_type
== 'bridge':
1947 net_type_bridge
= True
1949 net_type_data
= True
1951 if net_type_bridge
and net_type_data
:
1952 error_text
= "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
1954 # logger.debug("nfvo.new_scenario " + error_text)
1955 raise NfvoException(error_text
, HTTP_Bad_Request
)
1956 elif net_type_bridge
:
1959 type_
= 'data' if len(net
["interfaces"]) > 2 else 'ptp'
1961 if net
.get("implementation"): # for v0.3
1962 if type_
== "bridge" and net
["implementation"] == "underlay":
1963 error_text
= "Error connecting interfaces of data type to a network declared as 'underlay' at "\
1964 "'network':'{}'".format(net_name
)
1965 # logger.debug(error_text)
1966 raise NfvoException(error_text
, HTTP_Bad_Request
)
1967 elif type_
!= "bridge" and net
["implementation"] == "overlay":
1968 error_text
= "Error connecting interfaces of data type to a network declared as 'overlay' at "\
1969 "'network':'{}'".format(net_name
)
1970 # logger.debug(error_text)
1971 raise NfvoException(error_text
, HTTP_Bad_Request
)
1972 net
.pop("implementation")
1973 if "type" in net
and version
== "0.3": # for v0.3
1974 if type_
== "data" and net
["type"] == "e-line":
1975 error_text
= "Error connecting more than 2 interfaces of data type to a network declared as type "\
1976 "'e-line' at 'network':'{}'".format(net_name
)
1977 # logger.debug(error_text)
1978 raise NfvoException(error_text
, HTTP_Bad_Request
)
1979 elif type_
== "ptp" and net
["type"] == "e-lan":
1983 net
['name'] = net_name
1984 net
['external'] = net
.get('external', False)
1986 # 3: insert at database
1987 scenario
["nets"] = scenario
["networks"]
1988 scenario
['tenant_id'] = tenant_id
1989 scenario_id
= mydb
.new_scenario(scenario
)
1993 def new_nsd_v3(mydb
, tenant_id
, nsd_descriptor
):
1995 Parses an OSM IM nsd_catalog and insert at DB
1998 :param nsd_descriptor:
1999 :return: The list of cretated NSD ids
2002 mynsd
= nsd_catalog
.nsd()
2004 pybindJSONDecoder
.load_ietf_json(nsd_descriptor
, None, None, obj
=mynsd
)
2005 except Exception as e
:
2006 raise NfvoException("Invalid yang descriptor format " + str(e
), HTTP_Bad_Request
)
2010 db_sce_interfaces
= []
2012 db_ip_profiles_index
= 0
2015 for rift_nsd
in mynsd
.nsd_catalog
.nsd
.itervalues():
2016 nsd
= rift_nsd
.get()
2019 scenario_uuid
= str(uuid4())
2020 uuid_list
.append(scenario_uuid
)
2021 nsd_uuid_list
.append(scenario_uuid
)
2023 "uuid": scenario_uuid
,
2024 "osm_id": get_str(nsd
, "id", 255),
2025 "name": get_str(nsd
, "name", 255),
2026 "description": get_str(nsd
, "description", 255),
2027 "tenant_id": tenant_id
,
2028 "vendor": get_str(nsd
, "vendor", 255),
2029 "short_name": get_str(nsd
, "short-name", 255),
2030 "descriptor": str(nsd_descriptor
)[:60000],
2032 db_scenarios
.append(db_scenario
)
2034 # table sce_vnfs (constituent-vnfd)
2035 vnf_index2scevnf_uuid
= {}
2036 vnf_index2vnf_uuid
= {}
2037 for vnf
in nsd
.get("constituent-vnfd").itervalues():
2038 existing_vnf
= mydb
.get_rows(FROM
="vnfs", WHERE
={'osm_id': str(vnf
["vnfd-id-ref"])[:255],
2039 'tenant_id': tenant_id
})
2040 if not existing_vnf
:
2041 raise KeyError("Error at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':'{}' references a "
2042 "non existing VNFD in the catalog".format(str(nsd
["id"]),
2043 str(vnf
["vnfd-id-ref"])[:255]))
2044 sce_vnf_uuid
= str(uuid4())
2045 uuid_list
.append(sce_vnf_uuid
)
2047 "uuid": sce_vnf_uuid
,
2048 "scenario_id": scenario_uuid
,
2049 "name": existing_vnf
[0]["name"][:200] + "." + get_str(vnf
, "member-vnf-index", 5),
2050 "vnf_id": existing_vnf
[0]["uuid"],
2051 "member_vnf_index": int(vnf
["member-vnf-index"]),
2052 # TODO 'start-by-default': True
2054 vnf_index2scevnf_uuid
[int(vnf
['member-vnf-index'])] = sce_vnf_uuid
2055 vnf_index2vnf_uuid
[int(vnf
['member-vnf-index'])] = existing_vnf
[0]["uuid"]
2056 db_sce_vnfs
.append(db_sce_vnf
)
2058 # table ip_profiles (ip-profiles)
2059 ip_profile_name2db_table_index
= {}
2060 for ip_profile
in nsd
.get("ip-profiles").itervalues():
2062 "ip_version": str(ip_profile
["ip-profile-params"].get("ip-version", "ipv4")),
2063 "subnet_address": str(ip_profile
["ip-profile-params"].get("subnet-address")),
2064 "gateway_address": str(ip_profile
["ip-profile-params"].get("gateway-address")),
2065 "dhcp_enabled": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2066 "dhcp_start_address": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("start-address")),
2067 "dhcp_count": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("count")),
2070 for dns
in ip_profile
["ip-profile-params"]["dns-server"].itervalues():
2071 dns_list
.append(str(dns
.get("address")))
2072 db_ip_profile
["dns_address"] = ";".join(dns_list
)
2073 if ip_profile
["ip-profile-params"].get('security-group'):
2074 db_ip_profile
["security_group"] = ip_profile
["ip-profile-params"]['security-group']
2075 ip_profile_name2db_table_index
[str(ip_profile
["name"])] = db_ip_profiles_index
2076 db_ip_profiles_index
+= 1
2077 db_ip_profiles
.append(db_ip_profile
)
2079 # table sce_nets (internal-vld)
2080 for vld
in nsd
.get("vld").itervalues():
2081 sce_net_uuid
= str(uuid4())
2082 uuid_list
.append(sce_net_uuid
)
2084 "uuid": sce_net_uuid
,
2085 "name": get_str(vld
, "name", 255),
2086 "scenario_id": scenario_uuid
,
2088 "multipoint": not vld
.get("type") == "ELINE",
2090 "description": get_str(vld
, "description", 255),
2092 # guess type of network
2093 if vld
.get("mgmt-network"):
2094 db_sce_net
["type"] = "bridge"
2095 db_sce_net
["external"] = True
2096 elif vld
.get("provider-network").get("overlay-type") == "VLAN":
2097 db_sce_net
["type"] = "data"
2099 db_sce_net
["type"] = "bridge"
2100 db_sce_nets
.append(db_sce_net
)
2102 # ip-profile, link db_ip_profile with db_sce_net
2103 if vld
.get("ip-profile-ref"):
2104 ip_profile_name
= vld
.get("ip-profile-ref")
2105 if ip_profile_name
not in ip_profile_name2db_table_index
:
2106 raise KeyError("Error at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}' references a non existing "
2107 "'ip_profiles'".format(
2108 str(nsd
["id"]), str(vld
["id"]), str(vld
["ip-profile-ref"])))
2109 db_ip_profiles
[ip_profile_name2db_table_index
[ip_profile_name
]]["sce_net_id"] = sce_net_uuid
2111 # table sce_interfaces (vld:vnfd-connection-point-ref)
2112 for iface
in vld
.get("vnfd-connection-point-ref").itervalues():
2113 vnf_index
= int(iface
['member-vnf-index-ref'])
2114 # check correct parameters
2115 if vnf_index
not in vnf_index2vnf_uuid
:
2116 raise KeyError("Error at 'nsd[{}]':'vld[{}]':'vnfd-connection-point-ref':'member-vnf-index-ref'"
2117 ":'{}' references a non existing index at 'nsd':'constituent-vnfd'".format(
2118 str(nsd
["id"]), str(vld
["id"]), str(iface
["member-vnf-index-ref"])))
2120 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2121 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2122 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2123 'external_name': get_str(iface
, "vnfd-connection-point-ref",
2125 if not existing_ifaces
:
2126 raise KeyError("Error at 'nsd[{}]':'vld[{}]':'vnfd-connection-point-ref':'vnfd-connection-point"
2127 "-ref':'{}' references a non existing interface at VNFD '{}'".format(
2128 str(nsd
["id"]), str(vld
["id"]), str(iface
["vnfd-connection-point-ref"]),
2129 str(iface
.get("vnfd-id-ref"))[:255]))
2131 interface_uuid
= existing_ifaces
[0]["uuid"]
2132 sce_interface_uuid
= str(uuid4())
2133 uuid_list
.append(sce_net_uuid
)
2134 db_sce_interface
= {
2135 "uuid": sce_interface_uuid
,
2136 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2137 "sce_net_id": sce_net_uuid
,
2138 "interface_id": interface_uuid
,
2139 # "ip_address": #TODO
2141 db_sce_interfaces
.append(db_sce_interface
)
2144 {"scenarios": db_scenarios
},
2145 {"sce_nets": db_sce_nets
},
2146 {"ip_profiles": db_ip_profiles
},
2147 {"sce_vnfs": db_sce_vnfs
},
2148 {"sce_interfaces": db_sce_interfaces
},
2151 logger
.debug("create_vnf Deployment done vnfDict: %s",
2152 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
2153 mydb
.new_rows(db_tables
, uuid_list
)
2154 return nsd_uuid_list
2155 except Exception as e
:
2156 logger
.error("Exception {}".format(e
))
2157 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
2160 def edit_scenario(mydb
, tenant_id
, scenario_id
, data
):
2161 data
["uuid"] = scenario_id
2162 data
["tenant_id"] = tenant_id
2163 c
= mydb
.edit_scenario( data
)
2167 def start_scenario(mydb
, tenant_id
, scenario_id
, instance_scenario_name
, instance_scenario_description
, datacenter
=None,vim_tenant
=None, startvms
=True):
2168 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2169 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
, vim_tenant
=vim_tenant
)
2170 vims
= {datacenter_id
: myvim
}
2171 myvim_tenant
= myvim
['tenant_id']
2172 datacenter_name
= myvim
['name']
2176 #print "Checking that the scenario_id exists and getting the scenario dictionary"
2177 scenarioDict
= mydb
.get_scenario(scenario_id
, tenant_id
, datacenter_id
=datacenter_id
)
2178 scenarioDict
['datacenter2tenant'] = { datacenter_id
: myvim
['config']['datacenter_tenant_id'] }
2179 scenarioDict
['datacenter_id'] = datacenter_id
2180 #print '================scenarioDict======================='
2181 #print json.dumps(scenarioDict, indent=4)
2182 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
2184 logger
.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict
['name'],len(scenarioDict
['vnfs']))
2185 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2187 auxNetDict
= {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2188 auxNetDict
['scenario'] = {}
2190 logger
.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2191 for sce_net
in scenarioDict
['nets']:
2192 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
2194 myNetName
= "%s.%s" % (instance_scenario_name
, sce_net
['name'])
2195 myNetName
= myNetName
[0:255] #limit length
2196 myNetType
= sce_net
['type']
2198 myNetDict
["name"] = myNetName
2199 myNetDict
["type"] = myNetType
2200 myNetDict
["tenant_id"] = myvim_tenant
2201 myNetIPProfile
= sce_net
.get('ip_profile', None)
2203 #We should use the dictionary as input parameter for new_network
2205 if not sce_net
["external"]:
2206 network_id
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
)
2207 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2208 sce_net
['vim_id'] = network_id
2209 auxNetDict
['scenario'][sce_net
['uuid']] = network_id
2210 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2211 sce_net
["created"] = True
2213 if sce_net
['vim_id'] == None:
2214 error_text
= "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name
, sce_net
['name'])
2215 _
, message
= rollback(mydb
, vims
, rollbackList
)
2216 logger
.error("nfvo.start_scenario: %s", error_text
)
2217 raise NfvoException(error_text
, HTTP_Bad_Request
)
2218 logger
.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict
['name'],sce_net
['vim_id'])
2219 auxNetDict
['scenario'][sce_net
['uuid']] = sce_net
['vim_id']
2221 logger
.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2222 #For each vnf net, we create it and we add it to instanceNetlist.
2224 for sce_vnf
in scenarioDict
['vnfs']:
2225 for net
in sce_vnf
['nets']:
2226 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
2228 myNetName
= "%s.%s" % (instance_scenario_name
,net
['name'])
2229 myNetName
= myNetName
[0:255] #limit length
2230 myNetType
= net
['type']
2232 myNetDict
["name"] = myNetName
2233 myNetDict
["type"] = myNetType
2234 myNetDict
["tenant_id"] = myvim_tenant
2235 myNetIPProfile
= net
.get('ip_profile', None)
2238 #We should use the dictionary as input parameter for new_network
2239 network_id
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
)
2240 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2241 net
['vim_id'] = network_id
2242 if sce_vnf
['uuid'] not in auxNetDict
:
2243 auxNetDict
[sce_vnf
['uuid']] = {}
2244 auxNetDict
[sce_vnf
['uuid']][net
['uuid']] = network_id
2245 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2246 net
["created"] = True
2248 #print "auxNetDict:"
2249 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
2251 logger
.debug("start_scenario 3. Creating new vm instances in the VIM")
2252 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2254 for sce_vnf
in scenarioDict
['vnfs']:
2255 vnf_availability_zones
= []
2256 for vm
in sce_vnf
['vms']:
2257 vm_av
= vm
.get('availability_zone')
2258 if vm_av
and vm_av
not in vnf_availability_zones
:
2259 vnf_availability_zones
.append(vm_av
)
2261 # check if there is enough availability zones available at vim level.
2262 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
2263 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
2264 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request
)
2266 for vm
in sce_vnf
['vms']:
2269 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
2270 myVMDict
['name'] = "{}.{}.{}".format(instance_scenario_name
,sce_vnf
['name'],chr(96+i
))
2271 #myVMDict['description'] = vm['description']
2272 myVMDict
['description'] = myVMDict
['name'][0:99]
2274 myVMDict
['start'] = "no"
2275 myVMDict
['name'] = myVMDict
['name'][0:255] #limit name length
2276 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
2278 #create image at vim in case it not exist
2279 image_dict
= mydb
.get_table_by_uuid_name("images", vm
['image_id'])
2280 image_id
= create_or_use_image(mydb
, vims
, image_dict
, [], True)
2281 vm
['vim_image_id'] = image_id
2283 #create flavor at vim in case it not exist
2284 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
2285 if flavor_dict
['extended']!=None:
2286 flavor_dict
['extended']= yaml
.load(flavor_dict
['extended'])
2287 flavor_id
= create_or_use_flavor(mydb
, vims
, flavor_dict
, [], True)
2288 vm
['vim_flavor_id'] = flavor_id
2291 myVMDict
['imageRef'] = vm
['vim_image_id']
2292 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
2293 myVMDict
['networks'] = []
2294 for iface
in vm
['interfaces']:
2296 if iface
['type']=="data":
2297 netDict
['type'] = iface
['model']
2298 elif "model" in iface
and iface
["model"]!=None:
2299 netDict
['model']=iface
['model']
2300 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2301 #discover type of interface looking at flavor
2302 for numa
in flavor_dict
.get('extended',{}).get('numas',[]):
2303 for flavor_iface
in numa
.get('interfaces',[]):
2304 if flavor_iface
.get('name') == iface
['internal_name']:
2305 if flavor_iface
['dedicated'] == 'yes':
2306 netDict
['type']="PF" #passthrough
2307 elif flavor_iface
['dedicated'] == 'no':
2308 netDict
['type']="VF" #siov
2309 elif flavor_iface
['dedicated'] == 'yes:sriov':
2310 netDict
['type']="VFnotShared" #sriov but only one sriov on the PF
2311 netDict
["mac_address"] = flavor_iface
.get("mac_address")
2313 netDict
["use"]=iface
['type']
2314 if netDict
["use"]=="data" and not netDict
.get("type"):
2315 #print "netDict", netDict
2316 #print "iface", iface
2317 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'])
2318 if flavor_dict
.get('extended')==None:
2319 raise NfvoException(e_text
+ "After database migration some information is not available. \
2320 Try to delete and create the scenarios and VNFs again", HTTP_Conflict
)
2322 raise NfvoException(e_text
, HTTP_Internal_Server_Error
)
2323 if netDict
["use"]=="mgmt" or netDict
["use"]=="bridge":
2324 netDict
["type"]="virtual"
2325 if "vpci" in iface
and iface
["vpci"] is not None:
2326 netDict
['vpci'] = iface
['vpci']
2327 if "mac" in iface
and iface
["mac"] is not None:
2328 netDict
['mac_address'] = iface
['mac']
2329 if "port-security" in iface
and iface
["port-security"] is not None:
2330 netDict
['port_security'] = iface
['port-security']
2331 if "floating-ip" in iface
and iface
["floating-ip"] is not None:
2332 netDict
['floating_ip'] = iface
['floating-ip']
2333 netDict
['name'] = iface
['internal_name']
2334 if iface
['net_id'] is None:
2335 for vnf_iface
in sce_vnf
["interfaces"]:
2338 if vnf_iface
['interface_id']==iface
['uuid']:
2339 netDict
['net_id'] = auxNetDict
['scenario'][ vnf_iface
['sce_net_id'] ]
2342 netDict
['net_id'] = auxNetDict
[ sce_vnf
['uuid'] ][ iface
['net_id'] ]
2343 #skip bridge ifaces not connected to any net
2344 #if 'net_id' not in netDict or netDict['net_id']==None:
2346 myVMDict
['networks'].append(netDict
)
2347 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2348 #print myVMDict['name']
2349 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2350 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2351 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2353 if 'availability_zone' in myVMDict
:
2354 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
2358 vm_id
= myvim
.new_vminstance(myVMDict
['name'], myVMDict
['description'], myVMDict
.get('start', None),
2359 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'],
2360 availability_zone_index
=av_index
,
2361 availability_zone_list
=vnf_availability_zones
)
2362 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2363 vm
['vim_id'] = vm_id
2364 rollbackList
.append({'what':'vm','where':'vim','vim_id':datacenter_id
,'uuid':vm_id
})
2365 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2366 for net
in myVMDict
['networks']:
2368 for iface
in vm
['interfaces']:
2369 if net
["name"]==iface
["internal_name"]:
2370 iface
["vim_id"]=net
["vim_id"]
2373 logger
.debug("start scenario Deployment done")
2374 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2375 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
2376 instance_id
= mydb
.new_instance_scenario_as_a_whole(tenant_id
,instance_scenario_name
, instance_scenario_description
, scenarioDict
)
2377 return mydb
.get_instance_scenario(instance_id
)
2379 except (db_base_Exception
, vimconn
.vimconnException
) as e
:
2380 _
, message
= rollback(mydb
, vims
, rollbackList
)
2381 if isinstance(e
, db_base_Exception
):
2382 error_text
= "Exception at database"
2384 error_text
= "Exception at VIM"
2385 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
2386 #logger.error("start_scenario %s", error_text)
2387 raise NfvoException(error_text
, e
.http_code
)
2390 def unify_cloud_config(cloud_config_preserve
, cloud_config
):
2391 """ join the cloud config information into cloud_config_preserve.
2392 In case of conflict cloud_config_preserve preserves
2395 if not cloud_config_preserve
and not cloud_config
:
2398 new_cloud_config
= {"key-pairs":[], "users":[]}
2400 if cloud_config_preserve
:
2401 for key
in cloud_config_preserve
.get("key-pairs", () ):
2402 if key
not in new_cloud_config
["key-pairs"]:
2403 new_cloud_config
["key-pairs"].append(key
)
2405 for key
in cloud_config
.get("key-pairs", () ):
2406 if key
not in new_cloud_config
["key-pairs"]:
2407 new_cloud_config
["key-pairs"].append(key
)
2408 if not new_cloud_config
["key-pairs"]:
2409 del new_cloud_config
["key-pairs"]
2413 new_cloud_config
["users"] += cloud_config
.get("users", () )
2414 if cloud_config_preserve
:
2415 new_cloud_config
["users"] += cloud_config_preserve
.get("users", () )
2416 index_to_delete
= []
2417 users
= new_cloud_config
.get("users", [])
2418 for index0
in range(0,len(users
)):
2419 if index0
in index_to_delete
:
2421 for index1
in range(index0
+1,len(users
)):
2422 if index1
in index_to_delete
:
2424 if users
[index0
]["name"] == users
[index1
]["name"]:
2425 index_to_delete
.append(index1
)
2426 for key
in users
[index1
].get("key-pairs",()):
2427 if "key-pairs" not in users
[index0
]:
2428 users
[index0
]["key-pairs"] = [key
]
2429 elif key
not in users
[index0
]["key-pairs"]:
2430 users
[index0
]["key-pairs"].append(key
)
2431 index_to_delete
.sort(reverse
=True)
2432 for index
in index_to_delete
:
2434 if not new_cloud_config
["users"]:
2435 del new_cloud_config
["users"]
2438 if cloud_config
and cloud_config
.get("boot-data-drive") != None:
2439 new_cloud_config
["boot-data-drive"] = cloud_config
["boot-data-drive"]
2440 if cloud_config_preserve
and cloud_config_preserve
.get("boot-data-drive") != None:
2441 new_cloud_config
["boot-data-drive"] = cloud_config_preserve
["boot-data-drive"]
2444 new_cloud_config
["user-data"] = []
2445 if cloud_config
and cloud_config
.get("user-data"):
2446 if isinstance(cloud_config
["user-data"], list):
2447 new_cloud_config
["user-data"] += cloud_config
["user-data"]
2449 new_cloud_config
["user-data"].append(cloud_config
["user-data"])
2450 if cloud_config_preserve
and cloud_config_preserve
.get("user-data"):
2451 if isinstance(cloud_config_preserve
["user-data"], list):
2452 new_cloud_config
["user-data"] += cloud_config_preserve
["user-data"]
2454 new_cloud_config
["user-data"].append(cloud_config_preserve
["user-data"])
2455 if not new_cloud_config
["user-data"]:
2456 del new_cloud_config
["user-data"]
2459 new_cloud_config
["config-files"] = []
2460 if cloud_config
and cloud_config
.get("config-files") != None:
2461 new_cloud_config
["config-files"] += cloud_config
["config-files"]
2462 if cloud_config_preserve
:
2463 for file in cloud_config_preserve
.get("config-files", ()):
2464 for index
in range(0, len(new_cloud_config
["config-files"])):
2465 if new_cloud_config
["config-files"][index
]["dest"] == file["dest"]:
2466 new_cloud_config
["config-files"][index
] = file
2469 new_cloud_config
["config-files"].append(file)
2470 if not new_cloud_config
["config-files"]:
2471 del new_cloud_config
["config-files"]
2472 return new_cloud_config
2475 def get_vim_thread(mydb
, tenant_id
, datacenter_id_name
=None, datacenter_tenant_id
=None):
2476 datacenter_id
= None
2477 datacenter_name
= None
2480 if datacenter_tenant_id
:
2481 thread_id
= datacenter_tenant_id
2482 thread
= vim_threads
["running"].get(datacenter_tenant_id
)
2484 where_
={"td.nfvo_tenant_id": tenant_id
}
2485 if datacenter_id_name
:
2486 if utils
.check_valid_uuid(datacenter_id_name
):
2487 datacenter_id
= datacenter_id_name
2488 where_
["dt.datacenter_id"] = datacenter_id
2490 datacenter_name
= datacenter_id_name
2491 where_
["d.name"] = datacenter_name
2492 if datacenter_tenant_id
:
2493 where_
["dt.uuid"] = datacenter_tenant_id
2494 datacenters
= mydb
.get_rows(
2495 SELECT
=("dt.uuid as datacenter_tenant_id",),
2496 FROM
="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2497 "join datacenters as d on d.uuid=dt.datacenter_id",
2499 if len(datacenters
) > 1:
2500 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
2502 thread_id
= datacenters
[0]["datacenter_tenant_id"]
2503 thread
= vim_threads
["running"].get(thread_id
)
2505 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), HTTP_Not_Found
)
2506 return thread_id
, thread
2507 except db_base_Exception
as e
:
2508 raise NfvoException("{} {}".format(type(e
).__name
__ , str(e
)), e
.http_code
)
2511 def get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter_id_name
=None, **extra_filter
):
2512 datacenter_id
= None
2513 datacenter_name
= None
2514 if datacenter_id_name
:
2515 if utils
.check_valid_uuid(datacenter_id_name
):
2516 datacenter_id
= datacenter_id_name
2518 datacenter_name
= datacenter_id_name
2519 vims
= get_vim(mydb
, tenant_id
, datacenter_id
, datacenter_name
, **extra_filter
)
2521 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), HTTP_Not_Found
)
2523 #print "nfvo.datacenter_action() error. Several datacenters found"
2524 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
2525 return vims
.keys()[0], vims
.values()[0]
2529 '''Takes dict d and updates it with the values in dict u.'''
2530 '''It merges all depth levels'''
2531 for k
, v
in u
.iteritems():
2532 if isinstance(v
, collections
.Mapping
):
2533 r
= update(d
.get(k
, {}), v
)
2540 def create_instance(mydb
, tenant_id
, instance_dict
):
2541 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2542 # logger.debug("Creating instance...")
2543 scenario
= instance_dict
["scenario"]
2545 # find main datacenter
2547 myvim_threads_id
= {}
2548 datacenter
= instance_dict
.get("datacenter")
2549 default_datacenter_id
, vim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
2550 myvims
[default_datacenter_id
] = vim
2551 myvim_threads_id
[default_datacenter_id
], _
= get_vim_thread(mydb
, tenant_id
, default_datacenter_id
)
2552 # myvim_tenant = myvim['tenant_id']
2555 # print "Checking that the scenario exists and getting the scenario dictionary"
2556 scenarioDict
= mydb
.get_scenario(scenario
, tenant_id
, datacenter_vim_id
=myvim_threads_id
[default_datacenter_id
],
2557 datacenter_id
=default_datacenter_id
)
2559 # logger.debug(">>>>>> Dictionaries before merging")
2560 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
2561 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
2563 db_instance_vnfs
= []
2564 db_instance_vms
= []
2565 db_instance_interfaces
= []
2570 instance_name
= instance_dict
["name"]
2571 instance_uuid
= str(uuid4())
2572 uuid_list
.append(instance_uuid
)
2573 db_instance_scenario
= {
2574 "uuid": instance_uuid
,
2575 "name": instance_name
,
2576 "tenant_id": tenant_id
,
2577 "scenario_id": scenarioDict
['uuid'],
2578 "datacenter_id": default_datacenter_id
,
2579 # filled bellow 'datacenter_tenant_id'
2580 "description": instance_dict
.get("description"),
2582 if scenarioDict
.get("cloud-config"):
2583 db_instance_scenario
["cloud_config"] = yaml
.safe_dump(scenarioDict
["cloud-config"],
2584 default_flow_style
=True, width
=256)
2585 instance_action_id
= get_task_id()
2586 db_instance_action
= {
2587 "uuid": instance_action_id
, # same uuid for the instance and the action on create
2588 "tenant_id": tenant_id
,
2589 "instance_id": instance_uuid
,
2590 "description": "CREATE",
2593 # Auxiliary dictionaries from x to y
2594 vnf_net2instance
= {}
2595 sce_net2instance
= {}
2596 net2task_id
= {'scenario': {}}
2598 # logger.debug("Creating instance from scenario-dict:\n%s",
2599 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
2601 # 0 check correct parameters
2602 for net_name
, net_instance_desc
in instance_dict
.get("networks", {}).iteritems():
2604 for scenario_net
in scenarioDict
['nets']:
2605 if net_name
== scenario_net
["name"]:
2609 raise NfvoException("Invalid scenario network name '{}' at instance:networks".format(net_name
),
2611 if "sites" not in net_instance_desc
:
2612 net_instance_desc
["sites"] = [ {} ]
2613 site_without_datacenter_field
= False
2614 for site
in net_instance_desc
["sites"]:
2615 if site
.get("datacenter"):
2616 if site
["datacenter"] not in myvims
:
2617 # Add this datacenter to myvims
2618 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, site
["datacenter"])
2620 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, site
["datacenter"])
2621 site
["datacenter"] = d
# change name to id
2623 if site_without_datacenter_field
:
2624 raise NfvoException("Found more than one entries without datacenter field at "
2625 "instance:networks:{}:sites".format(net_name
), HTTP_Bad_Request
)
2626 site_without_datacenter_field
= True
2627 site
["datacenter"] = default_datacenter_id
# change name to id
2629 for vnf_name
, vnf_instance_desc
in instance_dict
.get("vnfs",{}).iteritems():
2631 for scenario_vnf
in scenarioDict
['vnfs']:
2632 if vnf_name
== scenario_vnf
['name']:
2636 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_instance_desc
), HTTP_Bad_Request
)
2637 if "datacenter" in vnf_instance_desc
:
2638 # Add this datacenter to myvims
2639 if vnf_instance_desc
["datacenter"] not in myvims
:
2640 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
2642 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
2643 scenario_vnf
["datacenter"] = vnf_instance_desc
["datacenter"]
2645 # 0.1 parse cloud-config parameters
2646 cloud_config
= unify_cloud_config(instance_dict
.get("cloud-config"), scenarioDict
.get("cloud-config"))
2648 # 0.2 merge instance information into scenario
2649 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
2650 # However, this is not possible yet.
2651 for net_name
, net_instance_desc
in instance_dict
.get("networks",{}).iteritems():
2652 for scenario_net
in scenarioDict
['nets']:
2653 if net_name
== scenario_net
["name"]:
2654 if 'ip-profile' in net_instance_desc
:
2655 # translate from input format to database format
2656 ipprofile_in
= net_instance_desc
['ip-profile']
2658 ipprofile_db
['subnet_address'] = ipprofile_in
.get('subnet-address')
2659 ipprofile_db
['ip_version'] = ipprofile_in
.get('ip-version', 'IPv4')
2660 ipprofile_db
['gateway_address'] = ipprofile_in
.get('gateway-address')
2661 ipprofile_db
['dns_address'] = ipprofile_in
.get('dns-address')
2662 if isinstance(ipprofile_db
['dns_address'], (list, tuple)):
2663 ipprofile_db
['dns_address'] = ";".join(ipprofile_db
['dns_address'])
2664 if 'dhcp' in ipprofile_in
:
2665 ipprofile_db
['dhcp_start_address'] = ipprofile_in
['dhcp'].get('start-address')
2666 ipprofile_db
['dhcp_enabled'] = ipprofile_in
['dhcp'].get('enabled', True)
2667 ipprofile_db
['dhcp_count'] = ipprofile_in
['dhcp'].get('count' )
2668 if 'ip_profile' not in scenario_net
:
2669 scenario_net
['ip_profile'] = ipprofile_db
2671 update(scenario_net
['ip_profile'], ipprofile_db
)
2672 for interface
in net_instance_desc
.get('interfaces', () ):
2673 if 'ip_address' in interface
:
2674 for vnf
in scenarioDict
['vnfs']:
2675 if interface
['vnf'] == vnf
['name']:
2676 for vnf_interface
in vnf
['interfaces']:
2677 if interface
['vnf_interface'] == vnf_interface
['external_name']:
2678 vnf_interface
['ip_address']=interface
['ip_address']
2680 # logger.debug(">>>>>>>> Merged dictionary")
2681 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
2682 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
2684 # 1. Creating new nets (sce_nets) in the VIM"
2685 db_instance_nets
= []
2686 for sce_net
in scenarioDict
['nets']:
2687 descriptor_net
= instance_dict
.get("networks", {}).get(sce_net
["name"], {})
2688 net_name
= descriptor_net
.get("vim-network-name")
2689 sce_net2instance
[sce_net
['uuid']] = {}
2690 net2task_id
['scenario'][sce_net
['uuid']] = {}
2692 sites
= descriptor_net
.get("sites", [ {} ])
2694 if site
.get("datacenter"):
2695 vim
= myvims
[ site
["datacenter"] ]
2696 datacenter_id
= site
["datacenter"]
2697 myvim_thread_id
= myvim_threads_id
[ site
["datacenter"] ]
2699 vim
= myvims
[ default_datacenter_id
]
2700 datacenter_id
= default_datacenter_id
2701 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
2702 net_type
= sce_net
['type']
2703 lookfor_filter
= {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
2706 if sce_net
["external"]:
2707 net_name
= sce_net
["name"]
2709 net_name
= "{}.{}".format(instance_name
, sce_net
["name"])
2710 net_name
= net_name
[:255] # limit length
2712 if "netmap-use" in site
or "netmap-create" in site
:
2713 create_network
= False
2714 lookfor_network
= False
2715 if "netmap-use" in site
:
2716 lookfor_network
= True
2717 if utils
.check_valid_uuid(site
["netmap-use"]):
2718 filter_text
= "scenario id '%s'" % site
["netmap-use"]
2719 lookfor_filter
["id"] = site
["netmap-use"]
2721 filter_text
= "scenario name '%s'" % site
["netmap-use"]
2722 lookfor_filter
["name"] = site
["netmap-use"]
2723 if "netmap-create" in site
:
2724 create_network
= True
2725 net_vim_name
= net_name
2726 if site
["netmap-create"]:
2727 net_vim_name
= site
["netmap-create"]
2728 elif sce_net
["external"]:
2729 if sce_net
['vim_id'] != None:
2730 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
2731 create_network
= False
2732 lookfor_network
= True
2733 lookfor_filter
["id"] = sce_net
['vim_id']
2734 filter_text
= "vim_id '{}' datacenter_netmap name '{}'. Try to reload vims with "\
2735 "datacenter-net-update".format(sce_net
['vim_id'], sce_net
["name"])
2736 # look for network at datacenter and return error
2738 # There is not a netmap, look at datacenter for a net with this name and create if not found
2739 create_network
= True
2740 lookfor_network
= True
2741 lookfor_filter
["name"] = sce_net
["name"]
2742 net_vim_name
= sce_net
["name"]
2743 filter_text
= "scenario name '%s'" % sce_net
["name"]
2745 net_vim_name
= net_name
2746 create_network
= True
2747 lookfor_network
= False
2749 if lookfor_network
and create_network
:
2750 # TODO create two tasks FIND + CREATE with their relationship
2751 task_action
= "FIND_CREATE"
2752 task_params
= (lookfor_filter
, (net_vim_name
, net_type
, sce_net
.get('ip_profile', None)))
2753 elif lookfor_network
:
2754 task_action
= "FIND"
2755 task_params
= (lookfor_filter
,)
2756 elif create_network
:
2757 task_action
= "CREATE"
2758 task_params
= (net_vim_name
, net_type
, sce_net
.get('ip_profile', None))
2760 # fill database content
2761 net_uuid
= str(uuid4())
2762 uuid_list
.append(net_uuid
)
2763 sce_net2instance
[sce_net
['uuid']][datacenter_id
] = net_uuid
2767 "instance_scenario_id": instance_uuid
,
2768 "sce_net_id": sce_net
["uuid"],
2769 "created": create_network
,
2770 'datacenter_id': datacenter_id
,
2771 'datacenter_tenant_id': myvim_thread_id
,
2772 'status': 'BUILD' if create_network
else "ACTIVE"
2774 db_instance_nets
.append(db_net
)
2776 "instance_action_id": instance_action_id
,
2777 "status": "SCHEDULED",
2778 "task_index": task_index
,
2779 "datacenter_vim_id": myvim_thread_id
,
2780 "action": task_action
,
2781 "item": "instance_nets",
2782 "item_id": net_uuid
,
2783 "extra": yaml
.safe_dump({"params": task_params
}, default_flow_style
=True, width
=256)
2785 net2task_id
['scenario'][sce_net
['uuid']][datacenter_id
] = task_index
2787 db_vim_actions
.append(db_vim_action
)
2789 if 'ip_profile' in sce_net
:
2791 'instance_net_id': net_uuid
,
2792 'ip_version': sce_net
['ip_profile']['ip_version'],
2793 'subnet_address': sce_net
['ip_profile']['subnet_address'],
2794 'gateway_address': sce_net
['ip_profile']['gateway_address'],
2795 'dns_address': sce_net
['ip_profile']['dns_address'],
2796 'dhcp_enabled': sce_net
['ip_profile']['dhcp_enabled'],
2797 'dhcp_start_address': sce_net
['ip_profile']['dhcp_start_address'],
2798 'dhcp_count': sce_net
['ip_profile']['dhcp_count'],
2800 db_ip_profiles
.append(db_ip_profile
)
2802 # 2. Creating new nets (vnf internal nets) in the VIM"
2803 # For each vnf net, we create it and we add it to instanceNetlist.
2804 for sce_vnf
in scenarioDict
['vnfs']:
2805 for net
in sce_vnf
['nets']:
2806 if sce_vnf
.get("datacenter"):
2807 datacenter_id
= sce_vnf
["datacenter"]
2808 myvim_thread_id
= myvim_threads_id
[sce_vnf
["datacenter"]]
2810 datacenter_id
= default_datacenter_id
2811 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
2812 descriptor_net
= instance_dict
.get("vnfs", {}).get(sce_vnf
["name"], {})
2813 net_name
= descriptor_net
.get("name")
2815 net_name
= "{}.{}".format(instance_name
, net
["name"])
2816 net_name
= net_name
[:255] # limit length
2817 net_type
= net
['type']
2819 if sce_vnf
['uuid'] not in vnf_net2instance
:
2820 vnf_net2instance
[sce_vnf
['uuid']] = {}
2821 if sce_vnf
['uuid'] not in net2task_id
:
2822 net2task_id
[sce_vnf
['uuid']] = {}
2823 net2task_id
[sce_vnf
['uuid']][net
['uuid']] = task_index
2825 # fill database content
2826 net_uuid
= str(uuid4())
2827 uuid_list
.append(net_uuid
)
2828 vnf_net2instance
[sce_vnf
['uuid']][net
['uuid']] = net_uuid
2832 "instance_scenario_id": instance_uuid
,
2833 "net_id": net
["uuid"],
2835 'datacenter_id': datacenter_id
,
2836 'datacenter_tenant_id': myvim_thread_id
,
2838 db_instance_nets
.append(db_net
)
2841 "instance_action_id": instance_action_id
,
2842 "task_index": task_index
,
2843 "datacenter_vim_id": myvim_thread_id
,
2844 "status": "SCHEDULED",
2846 "item": "instance_nets",
2847 "item_id": net_uuid
,
2848 "extra": yaml
.safe_dump({"params": (net_name
, net_type
, net
.get('ip_profile',None))},
2849 default_flow_style
=True, width
=256)
2852 db_vim_actions
.append(db_vim_action
)
2854 if 'ip_profile' in net
:
2856 'instance_net_id': net_uuid
,
2857 'ip_version': net
['ip_profile']['ip_version'],
2858 'subnet_address': net
['ip_profile']['subnet_address'],
2859 'gateway_address': net
['ip_profile']['gateway_address'],
2860 'dns_address': net
['ip_profile']['dns_address'],
2861 'dhcp_enabled': net
['ip_profile']['dhcp_enabled'],
2862 'dhcp_start_address': net
['ip_profile']['dhcp_start_address'],
2863 'dhcp_count': net
['ip_profile']['dhcp_count'],
2865 db_ip_profiles
.append(db_ip_profile
)
2867 # print "vnf_net2instance:"
2868 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
2870 # 3. Creating new vm instances in the VIM
2871 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2872 sce_vnf_list
= sorted(scenarioDict
['vnfs'], key
=lambda k
: k
['name'])
2873 for sce_vnf
in sce_vnf_list
:
2874 vnf_availability_zones
= []
2875 for vm
in sce_vnf
['vms']:
2876 vm_av
= vm
.get('availability_zone')
2877 if vm_av
and vm_av
not in vnf_availability_zones
:
2878 vnf_availability_zones
.append(vm_av
)
2880 # check if there is enough availability zones available at vim level.
2881 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
2882 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
2883 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request
)
2885 if sce_vnf
.get("datacenter"):
2886 vim
= myvims
[ sce_vnf
["datacenter"] ]
2887 myvim_thread_id
= myvim_threads_id
[ sce_vnf
["datacenter"] ]
2888 datacenter_id
= sce_vnf
["datacenter"]
2890 vim
= myvims
[ default_datacenter_id
]
2891 myvim_thread_id
= myvim_threads_id
[ default_datacenter_id
]
2892 datacenter_id
= default_datacenter_id
2893 sce_vnf
["datacenter_id"] = datacenter_id
2896 vnf_uuid
= str(uuid4())
2897 uuid_list
.append(vnf_uuid
)
2900 'instance_scenario_id': instance_uuid
,
2901 'vnf_id': sce_vnf
['vnf_id'],
2902 'sce_vnf_id': sce_vnf
['uuid'],
2903 'datacenter_id': datacenter_id
,
2904 'datacenter_tenant_id': myvim_thread_id
,
2906 db_instance_vnfs
.append(db_instance_vnf
)
2908 for vm
in sce_vnf
['vms']:
2910 myVMDict
['name'] = "{}.{}.{}".format(instance_name
[:64], sce_vnf
['name'][:64], vm
["name"][:64])
2911 myVMDict
['description'] = myVMDict
['name'][0:99]
2913 # myVMDict['start'] = "no"
2914 myVMDict
['name'] = myVMDict
['name'][0:255] # limit name length
2915 #create image at vim in case it not exist
2916 image_dict
= mydb
.get_table_by_uuid_name("images", vm
['image_id'])
2917 image_id
= create_or_use_image(mydb
, {datacenter_id
: vim
}, image_dict
, [], True)
2918 vm
['vim_image_id'] = image_id
2920 # create flavor at vim in case it not exist
2921 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
2922 if flavor_dict
['extended']!=None:
2923 flavor_dict
['extended'] = yaml
.load(flavor_dict
['extended'])
2924 flavor_id
= create_or_use_flavor(mydb
, {datacenter_id
: vim
}, flavor_dict
, rollbackList
, True)
2926 # Obtain information for additional disks
2927 extended_flavor_dict
= mydb
.get_rows(FROM
='datacenters_flavors', SELECT
=('extended',), WHERE
={'vim_id': flavor_id
})
2928 if not extended_flavor_dict
:
2929 raise NfvoException("flavor '{}' not found".format(flavor_id
), HTTP_Not_Found
)
2932 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
2933 myVMDict
['disks'] = None
2934 extended_info
= extended_flavor_dict
[0]['extended']
2935 if extended_info
!= None:
2936 extended_flavor_dict_yaml
= yaml
.load(extended_info
)
2937 if 'disks' in extended_flavor_dict_yaml
:
2938 myVMDict
['disks'] = extended_flavor_dict_yaml
['disks']
2940 vm
['vim_flavor_id'] = flavor_id
2941 myVMDict
['imageRef'] = vm
['vim_image_id']
2942 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
2943 myVMDict
['availability_zone'] = vm
.get('availability_zone')
2944 myVMDict
['networks'] = []
2945 task_depends_on
= []
2946 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
2948 for iface
in vm
['interfaces']:
2950 if iface
['type']=="data":
2951 netDict
['type'] = iface
['model']
2952 elif "model" in iface
and iface
["model"]!=None:
2953 netDict
['model']=iface
['model']
2954 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
2955 # is obtained from iterface table model
2956 # discover type of interface looking at flavor
2957 for numa
in flavor_dict
.get('extended',{}).get('numas',[]):
2958 for flavor_iface
in numa
.get('interfaces',[]):
2959 if flavor_iface
.get('name') == iface
['internal_name']:
2960 if flavor_iface
['dedicated'] == 'yes':
2961 netDict
['type']="PF" #passthrough
2962 elif flavor_iface
['dedicated'] == 'no':
2963 netDict
['type']="VF" #siov
2964 elif flavor_iface
['dedicated'] == 'yes:sriov':
2965 netDict
['type']="VFnotShared" #sriov but only one sriov on the PF
2966 netDict
["mac_address"] = flavor_iface
.get("mac_address")
2968 netDict
["use"]=iface
['type']
2969 if netDict
["use"]=="data" and not netDict
.get("type"):
2970 #print "netDict", netDict
2971 #print "iface", iface
2972 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'])
2973 if flavor_dict
.get('extended')==None:
2974 raise NfvoException(e_text
+ "After database migration some information is not available. \
2975 Try to delete and create the scenarios and VNFs again", HTTP_Conflict
)
2977 raise NfvoException(e_text
, HTTP_Internal_Server_Error
)
2978 if netDict
["use"]=="mgmt" or netDict
["use"]=="bridge":
2979 netDict
["type"]="virtual"
2980 if "vpci" in iface
and iface
["vpci"] is not None:
2981 netDict
['vpci'] = iface
['vpci']
2982 if "mac" in iface
and iface
["mac"] is not None:
2983 netDict
['mac_address'] = iface
['mac']
2984 if "port-security" in iface
and iface
["port-security"] is not None:
2985 netDict
['port_security'] = iface
['port-security']
2986 if "floating-ip" in iface
and iface
["floating-ip"] is not None:
2987 netDict
['floating_ip'] = iface
['floating-ip']
2988 netDict
['name'] = iface
['internal_name']
2989 if iface
['net_id'] is None:
2990 for vnf_iface
in sce_vnf
["interfaces"]:
2993 if vnf_iface
['interface_id']==iface
['uuid']:
2994 netDict
['net_id'] = "TASK-{}".format(net2task_id
['scenario'][ vnf_iface
['sce_net_id'] ][datacenter_id
])
2995 instance_net_id
= sce_net2instance
[ vnf_iface
['sce_net_id'] ][datacenter_id
]
2996 task_depends_on
.append(net2task_id
['scenario'][ vnf_iface
['sce_net_id'] ][datacenter_id
])
2999 netDict
['net_id'] = "TASK-{}".format(net2task_id
[ sce_vnf
['uuid'] ][ iface
['net_id'] ])
3000 instance_net_id
= vnf_net2instance
[ sce_vnf
['uuid'] ][ iface
['net_id'] ]
3001 task_depends_on
.append(net2task_id
[sce_vnf
['uuid'] ][ iface
['net_id']])
3002 # skip bridge ifaces not connected to any net
3003 if 'net_id' not in netDict
or netDict
['net_id']==None:
3005 myVMDict
['networks'].append(netDict
)
3008 # 'instance_vm_id': instance_vm_uuid,
3009 "instance_net_id": instance_net_id
,
3010 'interface_id': iface
['uuid'],
3011 # 'vim_interface_id': ,
3012 'type': 'external' if iface
['external_name'] is not None else 'internal',
3013 'ip_address': iface
.get('ip_address'),
3014 'floating_ip': int(iface
.get('floating-ip', False)),
3015 'port_security': int(iface
.get('port-security', True))
3017 db_vm_ifaces
.append(db_vm_iface
)
3018 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3019 # print myVMDict['name']
3020 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
3021 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
3022 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3023 if vm
.get("boot_data"):
3024 cloud_config_vm
= unify_cloud_config(vm
["boot_data"], cloud_config
)
3026 cloud_config_vm
= cloud_config
3027 if myVMDict
.get('availability_zone'):
3028 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
3031 for vm_index
in range(0, vm
.get('count', 1)):
3033 if vm
.get('count', 1) > 1:
3034 vm_index_name
+= "." + chr(97 + vm_index
)
3035 task_params
= (myVMDict
['name']+vm_index_name
, myVMDict
['description'], myVMDict
.get('start', None),
3036 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'], cloud_config_vm
,
3037 myVMDict
['disks'], av_index
, vnf_availability_zones
)
3038 # put interface uuid back to scenario[vnfs][vms[[interfaces]
3039 for net
in myVMDict
['networks']:
3041 for iface
in vm
['interfaces']:
3042 if net
["name"]==iface
["internal_name"]:
3043 iface
["vim_id"]=net
["vim_id"]
3045 vm_uuid
= str(uuid4())
3046 uuid_list
.append(vm_uuid
)
3049 'instance_vnf_id': vnf_uuid
,
3050 #TODO delete "vim_vm_id": vm_id,
3051 "vm_id": vm
["uuid"],
3054 db_instance_vms
.append(db_vm
)
3057 for db_vm_iface
in db_vm_ifaces
:
3058 iface_uuid
= str(uuid4())
3059 uuid_list
.append(iface_uuid
)
3060 db_vm_iface_instance
= {
3062 "instance_vm_id": vm_uuid
3064 db_vm_iface_instance
.update(db_vm_iface
)
3065 if db_vm_iface_instance
.get("ip_address"): # increment ip_address
3066 ip
= db_vm_iface_instance
.get("ip_address")
3071 ip
= ip
[i
:] + str(int(ip
[:i
]) +1)
3072 db_vm_iface_instance
["ip_address"] = ip
3074 db_vm_iface_instance
["ip_address"] = None
3075 db_instance_interfaces
.append(db_vm_iface_instance
)
3076 myVMDict
['networks'][iface_index
]["uuid"] = iface_uuid
3080 "instance_action_id": instance_action_id
,
3081 "task_index": task_index
,
3082 "datacenter_vim_id": myvim_thread_id
,
3084 "status": "SCHEDULED",
3085 "item": "instance_vms",
3087 "extra": yaml
.safe_dump({"params": task_params
, "depends_on": task_depends_on
},
3088 default_flow_style
=True, width
=256)
3091 db_vim_actions
.append(db_vim_action
)
3093 scenarioDict
["datacenter2tenant"] = myvim_threads_id
3095 db_instance_action
["number_tasks"] = task_index
3096 db_instance_scenario
['datacenter_tenant_id'] = myvim_threads_id
[default_datacenter_id
]
3097 db_instance_scenario
['datacenter_id'] = default_datacenter_id
3099 {"instance_scenarios": db_instance_scenario
},
3100 {"instance_vnfs": db_instance_vnfs
},
3101 {"instance_nets": db_instance_nets
},
3102 {"ip_profiles": db_ip_profiles
},
3103 {"instance_vms": db_instance_vms
},
3104 {"instance_interfaces": db_instance_interfaces
},
3105 {"instance_actions": db_instance_action
},
3106 {"vim_actions": db_vim_actions
}
3109 logger
.debug("create_instance done DB tables: %s",
3110 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
3111 mydb
.new_rows(db_tables
, uuid_list
)
3112 for myvim_thread_id
in myvim_threads_id
.values():
3113 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
3115 returned_instance
= mydb
.get_instance_scenario(instance_uuid
)
3116 returned_instance
["action_id"] = instance_action_id
3117 return returned_instance
3118 except (NfvoException
, vimconn
.vimconnException
, db_base_Exception
) as e
:
3119 message
= rollback(mydb
, myvims
, rollbackList
)
3120 if isinstance(e
, db_base_Exception
):
3121 error_text
= "database Exception"
3122 elif isinstance(e
, vimconn
.vimconnException
):
3123 error_text
= "VIM Exception"
3125 error_text
= "Exception"
3126 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
3127 # logger.error("create_instance: %s", error_text)
3128 raise NfvoException(error_text
, e
.http_code
)
3131 def delete_instance(mydb
, tenant_id
, instance_id
):
3132 # print "Checking that the instance_id exists and getting the instance dictionary"
3133 instanceDict
= mydb
.get_instance_scenario(instance_id
, tenant_id
)
3134 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
3135 tenant_id
= instanceDict
["tenant_id"]
3136 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
3138 # 1. Delete from Database
3139 message
= mydb
.delete_instance_scenario(instance_id
, tenant_id
)
3141 # 2. delete from VIM
3145 vimthread_affected
= {}
3148 instance_action_id
= get_task_id()
3150 db_instance_action
= {
3151 "uuid": instance_action_id
, # same uuid for the instance and the action on create
3152 "tenant_id": tenant_id
,
3153 "instance_id": instance_id
,
3154 "description": "DELETE",
3155 # "number_tasks": 0 # filled bellow
3160 for sce_vnf
in instanceDict
['vnfs']:
3161 datacenter_key
= (sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
3162 vimthread_affected
[sce_vnf
["datacenter_tenant_id"]] = None
3163 if datacenter_key
not in myvims
:
3165 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
3166 except NfvoException
as e
:
3167 logger
.error(str(e
))
3169 myvim_threads
[datacenter_key
] = myvim_thread
3170 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sce_vnf
["datacenter_id"],
3171 datacenter_tenant_id
=sce_vnf
["datacenter_tenant_id"])
3173 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf
["datacenter_id"],
3174 sce_vnf
["datacenter_tenant_id"]))
3175 myvims
[datacenter_key
] = None
3177 myvims
[datacenter_key
] = vims
.values()[0]
3178 myvim
= myvims
[datacenter_key
]
3179 myvim_thread
= myvim_threads
[datacenter_key
]
3180 for vm
in sce_vnf
['vms']:
3182 error_msg
+= "\n VM id={} cannot be deleted because datacenter={} not found".format(vm
['vim_vm_id'], sce_vnf
["datacenter_id"])
3186 "instance_action_id": instance_action_id
,
3187 "task_index": task_index
,
3188 "datacenter_vim_id": sce_vnf
["datacenter_tenant_id"],
3190 "status": "SCHEDULED",
3191 "item": "instance_vms",
3192 "item_id": vm
["uuid"],
3193 "extra": yaml
.safe_dump({"params": vm
["interfaces"]},
3194 default_flow_style
=True, width
=256)
3197 db_vim_actions
.append(db_vim_action
)
3199 except vimconn
.vimconnNotFoundException
as e
:
3200 error_msg
+="\n VM VIM_id={} not found at datacenter={}".format(vm
['vim_vm_id'], sce_vnf
["datacenter_id"])
3201 logger
.warn("VM instance '%s'uuid '%s', VIM id '%s', from VNF_id '%s' not found",
3202 vm
['name'], vm
['uuid'], vm
['vim_vm_id'], sce_vnf
['vnf_id'])
3203 except vimconn
.vimconnException
as e
:
3204 error_msg
+="\n VM VIM_id={} at datacenter={} Error: {} {}".format(vm
['vim_vm_id'], sce_vnf
["datacenter_id"], e
.http_code
, str(e
))
3205 logger
.error("Error %d deleting VM instance '%s'uuid '%s', VIM_id '%s', from VNF_id '%s': %s",
3206 e
.http_code
, vm
['name'], vm
['uuid'], vm
['vim_vm_id'], sce_vnf
['vnf_id'], str(e
))
3210 for net
in instanceDict
['nets']:
3211 # TODO if not net['created']:
3212 # TODO continue #skip not created nets
3214 vimthread_affected
[net
["datacenter_tenant_id"]] = None
3215 datacenter_key
= (net
["datacenter_id"], net
["datacenter_tenant_id"])
3216 if datacenter_key
not in myvims
:
3218 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
3219 except NfvoException
as e
:
3220 logger
.error(str(e
))
3222 myvim_threads
[datacenter_key
] = myvim_thread
3223 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=net
["datacenter_id"],
3224 datacenter_tenant_id
=net
["datacenter_tenant_id"])
3226 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net
["datacenter_id"], net
["datacenter_tenant_id"]))
3227 myvims
[datacenter_key
] = None
3229 myvims
[datacenter_key
] = vims
.values()[0]
3230 myvim
= myvims
[datacenter_key
]
3231 myvim_thread
= myvim_threads
[datacenter_key
]
3234 error_msg
+= "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net
['vim_net_id'], net
["datacenter_id"])
3238 "instance_action_id": instance_action_id
,
3239 "task_index": task_index
,
3240 "datacenter_vim_id": net
["datacenter_tenant_id"],
3242 "status": "SCHEDULED",
3243 "item": "instance_nets",
3244 "item_id": net
["uuid"],
3245 "extra": yaml
.safe_dump({"params": (net
['vim_net_id'], net
['sdn_net_id'])},
3246 default_flow_style
=True, width
=256)
3249 db_vim_actions
.append(db_vim_action
)
3251 except vimconn
.vimconnNotFoundException
as e
:
3252 error_msg
+= "\n NET VIM_id={} not found at datacenter={}".format(net
['vim_net_id'], net
["datacenter_id"])
3253 logger
.warn("NET '%s', VIM_id '%s', from VNF_net_id '%s' not found",
3254 net
['uuid'], net
['vim_net_id'], str(net
['vnf_net_id']))
3255 except vimconn
.vimconnException
as e
:
3256 error_msg
+= "\n NET VIM_id={} at datacenter={} Error: {} {}".format(net
['vim_net_id'],
3257 net
["datacenter_id"],
3258 e
.http_code
, str(e
))
3259 logger
.error("Error %d deleting NET '%s', VIM_id '%s', from VNF_net_id '%s': %s",
3260 e
.http_code
, net
['uuid'], net
['vim_net_id'], str(net
['vnf_net_id']), str(e
))
3262 db_instance_action
["number_tasks"] = task_index
3264 {"instance_actions": db_instance_action
},
3265 {"vim_actions": db_vim_actions
}
3268 logger
.debug("delete_instance done DB tables: %s",
3269 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False))
3270 mydb
.new_rows(db_tables
, ())
3271 for myvim_thread_id
in vimthread_affected
.keys():
3272 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
3274 if len(error_msg
) > 0:
3275 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
3276 '(error: 404) from VIM: {}'.format(instance_action_id
, message
, error_msg
)
3278 return "action_id={} instance {} deleted".format(instance_action_id
, message
)
3281 def refresh_instance(mydb
, nfvo_tenant
, instanceDict
, datacenter
=None, vim_tenant
=None):
3282 '''Refreshes a scenario instance. It modifies instanceDict'''
3284 - 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
3287 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
3288 # #print "nfvo.refresh_instance begins"
3289 # #print json.dumps(instanceDict, indent=4)
3291 # #print "Getting the VIM URL and the VIM tenant_id"
3294 # # 1. Getting VIM vm and net list
3295 # vms_updated = [] #List of VM instance uuids in openmano that were updated
3298 # for sce_vnf in instanceDict['vnfs']:
3299 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
3300 # if datacenter_key not in vm_list:
3301 # vm_list[datacenter_key] = []
3302 # if datacenter_key not in myvims:
3303 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
3304 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
3305 # if len(vims) == 0:
3306 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
3307 # myvims[datacenter_key] = None
3309 # myvims[datacenter_key] = vims.values()[0]
3310 # for vm in sce_vnf['vms']:
3311 # vm_list[datacenter_key].append(vm['vim_vm_id'])
3312 # vms_notupdated.append(vm["uuid"])
3314 # nets_updated = [] #List of VM instance uuids in openmano that were updated
3315 # nets_notupdated=[]
3317 # for net in instanceDict['nets']:
3318 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
3319 # if datacenter_key not in net_list:
3320 # net_list[datacenter_key] = []
3321 # if datacenter_key not in myvims:
3322 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
3323 # datacenter_tenant_id=net["datacenter_tenant_id"])
3324 # if len(vims) == 0:
3325 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
3326 # myvims[datacenter_key] = None
3328 # myvims[datacenter_key] = vims.values()[0]
3330 # net_list[datacenter_key].append(net['vim_net_id'])
3331 # nets_notupdated.append(net["uuid"])
3333 # # 1. Getting the status of all VMs
3335 # for datacenter_key in myvims:
3336 # if not vm_list.get(datacenter_key):
3340 # if not myvims[datacenter_key]:
3341 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
3344 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
3346 # except vimconn.vimconnException as e:
3347 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
3348 # failed_message = str(e)
3350 # for vm in vm_list[datacenter_key]:
3351 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
3353 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
3354 # for sce_vnf in instanceDict['vnfs']:
3355 # for vm in sce_vnf['vms']:
3356 # vm_id = vm['vim_vm_id']
3357 # interfaces = vm_dict[vm_id].pop('interfaces', [])
3358 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
3359 # has_mgmt_iface = False
3360 # for iface in vm["interfaces"]:
3361 # if iface["type"]=="mgmt":
3362 # has_mgmt_iface = True
3363 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
3364 # vm_dict[vm_id]['status'] = "ACTIVE"
3365 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
3366 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
3367 # 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'):
3368 # vm['status'] = vm_dict[vm_id]['status']
3369 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
3370 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
3371 # # 2.1. Update in openmano DB the VMs whose status changed
3373 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
3374 # vms_notupdated.remove(vm["uuid"])
3376 # vms_updated.append(vm["uuid"])
3377 # except db_base_Exception as e:
3378 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
3379 # # 2.2. Update in openmano DB the interface VMs
3380 # for interface in interfaces:
3381 # #translate from vim_net_id to instance_net_id
3382 # network_id_list=[]
3383 # for net in instanceDict['nets']:
3384 # if net["vim_net_id"] == interface["vim_net_id"]:
3385 # network_id_list.append(net["uuid"])
3386 # if not network_id_list:
3388 # del interface["vim_net_id"]
3390 # for network_id in network_id_list:
3391 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
3392 # except db_base_Exception as e:
3393 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
3395 # # 3. Getting the status of all nets
3397 # for datacenter_key in myvims:
3398 # if not net_list.get(datacenter_key):
3401 # failed_message = ""
3402 # if not myvims[datacenter_key]:
3403 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
3406 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
3408 # except vimconn.vimconnException as e:
3409 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
3410 # failed_message = str(e)
3412 # for net in net_list[datacenter_key]:
3413 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
3415 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
3416 # # TODO: update nets inside a vnf
3417 # for net in instanceDict['nets']:
3418 # net_id = net['vim_net_id']
3419 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
3420 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
3421 # 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'):
3422 # net['status'] = net_dict[net_id]['status']
3423 # net['error_msg'] = net_dict[net_id].get('error_msg')
3424 # net['vim_info'] = net_dict[net_id].get('vim_info')
3425 # # 5.1. Update in openmano DB the nets whose status changed
3427 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
3428 # nets_notupdated.remove(net["uuid"])
3430 # nets_updated.append(net["uuid"])
3431 # except db_base_Exception as e:
3432 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
3434 # # Returns appropriate output
3435 # #print "nfvo.refresh_instance finishes"
3436 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
3437 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
3438 instance_id
= instanceDict
['uuid']
3439 # if len(vms_notupdated)+len(nets_notupdated)>0:
3440 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
3441 # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
3443 return 0, 'Scenario instance ' + instance_id
+ ' refreshed.'
3446 def instance_action(mydb
,nfvo_tenant
,instance_id
, action_dict
):
3447 #print "Checking that the instance_id exists and getting the instance dictionary"
3448 instanceDict
= mydb
.get_instance_scenario(instance_id
, nfvo_tenant
)
3449 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
3451 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
3452 vims
= get_vim(mydb
, nfvo_tenant
, instanceDict
['datacenter_id'])
3454 raise NfvoException("datacenter '{}' not found".format(str(instanceDict
['datacenter_id'])), HTTP_Not_Found
)
3455 myvim
= vims
.values()[0]
3457 if action_dict
.get("create-vdu"):
3458 for vdu
in action_dict
["create-vdu"]:
3459 vdu_id
= vdu
.get("vdu-id")
3460 vdu_count
= vdu
.get("count", 1)
3461 # get from database TODO
3465 input_vnfs
= action_dict
.pop("vnfs", [])
3466 input_vms
= action_dict
.pop("vms", [])
3467 action_over_all
= True if len(input_vnfs
)==0 and len (input_vms
)==0 else False
3471 for sce_vnf
in instanceDict
['vnfs']:
3472 for vm
in sce_vnf
['vms']:
3473 if not action_over_all
:
3474 if sce_vnf
['uuid'] not in input_vnfs
and sce_vnf
['vnf_name'] not in input_vnfs
and \
3475 vm
['uuid'] not in input_vms
and vm
['name'] not in input_vms
:
3478 data
= myvim
.action_vminstance(vm
['vim_vm_id'], action_dict
)
3479 if "console" in action_dict
:
3480 if not global_config
["http_console_proxy"]:
3481 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
3482 "description": "{protocol}//{ip}:{port}/{suffix}".format(
3483 protocol
=data
["protocol"],
3484 ip
= data
["server"],
3485 port
= data
["port"],
3486 suffix
= data
["suffix"]),
3490 elif data
["server"]=="127.0.0.1" or data
["server"]=="localhost":
3491 vm_result
[ vm
['uuid'] ] = {"vim_result": -HTTP_Unauthorized
,
3492 "description": "this console is only reachable by local interface",
3497 #print "console data", data
3499 console_thread
= create_or_use_console_proxy_thread(data
["server"], data
["port"])
3500 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
3501 "description": "{protocol}//{ip}:{port}/{suffix}".format(
3502 protocol
=data
["protocol"],
3503 ip
= global_config
["http_console_host"],
3504 port
= console_thread
.port
,
3505 suffix
= data
["suffix"]),
3509 except NfvoException
as e
:
3510 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
3514 vm_result
[ vm
['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm
['name']}
3516 except vimconn
.vimconnException
as e
:
3517 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
3520 if vm_ok
==0: #all goes wrong
3525 def instance_action_get(mydb
, nfvo_tenant
, instance_id
, action_id
):
3527 if nfvo_tenant
and nfvo_tenant
!= "any":
3528 filter["tenant_id"] = nfvo_tenant
3529 if instance_id
and instance_id
!= "any":
3530 filter["instance_id"] = instance_id
3532 filter["uuid"] = action_id
3533 rows
= mydb
.get_rows(FROM
="instance_actions", WHERE
=filter)
3534 if not rows
and action_id
:
3535 raise NfvoException("Not found any action with this criteria", HTTP_Not_Found
)
3536 return {"ations": rows
}
3539 def create_or_use_console_proxy_thread(console_server
, console_port
):
3540 #look for a non-used port
3541 console_thread_key
= console_server
+ ":" + str(console_port
)
3542 if console_thread_key
in global_config
["console_thread"]:
3543 #global_config["console_thread"][console_thread_key].start_timeout()
3544 return global_config
["console_thread"][console_thread_key
]
3546 for port
in global_config
["console_port_iterator"]():
3547 #print "create_or_use_console_proxy_thread() port:", port
3548 if port
in global_config
["console_ports"]:
3551 clithread
= cli
.ConsoleProxyThread(global_config
['http_host'], port
, console_server
, console_port
)
3553 global_config
["console_thread"][console_thread_key
] = clithread
3554 global_config
["console_ports"][port
] = console_thread_key
3556 except cli
.ConsoleProxyExceptionPortUsed
as e
:
3557 #port used, try with onoher
3559 except cli
.ConsoleProxyException
as e
:
3560 raise NfvoException(str(e
), HTTP_Bad_Request
)
3561 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict
)
3564 def check_tenant(mydb
, tenant_id
):
3565 '''check that tenant exists at database'''
3566 tenant
= mydb
.get_rows(FROM
='nfvo_tenants', SELECT
=('uuid',), WHERE
={'uuid': tenant_id
})
3568 raise NfvoException("tenant '{}' not found".format(tenant_id
), HTTP_Not_Found
)
3572 def new_tenant(mydb
, tenant_dict
):
3573 tenant_id
= mydb
.new_row("nfvo_tenants", tenant_dict
, add_uuid
=True)
3577 def delete_tenant(mydb
, tenant
):
3578 #get nfvo_tenant info
3580 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant
, 'tenant')
3581 mydb
.delete_row_by_id("nfvo_tenants", tenant_dict
['uuid'])
3582 return tenant_dict
['uuid'] + " " + tenant_dict
["name"]
3585 def new_datacenter(mydb
, datacenter_descriptor
):
3586 if "config" in datacenter_descriptor
:
3587 datacenter_descriptor
["config"]=yaml
.safe_dump(datacenter_descriptor
["config"],default_flow_style
=True,width
=256)
3588 #Check that datacenter-type is correct
3589 datacenter_type
= datacenter_descriptor
.get("type", "openvim");
3592 module
= "vimconn_" + datacenter_type
3593 pkg
= __import__("osm_ro." + module
)
3594 vim_conn
= getattr(pkg
, module
)
3595 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
3596 except (IOError, ImportError):
3597 # if module_info and module_info[0]:
3598 # file.close(module_info[0])
3599 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}'.py not installed".format(datacenter_type
, module
), HTTP_Bad_Request
)
3601 datacenter_id
= mydb
.new_row("datacenters", datacenter_descriptor
, add_uuid
=True)
3602 return datacenter_id
3605 def edit_datacenter(mydb
, datacenter_id_name
, datacenter_descriptor
):
3606 # obtain data, check that only one exist
3607 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id_name
)
3610 datacenter_id
= datacenter
['uuid']
3611 where
={'uuid': datacenter
['uuid']}
3612 remove_port_mapping
= False
3613 if "config" in datacenter_descriptor
:
3614 if datacenter_descriptor
['config'] != None:
3616 new_config_dict
= datacenter_descriptor
["config"]
3619 for k
in new_config_dict
:
3620 if new_config_dict
[k
] == None:
3622 if k
== 'sdn-controller':
3623 remove_port_mapping
= True
3625 config_text
= datacenter
.get("config")
3628 config_dict
= yaml
.load(config_text
)
3629 config_dict
.update(new_config_dict
)
3633 except Exception as e
:
3634 raise NfvoException("Bad format at datacenter:config " + str(e
), HTTP_Bad_Request
)
3636 datacenter_descriptor
["config"] = yaml
.safe_dump(config_dict
, default_flow_style
=True, width
=256)
3638 datacenter_descriptor
["config"] = None
3639 if remove_port_mapping
:
3641 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_id
)
3642 except ovimException
as e
:
3643 logger
.error("Error deleting datacenter-port-mapping " + str(e
))
3645 mydb
.update_rows('datacenters', datacenter_descriptor
, where
)
3646 return datacenter_id
3649 def delete_datacenter(mydb
, datacenter
):
3650 #get nfvo_tenant info
3651 datacenter_dict
= mydb
.get_table_by_uuid_name('datacenters', datacenter
, 'datacenter')
3652 mydb
.delete_row_by_id("datacenters", datacenter_dict
['uuid'])
3654 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_dict
['uuid'])
3655 except ovimException
as e
:
3656 logger
.error("Error deleting datacenter-port-mapping " + str(e
))
3657 return datacenter_dict
['uuid'] + " " + datacenter_dict
['name']
3660 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):
3661 #get datacenter info
3662 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, None, datacenter
, vim_user
=vim_username
, vim_passwd
=vim_password
)
3663 datacenter_name
= myvim
["name"]
3665 create_vim_tenant
= True if not vim_tenant_id
and not vim_tenant_name
else False
3667 # get nfvo_tenant info
3668 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant
)
3669 if vim_tenant_name
==None:
3670 vim_tenant_name
=tenant_dict
['name']
3672 #check that this association does not exist before
3673 tenants_datacenter_dict
={"nfvo_tenant_id":tenant_dict
['uuid'], "datacenter_id":datacenter_id
}
3674 tenants_datacenters
= mydb
.get_rows(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
3675 if len(tenants_datacenters
)>0:
3676 raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id
, tenant_dict
['uuid']), HTTP_Conflict
)
3678 vim_tenant_id_exist_atdb
=False
3679 if not create_vim_tenant
:
3680 where_
={"datacenter_id": datacenter_id
}
3681 if vim_tenant_id
!=None:
3682 where_
["vim_tenant_id"] = vim_tenant_id
3683 if vim_tenant_name
!=None:
3684 where_
["vim_tenant_name"] = vim_tenant_name
3685 #check if vim_tenant_id is already at database
3686 datacenter_tenants_dict
= mydb
.get_rows(FROM
='datacenter_tenants', WHERE
=where_
)
3687 if len(datacenter_tenants_dict
)>=1:
3688 datacenter_tenants_dict
= datacenter_tenants_dict
[0]
3689 vim_tenant_id_exist_atdb
=True
3690 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
3692 datacenter_tenants_dict
= {}
3693 #insert at table datacenter_tenants
3694 else: #if vim_tenant_id==None:
3695 #create tenant at VIM if not provided
3697 vim_tenant_id
= myvim
.new_tenant(vim_tenant_name
, "created by openmano for datacenter "+datacenter_name
)
3698 except vimconn
.vimconnException
as e
:
3699 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id
, str(e
)), HTTP_Internal_Server_Error
)
3700 datacenter_tenants_dict
= {}
3701 datacenter_tenants_dict
["created"]="true"
3703 #fill datacenter_tenants table
3704 if not vim_tenant_id_exist_atdb
:
3705 datacenter_tenants_dict
["vim_tenant_id"] = vim_tenant_id
3706 datacenter_tenants_dict
["vim_tenant_name"] = vim_tenant_name
3707 datacenter_tenants_dict
["user"] = vim_username
3708 datacenter_tenants_dict
["passwd"] = vim_password
3709 datacenter_tenants_dict
["datacenter_id"] = datacenter_id
3711 datacenter_tenants_dict
["config"] = yaml
.safe_dump(config
, default_flow_style
=True, width
=256)
3712 id_
= mydb
.new_row('datacenter_tenants', datacenter_tenants_dict
, add_uuid
=True)
3713 datacenter_tenants_dict
["uuid"] = id_
3715 #fill tenants_datacenters table
3716 datacenter_tenant_id
= datacenter_tenants_dict
["uuid"]
3717 tenants_datacenter_dict
["datacenter_tenant_id"] = datacenter_tenant_id
3718 mydb
.new_row('tenants_datacenters', tenants_datacenter_dict
)
3720 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_dict
['uuid'], datacenter_id
) # reload data
3721 thread_name
= get_non_used_vim_name(datacenter_name
, datacenter_id
, tenant_dict
['name'], tenant_dict
['uuid'])
3722 new_thread
= vim_thread
.vim_thread(myvim
, task_lock
, thread_name
, datacenter_name
, datacenter_tenant_id
,
3723 db
=db
, db_lock
=db_lock
, ovim
=ovim
)
3725 thread_id
= datacenter_tenants_dict
["uuid"]
3726 vim_threads
["running"][thread_id
] = new_thread
3727 return datacenter_id
3730 def edit_datacenter_to_tenant(mydb
, nfvo_tenant
, datacenter_id
, vim_tenant_id
=None, vim_tenant_name
=None,
3731 vim_username
=None, vim_password
=None, config
=None):
3732 #Obtain the data of this datacenter_tenant_id
3733 vim_data
= mydb
.get_rows(
3734 SELECT
=("datacenter_tenants.vim_tenant_name", "datacenter_tenants.vim_tenant_id", "datacenter_tenants.user",
3735 "datacenter_tenants.passwd", "datacenter_tenants.config"),
3736 FROM
="datacenter_tenants JOIN tenants_datacenters ON datacenter_tenants.uuid=tenants_datacenters.datacenter_tenant_id",
3737 WHERE
={"tenants_datacenters.nfvo_tenant_id": nfvo_tenant
,
3738 "tenants_datacenters.datacenter_id": datacenter_id
})
3740 logger
.debug(str(vim_data
))
3741 if len(vim_data
) < 1:
3742 raise NfvoException("Datacenter {} is not attached for tenant {}".format(datacenter_id
, nfvo_tenant
), HTTP_Conflict
)
3746 v
['config'] = yaml
.load(v
['config'])
3749 v
['vim_tenant_id'] = vim_tenant_id
3751 v
['vim_tenant_name'] = vim_tenant_name
3753 v
['user'] = vim_username
3755 v
['passwd'] = vim_password
3759 v
['config'].update(config
)
3761 logger
.debug(str(v
))
3762 deassociate_datacenter_to_tenant(mydb
, nfvo_tenant
, datacenter_id
, vim_tenant_id
=v
['vim_tenant_id'])
3763 associate_datacenter_to_tenant(mydb
, nfvo_tenant
, datacenter_id
, vim_tenant_id
=v
['vim_tenant_id'], vim_tenant_name
=v
['vim_tenant_name'],
3764 vim_username
=v
['user'], vim_password
=v
['passwd'], config
=v
['config'])
3766 return datacenter_id
3768 def deassociate_datacenter_to_tenant(mydb
, tenant_id
, datacenter
, vim_tenant_id
=None):
3769 #get datacenter info
3770 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3772 #get nfvo_tenant info
3773 if not tenant_id
or tenant_id
=="any":
3776 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant_id
)
3777 tenant_uuid
= tenant_dict
['uuid']
3779 #check that this association exist before
3780 tenants_datacenter_dict
={"datacenter_id":datacenter_id
}
3782 tenants_datacenter_dict
["nfvo_tenant_id"] = tenant_uuid
3783 tenant_datacenter_list
= mydb
.get_rows(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
3784 if len(tenant_datacenter_list
)==0 and tenant_uuid
:
3785 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id
, tenant_dict
['uuid']), HTTP_Not_Found
)
3787 #delete this association
3788 mydb
.delete_row(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
3790 #get vim_tenant info and deletes
3792 for tenant_datacenter_item
in tenant_datacenter_list
:
3793 vim_tenant_dict
= mydb
.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
3794 #try to delete vim:tenant
3796 mydb
.delete_row_by_id('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
3797 if vim_tenant_dict
['created']=='true':
3798 #delete tenant at VIM if created by NFVO
3800 myvim
.delete_tenant(vim_tenant_dict
['vim_tenant_id'])
3801 except vimconn
.vimconnException
as e
:
3802 warning
= "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict
['vim_tenant_id'], str(e
))
3803 logger
.warn(warning
)
3804 except db_base_Exception
as e
:
3805 logger
.error("Cannot delete datacenter_tenants " + str(e
))
3806 pass # the error will be caused because dependencies, vim_tenant can not be deleted
3807 thread_id
= tenant_datacenter_item
["datacenter_tenant_id"]
3808 thread
= vim_threads
["running"][thread_id
]
3809 thread
.insert_task("exit")
3810 vim_threads
["deleting"][thread_id
] = thread
3811 return "datacenter {} detached. {}".format(datacenter_id
, warning
)
3814 def datacenter_action(mydb
, tenant_id
, datacenter
, action_dict
):
3816 #get datacenter info
3817 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3819 if 'net-update' in action_dict
:
3821 nets
= myvim
.get_network_list(filter_dict
={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
3823 except vimconn
.vimconnException
as e
:
3824 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
3825 raise NfvoException(str(e
), HTTP_Internal_Server_Error
)
3826 #update nets Change from VIM format to NFVO format
3829 net_nfvo
={'datacenter_id': datacenter_id
}
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
3836 net_list
.append(net_nfvo
)
3837 inserted
, deleted
= mydb
.update_datacenter_nets(datacenter_id
, net_list
)
3838 logger
.info("Inserted %d nets, deleted %d old nets", inserted
, deleted
)
3840 elif 'net-edit' in action_dict
:
3841 net
= action_dict
['net-edit'].pop('net')
3842 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
3843 result
= mydb
.update_rows('datacenter_nets', action_dict
['net-edit'],
3844 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
3846 elif 'net-delete' in action_dict
:
3847 net
= action_dict
['net-deelte'].get('net')
3848 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
3849 result
= mydb
.delete_row(FROM
='datacenter_nets',
3850 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
3854 raise NfvoException("Unknown action " + str(action_dict
), HTTP_Bad_Request
)
3857 def datacenter_edit_netmap(mydb
, tenant_id
, datacenter
, netmap
, action_dict
):
3858 #get datacenter info
3859 datacenter_id
, _
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3861 what
= 'uuid' if utils
.check_valid_uuid(netmap
) else 'name'
3862 result
= mydb
.update_rows('datacenter_nets', action_dict
['netmap'],
3863 WHERE
={'datacenter_id':datacenter_id
, what
: netmap
})
3867 def datacenter_new_netmap(mydb
, tenant_id
, datacenter
, action_dict
=None):
3868 #get datacenter info
3869 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3872 action_dict
= action_dict
["netmap"]
3873 if 'vim_id' in action_dict
:
3874 filter_dict
["id"] = action_dict
['vim_id']
3875 if 'vim_name' in action_dict
:
3876 filter_dict
["name"] = action_dict
['vim_name']
3878 filter_dict
["shared"] = True
3881 vim_nets
= myvim
.get_network_list(filter_dict
=filter_dict
)
3882 except vimconn
.vimconnException
as e
:
3883 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
3884 raise NfvoException(str(e
), HTTP_Internal_Server_Error
)
3885 if len(vim_nets
)>1 and action_dict
:
3886 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict
)
3887 elif len(vim_nets
)==0: # and action_dict:
3888 raise NfvoException("Not found a network at VIM with " + str(filter_dict
), HTTP_Not_Found
)
3890 for net
in vim_nets
:
3891 net_nfvo
={'datacenter_id': datacenter_id
}
3892 if action_dict
and "name" in action_dict
:
3893 net_nfvo
['name'] = action_dict
['name']
3895 net_nfvo
['name'] = net
['name']
3896 #net_nfvo['description']= net['name']
3897 net_nfvo
['vim_net_id'] = net
['id']
3898 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
3899 net_nfvo
['shared'] = net
['shared']
3900 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
3902 net_id
= mydb
.new_row("datacenter_nets", net_nfvo
, add_uuid
=True)
3903 net_nfvo
["status"] = "OK"
3904 net_nfvo
["uuid"] = net_id
3905 except db_base_Exception
as e
:
3909 net_nfvo
["status"] = "FAIL: " + str(e
)
3910 net_list
.append(net_nfvo
)
3913 def get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
):
3914 # obtain all network data
3916 if utils
.check_valid_uuid(network_id
):
3917 filter_dict
= {"id": network_id
}
3919 filter_dict
= {"name": network_id
}
3921 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3922 network
= myvim
.get_network_list(filter_dict
=filter_dict
)
3923 except vimconn
.vimconnException
as e
:
3924 raise NfvoException("Not possible to get_sdn_net_id from VIM: {}".format(str(e
)), e
.http_code
)
3926 # ensure the network is defined
3927 if len(network
) == 0:
3928 raise NfvoException("Network {} is not present in the system".format(network_id
),
3931 # ensure there is only one network with the provided name
3932 if len(network
) > 1:
3933 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id
), HTTP_Bad_Request
)
3935 # ensure it is a dataplane network
3936 if network
[0]['type'] != 'data':
3939 # ensure we use the id
3940 network_id
= network
[0]['id']
3942 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
3943 # and with instance_scenario_id==NULL
3944 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
3945 search_dict
= {'vim_net_id': network_id
}
3948 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
3949 result
= mydb
.get_rows(SELECT
=('sdn_net_id',), FROM
='instance_nets', WHERE
=search_dict
)
3950 except db_base_Exception
as e
:
3951 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
3952 network_id
) + str(e
), HTTP_Internal_Server_Error
)
3956 if net
['sdn_net_id'] != None:
3958 sdn_net_id
= net
['sdn_net_id']
3960 if sdn_net_counter
== 0:
3962 elif sdn_net_counter
== 1:
3965 raise NfvoException("More than one SDN network is associated to vim network {}".format(
3966 network_id
), HTTP_Internal_Server_Error
)
3968 def get_sdn_controller_id(mydb
, datacenter
):
3969 # Obtain sdn controller id
3970 config
= mydb
.get_rows(SELECT
=('config',), FROM
='datacenters', WHERE
={'uuid': datacenter
})[0].get('config', '{}')
3974 return yaml
.load(config
).get('sdn-controller')
3976 def vim_net_sdn_attach(mydb
, tenant_id
, datacenter
, network_id
, descriptor
):
3978 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
3979 if not sdn_network_id
:
3980 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
), HTTP_Internal_Server_Error
)
3982 #Obtain sdn controller id
3983 controller_id
= get_sdn_controller_id(mydb
, datacenter
)
3984 if not controller_id
:
3985 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter
), HTTP_Internal_Server_Error
)
3987 #Obtain sdn controller info
3988 sdn_controller
= ovim
.show_of_controller(controller_id
)
3991 'name': 'external_port',
3992 'net_id': sdn_network_id
,
3993 'ofc_id': controller_id
,
3994 'switch_dpid': sdn_controller
['dpid'],
3995 'switch_port': descriptor
['port']
3998 if 'vlan' in descriptor
:
3999 port_data
['vlan'] = descriptor
['vlan']
4000 if 'mac' in descriptor
:
4001 port_data
['mac'] = descriptor
['mac']
4003 result
= ovim
.new_port(port_data
)
4004 except ovimException
as e
:
4005 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
4006 sdn_network_id
, network_id
) + str(e
), HTTP_Internal_Server_Error
)
4007 except db_base_Exception
as e
:
4008 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
4009 network_id
) + str(e
), HTTP_Internal_Server_Error
)
4011 return 'Port uuid: '+ result
4013 def vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, network_id
, port_id
=None):
4015 filter = {'uuid': port_id
}
4017 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
4018 if not sdn_network_id
:
4019 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
),
4020 HTTP_Internal_Server_Error
)
4021 #in case no port_id is specified only ports marked as 'external_port' will be detached
4022 filter = {'name': 'external_port', 'net_id': sdn_network_id
}
4025 port_list
= ovim
.get_ports(columns
={'uuid'}, filter=filter)
4026 except ovimException
as e
:
4027 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
4028 HTTP_Internal_Server_Error
)
4030 if len(port_list
) == 0:
4031 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id
),
4035 for port
in port_list
:
4037 port_uuid_list
.append(port
['uuid'])
4038 ovim
.delete_port(port
['uuid'])
4039 except ovimException
as e
:
4040 raise NfvoException("ovimException deleting port {} for net {}. ".format(port
['uuid'], network_id
) + str(e
), HTTP_Internal_Server_Error
)
4042 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list
))
4044 def vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
):
4045 #get datacenter info
4046 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4049 if utils
.check_valid_uuid(name
):
4050 filter_dict
["id"] = name
4052 filter_dict
["name"] = name
4054 if item
=="networks":
4055 #filter_dict['tenant_id'] = myvim['tenant_id']
4056 content
= myvim
.get_network_list(filter_dict
=filter_dict
)
4058 if len(content
) == 0:
4059 raise NfvoException("Network {} is not present in the system. ".format(name
),
4062 #Update the networks with the attached ports
4064 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, net
['id'])
4065 if sdn_network_id
!= None:
4067 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
4068 port_list
= ovim
.get_ports(columns
={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id
})
4069 except ovimException
as e
:
4070 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
), HTTP_Internal_Server_Error
)
4071 #Remove field name and if port name is external_port save it as 'type'
4072 for port
in port_list
:
4073 if port
['name'] == 'external_port':
4074 port
['type'] = "External"
4076 net
['sdn_network_id'] = sdn_network_id
4077 net
['sdn_attached_ports'] = port_list
4079 elif item
=="tenants":
4080 content
= myvim
.get_tenant_list(filter_dict
=filter_dict
)
4081 elif item
== "images":
4083 content
= myvim
.get_image_list(filter_dict
=filter_dict
)
4085 raise NfvoException(item
+ "?", HTTP_Method_Not_Allowed
)
4086 logger
.debug("vim_action response %s", content
) #update nets Change from VIM format to NFVO format
4087 if name
and len(content
)==1:
4088 return {item
[:-1]: content
[0]}
4089 elif name
and len(content
)==0:
4090 raise NfvoException("No {} found with ".format(item
[:-1]) + " and ".join(map(lambda x
: str(x
[0])+": "+str(x
[1]), filter_dict
.iteritems())),
4093 return {item
: content
}
4094 except vimconn
.vimconnException
as e
:
4095 print "vim_action Not possible to get_%s_list from VIM: %s " % (item
, str(e
))
4096 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item
, str(e
)), e
.http_code
)
4099 def vim_action_delete(mydb
, tenant_id
, datacenter
, item
, name
):
4100 #get datacenter info
4101 if tenant_id
== "any":
4104 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4106 content
= vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
)
4107 logger
.debug("vim_action_delete vim response: " + str(content
))
4108 items
= content
.values()[0]
4109 if type(items
)==list and len(items
)==0:
4110 raise NfvoException("Not found " + item
, HTTP_Not_Found
)
4111 elif type(items
)==list and len(items
)>1:
4112 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item
), HTTP_Not_Found
)
4113 else: # it is a dict
4114 item_id
= items
["id"]
4115 item_name
= str(items
.get("name"))
4118 if item
=="networks":
4119 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
4120 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, item_id
)
4121 if sdn_network_id
!= None:
4122 #Delete any port attachment to this network
4124 port_list
= ovim
.get_ports(columns
={'uuid'}, filter={'net_id': sdn_network_id
})
4125 except ovimException
as e
:
4126 raise NfvoException(
4127 "ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
4128 HTTP_Internal_Server_Error
)
4130 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
4131 for port
in port_list
:
4132 vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, item_id
, port
['uuid'])
4134 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
4136 mydb
.delete_row(FROM
='instance_nets', WHERE
={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id
, 'vim_net_id': item_id
})
4137 except db_base_Exception
as e
:
4138 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence
) +
4139 str(e
), HTTP_Internal_Server_Error
)
4141 #Delete the SDN network
4143 ovim
.delete_network(sdn_network_id
)
4144 except ovimException
as e
:
4145 logger
.error("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
), exc_info
=True)
4146 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
),
4147 HTTP_Internal_Server_Error
)
4149 content
= myvim
.delete_network(item_id
)
4150 elif item
=="tenants":
4151 content
= myvim
.delete_tenant(item_id
)
4152 elif item
== "images":
4153 content
= myvim
.delete_image(item_id
)
4155 raise NfvoException(item
+ "?", HTTP_Method_Not_Allowed
)
4156 except vimconn
.vimconnException
as e
:
4157 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
4158 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item
, name
, str(e
)), e
.http_code
)
4160 return "{} {} {} deleted".format(item
[:-1], item_id
,item_name
)
4163 def vim_action_create(mydb
, tenant_id
, datacenter
, item
, descriptor
):
4164 #get datacenter info
4165 logger
.debug("vim_action_create descriptor %s", str(descriptor
))
4166 if tenant_id
== "any":
4168 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4170 if item
=="networks":
4171 net
= descriptor
["network"]
4172 net_name
= net
.pop("name")
4173 net_type
= net
.pop("type", "bridge")
4174 net_public
= net
.pop("shared", False)
4175 net_ipprofile
= net
.pop("ip_profile", None)
4176 net_vlan
= net
.pop("vlan", None)
4177 content
= myvim
.new_network(net_name
, net_type
, net_ipprofile
, shared
=net_public
, vlan
=net_vlan
) #, **net)
4179 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
4180 if get_sdn_controller_id(mydb
, datacenter
) != None and (net_type
== 'data' or net_type
== 'ptp'):
4183 sdn_network
['vlan'] = net_vlan
4184 sdn_network
['type'] = net_type
4185 sdn_network
['name'] = net_name
4186 ovim_content
= ovim
.new_network(sdn_network
)
4187 except ovimException
as e
:
4188 self
.logger
.error("ovimException creating SDN network={} ".format(
4189 sdn_network
) + str(e
), exc_info
=True)
4190 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network
) + str(e
),
4191 HTTP_Internal_Server_Error
)
4193 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
4194 # use instance_scenario_id=None to distinguish from real instaces of nets
4195 correspondence
= {'instance_scenario_id': None, 'sdn_net_id': ovim_content
, 'vim_net_id': content
}
4196 #obtain datacenter_tenant_id
4197 correspondence
['datacenter_tenant_id'] = mydb
.get_rows(SELECT
=('uuid',), FROM
='datacenter_tenants', WHERE
={'datacenter_id': datacenter
})[0]['uuid']
4200 mydb
.new_row('instance_nets', correspondence
, add_uuid
=True)
4201 except db_base_Exception
as e
:
4202 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: ".format(correspondence
) +
4203 str(e
), HTTP_Internal_Server_Error
)
4204 elif item
=="tenants":
4205 tenant
= descriptor
["tenant"]
4206 content
= myvim
.new_tenant(tenant
["name"], tenant
.get("description"))
4208 raise NfvoException(item
+ "?", HTTP_Method_Not_Allowed
)
4209 except vimconn
.vimconnException
as e
:
4210 raise NfvoException("Not possible to create {} at VIM: {}".format(item
, str(e
)), e
.http_code
)
4212 return vim_action_get(mydb
, tenant_id
, datacenter
, item
, content
)
4214 def sdn_controller_create(mydb
, tenant_id
, sdn_controller
):
4215 data
= ovim
.new_of_controller(sdn_controller
)
4216 logger
.debug('New SDN controller created with uuid {}'.format(data
))
4219 def sdn_controller_update(mydb
, tenant_id
, controller_id
, sdn_controller
):
4220 data
= ovim
.edit_of_controller(controller_id
, sdn_controller
)
4221 msg
= 'SDN controller {} updated'.format(data
)
4225 def sdn_controller_list(mydb
, tenant_id
, controller_id
=None):
4226 if controller_id
== None:
4227 data
= ovim
.get_of_controllers()
4229 data
= ovim
.show_of_controller(controller_id
)
4231 msg
= 'SDN controller list:\n {}'.format(data
)
4235 def sdn_controller_delete(mydb
, tenant_id
, controller_id
):
4236 select_
= ('uuid', 'config')
4237 datacenters
= mydb
.get_rows(FROM
='datacenters', SELECT
=select_
)
4238 for datacenter
in datacenters
:
4239 if datacenter
['config']:
4240 config
= yaml
.load(datacenter
['config'])
4241 if 'sdn-controller' in config
and config
['sdn-controller'] == controller_id
:
4242 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id
, datacenter
['uuid']), HTTP_Conflict
)
4244 data
= ovim
.delete_of_controller(controller_id
)
4245 msg
= 'SDN controller {} deleted'.format(data
)
4249 def datacenter_sdn_port_mapping_set(mydb
, tenant_id
, datacenter_id
, sdn_port_mapping
):
4250 controller
= mydb
.get_rows(FROM
="datacenters", SELECT
=("config",), WHERE
={"uuid":datacenter_id
})
4251 if len(controller
) < 1:
4252 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id
), HTTP_Not_Found
)
4255 sdn_controller_id
= yaml
.load(controller
[0]["config"])["sdn-controller"]
4257 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id
), HTTP_Bad_Request
)
4259 sdn_controller
= ovim
.show_of_controller(sdn_controller_id
)
4260 switch_dpid
= sdn_controller
["dpid"]
4263 for compute_node
in sdn_port_mapping
:
4264 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
4266 element
["compute_node"] = compute_node
["compute_node"]
4267 for port
in compute_node
["ports"]:
4268 element
["pci"] = port
.get("pci")
4269 element
["switch_port"] = port
.get("switch_port")
4270 element
["switch_mac"] = port
.get("switch_mac")
4271 if not element
["pci"] or not (element
["switch_port"] or element
["switch_mac"]):
4272 raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'"
4273 " or 'switch_mac'", HTTP_Bad_Request
)
4274 maps
.append(dict(element
))
4276 return ovim
.set_of_port_mapping(maps
, ofc_id
=sdn_controller_id
, switch_dpid
=switch_dpid
, region
=datacenter_id
)
4278 def datacenter_sdn_port_mapping_list(mydb
, tenant_id
, datacenter_id
):
4279 maps
= ovim
.get_of_port_mappings(db_filter
={"region": datacenter_id
})
4282 "sdn-controller": None,
4283 "datacenter-id": datacenter_id
,
4285 "ports_mapping": list()
4288 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id
)
4289 if datacenter
['config']:
4290 config
= yaml
.load(datacenter
['config'])
4291 if 'sdn-controller' in config
:
4292 controller_id
= config
['sdn-controller']
4293 sdn_controller
= sdn_controller_list(mydb
, tenant_id
, controller_id
)
4294 result
["sdn-controller"] = controller_id
4295 result
["dpid"] = sdn_controller
["dpid"]
4297 if result
["sdn-controller"] == None:
4298 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id
), HTTP_Bad_Request
)
4299 if result
["dpid"] == None:
4300 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result
["sdn-controller"]),
4301 HTTP_Internal_Server_Error
)
4306 ports_correspondence_dict
= dict()
4308 if result
["sdn-controller"] != link
["ofc_id"]:
4309 raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error
)
4310 if result
["dpid"] != link
["switch_dpid"]:
4311 raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error
)
4313 element
["pci"] = link
["pci"]
4314 if link
["switch_port"]:
4315 element
["switch_port"] = link
["switch_port"]
4316 if link
["switch_mac"]:
4317 element
["switch_mac"] = link
["switch_mac"]
4319 if not link
["compute_node"] in ports_correspondence_dict
:
4321 content
["compute_node"] = link
["compute_node"]
4322 content
["ports"] = list()
4323 ports_correspondence_dict
[link
["compute_node"]] = content
4325 ports_correspondence_dict
[link
["compute_node"]]["ports"].append(element
)
4327 for key
in sorted(ports_correspondence_dict
):
4328 result
["ports_mapping"].append(ports_correspondence_dict
[key
])
4332 def datacenter_sdn_port_mapping_delete(mydb
, tenant_id
, datacenter_id
):
4333 return ovim
.clear_of_port_mapping(db_filter
={"region":datacenter_id
})