1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefonica Investigacion 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 from .http_tools
import errors
as httperrors
39 tables_with_createdat_field
=["datacenters","instance_nets","instance_scenarios","instance_vms","instance_vnfs",
40 "interfaces","nets","nfvo_tenants","scenarios","sce_interfaces","sce_nets",
41 "sce_vnfs","tenants_datacenters","datacenter_tenants","vms","vnfs", "datacenter_nets",
42 "instance_actions", "sce_vnffgs", "sce_rsps", "sce_rsp_hops",
43 "sce_classifiers", "sce_classifier_matches", "instance_sfis", "instance_sfs",
44 "instance_classifications", "instance_sfps", "wims", "wim_accounts", "wim_nfvo_tenants",
45 "wim_port_mappings", "vim_wim_actions",
49 class nfvo_db(db_base
.db_base
):
50 def __init__(self
, host
=None, user
=None, passwd
=None, database
=None, log_name
='openmano.db', log_level
=None):
51 db_base
.db_base
.__init
__(self
, host
, user
, passwd
, database
, log_name
, log_level
)
52 db_base
.db_base
.tables_with_created_field
=tables_with_createdat_field
55 def new_vnf_as_a_whole(self
,nfvo_tenant
,vnf_name
,vnf_descriptor
,VNFCDict
):
56 self
.logger
.debug("Adding new vnf to the NFVO database")
59 created_time
= time
.time()
64 myVNFDict
["name"] = vnf_name
65 myVNFDict
["descriptor"] = vnf_descriptor
['vnf'].get('descriptor')
66 myVNFDict
["public"] = vnf_descriptor
['vnf'].get('public', "false")
67 myVNFDict
["description"] = vnf_descriptor
['vnf']['description']
68 myVNFDict
["class"] = vnf_descriptor
['vnf'].get('class',"MISC")
69 myVNFDict
["tenant_id"] = vnf_descriptor
['vnf'].get("tenant_id")
71 vnf_id
= self
._new
_row
_internal
('vnfs', myVNFDict
, add_uuid
=True, root_uuid
=None, created_time
=created_time
)
72 #print "Adding new vms to the NFVO database"
73 #For each vm, we must create the appropriate vm in the NFVO database.
75 for _
,vm
in VNFCDict
.iteritems():
76 #This code could make the name of the vms grow and grow.
77 #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name
78 #vm['name'] = "%s-%s" % (vnf_name,vm['name'])
79 #print "VM name: %s. Description: %s" % (vm['name'], vm['description'])
81 created_time
+= 0.00001
82 vm_id
= self
._new
_row
_internal
('vms', vm
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
83 #print "Internal vm id in NFVO DB: %s" % vm_id
84 vmDict
[vm
['name']] = vm_id
86 #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field
87 bridgeInterfacesDict
= {}
88 for vm
in vnf_descriptor
['vnf']['VNFC']:
89 if 'bridge-ifaces' in vm
:
90 bridgeInterfacesDict
[vm
['name']] = {}
91 for bridgeiface
in vm
['bridge-ifaces']:
92 created_time
+= 0.00001
93 if 'port-security' in bridgeiface
:
94 bridgeiface
['port_security'] = bridgeiface
.pop('port-security')
95 if 'floating-ip' in bridgeiface
:
96 bridgeiface
['floating_ip'] = bridgeiface
.pop('floating-ip')
97 db_base
._convert
_bandwidth
(bridgeiface
, logger
=self
.logger
)
98 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 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['port_security'] = \
104 int(bridgeiface
.get('port_security', True))
105 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['floating_ip'] = \
106 int(bridgeiface
.get('floating_ip', False))
107 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['created_time'] = created_time
109 # Collect the data interfaces of each VM/VNFC under the 'numas' field
111 for vm
in vnf_descriptor
['vnf']['VNFC']:
112 dataifacesDict
[vm
['name']] = {}
113 for numa
in vm
.get('numas', []):
114 for dataiface
in numa
.get('interfaces', []):
115 created_time
+= 0.00001
116 db_base
._convert
_bandwidth
(dataiface
, logger
=self
.logger
)
117 dataifacesDict
[vm
['name']][dataiface
['name']] = {}
118 dataifacesDict
[vm
['name']][dataiface
['name']]['vpci'] = dataiface
.get('vpci')
119 dataifacesDict
[vm
['name']][dataiface
['name']]['bw'] = dataiface
['bandwidth']
120 dataifacesDict
[vm
['name']][dataiface
['name']]['model'] = "PF" if dataiface
[
121 'dedicated'] == "yes" else (
122 "VF" if dataiface
['dedicated'] == "no" else "VFnotShared")
123 dataifacesDict
[vm
['name']][dataiface
['name']]['created_time'] = created_time
125 #For each internal connection, we add it to the interfaceDict and we create the appropriate net in the NFVO database.
126 #print "Adding new nets (VNF internal nets) to the NFVO database (if any)"
127 internalconnList
= []
128 if 'internal-connections' in vnf_descriptor
['vnf']:
129 for net
in vnf_descriptor
['vnf']['internal-connections']:
130 #print "Net name: %s. Description: %s" % (net['name'], net['description'])
133 myNetDict
["name"] = net
['name']
134 myNetDict
["description"] = net
['description']
135 myNetDict
["type"] = net
['type']
136 myNetDict
["vnf_id"] = vnf_id
138 created_time
+= 0.00001
139 net_id
= self
._new
_row
_internal
('nets', myNetDict
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
141 for element
in net
['elements']:
143 #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])
144 ifaceItem
["internal_name"] = element
['local_iface_name']
145 #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])]
146 ifaceItem
["vm_id"] = vmDict
[element
['VNFC']]
147 ifaceItem
["net_id"] = net_id
148 ifaceItem
["type"] = net
['type']
149 if ifaceItem
["type"] == "data":
150 dataiface
= dataifacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]
151 ifaceItem
["vpci"] = dataiface
['vpci']
152 ifaceItem
["bw"] = dataiface
['bw']
153 ifaceItem
["model"] = dataiface
['model']
154 created_time_iface
= dataiface
['created_time']
156 bridgeiface
= bridgeInterfacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]
157 ifaceItem
["vpci"] = bridgeiface
['vpci']
158 ifaceItem
["mac"] = bridgeiface
['mac']
159 ifaceItem
["bw"] = bridgeiface
['bw']
160 ifaceItem
["model"] = bridgeiface
['model']
161 ifaceItem
["port_security"] = bridgeiface
['port_security']
162 ifaceItem
["floating_ip"] = bridgeiface
['floating_ip']
163 created_time_iface
= bridgeiface
['created_time']
164 internalconnList
.append(ifaceItem
)
165 #print "Internal net id in NFVO DB: %s" % net_id
167 #print "Adding internal interfaces to the NFVO database (if any)"
168 for iface
in internalconnList
:
169 #print "Iface name: %s" % iface['internal_name']
170 iface_id
= self
._new
_row
_internal
('interfaces', iface
, add_uuid
=True, root_uuid
=vnf_id
, created_time
= created_time_iface
)
171 #print "Iface id in NFVO DB: %s" % iface_id
173 #print "Adding external interfaces to the NFVO database"
174 for iface
in vnf_descriptor
['vnf']['external-connections']:
176 #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])
177 myIfaceDict
["internal_name"] = iface
['local_iface_name']
178 #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])]
179 myIfaceDict
["vm_id"] = vmDict
[iface
['VNFC']]
180 myIfaceDict
["external_name"] = iface
['name']
181 myIfaceDict
["type"] = iface
['type']
182 if iface
["type"] == "data":
183 dataiface
= dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]
184 myIfaceDict
["vpci"] = dataiface
['vpci']
185 myIfaceDict
["bw"] = dataiface
['bw']
186 myIfaceDict
["model"] = dataiface
['model']
187 created_time_iface
= dataiface
['created_time']
189 bridgeiface
= bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]
190 myIfaceDict
["vpci"] = bridgeiface
['vpci']
191 myIfaceDict
["bw"] = bridgeiface
['bw']
192 myIfaceDict
["model"] = bridgeiface
['model']
193 myIfaceDict
["mac"] = bridgeiface
['mac']
194 myIfaceDict
["port_security"]= bridgeiface
['port_security']
195 myIfaceDict
["floating_ip"] = bridgeiface
['floating_ip']
196 created_time_iface
= bridgeiface
['created_time']
197 #print "Iface name: %s" % iface['name']
198 iface_id
= self
._new
_row
_internal
('interfaces', myIfaceDict
, add_uuid
=True, root_uuid
=vnf_id
, created_time
= created_time_iface
)
199 #print "Iface id in NFVO DB: %s" % iface_id
203 except (mdb
.Error
, AttributeError) as e
:
204 self
._format
_error
(e
, tries
)
207 def new_vnf_as_a_whole2(self
,nfvo_tenant
,vnf_name
,vnf_descriptor
,VNFCDict
):
208 self
.logger
.debug("Adding new vnf to the NFVO database")
211 created_time
= time
.time()
216 myVNFDict
["name"] = vnf_name
217 myVNFDict
["descriptor"] = vnf_descriptor
['vnf'].get('descriptor')
218 myVNFDict
["public"] = vnf_descriptor
['vnf'].get('public', "false")
219 myVNFDict
["description"] = vnf_descriptor
['vnf']['description']
220 myVNFDict
["class"] = vnf_descriptor
['vnf'].get('class',"MISC")
221 myVNFDict
["tenant_id"] = vnf_descriptor
['vnf'].get("tenant_id")
223 vnf_id
= self
._new
_row
_internal
('vnfs', myVNFDict
, add_uuid
=True, root_uuid
=None, created_time
=created_time
)
224 #print "Adding new vms to the NFVO database"
225 #For each vm, we must create the appropriate vm in the NFVO database.
227 for _
,vm
in VNFCDict
.iteritems():
228 #This code could make the name of the vms grow and grow.
229 #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name
230 #vm['name'] = "%s-%s" % (vnf_name,vm['name'])
231 #print "VM name: %s. Description: %s" % (vm['name'], vm['description'])
232 vm
["vnf_id"] = vnf_id
233 created_time
+= 0.00001
234 vm_id
= self
._new
_row
_internal
('vms', vm
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
235 #print "Internal vm id in NFVO DB: %s" % vm_id
236 vmDict
[vm
['name']] = vm_id
238 #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field
239 bridgeInterfacesDict
= {}
240 for vm
in vnf_descriptor
['vnf']['VNFC']:
241 if 'bridge-ifaces' in vm
:
242 bridgeInterfacesDict
[vm
['name']] = {}
243 for bridgeiface
in vm
['bridge-ifaces']:
244 created_time
+= 0.00001
245 db_base
._convert
_bandwidth
(bridgeiface
, logger
=self
.logger
)
246 if 'port-security' in bridgeiface
:
247 bridgeiface
['port_security'] = bridgeiface
.pop('port-security')
248 if 'floating-ip' in bridgeiface
:
249 bridgeiface
['floating_ip'] = bridgeiface
.pop('floating-ip')
251 ifaceDict
['vpci'] = bridgeiface
.get('vpci',None)
252 ifaceDict
['mac'] = bridgeiface
.get('mac_address',None)
253 ifaceDict
['bw'] = bridgeiface
.get('bandwidth', None)
254 ifaceDict
['model'] = bridgeiface
.get('model', None)
255 ifaceDict
['port_security'] = int(bridgeiface
.get('port_security', True))
256 ifaceDict
['floating_ip'] = int(bridgeiface
.get('floating_ip', False))
257 ifaceDict
['created_time'] = created_time
258 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']] = ifaceDict
260 # Collect the data interfaces of each VM/VNFC under the 'numas' field
262 for vm
in vnf_descriptor
['vnf']['VNFC']:
263 dataifacesDict
[vm
['name']] = {}
264 for numa
in vm
.get('numas', []):
265 for dataiface
in numa
.get('interfaces', []):
266 created_time
+= 0.00001
267 db_base
._convert
_bandwidth
(dataiface
, logger
=self
.logger
)
269 ifaceDict
['vpci'] = dataiface
.get('vpci')
270 ifaceDict
['bw'] = dataiface
['bandwidth']
271 ifaceDict
['model'] = "PF" if dataiface
['dedicated'] == "yes" else \
272 ("VF" if dataiface
['dedicated'] == "no" else "VFnotShared")
273 ifaceDict
['created_time'] = created_time
274 dataifacesDict
[vm
['name']][dataiface
['name']] = ifaceDict
276 #For each internal connection, we add it to the interfaceDict and we create the appropriate net in the NFVO database.
277 #print "Adding new nets (VNF internal nets) to the NFVO database (if any)"
278 if 'internal-connections' in vnf_descriptor
['vnf']:
279 for net
in vnf_descriptor
['vnf']['internal-connections']:
280 #print "Net name: %s. Description: %s" % (net['name'], net['description'])
283 myNetDict
["name"] = net
['name']
284 myNetDict
["description"] = net
['description']
285 if (net
["implementation"] == "overlay"):
286 net
["type"] = "bridge"
287 #It should give an error if the type is e-line. For the moment, we consider it as a bridge
288 elif (net
["implementation"] == "underlay"):
289 if (net
["type"] == "e-line"):
291 elif (net
["type"] == "e-lan"):
293 net
.pop("implementation")
294 myNetDict
["type"] = net
['type']
295 myNetDict
["vnf_id"] = vnf_id
297 created_time
+= 0.00001
298 net_id
= self
._new
_row
_internal
('nets', myNetDict
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
300 if "ip-profile" in net
:
301 ip_profile
= net
["ip-profile"]
303 myIPProfileDict
["net_id"] = net_id
304 myIPProfileDict
["ip_version"] = ip_profile
.get('ip-version',"IPv4")
305 myIPProfileDict
["subnet_address"] = ip_profile
.get('subnet-address',None)
306 myIPProfileDict
["gateway_address"] = ip_profile
.get('gateway-address',None)
307 myIPProfileDict
["dns_address"] = ip_profile
.get('dns-address',None)
308 if ("dhcp" in ip_profile
):
309 myIPProfileDict
["dhcp_enabled"] = ip_profile
["dhcp"].get('enabled',"true")
310 myIPProfileDict
["dhcp_start_address"] = ip_profile
["dhcp"].get('start-address',None)
311 myIPProfileDict
["dhcp_count"] = ip_profile
["dhcp"].get('count',None)
313 created_time
+= 0.00001
314 ip_profile_id
= self
._new
_row
_internal
('ip_profiles', myIPProfileDict
)
316 for element
in net
['elements']:
318 #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])
319 ifaceItem
["internal_name"] = element
['local_iface_name']
320 #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])]
321 ifaceItem
["vm_id"] = vmDict
[element
['VNFC']]
322 ifaceItem
["net_id"] = net_id
323 ifaceItem
["type"] = net
['type']
324 ifaceItem
["ip_address"] = element
.get('ip_address',None)
325 if ifaceItem
["type"] == "data":
326 ifaceDict
= dataifacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]
327 ifaceItem
["vpci"] = ifaceDict
['vpci']
328 ifaceItem
["bw"] = ifaceDict
['bw']
329 ifaceItem
["model"] = ifaceDict
['model']
331 ifaceDict
= bridgeInterfacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]
332 ifaceItem
["vpci"] = ifaceDict
['vpci']
333 ifaceItem
["mac"] = ifaceDict
['mac']
334 ifaceItem
["bw"] = ifaceDict
['bw']
335 ifaceItem
["model"] = ifaceDict
['model']
336 ifaceItem
["port_security"] = ifaceDict
['port_security']
337 ifaceItem
["floating_ip"] = ifaceDict
['floating_ip']
338 created_time_iface
= ifaceDict
["created_time"]
339 #print "Iface name: %s" % iface['internal_name']
340 iface_id
= self
._new
_row
_internal
('interfaces', ifaceItem
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time_iface
)
341 #print "Iface id in NFVO DB: %s" % iface_id
343 #print "Adding external interfaces to the NFVO database"
344 for iface
in vnf_descriptor
['vnf']['external-connections']:
346 #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])
347 myIfaceDict
["internal_name"] = iface
['local_iface_name']
348 #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])]
349 myIfaceDict
["vm_id"] = vmDict
[iface
['VNFC']]
350 myIfaceDict
["external_name"] = iface
['name']
351 myIfaceDict
["type"] = iface
['type']
352 if iface
["type"] == "data":
353 myIfaceDict
["vpci"] = dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['vpci']
354 myIfaceDict
["bw"] = dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['bw']
355 myIfaceDict
["model"] = dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['model']
356 created_time_iface
= dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['created_time']
358 myIfaceDict
["vpci"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['vpci']
359 myIfaceDict
["bw"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['bw']
360 myIfaceDict
["model"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['model']
361 myIfaceDict
["mac"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['mac']
362 myIfaceDict
["port_security"] = \
363 bridgeInterfacesDict
[iface
['VNFC']][iface
['local_iface_name']]['port_security']
364 myIfaceDict
["floating_ip"] = \
365 bridgeInterfacesDict
[iface
['VNFC']][iface
['local_iface_name']]['floating_ip']
366 created_time_iface
= bridgeInterfacesDict
[iface
['VNFC']][iface
['local_iface_name']]['created_time']
367 #print "Iface name: %s" % iface['name']
368 iface_id
= self
._new
_row
_internal
('interfaces', myIfaceDict
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time_iface
)
369 #print "Iface id in NFVO DB: %s" % iface_id
373 except (mdb
.Error
, AttributeError) as e
:
374 self
._format
_error
(e
, tries
)
375 # except KeyError as e2:
376 # exc_type, exc_obj, exc_tb = sys.exc_info()
377 # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
378 # self.logger.debug("Exception type: %s; Filename: %s; Line number: %s", exc_type, fname, exc_tb.tb_lineno)
382 def new_scenario(self
, scenario_dict
):
385 created_time
= time
.time()
388 self
.cur
= self
.con
.cursor()
389 tenant_id
= scenario_dict
.get('tenant_id')
391 INSERT_
={'tenant_id': tenant_id
,
392 'name': scenario_dict
['name'],
393 'description': scenario_dict
['description'],
394 'public': scenario_dict
.get('public', "false")}
396 scenario_uuid
= self
._new
_row
_internal
('scenarios', INSERT_
, add_uuid
=True, root_uuid
=None, created_time
=created_time
)
398 for net
in scenario_dict
['nets'].values():
399 net_dict
={'scenario_id': scenario_uuid
}
400 net_dict
["name"] = net
["name"]
401 net_dict
["type"] = net
["type"]
402 net_dict
["description"] = net
.get("description")
403 net_dict
["external"] = net
.get("external", False)
405 #net["graph"]=yaml.safe_dump(net["graph"],default_flow_style=True,width=256)
406 #TODO, must be json because of the GUI, change to yaml
407 net_dict
["graph"]=json
.dumps(net
["graph"])
408 created_time
+= 0.00001
409 net_uuid
= self
._new
_row
_internal
('sce_nets', net_dict
, add_uuid
=True, root_uuid
=scenario_uuid
, created_time
=created_time
)
412 if net
.get("ip-profile"):
413 ip_profile
= net
["ip-profile"]
415 "sce_net_id": net_uuid
,
416 "ip_version": ip_profile
.get('ip-version', "IPv4"),
417 "subnet_address": ip_profile
.get('subnet-address'),
418 "gateway_address": ip_profile
.get('gateway-address'),
419 "dns_address": ip_profile
.get('dns-address')}
420 if "dhcp" in ip_profile
:
421 myIPProfileDict
["dhcp_enabled"] = ip_profile
["dhcp"].get('enabled', "true")
422 myIPProfileDict
["dhcp_start_address"] = ip_profile
["dhcp"].get('start-address')
423 myIPProfileDict
["dhcp_count"] = ip_profile
["dhcp"].get('count')
424 self
._new
_row
_internal
('ip_profiles', myIPProfileDict
)
427 for k
, vnf
in scenario_dict
['vnfs'].items():
428 INSERT_
= {'scenario_id': scenario_uuid
,
430 'vnf_id': vnf
['uuid'],
431 # 'description': scenario_dict['name']
432 'description': vnf
['description']}
434 #I NSERT_["graph"]=yaml.safe_dump(vnf["graph"],default_flow_style=True,width=256)
435 # TODO, must be json because of the GUI, change to yaml
436 INSERT_
["graph"] = json
.dumps(vnf
["graph"])
437 created_time
+= 0.00001
438 scn_vnf_uuid
= self
._new
_row
_internal
('sce_vnfs', INSERT_
, add_uuid
=True,
439 root_uuid
=scenario_uuid
, created_time
=created_time
)
440 vnf
['scn_vnf_uuid']=scn_vnf_uuid
442 for iface
in vnf
['ifaces'].values():
443 # print 'iface', iface
444 if 'net_key' not in iface
:
446 iface
['net_id'] = scenario_dict
['nets'][ iface
['net_key'] ]['uuid']
447 INSERT_
={'sce_vnf_id': scn_vnf_uuid
,
448 'sce_net_id': iface
['net_id'],
449 'interface_id': iface
['uuid'],
450 'ip_address': iface
.get('ip_address')}
451 created_time
+= 0.00001
452 iface_uuid
= self
._new
_row
_internal
('sce_interfaces', INSERT_
, add_uuid
=True,
453 root_uuid
=scenario_uuid
, created_time
=created_time
)
457 except (mdb
.Error
, AttributeError) as e
:
458 self
._format
_error
(e
, tries
)
461 def edit_scenario(self
, scenario_dict
):
464 modified_time
= time
.time()
468 self
.cur
= self
.con
.cursor()
469 #check that scenario exist
470 tenant_id
= scenario_dict
.get('tenant_id')
471 scenario_uuid
= scenario_dict
['uuid']
473 where_text
= "uuid='{}'".format(scenario_uuid
)
474 if not tenant_id
and tenant_id
!= "any":
475 where_text
+= " AND (tenant_id='{}' OR public='True')".format(tenant_id
)
476 cmd
= "SELECT * FROM scenarios WHERE "+ where_text
477 self
.logger
.debug(cmd
)
478 self
.cur
.execute(cmd
)
480 if self
.cur
.rowcount
==0:
481 raise db_base
.db_base_Exception("No scenario found with this criteria " + where_text
, httperrors
.Bad_Request
)
482 elif self
.cur
.rowcount
>1:
483 raise db_base
.db_base_Exception("More than one scenario found with this criteria " + where_text
, httperrors
.Bad_Request
)
487 topology
= scenario_dict
.pop("topology", None)
488 if topology
!= None and "nodes" in topology
:
489 nodes
= topology
.get("nodes",{})
491 if "name" in scenario_dict
: UPDATE_
["name"] = scenario_dict
["name"]
492 if "description" in scenario_dict
: UPDATE_
["description"] = scenario_dict
["description"]
494 WHERE_
={'tenant_id': tenant_id
, 'uuid': scenario_uuid
}
495 item_changed
+= self
._update
_rows
('scenarios', UPDATE_
, WHERE_
, modified_time
=modified_time
)
497 for node_id
, node
in nodes
.items():
499 #node["graph"] = yaml.safe_dump(node["graph"],default_flow_style=True,width=256)
500 #TODO, must be json because of the GUI, change to yaml
501 node
["graph"] = json
.dumps(node
["graph"])
502 WHERE_
={'scenario_id': scenario_uuid
, 'uuid': node_id
}
503 #Try to change at sce_nets(version 0 API backward compatibility and sce_vnfs)
504 item_changed
+= self
._update
_rows
('sce_nets', node
, WHERE_
)
505 item_changed
+= self
._update
_rows
('sce_vnfs', node
, WHERE_
, modified_time
=modified_time
)
508 except (mdb
.Error
, AttributeError) as e
:
509 self
._format
_error
(e
, tries
)
512 # def get_instance_scenario(self, instance_scenario_id, tenant_id=None):
513 # '''Obtain the scenario instance information, filtering by one or serveral of the tenant, uuid or name
514 # instance_scenario_id is the uuid or the name if it is not a valid uuid format
515 # Only one scenario isntance must mutch the filtering or an error is returned
517 # print "1******************************************************************"
520 # self.cur = self.con.cursor(mdb.cursors.DictCursor)
523 # if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" )
524 # if db_base._check_valid_uuid(instance_scenario_id):
525 # where_list.append( "uuid='" + instance_scenario_id +"'" )
527 # where_list.append( "name='" + instance_scenario_id +"'" )
528 # where_text = " AND ".join(where_list)
529 # self.cur.execute("SELECT * FROM instance_scenarios WHERE "+ where_text)
530 # rows = self.cur.fetchall()
531 # if self.cur.rowcount==0:
532 # return -httperrors.Bad_Request, "No scenario instance found with this criteria " + where_text
533 # elif self.cur.rowcount>1:
534 # return -httperrors.Bad_Request, "More than one scenario instance found with this criteria " + where_text
535 # instance_scenario_dict = rows[0]
538 # self.cur.execute("SELECT uuid,vnf_id FROM instance_vnfs WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
539 # instance_scenario_dict['instance_vnfs'] = self.cur.fetchall()
540 # for vnf in instance_scenario_dict['instance_vnfs']:
542 # self.cur.execute("SELECT uuid, vim_vm_id "+
543 # "FROM instance_vms "+
544 # "WHERE instance_vnf_id='" + vnf['uuid'] +"'"
546 # vnf['instance_vms'] = self.cur.fetchall()
548 # self.cur.execute("SELECT uuid, vim_net_id FROM instance_nets WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
549 # instance_scenario_dict['instance_nets'] = self.cur.fetchall()
551 # #instance_interfaces
552 # self.cur.execute("SELECT uuid, vim_interface_id, instance_vm_id, instance_net_id FROM instance_interfaces WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
553 # instance_scenario_dict['instance_interfaces'] = self.cur.fetchall()
555 # db_base._convert_datetime2str(instance_scenario_dict)
556 # db_base._convert_str2boolean(instance_scenario_dict, ('public','shared','external') )
557 # print "2******************************************************************"
558 # return 1, instance_scenario_dict
559 # except (mdb.Error, AttributeError) as e:
560 # print "nfvo_db.get_instance_scenario DB Exception %d: %s" % (e.args[0], e.args[1])
561 # return self._format_error(e)
563 def get_scenario(self
, scenario_id
, tenant_id
=None, datacenter_vim_id
=None, datacenter_id
=None):
564 '''Obtain the scenario information, filtering by one or serveral of the tenant, uuid or name
565 scenario_id is the uuid or the name if it is not a valid uuid format
566 if datacenter_vim_id,d datacenter_id is provided, it supply aditional vim_id fields with the matching vim uuid
567 Only one scenario must mutch the filtering or an error is returned
573 self
.cur
= self
.con
.cursor(mdb
.cursors
.DictCursor
)
574 where_text
= "uuid='{}'".format(scenario_id
)
575 if not tenant_id
and tenant_id
!= "any":
576 where_text
+= " AND (tenant_id='{}' OR public='True')".format(tenant_id
)
577 cmd
= "SELECT * FROM scenarios WHERE " + where_text
578 self
.logger
.debug(cmd
)
579 self
.cur
.execute(cmd
)
580 rows
= self
.cur
.fetchall()
581 if self
.cur
.rowcount
==0:
582 raise db_base
.db_base_Exception("No scenario found with this criteria " + where_text
, httperrors
.Bad_Request
)
583 elif self
.cur
.rowcount
>1:
584 raise db_base
.db_base_Exception("More than one scenario found with this criteria " + where_text
, httperrors
.Bad_Request
)
585 scenario_dict
= rows
[0]
586 if scenario_dict
["cloud_config"]:
587 scenario_dict
["cloud-config"] = yaml
.load(scenario_dict
["cloud_config"])
588 del scenario_dict
["cloud_config"]
590 cmd
= "SELECT uuid,name,member_vnf_index,vnf_id,description FROM sce_vnfs WHERE scenario_id='{}' "\
591 "ORDER BY created_at".format(scenario_dict
['uuid'])
592 self
.logger
.debug(cmd
)
593 self
.cur
.execute(cmd
)
594 scenario_dict
['vnfs'] = self
.cur
.fetchall()
596 for vnf
in scenario_dict
['vnfs']:
597 cmd
= "SELECT mgmt_access FROM vnfs WHERE uuid='{}'".format(scenario_dict
['vnfs'][0]['vnf_id'])
598 self
.logger
.debug(cmd
)
599 self
.cur
.execute(cmd
)
600 mgmt_access_dict
= self
.cur
.fetchall()
601 if mgmt_access_dict
[0].get('mgmt_access'):
602 vnf
['mgmt_access'] = yaml
.load(mgmt_access_dict
[0]['mgmt_access'])
604 vnf
['mgmt_access'] = None
606 cmd
= "SELECT scei.uuid,scei.sce_net_id,scei.interface_id,i.external_name,scei.ip_address"\
607 " FROM sce_interfaces as scei join interfaces as i on scei.interface_id=i.uuid"\
608 " WHERE scei.sce_vnf_id='{}' ORDER BY scei.created_at".format(vnf
['uuid'])
609 self
.logger
.debug(cmd
)
610 self
.cur
.execute(cmd
)
611 vnf
['interfaces'] = self
.cur
.fetchall()
613 cmd
= "SELECT vms.uuid as uuid, flavor_id, image_id, image_list, vms.name as name," \
614 " vms.description as description, vms.boot_data as boot_data, count," \
615 " vms.availability_zone as availability_zone, vms.osm_id as osm_id, vms.pdu_type" \
616 " FROM vnfs join vms on vnfs.uuid=vms.vnf_id" \
617 " WHERE vnfs.uuid='" + vnf
['vnf_id'] + "'" \
618 " ORDER BY vms.created_at"
619 self
.logger
.debug(cmd
)
620 self
.cur
.execute(cmd
)
621 vnf
['vms'] = self
.cur
.fetchall()
622 for vm
in vnf
['vms']:
624 vm
["boot_data"] = yaml
.safe_load(vm
["boot_data"])
628 vm
["image_list"] = yaml
.safe_load(vm
["image_list"])
631 if datacenter_vim_id
!=None:
633 cmd
= "SELECT vim_id FROM datacenters_images WHERE image_id='{}' AND " \
634 "datacenter_vim_id='{}'".format(vm
['image_id'], datacenter_vim_id
)
635 self
.logger
.debug(cmd
)
636 self
.cur
.execute(cmd
)
637 if self
.cur
.rowcount
==1:
638 vim_image_dict
= self
.cur
.fetchone()
639 vm
['vim_image_id']=vim_image_dict
['vim_id']
641 cmd
= "SELECT vim_id FROM datacenters_flavors WHERE flavor_id='{}' AND " \
642 "datacenter_vim_id='{}'".format(vm
['flavor_id'], datacenter_vim_id
)
643 self
.logger
.debug(cmd
)
644 self
.cur
.execute(cmd
)
645 if self
.cur
.rowcount
==1:
646 vim_flavor_dict
= self
.cur
.fetchone()
647 vm
['vim_flavor_id']=vim_flavor_dict
['vim_id']
650 cmd
= "SELECT uuid,internal_name,external_name,net_id,type,vpci,mac,bw,model,ip_address," \
651 "floating_ip, port_security" \
653 " WHERE vm_id='{}'" \
654 " ORDER BY created_at".format(vm
['uuid'])
655 self
.logger
.debug(cmd
)
656 self
.cur
.execute(cmd
)
657 vm
['interfaces'] = self
.cur
.fetchall()
658 for iface
in vm
['interfaces']:
659 iface
['port-security'] = iface
.pop("port_security")
660 iface
['floating-ip'] = iface
.pop("floating_ip")
661 for sce_interface
in vnf
["interfaces"]:
662 if sce_interface
["interface_id"] == iface
["uuid"]:
663 if sce_interface
["ip_address"]:
664 iface
["ip_address"] = sce_interface
["ip_address"]
666 #nets every net of a vms
667 cmd
= "SELECT uuid,name,type,description, osm_id FROM nets WHERE vnf_id='{}'".format(vnf
['vnf_id'])
668 self
.logger
.debug(cmd
)
669 self
.cur
.execute(cmd
)
670 vnf
['nets'] = self
.cur
.fetchall()
671 for vnf_net
in vnf
['nets']:
672 SELECT_
= "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count"
673 cmd
= "SELECT {} FROM ip_profiles WHERE net_id='{}'".format(SELECT_
,vnf_net
['uuid'])
674 self
.logger
.debug(cmd
)
675 self
.cur
.execute(cmd
)
676 ipprofiles
= self
.cur
.fetchall()
677 if self
.cur
.rowcount
==1:
678 vnf_net
["ip_profile"] = ipprofiles
[0]
679 elif self
.cur
.rowcount
>1:
680 raise db_base
.db_base_Exception("More than one ip-profile found with this criteria: net_id='{}'".format(vnf_net
['uuid']), httperrors
.Bad_Request
)
683 cmd
= "SELECT uuid,name,type,external,description,vim_network_name, osm_id" \
684 " FROM sce_nets WHERE scenario_id='{}'" \
685 " ORDER BY created_at ".format(scenario_dict
['uuid'])
686 self
.logger
.debug(cmd
)
687 self
.cur
.execute(cmd
)
688 scenario_dict
['nets'] = self
.cur
.fetchall()
690 for net
in scenario_dict
['nets']:
691 if str(net
['external']) == 'false':
692 SELECT_
= "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count"
693 cmd
= "SELECT {} FROM ip_profiles WHERE sce_net_id='{}'".format(SELECT_
,net
['uuid'])
694 self
.logger
.debug(cmd
)
695 self
.cur
.execute(cmd
)
696 ipprofiles
= self
.cur
.fetchall()
697 if self
.cur
.rowcount
==1:
698 net
["ip_profile"] = ipprofiles
[0]
699 elif self
.cur
.rowcount
>1:
700 raise db_base
.db_base_Exception("More than one ip-profile found with this criteria: sce_net_id='{}'".format(net
['uuid']), httperrors
.Bad_Request
)
702 WHERE_
=" WHERE name='{}'".format(net
['name'])
703 if datacenter_id
!=None:
704 WHERE_
+= " AND datacenter_id='{}'".format(datacenter_id
)
705 cmd
= "SELECT vim_net_id FROM datacenter_nets" + WHERE_
706 self
.logger
.debug(cmd
)
707 self
.cur
.execute(cmd
)
708 d_net
= self
.cur
.fetchone()
709 if d_net
==None or datacenter_vim_id
==None:
710 #print "nfvo_db.get_scenario() WARNING external net %s not found" % net['name']
713 net
['vim_id']=d_net
['vim_net_id']
715 db_base
._convert
_datetime
2str
(scenario_dict
)
716 db_base
._convert
_str
2boolean
(scenario_dict
, ('public','shared','external','port-security','floating-ip') )
719 cmd
= "SELECT uuid,name,description,vendor FROM sce_vnffgs WHERE scenario_id='{}' "\
720 "ORDER BY created_at".format(scenario_dict
['uuid'])
721 self
.logger
.debug(cmd
)
722 self
.cur
.execute(cmd
)
723 scenario_dict
['vnffgs'] = self
.cur
.fetchall()
724 for vnffg
in scenario_dict
['vnffgs']:
725 cmd
= "SELECT uuid,name FROM sce_rsps WHERE sce_vnffg_id='{}' "\
726 "ORDER BY created_at".format(vnffg
['uuid'])
727 self
.logger
.debug(cmd
)
728 self
.cur
.execute(cmd
)
729 vnffg
['rsps'] = self
.cur
.fetchall()
730 for rsp
in vnffg
['rsps']:
731 cmd
= "SELECT uuid,if_order,ingress_interface_id,egress_interface_id,sce_vnf_id " \
732 "FROM sce_rsp_hops WHERE sce_rsp_id='{}' "\
733 "ORDER BY created_at".format(rsp
['uuid'])
734 self
.logger
.debug(cmd
)
735 self
.cur
.execute(cmd
)
736 rsp
['connection_points'] = self
.cur
.fetchall();
737 cmd
= "SELECT uuid,name,sce_vnf_id,interface_id FROM sce_classifiers WHERE sce_vnffg_id='{}' "\
738 "AND sce_rsp_id='{}' ORDER BY created_at".format(vnffg
['uuid'], rsp
['uuid'])
739 self
.logger
.debug(cmd
)
740 self
.cur
.execute(cmd
)
741 rsp
['classifier'] = self
.cur
.fetchone();
742 cmd
= "SELECT uuid,ip_proto,source_ip,destination_ip,source_port,destination_port FROM sce_classifier_matches "\
743 "WHERE sce_classifier_id='{}' ORDER BY created_at".format(rsp
['classifier']['uuid'])
744 self
.logger
.debug(cmd
)
745 self
.cur
.execute(cmd
)
746 rsp
['classifier']['matches'] = self
.cur
.fetchall()
749 except (mdb
.Error
, AttributeError) as e
:
750 self
._format
_error
(e
, tries
)
753 def delete_scenario(self
, scenario_id
, tenant_id
=None):
754 '''Deletes a scenario, filtering by one or several of the tenant, uuid or name
755 scenario_id is the uuid or the name if it is not a valid uuid format
756 Only one scenario must mutch the filtering or an error is returned
762 self
.cur
= self
.con
.cursor(mdb
.cursors
.DictCursor
)
765 where_text
= "uuid='{}'".format(scenario_id
)
766 if not tenant_id
and tenant_id
!= "any":
767 where_text
+= " AND (tenant_id='{}' OR public='True')".format(tenant_id
)
768 cmd
= "SELECT * FROM scenarios WHERE "+ where_text
769 self
.logger
.debug(cmd
)
770 self
.cur
.execute(cmd
)
771 rows
= self
.cur
.fetchall()
772 if self
.cur
.rowcount
==0:
773 raise db_base
.db_base_Exception("No scenario found where " + where_text
, httperrors
.Not_Found
)
774 elif self
.cur
.rowcount
>1:
775 raise db_base
.db_base_Exception("More than one scenario found where " + where_text
, httperrors
.Conflict
)
776 scenario_uuid
= rows
[0]["uuid"]
777 scenario_name
= rows
[0]["name"]
780 cmd
= "DELETE FROM scenarios WHERE uuid='{}'".format(scenario_uuid
)
781 self
.logger
.debug(cmd
)
782 self
.cur
.execute(cmd
)
784 return scenario_uuid
+ " " + scenario_name
785 except (mdb
.Error
, AttributeError) as e
:
786 self
._format
_error
(e
, tries
, "delete", "instances running")
789 def new_rows(self
, tables
, uuid_list
=None, confidential_data
=False):
791 Make a transactional insertion of rows at several tables. Can be also a deletion
792 :param tables: list with dictionary where the keys are the table names and the values are a row or row list
793 with the values to be inserted at the table. Each row is a dictionary with the key values. E.g.:
795 {"table1": [ {"column1": value, "column2: value, ... }, {"column1": value, "column2: value, ... }, ...],
796 {"table2": [ {"column1": value, "column2: value, ... }, {"column1": value, "column2: value, ... }, ...],
797 {"table3": {"column1": value, "column2: value, ... }
799 If tables does not contain the 'created_at', it is generated incrementally with the order of tables. You can
800 provide a integer value, that it is an index multiply by 0.00001 to add to the created time to manually set
802 If dict contains {"TO-DELETE": uuid} the entry is deleted if exist instead of inserted
803 :param uuid_list: list of created uuids, first one is the root (#TODO to store at uuid table)
804 :return: None if success, raise exception otherwise
809 created_time
= time
.time()
812 self
.cur
= self
.con
.cursor()
814 for table_name
, row_list
in table
.items():
816 if isinstance(row_list
, dict):
817 row_list
= (row_list
, ) #create a list with the single value
819 if "TO-DELETE" in row
:
820 self
._delete
_row
_by
_id
_internal
(table_name
, row
["TO-DELETE"])
823 if table_name
in self
.tables_with_created_field
:
824 if "created_at" in row
:
825 created_time_param
= created_time
+ (index
+ row
.pop("created_at"))*0.00001
827 created_time_param
= created_time
+ index
*0.00001
830 created_time_param
= 0
831 self
._new
_row
_internal
(table_name
, row
, add_uuid
=False, root_uuid
=None,
832 confidential_data
=confidential_data
,
833 created_time
=created_time_param
)
835 except (mdb
.Error
, AttributeError) as e
:
836 self
._format
_error
(e
, tries
, table
=table_name
)
839 def new_instance_scenario_as_a_whole(self
,tenant_id
,instance_scenario_name
,instance_scenario_description
,scenarioDict
):
842 created_time
= time
.time()
845 self
.cur
= self
.con
.cursor()
847 datacenter_id
= scenarioDict
['datacenter_id']
848 INSERT_
={'tenant_id': tenant_id
,
849 'datacenter_tenant_id': scenarioDict
["datacenter2tenant"][datacenter_id
],
850 'name': instance_scenario_name
,
851 'description': instance_scenario_description
,
852 'scenario_id' : scenarioDict
['uuid'],
853 'datacenter_id': datacenter_id
855 if scenarioDict
.get("cloud-config"):
856 INSERT_
["cloud_config"] = yaml
.safe_dump(scenarioDict
["cloud-config"], default_flow_style
=True, width
=256)
858 instance_uuid
= self
._new
_row
_internal
('instance_scenarios', INSERT_
, add_uuid
=True, root_uuid
=None, created_time
=created_time
)
860 net_scene2instance
={}
861 #instance_nets #nets interVNF
862 for net
in scenarioDict
['nets']:
863 net_scene2instance
[ net
['uuid'] ] ={}
864 datacenter_site_id
= net
.get('datacenter_id', datacenter_id
)
865 if not "vim_id_sites" in net
:
866 net
["vim_id_sites"] ={datacenter_site_id
: net
['vim_id']}
867 net
["vim_id_sites"]["datacenter_site_id"] = {datacenter_site_id
: net
['vim_id']}
868 sce_net_id
= net
.get("uuid")
870 for datacenter_site_id
,vim_id
in net
["vim_id_sites"].iteritems():
871 INSERT_
={'vim_net_id': vim_id
, 'created': net
.get('created', False), 'instance_scenario_id':instance_uuid
} #, 'type': net['type']
872 INSERT_
['datacenter_id'] = datacenter_site_id
873 INSERT_
['datacenter_tenant_id'] = scenarioDict
["datacenter2tenant"][datacenter_site_id
]
874 if not net
.get('created', False):
875 INSERT_
['status'] = "ACTIVE"
877 INSERT_
['sce_net_id'] = sce_net_id
878 created_time
+= 0.00001
879 instance_net_uuid
= self
._new
_row
_internal
('instance_nets', INSERT_
, True, instance_uuid
, created_time
)
880 net_scene2instance
[ sce_net_id
][datacenter_site_id
] = instance_net_uuid
881 net
['uuid'] = instance_net_uuid
#overwrite scnario uuid by instance uuid
883 if 'ip_profile' in net
:
884 net
['ip_profile']['net_id'] = None
885 net
['ip_profile']['sce_net_id'] = None
886 net
['ip_profile']['instance_net_id'] = instance_net_uuid
887 created_time
+= 0.00001
888 ip_profile_id
= self
._new
_row
_internal
('ip_profiles', net
['ip_profile'])
891 for vnf
in scenarioDict
['vnfs']:
892 datacenter_site_id
= vnf
.get('datacenter_id', datacenter_id
)
893 INSERT_
={'instance_scenario_id': instance_uuid
, 'vnf_id': vnf
['vnf_id'] }
894 INSERT_
['datacenter_id'] = datacenter_site_id
895 INSERT_
['datacenter_tenant_id'] = scenarioDict
["datacenter2tenant"][datacenter_site_id
]
897 INSERT_
['sce_vnf_id'] = vnf
['uuid']
898 created_time
+= 0.00001
899 instance_vnf_uuid
= self
._new
_row
_internal
('instance_vnfs', INSERT_
, True, instance_uuid
, created_time
)
900 vnf
['uuid'] = instance_vnf_uuid
#overwrite scnario uuid by instance uuid
902 #instance_nets #nets intraVNF
903 for net
in vnf
['nets']:
904 net_scene2instance
[ net
['uuid'] ] = {}
905 INSERT_
={'vim_net_id': net
['vim_id'], 'created': net
.get('created', False), 'instance_scenario_id':instance_uuid
} #, 'type': net['type']
906 INSERT_
['datacenter_id'] = net
.get('datacenter_id', datacenter_site_id
)
907 INSERT_
['datacenter_tenant_id'] = scenarioDict
["datacenter2tenant"][datacenter_id
]
909 INSERT_
['net_id'] = net
['uuid']
910 created_time
+= 0.00001
911 instance_net_uuid
= self
._new
_row
_internal
('instance_nets', INSERT_
, True, instance_uuid
, created_time
)
912 net_scene2instance
[ net
['uuid'] ][datacenter_site_id
] = instance_net_uuid
913 net
['uuid'] = instance_net_uuid
#overwrite scnario uuid by instance uuid
915 if 'ip_profile' in net
:
916 net
['ip_profile']['net_id'] = None
917 net
['ip_profile']['sce_net_id'] = None
918 net
['ip_profile']['instance_net_id'] = instance_net_uuid
919 created_time
+= 0.00001
920 ip_profile_id
= self
._new
_row
_internal
('ip_profiles', net
['ip_profile'])
923 for vm
in vnf
['vms']:
924 INSERT_
={'instance_vnf_id': instance_vnf_uuid
, 'vm_id': vm
['uuid'], 'vim_vm_id': vm
['vim_id'] }
925 created_time
+= 0.00001
926 instance_vm_uuid
= self
._new
_row
_internal
('instance_vms', INSERT_
, True, instance_uuid
, created_time
)
927 vm
['uuid'] = instance_vm_uuid
#overwrite scnario uuid by instance uuid
930 for interface
in vm
['interfaces']:
931 net_id
= interface
.get('net_id', None)
933 #check if is connected to a inter VNFs net
934 for iface
in vnf
['interfaces']:
935 if iface
['interface_id'] == interface
['uuid']:
936 if 'ip_address' in iface
:
937 interface
['ip_address'] = iface
['ip_address']
938 net_id
= iface
.get('sce_net_id', None)
942 interface_type
='external' if interface
['external_name'] is not None else 'internal'
943 INSERT_
={'instance_vm_id': instance_vm_uuid
, 'instance_net_id': net_scene2instance
[net_id
][datacenter_site_id
],
944 'interface_id': interface
['uuid'], 'vim_interface_id': interface
.get('vim_id'), 'type': interface_type
,
945 'ip_address': interface
.get('ip_address'), 'floating_ip': int(interface
.get('floating-ip',False)),
946 'port_security': int(interface
.get('port-security',True))}
947 #created_time += 0.00001
948 interface_uuid
= self
._new
_row
_internal
('instance_interfaces', INSERT_
, True, instance_uuid
) #, created_time)
949 interface
['uuid'] = interface_uuid
#overwrite scnario uuid by instance uuid
951 except (mdb
.Error
, AttributeError) as e
:
952 self
._format
_error
(e
, tries
)
955 def get_instance_scenario(self
, instance_id
, tenant_id
=None, verbose
=False):
956 '''Obtain the instance information, filtering by one or several of the tenant, uuid or name
957 instance_id is the uuid or the name if it is not a valid uuid format
958 Only one instance must mutch the filtering or an error is returned
964 self
.cur
= self
.con
.cursor(mdb
.cursors
.DictCursor
)
968 where_list
.append("inst.tenant_id='{}'".format(tenant_id
))
969 if db_base
._check
_valid
_uuid
(instance_id
):
970 where_list
.append("inst.uuid='{}'".format(instance_id
))
972 where_list
.append("inst.name='{}'".format(instance_id
))
973 where_text
= " AND ".join(where_list
)
974 cmd
= "SELECT inst.uuid as uuid, inst.name as name, inst.scenario_id as scenario_id, datacenter_id"\
975 " ,datacenter_tenant_id, s.name as scenario_name,inst.tenant_id as tenant_id" \
976 " ,inst.description as description, inst.created_at as created_at" \
977 " ,inst.cloud_config as cloud_config, s.osm_id as nsd_osm_id" \
978 " FROM instance_scenarios as inst left join scenarios as s on inst.scenario_id=s.uuid" \
979 " WHERE " + where_text
980 self
.logger
.debug(cmd
)
981 self
.cur
.execute(cmd
)
982 rows
= self
.cur
.fetchall()
984 if self
.cur
.rowcount
== 0:
985 raise db_base
.db_base_Exception("No instance found where " + where_text
, httperrors
.Not_Found
)
986 elif self
.cur
.rowcount
> 1:
987 raise db_base
.db_base_Exception("More than one instance found where " + where_text
,
988 httperrors
.Bad_Request
)
989 instance_dict
= rows
[0]
990 if instance_dict
["cloud_config"]:
991 instance_dict
["cloud-config"] = yaml
.load(instance_dict
["cloud_config"])
992 del instance_dict
["cloud_config"]
995 cmd
= "SELECT iv.uuid as uuid, iv.vnf_id as vnf_id, sv.name as vnf_name, sce_vnf_id, datacenter_id"\
996 ", datacenter_tenant_id, v.mgmt_access, sv.member_vnf_index, v.osm_id as vnfd_osm_id "\
997 "FROM instance_vnfs as iv left join sce_vnfs as sv "\
998 " on iv.sce_vnf_id=sv.uuid join vnfs as v on iv.vnf_id=v.uuid " \
999 "WHERE iv.instance_scenario_id='{}' " \
1000 "ORDER BY iv.created_at ".format(instance_dict
['uuid'])
1001 self
.logger
.debug(cmd
)
1002 self
.cur
.execute(cmd
)
1003 instance_dict
['vnfs'] = self
.cur
.fetchall()
1004 for vnf
in instance_dict
['vnfs']:
1005 vnf
["ip_address"] = None
1006 vnf_mgmt_access_iface
= None
1007 vnf_mgmt_access_vm
= None
1008 if vnf
["mgmt_access"]:
1009 vnf_mgmt_access
= yaml
.load(vnf
["mgmt_access"])
1010 vnf_mgmt_access_iface
= vnf_mgmt_access
.get("interface_id")
1011 vnf_mgmt_access_vm
= vnf_mgmt_access
.get("vm_id")
1012 vnf
["ip_address"] = vnf_mgmt_access
.get("ip-address")
1015 cmd
= "SELECT iv.uuid as uuid, vim_vm_id, status, error_msg, vim_info, iv.created_at as "\
1016 "created_at, name, vms.osm_id as vdu_osm_id, vim_name, vms.uuid as vm_uuid"\
1017 " FROM instance_vms as iv join vms on iv.vm_id=vms.uuid "\
1018 " WHERE instance_vnf_id='{}' ORDER BY iv.created_at".format(vnf
['uuid'])
1019 self
.logger
.debug(cmd
)
1020 self
.cur
.execute(cmd
)
1021 vnf
['vms'] = self
.cur
.fetchall()
1022 for vm
in vnf
['vms']:
1023 vm_manage_iface_list
=[]
1024 # instance_interfaces
1025 cmd
= "SELECT vim_interface_id, instance_net_id, internal_name,external_name, mac_address,"\
1026 " ii.ip_address as ip_address, vim_info, i.type as type, sdn_port_id, i.uuid"\
1027 " FROM instance_interfaces as ii join interfaces as i on ii.interface_id=i.uuid"\
1028 " WHERE instance_vm_id='{}' ORDER BY created_at".format(vm
['uuid'])
1029 self
.logger
.debug(cmd
)
1030 self
.cur
.execute(cmd
)
1031 vm
['interfaces'] = self
.cur
.fetchall()
1032 for iface
in vm
['interfaces']:
1033 if vnf_mgmt_access_iface
and vnf_mgmt_access_iface
== iface
["uuid"]:
1034 if not vnf
["ip_address"]:
1035 vnf
["ip_address"] = iface
["ip_address"]
1036 if iface
["type"] == "mgmt" and iface
["ip_address"]:
1037 vm_manage_iface_list
.append(iface
["ip_address"])
1041 if vm_manage_iface_list
:
1042 vm
["ip_address"] = ",".join(vm_manage_iface_list
)
1043 if not vnf
["ip_address"] and vnf_mgmt_access_vm
== vm
["vm_uuid"]:
1044 vnf
["ip_address"] = vm
["ip_address"]
1048 #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"
1049 #from_text = "instance_nets join instance_scenarios on instance_nets.instance_scenario_id=instance_scenarios.uuid " + \
1050 # "join sce_nets on instance_scenarios.scenario_id=sce_nets.scenario_id"
1051 #where_text = "instance_nets.instance_scenario_id='"+ instance_dict['uuid'] + "'"
1052 cmd
= "SELECT inets.uuid as uuid,vim_net_id,status,error_msg,vim_info,created, sce_net_id, " \
1053 "net_id as vnf_net_id, datacenter_id, datacenter_tenant_id, sdn_net_id, " \
1054 "snets.osm_id as ns_net_osm_id, nets.osm_id as vnf_net_osm_id, inets.vim_name " \
1055 "FROM instance_nets as inets left join sce_nets as snets on inets.sce_net_id=snets.uuid " \
1056 "left join nets on inets.net_id=nets.uuid " \
1057 "WHERE instance_scenario_id='{}' ORDER BY inets.created_at".format(instance_dict
['uuid'])
1058 self
.logger
.debug(cmd
)
1059 self
.cur
.execute(cmd
)
1060 instance_dict
['nets'] = self
.cur
.fetchall()
1063 cmd
= "SELECT uuid,vim_sfp_id,sce_rsp_id,datacenter_id,"\
1064 "datacenter_tenant_id,status,error_msg,vim_info"\
1065 " FROM instance_sfps" \
1066 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict
['uuid'])
1067 self
.logger
.debug(cmd
)
1068 self
.cur
.execute(cmd
)
1069 instance_dict
['sfps'] = self
.cur
.fetchall()
1071 # for sfp in instance_dict['sfps']:
1073 cmd
= "SELECT uuid,vim_sf_id,sce_rsp_hop_id,datacenter_id,"\
1074 "datacenter_tenant_id,status,error_msg,vim_info"\
1075 " FROM instance_sfs" \
1076 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict
['uuid']) # TODO: replace instance_scenario_id with instance_sfp_id
1077 self
.logger
.debug(cmd
)
1078 self
.cur
.execute(cmd
)
1079 instance_dict
['sfs'] = self
.cur
.fetchall()
1081 #for sf in instance_dict['sfs']:
1083 cmd
= "SELECT uuid,vim_sfi_id,sce_rsp_hop_id,datacenter_id,"\
1084 "datacenter_tenant_id,status,error_msg,vim_info"\
1085 " FROM instance_sfis" \
1086 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict
['uuid']) # TODO: replace instance_scenario_id with instance_sf_id
1087 self
.logger
.debug(cmd
)
1088 self
.cur
.execute(cmd
)
1089 instance_dict
['sfis'] = self
.cur
.fetchall()
1090 # for sfi in instance_dict['sfi']:
1092 #instance_classifications
1093 cmd
= "SELECT uuid,vim_classification_id,sce_classifier_match_id,datacenter_id,"\
1094 "datacenter_tenant_id,status,error_msg,vim_info"\
1095 " FROM instance_classifications" \
1096 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict
['uuid'])
1097 self
.logger
.debug(cmd
)
1098 self
.cur
.execute(cmd
)
1099 instance_dict
['classifications'] = self
.cur
.fetchall()
1100 # for classification in instance_dict['classifications']
1102 db_base
._convert
_datetime
2str
(instance_dict
)
1103 db_base
._convert
_str
2boolean
(instance_dict
, ('public','shared','created') )
1104 return instance_dict
1105 except (mdb
.Error
, AttributeError) as e
:
1106 self
._format
_error
(e
, tries
)
1109 def delete_instance_scenario(self
, instance_id
, tenant_id
=None):
1110 '''Deletes a instance_Scenario, filtering by one or serveral of the tenant, uuid or name
1111 instance_id is the uuid or the name if it is not a valid uuid format
1112 Only one instance_scenario must mutch the filtering or an error is returned
1118 self
.cur
= self
.con
.cursor(mdb
.cursors
.DictCursor
)
1122 if tenant_id
is not None: where_list
.append( "tenant_id='" + tenant_id
+"'" )
1123 if db_base
._check
_valid
_uuid
(instance_id
):
1124 where_list
.append( "uuid='" + instance_id
+"'" )
1126 where_list
.append( "name='" + instance_id
+"'" )
1127 where_text
= " AND ".join(where_list
)
1128 cmd
= "SELECT * FROM instance_scenarios WHERE "+ where_text
1129 self
.logger
.debug(cmd
)
1130 self
.cur
.execute(cmd
)
1131 rows
= self
.cur
.fetchall()
1133 if self
.cur
.rowcount
==0:
1134 raise db_base
.db_base_Exception("No instance found where " + where_text
, httperrors
.Bad_Request
)
1135 elif self
.cur
.rowcount
>1:
1136 raise db_base
.db_base_Exception("More than one instance found where " + where_text
, httperrors
.Bad_Request
)
1137 instance_uuid
= rows
[0]["uuid"]
1138 instance_name
= rows
[0]["name"]
1141 cmd
= "DELETE FROM instance_scenarios WHERE uuid='{}'".format(instance_uuid
)
1142 self
.logger
.debug(cmd
)
1143 self
.cur
.execute(cmd
)
1145 return instance_uuid
+ " " + instance_name
1146 except (mdb
.Error
, AttributeError) as e
:
1147 self
._format
_error
(e
, tries
, "delete", "No dependences can avoid deleting!!!!")
1150 def new_instance_scenario(self
, instance_scenario_dict
, tenant_id
):
1151 #return self.new_row('vnfs', vnf_dict, None, tenant_id, True, True)
1152 return self
._new
_row
_internal
('instance_scenarios', instance_scenario_dict
, tenant_id
, add_uuid
=True, root_uuid
=None, log
=True)
1154 def update_instance_scenario(self
, instance_scenario_dict
):
1158 def new_instance_vnf(self
, instance_vnf_dict
, tenant_id
, instance_scenario_id
= None):
1159 #return self.new_row('vms', vm_dict, tenant_id, True, True)
1160 return self
._new
_row
_internal
('instance_vnfs', instance_vnf_dict
, tenant_id
, add_uuid
=True, root_uuid
=instance_scenario_id
, log
=True)
1162 def update_instance_vnf(self
, instance_vnf_dict
):
1166 def delete_instance_vnf(self
, instance_vnf_id
):
1170 def new_instance_vm(self
, instance_vm_dict
, tenant_id
, instance_scenario_id
= None):
1171 #return self.new_row('vms', vm_dict, tenant_id, True, True)
1172 return self
._new
_row
_internal
('instance_vms', instance_vm_dict
, tenant_id
, add_uuid
=True, root_uuid
=instance_scenario_id
, log
=True)
1174 def update_instance_vm(self
, instance_vm_dict
):
1178 def delete_instance_vm(self
, instance_vm_id
):
1182 def new_instance_net(self
, instance_net_dict
, tenant_id
, instance_scenario_id
= None):
1183 return self
._new
_row
_internal
('instance_nets', instance_net_dict
, tenant_id
, add_uuid
=True, root_uuid
=instance_scenario_id
, log
=True)
1185 def update_instance_net(self
, instance_net_dict
):
1189 def delete_instance_net(self
, instance_net_id
):
1193 def new_instance_interface(self
, instance_interface_dict
, tenant_id
, instance_scenario_id
= None):
1194 return self
._new
_row
_internal
('instance_interfaces', instance_interface_dict
, tenant_id
, add_uuid
=True, root_uuid
=instance_scenario_id
, log
=True)
1196 def update_instance_interface(self
, instance_interface_dict
):
1200 def delete_instance_interface(self
, instance_interface_dict
):
1204 def update_datacenter_nets(self
, datacenter_id
, new_net_list
=[]):
1205 ''' Removes the old and adds the new net list at datacenter list for one datacenter.
1207 datacenter_id: uuid of the datacenter to act upon
1208 table: table where to insert
1209 new_net_list: the new values to be inserted. If empty it only deletes the existing nets
1210 Return: (Inserted items, Deleted items) if OK, (-Error, text) if error
1214 created_time
= time
.time()
1217 self
.cur
= self
.con
.cursor()
1218 cmd
="DELETE FROM datacenter_nets WHERE datacenter_id='{}'".format(datacenter_id
)
1219 self
.logger
.debug(cmd
)
1220 self
.cur
.execute(cmd
)
1221 deleted
= self
.cur
.rowcount
1223 for new_net
in new_net_list
:
1224 created_time
+= 0.00001
1225 self
._new
_row
_internal
('datacenter_nets', new_net
, add_uuid
=True, created_time
=created_time
)
1227 return inserted
, deleted
1228 except (mdb
.Error
, AttributeError) as e
:
1229 self
._format
_error
(e
, tries
)