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