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$"
30 from osm_ro
import db_base
37 from osm_ro
.db_base
import retry
, with_transaction
38 from osm_ro
.http_tools
import errors
as httperrors
39 from osm_ro
.utils
import Attempt
45 tables_with_createdat_field
=["datacenters","instance_nets","instance_scenarios","instance_vms","instance_vnfs",
46 "interfaces","nets","nfvo_tenants","scenarios","sce_interfaces","sce_nets",
47 "sce_vnfs","tenants_datacenters","datacenter_tenants","vms","vnfs", "datacenter_nets",
48 "instance_actions", "sce_vnffgs", "sce_rsps", "sce_rsp_hops",
49 "sce_classifiers", "sce_classifier_matches", "instance_sfis", "instance_sfs",
50 "instance_classifications", "instance_sfps", "wims", "wim_accounts", "wim_nfvo_tenants",
51 "wim_port_mappings", "vim_wim_actions", "instance_interfaces",
55 class nfvo_db(db_base
.db_base
):
56 def __init__(self
, host
=None, user
=None, passwd
=None, database
=None,
57 log_name
='openmano.db', log_level
=None, lock
=None):
58 db_base
.db_base
.__init
__(self
, host
, user
, passwd
, database
,
59 log_name
, log_level
, lock
)
60 db_base
.db_base
.tables_with_created_field
=tables_with_createdat_field
65 def new_vnf_as_a_whole(self
,nfvo_tenant
,vnf_name
,vnf_descriptor
,VNFCDict
):
66 self
.logger
.debug("Adding new vnf to the NFVO database")
67 created_time
= time
.time()
69 myVNFDict
["name"] = vnf_name
70 myVNFDict
["descriptor"] = vnf_descriptor
['vnf'].get('descriptor')
71 myVNFDict
["public"] = vnf_descriptor
['vnf'].get('public', "false")
72 myVNFDict
["description"] = vnf_descriptor
['vnf']['description']
73 myVNFDict
["class"] = vnf_descriptor
['vnf'].get('class',"MISC")
74 myVNFDict
["tenant_id"] = vnf_descriptor
['vnf'].get("tenant_id")
76 vnf_id
= self
._new
_row
_internal
('vnfs', myVNFDict
, add_uuid
=True, root_uuid
=None, created_time
=created_time
)
77 #print "Adding new vms to the NFVO database"
78 #For each vm, we must create the appropriate vm in the NFVO database.
80 for _
,vm
in VNFCDict
.items():
81 #This code could make the name of the vms grow and grow.
82 #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name
83 #vm['name'] = "%s-%s" % (vnf_name,vm['name'])
84 #print "VM name: %s. Description: %s" % (vm['name'], vm['description'])
86 created_time
+= 0.00001
87 vm_id
= self
._new
_row
_internal
('vms', vm
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
88 #print "Internal vm id in NFVO DB: %s" % vm_id
89 vmDict
[vm
['name']] = vm_id
91 #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field
92 bridgeInterfacesDict
= {}
93 for vm
in vnf_descriptor
['vnf']['VNFC']:
94 if 'bridge-ifaces' in vm
:
95 bridgeInterfacesDict
[vm
['name']] = {}
96 for bridgeiface
in vm
['bridge-ifaces']:
97 created_time
+= 0.00001
98 if 'port-security' in bridgeiface
:
99 bridgeiface
['port_security'] = bridgeiface
.pop('port-security')
100 if 'floating-ip' in bridgeiface
:
101 bridgeiface
['floating_ip'] = bridgeiface
.pop('floating-ip')
102 db_base
._convert
_bandwidth
(bridgeiface
, logger
=self
.logger
)
103 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']] = {}
104 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['vpci'] = bridgeiface
.get('vpci',None)
105 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['mac'] = bridgeiface
.get('mac_address',None)
106 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['bw'] = bridgeiface
.get('bandwidth', None)
107 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['model'] = bridgeiface
.get('model', None)
108 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['port_security'] = \
109 int(bridgeiface
.get('port_security', True))
110 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['floating_ip'] = \
111 int(bridgeiface
.get('floating_ip', False))
112 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']]['created_time'] = created_time
114 # Collect the data interfaces of each VM/VNFC under the 'numas' field
116 for vm
in vnf_descriptor
['vnf']['VNFC']:
117 dataifacesDict
[vm
['name']] = {}
118 for numa
in vm
.get('numas', []):
119 for dataiface
in numa
.get('interfaces', []):
120 created_time
+= 0.00001
121 db_base
._convert
_bandwidth
(dataiface
, logger
=self
.logger
)
122 dataifacesDict
[vm
['name']][dataiface
['name']] = {}
123 dataifacesDict
[vm
['name']][dataiface
['name']]['vpci'] = dataiface
.get('vpci')
124 dataifacesDict
[vm
['name']][dataiface
['name']]['bw'] = dataiface
['bandwidth']
125 dataifacesDict
[vm
['name']][dataiface
['name']]['model'] = "PF" if dataiface
[
126 'dedicated'] == "yes" else (
127 "VF" if dataiface
['dedicated'] == "no" else "VFnotShared")
128 dataifacesDict
[vm
['name']][dataiface
['name']]['created_time'] = created_time
130 #For each internal connection, we add it to the interfaceDict and we create the appropriate net in the NFVO database.
131 #print "Adding new nets (VNF internal nets) to the NFVO database (if any)"
132 internalconnList
= []
133 if 'internal-connections' in vnf_descriptor
['vnf']:
134 for net
in vnf_descriptor
['vnf']['internal-connections']:
135 #print "Net name: %s. Description: %s" % (net['name'], net['description'])
138 myNetDict
["name"] = net
['name']
139 myNetDict
["description"] = net
['description']
140 myNetDict
["type"] = net
['type']
141 myNetDict
["vnf_id"] = vnf_id
143 created_time
+= 0.00001
144 net_id
= self
._new
_row
_internal
('nets', myNetDict
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
146 for element
in net
['elements']:
148 #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])
149 ifaceItem
["internal_name"] = element
['local_iface_name']
150 #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])]
151 ifaceItem
["vm_id"] = vmDict
[element
['VNFC']]
152 ifaceItem
["net_id"] = net_id
153 ifaceItem
["type"] = net
['type']
154 if ifaceItem
["type"] == "data":
155 dataiface
= dataifacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]
156 ifaceItem
["vpci"] = dataiface
['vpci']
157 ifaceItem
["bw"] = dataiface
['bw']
158 ifaceItem
["model"] = dataiface
['model']
159 created_time_iface
= dataiface
['created_time']
161 bridgeiface
= bridgeInterfacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]
162 ifaceItem
["vpci"] = bridgeiface
['vpci']
163 ifaceItem
["mac"] = bridgeiface
['mac']
164 ifaceItem
["bw"] = bridgeiface
['bw']
165 ifaceItem
["model"] = bridgeiface
['model']
166 ifaceItem
["port_security"] = bridgeiface
['port_security']
167 ifaceItem
["floating_ip"] = bridgeiface
['floating_ip']
168 created_time_iface
= bridgeiface
['created_time']
169 internalconnList
.append(ifaceItem
)
170 #print "Internal net id in NFVO DB: %s" % net_id
172 #print "Adding internal interfaces to the NFVO database (if any)"
173 for iface
in internalconnList
:
174 #print "Iface name: %s" % iface['internal_name']
175 iface_id
= self
._new
_row
_internal
('interfaces', iface
, add_uuid
=True, root_uuid
=vnf_id
, created_time
= created_time_iface
)
176 #print "Iface id in NFVO DB: %s" % iface_id
178 #print "Adding external interfaces to the NFVO database"
179 for iface
in vnf_descriptor
['vnf']['external-connections']:
181 #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])
182 myIfaceDict
["internal_name"] = iface
['local_iface_name']
183 #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])]
184 myIfaceDict
["vm_id"] = vmDict
[iface
['VNFC']]
185 myIfaceDict
["external_name"] = iface
['name']
186 myIfaceDict
["type"] = iface
['type']
187 if iface
["type"] == "data":
188 dataiface
= dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]
189 myIfaceDict
["vpci"] = dataiface
['vpci']
190 myIfaceDict
["bw"] = dataiface
['bw']
191 myIfaceDict
["model"] = dataiface
['model']
192 created_time_iface
= dataiface
['created_time']
194 bridgeiface
= bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]
195 myIfaceDict
["vpci"] = bridgeiface
['vpci']
196 myIfaceDict
["bw"] = bridgeiface
['bw']
197 myIfaceDict
["model"] = bridgeiface
['model']
198 myIfaceDict
["mac"] = bridgeiface
['mac']
199 myIfaceDict
["port_security"]= bridgeiface
['port_security']
200 myIfaceDict
["floating_ip"] = bridgeiface
['floating_ip']
201 created_time_iface
= bridgeiface
['created_time']
202 #print "Iface name: %s" % iface['name']
203 iface_id
= self
._new
_row
_internal
('interfaces', myIfaceDict
, add_uuid
=True, root_uuid
=vnf_id
, created_time
= created_time_iface
)
204 #print "Iface id in NFVO DB: %s" % iface_id
210 def new_vnf_as_a_whole2(self
,nfvo_tenant
,vnf_name
,vnf_descriptor
,VNFCDict
):
211 self
.logger
.debug("Adding new vnf to the NFVO database")
212 created_time
= time
.time()
214 myVNFDict
["name"] = vnf_name
215 myVNFDict
["descriptor"] = vnf_descriptor
['vnf'].get('descriptor')
216 myVNFDict
["public"] = vnf_descriptor
['vnf'].get('public', "false")
217 myVNFDict
["description"] = vnf_descriptor
['vnf']['description']
218 myVNFDict
["class"] = vnf_descriptor
['vnf'].get('class',"MISC")
219 myVNFDict
["tenant_id"] = vnf_descriptor
['vnf'].get("tenant_id")
221 vnf_id
= self
._new
_row
_internal
('vnfs', myVNFDict
, add_uuid
=True, root_uuid
=None, created_time
=created_time
)
222 #print "Adding new vms to the NFVO database"
223 #For each vm, we must create the appropriate vm in the NFVO database.
225 for _
,vm
in VNFCDict
.items():
226 #This code could make the name of the vms grow and grow.
227 #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name
228 #vm['name'] = "%s-%s" % (vnf_name,vm['name'])
229 #print "VM name: %s. Description: %s" % (vm['name'], vm['description'])
230 vm
["vnf_id"] = vnf_id
231 created_time
+= 0.00001
232 vm_id
= self
._new
_row
_internal
('vms', vm
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
233 #print "Internal vm id in NFVO DB: %s" % vm_id
234 vmDict
[vm
['name']] = vm_id
236 #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field
237 bridgeInterfacesDict
= {}
238 for vm
in vnf_descriptor
['vnf']['VNFC']:
239 if 'bridge-ifaces' in vm
:
240 bridgeInterfacesDict
[vm
['name']] = {}
241 for bridgeiface
in vm
['bridge-ifaces']:
242 created_time
+= 0.00001
243 db_base
._convert
_bandwidth
(bridgeiface
, logger
=self
.logger
)
244 if 'port-security' in bridgeiface
:
245 bridgeiface
['port_security'] = bridgeiface
.pop('port-security')
246 if 'floating-ip' in bridgeiface
:
247 bridgeiface
['floating_ip'] = bridgeiface
.pop('floating-ip')
249 ifaceDict
['vpci'] = bridgeiface
.get('vpci',None)
250 ifaceDict
['mac'] = bridgeiface
.get('mac_address',None)
251 ifaceDict
['bw'] = bridgeiface
.get('bandwidth', None)
252 ifaceDict
['model'] = bridgeiface
.get('model', None)
253 ifaceDict
['port_security'] = int(bridgeiface
.get('port_security', True))
254 ifaceDict
['floating_ip'] = int(bridgeiface
.get('floating_ip', False))
255 ifaceDict
['created_time'] = created_time
256 bridgeInterfacesDict
[vm
['name']][bridgeiface
['name']] = ifaceDict
258 # Collect the data interfaces of each VM/VNFC under the 'numas' field
260 for vm
in vnf_descriptor
['vnf']['VNFC']:
261 dataifacesDict
[vm
['name']] = {}
262 for numa
in vm
.get('numas', []):
263 for dataiface
in numa
.get('interfaces', []):
264 created_time
+= 0.00001
265 db_base
._convert
_bandwidth
(dataiface
, logger
=self
.logger
)
267 ifaceDict
['vpci'] = dataiface
.get('vpci')
268 ifaceDict
['bw'] = dataiface
['bandwidth']
269 ifaceDict
['model'] = "PF" if dataiface
['dedicated'] == "yes" else \
270 ("VF" if dataiface
['dedicated'] == "no" else "VFnotShared")
271 ifaceDict
['created_time'] = created_time
272 dataifacesDict
[vm
['name']][dataiface
['name']] = ifaceDict
274 #For each internal connection, we add it to the interfaceDict and we create the appropriate net in the NFVO database.
275 #print "Adding new nets (VNF internal nets) to the NFVO database (if any)"
276 if 'internal-connections' in vnf_descriptor
['vnf']:
277 for net
in vnf_descriptor
['vnf']['internal-connections']:
278 #print "Net name: %s. Description: %s" % (net['name'], net['description'])
281 myNetDict
["name"] = net
['name']
282 myNetDict
["description"] = net
['description']
283 if (net
["implementation"] == "overlay"):
284 net
["type"] = "bridge"
285 #It should give an error if the type is e-line. For the moment, we consider it as a bridge
286 elif (net
["implementation"] == "underlay"):
287 if (net
["type"] == "e-line"):
289 elif (net
["type"] == "e-lan"):
291 net
.pop("implementation")
292 myNetDict
["type"] = net
['type']
293 myNetDict
["vnf_id"] = vnf_id
295 created_time
+= 0.00001
296 net_id
= self
._new
_row
_internal
('nets', myNetDict
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time
)
298 if "ip-profile" in net
:
299 ip_profile
= net
["ip-profile"]
301 myIPProfileDict
["net_id"] = net_id
302 myIPProfileDict
["ip_version"] = ip_profile
.get('ip-version',"IPv4")
303 myIPProfileDict
["subnet_address"] = ip_profile
.get('subnet-address',None)
304 myIPProfileDict
["gateway_address"] = ip_profile
.get('gateway-address',None)
305 myIPProfileDict
["dns_address"] = ip_profile
.get('dns-address',None)
306 if ("dhcp" in ip_profile
):
307 myIPProfileDict
["dhcp_enabled"] = ip_profile
["dhcp"].get('enabled',"true")
308 myIPProfileDict
["dhcp_start_address"] = ip_profile
["dhcp"].get('start-address',None)
309 myIPProfileDict
["dhcp_count"] = ip_profile
["dhcp"].get('count',None)
311 created_time
+= 0.00001
312 ip_profile_id
= self
._new
_row
_internal
('ip_profiles', myIPProfileDict
)
314 for element
in net
['elements']:
316 #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])
317 ifaceItem
["internal_name"] = element
['local_iface_name']
318 #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])]
319 ifaceItem
["vm_id"] = vmDict
[element
['VNFC']]
320 ifaceItem
["net_id"] = net_id
321 ifaceItem
["type"] = net
['type']
322 ifaceItem
["ip_address"] = element
.get('ip_address',None)
323 if ifaceItem
["type"] == "data":
324 ifaceDict
= dataifacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]
325 ifaceItem
["vpci"] = ifaceDict
['vpci']
326 ifaceItem
["bw"] = ifaceDict
['bw']
327 ifaceItem
["model"] = ifaceDict
['model']
329 ifaceDict
= bridgeInterfacesDict
[ element
['VNFC'] ][ element
['local_iface_name'] ]
330 ifaceItem
["vpci"] = ifaceDict
['vpci']
331 ifaceItem
["mac"] = ifaceDict
['mac']
332 ifaceItem
["bw"] = ifaceDict
['bw']
333 ifaceItem
["model"] = ifaceDict
['model']
334 ifaceItem
["port_security"] = ifaceDict
['port_security']
335 ifaceItem
["floating_ip"] = ifaceDict
['floating_ip']
336 created_time_iface
= ifaceDict
["created_time"]
337 #print "Iface name: %s" % iface['internal_name']
338 iface_id
= self
._new
_row
_internal
('interfaces', ifaceItem
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time_iface
)
339 #print "Iface id in NFVO DB: %s" % iface_id
341 #print "Adding external interfaces to the NFVO database"
342 for iface
in vnf_descriptor
['vnf']['external-connections']:
344 #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])
345 myIfaceDict
["internal_name"] = iface
['local_iface_name']
346 #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])]
347 myIfaceDict
["vm_id"] = vmDict
[iface
['VNFC']]
348 myIfaceDict
["external_name"] = iface
['name']
349 myIfaceDict
["type"] = iface
['type']
350 if iface
["type"] == "data":
351 myIfaceDict
["vpci"] = dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['vpci']
352 myIfaceDict
["bw"] = dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['bw']
353 myIfaceDict
["model"] = dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['model']
354 created_time_iface
= dataifacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['created_time']
356 myIfaceDict
["vpci"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['vpci']
357 myIfaceDict
["bw"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['bw']
358 myIfaceDict
["model"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['model']
359 myIfaceDict
["mac"] = bridgeInterfacesDict
[ iface
['VNFC'] ][ iface
['local_iface_name'] ]['mac']
360 myIfaceDict
["port_security"] = \
361 bridgeInterfacesDict
[iface
['VNFC']][iface
['local_iface_name']]['port_security']
362 myIfaceDict
["floating_ip"] = \
363 bridgeInterfacesDict
[iface
['VNFC']][iface
['local_iface_name']]['floating_ip']
364 created_time_iface
= bridgeInterfacesDict
[iface
['VNFC']][iface
['local_iface_name']]['created_time']
365 #print "Iface name: %s" % iface['name']
366 iface_id
= self
._new
_row
_internal
('interfaces', myIfaceDict
, add_uuid
=True, root_uuid
=vnf_id
, created_time
=created_time_iface
)
367 #print "Iface id in NFVO DB: %s" % iface_id
371 # except KeyError as e2:
372 # exc_type, exc_obj, exc_tb = sys.exc_info()
373 # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
374 # self.logger.debug("Exception type: %s; Filename: %s; Line number: %s", exc_type, fname, exc_tb.tb_lineno)
379 def new_scenario(self
, scenario_dict
):
380 created_time
= time
.time()
381 tenant_id
= scenario_dict
.get('tenant_id')
383 INSERT_
={'tenant_id': tenant_id
,
384 'name': scenario_dict
['name'],
385 'description': scenario_dict
['description'],
386 'public': scenario_dict
.get('public', "false")}
388 scenario_uuid
= self
._new
_row
_internal
('scenarios', INSERT_
, add_uuid
=True, root_uuid
=None, created_time
=created_time
)
390 for net
in scenario_dict
['nets'].values():
391 net_dict
={'scenario_id': scenario_uuid
}
392 net_dict
["name"] = net
["name"]
393 net_dict
["type"] = net
["type"]
394 net_dict
["description"] = net
.get("description")
395 net_dict
["external"] = net
.get("external", False)
397 #net["graph"]=yaml.safe_dump(net["graph"],default_flow_style=True,width=256)
398 #TODO, must be json because of the GUI, change to yaml
399 net_dict
["graph"]=json
.dumps(net
["graph"])
400 created_time
+= 0.00001
401 net_uuid
= self
._new
_row
_internal
('sce_nets', net_dict
, add_uuid
=True, root_uuid
=scenario_uuid
, created_time
=created_time
)
404 if net
.get("ip-profile"):
405 ip_profile
= net
["ip-profile"]
407 "sce_net_id": net_uuid
,
408 "ip_version": ip_profile
.get('ip-version', "IPv4"),
409 "subnet_address": ip_profile
.get('subnet-address'),
410 "gateway_address": ip_profile
.get('gateway-address'),
411 "dns_address": ip_profile
.get('dns-address')}
412 if "dhcp" in ip_profile
:
413 myIPProfileDict
["dhcp_enabled"] = ip_profile
["dhcp"].get('enabled', "true")
414 myIPProfileDict
["dhcp_start_address"] = ip_profile
["dhcp"].get('start-address')
415 myIPProfileDict
["dhcp_count"] = ip_profile
["dhcp"].get('count')
416 self
._new
_row
_internal
('ip_profiles', myIPProfileDict
)
419 for k
, vnf
in scenario_dict
['vnfs'].items():
420 INSERT_
= {'scenario_id': scenario_uuid
,
422 'vnf_id': vnf
['uuid'],
423 # 'description': scenario_dict['name']
424 'description': vnf
['description']}
426 #I NSERT_["graph"]=yaml.safe_dump(vnf["graph"],default_flow_style=True,width=256)
427 # TODO, must be json because of the GUI, change to yaml
428 INSERT_
["graph"] = json
.dumps(vnf
["graph"])
429 created_time
+= 0.00001
430 scn_vnf_uuid
= self
._new
_row
_internal
('sce_vnfs', INSERT_
, add_uuid
=True,
431 root_uuid
=scenario_uuid
, created_time
=created_time
)
432 vnf
['scn_vnf_uuid']=scn_vnf_uuid
434 for iface
in vnf
['ifaces'].values():
435 # print 'iface', iface
436 if 'net_key' not in iface
:
438 iface
['net_id'] = scenario_dict
['nets'][ iface
['net_key'] ]['uuid']
439 INSERT_
={'sce_vnf_id': scn_vnf_uuid
,
440 'sce_net_id': iface
['net_id'],
441 'interface_id': iface
['uuid'],
442 'ip_address': iface
.get('ip_address')}
443 created_time
+= 0.00001
444 iface_uuid
= self
._new
_row
_internal
('sce_interfaces', INSERT_
, add_uuid
=True,
445 root_uuid
=scenario_uuid
, created_time
=created_time
)
451 def edit_scenario(self
, scenario_dict
):
452 modified_time
= time
.time()
454 #check that scenario exist
455 tenant_id
= scenario_dict
.get('tenant_id')
456 scenario_uuid
= scenario_dict
['uuid']
458 where_text
= "uuid='{}'".format(scenario_uuid
)
459 if not tenant_id
and tenant_id
!= "any":
460 where_text
+= " AND (tenant_id='{}' OR public='True')".format(tenant_id
)
461 cmd
= "SELECT * FROM scenarios WHERE "+ where_text
462 self
.logger
.debug(cmd
)
463 self
.cur
.execute(cmd
)
465 if self
.cur
.rowcount
==0:
466 raise db_base
.db_base_Exception("No scenario found with this criteria " + where_text
, httperrors
.Bad_Request
)
467 elif self
.cur
.rowcount
>1:
468 raise db_base
.db_base_Exception("More than one scenario found with this criteria " + where_text
, httperrors
.Bad_Request
)
472 topology
= scenario_dict
.pop("topology", None)
473 if topology
!= None and "nodes" in topology
:
474 nodes
= topology
.get("nodes",{})
476 if "name" in scenario_dict
: UPDATE_
["name"] = scenario_dict
["name"]
477 if "description" in scenario_dict
: UPDATE_
["description"] = scenario_dict
["description"]
479 WHERE_
={'tenant_id': tenant_id
, 'uuid': scenario_uuid
}
480 item_changed
+= self
._update
_rows
('scenarios', UPDATE_
, WHERE_
, modified_time
=modified_time
)
482 for node_id
, node
in nodes
.items():
484 #node["graph"] = yaml.safe_dump(node["graph"],default_flow_style=True,width=256)
485 #TODO, must be json because of the GUI, change to yaml
486 node
["graph"] = json
.dumps(node
["graph"])
487 WHERE_
={'scenario_id': scenario_uuid
, 'uuid': node_id
}
488 #Try to change at sce_nets(version 0 API backward compatibility and sce_vnfs)
489 item_changed
+= self
._update
_rows
('sce_nets', node
, WHERE_
)
490 item_changed
+= self
._update
_rows
('sce_vnfs', node
, WHERE_
, modified_time
=modified_time
)
493 # def get_instance_scenario(self, instance_scenario_id, tenant_id=None):
494 # '''Obtain the scenario instance information, filtering by one or several of the tenant, uuid or name
495 # instance_scenario_id is the uuid or the name if it is not a valid uuid format
496 # Only one scenario isntance must mutch the filtering or an error is returned
498 # print "1******************************************************************"
500 # with self.transaction(mdb.cursors.DictCursor):
503 # if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" )
504 # if db_base._check_valid_uuid(instance_scenario_id):
505 # where_list.append( "uuid='" + instance_scenario_id +"'" )
507 # where_list.append( "name='" + instance_scenario_id +"'" )
508 # where_text = " AND ".join(where_list)
509 # self.cur.execute("SELECT * FROM instance_scenarios WHERE "+ where_text)
510 # rows = self.cur.fetchall()
511 # if self.cur.rowcount==0:
512 # return -httperrors.Bad_Request, "No scenario instance found with this criteria " + where_text
513 # elif self.cur.rowcount>1:
514 # return -httperrors.Bad_Request, "More than one scenario instance found with this criteria " + where_text
515 # instance_scenario_dict = rows[0]
518 # self.cur.execute("SELECT uuid,vnf_id FROM instance_vnfs WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
519 # instance_scenario_dict['instance_vnfs'] = self.cur.fetchall()
520 # for vnf in instance_scenario_dict['instance_vnfs']:
522 # self.cur.execute("SELECT uuid, vim_vm_id "+
523 # "FROM instance_vms "+
524 # "WHERE instance_vnf_id='" + vnf['uuid'] +"'"
526 # vnf['instance_vms'] = self.cur.fetchall()
528 # self.cur.execute("SELECT uuid, vim_net_id FROM instance_nets WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
529 # instance_scenario_dict['instance_nets'] = self.cur.fetchall()
531 # #instance_interfaces
532 # 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'] + "'")
533 # instance_scenario_dict['instance_interfaces'] = self.cur.fetchall()
535 # db_base._convert_datetime2str(instance_scenario_dict)
536 # db_base._convert_str2boolean(instance_scenario_dict, ('public','shared','external') )
537 # print "2******************************************************************"
538 # return 1, instance_scenario_dict
539 # except (mdb.Error, AttributeError) as e:
540 # print "nfvo_db.get_instance_scenario DB Exception %d: %s" % (e.args[0], e.args[1])
541 # return self._format_error(e)
544 @with_transaction(cursor
='dict')
545 def get_scenario(self
, scenario_id
, tenant_id
=None, datacenter_vim_id
=None, datacenter_id
=None):
546 '''Obtain the scenario information, filtering by one or several of the tenant, uuid or name
547 scenario_id is the uuid or the name if it is not a valid uuid format
548 if datacenter_vim_id,d datacenter_id is provided, it supply aditional vim_id fields with the matching vim uuid
549 Only one scenario must mutch the filtering or an error is returned
551 where_text
= "uuid='{}'".format(scenario_id
)
552 if not tenant_id
and tenant_id
!= "any":
553 where_text
+= " AND (tenant_id='{}' OR public='True')".format(tenant_id
)
554 cmd
= "SELECT * FROM scenarios WHERE " + where_text
555 self
.logger
.debug(cmd
)
556 self
.cur
.execute(cmd
)
557 rows
= self
.cur
.fetchall()
558 if self
.cur
.rowcount
==0:
559 raise db_base
.db_base_Exception("No scenario found with this criteria " + where_text
, httperrors
.Bad_Request
)
560 elif self
.cur
.rowcount
>1:
561 raise db_base
.db_base_Exception("More than one scenario found with this criteria " + where_text
, httperrors
.Bad_Request
)
562 scenario_dict
= rows
[0]
563 if scenario_dict
["cloud_config"]:
564 scenario_dict
["cloud-config"] = yaml
.load(scenario_dict
["cloud_config"], Loader
=yaml
.Loader
)
565 del scenario_dict
["cloud_config"]
567 cmd
= "SELECT uuid,name,member_vnf_index,vnf_id,description FROM sce_vnfs WHERE scenario_id='{}' "\
568 "ORDER BY created_at".format(scenario_dict
['uuid'])
569 self
.logger
.debug(cmd
)
570 self
.cur
.execute(cmd
)
571 scenario_dict
['vnfs'] = self
.cur
.fetchall()
573 for vnf
in scenario_dict
['vnfs']:
574 cmd
= "SELECT mgmt_access FROM vnfs WHERE uuid='{}'".format(scenario_dict
['vnfs'][0]['vnf_id'])
575 self
.logger
.debug(cmd
)
576 self
.cur
.execute(cmd
)
577 mgmt_access_dict
= self
.cur
.fetchall()
578 if mgmt_access_dict
[0].get('mgmt_access'):
579 vnf
['mgmt_access'] = yaml
.load(mgmt_access_dict
[0]['mgmt_access'], Loader
=yaml
.Loader
)
581 vnf
['mgmt_access'] = None
583 cmd
= "SELECT scei.uuid,scei.sce_net_id,scei.interface_id,i.external_name,scei.ip_address"\
584 " FROM sce_interfaces as scei join interfaces as i on scei.interface_id=i.uuid"\
585 " WHERE scei.sce_vnf_id='{}' ORDER BY scei.created_at".format(vnf
['uuid'])
586 self
.logger
.debug(cmd
)
587 self
.cur
.execute(cmd
)
588 vnf
['interfaces'] = self
.cur
.fetchall()
590 cmd
= "SELECT vms.uuid as uuid, flavor_id, image_id, image_list, vms.name as name," \
591 " vms.description as description, vms.boot_data as boot_data, count," \
592 " vms.availability_zone as availability_zone, vms.osm_id as osm_id, vms.pdu_type" \
593 " FROM vnfs join vms on vnfs.uuid=vms.vnf_id" \
594 " WHERE vnfs.uuid='" + vnf
['vnf_id'] + "'" \
595 " ORDER BY vms.created_at"
596 self
.logger
.debug(cmd
)
597 self
.cur
.execute(cmd
)
598 vnf
['vms'] = self
.cur
.fetchall()
599 for vm
in vnf
['vms']:
601 vm
["boot_data"] = yaml
.safe_load(vm
["boot_data"])
605 vm
["image_list"] = yaml
.safe_load(vm
["image_list"])
608 if datacenter_vim_id
!=None:
610 cmd
= "SELECT vim_id FROM datacenters_images WHERE image_id='{}' AND " \
611 "datacenter_vim_id='{}'".format(vm
['image_id'], datacenter_vim_id
)
612 self
.logger
.debug(cmd
)
613 self
.cur
.execute(cmd
)
614 if self
.cur
.rowcount
==1:
615 vim_image_dict
= self
.cur
.fetchone()
616 vm
['vim_image_id']=vim_image_dict
['vim_id']
618 cmd
= "SELECT vim_id FROM datacenters_flavors WHERE flavor_id='{}' AND " \
619 "datacenter_vim_id='{}'".format(vm
['flavor_id'], datacenter_vim_id
)
620 self
.logger
.debug(cmd
)
621 self
.cur
.execute(cmd
)
622 if self
.cur
.rowcount
==1:
623 vim_flavor_dict
= self
.cur
.fetchone()
624 vm
['vim_flavor_id']=vim_flavor_dict
['vim_id']
627 cmd
= "SELECT uuid,internal_name,external_name,net_id,type,vpci,mac,bw,model,ip_address," \
628 "floating_ip, port_security" \
630 " WHERE vm_id='{}'" \
631 " ORDER BY created_at".format(vm
['uuid'])
632 self
.logger
.debug(cmd
)
633 self
.cur
.execute(cmd
)
634 vm
['interfaces'] = self
.cur
.fetchall()
635 for iface
in vm
['interfaces']:
636 iface
['port-security'] = iface
.pop("port_security")
637 iface
['floating-ip'] = iface
.pop("floating_ip")
638 for sce_interface
in vnf
["interfaces"]:
639 if sce_interface
["interface_id"] == iface
["uuid"]:
640 if sce_interface
["ip_address"]:
641 iface
["ip_address"] = sce_interface
["ip_address"]
643 #nets every net of a vms
644 cmd
= "SELECT uuid,name,type,description, osm_id FROM nets WHERE vnf_id='{}'".format(vnf
['vnf_id'])
645 self
.logger
.debug(cmd
)
646 self
.cur
.execute(cmd
)
647 vnf
['nets'] = self
.cur
.fetchall()
648 for vnf_net
in vnf
['nets']:
649 SELECT_
= "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count"
650 cmd
= "SELECT {} FROM ip_profiles WHERE net_id='{}'".format(SELECT_
,vnf_net
['uuid'])
651 self
.logger
.debug(cmd
)
652 self
.cur
.execute(cmd
)
653 ipprofiles
= self
.cur
.fetchall()
654 if self
.cur
.rowcount
==1:
655 vnf_net
["ip_profile"] = ipprofiles
[0]
656 elif self
.cur
.rowcount
>1:
657 raise db_base
.db_base_Exception("More than one ip-profile found with this criteria: net_id='{}'".format(vnf_net
['uuid']), httperrors
.Bad_Request
)
660 cmd
= "SELECT uuid,name,type,external,description,vim_network_name, osm_id" \
661 " FROM sce_nets WHERE scenario_id='{}'" \
662 " ORDER BY created_at ".format(scenario_dict
['uuid'])
663 self
.logger
.debug(cmd
)
664 self
.cur
.execute(cmd
)
665 scenario_dict
['nets'] = self
.cur
.fetchall()
667 for net
in scenario_dict
['nets']:
668 if str(net
['external']) == 'false':
669 SELECT_
= "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count"
670 cmd
= "SELECT {} FROM ip_profiles WHERE sce_net_id='{}'".format(SELECT_
,net
['uuid'])
671 self
.logger
.debug(cmd
)
672 self
.cur
.execute(cmd
)
673 ipprofiles
= self
.cur
.fetchall()
674 if self
.cur
.rowcount
==1:
675 net
["ip_profile"] = ipprofiles
[0]
676 elif self
.cur
.rowcount
>1:
677 raise db_base
.db_base_Exception("More than one ip-profile found with this criteria: sce_net_id='{}'".format(net
['uuid']), httperrors
.Bad_Request
)
679 WHERE_
=" WHERE name='{}'".format(net
['name'])
680 if datacenter_id
!=None:
681 WHERE_
+= " AND datacenter_id='{}'".format(datacenter_id
)
682 cmd
= "SELECT vim_net_id FROM datacenter_nets" + WHERE_
683 self
.logger
.debug(cmd
)
684 self
.cur
.execute(cmd
)
685 d_net
= self
.cur
.fetchone()
686 if d_net
==None or datacenter_vim_id
==None:
687 #print "nfvo_db.get_scenario() WARNING external net %s not found" % net['name']
690 net
['vim_id']=d_net
['vim_net_id']
692 db_base
._convert
_datetime
2str
(scenario_dict
)
693 db_base
._convert
_str
2boolean
(scenario_dict
, ('public','shared','external','port-security','floating-ip') )
696 cmd
= "SELECT uuid,name,description,vendor FROM sce_vnffgs WHERE scenario_id='{}' "\
697 "ORDER BY created_at".format(scenario_dict
['uuid'])
698 self
.logger
.debug(cmd
)
699 self
.cur
.execute(cmd
)
700 scenario_dict
['vnffgs'] = self
.cur
.fetchall()
701 for vnffg
in scenario_dict
['vnffgs']:
702 cmd
= "SELECT uuid,name FROM sce_rsps WHERE sce_vnffg_id='{}' "\
703 "ORDER BY created_at".format(vnffg
['uuid'])
704 self
.logger
.debug(cmd
)
705 self
.cur
.execute(cmd
)
706 vnffg
['rsps'] = self
.cur
.fetchall()
707 for rsp
in vnffg
['rsps']:
708 cmd
= "SELECT uuid,if_order,ingress_interface_id,egress_interface_id,sce_vnf_id " \
709 "FROM sce_rsp_hops WHERE sce_rsp_id='{}' "\
710 "ORDER BY created_at".format(rsp
['uuid'])
711 self
.logger
.debug(cmd
)
712 self
.cur
.execute(cmd
)
713 rsp
['connection_points'] = self
.cur
.fetchall();
714 cmd
= "SELECT uuid,name,sce_vnf_id,interface_id FROM sce_classifiers WHERE sce_vnffg_id='{}' "\
715 "AND sce_rsp_id='{}' ORDER BY created_at".format(vnffg
['uuid'], rsp
['uuid'])
716 self
.logger
.debug(cmd
)
717 self
.cur
.execute(cmd
)
718 rsp
['classifier'] = self
.cur
.fetchone();
719 cmd
= "SELECT uuid,ip_proto,source_ip,destination_ip,source_port,destination_port FROM sce_classifier_matches "\
720 "WHERE sce_classifier_id='{}' ORDER BY created_at".format(rsp
['classifier']['uuid'])
721 self
.logger
.debug(cmd
)
722 self
.cur
.execute(cmd
)
723 rsp
['classifier']['matches'] = self
.cur
.fetchall()
727 @retry(command
="delete", extra
="instances running")
728 @with_transaction(cursor
='dict')
729 def delete_scenario(self
, scenario_id
, tenant_id
=None):
730 '''Deletes a scenario, filtering by one or several of the tenant, uuid or name
731 scenario_id is the uuid or the name if it is not a valid uuid format
732 Only one scenario must mutch the filtering or an error is returned
735 where_text
= "uuid='{}'".format(scenario_id
)
736 if not tenant_id
and tenant_id
!= "any":
737 where_text
+= " AND (tenant_id='{}' OR public='True')".format(tenant_id
)
738 cmd
= "SELECT * FROM scenarios WHERE "+ where_text
739 self
.logger
.debug(cmd
)
740 self
.cur
.execute(cmd
)
741 rows
= self
.cur
.fetchall()
742 if self
.cur
.rowcount
==0:
743 raise db_base
.db_base_Exception("No scenario found where " + where_text
, httperrors
.Not_Found
)
744 elif self
.cur
.rowcount
>1:
745 raise db_base
.db_base_Exception("More than one scenario found where " + where_text
, httperrors
.Conflict
)
746 scenario_uuid
= rows
[0]["uuid"]
747 scenario_name
= rows
[0]["name"]
750 cmd
= "DELETE FROM scenarios WHERE uuid='{}'".format(scenario_uuid
)
751 self
.logger
.debug(cmd
)
752 self
.cur
.execute(cmd
)
754 return scenario_uuid
+ " " + scenario_name
758 def new_rows(self
, tables
, uuid_list
=None, confidential_data
=False, attempt
=_ATTEMPT
):
760 Make a transactional insertion of rows at several tables. Can be also a deletion
761 :param tables: list with dictionary where the keys are the table names and the values are a row or row list
762 with the values to be inserted at the table. Each row is a dictionary with the key values. E.g.:
764 {"table1": [ {"column1": value, "column2: value, ... }, {"column1": value, "column2: value, ... }, ...],
765 {"table2": [ {"column1": value, "column2: value, ... }, {"column1": value, "column2: value, ... }, ...],
766 {"table3": {"column1": value, "column2: value, ... }
768 If tables does not contain the 'created_at', it is generated incrementally with the order of tables. You can
769 provide a integer value, that it is an index multiply by 0.00001 to add to the created time to manually set
771 If dict contains {"TO-DELETE": uuid} the entry is deleted if exist instead of inserted
772 :param uuid_list: list of created uuids, first one is the root (#TODO to store at uuid table)
773 :return: None if success, raise exception otherwise
776 created_time
= time
.time()
778 for table_name
, row_list
in table
.items():
780 attempt
.info
['table'] = table_name
781 if isinstance(row_list
, dict):
782 row_list
= (row_list
, ) #create a list with the single value
784 if "TO-DELETE" in row
:
785 self
._delete
_row
_by
_id
_internal
(table_name
, row
["TO-DELETE"])
787 if "TO-UPDATE" in row
:
788 self
._update
_rows
(table_name
, UPDATE
=row
["TO-UPDATE"], WHERE
=row
["WHERE"],
789 modified_time
=created_time
)
791 if table_name
in self
.tables_with_created_field
:
792 if "created_at" in row
:
793 created_time_param
= created_time
+ (index
+ row
.pop("created_at"))*0.00001
795 created_time_param
= created_time
+ index
*0.00001
798 created_time_param
= 0
799 self
._new
_row
_internal
(table_name
, row
, add_uuid
=False, root_uuid
=None,
800 confidential_data
=confidential_data
,
801 created_time
=created_time_param
)
805 def new_instance_scenario_as_a_whole(self
,tenant_id
,instance_scenario_name
,instance_scenario_description
,scenarioDict
):
806 created_time
= time
.time()
808 datacenter_id
= scenarioDict
['datacenter_id']
809 INSERT_
={'tenant_id': tenant_id
,
810 'datacenter_tenant_id': scenarioDict
["datacenter2tenant"][datacenter_id
],
811 'name': instance_scenario_name
,
812 'description': instance_scenario_description
,
813 'scenario_id' : scenarioDict
['uuid'],
814 'datacenter_id': datacenter_id
816 if scenarioDict
.get("cloud-config"):
817 INSERT_
["cloud_config"] = yaml
.safe_dump(scenarioDict
["cloud-config"], default_flow_style
=True, width
=256)
819 instance_uuid
= self
._new
_row
_internal
('instance_scenarios', INSERT_
, add_uuid
=True, root_uuid
=None, created_time
=created_time
)
821 net_scene2instance
={}
822 #instance_nets #nets interVNF
823 for net
in scenarioDict
['nets']:
824 net_scene2instance
[ net
['uuid'] ] ={}
825 datacenter_site_id
= net
.get('datacenter_id', datacenter_id
)
826 if not "vim_id_sites" in net
:
827 net
["vim_id_sites"] ={datacenter_site_id
: net
['vim_id']}
828 net
["vim_id_sites"]["datacenter_site_id"] = {datacenter_site_id
: net
['vim_id']}
829 sce_net_id
= net
.get("uuid")
831 for datacenter_site_id
,vim_id
in net
["vim_id_sites"].items():
832 INSERT_
={'vim_net_id': vim_id
, 'created': net
.get('created', False), 'instance_scenario_id':instance_uuid
} #, 'type': net['type']
833 INSERT_
['datacenter_id'] = datacenter_site_id
834 INSERT_
['datacenter_tenant_id'] = scenarioDict
["datacenter2tenant"][datacenter_site_id
]
835 if not net
.get('created', False):
836 INSERT_
['status'] = "ACTIVE"
838 INSERT_
['sce_net_id'] = sce_net_id
839 created_time
+= 0.00001
840 instance_net_uuid
= self
._new
_row
_internal
('instance_nets', INSERT_
, True, instance_uuid
, created_time
)
841 net_scene2instance
[ sce_net_id
][datacenter_site_id
] = instance_net_uuid
842 net
['uuid'] = instance_net_uuid
#overwrite scnario uuid by instance uuid
844 if 'ip_profile' in net
:
845 net
['ip_profile']['net_id'] = None
846 net
['ip_profile']['sce_net_id'] = None
847 net
['ip_profile']['instance_net_id'] = instance_net_uuid
848 created_time
+= 0.00001
849 ip_profile_id
= self
._new
_row
_internal
('ip_profiles', net
['ip_profile'])
852 for vnf
in scenarioDict
['vnfs']:
853 datacenter_site_id
= vnf
.get('datacenter_id', datacenter_id
)
854 INSERT_
={'instance_scenario_id': instance_uuid
, 'vnf_id': vnf
['vnf_id'] }
855 INSERT_
['datacenter_id'] = datacenter_site_id
856 INSERT_
['datacenter_tenant_id'] = scenarioDict
["datacenter2tenant"][datacenter_site_id
]
858 INSERT_
['sce_vnf_id'] = vnf
['uuid']
859 created_time
+= 0.00001
860 instance_vnf_uuid
= self
._new
_row
_internal
('instance_vnfs', INSERT_
, True, instance_uuid
, created_time
)
861 vnf
['uuid'] = instance_vnf_uuid
#overwrite scnario uuid by instance uuid
863 #instance_nets #nets intraVNF
864 for net
in vnf
['nets']:
865 net_scene2instance
[ net
['uuid'] ] = {}
866 INSERT_
={'vim_net_id': net
['vim_id'], 'created': net
.get('created', False), 'instance_scenario_id':instance_uuid
} #, 'type': net['type']
867 INSERT_
['datacenter_id'] = net
.get('datacenter_id', datacenter_site_id
)
868 INSERT_
['datacenter_tenant_id'] = scenarioDict
["datacenter2tenant"][datacenter_id
]
870 INSERT_
['net_id'] = net
['uuid']
871 created_time
+= 0.00001
872 instance_net_uuid
= self
._new
_row
_internal
('instance_nets', INSERT_
, True, instance_uuid
, created_time
)
873 net_scene2instance
[ net
['uuid'] ][datacenter_site_id
] = instance_net_uuid
874 net
['uuid'] = instance_net_uuid
#overwrite scnario uuid by instance uuid
876 if 'ip_profile' in net
:
877 net
['ip_profile']['net_id'] = None
878 net
['ip_profile']['sce_net_id'] = None
879 net
['ip_profile']['instance_net_id'] = instance_net_uuid
880 created_time
+= 0.00001
881 ip_profile_id
= self
._new
_row
_internal
('ip_profiles', net
['ip_profile'])
884 for vm
in vnf
['vms']:
885 INSERT_
={'instance_vnf_id': instance_vnf_uuid
, 'vm_id': vm
['uuid'], 'vim_vm_id': vm
['vim_id'] }
886 created_time
+= 0.00001
887 instance_vm_uuid
= self
._new
_row
_internal
('instance_vms', INSERT_
, True, instance_uuid
, created_time
)
888 vm
['uuid'] = instance_vm_uuid
#overwrite scnario uuid by instance uuid
891 for interface
in vm
['interfaces']:
892 net_id
= interface
.get('net_id', None)
894 #check if is connected to a inter VNFs net
895 for iface
in vnf
['interfaces']:
896 if iface
['interface_id'] == interface
['uuid']:
897 if 'ip_address' in iface
:
898 interface
['ip_address'] = iface
['ip_address']
899 net_id
= iface
.get('sce_net_id', None)
903 interface_type
='external' if interface
['external_name'] is not None else 'internal'
904 INSERT_
={'instance_vm_id': instance_vm_uuid
, 'instance_net_id': net_scene2instance
[net_id
][datacenter_site_id
],
905 'interface_id': interface
['uuid'], 'vim_interface_id': interface
.get('vim_id'), 'type': interface_type
,
906 'ip_address': interface
.get('ip_address'), 'floating_ip': int(interface
.get('floating-ip',False)),
907 'port_security': int(interface
.get('port-security',True))}
908 #created_time += 0.00001
909 interface_uuid
= self
._new
_row
_internal
('instance_interfaces', INSERT_
, True, instance_uuid
) #, created_time)
910 interface
['uuid'] = interface_uuid
#overwrite scnario uuid by instance uuid
914 @with_transaction(cursor
='dict')
915 def get_instance_scenario(self
, instance_id
, tenant_id
=None, verbose
=False):
916 '''Obtain the instance information, filtering by one or several of the tenant, uuid or name
917 instance_id is the uuid or the name if it is not a valid uuid format
918 Only one instance must mutch the filtering or an error is returned
923 where_list
.append("inst.tenant_id='{}'".format(tenant_id
))
924 if db_base
._check
_valid
_uuid
(instance_id
):
925 where_list
.append("inst.uuid='{}'".format(instance_id
))
927 where_list
.append("inst.name='{}'".format(instance_id
))
928 where_text
= " AND ".join(where_list
)
929 cmd
= "SELECT inst.uuid as uuid, inst.name as name, inst.scenario_id as scenario_id, datacenter_id"\
930 " ,datacenter_tenant_id, s.name as scenario_name,inst.tenant_id as tenant_id" \
931 " ,inst.description as description, inst.created_at as created_at" \
932 " ,inst.cloud_config as cloud_config, s.osm_id as nsd_osm_id" \
933 " FROM instance_scenarios as inst left join scenarios as s on inst.scenario_id=s.uuid" \
934 " WHERE " + where_text
935 self
.logger
.debug(cmd
)
936 self
.cur
.execute(cmd
)
937 rows
= self
.cur
.fetchall()
939 if self
.cur
.rowcount
== 0:
940 raise db_base
.db_base_Exception("No instance found where " + where_text
, httperrors
.Not_Found
)
941 elif self
.cur
.rowcount
> 1:
942 raise db_base
.db_base_Exception("More than one instance found where " + where_text
,
943 httperrors
.Bad_Request
)
944 instance_dict
= rows
[0]
945 if instance_dict
["cloud_config"]:
946 instance_dict
["cloud-config"] = yaml
.load(instance_dict
["cloud_config"], Loader
=yaml
.Loader
)
947 del instance_dict
["cloud_config"]
950 cmd
= "SELECT iv.uuid as uuid, iv.vnf_id as vnf_id, sv.name as vnf_name, sce_vnf_id, datacenter_id"\
951 ", datacenter_tenant_id, v.mgmt_access, sv.member_vnf_index, v.osm_id as vnfd_osm_id "\
952 "FROM instance_vnfs as iv left join sce_vnfs as sv "\
953 " on iv.sce_vnf_id=sv.uuid join vnfs as v on iv.vnf_id=v.uuid " \
954 "WHERE iv.instance_scenario_id='{}' " \
955 "ORDER BY iv.created_at ".format(instance_dict
['uuid'])
956 self
.logger
.debug(cmd
)
957 self
.cur
.execute(cmd
)
958 instance_dict
['vnfs'] = self
.cur
.fetchall()
959 for vnf
in instance_dict
['vnfs']:
960 vnf
["ip_address"] = None
961 vnf_mgmt_access_iface
= None
962 vnf_mgmt_access_vm
= None
963 if vnf
["mgmt_access"]:
964 vnf_mgmt_access
= yaml
.load(vnf
["mgmt_access"], Loader
=yaml
.Loader
)
965 vnf_mgmt_access_iface
= vnf_mgmt_access
.get("interface_id")
966 vnf_mgmt_access_vm
= vnf_mgmt_access
.get("vm_id")
967 vnf
["ip_address"] = vnf_mgmt_access
.get("ip-address")
970 cmd
= "SELECT iv.uuid as uuid, vim_vm_id, status, error_msg, vim_info, iv.created_at as "\
971 "created_at, name, vms.osm_id as vdu_osm_id, vim_name, vms.uuid as vm_uuid, related"\
972 " FROM instance_vms as iv join vms on iv.vm_id=vms.uuid "\
973 " WHERE instance_vnf_id='{}' ORDER BY iv.created_at".format(vnf
['uuid'])
974 self
.logger
.debug(cmd
)
975 self
.cur
.execute(cmd
)
976 vnf
['vms'] = self
.cur
.fetchall()
977 for vm
in vnf
['vms']:
978 vm_manage_iface_list
=[]
979 # instance_interfaces
980 cmd
= "SELECT vim_interface_id, instance_net_id, internal_name,external_name, mac_address,"\
981 " ii.ip_address as ip_address, vim_info, i.type as type, sdn_port_id, i.uuid"\
982 " FROM instance_interfaces as ii join interfaces as i on ii.interface_id=i.uuid"\
983 " WHERE instance_vm_id='{}' ORDER BY i.created_at".format(vm
['uuid'])
984 self
.logger
.debug(cmd
)
985 self
.cur
.execute(cmd
)
986 vm
['interfaces'] = self
.cur
.fetchall()
987 for iface
in vm
['interfaces']:
988 if vnf_mgmt_access_iface
and vnf_mgmt_access_iface
== iface
["uuid"]:
989 if not vnf
["ip_address"]:
990 vnf
["ip_address"] = iface
["ip_address"]
991 if iface
["type"] == "mgmt" and iface
["ip_address"]:
992 vm_manage_iface_list
.append(iface
["ip_address"])
996 if vm_manage_iface_list
:
997 vm
["ip_address"] = ",".join(vm_manage_iface_list
)
998 if not vnf
["ip_address"] and vnf_mgmt_access_vm
== vm
["vm_uuid"]:
999 vnf
["ip_address"] = vm
["ip_address"]
1003 #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"
1004 #from_text = "instance_nets join instance_scenarios on instance_nets.instance_scenario_id=instance_scenarios.uuid " + \
1005 # "join sce_nets on instance_scenarios.scenario_id=sce_nets.scenario_id"
1006 #where_text = "instance_nets.instance_scenario_id='"+ instance_dict['uuid'] + "'"
1007 cmd
= "SELECT inets.uuid as uuid,vim_net_id,status,error_msg,vim_info,created, sce_net_id, " \
1008 "net_id as vnf_net_id, datacenter_id, datacenter_tenant_id, sdn_net_id, " \
1009 "snets.osm_id as ns_net_osm_id, nets.osm_id as vnf_net_osm_id, inets.vim_name, related " \
1010 "FROM instance_nets as inets left join sce_nets as snets on inets.sce_net_id=snets.uuid " \
1011 "left join nets on inets.net_id=nets.uuid " \
1012 "WHERE instance_scenario_id='{}' ORDER BY inets.created_at".format(instance_dict
['uuid'])
1013 self
.logger
.debug(cmd
)
1014 self
.cur
.execute(cmd
)
1015 instance_dict
['nets'] = self
.cur
.fetchall()
1017 # instance sdn_nets:
1018 cmd
= "SELECT * FROM instance_wim_nets WHERE instance_scenario_id='{}' ORDER BY created_at;".format(
1019 instance_dict
['uuid'])
1020 self
.logger
.debug(cmd
)
1021 self
.cur
.execute(cmd
)
1022 instance_dict
['sdn_nets'] = self
.cur
.fetchall()
1025 cmd
= "SELECT uuid,vim_sfp_id,sce_rsp_id,datacenter_id,"\
1026 "datacenter_tenant_id,status,error_msg,vim_info, related"\
1027 " FROM instance_sfps" \
1028 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict
['uuid'])
1029 self
.logger
.debug(cmd
)
1030 self
.cur
.execute(cmd
)
1031 instance_dict
['sfps'] = self
.cur
.fetchall()
1033 # for sfp in instance_dict['sfps']:
1035 cmd
= "SELECT uuid,vim_sf_id,sce_rsp_hop_id,datacenter_id,"\
1036 "datacenter_tenant_id,status,error_msg,vim_info, related"\
1037 " FROM instance_sfs" \
1038 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict
['uuid']) # TODO: replace instance_scenario_id with instance_sfp_id
1039 self
.logger
.debug(cmd
)
1040 self
.cur
.execute(cmd
)
1041 instance_dict
['sfs'] = self
.cur
.fetchall()
1043 #for sf in instance_dict['sfs']:
1045 cmd
= "SELECT uuid,vim_sfi_id,sce_rsp_hop_id,datacenter_id,"\
1046 "datacenter_tenant_id,status,error_msg,vim_info, related"\
1047 " FROM instance_sfis" \
1048 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict
['uuid']) # TODO: replace instance_scenario_id with instance_sf_id
1049 self
.logger
.debug(cmd
)
1050 self
.cur
.execute(cmd
)
1051 instance_dict
['sfis'] = self
.cur
.fetchall()
1052 # for sfi in instance_dict['sfi']:
1054 #instance_classifications
1055 cmd
= "SELECT uuid,vim_classification_id,sce_classifier_match_id,datacenter_id,"\
1056 "datacenter_tenant_id,status,error_msg,vim_info, related"\
1057 " FROM instance_classifications" \
1058 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict
['uuid'])
1059 self
.logger
.debug(cmd
)
1060 self
.cur
.execute(cmd
)
1061 instance_dict
['classifications'] = self
.cur
.fetchall()
1062 # for classification in instance_dict['classifications']
1064 db_base
._convert
_datetime
2str
(instance_dict
)
1065 db_base
._convert
_str
2boolean
(instance_dict
, ('public','shared','created') )
1066 return instance_dict
1068 @retry(command
='delete', extra
='No dependences can avoid deleting!!!!')
1069 @with_transaction(cursor
='dict')
1070 def delete_instance_scenario(self
, instance_id
, tenant_id
=None):
1071 '''Deletes a instance_Scenario, filtering by one or several of the tenant, uuid or name
1072 instance_id is the uuid or the name if it is not a valid uuid format
1073 Only one instance_scenario must mutch the filtering or an error is returned
1077 if tenant_id
is not None: where_list
.append( "tenant_id='" + tenant_id
+"'" )
1078 if db_base
._check
_valid
_uuid
(instance_id
):
1079 where_list
.append( "uuid='" + instance_id
+"'" )
1081 where_list
.append( "name='" + instance_id
+"'" )
1082 where_text
= " AND ".join(where_list
)
1083 cmd
= "SELECT * FROM instance_scenarios WHERE "+ where_text
1084 self
.logger
.debug(cmd
)
1085 self
.cur
.execute(cmd
)
1086 rows
= self
.cur
.fetchall()
1088 if self
.cur
.rowcount
==0:
1089 raise db_base
.db_base_Exception("No instance found where " + where_text
, httperrors
.Bad_Request
)
1090 elif self
.cur
.rowcount
>1:
1091 raise db_base
.db_base_Exception("More than one instance found where " + where_text
, httperrors
.Bad_Request
)
1092 instance_uuid
= rows
[0]["uuid"]
1093 instance_name
= rows
[0]["name"]
1096 cmd
= "DELETE FROM instance_scenarios WHERE uuid='{}'".format(instance_uuid
)
1097 self
.logger
.debug(cmd
)
1098 self
.cur
.execute(cmd
)
1100 return instance_uuid
+ " " + instance_name
1102 @retry(table
='instance_scenarios')
1104 def new_instance_scenario(self
, instance_scenario_dict
, tenant_id
):
1105 #return self.new_row('vnfs', vnf_dict, None, tenant_id, True, True)
1106 return self
._new
_row
_internal
('instance_scenarios', instance_scenario_dict
, tenant_id
, add_uuid
=True, root_uuid
=None, log
=True)
1108 def update_instance_scenario(self
, instance_scenario_dict
):
1112 @retry(table
='instance_vnfs')
1114 def new_instance_vnf(self
, instance_vnf_dict
, tenant_id
, instance_scenario_id
= None):
1115 #return self.new_row('vms', vm_dict, tenant_id, True, True)
1116 return self
._new
_row
_internal
('instance_vnfs', instance_vnf_dict
, tenant_id
, add_uuid
=True, root_uuid
=instance_scenario_id
, log
=True)
1118 def update_instance_vnf(self
, instance_vnf_dict
):
1122 def delete_instance_vnf(self
, instance_vnf_id
):
1126 @retry(table
='instance_vms')
1128 def new_instance_vm(self
, instance_vm_dict
, tenant_id
, instance_scenario_id
= None):
1129 #return self.new_row('vms', vm_dict, tenant_id, True, True)
1130 return self
._new
_row
_internal
('instance_vms', instance_vm_dict
, tenant_id
, add_uuid
=True, root_uuid
=instance_scenario_id
, log
=True)
1132 def update_instance_vm(self
, instance_vm_dict
):
1136 def delete_instance_vm(self
, instance_vm_id
):
1140 @retry(table
='instance_nets')
1142 def new_instance_net(self
, instance_net_dict
, tenant_id
, instance_scenario_id
= None):
1143 return self
._new
_row
_internal
('instance_nets', instance_net_dict
, tenant_id
, add_uuid
=True, root_uuid
=instance_scenario_id
, log
=True)
1145 def update_instance_net(self
, instance_net_dict
):
1149 def delete_instance_net(self
, instance_net_id
):
1153 @retry(table
='instance_interfaces')
1155 def new_instance_interface(self
, instance_interface_dict
, tenant_id
, instance_scenario_id
= None):
1156 return self
._new
_row
_internal
('instance_interfaces', instance_interface_dict
, tenant_id
, add_uuid
=True, root_uuid
=instance_scenario_id
, log
=True)
1158 def update_instance_interface(self
, instance_interface_dict
):
1162 def delete_instance_interface(self
, instance_interface_dict
):
1166 @retry(table
='datacenter_nets')
1168 def update_datacenter_nets(self
, datacenter_id
, new_net_list
=[]):
1169 ''' Removes the old and adds the new net list at datacenter list for one datacenter.
1171 datacenter_id: uuid of the datacenter to act upon
1172 table: table where to insert
1173 new_net_list: the new values to be inserted. If empty it only deletes the existing nets
1174 Return: (Inserted items, Deleted items) if OK, (-Error, text) if error
1176 created_time
= time
.time()
1177 cmd
="DELETE FROM datacenter_nets WHERE datacenter_id='{}'".format(datacenter_id
)
1178 self
.logger
.debug(cmd
)
1179 self
.cur
.execute(cmd
)
1180 deleted
= self
.cur
.rowcount
1182 for new_net
in new_net_list
:
1183 created_time
+= 0.00001
1184 self
._new
_row
_internal
('datacenter_nets', new_net
, add_uuid
=True, created_time
=created_time
)
1186 return inserted
, deleted