blob: 76469946c19e6505975abe4e9b9866996be326b7 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- 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'''
25NFVO engine, implementing all the methods for the creation, deletion and management of vnfs, scenarios and instances
26'''
27__author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
28__date__ ="$16-sep-2014 22:05:01$"
29
tierno361275f2017-04-25 16:24:34 +020030# import imp
31# import json
tierno7edb6752016-03-21 17:37:52 +010032import yaml
tierno42fcc3b2016-07-06 17:20:40 +020033import utils
tierno42026a02017-02-10 15:13:40 +010034import vim_thread
tiernof97fd272016-07-11 14:32:37 +020035from db_base import HTTP_Unauthorized, HTTP_Bad_Request, HTTP_Internal_Server_Error, HTTP_Not_Found,\
tierno7edb6752016-03-21 17:37:52 +010036 HTTP_Conflict, HTTP_Method_Not_Allowed
37import console_proxy_thread as cli
tiernoae4a8d12016-07-08 12:30:39 +020038import vimconn
39import logging
garciadeblas9f8456e2016-09-05 05:02:59 +020040import collections
tiernof97fd272016-07-11 14:32:37 +020041from db_base import db_base_Exception
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +010042
tiernob3d36742017-03-03 23:51:05 +010043import nfvo_db
44from threading import Lock
45from time import time
tierno01b3e172017-04-21 10:52:34 +020046from lib_osm_openvim import ovim as ovim_module
tierno7edb6752016-03-21 17:37:52 +010047
48global global_config
49global vimconn_imported
tierno73ad9e42016-09-12 18:11:11 +020050global logger
montesmoreno0c8def02016-12-22 12:16:23 +000051global default_volume_size
52default_volume_size = '5' #size in GB
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +010053global ovim
54ovim = None
tiernoc5651792017-03-27 10:50:43 +020055global_config = None
tiernoae4a8d12016-07-08 12:30:39 +020056
tierno42026a02017-02-10 15:13:40 +010057vimconn_imported = {} # dictionary with VIM type as key, loaded module as value
58vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
tiernob3d36742017-03-03 23:51:05 +010059vim_persistent_info = {}
tierno73ad9e42016-09-12 18:11:11 +020060logger = logging.getLogger('openmano.nfvo')
tiernob3d36742017-03-03 23:51:05 +010061task_lock = Lock()
tierno867ffe92017-03-27 12:50:34 +020062global_instance_tasks = {}
tiernob3d36742017-03-03 23:51:05 +010063last_task_id = 0.0
64db=None
65db_lock=Lock()
tierno7edb6752016-03-21 17:37:52 +010066
67class NfvoException(Exception):
tiernoae4a8d12016-07-08 12:30:39 +020068 def __init__(self, message, http_code):
69 self.http_code = http_code
70 Exception.__init__(self, message)
tierno7edb6752016-03-21 17:37:52 +010071
72
tiernob3d36742017-03-03 23:51:05 +010073def get_task_id():
74 global last_task_id
75 task_id = time()
76 if task_id <= last_task_id:
77 task_id = last_task_id + 0.000001
78 last_task_id = task_id
79 return "TASK.{:.6f}".format(task_id)
80
81
tierno867ffe92017-03-27 12:50:34 +020082def new_task(name, params, depends=None):
tiernob3d36742017-03-03 23:51:05 +010083 task_id = get_task_id()
84 task = {"status": "enqueued", "id": task_id, "name": name, "params": params}
85 if depends:
86 task["depends"] = depends
tiernob3d36742017-03-03 23:51:05 +010087 return task
88
89
90def is_task_id(id):
91 return True if id[:5] == "TASK." else False
92
93
tierno42026a02017-02-10 15:13:40 +010094def get_non_used_vim_name(datacenter_name, datacenter_id, tenant_name, tenant_id):
95 name = datacenter_name[:16]
96 if name not in vim_threads["names"]:
97 vim_threads["names"].append(name)
98 return name
tiernob3d36742017-03-03 23:51:05 +010099 name = datacenter_name[:16] + "." + tenant_name[:16]
tierno42026a02017-02-10 15:13:40 +0100100 if name not in vim_threads["names"]:
101 vim_threads["names"].append(name)
102 return name
103 name = datacenter_id + "-" + tenant_id
104 vim_threads["names"].append(name)
105 return name
106
107
108def start_service(mydb):
tiernob3d36742017-03-03 23:51:05 +0100109 global db, global_config
110 db = nfvo_db.nfvo_db()
111 db.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name'])
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100112 global ovim
113
114 # Initialize openvim for SDN control
115 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
116 # TODO: review ovim.py to delete not needed configuration
117 ovim_configuration = {
tierno639520f2017-04-05 19:55:36 +0200118 'logger_name': 'openmano.ovim',
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100119 'network_vlan_range_start': 1000,
120 'network_vlan_range_end': 4096,
tierno639520f2017-04-05 19:55:36 +0200121 'db_name': global_config["db_ovim_name"],
122 'db_host': global_config["db_ovim_host"],
123 'db_user': global_config["db_ovim_user"],
124 'db_passwd': global_config["db_ovim_passwd"],
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100125 'bridge_ifaces': {},
126 'mode': 'normal',
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100127 'network_type': 'bridge',
128 #TODO: log_level_of should not be needed. To be modified in ovim
129 'log_level_of': 'DEBUG'
130 }
tierno639520f2017-04-05 19:55:36 +0200131 ovim = ovim_module.ovim(ovim_configuration)
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +0200132 ovim.start_service()
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100133
tierno42026a02017-02-10 15:13:40 +0100134 from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
135 select_ = ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
136 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
137 'user','passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
138 try:
139 vims = mydb.get_rows(FROM=from_, SELECT=select_)
140 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200141 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
142 'datacenter_id': vim.get('datacenter_id')}
tierno42026a02017-02-10 15:13:40 +0100143 if vim["config"]:
144 extra.update(yaml.load(vim["config"]))
145 if vim.get('dt_config'):
146 extra.update(yaml.load(vim["dt_config"]))
147 if vim["type"] not in vimconn_imported:
148 module_info=None
149 try:
150 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200151 pkg = __import__("osm_ro." + module)
152 vim_conn = getattr(pkg, module)
153 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
154 # vim_conn = imp.load_module(vim["type"], *module_info)
tierno42026a02017-02-10 15:13:40 +0100155 vimconn_imported[vim["type"]] = vim_conn
156 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200157 # if module_info and module_info[0]:
158 # file.close(module_info[0])
tiernocdee8cc2017-04-25 13:42:06 +0200159 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
tiernob3d36742017-03-03 23:51:05 +0100160 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100161
tierno867ffe92017-03-27 12:50:34 +0200162 thread_id = vim['datacenter_tenant_id']
tiernob3d36742017-03-03 23:51:05 +0100163 vim_persistent_info[thread_id] = {}
tierno42026a02017-02-10 15:13:40 +0100164 try:
165 #if not tenant:
166 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
167 myvim = vimconn_imported[ vim["type"] ].vimconnector(
tiernob3d36742017-03-03 23:51:05 +0100168 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
169 tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'],
170 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
171 user=vim['user'], passwd=vim['passwd'],
172 config=extra, persistent_info=vim_persistent_info[thread_id]
173 )
tierno42026a02017-02-10 15:13:40 +0100174 except Exception as e:
175 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error)
176 thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'], vim['vim_tenant_id'])
tiernob3d36742017-03-03 23:51:05 +0100177 new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, vim['datacenter_name'],
tierno867ffe92017-03-27 12:50:34 +0200178 vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim)
tierno42026a02017-02-10 15:13:40 +0100179 new_thread.start()
tierno42026a02017-02-10 15:13:40 +0100180 vim_threads["running"][thread_id] = new_thread
181 except db_base_Exception as e:
182 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
183
tierno867ffe92017-03-27 12:50:34 +0200184
tierno42026a02017-02-10 15:13:40 +0100185def stop_service():
tiernoc5651792017-03-27 10:50:43 +0200186 global ovim, global_config
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100187 if ovim:
188 ovim.stop_service()
tierno42026a02017-02-10 15:13:40 +0100189 for thread_id,thread in vim_threads["running"].items():
tierno867ffe92017-03-27 12:50:34 +0200190 thread.insert_task(new_task("exit", None))
tierno42026a02017-02-10 15:13:40 +0100191 vim_threads["deleting"][thread_id] = thread
tiernob3d36742017-03-03 23:51:05 +0100192 vim_threads["running"] = {}
tiernoc5651792017-03-27 10:50:43 +0200193 if global_config and global_config.get("console_thread"):
194 for thread in global_config["console_thread"]:
195 thread.terminate = True
tiernob3d36742017-03-03 23:51:05 +0100196
tierno42026a02017-02-10 15:13:40 +0100197
tierno7edb6752016-03-21 17:37:52 +0100198def get_flavorlist(mydb, vnf_id, nfvo_tenant=None):
199 '''Obtain flavorList
200 return result, content:
201 <0, error_text upon error
202 nb_records, flavor_list on success
203 '''
204 WHERE_dict={}
205 WHERE_dict['vnf_id'] = vnf_id
206 if nfvo_tenant is not None:
207 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
tierno42026a02017-02-10 15:13:40 +0100208
tierno7edb6752016-03-21 17:37:52 +0100209 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
210 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
tiernof97fd272016-07-11 14:32:37 +0200211 flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict )
212 #print "get_flavor_list result:", result
213 #print "get_flavor_list content:", content
tierno7edb6752016-03-21 17:37:52 +0100214 flavorList=[]
tiernof97fd272016-07-11 14:32:37 +0200215 for flavor in flavors:
tierno7edb6752016-03-21 17:37:52 +0100216 flavorList.append(flavor['flavor_id'])
tiernof97fd272016-07-11 14:32:37 +0200217 return flavorList
tierno7edb6752016-03-21 17:37:52 +0100218
tiernob3d36742017-03-03 23:51:05 +0100219
tierno7edb6752016-03-21 17:37:52 +0100220def get_imagelist(mydb, vnf_id, nfvo_tenant=None):
221 '''Obtain imageList
222 return result, content:
223 <0, error_text upon error
224 nb_records, flavor_list on success
225 '''
226 WHERE_dict={}
227 WHERE_dict['vnf_id'] = vnf_id
228 if nfvo_tenant is not None:
229 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
tierno42026a02017-02-10 15:13:40 +0100230
tierno7edb6752016-03-21 17:37:52 +0100231 #result, content = mydb.get_table(FROM='vms join vnfs on vms-vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
tiernof97fd272016-07-11 14:32:37 +0200232 images = mydb.get_rows(FROM='vms join images on vms.image_id=images.uuid',SELECT=('image_id',),WHERE=WHERE_dict )
tierno7edb6752016-03-21 17:37:52 +0100233 imageList=[]
tiernof97fd272016-07-11 14:32:37 +0200234 for image in images:
tierno7edb6752016-03-21 17:37:52 +0100235 imageList.append(image['image_id'])
tiernof97fd272016-07-11 14:32:37 +0200236 return imageList
tierno7edb6752016-03-21 17:37:52 +0100237
tiernob3d36742017-03-03 23:51:05 +0100238
tiernoa2793912016-10-04 08:15:08 +0000239def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None,
240 vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None):
tierno7edb6752016-03-21 17:37:52 +0100241 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
tierno42026a02017-02-10 15:13:40 +0100242 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
tierno7edb6752016-03-21 17:37:52 +0100243 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
tiernobe41e222016-09-02 15:16:13 +0200244 raise exception upon error
tierno7edb6752016-03-21 17:37:52 +0100245 '''
246 WHERE_dict={}
247 if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
248 if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id
tiernoa2793912016-10-04 08:15:08 +0000249 if datacenter_tenant_id is not None: WHERE_dict['datacenter_tenant_id'] = datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +0100250 if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name
251 if vim_tenant is not None: WHERE_dict['dt.vim_tenant_id'] = vim_tenant
tiernoa2793912016-10-04 08:15:08 +0000252 if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name
253 if nfvo_tenant or vim_tenant or vim_tenant_name or datacenter_tenant_id:
tierno7edb6752016-03-21 17:37:52 +0100254 from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
tierno8008c3a2016-10-13 15:34:28 +0000255 select_ = ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
tierno7edb6752016-03-21 17:37:52 +0100256 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
tierno8008c3a2016-10-13 15:34:28 +0000257 'user','passwd', 'dt.config as dt_config')
tierno7edb6752016-03-21 17:37:52 +0100258 else:
259 from_ = 'datacenters as d'
260 select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
tiernof97fd272016-07-11 14:32:37 +0200261 try:
262 vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
263 vim_dict={}
264 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200265 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
266 'datacenter_id': vim.get('datacenter_id')}
tierno8008c3a2016-10-13 15:34:28 +0000267 if vim["config"]:
tiernof97fd272016-07-11 14:32:37 +0200268 extra.update(yaml.load(vim["config"]))
tierno8008c3a2016-10-13 15:34:28 +0000269 if vim.get('dt_config'):
270 extra.update(yaml.load(vim["dt_config"]))
tiernof97fd272016-07-11 14:32:37 +0200271 if vim["type"] not in vimconn_imported:
272 module_info=None
273 try:
274 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200275 pkg = __import__("osm_ro." + module)
276 vim_conn = getattr(pkg, module)
277 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
278 # vim_conn = imp.load_module(vim["type"], *module_info)
tiernof97fd272016-07-11 14:32:37 +0200279 vimconn_imported[vim["type"]] = vim_conn
280 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200281 # if module_info and module_info[0]:
282 # file.close(module_info[0])
tiernof97fd272016-07-11 14:32:37 +0200283 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
284 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100285
tierno7edb6752016-03-21 17:37:52 +0100286 try:
tierno867ffe92017-03-27 12:50:34 +0200287 if 'datacenter_tenant_id' in vim:
288 thread_id = vim["datacenter_tenant_id"]
tiernob3d36742017-03-03 23:51:05 +0100289 if thread_id not in vim_persistent_info:
290 vim_persistent_info[thread_id] = {}
291 persistent_info = vim_persistent_info[thread_id]
292 else:
293 persistent_info = {}
tiernof97fd272016-07-11 14:32:37 +0200294 #if not tenant:
295 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
296 vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector(
297 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
tiernob3d36742017-03-03 23:51:05 +0100298 tenant_id=vim.get('vim_tenant_id',vim_tenant),
299 tenant_name=vim.get('vim_tenant_name',vim_tenant_name),
tierno42026a02017-02-10 15:13:40 +0100300 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
tierno3ae39742016-09-07 12:17:51 +0200301 user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd),
tiernob3d36742017-03-03 23:51:05 +0100302 config=extra, persistent_info=persistent_info
tiernof97fd272016-07-11 14:32:37 +0200303 )
304 except Exception as e:
305 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error)
306 return vim_dict
307 except db_base_Exception as e:
308 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
tierno42026a02017-02-10 15:13:40 +0100309
tiernob3d36742017-03-03 23:51:05 +0100310
tierno7edb6752016-03-21 17:37:52 +0100311def rollback(mydb, vims, rollback_list):
312 undeleted_items=[]
tierno42026a02017-02-10 15:13:40 +0100313 #delete things by reverse order
tierno7edb6752016-03-21 17:37:52 +0100314 for i in range(len(rollback_list)-1, -1, -1):
315 item = rollback_list[i]
316 if item["where"]=="vim":
317 if item["vim_id"] not in vims:
318 continue
319 vim=vims[ item["vim_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +0200320 try:
321 if item["what"]=="image":
322 vim.delete_image(item["uuid"])
tiernof97fd272016-07-11 14:32:37 +0200323 mydb.delete_row(FROM="datacenters_images", WHERE={"datacenter_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200324 elif item["what"]=="flavor":
325 vim.delete_flavor(item["uuid"])
garciadeblas9f8456e2016-09-05 05:02:59 +0200326 mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200327 elif item["what"]=="network":
328 vim.delete_network(item["uuid"])
329 elif item["what"]=="vm":
330 vim.delete_vminstance(item["uuid"])
331 except vimconn.vimconnException as e:
332 logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e))
333 undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"]))
tiernof97fd272016-07-11 14:32:37 +0200334 except db_base_Exception as e:
335 logger.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item['what'], item["uuid"], str(e))
tierno42026a02017-02-10 15:13:40 +0100336
tierno7edb6752016-03-21 17:37:52 +0100337 else: # where==mano
tiernof97fd272016-07-11 14:32:37 +0200338 try:
339 if item["what"]=="image":
340 mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]})
341 elif item["what"]=="flavor":
342 mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]})
343 except db_base_Exception as e:
344 logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e))
345 undeleted_items.append("{} '{}'".format(item['what'], item["uuid"]))
tierno42026a02017-02-10 15:13:40 +0100346 if len(undeleted_items)==0:
tierno7edb6752016-03-21 17:37:52 +0100347 return True," Rollback successful."
348 else:
349 return False," Rollback fails to delete: " + str(undeleted_items)
tierno42026a02017-02-10 15:13:40 +0100350
tiernob3d36742017-03-03 23:51:05 +0100351
tiernoafed5f12017-01-26 17:57:43 +0100352def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1):
tierno7edb6752016-03-21 17:37:52 +0100353 global global_config
tierno42026a02017-02-10 15:13:40 +0100354 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
tierno7edb6752016-03-21 17:37:52 +0100355 vnfc_interfaces={}
356 for vnfc in vnf_descriptor["vnf"]["VNFC"]:
tiernoafed5f12017-01-26 17:57:43 +0100357 name_dict = {}
tierno7edb6752016-03-21 17:37:52 +0100358 #dataplane interfaces
359 for numa in vnfc.get("numas",() ):
360 for interface in numa.get("interfaces",()):
tiernoafed5f12017-01-26 17:57:43 +0100361 if interface["name"] in name_dict:
362 raise NfvoException(
363 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
364 vnfc["name"], interface["name"]),
365 HTTP_Bad_Request)
366 name_dict[ interface["name"] ] = "underlay"
tierno7edb6752016-03-21 17:37:52 +0100367 #bridge interfaces
368 for interface in vnfc.get("bridge-ifaces",() ):
tiernoafed5f12017-01-26 17:57:43 +0100369 if interface["name"] in name_dict:
370 raise NfvoException(
371 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
372 vnfc["name"], interface["name"]),
373 HTTP_Bad_Request)
374 name_dict[ interface["name"] ] = "overlay"
375 vnfc_interfaces[ vnfc["name"] ] = name_dict
tierno36c0b172017-01-12 18:32:28 +0100376 # check bood-data info
377 if "boot-data" in vnfc:
378 # check that user-data is incompatible with users and config-files
379 if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
380 raise NfvoException(
381 "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
382 HTTP_Bad_Request)
383
tierno7edb6752016-03-21 17:37:52 +0100384 #check if the info in external_connections matches with the one in the vnfcs
385 name_list=[]
386 for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
387 if external_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100388 raise NfvoException(
389 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
390 external_connection["name"]),
391 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100392 name_list.append(external_connection["name"])
393 if external_connection["VNFC"] not in vnfc_interfaces:
tiernoafed5f12017-01-26 17:57:43 +0100394 raise NfvoException(
395 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
396 external_connection["name"], external_connection["VNFC"]),
397 HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100398
tierno7edb6752016-03-21 17:37:52 +0100399 if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
tiernoafed5f12017-01-26 17:57:43 +0100400 raise NfvoException(
401 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
402 external_connection["name"],
403 external_connection["local_iface_name"]),
404 HTTP_Bad_Request )
tierno42026a02017-02-10 15:13:40 +0100405
tierno7edb6752016-03-21 17:37:52 +0100406 #check if the info in internal_connections matches with the one in the vnfcs
407 name_list=[]
408 for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
409 if internal_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100410 raise NfvoException(
411 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
412 internal_connection["name"]),
413 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100414 name_list.append(internal_connection["name"])
415 #We should check that internal-connections of type "ptp" have only 2 elements
tiernoafed5f12017-01-26 17:57:43 +0100416
417 if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"):
418 raise NfvoException(
419 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
420 internal_connection["name"],
421 'ptp' if vnf_descriptor_version==1 else 'e-line',
422 'data' if vnf_descriptor_version==1 else "e-lan"),
423 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100424 for port in internal_connection["elements"]:
tiernoafed5f12017-01-26 17:57:43 +0100425 vnf = port["VNFC"]
426 iface = port["local_iface_name"]
427 if vnf not in vnfc_interfaces:
428 raise NfvoException(
429 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
430 internal_connection["name"], vnf),
431 HTTP_Bad_Request)
432 if iface not in vnfc_interfaces[ vnf ]:
433 raise NfvoException(
434 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
435 internal_connection["name"], iface),
436 HTTP_Bad_Request)
437 return -HTTP_Bad_Request,
438 if vnf_descriptor_version==1 and "type" not in internal_connection:
439 if vnfc_interfaces[vnf][iface] == "overlay":
440 internal_connection["type"] = "bridge"
441 else:
442 internal_connection["type"] = "data"
443 if vnf_descriptor_version==2 and "implementation" not in internal_connection:
444 if vnfc_interfaces[vnf][iface] == "overlay":
445 internal_connection["implementation"] = "overlay"
446 else:
447 internal_connection["implementation"] = "underlay"
448 if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \
449 internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay":
450 raise NfvoException(
451 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
452 internal_connection["name"],
453 iface, 'bridge' if vnf_descriptor_version==1 else 'overlay',
454 'data' if vnf_descriptor_version==1 else 'underlay'),
455 HTTP_Bad_Request)
456 if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \
457 vnfc_interfaces[vnf][iface] == "underlay":
458 raise NfvoException(
459 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
460 internal_connection["name"], iface,
461 'data' if vnf_descriptor_version==1 else 'underlay',
462 'bridge' if vnf_descriptor_version==1 else 'overlay'),
463 HTTP_Bad_Request)
464
tierno7edb6752016-03-21 17:37:52 +0100465
tierno5e91eb82016-10-04 09:39:07 +0000466def create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error = None):
tierno7edb6752016-03-21 17:37:52 +0100467 #look if image exist
468 if only_create_at_vim:
469 image_mano_id = image_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000470 if return_on_error == None:
471 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100472 else:
garciadeblas14480452017-01-10 13:08:07 +0100473 if image_dict['location']:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200474 images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
475 else:
476 images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']})
tiernof97fd272016-07-11 14:32:37 +0200477 if len(images)>=1:
478 image_mano_id = images[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100479 else:
garciadeblas14480452017-01-10 13:08:07 +0100480 #create image in MANO DB
tierno7edb6752016-03-21 17:37:52 +0100481 temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None),
garciadeblasb69fa9f2016-09-28 12:04:10 +0200482 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None),
483 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum']
tierno7edb6752016-03-21 17:37:52 +0100484 }
garciadeblas14480452017-01-10 13:08:07 +0100485 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
tiernof97fd272016-07-11 14:32:37 +0200486 image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True)
487 rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100488 #create image at every vim
489 for vim_id,vim in vims.iteritems():
490 image_created="false"
491 #look at database
tiernof97fd272016-07-11 14:32:37 +0200492 image_db = mydb.get_rows(FROM="datacenters_images", WHERE={'datacenter_id':vim_id, 'image_id':image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100493 #look at VIM if this image exist
tiernoae4a8d12016-07-08 12:30:39 +0200494 try:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200495 if image_dict['location'] is not None:
496 image_vim_id = vim.get_image_id_from_path(image_dict['location'])
497 else:
garciadeblas30833382017-01-09 09:46:31 +0100498 filter_dict = {}
499 filter_dict['name'] = image_dict['universal_name']
500 if image_dict.get('checksum') != None:
501 filter_dict['checksum'] = image_dict['checksum']
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000502 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200503 vim_images = vim.get_image_list(filter_dict)
garciadeblas14480452017-01-10 13:08:07 +0100504 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200505 if len(vim_images) > 1:
garciadeblas3fa2c052017-01-05 12:00:08 +0100506 raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), HTTP_Conflict)
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000507 elif len(vim_images) == 0:
garciadeblas3fa2c052017-01-05 12:00:08 +0100508 raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict)))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200509 else:
garciadeblas14480452017-01-10 13:08:07 +0100510 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
511 image_vim_id = vim_images[0]['id']
garciadeblasb69fa9f2016-09-28 12:04:10 +0200512
tiernoae4a8d12016-07-08 12:30:39 +0200513 except vimconn.vimconnNotFoundException as e:
garciadeblas14480452017-01-10 13:08:07 +0100514 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
tierno42026a02017-02-10 15:13:40 +0100515 try:
garciadeblas14480452017-01-10 13:08:07 +0100516 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
517 if image_dict['location']:
518 image_vim_id = vim.new_image(image_dict)
519 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
520 image_created="true"
521 else:
garciadeblasb6153a22017-02-06 15:38:33 +0100522 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
523 raise vimconn.vimconnException(str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200524 except vimconn.vimconnException as e:
525 if return_on_error:
garciadeblas14480452017-01-10 13:08:07 +0100526 logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200527 raise
tierno5e91eb82016-10-04 09:39:07 +0000528 image_vim_id = None
garciadeblas14480452017-01-10 13:08:07 +0100529 logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200530 continue
531 except vimconn.vimconnException as e:
tierno5e91eb82016-10-04 09:39:07 +0000532 if return_on_error:
533 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
534 raise
garciadeblasb69fa9f2016-09-28 12:04:10 +0200535 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
tierno5e91eb82016-10-04 09:39:07 +0000536 image_vim_id = None
garciadeblas30833382017-01-09 09:46:31 +0100537 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200538 #if we reach here, the image has been created or existed
tiernof97fd272016-07-11 14:32:37 +0200539 if len(image_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100540 #add new vim_id at datacenters_images
541 mydb.new_row('datacenters_images', {'datacenter_id':vim_id, 'image_id':image_mano_id, 'vim_id': image_vim_id, 'created':image_created})
542 elif image_db[0]["vim_id"]!=image_vim_id:
543 #modify existing vim_id at datacenters_images
544 mydb.update_rows('datacenters_images', UPDATE={'vim_id':image_vim_id}, WHERE={'datacenter_id':vim_id, 'image_id':image_mano_id})
tierno42026a02017-02-10 15:13:40 +0100545
tiernof97fd272016-07-11 14:32:37 +0200546 return image_vim_id if only_create_at_vim else image_mano_id
tierno7edb6752016-03-21 17:37:52 +0100547
tiernob3d36742017-03-03 23:51:05 +0100548
tierno5e91eb82016-10-04 09:39:07 +0000549def create_or_use_flavor(mydb, vims, flavor_dict, rollback_list, only_create_at_vim=False, return_on_error = None):
tierno7edb6752016-03-21 17:37:52 +0100550 temp_flavor_dict= {'disk':flavor_dict.get('disk',1),
551 'ram':flavor_dict.get('ram'),
552 'vcpus':flavor_dict.get('vcpus'),
553 }
554 if 'extended' in flavor_dict and flavor_dict['extended']==None:
555 del flavor_dict['extended']
556 if 'extended' in flavor_dict:
557 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
558
559 #look if flavor exist
560 if only_create_at_vim:
561 flavor_mano_id = flavor_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000562 if return_on_error == None:
563 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100564 else:
tiernof97fd272016-07-11 14:32:37 +0200565 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
566 if len(flavors)>=1:
567 flavor_mano_id = flavors[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100568 else:
569 #create flavor
570 #create one by one the images of aditional disks
571 dev_image_list=[] #list of images
572 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
573 dev_nb=0
574 for device in flavor_dict['extended'].get('devices',[]):
garciadeblas41f18be2016-10-04 09:09:58 +0200575 if "image" not in device and "image name" not in device:
tierno7edb6752016-03-21 17:37:52 +0100576 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200577 image_dict={}
578 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
579 image_dict['universal_name']=device.get('image name')
580 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
581 image_dict['location']=device.get('image')
garciadeblas14480452017-01-10 13:08:07 +0100582 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200583 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100584 image_metadata_dict = device.get('image metadata', None)
585 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100586 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100587 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
588 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200589 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
590 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
tierno7edb6752016-03-21 17:37:52 +0100591 dev_image_list.append(image_id)
tierno42026a02017-02-10 15:13:40 +0100592 dev_nb += 1
tierno7edb6752016-03-21 17:37:52 +0100593 temp_flavor_dict['name'] = flavor_dict['name']
594 temp_flavor_dict['description'] = flavor_dict.get('description',None)
tiernof97fd272016-07-11 14:32:37 +0200595 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
596 flavor_mano_id= content
597 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100598 #create flavor at every vim
599 if 'uuid' in flavor_dict:
600 del flavor_dict['uuid']
601 flavor_vim_id=None
602 for vim_id,vim in vims.items():
603 flavor_created="false"
604 #look at database
tiernof97fd272016-07-11 14:32:37 +0200605 flavor_db = mydb.get_rows(FROM="datacenters_flavors", WHERE={'datacenter_id':vim_id, 'flavor_id':flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100606 #look at VIM if this flavor exist SKIPPED
607 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
608 #if res_vim < 0:
609 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
610 # continue
611 #elif res_vim==0:
tierno42026a02017-02-10 15:13:40 +0100612
tierno7edb6752016-03-21 17:37:52 +0100613 #Create the flavor in VIM
614 #Translate images at devices from MANO id to VIM id
montesmoreno0c8def02016-12-22 12:16:23 +0000615 disk_list = []
tierno7edb6752016-03-21 17:37:52 +0100616 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
617 #make a copy of original devices
618 devices_original=[]
montesmoreno0c8def02016-12-22 12:16:23 +0000619
tierno7edb6752016-03-21 17:37:52 +0100620 for device in flavor_dict["extended"].get("devices",[]):
621 dev={}
622 dev.update(device)
623 devices_original.append(dev)
624 if 'image' in device:
625 del device['image']
626 if 'image metadata' in device:
627 del device['image metadata']
628 dev_nb=0
629 for index in range(0,len(devices_original)) :
630 device=devices_original[index]
montesmoreno0c8def02016-12-22 12:16:23 +0000631 if "image" not in device and "image name" not in device:
632 if 'size' in device:
633 disk_list.append({'size': device.get('size', default_volume_size)})
tierno7edb6752016-03-21 17:37:52 +0100634 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200635 image_dict={}
636 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
637 image_dict['universal_name']=device.get('image name')
638 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
639 image_dict['location']=device.get('image')
garciadeblas14480452017-01-10 13:08:07 +0100640 #image_dict['new_location']=device.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200641 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100642 image_metadata_dict = device.get('image metadata', None)
643 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100644 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100645 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
646 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200647 image_mano_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=return_on_error )
tierno7edb6752016-03-21 17:37:52 +0100648 image_dict["uuid"]=image_mano_id
tiernof97fd272016-07-11 14:32:37 +0200649 image_vim_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=True, return_on_error=return_on_error)
montesmoreno0c8def02016-12-22 12:16:23 +0000650
651 #save disk information (image must be based on and size
652 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
653
tierno7edb6752016-03-21 17:37:52 +0100654 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
655 dev_nb += 1
tiernof97fd272016-07-11 14:32:37 +0200656 if len(flavor_db)>0:
tierno7edb6752016-03-21 17:37:52 +0100657 #check that this vim_id exist in VIM, if not create
658 flavor_vim_id=flavor_db[0]["vim_id"]
tiernoae4a8d12016-07-08 12:30:39 +0200659 try:
660 vim.get_flavor(flavor_vim_id)
661 continue #flavor exist
662 except vimconn.vimconnException:
663 pass
tierno7edb6752016-03-21 17:37:52 +0100664 #create flavor at vim
tiernoae4a8d12016-07-08 12:30:39 +0200665 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
666 try:
tiernocf157a82017-01-30 14:07:06 +0100667 flavor_vim_id = None
668 flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict)
669 flavor_create="false"
670 except vimconn.vimconnException as e:
671 pass
672 try:
673 if not flavor_vim_id:
674 flavor_vim_id = vim.new_flavor(flavor_dict)
675 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
676 flavor_created="true"
tiernoae4a8d12016-07-08 12:30:39 +0200677 except vimconn.vimconnException as e:
678 if return_on_error:
679 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200680 raise
tiernoae4a8d12016-07-08 12:30:39 +0200681 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tierno5e91eb82016-10-04 09:39:07 +0000682 flavor_vim_id = None
tiernoae4a8d12016-07-08 12:30:39 +0200683 continue
tierno7edb6752016-03-21 17:37:52 +0100684 #if reach here the flavor has been create or exist
tiernof97fd272016-07-11 14:32:37 +0200685 if len(flavor_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100686 #add new vim_id at datacenters_flavors
montesmoreno0c8def02016-12-22 12:16:23 +0000687 extended_devices_yaml = None
688 if len(disk_list) > 0:
689 extended_devices = dict()
690 extended_devices['disks'] = disk_list
691 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
692 mydb.new_row('datacenters_flavors',
693 {'datacenter_id':vim_id, 'flavor_id':flavor_mano_id, 'vim_id': flavor_vim_id,
694 'created':flavor_created,'extended': extended_devices_yaml})
tierno7edb6752016-03-21 17:37:52 +0100695 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
696 #modify existing vim_id at datacenters_flavors
697 mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id}, WHERE={'datacenter_id':vim_id, 'flavor_id':flavor_mano_id})
tierno42026a02017-02-10 15:13:40 +0100698
tiernof97fd272016-07-11 14:32:37 +0200699 return flavor_vim_id if only_create_at_vim else flavor_mano_id
tierno7edb6752016-03-21 17:37:52 +0100700
tiernob3d36742017-03-03 23:51:05 +0100701
tierno7edb6752016-03-21 17:37:52 +0100702def new_vnf(mydb, tenant_id, vnf_descriptor):
703 global global_config
tierno42026a02017-02-10 15:13:40 +0100704
tierno7edb6752016-03-21 17:37:52 +0100705 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +0100706 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
tierno7edb6752016-03-21 17:37:52 +0100707 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +0100708 vims = {}
tierno7edb6752016-03-21 17:37:52 +0100709 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +0100710 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100711 if "tenant_id" in vnf_descriptor["vnf"]:
712 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +0200713 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
714 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +0100715 else:
716 vnf_descriptor['vnf']['tenant_id'] = tenant_id
717 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +0100718 if global_config["auto_push_VNF_to_VIMs"]:
719 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100720
721 # Step 4. Review the descriptor and add missing fields
722 #print vnf_descriptor
tiernof97fd272016-07-11 14:32:37 +0200723 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
tierno7edb6752016-03-21 17:37:52 +0100724 vnf_name = vnf_descriptor['vnf']['name']
725 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
726 if "physical" in vnf_descriptor['vnf']:
727 del vnf_descriptor['vnf']['physical']
728 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +0100729
tierno42026a02017-02-10 15:13:40 +0100730 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
tiernof97fd272016-07-11 14:32:37 +0200731 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
732 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +0100733
tierno7edb6752016-03-21 17:37:52 +0100734 #For each VNFC, we add it to the VNFCDict and we create a flavor.
735 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
736 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
tierno7edb6752016-03-21 17:37:52 +0100737 try:
tiernof97fd272016-07-11 14:32:37 +0200738 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +0100739 for vnfc in vnf_descriptor['vnf']['VNFC']:
740 VNFCitem={}
741 VNFCitem["name"] = vnfc['name']
742 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +0100743
tiernof97fd272016-07-11 14:32:37 +0200744 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +0100745
tierno7edb6752016-03-21 17:37:52 +0100746 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +0200747 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
tierno7edb6752016-03-21 17:37:52 +0100748 myflavorDict["description"] = VNFCitem["description"]
749 myflavorDict["ram"] = vnfc.get("ram", 0)
750 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
751 myflavorDict["disk"] = vnfc.get("disk", 1)
752 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +0100753
tierno7edb6752016-03-21 17:37:52 +0100754 devices = vnfc.get("devices")
755 if devices != None:
756 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +0100757
tierno7edb6752016-03-21 17:37:52 +0100758 # TODO:
759 # Mapping from processor models to rankings should be available somehow in the NFVO. They could be taken from VIM or directly from a new database table
tierno42026a02017-02-10 15:13:40 +0100760 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
761
tierno7edb6752016-03-21 17:37:52 +0100762 # Previous code has been commented
763 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
764 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
765 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
766 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
767 #else:
768 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
769 # if result2:
770 # print "Error creating flavor: unknown processor model. Rollback successful."
771 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
772 # else:
773 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
774 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +0100775
tierno7edb6752016-03-21 17:37:52 +0100776 if 'numas' in vnfc and len(vnfc['numas'])>0:
777 myflavorDict['extended']['numas'] = vnfc['numas']
778
779 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +0100780
tierno7edb6752016-03-21 17:37:52 +0100781 # Step 6.2 New flavors are created in the VIM
tiernof97fd272016-07-11 14:32:37 +0200782 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
tierno7edb6752016-03-21 17:37:52 +0100783
tiernof97fd272016-07-11 14:32:37 +0200784 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
tierno7edb6752016-03-21 17:37:52 +0100785 VNFCitem["flavor_id"] = flavor_id
786 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +0100787
tiernof97fd272016-07-11 14:32:37 +0200788 logger.debug("Creating new images in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +0100789 # Step 6.3 New images are created in the VIM
790 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +0100791 #This "for" loop might be integrated with the previous one
tierno7edb6752016-03-21 17:37:52 +0100792 #In case this integration is made, the VNFCDict might become a VNFClist.
793 for vnfc in vnf_descriptor['vnf']['VNFC']:
tiernof97fd272016-07-11 14:32:37 +0200794 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +0200795 image_dict={}
796 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
797 image_dict['universal_name']=vnfc.get('image name')
798 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
799 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +0100800 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200801 image_dict['checksum']=vnfc.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100802 image_metadata_dict = vnfc.get('image metadata', None)
803 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100804 if image_metadata_dict is not None:
tierno7edb6752016-03-21 17:37:52 +0100805 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
806 image_dict['metadata']=image_metadata_str
807 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
tiernof97fd272016-07-11 14:32:37 +0200808 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
809 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
tierno7edb6752016-03-21 17:37:52 +0100810 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +0200811 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno36c0b172017-01-12 18:32:28 +0100812 if vnfc.get("boot-data"):
813 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
tierno7edb6752016-03-21 17:37:52 +0100814
tierno42026a02017-02-10 15:13:40 +0100815
tiernof97fd272016-07-11 14:32:37 +0200816 # Step 7. Storing the VNF descriptor in the repository
817 if "descriptor" not in vnf_descriptor["vnf"]:
818 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +0100819
tiernof97fd272016-07-11 14:32:37 +0200820 # Step 8. Adding the VNF to the NFVO DB
821 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
822 return vnf_id
823 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
tierno7edb6752016-03-21 17:37:52 +0100824 _, message = rollback(mydb, vims, rollback_list)
tiernof97fd272016-07-11 14:32:37 +0200825 if isinstance(e, db_base_Exception):
826 error_text = "Exception at database"
827 elif isinstance(e, KeyError):
828 error_text = "KeyError exception "
829 e.http_code = HTTP_Internal_Server_Error
830 else:
831 error_text = "Exception at VIM"
832 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
833 #logger.error("start_scenario %s", error_text)
834 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +0100835
tiernob3d36742017-03-03 23:51:05 +0100836
garciadeblas9f8456e2016-09-05 05:02:59 +0200837def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
838 global global_config
tierno42026a02017-02-10 15:13:40 +0100839
garciadeblas9f8456e2016-09-05 05:02:59 +0200840 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +0100841 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
garciadeblas9f8456e2016-09-05 05:02:59 +0200842 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +0100843 vims = {}
garciadeblas9f8456e2016-09-05 05:02:59 +0200844 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +0100845 check_tenant(mydb, tenant_id)
garciadeblas9f8456e2016-09-05 05:02:59 +0200846 if "tenant_id" in vnf_descriptor["vnf"]:
847 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
848 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
849 HTTP_Unauthorized)
850 else:
851 vnf_descriptor['vnf']['tenant_id'] = tenant_id
852 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +0100853 if global_config["auto_push_VNF_to_VIMs"]:
854 vims = get_vim(mydb, tenant_id)
garciadeblas9f8456e2016-09-05 05:02:59 +0200855
856 # Step 4. Review the descriptor and add missing fields
857 #print vnf_descriptor
858 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
859 vnf_name = vnf_descriptor['vnf']['name']
860 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
861 if "physical" in vnf_descriptor['vnf']:
862 del vnf_descriptor['vnf']['physical']
863 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +0100864
tierno42026a02017-02-10 15:13:40 +0100865 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
garciadeblas9f8456e2016-09-05 05:02:59 +0200866 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
867 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +0100868
garciadeblas9f8456e2016-09-05 05:02:59 +0200869 #For each VNFC, we add it to the VNFCDict and we create a flavor.
870 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
871 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
872 try:
873 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
874 for vnfc in vnf_descriptor['vnf']['VNFC']:
875 VNFCitem={}
876 VNFCitem["name"] = vnfc['name']
877 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +0100878
garciadeblas9f8456e2016-09-05 05:02:59 +0200879 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +0100880
garciadeblas9f8456e2016-09-05 05:02:59 +0200881 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +0200882 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
garciadeblas9f8456e2016-09-05 05:02:59 +0200883 myflavorDict["description"] = VNFCitem["description"]
884 myflavorDict["ram"] = vnfc.get("ram", 0)
885 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
886 myflavorDict["disk"] = vnfc.get("disk", 1)
887 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +0100888
garciadeblas9f8456e2016-09-05 05:02:59 +0200889 devices = vnfc.get("devices")
890 if devices != None:
891 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +0100892
garciadeblas9f8456e2016-09-05 05:02:59 +0200893 # TODO:
894 # Mapping from processor models to rankings should be available somehow in the NFVO. They could be taken from VIM or directly from a new database table
tierno42026a02017-02-10 15:13:40 +0100895 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
896
garciadeblas9f8456e2016-09-05 05:02:59 +0200897 # Previous code has been commented
898 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
899 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
900 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
901 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
902 #else:
903 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
904 # if result2:
905 # print "Error creating flavor: unknown processor model. Rollback successful."
906 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
907 # else:
908 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
909 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +0100910
garciadeblas9f8456e2016-09-05 05:02:59 +0200911 if 'numas' in vnfc and len(vnfc['numas'])>0:
912 myflavorDict['extended']['numas'] = vnfc['numas']
913
914 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +0100915
garciadeblas9f8456e2016-09-05 05:02:59 +0200916 # Step 6.2 New flavors are created in the VIM
917 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
918
919 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
920 VNFCitem["flavor_id"] = flavor_id
921 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +0100922
garciadeblas9f8456e2016-09-05 05:02:59 +0200923 logger.debug("Creating new images in the VIM for each VNFC")
924 # Step 6.3 New images are created in the VIM
925 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +0100926 #This "for" loop might be integrated with the previous one
garciadeblas9f8456e2016-09-05 05:02:59 +0200927 #In case this integration is made, the VNFCDict might become a VNFClist.
928 for vnfc in vnf_descriptor['vnf']['VNFC']:
929 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +0200930 image_dict={}
931 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
932 image_dict['universal_name']=vnfc.get('image name')
933 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
934 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +0100935 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200936 image_dict['checksum']=vnfc.get('image checksum')
garciadeblas9f8456e2016-09-05 05:02:59 +0200937 image_metadata_dict = vnfc.get('image metadata', None)
938 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100939 if image_metadata_dict is not None:
garciadeblas9f8456e2016-09-05 05:02:59 +0200940 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
941 image_dict['metadata']=image_metadata_str
942 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
943 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
944 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
945 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +0200946 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno36c0b172017-01-12 18:32:28 +0100947 if vnfc.get("boot-data"):
948 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
garciadeblas9f8456e2016-09-05 05:02:59 +0200949
garciadeblas9f8456e2016-09-05 05:02:59 +0200950 # Step 7. Storing the VNF descriptor in the repository
951 if "descriptor" not in vnf_descriptor["vnf"]:
952 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +0100953
garciadeblas9f8456e2016-09-05 05:02:59 +0200954 # Step 8. Adding the VNF to the NFVO DB
955 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
956 return vnf_id
957 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
958 _, message = rollback(mydb, vims, rollback_list)
959 if isinstance(e, db_base_Exception):
960 error_text = "Exception at database"
961 elif isinstance(e, KeyError):
962 error_text = "KeyError exception "
963 e.http_code = HTTP_Internal_Server_Error
964 else:
965 error_text = "Exception at VIM"
966 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
967 #logger.error("start_scenario %s", error_text)
968 raise NfvoException(error_text, e.http_code)
969
tiernob3d36742017-03-03 23:51:05 +0100970
tierno7edb6752016-03-21 17:37:52 +0100971def get_vnf_id(mydb, tenant_id, vnf_id):
972 #check valid tenant_id
tierno42026a02017-02-10 15:13:40 +0100973 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100974 #obtain data
975 where_or = {}
976 if tenant_id != "any":
977 where_or["tenant_id"] = tenant_id
978 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +0100979 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
980
tiernof97fd272016-07-11 14:32:37 +0200981 vnf_id=vnf["uuid"]
tierno7edb6752016-03-21 17:37:52 +0100982 filter_keys = ('uuid','name','description','public', "tenant_id", "created_at")
tiernof97fd272016-07-11 14:32:37 +0200983 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
tierno7edb6752016-03-21 17:37:52 +0100984 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
985 data={'vnf' : filtered_content}
986 #GET VM
tiernof97fd272016-07-11 14:32:37 +0200987 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
tierno36c0b172017-01-12 18:32:28 +0100988 SELECT=('vms.uuid as uuid','vms.name as name', 'vms.description as description', 'boot_data'),
tierno7edb6752016-03-21 17:37:52 +0100989 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +0200990 if len(content)==0:
991 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno36c0b172017-01-12 18:32:28 +0100992 # change boot_data into boot-data
993 for vm in content:
994 if vm.get("boot_data"):
995 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
996 del vm["boot_data"]
tierno7edb6752016-03-21 17:37:52 +0100997
998 data['vnf']['VNFC'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +0200999 #TODO: GET all the information from a VNFC and include it in the output.
tierno42026a02017-02-10 15:13:40 +01001000
tierno7edb6752016-03-21 17:37:52 +01001001 #GET NET
tierno42026a02017-02-10 15:13:40 +01001002 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
tierno7edb6752016-03-21 17:37:52 +01001003 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1004 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +02001005 data['vnf']['nets'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001006
1007 #GET ip-profile for each net
1008 for net in data['vnf']['nets']:
1009 ipprofiles = mydb.get_rows(FROM='ip_profiles',
1010 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1011 WHERE={'net_id': net["uuid"]} )
1012 if len(ipprofiles)==1:
1013 net["ip_profile"] = ipprofiles[0]
1014 elif len(ipprofiles)>1:
1015 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net['uuid']), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +01001016
1017
garciadeblas9f8456e2016-09-05 05:02:59 +02001018 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
tierno42026a02017-02-10 15:13:40 +01001019
garciadeblas9f8456e2016-09-05 05:02:59 +02001020 #GET External Interfaces
tiernof97fd272016-07-11 14:32:37 +02001021 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
tierno7edb6752016-03-21 17:37:52 +01001022 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1023 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
tierno42026a02017-02-10 15:13:40 +01001024 WHERE={'vnfs.uuid': vnf_id},
tierno7edb6752016-03-21 17:37:52 +01001025 WHERE_NOT={'interfaces.external_name': None} )
1026 #print content
tiernof97fd272016-07-11 14:32:37 +02001027 data['vnf']['external-connections'] = content
tierno42026a02017-02-10 15:13:40 +01001028
tiernof97fd272016-07-11 14:32:37 +02001029 return data
tierno7edb6752016-03-21 17:37:52 +01001030
1031
1032def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
1033 # Check tenant exist
1034 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +02001035 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001036 # Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernof97fd272016-07-11 14:32:37 +02001037 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001038 else:
1039 vims={}
1040
1041 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1042 where_or = {}
1043 if tenant_id != "any":
1044 where_or["tenant_id"] = tenant_id
1045 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001046 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
tiernof97fd272016-07-11 14:32:37 +02001047 vnf_id = vnf["uuid"]
tierno42026a02017-02-10 15:13:40 +01001048
tierno7edb6752016-03-21 17:37:52 +01001049 # "Getting the list of flavors and tenants of the VNF"
tierno42026a02017-02-10 15:13:40 +01001050 flavorList = get_flavorlist(mydb, vnf_id)
tiernof97fd272016-07-11 14:32:37 +02001051 if len(flavorList)==0:
1052 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001053
tiernof97fd272016-07-11 14:32:37 +02001054 imageList = get_imagelist(mydb, vnf_id)
1055 if len(imageList)==0:
1056 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001057
tiernof97fd272016-07-11 14:32:37 +02001058 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
1059 if deleted == 0:
1060 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno42026a02017-02-10 15:13:40 +01001061
tierno7edb6752016-03-21 17:37:52 +01001062 undeletedItems = []
1063 for flavor in flavorList:
1064 #check if flavor is used by other vnf
tiernof97fd272016-07-11 14:32:37 +02001065 try:
1066 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
1067 if len(c) > 0:
1068 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
1069 continue
1070 #flavor not used, must be deleted
1071 #delelte at VIM
1072 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id':flavor})
tierno7edb6752016-03-21 17:37:52 +01001073 for flavor_vim in c:
1074 if flavor_vim["datacenter_id"] not in vims:
1075 continue
1076 if flavor_vim['created']=='false': #skip this flavor because not created by openmano
1077 continue
1078 myvim=vims[ flavor_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +02001079 try:
1080 myvim.delete_flavor(flavor_vim["vim_id"])
1081 except vimconn.vimconnNotFoundException as e:
1082 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], flavor_vim["datacenter_id"] )
1083 except vimconn.vimconnException as e:
1084 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1085 flavor_vim["vim_id"], flavor_vim["datacenter_id"], type(e).__name__, str(e))
1086 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"], flavor_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +02001087 #delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1088 mydb.delete_row_by_id('flavors', flavor)
1089 except db_base_Exception as e:
1090 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
tierno7edb6752016-03-21 17:37:52 +01001091 undeletedItems.append("flavor %s" % flavor)
tiernof97fd272016-07-11 14:32:37 +02001092
tierno42026a02017-02-10 15:13:40 +01001093
tierno7edb6752016-03-21 17:37:52 +01001094 for image in imageList:
tiernof97fd272016-07-11 14:32:37 +02001095 try:
1096 #check if image is used by other vnf
1097 c = mydb.get_rows(FROM='vms', WHERE={'image_id':image} )
1098 if len(c) > 0:
1099 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
1100 continue
1101 #image not used, must be deleted
1102 #delelte at VIM
1103 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
tierno7edb6752016-03-21 17:37:52 +01001104 for image_vim in c:
1105 if image_vim["datacenter_id"] not in vims:
1106 continue
1107 if image_vim['created']=='false': #skip this image because not created by openmano
1108 continue
1109 myvim=vims[ image_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +02001110 try:
1111 myvim.delete_image(image_vim["vim_id"])
1112 except vimconn.vimconnNotFoundException as e:
1113 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
1114 except vimconn.vimconnException as e:
1115 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1116 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
1117 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +02001118 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1119 mydb.delete_row_by_id('images', image)
1120 except db_base_Exception as e:
1121 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
tierno7edb6752016-03-21 17:37:52 +01001122 undeletedItems.append("image %s" % image)
1123
tiernof97fd272016-07-11 14:32:37 +02001124 return vnf_id + " " + vnf["name"]
tierno42026a02017-02-10 15:13:40 +01001125 #if undeletedItems:
tiernof97fd272016-07-11 14:32:37 +02001126 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
tierno7edb6752016-03-21 17:37:52 +01001127
tiernob3d36742017-03-03 23:51:05 +01001128
tierno7edb6752016-03-21 17:37:52 +01001129def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
1130 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
1131 if result < 0:
1132 return result, vims
1133 elif result == 0:
1134 return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name
1135 myvim = vims.values()[0]
1136 result,servers = myvim.get_hosts_info()
1137 if result < 0:
1138 return result, servers
1139 topology = {'name':myvim['name'] , 'servers': servers}
1140 return result, topology
1141
tiernob3d36742017-03-03 23:51:05 +01001142
tierno7edb6752016-03-21 17:37:52 +01001143def get_hosts(mydb, nfvo_tenant_id):
tiernof97fd272016-07-11 14:32:37 +02001144 vims = get_vim(mydb, nfvo_tenant_id)
1145 if len(vims) == 0:
1146 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found)
1147 elif len(vims)>1:
1148 #print "nfvo.datacenter_action() error. Several datacenters found"
1149 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001150 myvim = vims.values()[0]
tiernof97fd272016-07-11 14:32:37 +02001151 try:
1152 hosts = myvim.get_hosts()
1153 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01001154
tiernof97fd272016-07-11 14:32:37 +02001155 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
1156 for host in hosts:
1157 server={'name':host['name'], 'vms':[]}
1158 for vm in host['instances']:
1159 #get internal name and model
tierno42026a02017-02-10 15:13:40 +01001160 try:
tiernof97fd272016-07-11 14:32:37 +02001161 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1162 WHERE={'vim_vm_id':vm['id']} )
1163 if len(c) == 0:
1164 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1165 continue
1166 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
tierno42026a02017-02-10 15:13:40 +01001167
tiernof97fd272016-07-11 14:32:37 +02001168 except db_base_Exception as e:
1169 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1170 datacenter['Datacenters'][0]['servers'].append(server)
1171 #return -400, "en construccion"
tierno42026a02017-02-10 15:13:40 +01001172
tiernof97fd272016-07-11 14:32:37 +02001173 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1174 return datacenter
1175 except vimconn.vimconnException as e:
1176 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001177
tiernob3d36742017-03-03 23:51:05 +01001178
tierno7edb6752016-03-21 17:37:52 +01001179def new_scenario(mydb, tenant_id, topo):
1180
1181# result, vims = get_vim(mydb, tenant_id)
1182# if result < 0:
1183# return result, vims
1184#1: parse input
1185 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001186 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001187 if "tenant_id" in topo:
1188 if topo["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001189 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id),
1190 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001191 else:
1192 tenant_id=None
1193
tierno42026a02017-02-10 15:13:40 +01001194#1.1: get VNFs and external_networks (other_nets).
tierno7edb6752016-03-21 17:37:52 +01001195 vnfs={}
1196 other_nets={} #external_networks, bridge_networks and data_networkds
1197 nodes = topo['topology']['nodes']
1198 for k in nodes.keys():
1199 if nodes[k]['type'] == 'VNF':
1200 vnfs[k] = nodes[k]
1201 vnfs[k]['ifaces'] = {}
tierno42026a02017-02-10 15:13:40 +01001202 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
tierno7edb6752016-03-21 17:37:52 +01001203 other_nets[k] = nodes[k]
1204 other_nets[k]['external']=True
tierno42026a02017-02-10 15:13:40 +01001205 elif nodes[k]['type'] == 'network':
tierno7edb6752016-03-21 17:37:52 +01001206 other_nets[k] = nodes[k]
1207 other_nets[k]['external']=False
tierno42026a02017-02-10 15:13:40 +01001208
tierno7edb6752016-03-21 17:37:52 +01001209
1210#1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1211 for name,vnf in vnfs.items():
tiernocea279c2016-07-18 12:36:49 +02001212 where={}
1213 where_or={"tenant_id": tenant_id, 'public': "true"}
tierno7edb6752016-03-21 17:37:52 +01001214 error_text = ""
1215 error_pos = "'topology':'nodes':'" + name + "'"
1216 if 'vnf_id' in vnf:
1217 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001218 where['uuid'] = vnf['vnf_id']
tierno7edb6752016-03-21 17:37:52 +01001219 if 'VNF model' in vnf:
1220 error_text += " 'VNF model' " + vnf['VNF model']
tiernocea279c2016-07-18 12:36:49 +02001221 where['name'] = vnf['VNF model']
1222 if len(where) == 0:
tiernof97fd272016-07-11 14:32:37 +02001223 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +01001224
tiernocea279c2016-07-18 12:36:49 +02001225 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1226 FROM='vnfs',
tierno42026a02017-02-10 15:13:40 +01001227 WHERE=where,
tiernocea279c2016-07-18 12:36:49 +02001228 WHERE_OR=where_or,
1229 WHERE_AND_OR="AND")
tiernof97fd272016-07-11 14:32:37 +02001230 if len(vnf_db)==0:
1231 raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1232 elif len(vnf_db)>1:
1233 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001234 vnf['uuid']=vnf_db[0]['uuid']
1235 vnf['description']=vnf_db[0]['description']
1236 #get external interfaces
tierno42026a02017-02-10 15:13:40 +01001237 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1238 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
tierno7edb6752016-03-21 17:37:52 +01001239 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
tierno7edb6752016-03-21 17:37:52 +01001240 for ext_iface in ext_ifaces:
1241 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1242
1243#1.4 get list of connections
1244 conections = topo['topology']['connections']
1245 conections_list = []
tiernoefd80c92016-09-16 14:17:46 +02001246 conections_list_name = []
tierno7edb6752016-03-21 17:37:52 +01001247 for k in conections.keys():
1248 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1249 ifaces_list = conections[k]['nodes'].items()
1250 elif type(conections[k]['nodes'])==list: #list with dictionary
1251 ifaces_list=[]
1252 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1253 for k2 in conection_pair_list:
1254 ifaces_list += k2
1255
1256 con_type = conections[k].get("type", "link")
1257 if con_type != "link":
1258 if k in other_nets:
tiernof97fd272016-07-11 14:32:37 +02001259 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001260 other_nets[k] = {'external': False}
1261 if conections[k].get("graph"):
1262 other_nets[k]["graph"] = conections[k]["graph"]
1263 ifaces_list.append( (k, None) )
1264
tierno42026a02017-02-10 15:13:40 +01001265
tierno7edb6752016-03-21 17:37:52 +01001266 if con_type == "external_network":
1267 other_nets[k]['external'] = True
1268 if conections[k].get("model"):
1269 other_nets[k]["model"] = conections[k]["model"]
1270 else:
1271 other_nets[k]["model"] = k
tierno42026a02017-02-10 15:13:40 +01001272 if con_type == "dataplane_net" or con_type == "bridge_net":
tierno7edb6752016-03-21 17:37:52 +01001273 other_nets[k]["model"] = con_type
tierno42026a02017-02-10 15:13:40 +01001274
tiernoefd80c92016-09-16 14:17:46 +02001275 conections_list_name.append(k)
tierno7edb6752016-03-21 17:37:52 +01001276 conections_list.append(set(ifaces_list)) #from list to set to operate as a set (this conversion removes elements that are repeated in a list)
1277 #print set(ifaces_list)
1278 #check valid VNF and iface names
1279 for iface in ifaces_list:
1280 if iface[0] not in vnfs and iface[0] not in other_nets :
tiernof97fd272016-07-11 14:32:37 +02001281 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1282 str(k), iface[0]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001283 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
tiernof97fd272016-07-11 14:32:37 +02001284 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1285 str(k), iface[0], iface[1]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001286
1287#1.5 unify connections from the pair list to a consolidated list
1288 index=0
1289 while index < len(conections_list):
1290 index2 = index+1
1291 while index2 < len(conections_list):
1292 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
1293 conections_list[index] |= conections_list[index2]
1294 del conections_list[index2]
tiernoefd80c92016-09-16 14:17:46 +02001295 del conections_list_name[index2]
tierno7edb6752016-03-21 17:37:52 +01001296 else:
1297 index2 += 1
1298 conections_list[index] = list(conections_list[index]) # from set to list again
1299 index += 1
1300 #for k in conections_list:
1301 # print k
tierno42026a02017-02-10 15:13:40 +01001302
tierno7edb6752016-03-21 17:37:52 +01001303
1304
1305#1.6 Delete non external nets
1306# for k in other_nets.keys():
1307# if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1308# for con in conections_list:
1309# delete_indexes=[]
1310# for index in range(0,len(con)):
1311# if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1312# for index in delete_indexes:
1313# del con[index]
1314# del other_nets[k]
1315#1.7: Check external_ports are present at database table datacenter_nets
1316 for k,net in other_nets.items():
1317 error_pos = "'topology':'nodes':'" + k + "'"
1318 if net['external']==False:
1319 if 'name' not in net:
1320 net['name']=k
1321 if 'model' not in net:
tiernof97fd272016-07-11 14:32:37 +02001322 raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001323 if net['model']=='bridge_net':
1324 net['type']='bridge';
1325 elif net['model']=='dataplane_net':
1326 net['type']='data';
1327 else:
tiernof97fd272016-07-11 14:32:37 +02001328 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001329 else: #external
1330#IF we do not want to check that external network exist at datacenter
1331 pass
tierno42026a02017-02-10 15:13:40 +01001332#ELSE
tierno7edb6752016-03-21 17:37:52 +01001333# error_text = ""
1334# WHERE_={}
1335# if 'net_id' in net:
1336# error_text += " 'net_id' " + net['net_id']
1337# WHERE_['uuid'] = net['net_id']
1338# if 'model' in net:
1339# error_text += " 'model' " + net['model']
1340# WHERE_['name'] = net['model']
1341# if len(WHERE_) == 0:
1342# return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1343# r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1344# FROM='datacenter_nets', WHERE=WHERE_ )
1345# if r<0:
1346# print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1347# elif r==0:
1348# print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1349# return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1350# elif r>1:
tierno42026a02017-02-10 15:13:40 +01001351# print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1352# return -HTTP_Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
tierno7edb6752016-03-21 17:37:52 +01001353# other_nets[k].update(net_db[0])
tierno42026a02017-02-10 15:13:40 +01001354#ENDIF
tierno7edb6752016-03-21 17:37:52 +01001355 net_list={}
1356 net_nb=0 #Number of nets
1357 for con in conections_list:
1358 #check if this is connected to a external net
1359 other_net_index=-1
1360 #print
1361 #print "con", con
1362 for index in range(0,len(con)):
1363 #check if this is connected to a external net
1364 for net_key in other_nets.keys():
1365 if con[index][0]==net_key:
1366 if other_net_index>=0:
tierno42026a02017-02-10 15:13:40 +01001367 error_text="There is some interface connected both to net '%s' and net '%s'" % (con[other_net_index][0], net_key)
tiernof97fd272016-07-11 14:32:37 +02001368 #print "nfvo.new_scenario " + error_text
1369 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001370 else:
1371 other_net_index = index
1372 net_target = net_key
1373 break
1374 #print "other_net_index", other_net_index
1375 try:
1376 if other_net_index>=0:
1377 del con[other_net_index]
1378#IF we do not want to check that external network exist at datacenter
1379 if other_nets[net_target]['external'] :
1380 if "name" not in other_nets[net_target]:
1381 other_nets[net_target]['name'] = other_nets[net_target]['model']
1382 if other_nets[net_target]["type"] == "external_network":
1383 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
1384 other_nets[net_target]["type"] = "data"
1385 else:
1386 other_nets[net_target]["type"] = "bridge"
tierno42026a02017-02-10 15:13:40 +01001387#ELSE
tierno7edb6752016-03-21 17:37:52 +01001388# if other_nets[net_target]['external'] :
1389# type_='data' if len(con)>1 else 'ptp' #an external net is connected to a external port, so it is ptp if only one connection is done to this net
1390# if type_=='data' and other_nets[net_target]['type']=="ptp":
1391# error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
1392# print "nfvo.new_scenario " + error_text
1393# return -HTTP_Bad_Request, error_text
tierno42026a02017-02-10 15:13:40 +01001394#ENDIF
tierno7edb6752016-03-21 17:37:52 +01001395 for iface in con:
1396 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1397 else:
1398 #create a net
1399 net_type_bridge=False
1400 net_type_data=False
1401 net_target = "__-__net"+str(net_nb)
tierno42026a02017-02-10 15:13:40 +01001402 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
tiernoefd80c92016-09-16 14:17:46 +02001403 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
tierno42026a02017-02-10 15:13:40 +01001404 'external':False}
tierno7edb6752016-03-21 17:37:52 +01001405 for iface in con:
1406 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1407 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
1408 if iface_type=='mgmt' or iface_type=='bridge':
1409 net_type_bridge = True
1410 else:
1411 net_type_data = True
1412 if net_type_bridge and net_type_data:
1413 error_text = "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02001414 #print "nfvo.new_scenario " + error_text
1415 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001416 elif net_type_bridge:
1417 type_='bridge'
1418 else:
1419 type_='data' if len(con)>2 else 'ptp'
1420 net_list[net_target]['type'] = type_
1421 net_nb+=1
1422 except Exception:
1423 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02001424 #print "nfvo.new_scenario " + error_text
tierno7edb6752016-03-21 17:37:52 +01001425 #raise e
tiernof97fd272016-07-11 14:32:37 +02001426 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001427
1428#1.8: Connect to management net all not already connected interfaces of type 'mgmt'
tierno42026a02017-02-10 15:13:40 +01001429 #1.8.1 obtain management net
tiernof97fd272016-07-11 14:32:37 +02001430 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
tierno7edb6752016-03-21 17:37:52 +01001431 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
tierno42026a02017-02-10 15:13:40 +01001432 #1.8.2 check all interfaces from all vnfs
tiernof97fd272016-07-11 14:32:37 +02001433 if len(mgmt_net)>0:
tierno7edb6752016-03-21 17:37:52 +01001434 add_mgmt_net = False
1435 for vnf in vnfs.values():
1436 for iface in vnf['ifaces'].values():
1437 if iface['type']=='mgmt' and 'net_key' not in iface:
1438 #iface not connected
1439 iface['net_key'] = 'mgmt'
1440 add_mgmt_net = True
1441 if add_mgmt_net and 'mgmt' not in net_list:
1442 net_list['mgmt']=mgmt_net[0]
1443 net_list['mgmt']['external']=True
1444 net_list['mgmt']['graph']={'visible':False}
1445
1446 net_list.update(other_nets)
tiernof97fd272016-07-11 14:32:37 +02001447 #print
1448 #print 'net_list', net_list
1449 #print
1450 #print 'vnfs', vnfs
1451 #print
tierno7edb6752016-03-21 17:37:52 +01001452
1453#2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
tiernof97fd272016-07-11 14:32:37 +02001454 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
tierno392f2852016-05-13 12:28:55 +02001455 'tenant_id':tenant_id, 'name':topo['name'],
1456 'description':topo.get('description',topo['name']),
1457 'public': topo.get('public', False)
1458 })
tierno42026a02017-02-10 15:13:40 +01001459
tiernof97fd272016-07-11 14:32:37 +02001460 return c
tierno7edb6752016-03-21 17:37:52 +01001461
tiernob3d36742017-03-03 23:51:05 +01001462
tierno5bb59dc2017-02-13 14:53:54 +01001463def new_scenario_v02(mydb, tenant_id, scenario_dict, version):
1464 """ This creates a new scenario for version 0.2 and 0.3"""
tierno392f2852016-05-13 12:28:55 +02001465 scenario = scenario_dict["scenario"]
tierno7edb6752016-03-21 17:37:52 +01001466 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001467 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001468 if "tenant_id" in scenario:
1469 if scenario["tenant_id"] != tenant_id:
tierno5bb59dc2017-02-13 14:53:54 +01001470 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
tiernof97fd272016-07-11 14:32:37 +02001471 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
1472 scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001473 else:
1474 tenant_id=None
1475
tierno5bb59dc2017-02-13 14:53:54 +01001476 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
tierno7edb6752016-03-21 17:37:52 +01001477 for name,vnf in scenario["vnfs"].iteritems():
tiernocea279c2016-07-18 12:36:49 +02001478 where={}
1479 where_or={"tenant_id": tenant_id, 'public': "true"}
tierno7edb6752016-03-21 17:37:52 +01001480 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02001481 error_pos = "'scenario':'vnfs':'" + name + "'"
tierno7edb6752016-03-21 17:37:52 +01001482 if 'vnf_id' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01001483 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001484 where['uuid'] = vnf['vnf_id']
tierno392f2852016-05-13 12:28:55 +02001485 if 'vnf_name' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01001486 error_text += " 'vnf_name' " + vnf['vnf_name']
tiernocea279c2016-07-18 12:36:49 +02001487 where['name'] = vnf['vnf_name']
1488 if len(where) == 0:
garciadeblas71781ea2016-09-19 14:41:59 +02001489 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01001490 vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'),
tiernocea279c2016-07-18 12:36:49 +02001491 FROM='vnfs',
1492 WHERE=where,
1493 WHERE_OR=where_or,
1494 WHERE_AND_OR="AND")
tierno5bb59dc2017-02-13 14:53:54 +01001495 if len(vnf_db) == 0:
tiernof97fd272016-07-11 14:32:37 +02001496 raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
tierno5bb59dc2017-02-13 14:53:54 +01001497 elif len(vnf_db) > 1:
tiernof97fd272016-07-11 14:32:37 +02001498 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno5bb59dc2017-02-13 14:53:54 +01001499 vnf['uuid'] = vnf_db[0]['uuid']
1500 vnf['description'] = vnf_db[0]['description']
tierno7edb6752016-03-21 17:37:52 +01001501 vnf['ifaces'] = {}
tierno5bb59dc2017-02-13 14:53:54 +01001502 # get external interfaces
1503 ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
1504 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1505 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name': None} )
tierno7edb6752016-03-21 17:37:52 +01001506 for ext_iface in ext_ifaces:
tierno5bb59dc2017-02-13 14:53:54 +01001507 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']}
1508 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
tierno7edb6752016-03-21 17:37:52 +01001509
tierno5bb59dc2017-02-13 14:53:54 +01001510 # 2: Insert net_key and ip_address at every vnf interface
1511 for net_name, net in scenario["networks"].items():
1512 net_type_bridge = False
1513 net_type_data = False
tierno7edb6752016-03-21 17:37:52 +01001514 for iface_dict in net["interfaces"]:
tierno5bb59dc2017-02-13 14:53:54 +01001515 if version == "0.2":
1516 temp_dict = iface_dict
1517 ip_address = None
1518 elif version == "0.3":
1519 temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]}
1520 ip_address = iface_dict.get('ip_address', None)
1521 for vnf, iface in temp_dict.items():
tierno7edb6752016-03-21 17:37:52 +01001522 if vnf not in scenario["vnfs"]:
tierno5bb59dc2017-02-13 14:53:54 +01001523 error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
1524 net_name, vnf)
1525 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02001526 raise NfvoException(error_text, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001527 if iface not in scenario["vnfs"][vnf]['ifaces']:
tierno5bb59dc2017-02-13 14:53:54 +01001528 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
1529 .format(net_name, iface)
1530 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02001531 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001532 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
tierno5bb59dc2017-02-13 14:53:54 +01001533 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
1534 "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
1535 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02001536 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001537 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
tierno5bb59dc2017-02-13 14:53:54 +01001538 scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address
tierno7edb6752016-03-21 17:37:52 +01001539 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
tierno5bb59dc2017-02-13 14:53:54 +01001540 if iface_type == 'mgmt' or iface_type == 'bridge':
tierno7edb6752016-03-21 17:37:52 +01001541 net_type_bridge = True
1542 else:
1543 net_type_data = True
tierno5bb59dc2017-02-13 14:53:54 +01001544
tierno7edb6752016-03-21 17:37:52 +01001545 if net_type_bridge and net_type_data:
tierno5bb59dc2017-02-13 14:53:54 +01001546 error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
1547 .format(net_name)
1548 # logger.debug("nfvo.new_scenario " + error_text)
tiernof97fd272016-07-11 14:32:37 +02001549 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001550 elif net_type_bridge:
tierno5bb59dc2017-02-13 14:53:54 +01001551 type_ = 'bridge'
tierno7edb6752016-03-21 17:37:52 +01001552 else:
tierno5bb59dc2017-02-13 14:53:54 +01001553 type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp'
1554
1555 if net.get("implementation"): # for v0.3
1556 if type_ == "bridge" and net["implementation"] == "underlay":
1557 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\
1558 "'network':'{}'".format(net_name)
1559 # logger.debug(error_text)
1560 raise NfvoException(error_text, HTTP_Bad_Request)
1561 elif type_ != "bridge" and net["implementation"] == "overlay":
1562 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\
1563 "'network':'{}'".format(net_name)
1564 # logger.debug(error_text)
1565 raise NfvoException(error_text, HTTP_Bad_Request)
1566 net.pop("implementation")
1567 if "type" in net and version == "0.3": # for v0.3
1568 if type_ == "data" and net["type"] == "e-line":
1569 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\
1570 "'e-line' at 'network':'{}'".format(net_name)
1571 # logger.debug(error_text)
1572 raise NfvoException(error_text, HTTP_Bad_Request)
1573 elif type_ == "ptp" and net["type"] == "e-lan":
1574 type_ = "data"
1575
tierno7edb6752016-03-21 17:37:52 +01001576 net['type'] = type_
1577 net['name'] = net_name
1578 net['external'] = net.get('external', False)
1579
tierno5bb59dc2017-02-13 14:53:54 +01001580 # 3: insert at database
tierno7edb6752016-03-21 17:37:52 +01001581 scenario["nets"] = scenario["networks"]
1582 scenario['tenant_id'] = tenant_id
tierno5bb59dc2017-02-13 14:53:54 +01001583 scenario_id = mydb.new_scenario(scenario)
tiernof97fd272016-07-11 14:32:37 +02001584 return scenario_id
tierno7edb6752016-03-21 17:37:52 +01001585
tiernob3d36742017-03-03 23:51:05 +01001586
tierno7edb6752016-03-21 17:37:52 +01001587def edit_scenario(mydb, tenant_id, scenario_id, data):
1588 data["uuid"] = scenario_id
1589 data["tenant_id"] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02001590 c = mydb.edit_scenario( data )
1591 return c
tierno7edb6752016-03-21 17:37:52 +01001592
tiernob3d36742017-03-03 23:51:05 +01001593
tierno7edb6752016-03-21 17:37:52 +01001594def start_scenario(mydb, tenant_id, scenario_id, instance_scenario_name, instance_scenario_description, datacenter=None,vim_tenant=None, startvms=True):
tiernoae4a8d12016-07-08 12:30:39 +02001595 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00001596 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
1597 vims = {datacenter_id: myvim}
tierno392f2852016-05-13 12:28:55 +02001598 myvim_tenant = myvim['tenant_id']
tierno7edb6752016-03-21 17:37:52 +01001599 datacenter_name = myvim['name']
tiernoa2793912016-10-04 08:15:08 +00001600
tierno7edb6752016-03-21 17:37:52 +01001601 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02001602 try:
1603 #print "Checking that the scenario_id exists and getting the scenario dictionary"
tiernof97fd272016-07-11 14:32:37 +02001604 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id)
tiernoa2793912016-10-04 08:15:08 +00001605 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
tiernoae4a8d12016-07-08 12:30:39 +02001606 scenarioDict['datacenter_id'] = datacenter_id
1607 #print '================scenarioDict======================='
1608 #print json.dumps(scenarioDict, indent=4)
1609 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
tierno42026a02017-02-10 15:13:40 +01001610
tiernoae4a8d12016-07-08 12:30:39 +02001611 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
1612 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001613
tiernoae4a8d12016-07-08 12:30:39 +02001614 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
1615 auxNetDict['scenario'] = {}
tierno42026a02017-02-10 15:13:40 +01001616
tiernoae4a8d12016-07-08 12:30:39 +02001617 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
1618 for sce_net in scenarioDict['nets']:
1619 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
tierno42026a02017-02-10 15:13:40 +01001620
tiernoae4a8d12016-07-08 12:30:39 +02001621 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
tierno7edb6752016-03-21 17:37:52 +01001622 myNetName = myNetName[0:255] #limit length
tiernoae4a8d12016-07-08 12:30:39 +02001623 myNetType = sce_net['type']
tierno7edb6752016-03-21 17:37:52 +01001624 myNetDict = {}
1625 myNetDict["name"] = myNetName
1626 myNetDict["type"] = myNetType
1627 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02001628 myNetIPProfile = sce_net.get('ip_profile', None)
tierno7edb6752016-03-21 17:37:52 +01001629 #TODO:
tiernoae4a8d12016-07-08 12:30:39 +02001630 #We should use the dictionary as input parameter for new_network
tiernof97fd272016-07-11 14:32:37 +02001631 #print myNetDict
tiernoae4a8d12016-07-08 12:30:39 +02001632 if not sce_net["external"]:
garciadeblas9f8456e2016-09-05 05:02:59 +02001633 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02001634 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
1635 sce_net['vim_id'] = network_id
1636 auxNetDict['scenario'][sce_net['uuid']] = network_id
1637 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001638 sce_net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02001639 else:
1640 if sce_net['vim_id'] == None:
1641 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
1642 _, message = rollback(mydb, vims, rollbackList)
1643 logger.error("nfvo.start_scenario: %s", error_text)
tiernof97fd272016-07-11 14:32:37 +02001644 raise NfvoException(error_text, HTTP_Bad_Request)
tiernoae4a8d12016-07-08 12:30:39 +02001645 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
1646 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
tierno42026a02017-02-10 15:13:40 +01001647
tiernoae4a8d12016-07-08 12:30:39 +02001648 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
1649 #For each vnf net, we create it and we add it to instanceNetlist.
1650 for sce_vnf in scenarioDict['vnfs']:
1651 for net in sce_vnf['nets']:
1652 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
tierno42026a02017-02-10 15:13:40 +01001653
tiernoae4a8d12016-07-08 12:30:39 +02001654 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
1655 myNetName = myNetName[0:255] #limit length
1656 myNetType = net['type']
1657 myNetDict = {}
1658 myNetDict["name"] = myNetName
1659 myNetDict["type"] = myNetType
1660 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02001661 myNetIPProfile = net.get('ip_profile', None)
tiernoae4a8d12016-07-08 12:30:39 +02001662 #print myNetDict
1663 #TODO:
1664 #We should use the dictionary as input parameter for new_network
garciadeblas9f8456e2016-09-05 05:02:59 +02001665 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02001666 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
1667 net['vim_id'] = network_id
1668 if sce_vnf['uuid'] not in auxNetDict:
1669 auxNetDict[sce_vnf['uuid']] = {}
1670 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
1671 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001672 net["created"] = True
tierno42026a02017-02-10 15:13:40 +01001673
tiernoae4a8d12016-07-08 12:30:39 +02001674 #print "auxNetDict:"
1675 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001676
tiernoae4a8d12016-07-08 12:30:39 +02001677 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
1678 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
1679 i = 0
1680 for sce_vnf in scenarioDict['vnfs']:
1681 for vm in sce_vnf['vms']:
1682 i += 1
1683 myVMDict = {}
1684 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
tiernoae65a482016-11-24 16:20:05 +01001685 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
tiernoae4a8d12016-07-08 12:30:39 +02001686 #myVMDict['description'] = vm['description']
1687 myVMDict['description'] = myVMDict['name'][0:99]
1688 if not startvms:
1689 myVMDict['start'] = "no"
1690 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
1691 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
tierno42026a02017-02-10 15:13:40 +01001692
tiernoae4a8d12016-07-08 12:30:39 +02001693 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02001694 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno42026a02017-02-10 15:13:40 +01001695 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02001696 vm['vim_image_id'] = image_id
tierno42026a02017-02-10 15:13:40 +01001697
tiernoae4a8d12016-07-08 12:30:39 +02001698 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02001699 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tiernoae4a8d12016-07-08 12:30:39 +02001700 if flavor_dict['extended']!=None:
1701 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
tierno42026a02017-02-10 15:13:40 +01001702 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02001703 vm['vim_flavor_id'] = flavor_id
tierno42026a02017-02-10 15:13:40 +01001704
1705
tiernoae4a8d12016-07-08 12:30:39 +02001706 myVMDict['imageRef'] = vm['vim_image_id']
1707 myVMDict['flavorRef'] = vm['vim_flavor_id']
1708 myVMDict['networks'] = []
1709 for iface in vm['interfaces']:
1710 netDict = {}
1711 if iface['type']=="data":
1712 netDict['type'] = iface['model']
1713 elif "model" in iface and iface["model"]!=None:
1714 netDict['model']=iface['model']
1715 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
1716 #discover type of interface looking at flavor
1717 for numa in flavor_dict.get('extended',{}).get('numas',[]):
1718 for flavor_iface in numa.get('interfaces',[]):
1719 if flavor_iface.get('name') == iface['internal_name']:
1720 if flavor_iface['dedicated'] == 'yes':
1721 netDict['type']="PF" #passthrough
1722 elif flavor_iface['dedicated'] == 'no':
1723 netDict['type']="VF" #siov
1724 elif flavor_iface['dedicated'] == 'yes:sriov':
1725 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
1726 netDict["mac_address"] = flavor_iface.get("mac_address")
1727 break;
1728 netDict["use"]=iface['type']
1729 if netDict["use"]=="data" and not netDict.get("type"):
1730 #print "netDict", netDict
1731 #print "iface", iface
1732 e_text = "Cannot determine the interface type PF or VF of VNF '%s' VM '%s' iface '%s'" %(sce_vnf['name'], vm['name'], iface['internal_name'])
1733 if flavor_dict.get('extended')==None:
tiernof97fd272016-07-11 14:32:37 +02001734 raise NfvoException(e_text + "After database migration some information is not available. \
1735 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tiernoae4a8d12016-07-08 12:30:39 +02001736 else:
tiernof97fd272016-07-11 14:32:37 +02001737 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tiernoae4a8d12016-07-08 12:30:39 +02001738 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
1739 netDict["type"]="virtual"
1740 if "vpci" in iface and iface["vpci"] is not None:
1741 netDict['vpci'] = iface['vpci']
1742 if "mac" in iface and iface["mac"] is not None:
1743 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00001744 if "port-security" in iface and iface["port-security"] is not None:
1745 netDict['port_security'] = iface['port-security']
1746 if "floating-ip" in iface and iface["floating-ip"] is not None:
1747 netDict['floating_ip'] = iface['floating-ip']
tiernoae4a8d12016-07-08 12:30:39 +02001748 netDict['name'] = iface['internal_name']
1749 if iface['net_id'] is None:
1750 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02001751 #print iface
1752 #print vnf_iface
tiernoae4a8d12016-07-08 12:30:39 +02001753 if vnf_iface['interface_id']==iface['uuid']:
1754 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
1755 break
1756 else:
1757 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
1758 #skip bridge ifaces not connected to any net
1759 #if 'net_id' not in netDict or netDict['net_id']==None:
1760 # continue
1761 myVMDict['networks'].append(netDict)
1762 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
1763 #print myVMDict['name']
1764 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
1765 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
1766 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
1767 vm_id = myvim.new_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None),
1768 myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks'])
1769 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
1770 vm['vim_id'] = vm_id
1771 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
1772 #put interface uuid back to scenario[vnfs][vms[[interfaces]
1773 for net in myVMDict['networks']:
1774 if "vim_id" in net:
1775 for iface in vm['interfaces']:
1776 if net["name"]==iface["internal_name"]:
1777 iface["vim_id"]=net["vim_id"]
1778 break
tierno42026a02017-02-10 15:13:40 +01001779
tiernoae4a8d12016-07-08 12:30:39 +02001780 logger.debug("start scenario Deployment done")
1781 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
1782 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
tiernof97fd272016-07-11 14:32:37 +02001783 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
1784 return mydb.get_instance_scenario(instance_id)
tierno42026a02017-02-10 15:13:40 +01001785
tiernof97fd272016-07-11 14:32:37 +02001786 except (db_base_Exception, vimconn.vimconnException) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001787 _, message = rollback(mydb, vims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02001788 if isinstance(e, db_base_Exception):
1789 error_text = "Exception at database"
1790 else:
1791 error_text = "Exception at VIM"
1792 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1793 #logger.error("start_scenario %s", error_text)
1794 raise NfvoException(error_text, e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001795
tiernob3d36742017-03-03 23:51:05 +01001796
tierno36c0b172017-01-12 18:32:28 +01001797def unify_cloud_config(cloud_config_preserve, cloud_config):
1798 ''' join the cloud config information into cloud_config_preserve.
1799 In case of conflict cloud_config_preserve preserves
1800 None is admited
1801 '''
1802 if not cloud_config_preserve and not cloud_config:
1803 return None
1804
1805 new_cloud_config = {"key-pairs":[], "users":[]}
1806 # key-pairs
1807 if cloud_config_preserve:
1808 for key in cloud_config_preserve.get("key-pairs", () ):
1809 if key not in new_cloud_config["key-pairs"]:
1810 new_cloud_config["key-pairs"].append(key)
1811 if cloud_config:
1812 for key in cloud_config.get("key-pairs", () ):
1813 if key not in new_cloud_config["key-pairs"]:
1814 new_cloud_config["key-pairs"].append(key)
1815 if not new_cloud_config["key-pairs"]:
1816 del new_cloud_config["key-pairs"]
1817
1818 # users
1819 if cloud_config:
1820 new_cloud_config["users"] += cloud_config.get("users", () )
1821 if cloud_config_preserve:
1822 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
tiernoa4e1a6e2016-08-31 14:19:40 +02001823 index_to_delete = []
tierno36c0b172017-01-12 18:32:28 +01001824 users = new_cloud_config.get("users", [])
tiernoa4e1a6e2016-08-31 14:19:40 +02001825 for index0 in range(0,len(users)):
1826 if index0 in index_to_delete:
1827 continue
1828 for index1 in range(index0+1,len(users)):
1829 if index1 in index_to_delete:
1830 continue
1831 if users[index0]["name"] == users[index1]["name"]:
1832 index_to_delete.append(index1)
1833 for key in users[index1].get("key-pairs",()):
tierno36c0b172017-01-12 18:32:28 +01001834 if "key-pairs" not in users[index0]:
tiernoa4e1a6e2016-08-31 14:19:40 +02001835 users[index0]["key-pairs"] = [key]
1836 elif key not in users[index0]["key-pairs"]:
1837 users[index0]["key-pairs"].append(key)
1838 index_to_delete.sort(reverse=True)
1839 for index in index_to_delete:
1840 del users[index]
tierno36c0b172017-01-12 18:32:28 +01001841 if not new_cloud_config["users"]:
1842 del new_cloud_config["users"]
1843
1844 #boot-data-drive
1845 if cloud_config and cloud_config.get("boot-data-drive") != None:
1846 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
1847 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
1848 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
1849
1850 # user-data
1851 if cloud_config and cloud_config.get("user-data") != None:
1852 new_cloud_config["user-data"] = cloud_config["user-data"]
1853 if cloud_config_preserve and cloud_config_preserve.get("user-data") != None:
1854 new_cloud_config["user-data"] = cloud_config_preserve["user-data"]
1855
1856 # config files
1857 new_cloud_config["config-files"] = []
1858 if cloud_config and cloud_config.get("config-files") != None:
1859 new_cloud_config["config-files"] += cloud_config["config-files"]
1860 if cloud_config_preserve:
1861 for file in cloud_config_preserve.get("config-files", ()):
1862 for index in range(0, len(new_cloud_config["config-files"])):
1863 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
1864 new_cloud_config["config-files"][index] = file
1865 break
1866 else:
1867 new_cloud_config["config-files"].append(file)
1868 if not new_cloud_config["config-files"]:
1869 del new_cloud_config["config-files"]
1870 return new_cloud_config
1871
1872
tierno867ffe92017-03-27 12:50:34 +02001873def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
tiernob3d36742017-03-03 23:51:05 +01001874 datacenter_id = None
1875 datacenter_name = None
1876 thread = None
tierno867ffe92017-03-27 12:50:34 +02001877 try:
1878 if datacenter_tenant_id:
1879 thread_id = datacenter_tenant_id
1880 thread = vim_threads["running"].get(datacenter_tenant_id)
tiernob3d36742017-03-03 23:51:05 +01001881 else:
tierno867ffe92017-03-27 12:50:34 +02001882 where_={"td.nfvo_tenant_id": tenant_id}
1883 if datacenter_id_name:
1884 if utils.check_valid_uuid(datacenter_id_name):
1885 datacenter_id = datacenter_id_name
1886 where_["dt.datacenter_id"] = datacenter_id
1887 else:
1888 datacenter_name = datacenter_id_name
1889 where_["d.name"] = datacenter_name
1890 if datacenter_tenant_id:
1891 where_["dt.uuid"] = datacenter_tenant_id
1892 datacenters = mydb.get_rows(
1893 SELECT=("dt.uuid as datacenter_tenant_id",),
1894 FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
1895 "join datacenters as d on d.uuid=dt.datacenter_id",
1896 WHERE=where_)
1897 if len(datacenters) > 1:
1898 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
1899 elif datacenters:
1900 thread_id = datacenters[0]["datacenter_tenant_id"]
1901 thread = vim_threads["running"].get(thread_id)
1902 if not thread:
1903 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
1904 return thread_id, thread
1905 except db_base_Exception as e:
1906 raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code)
tiernoa4e1a6e2016-08-31 14:19:40 +02001907
tiernoa2793912016-10-04 08:15:08 +00001908def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
tiernobe41e222016-09-02 15:16:13 +02001909 datacenter_id = None
1910 datacenter_name = None
1911 if datacenter_id_name:
tierno42026a02017-02-10 15:13:40 +01001912 if utils.check_valid_uuid(datacenter_id_name):
tiernobe41e222016-09-02 15:16:13 +02001913 datacenter_id = datacenter_id_name
1914 else:
1915 datacenter_name = datacenter_id_name
tiernoa2793912016-10-04 08:15:08 +00001916 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
tiernobe41e222016-09-02 15:16:13 +02001917 if len(vims) == 0:
1918 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
1919 elif len(vims)>1:
1920 #print "nfvo.datacenter_action() error. Several datacenters found"
1921 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
1922 return vims.keys()[0], vims.values()[0]
1923
tiernob3d36742017-03-03 23:51:05 +01001924
garciadeblas9f8456e2016-09-05 05:02:59 +02001925def update(d, u):
1926 '''Takes dict d and updates it with the values in dict u.'''
1927 '''It merges all depth levels'''
1928 for k, v in u.iteritems():
1929 if isinstance(v, collections.Mapping):
1930 r = update(d.get(k, {}), v)
1931 d[k] = r
1932 else:
1933 d[k] = u[k]
1934 return d
1935
tiernob3d36742017-03-03 23:51:05 +01001936
tierno7edb6752016-03-21 17:37:52 +01001937def create_instance(mydb, tenant_id, instance_dict):
tiernob3d36742017-03-03 23:51:05 +01001938 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
1939 # logger.debug("Creating instance...")
tierno7edb6752016-03-21 17:37:52 +01001940 scenario = instance_dict["scenario"]
tierno42026a02017-02-10 15:13:40 +01001941
tiernobe41e222016-09-02 15:16:13 +02001942 #find main datacenter
1943 myvims = {}
tierno867ffe92017-03-27 12:50:34 +02001944 myvim_threads_id = {}
1945 instance_tasks={}
1946 tasks_to_launch={}
tierno7edb6752016-03-21 17:37:52 +01001947 datacenter = instance_dict.get("datacenter")
tiernobe41e222016-09-02 15:16:13 +02001948 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
1949 myvims[default_datacenter_id] = vim
tierno867ffe92017-03-27 12:50:34 +02001950 myvim_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id)
1951 tasks_to_launch[myvim_threads_id[default_datacenter_id]] = []
tierno392f2852016-05-13 12:28:55 +02001952 #myvim_tenant = myvim['tenant_id']
tiernobe41e222016-09-02 15:16:13 +02001953# default_datacenter_name = vim['name']
tierno7edb6752016-03-21 17:37:52 +01001954 rollbackList=[]
tierno42026a02017-02-10 15:13:40 +01001955
tiernoae4a8d12016-07-08 12:30:39 +02001956 #print "Checking that the scenario exists and getting the scenario dictionary"
tiernobe41e222016-09-02 15:16:13 +02001957 scenarioDict = mydb.get_scenario(scenario, tenant_id, default_datacenter_id)
tierno42026a02017-02-10 15:13:40 +01001958
garciadeblasbb6a1ed2016-09-30 14:02:09 +00001959 #logger.debug(">>>>>>> Dictionaries before merging")
1960 #logger.debug(">>>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
1961 #logger.debug(">>>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
tierno42026a02017-02-10 15:13:40 +01001962
tiernobe41e222016-09-02 15:16:13 +02001963 scenarioDict['datacenter_id'] = default_datacenter_id
garciadeblas9f8456e2016-09-05 05:02:59 +02001964
tierno7edb6752016-03-21 17:37:52 +01001965 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
1966 auxNetDict['scenario'] = {}
tierno42026a02017-02-10 15:13:40 +01001967
1968 logger.debug("Creating instance from scenario-dict:\n%s", yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) #TODO remove
tierno7edb6752016-03-21 17:37:52 +01001969 instance_name = instance_dict["name"]
1970 instance_description = instance_dict.get("description")
1971 try:
tiernob3d36742017-03-03 23:51:05 +01001972 # 0 check correct parameters
tiernobe41e222016-09-02 15:16:13 +02001973 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
tiernob3d36742017-03-03 23:51:05 +01001974 found = False
tierno7edb6752016-03-21 17:37:52 +01001975 for scenario_net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +02001976 if net_name == scenario_net["name"]:
tierno7edb6752016-03-21 17:37:52 +01001977 found = True
1978 break
1979 if not found:
tiernobe41e222016-09-02 15:16:13 +02001980 raise NfvoException("Invalid scenario network name '{}' at instance:networks".format(net_name), HTTP_Bad_Request)
1981 if "sites" not in net_instance_desc:
1982 net_instance_desc["sites"] = [ {} ]
1983 site_without_datacenter_field = False
1984 for site in net_instance_desc["sites"]:
1985 if site.get("datacenter"):
1986 if site["datacenter"] not in myvims:
1987 #Add this datacenter to myvims
1988 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
1989 myvims[d] = v
tierno867ffe92017-03-27 12:50:34 +02001990 myvim_threads_id[d],_ = get_vim_thread(mydb, tenant_id, site["datacenter"])
1991 tasks_to_launch[myvim_threads_id[d]] = []
tiernob3d36742017-03-03 23:51:05 +01001992 site["datacenter"] = d #change name to id
tiernobe41e222016-09-02 15:16:13 +02001993 else:
1994 if site_without_datacenter_field:
1995 raise NfvoException("Found more than one entries without datacenter field at instance:networks:{}:sites".format(net_name), HTTP_Bad_Request)
1996 site_without_datacenter_field = True
tiernob3d36742017-03-03 23:51:05 +01001997 site["datacenter"] = default_datacenter_id #change name to id
tierno42026a02017-02-10 15:13:40 +01001998
tiernobe41e222016-09-02 15:16:13 +02001999 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01002000 found=False
2001 for scenario_vnf in scenarioDict['vnfs']:
tiernobe41e222016-09-02 15:16:13 +02002002 if vnf_name == scenario_vnf['name']:
tierno7edb6752016-03-21 17:37:52 +01002003 found = True
2004 break
2005 if not found:
tiernobe41e222016-09-02 15:16:13 +02002006 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_instance_desc), HTTP_Bad_Request)
2007 if "datacenter" in vnf_instance_desc:
tiernob3d36742017-03-03 23:51:05 +01002008 # Add this datacenter to myvims
tiernobe41e222016-09-02 15:16:13 +02002009 if vnf_instance_desc["datacenter"] not in myvims:
2010 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
2011 myvims[d] = v
tierno867ffe92017-03-27 12:50:34 +02002012 myvim_threads_id[d],_ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"])
2013 tasks_to_launch[myvim_threads_id[d]] = []
tiernoa2793912016-10-04 08:15:08 +00002014 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
garciadeblas30833382017-01-09 09:46:31 +01002015
tiernoa4e1a6e2016-08-31 14:19:40 +02002016 #0.1 parse cloud-config parameters
tierno36c0b172017-01-12 18:32:28 +01002017 cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config"))
garciadeblas9f8456e2016-09-05 05:02:59 +02002018
2019 #0.2 merge instance information into scenario
2020 #Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
2021 #However, this is not possible yet.
2022 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
2023 for scenario_net in scenarioDict['nets']:
2024 if net_name == scenario_net["name"]:
2025 if 'ip-profile' in net_instance_desc:
2026 ipprofile = net_instance_desc['ip-profile']
2027 ipprofile['subnet_address'] = ipprofile.pop('subnet-address',None)
2028 ipprofile['ip_version'] = ipprofile.pop('ip-version','IPv4')
2029 ipprofile['gateway_address'] = ipprofile.pop('gateway-address',None)
2030 ipprofile['dns_address'] = ipprofile.pop('dns-address',None)
2031 if 'dhcp' in ipprofile:
2032 ipprofile['dhcp_start_address'] = ipprofile['dhcp'].get('start-address',None)
2033 ipprofile['dhcp_enabled'] = ipprofile['dhcp'].get('enabled',True)
2034 ipprofile['dhcp_count'] = ipprofile['dhcp'].get('count',None)
2035 del ipprofile['dhcp']
garciadeblasedca7b32016-09-29 14:01:52 +00002036 if 'ip_profile' not in scenario_net:
2037 scenario_net['ip_profile'] = ipprofile
2038 else:
2039 update(scenario_net['ip_profile'],ipprofile)
tiernoe6c58ce2016-09-14 16:02:49 +02002040 for interface in net_instance_desc.get('interfaces', () ):
garciadeblas9f8456e2016-09-05 05:02:59 +02002041 if 'ip_address' in interface:
2042 for vnf in scenarioDict['vnfs']:
2043 if interface['vnf'] == vnf['name']:
2044 for vnf_interface in vnf['interfaces']:
2045 if interface['vnf_interface'] == vnf_interface['external_name']:
2046 vnf_interface['ip_address']=interface['ip_address']
2047
garciadeblasbb6a1ed2016-09-30 14:02:09 +00002048 #logger.debug(">>>>>>>> Merged dictionary")
tierno4319dad2016-09-05 12:11:11 +02002049 logger.debug("Creating instance scenario-dict MERGED:\n%s", yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
garciadeblas9f8456e2016-09-05 05:02:59 +02002050
tierno42026a02017-02-10 15:13:40 +01002051
tiernob3d36742017-03-03 23:51:05 +01002052 # 1. Creating new nets (sce_nets) in the VIM"
tierno7edb6752016-03-21 17:37:52 +01002053 for sce_net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +02002054 sce_net["vim_id_sites"]={}
tierno7edb6752016-03-21 17:37:52 +01002055 descriptor_net = instance_dict.get("networks",{}).get(sce_net["name"],{})
tiernobe41e222016-09-02 15:16:13 +02002056 net_name = descriptor_net.get("vim-network-name")
2057 auxNetDict['scenario'][sce_net['uuid']] = {}
2058
2059 sites = descriptor_net.get("sites", [ {} ])
2060 for site in sites:
2061 if site.get("datacenter"):
2062 vim = myvims[ site["datacenter"] ]
2063 datacenter_id = site["datacenter"]
tierno867ffe92017-03-27 12:50:34 +02002064 myvim_thread_id = myvim_threads_id[ site["datacenter"] ]
tierno7edb6752016-03-21 17:37:52 +01002065 else:
tiernobe41e222016-09-02 15:16:13 +02002066 vim = myvims[ default_datacenter_id ]
2067 datacenter_id = default_datacenter_id
tierno867ffe92017-03-27 12:50:34 +02002068 myvim_thread_id = myvim_threads_id[default_datacenter_id]
tiernobe41e222016-09-02 15:16:13 +02002069 net_type = sce_net['type']
2070 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} #'shared': True
2071 if sce_net["external"]:
2072 if not net_name:
tierno42026a02017-02-10 15:13:40 +01002073 net_name = sce_net["name"]
tiernobe41e222016-09-02 15:16:13 +02002074 if "netmap-use" in site or "netmap-create" in site:
2075 create_network = False
2076 lookfor_network = False
2077 if "netmap-use" in site:
2078 lookfor_network = True
2079 if utils.check_valid_uuid(site["netmap-use"]):
2080 filter_text = "scenario id '%s'" % site["netmap-use"]
2081 lookfor_filter["id"] = site["netmap-use"]
tierno42026a02017-02-10 15:13:40 +01002082 else:
tiernobe41e222016-09-02 15:16:13 +02002083 filter_text = "scenario name '%s'" % site["netmap-use"]
2084 lookfor_filter["name"] = site["netmap-use"]
2085 if "netmap-create" in site:
2086 create_network = True
2087 net_vim_name = net_name
2088 if site["netmap-create"]:
2089 net_vim_name = site["netmap-create"]
tierno42026a02017-02-10 15:13:40 +01002090
tiernobe41e222016-09-02 15:16:13 +02002091 elif sce_net['vim_id'] != None:
2092 #there is a netmap at datacenter_nets database #TODO REVISE!!!!
2093 create_network = False
2094 lookfor_network = True
2095 lookfor_filter["id"] = sce_net['vim_id']
2096 filter_text = "vim_id '%s' datacenter_netmap name '%s'. Try to reload vims with datacenter-net-update" % (sce_net['vim_id'], sce_net["name"])
2097 #look for network at datacenter and return error
2098 else:
2099 #There is not a netmap, look at datacenter for a net with this name and create if not found
2100 create_network = True
2101 lookfor_network = True
2102 lookfor_filter["name"] = sce_net["name"]
2103 net_vim_name = sce_net["name"]
2104 filter_text = "scenario name '%s'" % sce_net["name"]
tierno7edb6752016-03-21 17:37:52 +01002105 else:
tiernobe41e222016-09-02 15:16:13 +02002106 if not net_name:
2107 net_name = "%s.%s" %(instance_name, sce_net["name"])
2108 net_name = net_name[:255] #limit length
2109 net_vim_name = net_name
2110 create_network = True
2111 lookfor_network = False
tierno42026a02017-02-10 15:13:40 +01002112
tiernobe41e222016-09-02 15:16:13 +02002113 if lookfor_network:
2114 vim_nets = vim.get_network_list(filter_dict=lookfor_filter)
2115 if len(vim_nets) > 1:
2116 raise NfvoException("More than one candidate VIM network found for " + filter_text, HTTP_Bad_Request )
2117 elif len(vim_nets) == 0:
2118 if not create_network:
2119 raise NfvoException("No candidate VIM network found for " + filter_text, HTTP_Bad_Request )
2120 else:
2121 sce_net["vim_id_sites"][datacenter_id] = vim_nets[0]['id']
tiernobe41e222016-09-02 15:16:13 +02002122 auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = vim_nets[0]['id']
2123 create_network = False
2124 if create_network:
2125 #if network is not external
tiernob3d36742017-03-03 23:51:05 +01002126 task = new_task("new-net", (net_vim_name, net_type, sce_net.get('ip_profile',None)))
tierno867ffe92017-03-27 12:50:34 +02002127 task_id = task["id"]
tiernob3d36742017-03-03 23:51:05 +01002128 instance_tasks[task_id] = task
tierno867ffe92017-03-27 12:50:34 +02002129 tasks_to_launch[myvim_thread_id].append(task)
tiernob3d36742017-03-03 23:51:05 +01002130 #network_id = vim.new_network(net_vim_name, net_type, sce_net.get('ip_profile',None))
2131 sce_net["vim_id_sites"][datacenter_id] = task_id
2132 auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = task_id
2133 rollbackList.append({'what':'network', 'where':'vim', 'vim_id':datacenter_id, 'uuid':task_id})
tierno66345bc2016-09-26 11:37:55 +02002134 sce_net["created"] = True
tierno42026a02017-02-10 15:13:40 +01002135
tiernob3d36742017-03-03 23:51:05 +01002136 # 2. Creating new nets (vnf internal nets) in the VIM"
tierno7edb6752016-03-21 17:37:52 +01002137 #For each vnf net, we create it and we add it to instanceNetlist.
2138 for sce_vnf in scenarioDict['vnfs']:
2139 for net in sce_vnf['nets']:
tiernobe41e222016-09-02 15:16:13 +02002140 if sce_vnf.get("datacenter"):
2141 vim = myvims[ sce_vnf["datacenter"] ]
2142 datacenter_id = sce_vnf["datacenter"]
tierno867ffe92017-03-27 12:50:34 +02002143 myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"]]
tiernobe41e222016-09-02 15:16:13 +02002144 else:
2145 vim = myvims[ default_datacenter_id ]
2146 datacenter_id = default_datacenter_id
tierno867ffe92017-03-27 12:50:34 +02002147 myvim_thread_id = myvim_threads_id[default_datacenter_id]
tierno7edb6752016-03-21 17:37:52 +01002148 descriptor_net = instance_dict.get("vnfs",{}).get(sce_vnf["name"],{})
2149 net_name = descriptor_net.get("name")
2150 if not net_name:
2151 net_name = "%s.%s" %(instance_name, net["name"])
2152 net_name = net_name[:255] #limit length
2153 net_type = net['type']
tiernob3d36742017-03-03 23:51:05 +01002154 task = new_task("new-net", (net_name, net_type, net.get('ip_profile',None)))
tierno867ffe92017-03-27 12:50:34 +02002155 task_id = task["id"]
tiernob3d36742017-03-03 23:51:05 +01002156 instance_tasks[task_id] = task
tierno867ffe92017-03-27 12:50:34 +02002157 tasks_to_launch[myvim_thread_id].append(task)
tiernob3d36742017-03-03 23:51:05 +01002158 # network_id = vim.new_network(net_name, net_type, net.get('ip_profile',None))
2159 net['vim_id'] = task_id
tierno7edb6752016-03-21 17:37:52 +01002160 if sce_vnf['uuid'] not in auxNetDict:
2161 auxNetDict[sce_vnf['uuid']] = {}
tiernob3d36742017-03-03 23:51:05 +01002162 auxNetDict[sce_vnf['uuid']][net['uuid']] = task_id
2163 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':task_id})
tierno66345bc2016-09-26 11:37:55 +02002164 net["created"] = True
2165
tierno42026a02017-02-10 15:13:40 +01002166
tiernoae4a8d12016-07-08 12:30:39 +02002167 #print "auxNetDict:"
2168 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002169
tiernob3d36742017-03-03 23:51:05 +01002170 # 3. Creating new vm instances in the VIM
tiernoae4a8d12016-07-08 12:30:39 +02002171 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
tierno7edb6752016-03-21 17:37:52 +01002172 for sce_vnf in scenarioDict['vnfs']:
tiernobe41e222016-09-02 15:16:13 +02002173 if sce_vnf.get("datacenter"):
2174 vim = myvims[ sce_vnf["datacenter"] ]
tierno867ffe92017-03-27 12:50:34 +02002175 myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"] ]
tiernobe41e222016-09-02 15:16:13 +02002176 datacenter_id = sce_vnf["datacenter"]
2177 else:
2178 vim = myvims[ default_datacenter_id ]
tierno867ffe92017-03-27 12:50:34 +02002179 myvim_thread_id = myvim_threads_id[ default_datacenter_id ]
tiernobe41e222016-09-02 15:16:13 +02002180 datacenter_id = default_datacenter_id
2181 sce_vnf["datacenter_id"] = datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002182 i = 0
2183 for vm in sce_vnf['vms']:
2184 i += 1
2185 myVMDict = {}
tiernoae65a482016-11-24 16:20:05 +01002186 myVMDict['name'] = "{}.{}.{}".format(instance_name,sce_vnf['name'],chr(96+i))
tierno7edb6752016-03-21 17:37:52 +01002187 myVMDict['description'] = myVMDict['name'][0:99]
2188# if not startvms:
2189# myVMDict['start'] = "no"
2190 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2191 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002192 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno5e91eb82016-10-04 09:39:07 +00002193 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
tierno7edb6752016-03-21 17:37:52 +01002194 vm['vim_image_id'] = image_id
tierno42026a02017-02-10 15:13:40 +01002195
tierno7edb6752016-03-21 17:37:52 +01002196 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002197 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tierno7edb6752016-03-21 17:37:52 +01002198 if flavor_dict['extended']!=None:
2199 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
montesmoreno0c8def02016-12-22 12:16:23 +00002200 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
2201
montesmoreno0c8def02016-12-22 12:16:23 +00002202 #Obtain information for additional disks
2203 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), WHERE={'vim_id': flavor_id})
2204 if not extended_flavor_dict:
2205 raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found)
2206 return
2207
2208 #extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
2209 myVMDict['disks'] = None
2210 extended_info = extended_flavor_dict[0]['extended']
2211 if extended_info != None:
2212 extended_flavor_dict_yaml = yaml.load(extended_info)
2213 if 'disks' in extended_flavor_dict_yaml:
2214 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
2215
tierno7edb6752016-03-21 17:37:52 +01002216 vm['vim_flavor_id'] = flavor_id
tierno7edb6752016-03-21 17:37:52 +01002217 myVMDict['imageRef'] = vm['vim_image_id']
2218 myVMDict['flavorRef'] = vm['vim_flavor_id']
2219 myVMDict['networks'] = []
tiernob3d36742017-03-03 23:51:05 +01002220 task_depends = {}
tiernoa2793912016-10-04 08:15:08 +00002221 #TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
tierno7edb6752016-03-21 17:37:52 +01002222 for iface in vm['interfaces']:
2223 netDict = {}
2224 if iface['type']=="data":
2225 netDict['type'] = iface['model']
2226 elif "model" in iface and iface["model"]!=None:
2227 netDict['model']=iface['model']
2228 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2229 #discover type of interface looking at flavor
2230 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2231 for flavor_iface in numa.get('interfaces',[]):
2232 if flavor_iface.get('name') == iface['internal_name']:
2233 if flavor_iface['dedicated'] == 'yes':
2234 netDict['type']="PF" #passthrough
2235 elif flavor_iface['dedicated'] == 'no':
2236 netDict['type']="VF" #siov
2237 elif flavor_iface['dedicated'] == 'yes:sriov':
2238 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2239 netDict["mac_address"] = flavor_iface.get("mac_address")
2240 break;
2241 netDict["use"]=iface['type']
2242 if netDict["use"]=="data" and not netDict.get("type"):
2243 #print "netDict", netDict
2244 #print "iface", iface
2245 e_text = "Cannot determine the interface type PF or VF of VNF '%s' VM '%s' iface '%s'" %(sce_vnf['name'], vm['name'], iface['internal_name'])
2246 if flavor_dict.get('extended')==None:
tiernoae4a8d12016-07-08 12:30:39 +02002247 raise NfvoException(e_text + "After database migration some information is not available. \
2248 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002249 else:
tiernoae4a8d12016-07-08 12:30:39 +02002250 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01002251 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2252 netDict["type"]="virtual"
2253 if "vpci" in iface and iface["vpci"] is not None:
2254 netDict['vpci'] = iface['vpci']
2255 if "mac" in iface and iface["mac"] is not None:
2256 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00002257 if "port-security" in iface and iface["port-security"] is not None:
2258 netDict['port_security'] = iface['port-security']
2259 if "floating-ip" in iface and iface["floating-ip"] is not None:
2260 netDict['floating_ip'] = iface['floating-ip']
tierno7edb6752016-03-21 17:37:52 +01002261 netDict['name'] = iface['internal_name']
2262 if iface['net_id'] is None:
2263 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02002264 #print iface
2265 #print vnf_iface
tierno7edb6752016-03-21 17:37:52 +01002266 if vnf_iface['interface_id']==iface['uuid']:
tiernobe41e222016-09-02 15:16:13 +02002267 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id]
tierno7edb6752016-03-21 17:37:52 +01002268 break
2269 else:
2270 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
tierno867ffe92017-03-27 12:50:34 +02002271 if netDict.get('net_id') and is_task_id(netDict['net_id']):
tiernob3d36742017-03-03 23:51:05 +01002272 task_depends[netDict['net_id']] = instance_tasks[netDict['net_id']]
tierno7edb6752016-03-21 17:37:52 +01002273 #skip bridge ifaces not connected to any net
2274 #if 'net_id' not in netDict or netDict['net_id']==None:
2275 # continue
2276 myVMDict['networks'].append(netDict)
tiernoae4a8d12016-07-08 12:30:39 +02002277 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2278 #print myVMDict['name']
2279 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2280 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2281 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
tierno36c0b172017-01-12 18:32:28 +01002282 if vm.get("boot_data"):
2283 cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config)
2284 else:
2285 cloud_config_vm = cloud_config
tiernob3d36742017-03-03 23:51:05 +01002286 task = new_task("new-vm", (myVMDict['name'], myVMDict['description'], myVMDict.get('start', None),
2287 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'],
2288 cloud_config_vm, myVMDict['disks']), depends=task_depends)
tierno867ffe92017-03-27 12:50:34 +02002289 instance_tasks[task["id"]] = task
2290 tasks_to_launch[myvim_thread_id].append(task)
2291 vm_id = task["id"]
tierno7edb6752016-03-21 17:37:52 +01002292 vm['vim_id'] = vm_id
2293 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2294 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2295 for net in myVMDict['networks']:
2296 if "vim_id" in net:
2297 for iface in vm['interfaces']:
2298 if net["name"]==iface["internal_name"]:
2299 iface["vim_id"]=net["vim_id"]
2300 break
tierno867ffe92017-03-27 12:50:34 +02002301 scenarioDict["datacenter2tenant"] = myvim_threads_id
tiernoa2793912016-10-04 08:15:08 +00002302 logger.debug("create_instance Deployment done scenarioDict: %s",
2303 yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) )
tiernof97fd272016-07-11 14:32:37 +02002304 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_name, instance_description, scenarioDict)
tierno867ffe92017-03-27 12:50:34 +02002305 for myvim_thread_id,task_list in tasks_to_launch.items():
2306 for task in task_list:
2307 vim_threads["running"][myvim_thread_id].insert_task(task)
2308
2309 global_instance_tasks[instance_id] = instance_tasks
2310 # Update database with those ended instance_tasks
2311 # for task in instance_tasks.values():
2312 # if task["status"] == "ok":
2313 # if task["name"] == "new-vm":
2314 # mydb.update_rows("instance_vms", UPDATE={"vim_vm_id": task["result"]},
2315 # WHERE={"vim_vm_id": task["id"]})
2316 # elif task["name"] == "new-net":
2317 # mydb.update_rows("instance_nets", UPDATE={"vim_net_id": task["result"]},
2318 # WHERE={"vim_net_id": task["id"]})
tiernof97fd272016-07-11 14:32:37 +02002319 return mydb.get_instance_scenario(instance_id)
2320 except (NfvoException, vimconn.vimconnException,db_base_Exception) as e:
tiernobe41e222016-09-02 15:16:13 +02002321 message = rollback(mydb, myvims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02002322 if isinstance(e, db_base_Exception):
2323 error_text = "database Exception"
2324 elif isinstance(e, vimconn.vimconnException):
2325 error_text = "VIM Exception"
2326 else:
2327 error_text = "Exception"
2328 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2329 #logger.error("create_instance: %s", error_text)
2330 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01002331
tiernob3d36742017-03-03 23:51:05 +01002332
tierno7edb6752016-03-21 17:37:52 +01002333def delete_instance(mydb, tenant_id, instance_id):
tiernoae4a8d12016-07-08 12:30:39 +02002334 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02002335 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +02002336 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01002337 tenant_id = instanceDict["tenant_id"]
tiernoae4a8d12016-07-08 12:30:39 +02002338 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno7edb6752016-03-21 17:37:52 +01002339
tiernoa2793912016-10-04 08:15:08 +00002340 #1. Delete from Database
tiernof97fd272016-07-11 14:32:37 +02002341 message = mydb.delete_instance_scenario(instance_id, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002342
2343 #2. delete from VIM
tiernoa2793912016-10-04 08:15:08 +00002344 error_msg = ""
tiernob3d36742017-03-03 23:51:05 +01002345 myvims = {}
2346 myvim_threads = {}
tierno7edb6752016-03-21 17:37:52 +01002347
2348 #2.1 deleting VMs
2349 #vm_fail_list=[]
2350 for sce_vnf in instanceDict['vnfs']:
tiernoa2793912016-10-04 08:15:08 +00002351 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2352 if datacenter_key not in myvims:
tiernob3d36742017-03-03 23:51:05 +01002353 try:
tierno867ffe92017-03-27 12:50:34 +02002354 _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
tiernob3d36742017-03-03 23:51:05 +01002355 except NfvoException as e:
2356 logger.error(str(e))
2357 myvim_thread = None
2358 myvim_threads[datacenter_key] = myvim_thread
tiernoa2793912016-10-04 08:15:08 +00002359 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
2360 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
2361 if len(vims) == 0:
2362 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
2363 sce_vnf["datacenter_tenant_id"]))
2364 myvims[datacenter_key] = None
2365 else:
2366 myvims[datacenter_key] = vims.values()[0]
2367 myvim = myvims[datacenter_key]
tiernob3d36742017-03-03 23:51:05 +01002368 myvim_thread = myvim_threads[datacenter_key]
tierno7edb6752016-03-21 17:37:52 +01002369 for vm in sce_vnf['vms']:
tiernoa2793912016-10-04 08:15:08 +00002370 if not myvim:
2371 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
2372 continue
tiernoae4a8d12016-07-08 12:30:39 +02002373 try:
tiernob3d36742017-03-03 23:51:05 +01002374 task=None
2375 if is_task_id(vm['vim_vm_id']):
2376 task_id = vm['vim_vm_id']
tierno867ffe92017-03-27 12:50:34 +02002377 old_task = global_instance_tasks[instance_id].get(task_id)
tiernob3d36742017-03-03 23:51:05 +01002378 if not old_task:
2379 error_msg += "\n VM was scheduled for create, but task {} is not found".format(task_id)
2380 continue
2381 with task_lock:
2382 if old_task["status"] == "enqueued":
2383 old_task["status"] = "deleted"
2384 elif old_task["status"] == "error":
2385 continue
2386 elif old_task["status"] == "processing":
tierno867ffe92017-03-27 12:50:34 +02002387 task = new_task("del-vm", (task_id, vm["interfaces"]), depends={task_id: old_task})
tiernob3d36742017-03-03 23:51:05 +01002388 else: #ok
tierno867ffe92017-03-27 12:50:34 +02002389 task = new_task("del-vm", (old_task["result"], vm["interfaces"]))
tiernob3d36742017-03-03 23:51:05 +01002390 else:
tierno867ffe92017-03-27 12:50:34 +02002391 task = new_task("del-vm", (vm['vim_vm_id'], vm["interfaces"]) )
tiernob3d36742017-03-03 23:51:05 +01002392 if task:
2393 myvim_thread.insert_task(task)
tiernoae4a8d12016-07-08 12:30:39 +02002394 except vimconn.vimconnNotFoundException as e:
tiernoa2793912016-10-04 08:15:08 +00002395 error_msg+="\n VM VIM_id={} not found at datacenter={}".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
tiernoae4a8d12016-07-08 12:30:39 +02002396 logger.warn("VM instance '%s'uuid '%s', VIM id '%s', from VNF_id '%s' not found",
2397 vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'])
2398 except vimconn.vimconnException as e:
tiernoa2793912016-10-04 08:15:08 +00002399 error_msg+="\n VM VIM_id={} at datacenter={} Error: {} {}".format(vm['vim_vm_id'], sce_vnf["datacenter_id"], e.http_code, str(e))
2400 logger.error("Error %d deleting VM instance '%s'uuid '%s', VIM_id '%s', from VNF_id '%s': %s",
tiernoae4a8d12016-07-08 12:30:39 +02002401 e.http_code, vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'], str(e))
tierno42026a02017-02-10 15:13:40 +01002402
tierno7edb6752016-03-21 17:37:52 +01002403 #2.2 deleting NETS
2404 #net_fail_list=[]
2405 for net in instanceDict['nets']:
tierno66345bc2016-09-26 11:37:55 +02002406 if not net['created']:
tierno7edb6752016-03-21 17:37:52 +01002407 continue #skip not created nets
tiernoa2793912016-10-04 08:15:08 +00002408 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
2409 if datacenter_key not in myvims:
tiernob3d36742017-03-03 23:51:05 +01002410 try:
tierno867ffe92017-03-27 12:50:34 +02002411 _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
tiernob3d36742017-03-03 23:51:05 +01002412 except NfvoException as e:
2413 logger.error(str(e))
2414 myvim_thread = None
2415 myvim_threads[datacenter_key] = myvim_thread
tiernoa2793912016-10-04 08:15:08 +00002416 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
2417 datacenter_tenant_id=net["datacenter_tenant_id"])
2418 if len(vims) == 0:
2419 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
2420 myvims[datacenter_key] = None
2421 else:
2422 myvims[datacenter_key] = vims.values()[0]
2423 myvim = myvims[datacenter_key]
tiernob3d36742017-03-03 23:51:05 +01002424 myvim_thread = myvim_threads[datacenter_key]
tiernoa2793912016-10-04 08:15:08 +00002425
tierno7edb6752016-03-21 17:37:52 +01002426 if not myvim:
tiernoa2793912016-10-04 08:15:08 +00002427 error_msg += "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net['vim_net_id'], net["datacenter_id"])
tierno7edb6752016-03-21 17:37:52 +01002428 continue
tiernoae4a8d12016-07-08 12:30:39 +02002429 try:
tiernob3d36742017-03-03 23:51:05 +01002430 task = None
2431 if is_task_id(net['vim_net_id']):
2432 task_id = net['vim_net_id']
tierno867ffe92017-03-27 12:50:34 +02002433 old_task = global_instance_tasks[instance_id].get(task_id)
tiernob3d36742017-03-03 23:51:05 +01002434 if not old_task:
2435 error_msg += "\n NET was scheduled for create, but task {} is not found".format(task_id)
2436 continue
2437 with task_lock:
2438 if old_task["status"] == "enqueued":
2439 old_task["status"] = "deleted"
2440 elif old_task["status"] == "error":
2441 continue
2442 elif old_task["status"] == "processing":
2443 task = new_task("del-net", task_id, depends={task_id: old_task})
2444 else: # ok
2445 task = new_task("del-net", old_task["result"])
2446 else:
tierno867ffe92017-03-27 12:50:34 +02002447 task = new_task("del-net", (net['vim_net_id'], net['sdn_net_id']))
tiernob3d36742017-03-03 23:51:05 +01002448 if task:
2449 myvim_thread.insert_task(task)
tiernoae4a8d12016-07-08 12:30:39 +02002450 except vimconn.vimconnNotFoundException as e:
tiernob3d36742017-03-03 23:51:05 +01002451 error_msg += "\n NET VIM_id={} not found at datacenter={}".format(net['vim_net_id'], net["datacenter_id"])
tiernoa2793912016-10-04 08:15:08 +00002452 logger.warn("NET '%s', VIM_id '%s', from VNF_net_id '%s' not found",
tiernob3d36742017-03-03 23:51:05 +01002453 net['uuid'], net['vim_net_id'], str(net['vnf_net_id']))
tiernoae4a8d12016-07-08 12:30:39 +02002454 except vimconn.vimconnException as e:
tiernob3d36742017-03-03 23:51:05 +01002455 error_msg += "\n NET VIM_id={} at datacenter={} Error: {} {}".format(net['vim_net_id'],
2456 net["datacenter_id"],
2457 e.http_code, str(e))
tiernoa2793912016-10-04 08:15:08 +00002458 logger.error("Error %d deleting NET '%s', VIM_id '%s', from VNF_net_id '%s': %s",
tiernob3d36742017-03-03 23:51:05 +01002459 e.http_code, net['uuid'], net['vim_net_id'], str(net['vnf_net_id']), str(e))
2460 if len(error_msg) > 0:
tiernof97fd272016-07-11 14:32:37 +02002461 return 'instance ' + message + ' deleted but some elements could not be deleted, or already deleted (error: 404) from VIM: ' + error_msg
tierno7edb6752016-03-21 17:37:52 +01002462 else:
tiernof97fd272016-07-11 14:32:37 +02002463 return 'instance ' + message + ' deleted'
tierno7edb6752016-03-21 17:37:52 +01002464
tiernob3d36742017-03-03 23:51:05 +01002465
tierno7edb6752016-03-21 17:37:52 +01002466def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
2467 '''Refreshes a scenario instance. It modifies instanceDict'''
2468 '''Returns:
2469 - result: <0 if there is any unexpected error, n>=0 if no errors where n is the number of vms and nets that couldn't be updated in the database
2470 - error_msg
2471 '''
tierno867ffe92017-03-27 12:50:34 +02002472 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
2473 # #print "nfvo.refresh_instance begins"
2474 # #print json.dumps(instanceDict, indent=4)
2475 #
2476 # #print "Getting the VIM URL and the VIM tenant_id"
2477 # myvims={}
2478 #
2479 # # 1. Getting VIM vm and net list
2480 # vms_updated = [] #List of VM instance uuids in openmano that were updated
2481 # vms_notupdated=[]
2482 # vm_list = {}
2483 # for sce_vnf in instanceDict['vnfs']:
2484 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2485 # if datacenter_key not in vm_list:
2486 # vm_list[datacenter_key] = []
2487 # if datacenter_key not in myvims:
2488 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
2489 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
2490 # if len(vims) == 0:
2491 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
2492 # myvims[datacenter_key] = None
2493 # else:
2494 # myvims[datacenter_key] = vims.values()[0]
2495 # for vm in sce_vnf['vms']:
2496 # vm_list[datacenter_key].append(vm['vim_vm_id'])
2497 # vms_notupdated.append(vm["uuid"])
2498 #
2499 # nets_updated = [] #List of VM instance uuids in openmano that were updated
2500 # nets_notupdated=[]
2501 # net_list = {}
2502 # for net in instanceDict['nets']:
2503 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
2504 # if datacenter_key not in net_list:
2505 # net_list[datacenter_key] = []
2506 # if datacenter_key not in myvims:
2507 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
2508 # datacenter_tenant_id=net["datacenter_tenant_id"])
2509 # if len(vims) == 0:
2510 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
2511 # myvims[datacenter_key] = None
2512 # else:
2513 # myvims[datacenter_key] = vims.values()[0]
2514 #
2515 # net_list[datacenter_key].append(net['vim_net_id'])
2516 # nets_notupdated.append(net["uuid"])
2517 #
2518 # # 1. Getting the status of all VMs
2519 # vm_dict={}
2520 # for datacenter_key in myvims:
2521 # if not vm_list.get(datacenter_key):
2522 # continue
2523 # failed = True
2524 # failed_message=""
2525 # if not myvims[datacenter_key]:
2526 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
2527 # else:
2528 # try:
2529 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
2530 # failed = False
2531 # except vimconn.vimconnException as e:
2532 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
2533 # failed_message = str(e)
2534 # if failed:
2535 # for vm in vm_list[datacenter_key]:
2536 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
2537 #
2538 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
2539 # for sce_vnf in instanceDict['vnfs']:
2540 # for vm in sce_vnf['vms']:
2541 # vm_id = vm['vim_vm_id']
2542 # interfaces = vm_dict[vm_id].pop('interfaces', [])
2543 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
2544 # has_mgmt_iface = False
2545 # for iface in vm["interfaces"]:
2546 # if iface["type"]=="mgmt":
2547 # has_mgmt_iface = True
2548 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
2549 # vm_dict[vm_id]['status'] = "ACTIVE"
2550 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
2551 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
2552 # if vm['status'] != vm_dict[vm_id]['status'] or vm.get('error_msg')!=vm_dict[vm_id].get('error_msg') or vm.get('vim_info')!=vm_dict[vm_id].get('vim_info'):
2553 # vm['status'] = vm_dict[vm_id]['status']
2554 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
2555 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
2556 # # 2.1. Update in openmano DB the VMs whose status changed
2557 # try:
2558 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
2559 # vms_notupdated.remove(vm["uuid"])
2560 # if updates>0:
2561 # vms_updated.append(vm["uuid"])
2562 # except db_base_Exception as e:
2563 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
2564 # # 2.2. Update in openmano DB the interface VMs
2565 # for interface in interfaces:
2566 # #translate from vim_net_id to instance_net_id
2567 # network_id_list=[]
2568 # for net in instanceDict['nets']:
2569 # if net["vim_net_id"] == interface["vim_net_id"]:
2570 # network_id_list.append(net["uuid"])
2571 # if not network_id_list:
2572 # continue
2573 # del interface["vim_net_id"]
2574 # try:
2575 # for network_id in network_id_list:
2576 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
2577 # except db_base_Exception as e:
2578 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
2579 #
2580 # # 3. Getting the status of all nets
2581 # net_dict = {}
2582 # for datacenter_key in myvims:
2583 # if not net_list.get(datacenter_key):
2584 # continue
2585 # failed = True
2586 # failed_message = ""
2587 # if not myvims[datacenter_key]:
2588 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
2589 # else:
2590 # try:
2591 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
2592 # failed = False
2593 # except vimconn.vimconnException as e:
2594 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
2595 # failed_message = str(e)
2596 # if failed:
2597 # for net in net_list[datacenter_key]:
2598 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
2599 #
2600 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
2601 # # TODO: update nets inside a vnf
2602 # for net in instanceDict['nets']:
2603 # net_id = net['vim_net_id']
2604 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
2605 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
2606 # if net['status'] != net_dict[net_id]['status'] or net.get('error_msg')!=net_dict[net_id].get('error_msg') or net.get('vim_info')!=net_dict[net_id].get('vim_info'):
2607 # net['status'] = net_dict[net_id]['status']
2608 # net['error_msg'] = net_dict[net_id].get('error_msg')
2609 # net['vim_info'] = net_dict[net_id].get('vim_info')
2610 # # 5.1. Update in openmano DB the nets whose status changed
2611 # try:
2612 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
2613 # nets_notupdated.remove(net["uuid"])
2614 # if updated>0:
2615 # nets_updated.append(net["uuid"])
2616 # except db_base_Exception as e:
2617 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
2618 #
2619 # # Returns appropriate output
2620 # #print "nfvo.refresh_instance finishes"
2621 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
2622 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
tierno7edb6752016-03-21 17:37:52 +01002623 instance_id = instanceDict['uuid']
tierno867ffe92017-03-27 12:50:34 +02002624 # if len(vms_notupdated)+len(nets_notupdated)>0:
2625 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
2626 # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
tierno42026a02017-02-10 15:13:40 +01002627
tiernoae4a8d12016-07-08 12:30:39 +02002628 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
tierno7edb6752016-03-21 17:37:52 +01002629
tiernob3d36742017-03-03 23:51:05 +01002630
tierno7edb6752016-03-21 17:37:52 +01002631def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
tiernoae4a8d12016-07-08 12:30:39 +02002632 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02002633 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01002634 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
2635
tiernoae4a8d12016-07-08 12:30:39 +02002636 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernof97fd272016-07-11 14:32:37 +02002637 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
2638 if len(vims) == 0:
2639 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002640 myvim = vims.values()[0]
tierno42026a02017-02-10 15:13:40 +01002641
tierno7edb6752016-03-21 17:37:52 +01002642
2643 input_vnfs = action_dict.pop("vnfs", [])
2644 input_vms = action_dict.pop("vms", [])
2645 action_over_all = True if len(input_vnfs)==0 and len (input_vms)==0 else False
2646 vm_result = {}
2647 vm_error = 0
2648 vm_ok = 0
2649 for sce_vnf in instanceDict['vnfs']:
2650 for vm in sce_vnf['vms']:
2651 if not action_over_all:
2652 if sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
2653 vm['uuid'] not in input_vms and vm['name'] not in input_vms:
2654 continue
tiernoae4a8d12016-07-08 12:30:39 +02002655 try:
2656 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
tierno7edb6752016-03-21 17:37:52 +01002657 if "console" in action_dict:
tierno20fc2a22016-08-19 17:02:35 +02002658 if not global_config["http_console_proxy"]:
2659 vm_result[ vm['uuid'] ] = {"vim_result": 200,
2660 "description": "{protocol}//{ip}:{port}/{suffix}".format(
2661 protocol=data["protocol"],
2662 ip = data["server"],
2663 port = data["port"],
2664 suffix = data["suffix"]),
2665 "name":vm['name']
2666 }
2667 vm_ok +=1
2668 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
tierno7edb6752016-03-21 17:37:52 +01002669 vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized,
2670 "description": "this console is only reachable by local interface",
2671 "name":vm['name']
2672 }
2673 vm_error+=1
tierno20fc2a22016-08-19 17:02:35 +02002674 else:
tierno7edb6752016-03-21 17:37:52 +01002675 #print "console data", data
tierno42026a02017-02-10 15:13:40 +01002676 try:
tierno20fc2a22016-08-19 17:02:35 +02002677 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
2678 vm_result[ vm['uuid'] ] = {"vim_result": 200,
2679 "description": "{protocol}//{ip}:{port}/{suffix}".format(
2680 protocol=data["protocol"],
2681 ip = global_config["http_console_host"],
2682 port = console_thread.port,
2683 suffix = data["suffix"]),
2684 "name":vm['name']
2685 }
2686 vm_ok +=1
2687 except NfvoException as e:
2688 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
2689 vm_error+=1
2690
tierno7edb6752016-03-21 17:37:52 +01002691 else:
tiernof97fd272016-07-11 14:32:37 +02002692 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
tierno7edb6752016-03-21 17:37:52 +01002693 vm_ok +=1
tiernoae4a8d12016-07-08 12:30:39 +02002694 except vimconn.vimconnException as e:
2695 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
2696 vm_error+=1
tierno7edb6752016-03-21 17:37:52 +01002697
2698 if vm_ok==0: #all goes wrong
tierno351863c2016-07-23 01:46:03 +02002699 return vm_result
tierno7edb6752016-03-21 17:37:52 +01002700 else:
tierno351863c2016-07-23 01:46:03 +02002701 return vm_result
tierno42026a02017-02-10 15:13:40 +01002702
tiernob3d36742017-03-03 23:51:05 +01002703
tierno7edb6752016-03-21 17:37:52 +01002704def create_or_use_console_proxy_thread(console_server, console_port):
2705 #look for a non-used port
2706 console_thread_key = console_server + ":" + str(console_port)
2707 if console_thread_key in global_config["console_thread"]:
2708 #global_config["console_thread"][console_thread_key].start_timeout()
tiernof97fd272016-07-11 14:32:37 +02002709 return global_config["console_thread"][console_thread_key]
tierno42026a02017-02-10 15:13:40 +01002710
tierno7edb6752016-03-21 17:37:52 +01002711 for port in global_config["console_port_iterator"]():
tierno20fc2a22016-08-19 17:02:35 +02002712 #print "create_or_use_console_proxy_thread() port:", port
tierno7edb6752016-03-21 17:37:52 +01002713 if port in global_config["console_ports"]:
2714 continue
2715 try:
2716 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
2717 clithread.start()
2718 global_config["console_thread"][console_thread_key] = clithread
2719 global_config["console_ports"][port] = console_thread_key
tiernof97fd272016-07-11 14:32:37 +02002720 return clithread
tierno7edb6752016-03-21 17:37:52 +01002721 except cli.ConsoleProxyExceptionPortUsed as e:
2722 #port used, try with onoher
2723 continue
2724 except cli.ConsoleProxyException as e:
tiernof97fd272016-07-11 14:32:37 +02002725 raise NfvoException(str(e), HTTP_Bad_Request)
2726 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002727
tiernob3d36742017-03-03 23:51:05 +01002728
tierno7edb6752016-03-21 17:37:52 +01002729def check_tenant(mydb, tenant_id):
2730 '''check that tenant exists at database'''
tiernof97fd272016-07-11 14:32:37 +02002731 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
2732 if not tenant:
2733 raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found)
2734 return
tierno7edb6752016-03-21 17:37:52 +01002735
tiernob3d36742017-03-03 23:51:05 +01002736
tierno7edb6752016-03-21 17:37:52 +01002737def new_tenant(mydb, tenant_dict):
tiernof97fd272016-07-11 14:32:37 +02002738 tenant_id = mydb.new_row("nfvo_tenants", tenant_dict, add_uuid=True)
2739 return tenant_id
tierno7edb6752016-03-21 17:37:52 +01002740
tiernob3d36742017-03-03 23:51:05 +01002741
tierno7edb6752016-03-21 17:37:52 +01002742def delete_tenant(mydb, tenant):
2743 #get nfvo_tenant info
tierno42026a02017-02-10 15:13:40 +01002744
tiernof97fd272016-07-11 14:32:37 +02002745 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
2746 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
2747 return tenant_dict['uuid'] + " " + tenant_dict["name"]
tierno7edb6752016-03-21 17:37:52 +01002748
tiernob3d36742017-03-03 23:51:05 +01002749
tierno7edb6752016-03-21 17:37:52 +01002750def new_datacenter(mydb, datacenter_descriptor):
2751 if "config" in datacenter_descriptor:
2752 datacenter_descriptor["config"]=yaml.safe_dump(datacenter_descriptor["config"],default_flow_style=True,width=256)
tierno3ae39742016-09-07 12:17:51 +02002753 #Check that datacenter-type is correct
2754 datacenter_type = datacenter_descriptor.get("type", "openvim");
2755 module_info = None
2756 try:
2757 module = "vimconn_" + datacenter_type
tierno361275f2017-04-25 16:24:34 +02002758 pkg = __import__("osm_ro." + module)
2759 vim_conn = getattr(pkg, module)
2760 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
tierno3ae39742016-09-07 12:17:51 +02002761 except (IOError, ImportError):
tierno361275f2017-04-25 16:24:34 +02002762 # if module_info and module_info[0]:
2763 # file.close(module_info[0])
tierno3ae39742016-09-07 12:17:51 +02002764 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}'.py not installed".format(datacenter_type, module), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +01002765
tiernof97fd272016-07-11 14:32:37 +02002766 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True)
2767 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002768
tiernob3d36742017-03-03 23:51:05 +01002769
tierno7edb6752016-03-21 17:37:52 +01002770def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
2771 #obtain data, check that only one exist
tiernof97fd272016-07-11 14:32:37 +02002772 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
tierno42026a02017-02-10 15:13:40 +01002773 #edit data
tiernof97fd272016-07-11 14:32:37 +02002774 datacenter_id = datacenter['uuid']
2775 where={'uuid': datacenter['uuid']}
tierno7edb6752016-03-21 17:37:52 +01002776 if "config" in datacenter_descriptor:
2777 if datacenter_descriptor['config']!=None:
2778 try:
2779 new_config_dict = datacenter_descriptor["config"]
2780 #delete null fields
2781 to_delete=[]
2782 for k in new_config_dict:
2783 if new_config_dict[k]==None:
2784 to_delete.append(k)
tierno42026a02017-02-10 15:13:40 +01002785
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01002786 config_text = datacenter.get("config")
2787 if not config_text:
2788 config_text = '{}'
2789 config_dict = yaml.load(config_text)
tierno7edb6752016-03-21 17:37:52 +01002790 config_dict.update(new_config_dict)
2791 #delete null fields
2792 for k in to_delete:
2793 del config_dict[k]
tiernof97fd272016-07-11 14:32:37 +02002794 except Exception as e:
2795 raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002796 datacenter_descriptor["config"]= yaml.safe_dump(config_dict,default_flow_style=True,width=256) if len(config_dict)>0 else None
tiernof97fd272016-07-11 14:32:37 +02002797 mydb.update_rows('datacenters', datacenter_descriptor, where)
2798 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002799
tiernob3d36742017-03-03 23:51:05 +01002800
tierno7edb6752016-03-21 17:37:52 +01002801def delete_datacenter(mydb, datacenter):
2802 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02002803 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
2804 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
2805 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
tierno7edb6752016-03-21 17:37:52 +01002806
tiernob3d36742017-03-03 23:51:05 +01002807
tierno8008c3a2016-10-13 15:34:28 +00002808def associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter, vim_tenant_id=None, vim_tenant_name=None, vim_username=None, vim_password=None, config=None):
tierno7edb6752016-03-21 17:37:52 +01002809 #get datacenter info
Vance Shipleyc24b4e22017-05-12 02:34:53 +05302810 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username, vim_passwd=vim_password)
tierno42026a02017-02-10 15:13:40 +01002811 datacenter_name = myvim["name"]
tierno7edb6752016-03-21 17:37:52 +01002812
tierno42026a02017-02-10 15:13:40 +01002813 create_vim_tenant = True if not vim_tenant_id and not vim_tenant_name else False
2814
2815 # get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02002816 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01002817 if vim_tenant_name==None:
2818 vim_tenant_name=tenant_dict['name']
tierno42026a02017-02-10 15:13:40 +01002819
tierno7edb6752016-03-21 17:37:52 +01002820 #check that this association does not exist before
2821 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
tiernof97fd272016-07-11 14:32:37 +02002822 tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
2823 if len(tenants_datacenters)>0:
2824 raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002825
2826 vim_tenant_id_exist_atdb=False
2827 if not create_vim_tenant:
2828 where_={"datacenter_id": datacenter_id}
2829 if vim_tenant_id!=None:
2830 where_["vim_tenant_id"] = vim_tenant_id
2831 if vim_tenant_name!=None:
2832 where_["vim_tenant_name"] = vim_tenant_name
2833 #check if vim_tenant_id is already at database
tiernof97fd272016-07-11 14:32:37 +02002834 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
2835 if len(datacenter_tenants_dict)>=1:
tierno7edb6752016-03-21 17:37:52 +01002836 datacenter_tenants_dict = datacenter_tenants_dict[0]
2837 vim_tenant_id_exist_atdb=True
2838 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
2839 else: #result=0
2840 datacenter_tenants_dict = {}
2841 #insert at table datacenter_tenants
2842 else: #if vim_tenant_id==None:
2843 #create tenant at VIM if not provided
tiernoae4a8d12016-07-08 12:30:39 +02002844 try:
2845 vim_tenant_id = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
2846 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002847 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id, str(e)), HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01002848 datacenter_tenants_dict = {}
2849 datacenter_tenants_dict["created"]="true"
tierno42026a02017-02-10 15:13:40 +01002850
tierno7edb6752016-03-21 17:37:52 +01002851 #fill datacenter_tenants table
2852 if not vim_tenant_id_exist_atdb:
tierno42026a02017-02-10 15:13:40 +01002853 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant_id
tierno7edb6752016-03-21 17:37:52 +01002854 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
tierno42026a02017-02-10 15:13:40 +01002855 datacenter_tenants_dict["user"] = vim_username
2856 datacenter_tenants_dict["passwd"] = vim_password
2857 datacenter_tenants_dict["datacenter_id"] = datacenter_id
tierno8008c3a2016-10-13 15:34:28 +00002858 if config:
2859 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
tiernof97fd272016-07-11 14:32:37 +02002860 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01002861 datacenter_tenants_dict["uuid"] = id_
tierno42026a02017-02-10 15:13:40 +01002862
tierno7edb6752016-03-21 17:37:52 +01002863 #fill tenants_datacenters table
tierno99314902017-04-26 13:23:09 +02002864 datacenter_tenant_id = datacenter_tenants_dict["uuid"]
2865 tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id
tiernof97fd272016-07-11 14:32:37 +02002866 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
tierno42026a02017-02-10 15:13:40 +01002867 # create thread
2868 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_dict['uuid'], datacenter_id) # reload data
2869 thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid'])
tierno99314902017-04-26 13:23:09 +02002870 new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, datacenter_name, datacenter_tenant_id,
2871 db=db, db_lock=db_lock, ovim=ovim)
tierno42026a02017-02-10 15:13:40 +01002872 new_thread.start()
tierno867ffe92017-03-27 12:50:34 +02002873 thread_id = datacenter_tenants_dict["uuid"]
tiernob3d36742017-03-03 23:51:05 +01002874 vim_threads["running"][thread_id] = new_thread
tiernof97fd272016-07-11 14:32:37 +02002875 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002876
tierno99314902017-04-26 13:23:09 +02002877
2878def edit_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=None, vim_tenant_name=None,
2879 vim_username=None, vim_password=None, config=None):
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01002880 #Obtain the data of this datacenter_tenant_id
2881 vim_data = mydb.get_rows(
2882 SELECT=("datacenter_tenants.vim_tenant_name", "datacenter_tenants.vim_tenant_id", "datacenter_tenants.user",
2883 "datacenter_tenants.passwd", "datacenter_tenants.config"),
2884 FROM="datacenter_tenants JOIN tenants_datacenters ON datacenter_tenants.uuid=tenants_datacenters.datacenter_tenant_id",
2885 WHERE={"tenants_datacenters.nfvo_tenant_id": nfvo_tenant,
2886 "tenants_datacenters.datacenter_id": datacenter_id})
2887
2888 logger.debug(str(vim_data))
2889 if len(vim_data) < 1:
2890 raise NfvoException("Datacenter {} is not attached for tenant {}".format(datacenter_id, nfvo_tenant), HTTP_Conflict)
2891
2892 v = vim_data[0]
2893 if v['config']:
2894 v['config'] = yaml.load(v['config'])
2895
2896 if vim_tenant_id:
2897 v['vim_tenant_id'] = vim_tenant_id
2898 if vim_tenant_name:
2899 v['vim_tenant_name'] = vim_tenant_name
2900 if vim_username:
2901 v['user'] = vim_username
2902 if vim_password:
2903 v['passwd'] = vim_password
2904 if config:
2905 if not v['config']:
2906 v['config'] = {}
2907 v['config'].update(config)
2908
2909 logger.debug(str(v))
2910 deassociate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'])
2911 associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'], vim_tenant_name=v['vim_tenant_name'],
2912 vim_username=v['user'], vim_password=v['passwd'], config=v['config'])
2913
2914 return datacenter_id
tiernob3d36742017-03-03 23:51:05 +01002915
tierno7edb6752016-03-21 17:37:52 +01002916def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=None):
2917 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002918 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002919
2920 #get nfvo_tenant info
2921 if not tenant_id or tenant_id=="any":
2922 tenant_uuid = None
2923 else:
tiernof97fd272016-07-11 14:32:37 +02002924 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002925 tenant_uuid = tenant_dict['uuid']
2926
2927 #check that this association exist before
2928 tenants_datacenter_dict={"datacenter_id":datacenter_id }
2929 if tenant_uuid:
2930 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
tiernof97fd272016-07-11 14:32:37 +02002931 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
2932 if len(tenant_datacenter_list)==0 and tenant_uuid:
2933 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002934
2935 #delete this association
tiernof97fd272016-07-11 14:32:37 +02002936 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
tierno7edb6752016-03-21 17:37:52 +01002937
2938 #get vim_tenant info and deletes
2939 warning=''
2940 for tenant_datacenter_item in tenant_datacenter_list:
tiernof97fd272016-07-11 14:32:37 +02002941 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
2942 #try to delete vim:tenant
2943 try:
2944 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
2945 if vim_tenant_dict['created']=='true':
tierno7edb6752016-03-21 17:37:52 +01002946 #delete tenant at VIM if created by NFVO
tierno42026a02017-02-10 15:13:40 +01002947 try:
tiernoae4a8d12016-07-08 12:30:39 +02002948 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
2949 except vimconn.vimconnException as e:
2950 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
2951 logger.warn(warning)
tiernof97fd272016-07-11 14:32:37 +02002952 except db_base_Exception as e:
2953 logger.error("Cannot delete datacenter_tenants " + str(e))
tierno42026a02017-02-10 15:13:40 +01002954 pass # the error will be caused because dependencies, vim_tenant can not be deleted
tierno867ffe92017-03-27 12:50:34 +02002955 thread_id = tenant_datacenter_item["datacenter_tenant_id"]
tierno42026a02017-02-10 15:13:40 +01002956 thread = vim_threads["running"][thread_id]
tierno867ffe92017-03-27 12:50:34 +02002957 thread.insert_task(new_task("exit", None))
tierno42026a02017-02-10 15:13:40 +01002958 vim_threads["deleting"][thread_id] = thread
tiernof97fd272016-07-11 14:32:37 +02002959 return "datacenter {} detached. {}".format(datacenter_id, warning)
tierno7edb6752016-03-21 17:37:52 +01002960
tiernob3d36742017-03-03 23:51:05 +01002961
tierno7edb6752016-03-21 17:37:52 +01002962def datacenter_action(mydb, tenant_id, datacenter, action_dict):
2963 #DEPRECATED
tierno42026a02017-02-10 15:13:40 +01002964 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002965 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002966
2967 if 'net-update' in action_dict:
tiernoae4a8d12016-07-08 12:30:39 +02002968 try:
tiernof97fd272016-07-11 14:32:37 +02002969 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
tiernoae4a8d12016-07-08 12:30:39 +02002970 #print content
2971 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002972 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
2973 raise NfvoException(str(e), HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01002974 #update nets Change from VIM format to NFVO format
2975 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02002976 for net in nets:
tierno7edb6752016-03-21 17:37:52 +01002977 net_nfvo={'datacenter_id': datacenter_id}
2978 net_nfvo['name'] = net['name']
2979 #net_nfvo['description']= net['name']
2980 net_nfvo['vim_net_id'] = net['id']
2981 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
2982 net_nfvo['shared'] = net['shared']
2983 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
2984 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02002985 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
2986 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
2987 return inserted
tierno7edb6752016-03-21 17:37:52 +01002988 elif 'net-edit' in action_dict:
2989 net = action_dict['net-edit'].pop('net')
tierno42fcc3b2016-07-06 17:20:40 +02002990 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01002991 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
tierno7edb6752016-03-21 17:37:52 +01002992 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02002993 return result
tierno7edb6752016-03-21 17:37:52 +01002994 elif 'net-delete' in action_dict:
2995 net = action_dict['net-deelte'].get('net')
tierno42fcc3b2016-07-06 17:20:40 +02002996 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01002997 result = mydb.delete_row(FROM='datacenter_nets',
tierno7edb6752016-03-21 17:37:52 +01002998 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02002999 return result
tierno7edb6752016-03-21 17:37:52 +01003000
3001 else:
tiernof97fd272016-07-11 14:32:37 +02003002 raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01003003
tiernob3d36742017-03-03 23:51:05 +01003004
tierno7edb6752016-03-21 17:37:52 +01003005def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
3006 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00003007 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01003008
tierno42fcc3b2016-07-06 17:20:40 +02003009 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
tierno42026a02017-02-10 15:13:40 +01003010 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
tierno7edb6752016-03-21 17:37:52 +01003011 WHERE={'datacenter_id':datacenter_id, what: netmap})
tiernof97fd272016-07-11 14:32:37 +02003012 return result
tierno7edb6752016-03-21 17:37:52 +01003013
tiernob3d36742017-03-03 23:51:05 +01003014
tierno7edb6752016-03-21 17:37:52 +01003015def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
3016 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00003017 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01003018 filter_dict={}
3019 if action_dict:
3020 action_dict = action_dict["netmap"]
3021 if 'vim_id' in action_dict:
3022 filter_dict["id"] = action_dict['vim_id']
3023 if 'vim_name' in action_dict:
3024 filter_dict["name"] = action_dict['vim_name']
3025 else:
3026 filter_dict["shared"] = True
tierno42026a02017-02-10 15:13:40 +01003027
tiernoae4a8d12016-07-08 12:30:39 +02003028 try:
tiernof97fd272016-07-11 14:32:37 +02003029 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02003030 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02003031 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
3032 raise NfvoException(str(e), HTTP_Internal_Server_Error)
3033 if len(vim_nets)>1 and action_dict:
3034 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict)
3035 elif len(vim_nets)==0: # and action_dict:
3036 raise NfvoException("Not found a network at VIM with " + str(filter_dict), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01003037 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02003038 for net in vim_nets:
tierno7edb6752016-03-21 17:37:52 +01003039 net_nfvo={'datacenter_id': datacenter_id}
3040 if action_dict and "name" in action_dict:
3041 net_nfvo['name'] = action_dict['name']
3042 else:
3043 net_nfvo['name'] = net['name']
3044 #net_nfvo['description']= net['name']
3045 net_nfvo['vim_net_id'] = net['id']
3046 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
3047 net_nfvo['shared'] = net['shared']
3048 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
tiernof97fd272016-07-11 14:32:37 +02003049 try:
3050 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01003051 net_nfvo["status"] = "OK"
tiernof97fd272016-07-11 14:32:37 +02003052 net_nfvo["uuid"] = net_id
3053 except db_base_Exception as e:
3054 if action_dict:
3055 raise
3056 else:
3057 net_nfvo["status"] = "FAIL: " + str(e)
tierno42026a02017-02-10 15:13:40 +01003058 net_list.append(net_nfvo)
3059 return net_list
tierno7edb6752016-03-21 17:37:52 +01003060
tiernob3d36742017-03-03 23:51:05 +01003061
tierno7edb6752016-03-21 17:37:52 +01003062def vim_action_get(mydb, tenant_id, datacenter, item, name):
3063 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00003064 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01003065 filter_dict={}
3066 if name:
tierno42fcc3b2016-07-06 17:20:40 +02003067 if utils.check_valid_uuid(name):
tierno7edb6752016-03-21 17:37:52 +01003068 filter_dict["id"] = name
3069 else:
3070 filter_dict["name"] = name
tiernoae4a8d12016-07-08 12:30:39 +02003071 try:
3072 if item=="networks":
3073 #filter_dict['tenant_id'] = myvim['tenant_id']
3074 content = myvim.get_network_list(filter_dict=filter_dict)
3075 elif item=="tenants":
3076 content = myvim.get_tenant_list(filter_dict=filter_dict)
tierno4540ea52017-01-18 17:44:32 +01003077 elif item == "images":
3078 content = myvim.get_image_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02003079 else:
tiernof97fd272016-07-11 14:32:37 +02003080 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernobe41e222016-09-02 15:16:13 +02003081 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
tiernoae4a8d12016-07-08 12:30:39 +02003082 if name and len(content)==1:
tiernof97fd272016-07-11 14:32:37 +02003083 return {item[:-1]: content[0]}
tiernoae4a8d12016-07-08 12:30:39 +02003084 elif name and len(content)==0:
tiernof97fd272016-07-11 14:32:37 +02003085 raise NfvoException("No {} found with ".format(item[:-1]) + " and ".join(map(lambda x: str(x[0])+": "+str(x[1]), filter_dict.iteritems())),
tiernobe41e222016-09-02 15:16:13 +02003086 datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02003087 else:
tiernof97fd272016-07-11 14:32:37 +02003088 return {item: content}
tiernoae4a8d12016-07-08 12:30:39 +02003089 except vimconn.vimconnException as e:
3090 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
tiernof97fd272016-07-11 14:32:37 +02003091 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
tierno42026a02017-02-10 15:13:40 +01003092
tiernob3d36742017-03-03 23:51:05 +01003093
tierno7edb6752016-03-21 17:37:52 +01003094def vim_action_delete(mydb, tenant_id, datacenter, item, name):
3095 #get datacenter info
tierno392f2852016-05-13 12:28:55 +02003096 if tenant_id == "any":
3097 tenant_id=None
3098
tiernoa2793912016-10-04 08:15:08 +00003099 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno392f2852016-05-13 12:28:55 +02003100 #get uuid name
tiernof97fd272016-07-11 14:32:37 +02003101 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
3102 logger.debug("vim_action_delete vim response: " + str(content))
tierno392f2852016-05-13 12:28:55 +02003103 items = content.values()[0]
3104 if type(items)==list and len(items)==0:
tiernof97fd272016-07-11 14:32:37 +02003105 raise NfvoException("Not found " + item, HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02003106 elif type(items)==list and len(items)>1:
tiernof97fd272016-07-11 14:32:37 +02003107 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02003108 else: # it is a dict
3109 item_id = items["id"]
3110 item_name = str(items.get("name"))
tierno42026a02017-02-10 15:13:40 +01003111
tiernoae4a8d12016-07-08 12:30:39 +02003112 try:
3113 if item=="networks":
3114 content = myvim.delete_network(item_id)
3115 elif item=="tenants":
3116 content = myvim.delete_tenant(item_id)
tierno4540ea52017-01-18 17:44:32 +01003117 elif item == "images":
3118 content = myvim.delete_image(item_id)
tiernoae4a8d12016-07-08 12:30:39 +02003119 else:
tierno42026a02017-02-10 15:13:40 +01003120 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02003121 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02003122 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
3123 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02003124
tiernof97fd272016-07-11 14:32:37 +02003125 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
tierno42026a02017-02-10 15:13:40 +01003126
tiernob3d36742017-03-03 23:51:05 +01003127
tierno7edb6752016-03-21 17:37:52 +01003128def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
3129 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00003130 logger.debug("vim_action_create descriptor %s", str(descriptor))
tierno392f2852016-05-13 12:28:55 +02003131 if tenant_id == "any":
3132 tenant_id=None
tiernoa2793912016-10-04 08:15:08 +00003133 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02003134 try:
3135 if item=="networks":
3136 net = descriptor["network"]
3137 net_name = net.pop("name")
3138 net_type = net.pop("type", "bridge")
garciadeblas9f8456e2016-09-05 05:02:59 +02003139 net_public = net.pop("shared", False)
3140 net_ipprofile = net.pop("ip_profile", None)
tiernoa7d34d02017-02-23 14:42:07 +01003141 net_vlan = net.pop("vlan", None)
3142 content = myvim.new_network(net_name, net_type, net_ipprofile, shared=net_public, vlan=net_vlan) #, **net)
tiernoae4a8d12016-07-08 12:30:39 +02003143 elif item=="tenants":
3144 tenant = descriptor["tenant"]
3145 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
3146 else:
tierno42026a02017-02-10 15:13:40 +01003147 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02003148 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02003149 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02003150
tierno7edb6752016-03-21 17:37:52 +01003151 return vim_action_get(mydb, tenant_id, datacenter, item, content)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003152
3153def sdn_controller_create(mydb, tenant_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003154 data = ovim.new_of_controller(sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003155 logger.debug('New SDN controller created with uuid {}'.format(data))
3156 return data
3157
3158def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003159 data = ovim.edit_of_controller(controller_id, sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003160 msg = 'SDN controller {} updated'.format(data)
3161 logger.debug(msg)
3162 return msg
3163
3164def sdn_controller_list(mydb, tenant_id, controller_id=None):
3165 if controller_id == None:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003166 data = ovim.get_of_controllers()
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003167 else:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003168 data = ovim.show_of_controller(controller_id)
3169
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003170 msg = 'SDN controller list:\n {}'.format(data)
3171 logger.debug(msg)
3172 return data
3173
3174def sdn_controller_delete(mydb, tenant_id, controller_id):
3175 select_ = ('uuid', 'config')
3176 datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_)
3177 for datacenter in datacenters:
3178 if datacenter['config']:
3179 config = yaml.load(datacenter['config'])
3180 if 'sdn-controller' in config and config['sdn-controller'] == controller_id:
3181 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), HTTP_Conflict)
3182
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003183 data = ovim.delete_of_controller(controller_id)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003184 msg = 'SDN controller {} deleted'.format(data)
3185 logger.debug(msg)
3186 return msg
3187
3188def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping):
3189 controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id})
3190 if len(controller) < 1:
3191 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), HTTP_Not_Found)
3192
3193 try:
3194 sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"]
3195 except:
3196 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), HTTP_Bad_Request)
3197
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003198 sdn_controller = ovim.show_of_controller(sdn_controller_id)
3199 switch_dpid = sdn_controller["dpid"]
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003200
3201 maps = list()
3202 for compute_node in sdn_port_mapping:
3203 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
3204 element = dict()
3205 element["compute_node"] = compute_node["compute_node"]
3206 for port in compute_node["ports"]:
3207 element["pci"] = port.get("pci")
3208 element["switch_port"] = port.get("switch_port")
3209 element["switch_mac"] = port.get("switch_mac")
3210 if not element["pci"] or not (element["switch_port"] or element["switch_mac"]):
3211 raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'"
3212 " or 'switch_mac'", HTTP_Bad_Request)
3213 maps.append(dict(element))
3214
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003215 return ovim.set_of_port_mapping(maps, ofc_id=sdn_controller_id, switch_dpid=switch_dpid, region=datacenter_id)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003216
3217def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003218 maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id})
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003219
3220 result = {
3221 "sdn-controller": None,
3222 "datacenter-id": datacenter_id,
3223 "dpid": None,
3224 "ports_mapping": list()
3225 }
3226
3227 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id)
3228 if datacenter['config']:
3229 config = yaml.load(datacenter['config'])
3230 if 'sdn-controller' in config:
3231 controller_id = config['sdn-controller']
3232 sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id)
3233 result["sdn-controller"] = controller_id
3234 result["dpid"] = sdn_controller["dpid"]
3235
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003236 if result["sdn-controller"] == None or result["dpid"] == None:
3237 raise NfvoException("Not all SDN controller information for datacenter {} could be found: {}".format(datacenter_id, result),
3238 HTTP_Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003239
3240 if len(maps) == 0:
3241 return result
3242
3243 ports_correspondence_dict = dict()
3244 for link in maps:
3245 if result["sdn-controller"] != link["ofc_id"]:
3246 raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error)
3247 if result["dpid"] != link["switch_dpid"]:
3248 raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error)
3249 element = dict()
3250 element["pci"] = link["pci"]
3251 if link["switch_port"]:
3252 element["switch_port"] = link["switch_port"]
3253 if link["switch_mac"]:
3254 element["switch_mac"] = link["switch_mac"]
3255
3256 if not link["compute_node"] in ports_correspondence_dict:
3257 content = dict()
3258 content["compute_node"] = link["compute_node"]
3259 content["ports"] = list()
3260 ports_correspondence_dict[link["compute_node"]] = content
3261
3262 ports_correspondence_dict[link["compute_node"]]["ports"].append(element)
3263
3264 for key in sorted(ports_correspondence_dict):
3265 result["ports_mapping"].append(ports_correspondence_dict[key])
3266
3267 return result
3268
3269def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id):
tierno639520f2017-04-05 19:55:36 +02003270 return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id})