blob: bad7cec2248fa86753571bc2a440f1f10c51ccf1 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- coding: utf-8 -*-
2
3##
tierno92021022018-09-12 16:29:23 +02004# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
tierno7edb6752016-03-21 17:37:52 +01005# 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 Sousa16cfd562018-11-30 15:33:35 +000024"""
tierno7edb6752016-03-21 17:37:52 +010025NFVO DB engine. It implements all the methods to interact with the Openmano Database
Eduardo Sousa16cfd562018-11-30 15:33:35 +000026"""
tierno7edb6752016-03-21 17:37:52 +010027__author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
28__date__ ="$28-aug-2014 10:05:01$"
29
tiernof97fd272016-07-11 14:32:37 +020030import db_base
tierno7edb6752016-03-21 17:37:52 +010031import MySQLdb as mdb
tierno7edb6752016-03-21 17:37:52 +010032import json
tiernoa4e1a6e2016-08-31 14:19:40 +020033import yaml
tierno7edb6752016-03-21 17:37:52 +010034import time
tierno4319dad2016-09-05 12:11:11 +020035#import sys, os
tierno7edb6752016-03-21 17:37:52 +010036
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010037from .http_tools import errors as httperrors
38
tiernof97fd272016-07-11 14:32:37 +020039tables_with_createdat_field=["datacenters","instance_nets","instance_scenarios","instance_vms","instance_vnfs",
tierno7edb6752016-03-21 17:37:52 +010040 "interfaces","nets","nfvo_tenants","scenarios","sce_interfaces","sce_nets",
tierno868220c2017-09-26 00:11:05 +020041 "sce_vnfs","tenants_datacenters","datacenter_tenants","vms","vnfs", "datacenter_nets",
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010042 "instance_actions", "sce_vnffgs", "sce_rsps", "sce_rsp_hops",
Igor D.Ccaadc442017-11-06 12:48:48 +000043 "sce_classifiers", "sce_classifier_matches", "instance_sfis", "instance_sfs",
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010044 "instance_classifications", "instance_sfps", "wims", "wim_accounts", "wim_nfvo_tenants",
45 "wim_port_mappings", "vim_wim_actions",
46 "instance_wim_nets"]
tierno7edb6752016-03-21 17:37:52 +010047
tierno8e690322017-08-10 15:58:50 +020048
tiernof97fd272016-07-11 14:32:37 +020049class nfvo_db(db_base.db_base):
tiernob13f3cc2016-09-26 10:14:44 +020050 def __init__(self, host=None, user=None, passwd=None, database=None, log_name='openmano.db', log_level=None):
tiernof97fd272016-07-11 14:32:37 +020051 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
tierno7edb6752016-03-21 17:37:52 +010053 return
54
tierno7edb6752016-03-21 17:37:52 +010055 def new_vnf_as_a_whole(self,nfvo_tenant,vnf_name,vnf_descriptor,VNFCDict):
tiernof97fd272016-07-11 14:32:37 +020056 self.logger.debug("Adding new vnf to the NFVO database")
57 tries = 2
58 while tries:
tierno7edb6752016-03-21 17:37:52 +010059 created_time = time.time()
60 try:
61 with self.con:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010062
tierno7edb6752016-03-21 17:37:52 +010063 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 Bravalheri0446cd52018-08-17 15:26:19 +010070
tiernof97fd272016-07-11 14:32:37 +020071 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"
tierno7edb6752016-03-21 17:37:52 +010073 #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 Bravalheri0446cd52018-08-17 15:26:19 +010077 #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name
tierno7edb6752016-03-21 17:37:52 +010078 #vm['name'] = "%s-%s" % (vnf_name,vm['name'])
tiernof97fd272016-07-11 14:32:37 +020079 #print "VM name: %s. Description: %s" % (vm['name'], vm['description'])
tierno7edb6752016-03-21 17:37:52 +010080 vm["vnf_id"] = vnf_id
81 created_time += 0.00001
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010082 vm_id = self._new_row_internal('vms', vm, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
tiernof97fd272016-07-11 14:32:37 +020083 #print "Internal vm id in NFVO DB: %s" % vm_id
tierno7edb6752016-03-21 17:37:52 +010084 vmDict[vm['name']] = vm_id
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010085
tierno7edb6752016-03-21 17:37:52 +010086 #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']:
tiernofa51c202017-01-27 14:58:17 +010092 created_time += 0.00001
montesmoreno2a1fc4e2017-01-09 16:46:04 +000093 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')
tierno44528e42016-10-11 12:06:25 +000097 db_base._convert_bandwidth(bridgeiface, logger=self.logger)
tierno7edb6752016-03-21 17:37:52 +010098 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)
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000103 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))
tiernofa51c202017-01-27 14:58:17 +0100107 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']] = {}
tiernoc3b6d372017-05-30 17:12:00 +0200118 dataifacesDict[vm['name']][dataiface['name']]['vpci'] = dataiface.get('vpci')
tiernofa51c202017-01-27 14:58:17 +0100119 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
tierno7edb6752016-03-21 17:37:52 +0100125 #For each internal connection, we add it to the interfaceDict and we create the appropriate net in the NFVO database.
tiernof97fd272016-07-11 14:32:37 +0200126 #print "Adding new nets (VNF internal nets) to the NFVO database (if any)"
tierno7edb6752016-03-21 17:37:52 +0100127 internalconnList = []
128 if 'internal-connections' in vnf_descriptor['vnf']:
129 for net in vnf_descriptor['vnf']['internal-connections']:
tiernof97fd272016-07-11 14:32:37 +0200130 #print "Net name: %s. Description: %s" % (net['name'], net['description'])
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100131
tierno7edb6752016-03-21 17:37:52 +0100132 myNetDict = {}
133 myNetDict["name"] = net['name']
134 myNetDict["description"] = net['description']
135 myNetDict["type"] = net['type']
136 myNetDict["vnf_id"] = vnf_id
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100137
tierno7edb6752016-03-21 17:37:52 +0100138 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200139 net_id = self._new_row_internal('nets', myNetDict, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100140
tierno7edb6752016-03-21 17:37:52 +0100141 for element in net['elements']:
142 ifaceItem = {}
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100143 #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])
tierno7edb6752016-03-21 17:37:52 +0100144 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":
tiernofa51c202017-01-27 14:58:17 +0100150 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']
tierno7edb6752016-03-21 17:37:52 +0100155 else:
tiernofa51c202017-01-27 14:58:17 +0100156 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']
tierno7edb6752016-03-21 17:37:52 +0100164 internalconnList.append(ifaceItem)
tiernof97fd272016-07-11 14:32:37 +0200165 #print "Internal net id in NFVO DB: %s" % net_id
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100166
tiernof97fd272016-07-11 14:32:37 +0200167 #print "Adding internal interfaces to the NFVO database (if any)"
tierno7edb6752016-03-21 17:37:52 +0100168 for iface in internalconnList:
tiernofa51c202017-01-27 14:58:17 +0100169 #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)
tiernof97fd272016-07-11 14:32:37 +0200171 #print "Iface id in NFVO DB: %s" % iface_id
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100172
tiernof97fd272016-07-11 14:32:37 +0200173 #print "Adding external interfaces to the NFVO database"
tierno7edb6752016-03-21 17:37:52 +0100174 for iface in vnf_descriptor['vnf']['external-connections']:
175 myIfaceDict = {}
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100176 #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])
tierno7edb6752016-03-21 17:37:52 +0100177 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":
tiernofa51c202017-01-27 14:58:17 +0100183 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']
tierno7edb6752016-03-21 17:37:52 +0100188 else:
tiernofa51c202017-01-27 14:58:17 +0100189 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)
tiernof97fd272016-07-11 14:32:37 +0200199 #print "Iface id in NFVO DB: %s" % iface_id
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100200
tiernof97fd272016-07-11 14:32:37 +0200201 return vnf_id
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100202
tiernof97fd272016-07-11 14:32:37 +0200203 except (mdb.Error, AttributeError) as e:
204 self._format_error(e, tries)
205 tries -= 1
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100206
garciadeblas9f8456e2016-09-05 05:02:59 +0200207 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 Bravalheri0446cd52018-08-17 15:26:19 +0100214
garciadeblas9f8456e2016-09-05 05:02:59 +0200215 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")
gcalvinoe580c7d2017-09-22 14:09:51 +0200222
garciadeblas9f8456e2016-09-05 05:02:59 +0200223 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 Bravalheri0446cd52018-08-17 15:26:19 +0100229 #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name
garciadeblas9f8456e2016-09-05 05:02:59 +0200230 #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 Bravalheri0446cd52018-08-17 15:26:19 +0100234 vm_id = self._new_row_internal('vms', vm, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
garciadeblas9f8456e2016-09-05 05:02:59 +0200235 #print "Internal vm id in NFVO DB: %s" % vm_id
236 vmDict[vm['name']] = vm_id
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100237
garciadeblas9f8456e2016-09-05 05:02:59 +0200238 #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']:
tiernofa51c202017-01-27 14:58:17 +0100244 created_time += 0.00001
tierno44528e42016-10-11 12:06:25 +0000245 db_base._convert_bandwidth(bridgeiface, logger=self.logger)
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000246 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')
tiernofa51c202017-01-27 14:58:17 +0100250 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 = {}
tiernoc3b6d372017-05-30 17:12:00 +0200269 ifaceDict['vpci'] = dataiface.get('vpci')
tiernofa51c202017-01-27 14:58:17 +0100270 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
garciadeblas9f8456e2016-09-05 05:02:59 +0200276 #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)"
garciadeblas9f8456e2016-09-05 05:02:59 +0200278 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 Bravalheri0446cd52018-08-17 15:26:19 +0100281
garciadeblas9f8456e2016-09-05 05:02:59 +0200282 myNetDict = {}
283 myNetDict["name"] = net['name']
284 myNetDict["description"] = net['description']
285 if (net["implementation"] == "overlay"):
286 net["type"] = "bridge"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100287 #It should give an error if the type is e-line. For the moment, we consider it as a bridge
garciadeblas9f8456e2016-09-05 05:02:59 +0200288 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 Bravalheri0446cd52018-08-17 15:26:19 +0100296
garciadeblas9f8456e2016-09-05 05:02:59 +0200297 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 Bravalheri0446cd52018-08-17 15:26:19 +0100299
garciadeblas9f8456e2016-09-05 05:02:59 +0200300 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 Bravalheri0446cd52018-08-17 15:26:19 +0100312
garciadeblas9f8456e2016-09-05 05:02:59 +0200313 created_time += 0.00001
314 ip_profile_id = self._new_row_internal('ip_profiles', myIPProfileDict)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100315
garciadeblas9f8456e2016-09-05 05:02:59 +0200316 for element in net['elements']:
317 ifaceItem = {}
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100318 #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])
garciadeblas9f8456e2016-09-05 05:02:59 +0200319 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":
tiernofa51c202017-01-27 14:58:17 +0100326 ifaceDict = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]
327 ifaceItem["vpci"] = ifaceDict['vpci']
328 ifaceItem["bw"] = ifaceDict['bw']
329 ifaceItem["model"] = ifaceDict['model']
garciadeblas9f8456e2016-09-05 05:02:59 +0200330 else:
tiernofa51c202017-01-27 14:58:17 +0100331 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 Bravalheri0446cd52018-08-17 15:26:19 +0100342
garciadeblas9f8456e2016-09-05 05:02:59 +0200343 #print "Adding external interfaces to the NFVO database"
344 for iface in vnf_descriptor['vnf']['external-connections']:
345 myIfaceDict = {}
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100346 #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])
garciadeblas9f8456e2016-09-05 05:02:59 +0200347 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']
tiernofa51c202017-01-27 14:58:17 +0100356 created_time_iface = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['created_time']
garciadeblas9f8456e2016-09-05 05:02:59 +0200357 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']
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000362 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']
tiernofa51c202017-01-27 14:58:17 +0100366 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)
garciadeblas9f8456e2016-09-05 05:02:59 +0200369 #print "Iface id in NFVO DB: %s" % iface_id
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100370
garciadeblas9f8456e2016-09-05 05:02:59 +0200371 return vnf_id
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100372
garciadeblas9f8456e2016-09-05 05:02:59 +0200373 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
tierno7edb6752016-03-21 17:37:52 +0100381
382 def new_scenario(self, scenario_dict):
tiernof97fd272016-07-11 14:32:37 +0200383 tries = 2
384 while tries:
tierno7edb6752016-03-21 17:37:52 +0100385 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,
tiernof97fd272016-07-11 14:32:37 +0200392 'name': scenario_dict['name'],
393 'description': scenario_dict['description'],
394 'public': scenario_dict.get('public', "false")}
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100395
tiernof97fd272016-07-11 14:32:37 +0200396 scenario_uuid = self._new_row_internal('scenarios', INSERT_, add_uuid=True, root_uuid=None, created_time=created_time)
tierno7edb6752016-03-21 17:37:52 +0100397 #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
tiernof97fd272016-07-11 14:32:37 +0200409 net_uuid = self._new_row_internal('sce_nets', net_dict, add_uuid=True, root_uuid=scenario_uuid, created_time=created_time)
tierno7edb6752016-03-21 17:37:52 +0100410 net['uuid']=net_uuid
tierno7edb6752016-03-21 17:37:52 +0100411
tierno5bb59dc2017-02-13 14:53:54 +0100412 if net.get("ip-profile"):
garciadeblas9f8456e2016-09-05 05:02:59 +0200413 ip_profile = net["ip-profile"]
tierno5bb59dc2017-02-13 14:53:54 +0100414 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)
garciadeblas9f8456e2016-09-05 05:02:59 +0200425
tierno5bb59dc2017-02-13 14:53:54 +0100426 # 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']}
garciadeblas9f8456e2016-09-05 05:02:59 +0200433 if "graph" in vnf:
tierno5bb59dc2017-02-13 14:53:54 +0100434 #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"])
garciadeblas9f8456e2016-09-05 05:02:59 +0200437 created_time += 0.00001
tierno5bb59dc2017-02-13 14:53:54 +0100438 scn_vnf_uuid = self._new_row_internal('sce_vnfs', INSERT_, add_uuid=True,
439 root_uuid=scenario_uuid, created_time=created_time)
garciadeblas9f8456e2016-09-05 05:02:59 +0200440 vnf['scn_vnf_uuid']=scn_vnf_uuid
tierno5bb59dc2017-02-13 14:53:54 +0100441 # sce_interfaces
garciadeblas9f8456e2016-09-05 05:02:59 +0200442 for iface in vnf['ifaces'].values():
tierno5bb59dc2017-02-13 14:53:54 +0100443 # print 'iface', iface
garciadeblas9f8456e2016-09-05 05:02:59 +0200444 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,
tierno5bb59dc2017-02-13 14:53:54 +0100448 'sce_net_id': iface['net_id'],
449 'interface_id': iface['uuid'],
450 'ip_address': iface.get('ip_address')}
garciadeblas9f8456e2016-09-05 05:02:59 +0200451 created_time += 0.00001
tierno5bb59dc2017-02-13 14:53:54 +0100452 iface_uuid = self._new_row_internal('sce_interfaces', INSERT_, add_uuid=True,
453 root_uuid=scenario_uuid, created_time=created_time)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100454
garciadeblas9f8456e2016-09-05 05:02:59 +0200455 return scenario_uuid
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100456
garciadeblas9f8456e2016-09-05 05:02:59 +0200457 except (mdb.Error, AttributeError) as e:
458 self._format_error(e, tries)
garciadeblas9f8456e2016-09-05 05:02:59 +0200459 tries -= 1
460
tierno7edb6752016-03-21 17:37:52 +0100461 def edit_scenario(self, scenario_dict):
tiernof97fd272016-07-11 14:32:37 +0200462 tries = 2
463 while tries:
tierno7edb6752016-03-21 17:37:52 +0100464 modified_time = time.time()
tiernof97fd272016-07-11 14:32:37 +0200465 item_changed=0
tierno7edb6752016-03-21 17:37:52 +0100466 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 Bravalheri0446cd52018-08-17 15:26:19 +0100472
tiernof97fd272016-07-11 14:32:37 +0200473 where_text = "uuid='{}'".format(scenario_uuid)
tierno7edb6752016-03-21 17:37:52 +0100474 if not tenant_id and tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200475 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)
tierno7edb6752016-03-21 17:37:52 +0100479 self.cur.fetchall()
480 if self.cur.rowcount==0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100481 raise db_base.db_base_Exception("No scenario found with this criteria " + where_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100482 elif self.cur.rowcount>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100483 raise db_base.db_base_Exception("More than one scenario found with this criteria " + where_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100484
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}
tiernof97fd272016-07-11 14:32:37 +0200495 item_changed += self._update_rows('scenarios', UPDATE_, WHERE_, modified_time=modified_time)
tierno7edb6752016-03-21 17:37:52 +0100496 #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}
tiernof97fd272016-07-11 14:32:37 +0200503 #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 Bravalheri0446cd52018-08-17 15:26:19 +0100507
tiernof97fd272016-07-11 14:32:37 +0200508 except (mdb.Error, AttributeError) as e:
509 self._format_error(e, tries)
510 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100511
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 Bravalheri0446cd52018-08-17 15:26:19 +0100516# '''
tierno7edb6752016-03-21 17:37:52 +0100517# 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 +"'" )
tiernof97fd272016-07-11 14:32:37 +0200524# if db_base._check_valid_uuid(instance_scenario_id):
tierno7edb6752016-03-21 17:37:52 +0100525# 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 Bravalheri0446cd52018-08-17 15:26:19 +0100532# return -httperrors.Bad_Request, "No scenario instance found with this criteria " + where_text
tierno7edb6752016-03-21 17:37:52 +0100533# elif self.cur.rowcount>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100534# return -httperrors.Bad_Request, "More than one scenario instance found with this criteria " + where_text
tierno7edb6752016-03-21 17:37:52 +0100535# instance_scenario_dict = rows[0]
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100536#
tierno7edb6752016-03-21 17:37:52 +0100537# #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 Bravalheri0446cd52018-08-17 15:26:19 +0100544# "WHERE instance_vnf_id='" + vnf['uuid'] +"'"
tierno7edb6752016-03-21 17:37:52 +0100545# )
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 Bravalheri0446cd52018-08-17 15:26:19 +0100550#
tierno7edb6752016-03-21 17:37:52 +0100551# #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 Bravalheri0446cd52018-08-17 15:26:19 +0100554#
tiernof97fd272016-07-11 14:32:37 +0200555# db_base._convert_datetime2str(instance_scenario_dict)
556# db_base._convert_str2boolean(instance_scenario_dict, ('public','shared','external') )
tierno7edb6752016-03-21 17:37:52 +0100557# print "2******************************************************************"
558# return 1, instance_scenario_dict
tiernof97fd272016-07-11 14:32:37 +0200559# except (mdb.Error, AttributeError) as e:
tierno7edb6752016-03-21 17:37:52 +0100560# print "nfvo_db.get_instance_scenario DB Exception %d: %s" % (e.args[0], e.args[1])
tiernof97fd272016-07-11 14:32:37 +0200561# return self._format_error(e)
tierno7edb6752016-03-21 17:37:52 +0100562
tierno868220c2017-09-26 00:11:05 +0200563 def get_scenario(self, scenario_id, tenant_id=None, datacenter_vim_id=None, datacenter_id=None):
tierno7edb6752016-03-21 17:37:52 +0100564 '''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
tierno868220c2017-09-26 00:11:05 +0200566 if datacenter_vim_id,d datacenter_id is provided, it supply aditional vim_id fields with the matching vim uuid
tierno7edb6752016-03-21 17:37:52 +0100567 Only one scenario must mutch the filtering or an error is returned
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100568 '''
tiernof97fd272016-07-11 14:32:37 +0200569 tries = 2
570 while tries:
tierno7edb6752016-03-21 17:37:52 +0100571 try:
572 with self.con:
573 self.cur = self.con.cursor(mdb.cursors.DictCursor)
tiernof97fd272016-07-11 14:32:37 +0200574 where_text = "uuid='{}'".format(scenario_id)
tierno7edb6752016-03-21 17:37:52 +0100575 if not tenant_id and tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200576 where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id)
577 cmd = "SELECT * FROM scenarios WHERE " + where_text
578 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100579 self.cur.execute(cmd)
580 rows = self.cur.fetchall()
581 if self.cur.rowcount==0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100582 raise db_base.db_base_Exception("No scenario found with this criteria " + where_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100583 elif self.cur.rowcount>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100584 raise db_base.db_base_Exception("More than one scenario found with this criteria " + where_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100585 scenario_dict = rows[0]
tiernoa4e1a6e2016-08-31 14:19:40 +0200586 if scenario_dict["cloud_config"]:
587 scenario_dict["cloud-config"] = yaml.load(scenario_dict["cloud_config"])
588 del scenario_dict["cloud_config"]
tierno8f79ea12018-05-03 17:37:40 +0200589 # sce_vnfs
tiernof1ba57e2017-09-07 12:23:19 +0200590 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'])
tiernof97fd272016-07-11 14:32:37 +0200592 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100593 self.cur.execute(cmd)
594 scenario_dict['vnfs'] = self.cur.fetchall()
gcalvinoe580c7d2017-09-22 14:09:51 +0200595
tierno7edb6752016-03-21 17:37:52 +0100596 for vnf in scenario_dict['vnfs']:
gcalvinoe580c7d2017-09-22 14:09:51 +0200597 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
tierno41a69812018-02-16 14:34:33 +0100605 # sce_interfaces
tierno8e690322017-08-10 15:58:50 +0200606 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'])
tiernof97fd272016-07-11 14:32:37 +0200609 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100610 self.cur.execute(cmd)
611 vnf['interfaces'] = self.cur.fetchall()
tierno41a69812018-02-16 14:34:33 +0100612 # vms
tierno16e3dd42018-04-24 12:52:40 +0200613 cmd = "SELECT vms.uuid as uuid, flavor_id, image_id, image_list, vms.name as name," \
tierno8e690322017-08-10 15:58:50 +0200614 " vms.description as description, vms.boot_data as boot_data, count," \
tiernob6990792018-11-13 10:37:42 +0100615 " vms.availability_zone as availability_zone, vms.osm_id as osm_id, vms.pdu_type" \
tierno8e690322017-08-10 15:58:50 +0200616 " FROM vnfs join vms on vnfs.uuid=vms.vnf_id" \
617 " WHERE vnfs.uuid='" + vnf['vnf_id'] + "'" \
mirabal29356312017-07-27 12:21:22 +0200618 " ORDER BY vms.created_at"
tiernof97fd272016-07-11 14:32:37 +0200619 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100620 self.cur.execute(cmd)
621 vnf['vms'] = self.cur.fetchall()
622 for vm in vnf['vms']:
tierno36c0b172017-01-12 18:32:28 +0100623 if vm["boot_data"]:
624 vm["boot_data"] = yaml.safe_load(vm["boot_data"])
625 else:
626 del vm["boot_data"]
tierno16e3dd42018-04-24 12:52:40 +0200627 if vm["image_list"]:
628 vm["image_list"] = yaml.safe_load(vm["image_list"])
629 else:
630 del vm["image_list"]
tierno868220c2017-09-26 00:11:05 +0200631 if datacenter_vim_id!=None:
tierno89aada42018-12-19 16:00:25 +0000632 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 Bravalheri0446cd52018-08-17 15:26:19 +0100648
tierno7edb6752016-03-21 17:37:52 +0100649 #interfaces
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000650 cmd = "SELECT uuid,internal_name,external_name,net_id,type,vpci,mac,bw,model,ip_address," \
651 "floating_ip, port_security" \
tierno7edb6752016-03-21 17:37:52 +0100652 " FROM interfaces" \
tiernof97fd272016-07-11 14:32:37 +0200653 " WHERE vm_id='{}'" \
654 " ORDER BY created_at".format(vm['uuid'])
655 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100656 self.cur.execute(cmd)
657 vm['interfaces'] = self.cur.fetchall()
tierno41a69812018-02-16 14:34:33 +0100658 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
tierno7edb6752016-03-21 17:37:52 +0100666 #nets every net of a vms
tierno1df468d2018-07-06 14:25:16 +0200667 cmd = "SELECT uuid,name,type,description, osm_id FROM nets WHERE vnf_id='{}'".format(vnf['vnf_id'])
tiernof97fd272016-07-11 14:32:37 +0200668 self.logger.debug(cmd)
669 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100670 vnf['nets'] = self.cur.fetchall()
garciadeblas9f8456e2016-09-05 05:02:59 +0200671 for vnf_net in vnf['nets']:
672 SELECT_ = "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100673 cmd = "SELECT {} FROM ip_profiles WHERE net_id='{}'".format(SELECT_,vnf_net['uuid'])
garciadeblas9f8456e2016-09-05 05:02:59 +0200674 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 Bravalheri0446cd52018-08-17 15:26:19 +0100680 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
tierno7edb6752016-03-21 17:37:52 +0100682 #sce_nets
tierno1df468d2018-07-06 14:25:16 +0200683 cmd = "SELECT uuid,name,type,external,description,vim_network_name, osm_id" \
tiernof97fd272016-07-11 14:32:37 +0200684 " FROM sce_nets WHERE scenario_id='{}'" \
685 " ORDER BY created_at ".format(scenario_dict['uuid'])
686 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100687 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':
garciadeblas9f8456e2016-09-05 05:02:59 +0200692 SELECT_ = "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100693 cmd = "SELECT {} FROM ip_profiles WHERE sce_net_id='{}'".format(SELECT_,net['uuid'])
garciadeblas9f8456e2016-09-05 05:02:59 +0200694 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 Bravalheri0446cd52018-08-17 15:26:19 +0100700 raise db_base.db_base_Exception("More than one ip-profile found with this criteria: sce_net_id='{}'".format(net['uuid']), httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100701 continue
tiernof97fd272016-07-11 14:32:37 +0200702 WHERE_=" WHERE name='{}'".format(net['name'])
tierno7edb6752016-03-21 17:37:52 +0100703 if datacenter_id!=None:
tiernof97fd272016-07-11 14:32:37 +0200704 WHERE_ += " AND datacenter_id='{}'".format(datacenter_id)
705 cmd = "SELECT vim_net_id FROM datacenter_nets" + WHERE_
706 self.logger.debug(cmd)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100707 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100708 d_net = self.cur.fetchone()
tierno868220c2017-09-26 00:11:05 +0200709 if d_net==None or datacenter_vim_id==None:
tierno7edb6752016-03-21 17:37:52 +0100710 #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 Bravalheri0446cd52018-08-17 15:26:19 +0100714
tiernof97fd272016-07-11 14:32:37 +0200715 db_base._convert_datetime2str(scenario_dict)
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000716 db_base._convert_str2boolean(scenario_dict, ('public','shared','external','port-security','floating-ip') )
Igor D.Ccaadc442017-11-06 12:48:48 +0000717
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 Sousa16cfd562018-11-30 15:33:35 +0000731 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.Ccaadc442017-11-06 12:48:48 +0000733 "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
tiernof97fd272016-07-11 14:32:37 +0200748 return scenario_dict
749 except (mdb.Error, AttributeError) as e:
750 self._format_error(e, tries)
751 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100752
tierno7edb6752016-03-21 17:37:52 +0100753 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 Bravalheri0446cd52018-08-17 15:26:19 +0100757 '''
tiernof97fd272016-07-11 14:32:37 +0200758 tries = 2
759 while tries:
tierno7edb6752016-03-21 17:37:52 +0100760 try:
761 with self.con:
762 self.cur = self.con.cursor(mdb.cursors.DictCursor)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100763
tierno7edb6752016-03-21 17:37:52 +0100764 #scenario table
tiernof97fd272016-07-11 14:32:37 +0200765 where_text = "uuid='{}'".format(scenario_id)
tierno7edb6752016-03-21 17:37:52 +0100766 if not tenant_id and tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200767 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)
tierno7edb6752016-03-21 17:37:52 +0100771 rows = self.cur.fetchall()
772 if self.cur.rowcount==0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100773 raise db_base.db_base_Exception("No scenario found where " + where_text, httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +0100774 elif self.cur.rowcount>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100775 raise db_base.db_base_Exception("More than one scenario found where " + where_text, httperrors.Conflict)
tierno7edb6752016-03-21 17:37:52 +0100776 scenario_uuid = rows[0]["uuid"]
777 scenario_name = rows[0]["name"]
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100778
tierno7edb6752016-03-21 17:37:52 +0100779 #sce_vnfs
tiernof97fd272016-07-11 14:32:37 +0200780 cmd = "DELETE FROM scenarios WHERE uuid='{}'".format(scenario_uuid)
781 self.logger.debug(cmd)
782 self.cur.execute(cmd)
gcalvinoe580c7d2017-09-22 14:09:51 +0200783
tiernof97fd272016-07-11 14:32:37 +0200784 return scenario_uuid + " " + scenario_name
785 except (mdb.Error, AttributeError) as e:
786 self._format_error(e, tries, "delete", "instances running")
787 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100788
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100789 def new_rows(self, tables, uuid_list=None, confidential_data=False):
tierno8e690322017-08-10 15:58:50 +0200790 """
tiernofc5f80b2018-05-29 16:00:43 +0200791 Make a transactional insertion of rows at several tables. Can be also a deletion
tierno8e690322017-08-10 15:58:50 +0200792 :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 }
tiernoa9550202017-09-22 13:31:35 +0200799 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
tiernofc5f80b2018-05-29 16:00:43 +0200802 If dict contains {"TO-DELETE": uuid} the entry is deleted if exist instead of inserted
tierno8e690322017-08-10 15:58:50 +0200803 :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 Bravalheri0446cd52018-08-17 15:26:19 +0100807 table_name = None
tierno8e690322017-08-10 15:58:50 +0200808 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:
tiernofc5f80b2018-05-29 16:00:43 +0200819 if "TO-DELETE" in row:
820 self._delete_row_by_id_internal(table_name, row["TO-DELETE"])
821 continue
822
tierno8e690322017-08-10 15:58:50 +0200823 if table_name in self.tables_with_created_field:
tiernoa9550202017-09-22 13:31:35 +0200824 if "created_at" in row:
tierno55d234c2018-07-04 18:29:21 +0200825 created_time_param = created_time + (index + row.pop("created_at"))*0.00001
tiernoa9550202017-09-22 13:31:35 +0200826 else:
827 created_time_param = created_time + index*0.00001
tierno55d234c2018-07-04 18:29:21 +0200828 index += 1
tierno8e690322017-08-10 15:58:50 +0200829 else:
tiernoa9550202017-09-22 13:31:35 +0200830 created_time_param = 0
tierno8e690322017-08-10 15:58:50 +0200831 self._new_row_internal(table_name, row, add_uuid=False, root_uuid=None,
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100832 confidential_data=confidential_data,
833 created_time=created_time_param)
tierno8e690322017-08-10 15:58:50 +0200834 return
835 except (mdb.Error, AttributeError) as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100836 self._format_error(e, tries, table=table_name)
tierno8e690322017-08-10 15:58:50 +0200837 tries -= 1
838
tierno7edb6752016-03-21 17:37:52 +0100839 def new_instance_scenario_as_a_whole(self,tenant_id,instance_scenario_name,instance_scenario_description,scenarioDict):
tiernof97fd272016-07-11 14:32:37 +0200840 tries = 2
841 while tries:
tierno7edb6752016-03-21 17:37:52 +0100842 created_time = time.time()
843 try:
844 with self.con:
845 self.cur = self.con.cursor()
846 #instance_scenarios
tierno7edb6752016-03-21 17:37:52 +0100847 datacenter_id = scenarioDict['datacenter_id']
848 INSERT_={'tenant_id': tenant_id,
tiernoa2793912016-10-04 08:15:08 +0000849 'datacenter_tenant_id': scenarioDict["datacenter2tenant"][datacenter_id],
tierno7edb6752016-03-21 17:37:52 +0100850 'name': instance_scenario_name,
851 'description': instance_scenario_description,
852 'scenario_id' : scenarioDict['uuid'],
853 'datacenter_id': datacenter_id
854 }
tiernoa4e1a6e2016-08-31 14:19:40 +0200855 if scenarioDict.get("cloud-config"):
856 INSERT_["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"], default_flow_style=True, width=256)
857
tiernof97fd272016-07-11 14:32:37 +0200858 instance_uuid = self._new_row_internal('instance_scenarios', INSERT_, add_uuid=True, root_uuid=None, created_time=created_time)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100859
tierno7edb6752016-03-21 17:37:52 +0100860 net_scene2instance={}
861 #instance_nets #nets interVNF
862 for net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +0200863 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']}
tiernoa2793912016-10-04 08:15:08 +0000867 net["vim_id_sites"]["datacenter_site_id"] = {datacenter_site_id: net['vim_id']}
tiernobe41e222016-09-02 15:16:13 +0200868 sce_net_id = net.get("uuid")
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100869
tiernobe41e222016-09-02 15:16:13 +0200870 for datacenter_site_id,vim_id in net["vim_id_sites"].iteritems():
tierno66345bc2016-09-26 11:37:55 +0200871 INSERT_={'vim_net_id': vim_id, 'created': net.get('created', False), 'instance_scenario_id':instance_uuid } #, 'type': net['type']
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100872 INSERT_['datacenter_id'] = datacenter_site_id
tiernoa2793912016-10-04 08:15:08 +0000873 INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_site_id]
tierno91baf982017-03-30 19:37:57 +0200874 if not net.get('created', False):
875 INSERT_['status'] = "ACTIVE"
tiernobe41e222016-09-02 15:16:13 +0200876 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 Bravalheri0446cd52018-08-17 15:26:19 +0100882
garciadeblas9f8456e2016-09-05 05:02:59 +0200883 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 Bravalheri0446cd52018-08-17 15:26:19 +0100889
tierno7edb6752016-03-21 17:37:52 +0100890 #instance_vnfs
891 for vnf in scenarioDict['vnfs']:
tiernobe41e222016-09-02 15:16:13 +0200892 datacenter_site_id = vnf.get('datacenter_id', datacenter_id)
tierno7edb6752016-03-21 17:37:52 +0100893 INSERT_={'instance_scenario_id': instance_uuid, 'vnf_id': vnf['vnf_id'] }
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100894 INSERT_['datacenter_id'] = datacenter_site_id
tiernoa2793912016-10-04 08:15:08 +0000895 INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_site_id]
tierno7edb6752016-03-21 17:37:52 +0100896 if vnf.get("uuid"):
897 INSERT_['sce_vnf_id'] = vnf['uuid']
898 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200899 instance_vnf_uuid = self._new_row_internal('instance_vnfs', INSERT_, True, instance_uuid, created_time)
tierno7edb6752016-03-21 17:37:52 +0100900 vnf['uuid'] = instance_vnf_uuid #overwrite scnario uuid by instance uuid
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100901
tierno7edb6752016-03-21 17:37:52 +0100902 #instance_nets #nets intraVNF
903 for net in vnf['nets']:
tiernobe41e222016-09-02 15:16:13 +0200904 net_scene2instance[ net['uuid'] ] = {}
tierno66345bc2016-09-26 11:37:55 +0200905 INSERT_={'vim_net_id': net['vim_id'], 'created': net.get('created', False), 'instance_scenario_id':instance_uuid } #, 'type': net['type']
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100906 INSERT_['datacenter_id'] = net.get('datacenter_id', datacenter_site_id)
tiernoa2793912016-10-04 08:15:08 +0000907 INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_id]
tierno7edb6752016-03-21 17:37:52 +0100908 if net.get("uuid"):
909 INSERT_['net_id'] = net['uuid']
910 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200911 instance_net_uuid = self._new_row_internal('instance_nets', INSERT_, True, instance_uuid, created_time)
tiernobe41e222016-09-02 15:16:13 +0200912 net_scene2instance[ net['uuid'] ][datacenter_site_id] = instance_net_uuid
tierno7edb6752016-03-21 17:37:52 +0100913 net['uuid'] = instance_net_uuid #overwrite scnario uuid by instance uuid
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100914
garciadeblas9f8456e2016-09-05 05:02:59 +0200915 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
tierno7edb6752016-03-21 17:37:52 +0100922 #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
tiernof97fd272016-07-11 14:32:37 +0200926 instance_vm_uuid = self._new_row_internal('instance_vms', INSERT_, True, instance_uuid, created_time)
tierno7edb6752016-03-21 17:37:52 +0100927 vm['uuid'] = instance_vm_uuid #overwrite scnario uuid by instance uuid
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100928
tierno7edb6752016-03-21 17:37:52 +0100929 #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']:
garciadeblas9f8456e2016-09-05 05:02:59 +0200936 if 'ip_address' in iface:
937 interface['ip_address'] = iface['ip_address']
tierno7edb6752016-03-21 17:37:52 +0100938 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'
tiernobe41e222016-09-02 15:16:13 +0200943 INSERT_={'instance_vm_id': instance_vm_uuid, 'instance_net_id': net_scene2instance[net_id][datacenter_site_id],
garciadeblas9f8456e2016-09-05 05:02:59 +0200944 'interface_id': interface['uuid'], 'vim_interface_id': interface.get('vim_id'), 'type': interface_type,
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000945 'ip_address': interface.get('ip_address'), 'floating_ip': int(interface.get('floating-ip',False)),
946 'port_security': int(interface.get('port-security',True))}
tierno7edb6752016-03-21 17:37:52 +0100947 #created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200948 interface_uuid = self._new_row_internal('instance_interfaces', INSERT_, True, instance_uuid) #, created_time)
tierno7edb6752016-03-21 17:37:52 +0100949 interface['uuid'] = interface_uuid #overwrite scnario uuid by instance uuid
tiernof97fd272016-07-11 14:32:37 +0200950 return instance_uuid
951 except (mdb.Error, AttributeError) as e:
952 self._format_error(e, tries)
953 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100954
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 Bravalheri0446cd52018-08-17 15:26:19 +0100959 '''
tiernof97fd272016-07-11 14:32:37 +0200960 tries = 2
961 while tries:
tierno7edb6752016-03-21 17:37:52 +0100962 try:
963 with self.con:
964 self.cur = self.con.cursor(mdb.cursors.DictCursor)
tierno868220c2017-09-26 00:11:05 +0200965 # instance table
tiernoa15c4b92017-10-05 12:41:44 +0200966 where_list = []
967 if tenant_id:
968 where_list.append("inst.tenant_id='{}'".format(tenant_id))
tiernof97fd272016-07-11 14:32:37 +0200969 if db_base._check_valid_uuid(instance_id):
tiernoa15c4b92017-10-05 12:41:44 +0200970 where_list.append("inst.uuid='{}'".format(instance_id))
tierno7edb6752016-03-21 17:37:52 +0100971 else:
tiernoa15c4b92017-10-05 12:41:44 +0200972 where_list.append("inst.name='{}'".format(instance_id))
tierno7edb6752016-03-21 17:37:52 +0100973 where_text = " AND ".join(where_list)
tiernoa15c4b92017-10-05 12:41:44 +0200974 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" \
tierno54467bb2018-04-11 23:21:02 +0200977 " ,inst.cloud_config as cloud_config, s.osm_id as nsd_osm_id" \
tiernoa15c4b92017-10-05 12:41:44 +0200978 " FROM instance_scenarios as inst left join scenarios as s on inst.scenario_id=s.uuid" \
tierno7edb6752016-03-21 17:37:52 +0100979 " WHERE " + where_text
tiernof97fd272016-07-11 14:32:37 +0200980 self.logger.debug(cmd)
981 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100982 rows = self.cur.fetchall()
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100983
tiernoa15c4b92017-10-05 12:41:44 +0200984 if self.cur.rowcount == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100985 raise db_base.db_base_Exception("No instance found where " + where_text, httperrors.Not_Found)
tiernoa15c4b92017-10-05 12:41:44 +0200986 elif self.cur.rowcount > 1:
987 raise db_base.db_base_Exception("More than one instance found where " + where_text,
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100988 httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100989 instance_dict = rows[0]
tiernoa4e1a6e2016-08-31 14:19:40 +0200990 if instance_dict["cloud_config"]:
991 instance_dict["cloud-config"] = yaml.load(instance_dict["cloud_config"])
992 del instance_dict["cloud_config"]
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100993
tiernoa15c4b92017-10-05 12:41:44 +0200994 # 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"\
tiernoe72710b2018-07-23 16:16:00 +0200996 ", 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'])
tiernof97fd272016-07-11 14:32:37 +02001001 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +01001002 self.cur.execute(cmd)
1003 instance_dict['vnfs'] = self.cur.fetchall()
1004 for vnf in instance_dict['vnfs']:
tierno67881db2018-10-24 18:46:03 +02001005 vnf["ip_address"] = None
tiernoe72710b2018-07-23 16:16:00 +02001006 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
tierno54467bb2018-04-11 23:21:02 +02001015 cmd = "SELECT iv.uuid as uuid, vim_vm_id, status, error_msg, vim_info, iv.created_at as "\
tiernoe72710b2018-07-23 16:16:00 +02001016 "created_at, name, vms.osm_id as vdu_osm_id, vim_name, vms.uuid as vm_uuid"\
tierno7edb6752016-03-21 17:37:52 +01001017 " FROM instance_vms as iv join vms on iv.vm_id=vms.uuid "\
tiernof97fd272016-07-11 14:32:37 +02001018 " WHERE instance_vnf_id='{}' ORDER BY iv.created_at".format(vnf['uuid'])
1019 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +01001020 self.cur.execute(cmd)
1021 vnf['vms'] = self.cur.fetchall()
1022 for vm in vnf['vms']:
1023 vm_manage_iface_list=[]
tiernoa15c4b92017-10-05 12:41:44 +02001024 # instance_interfaces
tiernofa51c202017-01-27 14:58:17 +01001025 cmd = "SELECT vim_interface_id, instance_net_id, internal_name,external_name, mac_address,"\
tiernoe72710b2018-07-23 16:16:00 +02001026 " ii.ip_address as ip_address, vim_info, i.type as type, sdn_port_id, i.uuid"\
tiernofa51c202017-01-27 14:58:17 +01001027 " 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'])
tiernof97fd272016-07-11 14:32:37 +02001029 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +01001030 self.cur.execute(cmd )
1031 vm['interfaces'] = self.cur.fetchall()
1032 for iface in vm['interfaces']:
tiernoe72710b2018-07-23 16:16:00 +02001033 if vnf_mgmt_access_iface and vnf_mgmt_access_iface == iface["uuid"]:
tierno67881db2018-10-24 18:46:03 +02001034 if not vnf["ip_address"]:
1035 vnf["ip_address"] = iface["ip_address"]
tierno7edb6752016-03-21 17:37:52 +01001036 if iface["type"] == "mgmt" and iface["ip_address"]:
tierno7edb6752016-03-21 17:37:52 +01001037 vm_manage_iface_list.append(iface["ip_address"])
1038 if not verbose:
1039 del iface["type"]
tiernoe72710b2018-07-23 16:16:00 +02001040 del iface["uuid"]
1041 if vm_manage_iface_list:
1042 vm["ip_address"] = ",".join(vm_manage_iface_list)
tierno67881db2018-10-24 18:46:03 +02001043 if not vnf["ip_address"] and vnf_mgmt_access_vm == vm["vm_uuid"]:
tiernoe72710b2018-07-23 16:16:00 +02001044 vnf["ip_address"] = vm["ip_address"]
1045 del vm["vm_uuid"]
1046
tierno7edb6752016-03-21 17:37:52 +01001047 #instance_nets
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001048 #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"
tierno7edb6752016-03-21 17:37:52 +01001049 #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'] + "'"
tiernob6990792018-11-13 10:37:42 +01001052 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'])
tiernof97fd272016-07-11 14:32:37 +02001058 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +01001059 self.cur.execute(cmd)
1060 instance_dict['nets'] = self.cur.fetchall()
Igor D.Ccaadc442017-11-06 12:48:48 +00001061
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
tierno69b590e2018-03-13 18:52:23 +01001071 # 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.Ccaadc442017-11-06 12:48:48 +00001080
tierno69b590e2018-03-13 18:52:23 +01001081 #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.Ccaadc442017-11-06 12:48:48 +00001090# 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
tiernof97fd272016-07-11 14:32:37 +02001102 db_base._convert_datetime2str(instance_dict)
tierno66345bc2016-09-26 11:37:55 +02001103 db_base._convert_str2boolean(instance_dict, ('public','shared','created') )
tiernof97fd272016-07-11 14:32:37 +02001104 return instance_dict
1105 except (mdb.Error, AttributeError) as e:
1106 self._format_error(e, tries)
1107 tries -= 1
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001108
tierno7edb6752016-03-21 17:37:52 +01001109 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 Bravalheri0446cd52018-08-17 15:26:19 +01001113 '''
tiernof97fd272016-07-11 14:32:37 +02001114 tries = 2
1115 while tries:
tierno7edb6752016-03-21 17:37:52 +01001116 try:
1117 with self.con:
1118 self.cur = self.con.cursor(mdb.cursors.DictCursor)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001119
tierno7edb6752016-03-21 17:37:52 +01001120 #instance table
1121 where_list=[]
1122 if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" )
tiernof97fd272016-07-11 14:32:37 +02001123 if db_base._check_valid_uuid(instance_id):
tierno7edb6752016-03-21 17:37:52 +01001124 where_list.append( "uuid='" + instance_id +"'" )
1125 else:
1126 where_list.append( "name='" + instance_id +"'" )
1127 where_text = " AND ".join(where_list)
tiernof97fd272016-07-11 14:32:37 +02001128 cmd = "SELECT * FROM instance_scenarios WHERE "+ where_text
1129 self.logger.debug(cmd)
1130 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +01001131 rows = self.cur.fetchall()
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001132
tierno7edb6752016-03-21 17:37:52 +01001133 if self.cur.rowcount==0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001134 raise db_base.db_base_Exception("No instance found where " + where_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001135 elif self.cur.rowcount>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001136 raise db_base.db_base_Exception("More than one instance found where " + where_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001137 instance_uuid = rows[0]["uuid"]
1138 instance_name = rows[0]["name"]
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001139
tierno7edb6752016-03-21 17:37:52 +01001140 #sce_vnfs
tiernof97fd272016-07-11 14:32:37 +02001141 cmd = "DELETE FROM instance_scenarios WHERE uuid='{}'".format(instance_uuid)
1142 self.logger.debug(cmd)
1143 self.cur.execute(cmd)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001144
tiernof97fd272016-07-11 14:32:37 +02001145 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 Bravalheri0446cd52018-08-17 15:26:19 +01001149
tierno7edb6752016-03-21 17:37:52 +01001150 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 Bravalheri0446cd52018-08-17 15:26:19 +01001165
tierno7edb6752016-03-21 17:37:52 +01001166 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 Bravalheri0446cd52018-08-17 15:26:19 +01001177
tierno7edb6752016-03-21 17:37:52 +01001178 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 Bravalheri0446cd52018-08-17 15:26:19 +01001184
tierno7edb6752016-03-21 17:37:52 +01001185 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 Bravalheri0446cd52018-08-17 15:26:19 +01001192
tierno7edb6752016-03-21 17:37:52 +01001193 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 Bravalheri0446cd52018-08-17 15:26:19 +01001206 Attribute
tierno7edb6752016-03-21 17:37:52 +01001207 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 '''
tiernof97fd272016-07-11 14:32:37 +02001212 tries = 2
1213 while tries:
tierno7edb6752016-03-21 17:37:52 +01001214 created_time = time.time()
1215 try:
1216 with self.con:
1217 self.cur = self.con.cursor()
tiernof97fd272016-07-11 14:32:37 +02001218 cmd="DELETE FROM datacenter_nets WHERE datacenter_id='{}'".format(datacenter_id)
1219 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +01001220 self.cur.execute(cmd)
1221 deleted = self.cur.rowcount
tiernof97fd272016-07-11 14:32:37 +02001222 inserted = 0
tierno7edb6752016-03-21 17:37:52 +01001223 for new_net in new_net_list:
1224 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +02001225 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 Bravalheri0446cd52018-08-17 15:26:19 +01001232