| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | ## |
| 4 | # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. |
| 5 | # This file is part of openmano |
| 6 | # All Rights Reserved. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | # not use this file except in compliance with the License. You may obtain |
| 10 | # a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 | # License for the specific language governing permissions and limitations |
| 18 | # under the License. |
| 19 | # |
| 20 | # For those usages not covered by the Apache License, Version 2.0 please |
| 21 | # contact with: nfvlabs@tid.es |
| 22 | ## |
| 23 | |
| 24 | ''' |
| 25 | NFVO DB engine. It implements all the methods to interact with the Openmano Database |
| 26 | ''' |
| 27 | __author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes" |
| 28 | __date__ ="$28-aug-2014 10:05:01$" |
| 29 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 30 | import db_base |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 31 | import MySQLdb as mdb |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 32 | import json |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame^] | 33 | import yaml |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 34 | import time |
| 35 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 36 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 37 | tables_with_createdat_field=["datacenters","instance_nets","instance_scenarios","instance_vms","instance_vnfs", |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 38 | "interfaces","nets","nfvo_tenants","scenarios","sce_interfaces","sce_nets", |
| 39 | "sce_vnfs","tenants_datacenters","datacenter_tenants","vms","vnfs"] |
| 40 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 41 | class nfvo_db(db_base.db_base): |
| 42 | def __init__(self, host=None, user=None, passwd=None, database=None, log_name='openmano.db', log_level="ERROR"): |
| 43 | db_base.db_base.__init__(self, host, user, passwd, database, log_name, log_level) |
| 44 | db_base.db_base.tables_with_created_field=tables_with_createdat_field |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 45 | return |
| 46 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 47 | |
| 48 | def new_vnf_as_a_whole(self,nfvo_tenant,vnf_name,vnf_descriptor,VNFCDict): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 49 | self.logger.debug("Adding new vnf to the NFVO database") |
| 50 | tries = 2 |
| 51 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 52 | created_time = time.time() |
| 53 | try: |
| 54 | with self.con: |
| 55 | |
| 56 | myVNFDict = {} |
| 57 | myVNFDict["name"] = vnf_name |
| 58 | myVNFDict["descriptor"] = vnf_descriptor['vnf'].get('descriptor') |
| 59 | myVNFDict["public"] = vnf_descriptor['vnf'].get('public', "false") |
| 60 | myVNFDict["description"] = vnf_descriptor['vnf']['description'] |
| 61 | myVNFDict["class"] = vnf_descriptor['vnf'].get('class',"MISC") |
| 62 | myVNFDict["tenant_id"] = vnf_descriptor['vnf'].get("tenant_id") |
| 63 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 64 | vnf_id = self._new_row_internal('vnfs', myVNFDict, add_uuid=True, root_uuid=None, created_time=created_time) |
| 65 | #print "Adding new vms to the NFVO database" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 66 | #For each vm, we must create the appropriate vm in the NFVO database. |
| 67 | vmDict = {} |
| 68 | for _,vm in VNFCDict.iteritems(): |
| 69 | #This code could make the name of the vms grow and grow. |
| 70 | #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name |
| 71 | #vm['name'] = "%s-%s" % (vnf_name,vm['name']) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 72 | #print "VM name: %s. Description: %s" % (vm['name'], vm['description']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 73 | vm["vnf_id"] = vnf_id |
| 74 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 75 | vm_id = self._new_row_internal('vms', vm, add_uuid=True, root_uuid=vnf_id, created_time=created_time) |
| 76 | #print "Internal vm id in NFVO DB: %s" % vm_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 77 | vmDict[vm['name']] = vm_id |
| 78 | |
| 79 | #Collect the data interfaces of each VM/VNFC under the 'numas' field |
| 80 | dataifacesDict = {} |
| 81 | for vm in vnf_descriptor['vnf']['VNFC']: |
| 82 | dataifacesDict[vm['name']] = {} |
| 83 | for numa in vm.get('numas', []): |
| 84 | for dataiface in numa.get('interfaces',[]): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 85 | db_base._convert_bandwidth(dataiface) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 86 | dataifacesDict[vm['name']][dataiface['name']] = {} |
| 87 | dataifacesDict[vm['name']][dataiface['name']]['vpci'] = dataiface['vpci'] |
| 88 | dataifacesDict[vm['name']][dataiface['name']]['bw'] = dataiface['bandwidth'] |
| 89 | dataifacesDict[vm['name']][dataiface['name']]['model'] = "PF" if dataiface['dedicated']=="yes" else ("VF" if dataiface['dedicated']=="no" else "VFnotShared") |
| 90 | |
| 91 | #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field |
| 92 | bridgeInterfacesDict = {} |
| 93 | for vm in vnf_descriptor['vnf']['VNFC']: |
| 94 | if 'bridge-ifaces' in vm: |
| 95 | bridgeInterfacesDict[vm['name']] = {} |
| 96 | for bridgeiface in vm['bridge-ifaces']: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 97 | db_base._convert_bandwidth(bridgeiface) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 98 | bridgeInterfacesDict[vm['name']][bridgeiface['name']] = {} |
| 99 | bridgeInterfacesDict[vm['name']][bridgeiface['name']]['vpci'] = bridgeiface.get('vpci',None) |
| 100 | bridgeInterfacesDict[vm['name']][bridgeiface['name']]['mac'] = bridgeiface.get('mac_address',None) |
| 101 | bridgeInterfacesDict[vm['name']][bridgeiface['name']]['bw'] = bridgeiface.get('bandwidth', None) |
| 102 | bridgeInterfacesDict[vm['name']][bridgeiface['name']]['model'] = bridgeiface.get('model', None) |
| 103 | |
| 104 | #For each internal connection, we add it to the interfaceDict and we create the appropriate net in the NFVO database. |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 105 | #print "Adding new nets (VNF internal nets) to the NFVO database (if any)" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 106 | internalconnList = [] |
| 107 | if 'internal-connections' in vnf_descriptor['vnf']: |
| 108 | for net in vnf_descriptor['vnf']['internal-connections']: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 109 | #print "Net name: %s. Description: %s" % (net['name'], net['description']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 110 | |
| 111 | myNetDict = {} |
| 112 | myNetDict["name"] = net['name'] |
| 113 | myNetDict["description"] = net['description'] |
| 114 | myNetDict["type"] = net['type'] |
| 115 | myNetDict["vnf_id"] = vnf_id |
| 116 | |
| 117 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 118 | net_id = self._new_row_internal('nets', myNetDict, add_uuid=True, root_uuid=vnf_id, created_time=created_time) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 119 | |
| 120 | for element in net['elements']: |
| 121 | ifaceItem = {} |
| 122 | #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name']) |
| 123 | ifaceItem["internal_name"] = element['local_iface_name'] |
| 124 | #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])] |
| 125 | ifaceItem["vm_id"] = vmDict[element['VNFC']] |
| 126 | ifaceItem["net_id"] = net_id |
| 127 | ifaceItem["type"] = net['type'] |
| 128 | if ifaceItem ["type"] == "data": |
| 129 | ifaceItem["vpci"] = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['vpci'] |
| 130 | ifaceItem["bw"] = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['bw'] |
| 131 | ifaceItem["model"] = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['model'] |
| 132 | else: |
| 133 | ifaceItem["vpci"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['vpci'] |
| 134 | ifaceItem["mac"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['mac_address'] |
| 135 | ifaceItem["bw"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['bw'] |
| 136 | ifaceItem["model"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['model'] |
| 137 | internalconnList.append(ifaceItem) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 138 | #print "Internal net id in NFVO DB: %s" % net_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 139 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 140 | #print "Adding internal interfaces to the NFVO database (if any)" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 141 | for iface in internalconnList: |
| 142 | print "Iface name: %s" % iface['internal_name'] |
| 143 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 144 | iface_id = self._new_row_internal('interfaces', iface, add_uuid=True, root_uuid=vnf_id, created_time=created_time) |
| 145 | #print "Iface id in NFVO DB: %s" % iface_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 146 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 147 | #print "Adding external interfaces to the NFVO database" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 148 | for iface in vnf_descriptor['vnf']['external-connections']: |
| 149 | myIfaceDict = {} |
| 150 | #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name']) |
| 151 | myIfaceDict["internal_name"] = iface['local_iface_name'] |
| 152 | #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])] |
| 153 | myIfaceDict["vm_id"] = vmDict[iface['VNFC']] |
| 154 | myIfaceDict["external_name"] = iface['name'] |
| 155 | myIfaceDict["type"] = iface['type'] |
| 156 | if iface["type"] == "data": |
| 157 | myIfaceDict["vpci"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci'] |
| 158 | myIfaceDict["bw"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw'] |
| 159 | myIfaceDict["model"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model'] |
| 160 | else: |
| 161 | myIfaceDict["vpci"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci'] |
| 162 | myIfaceDict["bw"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw'] |
| 163 | myIfaceDict["model"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model'] |
| 164 | myIfaceDict["mac"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['mac'] |
| 165 | print "Iface name: %s" % iface['name'] |
| 166 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 167 | iface_id = self._new_row_internal('interfaces', myIfaceDict, add_uuid=True, root_uuid=vnf_id, created_time=created_time) |
| 168 | #print "Iface id in NFVO DB: %s" % iface_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 169 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 170 | return vnf_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 171 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 172 | except (mdb.Error, AttributeError) as e: |
| 173 | self._format_error(e, tries) |
| 174 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 175 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 176 | |
| 177 | def new_scenario(self, scenario_dict): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 178 | tries = 2 |
| 179 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 180 | created_time = time.time() |
| 181 | try: |
| 182 | with self.con: |
| 183 | self.cur = self.con.cursor() |
| 184 | tenant_id = scenario_dict.get('tenant_id') |
| 185 | #scenario |
| 186 | INSERT_={'tenant_id': tenant_id, |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 187 | 'name': scenario_dict['name'], |
| 188 | 'description': scenario_dict['description'], |
| 189 | 'public': scenario_dict.get('public', "false")} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 190 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 191 | scenario_uuid = self._new_row_internal('scenarios', INSERT_, add_uuid=True, root_uuid=None, created_time=created_time) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 192 | #sce_nets |
| 193 | for net in scenario_dict['nets'].values(): |
| 194 | net_dict={'scenario_id': scenario_uuid} |
| 195 | net_dict["name"] = net["name"] |
| 196 | net_dict["type"] = net["type"] |
| 197 | net_dict["description"] = net.get("description") |
| 198 | net_dict["external"] = net.get("external", False) |
| 199 | if "graph" in net: |
| 200 | #net["graph"]=yaml.safe_dump(net["graph"],default_flow_style=True,width=256) |
| 201 | #TODO, must be json because of the GUI, change to yaml |
| 202 | net_dict["graph"]=json.dumps(net["graph"]) |
| 203 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 204 | net_uuid = self._new_row_internal('sce_nets', net_dict, add_uuid=True, root_uuid=scenario_uuid, created_time=created_time) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 205 | net['uuid']=net_uuid |
| 206 | #sce_vnfs |
| 207 | for k,vnf in scenario_dict['vnfs'].items(): |
| 208 | INSERT_={'scenario_id': scenario_uuid, |
| 209 | 'name': k, |
| 210 | 'vnf_id': vnf['uuid'], |
| 211 | #'description': scenario_dict['name'] |
| 212 | 'description': vnf['description'] |
| 213 | } |
| 214 | if "graph" in vnf: |
| 215 | #INSERT_["graph"]=yaml.safe_dump(vnf["graph"],default_flow_style=True,width=256) |
| 216 | #TODO, must be json because of the GUI, change to yaml |
| 217 | INSERT_["graph"]=json.dumps(vnf["graph"]) |
| 218 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 219 | scn_vnf_uuid = self._new_row_internal('sce_vnfs', INSERT_, add_uuid=True, root_uuid=scenario_uuid, created_time=created_time) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 220 | vnf['scn_vnf_uuid']=scn_vnf_uuid |
| 221 | #sce_interfaces |
| 222 | for iface in vnf['ifaces'].values(): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 223 | #print 'iface', iface |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 224 | if 'net_key' not in iface: |
| 225 | continue |
| 226 | iface['net_id'] = scenario_dict['nets'][ iface['net_key'] ]['uuid'] |
| 227 | INSERT_={'sce_vnf_id': scn_vnf_uuid, |
| 228 | 'sce_net_id': iface['net_id'], |
| 229 | 'interface_id': iface[ 'uuid' ] |
| 230 | } |
| 231 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 232 | iface_uuid = self._new_row_internal('sce_interfaces', INSERT_, add_uuid=True, root_uuid=scenario_uuid, created_time=created_time) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 233 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 234 | return scenario_uuid |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 235 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 236 | except (mdb.Error, AttributeError) as e: |
| 237 | self._format_error(e, tries) |
| 238 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 239 | |
| 240 | def edit_scenario(self, scenario_dict): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 241 | tries = 2 |
| 242 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 243 | modified_time = time.time() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 244 | item_changed=0 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 245 | try: |
| 246 | with self.con: |
| 247 | self.cur = self.con.cursor() |
| 248 | #check that scenario exist |
| 249 | tenant_id = scenario_dict.get('tenant_id') |
| 250 | scenario_uuid = scenario_dict['uuid'] |
| 251 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 252 | where_text = "uuid='{}'".format(scenario_uuid) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 253 | if not tenant_id and tenant_id != "any": |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 254 | where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id) |
| 255 | cmd = "SELECT * FROM scenarios WHERE "+ where_text |
| 256 | self.logger.debug(cmd) |
| 257 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 258 | self.cur.fetchall() |
| 259 | if self.cur.rowcount==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 260 | raise db_base.db_base_Exception("No scenario found with this criteria " + where_text, db_base.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 261 | elif self.cur.rowcount>1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 262 | raise db_base.db_base_Exception("More than one scenario found with this criteria " + where_text, db_base.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 263 | |
| 264 | #scenario |
| 265 | nodes = {} |
| 266 | topology = scenario_dict.pop("topology", None) |
| 267 | if topology != None and "nodes" in topology: |
| 268 | nodes = topology.get("nodes",{}) |
| 269 | UPDATE_ = {} |
| 270 | if "name" in scenario_dict: UPDATE_["name"] = scenario_dict["name"] |
| 271 | if "description" in scenario_dict: UPDATE_["description"] = scenario_dict["description"] |
| 272 | if len(UPDATE_)>0: |
| 273 | WHERE_={'tenant_id': tenant_id, 'uuid': scenario_uuid} |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 274 | item_changed += self._update_rows('scenarios', UPDATE_, WHERE_, modified_time=modified_time) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 275 | #sce_nets |
| 276 | for node_id, node in nodes.items(): |
| 277 | if "graph" in node: |
| 278 | #node["graph"] = yaml.safe_dump(node["graph"],default_flow_style=True,width=256) |
| 279 | #TODO, must be json because of the GUI, change to yaml |
| 280 | node["graph"] = json.dumps(node["graph"]) |
| 281 | WHERE_={'scenario_id': scenario_uuid, 'uuid': node_id} |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 282 | #Try to change at sce_nets(version 0 API backward compatibility and sce_vnfs) |
| 283 | item_changed += self._update_rows('sce_nets', node, WHERE_) |
| 284 | item_changed += self._update_rows('sce_vnfs', node, WHERE_, modified_time=modified_time) |
| 285 | return item_changed |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 286 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 287 | except (mdb.Error, AttributeError) as e: |
| 288 | self._format_error(e, tries) |
| 289 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 290 | |
| 291 | # def get_instance_scenario(self, instance_scenario_id, tenant_id=None): |
| 292 | # '''Obtain the scenario instance information, filtering by one or serveral of the tenant, uuid or name |
| 293 | # instance_scenario_id is the uuid or the name if it is not a valid uuid format |
| 294 | # Only one scenario isntance must mutch the filtering or an error is returned |
| 295 | # ''' |
| 296 | # print "1******************************************************************" |
| 297 | # try: |
| 298 | # with self.con: |
| 299 | # self.cur = self.con.cursor(mdb.cursors.DictCursor) |
| 300 | # #scenario table |
| 301 | # where_list=[] |
| 302 | # if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 303 | # if db_base._check_valid_uuid(instance_scenario_id): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 304 | # where_list.append( "uuid='" + instance_scenario_id +"'" ) |
| 305 | # else: |
| 306 | # where_list.append( "name='" + instance_scenario_id +"'" ) |
| 307 | # where_text = " AND ".join(where_list) |
| 308 | # self.cur.execute("SELECT * FROM instance_scenarios WHERE "+ where_text) |
| 309 | # rows = self.cur.fetchall() |
| 310 | # if self.cur.rowcount==0: |
| 311 | # return -HTTP_Bad_Request, "No scenario instance found with this criteria " + where_text |
| 312 | # elif self.cur.rowcount>1: |
| 313 | # return -HTTP_Bad_Request, "More than one scenario instance found with this criteria " + where_text |
| 314 | # instance_scenario_dict = rows[0] |
| 315 | # |
| 316 | # #instance_vnfs |
| 317 | # self.cur.execute("SELECT uuid,vnf_id FROM instance_vnfs WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'") |
| 318 | # instance_scenario_dict['instance_vnfs'] = self.cur.fetchall() |
| 319 | # for vnf in instance_scenario_dict['instance_vnfs']: |
| 320 | # #instance_vms |
| 321 | # self.cur.execute("SELECT uuid, vim_vm_id "+ |
| 322 | # "FROM instance_vms "+ |
| 323 | # "WHERE instance_vnf_id='" + vnf['uuid'] +"'" |
| 324 | # ) |
| 325 | # vnf['instance_vms'] = self.cur.fetchall() |
| 326 | # #instance_nets |
| 327 | # self.cur.execute("SELECT uuid, vim_net_id FROM instance_nets WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'") |
| 328 | # instance_scenario_dict['instance_nets'] = self.cur.fetchall() |
| 329 | # |
| 330 | # #instance_interfaces |
| 331 | # self.cur.execute("SELECT uuid, vim_interface_id, instance_vm_id, instance_net_id FROM instance_interfaces WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'") |
| 332 | # instance_scenario_dict['instance_interfaces'] = self.cur.fetchall() |
| 333 | # |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 334 | # db_base._convert_datetime2str(instance_scenario_dict) |
| 335 | # db_base._convert_str2boolean(instance_scenario_dict, ('public','shared','external') ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 336 | # print "2******************************************************************" |
| 337 | # return 1, instance_scenario_dict |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 338 | # except (mdb.Error, AttributeError) as e: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 339 | # print "nfvo_db.get_instance_scenario DB Exception %d: %s" % (e.args[0], e.args[1]) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 340 | # return self._format_error(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 341 | |
| 342 | def get_scenario(self, scenario_id, tenant_id=None, datacenter_id=None): |
| 343 | '''Obtain the scenario information, filtering by one or serveral of the tenant, uuid or name |
| 344 | scenario_id is the uuid or the name if it is not a valid uuid format |
| 345 | if datacenter_id is provided, it supply aditional vim_id fields with the matching vim uuid |
| 346 | Only one scenario must mutch the filtering or an error is returned |
| 347 | ''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 348 | tries = 2 |
| 349 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 350 | try: |
| 351 | with self.con: |
| 352 | self.cur = self.con.cursor(mdb.cursors.DictCursor) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 353 | where_text = "uuid='{}'".format(scenario_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 354 | if not tenant_id and tenant_id != "any": |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 355 | where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id) |
| 356 | cmd = "SELECT * FROM scenarios WHERE " + where_text |
| 357 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 358 | self.cur.execute(cmd) |
| 359 | rows = self.cur.fetchall() |
| 360 | if self.cur.rowcount==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 361 | raise db_base.db_base_Exception("No scenario found with this criteria " + where_text, db_base.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 362 | elif self.cur.rowcount>1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 363 | raise db_base.db_base_Exception("More than one scenario found with this criteria " + where_text, db_base.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 364 | scenario_dict = rows[0] |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame^] | 365 | if scenario_dict["cloud_config"]: |
| 366 | scenario_dict["cloud-config"] = yaml.load(scenario_dict["cloud_config"]) |
| 367 | del scenario_dict["cloud_config"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 368 | #sce_vnfs |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 369 | cmd = "SELECT uuid,name,vnf_id,description FROM sce_vnfs WHERE scenario_id='{}' ORDER BY created_at".format(scenario_dict['uuid']) |
| 370 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 371 | self.cur.execute(cmd) |
| 372 | scenario_dict['vnfs'] = self.cur.fetchall() |
| 373 | for vnf in scenario_dict['vnfs']: |
| 374 | #sce_interfaces |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 375 | cmd = "SELECT uuid,sce_net_id,interface_id FROM sce_interfaces WHERE sce_vnf_id='{}' ORDER BY created_at".format(vnf['uuid']) |
| 376 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 377 | self.cur.execute(cmd) |
| 378 | vnf['interfaces'] = self.cur.fetchall() |
| 379 | #vms |
| 380 | cmd = "SELECT vms.uuid as uuid, flavor_id, image_id, vms.name as name, vms.description as description " \ |
| 381 | " FROM vnfs join vms on vnfs.uuid=vms.vnf_id " \ |
| 382 | " WHERE vnfs.uuid='" + vnf['vnf_id'] +"'" \ |
| 383 | " ORDER BY vms.created_at" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 384 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 385 | self.cur.execute(cmd) |
| 386 | vnf['vms'] = self.cur.fetchall() |
| 387 | for vm in vnf['vms']: |
| 388 | if datacenter_id!=None: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 389 | cmd = "SELECT vim_id FROM datacenters_images WHERE image_id='{}' AND datacenter_id='{}'".format(vm['image_id'],datacenter_id) |
| 390 | self.logger.debug(cmd) |
| 391 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 392 | if self.cur.rowcount==1: |
| 393 | vim_image_dict = self.cur.fetchone() |
| 394 | vm['vim_image_id']=vim_image_dict['vim_id'] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 395 | cmd = "SELECT vim_id FROM datacenters_flavors WHERE flavor_id='{}' AND datacenter_id='{}'".format(vm['flavor_id'],datacenter_id) |
| 396 | self.logger.debug(cmd) |
| 397 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 398 | if self.cur.rowcount==1: |
| 399 | vim_flavor_dict = self.cur.fetchone() |
| 400 | vm['vim_flavor_id']=vim_flavor_dict['vim_id'] |
| 401 | |
| 402 | #interfaces |
| 403 | cmd = "SELECT uuid,internal_name,external_name,net_id,type,vpci,mac,bw,model" \ |
| 404 | " FROM interfaces" \ |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 405 | " WHERE vm_id='{}'" \ |
| 406 | " ORDER BY created_at".format(vm['uuid']) |
| 407 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 408 | self.cur.execute(cmd) |
| 409 | vm['interfaces'] = self.cur.fetchall() |
| 410 | #nets every net of a vms |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 411 | cmd = "SELECT uuid,name,type,description FROM nets WHERE vnf_id='{}'".format(vnf['vnf_id']) |
| 412 | self.logger.debug(cmd) |
| 413 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 414 | vnf['nets'] = self.cur.fetchall() |
| 415 | #sce_nets |
| 416 | cmd = "SELECT uuid,name,type,external,description" \ |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 417 | " FROM sce_nets WHERE scenario_id='{}'" \ |
| 418 | " ORDER BY created_at ".format(scenario_dict['uuid']) |
| 419 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 420 | self.cur.execute(cmd) |
| 421 | scenario_dict['nets'] = self.cur.fetchall() |
| 422 | #datacenter_nets |
| 423 | for net in scenario_dict['nets']: |
| 424 | if str(net['external']) == 'false': |
| 425 | continue |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 426 | WHERE_=" WHERE name='{}'".format(net['name']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 427 | if datacenter_id!=None: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 428 | WHERE_ += " AND datacenter_id='{}'".format(datacenter_id) |
| 429 | cmd = "SELECT vim_net_id FROM datacenter_nets" + WHERE_ |
| 430 | self.logger.debug(cmd) |
| 431 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 432 | d_net = self.cur.fetchone() |
| 433 | if d_net==None or datacenter_id==None: |
| 434 | #print "nfvo_db.get_scenario() WARNING external net %s not found" % net['name'] |
| 435 | net['vim_id']=None |
| 436 | else: |
| 437 | net['vim_id']=d_net['vim_net_id'] |
| 438 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 439 | db_base._convert_datetime2str(scenario_dict) |
| 440 | db_base._convert_str2boolean(scenario_dict, ('public','shared','external') ) |
| 441 | return scenario_dict |
| 442 | except (mdb.Error, AttributeError) as e: |
| 443 | self._format_error(e, tries) |
| 444 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 445 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 446 | |
| 447 | def delete_scenario(self, scenario_id, tenant_id=None): |
| 448 | '''Deletes a scenario, filtering by one or several of the tenant, uuid or name |
| 449 | scenario_id is the uuid or the name if it is not a valid uuid format |
| 450 | Only one scenario must mutch the filtering or an error is returned |
| 451 | ''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 452 | tries = 2 |
| 453 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 454 | try: |
| 455 | with self.con: |
| 456 | self.cur = self.con.cursor(mdb.cursors.DictCursor) |
| 457 | |
| 458 | #scenario table |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 459 | where_text = "uuid='{}'".format(scenario_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 460 | if not tenant_id and tenant_id != "any": |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 461 | where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id) |
| 462 | cmd = "SELECT * FROM scenarios WHERE "+ where_text |
| 463 | self.logger.debug(cmd) |
| 464 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 465 | rows = self.cur.fetchall() |
| 466 | if self.cur.rowcount==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 467 | raise db_base.db_base_Exception("No scenario found where " + where_text, db_base.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 468 | elif self.cur.rowcount>1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 469 | raise db_base.db_base_Exception("More than one scenario found where " + where_text, db_base.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 470 | scenario_uuid = rows[0]["uuid"] |
| 471 | scenario_name = rows[0]["name"] |
| 472 | |
| 473 | #sce_vnfs |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 474 | cmd = "DELETE FROM scenarios WHERE uuid='{}'".format(scenario_uuid) |
| 475 | self.logger.debug(cmd) |
| 476 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 477 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 478 | return scenario_uuid + " " + scenario_name |
| 479 | except (mdb.Error, AttributeError) as e: |
| 480 | self._format_error(e, tries, "delete", "instances running") |
| 481 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 482 | |
| 483 | def new_instance_scenario_as_a_whole(self,tenant_id,instance_scenario_name,instance_scenario_description,scenarioDict): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 484 | tries = 2 |
| 485 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 486 | created_time = time.time() |
| 487 | try: |
| 488 | with self.con: |
| 489 | self.cur = self.con.cursor() |
| 490 | #instance_scenarios |
| 491 | datacenter_tenant_id = scenarioDict['datacenter_tenant_id'] |
| 492 | datacenter_id = scenarioDict['datacenter_id'] |
| 493 | INSERT_={'tenant_id': tenant_id, |
| 494 | 'datacenter_tenant_id': datacenter_tenant_id, |
| 495 | 'name': instance_scenario_name, |
| 496 | 'description': instance_scenario_description, |
| 497 | 'scenario_id' : scenarioDict['uuid'], |
| 498 | 'datacenter_id': datacenter_id |
| 499 | } |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame^] | 500 | if scenarioDict.get("cloud-config"): |
| 501 | INSERT_["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"], default_flow_style=True, width=256) |
| 502 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 503 | instance_uuid = self._new_row_internal('instance_scenarios', INSERT_, add_uuid=True, root_uuid=None, created_time=created_time) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 504 | |
| 505 | net_scene2instance={} |
| 506 | #instance_nets #nets interVNF |
| 507 | for net in scenarioDict['nets']: |
| 508 | INSERT_={'vim_net_id': net['vim_id'], 'external': net['external'], 'instance_scenario_id':instance_uuid } #, 'type': net['type'] |
| 509 | INSERT_['datacenter_id'] = net.get('datacenter_id', datacenter_id) |
| 510 | INSERT_['datacenter_tenant_id'] = net.get('datacenter_tenant_id', datacenter_tenant_id) |
| 511 | if net.get("uuid"): |
| 512 | INSERT_['sce_net_id'] = net['uuid'] |
| 513 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 514 | instance_net_uuid = self._new_row_internal('instance_nets', INSERT_, True, instance_uuid, created_time) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 515 | net_scene2instance[ net['uuid'] ] = instance_net_uuid |
| 516 | net['uuid'] = instance_net_uuid #overwrite scnario uuid by instance uuid |
| 517 | |
| 518 | #instance_vnfs |
| 519 | for vnf in scenarioDict['vnfs']: |
| 520 | INSERT_={'instance_scenario_id': instance_uuid, 'vnf_id': vnf['vnf_id'] } |
| 521 | INSERT_['datacenter_id'] = vnf.get('datacenter_id', datacenter_id) |
| 522 | INSERT_['datacenter_tenant_id'] = vnf.get('datacenter_tenant_id', datacenter_tenant_id) |
| 523 | if vnf.get("uuid"): |
| 524 | INSERT_['sce_vnf_id'] = vnf['uuid'] |
| 525 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 526 | instance_vnf_uuid = self._new_row_internal('instance_vnfs', INSERT_, True, instance_uuid, created_time) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 527 | vnf['uuid'] = instance_vnf_uuid #overwrite scnario uuid by instance uuid |
| 528 | |
| 529 | #instance_nets #nets intraVNF |
| 530 | for net in vnf['nets']: |
| 531 | INSERT_={'vim_net_id': net['vim_id'], 'external': 'false', 'instance_scenario_id':instance_uuid } #, 'type': net['type'] |
| 532 | INSERT_['datacenter_id'] = net.get('datacenter_id', datacenter_id) |
| 533 | INSERT_['datacenter_tenant_id'] = net.get('datacenter_tenant_id', datacenter_tenant_id) |
| 534 | if net.get("uuid"): |
| 535 | INSERT_['net_id'] = net['uuid'] |
| 536 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 537 | instance_net_uuid = self._new_row_internal('instance_nets', INSERT_, True, instance_uuid, created_time) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 538 | net_scene2instance[ net['uuid'] ] = instance_net_uuid |
| 539 | net['uuid'] = instance_net_uuid #overwrite scnario uuid by instance uuid |
| 540 | |
| 541 | #instance_vms |
| 542 | for vm in vnf['vms']: |
| 543 | INSERT_={'instance_vnf_id': instance_vnf_uuid, 'vm_id': vm['uuid'], 'vim_vm_id': vm['vim_id'] } |
| 544 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 545 | instance_vm_uuid = self._new_row_internal('instance_vms', INSERT_, True, instance_uuid, created_time) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 546 | vm['uuid'] = instance_vm_uuid #overwrite scnario uuid by instance uuid |
| 547 | |
| 548 | #instance_interfaces |
| 549 | for interface in vm['interfaces']: |
| 550 | net_id = interface.get('net_id', None) |
| 551 | if net_id is None: |
| 552 | #check if is connected to a inter VNFs net |
| 553 | for iface in vnf['interfaces']: |
| 554 | if iface['interface_id'] == interface['uuid']: |
| 555 | net_id = iface.get('sce_net_id', None) |
| 556 | break |
| 557 | if net_id is None: |
| 558 | continue |
| 559 | interface_type='external' if interface['external_name'] is not None else 'internal' |
| 560 | INSERT_={'instance_vm_id': instance_vm_uuid, 'instance_net_id': net_scene2instance[net_id], |
| 561 | 'interface_id': interface['uuid'], 'vim_interface_id': interface.get('vim_id'), 'type': interface_type } |
| 562 | #created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 563 | interface_uuid = self._new_row_internal('instance_interfaces', INSERT_, True, instance_uuid) #, created_time) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 564 | interface['uuid'] = interface_uuid #overwrite scnario uuid by instance uuid |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 565 | return instance_uuid |
| 566 | except (mdb.Error, AttributeError) as e: |
| 567 | self._format_error(e, tries) |
| 568 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 569 | |
| 570 | def get_instance_scenario(self, instance_id, tenant_id=None, verbose=False): |
| 571 | '''Obtain the instance information, filtering by one or several of the tenant, uuid or name |
| 572 | instance_id is the uuid or the name if it is not a valid uuid format |
| 573 | Only one instance must mutch the filtering or an error is returned |
| 574 | ''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 575 | tries = 2 |
| 576 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 577 | try: |
| 578 | with self.con: |
| 579 | self.cur = self.con.cursor(mdb.cursors.DictCursor) |
| 580 | #instance table |
| 581 | where_list=[] |
| 582 | if tenant_id is not None: where_list.append( "inst.tenant_id='" + tenant_id +"'" ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 583 | if db_base._check_valid_uuid(instance_id): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 584 | where_list.append( "inst.uuid='" + instance_id +"'" ) |
| 585 | else: |
| 586 | where_list.append( "inst.name='" + instance_id +"'" ) |
| 587 | where_text = " AND ".join(where_list) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 588 | cmd = "SELECT inst.uuid as uuid,inst.name as name,inst.scenario_id as scenario_id, datacenter_id" +\ |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 589 | " ,datacenter_tenant_id, s.name as scenario_name,inst.tenant_id as tenant_id" + \ |
| 590 | " ,inst.description as description,inst.created_at as created_at" +\ |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame^] | 591 | " ,inst.cloud_config as 'cloud_config'" +\ |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 592 | " FROM instance_scenarios as inst join scenarios as s on inst.scenario_id=s.uuid"+\ |
| 593 | " WHERE " + where_text |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 594 | self.logger.debug(cmd) |
| 595 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 596 | rows = self.cur.fetchall() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 597 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 598 | if self.cur.rowcount==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 599 | raise db_base.db_base_Exception("No instance found where " + where_text, db_base.HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 600 | elif self.cur.rowcount>1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 601 | raise db_base.db_base_Exception("More than one instance found where " + where_text, db_base.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 602 | instance_dict = rows[0] |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame^] | 603 | if instance_dict["cloud_config"]: |
| 604 | instance_dict["cloud-config"] = yaml.load(instance_dict["cloud_config"]) |
| 605 | del instance_dict["cloud_config"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 606 | |
| 607 | #instance_vnfs |
| 608 | cmd = "SELECT iv.uuid as uuid,sv.vnf_id as vnf_id,sv.name as vnf_name, sce_vnf_id, datacenter_id, datacenter_tenant_id"\ |
| 609 | " FROM instance_vnfs as iv join sce_vnfs as sv on iv.sce_vnf_id=sv.uuid" \ |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 610 | " WHERE iv.instance_scenario_id='{}'" \ |
| 611 | " ORDER BY iv.created_at ".format(instance_dict['uuid']) |
| 612 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 613 | self.cur.execute(cmd) |
| 614 | instance_dict['vnfs'] = self.cur.fetchall() |
| 615 | for vnf in instance_dict['vnfs']: |
| 616 | vnf_manage_iface_list=[] |
| 617 | #instance vms |
| 618 | cmd = "SELECT iv.uuid as uuid, vim_vm_id, status, error_msg, vim_info, iv.created_at as created_at, name "\ |
| 619 | " FROM instance_vms as iv join vms on iv.vm_id=vms.uuid "\ |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 620 | " WHERE instance_vnf_id='{}' ORDER BY iv.created_at".format(vnf['uuid']) |
| 621 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 622 | self.cur.execute(cmd) |
| 623 | vnf['vms'] = self.cur.fetchall() |
| 624 | for vm in vnf['vms']: |
| 625 | vm_manage_iface_list=[] |
| 626 | #instance_interfaces |
| garciadeblas | 0c317ee | 2016-08-29 12:33:06 +0200 | [diff] [blame] | 627 | cmd = "SELECT vim_interface_id, instance_net_id, internal_name,external_name, mac_address, ii.ip_address as ip_address, vim_info, i.type as type "\ |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 628 | " FROM instance_interfaces as ii join interfaces as i on ii.interface_id=i.uuid "\ |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 629 | " WHERE instance_vm_id='{}' ORDER BY created_at".format(vm['uuid']) |
| 630 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 631 | self.cur.execute(cmd ) |
| 632 | vm['interfaces'] = self.cur.fetchall() |
| 633 | for iface in vm['interfaces']: |
| 634 | if iface["type"] == "mgmt" and iface["ip_address"]: |
| 635 | vnf_manage_iface_list.append(iface["ip_address"]) |
| 636 | vm_manage_iface_list.append(iface["ip_address"]) |
| 637 | if not verbose: |
| 638 | del iface["type"] |
| 639 | if vm_manage_iface_list: vm["ip_address"] = ",".join(vm_manage_iface_list) |
| 640 | if vnf_manage_iface_list: vnf["ip_address"] = ",".join(vnf_manage_iface_list) |
| 641 | |
| 642 | #instance_nets |
| 643 | #select_text = "instance_nets.uuid as uuid,sce_nets.name as net_name,instance_nets.vim_net_id as net_id,instance_nets.status as status,instance_nets.external as external" |
| 644 | #from_text = "instance_nets join instance_scenarios on instance_nets.instance_scenario_id=instance_scenarios.uuid " + \ |
| 645 | # "join sce_nets on instance_scenarios.scenario_id=sce_nets.scenario_id" |
| 646 | #where_text = "instance_nets.instance_scenario_id='"+ instance_dict['uuid'] + "'" |
| 647 | cmd = "SELECT uuid,vim_net_id,status,error_msg,vim_info,external, sce_net_id, net_id as vnf_net_id, datacenter_id, datacenter_tenant_id"\ |
| 648 | " FROM instance_nets" \ |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 649 | " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict['uuid']) |
| 650 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 651 | self.cur.execute(cmd) |
| 652 | instance_dict['nets'] = self.cur.fetchall() |
| 653 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 654 | db_base._convert_datetime2str(instance_dict) |
| 655 | db_base._convert_str2boolean(instance_dict, ('public','shared','external') ) |
| 656 | return instance_dict |
| 657 | except (mdb.Error, AttributeError) as e: |
| 658 | self._format_error(e, tries) |
| 659 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 660 | |
| 661 | def delete_instance_scenario(self, instance_id, tenant_id=None): |
| 662 | '''Deletes a instance_Scenario, filtering by one or serveral of the tenant, uuid or name |
| 663 | instance_id is the uuid or the name if it is not a valid uuid format |
| 664 | Only one instance_scenario must mutch the filtering or an error is returned |
| 665 | ''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 666 | tries = 2 |
| 667 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 668 | try: |
| 669 | with self.con: |
| 670 | self.cur = self.con.cursor(mdb.cursors.DictCursor) |
| 671 | |
| 672 | #instance table |
| 673 | where_list=[] |
| 674 | if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 675 | if db_base._check_valid_uuid(instance_id): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 676 | where_list.append( "uuid='" + instance_id +"'" ) |
| 677 | else: |
| 678 | where_list.append( "name='" + instance_id +"'" ) |
| 679 | where_text = " AND ".join(where_list) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 680 | cmd = "SELECT * FROM instance_scenarios WHERE "+ where_text |
| 681 | self.logger.debug(cmd) |
| 682 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 683 | rows = self.cur.fetchall() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 684 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 685 | if self.cur.rowcount==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 686 | raise db_base.db_base_Exception("No instance found where " + where_text, db_base.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 687 | elif self.cur.rowcount>1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 688 | raise db_base.db_base_Exception("More than one instance found where " + where_text, db_base.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 689 | instance_uuid = rows[0]["uuid"] |
| 690 | instance_name = rows[0]["name"] |
| 691 | |
| 692 | #sce_vnfs |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 693 | cmd = "DELETE FROM instance_scenarios WHERE uuid='{}'".format(instance_uuid) |
| 694 | self.logger.debug(cmd) |
| 695 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 696 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 697 | return instance_uuid + " " + instance_name |
| 698 | except (mdb.Error, AttributeError) as e: |
| 699 | self._format_error(e, tries, "delete", "No dependences can avoid deleting!!!!") |
| 700 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 701 | |
| 702 | def new_instance_scenario(self, instance_scenario_dict, tenant_id): |
| 703 | #return self.new_row('vnfs', vnf_dict, None, tenant_id, True, True) |
| 704 | return self._new_row_internal('instance_scenarios', instance_scenario_dict, tenant_id, add_uuid=True, root_uuid=None, log=True) |
| 705 | |
| 706 | def update_instance_scenario(self, instance_scenario_dict): |
| 707 | #TODO: |
| 708 | return |
| 709 | |
| 710 | def new_instance_vnf(self, instance_vnf_dict, tenant_id, instance_scenario_id = None): |
| 711 | #return self.new_row('vms', vm_dict, tenant_id, True, True) |
| 712 | return self._new_row_internal('instance_vnfs', instance_vnf_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True) |
| 713 | |
| 714 | def update_instance_vnf(self, instance_vnf_dict): |
| 715 | #TODO: |
| 716 | return |
| 717 | |
| 718 | def delete_instance_vnf(self, instance_vnf_id): |
| 719 | #TODO: |
| 720 | return |
| 721 | |
| 722 | def new_instance_vm(self, instance_vm_dict, tenant_id, instance_scenario_id = None): |
| 723 | #return self.new_row('vms', vm_dict, tenant_id, True, True) |
| 724 | return self._new_row_internal('instance_vms', instance_vm_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True) |
| 725 | |
| 726 | def update_instance_vm(self, instance_vm_dict): |
| 727 | #TODO: |
| 728 | return |
| 729 | |
| 730 | def delete_instance_vm(self, instance_vm_id): |
| 731 | #TODO: |
| 732 | return |
| 733 | |
| 734 | def new_instance_net(self, instance_net_dict, tenant_id, instance_scenario_id = None): |
| 735 | return self._new_row_internal('instance_nets', instance_net_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True) |
| 736 | |
| 737 | def update_instance_net(self, instance_net_dict): |
| 738 | #TODO: |
| 739 | return |
| 740 | |
| 741 | def delete_instance_net(self, instance_net_id): |
| 742 | #TODO: |
| 743 | return |
| 744 | |
| 745 | def new_instance_interface(self, instance_interface_dict, tenant_id, instance_scenario_id = None): |
| 746 | return self._new_row_internal('instance_interfaces', instance_interface_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True) |
| 747 | |
| 748 | def update_instance_interface(self, instance_interface_dict): |
| 749 | #TODO: |
| 750 | return |
| 751 | |
| 752 | def delete_instance_interface(self, instance_interface_dict): |
| 753 | #TODO: |
| 754 | return |
| 755 | |
| 756 | def update_datacenter_nets(self, datacenter_id, new_net_list=[]): |
| 757 | ''' Removes the old and adds the new net list at datacenter list for one datacenter. |
| 758 | Attribute |
| 759 | datacenter_id: uuid of the datacenter to act upon |
| 760 | table: table where to insert |
| 761 | new_net_list: the new values to be inserted. If empty it only deletes the existing nets |
| 762 | Return: (Inserted items, Deleted items) if OK, (-Error, text) if error |
| 763 | ''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 764 | tries = 2 |
| 765 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 766 | created_time = time.time() |
| 767 | try: |
| 768 | with self.con: |
| 769 | self.cur = self.con.cursor() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 770 | cmd="DELETE FROM datacenter_nets WHERE datacenter_id='{}'".format(datacenter_id) |
| 771 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 772 | self.cur.execute(cmd) |
| 773 | deleted = self.cur.rowcount |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 774 | inserted = 0 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 775 | for new_net in new_net_list: |
| 776 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 777 | self._new_row_internal('datacenter_nets', new_net, add_uuid=True, created_time=created_time) |
| 778 | inserted += 1 |
| 779 | return inserted, deleted |
| 780 | except (mdb.Error, AttributeError) as e: |
| 781 | self._format_error(e, tries) |
| 782 | tries -= 1 |
| 783 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 784 | |