Implemented option to disable port-security for openstack. Implemented option to...
[osm/RO.git] / nfvo_db.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
24 '''
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 import db_base
31 import MySQLdb as mdb
32 import json
33 import yaml
34 import time
35 #import sys, os
36
37 tables_with_createdat_field=["datacenters","instance_nets","instance_scenarios","instance_vms","instance_vnfs",
38 "interfaces","nets","nfvo_tenants","scenarios","sce_interfaces","sce_nets",
39 "sce_vnfs","tenants_datacenters","datacenter_tenants","vms","vnfs", "datacenter_nets"]
40
41 class nfvo_db(db_base.db_base):
42 def __init__(self, host=None, user=None, passwd=None, database=None, log_name='openmano.db', log_level=None):
43 db_base.db_base.__init__(self, host, user, passwd, database, log_name, log_level)
44 db_base.db_base.tables_with_created_field=tables_with_createdat_field
45 return
46
47 def new_vnf_as_a_whole(self,nfvo_tenant,vnf_name,vnf_descriptor,VNFCDict):
48 self.logger.debug("Adding new vnf to the NFVO database")
49 tries = 2
50 while tries:
51 created_time = time.time()
52 try:
53 with self.con:
54
55 myVNFDict = {}
56 myVNFDict["name"] = vnf_name
57 myVNFDict["descriptor"] = vnf_descriptor['vnf'].get('descriptor')
58 myVNFDict["public"] = vnf_descriptor['vnf'].get('public', "false")
59 myVNFDict["description"] = vnf_descriptor['vnf']['description']
60 myVNFDict["class"] = vnf_descriptor['vnf'].get('class',"MISC")
61 myVNFDict["tenant_id"] = vnf_descriptor['vnf'].get("tenant_id")
62
63 vnf_id = self._new_row_internal('vnfs', myVNFDict, add_uuid=True, root_uuid=None, created_time=created_time)
64 #print "Adding new vms to the NFVO database"
65 #For each vm, we must create the appropriate vm in the NFVO database.
66 vmDict = {}
67 for _,vm in VNFCDict.iteritems():
68 #This code could make the name of the vms grow and grow.
69 #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name
70 #vm['name'] = "%s-%s" % (vnf_name,vm['name'])
71 #print "VM name: %s. Description: %s" % (vm['name'], vm['description'])
72 vm["vnf_id"] = vnf_id
73 created_time += 0.00001
74 vm_id = self._new_row_internal('vms', vm, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
75 #print "Internal vm id in NFVO DB: %s" % vm_id
76 vmDict[vm['name']] = vm_id
77
78 #Collect the data interfaces of each VM/VNFC under the 'numas' field
79 dataifacesDict = {}
80 for vm in vnf_descriptor['vnf']['VNFC']:
81 dataifacesDict[vm['name']] = {}
82 for numa in vm.get('numas', []):
83 for dataiface in numa.get('interfaces',[]):
84 db_base._convert_bandwidth(dataiface, logger=self.logger)
85 dataifacesDict[vm['name']][dataiface['name']] = {}
86 dataifacesDict[vm['name']][dataiface['name']]['vpci'] = dataiface['vpci']
87 dataifacesDict[vm['name']][dataiface['name']]['bw'] = dataiface['bandwidth']
88 dataifacesDict[vm['name']][dataiface['name']]['model'] = "PF" if dataiface['dedicated']=="yes" else ("VF" if dataiface['dedicated']=="no" else "VFnotShared")
89
90 #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field
91 bridgeInterfacesDict = {}
92 for vm in vnf_descriptor['vnf']['VNFC']:
93 if 'bridge-ifaces' in vm:
94 bridgeInterfacesDict[vm['name']] = {}
95 for bridgeiface in vm['bridge-ifaces']:
96 if 'port-security' in bridgeiface:
97 bridgeiface['port_security'] = bridgeiface.pop('port-security')
98 if 'floating-ip' in bridgeiface:
99 bridgeiface['floating_ip'] = bridgeiface.pop('floating-ip')
100 db_base._convert_bandwidth(bridgeiface, logger=self.logger)
101 bridgeInterfacesDict[vm['name']][bridgeiface['name']] = {}
102 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['vpci'] = bridgeiface.get('vpci',None)
103 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['mac'] = bridgeiface.get('mac_address',None)
104 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['bw'] = bridgeiface.get('bandwidth', None)
105 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['model'] = bridgeiface.get('model', None)
106 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['port_security'] = \
107 int(bridgeiface.get('port_security', True))
108 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['floating_ip'] = \
109 int(bridgeiface.get('floating_ip', False))
110
111 #For each internal connection, we add it to the interfaceDict and we create the appropriate net in the NFVO database.
112 #print "Adding new nets (VNF internal nets) to the NFVO database (if any)"
113 internalconnList = []
114 if 'internal-connections' in vnf_descriptor['vnf']:
115 for net in vnf_descriptor['vnf']['internal-connections']:
116 #print "Net name: %s. Description: %s" % (net['name'], net['description'])
117
118 myNetDict = {}
119 myNetDict["name"] = net['name']
120 myNetDict["description"] = net['description']
121 myNetDict["type"] = net['type']
122 myNetDict["vnf_id"] = vnf_id
123
124 created_time += 0.00001
125 net_id = self._new_row_internal('nets', myNetDict, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
126
127 for element in net['elements']:
128 ifaceItem = {}
129 #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])
130 ifaceItem["internal_name"] = element['local_iface_name']
131 #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])]
132 ifaceItem["vm_id"] = vmDict[element['VNFC']]
133 ifaceItem["net_id"] = net_id
134 ifaceItem["type"] = net['type']
135 if ifaceItem ["type"] == "data":
136 ifaceItem["vpci"] = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['vpci']
137 ifaceItem["bw"] = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['bw']
138 ifaceItem["model"] = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['model']
139 else:
140 ifaceItem["vpci"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['vpci']
141 ifaceItem["mac"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['mac_address']
142 ifaceItem["bw"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['bw']
143 ifaceItem["model"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['model']
144 ifaceItem["port_security"] = \
145 bridgeInterfacesDict[element['VNFC']][element['local_iface_name']]['port_security']
146 ifaceItem["floating_ip"] = \
147 bridgeInterfacesDict[element['VNFC']][element['local_iface_name']]['floating_ip']
148 internalconnList.append(ifaceItem)
149 #print "Internal net id in NFVO DB: %s" % net_id
150
151 #print "Adding internal interfaces to the NFVO database (if any)"
152 for iface in internalconnList:
153 print "Iface name: %s" % iface['internal_name']
154 created_time += 0.00001
155 iface_id = self._new_row_internal('interfaces', iface, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
156 #print "Iface id in NFVO DB: %s" % iface_id
157
158 #print "Adding external interfaces to the NFVO database"
159 for iface in vnf_descriptor['vnf']['external-connections']:
160 myIfaceDict = {}
161 #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])
162 myIfaceDict["internal_name"] = iface['local_iface_name']
163 #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])]
164 myIfaceDict["vm_id"] = vmDict[iface['VNFC']]
165 myIfaceDict["external_name"] = iface['name']
166 myIfaceDict["type"] = iface['type']
167 if iface["type"] == "data":
168 myIfaceDict["vpci"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci']
169 myIfaceDict["bw"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw']
170 myIfaceDict["model"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model']
171 else:
172 myIfaceDict["vpci"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci']
173 myIfaceDict["bw"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw']
174 myIfaceDict["model"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model']
175 myIfaceDict["mac"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['mac']
176 myIfaceDict["port_security"] = \
177 bridgeInterfacesDict[iface['VNFC']][iface['local_iface_name']]['port_security']
178 myIfaceDict["floating_ip"] = \
179 bridgeInterfacesDict[iface['VNFC']][iface['local_iface_name']]['floating_ip']
180 print "Iface name: %s" % iface['name']
181 created_time += 0.00001
182 iface_id = self._new_row_internal('interfaces', myIfaceDict, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
183 #print "Iface id in NFVO DB: %s" % iface_id
184
185 return vnf_id
186
187 except (mdb.Error, AttributeError) as e:
188 self._format_error(e, tries)
189 tries -= 1
190
191 def new_vnf_as_a_whole2(self,nfvo_tenant,vnf_name,vnf_descriptor,VNFCDict):
192 self.logger.debug("Adding new vnf to the NFVO database")
193 tries = 2
194 while tries:
195 created_time = time.time()
196 try:
197 with self.con:
198
199 myVNFDict = {}
200 myVNFDict["name"] = vnf_name
201 myVNFDict["descriptor"] = vnf_descriptor['vnf'].get('descriptor')
202 myVNFDict["public"] = vnf_descriptor['vnf'].get('public', "false")
203 myVNFDict["description"] = vnf_descriptor['vnf']['description']
204 myVNFDict["class"] = vnf_descriptor['vnf'].get('class',"MISC")
205 myVNFDict["tenant_id"] = vnf_descriptor['vnf'].get("tenant_id")
206
207 vnf_id = self._new_row_internal('vnfs', myVNFDict, add_uuid=True, root_uuid=None, created_time=created_time)
208 #print "Adding new vms to the NFVO database"
209 #For each vm, we must create the appropriate vm in the NFVO database.
210 vmDict = {}
211 for _,vm in VNFCDict.iteritems():
212 #This code could make the name of the vms grow and grow.
213 #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name
214 #vm['name'] = "%s-%s" % (vnf_name,vm['name'])
215 #print "VM name: %s. Description: %s" % (vm['name'], vm['description'])
216 vm["vnf_id"] = vnf_id
217 created_time += 0.00001
218 vm_id = self._new_row_internal('vms', vm, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
219 #print "Internal vm id in NFVO DB: %s" % vm_id
220 vmDict[vm['name']] = vm_id
221
222 #Collect the data interfaces of each VM/VNFC under the 'numas' field
223 dataifacesDict = {}
224 for vm in vnf_descriptor['vnf']['VNFC']:
225 dataifacesDict[vm['name']] = {}
226 for numa in vm.get('numas', []):
227 for dataiface in numa.get('interfaces',[]):
228 db_base._convert_bandwidth(dataiface, logger=self.logger)
229 dataifacesDict[vm['name']][dataiface['name']] = {}
230 dataifacesDict[vm['name']][dataiface['name']]['vpci'] = dataiface['vpci']
231 dataifacesDict[vm['name']][dataiface['name']]['bw'] = dataiface['bandwidth']
232 dataifacesDict[vm['name']][dataiface['name']]['model'] = "PF" if dataiface['dedicated']=="yes" else ("VF" if dataiface['dedicated']=="no" else "VFnotShared")
233
234 #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field
235 bridgeInterfacesDict = {}
236 for vm in vnf_descriptor['vnf']['VNFC']:
237 if 'bridge-ifaces' in vm:
238 bridgeInterfacesDict[vm['name']] = {}
239 for bridgeiface in vm['bridge-ifaces']:
240 db_base._convert_bandwidth(bridgeiface, logger=self.logger)
241 if 'port-security' in bridgeiface:
242 bridgeiface['port_security'] = bridgeiface.pop('port-security')
243 if 'floating-ip' in bridgeiface:
244 bridgeiface['floating_ip'] = bridgeiface.pop('floating-ip')
245 bridgeInterfacesDict[vm['name']][bridgeiface['name']] = {}
246 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['vpci'] = bridgeiface.get('vpci',None)
247 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['mac'] = bridgeiface.get('mac_address',None)
248 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['bw'] = bridgeiface.get('bandwidth', None)
249 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['model'] = bridgeiface.get('model', None)
250 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['port_security'] = \
251 int(bridgeiface.get('port_security', True))
252 bridgeInterfacesDict[vm['name']][bridgeiface['name']]['floating_ip'] = \
253 int(bridgeiface.get('floating_ip', False))
254
255 #For each internal connection, we add it to the interfaceDict and we create the appropriate net in the NFVO database.
256 #print "Adding new nets (VNF internal nets) to the NFVO database (if any)"
257 internalconnList = []
258 if 'internal-connections' in vnf_descriptor['vnf']:
259 for net in vnf_descriptor['vnf']['internal-connections']:
260 #print "Net name: %s. Description: %s" % (net['name'], net['description'])
261
262 myNetDict = {}
263 myNetDict["name"] = net['name']
264 myNetDict["description"] = net['description']
265 if (net["implementation"] == "overlay"):
266 net["type"] = "bridge"
267 #It should give an error if the type is e-line. For the moment, we consider it as a bridge
268 elif (net["implementation"] == "underlay"):
269 if (net["type"] == "e-line"):
270 net["type"] = "ptp"
271 elif (net["type"] == "e-lan"):
272 net["type"] = "data"
273 net.pop("implementation")
274 myNetDict["type"] = net['type']
275 myNetDict["vnf_id"] = vnf_id
276
277 created_time += 0.00001
278 net_id = self._new_row_internal('nets', myNetDict, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
279
280 if "ip-profile" in net:
281 ip_profile = net["ip-profile"]
282 myIPProfileDict = {}
283 myIPProfileDict["net_id"] = net_id
284 myIPProfileDict["ip_version"] = ip_profile.get('ip-version',"IPv4")
285 myIPProfileDict["subnet_address"] = ip_profile.get('subnet-address',None)
286 myIPProfileDict["gateway_address"] = ip_profile.get('gateway-address',None)
287 myIPProfileDict["dns_address"] = ip_profile.get('dns-address',None)
288 if ("dhcp" in ip_profile):
289 myIPProfileDict["dhcp_enabled"] = ip_profile["dhcp"].get('enabled',"true")
290 myIPProfileDict["dhcp_start_address"] = ip_profile["dhcp"].get('start-address',None)
291 myIPProfileDict["dhcp_count"] = ip_profile["dhcp"].get('count',None)
292
293 created_time += 0.00001
294 ip_profile_id = self._new_row_internal('ip_profiles', myIPProfileDict)
295
296 for element in net['elements']:
297 ifaceItem = {}
298 #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])
299 ifaceItem["internal_name"] = element['local_iface_name']
300 #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])]
301 ifaceItem["vm_id"] = vmDict[element['VNFC']]
302 ifaceItem["net_id"] = net_id
303 ifaceItem["type"] = net['type']
304 ifaceItem["ip_address"] = element.get('ip_address',None)
305 if ifaceItem ["type"] == "data":
306 ifaceItem["vpci"] = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['vpci']
307 ifaceItem["bw"] = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['bw']
308 ifaceItem["model"] = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['model']
309 else:
310 ifaceItem["vpci"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['vpci']
311 ifaceItem["mac"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['mac']
312 ifaceItem["bw"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['bw']
313 ifaceItem["model"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['model']
314 ifaceItem["port_security"] = \
315 bridgeInterfacesDict[element['VNFC']][element['local_iface_name']]['port_security']
316 ifaceItem["floating_ip"] = \
317 bridgeInterfacesDict[element['VNFC']][element['local_iface_name']]['floating_ip']
318 internalconnList.append(ifaceItem)
319 #print "Internal net id in NFVO DB: %s" % net_id
320
321 #print "Adding internal interfaces to the NFVO database (if any)"
322 for iface in internalconnList:
323 print "Iface name: %s" % iface['internal_name']
324 created_time += 0.00001
325 iface_id = self._new_row_internal('interfaces', iface, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
326 #print "Iface id in NFVO DB: %s" % iface_id
327
328 #print "Adding external interfaces to the NFVO database"
329 for iface in vnf_descriptor['vnf']['external-connections']:
330 myIfaceDict = {}
331 #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])
332 myIfaceDict["internal_name"] = iface['local_iface_name']
333 #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])]
334 myIfaceDict["vm_id"] = vmDict[iface['VNFC']]
335 myIfaceDict["external_name"] = iface['name']
336 myIfaceDict["type"] = iface['type']
337 if iface["type"] == "data":
338 myIfaceDict["vpci"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci']
339 myIfaceDict["bw"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw']
340 myIfaceDict["model"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model']
341 else:
342 myIfaceDict["vpci"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci']
343 myIfaceDict["bw"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw']
344 myIfaceDict["model"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model']
345 myIfaceDict["mac"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['mac']
346 myIfaceDict["port_security"] = \
347 bridgeInterfacesDict[iface['VNFC']][iface['local_iface_name']]['port_security']
348 myIfaceDict["floating_ip"] = \
349 bridgeInterfacesDict[iface['VNFC']][iface['local_iface_name']]['floating_ip']
350 print "Iface name: %s" % iface['name']
351 created_time += 0.00001
352 iface_id = self._new_row_internal('interfaces', myIfaceDict, add_uuid=True, root_uuid=vnf_id, created_time=created_time)
353 #print "Iface id in NFVO DB: %s" % iface_id
354
355 return vnf_id
356
357 except (mdb.Error, AttributeError) as e:
358 self._format_error(e, tries)
359 # except KeyError as e2:
360 # exc_type, exc_obj, exc_tb = sys.exc_info()
361 # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
362 # self.logger.debug("Exception type: %s; Filename: %s; Line number: %s", exc_type, fname, exc_tb.tb_lineno)
363 # raise KeyError
364 tries -= 1
365
366 def new_scenario(self, scenario_dict):
367 tries = 2
368 while tries:
369 created_time = time.time()
370 try:
371 with self.con:
372 self.cur = self.con.cursor()
373 tenant_id = scenario_dict.get('tenant_id')
374 #scenario
375 INSERT_={'tenant_id': tenant_id,
376 'name': scenario_dict['name'],
377 'description': scenario_dict['description'],
378 'public': scenario_dict.get('public', "false")}
379
380 scenario_uuid = self._new_row_internal('scenarios', INSERT_, add_uuid=True, root_uuid=None, created_time=created_time)
381 #sce_nets
382 for net in scenario_dict['nets'].values():
383 net_dict={'scenario_id': scenario_uuid}
384 net_dict["name"] = net["name"]
385 net_dict["type"] = net["type"]
386 net_dict["description"] = net.get("description")
387 net_dict["external"] = net.get("external", False)
388 if "graph" in net:
389 #net["graph"]=yaml.safe_dump(net["graph"],default_flow_style=True,width=256)
390 #TODO, must be json because of the GUI, change to yaml
391 net_dict["graph"]=json.dumps(net["graph"])
392 created_time += 0.00001
393 net_uuid = self._new_row_internal('sce_nets', net_dict, add_uuid=True, root_uuid=scenario_uuid, created_time=created_time)
394 net['uuid']=net_uuid
395 #sce_vnfs
396 for k,vnf in scenario_dict['vnfs'].items():
397 INSERT_={'scenario_id': scenario_uuid,
398 'name': k,
399 'vnf_id': vnf['uuid'],
400 #'description': scenario_dict['name']
401 'description': vnf['description']
402 }
403 if "graph" in vnf:
404 #INSERT_["graph"]=yaml.safe_dump(vnf["graph"],default_flow_style=True,width=256)
405 #TODO, must be json because of the GUI, change to yaml
406 INSERT_["graph"]=json.dumps(vnf["graph"])
407 created_time += 0.00001
408 scn_vnf_uuid = self._new_row_internal('sce_vnfs', INSERT_, add_uuid=True, root_uuid=scenario_uuid, created_time=created_time)
409 vnf['scn_vnf_uuid']=scn_vnf_uuid
410 #sce_interfaces
411 for iface in vnf['ifaces'].values():
412 #print 'iface', iface
413 if 'net_key' not in iface:
414 continue
415 iface['net_id'] = scenario_dict['nets'][ iface['net_key'] ]['uuid']
416 INSERT_={'sce_vnf_id': scn_vnf_uuid,
417 'sce_net_id': iface['net_id'],
418 'interface_id': iface[ 'uuid' ]
419 }
420 created_time += 0.00001
421 iface_uuid = self._new_row_internal('sce_interfaces', INSERT_, add_uuid=True, root_uuid=scenario_uuid, created_time=created_time)
422
423 return scenario_uuid
424
425 except (mdb.Error, AttributeError) as e:
426 self._format_error(e, tries)
427 tries -= 1
428
429 def new_scenario2(self, scenario_dict):
430 tries = 2
431 while tries:
432 created_time = time.time()
433 try:
434 with self.con:
435 self.cur = self.con.cursor()
436 tenant_id = scenario_dict.get('tenant_id')
437 #scenario
438 INSERT_={'tenant_id': tenant_id,
439 'name': scenario_dict['name'],
440 'description': scenario_dict['description'],
441 'public': scenario_dict.get('public', "false")}
442
443 scenario_uuid = self._new_row_internal('scenarios', INSERT_, add_uuid=True, root_uuid=None, created_time=created_time)
444 #sce_nets
445 for net in scenario_dict['nets'].values():
446 net_dict={'scenario_id': scenario_uuid}
447 net_dict["name"] = net["name"]
448 net_dict["type"] = net["type"]
449 net_dict["description"] = net.get("description")
450 net_dict["external"] = net.get("external", False)
451 if "graph" in net:
452 #net["graph"]=yaml.safe_dump(net["graph"],default_flow_style=True,width=256)
453 #TODO, must be json because of the GUI, change to yaml
454 net_dict["graph"]=json.dumps(net["graph"])
455 created_time += 0.00001
456 net_uuid = self._new_row_internal('sce_nets', net_dict, add_uuid=True, root_uuid=scenario_uuid, created_time=created_time)
457 net['uuid']=net_uuid
458
459 if "ip-profile" in net:
460 ip_profile = net["ip-profile"]
461 myIPProfileDict = {}
462 myIPProfileDict["sce_net_id"] = net_uuid
463 myIPProfileDict["ip_version"] = ip_profile.get('ip-version',"IPv4")
464 myIPProfileDict["subnet_address"] = ip_profile.get('subnet-address',None)
465 myIPProfileDict["gateway_address"] = ip_profile.get('gateway-address',None)
466 myIPProfileDict["dns_address"] = ip_profile.get('dns-address',None)
467 if ("dhcp" in ip_profile):
468 myIPProfileDict["dhcp_enabled"] = ip_profile["dhcp"].get('enabled',"true")
469 myIPProfileDict["dhcp_start_address"] = ip_profile["dhcp"].get('start-address',None)
470 myIPProfileDict["dhcp_count"] = ip_profile["dhcp"].get('count',None)
471
472 created_time += 0.00001
473 ip_profile_id = self._new_row_internal('ip_profiles', myIPProfileDict)
474
475 #sce_vnfs
476 for k,vnf in scenario_dict['vnfs'].items():
477 INSERT_={'scenario_id': scenario_uuid,
478 'name': k,
479 'vnf_id': vnf['uuid'],
480 #'description': scenario_dict['name']
481 'description': vnf['description']
482 }
483 if "graph" in vnf:
484 #INSERT_["graph"]=yaml.safe_dump(vnf["graph"],default_flow_style=True,width=256)
485 #TODO, must be json because of the GUI, change to yaml
486 INSERT_["graph"]=json.dumps(vnf["graph"])
487 created_time += 0.00001
488 scn_vnf_uuid = self._new_row_internal('sce_vnfs', INSERT_, add_uuid=True, root_uuid=scenario_uuid, created_time=created_time)
489 vnf['scn_vnf_uuid']=scn_vnf_uuid
490 #sce_interfaces
491 for iface in vnf['ifaces'].values():
492 #print 'iface', iface
493 if 'net_key' not in iface:
494 continue
495 iface['net_id'] = scenario_dict['nets'][ iface['net_key'] ]['uuid']
496 INSERT_={'sce_vnf_id': scn_vnf_uuid,
497 'sce_net_id': iface['net_id'],
498 'interface_id': iface['uuid'],
499 'ip_address': iface['ip_address']
500 }
501 created_time += 0.00001
502 iface_uuid = self._new_row_internal('sce_interfaces', INSERT_, add_uuid=True, root_uuid=scenario_uuid, created_time=created_time)
503
504 return scenario_uuid
505
506 except (mdb.Error, AttributeError) as e:
507 self._format_error(e, tries)
508 # except KeyError as e2:
509 # exc_type, exc_obj, exc_tb = sys.exc_info()
510 # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
511 # self.logger.debug("Exception type: %s; Filename: %s; Line number: %s", exc_type, fname, exc_tb.tb_lineno)
512 # raise KeyError
513 tries -= 1
514
515 def edit_scenario(self, scenario_dict):
516 tries = 2
517 while tries:
518 modified_time = time.time()
519 item_changed=0
520 try:
521 with self.con:
522 self.cur = self.con.cursor()
523 #check that scenario exist
524 tenant_id = scenario_dict.get('tenant_id')
525 scenario_uuid = scenario_dict['uuid']
526
527 where_text = "uuid='{}'".format(scenario_uuid)
528 if not tenant_id and tenant_id != "any":
529 where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id)
530 cmd = "SELECT * FROM scenarios WHERE "+ where_text
531 self.logger.debug(cmd)
532 self.cur.execute(cmd)
533 self.cur.fetchall()
534 if self.cur.rowcount==0:
535 raise db_base.db_base_Exception("No scenario found with this criteria " + where_text, db_base.HTTP_Bad_Request)
536 elif self.cur.rowcount>1:
537 raise db_base.db_base_Exception("More than one scenario found with this criteria " + where_text, db_base.HTTP_Bad_Request)
538
539 #scenario
540 nodes = {}
541 topology = scenario_dict.pop("topology", None)
542 if topology != None and "nodes" in topology:
543 nodes = topology.get("nodes",{})
544 UPDATE_ = {}
545 if "name" in scenario_dict: UPDATE_["name"] = scenario_dict["name"]
546 if "description" in scenario_dict: UPDATE_["description"] = scenario_dict["description"]
547 if len(UPDATE_)>0:
548 WHERE_={'tenant_id': tenant_id, 'uuid': scenario_uuid}
549 item_changed += self._update_rows('scenarios', UPDATE_, WHERE_, modified_time=modified_time)
550 #sce_nets
551 for node_id, node in nodes.items():
552 if "graph" in node:
553 #node["graph"] = yaml.safe_dump(node["graph"],default_flow_style=True,width=256)
554 #TODO, must be json because of the GUI, change to yaml
555 node["graph"] = json.dumps(node["graph"])
556 WHERE_={'scenario_id': scenario_uuid, 'uuid': node_id}
557 #Try to change at sce_nets(version 0 API backward compatibility and sce_vnfs)
558 item_changed += self._update_rows('sce_nets', node, WHERE_)
559 item_changed += self._update_rows('sce_vnfs', node, WHERE_, modified_time=modified_time)
560 return item_changed
561
562 except (mdb.Error, AttributeError) as e:
563 self._format_error(e, tries)
564 tries -= 1
565
566 # def get_instance_scenario(self, instance_scenario_id, tenant_id=None):
567 # '''Obtain the scenario instance information, filtering by one or serveral of the tenant, uuid or name
568 # instance_scenario_id is the uuid or the name if it is not a valid uuid format
569 # Only one scenario isntance must mutch the filtering or an error is returned
570 # '''
571 # print "1******************************************************************"
572 # try:
573 # with self.con:
574 # self.cur = self.con.cursor(mdb.cursors.DictCursor)
575 # #scenario table
576 # where_list=[]
577 # if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" )
578 # if db_base._check_valid_uuid(instance_scenario_id):
579 # where_list.append( "uuid='" + instance_scenario_id +"'" )
580 # else:
581 # where_list.append( "name='" + instance_scenario_id +"'" )
582 # where_text = " AND ".join(where_list)
583 # self.cur.execute("SELECT * FROM instance_scenarios WHERE "+ where_text)
584 # rows = self.cur.fetchall()
585 # if self.cur.rowcount==0:
586 # return -HTTP_Bad_Request, "No scenario instance found with this criteria " + where_text
587 # elif self.cur.rowcount>1:
588 # return -HTTP_Bad_Request, "More than one scenario instance found with this criteria " + where_text
589 # instance_scenario_dict = rows[0]
590 #
591 # #instance_vnfs
592 # self.cur.execute("SELECT uuid,vnf_id FROM instance_vnfs WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
593 # instance_scenario_dict['instance_vnfs'] = self.cur.fetchall()
594 # for vnf in instance_scenario_dict['instance_vnfs']:
595 # #instance_vms
596 # self.cur.execute("SELECT uuid, vim_vm_id "+
597 # "FROM instance_vms "+
598 # "WHERE instance_vnf_id='" + vnf['uuid'] +"'"
599 # )
600 # vnf['instance_vms'] = self.cur.fetchall()
601 # #instance_nets
602 # self.cur.execute("SELECT uuid, vim_net_id FROM instance_nets WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
603 # instance_scenario_dict['instance_nets'] = self.cur.fetchall()
604 #
605 # #instance_interfaces
606 # self.cur.execute("SELECT uuid, vim_interface_id, instance_vm_id, instance_net_id FROM instance_interfaces WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
607 # instance_scenario_dict['instance_interfaces'] = self.cur.fetchall()
608 #
609 # db_base._convert_datetime2str(instance_scenario_dict)
610 # db_base._convert_str2boolean(instance_scenario_dict, ('public','shared','external') )
611 # print "2******************************************************************"
612 # return 1, instance_scenario_dict
613 # except (mdb.Error, AttributeError) as e:
614 # print "nfvo_db.get_instance_scenario DB Exception %d: %s" % (e.args[0], e.args[1])
615 # return self._format_error(e)
616
617 def get_scenario(self, scenario_id, tenant_id=None, datacenter_id=None):
618 '''Obtain the scenario information, filtering by one or serveral of the tenant, uuid or name
619 scenario_id is the uuid or the name if it is not a valid uuid format
620 if datacenter_id is provided, it supply aditional vim_id fields with the matching vim uuid
621 Only one scenario must mutch the filtering or an error is returned
622 '''
623 tries = 2
624 while tries:
625 try:
626 with self.con:
627 self.cur = self.con.cursor(mdb.cursors.DictCursor)
628 where_text = "uuid='{}'".format(scenario_id)
629 if not tenant_id and tenant_id != "any":
630 where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id)
631 cmd = "SELECT * FROM scenarios WHERE " + where_text
632 self.logger.debug(cmd)
633 self.cur.execute(cmd)
634 rows = self.cur.fetchall()
635 if self.cur.rowcount==0:
636 raise db_base.db_base_Exception("No scenario found with this criteria " + where_text, db_base.HTTP_Bad_Request)
637 elif self.cur.rowcount>1:
638 raise db_base.db_base_Exception("More than one scenario found with this criteria " + where_text, db_base.HTTP_Bad_Request)
639 scenario_dict = rows[0]
640 if scenario_dict["cloud_config"]:
641 scenario_dict["cloud-config"] = yaml.load(scenario_dict["cloud_config"])
642 del scenario_dict["cloud_config"]
643 #sce_vnfs
644 cmd = "SELECT uuid,name,vnf_id,description FROM sce_vnfs WHERE scenario_id='{}' ORDER BY created_at".format(scenario_dict['uuid'])
645 self.logger.debug(cmd)
646 self.cur.execute(cmd)
647 scenario_dict['vnfs'] = self.cur.fetchall()
648 for vnf in scenario_dict['vnfs']:
649 #sce_interfaces
650 cmd = "SELECT scei.uuid,scei.sce_net_id,scei.interface_id,i.external_name,scei.ip_address FROM sce_interfaces as scei join interfaces as i on scei.interface_id=i.uuid WHERE scei.sce_vnf_id='{}' ORDER BY scei.created_at".format(vnf['uuid'])
651 self.logger.debug(cmd)
652 self.cur.execute(cmd)
653 vnf['interfaces'] = self.cur.fetchall()
654 #vms
655 cmd = "SELECT vms.uuid as uuid, flavor_id, image_id, vms.name as name, vms.description as description " \
656 " FROM vnfs join vms on vnfs.uuid=vms.vnf_id " \
657 " WHERE vnfs.uuid='" + vnf['vnf_id'] +"'" \
658 " ORDER BY vms.created_at"
659 self.logger.debug(cmd)
660 self.cur.execute(cmd)
661 vnf['vms'] = self.cur.fetchall()
662 for vm in vnf['vms']:
663 if datacenter_id!=None:
664 cmd = "SELECT vim_id FROM datacenters_images WHERE image_id='{}' AND datacenter_id='{}'".format(vm['image_id'],datacenter_id)
665 self.logger.debug(cmd)
666 self.cur.execute(cmd)
667 if self.cur.rowcount==1:
668 vim_image_dict = self.cur.fetchone()
669 vm['vim_image_id']=vim_image_dict['vim_id']
670 cmd = "SELECT vim_id FROM datacenters_flavors WHERE flavor_id='{}' AND datacenter_id='{}'".format(vm['flavor_id'],datacenter_id)
671 self.logger.debug(cmd)
672 self.cur.execute(cmd)
673 if self.cur.rowcount==1:
674 vim_flavor_dict = self.cur.fetchone()
675 vm['vim_flavor_id']=vim_flavor_dict['vim_id']
676
677 #interfaces
678 cmd = "SELECT uuid,internal_name,external_name,net_id,type,vpci,mac,bw,model,ip_address," \
679 "floating_ip, port_security" \
680 " FROM interfaces" \
681 " WHERE vm_id='{}'" \
682 " ORDER BY created_at".format(vm['uuid'])
683 self.logger.debug(cmd)
684 self.cur.execute(cmd)
685 vm['interfaces'] = self.cur.fetchall()
686 for index in range(0,len(vm['interfaces'])):
687 vm['interfaces'][index]['port-security'] = vm['interfaces'][index].pop("port_security")
688 vm['interfaces'][index]['floating-ip'] = vm['interfaces'][index].pop("floating_ip")
689 #nets every net of a vms
690 cmd = "SELECT uuid,name,type,description FROM nets WHERE vnf_id='{}'".format(vnf['vnf_id'])
691 self.logger.debug(cmd)
692 self.cur.execute(cmd)
693 vnf['nets'] = self.cur.fetchall()
694 for vnf_net in vnf['nets']:
695 SELECT_ = "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count"
696 cmd = "SELECT {} FROM ip_profiles WHERE net_id='{}'".format(SELECT_,vnf_net['uuid'])
697 self.logger.debug(cmd)
698 self.cur.execute(cmd)
699 ipprofiles = self.cur.fetchall()
700 if self.cur.rowcount==1:
701 vnf_net["ip_profile"] = ipprofiles[0]
702 elif self.cur.rowcount>1:
703 raise db_base.db_base_Exception("More than one ip-profile found with this criteria: net_id='{}'".format(vnf_net['uuid']), db_base.HTTP_Bad_Request)
704
705 #sce_nets
706 cmd = "SELECT uuid,name,type,external,description" \
707 " FROM sce_nets WHERE scenario_id='{}'" \
708 " ORDER BY created_at ".format(scenario_dict['uuid'])
709 self.logger.debug(cmd)
710 self.cur.execute(cmd)
711 scenario_dict['nets'] = self.cur.fetchall()
712 #datacenter_nets
713 for net in scenario_dict['nets']:
714 if str(net['external']) == 'false':
715 SELECT_ = "ip_version,subnet_address,gateway_address,dns_address,dhcp_enabled,dhcp_start_address,dhcp_count"
716 cmd = "SELECT {} FROM ip_profiles WHERE sce_net_id='{}'".format(SELECT_,net['uuid'])
717 self.logger.debug(cmd)
718 self.cur.execute(cmd)
719 ipprofiles = self.cur.fetchall()
720 if self.cur.rowcount==1:
721 net["ip_profile"] = ipprofiles[0]
722 elif self.cur.rowcount>1:
723 raise db_base.db_base_Exception("More than one ip-profile found with this criteria: sce_net_id='{}'".format(net['uuid']), db_base.HTTP_Bad_Request)
724 continue
725 WHERE_=" WHERE name='{}'".format(net['name'])
726 if datacenter_id!=None:
727 WHERE_ += " AND datacenter_id='{}'".format(datacenter_id)
728 cmd = "SELECT vim_net_id FROM datacenter_nets" + WHERE_
729 self.logger.debug(cmd)
730 self.cur.execute(cmd)
731 d_net = self.cur.fetchone()
732 if d_net==None or datacenter_id==None:
733 #print "nfvo_db.get_scenario() WARNING external net %s not found" % net['name']
734 net['vim_id']=None
735 else:
736 net['vim_id']=d_net['vim_net_id']
737
738 db_base._convert_datetime2str(scenario_dict)
739 db_base._convert_str2boolean(scenario_dict, ('public','shared','external','port-security','floating-ip') )
740 return scenario_dict
741 except (mdb.Error, AttributeError) as e:
742 self._format_error(e, tries)
743 tries -= 1
744
745 def delete_scenario(self, scenario_id, tenant_id=None):
746 '''Deletes a scenario, filtering by one or several of the tenant, uuid or name
747 scenario_id is the uuid or the name if it is not a valid uuid format
748 Only one scenario must mutch the filtering or an error is returned
749 '''
750 tries = 2
751 while tries:
752 try:
753 with self.con:
754 self.cur = self.con.cursor(mdb.cursors.DictCursor)
755
756 #scenario table
757 where_text = "uuid='{}'".format(scenario_id)
758 if not tenant_id and tenant_id != "any":
759 where_text += " AND (tenant_id='{}' OR public='True')".format(tenant_id)
760 cmd = "SELECT * FROM scenarios WHERE "+ where_text
761 self.logger.debug(cmd)
762 self.cur.execute(cmd)
763 rows = self.cur.fetchall()
764 if self.cur.rowcount==0:
765 raise db_base.db_base_Exception("No scenario found where " + where_text, db_base.HTTP_Bad_Request)
766 elif self.cur.rowcount>1:
767 raise db_base.db_base_Exception("More than one scenario found where " + where_text, db_base.HTTP_Bad_Request)
768 scenario_uuid = rows[0]["uuid"]
769 scenario_name = rows[0]["name"]
770
771 #sce_vnfs
772 cmd = "DELETE FROM scenarios WHERE uuid='{}'".format(scenario_uuid)
773 self.logger.debug(cmd)
774 self.cur.execute(cmd)
775
776 return scenario_uuid + " " + scenario_name
777 except (mdb.Error, AttributeError) as e:
778 self._format_error(e, tries, "delete", "instances running")
779 tries -= 1
780
781 def new_instance_scenario_as_a_whole(self,tenant_id,instance_scenario_name,instance_scenario_description,scenarioDict):
782 tries = 2
783 while tries:
784 created_time = time.time()
785 try:
786 with self.con:
787 self.cur = self.con.cursor()
788 #instance_scenarios
789 datacenter_id = scenarioDict['datacenter_id']
790 INSERT_={'tenant_id': tenant_id,
791 'datacenter_tenant_id': scenarioDict["datacenter2tenant"][datacenter_id],
792 'name': instance_scenario_name,
793 'description': instance_scenario_description,
794 'scenario_id' : scenarioDict['uuid'],
795 'datacenter_id': datacenter_id
796 }
797 if scenarioDict.get("cloud-config"):
798 INSERT_["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"], default_flow_style=True, width=256)
799
800 instance_uuid = self._new_row_internal('instance_scenarios', INSERT_, add_uuid=True, root_uuid=None, created_time=created_time)
801
802 net_scene2instance={}
803 #instance_nets #nets interVNF
804 for net in scenarioDict['nets']:
805 net_scene2instance[ net['uuid'] ] ={}
806 datacenter_site_id = net.get('datacenter_id', datacenter_id)
807 if not "vim_id_sites" in net:
808 net["vim_id_sites"] ={datacenter_site_id: net['vim_id']}
809 net["vim_id_sites"]["datacenter_site_id"] = {datacenter_site_id: net['vim_id']}
810 sce_net_id = net.get("uuid")
811
812 for datacenter_site_id,vim_id in net["vim_id_sites"].iteritems():
813 INSERT_={'vim_net_id': vim_id, 'created': net.get('created', False), 'instance_scenario_id':instance_uuid } #, 'type': net['type']
814 INSERT_['datacenter_id'] = datacenter_site_id
815 INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_site_id]
816 if sce_net_id:
817 INSERT_['sce_net_id'] = sce_net_id
818 created_time += 0.00001
819 instance_net_uuid = self._new_row_internal('instance_nets', INSERT_, True, instance_uuid, created_time)
820 net_scene2instance[ sce_net_id ][datacenter_site_id] = instance_net_uuid
821 net['uuid'] = instance_net_uuid #overwrite scnario uuid by instance uuid
822
823 if 'ip_profile' in net:
824 net['ip_profile']['net_id'] = None
825 net['ip_profile']['sce_net_id'] = None
826 net['ip_profile']['instance_net_id'] = instance_net_uuid
827 created_time += 0.00001
828 ip_profile_id = self._new_row_internal('ip_profiles', net['ip_profile'])
829
830 #instance_vnfs
831 for vnf in scenarioDict['vnfs']:
832 datacenter_site_id = vnf.get('datacenter_id', datacenter_id)
833 INSERT_={'instance_scenario_id': instance_uuid, 'vnf_id': vnf['vnf_id'] }
834 INSERT_['datacenter_id'] = datacenter_site_id
835 INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_site_id]
836 if vnf.get("uuid"):
837 INSERT_['sce_vnf_id'] = vnf['uuid']
838 created_time += 0.00001
839 instance_vnf_uuid = self._new_row_internal('instance_vnfs', INSERT_, True, instance_uuid, created_time)
840 vnf['uuid'] = instance_vnf_uuid #overwrite scnario uuid by instance uuid
841
842 #instance_nets #nets intraVNF
843 for net in vnf['nets']:
844 net_scene2instance[ net['uuid'] ] = {}
845 INSERT_={'vim_net_id': net['vim_id'], 'created': net.get('created', False), 'instance_scenario_id':instance_uuid } #, 'type': net['type']
846 INSERT_['datacenter_id'] = net.get('datacenter_id', datacenter_site_id)
847 INSERT_['datacenter_tenant_id'] = scenarioDict["datacenter2tenant"][datacenter_id]
848 if net.get("uuid"):
849 INSERT_['net_id'] = net['uuid']
850 created_time += 0.00001
851 instance_net_uuid = self._new_row_internal('instance_nets', INSERT_, True, instance_uuid, created_time)
852 net_scene2instance[ net['uuid'] ][datacenter_site_id] = instance_net_uuid
853 net['uuid'] = instance_net_uuid #overwrite scnario uuid by instance uuid
854
855 if 'ip_profile' in net:
856 net['ip_profile']['net_id'] = None
857 net['ip_profile']['sce_net_id'] = None
858 net['ip_profile']['instance_net_id'] = instance_net_uuid
859 created_time += 0.00001
860 ip_profile_id = self._new_row_internal('ip_profiles', net['ip_profile'])
861
862 #instance_vms
863 for vm in vnf['vms']:
864 INSERT_={'instance_vnf_id': instance_vnf_uuid, 'vm_id': vm['uuid'], 'vim_vm_id': vm['vim_id'] }
865 created_time += 0.00001
866 instance_vm_uuid = self._new_row_internal('instance_vms', INSERT_, True, instance_uuid, created_time)
867 vm['uuid'] = instance_vm_uuid #overwrite scnario uuid by instance uuid
868
869 #instance_interfaces
870 for interface in vm['interfaces']:
871 net_id = interface.get('net_id', None)
872 if net_id is None:
873 #check if is connected to a inter VNFs net
874 for iface in vnf['interfaces']:
875 if iface['interface_id'] == interface['uuid']:
876 if 'ip_address' in iface:
877 interface['ip_address'] = iface['ip_address']
878 net_id = iface.get('sce_net_id', None)
879 break
880 if net_id is None:
881 continue
882 interface_type='external' if interface['external_name'] is not None else 'internal'
883 INSERT_={'instance_vm_id': instance_vm_uuid, 'instance_net_id': net_scene2instance[net_id][datacenter_site_id],
884 'interface_id': interface['uuid'], 'vim_interface_id': interface.get('vim_id'), 'type': interface_type,
885 'ip_address': interface.get('ip_address'), 'floating_ip': int(interface.get('floating-ip',False)),
886 'port_security': int(interface.get('port-security',True))}
887 #created_time += 0.00001
888 interface_uuid = self._new_row_internal('instance_interfaces', INSERT_, True, instance_uuid) #, created_time)
889 interface['uuid'] = interface_uuid #overwrite scnario uuid by instance uuid
890 return instance_uuid
891 except (mdb.Error, AttributeError) as e:
892 self._format_error(e, tries)
893 tries -= 1
894
895 def get_instance_scenario(self, instance_id, tenant_id=None, verbose=False):
896 '''Obtain the instance information, filtering by one or several of the tenant, uuid or name
897 instance_id is the uuid or the name if it is not a valid uuid format
898 Only one instance must mutch the filtering or an error is returned
899 '''
900 tries = 2
901 while tries:
902 try:
903 with self.con:
904 self.cur = self.con.cursor(mdb.cursors.DictCursor)
905 #instance table
906 where_list=[]
907 if tenant_id is not None: where_list.append( "inst.tenant_id='" + tenant_id +"'" )
908 if db_base._check_valid_uuid(instance_id):
909 where_list.append( "inst.uuid='" + instance_id +"'" )
910 else:
911 where_list.append( "inst.name='" + instance_id +"'" )
912 where_text = " AND ".join(where_list)
913 cmd = "SELECT inst.uuid as uuid,inst.name as name,inst.scenario_id as scenario_id, datacenter_id" +\
914 " ,datacenter_tenant_id, s.name as scenario_name,inst.tenant_id as tenant_id" + \
915 " ,inst.description as description,inst.created_at as created_at" +\
916 " ,inst.cloud_config as 'cloud_config'" +\
917 " FROM instance_scenarios as inst join scenarios as s on inst.scenario_id=s.uuid"+\
918 " WHERE " + where_text
919 self.logger.debug(cmd)
920 self.cur.execute(cmd)
921 rows = self.cur.fetchall()
922
923 if self.cur.rowcount==0:
924 raise db_base.db_base_Exception("No instance found where " + where_text, db_base.HTTP_Not_Found)
925 elif self.cur.rowcount>1:
926 raise db_base.db_base_Exception("More than one instance found where " + where_text, db_base.HTTP_Bad_Request)
927 instance_dict = rows[0]
928 if instance_dict["cloud_config"]:
929 instance_dict["cloud-config"] = yaml.load(instance_dict["cloud_config"])
930 del instance_dict["cloud_config"]
931
932 #instance_vnfs
933 cmd = "SELECT iv.uuid as uuid,sv.vnf_id as vnf_id,sv.name as vnf_name, sce_vnf_id, datacenter_id, datacenter_tenant_id"\
934 " FROM instance_vnfs as iv join sce_vnfs as sv on iv.sce_vnf_id=sv.uuid" \
935 " WHERE iv.instance_scenario_id='{}'" \
936 " ORDER BY iv.created_at ".format(instance_dict['uuid'])
937 self.logger.debug(cmd)
938 self.cur.execute(cmd)
939 instance_dict['vnfs'] = self.cur.fetchall()
940 for vnf in instance_dict['vnfs']:
941 vnf_manage_iface_list=[]
942 #instance vms
943 cmd = "SELECT iv.uuid as uuid, vim_vm_id, status, error_msg, vim_info, iv.created_at as created_at, name "\
944 " FROM instance_vms as iv join vms on iv.vm_id=vms.uuid "\
945 " WHERE instance_vnf_id='{}' ORDER BY iv.created_at".format(vnf['uuid'])
946 self.logger.debug(cmd)
947 self.cur.execute(cmd)
948 vnf['vms'] = self.cur.fetchall()
949 for vm in vnf['vms']:
950 vm_manage_iface_list=[]
951 #instance_interfaces
952 cmd = "SELECT vim_interface_id, instance_net_id, internal_name,external_name, mac_address, ii.ip_address as ip_address, vim_info, i.type as type "\
953 " FROM instance_interfaces as ii join interfaces as i on ii.interface_id=i.uuid "\
954 " WHERE instance_vm_id='{}' ORDER BY created_at".format(vm['uuid'])
955 self.logger.debug(cmd)
956 self.cur.execute(cmd )
957 vm['interfaces'] = self.cur.fetchall()
958 for iface in vm['interfaces']:
959 if iface["type"] == "mgmt" and iface["ip_address"]:
960 vnf_manage_iface_list.append(iface["ip_address"])
961 vm_manage_iface_list.append(iface["ip_address"])
962 if not verbose:
963 del iface["type"]
964 if vm_manage_iface_list: vm["ip_address"] = ",".join(vm_manage_iface_list)
965 if vnf_manage_iface_list: vnf["ip_address"] = ",".join(vnf_manage_iface_list)
966
967 #instance_nets
968 #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"
969 #from_text = "instance_nets join instance_scenarios on instance_nets.instance_scenario_id=instance_scenarios.uuid " + \
970 # "join sce_nets on instance_scenarios.scenario_id=sce_nets.scenario_id"
971 #where_text = "instance_nets.instance_scenario_id='"+ instance_dict['uuid'] + "'"
972 cmd = "SELECT uuid,vim_net_id,status,error_msg,vim_info,created, sce_net_id, net_id as vnf_net_id, datacenter_id, datacenter_tenant_id"\
973 " FROM instance_nets" \
974 " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict['uuid'])
975 self.logger.debug(cmd)
976 self.cur.execute(cmd)
977 instance_dict['nets'] = self.cur.fetchall()
978
979 db_base._convert_datetime2str(instance_dict)
980 db_base._convert_str2boolean(instance_dict, ('public','shared','created') )
981 return instance_dict
982 except (mdb.Error, AttributeError) as e:
983 self._format_error(e, tries)
984 tries -= 1
985
986 def delete_instance_scenario(self, instance_id, tenant_id=None):
987 '''Deletes a instance_Scenario, filtering by one or serveral of the tenant, uuid or name
988 instance_id is the uuid or the name if it is not a valid uuid format
989 Only one instance_scenario must mutch the filtering or an error is returned
990 '''
991 tries = 2
992 while tries:
993 try:
994 with self.con:
995 self.cur = self.con.cursor(mdb.cursors.DictCursor)
996
997 #instance table
998 where_list=[]
999 if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" )
1000 if db_base._check_valid_uuid(instance_id):
1001 where_list.append( "uuid='" + instance_id +"'" )
1002 else:
1003 where_list.append( "name='" + instance_id +"'" )
1004 where_text = " AND ".join(where_list)
1005 cmd = "SELECT * FROM instance_scenarios WHERE "+ where_text
1006 self.logger.debug(cmd)
1007 self.cur.execute(cmd)
1008 rows = self.cur.fetchall()
1009
1010 if self.cur.rowcount==0:
1011 raise db_base.db_base_Exception("No instance found where " + where_text, db_base.HTTP_Bad_Request)
1012 elif self.cur.rowcount>1:
1013 raise db_base.db_base_Exception("More than one instance found where " + where_text, db_base.HTTP_Bad_Request)
1014 instance_uuid = rows[0]["uuid"]
1015 instance_name = rows[0]["name"]
1016
1017 #sce_vnfs
1018 cmd = "DELETE FROM instance_scenarios WHERE uuid='{}'".format(instance_uuid)
1019 self.logger.debug(cmd)
1020 self.cur.execute(cmd)
1021
1022 return instance_uuid + " " + instance_name
1023 except (mdb.Error, AttributeError) as e:
1024 self._format_error(e, tries, "delete", "No dependences can avoid deleting!!!!")
1025 tries -= 1
1026
1027 def new_instance_scenario(self, instance_scenario_dict, tenant_id):
1028 #return self.new_row('vnfs', vnf_dict, None, tenant_id, True, True)
1029 return self._new_row_internal('instance_scenarios', instance_scenario_dict, tenant_id, add_uuid=True, root_uuid=None, log=True)
1030
1031 def update_instance_scenario(self, instance_scenario_dict):
1032 #TODO:
1033 return
1034
1035 def new_instance_vnf(self, instance_vnf_dict, tenant_id, instance_scenario_id = None):
1036 #return self.new_row('vms', vm_dict, tenant_id, True, True)
1037 return self._new_row_internal('instance_vnfs', instance_vnf_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
1038
1039 def update_instance_vnf(self, instance_vnf_dict):
1040 #TODO:
1041 return
1042
1043 def delete_instance_vnf(self, instance_vnf_id):
1044 #TODO:
1045 return
1046
1047 def new_instance_vm(self, instance_vm_dict, tenant_id, instance_scenario_id = None):
1048 #return self.new_row('vms', vm_dict, tenant_id, True, True)
1049 return self._new_row_internal('instance_vms', instance_vm_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
1050
1051 def update_instance_vm(self, instance_vm_dict):
1052 #TODO:
1053 return
1054
1055 def delete_instance_vm(self, instance_vm_id):
1056 #TODO:
1057 return
1058
1059 def new_instance_net(self, instance_net_dict, tenant_id, instance_scenario_id = None):
1060 return self._new_row_internal('instance_nets', instance_net_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
1061
1062 def update_instance_net(self, instance_net_dict):
1063 #TODO:
1064 return
1065
1066 def delete_instance_net(self, instance_net_id):
1067 #TODO:
1068 return
1069
1070 def new_instance_interface(self, instance_interface_dict, tenant_id, instance_scenario_id = None):
1071 return self._new_row_internal('instance_interfaces', instance_interface_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
1072
1073 def update_instance_interface(self, instance_interface_dict):
1074 #TODO:
1075 return
1076
1077 def delete_instance_interface(self, instance_interface_dict):
1078 #TODO:
1079 return
1080
1081 def update_datacenter_nets(self, datacenter_id, new_net_list=[]):
1082 ''' Removes the old and adds the new net list at datacenter list for one datacenter.
1083 Attribute
1084 datacenter_id: uuid of the datacenter to act upon
1085 table: table where to insert
1086 new_net_list: the new values to be inserted. If empty it only deletes the existing nets
1087 Return: (Inserted items, Deleted items) if OK, (-Error, text) if error
1088 '''
1089 tries = 2
1090 while tries:
1091 created_time = time.time()
1092 try:
1093 with self.con:
1094 self.cur = self.con.cursor()
1095 cmd="DELETE FROM datacenter_nets WHERE datacenter_id='{}'".format(datacenter_id)
1096 self.logger.debug(cmd)
1097 self.cur.execute(cmd)
1098 deleted = self.cur.rowcount
1099 inserted = 0
1100 for new_net in new_net_list:
1101 created_time += 0.00001
1102 self._new_row_internal('datacenter_nets', new_net, add_uuid=True, created_time=created_time)
1103 inserted += 1
1104 return inserted, deleted
1105 except (mdb.Error, AttributeError) as e:
1106 self._format_error(e, tries)
1107 tries -= 1
1108
1109