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