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