blob: 6e6b4941b49f147bd4b7c14a4bc2efca9d60c7bd [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
tiernof97fd272016-07-11 14:32:37 +020030import db_base
tierno7edb6752016-03-21 17:37:52 +010031import MySQLdb as mdb
tierno7edb6752016-03-21 17:37:52 +010032import json
tiernoa4e1a6e2016-08-31 14:19:40 +020033import yaml
tierno7edb6752016-03-21 17:37:52 +010034import time
35
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",
39 "sce_vnfs","tenants_datacenters","datacenter_tenants","vms","vnfs"]
40
tiernof97fd272016-07-11 14:32:37 +020041class nfvo_db(db_base.db_base):
42 def __init__(self, host=None, user=None, passwd=None, database=None, log_name='openmano.db', log_level="ERROR"):
43 db_base.db_base.__init__(self, host, user, passwd, database, log_name, log_level)
44 db_base.db_base.tables_with_created_field=tables_with_createdat_field
tierno7edb6752016-03-21 17:37:52 +010045 return
46
tierno7edb6752016-03-21 17:37:52 +010047
48 def new_vnf_as_a_whole(self,nfvo_tenant,vnf_name,vnf_descriptor,VNFCDict):
tiernof97fd272016-07-11 14:32:37 +020049 self.logger.debug("Adding new vnf to the NFVO database")
50 tries = 2
51 while tries:
tierno7edb6752016-03-21 17:37:52 +010052 created_time = time.time()
53 try:
54 with self.con:
55
56 myVNFDict = {}
57 myVNFDict["name"] = vnf_name
58 myVNFDict["descriptor"] = vnf_descriptor['vnf'].get('descriptor')
59 myVNFDict["public"] = vnf_descriptor['vnf'].get('public', "false")
60 myVNFDict["description"] = vnf_descriptor['vnf']['description']
61 myVNFDict["class"] = vnf_descriptor['vnf'].get('class',"MISC")
62 myVNFDict["tenant_id"] = vnf_descriptor['vnf'].get("tenant_id")
63
tiernof97fd272016-07-11 14:32:37 +020064 vnf_id = self._new_row_internal('vnfs', myVNFDict, add_uuid=True, root_uuid=None, created_time=created_time)
65 #print "Adding new vms to the NFVO database"
tierno7edb6752016-03-21 17:37:52 +010066 #For each vm, we must create the appropriate vm in the NFVO database.
67 vmDict = {}
68 for _,vm in VNFCDict.iteritems():
69 #This code could make the name of the vms grow and grow.
70 #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name
71 #vm['name'] = "%s-%s" % (vnf_name,vm['name'])
tiernof97fd272016-07-11 14:32:37 +020072 #print "VM name: %s. Description: %s" % (vm['name'], vm['description'])
tierno7edb6752016-03-21 17:37:52 +010073 vm["vnf_id"] = vnf_id
74 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +020075 vm_id = self._new_row_internal('vms', vm, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
76 #print "Internal vm id in NFVO DB: %s" % vm_id
tierno7edb6752016-03-21 17:37:52 +010077 vmDict[vm['name']] = vm_id
78
79 #Collect the data interfaces of each VM/VNFC under the 'numas' field
80 dataifacesDict = {}
81 for vm in vnf_descriptor['vnf']['VNFC']:
82 dataifacesDict[vm['name']] = {}
83 for numa in vm.get('numas', []):
84 for dataiface in numa.get('interfaces',[]):
tiernof97fd272016-07-11 14:32:37 +020085 db_base._convert_bandwidth(dataiface)
tierno7edb6752016-03-21 17:37:52 +010086 dataifacesDict[vm['name']][dataiface['name']] = {}
87 dataifacesDict[vm['name']][dataiface['name']]['vpci'] = dataiface['vpci']
88 dataifacesDict[vm['name']][dataiface['name']]['bw'] = dataiface['bandwidth']
89 dataifacesDict[vm['name']][dataiface['name']]['model'] = "PF" if dataiface['dedicated']=="yes" else ("VF" if dataiface['dedicated']=="no" else "VFnotShared")
90
91 #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field
92 bridgeInterfacesDict = {}
93 for vm in vnf_descriptor['vnf']['VNFC']:
94 if 'bridge-ifaces' in vm:
95 bridgeInterfacesDict[vm['name']] = {}
96 for bridgeiface in vm['bridge-ifaces']:
tiernof97fd272016-07-11 14:32:37 +020097 db_base._convert_bandwidth(bridgeiface)
tierno7edb6752016-03-21 17:37:52 +010098 bridgeInterfacesDict[vm['name']][bridgeiface['name']] = {}
99 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['vpci'] = bridgeiface.get('vpci',None)
100 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['mac'] = bridgeiface.get('mac_address',None)
101 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['bw'] = bridgeiface.get('bandwidth', None)
102 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['model'] = bridgeiface.get('model', None)
103
104 #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 +0200105 #print "Adding new nets (VNF internal nets) to the NFVO database (if any)"
tierno7edb6752016-03-21 17:37:52 +0100106 internalconnList = []
107 if 'internal-connections' in vnf_descriptor['vnf']:
108 for net in vnf_descriptor['vnf']['internal-connections']:
tiernof97fd272016-07-11 14:32:37 +0200109 #print "Net name: %s. Description: %s" % (net['name'], net['description'])
tierno7edb6752016-03-21 17:37:52 +0100110
111 myNetDict = {}
112 myNetDict["name"] = net['name']
113 myNetDict["description"] = net['description']
114 myNetDict["type"] = net['type']
115 myNetDict["vnf_id"] = vnf_id
116
117 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200118 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 +0100119
120 for element in net['elements']:
121 ifaceItem = {}
122 #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])
123 ifaceItem["internal_name"] = element['local_iface_name']
124 #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])]
125 ifaceItem["vm_id"] = vmDict[element['VNFC']]
126 ifaceItem["net_id"] = net_id
127 ifaceItem["type"] = net['type']
128 if ifaceItem ["type"] == "data":
129 ifaceItem["vpci"] = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['vpci']
130 ifaceItem["bw"] = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['bw']
131 ifaceItem["model"] = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['model']
132 else:
133 ifaceItem["vpci"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['vpci']
134 ifaceItem["mac"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['mac_address']
135 ifaceItem["bw"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['bw']
136 ifaceItem["model"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['model']
137 internalconnList.append(ifaceItem)
tiernof97fd272016-07-11 14:32:37 +0200138 #print "Internal net id in NFVO DB: %s" % net_id
tierno7edb6752016-03-21 17:37:52 +0100139
tiernof97fd272016-07-11 14:32:37 +0200140 #print "Adding internal interfaces to the NFVO database (if any)"
tierno7edb6752016-03-21 17:37:52 +0100141 for iface in internalconnList:
142 print "Iface name: %s" % iface['internal_name']
143 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200144 iface_id = self._new_row_internal('interfaces', iface, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
145 #print "Iface id in NFVO DB: %s" % iface_id
tierno7edb6752016-03-21 17:37:52 +0100146
tiernof97fd272016-07-11 14:32:37 +0200147 #print "Adding external interfaces to the NFVO database"
tierno7edb6752016-03-21 17:37:52 +0100148 for iface in vnf_descriptor['vnf']['external-connections']:
149 myIfaceDict = {}
150 #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])
151 myIfaceDict["internal_name"] = iface['local_iface_name']
152 #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])]
153 myIfaceDict["vm_id"] = vmDict[iface['VNFC']]
154 myIfaceDict["external_name"] = iface['name']
155 myIfaceDict["type"] = iface['type']
156 if iface["type"] == "data":
157 myIfaceDict["vpci"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci']
158 myIfaceDict["bw"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw']
159 myIfaceDict["model"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model']
160 else:
161 myIfaceDict["vpci"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci']
162 myIfaceDict["bw"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw']
163 myIfaceDict["model"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model']
164 myIfaceDict["mac"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['mac']
165 print "Iface name: %s" % iface['name']
166 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200167 iface_id = self._new_row_internal('interfaces', myIfaceDict, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
168 #print "Iface id in NFVO DB: %s" % iface_id
tierno7edb6752016-03-21 17:37:52 +0100169
tiernof97fd272016-07-11 14:32:37 +0200170 return vnf_id
tierno7edb6752016-03-21 17:37:52 +0100171
tiernof97fd272016-07-11 14:32:37 +0200172 except (mdb.Error, AttributeError) as e:
173 self._format_error(e, tries)
174 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100175
tierno7edb6752016-03-21 17:37:52 +0100176
177 def new_scenario(self, scenario_dict):
tiernof97fd272016-07-11 14:32:37 +0200178 tries = 2
179 while tries:
tierno7edb6752016-03-21 17:37:52 +0100180 created_time = time.time()
181 try:
182 with self.con:
183 self.cur = self.con.cursor()
184 tenant_id = scenario_dict.get('tenant_id')
185 #scenario
186 INSERT_={'tenant_id': tenant_id,
tiernof97fd272016-07-11 14:32:37 +0200187 'name': scenario_dict['name'],
188 'description': scenario_dict['description'],
189 'public': scenario_dict.get('public', "false")}
tierno7edb6752016-03-21 17:37:52 +0100190
tiernof97fd272016-07-11 14:32:37 +0200191 scenario_uuid = self._new_row_internal('scenarios', INSERT_, add_uuid=True, root_uuid=None, created_time=created_time)
tierno7edb6752016-03-21 17:37:52 +0100192 #sce_nets
193 for net in scenario_dict['nets'].values():
194 net_dict={'scenario_id': scenario_uuid}
195 net_dict["name"] = net["name"]
196 net_dict["type"] = net["type"]
197 net_dict["description"] = net.get("description")
198 net_dict["external"] = net.get("external", False)
199 if "graph" in net:
200 #net["graph"]=yaml.safe_dump(net["graph"],default_flow_style=True,width=256)
201 #TODO, must be json because of the GUI, change to yaml
202 net_dict["graph"]=json.dumps(net["graph"])
203 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200204 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 +0100205 net['uuid']=net_uuid
206 #sce_vnfs
207 for k,vnf in scenario_dict['vnfs'].items():
208 INSERT_={'scenario_id': scenario_uuid,
209 'name': k,
210 'vnf_id': vnf['uuid'],
211 #'description': scenario_dict['name']
212 'description': vnf['description']
213 }
214 if "graph" in vnf:
215 #INSERT_["graph"]=yaml.safe_dump(vnf["graph"],default_flow_style=True,width=256)
216 #TODO, must be json because of the GUI, change to yaml
217 INSERT_["graph"]=json.dumps(vnf["graph"])
218 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200219 scn_vnf_uuid = self._new_row_internal('sce_vnfs', INSERT_, add_uuid=True, root_uuid=scenario_uuid, created_time=created_time)
tierno7edb6752016-03-21 17:37:52 +0100220 vnf['scn_vnf_uuid']=scn_vnf_uuid
221 #sce_interfaces
222 for iface in vnf['ifaces'].values():
tiernof97fd272016-07-11 14:32:37 +0200223 #print 'iface', iface
tierno7edb6752016-03-21 17:37:52 +0100224 if 'net_key' not in iface:
225 continue
226 iface['net_id'] = scenario_dict['nets'][ iface['net_key'] ]['uuid']
227 INSERT_={'sce_vnf_id': scn_vnf_uuid,
228 'sce_net_id': iface['net_id'],
229 'interface_id': iface[ 'uuid' ]
230 }
231 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200232 iface_uuid = self._new_row_internal('sce_interfaces', INSERT_, add_uuid=True, root_uuid=scenario_uuid, created_time=created_time)
tierno7edb6752016-03-21 17:37:52 +0100233
tiernof97fd272016-07-11 14:32:37 +0200234 return scenario_uuid
tierno7edb6752016-03-21 17:37:52 +0100235
tiernof97fd272016-07-11 14:32:37 +0200236 except (mdb.Error, AttributeError) as e:
237 self._format_error(e, tries)
238 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100239
240 def edit_scenario(self, scenario_dict):
tiernof97fd272016-07-11 14:32:37 +0200241 tries = 2
242 while tries:
tierno7edb6752016-03-21 17:37:52 +0100243 modified_time = time.time()
tiernof97fd272016-07-11 14:32:37 +0200244 item_changed=0
tierno7edb6752016-03-21 17:37:52 +0100245 try:
246 with self.con:
247 self.cur = self.con.cursor()
248 #check that scenario exist
249 tenant_id = scenario_dict.get('tenant_id')
250 scenario_uuid = scenario_dict['uuid']
251
tiernof97fd272016-07-11 14:32:37 +0200252 where_text = "uuid='{}'".format(scenario_uuid)
tierno7edb6752016-03-21 17:37:52 +0100253 if not tenant_id and tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200254 where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id)
255 cmd = "SELECT * FROM scenarios WHERE "+ where_text
256 self.logger.debug(cmd)
257 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100258 self.cur.fetchall()
259 if self.cur.rowcount==0:
tiernof97fd272016-07-11 14:32:37 +0200260 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 +0100261 elif self.cur.rowcount>1:
tiernof97fd272016-07-11 14:32:37 +0200262 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 +0100263
264 #scenario
265 nodes = {}
266 topology = scenario_dict.pop("topology", None)
267 if topology != None and "nodes" in topology:
268 nodes = topology.get("nodes",{})
269 UPDATE_ = {}
270 if "name" in scenario_dict: UPDATE_["name"] = scenario_dict["name"]
271 if "description" in scenario_dict: UPDATE_["description"] = scenario_dict["description"]
272 if len(UPDATE_)>0:
273 WHERE_={'tenant_id': tenant_id, 'uuid': scenario_uuid}
tiernof97fd272016-07-11 14:32:37 +0200274 item_changed += self._update_rows('scenarios', UPDATE_, WHERE_, modified_time=modified_time)
tierno7edb6752016-03-21 17:37:52 +0100275 #sce_nets
276 for node_id, node in nodes.items():
277 if "graph" in node:
278 #node["graph"] = yaml.safe_dump(node["graph"],default_flow_style=True,width=256)
279 #TODO, must be json because of the GUI, change to yaml
280 node["graph"] = json.dumps(node["graph"])
281 WHERE_={'scenario_id': scenario_uuid, 'uuid': node_id}
tiernof97fd272016-07-11 14:32:37 +0200282 #Try to change at sce_nets(version 0 API backward compatibility and sce_vnfs)
283 item_changed += self._update_rows('sce_nets', node, WHERE_)
284 item_changed += self._update_rows('sce_vnfs', node, WHERE_, modified_time=modified_time)
285 return item_changed
tierno7edb6752016-03-21 17:37:52 +0100286
tiernof97fd272016-07-11 14:32:37 +0200287 except (mdb.Error, AttributeError) as e:
288 self._format_error(e, tries)
289 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100290
291# def get_instance_scenario(self, instance_scenario_id, tenant_id=None):
292# '''Obtain the scenario instance information, filtering by one or serveral of the tenant, uuid or name
293# instance_scenario_id is the uuid or the name if it is not a valid uuid format
294# Only one scenario isntance must mutch the filtering or an error is returned
295# '''
296# print "1******************************************************************"
297# try:
298# with self.con:
299# self.cur = self.con.cursor(mdb.cursors.DictCursor)
300# #scenario table
301# where_list=[]
302# if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" )
tiernof97fd272016-07-11 14:32:37 +0200303# if db_base._check_valid_uuid(instance_scenario_id):
tierno7edb6752016-03-21 17:37:52 +0100304# where_list.append( "uuid='" + instance_scenario_id +"'" )
305# else:
306# where_list.append( "name='" + instance_scenario_id +"'" )
307# where_text = " AND ".join(where_list)
308# self.cur.execute("SELECT * FROM instance_scenarios WHERE "+ where_text)
309# rows = self.cur.fetchall()
310# if self.cur.rowcount==0:
311# return -HTTP_Bad_Request, "No scenario instance found with this criteria " + where_text
312# elif self.cur.rowcount>1:
313# return -HTTP_Bad_Request, "More than one scenario instance found with this criteria " + where_text
314# instance_scenario_dict = rows[0]
315#
316# #instance_vnfs
317# self.cur.execute("SELECT uuid,vnf_id FROM instance_vnfs WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
318# instance_scenario_dict['instance_vnfs'] = self.cur.fetchall()
319# for vnf in instance_scenario_dict['instance_vnfs']:
320# #instance_vms
321# self.cur.execute("SELECT uuid, vim_vm_id "+
322# "FROM instance_vms "+
323# "WHERE instance_vnf_id='" + vnf['uuid'] +"'"
324# )
325# vnf['instance_vms'] = self.cur.fetchall()
326# #instance_nets
327# self.cur.execute("SELECT uuid, vim_net_id FROM instance_nets WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
328# instance_scenario_dict['instance_nets'] = self.cur.fetchall()
329#
330# #instance_interfaces
331# 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'] + "'")
332# instance_scenario_dict['instance_interfaces'] = self.cur.fetchall()
333#
tiernof97fd272016-07-11 14:32:37 +0200334# db_base._convert_datetime2str(instance_scenario_dict)
335# db_base._convert_str2boolean(instance_scenario_dict, ('public','shared','external') )
tierno7edb6752016-03-21 17:37:52 +0100336# print "2******************************************************************"
337# return 1, instance_scenario_dict
tiernof97fd272016-07-11 14:32:37 +0200338# except (mdb.Error, AttributeError) as e:
tierno7edb6752016-03-21 17:37:52 +0100339# print "nfvo_db.get_instance_scenario DB Exception %d: %s" % (e.args[0], e.args[1])
tiernof97fd272016-07-11 14:32:37 +0200340# return self._format_error(e)
tierno7edb6752016-03-21 17:37:52 +0100341
342 def get_scenario(self, scenario_id, tenant_id=None, datacenter_id=None):
343 '''Obtain the scenario information, filtering by one or serveral of the tenant, uuid or name
344 scenario_id is the uuid or the name if it is not a valid uuid format
345 if datacenter_id is provided, it supply aditional vim_id fields with the matching vim uuid
346 Only one scenario must mutch the filtering or an error is returned
347 '''
tiernof97fd272016-07-11 14:32:37 +0200348 tries = 2
349 while tries:
tierno7edb6752016-03-21 17:37:52 +0100350 try:
351 with self.con:
352 self.cur = self.con.cursor(mdb.cursors.DictCursor)
tiernof97fd272016-07-11 14:32:37 +0200353 where_text = "uuid='{}'".format(scenario_id)
tierno7edb6752016-03-21 17:37:52 +0100354 if not tenant_id and tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200355 where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id)
356 cmd = "SELECT * FROM scenarios WHERE " + where_text
357 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100358 self.cur.execute(cmd)
359 rows = self.cur.fetchall()
360 if self.cur.rowcount==0:
tiernof97fd272016-07-11 14:32:37 +0200361 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 +0100362 elif self.cur.rowcount>1:
tiernof97fd272016-07-11 14:32:37 +0200363 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 +0100364 scenario_dict = rows[0]
tiernoa4e1a6e2016-08-31 14:19:40 +0200365 if scenario_dict["cloud_config"]:
366 scenario_dict["cloud-config"] = yaml.load(scenario_dict["cloud_config"])
367 del scenario_dict["cloud_config"]
tierno7edb6752016-03-21 17:37:52 +0100368 #sce_vnfs
tiernof97fd272016-07-11 14:32:37 +0200369 cmd = "SELECT uuid,name,vnf_id,description FROM sce_vnfs WHERE scenario_id='{}' ORDER BY created_at".format(scenario_dict['uuid'])
370 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100371 self.cur.execute(cmd)
372 scenario_dict['vnfs'] = self.cur.fetchall()
373 for vnf in scenario_dict['vnfs']:
374 #sce_interfaces
tiernof97fd272016-07-11 14:32:37 +0200375 cmd = "SELECT uuid,sce_net_id,interface_id FROM sce_interfaces WHERE sce_vnf_id='{}' ORDER BY created_at".format(vnf['uuid'])
376 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100377 self.cur.execute(cmd)
378 vnf['interfaces'] = self.cur.fetchall()
379 #vms
380 cmd = "SELECT vms.uuid as uuid, flavor_id, image_id, vms.name as name, vms.description as description " \
381 " FROM vnfs join vms on vnfs.uuid=vms.vnf_id " \
382 " WHERE vnfs.uuid='" + vnf['vnf_id'] +"'" \
383 " ORDER BY vms.created_at"
tiernof97fd272016-07-11 14:32:37 +0200384 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100385 self.cur.execute(cmd)
386 vnf['vms'] = self.cur.fetchall()
387 for vm in vnf['vms']:
388 if datacenter_id!=None:
tiernof97fd272016-07-11 14:32:37 +0200389 cmd = "SELECT vim_id FROM datacenters_images WHERE image_id='{}' AND datacenter_id='{}'".format(vm['image_id'],datacenter_id)
390 self.logger.debug(cmd)
391 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100392 if self.cur.rowcount==1:
393 vim_image_dict = self.cur.fetchone()
394 vm['vim_image_id']=vim_image_dict['vim_id']
tiernof97fd272016-07-11 14:32:37 +0200395 cmd = "SELECT vim_id FROM datacenters_flavors WHERE flavor_id='{}' AND datacenter_id='{}'".format(vm['flavor_id'],datacenter_id)
396 self.logger.debug(cmd)
397 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100398 if self.cur.rowcount==1:
399 vim_flavor_dict = self.cur.fetchone()
400 vm['vim_flavor_id']=vim_flavor_dict['vim_id']
401
402 #interfaces
403 cmd = "SELECT uuid,internal_name,external_name,net_id,type,vpci,mac,bw,model" \
404 " FROM interfaces" \
tiernof97fd272016-07-11 14:32:37 +0200405 " WHERE vm_id='{}'" \
406 " ORDER BY created_at".format(vm['uuid'])
407 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100408 self.cur.execute(cmd)
409 vm['interfaces'] = self.cur.fetchall()
410 #nets every net of a vms
tiernof97fd272016-07-11 14:32:37 +0200411 cmd = "SELECT uuid,name,type,description FROM nets WHERE vnf_id='{}'".format(vnf['vnf_id'])
412 self.logger.debug(cmd)
413 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100414 vnf['nets'] = self.cur.fetchall()
415 #sce_nets
416 cmd = "SELECT uuid,name,type,external,description" \
tiernof97fd272016-07-11 14:32:37 +0200417 " FROM sce_nets WHERE scenario_id='{}'" \
418 " ORDER BY created_at ".format(scenario_dict['uuid'])
419 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100420 self.cur.execute(cmd)
421 scenario_dict['nets'] = self.cur.fetchall()
422 #datacenter_nets
423 for net in scenario_dict['nets']:
424 if str(net['external']) == 'false':
425 continue
tiernof97fd272016-07-11 14:32:37 +0200426 WHERE_=" WHERE name='{}'".format(net['name'])
tierno7edb6752016-03-21 17:37:52 +0100427 if datacenter_id!=None:
tiernof97fd272016-07-11 14:32:37 +0200428 WHERE_ += " AND datacenter_id='{}'".format(datacenter_id)
429 cmd = "SELECT vim_net_id FROM datacenter_nets" + WHERE_
430 self.logger.debug(cmd)
431 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100432 d_net = self.cur.fetchone()
433 if d_net==None or datacenter_id==None:
434 #print "nfvo_db.get_scenario() WARNING external net %s not found" % net['name']
435 net['vim_id']=None
436 else:
437 net['vim_id']=d_net['vim_net_id']
438
tiernof97fd272016-07-11 14:32:37 +0200439 db_base._convert_datetime2str(scenario_dict)
440 db_base._convert_str2boolean(scenario_dict, ('public','shared','external') )
441 return scenario_dict
442 except (mdb.Error, AttributeError) as e:
443 self._format_error(e, tries)
444 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100445
tierno7edb6752016-03-21 17:37:52 +0100446
447 def delete_scenario(self, scenario_id, tenant_id=None):
448 '''Deletes a scenario, filtering by one or several of the tenant, uuid or name
449 scenario_id is the uuid or the name if it is not a valid uuid format
450 Only one scenario must mutch the filtering or an error is returned
451 '''
tiernof97fd272016-07-11 14:32:37 +0200452 tries = 2
453 while tries:
tierno7edb6752016-03-21 17:37:52 +0100454 try:
455 with self.con:
456 self.cur = self.con.cursor(mdb.cursors.DictCursor)
457
458 #scenario table
tiernof97fd272016-07-11 14:32:37 +0200459 where_text = "uuid='{}'".format(scenario_id)
tierno7edb6752016-03-21 17:37:52 +0100460 if not tenant_id and tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200461 where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id)
462 cmd = "SELECT * FROM scenarios WHERE "+ where_text
463 self.logger.debug(cmd)
464 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100465 rows = self.cur.fetchall()
466 if self.cur.rowcount==0:
tiernof97fd272016-07-11 14:32:37 +0200467 raise db_base.db_base_Exception("No scenario found where " + where_text, db_base.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100468 elif self.cur.rowcount>1:
tiernof97fd272016-07-11 14:32:37 +0200469 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 +0100470 scenario_uuid = rows[0]["uuid"]
471 scenario_name = rows[0]["name"]
472
473 #sce_vnfs
tiernof97fd272016-07-11 14:32:37 +0200474 cmd = "DELETE FROM scenarios WHERE uuid='{}'".format(scenario_uuid)
475 self.logger.debug(cmd)
476 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100477
tiernof97fd272016-07-11 14:32:37 +0200478 return scenario_uuid + " " + scenario_name
479 except (mdb.Error, AttributeError) as e:
480 self._format_error(e, tries, "delete", "instances running")
481 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100482
483 def new_instance_scenario_as_a_whole(self,tenant_id,instance_scenario_name,instance_scenario_description,scenarioDict):
tiernof97fd272016-07-11 14:32:37 +0200484 tries = 2
485 while tries:
tierno7edb6752016-03-21 17:37:52 +0100486 created_time = time.time()
487 try:
488 with self.con:
489 self.cur = self.con.cursor()
490 #instance_scenarios
491 datacenter_tenant_id = scenarioDict['datacenter_tenant_id']
492 datacenter_id = scenarioDict['datacenter_id']
493 INSERT_={'tenant_id': tenant_id,
494 'datacenter_tenant_id': datacenter_tenant_id,
495 'name': instance_scenario_name,
496 'description': instance_scenario_description,
497 'scenario_id' : scenarioDict['uuid'],
498 'datacenter_id': datacenter_id
499 }
tiernoa4e1a6e2016-08-31 14:19:40 +0200500 if scenarioDict.get("cloud-config"):
501 INSERT_["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"], default_flow_style=True, width=256)
502
tiernof97fd272016-07-11 14:32:37 +0200503 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 +0100504
505 net_scene2instance={}
506 #instance_nets #nets interVNF
507 for net in scenarioDict['nets']:
508 INSERT_={'vim_net_id': net['vim_id'], 'external': net['external'], 'instance_scenario_id':instance_uuid } #, 'type': net['type']
509 INSERT_['datacenter_id'] = net.get('datacenter_id', datacenter_id)
510 INSERT_['datacenter_tenant_id'] = net.get('datacenter_tenant_id', datacenter_tenant_id)
511 if net.get("uuid"):
512 INSERT_['sce_net_id'] = net['uuid']
513 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200514 instance_net_uuid = self._new_row_internal('instance_nets', INSERT_, True, instance_uuid, created_time)
tierno7edb6752016-03-21 17:37:52 +0100515 net_scene2instance[ net['uuid'] ] = instance_net_uuid
516 net['uuid'] = instance_net_uuid #overwrite scnario uuid by instance uuid
517
518 #instance_vnfs
519 for vnf in scenarioDict['vnfs']:
520 INSERT_={'instance_scenario_id': instance_uuid, 'vnf_id': vnf['vnf_id'] }
521 INSERT_['datacenter_id'] = vnf.get('datacenter_id', datacenter_id)
522 INSERT_['datacenter_tenant_id'] = vnf.get('datacenter_tenant_id', datacenter_tenant_id)
523 if vnf.get("uuid"):
524 INSERT_['sce_vnf_id'] = vnf['uuid']
525 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200526 instance_vnf_uuid = self._new_row_internal('instance_vnfs', INSERT_, True, instance_uuid, created_time)
tierno7edb6752016-03-21 17:37:52 +0100527 vnf['uuid'] = instance_vnf_uuid #overwrite scnario uuid by instance uuid
528
529 #instance_nets #nets intraVNF
530 for net in vnf['nets']:
531 INSERT_={'vim_net_id': net['vim_id'], 'external': 'false', 'instance_scenario_id':instance_uuid } #, 'type': net['type']
532 INSERT_['datacenter_id'] = net.get('datacenter_id', datacenter_id)
533 INSERT_['datacenter_tenant_id'] = net.get('datacenter_tenant_id', datacenter_tenant_id)
534 if net.get("uuid"):
535 INSERT_['net_id'] = net['uuid']
536 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200537 instance_net_uuid = self._new_row_internal('instance_nets', INSERT_, True, instance_uuid, created_time)
tierno7edb6752016-03-21 17:37:52 +0100538 net_scene2instance[ net['uuid'] ] = instance_net_uuid
539 net['uuid'] = instance_net_uuid #overwrite scnario uuid by instance uuid
540
541 #instance_vms
542 for vm in vnf['vms']:
543 INSERT_={'instance_vnf_id': instance_vnf_uuid, 'vm_id': vm['uuid'], 'vim_vm_id': vm['vim_id'] }
544 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200545 instance_vm_uuid = self._new_row_internal('instance_vms', INSERT_, True, instance_uuid, created_time)
tierno7edb6752016-03-21 17:37:52 +0100546 vm['uuid'] = instance_vm_uuid #overwrite scnario uuid by instance uuid
547
548 #instance_interfaces
549 for interface in vm['interfaces']:
550 net_id = interface.get('net_id', None)
551 if net_id is None:
552 #check if is connected to a inter VNFs net
553 for iface in vnf['interfaces']:
554 if iface['interface_id'] == interface['uuid']:
555 net_id = iface.get('sce_net_id', None)
556 break
557 if net_id is None:
558 continue
559 interface_type='external' if interface['external_name'] is not None else 'internal'
560 INSERT_={'instance_vm_id': instance_vm_uuid, 'instance_net_id': net_scene2instance[net_id],
561 'interface_id': interface['uuid'], 'vim_interface_id': interface.get('vim_id'), 'type': interface_type }
562 #created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200563 interface_uuid = self._new_row_internal('instance_interfaces', INSERT_, True, instance_uuid) #, created_time)
tierno7edb6752016-03-21 17:37:52 +0100564 interface['uuid'] = interface_uuid #overwrite scnario uuid by instance uuid
tiernof97fd272016-07-11 14:32:37 +0200565 return instance_uuid
566 except (mdb.Error, AttributeError) as e:
567 self._format_error(e, tries)
568 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100569
570 def get_instance_scenario(self, instance_id, tenant_id=None, verbose=False):
571 '''Obtain the instance information, filtering by one or several of the tenant, uuid or name
572 instance_id is the uuid or the name if it is not a valid uuid format
573 Only one instance must mutch the filtering or an error is returned
574 '''
tiernof97fd272016-07-11 14:32:37 +0200575 tries = 2
576 while tries:
tierno7edb6752016-03-21 17:37:52 +0100577 try:
578 with self.con:
579 self.cur = self.con.cursor(mdb.cursors.DictCursor)
580 #instance table
581 where_list=[]
582 if tenant_id is not None: where_list.append( "inst.tenant_id='" + tenant_id +"'" )
tiernof97fd272016-07-11 14:32:37 +0200583 if db_base._check_valid_uuid(instance_id):
tierno7edb6752016-03-21 17:37:52 +0100584 where_list.append( "inst.uuid='" + instance_id +"'" )
585 else:
586 where_list.append( "inst.name='" + instance_id +"'" )
587 where_text = " AND ".join(where_list)
tiernof97fd272016-07-11 14:32:37 +0200588 cmd = "SELECT inst.uuid as uuid,inst.name as name,inst.scenario_id as scenario_id, datacenter_id" +\
tierno7edb6752016-03-21 17:37:52 +0100589 " ,datacenter_tenant_id, s.name as scenario_name,inst.tenant_id as tenant_id" + \
590 " ,inst.description as description,inst.created_at as created_at" +\
tiernoa4e1a6e2016-08-31 14:19:40 +0200591 " ,inst.cloud_config as 'cloud_config'" +\
tierno7edb6752016-03-21 17:37:52 +0100592 " FROM instance_scenarios as inst join scenarios as s on inst.scenario_id=s.uuid"+\
593 " WHERE " + where_text
tiernof97fd272016-07-11 14:32:37 +0200594 self.logger.debug(cmd)
595 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100596 rows = self.cur.fetchall()
tiernof97fd272016-07-11 14:32:37 +0200597
tierno7edb6752016-03-21 17:37:52 +0100598 if self.cur.rowcount==0:
tiernof97fd272016-07-11 14:32:37 +0200599 raise db_base.db_base_Exception("No instance found where " + where_text, db_base.HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +0100600 elif self.cur.rowcount>1:
tiernof97fd272016-07-11 14:32:37 +0200601 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 +0100602 instance_dict = rows[0]
tiernoa4e1a6e2016-08-31 14:19:40 +0200603 if instance_dict["cloud_config"]:
604 instance_dict["cloud-config"] = yaml.load(instance_dict["cloud_config"])
605 del instance_dict["cloud_config"]
tierno7edb6752016-03-21 17:37:52 +0100606
607 #instance_vnfs
608 cmd = "SELECT iv.uuid as uuid,sv.vnf_id as vnf_id,sv.name as vnf_name, sce_vnf_id, datacenter_id, datacenter_tenant_id"\
609 " FROM instance_vnfs as iv join sce_vnfs as sv on iv.sce_vnf_id=sv.uuid" \
tiernof97fd272016-07-11 14:32:37 +0200610 " WHERE iv.instance_scenario_id='{}'" \
611 " ORDER BY iv.created_at ".format(instance_dict['uuid'])
612 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100613 self.cur.execute(cmd)
614 instance_dict['vnfs'] = self.cur.fetchall()
615 for vnf in instance_dict['vnfs']:
616 vnf_manage_iface_list=[]
617 #instance vms
618 cmd = "SELECT iv.uuid as uuid, vim_vm_id, status, error_msg, vim_info, iv.created_at as created_at, name "\
619 " FROM instance_vms as iv join vms on iv.vm_id=vms.uuid "\
tiernof97fd272016-07-11 14:32:37 +0200620 " WHERE instance_vnf_id='{}' ORDER BY iv.created_at".format(vnf['uuid'])
621 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100622 self.cur.execute(cmd)
623 vnf['vms'] = self.cur.fetchall()
624 for vm in vnf['vms']:
625 vm_manage_iface_list=[]
626 #instance_interfaces
garciadeblas0c317ee2016-08-29 12:33:06 +0200627 cmd = "SELECT vim_interface_id, instance_net_id, internal_name,external_name, mac_address, ii.ip_address as ip_address, vim_info, i.type as type "\
tierno7edb6752016-03-21 17:37:52 +0100628 " FROM instance_interfaces as ii join interfaces as i on ii.interface_id=i.uuid "\
tiernof97fd272016-07-11 14:32:37 +0200629 " WHERE instance_vm_id='{}' ORDER BY created_at".format(vm['uuid'])
630 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100631 self.cur.execute(cmd )
632 vm['interfaces'] = self.cur.fetchall()
633 for iface in vm['interfaces']:
634 if iface["type"] == "mgmt" and iface["ip_address"]:
635 vnf_manage_iface_list.append(iface["ip_address"])
636 vm_manage_iface_list.append(iface["ip_address"])
637 if not verbose:
638 del iface["type"]
639 if vm_manage_iface_list: vm["ip_address"] = ",".join(vm_manage_iface_list)
640 if vnf_manage_iface_list: vnf["ip_address"] = ",".join(vnf_manage_iface_list)
641
642 #instance_nets
643 #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"
644 #from_text = "instance_nets join instance_scenarios on instance_nets.instance_scenario_id=instance_scenarios.uuid " + \
645 # "join sce_nets on instance_scenarios.scenario_id=sce_nets.scenario_id"
646 #where_text = "instance_nets.instance_scenario_id='"+ instance_dict['uuid'] + "'"
647 cmd = "SELECT uuid,vim_net_id,status,error_msg,vim_info,external, sce_net_id, net_id as vnf_net_id, datacenter_id, datacenter_tenant_id"\
648 " FROM instance_nets" \
tiernof97fd272016-07-11 14:32:37 +0200649 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict['uuid'])
650 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100651 self.cur.execute(cmd)
652 instance_dict['nets'] = self.cur.fetchall()
653
tiernof97fd272016-07-11 14:32:37 +0200654 db_base._convert_datetime2str(instance_dict)
655 db_base._convert_str2boolean(instance_dict, ('public','shared','external') )
656 return instance_dict
657 except (mdb.Error, AttributeError) as e:
658 self._format_error(e, tries)
659 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100660
661 def delete_instance_scenario(self, instance_id, tenant_id=None):
662 '''Deletes a instance_Scenario, filtering by one or serveral of the tenant, uuid or name
663 instance_id is the uuid or the name if it is not a valid uuid format
664 Only one instance_scenario must mutch the filtering or an error is returned
665 '''
tiernof97fd272016-07-11 14:32:37 +0200666 tries = 2
667 while tries:
tierno7edb6752016-03-21 17:37:52 +0100668 try:
669 with self.con:
670 self.cur = self.con.cursor(mdb.cursors.DictCursor)
671
672 #instance table
673 where_list=[]
674 if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" )
tiernof97fd272016-07-11 14:32:37 +0200675 if db_base._check_valid_uuid(instance_id):
tierno7edb6752016-03-21 17:37:52 +0100676 where_list.append( "uuid='" + instance_id +"'" )
677 else:
678 where_list.append( "name='" + instance_id +"'" )
679 where_text = " AND ".join(where_list)
tiernof97fd272016-07-11 14:32:37 +0200680 cmd = "SELECT * FROM instance_scenarios WHERE "+ where_text
681 self.logger.debug(cmd)
682 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100683 rows = self.cur.fetchall()
tiernof97fd272016-07-11 14:32:37 +0200684
tierno7edb6752016-03-21 17:37:52 +0100685 if self.cur.rowcount==0:
tiernof97fd272016-07-11 14:32:37 +0200686 raise db_base.db_base_Exception("No instance found where " + where_text, db_base.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100687 elif self.cur.rowcount>1:
tiernof97fd272016-07-11 14:32:37 +0200688 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 +0100689 instance_uuid = rows[0]["uuid"]
690 instance_name = rows[0]["name"]
691
692 #sce_vnfs
tiernof97fd272016-07-11 14:32:37 +0200693 cmd = "DELETE FROM instance_scenarios WHERE uuid='{}'".format(instance_uuid)
694 self.logger.debug(cmd)
695 self.cur.execute(cmd)
tierno7edb6752016-03-21 17:37:52 +0100696
tiernof97fd272016-07-11 14:32:37 +0200697 return instance_uuid + " " + instance_name
698 except (mdb.Error, AttributeError) as e:
699 self._format_error(e, tries, "delete", "No dependences can avoid deleting!!!!")
700 tries -= 1
tierno7edb6752016-03-21 17:37:52 +0100701
702 def new_instance_scenario(self, instance_scenario_dict, tenant_id):
703 #return self.new_row('vnfs', vnf_dict, None, tenant_id, True, True)
704 return self._new_row_internal('instance_scenarios', instance_scenario_dict, tenant_id, add_uuid=True, root_uuid=None, log=True)
705
706 def update_instance_scenario(self, instance_scenario_dict):
707 #TODO:
708 return
709
710 def new_instance_vnf(self, instance_vnf_dict, tenant_id, instance_scenario_id = None):
711 #return self.new_row('vms', vm_dict, tenant_id, True, True)
712 return self._new_row_internal('instance_vnfs', instance_vnf_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
713
714 def update_instance_vnf(self, instance_vnf_dict):
715 #TODO:
716 return
717
718 def delete_instance_vnf(self, instance_vnf_id):
719 #TODO:
720 return
721
722 def new_instance_vm(self, instance_vm_dict, tenant_id, instance_scenario_id = None):
723 #return self.new_row('vms', vm_dict, tenant_id, True, True)
724 return self._new_row_internal('instance_vms', instance_vm_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
725
726 def update_instance_vm(self, instance_vm_dict):
727 #TODO:
728 return
729
730 def delete_instance_vm(self, instance_vm_id):
731 #TODO:
732 return
733
734 def new_instance_net(self, instance_net_dict, tenant_id, instance_scenario_id = None):
735 return self._new_row_internal('instance_nets', instance_net_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
736
737 def update_instance_net(self, instance_net_dict):
738 #TODO:
739 return
740
741 def delete_instance_net(self, instance_net_id):
742 #TODO:
743 return
744
745 def new_instance_interface(self, instance_interface_dict, tenant_id, instance_scenario_id = None):
746 return self._new_row_internal('instance_interfaces', instance_interface_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
747
748 def update_instance_interface(self, instance_interface_dict):
749 #TODO:
750 return
751
752 def delete_instance_interface(self, instance_interface_dict):
753 #TODO:
754 return
755
756 def update_datacenter_nets(self, datacenter_id, new_net_list=[]):
757 ''' Removes the old and adds the new net list at datacenter list for one datacenter.
758 Attribute
759 datacenter_id: uuid of the datacenter to act upon
760 table: table where to insert
761 new_net_list: the new values to be inserted. If empty it only deletes the existing nets
762 Return: (Inserted items, Deleted items) if OK, (-Error, text) if error
763 '''
tiernof97fd272016-07-11 14:32:37 +0200764 tries = 2
765 while tries:
tierno7edb6752016-03-21 17:37:52 +0100766 created_time = time.time()
767 try:
768 with self.con:
769 self.cur = self.con.cursor()
tiernof97fd272016-07-11 14:32:37 +0200770 cmd="DELETE FROM datacenter_nets WHERE datacenter_id='{}'".format(datacenter_id)
771 self.logger.debug(cmd)
tierno7edb6752016-03-21 17:37:52 +0100772 self.cur.execute(cmd)
773 deleted = self.cur.rowcount
tiernof97fd272016-07-11 14:32:37 +0200774 inserted = 0
tierno7edb6752016-03-21 17:37:52 +0100775 for new_net in new_net_list:
776 created_time += 0.00001
tiernof97fd272016-07-11 14:32:37 +0200777 self._new_row_internal('datacenter_nets', new_net, add_uuid=True, created_time=created_time)
778 inserted += 1
779 return inserted, deleted
780 except (mdb.Error, AttributeError) as e:
781 self._format_error(e, tries)
782 tries -= 1
783
tierno7edb6752016-03-21 17:37:52 +0100784