Merge branch 'py3' features 8029 8030
[osm/RO.git] / RO / osm_ro / nfvo_db.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
5 # This file is part of openmano
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
24 """
25 NFVO DB engine. It implements all the methods to interact with the Openmano Database
26 """
27 __author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
28 __date__ ="$28-aug-2014 10:05:01$"
29
30 from osm_ro import db_base
31 import MySQLdb as mdb
32 import json
33 import yaml
34 import time
35 #import sys, os
36
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
40
41
42 _ATTEMPT = Attempt()
43
44
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",
52 "instance_wim_nets"]
53
54
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
61 return
62
63 @retry
64 @with_transaction
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()
68 myVNFDict = {}
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")
75
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.
79 vmDict = {}
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'])
85 vm["vnf_id"] = vnf_id
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
90
91 #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field
92 bridgeInterfacesDict = {}
93 for vm in vnf_descriptor['vnf']['VNFC']:
94 if 'bridge-ifaces' in vm:
95 bridgeInterfacesDict[vm['name']] = {}
96 for bridgeiface in vm['bridge-ifaces']:
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
113
114 # Collect the data interfaces of each VM/VNFC under the 'numas' field
115 dataifacesDict = {}
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
129
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'])
136
137 myNetDict = {}
138 myNetDict["name"] = net['name']
139 myNetDict["description"] = net['description']
140 myNetDict["type"] = net['type']
141 myNetDict["vnf_id"] = vnf_id
142
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)
145
146 for element in net['elements']:
147 ifaceItem = {}
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']
160 else:
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
171
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
177
178 #print "Adding external interfaces to the NFVO database"
179 for iface in vnf_descriptor['vnf']['external-connections']:
180 myIfaceDict = {}
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']
193 else:
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
205
206 return vnf_id
207
208 @retry
209 @with_transaction
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()
213 myVNFDict = {}
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")
220
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.
224 vmDict = {}
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
235
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')
248 ifaceDict = {}
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
257
258 # Collect the data interfaces of each VM/VNFC under the 'numas' field
259 dataifacesDict = {}
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)
266 ifaceDict = {}
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
273
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'])
279
280 myNetDict = {}
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"):
288 net["type"] = "ptp"
289 elif (net["type"] == "e-lan"):
290 net["type"] = "data"
291 net.pop("implementation")
292 myNetDict["type"] = net['type']
293 myNetDict["vnf_id"] = vnf_id
294
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)
297
298 if "ip-profile" in net:
299 ip_profile = net["ip-profile"]
300 myIPProfileDict = {}
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)
310
311 created_time += 0.00001
312 ip_profile_id = self._new_row_internal('ip_profiles', myIPProfileDict)
313
314 for element in net['elements']:
315 ifaceItem = {}
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']
328 else:
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
340
341 #print "Adding external interfaces to the NFVO database"
342 for iface in vnf_descriptor['vnf']['external-connections']:
343 myIfaceDict = {}
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']
355 else:
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
368
369 return vnf_id
370
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)
375 # raise KeyError
376
377 @retry
378 @with_transaction
379 def new_scenario(self, scenario_dict):
380 created_time = time.time()
381 tenant_id = scenario_dict.get('tenant_id')
382 #scenario
383 INSERT_={'tenant_id': tenant_id,
384 'name': scenario_dict['name'],
385 'description': scenario_dict['description'],
386 'public': scenario_dict.get('public', "false")}
387
388 scenario_uuid = self._new_row_internal('scenarios', INSERT_, add_uuid=True, root_uuid=None, created_time=created_time)
389 #sce_nets
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)
396 if "graph" in net:
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)
402 net['uuid']=net_uuid
403
404 if net.get("ip-profile"):
405 ip_profile = net["ip-profile"]
406 myIPProfileDict = {
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)
417
418 # sce_vnfs
419 for k, vnf in scenario_dict['vnfs'].items():
420 INSERT_ = {'scenario_id': scenario_uuid,
421 'name': k,
422 'vnf_id': vnf['uuid'],
423 # 'description': scenario_dict['name']
424 'description': vnf['description']}
425 if "graph" in vnf:
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
433 # sce_interfaces
434 for iface in vnf['ifaces'].values():
435 # print 'iface', iface
436 if 'net_key' not in iface:
437 continue
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)
446
447 return scenario_uuid
448
449 @retry
450 @with_transaction
451 def edit_scenario(self, scenario_dict):
452 modified_time = time.time()
453 item_changed=0
454 #check that scenario exist
455 tenant_id = scenario_dict.get('tenant_id')
456 scenario_uuid = scenario_dict['uuid']
457
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)
464 self.cur.fetchall()
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)
469
470 #scenario
471 nodes = {}
472 topology = scenario_dict.pop("topology", None)
473 if topology != None and "nodes" in topology:
474 nodes = topology.get("nodes",{})
475 UPDATE_ = {}
476 if "name" in scenario_dict: UPDATE_["name"] = scenario_dict["name"]
477 if "description" in scenario_dict: UPDATE_["description"] = scenario_dict["description"]
478 if len(UPDATE_)>0:
479 WHERE_={'tenant_id': tenant_id, 'uuid': scenario_uuid}
480 item_changed += self._update_rows('scenarios', UPDATE_, WHERE_, modified_time=modified_time)
481 #sce_nets
482 for node_id, node in nodes.items():
483 if "graph" in node:
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)
491 return item_changed
492
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
497 # '''
498 # print "1******************************************************************"
499 # try:
500 # with self.transaction(mdb.cursors.DictCursor):
501 # #scenario table
502 # where_list=[]
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 +"'" )
506 # else:
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]
516 #
517 # #instance_vnfs
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']:
521 # #instance_vms
522 # self.cur.execute("SELECT uuid, vim_vm_id "+
523 # "FROM instance_vms "+
524 # "WHERE instance_vnf_id='" + vnf['uuid'] +"'"
525 # )
526 # vnf['instance_vms'] = self.cur.fetchall()
527 # #instance_nets
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()
530 #
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()
534 #
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)
542
543 @retry
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
550 '''
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"]
566 # sce_vnfs
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()
572
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)
580 else:
581 vnf['mgmt_access'] = None
582 # sce_interfaces
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()
589 # vms
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']:
600 if vm["boot_data"]:
601 vm["boot_data"] = yaml.safe_load(vm["boot_data"])
602 else:
603 del vm["boot_data"]
604 if vm["image_list"]:
605 vm["image_list"] = yaml.safe_load(vm["image_list"])
606 else:
607 del vm["image_list"]
608 if datacenter_vim_id!=None:
609 if vm['image_id']:
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']
617 if vm['flavor_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']
625
626 #interfaces
627 cmd = "SELECT uuid,internal_name,external_name,net_id,type,vpci,mac,bw,model,ip_address," \
628 "floating_ip, port_security" \
629 " FROM interfaces" \
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"]
642 break
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)
658
659 #sce_nets
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()
666 #datacenter_nets
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)
678 continue
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']
688 net['vim_id']=None
689 else:
690 net['vim_id']=d_net['vim_net_id']
691
692 db_base._convert_datetime2str(scenario_dict)
693 db_base._convert_str2boolean(scenario_dict, ('public','shared','external','port-security','floating-ip') )
694
695 #forwarding graphs
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()
724
725 return scenario_dict
726
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
733 '''
734 #scenario table
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"]
748
749 #sce_vnfs
750 cmd = "DELETE FROM scenarios WHERE uuid='{}'".format(scenario_uuid)
751 self.logger.debug(cmd)
752 self.cur.execute(cmd)
753
754 return scenario_uuid + " " + scenario_name
755
756 @retry
757 @with_transaction
758 def new_rows(self, tables, uuid_list=None, confidential_data=False, attempt=_ATTEMPT):
759 """
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.:
763 tables = [
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, ... }
767 }
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
770 up and order
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
774 """
775 table_name = None
776 created_time = time.time()
777 for table in tables:
778 for table_name, row_list in table.items():
779 index = 0
780 attempt.info['table'] = table_name
781 if isinstance(row_list, dict):
782 row_list = (row_list, ) #create a list with the single value
783 for row in row_list:
784 if "TO-DELETE" in row:
785 self._delete_row_by_id_internal(table_name, row["TO-DELETE"])
786 continue
787 if table_name in self.tables_with_created_field:
788 if "created_at" in row:
789 created_time_param = created_time + (index + row.pop("created_at"))*0.00001
790 else:
791 created_time_param = created_time + index*0.00001
792 index += 1
793 else:
794 created_time_param = 0
795 self._new_row_internal(table_name, row, add_uuid=False, root_uuid=None,
796 confidential_data=confidential_data,
797 created_time=created_time_param)
798
799 @retry
800 @with_transaction
801 def new_instance_scenario_as_a_whole(self,tenant_id,instance_scenario_name,instance_scenario_description,scenarioDict):
802 created_time = time.time()
803 #instance_scenarios
804 datacenter_id = scenarioDict['datacenter_id']
805 INSERT_={'tenant_id': tenant_id,
806 'datacenter_tenant_id': scenarioDict["datacenter2tenant"][datacenter_id],
807 'name': instance_scenario_name,
808 'description': instance_scenario_description,
809 'scenario_id' : scenarioDict['uuid'],
810 'datacenter_id': datacenter_id
811 }
812 if scenarioDict.get("cloud-config"):
813 INSERT_["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"], default_flow_style=True, width=256)
814
815 instance_uuid = self._new_row_internal('instance_scenarios', INSERT_, add_uuid=True, root_uuid=None, created_time=created_time)
816
817 net_scene2instance={}
818 #instance_nets #nets interVNF
819 for net in scenarioDict['nets']:
820 net_scene2instance[ net['uuid'] ] ={}
821 datacenter_site_id = net.get('datacenter_id', datacenter_id)
822 if not "vim_id_sites" in net:
823 net["vim_id_sites"] ={datacenter_site_id: net['vim_id']}
824 net["vim_id_sites"]["datacenter_site_id"] = {datacenter_site_id: net['vim_id']}
825 sce_net_id = net.get("uuid")
826
827 for datacenter_site_id,vim_id in net["vim_id_sites"].items():
828 INSERT_={'vim_net_id': vim_id, 'created': net.get('created', False), 'instance_scenario_id':instance_uuid } #, 'type': net['type']
829 INSERT_['datacenter_id'] = datacenter_site_id
830 INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_site_id]
831 if not net.get('created', False):
832 INSERT_['status'] = "ACTIVE"
833 if sce_net_id:
834 INSERT_['sce_net_id'] = sce_net_id
835 created_time += 0.00001
836 instance_net_uuid = self._new_row_internal('instance_nets', INSERT_, True, instance_uuid, created_time)
837 net_scene2instance[ sce_net_id ][datacenter_site_id] = instance_net_uuid
838 net['uuid'] = instance_net_uuid #overwrite scnario uuid by instance uuid
839
840 if 'ip_profile' in net:
841 net['ip_profile']['net_id'] = None
842 net['ip_profile']['sce_net_id'] = None
843 net['ip_profile']['instance_net_id'] = instance_net_uuid
844 created_time += 0.00001
845 ip_profile_id = self._new_row_internal('ip_profiles', net['ip_profile'])
846
847 #instance_vnfs
848 for vnf in scenarioDict['vnfs']:
849 datacenter_site_id = vnf.get('datacenter_id', datacenter_id)
850 INSERT_={'instance_scenario_id': instance_uuid, 'vnf_id': vnf['vnf_id'] }
851 INSERT_['datacenter_id'] = datacenter_site_id
852 INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_site_id]
853 if vnf.get("uuid"):
854 INSERT_['sce_vnf_id'] = vnf['uuid']
855 created_time += 0.00001
856 instance_vnf_uuid = self._new_row_internal('instance_vnfs', INSERT_, True, instance_uuid, created_time)
857 vnf['uuid'] = instance_vnf_uuid #overwrite scnario uuid by instance uuid
858
859 #instance_nets #nets intraVNF
860 for net in vnf['nets']:
861 net_scene2instance[ net['uuid'] ] = {}
862 INSERT_={'vim_net_id': net['vim_id'], 'created': net.get('created', False), 'instance_scenario_id':instance_uuid } #, 'type': net['type']
863 INSERT_['datacenter_id'] = net.get('datacenter_id', datacenter_site_id)
864 INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_id]
865 if net.get("uuid"):
866 INSERT_['net_id'] = net['uuid']
867 created_time += 0.00001
868 instance_net_uuid = self._new_row_internal('instance_nets', INSERT_, True, instance_uuid, created_time)
869 net_scene2instance[ net['uuid'] ][datacenter_site_id] = instance_net_uuid
870 net['uuid'] = instance_net_uuid #overwrite scnario uuid by instance uuid
871
872 if 'ip_profile' in net:
873 net['ip_profile']['net_id'] = None
874 net['ip_profile']['sce_net_id'] = None
875 net['ip_profile']['instance_net_id'] = instance_net_uuid
876 created_time += 0.00001
877 ip_profile_id = self._new_row_internal('ip_profiles', net['ip_profile'])
878
879 #instance_vms
880 for vm in vnf['vms']:
881 INSERT_={'instance_vnf_id': instance_vnf_uuid, 'vm_id': vm['uuid'], 'vim_vm_id': vm['vim_id'] }
882 created_time += 0.00001
883 instance_vm_uuid = self._new_row_internal('instance_vms', INSERT_, True, instance_uuid, created_time)
884 vm['uuid'] = instance_vm_uuid #overwrite scnario uuid by instance uuid
885
886 #instance_interfaces
887 for interface in vm['interfaces']:
888 net_id = interface.get('net_id', None)
889 if net_id is None:
890 #check if is connected to a inter VNFs net
891 for iface in vnf['interfaces']:
892 if iface['interface_id'] == interface['uuid']:
893 if 'ip_address' in iface:
894 interface['ip_address'] = iface['ip_address']
895 net_id = iface.get('sce_net_id', None)
896 break
897 if net_id is None:
898 continue
899 interface_type='external' if interface['external_name'] is not None else 'internal'
900 INSERT_={'instance_vm_id': instance_vm_uuid, 'instance_net_id': net_scene2instance[net_id][datacenter_site_id],
901 'interface_id': interface['uuid'], 'vim_interface_id': interface.get('vim_id'), 'type': interface_type,
902 'ip_address': interface.get('ip_address'), 'floating_ip': int(interface.get('floating-ip',False)),
903 'port_security': int(interface.get('port-security',True))}
904 #created_time += 0.00001
905 interface_uuid = self._new_row_internal('instance_interfaces', INSERT_, True, instance_uuid) #, created_time)
906 interface['uuid'] = interface_uuid #overwrite scnario uuid by instance uuid
907 return instance_uuid
908
909 @retry
910 @with_transaction(cursor='dict')
911 def get_instance_scenario(self, instance_id, tenant_id=None, verbose=False):
912 '''Obtain the instance information, filtering by one or several of the tenant, uuid or name
913 instance_id is the uuid or the name if it is not a valid uuid format
914 Only one instance must mutch the filtering or an error is returned
915 '''
916 # instance table
917 where_list = []
918 if tenant_id:
919 where_list.append("inst.tenant_id='{}'".format(tenant_id))
920 if db_base._check_valid_uuid(instance_id):
921 where_list.append("inst.uuid='{}'".format(instance_id))
922 else:
923 where_list.append("inst.name='{}'".format(instance_id))
924 where_text = " AND ".join(where_list)
925 cmd = "SELECT inst.uuid as uuid, inst.name as name, inst.scenario_id as scenario_id, datacenter_id"\
926 " ,datacenter_tenant_id, s.name as scenario_name,inst.tenant_id as tenant_id" \
927 " ,inst.description as description, inst.created_at as created_at" \
928 " ,inst.cloud_config as cloud_config, s.osm_id as nsd_osm_id" \
929 " FROM instance_scenarios as inst left join scenarios as s on inst.scenario_id=s.uuid" \
930 " WHERE " + where_text
931 self.logger.debug(cmd)
932 self.cur.execute(cmd)
933 rows = self.cur.fetchall()
934
935 if self.cur.rowcount == 0:
936 raise db_base.db_base_Exception("No instance found where " + where_text, httperrors.Not_Found)
937 elif self.cur.rowcount > 1:
938 raise db_base.db_base_Exception("More than one instance found where " + where_text,
939 httperrors.Bad_Request)
940 instance_dict = rows[0]
941 if instance_dict["cloud_config"]:
942 instance_dict["cloud-config"] = yaml.load(instance_dict["cloud_config"], Loader=yaml.Loader)
943 del instance_dict["cloud_config"]
944
945 # instance_vnfs
946 cmd = "SELECT iv.uuid as uuid, iv.vnf_id as vnf_id, sv.name as vnf_name, sce_vnf_id, datacenter_id"\
947 ", datacenter_tenant_id, v.mgmt_access, sv.member_vnf_index, v.osm_id as vnfd_osm_id "\
948 "FROM instance_vnfs as iv left join sce_vnfs as sv "\
949 " on iv.sce_vnf_id=sv.uuid join vnfs as v on iv.vnf_id=v.uuid " \
950 "WHERE iv.instance_scenario_id='{}' " \
951 "ORDER BY iv.created_at ".format(instance_dict['uuid'])
952 self.logger.debug(cmd)
953 self.cur.execute(cmd)
954 instance_dict['vnfs'] = self.cur.fetchall()
955 for vnf in instance_dict['vnfs']:
956 vnf["ip_address"] = None
957 vnf_mgmt_access_iface = None
958 vnf_mgmt_access_vm = None
959 if vnf["mgmt_access"]:
960 vnf_mgmt_access = yaml.load(vnf["mgmt_access"], Loader=yaml.Loader)
961 vnf_mgmt_access_iface = vnf_mgmt_access.get("interface_id")
962 vnf_mgmt_access_vm = vnf_mgmt_access.get("vm_id")
963 vnf["ip_address"] = vnf_mgmt_access.get("ip-address")
964
965 # instance vms
966 cmd = "SELECT iv.uuid as uuid, vim_vm_id, status, error_msg, vim_info, iv.created_at as "\
967 "created_at, name, vms.osm_id as vdu_osm_id, vim_name, vms.uuid as vm_uuid, related"\
968 " FROM instance_vms as iv join vms on iv.vm_id=vms.uuid "\
969 " WHERE instance_vnf_id='{}' ORDER BY iv.created_at".format(vnf['uuid'])
970 self.logger.debug(cmd)
971 self.cur.execute(cmd)
972 vnf['vms'] = self.cur.fetchall()
973 for vm in vnf['vms']:
974 vm_manage_iface_list=[]
975 # instance_interfaces
976 cmd = "SELECT vim_interface_id, instance_net_id, internal_name,external_name, mac_address,"\
977 " ii.ip_address as ip_address, vim_info, i.type as type, sdn_port_id, i.uuid"\
978 " FROM instance_interfaces as ii join interfaces as i on ii.interface_id=i.uuid"\
979 " WHERE instance_vm_id='{}' ORDER BY i.created_at".format(vm['uuid'])
980 self.logger.debug(cmd)
981 self.cur.execute(cmd )
982 vm['interfaces'] = self.cur.fetchall()
983 for iface in vm['interfaces']:
984 if vnf_mgmt_access_iface and vnf_mgmt_access_iface == iface["uuid"]:
985 if not vnf["ip_address"]:
986 vnf["ip_address"] = iface["ip_address"]
987 if iface["type"] == "mgmt" and iface["ip_address"]:
988 vm_manage_iface_list.append(iface["ip_address"])
989 if not verbose:
990 del iface["type"]
991 del iface["uuid"]
992 if vm_manage_iface_list:
993 vm["ip_address"] = ",".join(vm_manage_iface_list)
994 if not vnf["ip_address"] and vnf_mgmt_access_vm == vm["vm_uuid"]:
995 vnf["ip_address"] = vm["ip_address"]
996 del vm["vm_uuid"]
997
998 #instance_nets
999 #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"
1000 #from_text = "instance_nets join instance_scenarios on instance_nets.instance_scenario_id=instance_scenarios.uuid " + \
1001 # "join sce_nets on instance_scenarios.scenario_id=sce_nets.scenario_id"
1002 #where_text = "instance_nets.instance_scenario_id='"+ instance_dict['uuid'] + "'"
1003 cmd = "SELECT inets.uuid as uuid,vim_net_id,status,error_msg,vim_info,created, sce_net_id, " \
1004 "net_id as vnf_net_id, datacenter_id, datacenter_tenant_id, sdn_net_id, " \
1005 "snets.osm_id as ns_net_osm_id, nets.osm_id as vnf_net_osm_id, inets.vim_name, related " \
1006 "FROM instance_nets as inets left join sce_nets as snets on inets.sce_net_id=snets.uuid " \
1007 "left join nets on inets.net_id=nets.uuid " \
1008 "WHERE instance_scenario_id='{}' ORDER BY inets.created_at".format(instance_dict['uuid'])
1009 self.logger.debug(cmd)
1010 self.cur.execute(cmd)
1011 instance_dict['nets'] = self.cur.fetchall()
1012
1013 # instance sdn_nets:
1014 cmd = "SELECT * FROM instance_wim_nets WHERE instance_scenario_id='{}' ORDER BY created_at;".format(
1015 instance_dict['uuid'])
1016 self.logger.debug(cmd)
1017 self.cur.execute(cmd)
1018 instance_dict['sdn_nets'] = self.cur.fetchall()
1019
1020 #instance_sfps
1021 cmd = "SELECT uuid,vim_sfp_id,sce_rsp_id,datacenter_id,"\
1022 "datacenter_tenant_id,status,error_msg,vim_info, related"\
1023 " FROM instance_sfps" \
1024 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict['uuid'])
1025 self.logger.debug(cmd)
1026 self.cur.execute(cmd)
1027 instance_dict['sfps'] = self.cur.fetchall()
1028
1029 # for sfp in instance_dict['sfps']:
1030 #instance_sfs
1031 cmd = "SELECT uuid,vim_sf_id,sce_rsp_hop_id,datacenter_id,"\
1032 "datacenter_tenant_id,status,error_msg,vim_info, related"\
1033 " FROM instance_sfs" \
1034 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict['uuid']) # TODO: replace instance_scenario_id with instance_sfp_id
1035 self.logger.debug(cmd)
1036 self.cur.execute(cmd)
1037 instance_dict['sfs'] = self.cur.fetchall()
1038
1039 #for sf in instance_dict['sfs']:
1040 #instance_sfis
1041 cmd = "SELECT uuid,vim_sfi_id,sce_rsp_hop_id,datacenter_id,"\
1042 "datacenter_tenant_id,status,error_msg,vim_info, related"\
1043 " FROM instance_sfis" \
1044 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict['uuid']) # TODO: replace instance_scenario_id with instance_sf_id
1045 self.logger.debug(cmd)
1046 self.cur.execute(cmd)
1047 instance_dict['sfis'] = self.cur.fetchall()
1048 # for sfi in instance_dict['sfi']:
1049
1050 #instance_classifications
1051 cmd = "SELECT uuid,vim_classification_id,sce_classifier_match_id,datacenter_id,"\
1052 "datacenter_tenant_id,status,error_msg,vim_info, related"\
1053 " FROM instance_classifications" \
1054 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict['uuid'])
1055 self.logger.debug(cmd)
1056 self.cur.execute(cmd)
1057 instance_dict['classifications'] = self.cur.fetchall()
1058 # for classification in instance_dict['classifications']
1059
1060 db_base._convert_datetime2str(instance_dict)
1061 db_base._convert_str2boolean(instance_dict, ('public','shared','created') )
1062 return instance_dict
1063
1064 @retry(command='delete', extra='No dependences can avoid deleting!!!!')
1065 @with_transaction(cursor='dict')
1066 def delete_instance_scenario(self, instance_id, tenant_id=None):
1067 '''Deletes a instance_Scenario, filtering by one or several of the tenant, uuid or name
1068 instance_id is the uuid or the name if it is not a valid uuid format
1069 Only one instance_scenario must mutch the filtering or an error is returned
1070 '''
1071 #instance table
1072 where_list=[]
1073 if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" )
1074 if db_base._check_valid_uuid(instance_id):
1075 where_list.append( "uuid='" + instance_id +"'" )
1076 else:
1077 where_list.append( "name='" + instance_id +"'" )
1078 where_text = " AND ".join(where_list)
1079 cmd = "SELECT * FROM instance_scenarios WHERE "+ where_text
1080 self.logger.debug(cmd)
1081 self.cur.execute(cmd)
1082 rows = self.cur.fetchall()
1083
1084 if self.cur.rowcount==0:
1085 raise db_base.db_base_Exception("No instance found where " + where_text, httperrors.Bad_Request)
1086 elif self.cur.rowcount>1:
1087 raise db_base.db_base_Exception("More than one instance found where " + where_text, httperrors.Bad_Request)
1088 instance_uuid = rows[0]["uuid"]
1089 instance_name = rows[0]["name"]
1090
1091 #sce_vnfs
1092 cmd = "DELETE FROM instance_scenarios WHERE uuid='{}'".format(instance_uuid)
1093 self.logger.debug(cmd)
1094 self.cur.execute(cmd)
1095
1096 return instance_uuid + " " + instance_name
1097
1098 @retry(table='instance_scenarios')
1099 @with_transaction
1100 def new_instance_scenario(self, instance_scenario_dict, tenant_id):
1101 #return self.new_row('vnfs', vnf_dict, None, tenant_id, True, True)
1102 return self._new_row_internal('instance_scenarios', instance_scenario_dict, tenant_id, add_uuid=True, root_uuid=None, log=True)
1103
1104 def update_instance_scenario(self, instance_scenario_dict):
1105 #TODO:
1106 return
1107
1108 @retry(table='instance_vnfs')
1109 @with_transaction
1110 def new_instance_vnf(self, instance_vnf_dict, tenant_id, instance_scenario_id = None):
1111 #return self.new_row('vms', vm_dict, tenant_id, True, True)
1112 return self._new_row_internal('instance_vnfs', instance_vnf_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
1113
1114 def update_instance_vnf(self, instance_vnf_dict):
1115 #TODO:
1116 return
1117
1118 def delete_instance_vnf(self, instance_vnf_id):
1119 #TODO:
1120 return
1121
1122 @retry(table='instance_vms')
1123 @with_transaction
1124 def new_instance_vm(self, instance_vm_dict, tenant_id, instance_scenario_id = None):
1125 #return self.new_row('vms', vm_dict, tenant_id, True, True)
1126 return self._new_row_internal('instance_vms', instance_vm_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
1127
1128 def update_instance_vm(self, instance_vm_dict):
1129 #TODO:
1130 return
1131
1132 def delete_instance_vm(self, instance_vm_id):
1133 #TODO:
1134 return
1135
1136 @retry(table='instance_nets')
1137 @with_transaction
1138 def new_instance_net(self, instance_net_dict, tenant_id, instance_scenario_id = None):
1139 return self._new_row_internal('instance_nets', instance_net_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
1140
1141 def update_instance_net(self, instance_net_dict):
1142 #TODO:
1143 return
1144
1145 def delete_instance_net(self, instance_net_id):
1146 #TODO:
1147 return
1148
1149 @retry(table='instance_interfaces')
1150 @with_transaction
1151 def new_instance_interface(self, instance_interface_dict, tenant_id, instance_scenario_id = None):
1152 return self._new_row_internal('instance_interfaces', instance_interface_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
1153
1154 def update_instance_interface(self, instance_interface_dict):
1155 #TODO:
1156 return
1157
1158 def delete_instance_interface(self, instance_interface_dict):
1159 #TODO:
1160 return
1161
1162 @retry(table='datacenter_nets')
1163 @with_transaction
1164 def update_datacenter_nets(self, datacenter_id, new_net_list=[]):
1165 ''' Removes the old and adds the new net list at datacenter list for one datacenter.
1166 Attribute
1167 datacenter_id: uuid of the datacenter to act upon
1168 table: table where to insert
1169 new_net_list: the new values to be inserted. If empty it only deletes the existing nets
1170 Return: (Inserted items, Deleted items) if OK, (-Error, text) if error
1171 '''
1172 created_time = time.time()
1173 cmd="DELETE FROM datacenter_nets WHERE datacenter_id='{}'".format(datacenter_id)
1174 self.logger.debug(cmd)
1175 self.cur.execute(cmd)
1176 deleted = self.cur.rowcount
1177 inserted = 0
1178 for new_net in new_net_list:
1179 created_time += 0.00001
1180 self._new_row_internal('datacenter_nets', new_net, add_uuid=True, created_time=created_time)
1181 inserted += 1
1182 return inserted, deleted