+ if isinstance(e, db_base_Exception):
+ error_text = "Exception at database"
+ else:
+ error_text = "Exception at VIM"
+ error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
+ #logger.error("start_scenario %s", error_text)
+ raise NfvoException(error_text, e.http_code)
+
+def unify_cloud_config(cloud_config):
+ index_to_delete = []
+ users = cloud_config.get("users", [])
+ for index0 in range(0,len(users)):
+ if index0 in index_to_delete:
+ continue
+ for index1 in range(index0+1,len(users)):
+ if index1 in index_to_delete:
+ continue
+ if users[index0]["name"] == users[index1]["name"]:
+ index_to_delete.append(index1)
+ for key in users[index1].get("key-pairs",()):
+ if "key-pairs" not in users[index0]:
+ users[index0]["key-pairs"] = [key]
+ elif key not in users[index0]["key-pairs"]:
+ users[index0]["key-pairs"].append(key)
+ index_to_delete.sort(reverse=True)
+ for index in index_to_delete:
+ del users[index]
+
+def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None):
+ datacenter_id = None
+ datacenter_name = None
+ if datacenter_id_name:
+ if utils.check_valid_uuid(datacenter_id_name):
+ datacenter_id = datacenter_id_name
+ else:
+ datacenter_name = datacenter_id_name
+ vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, vim_tenant=None)
+ if len(vims) == 0:
+ raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
+ elif len(vims)>1:
+ #print "nfvo.datacenter_action() error. Several datacenters found"
+ raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
+ return vims.keys()[0], vims.values()[0]
+
+def new_scenario_v03(mydb, tenant_id, scenario_dict):
+ scenario = scenario_dict["scenario"]
+ if tenant_id != "any":
+ check_tenant(mydb, tenant_id)
+ if "tenant_id" in scenario:
+ if scenario["tenant_id"] != tenant_id:
+ logger("Tenant '%s' not found", tenant_id)
+ raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
+ scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
+ else:
+ tenant_id=None
+
+#1: Check that VNF are present at database table vnfs and update content into scenario dict
+ for name,vnf in scenario["vnfs"].iteritems():
+ where={}
+ where_or={"tenant_id": tenant_id, 'public': "true"}
+ error_text = ""
+ error_pos = "'scenario':'vnfs':'" + name + "'"
+ if 'vnf_id' in vnf:
+ error_text += " 'vnf_id' " + vnf['vnf_id']
+ where['uuid'] = vnf['vnf_id']
+ if 'vnf_name' in vnf:
+ error_text += " 'vnf_name' " + vnf['vnf_name']
+ where['name'] = vnf['vnf_name']
+ if len(where) == 0:
+ raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
+ vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
+ FROM='vnfs',
+ WHERE=where,
+ WHERE_OR=where_or,
+ WHERE_AND_OR="AND")
+ if len(vnf_db)==0:
+ raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
+ elif len(vnf_db)>1:
+ raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
+ vnf['uuid']=vnf_db[0]['uuid']
+ vnf['description']=vnf_db[0]['description']
+ vnf['ifaces'] = {}
+ # get external interfaces
+ ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
+ FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
+ WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
+ for ext_iface in ext_ifaces:
+ vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
+
+ # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
+
+#2: Insert net_key and ip_address at every vnf interface
+ for net_name,net in scenario["networks"].iteritems():
+ net_type_bridge=False
+ net_type_data=False
+ for iface_dict in net["interfaces"]:
+ logger.debug("Iface_dict %s", iface_dict)
+ vnf = iface_dict["vnf"]
+ iface = iface_dict["vnf_interface"]
+ if vnf not in scenario["vnfs"]:
+ error_text = "Error at 'networks':'%s':'interfaces' VNF '%s' not match any VNF at 'vnfs'" % (net_name, vnf)
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Not_Found)
+ if iface not in scenario["vnfs"][vnf]['ifaces']:
+ error_text = "Error at 'networks':'%s':'interfaces':'%s' interface not match any VNF interface" % (net_name, iface)
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Bad_Request)
+ if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
+ error_text = "Error at 'networks':'%s':'interfaces':'%s' interface already connected at network '%s'" \
+ % (net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Bad_Request)
+ scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
+ scenario["vnfs"][vnf]['ifaces'][ iface ]['ip_address'] = iface_dict.get('ip_address',None)
+ iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
+ if iface_type=='mgmt' or iface_type=='bridge':
+ net_type_bridge = True
+ else:
+ net_type_data = True
+ if net_type_bridge and net_type_data:
+ error_text = "Error connection interfaces of bridge type and data type at 'networks':'%s':'interfaces'" % (net_name)
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Bad_Request)
+ elif net_type_bridge:
+ type_='bridge'
+ else:
+ type_='data' if len(net["interfaces"])>2 else 'ptp'
+
+ if ("implementation" in net):
+ if (type_ == "bridge" and net["implementation"] == "underlay"):
+ error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at 'network':'%s'" % (net_name)
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Bad_Request)
+ elif (type_ <> "bridge" and net["implementation"] == "overlay"):
+ error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at 'network':'%s'" % (net_name)
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Bad_Request)
+ net.pop("implementation")
+ if ("type" in net):
+ if (type_ == "data" and net["type"] == "e-line"):
+ error_text = "Error connecting more than 2 interfaces of data type to a network declared as type 'e-line' at 'network':'%s'" % (net_name)
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Bad_Request)
+ elif (type_ == "ptp" and net["type"] == "e-lan"):
+ type_ = "data"
+
+ net['type'] = type_
+ net['name'] = net_name
+ net['external'] = net.get('external', False)
+
+#3: insert at database
+ scenario["nets"] = scenario["networks"]
+ scenario['tenant_id'] = tenant_id
+ scenario_id = mydb.new_scenario2(scenario)
+ return scenario_id
+
+def update(d, u):
+ '''Takes dict d and updates it with the values in dict u.'''
+ '''It merges all depth levels'''
+ for k, v in u.iteritems():
+ if isinstance(v, collections.Mapping):
+ r = update(d.get(k, {}), v)
+ d[k] = r
+ else:
+ d[k] = u[k]
+ return d