blob: 3e59285bacb8680b7caeee262e11238011f7943c [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 }
tierno42026a02017-02-10 15:13:40 +0100131 try:
tierno46df9672017-05-26 13:12:21 +0200132 ovim = ovim_module.ovim(ovim_configuration)
133 ovim.start_service()
134
135 from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
136 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
137 select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
138 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
139 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
140 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
tierno42026a02017-02-10 15:13:40 +0100141 vims = mydb.get_rows(FROM=from_, SELECT=select_)
142 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200143 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
144 'datacenter_id': vim.get('datacenter_id')}
tierno42026a02017-02-10 15:13:40 +0100145 if vim["config"]:
146 extra.update(yaml.load(vim["config"]))
147 if vim.get('dt_config'):
148 extra.update(yaml.load(vim["dt_config"]))
149 if vim["type"] not in vimconn_imported:
150 module_info=None
151 try:
152 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200153 pkg = __import__("osm_ro." + module)
154 vim_conn = getattr(pkg, module)
155 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
156 # vim_conn = imp.load_module(vim["type"], *module_info)
tierno42026a02017-02-10 15:13:40 +0100157 vimconn_imported[vim["type"]] = vim_conn
158 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200159 # if module_info and module_info[0]:
160 # file.close(module_info[0])
tiernocdee8cc2017-04-25 13:42:06 +0200161 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
tiernob3d36742017-03-03 23:51:05 +0100162 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100163
tierno867ffe92017-03-27 12:50:34 +0200164 thread_id = vim['datacenter_tenant_id']
tiernob3d36742017-03-03 23:51:05 +0100165 vim_persistent_info[thread_id] = {}
tierno42026a02017-02-10 15:13:40 +0100166 try:
167 #if not tenant:
168 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
169 myvim = vimconn_imported[ vim["type"] ].vimconnector(
tiernob3d36742017-03-03 23:51:05 +0100170 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
171 tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'],
172 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
173 user=vim['user'], passwd=vim['passwd'],
174 config=extra, persistent_info=vim_persistent_info[thread_id]
175 )
tierno42026a02017-02-10 15:13:40 +0100176 except Exception as e:
tierno46df9672017-05-26 13:12:21 +0200177 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e),
178 HTTP_Internal_Server_Error)
179 thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'],
180 vim['vim_tenant_id'])
tiernob3d36742017-03-03 23:51:05 +0100181 new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, vim['datacenter_name'],
tierno867ffe92017-03-27 12:50:34 +0200182 vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim)
tierno42026a02017-02-10 15:13:40 +0100183 new_thread.start()
tierno42026a02017-02-10 15:13:40 +0100184 vim_threads["running"][thread_id] = new_thread
185 except db_base_Exception as e:
186 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
tierno46df9672017-05-26 13:12:21 +0200187 except ovim_module.ovimException as e:
188 message = str(e)
189 if message[:22] == "DATABASE wrong version":
190 message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
191 "at host {dbhost}".format(
192 msg=message[22:-3], dbname=global_config["db_ovim_name"],
193 dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"],
194 ver=message[-3:-1], dbhost=global_config["db_ovim_host"])
195 raise NfvoException(message, HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100196
tierno867ffe92017-03-27 12:50:34 +0200197
tierno42026a02017-02-10 15:13:40 +0100198def stop_service():
tiernoc5651792017-03-27 10:50:43 +0200199 global ovim, global_config
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100200 if ovim:
201 ovim.stop_service()
tierno42026a02017-02-10 15:13:40 +0100202 for thread_id,thread in vim_threads["running"].items():
tierno867ffe92017-03-27 12:50:34 +0200203 thread.insert_task(new_task("exit", None))
tierno42026a02017-02-10 15:13:40 +0100204 vim_threads["deleting"][thread_id] = thread
tiernob3d36742017-03-03 23:51:05 +0100205 vim_threads["running"] = {}
tiernoc5651792017-03-27 10:50:43 +0200206 if global_config and global_config.get("console_thread"):
207 for thread in global_config["console_thread"]:
208 thread.terminate = True
tiernob3d36742017-03-03 23:51:05 +0100209
tierno6ddeded2017-05-16 15:40:26 +0200210def get_version():
211 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"],
212 global_config["version_date"] ))
213
tierno42026a02017-02-10 15:13:40 +0100214
tierno7edb6752016-03-21 17:37:52 +0100215def get_flavorlist(mydb, vnf_id, nfvo_tenant=None):
216 '''Obtain flavorList
217 return result, content:
218 <0, error_text upon error
219 nb_records, flavor_list on success
220 '''
221 WHERE_dict={}
222 WHERE_dict['vnf_id'] = vnf_id
223 if nfvo_tenant is not None:
224 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
tierno42026a02017-02-10 15:13:40 +0100225
tierno7edb6752016-03-21 17:37:52 +0100226 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
227 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
tiernof97fd272016-07-11 14:32:37 +0200228 flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict )
229 #print "get_flavor_list result:", result
230 #print "get_flavor_list content:", content
tierno7edb6752016-03-21 17:37:52 +0100231 flavorList=[]
tiernof97fd272016-07-11 14:32:37 +0200232 for flavor in flavors:
tierno7edb6752016-03-21 17:37:52 +0100233 flavorList.append(flavor['flavor_id'])
tiernof97fd272016-07-11 14:32:37 +0200234 return flavorList
tierno7edb6752016-03-21 17:37:52 +0100235
tiernob3d36742017-03-03 23:51:05 +0100236
tierno7edb6752016-03-21 17:37:52 +0100237def get_imagelist(mydb, vnf_id, nfvo_tenant=None):
238 '''Obtain imageList
239 return result, content:
240 <0, error_text upon error
241 nb_records, flavor_list on success
242 '''
243 WHERE_dict={}
244 WHERE_dict['vnf_id'] = vnf_id
245 if nfvo_tenant is not None:
246 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
tierno42026a02017-02-10 15:13:40 +0100247
tierno7edb6752016-03-21 17:37:52 +0100248 #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 +0200249 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 +0100250 imageList=[]
tiernof97fd272016-07-11 14:32:37 +0200251 for image in images:
tierno7edb6752016-03-21 17:37:52 +0100252 imageList.append(image['image_id'])
tiernof97fd272016-07-11 14:32:37 +0200253 return imageList
tierno7edb6752016-03-21 17:37:52 +0100254
tiernob3d36742017-03-03 23:51:05 +0100255
tiernoa2793912016-10-04 08:15:08 +0000256def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None,
257 vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None):
tierno7edb6752016-03-21 17:37:52 +0100258 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
tierno42026a02017-02-10 15:13:40 +0100259 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
tierno7edb6752016-03-21 17:37:52 +0100260 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
tiernobe41e222016-09-02 15:16:13 +0200261 raise exception upon error
tierno7edb6752016-03-21 17:37:52 +0100262 '''
263 WHERE_dict={}
264 if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
265 if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id
tiernoa2793912016-10-04 08:15:08 +0000266 if datacenter_tenant_id is not None: WHERE_dict['datacenter_tenant_id'] = datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +0100267 if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name
268 if vim_tenant is not None: WHERE_dict['dt.vim_tenant_id'] = vim_tenant
tiernoa2793912016-10-04 08:15:08 +0000269 if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name
270 if nfvo_tenant or vim_tenant or vim_tenant_name or datacenter_tenant_id:
tierno7edb6752016-03-21 17:37:52 +0100271 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 +0000272 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 +0100273 '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 +0000274 'user','passwd', 'dt.config as dt_config')
tierno7edb6752016-03-21 17:37:52 +0100275 else:
276 from_ = 'datacenters as d'
277 select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
tiernof97fd272016-07-11 14:32:37 +0200278 try:
279 vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
280 vim_dict={}
281 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200282 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
283 'datacenter_id': vim.get('datacenter_id')}
tierno8008c3a2016-10-13 15:34:28 +0000284 if vim["config"]:
tiernof97fd272016-07-11 14:32:37 +0200285 extra.update(yaml.load(vim["config"]))
tierno8008c3a2016-10-13 15:34:28 +0000286 if vim.get('dt_config'):
287 extra.update(yaml.load(vim["dt_config"]))
tiernof97fd272016-07-11 14:32:37 +0200288 if vim["type"] not in vimconn_imported:
289 module_info=None
290 try:
291 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200292 pkg = __import__("osm_ro." + module)
293 vim_conn = getattr(pkg, module)
294 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
295 # vim_conn = imp.load_module(vim["type"], *module_info)
tiernof97fd272016-07-11 14:32:37 +0200296 vimconn_imported[vim["type"]] = vim_conn
297 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200298 # if module_info and module_info[0]:
299 # file.close(module_info[0])
tiernof97fd272016-07-11 14:32:37 +0200300 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
301 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100302
tierno7edb6752016-03-21 17:37:52 +0100303 try:
tierno867ffe92017-03-27 12:50:34 +0200304 if 'datacenter_tenant_id' in vim:
305 thread_id = vim["datacenter_tenant_id"]
tiernob3d36742017-03-03 23:51:05 +0100306 if thread_id not in vim_persistent_info:
307 vim_persistent_info[thread_id] = {}
308 persistent_info = vim_persistent_info[thread_id]
309 else:
310 persistent_info = {}
tiernof97fd272016-07-11 14:32:37 +0200311 #if not tenant:
312 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
313 vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector(
314 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
tiernob3d36742017-03-03 23:51:05 +0100315 tenant_id=vim.get('vim_tenant_id',vim_tenant),
316 tenant_name=vim.get('vim_tenant_name',vim_tenant_name),
tierno42026a02017-02-10 15:13:40 +0100317 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
tierno3ae39742016-09-07 12:17:51 +0200318 user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd),
tiernob3d36742017-03-03 23:51:05 +0100319 config=extra, persistent_info=persistent_info
tiernof97fd272016-07-11 14:32:37 +0200320 )
321 except Exception as e:
322 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error)
323 return vim_dict
324 except db_base_Exception as e:
325 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
tierno42026a02017-02-10 15:13:40 +0100326
tiernob3d36742017-03-03 23:51:05 +0100327
tierno7edb6752016-03-21 17:37:52 +0100328def rollback(mydb, vims, rollback_list):
329 undeleted_items=[]
tierno42026a02017-02-10 15:13:40 +0100330 #delete things by reverse order
tierno7edb6752016-03-21 17:37:52 +0100331 for i in range(len(rollback_list)-1, -1, -1):
332 item = rollback_list[i]
333 if item["where"]=="vim":
334 if item["vim_id"] not in vims:
335 continue
336 vim=vims[ item["vim_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +0200337 try:
338 if item["what"]=="image":
339 vim.delete_image(item["uuid"])
tiernof97fd272016-07-11 14:32:37 +0200340 mydb.delete_row(FROM="datacenters_images", WHERE={"datacenter_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200341 elif item["what"]=="flavor":
342 vim.delete_flavor(item["uuid"])
garciadeblas9f8456e2016-09-05 05:02:59 +0200343 mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200344 elif item["what"]=="network":
345 vim.delete_network(item["uuid"])
346 elif item["what"]=="vm":
347 vim.delete_vminstance(item["uuid"])
348 except vimconn.vimconnException as e:
349 logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e))
350 undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"]))
tiernof97fd272016-07-11 14:32:37 +0200351 except db_base_Exception as e:
352 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 +0100353
tierno7edb6752016-03-21 17:37:52 +0100354 else: # where==mano
tiernof97fd272016-07-11 14:32:37 +0200355 try:
356 if item["what"]=="image":
357 mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]})
358 elif item["what"]=="flavor":
359 mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]})
360 except db_base_Exception as e:
361 logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e))
362 undeleted_items.append("{} '{}'".format(item['what'], item["uuid"]))
tierno42026a02017-02-10 15:13:40 +0100363 if len(undeleted_items)==0:
tierno7edb6752016-03-21 17:37:52 +0100364 return True," Rollback successful."
365 else:
366 return False," Rollback fails to delete: " + str(undeleted_items)
tierno42026a02017-02-10 15:13:40 +0100367
tiernob3d36742017-03-03 23:51:05 +0100368
tiernoafed5f12017-01-26 17:57:43 +0100369def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1):
tierno7edb6752016-03-21 17:37:52 +0100370 global global_config
tierno42026a02017-02-10 15:13:40 +0100371 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
tierno7edb6752016-03-21 17:37:52 +0100372 vnfc_interfaces={}
373 for vnfc in vnf_descriptor["vnf"]["VNFC"]:
tiernoafed5f12017-01-26 17:57:43 +0100374 name_dict = {}
tierno7edb6752016-03-21 17:37:52 +0100375 #dataplane interfaces
376 for numa in vnfc.get("numas",() ):
377 for interface in numa.get("interfaces",()):
tiernoafed5f12017-01-26 17:57:43 +0100378 if interface["name"] in name_dict:
379 raise NfvoException(
380 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
381 vnfc["name"], interface["name"]),
382 HTTP_Bad_Request)
383 name_dict[ interface["name"] ] = "underlay"
tierno7edb6752016-03-21 17:37:52 +0100384 #bridge interfaces
385 for interface in vnfc.get("bridge-ifaces",() ):
tiernoafed5f12017-01-26 17:57:43 +0100386 if interface["name"] in name_dict:
387 raise NfvoException(
388 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
389 vnfc["name"], interface["name"]),
390 HTTP_Bad_Request)
391 name_dict[ interface["name"] ] = "overlay"
392 vnfc_interfaces[ vnfc["name"] ] = name_dict
tierno36c0b172017-01-12 18:32:28 +0100393 # check bood-data info
394 if "boot-data" in vnfc:
395 # check that user-data is incompatible with users and config-files
396 if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
397 raise NfvoException(
398 "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
399 HTTP_Bad_Request)
400
tierno7edb6752016-03-21 17:37:52 +0100401 #check if the info in external_connections matches with the one in the vnfcs
402 name_list=[]
403 for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
404 if external_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100405 raise NfvoException(
406 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
407 external_connection["name"]),
408 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100409 name_list.append(external_connection["name"])
410 if external_connection["VNFC"] not in vnfc_interfaces:
tiernoafed5f12017-01-26 17:57:43 +0100411 raise NfvoException(
412 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
413 external_connection["name"], external_connection["VNFC"]),
414 HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100415
tierno7edb6752016-03-21 17:37:52 +0100416 if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
tiernoafed5f12017-01-26 17:57:43 +0100417 raise NfvoException(
418 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
419 external_connection["name"],
420 external_connection["local_iface_name"]),
421 HTTP_Bad_Request )
tierno42026a02017-02-10 15:13:40 +0100422
tierno7edb6752016-03-21 17:37:52 +0100423 #check if the info in internal_connections matches with the one in the vnfcs
424 name_list=[]
425 for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
426 if internal_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100427 raise NfvoException(
428 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
429 internal_connection["name"]),
430 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100431 name_list.append(internal_connection["name"])
432 #We should check that internal-connections of type "ptp" have only 2 elements
tiernoafed5f12017-01-26 17:57:43 +0100433
434 if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"):
435 raise NfvoException(
436 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
437 internal_connection["name"],
438 'ptp' if vnf_descriptor_version==1 else 'e-line',
439 'data' if vnf_descriptor_version==1 else "e-lan"),
440 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100441 for port in internal_connection["elements"]:
tiernoafed5f12017-01-26 17:57:43 +0100442 vnf = port["VNFC"]
443 iface = port["local_iface_name"]
444 if vnf not in vnfc_interfaces:
445 raise NfvoException(
446 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
447 internal_connection["name"], vnf),
448 HTTP_Bad_Request)
449 if iface not in vnfc_interfaces[ vnf ]:
450 raise NfvoException(
451 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
452 internal_connection["name"], iface),
453 HTTP_Bad_Request)
454 return -HTTP_Bad_Request,
455 if vnf_descriptor_version==1 and "type" not in internal_connection:
456 if vnfc_interfaces[vnf][iface] == "overlay":
457 internal_connection["type"] = "bridge"
458 else:
459 internal_connection["type"] = "data"
460 if vnf_descriptor_version==2 and "implementation" not in internal_connection:
461 if vnfc_interfaces[vnf][iface] == "overlay":
462 internal_connection["implementation"] = "overlay"
463 else:
464 internal_connection["implementation"] = "underlay"
465 if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \
466 internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay":
467 raise NfvoException(
468 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
469 internal_connection["name"],
470 iface, 'bridge' if vnf_descriptor_version==1 else 'overlay',
471 'data' if vnf_descriptor_version==1 else 'underlay'),
472 HTTP_Bad_Request)
473 if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \
474 vnfc_interfaces[vnf][iface] == "underlay":
475 raise NfvoException(
476 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
477 internal_connection["name"], iface,
478 'data' if vnf_descriptor_version==1 else 'underlay',
479 'bridge' if vnf_descriptor_version==1 else 'overlay'),
480 HTTP_Bad_Request)
481
tierno7edb6752016-03-21 17:37:52 +0100482
tierno5e91eb82016-10-04 09:39:07 +0000483def 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 +0100484 #look if image exist
485 if only_create_at_vim:
486 image_mano_id = image_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000487 if return_on_error == None:
488 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100489 else:
garciadeblas14480452017-01-10 13:08:07 +0100490 if image_dict['location']:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200491 images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
492 else:
493 images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']})
tiernof97fd272016-07-11 14:32:37 +0200494 if len(images)>=1:
495 image_mano_id = images[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100496 else:
garciadeblas14480452017-01-10 13:08:07 +0100497 #create image in MANO DB
tierno7edb6752016-03-21 17:37:52 +0100498 temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None),
garciadeblasb69fa9f2016-09-28 12:04:10 +0200499 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None),
500 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum']
tierno7edb6752016-03-21 17:37:52 +0100501 }
garciadeblas14480452017-01-10 13:08:07 +0100502 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
tiernof97fd272016-07-11 14:32:37 +0200503 image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True)
504 rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100505 #create image at every vim
506 for vim_id,vim in vims.iteritems():
507 image_created="false"
508 #look at database
tiernof97fd272016-07-11 14:32:37 +0200509 image_db = mydb.get_rows(FROM="datacenters_images", WHERE={'datacenter_id':vim_id, 'image_id':image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100510 #look at VIM if this image exist
tiernoae4a8d12016-07-08 12:30:39 +0200511 try:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200512 if image_dict['location'] is not None:
513 image_vim_id = vim.get_image_id_from_path(image_dict['location'])
514 else:
garciadeblas30833382017-01-09 09:46:31 +0100515 filter_dict = {}
516 filter_dict['name'] = image_dict['universal_name']
517 if image_dict.get('checksum') != None:
518 filter_dict['checksum'] = image_dict['checksum']
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000519 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200520 vim_images = vim.get_image_list(filter_dict)
garciadeblas14480452017-01-10 13:08:07 +0100521 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200522 if len(vim_images) > 1:
garciadeblas3fa2c052017-01-05 12:00:08 +0100523 raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), HTTP_Conflict)
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000524 elif len(vim_images) == 0:
garciadeblas3fa2c052017-01-05 12:00:08 +0100525 raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict)))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200526 else:
garciadeblas14480452017-01-10 13:08:07 +0100527 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
528 image_vim_id = vim_images[0]['id']
garciadeblasb69fa9f2016-09-28 12:04:10 +0200529
tiernoae4a8d12016-07-08 12:30:39 +0200530 except vimconn.vimconnNotFoundException as e:
garciadeblas14480452017-01-10 13:08:07 +0100531 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
tierno42026a02017-02-10 15:13:40 +0100532 try:
garciadeblas14480452017-01-10 13:08:07 +0100533 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
534 if image_dict['location']:
535 image_vim_id = vim.new_image(image_dict)
536 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
537 image_created="true"
538 else:
garciadeblasb6153a22017-02-06 15:38:33 +0100539 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
540 raise vimconn.vimconnException(str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200541 except vimconn.vimconnException as e:
542 if return_on_error:
garciadeblas14480452017-01-10 13:08:07 +0100543 logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200544 raise
tierno5e91eb82016-10-04 09:39:07 +0000545 image_vim_id = None
garciadeblas14480452017-01-10 13:08:07 +0100546 logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200547 continue
548 except vimconn.vimconnException as e:
tierno5e91eb82016-10-04 09:39:07 +0000549 if return_on_error:
550 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
551 raise
garciadeblasb69fa9f2016-09-28 12:04:10 +0200552 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
tierno5e91eb82016-10-04 09:39:07 +0000553 image_vim_id = None
garciadeblas30833382017-01-09 09:46:31 +0100554 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200555 #if we reach here, the image has been created or existed
tiernof97fd272016-07-11 14:32:37 +0200556 if len(image_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100557 #add new vim_id at datacenters_images
558 mydb.new_row('datacenters_images', {'datacenter_id':vim_id, 'image_id':image_mano_id, 'vim_id': image_vim_id, 'created':image_created})
559 elif image_db[0]["vim_id"]!=image_vim_id:
560 #modify existing vim_id at datacenters_images
561 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 +0100562
tiernof97fd272016-07-11 14:32:37 +0200563 return image_vim_id if only_create_at_vim else image_mano_id
tierno7edb6752016-03-21 17:37:52 +0100564
tiernob3d36742017-03-03 23:51:05 +0100565
tierno5e91eb82016-10-04 09:39:07 +0000566def 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 +0100567 temp_flavor_dict= {'disk':flavor_dict.get('disk',1),
568 'ram':flavor_dict.get('ram'),
569 'vcpus':flavor_dict.get('vcpus'),
570 }
571 if 'extended' in flavor_dict and flavor_dict['extended']==None:
572 del flavor_dict['extended']
573 if 'extended' in flavor_dict:
574 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
575
576 #look if flavor exist
577 if only_create_at_vim:
578 flavor_mano_id = flavor_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000579 if return_on_error == None:
580 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100581 else:
tiernof97fd272016-07-11 14:32:37 +0200582 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
583 if len(flavors)>=1:
584 flavor_mano_id = flavors[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100585 else:
586 #create flavor
587 #create one by one the images of aditional disks
588 dev_image_list=[] #list of images
589 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
590 dev_nb=0
591 for device in flavor_dict['extended'].get('devices',[]):
garciadeblas41f18be2016-10-04 09:09:58 +0200592 if "image" not in device and "image name" not in device:
tierno7edb6752016-03-21 17:37:52 +0100593 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200594 image_dict={}
595 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
596 image_dict['universal_name']=device.get('image name')
597 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
598 image_dict['location']=device.get('image')
garciadeblas14480452017-01-10 13:08:07 +0100599 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200600 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100601 image_metadata_dict = device.get('image metadata', None)
602 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100603 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100604 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
605 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200606 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
607 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
tierno7edb6752016-03-21 17:37:52 +0100608 dev_image_list.append(image_id)
tierno42026a02017-02-10 15:13:40 +0100609 dev_nb += 1
tierno7edb6752016-03-21 17:37:52 +0100610 temp_flavor_dict['name'] = flavor_dict['name']
611 temp_flavor_dict['description'] = flavor_dict.get('description',None)
tiernof97fd272016-07-11 14:32:37 +0200612 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
613 flavor_mano_id= content
614 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100615 #create flavor at every vim
616 if 'uuid' in flavor_dict:
617 del flavor_dict['uuid']
618 flavor_vim_id=None
619 for vim_id,vim in vims.items():
620 flavor_created="false"
621 #look at database
tiernof97fd272016-07-11 14:32:37 +0200622 flavor_db = mydb.get_rows(FROM="datacenters_flavors", WHERE={'datacenter_id':vim_id, 'flavor_id':flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100623 #look at VIM if this flavor exist SKIPPED
624 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
625 #if res_vim < 0:
626 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
627 # continue
628 #elif res_vim==0:
tierno42026a02017-02-10 15:13:40 +0100629
tierno7edb6752016-03-21 17:37:52 +0100630 #Create the flavor in VIM
631 #Translate images at devices from MANO id to VIM id
montesmoreno0c8def02016-12-22 12:16:23 +0000632 disk_list = []
tierno7edb6752016-03-21 17:37:52 +0100633 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
634 #make a copy of original devices
635 devices_original=[]
montesmoreno0c8def02016-12-22 12:16:23 +0000636
tierno7edb6752016-03-21 17:37:52 +0100637 for device in flavor_dict["extended"].get("devices",[]):
638 dev={}
639 dev.update(device)
640 devices_original.append(dev)
641 if 'image' in device:
642 del device['image']
643 if 'image metadata' in device:
644 del device['image metadata']
645 dev_nb=0
646 for index in range(0,len(devices_original)) :
647 device=devices_original[index]
montesmoreno0c8def02016-12-22 12:16:23 +0000648 if "image" not in device and "image name" not in device:
649 if 'size' in device:
650 disk_list.append({'size': device.get('size', default_volume_size)})
tierno7edb6752016-03-21 17:37:52 +0100651 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200652 image_dict={}
653 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
654 image_dict['universal_name']=device.get('image name')
655 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
656 image_dict['location']=device.get('image')
garciadeblas14480452017-01-10 13:08:07 +0100657 #image_dict['new_location']=device.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200658 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100659 image_metadata_dict = device.get('image metadata', None)
660 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100661 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100662 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
663 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200664 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 +0100665 image_dict["uuid"]=image_mano_id
tiernof97fd272016-07-11 14:32:37 +0200666 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 +0000667
668 #save disk information (image must be based on and size
669 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
670
tierno7edb6752016-03-21 17:37:52 +0100671 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
672 dev_nb += 1
tiernof97fd272016-07-11 14:32:37 +0200673 if len(flavor_db)>0:
tierno7edb6752016-03-21 17:37:52 +0100674 #check that this vim_id exist in VIM, if not create
675 flavor_vim_id=flavor_db[0]["vim_id"]
tiernoae4a8d12016-07-08 12:30:39 +0200676 try:
677 vim.get_flavor(flavor_vim_id)
678 continue #flavor exist
679 except vimconn.vimconnException:
680 pass
tierno7edb6752016-03-21 17:37:52 +0100681 #create flavor at vim
tiernoae4a8d12016-07-08 12:30:39 +0200682 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
683 try:
tiernocf157a82017-01-30 14:07:06 +0100684 flavor_vim_id = None
685 flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict)
686 flavor_create="false"
687 except vimconn.vimconnException as e:
688 pass
689 try:
690 if not flavor_vim_id:
691 flavor_vim_id = vim.new_flavor(flavor_dict)
692 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
693 flavor_created="true"
tiernoae4a8d12016-07-08 12:30:39 +0200694 except vimconn.vimconnException as e:
695 if return_on_error:
696 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200697 raise
tiernoae4a8d12016-07-08 12:30:39 +0200698 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tierno5e91eb82016-10-04 09:39:07 +0000699 flavor_vim_id = None
tiernoae4a8d12016-07-08 12:30:39 +0200700 continue
tierno7edb6752016-03-21 17:37:52 +0100701 #if reach here the flavor has been create or exist
tiernof97fd272016-07-11 14:32:37 +0200702 if len(flavor_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100703 #add new vim_id at datacenters_flavors
montesmoreno0c8def02016-12-22 12:16:23 +0000704 extended_devices_yaml = None
705 if len(disk_list) > 0:
706 extended_devices = dict()
707 extended_devices['disks'] = disk_list
708 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
709 mydb.new_row('datacenters_flavors',
710 {'datacenter_id':vim_id, 'flavor_id':flavor_mano_id, 'vim_id': flavor_vim_id,
711 'created':flavor_created,'extended': extended_devices_yaml})
tierno7edb6752016-03-21 17:37:52 +0100712 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
713 #modify existing vim_id at datacenters_flavors
714 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 +0100715
tiernof97fd272016-07-11 14:32:37 +0200716 return flavor_vim_id if only_create_at_vim else flavor_mano_id
tierno7edb6752016-03-21 17:37:52 +0100717
tiernob3d36742017-03-03 23:51:05 +0100718
tierno7edb6752016-03-21 17:37:52 +0100719def new_vnf(mydb, tenant_id, vnf_descriptor):
720 global global_config
tierno42026a02017-02-10 15:13:40 +0100721
tierno7edb6752016-03-21 17:37:52 +0100722 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +0100723 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
tierno7edb6752016-03-21 17:37:52 +0100724 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +0100725 vims = {}
tierno7edb6752016-03-21 17:37:52 +0100726 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +0100727 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100728 if "tenant_id" in vnf_descriptor["vnf"]:
729 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +0200730 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
731 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +0100732 else:
733 vnf_descriptor['vnf']['tenant_id'] = tenant_id
734 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +0100735 if global_config["auto_push_VNF_to_VIMs"]:
736 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100737
738 # Step 4. Review the descriptor and add missing fields
739 #print vnf_descriptor
tiernof97fd272016-07-11 14:32:37 +0200740 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
tierno7edb6752016-03-21 17:37:52 +0100741 vnf_name = vnf_descriptor['vnf']['name']
742 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
743 if "physical" in vnf_descriptor['vnf']:
744 del vnf_descriptor['vnf']['physical']
745 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +0100746
tierno42026a02017-02-10 15:13:40 +0100747 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
tiernof97fd272016-07-11 14:32:37 +0200748 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
749 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +0100750
tierno7edb6752016-03-21 17:37:52 +0100751 #For each VNFC, we add it to the VNFCDict and we create a flavor.
752 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
753 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
tierno7edb6752016-03-21 17:37:52 +0100754 try:
tiernof97fd272016-07-11 14:32:37 +0200755 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +0100756 for vnfc in vnf_descriptor['vnf']['VNFC']:
757 VNFCitem={}
758 VNFCitem["name"] = vnfc['name']
759 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +0100760
tiernof97fd272016-07-11 14:32:37 +0200761 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +0100762
tierno7edb6752016-03-21 17:37:52 +0100763 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +0200764 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 +0100765 myflavorDict["description"] = VNFCitem["description"]
766 myflavorDict["ram"] = vnfc.get("ram", 0)
767 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
768 myflavorDict["disk"] = vnfc.get("disk", 1)
769 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +0100770
tierno7edb6752016-03-21 17:37:52 +0100771 devices = vnfc.get("devices")
772 if devices != None:
773 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +0100774
tierno7edb6752016-03-21 17:37:52 +0100775 # TODO:
776 # 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 +0100777 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
778
tierno7edb6752016-03-21 17:37:52 +0100779 # Previous code has been commented
780 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
781 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
782 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
783 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
784 #else:
785 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
786 # if result2:
787 # print "Error creating flavor: unknown processor model. Rollback successful."
788 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
789 # else:
790 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
791 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +0100792
tierno7edb6752016-03-21 17:37:52 +0100793 if 'numas' in vnfc and len(vnfc['numas'])>0:
794 myflavorDict['extended']['numas'] = vnfc['numas']
795
796 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +0100797
tierno7edb6752016-03-21 17:37:52 +0100798 # Step 6.2 New flavors are created in the VIM
tiernof97fd272016-07-11 14:32:37 +0200799 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
tierno7edb6752016-03-21 17:37:52 +0100800
tiernof97fd272016-07-11 14:32:37 +0200801 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
tierno7edb6752016-03-21 17:37:52 +0100802 VNFCitem["flavor_id"] = flavor_id
803 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +0100804
tiernof97fd272016-07-11 14:32:37 +0200805 logger.debug("Creating new images in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +0100806 # Step 6.3 New images are created in the VIM
807 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +0100808 #This "for" loop might be integrated with the previous one
tierno7edb6752016-03-21 17:37:52 +0100809 #In case this integration is made, the VNFCDict might become a VNFClist.
810 for vnfc in vnf_descriptor['vnf']['VNFC']:
tiernof97fd272016-07-11 14:32:37 +0200811 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +0200812 image_dict={}
813 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
814 image_dict['universal_name']=vnfc.get('image name')
815 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
816 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +0100817 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200818 image_dict['checksum']=vnfc.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100819 image_metadata_dict = vnfc.get('image metadata', None)
820 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100821 if image_metadata_dict is not None:
tierno7edb6752016-03-21 17:37:52 +0100822 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
823 image_dict['metadata']=image_metadata_str
824 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
tiernof97fd272016-07-11 14:32:37 +0200825 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
826 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
tierno7edb6752016-03-21 17:37:52 +0100827 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +0200828 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno36c0b172017-01-12 18:32:28 +0100829 if vnfc.get("boot-data"):
830 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
tierno7edb6752016-03-21 17:37:52 +0100831
tierno42026a02017-02-10 15:13:40 +0100832
tiernof97fd272016-07-11 14:32:37 +0200833 # Step 7. Storing the VNF descriptor in the repository
834 if "descriptor" not in vnf_descriptor["vnf"]:
835 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +0100836
tiernof97fd272016-07-11 14:32:37 +0200837 # Step 8. Adding the VNF to the NFVO DB
838 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
839 return vnf_id
840 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
tierno7edb6752016-03-21 17:37:52 +0100841 _, message = rollback(mydb, vims, rollback_list)
tiernof97fd272016-07-11 14:32:37 +0200842 if isinstance(e, db_base_Exception):
843 error_text = "Exception at database"
844 elif isinstance(e, KeyError):
845 error_text = "KeyError exception "
846 e.http_code = HTTP_Internal_Server_Error
847 else:
848 error_text = "Exception at VIM"
849 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
850 #logger.error("start_scenario %s", error_text)
851 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +0100852
tiernob3d36742017-03-03 23:51:05 +0100853
garciadeblas9f8456e2016-09-05 05:02:59 +0200854def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
855 global global_config
tierno42026a02017-02-10 15:13:40 +0100856
garciadeblas9f8456e2016-09-05 05:02:59 +0200857 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +0100858 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
garciadeblas9f8456e2016-09-05 05:02:59 +0200859 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +0100860 vims = {}
garciadeblas9f8456e2016-09-05 05:02:59 +0200861 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +0100862 check_tenant(mydb, tenant_id)
garciadeblas9f8456e2016-09-05 05:02:59 +0200863 if "tenant_id" in vnf_descriptor["vnf"]:
864 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
865 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
866 HTTP_Unauthorized)
867 else:
868 vnf_descriptor['vnf']['tenant_id'] = tenant_id
869 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +0100870 if global_config["auto_push_VNF_to_VIMs"]:
871 vims = get_vim(mydb, tenant_id)
garciadeblas9f8456e2016-09-05 05:02:59 +0200872
873 # Step 4. Review the descriptor and add missing fields
874 #print vnf_descriptor
875 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
876 vnf_name = vnf_descriptor['vnf']['name']
877 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
878 if "physical" in vnf_descriptor['vnf']:
879 del vnf_descriptor['vnf']['physical']
880 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +0100881
tierno42026a02017-02-10 15:13:40 +0100882 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
garciadeblas9f8456e2016-09-05 05:02:59 +0200883 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
884 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +0100885
garciadeblas9f8456e2016-09-05 05:02:59 +0200886 #For each VNFC, we add it to the VNFCDict and we create a flavor.
887 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
888 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
889 try:
890 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
891 for vnfc in vnf_descriptor['vnf']['VNFC']:
892 VNFCitem={}
893 VNFCitem["name"] = vnfc['name']
894 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +0100895
garciadeblas9f8456e2016-09-05 05:02:59 +0200896 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +0100897
garciadeblas9f8456e2016-09-05 05:02:59 +0200898 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +0200899 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 +0200900 myflavorDict["description"] = VNFCitem["description"]
901 myflavorDict["ram"] = vnfc.get("ram", 0)
902 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
903 myflavorDict["disk"] = vnfc.get("disk", 1)
904 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +0100905
garciadeblas9f8456e2016-09-05 05:02:59 +0200906 devices = vnfc.get("devices")
907 if devices != None:
908 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +0100909
garciadeblas9f8456e2016-09-05 05:02:59 +0200910 # TODO:
911 # 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 +0100912 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
913
garciadeblas9f8456e2016-09-05 05:02:59 +0200914 # Previous code has been commented
915 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
916 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
917 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
918 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
919 #else:
920 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
921 # if result2:
922 # print "Error creating flavor: unknown processor model. Rollback successful."
923 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
924 # else:
925 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
926 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +0100927
garciadeblas9f8456e2016-09-05 05:02:59 +0200928 if 'numas' in vnfc and len(vnfc['numas'])>0:
929 myflavorDict['extended']['numas'] = vnfc['numas']
930
931 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +0100932
garciadeblas9f8456e2016-09-05 05:02:59 +0200933 # Step 6.2 New flavors are created in the VIM
934 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
935
936 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
937 VNFCitem["flavor_id"] = flavor_id
938 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +0100939
garciadeblas9f8456e2016-09-05 05:02:59 +0200940 logger.debug("Creating new images in the VIM for each VNFC")
941 # Step 6.3 New images are created in the VIM
942 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +0100943 #This "for" loop might be integrated with the previous one
garciadeblas9f8456e2016-09-05 05:02:59 +0200944 #In case this integration is made, the VNFCDict might become a VNFClist.
945 for vnfc in vnf_descriptor['vnf']['VNFC']:
946 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +0200947 image_dict={}
948 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
949 image_dict['universal_name']=vnfc.get('image name')
950 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
951 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +0100952 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200953 image_dict['checksum']=vnfc.get('image checksum')
garciadeblas9f8456e2016-09-05 05:02:59 +0200954 image_metadata_dict = vnfc.get('image metadata', None)
955 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100956 if image_metadata_dict is not None:
garciadeblas9f8456e2016-09-05 05:02:59 +0200957 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
958 image_dict['metadata']=image_metadata_str
959 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
960 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
961 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
962 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +0200963 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno36c0b172017-01-12 18:32:28 +0100964 if vnfc.get("boot-data"):
965 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
garciadeblas9f8456e2016-09-05 05:02:59 +0200966
garciadeblas9f8456e2016-09-05 05:02:59 +0200967 # Step 7. Storing the VNF descriptor in the repository
968 if "descriptor" not in vnf_descriptor["vnf"]:
969 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +0100970
garciadeblas9f8456e2016-09-05 05:02:59 +0200971 # Step 8. Adding the VNF to the NFVO DB
972 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
973 return vnf_id
974 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
975 _, message = rollback(mydb, vims, rollback_list)
976 if isinstance(e, db_base_Exception):
977 error_text = "Exception at database"
978 elif isinstance(e, KeyError):
979 error_text = "KeyError exception "
980 e.http_code = HTTP_Internal_Server_Error
981 else:
982 error_text = "Exception at VIM"
983 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
984 #logger.error("start_scenario %s", error_text)
985 raise NfvoException(error_text, e.http_code)
986
tiernob3d36742017-03-03 23:51:05 +0100987
tierno7edb6752016-03-21 17:37:52 +0100988def get_vnf_id(mydb, tenant_id, vnf_id):
989 #check valid tenant_id
tierno42026a02017-02-10 15:13:40 +0100990 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100991 #obtain data
992 where_or = {}
993 if tenant_id != "any":
994 where_or["tenant_id"] = tenant_id
995 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +0100996 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
997
tiernof97fd272016-07-11 14:32:37 +0200998 vnf_id=vnf["uuid"]
tierno7edb6752016-03-21 17:37:52 +0100999 filter_keys = ('uuid','name','description','public', "tenant_id", "created_at")
tiernof97fd272016-07-11 14:32:37 +02001000 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
tierno7edb6752016-03-21 17:37:52 +01001001 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1002 data={'vnf' : filtered_content}
1003 #GET VM
tiernof97fd272016-07-11 14:32:37 +02001004 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
tierno36c0b172017-01-12 18:32:28 +01001005 SELECT=('vms.uuid as uuid','vms.name as name', 'vms.description as description', 'boot_data'),
tierno7edb6752016-03-21 17:37:52 +01001006 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +02001007 if len(content)==0:
1008 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno36c0b172017-01-12 18:32:28 +01001009 # change boot_data into boot-data
1010 for vm in content:
1011 if vm.get("boot_data"):
1012 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
1013 del vm["boot_data"]
tierno7edb6752016-03-21 17:37:52 +01001014
1015 data['vnf']['VNFC'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001016 #TODO: GET all the information from a VNFC and include it in the output.
tierno42026a02017-02-10 15:13:40 +01001017
tierno7edb6752016-03-21 17:37:52 +01001018 #GET NET
tierno42026a02017-02-10 15:13:40 +01001019 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
tierno7edb6752016-03-21 17:37:52 +01001020 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1021 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +02001022 data['vnf']['nets'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001023
1024 #GET ip-profile for each net
1025 for net in data['vnf']['nets']:
1026 ipprofiles = mydb.get_rows(FROM='ip_profiles',
1027 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1028 WHERE={'net_id': net["uuid"]} )
1029 if len(ipprofiles)==1:
1030 net["ip_profile"] = ipprofiles[0]
1031 elif len(ipprofiles)>1:
1032 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 +01001033
1034
garciadeblas9f8456e2016-09-05 05:02:59 +02001035 #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 +01001036
garciadeblas9f8456e2016-09-05 05:02:59 +02001037 #GET External Interfaces
tiernof97fd272016-07-11 14:32:37 +02001038 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 +01001039 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1040 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
tierno42026a02017-02-10 15:13:40 +01001041 WHERE={'vnfs.uuid': vnf_id},
tierno7edb6752016-03-21 17:37:52 +01001042 WHERE_NOT={'interfaces.external_name': None} )
1043 #print content
tiernof97fd272016-07-11 14:32:37 +02001044 data['vnf']['external-connections'] = content
tierno42026a02017-02-10 15:13:40 +01001045
tiernof97fd272016-07-11 14:32:37 +02001046 return data
tierno7edb6752016-03-21 17:37:52 +01001047
1048
1049def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
1050 # Check tenant exist
1051 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +02001052 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001053 # Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernof97fd272016-07-11 14:32:37 +02001054 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001055 else:
1056 vims={}
1057
1058 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1059 where_or = {}
1060 if tenant_id != "any":
1061 where_or["tenant_id"] = tenant_id
1062 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001063 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 +02001064 vnf_id = vnf["uuid"]
tierno42026a02017-02-10 15:13:40 +01001065
tierno7edb6752016-03-21 17:37:52 +01001066 # "Getting the list of flavors and tenants of the VNF"
tierno42026a02017-02-10 15:13:40 +01001067 flavorList = get_flavorlist(mydb, vnf_id)
tiernof97fd272016-07-11 14:32:37 +02001068 if len(flavorList)==0:
1069 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001070
tiernof97fd272016-07-11 14:32:37 +02001071 imageList = get_imagelist(mydb, vnf_id)
1072 if len(imageList)==0:
1073 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001074
tiernof97fd272016-07-11 14:32:37 +02001075 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
1076 if deleted == 0:
1077 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno42026a02017-02-10 15:13:40 +01001078
tierno7edb6752016-03-21 17:37:52 +01001079 undeletedItems = []
1080 for flavor in flavorList:
1081 #check if flavor is used by other vnf
tiernof97fd272016-07-11 14:32:37 +02001082 try:
1083 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
1084 if len(c) > 0:
1085 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
1086 continue
1087 #flavor not used, must be deleted
1088 #delelte at VIM
1089 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id':flavor})
tierno7edb6752016-03-21 17:37:52 +01001090 for flavor_vim in c:
1091 if flavor_vim["datacenter_id"] not in vims:
1092 continue
1093 if flavor_vim['created']=='false': #skip this flavor because not created by openmano
1094 continue
1095 myvim=vims[ flavor_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +02001096 try:
1097 myvim.delete_flavor(flavor_vim["vim_id"])
1098 except vimconn.vimconnNotFoundException as e:
1099 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], flavor_vim["datacenter_id"] )
1100 except vimconn.vimconnException as e:
1101 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1102 flavor_vim["vim_id"], flavor_vim["datacenter_id"], type(e).__name__, str(e))
1103 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"], flavor_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +02001104 #delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1105 mydb.delete_row_by_id('flavors', flavor)
1106 except db_base_Exception as e:
1107 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
tierno7edb6752016-03-21 17:37:52 +01001108 undeletedItems.append("flavor %s" % flavor)
tiernof97fd272016-07-11 14:32:37 +02001109
tierno42026a02017-02-10 15:13:40 +01001110
tierno7edb6752016-03-21 17:37:52 +01001111 for image in imageList:
tiernof97fd272016-07-11 14:32:37 +02001112 try:
1113 #check if image is used by other vnf
1114 c = mydb.get_rows(FROM='vms', WHERE={'image_id':image} )
1115 if len(c) > 0:
1116 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
1117 continue
1118 #image not used, must be deleted
1119 #delelte at VIM
1120 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
tierno7edb6752016-03-21 17:37:52 +01001121 for image_vim in c:
1122 if image_vim["datacenter_id"] not in vims:
1123 continue
1124 if image_vim['created']=='false': #skip this image because not created by openmano
1125 continue
1126 myvim=vims[ image_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +02001127 try:
1128 myvim.delete_image(image_vim["vim_id"])
1129 except vimconn.vimconnNotFoundException as e:
1130 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
1131 except vimconn.vimconnException as e:
1132 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1133 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
1134 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +02001135 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1136 mydb.delete_row_by_id('images', image)
1137 except db_base_Exception as e:
1138 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
tierno7edb6752016-03-21 17:37:52 +01001139 undeletedItems.append("image %s" % image)
1140
tiernof97fd272016-07-11 14:32:37 +02001141 return vnf_id + " " + vnf["name"]
tierno42026a02017-02-10 15:13:40 +01001142 #if undeletedItems:
tiernof97fd272016-07-11 14:32:37 +02001143 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
tierno7edb6752016-03-21 17:37:52 +01001144
tiernob3d36742017-03-03 23:51:05 +01001145
tierno7edb6752016-03-21 17:37:52 +01001146def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
1147 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
1148 if result < 0:
1149 return result, vims
1150 elif result == 0:
1151 return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name
1152 myvim = vims.values()[0]
1153 result,servers = myvim.get_hosts_info()
1154 if result < 0:
1155 return result, servers
1156 topology = {'name':myvim['name'] , 'servers': servers}
1157 return result, topology
1158
tiernob3d36742017-03-03 23:51:05 +01001159
tierno7edb6752016-03-21 17:37:52 +01001160def get_hosts(mydb, nfvo_tenant_id):
tiernof97fd272016-07-11 14:32:37 +02001161 vims = get_vim(mydb, nfvo_tenant_id)
1162 if len(vims) == 0:
1163 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found)
1164 elif len(vims)>1:
1165 #print "nfvo.datacenter_action() error. Several datacenters found"
1166 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001167 myvim = vims.values()[0]
tiernof97fd272016-07-11 14:32:37 +02001168 try:
1169 hosts = myvim.get_hosts()
1170 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01001171
tiernof97fd272016-07-11 14:32:37 +02001172 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
1173 for host in hosts:
1174 server={'name':host['name'], 'vms':[]}
1175 for vm in host['instances']:
1176 #get internal name and model
tierno42026a02017-02-10 15:13:40 +01001177 try:
tiernof97fd272016-07-11 14:32:37 +02001178 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1179 WHERE={'vim_vm_id':vm['id']} )
1180 if len(c) == 0:
1181 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1182 continue
1183 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
tierno42026a02017-02-10 15:13:40 +01001184
tiernof97fd272016-07-11 14:32:37 +02001185 except db_base_Exception as e:
1186 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1187 datacenter['Datacenters'][0]['servers'].append(server)
1188 #return -400, "en construccion"
tierno42026a02017-02-10 15:13:40 +01001189
tiernof97fd272016-07-11 14:32:37 +02001190 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1191 return datacenter
1192 except vimconn.vimconnException as e:
1193 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001194
tiernob3d36742017-03-03 23:51:05 +01001195
tierno7edb6752016-03-21 17:37:52 +01001196def new_scenario(mydb, tenant_id, topo):
1197
1198# result, vims = get_vim(mydb, tenant_id)
1199# if result < 0:
1200# return result, vims
1201#1: parse input
1202 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001203 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001204 if "tenant_id" in topo:
1205 if topo["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001206 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id),
1207 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001208 else:
1209 tenant_id=None
1210
tierno42026a02017-02-10 15:13:40 +01001211#1.1: get VNFs and external_networks (other_nets).
tierno7edb6752016-03-21 17:37:52 +01001212 vnfs={}
1213 other_nets={} #external_networks, bridge_networks and data_networkds
1214 nodes = topo['topology']['nodes']
1215 for k in nodes.keys():
1216 if nodes[k]['type'] == 'VNF':
1217 vnfs[k] = nodes[k]
1218 vnfs[k]['ifaces'] = {}
tierno42026a02017-02-10 15:13:40 +01001219 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
tierno7edb6752016-03-21 17:37:52 +01001220 other_nets[k] = nodes[k]
1221 other_nets[k]['external']=True
tierno42026a02017-02-10 15:13:40 +01001222 elif nodes[k]['type'] == 'network':
tierno7edb6752016-03-21 17:37:52 +01001223 other_nets[k] = nodes[k]
1224 other_nets[k]['external']=False
tierno42026a02017-02-10 15:13:40 +01001225
tierno7edb6752016-03-21 17:37:52 +01001226
1227#1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1228 for name,vnf in vnfs.items():
tiernocea279c2016-07-18 12:36:49 +02001229 where={}
1230 where_or={"tenant_id": tenant_id, 'public': "true"}
tierno7edb6752016-03-21 17:37:52 +01001231 error_text = ""
1232 error_pos = "'topology':'nodes':'" + name + "'"
1233 if 'vnf_id' in vnf:
1234 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001235 where['uuid'] = vnf['vnf_id']
tierno7edb6752016-03-21 17:37:52 +01001236 if 'VNF model' in vnf:
1237 error_text += " 'VNF model' " + vnf['VNF model']
tiernocea279c2016-07-18 12:36:49 +02001238 where['name'] = vnf['VNF model']
1239 if len(where) == 0:
tiernof97fd272016-07-11 14:32:37 +02001240 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +01001241
tiernocea279c2016-07-18 12:36:49 +02001242 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1243 FROM='vnfs',
tierno42026a02017-02-10 15:13:40 +01001244 WHERE=where,
tiernocea279c2016-07-18 12:36:49 +02001245 WHERE_OR=where_or,
1246 WHERE_AND_OR="AND")
tiernof97fd272016-07-11 14:32:37 +02001247 if len(vnf_db)==0:
1248 raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1249 elif len(vnf_db)>1:
1250 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001251 vnf['uuid']=vnf_db[0]['uuid']
1252 vnf['description']=vnf_db[0]['description']
1253 #get external interfaces
tierno42026a02017-02-10 15:13:40 +01001254 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1255 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 +01001256 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
tierno7edb6752016-03-21 17:37:52 +01001257 for ext_iface in ext_ifaces:
1258 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1259
1260#1.4 get list of connections
1261 conections = topo['topology']['connections']
1262 conections_list = []
tiernoefd80c92016-09-16 14:17:46 +02001263 conections_list_name = []
tierno7edb6752016-03-21 17:37:52 +01001264 for k in conections.keys():
1265 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1266 ifaces_list = conections[k]['nodes'].items()
1267 elif type(conections[k]['nodes'])==list: #list with dictionary
1268 ifaces_list=[]
1269 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1270 for k2 in conection_pair_list:
1271 ifaces_list += k2
1272
1273 con_type = conections[k].get("type", "link")
1274 if con_type != "link":
1275 if k in other_nets:
tiernof97fd272016-07-11 14:32:37 +02001276 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001277 other_nets[k] = {'external': False}
1278 if conections[k].get("graph"):
1279 other_nets[k]["graph"] = conections[k]["graph"]
1280 ifaces_list.append( (k, None) )
1281
tierno42026a02017-02-10 15:13:40 +01001282
tierno7edb6752016-03-21 17:37:52 +01001283 if con_type == "external_network":
1284 other_nets[k]['external'] = True
1285 if conections[k].get("model"):
1286 other_nets[k]["model"] = conections[k]["model"]
1287 else:
1288 other_nets[k]["model"] = k
tierno42026a02017-02-10 15:13:40 +01001289 if con_type == "dataplane_net" or con_type == "bridge_net":
tierno7edb6752016-03-21 17:37:52 +01001290 other_nets[k]["model"] = con_type
tierno42026a02017-02-10 15:13:40 +01001291
tiernoefd80c92016-09-16 14:17:46 +02001292 conections_list_name.append(k)
tierno7edb6752016-03-21 17:37:52 +01001293 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)
1294 #print set(ifaces_list)
1295 #check valid VNF and iface names
1296 for iface in ifaces_list:
1297 if iface[0] not in vnfs and iface[0] not in other_nets :
tiernof97fd272016-07-11 14:32:37 +02001298 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1299 str(k), iface[0]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001300 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
tiernof97fd272016-07-11 14:32:37 +02001301 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1302 str(k), iface[0], iface[1]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001303
1304#1.5 unify connections from the pair list to a consolidated list
1305 index=0
1306 while index < len(conections_list):
1307 index2 = index+1
1308 while index2 < len(conections_list):
1309 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
1310 conections_list[index] |= conections_list[index2]
1311 del conections_list[index2]
tiernoefd80c92016-09-16 14:17:46 +02001312 del conections_list_name[index2]
tierno7edb6752016-03-21 17:37:52 +01001313 else:
1314 index2 += 1
1315 conections_list[index] = list(conections_list[index]) # from set to list again
1316 index += 1
1317 #for k in conections_list:
1318 # print k
tierno42026a02017-02-10 15:13:40 +01001319
tierno7edb6752016-03-21 17:37:52 +01001320
1321
1322#1.6 Delete non external nets
1323# for k in other_nets.keys():
1324# if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1325# for con in conections_list:
1326# delete_indexes=[]
1327# for index in range(0,len(con)):
1328# if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1329# for index in delete_indexes:
1330# del con[index]
1331# del other_nets[k]
1332#1.7: Check external_ports are present at database table datacenter_nets
1333 for k,net in other_nets.items():
1334 error_pos = "'topology':'nodes':'" + k + "'"
1335 if net['external']==False:
1336 if 'name' not in net:
1337 net['name']=k
1338 if 'model' not in net:
tiernof97fd272016-07-11 14:32:37 +02001339 raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001340 if net['model']=='bridge_net':
1341 net['type']='bridge';
1342 elif net['model']=='dataplane_net':
1343 net['type']='data';
1344 else:
tiernof97fd272016-07-11 14:32:37 +02001345 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001346 else: #external
1347#IF we do not want to check that external network exist at datacenter
1348 pass
tierno42026a02017-02-10 15:13:40 +01001349#ELSE
tierno7edb6752016-03-21 17:37:52 +01001350# error_text = ""
1351# WHERE_={}
1352# if 'net_id' in net:
1353# error_text += " 'net_id' " + net['net_id']
1354# WHERE_['uuid'] = net['net_id']
1355# if 'model' in net:
1356# error_text += " 'model' " + net['model']
1357# WHERE_['name'] = net['model']
1358# if len(WHERE_) == 0:
1359# return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1360# r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1361# FROM='datacenter_nets', WHERE=WHERE_ )
1362# if r<0:
1363# print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1364# elif r==0:
1365# print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1366# return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1367# elif r>1:
tierno42026a02017-02-10 15:13:40 +01001368# print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1369# 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 +01001370# other_nets[k].update(net_db[0])
tierno42026a02017-02-10 15:13:40 +01001371#ENDIF
tierno7edb6752016-03-21 17:37:52 +01001372 net_list={}
1373 net_nb=0 #Number of nets
1374 for con in conections_list:
1375 #check if this is connected to a external net
1376 other_net_index=-1
1377 #print
1378 #print "con", con
1379 for index in range(0,len(con)):
1380 #check if this is connected to a external net
1381 for net_key in other_nets.keys():
1382 if con[index][0]==net_key:
1383 if other_net_index>=0:
tierno42026a02017-02-10 15:13:40 +01001384 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 +02001385 #print "nfvo.new_scenario " + error_text
1386 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001387 else:
1388 other_net_index = index
1389 net_target = net_key
1390 break
1391 #print "other_net_index", other_net_index
1392 try:
1393 if other_net_index>=0:
1394 del con[other_net_index]
1395#IF we do not want to check that external network exist at datacenter
1396 if other_nets[net_target]['external'] :
1397 if "name" not in other_nets[net_target]:
1398 other_nets[net_target]['name'] = other_nets[net_target]['model']
1399 if other_nets[net_target]["type"] == "external_network":
1400 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
1401 other_nets[net_target]["type"] = "data"
1402 else:
1403 other_nets[net_target]["type"] = "bridge"
tierno42026a02017-02-10 15:13:40 +01001404#ELSE
tierno7edb6752016-03-21 17:37:52 +01001405# if other_nets[net_target]['external'] :
1406# 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
1407# if type_=='data' and other_nets[net_target]['type']=="ptp":
1408# error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
1409# print "nfvo.new_scenario " + error_text
1410# return -HTTP_Bad_Request, error_text
tierno42026a02017-02-10 15:13:40 +01001411#ENDIF
tierno7edb6752016-03-21 17:37:52 +01001412 for iface in con:
1413 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1414 else:
1415 #create a net
1416 net_type_bridge=False
1417 net_type_data=False
1418 net_target = "__-__net"+str(net_nb)
tierno42026a02017-02-10 15:13:40 +01001419 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
tiernoefd80c92016-09-16 14:17:46 +02001420 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
tierno42026a02017-02-10 15:13:40 +01001421 'external':False}
tierno7edb6752016-03-21 17:37:52 +01001422 for iface in con:
1423 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1424 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
1425 if iface_type=='mgmt' or iface_type=='bridge':
1426 net_type_bridge = True
1427 else:
1428 net_type_data = True
1429 if net_type_bridge and net_type_data:
1430 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 +02001431 #print "nfvo.new_scenario " + error_text
1432 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001433 elif net_type_bridge:
1434 type_='bridge'
1435 else:
1436 type_='data' if len(con)>2 else 'ptp'
1437 net_list[net_target]['type'] = type_
1438 net_nb+=1
1439 except Exception:
1440 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02001441 #print "nfvo.new_scenario " + error_text
tierno7edb6752016-03-21 17:37:52 +01001442 #raise e
tiernof97fd272016-07-11 14:32:37 +02001443 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001444
1445#1.8: Connect to management net all not already connected interfaces of type 'mgmt'
tierno42026a02017-02-10 15:13:40 +01001446 #1.8.1 obtain management net
tiernof97fd272016-07-11 14:32:37 +02001447 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
tierno7edb6752016-03-21 17:37:52 +01001448 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
tierno42026a02017-02-10 15:13:40 +01001449 #1.8.2 check all interfaces from all vnfs
tiernof97fd272016-07-11 14:32:37 +02001450 if len(mgmt_net)>0:
tierno7edb6752016-03-21 17:37:52 +01001451 add_mgmt_net = False
1452 for vnf in vnfs.values():
1453 for iface in vnf['ifaces'].values():
1454 if iface['type']=='mgmt' and 'net_key' not in iface:
1455 #iface not connected
1456 iface['net_key'] = 'mgmt'
1457 add_mgmt_net = True
1458 if add_mgmt_net and 'mgmt' not in net_list:
1459 net_list['mgmt']=mgmt_net[0]
1460 net_list['mgmt']['external']=True
1461 net_list['mgmt']['graph']={'visible':False}
1462
1463 net_list.update(other_nets)
tiernof97fd272016-07-11 14:32:37 +02001464 #print
1465 #print 'net_list', net_list
1466 #print
1467 #print 'vnfs', vnfs
1468 #print
tierno7edb6752016-03-21 17:37:52 +01001469
1470#2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
tiernof97fd272016-07-11 14:32:37 +02001471 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
tierno392f2852016-05-13 12:28:55 +02001472 'tenant_id':tenant_id, 'name':topo['name'],
1473 'description':topo.get('description',topo['name']),
1474 'public': topo.get('public', False)
1475 })
tierno42026a02017-02-10 15:13:40 +01001476
tiernof97fd272016-07-11 14:32:37 +02001477 return c
tierno7edb6752016-03-21 17:37:52 +01001478
tiernob3d36742017-03-03 23:51:05 +01001479
tierno5bb59dc2017-02-13 14:53:54 +01001480def new_scenario_v02(mydb, tenant_id, scenario_dict, version):
1481 """ This creates a new scenario for version 0.2 and 0.3"""
tierno392f2852016-05-13 12:28:55 +02001482 scenario = scenario_dict["scenario"]
tierno7edb6752016-03-21 17:37:52 +01001483 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001484 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001485 if "tenant_id" in scenario:
1486 if scenario["tenant_id"] != tenant_id:
tierno5bb59dc2017-02-13 14:53:54 +01001487 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
tiernof97fd272016-07-11 14:32:37 +02001488 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
1489 scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001490 else:
1491 tenant_id=None
1492
tierno5bb59dc2017-02-13 14:53:54 +01001493 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
tierno7edb6752016-03-21 17:37:52 +01001494 for name,vnf in scenario["vnfs"].iteritems():
tiernocea279c2016-07-18 12:36:49 +02001495 where={}
1496 where_or={"tenant_id": tenant_id, 'public': "true"}
tierno7edb6752016-03-21 17:37:52 +01001497 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02001498 error_pos = "'scenario':'vnfs':'" + name + "'"
tierno7edb6752016-03-21 17:37:52 +01001499 if 'vnf_id' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01001500 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001501 where['uuid'] = vnf['vnf_id']
tierno392f2852016-05-13 12:28:55 +02001502 if 'vnf_name' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01001503 error_text += " 'vnf_name' " + vnf['vnf_name']
tiernocea279c2016-07-18 12:36:49 +02001504 where['name'] = vnf['vnf_name']
1505 if len(where) == 0:
garciadeblas71781ea2016-09-19 14:41:59 +02001506 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01001507 vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'),
tiernocea279c2016-07-18 12:36:49 +02001508 FROM='vnfs',
1509 WHERE=where,
1510 WHERE_OR=where_or,
1511 WHERE_AND_OR="AND")
tierno5bb59dc2017-02-13 14:53:54 +01001512 if len(vnf_db) == 0:
tiernof97fd272016-07-11 14:32:37 +02001513 raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
tierno5bb59dc2017-02-13 14:53:54 +01001514 elif len(vnf_db) > 1:
tiernof97fd272016-07-11 14:32:37 +02001515 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno5bb59dc2017-02-13 14:53:54 +01001516 vnf['uuid'] = vnf_db[0]['uuid']
1517 vnf['description'] = vnf_db[0]['description']
tierno7edb6752016-03-21 17:37:52 +01001518 vnf['ifaces'] = {}
tierno5bb59dc2017-02-13 14:53:54 +01001519 # get external interfaces
1520 ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
1521 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1522 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name': None} )
tierno7edb6752016-03-21 17:37:52 +01001523 for ext_iface in ext_ifaces:
tierno5bb59dc2017-02-13 14:53:54 +01001524 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']}
1525 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
tierno7edb6752016-03-21 17:37:52 +01001526
tierno5bb59dc2017-02-13 14:53:54 +01001527 # 2: Insert net_key and ip_address at every vnf interface
1528 for net_name, net in scenario["networks"].items():
1529 net_type_bridge = False
1530 net_type_data = False
tierno7edb6752016-03-21 17:37:52 +01001531 for iface_dict in net["interfaces"]:
tierno5bb59dc2017-02-13 14:53:54 +01001532 if version == "0.2":
1533 temp_dict = iface_dict
1534 ip_address = None
1535 elif version == "0.3":
1536 temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]}
1537 ip_address = iface_dict.get('ip_address', None)
1538 for vnf, iface in temp_dict.items():
tierno7edb6752016-03-21 17:37:52 +01001539 if vnf not in scenario["vnfs"]:
tierno5bb59dc2017-02-13 14:53:54 +01001540 error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
1541 net_name, vnf)
1542 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02001543 raise NfvoException(error_text, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001544 if iface not in scenario["vnfs"][vnf]['ifaces']:
tierno5bb59dc2017-02-13 14:53:54 +01001545 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
1546 .format(net_name, iface)
1547 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02001548 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001549 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
tierno5bb59dc2017-02-13 14:53:54 +01001550 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
1551 "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
1552 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02001553 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001554 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
tierno5bb59dc2017-02-13 14:53:54 +01001555 scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address
tierno7edb6752016-03-21 17:37:52 +01001556 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
tierno5bb59dc2017-02-13 14:53:54 +01001557 if iface_type == 'mgmt' or iface_type == 'bridge':
tierno7edb6752016-03-21 17:37:52 +01001558 net_type_bridge = True
1559 else:
1560 net_type_data = True
tierno5bb59dc2017-02-13 14:53:54 +01001561
tierno7edb6752016-03-21 17:37:52 +01001562 if net_type_bridge and net_type_data:
tierno5bb59dc2017-02-13 14:53:54 +01001563 error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
1564 .format(net_name)
1565 # logger.debug("nfvo.new_scenario " + error_text)
tiernof97fd272016-07-11 14:32:37 +02001566 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001567 elif net_type_bridge:
tierno5bb59dc2017-02-13 14:53:54 +01001568 type_ = 'bridge'
tierno7edb6752016-03-21 17:37:52 +01001569 else:
tierno5bb59dc2017-02-13 14:53:54 +01001570 type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp'
1571
1572 if net.get("implementation"): # for v0.3
1573 if type_ == "bridge" and net["implementation"] == "underlay":
1574 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\
1575 "'network':'{}'".format(net_name)
1576 # logger.debug(error_text)
1577 raise NfvoException(error_text, HTTP_Bad_Request)
1578 elif type_ != "bridge" and net["implementation"] == "overlay":
1579 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\
1580 "'network':'{}'".format(net_name)
1581 # logger.debug(error_text)
1582 raise NfvoException(error_text, HTTP_Bad_Request)
1583 net.pop("implementation")
1584 if "type" in net and version == "0.3": # for v0.3
1585 if type_ == "data" and net["type"] == "e-line":
1586 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\
1587 "'e-line' at 'network':'{}'".format(net_name)
1588 # logger.debug(error_text)
1589 raise NfvoException(error_text, HTTP_Bad_Request)
1590 elif type_ == "ptp" and net["type"] == "e-lan":
1591 type_ = "data"
1592
tierno7edb6752016-03-21 17:37:52 +01001593 net['type'] = type_
1594 net['name'] = net_name
1595 net['external'] = net.get('external', False)
1596
tierno5bb59dc2017-02-13 14:53:54 +01001597 # 3: insert at database
tierno7edb6752016-03-21 17:37:52 +01001598 scenario["nets"] = scenario["networks"]
1599 scenario['tenant_id'] = tenant_id
tierno5bb59dc2017-02-13 14:53:54 +01001600 scenario_id = mydb.new_scenario(scenario)
tiernof97fd272016-07-11 14:32:37 +02001601 return scenario_id
tierno7edb6752016-03-21 17:37:52 +01001602
tiernob3d36742017-03-03 23:51:05 +01001603
tierno7edb6752016-03-21 17:37:52 +01001604def edit_scenario(mydb, tenant_id, scenario_id, data):
1605 data["uuid"] = scenario_id
1606 data["tenant_id"] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02001607 c = mydb.edit_scenario( data )
1608 return c
tierno7edb6752016-03-21 17:37:52 +01001609
tiernob3d36742017-03-03 23:51:05 +01001610
tierno7edb6752016-03-21 17:37:52 +01001611def 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 +02001612 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00001613 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
1614 vims = {datacenter_id: myvim}
tierno392f2852016-05-13 12:28:55 +02001615 myvim_tenant = myvim['tenant_id']
tierno7edb6752016-03-21 17:37:52 +01001616 datacenter_name = myvim['name']
tiernoa2793912016-10-04 08:15:08 +00001617
tierno7edb6752016-03-21 17:37:52 +01001618 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02001619 try:
1620 #print "Checking that the scenario_id exists and getting the scenario dictionary"
tiernof97fd272016-07-11 14:32:37 +02001621 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id)
tiernoa2793912016-10-04 08:15:08 +00001622 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
tiernoae4a8d12016-07-08 12:30:39 +02001623 scenarioDict['datacenter_id'] = datacenter_id
1624 #print '================scenarioDict======================='
1625 #print json.dumps(scenarioDict, indent=4)
1626 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
tierno42026a02017-02-10 15:13:40 +01001627
tiernoae4a8d12016-07-08 12:30:39 +02001628 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
1629 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001630
tiernoae4a8d12016-07-08 12:30:39 +02001631 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
1632 auxNetDict['scenario'] = {}
tierno42026a02017-02-10 15:13:40 +01001633
tiernoae4a8d12016-07-08 12:30:39 +02001634 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
1635 for sce_net in scenarioDict['nets']:
1636 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
tierno42026a02017-02-10 15:13:40 +01001637
tiernoae4a8d12016-07-08 12:30:39 +02001638 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
tierno7edb6752016-03-21 17:37:52 +01001639 myNetName = myNetName[0:255] #limit length
tiernoae4a8d12016-07-08 12:30:39 +02001640 myNetType = sce_net['type']
tierno7edb6752016-03-21 17:37:52 +01001641 myNetDict = {}
1642 myNetDict["name"] = myNetName
1643 myNetDict["type"] = myNetType
1644 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02001645 myNetIPProfile = sce_net.get('ip_profile', None)
tierno7edb6752016-03-21 17:37:52 +01001646 #TODO:
tiernoae4a8d12016-07-08 12:30:39 +02001647 #We should use the dictionary as input parameter for new_network
tiernof97fd272016-07-11 14:32:37 +02001648 #print myNetDict
tiernoae4a8d12016-07-08 12:30:39 +02001649 if not sce_net["external"]:
garciadeblas9f8456e2016-09-05 05:02:59 +02001650 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02001651 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
1652 sce_net['vim_id'] = network_id
1653 auxNetDict['scenario'][sce_net['uuid']] = network_id
1654 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001655 sce_net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02001656 else:
1657 if sce_net['vim_id'] == None:
1658 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
1659 _, message = rollback(mydb, vims, rollbackList)
1660 logger.error("nfvo.start_scenario: %s", error_text)
tiernof97fd272016-07-11 14:32:37 +02001661 raise NfvoException(error_text, HTTP_Bad_Request)
tiernoae4a8d12016-07-08 12:30:39 +02001662 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
1663 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
tierno42026a02017-02-10 15:13:40 +01001664
tiernoae4a8d12016-07-08 12:30:39 +02001665 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
1666 #For each vnf net, we create it and we add it to instanceNetlist.
1667 for sce_vnf in scenarioDict['vnfs']:
1668 for net in sce_vnf['nets']:
1669 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
tierno42026a02017-02-10 15:13:40 +01001670
tiernoae4a8d12016-07-08 12:30:39 +02001671 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
1672 myNetName = myNetName[0:255] #limit length
1673 myNetType = net['type']
1674 myNetDict = {}
1675 myNetDict["name"] = myNetName
1676 myNetDict["type"] = myNetType
1677 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02001678 myNetIPProfile = net.get('ip_profile', None)
tiernoae4a8d12016-07-08 12:30:39 +02001679 #print myNetDict
1680 #TODO:
1681 #We should use the dictionary as input parameter for new_network
garciadeblas9f8456e2016-09-05 05:02:59 +02001682 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02001683 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
1684 net['vim_id'] = network_id
1685 if sce_vnf['uuid'] not in auxNetDict:
1686 auxNetDict[sce_vnf['uuid']] = {}
1687 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
1688 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001689 net["created"] = True
tierno42026a02017-02-10 15:13:40 +01001690
tiernoae4a8d12016-07-08 12:30:39 +02001691 #print "auxNetDict:"
1692 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001693
tiernoae4a8d12016-07-08 12:30:39 +02001694 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
1695 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
1696 i = 0
1697 for sce_vnf in scenarioDict['vnfs']:
1698 for vm in sce_vnf['vms']:
1699 i += 1
1700 myVMDict = {}
1701 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
tiernoae65a482016-11-24 16:20:05 +01001702 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
tiernoae4a8d12016-07-08 12:30:39 +02001703 #myVMDict['description'] = vm['description']
1704 myVMDict['description'] = myVMDict['name'][0:99]
1705 if not startvms:
1706 myVMDict['start'] = "no"
1707 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
1708 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
tierno42026a02017-02-10 15:13:40 +01001709
tiernoae4a8d12016-07-08 12:30:39 +02001710 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02001711 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno42026a02017-02-10 15:13:40 +01001712 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02001713 vm['vim_image_id'] = image_id
tierno42026a02017-02-10 15:13:40 +01001714
tiernoae4a8d12016-07-08 12:30:39 +02001715 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02001716 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tiernoae4a8d12016-07-08 12:30:39 +02001717 if flavor_dict['extended']!=None:
1718 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
tierno42026a02017-02-10 15:13:40 +01001719 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02001720 vm['vim_flavor_id'] = flavor_id
tierno42026a02017-02-10 15:13:40 +01001721
1722
tiernoae4a8d12016-07-08 12:30:39 +02001723 myVMDict['imageRef'] = vm['vim_image_id']
1724 myVMDict['flavorRef'] = vm['vim_flavor_id']
1725 myVMDict['networks'] = []
1726 for iface in vm['interfaces']:
1727 netDict = {}
1728 if iface['type']=="data":
1729 netDict['type'] = iface['model']
1730 elif "model" in iface and iface["model"]!=None:
1731 netDict['model']=iface['model']
1732 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
1733 #discover type of interface looking at flavor
1734 for numa in flavor_dict.get('extended',{}).get('numas',[]):
1735 for flavor_iface in numa.get('interfaces',[]):
1736 if flavor_iface.get('name') == iface['internal_name']:
1737 if flavor_iface['dedicated'] == 'yes':
1738 netDict['type']="PF" #passthrough
1739 elif flavor_iface['dedicated'] == 'no':
1740 netDict['type']="VF" #siov
1741 elif flavor_iface['dedicated'] == 'yes:sriov':
1742 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
1743 netDict["mac_address"] = flavor_iface.get("mac_address")
1744 break;
1745 netDict["use"]=iface['type']
1746 if netDict["use"]=="data" and not netDict.get("type"):
1747 #print "netDict", netDict
1748 #print "iface", iface
1749 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'])
1750 if flavor_dict.get('extended')==None:
tiernof97fd272016-07-11 14:32:37 +02001751 raise NfvoException(e_text + "After database migration some information is not available. \
1752 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tiernoae4a8d12016-07-08 12:30:39 +02001753 else:
tiernof97fd272016-07-11 14:32:37 +02001754 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tiernoae4a8d12016-07-08 12:30:39 +02001755 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
1756 netDict["type"]="virtual"
1757 if "vpci" in iface and iface["vpci"] is not None:
1758 netDict['vpci'] = iface['vpci']
1759 if "mac" in iface and iface["mac"] is not None:
1760 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00001761 if "port-security" in iface and iface["port-security"] is not None:
1762 netDict['port_security'] = iface['port-security']
1763 if "floating-ip" in iface and iface["floating-ip"] is not None:
1764 netDict['floating_ip'] = iface['floating-ip']
tiernoae4a8d12016-07-08 12:30:39 +02001765 netDict['name'] = iface['internal_name']
1766 if iface['net_id'] is None:
1767 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02001768 #print iface
1769 #print vnf_iface
tiernoae4a8d12016-07-08 12:30:39 +02001770 if vnf_iface['interface_id']==iface['uuid']:
1771 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
1772 break
1773 else:
1774 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
1775 #skip bridge ifaces not connected to any net
1776 #if 'net_id' not in netDict or netDict['net_id']==None:
1777 # continue
1778 myVMDict['networks'].append(netDict)
1779 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
1780 #print myVMDict['name']
1781 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
1782 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
1783 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
1784 vm_id = myvim.new_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None),
1785 myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks'])
1786 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
1787 vm['vim_id'] = vm_id
1788 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
1789 #put interface uuid back to scenario[vnfs][vms[[interfaces]
1790 for net in myVMDict['networks']:
1791 if "vim_id" in net:
1792 for iface in vm['interfaces']:
1793 if net["name"]==iface["internal_name"]:
1794 iface["vim_id"]=net["vim_id"]
1795 break
tierno42026a02017-02-10 15:13:40 +01001796
tiernoae4a8d12016-07-08 12:30:39 +02001797 logger.debug("start scenario Deployment done")
1798 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
1799 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
tiernof97fd272016-07-11 14:32:37 +02001800 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
1801 return mydb.get_instance_scenario(instance_id)
tierno42026a02017-02-10 15:13:40 +01001802
tiernof97fd272016-07-11 14:32:37 +02001803 except (db_base_Exception, vimconn.vimconnException) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001804 _, message = rollback(mydb, vims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02001805 if isinstance(e, db_base_Exception):
1806 error_text = "Exception at database"
1807 else:
1808 error_text = "Exception at VIM"
1809 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1810 #logger.error("start_scenario %s", error_text)
1811 raise NfvoException(error_text, e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001812
tiernob3d36742017-03-03 23:51:05 +01001813
tierno36c0b172017-01-12 18:32:28 +01001814def unify_cloud_config(cloud_config_preserve, cloud_config):
1815 ''' join the cloud config information into cloud_config_preserve.
1816 In case of conflict cloud_config_preserve preserves
1817 None is admited
1818 '''
1819 if not cloud_config_preserve and not cloud_config:
1820 return None
1821
1822 new_cloud_config = {"key-pairs":[], "users":[]}
1823 # key-pairs
1824 if cloud_config_preserve:
1825 for key in cloud_config_preserve.get("key-pairs", () ):
1826 if key not in new_cloud_config["key-pairs"]:
1827 new_cloud_config["key-pairs"].append(key)
1828 if cloud_config:
1829 for key in cloud_config.get("key-pairs", () ):
1830 if key not in new_cloud_config["key-pairs"]:
1831 new_cloud_config["key-pairs"].append(key)
1832 if not new_cloud_config["key-pairs"]:
1833 del new_cloud_config["key-pairs"]
1834
1835 # users
1836 if cloud_config:
1837 new_cloud_config["users"] += cloud_config.get("users", () )
1838 if cloud_config_preserve:
1839 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
tiernoa4e1a6e2016-08-31 14:19:40 +02001840 index_to_delete = []
tierno36c0b172017-01-12 18:32:28 +01001841 users = new_cloud_config.get("users", [])
tiernoa4e1a6e2016-08-31 14:19:40 +02001842 for index0 in range(0,len(users)):
1843 if index0 in index_to_delete:
1844 continue
1845 for index1 in range(index0+1,len(users)):
1846 if index1 in index_to_delete:
1847 continue
1848 if users[index0]["name"] == users[index1]["name"]:
1849 index_to_delete.append(index1)
1850 for key in users[index1].get("key-pairs",()):
tierno36c0b172017-01-12 18:32:28 +01001851 if "key-pairs" not in users[index0]:
tiernoa4e1a6e2016-08-31 14:19:40 +02001852 users[index0]["key-pairs"] = [key]
1853 elif key not in users[index0]["key-pairs"]:
1854 users[index0]["key-pairs"].append(key)
1855 index_to_delete.sort(reverse=True)
1856 for index in index_to_delete:
1857 del users[index]
tierno36c0b172017-01-12 18:32:28 +01001858 if not new_cloud_config["users"]:
1859 del new_cloud_config["users"]
1860
1861 #boot-data-drive
1862 if cloud_config and cloud_config.get("boot-data-drive") != None:
1863 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
1864 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
1865 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
1866
1867 # user-data
1868 if cloud_config and cloud_config.get("user-data") != None:
1869 new_cloud_config["user-data"] = cloud_config["user-data"]
1870 if cloud_config_preserve and cloud_config_preserve.get("user-data") != None:
1871 new_cloud_config["user-data"] = cloud_config_preserve["user-data"]
1872
1873 # config files
1874 new_cloud_config["config-files"] = []
1875 if cloud_config and cloud_config.get("config-files") != None:
1876 new_cloud_config["config-files"] += cloud_config["config-files"]
1877 if cloud_config_preserve:
1878 for file in cloud_config_preserve.get("config-files", ()):
1879 for index in range(0, len(new_cloud_config["config-files"])):
1880 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
1881 new_cloud_config["config-files"][index] = file
1882 break
1883 else:
1884 new_cloud_config["config-files"].append(file)
1885 if not new_cloud_config["config-files"]:
1886 del new_cloud_config["config-files"]
1887 return new_cloud_config
1888
1889
tierno867ffe92017-03-27 12:50:34 +02001890def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
tiernob3d36742017-03-03 23:51:05 +01001891 datacenter_id = None
1892 datacenter_name = None
1893 thread = None
tierno867ffe92017-03-27 12:50:34 +02001894 try:
1895 if datacenter_tenant_id:
1896 thread_id = datacenter_tenant_id
1897 thread = vim_threads["running"].get(datacenter_tenant_id)
tiernob3d36742017-03-03 23:51:05 +01001898 else:
tierno867ffe92017-03-27 12:50:34 +02001899 where_={"td.nfvo_tenant_id": tenant_id}
1900 if datacenter_id_name:
1901 if utils.check_valid_uuid(datacenter_id_name):
1902 datacenter_id = datacenter_id_name
1903 where_["dt.datacenter_id"] = datacenter_id
1904 else:
1905 datacenter_name = datacenter_id_name
1906 where_["d.name"] = datacenter_name
1907 if datacenter_tenant_id:
1908 where_["dt.uuid"] = datacenter_tenant_id
1909 datacenters = mydb.get_rows(
1910 SELECT=("dt.uuid as datacenter_tenant_id",),
1911 FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
1912 "join datacenters as d on d.uuid=dt.datacenter_id",
1913 WHERE=where_)
1914 if len(datacenters) > 1:
1915 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
1916 elif datacenters:
1917 thread_id = datacenters[0]["datacenter_tenant_id"]
1918 thread = vim_threads["running"].get(thread_id)
1919 if not thread:
1920 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
1921 return thread_id, thread
1922 except db_base_Exception as e:
1923 raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code)
tiernoa4e1a6e2016-08-31 14:19:40 +02001924
tiernoa2793912016-10-04 08:15:08 +00001925def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
tiernobe41e222016-09-02 15:16:13 +02001926 datacenter_id = None
1927 datacenter_name = None
1928 if datacenter_id_name:
tierno42026a02017-02-10 15:13:40 +01001929 if utils.check_valid_uuid(datacenter_id_name):
tiernobe41e222016-09-02 15:16:13 +02001930 datacenter_id = datacenter_id_name
1931 else:
1932 datacenter_name = datacenter_id_name
tiernoa2793912016-10-04 08:15:08 +00001933 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
tiernobe41e222016-09-02 15:16:13 +02001934 if len(vims) == 0:
1935 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
1936 elif len(vims)>1:
1937 #print "nfvo.datacenter_action() error. Several datacenters found"
1938 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
1939 return vims.keys()[0], vims.values()[0]
1940
tiernob3d36742017-03-03 23:51:05 +01001941
garciadeblas9f8456e2016-09-05 05:02:59 +02001942def update(d, u):
1943 '''Takes dict d and updates it with the values in dict u.'''
1944 '''It merges all depth levels'''
1945 for k, v in u.iteritems():
1946 if isinstance(v, collections.Mapping):
1947 r = update(d.get(k, {}), v)
1948 d[k] = r
1949 else:
1950 d[k] = u[k]
1951 return d
1952
tiernob3d36742017-03-03 23:51:05 +01001953
tierno7edb6752016-03-21 17:37:52 +01001954def create_instance(mydb, tenant_id, instance_dict):
tiernob3d36742017-03-03 23:51:05 +01001955 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
1956 # logger.debug("Creating instance...")
tierno7edb6752016-03-21 17:37:52 +01001957 scenario = instance_dict["scenario"]
tierno42026a02017-02-10 15:13:40 +01001958
tiernobe41e222016-09-02 15:16:13 +02001959 #find main datacenter
1960 myvims = {}
tierno867ffe92017-03-27 12:50:34 +02001961 myvim_threads_id = {}
1962 instance_tasks={}
1963 tasks_to_launch={}
tierno7edb6752016-03-21 17:37:52 +01001964 datacenter = instance_dict.get("datacenter")
tiernobe41e222016-09-02 15:16:13 +02001965 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
1966 myvims[default_datacenter_id] = vim
tierno867ffe92017-03-27 12:50:34 +02001967 myvim_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id)
1968 tasks_to_launch[myvim_threads_id[default_datacenter_id]] = []
tierno392f2852016-05-13 12:28:55 +02001969 #myvim_tenant = myvim['tenant_id']
tiernobe41e222016-09-02 15:16:13 +02001970# default_datacenter_name = vim['name']
tierno7edb6752016-03-21 17:37:52 +01001971 rollbackList=[]
tierno42026a02017-02-10 15:13:40 +01001972
tiernoae4a8d12016-07-08 12:30:39 +02001973 #print "Checking that the scenario exists and getting the scenario dictionary"
tiernobe41e222016-09-02 15:16:13 +02001974 scenarioDict = mydb.get_scenario(scenario, tenant_id, default_datacenter_id)
tierno42026a02017-02-10 15:13:40 +01001975
garciadeblasbb6a1ed2016-09-30 14:02:09 +00001976 #logger.debug(">>>>>>> Dictionaries before merging")
1977 #logger.debug(">>>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
1978 #logger.debug(">>>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
tierno42026a02017-02-10 15:13:40 +01001979
tiernobe41e222016-09-02 15:16:13 +02001980 scenarioDict['datacenter_id'] = default_datacenter_id
garciadeblas9f8456e2016-09-05 05:02:59 +02001981
tierno7edb6752016-03-21 17:37:52 +01001982 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
1983 auxNetDict['scenario'] = {}
tierno42026a02017-02-10 15:13:40 +01001984
1985 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 +01001986 instance_name = instance_dict["name"]
1987 instance_description = instance_dict.get("description")
1988 try:
tiernob3d36742017-03-03 23:51:05 +01001989 # 0 check correct parameters
tiernobe41e222016-09-02 15:16:13 +02001990 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
tiernob3d36742017-03-03 23:51:05 +01001991 found = False
tierno7edb6752016-03-21 17:37:52 +01001992 for scenario_net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +02001993 if net_name == scenario_net["name"]:
tierno7edb6752016-03-21 17:37:52 +01001994 found = True
1995 break
1996 if not found:
tiernobe41e222016-09-02 15:16:13 +02001997 raise NfvoException("Invalid scenario network name '{}' at instance:networks".format(net_name), HTTP_Bad_Request)
1998 if "sites" not in net_instance_desc:
1999 net_instance_desc["sites"] = [ {} ]
2000 site_without_datacenter_field = False
2001 for site in net_instance_desc["sites"]:
2002 if site.get("datacenter"):
2003 if site["datacenter"] not in myvims:
2004 #Add this datacenter to myvims
2005 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
2006 myvims[d] = v
tierno867ffe92017-03-27 12:50:34 +02002007 myvim_threads_id[d],_ = get_vim_thread(mydb, tenant_id, site["datacenter"])
2008 tasks_to_launch[myvim_threads_id[d]] = []
tiernob3d36742017-03-03 23:51:05 +01002009 site["datacenter"] = d #change name to id
tiernobe41e222016-09-02 15:16:13 +02002010 else:
2011 if site_without_datacenter_field:
2012 raise NfvoException("Found more than one entries without datacenter field at instance:networks:{}:sites".format(net_name), HTTP_Bad_Request)
2013 site_without_datacenter_field = True
tiernob3d36742017-03-03 23:51:05 +01002014 site["datacenter"] = default_datacenter_id #change name to id
tierno42026a02017-02-10 15:13:40 +01002015
tiernobe41e222016-09-02 15:16:13 +02002016 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01002017 found=False
2018 for scenario_vnf in scenarioDict['vnfs']:
tiernobe41e222016-09-02 15:16:13 +02002019 if vnf_name == scenario_vnf['name']:
tierno7edb6752016-03-21 17:37:52 +01002020 found = True
2021 break
2022 if not found:
tiernobe41e222016-09-02 15:16:13 +02002023 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_instance_desc), HTTP_Bad_Request)
2024 if "datacenter" in vnf_instance_desc:
tiernob3d36742017-03-03 23:51:05 +01002025 # Add this datacenter to myvims
tiernobe41e222016-09-02 15:16:13 +02002026 if vnf_instance_desc["datacenter"] not in myvims:
2027 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
2028 myvims[d] = v
tierno867ffe92017-03-27 12:50:34 +02002029 myvim_threads_id[d],_ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"])
2030 tasks_to_launch[myvim_threads_id[d]] = []
tiernoa2793912016-10-04 08:15:08 +00002031 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
garciadeblas30833382017-01-09 09:46:31 +01002032
tiernoa4e1a6e2016-08-31 14:19:40 +02002033 #0.1 parse cloud-config parameters
tierno36c0b172017-01-12 18:32:28 +01002034 cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config"))
garciadeblas9f8456e2016-09-05 05:02:59 +02002035
2036 #0.2 merge instance information into scenario
2037 #Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
2038 #However, this is not possible yet.
2039 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
2040 for scenario_net in scenarioDict['nets']:
2041 if net_name == scenario_net["name"]:
2042 if 'ip-profile' in net_instance_desc:
2043 ipprofile = net_instance_desc['ip-profile']
2044 ipprofile['subnet_address'] = ipprofile.pop('subnet-address',None)
2045 ipprofile['ip_version'] = ipprofile.pop('ip-version','IPv4')
2046 ipprofile['gateway_address'] = ipprofile.pop('gateway-address',None)
2047 ipprofile['dns_address'] = ipprofile.pop('dns-address',None)
2048 if 'dhcp' in ipprofile:
2049 ipprofile['dhcp_start_address'] = ipprofile['dhcp'].get('start-address',None)
2050 ipprofile['dhcp_enabled'] = ipprofile['dhcp'].get('enabled',True)
2051 ipprofile['dhcp_count'] = ipprofile['dhcp'].get('count',None)
2052 del ipprofile['dhcp']
garciadeblasedca7b32016-09-29 14:01:52 +00002053 if 'ip_profile' not in scenario_net:
2054 scenario_net['ip_profile'] = ipprofile
2055 else:
2056 update(scenario_net['ip_profile'],ipprofile)
tiernoe6c58ce2016-09-14 16:02:49 +02002057 for interface in net_instance_desc.get('interfaces', () ):
garciadeblas9f8456e2016-09-05 05:02:59 +02002058 if 'ip_address' in interface:
2059 for vnf in scenarioDict['vnfs']:
2060 if interface['vnf'] == vnf['name']:
2061 for vnf_interface in vnf['interfaces']:
2062 if interface['vnf_interface'] == vnf_interface['external_name']:
2063 vnf_interface['ip_address']=interface['ip_address']
2064
garciadeblasbb6a1ed2016-09-30 14:02:09 +00002065 #logger.debug(">>>>>>>> Merged dictionary")
tierno4319dad2016-09-05 12:11:11 +02002066 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 +02002067
tierno42026a02017-02-10 15:13:40 +01002068
tiernob3d36742017-03-03 23:51:05 +01002069 # 1. Creating new nets (sce_nets) in the VIM"
tierno7edb6752016-03-21 17:37:52 +01002070 for sce_net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +02002071 sce_net["vim_id_sites"]={}
tierno7edb6752016-03-21 17:37:52 +01002072 descriptor_net = instance_dict.get("networks",{}).get(sce_net["name"],{})
tiernobe41e222016-09-02 15:16:13 +02002073 net_name = descriptor_net.get("vim-network-name")
2074 auxNetDict['scenario'][sce_net['uuid']] = {}
2075
2076 sites = descriptor_net.get("sites", [ {} ])
2077 for site in sites:
2078 if site.get("datacenter"):
2079 vim = myvims[ site["datacenter"] ]
2080 datacenter_id = site["datacenter"]
tierno867ffe92017-03-27 12:50:34 +02002081 myvim_thread_id = myvim_threads_id[ site["datacenter"] ]
tierno7edb6752016-03-21 17:37:52 +01002082 else:
tiernobe41e222016-09-02 15:16:13 +02002083 vim = myvims[ default_datacenter_id ]
2084 datacenter_id = default_datacenter_id
tierno867ffe92017-03-27 12:50:34 +02002085 myvim_thread_id = myvim_threads_id[default_datacenter_id]
tiernobe41e222016-09-02 15:16:13 +02002086 net_type = sce_net['type']
2087 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} #'shared': True
2088 if sce_net["external"]:
2089 if not net_name:
tierno42026a02017-02-10 15:13:40 +01002090 net_name = sce_net["name"]
tiernobe41e222016-09-02 15:16:13 +02002091 if "netmap-use" in site or "netmap-create" in site:
2092 create_network = False
2093 lookfor_network = False
2094 if "netmap-use" in site:
2095 lookfor_network = True
2096 if utils.check_valid_uuid(site["netmap-use"]):
2097 filter_text = "scenario id '%s'" % site["netmap-use"]
2098 lookfor_filter["id"] = site["netmap-use"]
tierno42026a02017-02-10 15:13:40 +01002099 else:
tiernobe41e222016-09-02 15:16:13 +02002100 filter_text = "scenario name '%s'" % site["netmap-use"]
2101 lookfor_filter["name"] = site["netmap-use"]
2102 if "netmap-create" in site:
2103 create_network = True
2104 net_vim_name = net_name
2105 if site["netmap-create"]:
2106 net_vim_name = site["netmap-create"]
tierno42026a02017-02-10 15:13:40 +01002107
tiernobe41e222016-09-02 15:16:13 +02002108 elif sce_net['vim_id'] != None:
2109 #there is a netmap at datacenter_nets database #TODO REVISE!!!!
2110 create_network = False
2111 lookfor_network = True
2112 lookfor_filter["id"] = sce_net['vim_id']
2113 filter_text = "vim_id '%s' datacenter_netmap name '%s'. Try to reload vims with datacenter-net-update" % (sce_net['vim_id'], sce_net["name"])
2114 #look for network at datacenter and return error
2115 else:
2116 #There is not a netmap, look at datacenter for a net with this name and create if not found
2117 create_network = True
2118 lookfor_network = True
2119 lookfor_filter["name"] = sce_net["name"]
2120 net_vim_name = sce_net["name"]
2121 filter_text = "scenario name '%s'" % sce_net["name"]
tierno7edb6752016-03-21 17:37:52 +01002122 else:
tiernobe41e222016-09-02 15:16:13 +02002123 if not net_name:
2124 net_name = "%s.%s" %(instance_name, sce_net["name"])
2125 net_name = net_name[:255] #limit length
2126 net_vim_name = net_name
2127 create_network = True
2128 lookfor_network = False
tierno42026a02017-02-10 15:13:40 +01002129
tiernobe41e222016-09-02 15:16:13 +02002130 if lookfor_network:
2131 vim_nets = vim.get_network_list(filter_dict=lookfor_filter)
2132 if len(vim_nets) > 1:
2133 raise NfvoException("More than one candidate VIM network found for " + filter_text, HTTP_Bad_Request )
2134 elif len(vim_nets) == 0:
2135 if not create_network:
2136 raise NfvoException("No candidate VIM network found for " + filter_text, HTTP_Bad_Request )
2137 else:
2138 sce_net["vim_id_sites"][datacenter_id] = vim_nets[0]['id']
tiernobe41e222016-09-02 15:16:13 +02002139 auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = vim_nets[0]['id']
2140 create_network = False
2141 if create_network:
2142 #if network is not external
tiernob3d36742017-03-03 23:51:05 +01002143 task = new_task("new-net", (net_vim_name, net_type, sce_net.get('ip_profile',None)))
tierno867ffe92017-03-27 12:50:34 +02002144 task_id = task["id"]
tiernob3d36742017-03-03 23:51:05 +01002145 instance_tasks[task_id] = task
tierno867ffe92017-03-27 12:50:34 +02002146 tasks_to_launch[myvim_thread_id].append(task)
tiernob3d36742017-03-03 23:51:05 +01002147 #network_id = vim.new_network(net_vim_name, net_type, sce_net.get('ip_profile',None))
2148 sce_net["vim_id_sites"][datacenter_id] = task_id
2149 auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = task_id
2150 rollbackList.append({'what':'network', 'where':'vim', 'vim_id':datacenter_id, 'uuid':task_id})
tierno66345bc2016-09-26 11:37:55 +02002151 sce_net["created"] = True
tierno42026a02017-02-10 15:13:40 +01002152
tiernob3d36742017-03-03 23:51:05 +01002153 # 2. Creating new nets (vnf internal nets) in the VIM"
tierno7edb6752016-03-21 17:37:52 +01002154 #For each vnf net, we create it and we add it to instanceNetlist.
2155 for sce_vnf in scenarioDict['vnfs']:
2156 for net in sce_vnf['nets']:
tiernobe41e222016-09-02 15:16:13 +02002157 if sce_vnf.get("datacenter"):
2158 vim = myvims[ sce_vnf["datacenter"] ]
2159 datacenter_id = sce_vnf["datacenter"]
tierno867ffe92017-03-27 12:50:34 +02002160 myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"]]
tiernobe41e222016-09-02 15:16:13 +02002161 else:
2162 vim = myvims[ default_datacenter_id ]
2163 datacenter_id = default_datacenter_id
tierno867ffe92017-03-27 12:50:34 +02002164 myvim_thread_id = myvim_threads_id[default_datacenter_id]
tierno7edb6752016-03-21 17:37:52 +01002165 descriptor_net = instance_dict.get("vnfs",{}).get(sce_vnf["name"],{})
2166 net_name = descriptor_net.get("name")
2167 if not net_name:
2168 net_name = "%s.%s" %(instance_name, net["name"])
2169 net_name = net_name[:255] #limit length
2170 net_type = net['type']
tiernob3d36742017-03-03 23:51:05 +01002171 task = new_task("new-net", (net_name, net_type, net.get('ip_profile',None)))
tierno867ffe92017-03-27 12:50:34 +02002172 task_id = task["id"]
tiernob3d36742017-03-03 23:51:05 +01002173 instance_tasks[task_id] = task
tierno867ffe92017-03-27 12:50:34 +02002174 tasks_to_launch[myvim_thread_id].append(task)
tiernob3d36742017-03-03 23:51:05 +01002175 # network_id = vim.new_network(net_name, net_type, net.get('ip_profile',None))
2176 net['vim_id'] = task_id
tierno7edb6752016-03-21 17:37:52 +01002177 if sce_vnf['uuid'] not in auxNetDict:
2178 auxNetDict[sce_vnf['uuid']] = {}
tiernob3d36742017-03-03 23:51:05 +01002179 auxNetDict[sce_vnf['uuid']][net['uuid']] = task_id
2180 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':task_id})
tierno66345bc2016-09-26 11:37:55 +02002181 net["created"] = True
2182
tierno42026a02017-02-10 15:13:40 +01002183
tiernoae4a8d12016-07-08 12:30:39 +02002184 #print "auxNetDict:"
2185 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002186
tiernob3d36742017-03-03 23:51:05 +01002187 # 3. Creating new vm instances in the VIM
tiernoae4a8d12016-07-08 12:30:39 +02002188 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
tierno7edb6752016-03-21 17:37:52 +01002189 for sce_vnf in scenarioDict['vnfs']:
tiernobe41e222016-09-02 15:16:13 +02002190 if sce_vnf.get("datacenter"):
2191 vim = myvims[ sce_vnf["datacenter"] ]
tierno867ffe92017-03-27 12:50:34 +02002192 myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"] ]
tiernobe41e222016-09-02 15:16:13 +02002193 datacenter_id = sce_vnf["datacenter"]
2194 else:
2195 vim = myvims[ default_datacenter_id ]
tierno867ffe92017-03-27 12:50:34 +02002196 myvim_thread_id = myvim_threads_id[ default_datacenter_id ]
tiernobe41e222016-09-02 15:16:13 +02002197 datacenter_id = default_datacenter_id
2198 sce_vnf["datacenter_id"] = datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002199 i = 0
2200 for vm in sce_vnf['vms']:
2201 i += 1
2202 myVMDict = {}
tiernoae65a482016-11-24 16:20:05 +01002203 myVMDict['name'] = "{}.{}.{}".format(instance_name,sce_vnf['name'],chr(96+i))
tierno7edb6752016-03-21 17:37:52 +01002204 myVMDict['description'] = myVMDict['name'][0:99]
2205# if not startvms:
2206# myVMDict['start'] = "no"
2207 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2208 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002209 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno5e91eb82016-10-04 09:39:07 +00002210 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
tierno7edb6752016-03-21 17:37:52 +01002211 vm['vim_image_id'] = image_id
tierno42026a02017-02-10 15:13:40 +01002212
tierno7edb6752016-03-21 17:37:52 +01002213 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002214 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tierno7edb6752016-03-21 17:37:52 +01002215 if flavor_dict['extended']!=None:
2216 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
montesmoreno0c8def02016-12-22 12:16:23 +00002217 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
2218
montesmoreno0c8def02016-12-22 12:16:23 +00002219 #Obtain information for additional disks
2220 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), WHERE={'vim_id': flavor_id})
2221 if not extended_flavor_dict:
2222 raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found)
2223 return
2224
2225 #extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
2226 myVMDict['disks'] = None
2227 extended_info = extended_flavor_dict[0]['extended']
2228 if extended_info != None:
2229 extended_flavor_dict_yaml = yaml.load(extended_info)
2230 if 'disks' in extended_flavor_dict_yaml:
2231 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
2232
tierno7edb6752016-03-21 17:37:52 +01002233 vm['vim_flavor_id'] = flavor_id
tierno7edb6752016-03-21 17:37:52 +01002234 myVMDict['imageRef'] = vm['vim_image_id']
2235 myVMDict['flavorRef'] = vm['vim_flavor_id']
2236 myVMDict['networks'] = []
tiernob3d36742017-03-03 23:51:05 +01002237 task_depends = {}
tiernoa2793912016-10-04 08:15:08 +00002238 #TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
tierno7edb6752016-03-21 17:37:52 +01002239 for iface in vm['interfaces']:
2240 netDict = {}
2241 if iface['type']=="data":
2242 netDict['type'] = iface['model']
2243 elif "model" in iface and iface["model"]!=None:
2244 netDict['model']=iface['model']
2245 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2246 #discover type of interface looking at flavor
2247 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2248 for flavor_iface in numa.get('interfaces',[]):
2249 if flavor_iface.get('name') == iface['internal_name']:
2250 if flavor_iface['dedicated'] == 'yes':
2251 netDict['type']="PF" #passthrough
2252 elif flavor_iface['dedicated'] == 'no':
2253 netDict['type']="VF" #siov
2254 elif flavor_iface['dedicated'] == 'yes:sriov':
2255 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2256 netDict["mac_address"] = flavor_iface.get("mac_address")
2257 break;
2258 netDict["use"]=iface['type']
2259 if netDict["use"]=="data" and not netDict.get("type"):
2260 #print "netDict", netDict
2261 #print "iface", iface
2262 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'])
2263 if flavor_dict.get('extended')==None:
tiernoae4a8d12016-07-08 12:30:39 +02002264 raise NfvoException(e_text + "After database migration some information is not available. \
2265 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002266 else:
tiernoae4a8d12016-07-08 12:30:39 +02002267 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01002268 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2269 netDict["type"]="virtual"
2270 if "vpci" in iface and iface["vpci"] is not None:
2271 netDict['vpci'] = iface['vpci']
2272 if "mac" in iface and iface["mac"] is not None:
2273 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00002274 if "port-security" in iface and iface["port-security"] is not None:
2275 netDict['port_security'] = iface['port-security']
2276 if "floating-ip" in iface and iface["floating-ip"] is not None:
2277 netDict['floating_ip'] = iface['floating-ip']
tierno7edb6752016-03-21 17:37:52 +01002278 netDict['name'] = iface['internal_name']
2279 if iface['net_id'] is None:
2280 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02002281 #print iface
2282 #print vnf_iface
tierno7edb6752016-03-21 17:37:52 +01002283 if vnf_iface['interface_id']==iface['uuid']:
tiernobe41e222016-09-02 15:16:13 +02002284 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id]
tierno7edb6752016-03-21 17:37:52 +01002285 break
2286 else:
2287 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
tierno867ffe92017-03-27 12:50:34 +02002288 if netDict.get('net_id') and is_task_id(netDict['net_id']):
tiernob3d36742017-03-03 23:51:05 +01002289 task_depends[netDict['net_id']] = instance_tasks[netDict['net_id']]
tierno7edb6752016-03-21 17:37:52 +01002290 #skip bridge ifaces not connected to any net
2291 #if 'net_id' not in netDict or netDict['net_id']==None:
2292 # continue
2293 myVMDict['networks'].append(netDict)
tiernoae4a8d12016-07-08 12:30:39 +02002294 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2295 #print myVMDict['name']
2296 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2297 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2298 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
tierno36c0b172017-01-12 18:32:28 +01002299 if vm.get("boot_data"):
2300 cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config)
2301 else:
2302 cloud_config_vm = cloud_config
tiernob3d36742017-03-03 23:51:05 +01002303 task = new_task("new-vm", (myVMDict['name'], myVMDict['description'], myVMDict.get('start', None),
2304 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'],
2305 cloud_config_vm, myVMDict['disks']), depends=task_depends)
tierno867ffe92017-03-27 12:50:34 +02002306 instance_tasks[task["id"]] = task
2307 tasks_to_launch[myvim_thread_id].append(task)
2308 vm_id = task["id"]
tierno7edb6752016-03-21 17:37:52 +01002309 vm['vim_id'] = vm_id
2310 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2311 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2312 for net in myVMDict['networks']:
2313 if "vim_id" in net:
2314 for iface in vm['interfaces']:
2315 if net["name"]==iface["internal_name"]:
2316 iface["vim_id"]=net["vim_id"]
2317 break
tierno867ffe92017-03-27 12:50:34 +02002318 scenarioDict["datacenter2tenant"] = myvim_threads_id
tiernoa2793912016-10-04 08:15:08 +00002319 logger.debug("create_instance Deployment done scenarioDict: %s",
2320 yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) )
tiernof97fd272016-07-11 14:32:37 +02002321 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_name, instance_description, scenarioDict)
tierno867ffe92017-03-27 12:50:34 +02002322 for myvim_thread_id,task_list in tasks_to_launch.items():
2323 for task in task_list:
2324 vim_threads["running"][myvim_thread_id].insert_task(task)
2325
2326 global_instance_tasks[instance_id] = instance_tasks
2327 # Update database with those ended instance_tasks
2328 # for task in instance_tasks.values():
2329 # if task["status"] == "ok":
2330 # if task["name"] == "new-vm":
2331 # mydb.update_rows("instance_vms", UPDATE={"vim_vm_id": task["result"]},
2332 # WHERE={"vim_vm_id": task["id"]})
2333 # elif task["name"] == "new-net":
2334 # mydb.update_rows("instance_nets", UPDATE={"vim_net_id": task["result"]},
2335 # WHERE={"vim_net_id": task["id"]})
tiernof97fd272016-07-11 14:32:37 +02002336 return mydb.get_instance_scenario(instance_id)
2337 except (NfvoException, vimconn.vimconnException,db_base_Exception) as e:
tiernobe41e222016-09-02 15:16:13 +02002338 message = rollback(mydb, myvims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02002339 if isinstance(e, db_base_Exception):
2340 error_text = "database Exception"
2341 elif isinstance(e, vimconn.vimconnException):
2342 error_text = "VIM Exception"
2343 else:
2344 error_text = "Exception"
2345 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2346 #logger.error("create_instance: %s", error_text)
2347 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01002348
tiernob3d36742017-03-03 23:51:05 +01002349
tierno7edb6752016-03-21 17:37:52 +01002350def delete_instance(mydb, tenant_id, instance_id):
tiernoae4a8d12016-07-08 12:30:39 +02002351 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02002352 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +02002353 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01002354 tenant_id = instanceDict["tenant_id"]
tiernoae4a8d12016-07-08 12:30:39 +02002355 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno7edb6752016-03-21 17:37:52 +01002356
tiernoa2793912016-10-04 08:15:08 +00002357 #1. Delete from Database
tiernof97fd272016-07-11 14:32:37 +02002358 message = mydb.delete_instance_scenario(instance_id, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002359
2360 #2. delete from VIM
tiernoa2793912016-10-04 08:15:08 +00002361 error_msg = ""
tiernob3d36742017-03-03 23:51:05 +01002362 myvims = {}
2363 myvim_threads = {}
tierno7edb6752016-03-21 17:37:52 +01002364
2365 #2.1 deleting VMs
2366 #vm_fail_list=[]
2367 for sce_vnf in instanceDict['vnfs']:
tiernoa2793912016-10-04 08:15:08 +00002368 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2369 if datacenter_key not in myvims:
tiernob3d36742017-03-03 23:51:05 +01002370 try:
tierno867ffe92017-03-27 12:50:34 +02002371 _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
tiernob3d36742017-03-03 23:51:05 +01002372 except NfvoException as e:
2373 logger.error(str(e))
2374 myvim_thread = None
2375 myvim_threads[datacenter_key] = myvim_thread
tiernoa2793912016-10-04 08:15:08 +00002376 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
2377 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
2378 if len(vims) == 0:
2379 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
2380 sce_vnf["datacenter_tenant_id"]))
2381 myvims[datacenter_key] = None
2382 else:
2383 myvims[datacenter_key] = vims.values()[0]
2384 myvim = myvims[datacenter_key]
tiernob3d36742017-03-03 23:51:05 +01002385 myvim_thread = myvim_threads[datacenter_key]
tierno7edb6752016-03-21 17:37:52 +01002386 for vm in sce_vnf['vms']:
tiernoa2793912016-10-04 08:15:08 +00002387 if not myvim:
2388 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
2389 continue
tiernoae4a8d12016-07-08 12:30:39 +02002390 try:
tiernob3d36742017-03-03 23:51:05 +01002391 task=None
2392 if is_task_id(vm['vim_vm_id']):
2393 task_id = vm['vim_vm_id']
tierno867ffe92017-03-27 12:50:34 +02002394 old_task = global_instance_tasks[instance_id].get(task_id)
tiernob3d36742017-03-03 23:51:05 +01002395 if not old_task:
2396 error_msg += "\n VM was scheduled for create, but task {} is not found".format(task_id)
2397 continue
2398 with task_lock:
2399 if old_task["status"] == "enqueued":
2400 old_task["status"] = "deleted"
2401 elif old_task["status"] == "error":
2402 continue
2403 elif old_task["status"] == "processing":
tierno867ffe92017-03-27 12:50:34 +02002404 task = new_task("del-vm", (task_id, vm["interfaces"]), depends={task_id: old_task})
tiernob3d36742017-03-03 23:51:05 +01002405 else: #ok
tierno867ffe92017-03-27 12:50:34 +02002406 task = new_task("del-vm", (old_task["result"], vm["interfaces"]))
tiernob3d36742017-03-03 23:51:05 +01002407 else:
tierno867ffe92017-03-27 12:50:34 +02002408 task = new_task("del-vm", (vm['vim_vm_id'], vm["interfaces"]) )
tiernob3d36742017-03-03 23:51:05 +01002409 if task:
2410 myvim_thread.insert_task(task)
tiernoae4a8d12016-07-08 12:30:39 +02002411 except vimconn.vimconnNotFoundException as e:
tiernoa2793912016-10-04 08:15:08 +00002412 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 +02002413 logger.warn("VM instance '%s'uuid '%s', VIM id '%s', from VNF_id '%s' not found",
2414 vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'])
2415 except vimconn.vimconnException as e:
tiernoa2793912016-10-04 08:15:08 +00002416 error_msg+="\n VM VIM_id={} at datacenter={} Error: {} {}".format(vm['vim_vm_id'], sce_vnf["datacenter_id"], e.http_code, str(e))
2417 logger.error("Error %d deleting VM instance '%s'uuid '%s', VIM_id '%s', from VNF_id '%s': %s",
tiernoae4a8d12016-07-08 12:30:39 +02002418 e.http_code, vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'], str(e))
tierno42026a02017-02-10 15:13:40 +01002419
tierno7edb6752016-03-21 17:37:52 +01002420 #2.2 deleting NETS
2421 #net_fail_list=[]
2422 for net in instanceDict['nets']:
tierno66345bc2016-09-26 11:37:55 +02002423 if not net['created']:
tierno7edb6752016-03-21 17:37:52 +01002424 continue #skip not created nets
tiernoa2793912016-10-04 08:15:08 +00002425 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
2426 if datacenter_key not in myvims:
tiernob3d36742017-03-03 23:51:05 +01002427 try:
tierno867ffe92017-03-27 12:50:34 +02002428 _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
tiernob3d36742017-03-03 23:51:05 +01002429 except NfvoException as e:
2430 logger.error(str(e))
2431 myvim_thread = None
2432 myvim_threads[datacenter_key] = myvim_thread
tiernoa2793912016-10-04 08:15:08 +00002433 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
2434 datacenter_tenant_id=net["datacenter_tenant_id"])
2435 if len(vims) == 0:
2436 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
2437 myvims[datacenter_key] = None
2438 else:
2439 myvims[datacenter_key] = vims.values()[0]
2440 myvim = myvims[datacenter_key]
tiernob3d36742017-03-03 23:51:05 +01002441 myvim_thread = myvim_threads[datacenter_key]
tiernoa2793912016-10-04 08:15:08 +00002442
tierno7edb6752016-03-21 17:37:52 +01002443 if not myvim:
tiernoa2793912016-10-04 08:15:08 +00002444 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 +01002445 continue
tiernoae4a8d12016-07-08 12:30:39 +02002446 try:
tiernob3d36742017-03-03 23:51:05 +01002447 task = None
2448 if is_task_id(net['vim_net_id']):
2449 task_id = net['vim_net_id']
tierno867ffe92017-03-27 12:50:34 +02002450 old_task = global_instance_tasks[instance_id].get(task_id)
tiernob3d36742017-03-03 23:51:05 +01002451 if not old_task:
2452 error_msg += "\n NET was scheduled for create, but task {} is not found".format(task_id)
2453 continue
2454 with task_lock:
2455 if old_task["status"] == "enqueued":
2456 old_task["status"] = "deleted"
2457 elif old_task["status"] == "error":
2458 continue
2459 elif old_task["status"] == "processing":
2460 task = new_task("del-net", task_id, depends={task_id: old_task})
2461 else: # ok
2462 task = new_task("del-net", old_task["result"])
2463 else:
tierno867ffe92017-03-27 12:50:34 +02002464 task = new_task("del-net", (net['vim_net_id'], net['sdn_net_id']))
tiernob3d36742017-03-03 23:51:05 +01002465 if task:
2466 myvim_thread.insert_task(task)
tiernoae4a8d12016-07-08 12:30:39 +02002467 except vimconn.vimconnNotFoundException as e:
tiernob3d36742017-03-03 23:51:05 +01002468 error_msg += "\n NET VIM_id={} not found at datacenter={}".format(net['vim_net_id'], net["datacenter_id"])
tiernoa2793912016-10-04 08:15:08 +00002469 logger.warn("NET '%s', VIM_id '%s', from VNF_net_id '%s' not found",
tiernob3d36742017-03-03 23:51:05 +01002470 net['uuid'], net['vim_net_id'], str(net['vnf_net_id']))
tiernoae4a8d12016-07-08 12:30:39 +02002471 except vimconn.vimconnException as e:
tiernob3d36742017-03-03 23:51:05 +01002472 error_msg += "\n NET VIM_id={} at datacenter={} Error: {} {}".format(net['vim_net_id'],
2473 net["datacenter_id"],
2474 e.http_code, str(e))
tiernoa2793912016-10-04 08:15:08 +00002475 logger.error("Error %d deleting NET '%s', VIM_id '%s', from VNF_net_id '%s': %s",
tiernob3d36742017-03-03 23:51:05 +01002476 e.http_code, net['uuid'], net['vim_net_id'], str(net['vnf_net_id']), str(e))
2477 if len(error_msg) > 0:
tiernof97fd272016-07-11 14:32:37 +02002478 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 +01002479 else:
tiernof97fd272016-07-11 14:32:37 +02002480 return 'instance ' + message + ' deleted'
tierno7edb6752016-03-21 17:37:52 +01002481
tiernob3d36742017-03-03 23:51:05 +01002482
tierno7edb6752016-03-21 17:37:52 +01002483def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
2484 '''Refreshes a scenario instance. It modifies instanceDict'''
2485 '''Returns:
2486 - 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
2487 - error_msg
2488 '''
tierno867ffe92017-03-27 12:50:34 +02002489 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
2490 # #print "nfvo.refresh_instance begins"
2491 # #print json.dumps(instanceDict, indent=4)
2492 #
2493 # #print "Getting the VIM URL and the VIM tenant_id"
2494 # myvims={}
2495 #
2496 # # 1. Getting VIM vm and net list
2497 # vms_updated = [] #List of VM instance uuids in openmano that were updated
2498 # vms_notupdated=[]
2499 # vm_list = {}
2500 # for sce_vnf in instanceDict['vnfs']:
2501 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2502 # if datacenter_key not in vm_list:
2503 # vm_list[datacenter_key] = []
2504 # if datacenter_key not in myvims:
2505 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
2506 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
2507 # if len(vims) == 0:
2508 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
2509 # myvims[datacenter_key] = None
2510 # else:
2511 # myvims[datacenter_key] = vims.values()[0]
2512 # for vm in sce_vnf['vms']:
2513 # vm_list[datacenter_key].append(vm['vim_vm_id'])
2514 # vms_notupdated.append(vm["uuid"])
2515 #
2516 # nets_updated = [] #List of VM instance uuids in openmano that were updated
2517 # nets_notupdated=[]
2518 # net_list = {}
2519 # for net in instanceDict['nets']:
2520 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
2521 # if datacenter_key not in net_list:
2522 # net_list[datacenter_key] = []
2523 # if datacenter_key not in myvims:
2524 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
2525 # datacenter_tenant_id=net["datacenter_tenant_id"])
2526 # if len(vims) == 0:
2527 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
2528 # myvims[datacenter_key] = None
2529 # else:
2530 # myvims[datacenter_key] = vims.values()[0]
2531 #
2532 # net_list[datacenter_key].append(net['vim_net_id'])
2533 # nets_notupdated.append(net["uuid"])
2534 #
2535 # # 1. Getting the status of all VMs
2536 # vm_dict={}
2537 # for datacenter_key in myvims:
2538 # if not vm_list.get(datacenter_key):
2539 # continue
2540 # failed = True
2541 # failed_message=""
2542 # if not myvims[datacenter_key]:
2543 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
2544 # else:
2545 # try:
2546 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
2547 # failed = False
2548 # except vimconn.vimconnException as e:
2549 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
2550 # failed_message = str(e)
2551 # if failed:
2552 # for vm in vm_list[datacenter_key]:
2553 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
2554 #
2555 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
2556 # for sce_vnf in instanceDict['vnfs']:
2557 # for vm in sce_vnf['vms']:
2558 # vm_id = vm['vim_vm_id']
2559 # interfaces = vm_dict[vm_id].pop('interfaces', [])
2560 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
2561 # has_mgmt_iface = False
2562 # for iface in vm["interfaces"]:
2563 # if iface["type"]=="mgmt":
2564 # has_mgmt_iface = True
2565 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
2566 # vm_dict[vm_id]['status'] = "ACTIVE"
2567 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
2568 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
2569 # 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'):
2570 # vm['status'] = vm_dict[vm_id]['status']
2571 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
2572 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
2573 # # 2.1. Update in openmano DB the VMs whose status changed
2574 # try:
2575 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
2576 # vms_notupdated.remove(vm["uuid"])
2577 # if updates>0:
2578 # vms_updated.append(vm["uuid"])
2579 # except db_base_Exception as e:
2580 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
2581 # # 2.2. Update in openmano DB the interface VMs
2582 # for interface in interfaces:
2583 # #translate from vim_net_id to instance_net_id
2584 # network_id_list=[]
2585 # for net in instanceDict['nets']:
2586 # if net["vim_net_id"] == interface["vim_net_id"]:
2587 # network_id_list.append(net["uuid"])
2588 # if not network_id_list:
2589 # continue
2590 # del interface["vim_net_id"]
2591 # try:
2592 # for network_id in network_id_list:
2593 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
2594 # except db_base_Exception as e:
2595 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
2596 #
2597 # # 3. Getting the status of all nets
2598 # net_dict = {}
2599 # for datacenter_key in myvims:
2600 # if not net_list.get(datacenter_key):
2601 # continue
2602 # failed = True
2603 # failed_message = ""
2604 # if not myvims[datacenter_key]:
2605 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
2606 # else:
2607 # try:
2608 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
2609 # failed = False
2610 # except vimconn.vimconnException as e:
2611 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
2612 # failed_message = str(e)
2613 # if failed:
2614 # for net in net_list[datacenter_key]:
2615 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
2616 #
2617 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
2618 # # TODO: update nets inside a vnf
2619 # for net in instanceDict['nets']:
2620 # net_id = net['vim_net_id']
2621 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
2622 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
2623 # 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'):
2624 # net['status'] = net_dict[net_id]['status']
2625 # net['error_msg'] = net_dict[net_id].get('error_msg')
2626 # net['vim_info'] = net_dict[net_id].get('vim_info')
2627 # # 5.1. Update in openmano DB the nets whose status changed
2628 # try:
2629 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
2630 # nets_notupdated.remove(net["uuid"])
2631 # if updated>0:
2632 # nets_updated.append(net["uuid"])
2633 # except db_base_Exception as e:
2634 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
2635 #
2636 # # Returns appropriate output
2637 # #print "nfvo.refresh_instance finishes"
2638 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
2639 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
tierno7edb6752016-03-21 17:37:52 +01002640 instance_id = instanceDict['uuid']
tierno867ffe92017-03-27 12:50:34 +02002641 # if len(vms_notupdated)+len(nets_notupdated)>0:
2642 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
2643 # 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 +01002644
tiernoae4a8d12016-07-08 12:30:39 +02002645 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
tierno7edb6752016-03-21 17:37:52 +01002646
tiernob3d36742017-03-03 23:51:05 +01002647
tierno7edb6752016-03-21 17:37:52 +01002648def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
tiernoae4a8d12016-07-08 12:30:39 +02002649 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02002650 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01002651 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
2652
tiernoae4a8d12016-07-08 12:30:39 +02002653 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernof97fd272016-07-11 14:32:37 +02002654 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
2655 if len(vims) == 0:
2656 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002657 myvim = vims.values()[0]
tierno42026a02017-02-10 15:13:40 +01002658
tierno7edb6752016-03-21 17:37:52 +01002659
2660 input_vnfs = action_dict.pop("vnfs", [])
2661 input_vms = action_dict.pop("vms", [])
2662 action_over_all = True if len(input_vnfs)==0 and len (input_vms)==0 else False
2663 vm_result = {}
2664 vm_error = 0
2665 vm_ok = 0
2666 for sce_vnf in instanceDict['vnfs']:
2667 for vm in sce_vnf['vms']:
2668 if not action_over_all:
2669 if sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
2670 vm['uuid'] not in input_vms and vm['name'] not in input_vms:
2671 continue
tiernoae4a8d12016-07-08 12:30:39 +02002672 try:
2673 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
tierno7edb6752016-03-21 17:37:52 +01002674 if "console" in action_dict:
tierno20fc2a22016-08-19 17:02:35 +02002675 if not global_config["http_console_proxy"]:
2676 vm_result[ vm['uuid'] ] = {"vim_result": 200,
2677 "description": "{protocol}//{ip}:{port}/{suffix}".format(
2678 protocol=data["protocol"],
2679 ip = data["server"],
2680 port = data["port"],
2681 suffix = data["suffix"]),
2682 "name":vm['name']
2683 }
2684 vm_ok +=1
2685 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
tierno7edb6752016-03-21 17:37:52 +01002686 vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized,
2687 "description": "this console is only reachable by local interface",
2688 "name":vm['name']
2689 }
2690 vm_error+=1
tierno20fc2a22016-08-19 17:02:35 +02002691 else:
tierno7edb6752016-03-21 17:37:52 +01002692 #print "console data", data
tierno42026a02017-02-10 15:13:40 +01002693 try:
tierno20fc2a22016-08-19 17:02:35 +02002694 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
2695 vm_result[ vm['uuid'] ] = {"vim_result": 200,
2696 "description": "{protocol}//{ip}:{port}/{suffix}".format(
2697 protocol=data["protocol"],
2698 ip = global_config["http_console_host"],
2699 port = console_thread.port,
2700 suffix = data["suffix"]),
2701 "name":vm['name']
2702 }
2703 vm_ok +=1
2704 except NfvoException as e:
2705 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
2706 vm_error+=1
2707
tierno7edb6752016-03-21 17:37:52 +01002708 else:
tiernof97fd272016-07-11 14:32:37 +02002709 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
tierno7edb6752016-03-21 17:37:52 +01002710 vm_ok +=1
tiernoae4a8d12016-07-08 12:30:39 +02002711 except vimconn.vimconnException as e:
2712 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
2713 vm_error+=1
tierno7edb6752016-03-21 17:37:52 +01002714
2715 if vm_ok==0: #all goes wrong
tierno351863c2016-07-23 01:46:03 +02002716 return vm_result
tierno7edb6752016-03-21 17:37:52 +01002717 else:
tierno351863c2016-07-23 01:46:03 +02002718 return vm_result
tierno42026a02017-02-10 15:13:40 +01002719
tiernob3d36742017-03-03 23:51:05 +01002720
tierno7edb6752016-03-21 17:37:52 +01002721def create_or_use_console_proxy_thread(console_server, console_port):
2722 #look for a non-used port
2723 console_thread_key = console_server + ":" + str(console_port)
2724 if console_thread_key in global_config["console_thread"]:
2725 #global_config["console_thread"][console_thread_key].start_timeout()
tiernof97fd272016-07-11 14:32:37 +02002726 return global_config["console_thread"][console_thread_key]
tierno42026a02017-02-10 15:13:40 +01002727
tierno7edb6752016-03-21 17:37:52 +01002728 for port in global_config["console_port_iterator"]():
tierno20fc2a22016-08-19 17:02:35 +02002729 #print "create_or_use_console_proxy_thread() port:", port
tierno7edb6752016-03-21 17:37:52 +01002730 if port in global_config["console_ports"]:
2731 continue
2732 try:
2733 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
2734 clithread.start()
2735 global_config["console_thread"][console_thread_key] = clithread
2736 global_config["console_ports"][port] = console_thread_key
tiernof97fd272016-07-11 14:32:37 +02002737 return clithread
tierno7edb6752016-03-21 17:37:52 +01002738 except cli.ConsoleProxyExceptionPortUsed as e:
2739 #port used, try with onoher
2740 continue
2741 except cli.ConsoleProxyException as e:
tiernof97fd272016-07-11 14:32:37 +02002742 raise NfvoException(str(e), HTTP_Bad_Request)
2743 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002744
tiernob3d36742017-03-03 23:51:05 +01002745
tierno7edb6752016-03-21 17:37:52 +01002746def check_tenant(mydb, tenant_id):
2747 '''check that tenant exists at database'''
tiernof97fd272016-07-11 14:32:37 +02002748 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
2749 if not tenant:
2750 raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found)
2751 return
tierno7edb6752016-03-21 17:37:52 +01002752
tiernob3d36742017-03-03 23:51:05 +01002753
tierno7edb6752016-03-21 17:37:52 +01002754def new_tenant(mydb, tenant_dict):
tiernof97fd272016-07-11 14:32:37 +02002755 tenant_id = mydb.new_row("nfvo_tenants", tenant_dict, add_uuid=True)
2756 return tenant_id
tierno7edb6752016-03-21 17:37:52 +01002757
tiernob3d36742017-03-03 23:51:05 +01002758
tierno7edb6752016-03-21 17:37:52 +01002759def delete_tenant(mydb, tenant):
2760 #get nfvo_tenant info
tierno42026a02017-02-10 15:13:40 +01002761
tiernof97fd272016-07-11 14:32:37 +02002762 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
2763 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
2764 return tenant_dict['uuid'] + " " + tenant_dict["name"]
tierno7edb6752016-03-21 17:37:52 +01002765
tiernob3d36742017-03-03 23:51:05 +01002766
tierno7edb6752016-03-21 17:37:52 +01002767def new_datacenter(mydb, datacenter_descriptor):
2768 if "config" in datacenter_descriptor:
2769 datacenter_descriptor["config"]=yaml.safe_dump(datacenter_descriptor["config"],default_flow_style=True,width=256)
tierno3ae39742016-09-07 12:17:51 +02002770 #Check that datacenter-type is correct
2771 datacenter_type = datacenter_descriptor.get("type", "openvim");
2772 module_info = None
2773 try:
2774 module = "vimconn_" + datacenter_type
tierno361275f2017-04-25 16:24:34 +02002775 pkg = __import__("osm_ro." + module)
2776 vim_conn = getattr(pkg, module)
2777 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
tierno3ae39742016-09-07 12:17:51 +02002778 except (IOError, ImportError):
tierno361275f2017-04-25 16:24:34 +02002779 # if module_info and module_info[0]:
2780 # file.close(module_info[0])
tierno3ae39742016-09-07 12:17:51 +02002781 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}'.py not installed".format(datacenter_type, module), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +01002782
tiernof97fd272016-07-11 14:32:37 +02002783 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True)
2784 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002785
tiernob3d36742017-03-03 23:51:05 +01002786
tierno7edb6752016-03-21 17:37:52 +01002787def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
2788 #obtain data, check that only one exist
tiernof97fd272016-07-11 14:32:37 +02002789 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
tierno42026a02017-02-10 15:13:40 +01002790 #edit data
tiernof97fd272016-07-11 14:32:37 +02002791 datacenter_id = datacenter['uuid']
2792 where={'uuid': datacenter['uuid']}
tierno7edb6752016-03-21 17:37:52 +01002793 if "config" in datacenter_descriptor:
2794 if datacenter_descriptor['config']!=None:
2795 try:
2796 new_config_dict = datacenter_descriptor["config"]
2797 #delete null fields
2798 to_delete=[]
2799 for k in new_config_dict:
2800 if new_config_dict[k]==None:
2801 to_delete.append(k)
tierno42026a02017-02-10 15:13:40 +01002802
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01002803 config_text = datacenter.get("config")
2804 if not config_text:
2805 config_text = '{}'
2806 config_dict = yaml.load(config_text)
tierno7edb6752016-03-21 17:37:52 +01002807 config_dict.update(new_config_dict)
2808 #delete null fields
2809 for k in to_delete:
2810 del config_dict[k]
tiernof97fd272016-07-11 14:32:37 +02002811 except Exception as e:
2812 raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002813 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 +02002814 mydb.update_rows('datacenters', datacenter_descriptor, where)
2815 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002816
tiernob3d36742017-03-03 23:51:05 +01002817
tierno7edb6752016-03-21 17:37:52 +01002818def delete_datacenter(mydb, datacenter):
2819 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02002820 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
2821 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
2822 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
tierno7edb6752016-03-21 17:37:52 +01002823
tiernob3d36742017-03-03 23:51:05 +01002824
tierno8008c3a2016-10-13 15:34:28 +00002825def 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 +01002826 #get datacenter info
Vance Shipleyc24b4e22017-05-12 02:34:53 +05302827 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 +01002828 datacenter_name = myvim["name"]
tierno7edb6752016-03-21 17:37:52 +01002829
tierno42026a02017-02-10 15:13:40 +01002830 create_vim_tenant = True if not vim_tenant_id and not vim_tenant_name else False
2831
2832 # get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02002833 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01002834 if vim_tenant_name==None:
2835 vim_tenant_name=tenant_dict['name']
tierno42026a02017-02-10 15:13:40 +01002836
tierno7edb6752016-03-21 17:37:52 +01002837 #check that this association does not exist before
2838 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
tiernof97fd272016-07-11 14:32:37 +02002839 tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
2840 if len(tenants_datacenters)>0:
2841 raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002842
2843 vim_tenant_id_exist_atdb=False
2844 if not create_vim_tenant:
2845 where_={"datacenter_id": datacenter_id}
2846 if vim_tenant_id!=None:
2847 where_["vim_tenant_id"] = vim_tenant_id
2848 if vim_tenant_name!=None:
2849 where_["vim_tenant_name"] = vim_tenant_name
2850 #check if vim_tenant_id is already at database
tiernof97fd272016-07-11 14:32:37 +02002851 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
2852 if len(datacenter_tenants_dict)>=1:
tierno7edb6752016-03-21 17:37:52 +01002853 datacenter_tenants_dict = datacenter_tenants_dict[0]
2854 vim_tenant_id_exist_atdb=True
2855 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
2856 else: #result=0
2857 datacenter_tenants_dict = {}
2858 #insert at table datacenter_tenants
2859 else: #if vim_tenant_id==None:
2860 #create tenant at VIM if not provided
tiernoae4a8d12016-07-08 12:30:39 +02002861 try:
2862 vim_tenant_id = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
2863 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002864 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 +01002865 datacenter_tenants_dict = {}
2866 datacenter_tenants_dict["created"]="true"
tierno42026a02017-02-10 15:13:40 +01002867
tierno7edb6752016-03-21 17:37:52 +01002868 #fill datacenter_tenants table
2869 if not vim_tenant_id_exist_atdb:
tierno42026a02017-02-10 15:13:40 +01002870 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant_id
tierno7edb6752016-03-21 17:37:52 +01002871 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
tierno42026a02017-02-10 15:13:40 +01002872 datacenter_tenants_dict["user"] = vim_username
2873 datacenter_tenants_dict["passwd"] = vim_password
2874 datacenter_tenants_dict["datacenter_id"] = datacenter_id
tierno8008c3a2016-10-13 15:34:28 +00002875 if config:
2876 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
tiernof97fd272016-07-11 14:32:37 +02002877 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01002878 datacenter_tenants_dict["uuid"] = id_
tierno42026a02017-02-10 15:13:40 +01002879
tierno7edb6752016-03-21 17:37:52 +01002880 #fill tenants_datacenters table
tierno99314902017-04-26 13:23:09 +02002881 datacenter_tenant_id = datacenter_tenants_dict["uuid"]
2882 tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id
tiernof97fd272016-07-11 14:32:37 +02002883 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
tierno42026a02017-02-10 15:13:40 +01002884 # create thread
2885 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_dict['uuid'], datacenter_id) # reload data
2886 thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid'])
tierno99314902017-04-26 13:23:09 +02002887 new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, datacenter_name, datacenter_tenant_id,
2888 db=db, db_lock=db_lock, ovim=ovim)
tierno42026a02017-02-10 15:13:40 +01002889 new_thread.start()
tierno867ffe92017-03-27 12:50:34 +02002890 thread_id = datacenter_tenants_dict["uuid"]
tiernob3d36742017-03-03 23:51:05 +01002891 vim_threads["running"][thread_id] = new_thread
tiernof97fd272016-07-11 14:32:37 +02002892 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002893
tierno99314902017-04-26 13:23:09 +02002894
2895def edit_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=None, vim_tenant_name=None,
2896 vim_username=None, vim_password=None, config=None):
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01002897 #Obtain the data of this datacenter_tenant_id
2898 vim_data = mydb.get_rows(
2899 SELECT=("datacenter_tenants.vim_tenant_name", "datacenter_tenants.vim_tenant_id", "datacenter_tenants.user",
2900 "datacenter_tenants.passwd", "datacenter_tenants.config"),
2901 FROM="datacenter_tenants JOIN tenants_datacenters ON datacenter_tenants.uuid=tenants_datacenters.datacenter_tenant_id",
2902 WHERE={"tenants_datacenters.nfvo_tenant_id": nfvo_tenant,
2903 "tenants_datacenters.datacenter_id": datacenter_id})
2904
2905 logger.debug(str(vim_data))
2906 if len(vim_data) < 1:
2907 raise NfvoException("Datacenter {} is not attached for tenant {}".format(datacenter_id, nfvo_tenant), HTTP_Conflict)
2908
2909 v = vim_data[0]
2910 if v['config']:
2911 v['config'] = yaml.load(v['config'])
2912
2913 if vim_tenant_id:
2914 v['vim_tenant_id'] = vim_tenant_id
2915 if vim_tenant_name:
2916 v['vim_tenant_name'] = vim_tenant_name
2917 if vim_username:
2918 v['user'] = vim_username
2919 if vim_password:
2920 v['passwd'] = vim_password
2921 if config:
2922 if not v['config']:
2923 v['config'] = {}
2924 v['config'].update(config)
2925
2926 logger.debug(str(v))
2927 deassociate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'])
2928 associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'], vim_tenant_name=v['vim_tenant_name'],
2929 vim_username=v['user'], vim_password=v['passwd'], config=v['config'])
2930
2931 return datacenter_id
tiernob3d36742017-03-03 23:51:05 +01002932
tierno7edb6752016-03-21 17:37:52 +01002933def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=None):
2934 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002935 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002936
2937 #get nfvo_tenant info
2938 if not tenant_id or tenant_id=="any":
2939 tenant_uuid = None
2940 else:
tiernof97fd272016-07-11 14:32:37 +02002941 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002942 tenant_uuid = tenant_dict['uuid']
2943
2944 #check that this association exist before
2945 tenants_datacenter_dict={"datacenter_id":datacenter_id }
2946 if tenant_uuid:
2947 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
tiernof97fd272016-07-11 14:32:37 +02002948 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
2949 if len(tenant_datacenter_list)==0 and tenant_uuid:
2950 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002951
2952 #delete this association
tiernof97fd272016-07-11 14:32:37 +02002953 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
tierno7edb6752016-03-21 17:37:52 +01002954
2955 #get vim_tenant info and deletes
2956 warning=''
2957 for tenant_datacenter_item in tenant_datacenter_list:
tiernof97fd272016-07-11 14:32:37 +02002958 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
2959 #try to delete vim:tenant
2960 try:
2961 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
2962 if vim_tenant_dict['created']=='true':
tierno7edb6752016-03-21 17:37:52 +01002963 #delete tenant at VIM if created by NFVO
tierno42026a02017-02-10 15:13:40 +01002964 try:
tiernoae4a8d12016-07-08 12:30:39 +02002965 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
2966 except vimconn.vimconnException as e:
2967 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
2968 logger.warn(warning)
tiernof97fd272016-07-11 14:32:37 +02002969 except db_base_Exception as e:
2970 logger.error("Cannot delete datacenter_tenants " + str(e))
tierno42026a02017-02-10 15:13:40 +01002971 pass # the error will be caused because dependencies, vim_tenant can not be deleted
tierno867ffe92017-03-27 12:50:34 +02002972 thread_id = tenant_datacenter_item["datacenter_tenant_id"]
tierno42026a02017-02-10 15:13:40 +01002973 thread = vim_threads["running"][thread_id]
tierno867ffe92017-03-27 12:50:34 +02002974 thread.insert_task(new_task("exit", None))
tierno42026a02017-02-10 15:13:40 +01002975 vim_threads["deleting"][thread_id] = thread
tiernof97fd272016-07-11 14:32:37 +02002976 return "datacenter {} detached. {}".format(datacenter_id, warning)
tierno7edb6752016-03-21 17:37:52 +01002977
tiernob3d36742017-03-03 23:51:05 +01002978
tierno7edb6752016-03-21 17:37:52 +01002979def datacenter_action(mydb, tenant_id, datacenter, action_dict):
2980 #DEPRECATED
tierno42026a02017-02-10 15:13:40 +01002981 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002982 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002983
2984 if 'net-update' in action_dict:
tiernoae4a8d12016-07-08 12:30:39 +02002985 try:
tiernof97fd272016-07-11 14:32:37 +02002986 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
tiernoae4a8d12016-07-08 12:30:39 +02002987 #print content
2988 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002989 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
2990 raise NfvoException(str(e), HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01002991 #update nets Change from VIM format to NFVO format
2992 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02002993 for net in nets:
tierno7edb6752016-03-21 17:37:52 +01002994 net_nfvo={'datacenter_id': datacenter_id}
2995 net_nfvo['name'] = net['name']
2996 #net_nfvo['description']= net['name']
2997 net_nfvo['vim_net_id'] = net['id']
2998 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
2999 net_nfvo['shared'] = net['shared']
3000 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
3001 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02003002 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
3003 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
3004 return inserted
tierno7edb6752016-03-21 17:37:52 +01003005 elif 'net-edit' in action_dict:
3006 net = action_dict['net-edit'].pop('net')
tierno42fcc3b2016-07-06 17:20:40 +02003007 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01003008 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
tierno7edb6752016-03-21 17:37:52 +01003009 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02003010 return result
tierno7edb6752016-03-21 17:37:52 +01003011 elif 'net-delete' in action_dict:
3012 net = action_dict['net-deelte'].get('net')
tierno42fcc3b2016-07-06 17:20:40 +02003013 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01003014 result = mydb.delete_row(FROM='datacenter_nets',
tierno7edb6752016-03-21 17:37:52 +01003015 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02003016 return result
tierno7edb6752016-03-21 17:37:52 +01003017
3018 else:
tiernof97fd272016-07-11 14:32:37 +02003019 raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01003020
tiernob3d36742017-03-03 23:51:05 +01003021
tierno7edb6752016-03-21 17:37:52 +01003022def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
3023 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00003024 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01003025
tierno42fcc3b2016-07-06 17:20:40 +02003026 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
tierno42026a02017-02-10 15:13:40 +01003027 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
tierno7edb6752016-03-21 17:37:52 +01003028 WHERE={'datacenter_id':datacenter_id, what: netmap})
tiernof97fd272016-07-11 14:32:37 +02003029 return result
tierno7edb6752016-03-21 17:37:52 +01003030
tiernob3d36742017-03-03 23:51:05 +01003031
tierno7edb6752016-03-21 17:37:52 +01003032def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
3033 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00003034 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01003035 filter_dict={}
3036 if action_dict:
3037 action_dict = action_dict["netmap"]
3038 if 'vim_id' in action_dict:
3039 filter_dict["id"] = action_dict['vim_id']
3040 if 'vim_name' in action_dict:
3041 filter_dict["name"] = action_dict['vim_name']
3042 else:
3043 filter_dict["shared"] = True
tierno42026a02017-02-10 15:13:40 +01003044
tiernoae4a8d12016-07-08 12:30:39 +02003045 try:
tiernof97fd272016-07-11 14:32:37 +02003046 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02003047 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02003048 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
3049 raise NfvoException(str(e), HTTP_Internal_Server_Error)
3050 if len(vim_nets)>1 and action_dict:
3051 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict)
3052 elif len(vim_nets)==0: # and action_dict:
3053 raise NfvoException("Not found a network at VIM with " + str(filter_dict), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01003054 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02003055 for net in vim_nets:
tierno7edb6752016-03-21 17:37:52 +01003056 net_nfvo={'datacenter_id': datacenter_id}
3057 if action_dict and "name" in action_dict:
3058 net_nfvo['name'] = action_dict['name']
3059 else:
3060 net_nfvo['name'] = net['name']
3061 #net_nfvo['description']= net['name']
3062 net_nfvo['vim_net_id'] = net['id']
3063 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
3064 net_nfvo['shared'] = net['shared']
3065 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
tiernof97fd272016-07-11 14:32:37 +02003066 try:
3067 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01003068 net_nfvo["status"] = "OK"
tiernof97fd272016-07-11 14:32:37 +02003069 net_nfvo["uuid"] = net_id
3070 except db_base_Exception as e:
3071 if action_dict:
3072 raise
3073 else:
3074 net_nfvo["status"] = "FAIL: " + str(e)
tierno42026a02017-02-10 15:13:40 +01003075 net_list.append(net_nfvo)
3076 return net_list
tierno7edb6752016-03-21 17:37:52 +01003077
tiernob3d36742017-03-03 23:51:05 +01003078
tierno7edb6752016-03-21 17:37:52 +01003079def vim_action_get(mydb, tenant_id, datacenter, item, name):
3080 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00003081 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01003082 filter_dict={}
3083 if name:
tierno42fcc3b2016-07-06 17:20:40 +02003084 if utils.check_valid_uuid(name):
tierno7edb6752016-03-21 17:37:52 +01003085 filter_dict["id"] = name
3086 else:
3087 filter_dict["name"] = name
tiernoae4a8d12016-07-08 12:30:39 +02003088 try:
3089 if item=="networks":
3090 #filter_dict['tenant_id'] = myvim['tenant_id']
3091 content = myvim.get_network_list(filter_dict=filter_dict)
3092 elif item=="tenants":
3093 content = myvim.get_tenant_list(filter_dict=filter_dict)
tierno4540ea52017-01-18 17:44:32 +01003094 elif item == "images":
3095 content = myvim.get_image_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02003096 else:
tiernof97fd272016-07-11 14:32:37 +02003097 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernobe41e222016-09-02 15:16:13 +02003098 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
tiernoae4a8d12016-07-08 12:30:39 +02003099 if name and len(content)==1:
tiernof97fd272016-07-11 14:32:37 +02003100 return {item[:-1]: content[0]}
tiernoae4a8d12016-07-08 12:30:39 +02003101 elif name and len(content)==0:
tiernof97fd272016-07-11 14:32:37 +02003102 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 +02003103 datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02003104 else:
tiernof97fd272016-07-11 14:32:37 +02003105 return {item: content}
tiernoae4a8d12016-07-08 12:30:39 +02003106 except vimconn.vimconnException as e:
3107 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
tiernof97fd272016-07-11 14:32:37 +02003108 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
tierno42026a02017-02-10 15:13:40 +01003109
tiernob3d36742017-03-03 23:51:05 +01003110
tierno7edb6752016-03-21 17:37:52 +01003111def vim_action_delete(mydb, tenant_id, datacenter, item, name):
3112 #get datacenter info
tierno392f2852016-05-13 12:28:55 +02003113 if tenant_id == "any":
3114 tenant_id=None
3115
tiernoa2793912016-10-04 08:15:08 +00003116 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno392f2852016-05-13 12:28:55 +02003117 #get uuid name
tiernof97fd272016-07-11 14:32:37 +02003118 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
3119 logger.debug("vim_action_delete vim response: " + str(content))
tierno392f2852016-05-13 12:28:55 +02003120 items = content.values()[0]
3121 if type(items)==list and len(items)==0:
tiernof97fd272016-07-11 14:32:37 +02003122 raise NfvoException("Not found " + item, HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02003123 elif type(items)==list and len(items)>1:
tiernof97fd272016-07-11 14:32:37 +02003124 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02003125 else: # it is a dict
3126 item_id = items["id"]
3127 item_name = str(items.get("name"))
tierno42026a02017-02-10 15:13:40 +01003128
tiernoae4a8d12016-07-08 12:30:39 +02003129 try:
3130 if item=="networks":
3131 content = myvim.delete_network(item_id)
3132 elif item=="tenants":
3133 content = myvim.delete_tenant(item_id)
tierno4540ea52017-01-18 17:44:32 +01003134 elif item == "images":
3135 content = myvim.delete_image(item_id)
tiernoae4a8d12016-07-08 12:30:39 +02003136 else:
tierno42026a02017-02-10 15:13:40 +01003137 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02003138 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02003139 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
3140 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02003141
tiernof97fd272016-07-11 14:32:37 +02003142 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
tierno42026a02017-02-10 15:13:40 +01003143
tiernob3d36742017-03-03 23:51:05 +01003144
tierno7edb6752016-03-21 17:37:52 +01003145def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
3146 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00003147 logger.debug("vim_action_create descriptor %s", str(descriptor))
tierno392f2852016-05-13 12:28:55 +02003148 if tenant_id == "any":
3149 tenant_id=None
tiernoa2793912016-10-04 08:15:08 +00003150 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02003151 try:
3152 if item=="networks":
3153 net = descriptor["network"]
3154 net_name = net.pop("name")
3155 net_type = net.pop("type", "bridge")
garciadeblas9f8456e2016-09-05 05:02:59 +02003156 net_public = net.pop("shared", False)
3157 net_ipprofile = net.pop("ip_profile", None)
tiernoa7d34d02017-02-23 14:42:07 +01003158 net_vlan = net.pop("vlan", None)
3159 content = myvim.new_network(net_name, net_type, net_ipprofile, shared=net_public, vlan=net_vlan) #, **net)
tiernoae4a8d12016-07-08 12:30:39 +02003160 elif item=="tenants":
3161 tenant = descriptor["tenant"]
3162 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
3163 else:
tierno42026a02017-02-10 15:13:40 +01003164 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02003165 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02003166 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02003167
tierno7edb6752016-03-21 17:37:52 +01003168 return vim_action_get(mydb, tenant_id, datacenter, item, content)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003169
3170def sdn_controller_create(mydb, tenant_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003171 data = ovim.new_of_controller(sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003172 logger.debug('New SDN controller created with uuid {}'.format(data))
3173 return data
3174
3175def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003176 data = ovim.edit_of_controller(controller_id, sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003177 msg = 'SDN controller {} updated'.format(data)
3178 logger.debug(msg)
3179 return msg
3180
3181def sdn_controller_list(mydb, tenant_id, controller_id=None):
3182 if controller_id == None:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003183 data = ovim.get_of_controllers()
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003184 else:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003185 data = ovim.show_of_controller(controller_id)
3186
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003187 msg = 'SDN controller list:\n {}'.format(data)
3188 logger.debug(msg)
3189 return data
3190
3191def sdn_controller_delete(mydb, tenant_id, controller_id):
3192 select_ = ('uuid', 'config')
3193 datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_)
3194 for datacenter in datacenters:
3195 if datacenter['config']:
3196 config = yaml.load(datacenter['config'])
3197 if 'sdn-controller' in config and config['sdn-controller'] == controller_id:
3198 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), HTTP_Conflict)
3199
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003200 data = ovim.delete_of_controller(controller_id)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003201 msg = 'SDN controller {} deleted'.format(data)
3202 logger.debug(msg)
3203 return msg
3204
3205def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping):
3206 controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id})
3207 if len(controller) < 1:
3208 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), HTTP_Not_Found)
3209
3210 try:
3211 sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"]
3212 except:
3213 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), HTTP_Bad_Request)
3214
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003215 sdn_controller = ovim.show_of_controller(sdn_controller_id)
3216 switch_dpid = sdn_controller["dpid"]
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003217
3218 maps = list()
3219 for compute_node in sdn_port_mapping:
3220 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
3221 element = dict()
3222 element["compute_node"] = compute_node["compute_node"]
3223 for port in compute_node["ports"]:
3224 element["pci"] = port.get("pci")
3225 element["switch_port"] = port.get("switch_port")
3226 element["switch_mac"] = port.get("switch_mac")
3227 if not element["pci"] or not (element["switch_port"] or element["switch_mac"]):
3228 raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'"
3229 " or 'switch_mac'", HTTP_Bad_Request)
3230 maps.append(dict(element))
3231
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003232 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 +01003233
3234def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003235 maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id})
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003236
3237 result = {
3238 "sdn-controller": None,
3239 "datacenter-id": datacenter_id,
3240 "dpid": None,
3241 "ports_mapping": list()
3242 }
3243
3244 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id)
3245 if datacenter['config']:
3246 config = yaml.load(datacenter['config'])
3247 if 'sdn-controller' in config:
3248 controller_id = config['sdn-controller']
3249 sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id)
3250 result["sdn-controller"] = controller_id
3251 result["dpid"] = sdn_controller["dpid"]
3252
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02003253 if result["sdn-controller"] == None or result["dpid"] == None:
3254 raise NfvoException("Not all SDN controller information for datacenter {} could be found: {}".format(datacenter_id, result),
3255 HTTP_Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003256
3257 if len(maps) == 0:
3258 return result
3259
3260 ports_correspondence_dict = dict()
3261 for link in maps:
3262 if result["sdn-controller"] != link["ofc_id"]:
3263 raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error)
3264 if result["dpid"] != link["switch_dpid"]:
3265 raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error)
3266 element = dict()
3267 element["pci"] = link["pci"]
3268 if link["switch_port"]:
3269 element["switch_port"] = link["switch_port"]
3270 if link["switch_mac"]:
3271 element["switch_mac"] = link["switch_mac"]
3272
3273 if not link["compute_node"] in ports_correspondence_dict:
3274 content = dict()
3275 content["compute_node"] = link["compute_node"]
3276 content["ports"] = list()
3277 ports_correspondence_dict[link["compute_node"]] = content
3278
3279 ports_correspondence_dict[link["compute_node"]]["ports"].append(element)
3280
3281 for key in sorted(ports_correspondence_dict):
3282 result["ports_mapping"].append(ports_correspondence_dict[key])
3283
3284 return result
3285
3286def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id):
tierno639520f2017-04-05 19:55:36 +02003287 return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id})