1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
25 NFVO DB engine. It implements all the methods to interact with the Openmano Database
27 __author__
="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
28 __date__
="$28-aug-2014 10:05:01$"
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"]
41 class nfvo_db(db_base
.db_base
):
42 def __init__(self
, host
=None, user
=None, passwd
=None, database
=None, log_name
='openmano.db', log_level
=None):
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
47 def new_vnf_as_a_whole(self
,nfvo_tenant
,vnf_name
,vnf_descriptor
,VNFCDict
):
48 self
.logger
.debug("Adding new vnf to the NFVO database")
51 created_time
= time
.time()
56 myVNFDict
["name"] = vnf_name
57 myVNFDict
["descriptor"] = vnf_descriptor
['vnf'].get('descriptor')
58 myVNFDict
["public"] = vnf_descriptor
['vnf'].get('public', "false")
59 myVNFDict
["description"] = vnf_descriptor
['vnf']['description']
60 myVNFDict
["class"] = vnf_descriptor
['vnf'].get('class',"MISC")
61 myVNFDict
["tenant_id"] = vnf_descriptor
['vnf'].get("tenant_id")
63 vnf_id
= self
._new
_row
_internal
('vnfs', myVNFDict
, add_uuid
=True, root_uuid
=None, created_time
=created_time
)
64 #print "Adding new vms to the NFVO database"
65 #For each vm, we must create the appropriate vm in the NFVO database.
67 for _
,vm
in VNFCDict
.iteritems():
68 #This code could make the name of the vms grow and grow.
69 #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name
70 #vm['name'] = "%s-%s" % (vnf_name,vm['name'])
71 #print "VM name: %s. Description: %s" % (vm['name'], vm['description'])
73 created_time
+= 0.00001
74 vm_id
= self
._new
_row
_internal
('vms', vm
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
75 #print "Internal vm id in NFVO DB: %s" % vm_id
76 vmDict
[vm
['name']] = vm_id
78 #Collect the data interfaces of each VM/VNFC under the 'numas' field
80 for vm
in vnf_descriptor
['vnf']['VNFC']:
81 dataifacesDict
[vm
['name']] = {}
82 for numa
in vm
.get('numas', []):
83 for dataiface
in numa
.get('interfaces',[]):
84 db_base
._convert
_bandwidth
(dataiface
, logger
=self
.logger
)
85 dataifacesDict
[vm
['name']][dataiface
['name']] = {}
86 dataifacesDict
[vm
['name']][dataiface
['name']]['vpci'] = dataiface
['vpci']
87 dataifacesDict
[vm
['name']][dataiface
['name']]['bw'] = dataiface
['bandwidth']
88 dataifacesDict
[vm
['name']][dataiface
['name']]['model'] = "PF" if dataiface
['dedicated']=="yes" else ("VF" if dataiface
['dedicated']=="no" else "VFnotShared")
90 #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field
91 bridgeInterfacesDict
= {}
92 for vm
in vnf_descriptor
['vnf']['VNFC']:
93 if 'bridge-ifaces' in vm
:
94 bridgeInterfacesDict
[vm
['name']] = {}
95 for bridgeiface
in vm
['bridge-ifaces']:
96 if 'port-security' in bridgeiface
:
97 bridgeiface
['port_security'] = bridgeiface
.pop('port-security')
98 if 'floating-ip' in bridgeiface
:
99 bridgeiface
['floating_ip'] = bridgeiface
.pop('floating-ip')
100 db_base
._convert
_bandwidth
(bridgeiface
, logger
=self
.logger
)
101 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']] = {}
102 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['vpci'] = bridgeiface
.get('vpci',None)
103 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['mac'] = bridgeiface
.get('mac_address',None)
104 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['bw'] = bridgeiface
.get('bandwidth', None)
105 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['model'] = bridgeiface
.get('model', None)
106 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['port_security'] = \
107 int(bridgeiface
.get('port_security', True))
108 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['floating_ip'] = \
109 int(bridgeiface
.get('floating_ip', False))
111 #For each internal connection, we add it to the interfaceDict and we create the appropriate net in the NFVO database.
112 #print "Adding new nets (VNF internal nets) to the NFVO database (if any)"
113 internalconnList
= []
114 if 'internal-connections' in vnf_descriptor
['vnf']:
115 for net
in vnf_descriptor
['vnf']['internal-connections']:
116 #print "Net name: %s. Description: %s" % (net['name'], net['description'])
119 myNetDict
["name"] = net
['name']
120 myNetDict
["description"] = net
['description']
121 myNetDict
["type"] = net
['type']
122 myNetDict
["vnf_id"] = vnf_id
124 created_time
+= 0.00001
125 net_id
= self
._new
_row
_internal
('nets', myNetDict
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
127 for element
in net
['elements']:
129 #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])
130 ifaceItem
["internal_name"] = element
['local_iface_name']
131 #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])]
132 ifaceItem
["vm_id"] = vmDict
[element
['VNFC']]
133 ifaceItem
["net_id"] = net_id
134 ifaceItem
["type"] = net
['type']
135 if ifaceItem
["type"] == "data":
136 ifaceItem
["vpci"] = dataifacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['vpci']
137 ifaceItem
["bw"] = dataifacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['bw']
138 ifaceItem
["model"] = dataifacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['model']
140 ifaceItem
["vpci"] = bridgeInterfacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['vpci']
141 ifaceItem
["mac"] = bridgeInterfacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['mac_address']
142 ifaceItem
["bw"] = bridgeInterfacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['bw']
143 ifaceItem
["model"] = bridgeInterfacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['model']
144 ifaceItem
["port_security"] = \
145 bridgeInterfacesDict
[element
['VNFC']][element
['local_iface_name']]['port_security']
146 ifaceItem
["floating_ip"] = \
147 bridgeInterfacesDict
[element
['VNFC']][element
['local_iface_name']]['floating_ip']
148 internalconnList
.append(ifaceItem
)
149 #print "Internal net id in NFVO DB: %s" % net_id
151 #print "Adding internal interfaces to the NFVO database (if any)"
152 for iface
in internalconnList
:
153 print "Iface name: %s" % iface
['internal_name']
154 created_time
+= 0.00001
155 iface_id
= self
._new
_row
_internal
('interfaces', iface
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
156 #print "Iface id in NFVO DB: %s" % iface_id
158 #print "Adding external interfaces to the NFVO database"
159 for iface
in vnf_descriptor
['vnf']['external-connections']:
161 #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])
162 myIfaceDict
["internal_name"] = iface
['local_iface_name']
163 #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])]
164 myIfaceDict
["vm_id"] = vmDict
[iface
['VNFC']]
165 myIfaceDict
["external_name"] = iface
['name']
166 myIfaceDict
["type"] = iface
['type']
167 if iface
["type"] == "data":
168 myIfaceDict
["vpci"] = dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['vpci']
169 myIfaceDict
["bw"] = dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['bw']
170 myIfaceDict
["model"] = dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['model']
172 myIfaceDict
["vpci"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['vpci']
173 myIfaceDict
["bw"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['bw']
174 myIfaceDict
["model"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['model']
175 myIfaceDict
["mac"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['mac']
176 myIfaceDict
["port_security"] = \
177 bridgeInterfacesDict
[iface
['VNFC']][iface
['local_iface_name']]['port_security']
178 myIfaceDict
["floating_ip"] = \
179 bridgeInterfacesDict
[iface
['VNFC']][iface
['local_iface_name']]['floating_ip']
180 print "Iface name: %s" % iface
['name']
181 created_time
+= 0.00001
182 iface_id
= self
._new
_row
_internal
('interfaces', myIfaceDict
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
183 #print "Iface id in NFVO DB: %s" % iface_id
187 except (mdb
.Error
, AttributeError) as e
:
188 self
._format
_error
(e
, tries
)
191 def new_vnf_as_a_whole2(self
,nfvo_tenant
,vnf_name
,vnf_descriptor
,VNFCDict
):
192 self
.logger
.debug("Adding new vnf to the NFVO database")
195 created_time
= time
.time()
200 myVNFDict
["name"] = vnf_name
201 myVNFDict
["descriptor"] = vnf_descriptor
['vnf'].get('descriptor')
202 myVNFDict
["public"] = vnf_descriptor
['vnf'].get('public', "false")
203 myVNFDict
["description"] = vnf_descriptor
['vnf']['description']
204 myVNFDict
["class"] = vnf_descriptor
['vnf'].get('class',"MISC")
205 myVNFDict
["tenant_id"] = vnf_descriptor
['vnf'].get("tenant_id")
207 vnf_id
= self
._new
_row
_internal
('vnfs', myVNFDict
, add_uuid
=True, root_uuid
=None, created_time
=created_time
)
208 #print "Adding new vms to the NFVO database"
209 #For each vm, we must create the appropriate vm in the NFVO database.
211 for _
,vm
in VNFCDict
.iteritems():
212 #This code could make the name of the vms grow and grow.
213 #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name
214 #vm['name'] = "%s-%s" % (vnf_name,vm['name'])
215 #print "VM name: %s. Description: %s" % (vm['name'], vm['description'])
216 vm
["vnf_id"] = vnf_id
217 created_time
+= 0.00001
218 vm_id
= self
._new
_row
_internal
('vms', vm
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
219 #print "Internal vm id in NFVO DB: %s" % vm_id
220 vmDict
[vm
['name']] = vm_id
222 #Collect the data interfaces of each VM/VNFC under the 'numas' field
224 for vm
in vnf_descriptor
['vnf']['VNFC']:
225 dataifacesDict
[vm
['name']] = {}
226 for numa
in vm
.get('numas', []):
227 for dataiface
in numa
.get('interfaces',[]):
228 db_base
._convert
_bandwidth
(dataiface
, logger
=self
.logger
)
229 dataifacesDict
[vm
['name']][dataiface
['name']] = {}
230 dataifacesDict
[vm
['name']][dataiface
['name']]['vpci'] = dataiface
['vpci']
231 dataifacesDict
[vm
['name']][dataiface
['name']]['bw'] = dataiface
['bandwidth']
232 dataifacesDict
[vm
['name']][dataiface
['name']]['model'] = "PF" if dataiface
['dedicated']=="yes" else ("VF" if dataiface
['dedicated']=="no" else "VFnotShared")
234 #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field
235 bridgeInterfacesDict
= {}
236 for vm
in vnf_descriptor
['vnf']['VNFC']:
237 if 'bridge-ifaces' in vm
:
238 bridgeInterfacesDict
[vm
['name']] = {}
239 for bridgeiface
in vm
['bridge-ifaces']:
240 db_base
._convert
_bandwidth
(bridgeiface
, logger
=self
.logger
)
241 if 'port-security' in bridgeiface
:
242 bridgeiface
['port_security'] = bridgeiface
.pop('port-security')
243 if 'floating-ip' in bridgeiface
:
244 bridgeiface
['floating_ip'] = bridgeiface
.pop('floating-ip')
245 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']] = {}
246 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['vpci'] = bridgeiface
.get('vpci',None)
247 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['mac'] = bridgeiface
.get('mac_address',None)
248 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['bw'] = bridgeiface
.get('bandwidth', None)
249 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['model'] = bridgeiface
.get('model', None)
250 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['port_security'] = \
251 int(bridgeiface
.get('port_security', True))
252 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['floating_ip'] = \
253 int(bridgeiface
.get('floating_ip', False))
255 #For each internal connection, we add it to the interfaceDict and we create the appropriate net in the NFVO database.
256 #print "Adding new nets (VNF internal nets) to the NFVO database (if any)"
257 internalconnList
= []
258 if 'internal-connections' in vnf_descriptor
['vnf']:
259 for net
in vnf_descriptor
['vnf']['internal-connections']:
260 #print "Net name: %s. Description: %s" % (net['name'], net['description'])
263 myNetDict
["name"] = net
['name']
264 myNetDict
["description"] = net
['description']
265 if (net
["implementation"] == "overlay"):
266 net
["type"] = "bridge"
267 #It should give an error if the type is e-line. For the moment, we consider it as a bridge
268 elif (net
["implementation"] == "underlay"):
269 if (net
["type"] == "e-line"):
271 elif (net
["type"] == "e-lan"):
273 net
.pop("implementation")
274 myNetDict
["type"] = net
['type']
275 myNetDict
["vnf_id"] = vnf_id
277 created_time
+= 0.00001
278 net_id
= self
._new
_row
_internal
('nets', myNetDict
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
280 if "ip-profile" in net
:
281 ip_profile
= net
["ip-profile"]
283 myIPProfileDict
["net_id"] = net_id
284 myIPProfileDict
["ip_version"] = ip_profile
.get('ip-version',"IPv4")
285 myIPProfileDict
["subnet_address"] = ip_profile
.get('subnet-address',None)
286 myIPProfileDict
["gateway_address"] = ip_profile
.get('gateway-address',None)
287 myIPProfileDict
["dns_address"] = ip_profile
.get('dns-address',None)
288 if ("dhcp" in ip_profile
):
289 myIPProfileDict
["dhcp_enabled"] = ip_profile
["dhcp"].get('enabled',"true")
290 myIPProfileDict
["dhcp_start_address"] = ip_profile
["dhcp"].get('start-address',None)
291 myIPProfileDict
["dhcp_count"] = ip_profile
["dhcp"].get('count',None)
293 created_time
+= 0.00001
294 ip_profile_id
= self
._new
_row
_internal
('ip_profiles', myIPProfileDict
)
296 for element
in net
['elements']:
298 #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])
299 ifaceItem
["internal_name"] = element
['local_iface_name']
300 #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])]
301 ifaceItem
["vm_id"] = vmDict
[element
['VNFC']]
302 ifaceItem
["net_id"] = net_id
303 ifaceItem
["type"] = net
['type']
304 ifaceItem
["ip_address"] = element
.get('ip_address',None)
305 if ifaceItem
["type"] == "data":
306 ifaceItem
["vpci"] = dataifacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['vpci']
307 ifaceItem
["bw"] = dataifacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['bw']
308 ifaceItem
["model"] = dataifacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['model']
310 ifaceItem
["vpci"] = bridgeInterfacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['vpci']
311 ifaceItem
["mac"] = bridgeInterfacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['mac']
312 ifaceItem
["bw"] = bridgeInterfacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['bw']
313 ifaceItem
["model"] = bridgeInterfacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]['model']
314 ifaceItem
["port_security"] = \
315 bridgeInterfacesDict
[element
['VNFC']][element
['local_iface_name']]['port_security']
316 ifaceItem
["floating_ip"] = \
317 bridgeInterfacesDict
[element
['VNFC']][element
['local_iface_name']]['floating_ip']
318 internalconnList
.append(ifaceItem
)
319 #print "Internal net id in NFVO DB: %s" % net_id
321 #print "Adding internal interfaces to the NFVO database (if any)"
322 for iface
in internalconnList
:
323 print "Iface name: %s" % iface
['internal_name']
324 created_time
+= 0.00001
325 iface_id
= self
._new
_row
_internal
('interfaces', iface
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
326 #print "Iface id in NFVO DB: %s" % iface_id
328 #print "Adding external interfaces to the NFVO database"
329 for iface
in vnf_descriptor
['vnf']['external-connections']:
331 #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])
332 myIfaceDict
["internal_name"] = iface
['local_iface_name']
333 #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])]
334 myIfaceDict
["vm_id"] = vmDict
[iface
['VNFC']]
335 myIfaceDict
["external_name"] = iface
['name']
336 myIfaceDict
["type"] = iface
['type']
337 if iface
["type"] == "data":
338 myIfaceDict
["vpci"] = dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['vpci']
339 myIfaceDict
["bw"] = dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['bw']
340 myIfaceDict
["model"] = dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['model']
342 myIfaceDict
["vpci"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['vpci']
343 myIfaceDict
["bw"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['bw']
344 myIfaceDict
["model"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['model']
345 myIfaceDict
["mac"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['mac']
346 myIfaceDict
["port_security"] = \
347 bridgeInterfacesDict
[iface
['VNFC']][iface
['local_iface_name']]['port_security']
348 myIfaceDict
["floating_ip"] = \
349 bridgeInterfacesDict
[iface
['VNFC']][iface
['local_iface_name']]['floating_ip']
350 print "Iface name: %s" % iface
['name']
351 created_time
+= 0.00001
352 iface_id
= self
._new
_row
_internal
('interfaces', myIfaceDict
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
353 #print "Iface id in NFVO DB: %s" % iface_id
357 except (mdb
.Error
, AttributeError) as e
:
358 self
._format
_error
(e
, tries
)
359 # except KeyError as e2:
360 # exc_type, exc_obj, exc_tb = sys.exc_info()
361 # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
362 # self.logger.debug("Exception type: %s; Filename: %s; Line number: %s", exc_type, fname, exc_tb.tb_lineno)
366 def new_scenario(self
, scenario_dict
):
369 created_time
= time
.time()
372 self
.cur
= self
.con
.cursor()
373 tenant_id
= scenario_dict
.get('tenant_id')
375 INSERT_
={'tenant_id': tenant_id
,
376 'name': scenario_dict
['name'],
377 'description': scenario_dict
['description'],
378 'public': scenario_dict
.get('public', "false")}
380 scenario_uuid
= self
._new
_row
_internal
('scenarios', INSERT_
, add_uuid
=True, root_uuid
=None, created_time
=created_time
)
382 for net
in scenario_dict
['nets'].values():
383 net_dict
={'scenario_id': scenario_uuid
}
384 net_dict
["name"] = net
["name"]
385 net_dict
["type"] = net
["type"]
386 net_dict
["description"] = net
.get("description")
387 net_dict
["external"] = net
.get("external", False)
389 #net["graph"]=yaml.safe_dump(net["graph"],default_flow_style=True,width=256)
390 #TODO, must be json because of the GUI, change to yaml
391 net_dict
["graph"]=json
.dumps(net
["graph"])
392 created_time
+= 0.00001
393 net_uuid
= self
._new
_row
_internal
('sce_nets', net_dict
, add_uuid
=True, root_uuid
=scenario_uuid
, created_time
=created_time
)
396 for k
,vnf
in scenario_dict
['vnfs'].items():
397 INSERT_
={'scenario_id': scenario_uuid
,
399 'vnf_id': vnf
['uuid'],
400 #'description': scenario_dict['name']
401 'description': vnf
['description']
404 #INSERT_["graph"]=yaml.safe_dump(vnf["graph"],default_flow_style=True,width=256)
405 #TODO, must be json because of the GUI, change to yaml
406 INSERT_
["graph"]=json
.dumps(vnf
["graph"])
407 created_time
+= 0.00001
408 scn_vnf_uuid
= self
._new
_row
_internal
('sce_vnfs', INSERT_
, add_uuid
=True, root_uuid
=scenario_uuid
, created_time
=created_time
)
409 vnf
['scn_vnf_uuid']=scn_vnf_uuid
411 for iface
in vnf
['ifaces'].values():
412 #print 'iface', iface
413 if 'net_key' not in iface
:
415 iface
['net_id'] = scenario_dict
['nets'][ iface
['net_key'] ]['uuid']
416 INSERT_
={'sce_vnf_id': scn_vnf_uuid
,
417 'sce_net_id': iface
['net_id'],
418 'interface_id': iface
[ 'uuid' ]
420 created_time
+= 0.00001
421 iface_uuid
= self
._new
_row
_internal
('sce_interfaces', INSERT_
, add_uuid
=True, root_uuid
=scenario_uuid
, created_time
=created_time
)
425 except (mdb
.Error
, AttributeError) as e
:
426 self
._format
_error
(e
, tries
)
429 def new_scenario2(self
, scenario_dict
):
432 created_time
= time
.time()
435 self
.cur
= self
.con
.cursor()
436 tenant_id
= scenario_dict
.get('tenant_id')
438 INSERT_
={'tenant_id': tenant_id
,
439 'name': scenario_dict
['name'],
440 'description': scenario_dict
['description'],
441 'public': scenario_dict
.get('public', "false")}
443 scenario_uuid
= self
._new
_row
_internal
('scenarios', INSERT_
, add_uuid
=True, root_uuid
=None, created_time
=created_time
)
445 for net
in scenario_dict
['nets'].values():
446 net_dict
={'scenario_id': scenario_uuid
}
447 net_dict
["name"] = net
["name"]
448 net_dict
["type"] = net
["type"]
449 net_dict
["description"] = net
.get("description")
450 net_dict
["external"] = net
.get("external", False)
452 #net["graph"]=yaml.safe_dump(net["graph"],default_flow_style=True,width=256)
453 #TODO, must be json because of the GUI, change to yaml
454 net_dict
["graph"]=json
.dumps(net
["graph"])
455 created_time
+= 0.00001
456 net_uuid
= self
._new
_row
_internal
('sce_nets', net_dict
, add_uuid
=True, root_uuid
=scenario_uuid
, created_time
=created_time
)
459 if "ip-profile" in net
:
460 ip_profile
= net
["ip-profile"]
462 myIPProfileDict
["sce_net_id"] = net_uuid
463 myIPProfileDict
["ip_version"] = ip_profile
.get('ip-version',"IPv4")
464 myIPProfileDict
["subnet_address"] = ip_profile
.get('subnet-address',None)
465 myIPProfileDict
["gateway_address"] = ip_profile
.get('gateway-address',None)
466 myIPProfileDict
["dns_address"] = ip_profile
.get('dns-address',None)
467 if ("dhcp" in ip_profile
):
468 myIPProfileDict
["dhcp_enabled"] = ip_profile
["dhcp"].get('enabled',"true")
469 myIPProfileDict
["dhcp_start_address"] = ip_profile
["dhcp"].get('start-address',None)
470 myIPProfileDict
["dhcp_count"] = ip_profile
["dhcp"].get('count',None)
472 created_time
+= 0.00001
473 ip_profile_id
= self
._new
_row
_internal
('ip_profiles', myIPProfileDict
)
476 for k
,vnf
in scenario_dict
['vnfs'].items():
477 INSERT_
={'scenario_id': scenario_uuid
,
479 'vnf_id': vnf
['uuid'],
480 #'description': scenario_dict['name']
481 'description': vnf
['description']
484 #INSERT_["graph"]=yaml.safe_dump(vnf["graph"],default_flow_style=True,width=256)
485 #TODO, must be json because of the GUI, change to yaml
486 INSERT_
["graph"]=json
.dumps(vnf
["graph"])
487 created_time
+= 0.00001
488 scn_vnf_uuid
= self
._new
_row
_internal
('sce_vnfs', INSERT_
, add_uuid
=True, root_uuid
=scenario_uuid
, created_time
=created_time
)
489 vnf
['scn_vnf_uuid']=scn_vnf_uuid
491 for iface
in vnf
['ifaces'].values():
492 #print 'iface', iface
493 if 'net_key' not in iface
:
495 iface
['net_id'] = scenario_dict
['nets'][ iface
['net_key'] ]['uuid']
496 INSERT_
={'sce_vnf_id': scn_vnf_uuid
,
497 'sce_net_id': iface
['net_id'],
498 'interface_id': iface
['uuid'],
499 'ip_address': iface
['ip_address']
501 created_time
+= 0.00001
502 iface_uuid
= self
._new
_row
_internal
('sce_interfaces', INSERT_
, add_uuid
=True, root_uuid
=scenario_uuid
, created_time
=created_time
)
506 except (mdb
.Error
, AttributeError) as e
:
507 self
._format
_error
(e
, tries
)
508 # except KeyError as e2:
509 # exc_type, exc_obj, exc_tb = sys.exc_info()
510 # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
511 # self.logger.debug("Exception type: %s; Filename: %s; Line number: %s", exc_type, fname, exc_tb.tb_lineno)
515 def edit_scenario(self
, scenario_dict
):
518 modified_time
= time
.time()
522 self
.cur
= self
.con
.cursor()
523 #check that scenario exist
524 tenant_id
= scenario_dict
.get('tenant_id')
525 scenario_uuid
= scenario_dict
['uuid']
527 where_text
= "uuid='{}'".format(scenario_uuid
)
528 if not tenant_id
and tenant_id
!= "any":
529 where_text
+= " AND (tenant_id='{}' OR public='True')".format(tenant_id
)
530 cmd
= "SELECT * FROM scenarios WHERE "+ where_text
531 self
.logger
.debug(cmd
)
532 self
.cur
.execute(cmd
)
534 if self
.cur
.rowcount
==0:
535 raise db_base
.db_base_Exception("No scenario found with this criteria " + where_text
, db_base
.HTTP_Bad_Request
)
536 elif self
.cur
.rowcount
>1:
537 raise db_base
.db_base_Exception("More than one scenario found with this criteria " + where_text
, db_base
.HTTP_Bad_Request
)
541 topology
= scenario_dict
.pop("topology", None)
542 if topology
!= None and "nodes" in topology
:
543 nodes
= topology
.get("nodes",{})
545 if "name" in scenario_dict
: UPDATE_
["name"] = scenario_dict
["name"]
546 if "description" in scenario_dict
: UPDATE_
["description"] = scenario_dict
["description"]
548 WHERE_
={'tenant_id': tenant_id
, 'uuid': scenario_uuid
}
549 item_changed
+= self
._update
_rows
('scenarios', UPDATE_
, WHERE_
, modified_time
=modified_time
)
551 for node_id
, node
in nodes
.items():
553 #node["graph"] = yaml.safe_dump(node["graph"],default_flow_style=True,width=256)
554 #TODO, must be json because of the GUI, change to yaml
555 node
["graph"] = json
.dumps(node
["graph"])
556 WHERE_
={'scenario_id': scenario_uuid
, 'uuid': node_id
}
557 #Try to change at sce_nets(version 0 API backward compatibility and sce_vnfs)
558 item_changed
+= self
._update
_rows
('sce_nets', node
, WHERE_
)
559 item_changed
+= self
._update
_rows
('sce_vnfs', node
, WHERE_
, modified_time
=modified_time
)
562 except (mdb
.Error
, AttributeError) as e
:
563 self
._format
_error
(e
, tries
)
566 # def get_instance_scenario(self, instance_scenario_id, tenant_id=None):
567 # '''Obtain the scenario instance information, filtering by one or serveral of the tenant, uuid or name
568 # instance_scenario_id is the uuid or the name if it is not a valid uuid format
569 # Only one scenario isntance must mutch the filtering or an error is returned
571 # print "1******************************************************************"
574 # self.cur = self.con.cursor(mdb.cursors.DictCursor)
577 # if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" )
578 # if db_base._check_valid_uuid(instance_scenario_id):
579 # where_list.append( "uuid='" + instance_scenario_id +"'" )
581 # where_list.append( "name='" + instance_scenario_id +"'" )
582 # where_text = " AND ".join(where_list)
583 # self.cur.execute("SELECT * FROM instance_scenarios WHERE "+ where_text)
584 # rows = self.cur.fetchall()
585 # if self.cur.rowcount==0:
586 # return -HTTP_Bad_Request, "No scenario instance found with this criteria " + where_text
587 # elif self.cur.rowcount>1:
588 # return -HTTP_Bad_Request, "More than one scenario instance found with this criteria " + where_text
589 # instance_scenario_dict = rows[0]
592 # self.cur.execute("SELECT uuid,vnf_id FROM instance_vnfs WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
593 # instance_scenario_dict['instance_vnfs'] = self.cur.fetchall()
594 # for vnf in instance_scenario_dict['instance_vnfs']:
596 # self.cur.execute("SELECT uuid, vim_vm_id "+
597 # "FROM instance_vms "+
598 # "WHERE instance_vnf_id='" + vnf['uuid'] +"'"
600 # vnf['instance_vms'] = self.cur.fetchall()
602 # self.cur.execute("SELECT uuid, vim_net_id FROM instance_nets WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
603 # instance_scenario_dict['instance_nets'] = self.cur.fetchall()
605 # #instance_interfaces
606 # 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'] + "'")
607 # instance_scenario_dict['instance_interfaces'] = self.cur.fetchall()
609 # db_base._convert_datetime2str(instance_scenario_dict)
610 # db_base._convert_str2boolean(instance_scenario_dict, ('public','shared','external') )
611 # print "2******************************************************************"
612 # return 1, instance_scenario_dict
613 # except (mdb.Error, AttributeError) as e:
614 # print "nfvo_db.get_instance_scenario DB Exception %d: %s" % (e.args[0], e.args[1])
615 # return self._format_error(e)
617 def get_scenario(self
, scenario_id
, tenant_id
=None, datacenter_id
=None):
618 '''Obtain the scenario information, filtering by one or serveral of the tenant, uuid or name
619 scenario_id is the uuid or the name if it is not a valid uuid format
620 if datacenter_id is provided, it supply aditional vim_id fields with the matching vim uuid
621 Only one scenario must mutch the filtering or an error is returned
627 self
.cur
= self
.con
.cursor(mdb
.cursors
.DictCursor
)
628 where_text
= "uuid='{}'".format(scenario_id
)
629 if not tenant_id
and tenant_id
!= "any":
630 where_text
+= " AND (tenant_id='{}' OR public='True')".format(tenant_id
)
631 cmd
= "SELECT * FROM scenarios WHERE " + where_text
632 self
.logger
.debug(cmd
)
633 self
.cur
.execute(cmd
)
634 rows
= self
.cur
.fetchall()
635 if self
.cur
.rowcount
==0:
636 raise db_base
.db_base_Exception("No scenario found with this criteria " + where_text
, db_base
.HTTP_Bad_Request
)
637 elif self
.cur
.rowcount
>1:
638 raise db_base
.db_base_Exception("More than one scenario found with this criteria " + where_text
, db_base
.HTTP_Bad_Request
)
639 scenario_dict
= rows
[0]
640 if scenario_dict
["cloud_config"]:
641 scenario_dict
["cloud-config"] = yaml
.load(scenario_dict
["cloud_config"])
642 del scenario_dict
["cloud_config"]
644 cmd
= "SELECT uuid,name,vnf_id,description FROM sce_vnfs WHERE scenario_id='{}' ORDER BY created_at".format(scenario_dict
['uuid'])
645 self
.logger
.debug(cmd
)
646 self
.cur
.execute(cmd
)
647 scenario_dict
['vnfs'] = self
.cur
.fetchall()
648 for vnf
in scenario_dict
['vnfs']:
650 cmd
= "SELECT scei.uuid,scei.sce_net_id,scei.interface_id,i.external_name,scei.ip_address FROM sce_interfaces as scei join interfaces as i on scei.interface_id=i.uuid WHERE scei.sce_vnf_id='{}' ORDER BY scei.created_at".format(vnf
['uuid'])
651 self
.logger
.debug(cmd
)
652 self
.cur
.execute(cmd
)
653 vnf
['interfaces'] = self
.cur
.fetchall()
655 cmd
= "SELECT vms.uuid as uuid, flavor_id, image_id, vms.name as name, vms.description as description, vms.boot_data as boot_data " \
656 " FROM vnfs join vms on vnfs.uuid=vms.vnf_id " \
657 " WHERE vnfs.uuid='" + vnf
['vnf_id'] +"'" \
658 " ORDER BY vms.created_at"
659 self
.logger
.debug(cmd
)
660 self
.cur
.execute(cmd
)
661 vnf
['vms'] = self
.cur
.fetchall()
662 for vm
in vnf
['vms']:
664 vm
["boot_data"] = yaml
.safe_load(vm
["boot_data"])
667 if datacenter_id
!=None:
668 cmd
= "SELECT vim_id FROM datacenters_images WHERE image_id='{}' AND datacenter_id='{}'".format(vm
['image_id'],datacenter_id
)
669 self
.logger
.debug(cmd
)
670 self
.cur
.execute(cmd
)
671 if self
.cur
.rowcount
==1:
672 vim_image_dict
= self
.cur
.fetchone()
673 vm
['vim_image_id']=vim_image_dict
['vim_id']
674 cmd
= "SELECT vim_id FROM datacenters_flavors WHERE flavor_id='{}' AND datacenter_id='{}'".format(vm
['flavor_id'],datacenter_id
)
675 self
.logger
.debug(cmd
)
676 self
.cur
.execute(cmd
)
677 if self
.cur
.rowcount
==1:
678 vim_flavor_dict
= self
.cur
.fetchone()
679 vm
['vim_flavor_id']=vim_flavor_dict
['vim_id']
682 cmd
= "SELECT uuid,internal_name,external_name,net_id,type,vpci,mac,bw,model,ip_address," \
683 "floating_ip, port_security" \
685 " WHERE vm_id='{}'" \
686 " ORDER BY created_at".format(vm
['uuid'])
687 self
.logger
.debug(cmd
)
688 self
.cur
.execute(cmd
)
689 vm
['interfaces'] = self
.cur
.fetchall()
690 for index
in range(0,len(vm
['interfaces'])):
691 vm
['interfaces'][index
]['port-security'] = vm
['interfaces'][index
].pop("port_security")
692 vm
['interfaces'][index
]['floating-ip'] = vm
['interfaces'][index
].pop("floating_ip")
693 #nets every net of a vms
694 cmd
= "SELECT uuid,name,type,description FROM nets WHERE vnf_id='{}'".format(vnf
['vnf_id'])
695 self
.logger
.debug(cmd
)
696 self
.cur
.execute(cmd
)
697 vnf
['nets'] = self
.cur
.fetchall()
698 for vnf_net
in vnf
['nets']:
699 SELECT_
= "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count"
700 cmd
= "SELECT {} FROM ip_profiles WHERE net_id='{}'".format(SELECT_
,vnf_net
['uuid'])
701 self
.logger
.debug(cmd
)
702 self
.cur
.execute(cmd
)
703 ipprofiles
= self
.cur
.fetchall()
704 if self
.cur
.rowcount
==1:
705 vnf_net
["ip_profile"] = ipprofiles
[0]
706 elif self
.cur
.rowcount
>1:
707 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
)
710 cmd
= "SELECT uuid,name,type,external,description" \
711 " FROM sce_nets WHERE scenario_id='{}'" \
712 " ORDER BY created_at ".format(scenario_dict
['uuid'])
713 self
.logger
.debug(cmd
)
714 self
.cur
.execute(cmd
)
715 scenario_dict
['nets'] = self
.cur
.fetchall()
717 for net
in scenario_dict
['nets']:
718 if str(net
['external']) == 'false':
719 SELECT_
= "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count"
720 cmd
= "SELECT {} FROM ip_profiles WHERE sce_net_id='{}'".format(SELECT_
,net
['uuid'])
721 self
.logger
.debug(cmd
)
722 self
.cur
.execute(cmd
)
723 ipprofiles
= self
.cur
.fetchall()
724 if self
.cur
.rowcount
==1:
725 net
["ip_profile"] = ipprofiles
[0]
726 elif self
.cur
.rowcount
>1:
727 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
)
729 WHERE_
=" WHERE name='{}'".format(net
['name'])
730 if datacenter_id
!=None:
731 WHERE_
+= " AND datacenter_id='{}'".format(datacenter_id
)
732 cmd
= "SELECT vim_net_id FROM datacenter_nets" + WHERE_
733 self
.logger
.debug(cmd
)
734 self
.cur
.execute(cmd
)
735 d_net
= self
.cur
.fetchone()
736 if d_net
==None or datacenter_id
==None:
737 #print "nfvo_db.get_scenario() WARNING external net %s not found" % net['name']
740 net
['vim_id']=d_net
['vim_net_id']
742 db_base
._convert
_datetime
2str
(scenario_dict
)
743 db_base
._convert
_str
2boolean
(scenario_dict
, ('public','shared','external','port-security','floating-ip') )
745 except (mdb
.Error
, AttributeError) as e
:
746 self
._format
_error
(e
, tries
)
749 def delete_scenario(self
, scenario_id
, tenant_id
=None):
750 '''Deletes a scenario, filtering by one or several of the tenant, uuid or name
751 scenario_id is the uuid or the name if it is not a valid uuid format
752 Only one scenario must mutch the filtering or an error is returned
758 self
.cur
= self
.con
.cursor(mdb
.cursors
.DictCursor
)
761 where_text
= "uuid='{}'".format(scenario_id
)
762 if not tenant_id
and tenant_id
!= "any":
763 where_text
+= " AND (tenant_id='{}' OR public='True')".format(tenant_id
)
764 cmd
= "SELECT * FROM scenarios WHERE "+ where_text
765 self
.logger
.debug(cmd
)
766 self
.cur
.execute(cmd
)
767 rows
= self
.cur
.fetchall()
768 if self
.cur
.rowcount
==0:
769 raise db_base
.db_base_Exception("No scenario found where " + where_text
, db_base
.HTTP_Bad_Request
)
770 elif self
.cur
.rowcount
>1:
771 raise db_base
.db_base_Exception("More than one scenario found where " + where_text
, db_base
.HTTP_Bad_Request
)
772 scenario_uuid
= rows
[0]["uuid"]
773 scenario_name
= rows
[0]["name"]
776 cmd
= "DELETE FROM scenarios WHERE uuid='{}'".format(scenario_uuid
)
777 self
.logger
.debug(cmd
)
778 self
.cur
.execute(cmd
)
780 return scenario_uuid
+ " " + scenario_name
781 except (mdb
.Error
, AttributeError) as e
:
782 self
._format
_error
(e
, tries
, "delete", "instances running")
785 def new_instance_scenario_as_a_whole(self
,tenant_id
,instance_scenario_name
,instance_scenario_description
,scenarioDict
):
788 created_time
= time
.time()
791 self
.cur
= self
.con
.cursor()
793 datacenter_id
= scenarioDict
['datacenter_id']
794 INSERT_
={'tenant_id': tenant_id
,
795 'datacenter_tenant_id': scenarioDict
["datacenter2tenant"][datacenter_id
],
796 'name': instance_scenario_name
,
797 'description': instance_scenario_description
,
798 'scenario_id' : scenarioDict
['uuid'],
799 'datacenter_id': datacenter_id
801 if scenarioDict
.get("cloud-config"):
802 INSERT_
["cloud_config"] = yaml
.safe_dump(scenarioDict
["cloud-config"], default_flow_style
=True, width
=256)
804 instance_uuid
= self
._new
_row
_internal
('instance_scenarios', INSERT_
, add_uuid
=True, root_uuid
=None, created_time
=created_time
)
806 net_scene2instance
={}
807 #instance_nets #nets interVNF
808 for net
in scenarioDict
['nets']:
809 net_scene2instance
[ net
['uuid'] ] ={}
810 datacenter_site_id
= net
.get('datacenter_id', datacenter_id
)
811 if not "vim_id_sites" in net
:
812 net
["vim_id_sites"] ={datacenter_site_id
: net
['vim_id']}
813 net
["vim_id_sites"]["datacenter_site_id"] = {datacenter_site_id
: net
['vim_id']}
814 sce_net_id
= net
.get("uuid")
816 for datacenter_site_id
,vim_id
in net
["vim_id_sites"].iteritems():
817 INSERT_
={'vim_net_id': vim_id
, 'created': net
.get('created', False), 'instance_scenario_id':instance_uuid
} #, 'type': net['type']
818 INSERT_
['datacenter_id'] = datacenter_site_id
819 INSERT_
['datacenter_tenant_id'] = scenarioDict
["datacenter2tenant"][datacenter_site_id
]
821 INSERT_
['sce_net_id'] = sce_net_id
822 created_time
+= 0.00001
823 instance_net_uuid
= self
._new
_row
_internal
('instance_nets', INSERT_
, True, instance_uuid
, created_time
)
824 net_scene2instance
[ sce_net_id
][datacenter_site_id
] = instance_net_uuid
825 net
['uuid'] = instance_net_uuid
#overwrite scnario uuid by instance uuid
827 if 'ip_profile' in net
:
828 net
['ip_profile']['net_id'] = None
829 net
['ip_profile']['sce_net_id'] = None
830 net
['ip_profile']['instance_net_id'] = instance_net_uuid
831 created_time
+= 0.00001
832 ip_profile_id
= self
._new
_row
_internal
('ip_profiles', net
['ip_profile'])
835 for vnf
in scenarioDict
['vnfs']:
836 datacenter_site_id
= vnf
.get('datacenter_id', datacenter_id
)
837 INSERT_
={'instance_scenario_id': instance_uuid
, 'vnf_id': vnf
['vnf_id'] }
838 INSERT_
['datacenter_id'] = datacenter_site_id
839 INSERT_
['datacenter_tenant_id'] = scenarioDict
["datacenter2tenant"][datacenter_site_id
]
841 INSERT_
['sce_vnf_id'] = vnf
['uuid']
842 created_time
+= 0.00001
843 instance_vnf_uuid
= self
._new
_row
_internal
('instance_vnfs', INSERT_
, True, instance_uuid
, created_time
)
844 vnf
['uuid'] = instance_vnf_uuid
#overwrite scnario uuid by instance uuid
846 #instance_nets #nets intraVNF
847 for net
in vnf
['nets']:
848 net_scene2instance
[ net
['uuid'] ] = {}
849 INSERT_
={'vim_net_id': net
['vim_id'], 'created': net
.get('created', False), 'instance_scenario_id':instance_uuid
} #, 'type': net['type']
850 INSERT_
['datacenter_id'] = net
.get('datacenter_id', datacenter_site_id
)
851 INSERT_
['datacenter_tenant_id'] = scenarioDict
["datacenter2tenant"][datacenter_id
]
853 INSERT_
['net_id'] = net
['uuid']
854 created_time
+= 0.00001
855 instance_net_uuid
= self
._new
_row
_internal
('instance_nets', INSERT_
, True, instance_uuid
, created_time
)
856 net_scene2instance
[ net
['uuid'] ][datacenter_site_id
] = instance_net_uuid
857 net
['uuid'] = instance_net_uuid
#overwrite scnario uuid by instance uuid
859 if 'ip_profile' in net
:
860 net
['ip_profile']['net_id'] = None
861 net
['ip_profile']['sce_net_id'] = None
862 net
['ip_profile']['instance_net_id'] = instance_net_uuid
863 created_time
+= 0.00001
864 ip_profile_id
= self
._new
_row
_internal
('ip_profiles', net
['ip_profile'])
867 for vm
in vnf
['vms']:
868 INSERT_
={'instance_vnf_id': instance_vnf_uuid
, 'vm_id': vm
['uuid'], 'vim_vm_id': vm
['vim_id'] }
869 created_time
+= 0.00001
870 instance_vm_uuid
= self
._new
_row
_internal
('instance_vms', INSERT_
, True, instance_uuid
, created_time
)
871 vm
['uuid'] = instance_vm_uuid
#overwrite scnario uuid by instance uuid
874 for interface
in vm
['interfaces']:
875 net_id
= interface
.get('net_id', None)
877 #check if is connected to a inter VNFs net
878 for iface
in vnf
['interfaces']:
879 if iface
['interface_id'] == interface
['uuid']:
880 if 'ip_address' in iface
:
881 interface
['ip_address'] = iface
['ip_address']
882 net_id
= iface
.get('sce_net_id', None)
886 interface_type
='external' if interface
['external_name'] is not None else 'internal'
887 INSERT_
={'instance_vm_id': instance_vm_uuid
, 'instance_net_id': net_scene2instance
[net_id
][datacenter_site_id
],
888 'interface_id': interface
['uuid'], 'vim_interface_id': interface
.get('vim_id'), 'type': interface_type
,
889 'ip_address': interface
.get('ip_address'), 'floating_ip': int(interface
.get('floating-ip',False)),
890 'port_security': int(interface
.get('port-security',True))}
891 #created_time += 0.00001
892 interface_uuid
= self
._new
_row
_internal
('instance_interfaces', INSERT_
, True, instance_uuid
) #, created_time)
893 interface
['uuid'] = interface_uuid
#overwrite scnario uuid by instance uuid
895 except (mdb
.Error
, AttributeError) as e
:
896 self
._format
_error
(e
, tries
)
899 def get_instance_scenario(self
, instance_id
, tenant_id
=None, verbose
=False):
900 '''Obtain the instance information, filtering by one or several of the tenant, uuid or name
901 instance_id is the uuid or the name if it is not a valid uuid format
902 Only one instance must mutch the filtering or an error is returned
908 self
.cur
= self
.con
.cursor(mdb
.cursors
.DictCursor
)
911 if tenant_id
is not None: where_list
.append( "inst.tenant_id='" + tenant_id
+"'" )
912 if db_base
._check
_valid
_uuid
(instance_id
):
913 where_list
.append( "inst.uuid='" + instance_id
+"'" )
915 where_list
.append( "inst.name='" + 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 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()
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
, db_base
.HTTP_Bad_Request
)
931 instance_dict
= rows
[0]
932 if instance_dict
["cloud_config"]:
933 instance_dict
["cloud-config"] = yaml
.load(instance_dict
["cloud_config"])
934 del instance_dict
["cloud_config"]
937 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"\
938 " FROM instance_vnfs as iv join sce_vnfs as sv on iv.sce_vnf_id=sv.uuid" \
939 " WHERE iv.instance_scenario_id='{}'" \
940 " ORDER BY iv.created_at ".format(instance_dict
['uuid'])
941 self
.logger
.debug(cmd
)
942 self
.cur
.execute(cmd
)
943 instance_dict
['vnfs'] = self
.cur
.fetchall()
944 for vnf
in instance_dict
['vnfs']:
945 vnf_manage_iface_list
=[]
947 cmd
= "SELECT iv.uuid as uuid, vim_vm_id, status, error_msg, vim_info, iv.created_at as created_at, name "\
948 " FROM instance_vms as iv join vms on iv.vm_id=vms.uuid "\
949 " WHERE instance_vnf_id='{}' ORDER BY iv.created_at".format(vnf
['uuid'])
950 self
.logger
.debug(cmd
)
951 self
.cur
.execute(cmd
)
952 vnf
['vms'] = self
.cur
.fetchall()
953 for vm
in vnf
['vms']:
954 vm_manage_iface_list
=[]
956 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 "\
957 " FROM instance_interfaces as ii join interfaces as i on ii.interface_id=i.uuid "\
958 " WHERE instance_vm_id='{}' ORDER BY created_at".format(vm
['uuid'])
959 self
.logger
.debug(cmd
)
960 self
.cur
.execute(cmd
)
961 vm
['interfaces'] = self
.cur
.fetchall()
962 for iface
in vm
['interfaces']:
963 if iface
["type"] == "mgmt" and iface
["ip_address"]:
964 vnf_manage_iface_list
.append(iface
["ip_address"])
965 vm_manage_iface_list
.append(iface
["ip_address"])
968 if vm_manage_iface_list
: vm
["ip_address"] = ",".join(vm_manage_iface_list
)
969 if vnf_manage_iface_list
: vnf
["ip_address"] = ",".join(vnf_manage_iface_list
)
972 #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"
973 #from_text = "instance_nets join instance_scenarios on instance_nets.instance_scenario_id=instance_scenarios.uuid " + \
974 # "join sce_nets on instance_scenarios.scenario_id=sce_nets.scenario_id"
975 #where_text = "instance_nets.instance_scenario_id='"+ instance_dict['uuid'] + "'"
976 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"\
977 " FROM instance_nets" \
978 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict
['uuid'])
979 self
.logger
.debug(cmd
)
980 self
.cur
.execute(cmd
)
981 instance_dict
['nets'] = self
.cur
.fetchall()
983 db_base
._convert
_datetime
2str
(instance_dict
)
984 db_base
._convert
_str
2boolean
(instance_dict
, ('public','shared','created') )
986 except (mdb
.Error
, AttributeError) as e
:
987 self
._format
_error
(e
, tries
)
990 def delete_instance_scenario(self
, instance_id
, tenant_id
=None):
991 '''Deletes a instance_Scenario, filtering by one or serveral of the tenant, uuid or name
992 instance_id is the uuid or the name if it is not a valid uuid format
993 Only one instance_scenario must mutch the filtering or an error is returned
999 self
.cur
= self
.con
.cursor(mdb
.cursors
.DictCursor
)
1003 if tenant_id
is not None: where_list
.append( "tenant_id='" + tenant_id
+"'" )
1004 if db_base
._check
_valid
_uuid
(instance_id
):
1005 where_list
.append( "uuid='" + instance_id
+"'" )
1007 where_list
.append( "name='" + instance_id
+"'" )
1008 where_text
= " AND ".join(where_list
)
1009 cmd
= "SELECT * FROM instance_scenarios WHERE "+ where_text
1010 self
.logger
.debug(cmd
)
1011 self
.cur
.execute(cmd
)
1012 rows
= self
.cur
.fetchall()
1014 if self
.cur
.rowcount
==0:
1015 raise db_base
.db_base_Exception("No instance found where " + where_text
, db_base
.HTTP_Bad_Request
)
1016 elif self
.cur
.rowcount
>1:
1017 raise db_base
.db_base_Exception("More than one instance found where " + where_text
, db_base
.HTTP_Bad_Request
)
1018 instance_uuid
= rows
[0]["uuid"]
1019 instance_name
= rows
[0]["name"]
1022 cmd
= "DELETE FROM instance_scenarios WHERE uuid='{}'".format(instance_uuid
)
1023 self
.logger
.debug(cmd
)
1024 self
.cur
.execute(cmd
)
1026 return instance_uuid
+ " " + instance_name
1027 except (mdb
.Error
, AttributeError) as e
:
1028 self
._format
_error
(e
, tries
, "delete", "No dependences can avoid deleting!!!!")
1031 def new_instance_scenario(self
, instance_scenario_dict
, tenant_id
):
1032 #return self.new_row('vnfs', vnf_dict, None, tenant_id, True, True)
1033 return self
._new
_row
_internal
('instance_scenarios', instance_scenario_dict
, tenant_id
, add_uuid
=True, root_uuid
=None, log
=True)
1035 def update_instance_scenario(self
, instance_scenario_dict
):
1039 def new_instance_vnf(self
, instance_vnf_dict
, tenant_id
, instance_scenario_id
= None):
1040 #return self.new_row('vms', vm_dict, tenant_id, True, True)
1041 return self
._new
_row
_internal
('instance_vnfs', instance_vnf_dict
, tenant_id
, add_uuid
=True, root_uuid
=instance_scenario_id
, log
=True)
1043 def update_instance_vnf(self
, instance_vnf_dict
):
1047 def delete_instance_vnf(self
, instance_vnf_id
):
1051 def new_instance_vm(self
, instance_vm_dict
, tenant_id
, instance_scenario_id
= None):
1052 #return self.new_row('vms', vm_dict, tenant_id, True, True)
1053 return self
._new
_row
_internal
('instance_vms', instance_vm_dict
, tenant_id
, add_uuid
=True, root_uuid
=instance_scenario_id
, log
=True)
1055 def update_instance_vm(self
, instance_vm_dict
):
1059 def delete_instance_vm(self
, instance_vm_id
):
1063 def new_instance_net(self
, instance_net_dict
, tenant_id
, instance_scenario_id
= None):
1064 return self
._new
_row
_internal
('instance_nets', instance_net_dict
, tenant_id
, add_uuid
=True, root_uuid
=instance_scenario_id
, log
=True)
1066 def update_instance_net(self
, instance_net_dict
):
1070 def delete_instance_net(self
, instance_net_id
):
1074 def new_instance_interface(self
, instance_interface_dict
, tenant_id
, instance_scenario_id
= None):
1075 return self
._new
_row
_internal
('instance_interfaces', instance_interface_dict
, tenant_id
, add_uuid
=True, root_uuid
=instance_scenario_id
, log
=True)
1077 def update_instance_interface(self
, instance_interface_dict
):
1081 def delete_instance_interface(self
, instance_interface_dict
):
1085 def update_datacenter_nets(self
, datacenter_id
, new_net_list
=[]):
1086 ''' Removes the old and adds the new net list at datacenter list for one datacenter.
1088 datacenter_id: uuid of the datacenter to act upon
1089 table: table where to insert
1090 new_net_list: the new values to be inserted. If empty it only deletes the existing nets
1091 Return: (Inserted items, Deleted items) if OK, (-Error, text) if error
1095 created_time
= time
.time()
1098 self
.cur
= self
.con
.cursor()
1099 cmd
="DELETE FROM datacenter_nets WHERE datacenter_id='{}'".format(datacenter_id
)
1100 self
.logger
.debug(cmd
)
1101 self
.cur
.execute(cmd
)
1102 deleted
= self
.cur
.rowcount
1104 for new_net
in new_net_list
:
1105 created_time
+= 0.00001
1106 self
._new
_row
_internal
('datacenter_nets', new_net
, add_uuid
=True, created_time
=created_time
)
1108 return inserted
, deleted
1109 except (mdb
.Error
, AttributeError) as e
:
1110 self
._format
_error
(e
, tries
)