| 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 | |
| Adam Israel | 8e3ce87 | 2018-01-08 18:43:40 +0000 | [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 |
| tierno | 4319dad | 2016-09-05 12:11:11 +0200 | [diff] [blame] | 35 | #import sys, os |
| 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", |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 39 | "sce_vnfs","tenants_datacenters","datacenter_tenants","vms","vnfs", "datacenter_nets", |
| 40 | "instance_actions", "vim_actions"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 41 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 42 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 43 | class nfvo_db(db_base.db_base): |
| tierno | b13f3cc | 2016-09-26 10:14:44 +0200 | [diff] [blame] | 44 | def __init__(self, host=None, user=None, passwd=None, database=None, log_name='openmano.db', log_level=None): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 45 | db_base.db_base.__init__(self, host, user, passwd, database, log_name, log_level) |
| 46 | db_base.db_base.tables_with_created_field=tables_with_createdat_field |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 47 | return |
| 48 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 49 | 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] | 50 | self.logger.debug("Adding new vnf to the NFVO database") |
| 51 | tries = 2 |
| 52 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 53 | created_time = time.time() |
| 54 | try: |
| 55 | with self.con: |
| 56 | |
| 57 | myVNFDict = {} |
| 58 | myVNFDict["name"] = vnf_name |
| 59 | myVNFDict["descriptor"] = vnf_descriptor['vnf'].get('descriptor') |
| 60 | myVNFDict["public"] = vnf_descriptor['vnf'].get('public', "false") |
| 61 | myVNFDict["description"] = vnf_descriptor['vnf']['description'] |
| 62 | myVNFDict["class"] = vnf_descriptor['vnf'].get('class',"MISC") |
| 63 | myVNFDict["tenant_id"] = vnf_descriptor['vnf'].get("tenant_id") |
| 64 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 65 | vnf_id = self._new_row_internal('vnfs', myVNFDict, add_uuid=True, root_uuid=None, created_time=created_time) |
| 66 | #print "Adding new vms to the NFVO database" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 67 | #For each vm, we must create the appropriate vm in the NFVO database. |
| 68 | vmDict = {} |
| Adam Israel | 8e3ce87 | 2018-01-08 18:43:40 +0000 | [diff] [blame] | 69 | for _,vm in VNFCDict.iteritems(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 70 | #This code could make the name of the vms grow and grow. |
| 71 | #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name |
| 72 | #vm['name'] = "%s-%s" % (vnf_name,vm['name']) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 73 | #print "VM name: %s. Description: %s" % (vm['name'], vm['description']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 74 | vm["vnf_id"] = vnf_id |
| 75 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 76 | vm_id = self._new_row_internal('vms', vm, add_uuid=True, root_uuid=vnf_id, created_time=created_time) |
| 77 | #print "Internal vm id in NFVO DB: %s" % vm_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 78 | vmDict[vm['name']] = vm_id |
| 79 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 80 | #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field |
| 81 | bridgeInterfacesDict = {} |
| 82 | for vm in vnf_descriptor['vnf']['VNFC']: |
| 83 | if 'bridge-ifaces' in vm: |
| 84 | bridgeInterfacesDict[vm['name']] = {} |
| 85 | for bridgeiface in vm['bridge-ifaces']: |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 86 | created_time += 0.00001 |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 87 | if 'port-security' in bridgeiface: |
| 88 | bridgeiface['port_security'] = bridgeiface.pop('port-security') |
| 89 | if 'floating-ip' in bridgeiface: |
| 90 | bridgeiface['floating_ip'] = bridgeiface.pop('floating-ip') |
| tierno | 44528e4 | 2016-10-11 12:06:25 +0000 | [diff] [blame] | 91 | db_base._convert_bandwidth(bridgeiface, logger=self.logger) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 92 | bridgeInterfacesDict[vm['name']][bridgeiface['name']] = {} |
| 93 | bridgeInterfacesDict[vm['name']][bridgeiface['name']]['vpci'] = bridgeiface.get('vpci',None) |
| 94 | bridgeInterfacesDict[vm['name']][bridgeiface['name']]['mac'] = bridgeiface.get('mac_address',None) |
| 95 | bridgeInterfacesDict[vm['name']][bridgeiface['name']]['bw'] = bridgeiface.get('bandwidth', None) |
| 96 | bridgeInterfacesDict[vm['name']][bridgeiface['name']]['model'] = bridgeiface.get('model', None) |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 97 | bridgeInterfacesDict[vm['name']][bridgeiface['name']]['port_security'] = \ |
| 98 | int(bridgeiface.get('port_security', True)) |
| 99 | bridgeInterfacesDict[vm['name']][bridgeiface['name']]['floating_ip'] = \ |
| 100 | int(bridgeiface.get('floating_ip', False)) |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 101 | bridgeInterfacesDict[vm['name']][bridgeiface['name']]['created_time'] = created_time |
| 102 | |
| 103 | # Collect the data interfaces of each VM/VNFC under the 'numas' field |
| 104 | dataifacesDict = {} |
| 105 | for vm in vnf_descriptor['vnf']['VNFC']: |
| 106 | dataifacesDict[vm['name']] = {} |
| 107 | for numa in vm.get('numas', []): |
| 108 | for dataiface in numa.get('interfaces', []): |
| 109 | created_time += 0.00001 |
| 110 | db_base._convert_bandwidth(dataiface, logger=self.logger) |
| 111 | dataifacesDict[vm['name']][dataiface['name']] = {} |
| tierno | c3b6d37 | 2017-05-30 17:12:00 +0200 | [diff] [blame] | 112 | dataifacesDict[vm['name']][dataiface['name']]['vpci'] = dataiface.get('vpci') |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 113 | dataifacesDict[vm['name']][dataiface['name']]['bw'] = dataiface['bandwidth'] |
| 114 | dataifacesDict[vm['name']][dataiface['name']]['model'] = "PF" if dataiface[ |
| 115 | 'dedicated'] == "yes" else ( |
| 116 | "VF" if dataiface['dedicated'] == "no" else "VFnotShared") |
| 117 | dataifacesDict[vm['name']][dataiface['name']]['created_time'] = created_time |
| 118 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 119 | #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] | 120 | #print "Adding new nets (VNF internal nets) to the NFVO database (if any)" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 121 | internalconnList = [] |
| 122 | if 'internal-connections' in vnf_descriptor['vnf']: |
| 123 | for net in vnf_descriptor['vnf']['internal-connections']: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 124 | #print "Net name: %s. Description: %s" % (net['name'], net['description']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 125 | |
| 126 | myNetDict = {} |
| 127 | myNetDict["name"] = net['name'] |
| 128 | myNetDict["description"] = net['description'] |
| 129 | myNetDict["type"] = net['type'] |
| 130 | myNetDict["vnf_id"] = vnf_id |
| 131 | |
| 132 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 133 | 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] | 134 | |
| 135 | for element in net['elements']: |
| 136 | ifaceItem = {} |
| 137 | #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name']) |
| 138 | ifaceItem["internal_name"] = element['local_iface_name'] |
| 139 | #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])] |
| 140 | ifaceItem["vm_id"] = vmDict[element['VNFC']] |
| 141 | ifaceItem["net_id"] = net_id |
| 142 | ifaceItem["type"] = net['type'] |
| 143 | if ifaceItem ["type"] == "data": |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 144 | dataiface = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ] |
| 145 | ifaceItem["vpci"] = dataiface['vpci'] |
| 146 | ifaceItem["bw"] = dataiface['bw'] |
| 147 | ifaceItem["model"] = dataiface['model'] |
| 148 | created_time_iface = dataiface['created_time'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 149 | else: |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 150 | bridgeiface = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ] |
| 151 | ifaceItem["vpci"] = bridgeiface['vpci'] |
| 152 | ifaceItem["mac"] = bridgeiface['mac'] |
| 153 | ifaceItem["bw"] = bridgeiface['bw'] |
| 154 | ifaceItem["model"] = bridgeiface['model'] |
| 155 | ifaceItem["port_security"] = bridgeiface['port_security'] |
| 156 | ifaceItem["floating_ip"] = bridgeiface['floating_ip'] |
| 157 | created_time_iface = bridgeiface['created_time'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 158 | internalconnList.append(ifaceItem) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 159 | #print "Internal net id in NFVO DB: %s" % net_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 160 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 161 | #print "Adding internal interfaces to the NFVO database (if any)" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 162 | for iface in internalconnList: |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 163 | #print "Iface name: %s" % iface['internal_name'] |
| 164 | iface_id = self._new_row_internal('interfaces', iface, add_uuid=True, root_uuid=vnf_id, created_time = created_time_iface) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 165 | #print "Iface id in NFVO DB: %s" % iface_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 166 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 167 | #print "Adding external interfaces to the NFVO database" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 168 | for iface in vnf_descriptor['vnf']['external-connections']: |
| 169 | myIfaceDict = {} |
| 170 | #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name']) |
| 171 | myIfaceDict["internal_name"] = iface['local_iface_name'] |
| 172 | #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])] |
| 173 | myIfaceDict["vm_id"] = vmDict[iface['VNFC']] |
| 174 | myIfaceDict["external_name"] = iface['name'] |
| 175 | myIfaceDict["type"] = iface['type'] |
| 176 | if iface["type"] == "data": |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 177 | dataiface = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ] |
| 178 | myIfaceDict["vpci"] = dataiface['vpci'] |
| 179 | myIfaceDict["bw"] = dataiface['bw'] |
| 180 | myIfaceDict["model"] = dataiface['model'] |
| 181 | created_time_iface = dataiface['created_time'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 182 | else: |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 183 | bridgeiface = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ] |
| 184 | myIfaceDict["vpci"] = bridgeiface['vpci'] |
| 185 | myIfaceDict["bw"] = bridgeiface['bw'] |
| 186 | myIfaceDict["model"] = bridgeiface['model'] |
| 187 | myIfaceDict["mac"] = bridgeiface['mac'] |
| 188 | myIfaceDict["port_security"]= bridgeiface['port_security'] |
| 189 | myIfaceDict["floating_ip"] = bridgeiface['floating_ip'] |
| 190 | created_time_iface = bridgeiface['created_time'] |
| 191 | #print "Iface name: %s" % iface['name'] |
| 192 | iface_id = self._new_row_internal('interfaces', myIfaceDict, add_uuid=True, root_uuid=vnf_id, created_time = created_time_iface) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 193 | #print "Iface id in NFVO DB: %s" % iface_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 194 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 195 | return vnf_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 196 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 197 | except (mdb.Error, AttributeError) as e: |
| 198 | self._format_error(e, tries) |
| 199 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 200 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 201 | def new_vnf_as_a_whole2(self,nfvo_tenant,vnf_name,vnf_descriptor,VNFCDict): |
| 202 | self.logger.debug("Adding new vnf to the NFVO database") |
| 203 | tries = 2 |
| 204 | while tries: |
| 205 | created_time = time.time() |
| 206 | try: |
| 207 | with self.con: |
| 208 | |
| 209 | myVNFDict = {} |
| 210 | myVNFDict["name"] = vnf_name |
| 211 | myVNFDict["descriptor"] = vnf_descriptor['vnf'].get('descriptor') |
| 212 | myVNFDict["public"] = vnf_descriptor['vnf'].get('public', "false") |
| 213 | myVNFDict["description"] = vnf_descriptor['vnf']['description'] |
| 214 | myVNFDict["class"] = vnf_descriptor['vnf'].get('class',"MISC") |
| 215 | myVNFDict["tenant_id"] = vnf_descriptor['vnf'].get("tenant_id") |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 216 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 217 | vnf_id = self._new_row_internal('vnfs', myVNFDict, add_uuid=True, root_uuid=None, created_time=created_time) |
| 218 | #print "Adding new vms to the NFVO database" |
| 219 | #For each vm, we must create the appropriate vm in the NFVO database. |
| 220 | vmDict = {} |
| Adam Israel | 8e3ce87 | 2018-01-08 18:43:40 +0000 | [diff] [blame] | 221 | for _,vm in VNFCDict.iteritems(): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 222 | #This code could make the name of the vms grow and grow. |
| 223 | #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name |
| 224 | #vm['name'] = "%s-%s" % (vnf_name,vm['name']) |
| 225 | #print "VM name: %s. Description: %s" % (vm['name'], vm['description']) |
| 226 | vm["vnf_id"] = vnf_id |
| 227 | created_time += 0.00001 |
| 228 | vm_id = self._new_row_internal('vms', vm, add_uuid=True, root_uuid=vnf_id, created_time=created_time) |
| 229 | #print "Internal vm id in NFVO DB: %s" % vm_id |
| 230 | vmDict[vm['name']] = vm_id |
| 231 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 232 | #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field |
| 233 | bridgeInterfacesDict = {} |
| 234 | for vm in vnf_descriptor['vnf']['VNFC']: |
| 235 | if 'bridge-ifaces' in vm: |
| 236 | bridgeInterfacesDict[vm['name']] = {} |
| 237 | for bridgeiface in vm['bridge-ifaces']: |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 238 | created_time += 0.00001 |
| tierno | 44528e4 | 2016-10-11 12:06:25 +0000 | [diff] [blame] | 239 | db_base._convert_bandwidth(bridgeiface, logger=self.logger) |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 240 | if 'port-security' in bridgeiface: |
| 241 | bridgeiface['port_security'] = bridgeiface.pop('port-security') |
| 242 | if 'floating-ip' in bridgeiface: |
| 243 | bridgeiface['floating_ip'] = bridgeiface.pop('floating-ip') |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 244 | ifaceDict = {} |
| 245 | ifaceDict['vpci'] = bridgeiface.get('vpci',None) |
| 246 | ifaceDict['mac'] = bridgeiface.get('mac_address',None) |
| 247 | ifaceDict['bw'] = bridgeiface.get('bandwidth', None) |
| 248 | ifaceDict['model'] = bridgeiface.get('model', None) |
| 249 | ifaceDict['port_security'] = int(bridgeiface.get('port_security', True)) |
| 250 | ifaceDict['floating_ip'] = int(bridgeiface.get('floating_ip', False)) |
| 251 | ifaceDict['created_time'] = created_time |
| 252 | bridgeInterfacesDict[vm['name']][bridgeiface['name']] = ifaceDict |
| 253 | |
| 254 | # Collect the data interfaces of each VM/VNFC under the 'numas' field |
| 255 | dataifacesDict = {} |
| 256 | for vm in vnf_descriptor['vnf']['VNFC']: |
| 257 | dataifacesDict[vm['name']] = {} |
| 258 | for numa in vm.get('numas', []): |
| 259 | for dataiface in numa.get('interfaces', []): |
| 260 | created_time += 0.00001 |
| 261 | db_base._convert_bandwidth(dataiface, logger=self.logger) |
| 262 | ifaceDict = {} |
| tierno | c3b6d37 | 2017-05-30 17:12:00 +0200 | [diff] [blame] | 263 | ifaceDict['vpci'] = dataiface.get('vpci') |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 264 | ifaceDict['bw'] = dataiface['bandwidth'] |
| 265 | ifaceDict['model'] = "PF" if dataiface['dedicated'] == "yes" else \ |
| 266 | ("VF" if dataiface['dedicated'] == "no" else "VFnotShared") |
| 267 | ifaceDict['created_time'] = created_time |
| 268 | dataifacesDict[vm['name']][dataiface['name']] = ifaceDict |
| 269 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 270 | #For each internal connection, we add it to the interfaceDict and we create the appropriate net in the NFVO database. |
| 271 | #print "Adding new nets (VNF internal nets) to the NFVO database (if any)" |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 272 | if 'internal-connections' in vnf_descriptor['vnf']: |
| 273 | for net in vnf_descriptor['vnf']['internal-connections']: |
| 274 | #print "Net name: %s. Description: %s" % (net['name'], net['description']) |
| 275 | |
| 276 | myNetDict = {} |
| 277 | myNetDict["name"] = net['name'] |
| 278 | myNetDict["description"] = net['description'] |
| 279 | if (net["implementation"] == "overlay"): |
| 280 | net["type"] = "bridge" |
| 281 | #It should give an error if the type is e-line. For the moment, we consider it as a bridge |
| 282 | elif (net["implementation"] == "underlay"): |
| 283 | if (net["type"] == "e-line"): |
| 284 | net["type"] = "ptp" |
| 285 | elif (net["type"] == "e-lan"): |
| 286 | net["type"] = "data" |
| 287 | net.pop("implementation") |
| 288 | myNetDict["type"] = net['type'] |
| 289 | myNetDict["vnf_id"] = vnf_id |
| 290 | |
| 291 | created_time += 0.00001 |
| 292 | net_id = self._new_row_internal('nets', myNetDict, add_uuid=True, root_uuid=vnf_id, created_time=created_time) |
| 293 | |
| 294 | if "ip-profile" in net: |
| 295 | ip_profile = net["ip-profile"] |
| 296 | myIPProfileDict = {} |
| 297 | myIPProfileDict["net_id"] = net_id |
| 298 | myIPProfileDict["ip_version"] = ip_profile.get('ip-version',"IPv4") |
| 299 | myIPProfileDict["subnet_address"] = ip_profile.get('subnet-address',None) |
| 300 | myIPProfileDict["gateway_address"] = ip_profile.get('gateway-address',None) |
| 301 | myIPProfileDict["dns_address"] = ip_profile.get('dns-address',None) |
| 302 | if ("dhcp" in ip_profile): |
| 303 | myIPProfileDict["dhcp_enabled"] = ip_profile["dhcp"].get('enabled',"true") |
| 304 | myIPProfileDict["dhcp_start_address"] = ip_profile["dhcp"].get('start-address',None) |
| 305 | myIPProfileDict["dhcp_count"] = ip_profile["dhcp"].get('count',None) |
| 306 | |
| 307 | created_time += 0.00001 |
| 308 | ip_profile_id = self._new_row_internal('ip_profiles', myIPProfileDict) |
| 309 | |
| 310 | for element in net['elements']: |
| 311 | ifaceItem = {} |
| 312 | #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name']) |
| 313 | ifaceItem["internal_name"] = element['local_iface_name'] |
| 314 | #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])] |
| 315 | ifaceItem["vm_id"] = vmDict[element['VNFC']] |
| 316 | ifaceItem["net_id"] = net_id |
| 317 | ifaceItem["type"] = net['type'] |
| 318 | ifaceItem["ip_address"] = element.get('ip_address',None) |
| 319 | if ifaceItem ["type"] == "data": |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 320 | ifaceDict = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ] |
| 321 | ifaceItem["vpci"] = ifaceDict['vpci'] |
| 322 | ifaceItem["bw"] = ifaceDict['bw'] |
| 323 | ifaceItem["model"] = ifaceDict['model'] |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 324 | else: |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 325 | ifaceDict = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ] |
| 326 | ifaceItem["vpci"] = ifaceDict['vpci'] |
| 327 | ifaceItem["mac"] = ifaceDict['mac'] |
| 328 | ifaceItem["bw"] = ifaceDict['bw'] |
| 329 | ifaceItem["model"] = ifaceDict['model'] |
| 330 | ifaceItem["port_security"] = ifaceDict['port_security'] |
| 331 | ifaceItem["floating_ip"] = ifaceDict['floating_ip'] |
| 332 | created_time_iface = ifaceDict["created_time"] |
| 333 | #print "Iface name: %s" % iface['internal_name'] |
| 334 | iface_id = self._new_row_internal('interfaces', ifaceItem, add_uuid=True, root_uuid=vnf_id, created_time=created_time_iface) |
| 335 | #print "Iface id in NFVO DB: %s" % iface_id |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 336 | |
| 337 | #print "Adding external interfaces to the NFVO database" |
| 338 | for iface in vnf_descriptor['vnf']['external-connections']: |
| 339 | myIfaceDict = {} |
| 340 | #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name']) |
| 341 | myIfaceDict["internal_name"] = iface['local_iface_name'] |
| 342 | #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])] |
| 343 | myIfaceDict["vm_id"] = vmDict[iface['VNFC']] |
| 344 | myIfaceDict["external_name"] = iface['name'] |
| 345 | myIfaceDict["type"] = iface['type'] |
| 346 | if iface["type"] == "data": |
| 347 | myIfaceDict["vpci"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci'] |
| 348 | myIfaceDict["bw"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw'] |
| 349 | myIfaceDict["model"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model'] |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 350 | created_time_iface = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['created_time'] |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 351 | else: |
| 352 | myIfaceDict["vpci"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci'] |
| 353 | myIfaceDict["bw"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw'] |
| 354 | myIfaceDict["model"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model'] |
| 355 | myIfaceDict["mac"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['mac'] |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 356 | myIfaceDict["port_security"] = \ |
| 357 | bridgeInterfacesDict[iface['VNFC']][iface['local_iface_name']]['port_security'] |
| 358 | myIfaceDict["floating_ip"] = \ |
| 359 | bridgeInterfacesDict[iface['VNFC']][iface['local_iface_name']]['floating_ip'] |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 360 | created_time_iface = bridgeInterfacesDict[iface['VNFC']][iface['local_iface_name']]['created_time'] |
| 361 | #print "Iface name: %s" % iface['name'] |
| 362 | iface_id = self._new_row_internal('interfaces', myIfaceDict, add_uuid=True, root_uuid=vnf_id, created_time=created_time_iface) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 363 | #print "Iface id in NFVO DB: %s" % iface_id |
| 364 | |
| 365 | return vnf_id |
| 366 | |
| 367 | except (mdb.Error, AttributeError) as e: |
| 368 | self._format_error(e, tries) |
| 369 | # except KeyError as e2: |
| 370 | # exc_type, exc_obj, exc_tb = sys.exc_info() |
| 371 | # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 372 | # self.logger.debug("Exception type: %s; Filename: %s; Line number: %s", exc_type, fname, exc_tb.tb_lineno) |
| 373 | # raise KeyError |
| 374 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 375 | |
| 376 | def new_scenario(self, scenario_dict): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 377 | tries = 2 |
| 378 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 379 | created_time = time.time() |
| 380 | try: |
| 381 | with self.con: |
| 382 | self.cur = self.con.cursor() |
| 383 | tenant_id = scenario_dict.get('tenant_id') |
| 384 | #scenario |
| 385 | INSERT_={'tenant_id': tenant_id, |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 386 | 'name': scenario_dict['name'], |
| 387 | 'description': scenario_dict['description'], |
| 388 | 'public': scenario_dict.get('public', "false")} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 389 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 390 | 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] | 391 | #sce_nets |
| Adam Israel | 8e3ce87 | 2018-01-08 18:43:40 +0000 | [diff] [blame] | 392 | for net in scenario_dict['nets'].values(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 393 | net_dict={'scenario_id': scenario_uuid} |
| 394 | net_dict["name"] = net["name"] |
| 395 | net_dict["type"] = net["type"] |
| 396 | net_dict["description"] = net.get("description") |
| 397 | net_dict["external"] = net.get("external", False) |
| 398 | if "graph" in net: |
| 399 | #net["graph"]=yaml.safe_dump(net["graph"],default_flow_style=True,width=256) |
| 400 | #TODO, must be json because of the GUI, change to yaml |
| 401 | net_dict["graph"]=json.dumps(net["graph"]) |
| 402 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 403 | 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] | 404 | net['uuid']=net_uuid |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 405 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 406 | if net.get("ip-profile"): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 407 | ip_profile = net["ip-profile"] |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 408 | myIPProfileDict = { |
| 409 | "sce_net_id": net_uuid, |
| 410 | "ip_version": ip_profile.get('ip-version', "IPv4"), |
| 411 | "subnet_address": ip_profile.get('subnet-address'), |
| 412 | "gateway_address": ip_profile.get('gateway-address'), |
| 413 | "dns_address": ip_profile.get('dns-address')} |
| 414 | if "dhcp" in ip_profile: |
| 415 | myIPProfileDict["dhcp_enabled"] = ip_profile["dhcp"].get('enabled', "true") |
| 416 | myIPProfileDict["dhcp_start_address"] = ip_profile["dhcp"].get('start-address') |
| 417 | myIPProfileDict["dhcp_count"] = ip_profile["dhcp"].get('count') |
| 418 | self._new_row_internal('ip_profiles', myIPProfileDict) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 419 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 420 | # sce_vnfs |
| Adam Israel | 8e3ce87 | 2018-01-08 18:43:40 +0000 | [diff] [blame] | 421 | for k, vnf in scenario_dict['vnfs'].items(): |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 422 | INSERT_ = {'scenario_id': scenario_uuid, |
| 423 | 'name': k, |
| 424 | 'vnf_id': vnf['uuid'], |
| 425 | # 'description': scenario_dict['name'] |
| 426 | 'description': vnf['description']} |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 427 | if "graph" in vnf: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 428 | #I NSERT_["graph"]=yaml.safe_dump(vnf["graph"],default_flow_style=True,width=256) |
| 429 | # TODO, must be json because of the GUI, change to yaml |
| 430 | INSERT_["graph"] = json.dumps(vnf["graph"]) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 431 | created_time += 0.00001 |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 432 | scn_vnf_uuid = self._new_row_internal('sce_vnfs', INSERT_, add_uuid=True, |
| 433 | root_uuid=scenario_uuid, created_time=created_time) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 434 | vnf['scn_vnf_uuid']=scn_vnf_uuid |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 435 | # sce_interfaces |
| Adam Israel | 8e3ce87 | 2018-01-08 18:43:40 +0000 | [diff] [blame] | 436 | for iface in vnf['ifaces'].values(): |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 437 | # print 'iface', iface |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 438 | if 'net_key' not in iface: |
| 439 | continue |
| 440 | iface['net_id'] = scenario_dict['nets'][ iface['net_key'] ]['uuid'] |
| 441 | INSERT_={'sce_vnf_id': scn_vnf_uuid, |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 442 | 'sce_net_id': iface['net_id'], |
| 443 | 'interface_id': iface['uuid'], |
| 444 | 'ip_address': iface.get('ip_address')} |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 445 | created_time += 0.00001 |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 446 | iface_uuid = self._new_row_internal('sce_interfaces', INSERT_, add_uuid=True, |
| 447 | root_uuid=scenario_uuid, created_time=created_time) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 448 | |
| 449 | return scenario_uuid |
| 450 | |
| 451 | except (mdb.Error, AttributeError) as e: |
| 452 | self._format_error(e, tries) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 453 | tries -= 1 |
| 454 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 455 | def edit_scenario(self, scenario_dict): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 456 | tries = 2 |
| 457 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 458 | modified_time = time.time() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 459 | item_changed=0 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 460 | try: |
| 461 | with self.con: |
| 462 | self.cur = self.con.cursor() |
| 463 | #check that scenario exist |
| 464 | tenant_id = scenario_dict.get('tenant_id') |
| 465 | scenario_uuid = scenario_dict['uuid'] |
| 466 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 467 | where_text = "uuid='{}'".format(scenario_uuid) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 468 | if not tenant_id and tenant_id != "any": |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 469 | where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id) |
| 470 | cmd = "SELECT * FROM scenarios WHERE "+ where_text |
| 471 | self.logger.debug(cmd) |
| 472 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 473 | self.cur.fetchall() |
| 474 | if self.cur.rowcount==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 475 | 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] | 476 | elif self.cur.rowcount>1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 477 | 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] | 478 | |
| 479 | #scenario |
| 480 | nodes = {} |
| 481 | topology = scenario_dict.pop("topology", None) |
| 482 | if topology != None and "nodes" in topology: |
| 483 | nodes = topology.get("nodes",{}) |
| 484 | UPDATE_ = {} |
| 485 | if "name" in scenario_dict: UPDATE_["name"] = scenario_dict["name"] |
| 486 | if "description" in scenario_dict: UPDATE_["description"] = scenario_dict["description"] |
| 487 | if len(UPDATE_)>0: |
| 488 | WHERE_={'tenant_id': tenant_id, 'uuid': scenario_uuid} |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 489 | item_changed += self._update_rows('scenarios', UPDATE_, WHERE_, modified_time=modified_time) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 490 | #sce_nets |
| Adam Israel | 8e3ce87 | 2018-01-08 18:43:40 +0000 | [diff] [blame] | 491 | for node_id, node in nodes.items(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 492 | if "graph" in node: |
| 493 | #node["graph"] = yaml.safe_dump(node["graph"],default_flow_style=True,width=256) |
| 494 | #TODO, must be json because of the GUI, change to yaml |
| 495 | node["graph"] = json.dumps(node["graph"]) |
| 496 | WHERE_={'scenario_id': scenario_uuid, 'uuid': node_id} |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 497 | #Try to change at sce_nets(version 0 API backward compatibility and sce_vnfs) |
| 498 | item_changed += self._update_rows('sce_nets', node, WHERE_) |
| 499 | item_changed += self._update_rows('sce_vnfs', node, WHERE_, modified_time=modified_time) |
| 500 | return item_changed |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 501 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 502 | except (mdb.Error, AttributeError) as e: |
| 503 | self._format_error(e, tries) |
| 504 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 505 | |
| 506 | # def get_instance_scenario(self, instance_scenario_id, tenant_id=None): |
| 507 | # '''Obtain the scenario instance information, filtering by one or serveral of the tenant, uuid or name |
| 508 | # instance_scenario_id is the uuid or the name if it is not a valid uuid format |
| 509 | # Only one scenario isntance must mutch the filtering or an error is returned |
| 510 | # ''' |
| 511 | # print "1******************************************************************" |
| 512 | # try: |
| 513 | # with self.con: |
| 514 | # self.cur = self.con.cursor(mdb.cursors.DictCursor) |
| 515 | # #scenario table |
| 516 | # where_list=[] |
| 517 | # if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 518 | # if db_base._check_valid_uuid(instance_scenario_id): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 519 | # where_list.append( "uuid='" + instance_scenario_id +"'" ) |
| 520 | # else: |
| 521 | # where_list.append( "name='" + instance_scenario_id +"'" ) |
| 522 | # where_text = " AND ".join(where_list) |
| 523 | # self.cur.execute("SELECT * FROM instance_scenarios WHERE "+ where_text) |
| 524 | # rows = self.cur.fetchall() |
| 525 | # if self.cur.rowcount==0: |
| 526 | # return -HTTP_Bad_Request, "No scenario instance found with this criteria " + where_text |
| 527 | # elif self.cur.rowcount>1: |
| 528 | # return -HTTP_Bad_Request, "More than one scenario instance found with this criteria " + where_text |
| 529 | # instance_scenario_dict = rows[0] |
| 530 | # |
| 531 | # #instance_vnfs |
| 532 | # self.cur.execute("SELECT uuid,vnf_id FROM instance_vnfs WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'") |
| 533 | # instance_scenario_dict['instance_vnfs'] = self.cur.fetchall() |
| 534 | # for vnf in instance_scenario_dict['instance_vnfs']: |
| 535 | # #instance_vms |
| 536 | # self.cur.execute("SELECT uuid, vim_vm_id "+ |
| 537 | # "FROM instance_vms "+ |
| 538 | # "WHERE instance_vnf_id='" + vnf['uuid'] +"'" |
| 539 | # ) |
| 540 | # vnf['instance_vms'] = self.cur.fetchall() |
| 541 | # #instance_nets |
| 542 | # self.cur.execute("SELECT uuid, vim_net_id FROM instance_nets WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'") |
| 543 | # instance_scenario_dict['instance_nets'] = self.cur.fetchall() |
| 544 | # |
| 545 | # #instance_interfaces |
| 546 | # 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'] + "'") |
| 547 | # instance_scenario_dict['instance_interfaces'] = self.cur.fetchall() |
| 548 | # |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 549 | # db_base._convert_datetime2str(instance_scenario_dict) |
| 550 | # db_base._convert_str2boolean(instance_scenario_dict, ('public','shared','external') ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 551 | # print "2******************************************************************" |
| 552 | # return 1, instance_scenario_dict |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 553 | # except (mdb.Error, AttributeError) as e: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 554 | # 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] | 555 | # return self._format_error(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 556 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 557 | def get_scenario(self, scenario_id, tenant_id=None, datacenter_vim_id=None, datacenter_id=None): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 558 | '''Obtain the scenario information, filtering by one or serveral of the tenant, uuid or name |
| 559 | scenario_id is the uuid or the name if it is not a valid uuid format |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 560 | if datacenter_vim_id,d datacenter_id is provided, it supply aditional vim_id fields with the matching vim uuid |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 561 | Only one scenario must mutch the filtering or an error is returned |
| 562 | ''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 563 | tries = 2 |
| 564 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 565 | try: |
| 566 | with self.con: |
| 567 | self.cur = self.con.cursor(mdb.cursors.DictCursor) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 568 | where_text = "uuid='{}'".format(scenario_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 569 | if not tenant_id and tenant_id != "any": |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 570 | where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id) |
| 571 | cmd = "SELECT * FROM scenarios WHERE " + where_text |
| 572 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 573 | self.cur.execute(cmd) |
| 574 | rows = self.cur.fetchall() |
| 575 | if self.cur.rowcount==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 576 | 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] | 577 | elif self.cur.rowcount>1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 578 | 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] | 579 | scenario_dict = rows[0] |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 580 | if scenario_dict["cloud_config"]: |
| 581 | scenario_dict["cloud-config"] = yaml.load(scenario_dict["cloud_config"]) |
| 582 | del scenario_dict["cloud_config"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 583 | #sce_vnfs |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 584 | cmd = "SELECT uuid,name,member_vnf_index,vnf_id,description FROM sce_vnfs WHERE scenario_id='{}' "\ |
| 585 | "ORDER BY created_at".format(scenario_dict['uuid']) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 586 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 587 | self.cur.execute(cmd) |
| 588 | scenario_dict['vnfs'] = self.cur.fetchall() |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 589 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 590 | for vnf in scenario_dict['vnfs']: |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 591 | cmd = "SELECT mgmt_access FROM vnfs WHERE uuid='{}'".format(scenario_dict['vnfs'][0]['vnf_id']) |
| 592 | self.logger.debug(cmd) |
| 593 | self.cur.execute(cmd) |
| 594 | mgmt_access_dict = self.cur.fetchall() |
| 595 | if mgmt_access_dict[0].get('mgmt_access'): |
| 596 | vnf['mgmt_access'] = yaml.load(mgmt_access_dict[0]['mgmt_access']) |
| 597 | else: |
| 598 | vnf['mgmt_access'] = None |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 599 | #sce_interfaces |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 600 | cmd = "SELECT scei.uuid,scei.sce_net_id,scei.interface_id,i.external_name,scei.ip_address"\ |
| 601 | " FROM sce_interfaces as scei join interfaces as i on scei.interface_id=i.uuid"\ |
| 602 | " WHERE scei.sce_vnf_id='{}' ORDER BY scei.created_at".format(vnf['uuid']) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 603 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 604 | self.cur.execute(cmd) |
| 605 | vnf['interfaces'] = self.cur.fetchall() |
| 606 | #vms |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 607 | cmd = "SELECT vms.uuid as uuid, flavor_id, image_id, vms.name as name," \ |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 608 | " vms.description as description, vms.boot_data as boot_data, count," \ |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 609 | " vms.availability_zone as availability_zone" \ |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 610 | " FROM vnfs join vms on vnfs.uuid=vms.vnf_id" \ |
| 611 | " WHERE vnfs.uuid='" + vnf['vnf_id'] + "'" \ |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 612 | " ORDER BY vms.created_at" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 613 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 614 | self.cur.execute(cmd) |
| 615 | vnf['vms'] = self.cur.fetchall() |
| 616 | for vm in vnf['vms']: |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 617 | if vm["boot_data"]: |
| 618 | vm["boot_data"] = yaml.safe_load(vm["boot_data"]) |
| 619 | else: |
| 620 | del vm["boot_data"] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 621 | if datacenter_vim_id!=None: |
| 622 | cmd = "SELECT vim_id FROM datacenters_images WHERE image_id='{}' AND datacenter_vim_id='{}'".format(vm['image_id'],datacenter_vim_id) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 623 | self.logger.debug(cmd) |
| 624 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 625 | if self.cur.rowcount==1: |
| 626 | vim_image_dict = self.cur.fetchone() |
| 627 | vm['vim_image_id']=vim_image_dict['vim_id'] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 628 | cmd = "SELECT vim_id FROM datacenters_flavors WHERE flavor_id='{}' AND datacenter_vim_id='{}'".format(vm['flavor_id'],datacenter_vim_id) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 629 | self.logger.debug(cmd) |
| 630 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 631 | if self.cur.rowcount==1: |
| 632 | vim_flavor_dict = self.cur.fetchone() |
| 633 | vm['vim_flavor_id']=vim_flavor_dict['vim_id'] |
| 634 | |
| 635 | #interfaces |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 636 | cmd = "SELECT uuid,internal_name,external_name,net_id,type,vpci,mac,bw,model,ip_address," \ |
| 637 | "floating_ip, port_security" \ |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 638 | " FROM interfaces" \ |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 639 | " WHERE vm_id='{}'" \ |
| 640 | " ORDER BY created_at".format(vm['uuid']) |
| 641 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 642 | self.cur.execute(cmd) |
| 643 | vm['interfaces'] = self.cur.fetchall() |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 644 | for index in range(0,len(vm['interfaces'])): |
| 645 | vm['interfaces'][index]['port-security'] = vm['interfaces'][index].pop("port_security") |
| 646 | vm['interfaces'][index]['floating-ip'] = vm['interfaces'][index].pop("floating_ip") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 647 | #nets every net of a vms |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 648 | cmd = "SELECT uuid,name,type,description FROM nets WHERE vnf_id='{}'".format(vnf['vnf_id']) |
| 649 | self.logger.debug(cmd) |
| 650 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 651 | vnf['nets'] = self.cur.fetchall() |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 652 | for vnf_net in vnf['nets']: |
| 653 | SELECT_ = "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count" |
| 654 | cmd = "SELECT {} FROM ip_profiles WHERE net_id='{}'".format(SELECT_,vnf_net['uuid']) |
| 655 | self.logger.debug(cmd) |
| 656 | self.cur.execute(cmd) |
| 657 | ipprofiles = self.cur.fetchall() |
| 658 | if self.cur.rowcount==1: |
| 659 | vnf_net["ip_profile"] = ipprofiles[0] |
| 660 | elif self.cur.rowcount>1: |
| 661 | raise db_base.db_base_Exception("More than one ip-profile found with this criteria: net_id='{}'".format(vnf_net['uuid']), db_base.HTTP_Bad_Request) |
| 662 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 663 | #sce_nets |
| 664 | cmd = "SELECT uuid,name,type,external,description" \ |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 665 | " FROM sce_nets WHERE scenario_id='{}'" \ |
| 666 | " ORDER BY created_at ".format(scenario_dict['uuid']) |
| 667 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 668 | self.cur.execute(cmd) |
| 669 | scenario_dict['nets'] = self.cur.fetchall() |
| 670 | #datacenter_nets |
| 671 | for net in scenario_dict['nets']: |
| 672 | if str(net['external']) == 'false': |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 673 | SELECT_ = "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count" |
| 674 | cmd = "SELECT {} FROM ip_profiles WHERE sce_net_id='{}'".format(SELECT_,net['uuid']) |
| 675 | self.logger.debug(cmd) |
| 676 | self.cur.execute(cmd) |
| 677 | ipprofiles = self.cur.fetchall() |
| 678 | if self.cur.rowcount==1: |
| 679 | net["ip_profile"] = ipprofiles[0] |
| 680 | elif self.cur.rowcount>1: |
| 681 | raise db_base.db_base_Exception("More than one ip-profile found with this criteria: sce_net_id='{}'".format(net['uuid']), db_base.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 682 | continue |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 683 | WHERE_=" WHERE name='{}'".format(net['name']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 684 | if datacenter_id!=None: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 685 | WHERE_ += " AND datacenter_id='{}'".format(datacenter_id) |
| 686 | cmd = "SELECT vim_net_id FROM datacenter_nets" + WHERE_ |
| 687 | self.logger.debug(cmd) |
| 688 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 689 | d_net = self.cur.fetchone() |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 690 | if d_net==None or datacenter_vim_id==None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 691 | #print "nfvo_db.get_scenario() WARNING external net %s not found" % net['name'] |
| 692 | net['vim_id']=None |
| 693 | else: |
| 694 | net['vim_id']=d_net['vim_net_id'] |
| 695 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 696 | db_base._convert_datetime2str(scenario_dict) |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 697 | db_base._convert_str2boolean(scenario_dict, ('public','shared','external','port-security','floating-ip') ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 698 | return scenario_dict |
| 699 | except (mdb.Error, AttributeError) as e: |
| 700 | self._format_error(e, tries) |
| 701 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 702 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 703 | def delete_scenario(self, scenario_id, tenant_id=None): |
| 704 | '''Deletes a scenario, filtering by one or several of the tenant, uuid or name |
| 705 | scenario_id is the uuid or the name if it is not a valid uuid format |
| 706 | Only one scenario must mutch the filtering or an error is returned |
| 707 | ''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 708 | tries = 2 |
| 709 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 710 | try: |
| 711 | with self.con: |
| 712 | self.cur = self.con.cursor(mdb.cursors.DictCursor) |
| 713 | |
| 714 | #scenario table |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 715 | where_text = "uuid='{}'".format(scenario_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 716 | if not tenant_id and tenant_id != "any": |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 717 | where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id) |
| 718 | cmd = "SELECT * FROM scenarios WHERE "+ where_text |
| 719 | self.logger.debug(cmd) |
| 720 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 721 | rows = self.cur.fetchall() |
| 722 | if self.cur.rowcount==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 723 | 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] | 724 | elif self.cur.rowcount>1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 725 | 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] | 726 | scenario_uuid = rows[0]["uuid"] |
| 727 | scenario_name = rows[0]["name"] |
| 728 | |
| 729 | #sce_vnfs |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 730 | cmd = "DELETE FROM scenarios WHERE uuid='{}'".format(scenario_uuid) |
| 731 | self.logger.debug(cmd) |
| 732 | self.cur.execute(cmd) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 733 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 734 | return scenario_uuid + " " + scenario_name |
| 735 | except (mdb.Error, AttributeError) as e: |
| 736 | self._format_error(e, tries, "delete", "instances running") |
| 737 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 738 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 739 | def new_rows(self, tables, uuid_list=None): |
| 740 | """ |
| 741 | Make a transactional insertion of rows at several tables |
| 742 | :param tables: list with dictionary where the keys are the table names and the values are a row or row list |
| 743 | with the values to be inserted at the table. Each row is a dictionary with the key values. E.g.: |
| 744 | tables = [ |
| 745 | {"table1": [ {"column1": value, "column2: value, ... }, {"column1": value, "column2: value, ... }, ...], |
| 746 | {"table2": [ {"column1": value, "column2: value, ... }, {"column1": value, "column2: value, ... }, ...], |
| 747 | {"table3": {"column1": value, "column2: value, ... } |
| 748 | } |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 749 | If tables does not contain the 'created_at', it is generated incrementally with the order of tables. You can |
| 750 | provide a integer value, that it is an index multiply by 0.00001 to add to the created time to manually set |
| 751 | up and order |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 752 | :param uuid_list: list of created uuids, first one is the root (#TODO to store at uuid table) |
| 753 | :return: None if success, raise exception otherwise |
| 754 | """ |
| 755 | tries = 2 |
| 756 | while tries: |
| 757 | created_time = time.time() |
| 758 | try: |
| 759 | with self.con: |
| 760 | self.cur = self.con.cursor() |
| 761 | for table in tables: |
| Adam Israel | 8e3ce87 | 2018-01-08 18:43:40 +0000 | [diff] [blame] | 762 | for table_name, row_list in table.items(): |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 763 | index = 0 |
| 764 | if isinstance(row_list, dict): |
| 765 | row_list = (row_list, ) #create a list with the single value |
| 766 | for row in row_list: |
| 767 | if table_name in self.tables_with_created_field: |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 768 | if "created_at" in row: |
| 769 | created_time_param = created_time + row.pop("created_at")*0.00001 |
| 770 | else: |
| 771 | created_time_param = created_time + index*0.00001 |
| 772 | index += 1 |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 773 | else: |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 774 | created_time_param = 0 |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 775 | self._new_row_internal(table_name, row, add_uuid=False, root_uuid=None, |
| 776 | created_time=created_time_param) |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 777 | return |
| 778 | except (mdb.Error, AttributeError) as e: |
| 779 | self._format_error(e, tries) |
| 780 | tries -= 1 |
| 781 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 782 | 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] | 783 | tries = 2 |
| 784 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 785 | created_time = time.time() |
| 786 | try: |
| 787 | with self.con: |
| 788 | self.cur = self.con.cursor() |
| 789 | #instance_scenarios |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 790 | datacenter_id = scenarioDict['datacenter_id'] |
| 791 | INSERT_={'tenant_id': tenant_id, |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 792 | 'datacenter_tenant_id': scenarioDict["datacenter2tenant"][datacenter_id], |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 793 | 'name': instance_scenario_name, |
| 794 | 'description': instance_scenario_description, |
| 795 | 'scenario_id' : scenarioDict['uuid'], |
| 796 | 'datacenter_id': datacenter_id |
| 797 | } |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 798 | if scenarioDict.get("cloud-config"): |
| 799 | INSERT_["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"], default_flow_style=True, width=256) |
| 800 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 801 | 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] | 802 | |
| 803 | net_scene2instance={} |
| 804 | #instance_nets #nets interVNF |
| 805 | for net in scenarioDict['nets']: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 806 | net_scene2instance[ net['uuid'] ] ={} |
| 807 | datacenter_site_id = net.get('datacenter_id', datacenter_id) |
| 808 | if not "vim_id_sites" in net: |
| 809 | net["vim_id_sites"] ={datacenter_site_id: net['vim_id']} |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 810 | net["vim_id_sites"]["datacenter_site_id"] = {datacenter_site_id: net['vim_id']} |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 811 | sce_net_id = net.get("uuid") |
| 812 | |
| Adam Israel | 8e3ce87 | 2018-01-08 18:43:40 +0000 | [diff] [blame] | 813 | for datacenter_site_id,vim_id in net["vim_id_sites"].iteritems(): |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 814 | INSERT_={'vim_net_id': vim_id, 'created': net.get('created', False), 'instance_scenario_id':instance_uuid } #, 'type': net['type'] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 815 | INSERT_['datacenter_id'] = datacenter_site_id |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 816 | INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_site_id] |
| tierno | 91baf98 | 2017-03-30 19:37:57 +0200 | [diff] [blame] | 817 | if not net.get('created', False): |
| 818 | INSERT_['status'] = "ACTIVE" |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 819 | if sce_net_id: |
| 820 | INSERT_['sce_net_id'] = sce_net_id |
| 821 | created_time += 0.00001 |
| 822 | instance_net_uuid = self._new_row_internal('instance_nets', INSERT_, True, instance_uuid, created_time) |
| 823 | net_scene2instance[ sce_net_id ][datacenter_site_id] = instance_net_uuid |
| 824 | net['uuid'] = instance_net_uuid #overwrite scnario uuid by instance uuid |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 825 | |
| 826 | if 'ip_profile' in net: |
| 827 | net['ip_profile']['net_id'] = None |
| 828 | net['ip_profile']['sce_net_id'] = None |
| 829 | net['ip_profile']['instance_net_id'] = instance_net_uuid |
| 830 | created_time += 0.00001 |
| 831 | ip_profile_id = self._new_row_internal('ip_profiles', net['ip_profile']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 832 | |
| 833 | #instance_vnfs |
| 834 | for vnf in scenarioDict['vnfs']: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 835 | datacenter_site_id = vnf.get('datacenter_id', datacenter_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 836 | INSERT_={'instance_scenario_id': instance_uuid, 'vnf_id': vnf['vnf_id'] } |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 837 | INSERT_['datacenter_id'] = datacenter_site_id |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 838 | INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_site_id] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 839 | if vnf.get("uuid"): |
| 840 | INSERT_['sce_vnf_id'] = vnf['uuid'] |
| 841 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 842 | 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] | 843 | vnf['uuid'] = instance_vnf_uuid #overwrite scnario uuid by instance uuid |
| 844 | |
| 845 | #instance_nets #nets intraVNF |
| 846 | for net in vnf['nets']: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 847 | net_scene2instance[ net['uuid'] ] = {} |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 848 | INSERT_={'vim_net_id': net['vim_id'], 'created': net.get('created', False), 'instance_scenario_id':instance_uuid } #, 'type': net['type'] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 849 | INSERT_['datacenter_id'] = net.get('datacenter_id', datacenter_site_id) |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 850 | INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_id] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 851 | if net.get("uuid"): |
| 852 | INSERT_['net_id'] = net['uuid'] |
| 853 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 854 | instance_net_uuid = self._new_row_internal('instance_nets', INSERT_, True, instance_uuid, created_time) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 855 | net_scene2instance[ net['uuid'] ][datacenter_site_id] = instance_net_uuid |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 856 | net['uuid'] = instance_net_uuid #overwrite scnario uuid by instance uuid |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 857 | |
| 858 | if 'ip_profile' in net: |
| 859 | net['ip_profile']['net_id'] = None |
| 860 | net['ip_profile']['sce_net_id'] = None |
| 861 | net['ip_profile']['instance_net_id'] = instance_net_uuid |
| 862 | created_time += 0.00001 |
| 863 | ip_profile_id = self._new_row_internal('ip_profiles', net['ip_profile']) |
| 864 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 865 | #instance_vms |
| 866 | for vm in vnf['vms']: |
| 867 | INSERT_={'instance_vnf_id': instance_vnf_uuid, 'vm_id': vm['uuid'], 'vim_vm_id': vm['vim_id'] } |
| 868 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 869 | 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] | 870 | vm['uuid'] = instance_vm_uuid #overwrite scnario uuid by instance uuid |
| 871 | |
| 872 | #instance_interfaces |
| 873 | for interface in vm['interfaces']: |
| 874 | net_id = interface.get('net_id', None) |
| 875 | if net_id is None: |
| 876 | #check if is connected to a inter VNFs net |
| 877 | for iface in vnf['interfaces']: |
| 878 | if iface['interface_id'] == interface['uuid']: |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 879 | if 'ip_address' in iface: |
| 880 | interface['ip_address'] = iface['ip_address'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 881 | net_id = iface.get('sce_net_id', None) |
| 882 | break |
| 883 | if net_id is None: |
| 884 | continue |
| 885 | interface_type='external' if interface['external_name'] is not None else 'internal' |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 886 | INSERT_={'instance_vm_id': instance_vm_uuid, 'instance_net_id': net_scene2instance[net_id][datacenter_site_id], |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 887 | 'interface_id': interface['uuid'], 'vim_interface_id': interface.get('vim_id'), 'type': interface_type, |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 888 | 'ip_address': interface.get('ip_address'), 'floating_ip': int(interface.get('floating-ip',False)), |
| 889 | 'port_security': int(interface.get('port-security',True))} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 890 | #created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 891 | 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] | 892 | interface['uuid'] = interface_uuid #overwrite scnario uuid by instance uuid |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 893 | return instance_uuid |
| 894 | except (mdb.Error, AttributeError) as e: |
| 895 | self._format_error(e, tries) |
| 896 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 897 | |
| 898 | def get_instance_scenario(self, instance_id, tenant_id=None, verbose=False): |
| 899 | '''Obtain the instance information, filtering by one or several of the tenant, uuid or name |
| 900 | instance_id is the uuid or the name if it is not a valid uuid format |
| 901 | Only one instance must mutch the filtering or an error is returned |
| 902 | ''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 903 | tries = 2 |
| 904 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 905 | try: |
| 906 | with self.con: |
| 907 | self.cur = self.con.cursor(mdb.cursors.DictCursor) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 908 | # instance table |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 909 | where_list = [] |
| 910 | if tenant_id: |
| 911 | where_list.append("inst.tenant_id='{}'".format(tenant_id)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 912 | if db_base._check_valid_uuid(instance_id): |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 913 | where_list.append("inst.uuid='{}'".format(instance_id)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 914 | else: |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 915 | where_list.append("inst.name='{}'".format(instance_id)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 916 | where_text = " AND ".join(where_list) |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 917 | cmd = "SELECT inst.uuid as uuid, inst.name as name, inst.scenario_id as scenario_id, datacenter_id"\ |
| 918 | " ,datacenter_tenant_id, s.name as scenario_name,inst.tenant_id as tenant_id" \ |
| 919 | " ,inst.description as description, inst.created_at as created_at" \ |
| 920 | " ,inst.cloud_config as cloud_config" \ |
| 921 | " FROM instance_scenarios as inst left join scenarios as s on inst.scenario_id=s.uuid" \ |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 922 | " WHERE " + where_text |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 923 | self.logger.debug(cmd) |
| 924 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 925 | rows = self.cur.fetchall() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 926 | |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 927 | if self.cur.rowcount == 0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 928 | raise db_base.db_base_Exception("No instance found where " + where_text, db_base.HTTP_Not_Found) |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 929 | elif self.cur.rowcount > 1: |
| 930 | raise db_base.db_base_Exception("More than one instance found where " + where_text, |
| 931 | db_base.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 932 | instance_dict = rows[0] |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 933 | if instance_dict["cloud_config"]: |
| 934 | instance_dict["cloud-config"] = yaml.load(instance_dict["cloud_config"]) |
| 935 | del instance_dict["cloud_config"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 936 | |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 937 | # instance_vnfs |
| 938 | cmd = "SELECT iv.uuid as uuid, iv.vnf_id as vnf_id, sv.name as vnf_name, sce_vnf_id, datacenter_id"\ |
| 939 | " ,datacenter_tenant_id, v.mgmt_access "\ |
| 940 | " FROM instance_vnfs as iv left join sce_vnfs as sv "\ |
| 941 | "on iv.sce_vnf_id=sv.uuid join vnfs as v on iv.vnf_id=v.uuid" \ |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 942 | " WHERE iv.instance_scenario_id='{}'" \ |
| 943 | " ORDER BY iv.created_at ".format(instance_dict['uuid']) |
| 944 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 945 | self.cur.execute(cmd) |
| 946 | instance_dict['vnfs'] = self.cur.fetchall() |
| 947 | for vnf in instance_dict['vnfs']: |
| 948 | vnf_manage_iface_list=[] |
| 949 | #instance vms |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 950 | cmd = "SELECT iv.uuid as uuid, vim_vm_id, status, error_msg, vim_info, iv.created_at as created_at, name"\ |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 951 | " FROM instance_vms as iv join vms on iv.vm_id=vms.uuid "\ |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 952 | " WHERE instance_vnf_id='{}' ORDER BY iv.created_at".format(vnf['uuid']) |
| 953 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 954 | self.cur.execute(cmd) |
| 955 | vnf['vms'] = self.cur.fetchall() |
| 956 | for vm in vnf['vms']: |
| 957 | vm_manage_iface_list=[] |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 958 | # instance_interfaces |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 959 | cmd = "SELECT vim_interface_id, instance_net_id, internal_name,external_name, mac_address,"\ |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 960 | " ii.ip_address as ip_address, vim_info, i.type as type, sdn_port_id"\ |
| tierno | fa51c20 | 2017-01-27 14:58:17 +0100 | [diff] [blame] | 961 | " FROM instance_interfaces as ii join interfaces as i on ii.interface_id=i.uuid"\ |
| 962 | " WHERE instance_vm_id='{}' ORDER BY created_at".format(vm['uuid']) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 963 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 964 | self.cur.execute(cmd ) |
| 965 | vm['interfaces'] = self.cur.fetchall() |
| 966 | for iface in vm['interfaces']: |
| 967 | if iface["type"] == "mgmt" and iface["ip_address"]: |
| 968 | vnf_manage_iface_list.append(iface["ip_address"]) |
| 969 | vm_manage_iface_list.append(iface["ip_address"]) |
| 970 | if not verbose: |
| 971 | del iface["type"] |
| 972 | if vm_manage_iface_list: vm["ip_address"] = ",".join(vm_manage_iface_list) |
| 973 | if vnf_manage_iface_list: vnf["ip_address"] = ",".join(vnf_manage_iface_list) |
| 974 | |
| 975 | #instance_nets |
| 976 | #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" |
| 977 | #from_text = "instance_nets join instance_scenarios on instance_nets.instance_scenario_id=instance_scenarios.uuid " + \ |
| 978 | # "join sce_nets on instance_scenarios.scenario_id=sce_nets.scenario_id" |
| 979 | #where_text = "instance_nets.instance_scenario_id='"+ instance_dict['uuid'] + "'" |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 980 | cmd = "SELECT uuid,vim_net_id,status,error_msg,vim_info,created, sce_net_id, net_id as vnf_net_id, datacenter_id, datacenter_tenant_id, sdn_net_id"\ |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 981 | " FROM instance_nets" \ |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 982 | " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict['uuid']) |
| 983 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 984 | self.cur.execute(cmd) |
| 985 | instance_dict['nets'] = self.cur.fetchall() |
| 986 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 987 | db_base._convert_datetime2str(instance_dict) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 988 | db_base._convert_str2boolean(instance_dict, ('public','shared','created') ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 989 | return instance_dict |
| 990 | except (mdb.Error, AttributeError) as e: |
| 991 | self._format_error(e, tries) |
| 992 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 993 | |
| 994 | def delete_instance_scenario(self, instance_id, tenant_id=None): |
| 995 | '''Deletes a instance_Scenario, filtering by one or serveral of the tenant, uuid or name |
| 996 | instance_id is the uuid or the name if it is not a valid uuid format |
| 997 | Only one instance_scenario must mutch the filtering or an error is returned |
| 998 | ''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 999 | tries = 2 |
| 1000 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1001 | try: |
| 1002 | with self.con: |
| 1003 | self.cur = self.con.cursor(mdb.cursors.DictCursor) |
| 1004 | |
| 1005 | #instance table |
| 1006 | where_list=[] |
| 1007 | if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1008 | if db_base._check_valid_uuid(instance_id): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1009 | where_list.append( "uuid='" + instance_id +"'" ) |
| 1010 | else: |
| 1011 | where_list.append( "name='" + instance_id +"'" ) |
| 1012 | where_text = " AND ".join(where_list) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1013 | cmd = "SELECT * FROM instance_scenarios WHERE "+ where_text |
| 1014 | self.logger.debug(cmd) |
| 1015 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1016 | rows = self.cur.fetchall() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1017 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1018 | if self.cur.rowcount==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1019 | 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] | 1020 | elif self.cur.rowcount>1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1021 | 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] | 1022 | instance_uuid = rows[0]["uuid"] |
| 1023 | instance_name = rows[0]["name"] |
| 1024 | |
| 1025 | #sce_vnfs |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1026 | cmd = "DELETE FROM instance_scenarios WHERE uuid='{}'".format(instance_uuid) |
| 1027 | self.logger.debug(cmd) |
| 1028 | self.cur.execute(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1029 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1030 | return instance_uuid + " " + instance_name |
| 1031 | except (mdb.Error, AttributeError) as e: |
| 1032 | self._format_error(e, tries, "delete", "No dependences can avoid deleting!!!!") |
| 1033 | tries -= 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1034 | |
| 1035 | def new_instance_scenario(self, instance_scenario_dict, tenant_id): |
| 1036 | #return self.new_row('vnfs', vnf_dict, None, tenant_id, True, True) |
| 1037 | return self._new_row_internal('instance_scenarios', instance_scenario_dict, tenant_id, add_uuid=True, root_uuid=None, log=True) |
| 1038 | |
| 1039 | def update_instance_scenario(self, instance_scenario_dict): |
| 1040 | #TODO: |
| 1041 | return |
| 1042 | |
| 1043 | def new_instance_vnf(self, instance_vnf_dict, tenant_id, instance_scenario_id = None): |
| 1044 | #return self.new_row('vms', vm_dict, tenant_id, True, True) |
| 1045 | return self._new_row_internal('instance_vnfs', instance_vnf_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True) |
| 1046 | |
| 1047 | def update_instance_vnf(self, instance_vnf_dict): |
| 1048 | #TODO: |
| 1049 | return |
| 1050 | |
| 1051 | def delete_instance_vnf(self, instance_vnf_id): |
| 1052 | #TODO: |
| 1053 | return |
| 1054 | |
| 1055 | def new_instance_vm(self, instance_vm_dict, tenant_id, instance_scenario_id = None): |
| 1056 | #return self.new_row('vms', vm_dict, tenant_id, True, True) |
| 1057 | return self._new_row_internal('instance_vms', instance_vm_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True) |
| 1058 | |
| 1059 | def update_instance_vm(self, instance_vm_dict): |
| 1060 | #TODO: |
| 1061 | return |
| 1062 | |
| 1063 | def delete_instance_vm(self, instance_vm_id): |
| 1064 | #TODO: |
| 1065 | return |
| 1066 | |
| 1067 | def new_instance_net(self, instance_net_dict, tenant_id, instance_scenario_id = None): |
| 1068 | return self._new_row_internal('instance_nets', instance_net_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True) |
| 1069 | |
| 1070 | def update_instance_net(self, instance_net_dict): |
| 1071 | #TODO: |
| 1072 | return |
| 1073 | |
| 1074 | def delete_instance_net(self, instance_net_id): |
| 1075 | #TODO: |
| 1076 | return |
| 1077 | |
| 1078 | def new_instance_interface(self, instance_interface_dict, tenant_id, instance_scenario_id = None): |
| 1079 | return self._new_row_internal('instance_interfaces', instance_interface_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True) |
| 1080 | |
| 1081 | def update_instance_interface(self, instance_interface_dict): |
| 1082 | #TODO: |
| 1083 | return |
| 1084 | |
| 1085 | def delete_instance_interface(self, instance_interface_dict): |
| 1086 | #TODO: |
| 1087 | return |
| 1088 | |
| 1089 | def update_datacenter_nets(self, datacenter_id, new_net_list=[]): |
| 1090 | ''' Removes the old and adds the new net list at datacenter list for one datacenter. |
| 1091 | Attribute |
| 1092 | datacenter_id: uuid of the datacenter to act upon |
| 1093 | table: table where to insert |
| 1094 | new_net_list: the new values to be inserted. If empty it only deletes the existing nets |
| 1095 | Return: (Inserted items, Deleted items) if OK, (-Error, text) if error |
| 1096 | ''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1097 | tries = 2 |
| 1098 | while tries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1099 | created_time = time.time() |
| 1100 | try: |
| 1101 | with self.con: |
| 1102 | self.cur = self.con.cursor() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1103 | cmd="DELETE FROM datacenter_nets WHERE datacenter_id='{}'".format(datacenter_id) |
| 1104 | self.logger.debug(cmd) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1105 | self.cur.execute(cmd) |
| 1106 | deleted = self.cur.rowcount |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1107 | inserted = 0 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1108 | for new_net in new_net_list: |
| 1109 | created_time += 0.00001 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1110 | self._new_row_internal('datacenter_nets', new_net, add_uuid=True, created_time=created_time) |
| 1111 | inserted += 1 |
| 1112 | return inserted, deleted |
| 1113 | except (mdb.Error, AttributeError) as e: |
| 1114 | self._format_error(e, tries) |
| 1115 | tries -= 1 |
| 1116 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1117 | |