blob: f47ad162bc50183983ff4bd77a07e5d0b355ba69 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- coding: utf-8 -*-
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5# This file is part of openmano
6# All Rights Reserved.
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License. You may obtain
10# a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17# License for the specific language governing permissions and limitations
18# under the License.
19#
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact with: nfvlabs@tid.es
22##
23
24'''
25NFVO DB engine. It implements all the methods to interact with the Openmano Database
26'''
27__author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
28__date__ ="$28-aug-2014 10:05:01$"
29
Adam Israel8e3ce872018-01-08 18:43:40 +000030import 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
tiernof97fd272016-07-11 14:32:37 +020037tables_with_createdat_field=["datacenters","instance_nets","instance_scenarios","instance_vms","instance_vnfs",
tierno7edb6752016-03-21 17:37:52 +010038 "interfaces","nets","nfvo_tenants","scenarios","sce_interfaces","sce_nets",
tierno868220c2017-09-26 00:11:05 +020039 "sce_vnfs","tenants_datacenters","datacenter_tenants","vms","vnfs", "datacenter_nets",
40 "instance_actions", "vim_actions"]
tierno7edb6752016-03-21 17:37:52 +010041
tierno8e690322017-08-10 15:58:50 +020042
tiernof97fd272016-07-11 14:32:37 +020043class nfvo_db(db_base.db_base):
tiernob13f3cc2016-09-26 10:14:44 +020044 def __init__(self, host=None, user=None, passwd=None, database=None, log_name='openmano.db', log_level=None):
tiernof97fd272016-07-11 14:32:37 +020045 db_base.db_base.__init__(self, host, user, passwd, database, log_name, log_level)
46 db_base.db_base.tables_with_created_field=tables_with_createdat_field
tierno7edb6752016-03-21 17:37:52 +010047 return
48
tierno7edb6752016-03-21 17:37:52 +010049 def new_vnf_as_a_whole(self,nfvo_tenant,vnf_name,vnf_descriptor,VNFCDict):
tiernof97fd272016-07-11 14:32:37 +020050 self.logger.debug("Adding new vnf to the NFVO database")
51 tries = 2
52 while tries:
tierno7edb6752016-03-21 17:37:52 +010053 created_time = time.time()
54 try:
55 with self.con:
56
57 myVNFDict = {}
58 myVNFDict["name"] = vnf_name
59 myVNFDict["descriptor"] = vnf_descriptor['vnf'].get('descriptor')
60 myVNFDict["public"] = vnf_descriptor['vnf'].get('public', "false")
61 myVNFDict["description"] = vnf_descriptor['vnf']['description']
62 myVNFDict["class"] = vnf_descriptor['vnf'].get('class',"MISC")
63 myVNFDict["tenant_id"] = vnf_descriptor['vnf'].get("tenant_id")
64
tiernof97fd272016-07-11 14:32:37 +020065 vnf_id = self._new_row_internal('vnfs', myVNFDict, add_uuid=True, root_uuid=None, created_time=created_time)
66 #print "Adding new vms to the NFVO database"
tierno7edb6752016-03-21 17:37:52 +010067 #For each vm, we must create the appropriate vm in the NFVO database.
68 vmDict = {}
Adam Israel8e3ce872018-01-08 18:43:40 +000069 for _,vm in VNFCDict.iteritems():
tierno7edb6752016-03-21 17:37:52 +010070 #This code could make the name of the vms grow and grow.
71 #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name
72 #vm['name'] = "%s-%s" % (vnf_name,vm['name'])
tiernof97fd272016-07-11 14:32:37 +020073 #print "VM name: %s. Description: %s" % (vm['name'], vm['description'])
tierno7edb6752016-03-21 17:37:52 +010074 vm["vnf_id"] = vnf_id
75 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +020076 vm_id = self._new_row_internal('vms', vm, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
77 #print "Internal vm id in NFVO DB: %s" % vm_id
tierno7edb6752016-03-21 17:37:52 +010078 vmDict[vm['name']] = vm_id
79
tierno7edb6752016-03-21 17:37:52 +010080 #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field
81 bridgeInterfacesDict = {}
82 for vm in vnf_descriptor['vnf']['VNFC']:
83 if 'bridge-ifaces' in vm:
84 bridgeInterfacesDict[vm['name']] = {}
85 for bridgeiface in vm['bridge-ifaces']:
tiernofa51c202017-01-27 14:58:17 +010086 created_time += 0.00001
montesmoreno2a1fc4e2017-01-09 16:46:04 +000087 if 'port-security' in bridgeiface:
88 bridgeiface['port_security'] = bridgeiface.pop('port-security')
89 if 'floating-ip' in bridgeiface:
90 bridgeiface['floating_ip'] = bridgeiface.pop('floating-ip')
tierno44528e42016-10-11 12:06:25 +000091 db_base._convert_bandwidth(bridgeiface, logger=self.logger)
tierno7edb6752016-03-21 17:37:52 +010092 bridgeInterfacesDict[vm['name']][bridgeiface['name']] = {}
93 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['vpci'] = bridgeiface.get('vpci',None)
94 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['mac'] = bridgeiface.get('mac_address',None)
95 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['bw'] = bridgeiface.get('bandwidth', None)
96 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['model'] = bridgeiface.get('model', None)
montesmoreno2a1fc4e2017-01-09 16:46:04 +000097 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['port_security'] = \
98 int(bridgeiface.get('port_security', True))
99 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['floating_ip'] = \
100 int(bridgeiface.get('floating_ip', False))
tiernofa51c202017-01-27 14:58:17 +0100101 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['created_time'] = created_time
102
103 # Collect the data interfaces of each VM/VNFC under the 'numas' field
104 dataifacesDict = {}
105 for vm in vnf_descriptor['vnf']['VNFC']:
106 dataifacesDict[vm['name']] = {}
107 for numa in vm.get('numas', []):
108 for dataiface in numa.get('interfaces', []):
109 created_time += 0.00001
110 db_base._convert_bandwidth(dataiface, logger=self.logger)
111 dataifacesDict[vm['name']][dataiface['name']] = {}
tiernoc3b6d372017-05-30 17:12:00 +0200112 dataifacesDict[vm['name']][dataiface['name']]['vpci'] = dataiface.get('vpci')
tiernofa51c202017-01-27 14:58:17 +0100113 dataifacesDict[vm['name']][dataiface['name']]['bw'] = dataiface['bandwidth']
114 dataifacesDict[vm['name']][dataiface['name']]['model'] = "PF" if dataiface[
115 'dedicated'] == "yes" else (
116 "VF" if dataiface['dedicated'] == "no" else "VFnotShared")
117 dataifacesDict[vm['name']][dataiface['name']]['created_time'] = created_time
118
tierno7edb6752016-03-21 17:37:52 +0100119 #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 +0200120 #print "Adding new nets (VNF internal nets) to the NFVO database (if any)"
tierno7edb6752016-03-21 17:37:52 +0100121 internalconnList = []
122 if 'internal-connections' in vnf_descriptor['vnf']:
123 for net in vnf_descriptor['vnf']['internal-connections']:
tiernof97fd272016-07-11 14:32:37 +0200124 #print "Net name: %s. Description: %s" % (net['name'], net['description'])
tierno7edb6752016-03-21 17:37:52 +0100125
126 myNetDict = {}
127 myNetDict["name"] = net['name']
128 myNetDict["description"] = net['description']
129 myNetDict["type"] = net['type']
130 myNetDict["vnf_id"] = vnf_id
131
132 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200133 net_id = self._new_row_internal('nets', myNetDict, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
tierno7edb6752016-03-21 17:37:52 +0100134
135 for element in net['elements']:
136 ifaceItem = {}
137 #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])
138 ifaceItem["internal_name"] = element['local_iface_name']
139 #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])]
140 ifaceItem["vm_id"] = vmDict[element['VNFC']]
141 ifaceItem["net_id"] = net_id
142 ifaceItem["type"] = net['type']
143 if ifaceItem ["type"] == "data":
tiernofa51c202017-01-27 14:58:17 +0100144 dataiface = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]
145 ifaceItem["vpci"] = dataiface['vpci']
146 ifaceItem["bw"] = dataiface['bw']
147 ifaceItem["model"] = dataiface['model']
148 created_time_iface = dataiface['created_time']
tierno7edb6752016-03-21 17:37:52 +0100149 else:
tiernofa51c202017-01-27 14:58:17 +0100150 bridgeiface = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]
151 ifaceItem["vpci"] = bridgeiface['vpci']
152 ifaceItem["mac"] = bridgeiface['mac']
153 ifaceItem["bw"] = bridgeiface['bw']
154 ifaceItem["model"] = bridgeiface['model']
155 ifaceItem["port_security"] = bridgeiface['port_security']
156 ifaceItem["floating_ip"] = bridgeiface['floating_ip']
157 created_time_iface = bridgeiface['created_time']
tierno7edb6752016-03-21 17:37:52 +0100158 internalconnList.append(ifaceItem)
tiernof97fd272016-07-11 14:32:37 +0200159 #print "Internal net id in NFVO DB: %s" % net_id
tierno7edb6752016-03-21 17:37:52 +0100160
tiernof97fd272016-07-11 14:32:37 +0200161 #print "Adding internal interfaces to the NFVO database (if any)"
tierno7edb6752016-03-21 17:37:52 +0100162 for iface in internalconnList:
tiernofa51c202017-01-27 14:58:17 +0100163 #print "Iface name: %s" % iface['internal_name']
164 iface_id = self._new_row_internal('interfaces', iface, add_uuid=True, root_uuid=vnf_id, created_time = created_time_iface)
tiernof97fd272016-07-11 14:32:37 +0200165 #print "Iface id in NFVO DB: %s" % iface_id
tierno7edb6752016-03-21 17:37:52 +0100166
tiernof97fd272016-07-11 14:32:37 +0200167 #print "Adding external interfaces to the NFVO database"
tierno7edb6752016-03-21 17:37:52 +0100168 for iface in vnf_descriptor['vnf']['external-connections']:
169 myIfaceDict = {}
170 #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])
171 myIfaceDict["internal_name"] = iface['local_iface_name']
172 #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])]
173 myIfaceDict["vm_id"] = vmDict[iface['VNFC']]
174 myIfaceDict["external_name"] = iface['name']
175 myIfaceDict["type"] = iface['type']
176 if iface["type"] == "data":
tiernofa51c202017-01-27 14:58:17 +0100177 dataiface = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]
178 myIfaceDict["vpci"] = dataiface['vpci']
179 myIfaceDict["bw"] = dataiface['bw']
180 myIfaceDict["model"] = dataiface['model']
181 created_time_iface = dataiface['created_time']
tierno7edb6752016-03-21 17:37:52 +0100182 else:
tiernofa51c202017-01-27 14:58:17 +0100183 bridgeiface = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]
184 myIfaceDict["vpci"] = bridgeiface['vpci']
185 myIfaceDict["bw"] = bridgeiface['bw']
186 myIfaceDict["model"] = bridgeiface['model']
187 myIfaceDict["mac"] = bridgeiface['mac']
188 myIfaceDict["port_security"]= bridgeiface['port_security']
189 myIfaceDict["floating_ip"] = bridgeiface['floating_ip']
190 created_time_iface = bridgeiface['created_time']
191 #print "Iface name: %s" % iface['name']
192 iface_id = self._new_row_internal('interfaces', myIfaceDict, add_uuid=True, root_uuid=vnf_id, created_time = created_time_iface)
tiernof97fd272016-07-11 14:32:37 +0200193 #print "Iface id in NFVO DB: %s" % iface_id
tierno7edb6752016-03-21 17:37:52 +0100194
tiernof97fd272016-07-11 14:32:37 +0200195 return vnf_id
tierno7edb6752016-03-21 17:37:52 +0100196
tiernof97fd272016-07-11 14:32:37 +0200197 except (mdb.Error, AttributeError) as e:
198 self._format_error(e, tries)
199 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100200
garciadeblas9f8456e2016-09-05 05:02:59 +0200201 def new_vnf_as_a_whole2(self,nfvo_tenant,vnf_name,vnf_descriptor,VNFCDict):
202 self.logger.debug("Adding new vnf to the NFVO database")
203 tries = 2
204 while tries:
205 created_time = time.time()
206 try:
207 with self.con:
208
209 myVNFDict = {}
210 myVNFDict["name"] = vnf_name
211 myVNFDict["descriptor"] = vnf_descriptor['vnf'].get('descriptor')
212 myVNFDict["public"] = vnf_descriptor['vnf'].get('public', "false")
213 myVNFDict["description"] = vnf_descriptor['vnf']['description']
214 myVNFDict["class"] = vnf_descriptor['vnf'].get('class',"MISC")
215 myVNFDict["tenant_id"] = vnf_descriptor['vnf'].get("tenant_id")
gcalvinoe580c7d2017-09-22 14:09:51 +0200216
garciadeblas9f8456e2016-09-05 05:02:59 +0200217 vnf_id = self._new_row_internal('vnfs', myVNFDict, add_uuid=True, root_uuid=None, created_time=created_time)
218 #print "Adding new vms to the NFVO database"
219 #For each vm, we must create the appropriate vm in the NFVO database.
220 vmDict = {}
Adam Israel8e3ce872018-01-08 18:43:40 +0000221 for _,vm in VNFCDict.iteritems():
garciadeblas9f8456e2016-09-05 05:02:59 +0200222 #This code could make the name of the vms grow and grow.
223 #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name
224 #vm['name'] = "%s-%s" % (vnf_name,vm['name'])
225 #print "VM name: %s. Description: %s" % (vm['name'], vm['description'])
226 vm["vnf_id"] = vnf_id
227 created_time += 0.00001
228 vm_id = self._new_row_internal('vms', vm, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
229 #print "Internal vm id in NFVO DB: %s" % vm_id
230 vmDict[vm['name']] = vm_id
231
garciadeblas9f8456e2016-09-05 05:02:59 +0200232 #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field
233 bridgeInterfacesDict = {}
234 for vm in vnf_descriptor['vnf']['VNFC']:
235 if 'bridge-ifaces' in vm:
236 bridgeInterfacesDict[vm['name']] = {}
237 for bridgeiface in vm['bridge-ifaces']:
tiernofa51c202017-01-27 14:58:17 +0100238 created_time += 0.00001
tierno44528e42016-10-11 12:06:25 +0000239 db_base._convert_bandwidth(bridgeiface, logger=self.logger)
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000240 if 'port-security' in bridgeiface:
241 bridgeiface['port_security'] = bridgeiface.pop('port-security')
242 if 'floating-ip' in bridgeiface:
243 bridgeiface['floating_ip'] = bridgeiface.pop('floating-ip')
tiernofa51c202017-01-27 14:58:17 +0100244 ifaceDict = {}
245 ifaceDict['vpci'] = bridgeiface.get('vpci',None)
246 ifaceDict['mac'] = bridgeiface.get('mac_address',None)
247 ifaceDict['bw'] = bridgeiface.get('bandwidth', None)
248 ifaceDict['model'] = bridgeiface.get('model', None)
249 ifaceDict['port_security'] = int(bridgeiface.get('port_security', True))
250 ifaceDict['floating_ip'] = int(bridgeiface.get('floating_ip', False))
251 ifaceDict['created_time'] = created_time
252 bridgeInterfacesDict[vm['name']][bridgeiface['name']] = ifaceDict
253
254 # Collect the data interfaces of each VM/VNFC under the 'numas' field
255 dataifacesDict = {}
256 for vm in vnf_descriptor['vnf']['VNFC']:
257 dataifacesDict[vm['name']] = {}
258 for numa in vm.get('numas', []):
259 for dataiface in numa.get('interfaces', []):
260 created_time += 0.00001
261 db_base._convert_bandwidth(dataiface, logger=self.logger)
262 ifaceDict = {}
tiernoc3b6d372017-05-30 17:12:00 +0200263 ifaceDict['vpci'] = dataiface.get('vpci')
tiernofa51c202017-01-27 14:58:17 +0100264 ifaceDict['bw'] = dataiface['bandwidth']
265 ifaceDict['model'] = "PF" if dataiface['dedicated'] == "yes" else \
266 ("VF" if dataiface['dedicated'] == "no" else "VFnotShared")
267 ifaceDict['created_time'] = created_time
268 dataifacesDict[vm['name']][dataiface['name']] = ifaceDict
269
garciadeblas9f8456e2016-09-05 05:02:59 +0200270 #For each internal connection, we add it to the interfaceDict and we create the appropriate net in the NFVO database.
271 #print "Adding new nets (VNF internal nets) to the NFVO database (if any)"
garciadeblas9f8456e2016-09-05 05:02:59 +0200272 if 'internal-connections' in vnf_descriptor['vnf']:
273 for net in vnf_descriptor['vnf']['internal-connections']:
274 #print "Net name: %s. Description: %s" % (net['name'], net['description'])
275
276 myNetDict = {}
277 myNetDict["name"] = net['name']
278 myNetDict["description"] = net['description']
279 if (net["implementation"] == "overlay"):
280 net["type"] = "bridge"
281 #It should give an error if the type is e-line. For the moment, we consider it as a bridge
282 elif (net["implementation"] == "underlay"):
283 if (net["type"] == "e-line"):
284 net["type"] = "ptp"
285 elif (net["type"] == "e-lan"):
286 net["type"] = "data"
287 net.pop("implementation")
288 myNetDict["type"] = net['type']
289 myNetDict["vnf_id"] = vnf_id
290
291 created_time += 0.00001
292 net_id = self._new_row_internal('nets', myNetDict, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
293
294 if "ip-profile" in net:
295 ip_profile = net["ip-profile"]
296 myIPProfileDict = {}
297 myIPProfileDict["net_id"] = net_id
298 myIPProfileDict["ip_version"] = ip_profile.get('ip-version',"IPv4")
299 myIPProfileDict["subnet_address"] = ip_profile.get('subnet-address',None)
300 myIPProfileDict["gateway_address"] = ip_profile.get('gateway-address',None)
301 myIPProfileDict["dns_address"] = ip_profile.get('dns-address',None)
302 if ("dhcp" in ip_profile):
303 myIPProfileDict["dhcp_enabled"] = ip_profile["dhcp"].get('enabled',"true")
304 myIPProfileDict["dhcp_start_address"] = ip_profile["dhcp"].get('start-address',None)
305 myIPProfileDict["dhcp_count"] = ip_profile["dhcp"].get('count',None)
306
307 created_time += 0.00001
308 ip_profile_id = self._new_row_internal('ip_profiles', myIPProfileDict)
309
310 for element in net['elements']:
311 ifaceItem = {}
312 #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])
313 ifaceItem["internal_name"] = element['local_iface_name']
314 #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])]
315 ifaceItem["vm_id"] = vmDict[element['VNFC']]
316 ifaceItem["net_id"] = net_id
317 ifaceItem["type"] = net['type']
318 ifaceItem["ip_address"] = element.get('ip_address',None)
319 if ifaceItem ["type"] == "data":
tiernofa51c202017-01-27 14:58:17 +0100320 ifaceDict = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]
321 ifaceItem["vpci"] = ifaceDict['vpci']
322 ifaceItem["bw"] = ifaceDict['bw']
323 ifaceItem["model"] = ifaceDict['model']
garciadeblas9f8456e2016-09-05 05:02:59 +0200324 else:
tiernofa51c202017-01-27 14:58:17 +0100325 ifaceDict = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]
326 ifaceItem["vpci"] = ifaceDict['vpci']
327 ifaceItem["mac"] = ifaceDict['mac']
328 ifaceItem["bw"] = ifaceDict['bw']
329 ifaceItem["model"] = ifaceDict['model']
330 ifaceItem["port_security"] = ifaceDict['port_security']
331 ifaceItem["floating_ip"] = ifaceDict['floating_ip']
332 created_time_iface = ifaceDict["created_time"]
333 #print "Iface name: %s" % iface['internal_name']
334 iface_id = self._new_row_internal('interfaces', ifaceItem, add_uuid=True, root_uuid=vnf_id, created_time=created_time_iface)
335 #print "Iface id in NFVO DB: %s" % iface_id
garciadeblas9f8456e2016-09-05 05:02:59 +0200336
337 #print "Adding external interfaces to the NFVO database"
338 for iface in vnf_descriptor['vnf']['external-connections']:
339 myIfaceDict = {}
340 #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])
341 myIfaceDict["internal_name"] = iface['local_iface_name']
342 #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])]
343 myIfaceDict["vm_id"] = vmDict[iface['VNFC']]
344 myIfaceDict["external_name"] = iface['name']
345 myIfaceDict["type"] = iface['type']
346 if iface["type"] == "data":
347 myIfaceDict["vpci"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci']
348 myIfaceDict["bw"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw']
349 myIfaceDict["model"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model']
tiernofa51c202017-01-27 14:58:17 +0100350 created_time_iface = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['created_time']
garciadeblas9f8456e2016-09-05 05:02:59 +0200351 else:
352 myIfaceDict["vpci"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci']
353 myIfaceDict["bw"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw']
354 myIfaceDict["model"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model']
355 myIfaceDict["mac"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000356 myIfaceDict["port_security"] = \
357 bridgeInterfacesDict[iface['VNFC']][iface['local_iface_name']]['port_security']
358 myIfaceDict["floating_ip"] = \
359 bridgeInterfacesDict[iface['VNFC']][iface['local_iface_name']]['floating_ip']
tiernofa51c202017-01-27 14:58:17 +0100360 created_time_iface = bridgeInterfacesDict[iface['VNFC']][iface['local_iface_name']]['created_time']
361 #print "Iface name: %s" % iface['name']
362 iface_id = self._new_row_internal('interfaces', myIfaceDict, add_uuid=True, root_uuid=vnf_id, created_time=created_time_iface)
garciadeblas9f8456e2016-09-05 05:02:59 +0200363 #print "Iface id in NFVO DB: %s" % iface_id
364
365 return vnf_id
366
367 except (mdb.Error, AttributeError) as e:
368 self._format_error(e, tries)
369# except KeyError as e2:
370# exc_type, exc_obj, exc_tb = sys.exc_info()
371# fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
372# self.logger.debug("Exception type: %s; Filename: %s; Line number: %s", exc_type, fname, exc_tb.tb_lineno)
373# raise KeyError
374 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100375
376 def new_scenario(self, scenario_dict):
tiernof97fd272016-07-11 14:32:37 +0200377 tries = 2
378 while tries:
tierno7edb6752016-03-21 17:37:52 +0100379 created_time = time.time()
380 try:
381 with self.con:
382 self.cur = self.con.cursor()
383 tenant_id = scenario_dict.get('tenant_id')
384 #scenario
385 INSERT_={'tenant_id': tenant_id,
tiernof97fd272016-07-11 14:32:37 +0200386 'name': scenario_dict['name'],
387 'description': scenario_dict['description'],
388 'public': scenario_dict.get('public', "false")}
tierno7edb6752016-03-21 17:37:52 +0100389
tiernof97fd272016-07-11 14:32:37 +0200390 scenario_uuid = self._new_row_internal('scenarios', INSERT_, add_uuid=True, root_uuid=None, created_time=created_time)
tierno7edb6752016-03-21 17:37:52 +0100391 #sce_nets
Adam Israel8e3ce872018-01-08 18:43:40 +0000392 for net in scenario_dict['nets'].values():
tierno7edb6752016-03-21 17:37:52 +0100393 net_dict={'scenario_id': scenario_uuid}
394 net_dict["name"] = net["name"]
395 net_dict["type"] = net["type"]
396 net_dict["description"] = net.get("description")
397 net_dict["external"] = net.get("external", False)
398 if "graph" in net:
399 #net["graph"]=yaml.safe_dump(net["graph"],default_flow_style=True,width=256)
400 #TODO, must be json because of the GUI, change to yaml
401 net_dict["graph"]=json.dumps(net["graph"])
402 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200403 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 +0100404 net['uuid']=net_uuid
tierno7edb6752016-03-21 17:37:52 +0100405
tierno5bb59dc2017-02-13 14:53:54 +0100406 if net.get("ip-profile"):
garciadeblas9f8456e2016-09-05 05:02:59 +0200407 ip_profile = net["ip-profile"]
tierno5bb59dc2017-02-13 14:53:54 +0100408 myIPProfileDict = {
409 "sce_net_id": net_uuid,
410 "ip_version": ip_profile.get('ip-version', "IPv4"),
411 "subnet_address": ip_profile.get('subnet-address'),
412 "gateway_address": ip_profile.get('gateway-address'),
413 "dns_address": ip_profile.get('dns-address')}
414 if "dhcp" in ip_profile:
415 myIPProfileDict["dhcp_enabled"] = ip_profile["dhcp"].get('enabled', "true")
416 myIPProfileDict["dhcp_start_address"] = ip_profile["dhcp"].get('start-address')
417 myIPProfileDict["dhcp_count"] = ip_profile["dhcp"].get('count')
418 self._new_row_internal('ip_profiles', myIPProfileDict)
garciadeblas9f8456e2016-09-05 05:02:59 +0200419
tierno5bb59dc2017-02-13 14:53:54 +0100420 # sce_vnfs
Adam Israel8e3ce872018-01-08 18:43:40 +0000421 for k, vnf in scenario_dict['vnfs'].items():
tierno5bb59dc2017-02-13 14:53:54 +0100422 INSERT_ = {'scenario_id': scenario_uuid,
423 'name': k,
424 'vnf_id': vnf['uuid'],
425 # 'description': scenario_dict['name']
426 'description': vnf['description']}
garciadeblas9f8456e2016-09-05 05:02:59 +0200427 if "graph" in vnf:
tierno5bb59dc2017-02-13 14:53:54 +0100428 #I NSERT_["graph"]=yaml.safe_dump(vnf["graph"],default_flow_style=True,width=256)
429 # TODO, must be json because of the GUI, change to yaml
430 INSERT_["graph"] = json.dumps(vnf["graph"])
garciadeblas9f8456e2016-09-05 05:02:59 +0200431 created_time += 0.00001
tierno5bb59dc2017-02-13 14:53:54 +0100432 scn_vnf_uuid = self._new_row_internal('sce_vnfs', INSERT_, add_uuid=True,
433 root_uuid=scenario_uuid, created_time=created_time)
garciadeblas9f8456e2016-09-05 05:02:59 +0200434 vnf['scn_vnf_uuid']=scn_vnf_uuid
tierno5bb59dc2017-02-13 14:53:54 +0100435 # sce_interfaces
Adam Israel8e3ce872018-01-08 18:43:40 +0000436 for iface in vnf['ifaces'].values():
tierno5bb59dc2017-02-13 14:53:54 +0100437 # print 'iface', iface
garciadeblas9f8456e2016-09-05 05:02:59 +0200438 if 'net_key' not in iface:
439 continue
440 iface['net_id'] = scenario_dict['nets'][ iface['net_key'] ]['uuid']
441 INSERT_={'sce_vnf_id': scn_vnf_uuid,
tierno5bb59dc2017-02-13 14:53:54 +0100442 'sce_net_id': iface['net_id'],
443 'interface_id': iface['uuid'],
444 'ip_address': iface.get('ip_address')}
garciadeblas9f8456e2016-09-05 05:02:59 +0200445 created_time += 0.00001
tierno5bb59dc2017-02-13 14:53:54 +0100446 iface_uuid = self._new_row_internal('sce_interfaces', INSERT_, add_uuid=True,
447 root_uuid=scenario_uuid, created_time=created_time)
garciadeblas9f8456e2016-09-05 05:02:59 +0200448
449 return scenario_uuid
450
451 except (mdb.Error, AttributeError) as e:
452 self._format_error(e, tries)
garciadeblas9f8456e2016-09-05 05:02:59 +0200453 tries -= 1
454
tierno7edb6752016-03-21 17:37:52 +0100455 def edit_scenario(self, scenario_dict):
tiernof97fd272016-07-11 14:32:37 +0200456 tries = 2
457 while tries:
tierno7edb6752016-03-21 17:37:52 +0100458 modified_time = time.time()
tiernof97fd272016-07-11 14:32:37 +0200459 item_changed=0
tierno7edb6752016-03-21 17:37:52 +0100460 try:
461 with self.con:
462 self.cur = self.con.cursor()
463 #check that scenario exist
464 tenant_id = scenario_dict.get('tenant_id')
465 scenario_uuid = scenario_dict['uuid']
466
tiernof97fd272016-07-11 14:32:37 +0200467 where_text = "uuid='{}'".format(scenario_uuid)
tierno7edb6752016-03-21 17:37:52 +0100468 if not tenant_id and tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200469 where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id)
470 cmd = "SELECT * FROM scenarios WHERE "+ where_text
471 self.logger.debug(cmd)
472 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100473 self.cur.fetchall()
474 if self.cur.rowcount==0:
tiernof97fd272016-07-11 14:32:37 +0200475 raise db_base.db_base_Exception("No scenario found with this criteria " + where_text, db_base.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100476 elif self.cur.rowcount>1:
tiernof97fd272016-07-11 14:32:37 +0200477 raise db_base.db_base_Exception("More than one scenario found with this criteria " + where_text, db_base.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100478
479 #scenario
480 nodes = {}
481 topology = scenario_dict.pop("topology", None)
482 if topology != None and "nodes" in topology:
483 nodes = topology.get("nodes",{})
484 UPDATE_ = {}
485 if "name" in scenario_dict: UPDATE_["name"] = scenario_dict["name"]
486 if "description" in scenario_dict: UPDATE_["description"] = scenario_dict["description"]
487 if len(UPDATE_)>0:
488 WHERE_={'tenant_id': tenant_id, 'uuid': scenario_uuid}
tiernof97fd272016-07-11 14:32:37 +0200489 item_changed += self._update_rows('scenarios', UPDATE_, WHERE_, modified_time=modified_time)
tierno7edb6752016-03-21 17:37:52 +0100490 #sce_nets
Adam Israel8e3ce872018-01-08 18:43:40 +0000491 for node_id, node in nodes.items():
tierno7edb6752016-03-21 17:37:52 +0100492 if "graph" in node:
493 #node["graph"] = yaml.safe_dump(node["graph"],default_flow_style=True,width=256)
494 #TODO, must be json because of the GUI, change to yaml
495 node["graph"] = json.dumps(node["graph"])
496 WHERE_={'scenario_id': scenario_uuid, 'uuid': node_id}
tiernof97fd272016-07-11 14:32:37 +0200497 #Try to change at sce_nets(version 0 API backward compatibility and sce_vnfs)
498 item_changed += self._update_rows('sce_nets', node, WHERE_)
499 item_changed += self._update_rows('sce_vnfs', node, WHERE_, modified_time=modified_time)
500 return item_changed
tierno7edb6752016-03-21 17:37:52 +0100501
tiernof97fd272016-07-11 14:32:37 +0200502 except (mdb.Error, AttributeError) as e:
503 self._format_error(e, tries)
504 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100505
506# def get_instance_scenario(self, instance_scenario_id, tenant_id=None):
507# '''Obtain the scenario instance information, filtering by one or serveral of the tenant, uuid or name
508# instance_scenario_id is the uuid or the name if it is not a valid uuid format
509# Only one scenario isntance must mutch the filtering or an error is returned
510# '''
511# print "1******************************************************************"
512# try:
513# with self.con:
514# self.cur = self.con.cursor(mdb.cursors.DictCursor)
515# #scenario table
516# where_list=[]
517# if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" )
tiernof97fd272016-07-11 14:32:37 +0200518# if db_base._check_valid_uuid(instance_scenario_id):
tierno7edb6752016-03-21 17:37:52 +0100519# where_list.append( "uuid='" + instance_scenario_id +"'" )
520# else:
521# where_list.append( "name='" + instance_scenario_id +"'" )
522# where_text = " AND ".join(where_list)
523# self.cur.execute("SELECT * FROM instance_scenarios WHERE "+ where_text)
524# rows = self.cur.fetchall()
525# if self.cur.rowcount==0:
526# return -HTTP_Bad_Request, "No scenario instance found with this criteria " + where_text
527# elif self.cur.rowcount>1:
528# return -HTTP_Bad_Request, "More than one scenario instance found with this criteria " + where_text
529# instance_scenario_dict = rows[0]
530#
531# #instance_vnfs
532# self.cur.execute("SELECT uuid,vnf_id FROM instance_vnfs WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
533# instance_scenario_dict['instance_vnfs'] = self.cur.fetchall()
534# for vnf in instance_scenario_dict['instance_vnfs']:
535# #instance_vms
536# self.cur.execute("SELECT uuid, vim_vm_id "+
537# "FROM instance_vms "+
538# "WHERE instance_vnf_id='" + vnf['uuid'] +"'"
539# )
540# vnf['instance_vms'] = self.cur.fetchall()
541# #instance_nets
542# self.cur.execute("SELECT uuid, vim_net_id FROM instance_nets WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
543# instance_scenario_dict['instance_nets'] = self.cur.fetchall()
544#
545# #instance_interfaces
546# self.cur.execute("SELECT uuid, vim_interface_id, instance_vm_id, instance_net_id FROM instance_interfaces WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
547# instance_scenario_dict['instance_interfaces'] = self.cur.fetchall()
548#
tiernof97fd272016-07-11 14:32:37 +0200549# db_base._convert_datetime2str(instance_scenario_dict)
550# db_base._convert_str2boolean(instance_scenario_dict, ('public','shared','external') )
tierno7edb6752016-03-21 17:37:52 +0100551# print "2******************************************************************"
552# return 1, instance_scenario_dict
tiernof97fd272016-07-11 14:32:37 +0200553# except (mdb.Error, AttributeError) as e:
tierno7edb6752016-03-21 17:37:52 +0100554# print "nfvo_db.get_instance_scenario DB Exception %d: %s" % (e.args[0], e.args[1])
tiernof97fd272016-07-11 14:32:37 +0200555# return self._format_error(e)
tierno7edb6752016-03-21 17:37:52 +0100556
tierno868220c2017-09-26 00:11:05 +0200557 def get_scenario(self, scenario_id, tenant_id=None, datacenter_vim_id=None, datacenter_id=None):
tierno7edb6752016-03-21 17:37:52 +0100558 '''Obtain the scenario information, filtering by one or serveral of the tenant, uuid or name
559 scenario_id is the uuid or the name if it is not a valid uuid format
tierno868220c2017-09-26 00:11:05 +0200560 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 +0100561 Only one scenario must mutch the filtering or an error is returned
562 '''
tiernof97fd272016-07-11 14:32:37 +0200563 tries = 2
564 while tries:
tierno7edb6752016-03-21 17:37:52 +0100565 try:
566 with self.con:
567 self.cur = self.con.cursor(mdb.cursors.DictCursor)
tiernof97fd272016-07-11 14:32:37 +0200568 where_text = "uuid='{}'".format(scenario_id)
tierno7edb6752016-03-21 17:37:52 +0100569 if not tenant_id and tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200570 where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id)
571 cmd = "SELECT * FROM scenarios WHERE " + where_text
572 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100573 self.cur.execute(cmd)
574 rows = self.cur.fetchall()
575 if self.cur.rowcount==0:
tiernof97fd272016-07-11 14:32:37 +0200576 raise db_base.db_base_Exception("No scenario found with this criteria " + where_text, db_base.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100577 elif self.cur.rowcount>1:
tiernof97fd272016-07-11 14:32:37 +0200578 raise db_base.db_base_Exception("More than one scenario found with this criteria " + where_text, db_base.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100579 scenario_dict = rows[0]
tiernoa4e1a6e2016-08-31 14:19:40 +0200580 if scenario_dict["cloud_config"]:
581 scenario_dict["cloud-config"] = yaml.load(scenario_dict["cloud_config"])
582 del scenario_dict["cloud_config"]
tierno7edb6752016-03-21 17:37:52 +0100583 #sce_vnfs
tiernof1ba57e2017-09-07 12:23:19 +0200584 cmd = "SELECT uuid,name,member_vnf_index,vnf_id,description FROM sce_vnfs WHERE scenario_id='{}' "\
585 "ORDER BY created_at".format(scenario_dict['uuid'])
tiernof97fd272016-07-11 14:32:37 +0200586 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100587 self.cur.execute(cmd)
588 scenario_dict['vnfs'] = self.cur.fetchall()
gcalvinoe580c7d2017-09-22 14:09:51 +0200589
tierno7edb6752016-03-21 17:37:52 +0100590 for vnf in scenario_dict['vnfs']:
gcalvinoe580c7d2017-09-22 14:09:51 +0200591 cmd = "SELECT mgmt_access FROM vnfs WHERE uuid='{}'".format(scenario_dict['vnfs'][0]['vnf_id'])
592 self.logger.debug(cmd)
593 self.cur.execute(cmd)
594 mgmt_access_dict = self.cur.fetchall()
595 if mgmt_access_dict[0].get('mgmt_access'):
596 vnf['mgmt_access'] = yaml.load(mgmt_access_dict[0]['mgmt_access'])
597 else:
598 vnf['mgmt_access'] = None
tierno7edb6752016-03-21 17:37:52 +0100599 #sce_interfaces
tierno8e690322017-08-10 15:58:50 +0200600 cmd = "SELECT scei.uuid,scei.sce_net_id,scei.interface_id,i.external_name,scei.ip_address"\
601 " FROM sce_interfaces as scei join interfaces as i on scei.interface_id=i.uuid"\
602 " WHERE scei.sce_vnf_id='{}' ORDER BY scei.created_at".format(vnf['uuid'])
tiernof97fd272016-07-11 14:32:37 +0200603 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100604 self.cur.execute(cmd)
605 vnf['interfaces'] = self.cur.fetchall()
606 #vms
mirabal29356312017-07-27 12:21:22 +0200607 cmd = "SELECT vms.uuid as uuid, flavor_id, image_id, vms.name as name," \
tierno8e690322017-08-10 15:58:50 +0200608 " vms.description as description, vms.boot_data as boot_data, count," \
mirabal29356312017-07-27 12:21:22 +0200609 " vms.availability_zone as availability_zone" \
tierno8e690322017-08-10 15:58:50 +0200610 " FROM vnfs join vms on vnfs.uuid=vms.vnf_id" \
611 " WHERE vnfs.uuid='" + vnf['vnf_id'] + "'" \
mirabal29356312017-07-27 12:21:22 +0200612 " ORDER BY vms.created_at"
tiernof97fd272016-07-11 14:32:37 +0200613 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100614 self.cur.execute(cmd)
615 vnf['vms'] = self.cur.fetchall()
616 for vm in vnf['vms']:
tierno36c0b172017-01-12 18:32:28 +0100617 if vm["boot_data"]:
618 vm["boot_data"] = yaml.safe_load(vm["boot_data"])
619 else:
620 del vm["boot_data"]
tierno868220c2017-09-26 00:11:05 +0200621 if datacenter_vim_id!=None:
622 cmd = "SELECT vim_id FROM datacenters_images WHERE image_id='{}' AND datacenter_vim_id='{}'".format(vm['image_id'],datacenter_vim_id)
tiernof97fd272016-07-11 14:32:37 +0200623 self.logger.debug(cmd)
624 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100625 if self.cur.rowcount==1:
626 vim_image_dict = self.cur.fetchone()
627 vm['vim_image_id']=vim_image_dict['vim_id']
tierno868220c2017-09-26 00:11:05 +0200628 cmd = "SELECT vim_id FROM datacenters_flavors WHERE flavor_id='{}' AND datacenter_vim_id='{}'".format(vm['flavor_id'],datacenter_vim_id)
tiernof97fd272016-07-11 14:32:37 +0200629 self.logger.debug(cmd)
630 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100631 if self.cur.rowcount==1:
632 vim_flavor_dict = self.cur.fetchone()
633 vm['vim_flavor_id']=vim_flavor_dict['vim_id']
634
635 #interfaces
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000636 cmd = "SELECT uuid,internal_name,external_name,net_id,type,vpci,mac,bw,model,ip_address," \
637 "floating_ip, port_security" \
tierno7edb6752016-03-21 17:37:52 +0100638 " FROM interfaces" \
tiernof97fd272016-07-11 14:32:37 +0200639 " WHERE vm_id='{}'" \
640 " ORDER BY created_at".format(vm['uuid'])
641 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100642 self.cur.execute(cmd)
643 vm['interfaces'] = self.cur.fetchall()
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000644 for index in range(0,len(vm['interfaces'])):
645 vm['interfaces'][index]['port-security'] = vm['interfaces'][index].pop("port_security")
646 vm['interfaces'][index]['floating-ip'] = vm['interfaces'][index].pop("floating_ip")
tierno7edb6752016-03-21 17:37:52 +0100647 #nets every net of a vms
tiernof97fd272016-07-11 14:32:37 +0200648 cmd = "SELECT uuid,name,type,description FROM nets WHERE vnf_id='{}'".format(vnf['vnf_id'])
649 self.logger.debug(cmd)
650 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100651 vnf['nets'] = self.cur.fetchall()
garciadeblas9f8456e2016-09-05 05:02:59 +0200652 for vnf_net in vnf['nets']:
653 SELECT_ = "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count"
654 cmd = "SELECT {} FROM ip_profiles WHERE net_id='{}'".format(SELECT_,vnf_net['uuid'])
655 self.logger.debug(cmd)
656 self.cur.execute(cmd)
657 ipprofiles = self.cur.fetchall()
658 if self.cur.rowcount==1:
659 vnf_net["ip_profile"] = ipprofiles[0]
660 elif self.cur.rowcount>1:
661 raise db_base.db_base_Exception("More than one ip-profile found with this criteria: net_id='{}'".format(vnf_net['uuid']), db_base.HTTP_Bad_Request)
662
tierno7edb6752016-03-21 17:37:52 +0100663 #sce_nets
664 cmd = "SELECT uuid,name,type,external,description" \
tiernof97fd272016-07-11 14:32:37 +0200665 " FROM sce_nets WHERE scenario_id='{}'" \
666 " ORDER BY created_at ".format(scenario_dict['uuid'])
667 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100668 self.cur.execute(cmd)
669 scenario_dict['nets'] = self.cur.fetchall()
670 #datacenter_nets
671 for net in scenario_dict['nets']:
672 if str(net['external']) == 'false':
garciadeblas9f8456e2016-09-05 05:02:59 +0200673 SELECT_ = "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count"
674 cmd = "SELECT {} FROM ip_profiles WHERE sce_net_id='{}'".format(SELECT_,net['uuid'])
675 self.logger.debug(cmd)
676 self.cur.execute(cmd)
677 ipprofiles = self.cur.fetchall()
678 if self.cur.rowcount==1:
679 net["ip_profile"] = ipprofiles[0]
680 elif self.cur.rowcount>1:
681 raise db_base.db_base_Exception("More than one ip-profile found with this criteria: sce_net_id='{}'".format(net['uuid']), db_base.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100682 continue
tiernof97fd272016-07-11 14:32:37 +0200683 WHERE_=" WHERE name='{}'".format(net['name'])
tierno7edb6752016-03-21 17:37:52 +0100684 if datacenter_id!=None:
tiernof97fd272016-07-11 14:32:37 +0200685 WHERE_ += " AND datacenter_id='{}'".format(datacenter_id)
686 cmd = "SELECT vim_net_id FROM datacenter_nets" + WHERE_
687 self.logger.debug(cmd)
688 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100689 d_net = self.cur.fetchone()
tierno868220c2017-09-26 00:11:05 +0200690 if d_net==None or datacenter_vim_id==None:
tierno7edb6752016-03-21 17:37:52 +0100691 #print "nfvo_db.get_scenario() WARNING external net %s not found" % net['name']
692 net['vim_id']=None
693 else:
694 net['vim_id']=d_net['vim_net_id']
695
tiernof97fd272016-07-11 14:32:37 +0200696 db_base._convert_datetime2str(scenario_dict)
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000697 db_base._convert_str2boolean(scenario_dict, ('public','shared','external','port-security','floating-ip') )
tiernof97fd272016-07-11 14:32:37 +0200698 return scenario_dict
699 except (mdb.Error, AttributeError) as e:
700 self._format_error(e, tries)
701 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100702
tierno7edb6752016-03-21 17:37:52 +0100703 def delete_scenario(self, scenario_id, tenant_id=None):
704 '''Deletes a scenario, filtering by one or several of the tenant, uuid or name
705 scenario_id is the uuid or the name if it is not a valid uuid format
706 Only one scenario must mutch the filtering or an error is returned
707 '''
tiernof97fd272016-07-11 14:32:37 +0200708 tries = 2
709 while tries:
tierno7edb6752016-03-21 17:37:52 +0100710 try:
711 with self.con:
712 self.cur = self.con.cursor(mdb.cursors.DictCursor)
713
714 #scenario table
tiernof97fd272016-07-11 14:32:37 +0200715 where_text = "uuid='{}'".format(scenario_id)
tierno7edb6752016-03-21 17:37:52 +0100716 if not tenant_id and tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200717 where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id)
718 cmd = "SELECT * FROM scenarios WHERE "+ where_text
719 self.logger.debug(cmd)
720 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100721 rows = self.cur.fetchall()
722 if self.cur.rowcount==0:
tiernof97fd272016-07-11 14:32:37 +0200723 raise db_base.db_base_Exception("No scenario found where " + where_text, db_base.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100724 elif self.cur.rowcount>1:
tiernof97fd272016-07-11 14:32:37 +0200725 raise db_base.db_base_Exception("More than one scenario found where " + where_text, db_base.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100726 scenario_uuid = rows[0]["uuid"]
727 scenario_name = rows[0]["name"]
728
729 #sce_vnfs
tiernof97fd272016-07-11 14:32:37 +0200730 cmd = "DELETE FROM scenarios WHERE uuid='{}'".format(scenario_uuid)
731 self.logger.debug(cmd)
732 self.cur.execute(cmd)
gcalvinoe580c7d2017-09-22 14:09:51 +0200733
tiernof97fd272016-07-11 14:32:37 +0200734 return scenario_uuid + " " + scenario_name
735 except (mdb.Error, AttributeError) as e:
736 self._format_error(e, tries, "delete", "instances running")
737 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100738
tierno8e690322017-08-10 15:58:50 +0200739 def new_rows(self, tables, uuid_list=None):
740 """
741 Make a transactional insertion of rows at several tables
742 :param tables: list with dictionary where the keys are the table names and the values are a row or row list
743 with the values to be inserted at the table. Each row is a dictionary with the key values. E.g.:
744 tables = [
745 {"table1": [ {"column1": value, "column2: value, ... }, {"column1": value, "column2: value, ... }, ...],
746 {"table2": [ {"column1": value, "column2: value, ... }, {"column1": value, "column2: value, ... }, ...],
747 {"table3": {"column1": value, "column2: value, ... }
748 }
tiernoa9550202017-09-22 13:31:35 +0200749 If tables does not contain the 'created_at', it is generated incrementally with the order of tables. You can
750 provide a integer value, that it is an index multiply by 0.00001 to add to the created time to manually set
751 up and order
tierno8e690322017-08-10 15:58:50 +0200752 :param uuid_list: list of created uuids, first one is the root (#TODO to store at uuid table)
753 :return: None if success, raise exception otherwise
754 """
755 tries = 2
756 while tries:
757 created_time = time.time()
758 try:
759 with self.con:
760 self.cur = self.con.cursor()
761 for table in tables:
Adam Israel8e3ce872018-01-08 18:43:40 +0000762 for table_name, row_list in table.items():
tierno8e690322017-08-10 15:58:50 +0200763 index = 0
764 if isinstance(row_list, dict):
765 row_list = (row_list, ) #create a list with the single value
766 for row in row_list:
767 if table_name in self.tables_with_created_field:
tiernoa9550202017-09-22 13:31:35 +0200768 if "created_at" in row:
769 created_time_param = created_time + row.pop("created_at")*0.00001
770 else:
771 created_time_param = created_time + index*0.00001
772 index += 1
tierno8e690322017-08-10 15:58:50 +0200773 else:
tiernoa9550202017-09-22 13:31:35 +0200774 created_time_param = 0
tierno8e690322017-08-10 15:58:50 +0200775 self._new_row_internal(table_name, row, add_uuid=False, root_uuid=None,
776 created_time=created_time_param)
tierno8e690322017-08-10 15:58:50 +0200777 return
778 except (mdb.Error, AttributeError) as e:
779 self._format_error(e, tries)
780 tries -= 1
781
tierno7edb6752016-03-21 17:37:52 +0100782 def new_instance_scenario_as_a_whole(self,tenant_id,instance_scenario_name,instance_scenario_description,scenarioDict):
tiernof97fd272016-07-11 14:32:37 +0200783 tries = 2
784 while tries:
tierno7edb6752016-03-21 17:37:52 +0100785 created_time = time.time()
786 try:
787 with self.con:
788 self.cur = self.con.cursor()
789 #instance_scenarios
tierno7edb6752016-03-21 17:37:52 +0100790 datacenter_id = scenarioDict['datacenter_id']
791 INSERT_={'tenant_id': tenant_id,
tiernoa2793912016-10-04 08:15:08 +0000792 'datacenter_tenant_id': scenarioDict["datacenter2tenant"][datacenter_id],
tierno7edb6752016-03-21 17:37:52 +0100793 'name': instance_scenario_name,
794 'description': instance_scenario_description,
795 'scenario_id' : scenarioDict['uuid'],
796 'datacenter_id': datacenter_id
797 }
tiernoa4e1a6e2016-08-31 14:19:40 +0200798 if scenarioDict.get("cloud-config"):
799 INSERT_["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"], default_flow_style=True, width=256)
800
tiernof97fd272016-07-11 14:32:37 +0200801 instance_uuid = self._new_row_internal('instance_scenarios', INSERT_, add_uuid=True, root_uuid=None, created_time=created_time)
tierno7edb6752016-03-21 17:37:52 +0100802
803 net_scene2instance={}
804 #instance_nets #nets interVNF
805 for net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +0200806 net_scene2instance[ net['uuid'] ] ={}
807 datacenter_site_id = net.get('datacenter_id', datacenter_id)
808 if not "vim_id_sites" in net:
809 net["vim_id_sites"] ={datacenter_site_id: net['vim_id']}
tiernoa2793912016-10-04 08:15:08 +0000810 net["vim_id_sites"]["datacenter_site_id"] = {datacenter_site_id: net['vim_id']}
tiernobe41e222016-09-02 15:16:13 +0200811 sce_net_id = net.get("uuid")
812
Adam Israel8e3ce872018-01-08 18:43:40 +0000813 for datacenter_site_id,vim_id in net["vim_id_sites"].iteritems():
tierno66345bc2016-09-26 11:37:55 +0200814 INSERT_={'vim_net_id': vim_id, 'created': net.get('created', False), 'instance_scenario_id':instance_uuid } #, 'type': net['type']
tiernobe41e222016-09-02 15:16:13 +0200815 INSERT_['datacenter_id'] = datacenter_site_id
tiernoa2793912016-10-04 08:15:08 +0000816 INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_site_id]
tierno91baf982017-03-30 19:37:57 +0200817 if not net.get('created', False):
818 INSERT_['status'] = "ACTIVE"
tiernobe41e222016-09-02 15:16:13 +0200819 if sce_net_id:
820 INSERT_['sce_net_id'] = sce_net_id
821 created_time += 0.00001
822 instance_net_uuid = self._new_row_internal('instance_nets', INSERT_, True, instance_uuid, created_time)
823 net_scene2instance[ sce_net_id ][datacenter_site_id] = instance_net_uuid
824 net['uuid'] = instance_net_uuid #overwrite scnario uuid by instance uuid
garciadeblas9f8456e2016-09-05 05:02:59 +0200825
826 if 'ip_profile' in net:
827 net['ip_profile']['net_id'] = None
828 net['ip_profile']['sce_net_id'] = None
829 net['ip_profile']['instance_net_id'] = instance_net_uuid
830 created_time += 0.00001
831 ip_profile_id = self._new_row_internal('ip_profiles', net['ip_profile'])
tierno7edb6752016-03-21 17:37:52 +0100832
833 #instance_vnfs
834 for vnf in scenarioDict['vnfs']:
tiernobe41e222016-09-02 15:16:13 +0200835 datacenter_site_id = vnf.get('datacenter_id', datacenter_id)
tierno7edb6752016-03-21 17:37:52 +0100836 INSERT_={'instance_scenario_id': instance_uuid, 'vnf_id': vnf['vnf_id'] }
tiernobe41e222016-09-02 15:16:13 +0200837 INSERT_['datacenter_id'] = datacenter_site_id
tiernoa2793912016-10-04 08:15:08 +0000838 INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_site_id]
tierno7edb6752016-03-21 17:37:52 +0100839 if vnf.get("uuid"):
840 INSERT_['sce_vnf_id'] = vnf['uuid']
841 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200842 instance_vnf_uuid = self._new_row_internal('instance_vnfs', INSERT_, True, instance_uuid, created_time)
tierno7edb6752016-03-21 17:37:52 +0100843 vnf['uuid'] = instance_vnf_uuid #overwrite scnario uuid by instance uuid
844
845 #instance_nets #nets intraVNF
846 for net in vnf['nets']:
tiernobe41e222016-09-02 15:16:13 +0200847 net_scene2instance[ net['uuid'] ] = {}
tierno66345bc2016-09-26 11:37:55 +0200848 INSERT_={'vim_net_id': net['vim_id'], 'created': net.get('created', False), 'instance_scenario_id':instance_uuid } #, 'type': net['type']
tiernobe41e222016-09-02 15:16:13 +0200849 INSERT_['datacenter_id'] = net.get('datacenter_id', datacenter_site_id)
tiernoa2793912016-10-04 08:15:08 +0000850 INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_id]
tierno7edb6752016-03-21 17:37:52 +0100851 if net.get("uuid"):
852 INSERT_['net_id'] = net['uuid']
853 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200854 instance_net_uuid = self._new_row_internal('instance_nets', INSERT_, True, instance_uuid, created_time)
tiernobe41e222016-09-02 15:16:13 +0200855 net_scene2instance[ net['uuid'] ][datacenter_site_id] = instance_net_uuid
tierno7edb6752016-03-21 17:37:52 +0100856 net['uuid'] = instance_net_uuid #overwrite scnario uuid by instance uuid
garciadeblas9f8456e2016-09-05 05:02:59 +0200857
858 if 'ip_profile' in net:
859 net['ip_profile']['net_id'] = None
860 net['ip_profile']['sce_net_id'] = None
861 net['ip_profile']['instance_net_id'] = instance_net_uuid
862 created_time += 0.00001
863 ip_profile_id = self._new_row_internal('ip_profiles', net['ip_profile'])
864
tierno7edb6752016-03-21 17:37:52 +0100865 #instance_vms
866 for vm in vnf['vms']:
867 INSERT_={'instance_vnf_id': instance_vnf_uuid, 'vm_id': vm['uuid'], 'vim_vm_id': vm['vim_id'] }
868 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200869 instance_vm_uuid = self._new_row_internal('instance_vms', INSERT_, True, instance_uuid, created_time)
tierno7edb6752016-03-21 17:37:52 +0100870 vm['uuid'] = instance_vm_uuid #overwrite scnario uuid by instance uuid
871
872 #instance_interfaces
873 for interface in vm['interfaces']:
874 net_id = interface.get('net_id', None)
875 if net_id is None:
876 #check if is connected to a inter VNFs net
877 for iface in vnf['interfaces']:
878 if iface['interface_id'] == interface['uuid']:
garciadeblas9f8456e2016-09-05 05:02:59 +0200879 if 'ip_address' in iface:
880 interface['ip_address'] = iface['ip_address']
tierno7edb6752016-03-21 17:37:52 +0100881 net_id = iface.get('sce_net_id', None)
882 break
883 if net_id is None:
884 continue
885 interface_type='external' if interface['external_name'] is not None else 'internal'
tiernobe41e222016-09-02 15:16:13 +0200886 INSERT_={'instance_vm_id': instance_vm_uuid, 'instance_net_id': net_scene2instance[net_id][datacenter_site_id],
garciadeblas9f8456e2016-09-05 05:02:59 +0200887 'interface_id': interface['uuid'], 'vim_interface_id': interface.get('vim_id'), 'type': interface_type,
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000888 'ip_address': interface.get('ip_address'), 'floating_ip': int(interface.get('floating-ip',False)),
889 'port_security': int(interface.get('port-security',True))}
tierno7edb6752016-03-21 17:37:52 +0100890 #created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200891 interface_uuid = self._new_row_internal('instance_interfaces', INSERT_, True, instance_uuid) #, created_time)
tierno7edb6752016-03-21 17:37:52 +0100892 interface['uuid'] = interface_uuid #overwrite scnario uuid by instance uuid
tiernof97fd272016-07-11 14:32:37 +0200893 return instance_uuid
894 except (mdb.Error, AttributeError) as e:
895 self._format_error(e, tries)
896 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100897
898 def get_instance_scenario(self, instance_id, tenant_id=None, verbose=False):
899 '''Obtain the instance information, filtering by one or several of the tenant, uuid or name
900 instance_id is the uuid or the name if it is not a valid uuid format
901 Only one instance must mutch the filtering or an error is returned
902 '''
tiernof97fd272016-07-11 14:32:37 +0200903 tries = 2
904 while tries:
tierno7edb6752016-03-21 17:37:52 +0100905 try:
906 with self.con:
907 self.cur = self.con.cursor(mdb.cursors.DictCursor)
tierno868220c2017-09-26 00:11:05 +0200908 # instance table
tiernoa15c4b92017-10-05 12:41:44 +0200909 where_list = []
910 if tenant_id:
911 where_list.append("inst.tenant_id='{}'".format(tenant_id))
tiernof97fd272016-07-11 14:32:37 +0200912 if db_base._check_valid_uuid(instance_id):
tiernoa15c4b92017-10-05 12:41:44 +0200913 where_list.append("inst.uuid='{}'".format(instance_id))
tierno7edb6752016-03-21 17:37:52 +0100914 else:
tiernoa15c4b92017-10-05 12:41:44 +0200915 where_list.append("inst.name='{}'".format(instance_id))
tierno7edb6752016-03-21 17:37:52 +0100916 where_text = " AND ".join(where_list)
tiernoa15c4b92017-10-05 12:41:44 +0200917 cmd = "SELECT inst.uuid as uuid, inst.name as name, inst.scenario_id as scenario_id, datacenter_id"\
918 " ,datacenter_tenant_id, s.name as scenario_name,inst.tenant_id as tenant_id" \
919 " ,inst.description as description, inst.created_at as created_at" \
920 " ,inst.cloud_config as cloud_config" \
921 " FROM instance_scenarios as inst left join scenarios as s on inst.scenario_id=s.uuid" \
tierno7edb6752016-03-21 17:37:52 +0100922 " WHERE " + where_text
tiernof97fd272016-07-11 14:32:37 +0200923 self.logger.debug(cmd)
924 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100925 rows = self.cur.fetchall()
tiernof97fd272016-07-11 14:32:37 +0200926
tiernoa15c4b92017-10-05 12:41:44 +0200927 if self.cur.rowcount == 0:
tiernof97fd272016-07-11 14:32:37 +0200928 raise db_base.db_base_Exception("No instance found where " + where_text, db_base.HTTP_Not_Found)
tiernoa15c4b92017-10-05 12:41:44 +0200929 elif self.cur.rowcount > 1:
930 raise db_base.db_base_Exception("More than one instance found where " + where_text,
931 db_base.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100932 instance_dict = rows[0]
tiernoa4e1a6e2016-08-31 14:19:40 +0200933 if instance_dict["cloud_config"]:
934 instance_dict["cloud-config"] = yaml.load(instance_dict["cloud_config"])
935 del instance_dict["cloud_config"]
tierno7edb6752016-03-21 17:37:52 +0100936
tiernoa15c4b92017-10-05 12:41:44 +0200937 # instance_vnfs
938 cmd = "SELECT iv.uuid as uuid, iv.vnf_id as vnf_id, sv.name as vnf_name, sce_vnf_id, datacenter_id"\
939 " ,datacenter_tenant_id, v.mgmt_access "\
940 " FROM instance_vnfs as iv left join sce_vnfs as sv "\
941 "on iv.sce_vnf_id=sv.uuid join vnfs as v on iv.vnf_id=v.uuid" \
tiernof97fd272016-07-11 14:32:37 +0200942 " WHERE iv.instance_scenario_id='{}'" \
943 " ORDER BY iv.created_at ".format(instance_dict['uuid'])
944 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100945 self.cur.execute(cmd)
946 instance_dict['vnfs'] = self.cur.fetchall()
947 for vnf in instance_dict['vnfs']:
948 vnf_manage_iface_list=[]
949 #instance vms
gcalvinoe580c7d2017-09-22 14:09:51 +0200950 cmd = "SELECT iv.uuid as uuid, vim_vm_id, status, error_msg, vim_info, iv.created_at as created_at, name"\
tierno7edb6752016-03-21 17:37:52 +0100951 " FROM instance_vms as iv join vms on iv.vm_id=vms.uuid "\
tiernof97fd272016-07-11 14:32:37 +0200952 " WHERE instance_vnf_id='{}' ORDER BY iv.created_at".format(vnf['uuid'])
953 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100954 self.cur.execute(cmd)
955 vnf['vms'] = self.cur.fetchall()
956 for vm in vnf['vms']:
957 vm_manage_iface_list=[]
tiernoa15c4b92017-10-05 12:41:44 +0200958 # instance_interfaces
tiernofa51c202017-01-27 14:58:17 +0100959 cmd = "SELECT vim_interface_id, instance_net_id, internal_name,external_name, mac_address,"\
tierno867ffe92017-03-27 12:50:34 +0200960 " ii.ip_address as ip_address, vim_info, i.type as type, sdn_port_id"\
tiernofa51c202017-01-27 14:58:17 +0100961 " FROM instance_interfaces as ii join interfaces as i on ii.interface_id=i.uuid"\
962 " WHERE instance_vm_id='{}' ORDER BY created_at".format(vm['uuid'])
tiernof97fd272016-07-11 14:32:37 +0200963 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100964 self.cur.execute(cmd )
965 vm['interfaces'] = self.cur.fetchall()
966 for iface in vm['interfaces']:
967 if iface["type"] == "mgmt" and iface["ip_address"]:
968 vnf_manage_iface_list.append(iface["ip_address"])
969 vm_manage_iface_list.append(iface["ip_address"])
970 if not verbose:
971 del iface["type"]
972 if vm_manage_iface_list: vm["ip_address"] = ",".join(vm_manage_iface_list)
973 if vnf_manage_iface_list: vnf["ip_address"] = ",".join(vnf_manage_iface_list)
974
975 #instance_nets
976 #select_text = "instance_nets.uuid as uuid,sce_nets.name as net_name,instance_nets.vim_net_id as net_id,instance_nets.status as status,instance_nets.external as external"
977 #from_text = "instance_nets join instance_scenarios on instance_nets.instance_scenario_id=instance_scenarios.uuid " + \
978 # "join sce_nets on instance_scenarios.scenario_id=sce_nets.scenario_id"
979 #where_text = "instance_nets.instance_scenario_id='"+ instance_dict['uuid'] + "'"
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100980 cmd = "SELECT uuid,vim_net_id,status,error_msg,vim_info,created, sce_net_id, net_id as vnf_net_id, datacenter_id, datacenter_tenant_id, sdn_net_id"\
tierno7edb6752016-03-21 17:37:52 +0100981 " FROM instance_nets" \
tiernof97fd272016-07-11 14:32:37 +0200982 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict['uuid'])
983 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100984 self.cur.execute(cmd)
985 instance_dict['nets'] = self.cur.fetchall()
986
tiernof97fd272016-07-11 14:32:37 +0200987 db_base._convert_datetime2str(instance_dict)
tierno66345bc2016-09-26 11:37:55 +0200988 db_base._convert_str2boolean(instance_dict, ('public','shared','created') )
tiernof97fd272016-07-11 14:32:37 +0200989 return instance_dict
990 except (mdb.Error, AttributeError) as e:
991 self._format_error(e, tries)
992 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100993
994 def delete_instance_scenario(self, instance_id, tenant_id=None):
995 '''Deletes a instance_Scenario, filtering by one or serveral of the tenant, uuid or name
996 instance_id is the uuid or the name if it is not a valid uuid format
997 Only one instance_scenario must mutch the filtering or an error is returned
998 '''
tiernof97fd272016-07-11 14:32:37 +0200999 tries = 2
1000 while tries:
tierno7edb6752016-03-21 17:37:52 +01001001 try:
1002 with self.con:
1003 self.cur = self.con.cursor(mdb.cursors.DictCursor)
1004
1005 #instance table
1006 where_list=[]
1007 if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" )
tiernof97fd272016-07-11 14:32:37 +02001008 if db_base._check_valid_uuid(instance_id):
tierno7edb6752016-03-21 17:37:52 +01001009 where_list.append( "uuid='" + instance_id +"'" )
1010 else:
1011 where_list.append( "name='" + instance_id +"'" )
1012 where_text = " AND ".join(where_list)
tiernof97fd272016-07-11 14:32:37 +02001013 cmd = "SELECT * FROM instance_scenarios WHERE "+ where_text
1014 self.logger.debug(cmd)
1015 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +01001016 rows = self.cur.fetchall()
tiernof97fd272016-07-11 14:32:37 +02001017
tierno7edb6752016-03-21 17:37:52 +01001018 if self.cur.rowcount==0:
tiernof97fd272016-07-11 14:32:37 +02001019 raise db_base.db_base_Exception("No instance found where " + where_text, db_base.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001020 elif self.cur.rowcount>1:
tiernof97fd272016-07-11 14:32:37 +02001021 raise db_base.db_base_Exception("More than one instance found where " + where_text, db_base.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001022 instance_uuid = rows[0]["uuid"]
1023 instance_name = rows[0]["name"]
1024
1025 #sce_vnfs
tiernof97fd272016-07-11 14:32:37 +02001026 cmd = "DELETE FROM instance_scenarios WHERE uuid='{}'".format(instance_uuid)
1027 self.logger.debug(cmd)
1028 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +01001029
tiernof97fd272016-07-11 14:32:37 +02001030 return instance_uuid + " " + instance_name
1031 except (mdb.Error, AttributeError) as e:
1032 self._format_error(e, tries, "delete", "No dependences can avoid deleting!!!!")
1033 tries -= 1
tierno7edb6752016-03-21 17:37:52 +01001034
1035 def new_instance_scenario(self, instance_scenario_dict, tenant_id):
1036 #return self.new_row('vnfs', vnf_dict, None, tenant_id, True, True)
1037 return self._new_row_internal('instance_scenarios', instance_scenario_dict, tenant_id, add_uuid=True, root_uuid=None, log=True)
1038
1039 def update_instance_scenario(self, instance_scenario_dict):
1040 #TODO:
1041 return
1042
1043 def new_instance_vnf(self, instance_vnf_dict, tenant_id, instance_scenario_id = None):
1044 #return self.new_row('vms', vm_dict, tenant_id, True, True)
1045 return self._new_row_internal('instance_vnfs', instance_vnf_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
1046
1047 def update_instance_vnf(self, instance_vnf_dict):
1048 #TODO:
1049 return
1050
1051 def delete_instance_vnf(self, instance_vnf_id):
1052 #TODO:
1053 return
1054
1055 def new_instance_vm(self, instance_vm_dict, tenant_id, instance_scenario_id = None):
1056 #return self.new_row('vms', vm_dict, tenant_id, True, True)
1057 return self._new_row_internal('instance_vms', instance_vm_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
1058
1059 def update_instance_vm(self, instance_vm_dict):
1060 #TODO:
1061 return
1062
1063 def delete_instance_vm(self, instance_vm_id):
1064 #TODO:
1065 return
1066
1067 def new_instance_net(self, instance_net_dict, tenant_id, instance_scenario_id = None):
1068 return self._new_row_internal('instance_nets', instance_net_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
1069
1070 def update_instance_net(self, instance_net_dict):
1071 #TODO:
1072 return
1073
1074 def delete_instance_net(self, instance_net_id):
1075 #TODO:
1076 return
1077
1078 def new_instance_interface(self, instance_interface_dict, tenant_id, instance_scenario_id = None):
1079 return self._new_row_internal('instance_interfaces', instance_interface_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
1080
1081 def update_instance_interface(self, instance_interface_dict):
1082 #TODO:
1083 return
1084
1085 def delete_instance_interface(self, instance_interface_dict):
1086 #TODO:
1087 return
1088
1089 def update_datacenter_nets(self, datacenter_id, new_net_list=[]):
1090 ''' Removes the old and adds the new net list at datacenter list for one datacenter.
1091 Attribute
1092 datacenter_id: uuid of the datacenter to act upon
1093 table: table where to insert
1094 new_net_list: the new values to be inserted. If empty it only deletes the existing nets
1095 Return: (Inserted items, Deleted items) if OK, (-Error, text) if error
1096 '''
tiernof97fd272016-07-11 14:32:37 +02001097 tries = 2
1098 while tries:
tierno7edb6752016-03-21 17:37:52 +01001099 created_time = time.time()
1100 try:
1101 with self.con:
1102 self.cur = self.con.cursor()
tiernof97fd272016-07-11 14:32:37 +02001103 cmd="DELETE FROM datacenter_nets WHERE datacenter_id='{}'".format(datacenter_id)
1104 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +01001105 self.cur.execute(cmd)
1106 deleted = self.cur.rowcount
tiernof97fd272016-07-11 14:32:37 +02001107 inserted = 0
tierno7edb6752016-03-21 17:37:52 +01001108 for new_net in new_net_list:
1109 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +02001110 self._new_row_internal('datacenter_nets', new_net, add_uuid=True, created_time=created_time)
1111 inserted += 1
1112 return inserted, deleted
1113 except (mdb.Error, AttributeError) as e:
1114 self._format_error(e, tries)
1115 tries -= 1
1116
tierno7edb6752016-03-21 17:37:52 +01001117