blob: 7a3e959e88751fa588bc9cbe14238881af81fd5f [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
tierno66eba6e2017-11-10 17:09:18 +010041import math
tierno8e690322017-08-10 15:58:50 +020042from uuid import uuid4
tiernof97fd272016-07-11 14:32:37 +020043from db_base import db_base_Exception
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +010044
tiernob3d36742017-03-03 23:51:05 +010045import nfvo_db
46from threading import Lock
tierno868220c2017-09-26 00:11:05 +020047import time as t
tierno01b3e172017-04-21 10:52:34 +020048from lib_osm_openvim import ovim as ovim_module
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +020049from lib_osm_openvim.ovim import ovimException
gcalvinoe580c7d2017-09-22 14:09:51 +020050from Crypto.PublicKey import RSA
tierno7edb6752016-03-21 17:37:52 +010051
tiernof1ba57e2017-09-07 12:23:19 +020052import osm_im.vnfd as vnfd_catalog
53import osm_im.nsd as nsd_catalog
tiernof1ba57e2017-09-07 12:23:19 +020054from pyangbind.lib.serialise import pybindJSONDecoder
55from itertools import chain
56
tierno7edb6752016-03-21 17:37:52 +010057global global_config
58global vimconn_imported
tierno73ad9e42016-09-12 18:11:11 +020059global logger
montesmoreno0c8def02016-12-22 12:16:23 +000060global default_volume_size
61default_volume_size = '5' #size in GB
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +010062global ovim
63ovim = None
tiernoc5651792017-03-27 10:50:43 +020064global_config = None
tiernoae4a8d12016-07-08 12:30:39 +020065
tierno42026a02017-02-10 15:13:40 +010066vimconn_imported = {} # dictionary with VIM type as key, loaded module as value
67vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
tiernob3d36742017-03-03 23:51:05 +010068vim_persistent_info = {}
tierno73ad9e42016-09-12 18:11:11 +020069logger = logging.getLogger('openmano.nfvo')
tiernob3d36742017-03-03 23:51:05 +010070task_lock = Lock()
tiernob3d36742017-03-03 23:51:05 +010071last_task_id = 0.0
tierno868220c2017-09-26 00:11:05 +020072db = None
73db_lock = Lock()
tierno7edb6752016-03-21 17:37:52 +010074
75class NfvoException(Exception):
tiernoae4a8d12016-07-08 12:30:39 +020076 def __init__(self, message, http_code):
77 self.http_code = http_code
78 Exception.__init__(self, message)
tierno7edb6752016-03-21 17:37:52 +010079
80
tiernob3d36742017-03-03 23:51:05 +010081def get_task_id():
82 global last_task_id
tierno868220c2017-09-26 00:11:05 +020083 task_id = t.time()
tiernob3d36742017-03-03 23:51:05 +010084 if task_id <= last_task_id:
85 task_id = last_task_id + 0.000001
86 last_task_id = task_id
tierno868220c2017-09-26 00:11:05 +020087 return "ACTION-{:.6f}".format(task_id)
88 # return (t.strftime("%Y%m%dT%H%M%S.{}%Z", t.localtime(task_id))).format(int((task_id % 1)*1e6))
tiernob3d36742017-03-03 23:51:05 +010089
90
tierno867ffe92017-03-27 12:50:34 +020091def new_task(name, params, depends=None):
tierno868220c2017-09-26 00:11:05 +020092 """Deprected!!!"""
tiernob3d36742017-03-03 23:51:05 +010093 task_id = get_task_id()
94 task = {"status": "enqueued", "id": task_id, "name": name, "params": params}
95 if depends:
96 task["depends"] = depends
tiernob3d36742017-03-03 23:51:05 +010097 return task
98
99
100def is_task_id(id):
tierno868220c2017-09-26 00:11:05 +0200101 return True if id[:5] == "TASK-" else False
tiernob3d36742017-03-03 23:51:05 +0100102
103
tierno42026a02017-02-10 15:13:40 +0100104def get_non_used_vim_name(datacenter_name, datacenter_id, tenant_name, tenant_id):
105 name = datacenter_name[:16]
106 if name not in vim_threads["names"]:
107 vim_threads["names"].append(name)
108 return name
tiernob3d36742017-03-03 23:51:05 +0100109 name = datacenter_name[:16] + "." + tenant_name[:16]
tierno42026a02017-02-10 15:13:40 +0100110 if name not in vim_threads["names"]:
111 vim_threads["names"].append(name)
112 return name
113 name = datacenter_id + "-" + tenant_id
114 vim_threads["names"].append(name)
115 return name
116
117
118def start_service(mydb):
tiernob3d36742017-03-03 23:51:05 +0100119 global db, global_config
120 db = nfvo_db.nfvo_db()
121 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 +0100122 global ovim
123
124 # Initialize openvim for SDN control
125 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
126 # TODO: review ovim.py to delete not needed configuration
127 ovim_configuration = {
tierno639520f2017-04-05 19:55:36 +0200128 'logger_name': 'openmano.ovim',
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100129 'network_vlan_range_start': 1000,
130 'network_vlan_range_end': 4096,
tierno639520f2017-04-05 19:55:36 +0200131 'db_name': global_config["db_ovim_name"],
132 'db_host': global_config["db_ovim_host"],
133 'db_user': global_config["db_ovim_user"],
134 'db_passwd': global_config["db_ovim_passwd"],
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100135 'bridge_ifaces': {},
136 'mode': 'normal',
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100137 'network_type': 'bridge',
138 #TODO: log_level_of should not be needed. To be modified in ovim
139 'log_level_of': 'DEBUG'
140 }
tierno42026a02017-02-10 15:13:40 +0100141 try:
tierno3fcfdb72017-10-24 07:48:24 +0200142 # starts ovim library
tierno46df9672017-05-26 13:12:21 +0200143 ovim = ovim_module.ovim(ovim_configuration)
144 ovim.start_service()
145
tierno3fcfdb72017-10-24 07:48:24 +0200146 #delete old unneeded vim_actions
147 clean_db(mydb)
148
149 # starts vim_threads
tierno46df9672017-05-26 13:12:21 +0200150 from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
151 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
152 select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
153 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
154 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
155 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
tierno42026a02017-02-10 15:13:40 +0100156 vims = mydb.get_rows(FROM=from_, SELECT=select_)
157 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200158 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
159 'datacenter_id': vim.get('datacenter_id')}
tierno42026a02017-02-10 15:13:40 +0100160 if vim["config"]:
161 extra.update(yaml.load(vim["config"]))
162 if vim.get('dt_config'):
163 extra.update(yaml.load(vim["dt_config"]))
164 if vim["type"] not in vimconn_imported:
165 module_info=None
166 try:
167 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200168 pkg = __import__("osm_ro." + module)
169 vim_conn = getattr(pkg, module)
170 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
171 # vim_conn = imp.load_module(vim["type"], *module_info)
tierno42026a02017-02-10 15:13:40 +0100172 vimconn_imported[vim["type"]] = vim_conn
173 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200174 # if module_info and module_info[0]:
175 # file.close(module_info[0])
tiernocdee8cc2017-04-25 13:42:06 +0200176 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
tiernob3d36742017-03-03 23:51:05 +0100177 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100178
tierno867ffe92017-03-27 12:50:34 +0200179 thread_id = vim['datacenter_tenant_id']
tiernob3d36742017-03-03 23:51:05 +0100180 vim_persistent_info[thread_id] = {}
tierno42026a02017-02-10 15:13:40 +0100181 try:
182 #if not tenant:
183 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
184 myvim = vimconn_imported[ vim["type"] ].vimconnector(
tiernob3d36742017-03-03 23:51:05 +0100185 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
186 tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'],
187 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
188 user=vim['user'], passwd=vim['passwd'],
189 config=extra, persistent_info=vim_persistent_info[thread_id]
190 )
tierno9c22f2d2017-10-09 16:23:55 +0200191 except vimconn.vimconnException as e:
192 myvim = e
193 logger.error("Cannot launch thread for VIM {} '{}': {}".format(vim['datacenter_name'],
194 vim['datacenter_id'], e))
tierno42026a02017-02-10 15:13:40 +0100195 except Exception as e:
tierno46df9672017-05-26 13:12:21 +0200196 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e),
197 HTTP_Internal_Server_Error)
198 thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'],
199 vim['vim_tenant_id'])
tiernob3d36742017-03-03 23:51:05 +0100200 new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, vim['datacenter_name'],
tierno867ffe92017-03-27 12:50:34 +0200201 vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim)
tierno42026a02017-02-10 15:13:40 +0100202 new_thread.start()
tierno42026a02017-02-10 15:13:40 +0100203 vim_threads["running"][thread_id] = new_thread
204 except db_base_Exception as e:
205 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
tierno46df9672017-05-26 13:12:21 +0200206 except ovim_module.ovimException as e:
207 message = str(e)
208 if message[:22] == "DATABASE wrong version":
209 message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
210 "at host {dbhost}".format(
211 msg=message[22:-3], dbname=global_config["db_ovim_name"],
212 dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"],
213 ver=message[-3:-1], dbhost=global_config["db_ovim_host"])
214 raise NfvoException(message, HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100215
tierno867ffe92017-03-27 12:50:34 +0200216
tierno42026a02017-02-10 15:13:40 +0100217def stop_service():
tiernoc5651792017-03-27 10:50:43 +0200218 global ovim, global_config
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100219 if ovim:
220 ovim.stop_service()
tierno42026a02017-02-10 15:13:40 +0100221 for thread_id,thread in vim_threads["running"].items():
tierno868220c2017-09-26 00:11:05 +0200222 thread.insert_task("exit")
tierno42026a02017-02-10 15:13:40 +0100223 vim_threads["deleting"][thread_id] = thread
tiernob3d36742017-03-03 23:51:05 +0100224 vim_threads["running"] = {}
tiernoc5651792017-03-27 10:50:43 +0200225 if global_config and global_config.get("console_thread"):
226 for thread in global_config["console_thread"]:
227 thread.terminate = True
tiernob3d36742017-03-03 23:51:05 +0100228
tierno6ddeded2017-05-16 15:40:26 +0200229def get_version():
230 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"],
231 global_config["version_date"] ))
232
tierno3fcfdb72017-10-24 07:48:24 +0200233def clean_db(mydb):
234 """
235 Clean unused or old entries at database to avoid unlimited growing
236 :param mydb: database connector
237 :return: None
238 """
239 # get and delete unused vim_actions: all elements deleted, one week before, instance not present
240 now = t.time()-3600*24*7
241 instance_action_id = None
242 nb_deleted = 0
243 while True:
244 actions_to_delete = mydb.get_rows(
245 SELECT=("item", "item_id", "instance_action_id"),
246 FROM="vim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid "
247 "left join instance_scenarios as i on ia.instance_id=i.uuid",
248 WHERE={"va.action": "DELETE", "va.modified_at<": now, "i.uuid": None,
249 "va.status": ("DONE", "SUPERSEDED")},
250 LIMIT=100
251 )
252 for to_delete in actions_to_delete:
253 mydb.delete_row(FROM="vim_actions", WHERE=to_delete)
254 if instance_action_id != to_delete["instance_action_id"]:
255 instance_action_id = to_delete["instance_action_id"]
256 mydb.delete_row(FROM="instance_actions", WHERE={"uuid": instance_action_id})
257 nb_deleted += len(actions_to_delete)
258 if len(actions_to_delete) < 100:
259 break
260 if nb_deleted:
261 logger.debug("Removed {} unused vim_actions".format(nb_deleted))
262
263
tierno42026a02017-02-10 15:13:40 +0100264
tierno7edb6752016-03-21 17:37:52 +0100265def get_flavorlist(mydb, vnf_id, nfvo_tenant=None):
266 '''Obtain flavorList
267 return result, content:
268 <0, error_text upon error
269 nb_records, flavor_list on success
270 '''
271 WHERE_dict={}
272 WHERE_dict['vnf_id'] = vnf_id
273 if nfvo_tenant is not None:
274 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
tierno42026a02017-02-10 15:13:40 +0100275
tierno7edb6752016-03-21 17:37:52 +0100276 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
277 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
tiernof97fd272016-07-11 14:32:37 +0200278 flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict )
279 #print "get_flavor_list result:", result
280 #print "get_flavor_list content:", content
tierno7edb6752016-03-21 17:37:52 +0100281 flavorList=[]
tiernof97fd272016-07-11 14:32:37 +0200282 for flavor in flavors:
tierno7edb6752016-03-21 17:37:52 +0100283 flavorList.append(flavor['flavor_id'])
tiernof97fd272016-07-11 14:32:37 +0200284 return flavorList
tierno7edb6752016-03-21 17:37:52 +0100285
tiernob3d36742017-03-03 23:51:05 +0100286
tierno7edb6752016-03-21 17:37:52 +0100287def get_imagelist(mydb, vnf_id, nfvo_tenant=None):
tierno16e3dd42018-04-24 12:52:40 +0200288 """
289 Get used images of all vms belonging to this VNFD
290 :param mydb: database conector
291 :param vnf_id: vnfd uuid
292 :param nfvo_tenant: tenant, not used
293 :return: The list of image uuid used
294 """
295 image_list = []
296 vms = mydb.get_rows(SELECT=('image_id','image_list'), FROM='vms', WHERE={'vnf_id': vnf_id})
297 for vm in vms:
298 if vm["image_id"] not in image_list:
299 image_list.append(vm["image_id"])
300 if vm["image_list"]:
301 vm_image_list = yaml.load(vm["image_list"])
302 for image_dict in vm_image_list:
303 if image_dict["image_id"] not in image_list:
304 image_list.append(image_dict["image_id"])
305 return image_list
tierno7edb6752016-03-21 17:37:52 +0100306
tiernob3d36742017-03-03 23:51:05 +0100307
tiernoa2793912016-10-04 08:15:08 +0000308def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None,
309 vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None):
tierno7edb6752016-03-21 17:37:52 +0100310 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
tierno42026a02017-02-10 15:13:40 +0100311 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
tierno7edb6752016-03-21 17:37:52 +0100312 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
tiernobe41e222016-09-02 15:16:13 +0200313 raise exception upon error
tierno7edb6752016-03-21 17:37:52 +0100314 '''
315 WHERE_dict={}
316 if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
317 if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id
tiernoa2793912016-10-04 08:15:08 +0000318 if datacenter_tenant_id is not None: WHERE_dict['datacenter_tenant_id'] = datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +0100319 if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name
320 if vim_tenant is not None: WHERE_dict['dt.vim_tenant_id'] = vim_tenant
tiernoa2793912016-10-04 08:15:08 +0000321 if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name
322 if nfvo_tenant or vim_tenant or vim_tenant_name or datacenter_tenant_id:
tierno7edb6752016-03-21 17:37:52 +0100323 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 +0000324 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 +0100325 '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 +0000326 'user','passwd', 'dt.config as dt_config')
tierno7edb6752016-03-21 17:37:52 +0100327 else:
328 from_ = 'datacenters as d'
329 select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
tiernof97fd272016-07-11 14:32:37 +0200330 try:
331 vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
332 vim_dict={}
333 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200334 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
tierno16e3dd42018-04-24 12:52:40 +0200335 'datacenter_id': vim.get('datacenter_id'),
tiernob6434212018-04-26 16:27:47 +0200336 '_vim_type_internal': vim.get('type')}
tierno8008c3a2016-10-13 15:34:28 +0000337 if vim["config"]:
tiernof97fd272016-07-11 14:32:37 +0200338 extra.update(yaml.load(vim["config"]))
tierno8008c3a2016-10-13 15:34:28 +0000339 if vim.get('dt_config'):
340 extra.update(yaml.load(vim["dt_config"]))
tiernof97fd272016-07-11 14:32:37 +0200341 if vim["type"] not in vimconn_imported:
342 module_info=None
343 try:
344 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200345 pkg = __import__("osm_ro." + module)
346 vim_conn = getattr(pkg, module)
347 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
348 # vim_conn = imp.load_module(vim["type"], *module_info)
tiernof97fd272016-07-11 14:32:37 +0200349 vimconn_imported[vim["type"]] = vim_conn
350 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200351 # if module_info and module_info[0]:
352 # file.close(module_info[0])
tiernof97fd272016-07-11 14:32:37 +0200353 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
354 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100355
tierno7edb6752016-03-21 17:37:52 +0100356 try:
tierno867ffe92017-03-27 12:50:34 +0200357 if 'datacenter_tenant_id' in vim:
358 thread_id = vim["datacenter_tenant_id"]
tiernob3d36742017-03-03 23:51:05 +0100359 if thread_id not in vim_persistent_info:
360 vim_persistent_info[thread_id] = {}
361 persistent_info = vim_persistent_info[thread_id]
362 else:
363 persistent_info = {}
tiernof97fd272016-07-11 14:32:37 +0200364 #if not tenant:
365 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
366 vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector(
367 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
tiernob3d36742017-03-03 23:51:05 +0100368 tenant_id=vim.get('vim_tenant_id',vim_tenant),
369 tenant_name=vim.get('vim_tenant_name',vim_tenant_name),
tierno42026a02017-02-10 15:13:40 +0100370 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
tierno3ae39742016-09-07 12:17:51 +0200371 user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd),
tiernob3d36742017-03-03 23:51:05 +0100372 config=extra, persistent_info=persistent_info
tiernof97fd272016-07-11 14:32:37 +0200373 )
374 except Exception as e:
tiernoa3572692018-05-14 13:09:33 +0200375 http_code = HTTP_Internal_Server_Error
376 if isinstance(e, vimconn.vimconnException):
377 http_code = e.http_code
378 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), http_code)
tiernof97fd272016-07-11 14:32:37 +0200379 return vim_dict
380 except db_base_Exception as e:
381 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
tierno42026a02017-02-10 15:13:40 +0100382
tiernob3d36742017-03-03 23:51:05 +0100383
tierno7edb6752016-03-21 17:37:52 +0100384def rollback(mydb, vims, rollback_list):
385 undeleted_items=[]
tierno42026a02017-02-10 15:13:40 +0100386 #delete things by reverse order
tierno7edb6752016-03-21 17:37:52 +0100387 for i in range(len(rollback_list)-1, -1, -1):
388 item = rollback_list[i]
389 if item["where"]=="vim":
390 if item["vim_id"] not in vims:
391 continue
tierno56d73d22017-08-02 13:53:02 +0200392 if is_task_id(item["uuid"]):
393 continue
394 vim = vims[item["vim_id"]]
tiernoae4a8d12016-07-08 12:30:39 +0200395 try:
396 if item["what"]=="image":
397 vim.delete_image(item["uuid"])
tierno868220c2017-09-26 00:11:05 +0200398 mydb.delete_row(FROM="datacenters_images", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200399 elif item["what"]=="flavor":
400 vim.delete_flavor(item["uuid"])
tiernoad6bdd42018-01-10 10:43:46 +0100401 mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200402 elif item["what"]=="network":
403 vim.delete_network(item["uuid"])
404 elif item["what"]=="vm":
405 vim.delete_vminstance(item["uuid"])
406 except vimconn.vimconnException as e:
407 logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e))
408 undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"]))
tiernof97fd272016-07-11 14:32:37 +0200409 except db_base_Exception as e:
410 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 +0100411
tierno7edb6752016-03-21 17:37:52 +0100412 else: # where==mano
tiernof97fd272016-07-11 14:32:37 +0200413 try:
414 if item["what"]=="image":
415 mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]})
416 elif item["what"]=="flavor":
417 mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]})
418 except db_base_Exception as e:
419 logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e))
420 undeleted_items.append("{} '{}'".format(item['what'], item["uuid"]))
tierno42026a02017-02-10 15:13:40 +0100421 if len(undeleted_items)==0:
tierno7edb6752016-03-21 17:37:52 +0100422 return True," Rollback successful."
423 else:
424 return False," Rollback fails to delete: " + str(undeleted_items)
tierno42026a02017-02-10 15:13:40 +0100425
tiernob3d36742017-03-03 23:51:05 +0100426
tiernoafed5f12017-01-26 17:57:43 +0100427def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1):
tierno7edb6752016-03-21 17:37:52 +0100428 global global_config
tierno42026a02017-02-10 15:13:40 +0100429 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
tierno7edb6752016-03-21 17:37:52 +0100430 vnfc_interfaces={}
431 for vnfc in vnf_descriptor["vnf"]["VNFC"]:
tiernoafed5f12017-01-26 17:57:43 +0100432 name_dict = {}
tierno7edb6752016-03-21 17:37:52 +0100433 #dataplane interfaces
434 for numa in vnfc.get("numas",() ):
435 for interface in numa.get("interfaces",()):
tiernoafed5f12017-01-26 17:57:43 +0100436 if interface["name"] in name_dict:
437 raise NfvoException(
438 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
439 vnfc["name"], interface["name"]),
440 HTTP_Bad_Request)
441 name_dict[ interface["name"] ] = "underlay"
tierno7edb6752016-03-21 17:37:52 +0100442 #bridge interfaces
443 for interface in vnfc.get("bridge-ifaces",() ):
tiernoafed5f12017-01-26 17:57:43 +0100444 if interface["name"] in name_dict:
445 raise NfvoException(
446 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
447 vnfc["name"], interface["name"]),
448 HTTP_Bad_Request)
449 name_dict[ interface["name"] ] = "overlay"
450 vnfc_interfaces[ vnfc["name"] ] = name_dict
tierno36c0b172017-01-12 18:32:28 +0100451 # check bood-data info
tierno40e1bce2017-08-09 09:12:04 +0200452 # if "boot-data" in vnfc:
453 # # check that user-data is incompatible with users and config-files
454 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
455 # raise NfvoException(
456 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
457 # HTTP_Bad_Request)
tierno36c0b172017-01-12 18:32:28 +0100458
tierno7edb6752016-03-21 17:37:52 +0100459 #check if the info in external_connections matches with the one in the vnfcs
460 name_list=[]
461 for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
462 if external_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100463 raise NfvoException(
464 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
465 external_connection["name"]),
466 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100467 name_list.append(external_connection["name"])
468 if external_connection["VNFC"] not in vnfc_interfaces:
tiernoafed5f12017-01-26 17:57:43 +0100469 raise NfvoException(
470 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
471 external_connection["name"], external_connection["VNFC"]),
472 HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100473
tierno7edb6752016-03-21 17:37:52 +0100474 if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
tiernoafed5f12017-01-26 17:57:43 +0100475 raise NfvoException(
476 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
477 external_connection["name"],
478 external_connection["local_iface_name"]),
479 HTTP_Bad_Request )
tierno42026a02017-02-10 15:13:40 +0100480
tierno7edb6752016-03-21 17:37:52 +0100481 #check if the info in internal_connections matches with the one in the vnfcs
482 name_list=[]
483 for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
484 if internal_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100485 raise NfvoException(
486 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
487 internal_connection["name"]),
488 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100489 name_list.append(internal_connection["name"])
490 #We should check that internal-connections of type "ptp" have only 2 elements
tiernoafed5f12017-01-26 17:57:43 +0100491
492 if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"):
493 raise NfvoException(
494 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
495 internal_connection["name"],
496 'ptp' if vnf_descriptor_version==1 else 'e-line',
497 'data' if vnf_descriptor_version==1 else "e-lan"),
498 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100499 for port in internal_connection["elements"]:
tiernoafed5f12017-01-26 17:57:43 +0100500 vnf = port["VNFC"]
501 iface = port["local_iface_name"]
502 if vnf not in vnfc_interfaces:
503 raise NfvoException(
504 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
505 internal_connection["name"], vnf),
506 HTTP_Bad_Request)
507 if iface not in vnfc_interfaces[ vnf ]:
508 raise NfvoException(
509 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
510 internal_connection["name"], iface),
511 HTTP_Bad_Request)
512 return -HTTP_Bad_Request,
513 if vnf_descriptor_version==1 and "type" not in internal_connection:
514 if vnfc_interfaces[vnf][iface] == "overlay":
515 internal_connection["type"] = "bridge"
516 else:
517 internal_connection["type"] = "data"
518 if vnf_descriptor_version==2 and "implementation" not in internal_connection:
519 if vnfc_interfaces[vnf][iface] == "overlay":
520 internal_connection["implementation"] = "overlay"
521 else:
522 internal_connection["implementation"] = "underlay"
523 if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \
524 internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay":
525 raise NfvoException(
526 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
527 internal_connection["name"],
528 iface, 'bridge' if vnf_descriptor_version==1 else 'overlay',
529 'data' if vnf_descriptor_version==1 else 'underlay'),
530 HTTP_Bad_Request)
531 if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \
532 vnfc_interfaces[vnf][iface] == "underlay":
533 raise NfvoException(
534 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
535 internal_connection["name"], iface,
536 'data' if vnf_descriptor_version==1 else 'underlay',
537 'bridge' if vnf_descriptor_version==1 else 'overlay'),
538 HTTP_Bad_Request)
539
tierno7edb6752016-03-21 17:37:52 +0100540
tierno56d73d22017-08-02 13:53:02 +0200541def 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 +0100542 #look if image exist
543 if only_create_at_vim:
544 image_mano_id = image_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000545 if return_on_error == None:
546 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100547 else:
garciadeblas14480452017-01-10 13:08:07 +0100548 if image_dict['location']:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200549 images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
550 else:
551 images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']})
tiernof97fd272016-07-11 14:32:37 +0200552 if len(images)>=1:
553 image_mano_id = images[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100554 else:
garciadeblas14480452017-01-10 13:08:07 +0100555 #create image in MANO DB
tierno7edb6752016-03-21 17:37:52 +0100556 temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None),
garciadeblasb69fa9f2016-09-28 12:04:10 +0200557 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None),
558 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum']
tierno7edb6752016-03-21 17:37:52 +0100559 }
garciadeblas14480452017-01-10 13:08:07 +0100560 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
tiernof97fd272016-07-11 14:32:37 +0200561 image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True)
562 rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100563 #create image at every vim
564 for vim_id,vim in vims.iteritems():
tierno868220c2017-09-26 00:11:05 +0200565 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
tierno7edb6752016-03-21 17:37:52 +0100566 image_created="false"
567 #look at database
tierno868220c2017-09-26 00:11:05 +0200568 image_db = mydb.get_rows(FROM="datacenters_images",
569 WHERE={'datacenter_vim_id': datacenter_vim_id, 'image_id': image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100570 #look at VIM if this image exist
tiernoae4a8d12016-07-08 12:30:39 +0200571 try:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200572 if image_dict['location'] is not None:
573 image_vim_id = vim.get_image_id_from_path(image_dict['location'])
574 else:
garciadeblas30833382017-01-09 09:46:31 +0100575 filter_dict = {}
576 filter_dict['name'] = image_dict['universal_name']
577 if image_dict.get('checksum') != None:
578 filter_dict['checksum'] = image_dict['checksum']
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000579 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200580 vim_images = vim.get_image_list(filter_dict)
garciadeblas14480452017-01-10 13:08:07 +0100581 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200582 if len(vim_images) > 1:
garciadeblas3fa2c052017-01-05 12:00:08 +0100583 raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), HTTP_Conflict)
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000584 elif len(vim_images) == 0:
garciadeblas3fa2c052017-01-05 12:00:08 +0100585 raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict)))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200586 else:
garciadeblas14480452017-01-10 13:08:07 +0100587 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
588 image_vim_id = vim_images[0]['id']
garciadeblasb69fa9f2016-09-28 12:04:10 +0200589
tiernoae4a8d12016-07-08 12:30:39 +0200590 except vimconn.vimconnNotFoundException as e:
garciadeblas14480452017-01-10 13:08:07 +0100591 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
tierno42026a02017-02-10 15:13:40 +0100592 try:
garciadeblas14480452017-01-10 13:08:07 +0100593 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
594 if image_dict['location']:
595 image_vim_id = vim.new_image(image_dict)
596 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
597 image_created="true"
598 else:
garciadeblasb6153a22017-02-06 15:38:33 +0100599 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
600 raise vimconn.vimconnException(str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200601 except vimconn.vimconnException as e:
602 if return_on_error:
garciadeblas14480452017-01-10 13:08:07 +0100603 logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200604 raise
tierno5e91eb82016-10-04 09:39:07 +0000605 image_vim_id = None
garciadeblas14480452017-01-10 13:08:07 +0100606 logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200607 continue
608 except vimconn.vimconnException as e:
tierno5e91eb82016-10-04 09:39:07 +0000609 if return_on_error:
610 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
611 raise
garciadeblasb69fa9f2016-09-28 12:04:10 +0200612 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
tierno5e91eb82016-10-04 09:39:07 +0000613 image_vim_id = None
garciadeblas30833382017-01-09 09:46:31 +0100614 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200615 #if we reach here, the image has been created or existed
tiernof97fd272016-07-11 14:32:37 +0200616 if len(image_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100617 #add new vim_id at datacenters_images
tierno868220c2017-09-26 00:11:05 +0200618 mydb.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id,
619 'image_id':image_mano_id,
620 'vim_id': image_vim_id,
621 'created':image_created})
tierno7edb6752016-03-21 17:37:52 +0100622 elif image_db[0]["vim_id"]!=image_vim_id:
623 #modify existing vim_id at datacenters_images
tierno868220c2017-09-26 00:11:05 +0200624 mydb.update_rows('datacenters_images', UPDATE={'vim_id':image_vim_id}, WHERE={'datacenter_vim_id':vim_id, 'image_id':image_mano_id})
tierno42026a02017-02-10 15:13:40 +0100625
tiernof97fd272016-07-11 14:32:37 +0200626 return image_vim_id if only_create_at_vim else image_mano_id
tierno7edb6752016-03-21 17:37:52 +0100627
tiernob3d36742017-03-03 23:51:05 +0100628
tierno5e91eb82016-10-04 09:39:07 +0000629def create_or_use_flavor(mydb, vims, flavor_dict, rollback_list, only_create_at_vim=False, return_on_error = None):
garciadeblas79d1a1a2017-12-11 16:07:07 +0100630 temp_flavor_dict= {'disk':flavor_dict.get('disk',0),
tierno7edb6752016-03-21 17:37:52 +0100631 'ram':flavor_dict.get('ram'),
632 'vcpus':flavor_dict.get('vcpus'),
633 }
634 if 'extended' in flavor_dict and flavor_dict['extended']==None:
635 del flavor_dict['extended']
636 if 'extended' in flavor_dict:
637 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
638
639 #look if flavor exist
640 if only_create_at_vim:
641 flavor_mano_id = flavor_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000642 if return_on_error == None:
643 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100644 else:
tiernof97fd272016-07-11 14:32:37 +0200645 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
646 if len(flavors)>=1:
647 flavor_mano_id = flavors[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100648 else:
649 #create flavor
650 #create one by one the images of aditional disks
651 dev_image_list=[] #list of images
652 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
653 dev_nb=0
654 for device in flavor_dict['extended'].get('devices',[]):
garciadeblas41f18be2016-10-04 09:09:58 +0200655 if "image" not in device and "image name" not in device:
tierno7edb6752016-03-21 17:37:52 +0100656 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200657 image_dict={}
658 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
659 image_dict['universal_name']=device.get('image name')
660 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
661 image_dict['location']=device.get('image')
garciadeblas14480452017-01-10 13:08:07 +0100662 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200663 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100664 image_metadata_dict = device.get('image metadata', None)
665 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100666 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100667 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
668 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200669 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
670 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
tierno7edb6752016-03-21 17:37:52 +0100671 dev_image_list.append(image_id)
tierno42026a02017-02-10 15:13:40 +0100672 dev_nb += 1
tierno7edb6752016-03-21 17:37:52 +0100673 temp_flavor_dict['name'] = flavor_dict['name']
674 temp_flavor_dict['description'] = flavor_dict.get('description',None)
tiernof97fd272016-07-11 14:32:37 +0200675 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
676 flavor_mano_id= content
677 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100678 #create flavor at every vim
679 if 'uuid' in flavor_dict:
680 del flavor_dict['uuid']
681 flavor_vim_id=None
682 for vim_id,vim in vims.items():
tierno868220c2017-09-26 00:11:05 +0200683 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
tierno7edb6752016-03-21 17:37:52 +0100684 flavor_created="false"
685 #look at database
tierno868220c2017-09-26 00:11:05 +0200686 flavor_db = mydb.get_rows(FROM="datacenters_flavors",
687 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100688 #look at VIM if this flavor exist SKIPPED
689 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
690 #if res_vim < 0:
691 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
692 # continue
693 #elif res_vim==0:
tierno42026a02017-02-10 15:13:40 +0100694
tiernof1ba57e2017-09-07 12:23:19 +0200695 # Create the flavor in VIM
696 # Translate images at devices from MANO id to VIM id
montesmoreno0c8def02016-12-22 12:16:23 +0000697 disk_list = []
tierno7edb6752016-03-21 17:37:52 +0100698 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
tiernof1ba57e2017-09-07 12:23:19 +0200699 # make a copy of original devices
tierno7edb6752016-03-21 17:37:52 +0100700 devices_original=[]
montesmoreno0c8def02016-12-22 12:16:23 +0000701
tierno7edb6752016-03-21 17:37:52 +0100702 for device in flavor_dict["extended"].get("devices",[]):
703 dev={}
704 dev.update(device)
705 devices_original.append(dev)
706 if 'image' in device:
707 del device['image']
708 if 'image metadata' in device:
709 del device['image metadata']
tiernof1ba57e2017-09-07 12:23:19 +0200710 if 'image checksum' in device:
711 del device['image checksum']
712 dev_nb = 0
tierno7edb6752016-03-21 17:37:52 +0100713 for index in range(0,len(devices_original)) :
714 device=devices_original[index]
montesmoreno0c8def02016-12-22 12:16:23 +0000715 if "image" not in device and "image name" not in device:
716 if 'size' in device:
717 disk_list.append({'size': device.get('size', default_volume_size)})
tierno7edb6752016-03-21 17:37:52 +0100718 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200719 image_dict={}
720 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
721 image_dict['universal_name']=device.get('image name')
722 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
723 image_dict['location']=device.get('image')
tiernof1ba57e2017-09-07 12:23:19 +0200724 # image_dict['new_location']=device.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200725 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100726 image_metadata_dict = device.get('image metadata', None)
727 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100728 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100729 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
730 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200731 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 +0100732 image_dict["uuid"]=image_mano_id
tiernof97fd272016-07-11 14:32:37 +0200733 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 +0000734
735 #save disk information (image must be based on and size
736 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
737
tierno7edb6752016-03-21 17:37:52 +0100738 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
739 dev_nb += 1
tiernof97fd272016-07-11 14:32:37 +0200740 if len(flavor_db)>0:
tierno7edb6752016-03-21 17:37:52 +0100741 #check that this vim_id exist in VIM, if not create
742 flavor_vim_id=flavor_db[0]["vim_id"]
tiernoae4a8d12016-07-08 12:30:39 +0200743 try:
744 vim.get_flavor(flavor_vim_id)
745 continue #flavor exist
746 except vimconn.vimconnException:
747 pass
tierno7edb6752016-03-21 17:37:52 +0100748 #create flavor at vim
tiernoae4a8d12016-07-08 12:30:39 +0200749 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
750 try:
tiernocf157a82017-01-30 14:07:06 +0100751 flavor_vim_id = None
752 flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict)
753 flavor_create="false"
754 except vimconn.vimconnException as e:
755 pass
756 try:
757 if not flavor_vim_id:
758 flavor_vim_id = vim.new_flavor(flavor_dict)
759 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
760 flavor_created="true"
tiernoae4a8d12016-07-08 12:30:39 +0200761 except vimconn.vimconnException as e:
762 if return_on_error:
763 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200764 raise
tiernoae4a8d12016-07-08 12:30:39 +0200765 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tierno5e91eb82016-10-04 09:39:07 +0000766 flavor_vim_id = None
tiernoae4a8d12016-07-08 12:30:39 +0200767 continue
tierno7edb6752016-03-21 17:37:52 +0100768 #if reach here the flavor has been create or exist
tiernof97fd272016-07-11 14:32:37 +0200769 if len(flavor_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100770 #add new vim_id at datacenters_flavors
montesmoreno0c8def02016-12-22 12:16:23 +0000771 extended_devices_yaml = None
772 if len(disk_list) > 0:
773 extended_devices = dict()
774 extended_devices['disks'] = disk_list
775 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
776 mydb.new_row('datacenters_flavors',
tierno868220c2017-09-26 00:11:05 +0200777 {'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id, 'vim_id': flavor_vim_id,
778 'created': flavor_created, 'extended': extended_devices_yaml})
tierno7edb6752016-03-21 17:37:52 +0100779 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
780 #modify existing vim_id at datacenters_flavors
tierno868220c2017-09-26 00:11:05 +0200781 mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id},
782 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
tierno42026a02017-02-10 15:13:40 +0100783
tiernof97fd272016-07-11 14:32:37 +0200784 return flavor_vim_id if only_create_at_vim else flavor_mano_id
tierno7edb6752016-03-21 17:37:52 +0100785
tiernob3d36742017-03-03 23:51:05 +0100786
tiernof1ba57e2017-09-07 12:23:19 +0200787def get_str(obj, field, length):
788 """
789 Obtain the str value,
790 :param obj:
791 :param length:
792 :return:
793 """
794 value = obj.get(field)
795 if value is not None:
796 value = str(value)[:length]
797 return value
798
799def _lookfor_or_create_image(db_image, mydb, descriptor):
800 """
801 fill image content at db_image dictionary. Check if the image with this image and checksum exist
802 :param db_image: dictionary to insert data
803 :param mydb: database connector
804 :param descriptor: yang descriptor
805 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
806 """
807
808 db_image["name"] = get_str(descriptor, "image", 255)
809 db_image["checksum"] = get_str(descriptor, "image-checksum", 32)
810 if not db_image["checksum"]: # Ensure that if empty string, None is stored
811 db_image["checksum"] = None
812 if db_image["name"].startswith("/"):
813 db_image["location"] = db_image["name"]
814 existing_images = mydb.get_rows(FROM="images", WHERE={'location': db_image["location"]})
815 else:
816 db_image["universal_name"] = db_image["name"]
817 existing_images = mydb.get_rows(FROM="images", WHERE={'universal_name': db_image['universal_name'],
818 'checksum': db_image['checksum']})
819 if existing_images:
820 return existing_images[0]["uuid"]
821 else:
822 image_uuid = str(uuid4())
823 db_image["uuid"] = image_uuid
824 return None
825
826def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
827 """
828 Parses an OSM IM vnfd_catalog and insert at DB
829 :param mydb:
830 :param tenant_id:
831 :param vnf_descriptor:
832 :return: The list of cretated vnf ids
833 """
834 try:
835 myvnfd = vnfd_catalog.vnfd()
tiernoa9550202017-09-22 13:31:35 +0200836 try:
tiernoad6bdd42018-01-10 10:43:46 +0100837 pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd, path_helper=True)
tiernoa9550202017-09-22 13:31:35 +0200838 except Exception as e:
tiernob2880eb2017-10-04 15:04:53 +0200839 raise NfvoException("Error. Invalid VNF descriptor format " + str(e), HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +0200840 db_vnfs = []
841 db_nets = []
842 db_vms = []
843 db_vms_index = 0
844 db_interfaces = []
845 db_images = []
846 db_flavors = []
tierno41a69812018-02-16 14:34:33 +0100847 db_ip_profiles_index = 0
848 db_ip_profiles = []
tiernof1ba57e2017-09-07 12:23:19 +0200849 uuid_list = []
850 vnfd_uuid_list = []
tiernoe18ba432017-10-12 10:22:45 +0200851 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd:vnfd-catalog")
852 if not vnfd_catalog_descriptor:
853 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd-catalog")
854 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd")
855 if not vnfd_descriptor_list:
856 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd:vnfd")
tiernob2880eb2017-10-04 15:04:53 +0200857 for vnfd_yang in myvnfd.vnfd_catalog.vnfd.itervalues():
858 vnfd = vnfd_yang.get()
tiernof1ba57e2017-09-07 12:23:19 +0200859
860 # table vnf
861 vnf_uuid = str(uuid4())
862 uuid_list.append(vnf_uuid)
863 vnfd_uuid_list.append(vnf_uuid)
tierno66eba6e2017-11-10 17:09:18 +0100864 vnfd_id = get_str(vnfd, "id", 255)
tiernof1ba57e2017-09-07 12:23:19 +0200865 db_vnf = {
866 "uuid": vnf_uuid,
tierno66eba6e2017-11-10 17:09:18 +0100867 "osm_id": vnfd_id,
tiernof1ba57e2017-09-07 12:23:19 +0200868 "name": get_str(vnfd, "name", 255),
869 "description": get_str(vnfd, "description", 255),
870 "tenant_id": tenant_id,
871 "vendor": get_str(vnfd, "vendor", 255),
872 "short_name": get_str(vnfd, "short-name", 255),
873 "descriptor": str(vnf_descriptor)[:60000]
874 }
875
tiernoe18ba432017-10-12 10:22:45 +0200876 for vnfd_descriptor in vnfd_descriptor_list:
877 if vnfd_descriptor["id"] == str(vnfd["id"]):
878 break
879
tierno41a69812018-02-16 14:34:33 +0100880 # table ip_profiles (ip-profiles)
881 ip_profile_name2db_table_index = {}
882 for ip_profile in vnfd.get("ip-profiles").itervalues():
883 db_ip_profile = {
884 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
885 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
886 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
887 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
888 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
889 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
890 }
891 dns_list = []
892 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
893 dns_list.append(str(dns.get("address")))
894 db_ip_profile["dns_address"] = ";".join(dns_list)
895 if ip_profile["ip-profile-params"].get('security-group'):
896 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
897 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
898 db_ip_profiles_index += 1
899 db_ip_profiles.append(db_ip_profile)
900
tiernof1ba57e2017-09-07 12:23:19 +0200901 # table nets (internal-vld)
902 net_id2uuid = {} # for mapping interface with network
903 for vld in vnfd.get("internal-vld").itervalues():
904 net_uuid = str(uuid4())
905 uuid_list.append(net_uuid)
906 db_net = {
907 "name": get_str(vld, "name", 255),
908 "vnf_id": vnf_uuid,
909 "uuid": net_uuid,
910 "description": get_str(vld, "description", 255),
911 "type": "bridge", # TODO adjust depending on connection point type
912 }
913 net_id2uuid[vld.get("id")] = net_uuid
914 db_nets.append(db_net)
tierno41a69812018-02-16 14:34:33 +0100915 # ip-profile, link db_ip_profile with db_sce_net
916 if vld.get("ip-profile-ref"):
917 ip_profile_name = vld.get("ip-profile-ref")
918 if ip_profile_name not in ip_profile_name2db_table_index:
919 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':"
920 "'{}'. Reference to a non-existing 'ip_profiles'".format(
921 str(vnfd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
922 HTTP_Bad_Request)
923 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["net_id"] = net_uuid
924 else: #check no ip-address has been defined
tierno45140f52018-03-26 12:11:46 +0200925 for icp in vld.get("internal-connection-point").itervalues():
tierno41a69812018-02-16 14:34:33 +0100926 if icp.get("ip-address"):
927 raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' "
928 "contains an ip-address but no ip-profile has been defined at VLD".format(
929 str(vnfd["id"]), str(vld["id"]), str(icp["id"])),
930 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +0200931
tiernocf596692017-11-20 15:47:51 +0100932 # connection points vaiable declaration
933 cp_name2iface_uuid = {}
934 cp_name2vm_uuid = {}
935 cp_name2db_interface = {}
936
tiernof1ba57e2017-09-07 12:23:19 +0200937 # table vms (vdus)
938 vdu_id2uuid = {}
939 vdu_id2db_table_index = {}
940 for vdu in vnfd.get("vdu").itervalues():
tierno41a69812018-02-16 14:34:33 +0100941
942 for vdu_descriptor in vnfd_descriptor["vdu"]:
943 if vdu_descriptor["id"] == str(vdu["id"]):
944 break
tiernof1ba57e2017-09-07 12:23:19 +0200945 vm_uuid = str(uuid4())
946 uuid_list.append(vm_uuid)
tierno66eba6e2017-11-10 17:09:18 +0100947 vdu_id = get_str(vdu, "id", 255)
tiernof1ba57e2017-09-07 12:23:19 +0200948 db_vm = {
949 "uuid": vm_uuid,
tierno66eba6e2017-11-10 17:09:18 +0100950 "osm_id": vdu_id,
tiernof1ba57e2017-09-07 12:23:19 +0200951 "name": get_str(vdu, "name", 255),
952 "description": get_str(vdu, "description", 255),
953 "vnf_id": vnf_uuid,
954 }
955 vdu_id2uuid[db_vm["osm_id"]] = vm_uuid
956 vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index
957 if vdu.get("count"):
958 db_vm["count"] = int(vdu["count"])
959
960 # table image
961 image_present = False
962 if vdu.get("image"):
963 image_present = True
964 db_image = {}
965 image_uuid = _lookfor_or_create_image(db_image, mydb, vdu)
966 if not image_uuid:
967 image_uuid = db_image["uuid"]
968 db_images.append(db_image)
969 db_vm["image_id"] = image_uuid
tierno16e3dd42018-04-24 12:52:40 +0200970 if vdu.get("alternative-images"):
971 vm_alternative_images = []
972 for alt_image in vdu.get("alternative-images").itervalues():
973 db_image = {}
974 image_uuid = _lookfor_or_create_image(db_image, mydb, alt_image)
975 if not image_uuid:
976 image_uuid = db_image["uuid"]
977 db_images.append(db_image)
978 vm_alternative_images.append({
979 "image_id": image_uuid,
980 "vim_type": str(alt_image["vim-type"]),
981 # "universal_name": str(alt_image["image"]),
982 # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None
983 })
984
985 db_vm["image_list"] = yaml.safe_dump(vm_alternative_images, default_flow_style=True, width=256)
tiernof1ba57e2017-09-07 12:23:19 +0200986
987 # volumes
988 devices = []
989 if vdu.get("volumes"):
990 for volume_key in sorted(vdu["volumes"]):
991 volume = vdu["volumes"][volume_key]
992 if not image_present:
993 # Convert the first volume to vnfc.image
994 image_present = True
995 db_image = {}
996 image_uuid = _lookfor_or_create_image(db_image, mydb, volume)
997 if not image_uuid:
998 image_uuid = db_image["uuid"]
999 db_images.append(db_image)
1000 db_vm["image_id"] = image_uuid
1001 else:
1002 # Add Openmano devices
1003 device = {}
1004 device["type"] = str(volume.get("device-type"))
1005 if volume.get("size"):
1006 device["size"] = int(volume["size"])
1007 if volume.get("image"):
1008 device["image name"] = str(volume["image"])
1009 if volume.get("image-checksum"):
1010 device["image checksum"] = str(volume["image-checksum"])
1011 devices.append(device)
1012
tierno66eba6e2017-11-10 17:09:18 +01001013 # cloud-init
1014 boot_data = {}
1015 if vdu.get("cloud-init"):
1016 boot_data["user-data"] = str(vdu["cloud-init"])
1017 elif vdu.get("cloud-init-file"):
1018 # TODO Where this file content is present???
1019 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
1020 boot_data["user-data"] = str(vdu["cloud-init-file"])
1021
1022 if vdu.get("supplemental-boot-data"):
1023 if vdu["supplemental-boot-data"].get('boot-data-drive'):
1024 boot_data['boot-data-drive'] = True
1025 if vdu["supplemental-boot-data"].get('config-file'):
1026 om_cfgfile_list = list()
1027 for custom_config_file in vdu["supplemental-boot-data"]['config-file'].itervalues():
1028 # TODO Where this file content is present???
1029 cfg_source = str(custom_config_file["source"])
1030 om_cfgfile_list.append({"dest": custom_config_file["dest"],
1031 "content": cfg_source})
1032 boot_data['config-files'] = om_cfgfile_list
1033 if boot_data:
1034 db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256)
1035
1036 db_vms.append(db_vm)
1037 db_vms_index += 1
1038
1039 # table interfaces (internal/external interfaces)
1040 flavor_epa_interfaces = []
tierno66eba6e2017-11-10 17:09:18 +01001041 vdu_id2cp_name = {} # stored only when one external connection point is presented at this VDU
1042 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
1043 for iface in vdu.get("interface").itervalues():
1044 flavor_epa_interface = {}
1045 iface_uuid = str(uuid4())
1046 uuid_list.append(iface_uuid)
1047 db_interface = {
1048 "uuid": iface_uuid,
1049 "internal_name": get_str(iface, "name", 255),
1050 "vm_id": vm_uuid,
1051 }
1052 flavor_epa_interface["name"] = db_interface["internal_name"]
1053 if iface.get("virtual-interface").get("vpci"):
1054 db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12)
1055 flavor_epa_interface["vpci"] = db_interface["vpci"]
1056
1057 if iface.get("virtual-interface").get("bandwidth"):
1058 bps = int(iface.get("virtual-interface").get("bandwidth"))
1059 db_interface["bw"] = int(math.ceil(bps/1000000.0))
1060 flavor_epa_interface["bandwidth"] = "{} Mbps".format(db_interface["bw"])
1061
1062 if iface.get("virtual-interface").get("type") == "OM-MGMT":
1063 db_interface["type"] = "mgmt"
1064 elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000"):
1065 db_interface["type"] = "bridge"
1066 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1067 elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1068 db_interface["type"] = "data"
1069 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1070 flavor_epa_interface["dedicated"] = "no" if iface["virtual-interface"]["type"] == "SR-IOV" \
1071 else "yes"
1072 flavor_epa_interfaces.append(flavor_epa_interface)
1073 else:
1074 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1075 "-interface':'type':'{}'. Interface type is not supported".format(
1076 vnfd_id, vdu_id, iface.get("virtual-interface").get("type")),
1077 HTTP_Bad_Request)
1078
1079 if iface.get("external-connection-point-ref"):
1080 try:
1081 cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")]
1082 db_interface["external_name"] = get_str(cp, "name", 255)
1083 cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid
1084 cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid
1085 cp_name2db_interface[db_interface["external_name"]] = db_interface
1086 for cp_descriptor in vnfd_descriptor["connection-point"]:
1087 if cp_descriptor["name"] == db_interface["external_name"]:
1088 break
1089 else:
1090 raise KeyError()
1091
1092 if vdu_id in vdu_id2cp_name:
1093 vdu_id2cp_name[vdu_id] = None # more than two connecdtion point for this VDU
1094 else:
1095 vdu_id2cp_name[vdu_id] = db_interface["external_name"]
1096
1097 # port security
1098 if str(cp_descriptor.get("port-security-enabled")).lower() == "false":
1099 db_interface["port_security"] = 0
1100 elif str(cp_descriptor.get("port-security-enabled")).lower() == "true":
1101 db_interface["port_security"] = 1
1102 except KeyError:
1103 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1104 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1105 " at connection-point".format(
1106 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
1107 cp=iface.get("vnfd-connection-point-ref")),
1108 HTTP_Bad_Request)
1109 elif iface.get("internal-connection-point-ref"):
1110 try:
tierno41a69812018-02-16 14:34:33 +01001111 for icp_descriptor in vdu_descriptor["internal-connection-point"]:
1112 if icp_descriptor["id"] == str(iface.get("internal-connection-point-ref")):
1113 break
1114 else:
1115 raise KeyError("does not exist at vdu:internal-connection-point")
1116 icp = None
1117 icp_vld = None
tierno66eba6e2017-11-10 17:09:18 +01001118 for vld in vnfd.get("internal-vld").itervalues():
1119 for cp in vld.get("internal-connection-point").itervalues():
1120 if cp.get("id-ref") == iface.get("internal-connection-point-ref"):
tierno41a69812018-02-16 14:34:33 +01001121 if icp:
1122 raise KeyError("is referenced by more than one 'internal-vld'")
1123 icp = cp
1124 icp_vld = vld
1125 if not icp:
1126 raise KeyError("is not referenced by any 'internal-vld'")
1127
1128 db_interface["net_id"] = net_id2uuid[icp_vld.get("id")]
1129 if str(icp_descriptor.get("port-security-enabled")).lower() == "false":
1130 db_interface["port_security"] = 0
1131 elif str(icp_descriptor.get("port-security-enabled")).lower() == "true":
1132 db_interface["port_security"] = 1
1133 if icp.get("ip-address"):
1134 if not icp_vld.get("ip-profile-ref"):
1135 raise NfvoException
1136 db_interface["ip_address"] = str(icp.get("ip-address"))
1137 except KeyError as e:
tierno66eba6e2017-11-10 17:09:18 +01001138 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
tierno41a69812018-02-16 14:34:33 +01001139 "'interface[{iface}]':'internal-connection-point-ref':'{cp}'"
1140 " {msg}".format(
tierno66eba6e2017-11-10 17:09:18 +01001141 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
tierno41a69812018-02-16 14:34:33 +01001142 cp=iface.get("internal-connection-point-ref"), msg=str(e)),
tierno66eba6e2017-11-10 17:09:18 +01001143 HTTP_Bad_Request)
1144 if iface.get("position") is not None:
1145 db_interface["created_at"] = int(iface.get("position")) - 1000
tierno41a69812018-02-16 14:34:33 +01001146 if iface.get("mac-address"):
1147 db_interface["mac"] = str(iface.get("mac-address"))
tierno66eba6e2017-11-10 17:09:18 +01001148 db_interfaces.append(db_interface)
1149
tiernof1ba57e2017-09-07 12:23:19 +02001150 # table flavors
1151 db_flavor = {
1152 "name": get_str(vdu, "name", 250) + "-flv",
1153 "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)),
1154 "ram": int(vdu["vm-flavor"].get("memory-mb", 1)),
garciadeblas79d1a1a2017-12-11 16:07:07 +01001155 "disk": int(vdu["vm-flavor"].get("storage-gb", 0)),
tiernof1ba57e2017-09-07 12:23:19 +02001156 }
tiernocf596692017-11-20 15:47:51 +01001157 # TODO revise the case of several numa-node-policy node
tiernof1ba57e2017-09-07 12:23:19 +02001158 extended = {}
1159 numa = {}
1160 if devices:
1161 extended["devices"] = devices
tierno66eba6e2017-11-10 17:09:18 +01001162 if flavor_epa_interfaces:
1163 numa["interfaces"] = flavor_epa_interfaces
tiernof1ba57e2017-09-07 12:23:19 +02001164 if vdu.get("guest-epa"): # TODO or dedicated_int:
1165 epa_vcpu_set = False
1166 if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
1167 numa_node_policy = vdu["guest-epa"].get("numa-node-policy")
1168 if numa_node_policy.get("node"):
tiernocf596692017-11-20 15:47:51 +01001169 numa_node = numa_node_policy["node"].values()[0]
tiernof1ba57e2017-09-07 12:23:19 +02001170 if numa_node.get("num-cores"):
1171 numa["cores"] = numa_node["num-cores"]
1172 epa_vcpu_set = True
1173 if numa_node.get("paired-threads"):
1174 if numa_node["paired-threads"].get("num-paired-threads"):
tierno39dddcc2017-10-05 18:48:06 +02001175 numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"])
tiernof1ba57e2017-09-07 12:23:19 +02001176 epa_vcpu_set = True
tierno39dddcc2017-10-05 18:48:06 +02001177 if len(numa_node["paired-threads"].get("paired-thread-ids")):
tiernof1ba57e2017-09-07 12:23:19 +02001178 numa["paired-threads-id"] = []
tierno39dddcc2017-10-05 18:48:06 +02001179 for pair in numa_node["paired-threads"]["paired-thread-ids"].itervalues():
tiernof1ba57e2017-09-07 12:23:19 +02001180 numa["paired-threads-id"].append(
1181 (str(pair["thread-a"]), str(pair["thread-b"]))
1182 )
1183 if numa_node.get("num-threads"):
tierno39dddcc2017-10-05 18:48:06 +02001184 numa["threads"] = int(numa_node["num-threads"])
tiernof1ba57e2017-09-07 12:23:19 +02001185 epa_vcpu_set = True
1186 if numa_node.get("memory-mb"):
1187 numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1)
1188 if vdu["guest-epa"].get("mempage-size"):
1189 if vdu["guest-epa"]["mempage-size"] != "SMALL":
1190 numa["memory"] = max(int(db_flavor["ram"] / 1024), 1)
1191 if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set:
1192 if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
1193 if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \
1194 vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
1195 numa["cores"] = max(db_flavor["vcpus"], 1)
1196 else:
1197 numa["threads"] = max(db_flavor["vcpus"], 1)
1198 if numa:
1199 extended["numas"] = [numa]
1200 if extended:
1201 extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256)
1202 db_flavor["extended"] = extended_text
1203 # look if flavor exist
garciadeblas79d1a1a2017-12-11 16:07:07 +01001204 temp_flavor_dict = {'disk': db_flavor.get('disk', 0),
tiernof1ba57e2017-09-07 12:23:19 +02001205 'ram': db_flavor.get('ram'),
1206 'vcpus': db_flavor.get('vcpus'),
1207 'extended': db_flavor.get('extended')
1208 }
1209 existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
1210 if existing_flavors:
1211 flavor_uuid = existing_flavors[0]["uuid"]
1212 else:
1213 flavor_uuid = str(uuid4())
1214 uuid_list.append(flavor_uuid)
1215 db_flavor["uuid"] = flavor_uuid
1216 db_flavors.append(db_flavor)
1217 db_vm["flavor_id"] = flavor_uuid
1218
tiernof1ba57e2017-09-07 12:23:19 +02001219 # VNF affinity and antiaffinity
1220 for pg in vnfd.get("placement-groups").itervalues():
1221 pg_name = get_str(pg, "name", 255)
1222 for vdu in pg.get("member-vdus").itervalues():
1223 vdu_id = get_str(vdu, "member-vdu-ref", 255)
1224 if vdu_id not in vdu_id2db_table_index:
tiernob2880eb2017-10-04 15:04:53 +02001225 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1226 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
tierno66eba6e2017-11-10 17:09:18 +01001227 vnf=vnfd_id, pg=pg_name, vdu=vdu_id),
tiernob2880eb2017-10-04 15:04:53 +02001228 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001229 db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name
1230 # TODO consider the case of isolation and not colocation
1231 # if pg.get("strategy") == "ISOLATION":
1232
1233 # VNF mgmt configuration
1234 mgmt_access = {}
1235 if vnfd["mgmt-interface"].get("vdu-id"):
tierno66eba6e2017-11-10 17:09:18 +01001236 mgmt_vdu_id = get_str(vnfd["mgmt-interface"], "vdu-id", 255)
1237 if mgmt_vdu_id not in vdu_id2uuid:
tiernob2880eb2017-10-04 15:04:53 +02001238 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1239 "'{vdu}'. Reference to a non-existing vdu".format(
tierno66eba6e2017-11-10 17:09:18 +01001240 vnf=vnfd_id, vdu=mgmt_vdu_id),
tiernob2880eb2017-10-04 15:04:53 +02001241 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001242 mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]]
tierno66eba6e2017-11-10 17:09:18 +01001243 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
1244 if vdu_id2cp_name.get(mgmt_vdu_id):
1245 cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt"
1246
tiernof1ba57e2017-09-07 12:23:19 +02001247 if vnfd["mgmt-interface"].get("ip-address"):
1248 mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address"))
1249 if vnfd["mgmt-interface"].get("cp"):
1250 if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid:
tiernob2880eb2017-10-04 15:04:53 +02001251 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp':'{cp}'. "
1252 "Reference to a non-existing connection-point".format(
tierno66eba6e2017-11-10 17:09:18 +01001253 vnf=vnfd_id, cp=vnfd["mgmt-interface"]["cp"]),
tiernob2880eb2017-10-04 15:04:53 +02001254 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001255 mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]]
1256 mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]]
tiernoe2ff1ce2017-11-02 17:01:10 +01001257 # mark this interface as of type mgmt
1258 cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt"
1259
tiernoa9550202017-09-22 13:31:35 +02001260 default_user = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
tiernof1ba57e2017-09-07 12:23:19 +02001261 "default-user", 64)
gcalvinoe580c7d2017-09-22 14:09:51 +02001262
tiernof1ba57e2017-09-07 12:23:19 +02001263 if default_user:
1264 mgmt_access["default_user"] = default_user
gcalvinoe580c7d2017-09-22 14:09:51 +02001265 required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1266 "required", 6)
1267 if required:
1268 mgmt_access["required"] = required
1269
tiernof1ba57e2017-09-07 12:23:19 +02001270 if mgmt_access:
1271 db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256)
1272
1273 db_vnfs.append(db_vnf)
1274 db_tables=[
1275 {"vnfs": db_vnfs},
1276 {"nets": db_nets},
1277 {"images": db_images},
1278 {"flavors": db_flavors},
tierno41a69812018-02-16 14:34:33 +01001279 {"ip_profiles": db_ip_profiles},
tiernof1ba57e2017-09-07 12:23:19 +02001280 {"vms": db_vms},
1281 {"interfaces": db_interfaces},
1282 ]
1283
1284 logger.debug("create_vnf Deployment done vnfDict: %s",
1285 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
1286 mydb.new_rows(db_tables, uuid_list)
1287 return vnfd_uuid_list
tiernob2880eb2017-10-04 15:04:53 +02001288 except NfvoException:
1289 raise
tiernof1ba57e2017-09-07 12:23:19 +02001290 except Exception as e:
1291 logger.error("Exception {}".format(e))
1292 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
1293
1294
tierno7edb6752016-03-21 17:37:52 +01001295def new_vnf(mydb, tenant_id, vnf_descriptor):
1296 global global_config
tierno42026a02017-02-10 15:13:40 +01001297
tierno7edb6752016-03-21 17:37:52 +01001298 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +01001299 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
tierno7edb6752016-03-21 17:37:52 +01001300 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +01001301 vims = {}
tierno7edb6752016-03-21 17:37:52 +01001302 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001303 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001304 if "tenant_id" in vnf_descriptor["vnf"]:
1305 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001306 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
1307 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001308 else:
1309 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1310 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +01001311 if global_config["auto_push_VNF_to_VIMs"]:
1312 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001313
1314 # Step 4. Review the descriptor and add missing fields
1315 #print vnf_descriptor
tiernof97fd272016-07-11 14:32:37 +02001316 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
tierno7edb6752016-03-21 17:37:52 +01001317 vnf_name = vnf_descriptor['vnf']['name']
1318 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1319 if "physical" in vnf_descriptor['vnf']:
1320 del vnf_descriptor['vnf']['physical']
1321 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +01001322
tierno42026a02017-02-10 15:13:40 +01001323 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
tiernof97fd272016-07-11 14:32:37 +02001324 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1325 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +01001326
tierno7edb6752016-03-21 17:37:52 +01001327 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1328 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1329 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
tierno7edb6752016-03-21 17:37:52 +01001330 try:
tiernof97fd272016-07-11 14:32:37 +02001331 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +01001332 for vnfc in vnf_descriptor['vnf']['VNFC']:
1333 VNFCitem={}
1334 VNFCitem["name"] = vnfc['name']
mirabal29356312017-07-27 12:21:22 +02001335 VNFCitem["availability_zone"] = vnfc.get('availability_zone')
tierno7edb6752016-03-21 17:37:52 +01001336 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +01001337
tiernof97fd272016-07-11 14:32:37 +02001338 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +01001339
tierno7edb6752016-03-21 17:37:52 +01001340 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +02001341 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 +01001342 myflavorDict["description"] = VNFCitem["description"]
1343 myflavorDict["ram"] = vnfc.get("ram", 0)
1344 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
garciadeblas79d1a1a2017-12-11 16:07:07 +01001345 myflavorDict["disk"] = vnfc.get("disk", 0)
tierno7edb6752016-03-21 17:37:52 +01001346 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +01001347
tierno7edb6752016-03-21 17:37:52 +01001348 devices = vnfc.get("devices")
1349 if devices != None:
1350 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +01001351
tierno7edb6752016-03-21 17:37:52 +01001352 # TODO:
1353 # 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 +01001354 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1355
tierno7edb6752016-03-21 17:37:52 +01001356 # Previous code has been commented
1357 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1358 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1359 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1360 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1361 #else:
1362 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1363 # if result2:
1364 # print "Error creating flavor: unknown processor model. Rollback successful."
1365 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1366 # else:
1367 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1368 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +01001369
tierno7edb6752016-03-21 17:37:52 +01001370 if 'numas' in vnfc and len(vnfc['numas'])>0:
1371 myflavorDict['extended']['numas'] = vnfc['numas']
1372
1373 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +01001374
tierno7edb6752016-03-21 17:37:52 +01001375 # Step 6.2 New flavors are created in the VIM
tiernof97fd272016-07-11 14:32:37 +02001376 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
tierno7edb6752016-03-21 17:37:52 +01001377
tiernof97fd272016-07-11 14:32:37 +02001378 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
tierno7edb6752016-03-21 17:37:52 +01001379 VNFCitem["flavor_id"] = flavor_id
1380 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +01001381
tiernof97fd272016-07-11 14:32:37 +02001382 logger.debug("Creating new images in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +01001383 # Step 6.3 New images are created in the VIM
1384 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +01001385 #This "for" loop might be integrated with the previous one
tierno7edb6752016-03-21 17:37:52 +01001386 #In case this integration is made, the VNFCDict might become a VNFClist.
1387 for vnfc in vnf_descriptor['vnf']['VNFC']:
tiernof97fd272016-07-11 14:32:37 +02001388 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +02001389 image_dict={}
1390 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1391 image_dict['universal_name']=vnfc.get('image name')
1392 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1393 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +01001394 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +02001395 image_dict['checksum']=vnfc.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +01001396 image_metadata_dict = vnfc.get('image metadata', None)
1397 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +01001398 if image_metadata_dict is not None:
tierno7edb6752016-03-21 17:37:52 +01001399 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1400 image_dict['metadata']=image_metadata_str
1401 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
tiernof97fd272016-07-11 14:32:37 +02001402 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1403 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
tierno7edb6752016-03-21 17:37:52 +01001404 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +02001405 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno8e690322017-08-10 15:58:50 +02001406 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
tierno36c0b172017-01-12 18:32:28 +01001407 if vnfc.get("boot-data"):
1408 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
tierno7edb6752016-03-21 17:37:52 +01001409
tierno42026a02017-02-10 15:13:40 +01001410
tiernof97fd272016-07-11 14:32:37 +02001411 # Step 7. Storing the VNF descriptor in the repository
1412 if "descriptor" not in vnf_descriptor["vnf"]:
1413 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001414
tiernof97fd272016-07-11 14:32:37 +02001415 # Step 8. Adding the VNF to the NFVO DB
1416 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1417 return vnf_id
1418 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
tierno7edb6752016-03-21 17:37:52 +01001419 _, message = rollback(mydb, vims, rollback_list)
tiernof97fd272016-07-11 14:32:37 +02001420 if isinstance(e, db_base_Exception):
1421 error_text = "Exception at database"
1422 elif isinstance(e, KeyError):
1423 error_text = "KeyError exception "
1424 e.http_code = HTTP_Internal_Server_Error
1425 else:
1426 error_text = "Exception at VIM"
1427 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1428 #logger.error("start_scenario %s", error_text)
1429 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01001430
tiernob3d36742017-03-03 23:51:05 +01001431
garciadeblas9f8456e2016-09-05 05:02:59 +02001432def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
1433 global global_config
tierno42026a02017-02-10 15:13:40 +01001434
garciadeblas9f8456e2016-09-05 05:02:59 +02001435 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +01001436 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
garciadeblas9f8456e2016-09-05 05:02:59 +02001437 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +01001438 vims = {}
garciadeblas9f8456e2016-09-05 05:02:59 +02001439 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001440 check_tenant(mydb, tenant_id)
garciadeblas9f8456e2016-09-05 05:02:59 +02001441 if "tenant_id" in vnf_descriptor["vnf"]:
1442 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
1443 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
1444 HTTP_Unauthorized)
1445 else:
1446 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1447 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +01001448 if global_config["auto_push_VNF_to_VIMs"]:
1449 vims = get_vim(mydb, tenant_id)
garciadeblas9f8456e2016-09-05 05:02:59 +02001450
1451 # Step 4. Review the descriptor and add missing fields
1452 #print vnf_descriptor
1453 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1454 vnf_name = vnf_descriptor['vnf']['name']
1455 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1456 if "physical" in vnf_descriptor['vnf']:
1457 del vnf_descriptor['vnf']['physical']
1458 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +01001459
tierno42026a02017-02-10 15:13:40 +01001460 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
garciadeblas9f8456e2016-09-05 05:02:59 +02001461 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1462 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +01001463
garciadeblas9f8456e2016-09-05 05:02:59 +02001464 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1465 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1466 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
1467 try:
1468 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1469 for vnfc in vnf_descriptor['vnf']['VNFC']:
1470 VNFCitem={}
1471 VNFCitem["name"] = vnfc['name']
1472 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +01001473
garciadeblas9f8456e2016-09-05 05:02:59 +02001474 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +01001475
garciadeblas9f8456e2016-09-05 05:02:59 +02001476 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +02001477 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 +02001478 myflavorDict["description"] = VNFCitem["description"]
1479 myflavorDict["ram"] = vnfc.get("ram", 0)
1480 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
garciadeblas79d1a1a2017-12-11 16:07:07 +01001481 myflavorDict["disk"] = vnfc.get("disk", 0)
garciadeblas9f8456e2016-09-05 05:02:59 +02001482 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +01001483
garciadeblas9f8456e2016-09-05 05:02:59 +02001484 devices = vnfc.get("devices")
1485 if devices != None:
1486 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +01001487
garciadeblas9f8456e2016-09-05 05:02:59 +02001488 # TODO:
1489 # 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 +01001490 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1491
garciadeblas9f8456e2016-09-05 05:02:59 +02001492 # Previous code has been commented
1493 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1494 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1495 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1496 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1497 #else:
1498 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1499 # if result2:
1500 # print "Error creating flavor: unknown processor model. Rollback successful."
1501 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1502 # else:
1503 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1504 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +01001505
garciadeblas9f8456e2016-09-05 05:02:59 +02001506 if 'numas' in vnfc and len(vnfc['numas'])>0:
1507 myflavorDict['extended']['numas'] = vnfc['numas']
1508
1509 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +01001510
garciadeblas9f8456e2016-09-05 05:02:59 +02001511 # Step 6.2 New flavors are created in the VIM
1512 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
1513
1514 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1515 VNFCitem["flavor_id"] = flavor_id
1516 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +01001517
garciadeblas9f8456e2016-09-05 05:02:59 +02001518 logger.debug("Creating new images in the VIM for each VNFC")
1519 # Step 6.3 New images are created in the VIM
1520 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +01001521 #This "for" loop might be integrated with the previous one
garciadeblas9f8456e2016-09-05 05:02:59 +02001522 #In case this integration is made, the VNFCDict might become a VNFClist.
1523 for vnfc in vnf_descriptor['vnf']['VNFC']:
1524 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +02001525 image_dict={}
1526 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1527 image_dict['universal_name']=vnfc.get('image name')
1528 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1529 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +01001530 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +02001531 image_dict['checksum']=vnfc.get('image checksum')
garciadeblas9f8456e2016-09-05 05:02:59 +02001532 image_metadata_dict = vnfc.get('image metadata', None)
1533 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +01001534 if image_metadata_dict is not None:
garciadeblas9f8456e2016-09-05 05:02:59 +02001535 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1536 image_dict['metadata']=image_metadata_str
1537 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1538 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1539 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1540 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +02001541 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno8e690322017-08-10 15:58:50 +02001542 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
tierno36c0b172017-01-12 18:32:28 +01001543 if vnfc.get("boot-data"):
1544 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
garciadeblas9f8456e2016-09-05 05:02:59 +02001545
garciadeblas9f8456e2016-09-05 05:02:59 +02001546 # Step 7. Storing the VNF descriptor in the repository
1547 if "descriptor" not in vnf_descriptor["vnf"]:
1548 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001549
garciadeblas9f8456e2016-09-05 05:02:59 +02001550 # Step 8. Adding the VNF to the NFVO DB
1551 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1552 return vnf_id
1553 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
1554 _, message = rollback(mydb, vims, rollback_list)
1555 if isinstance(e, db_base_Exception):
1556 error_text = "Exception at database"
1557 elif isinstance(e, KeyError):
1558 error_text = "KeyError exception "
1559 e.http_code = HTTP_Internal_Server_Error
1560 else:
1561 error_text = "Exception at VIM"
1562 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1563 #logger.error("start_scenario %s", error_text)
1564 raise NfvoException(error_text, e.http_code)
1565
tiernob3d36742017-03-03 23:51:05 +01001566
tierno7edb6752016-03-21 17:37:52 +01001567def get_vnf_id(mydb, tenant_id, vnf_id):
1568 #check valid tenant_id
tierno42026a02017-02-10 15:13:40 +01001569 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001570 #obtain data
1571 where_or = {}
1572 if tenant_id != "any":
1573 where_or["tenant_id"] = tenant_id
1574 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001575 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
1576
tiernof1ba57e2017-09-07 12:23:19 +02001577 vnf_id = vnf["uuid"]
1578 filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
tiernof97fd272016-07-11 14:32:37 +02001579 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
tierno7edb6752016-03-21 17:37:52 +01001580 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1581 data={'vnf' : filtered_content}
1582 #GET VM
tiernof97fd272016-07-11 14:32:37 +02001583 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
tiernof1ba57e2017-09-07 12:23:19 +02001584 SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1585 'boot_data'),
tierno7edb6752016-03-21 17:37:52 +01001586 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +02001587 if len(content)==0:
1588 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno36c0b172017-01-12 18:32:28 +01001589 # change boot_data into boot-data
1590 for vm in content:
1591 if vm.get("boot_data"):
1592 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
1593 del vm["boot_data"]
tierno7edb6752016-03-21 17:37:52 +01001594
1595 data['vnf']['VNFC'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001596 #TODO: GET all the information from a VNFC and include it in the output.
tierno42026a02017-02-10 15:13:40 +01001597
tierno7edb6752016-03-21 17:37:52 +01001598 #GET NET
tierno42026a02017-02-10 15:13:40 +01001599 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
tierno7edb6752016-03-21 17:37:52 +01001600 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1601 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +02001602 data['vnf']['nets'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001603
1604 #GET ip-profile for each net
1605 for net in data['vnf']['nets']:
1606 ipprofiles = mydb.get_rows(FROM='ip_profiles',
1607 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1608 WHERE={'net_id': net["uuid"]} )
1609 if len(ipprofiles)==1:
1610 net["ip_profile"] = ipprofiles[0]
1611 elif len(ipprofiles)>1:
1612 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 +01001613
1614
garciadeblas9f8456e2016-09-05 05:02:59 +02001615 #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 +01001616
garciadeblas9f8456e2016-09-05 05:02:59 +02001617 #GET External Interfaces
tiernof97fd272016-07-11 14:32:37 +02001618 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 +01001619 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1620 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
tierno3fcfdb72017-10-24 07:48:24 +02001621 WHERE={'vnfs.uuid': vnf_id, 'interfaces.external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01001622 #print content
tiernof97fd272016-07-11 14:32:37 +02001623 data['vnf']['external-connections'] = content
tierno42026a02017-02-10 15:13:40 +01001624
tiernof97fd272016-07-11 14:32:37 +02001625 return data
tierno7edb6752016-03-21 17:37:52 +01001626
1627
1628def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
1629 # Check tenant exist
1630 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +02001631 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001632 # Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernof97fd272016-07-11 14:32:37 +02001633 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001634 else:
1635 vims={}
1636
1637 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1638 where_or = {}
1639 if tenant_id != "any":
1640 where_or["tenant_id"] = tenant_id
1641 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001642 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 +02001643 vnf_id = vnf["uuid"]
tierno42026a02017-02-10 15:13:40 +01001644
tierno7edb6752016-03-21 17:37:52 +01001645 # "Getting the list of flavors and tenants of the VNF"
tierno42026a02017-02-10 15:13:40 +01001646 flavorList = get_flavorlist(mydb, vnf_id)
tiernof97fd272016-07-11 14:32:37 +02001647 if len(flavorList)==0:
1648 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001649
tiernof97fd272016-07-11 14:32:37 +02001650 imageList = get_imagelist(mydb, vnf_id)
1651 if len(imageList)==0:
1652 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001653
tiernof97fd272016-07-11 14:32:37 +02001654 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
1655 if deleted == 0:
1656 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno42026a02017-02-10 15:13:40 +01001657
tierno7edb6752016-03-21 17:37:52 +01001658 undeletedItems = []
1659 for flavor in flavorList:
1660 #check if flavor is used by other vnf
tiernof97fd272016-07-11 14:32:37 +02001661 try:
1662 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
1663 if len(c) > 0:
1664 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
1665 continue
1666 #flavor not used, must be deleted
1667 #delelte at VIM
tierno96ebf002017-12-13 10:55:38 +01001668 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id': flavor})
tierno7edb6752016-03-21 17:37:52 +01001669 for flavor_vim in c:
tierno96ebf002017-12-13 10:55:38 +01001670 if not flavor_vim['created']: # skip this flavor because not created by openmano
tierno7edb6752016-03-21 17:37:52 +01001671 continue
tierno96ebf002017-12-13 10:55:38 +01001672 # look for vim
1673 myvim = None
1674 for vim in vims.values():
1675 if vim["config"]["datacenter_tenant_id"] == flavor_vim["datacenter_vim_id"]:
1676 myvim = vim
1677 break
1678 if not myvim:
tierno7edb6752016-03-21 17:37:52 +01001679 continue
tiernoae4a8d12016-07-08 12:30:39 +02001680 try:
1681 myvim.delete_flavor(flavor_vim["vim_id"])
tierno96ebf002017-12-13 10:55:38 +01001682 except vimconn.vimconnNotFoundException:
1683 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"],
1684 flavor_vim["datacenter_vim_id"] )
tiernoae4a8d12016-07-08 12:30:39 +02001685 except vimconn.vimconnException as e:
1686 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
tierno96ebf002017-12-13 10:55:38 +01001687 flavor_vim["vim_id"], flavor_vim["datacenter_vim_id"], type(e).__name__, str(e))
1688 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"],
1689 flavor_vim["datacenter_vim_id"]))
1690 # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
tiernof97fd272016-07-11 14:32:37 +02001691 mydb.delete_row_by_id('flavors', flavor)
1692 except db_base_Exception as e:
1693 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
tierno96ebf002017-12-13 10:55:38 +01001694 undeletedItems.append("flavor {}".format(flavor))
tiernof97fd272016-07-11 14:32:37 +02001695
tierno42026a02017-02-10 15:13:40 +01001696
tierno7edb6752016-03-21 17:37:52 +01001697 for image in imageList:
tiernof97fd272016-07-11 14:32:37 +02001698 try:
1699 #check if image is used by other vnf
tierno16e3dd42018-04-24 12:52:40 +02001700 c = mydb.get_rows(FROM='vms', WHERE=[{'image_id': image}, {'image_list LIKE ': '%' + image + '%'}])
tiernof97fd272016-07-11 14:32:37 +02001701 if len(c) > 0:
1702 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
1703 continue
1704 #image not used, must be deleted
1705 #delelte at VIM
1706 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
tierno7edb6752016-03-21 17:37:52 +01001707 for image_vim in c:
tierno868220c2017-09-26 00:11:05 +02001708 if image_vim["datacenter_vim_id"] not in vims: # TODO change to datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +01001709 continue
1710 if image_vim['created']=='false': #skip this image because not created by openmano
1711 continue
1712 myvim=vims[ image_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +02001713 try:
1714 myvim.delete_image(image_vim["vim_id"])
1715 except vimconn.vimconnNotFoundException as e:
1716 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
1717 except vimconn.vimconnException as e:
1718 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1719 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
1720 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +02001721 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1722 mydb.delete_row_by_id('images', image)
1723 except db_base_Exception as e:
1724 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
tierno7edb6752016-03-21 17:37:52 +01001725 undeletedItems.append("image %s" % image)
1726
tiernof97fd272016-07-11 14:32:37 +02001727 return vnf_id + " " + vnf["name"]
tierno42026a02017-02-10 15:13:40 +01001728 #if undeletedItems:
tiernof97fd272016-07-11 14:32:37 +02001729 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
tierno7edb6752016-03-21 17:37:52 +01001730
tiernob3d36742017-03-03 23:51:05 +01001731
tierno7edb6752016-03-21 17:37:52 +01001732def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
1733 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
1734 if result < 0:
1735 return result, vims
1736 elif result == 0:
1737 return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name
1738 myvim = vims.values()[0]
1739 result,servers = myvim.get_hosts_info()
1740 if result < 0:
1741 return result, servers
1742 topology = {'name':myvim['name'] , 'servers': servers}
1743 return result, topology
1744
tiernob3d36742017-03-03 23:51:05 +01001745
tierno7edb6752016-03-21 17:37:52 +01001746def get_hosts(mydb, nfvo_tenant_id):
tiernof97fd272016-07-11 14:32:37 +02001747 vims = get_vim(mydb, nfvo_tenant_id)
1748 if len(vims) == 0:
1749 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found)
1750 elif len(vims)>1:
1751 #print "nfvo.datacenter_action() error. Several datacenters found"
1752 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001753 myvim = vims.values()[0]
tiernof97fd272016-07-11 14:32:37 +02001754 try:
1755 hosts = myvim.get_hosts()
1756 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01001757
tiernof97fd272016-07-11 14:32:37 +02001758 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
1759 for host in hosts:
1760 server={'name':host['name'], 'vms':[]}
1761 for vm in host['instances']:
1762 #get internal name and model
tierno42026a02017-02-10 15:13:40 +01001763 try:
tiernof97fd272016-07-11 14:32:37 +02001764 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1765 WHERE={'vim_vm_id':vm['id']} )
1766 if len(c) == 0:
1767 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1768 continue
1769 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
tierno42026a02017-02-10 15:13:40 +01001770
tiernof97fd272016-07-11 14:32:37 +02001771 except db_base_Exception as e:
1772 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1773 datacenter['Datacenters'][0]['servers'].append(server)
1774 #return -400, "en construccion"
tierno42026a02017-02-10 15:13:40 +01001775
tiernof97fd272016-07-11 14:32:37 +02001776 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1777 return datacenter
1778 except vimconn.vimconnException as e:
1779 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001780
tiernob3d36742017-03-03 23:51:05 +01001781
tierno7edb6752016-03-21 17:37:52 +01001782def new_scenario(mydb, tenant_id, topo):
1783
1784# result, vims = get_vim(mydb, tenant_id)
1785# if result < 0:
1786# return result, vims
1787#1: parse input
1788 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001789 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001790 if "tenant_id" in topo:
1791 if topo["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001792 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id),
1793 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001794 else:
1795 tenant_id=None
1796
tierno42026a02017-02-10 15:13:40 +01001797#1.1: get VNFs and external_networks (other_nets).
tierno7edb6752016-03-21 17:37:52 +01001798 vnfs={}
1799 other_nets={} #external_networks, bridge_networks and data_networkds
1800 nodes = topo['topology']['nodes']
1801 for k in nodes.keys():
1802 if nodes[k]['type'] == 'VNF':
1803 vnfs[k] = nodes[k]
1804 vnfs[k]['ifaces'] = {}
tierno42026a02017-02-10 15:13:40 +01001805 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
tierno7edb6752016-03-21 17:37:52 +01001806 other_nets[k] = nodes[k]
1807 other_nets[k]['external']=True
tierno42026a02017-02-10 15:13:40 +01001808 elif nodes[k]['type'] == 'network':
tierno7edb6752016-03-21 17:37:52 +01001809 other_nets[k] = nodes[k]
1810 other_nets[k]['external']=False
tierno42026a02017-02-10 15:13:40 +01001811
tierno7edb6752016-03-21 17:37:52 +01001812
1813#1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1814 for name,vnf in vnfs.items():
tierno3fcfdb72017-10-24 07:48:24 +02001815 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
tierno7edb6752016-03-21 17:37:52 +01001816 error_text = ""
1817 error_pos = "'topology':'nodes':'" + name + "'"
1818 if 'vnf_id' in vnf:
1819 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001820 where['uuid'] = vnf['vnf_id']
tierno7edb6752016-03-21 17:37:52 +01001821 if 'VNF model' in vnf:
1822 error_text += " 'VNF model' " + vnf['VNF model']
tiernocea279c2016-07-18 12:36:49 +02001823 where['name'] = vnf['VNF model']
tierno3fcfdb72017-10-24 07:48:24 +02001824 if len(where) == 1:
tiernof97fd272016-07-11 14:32:37 +02001825 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +01001826
tiernocea279c2016-07-18 12:36:49 +02001827 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1828 FROM='vnfs',
tierno3fcfdb72017-10-24 07:48:24 +02001829 WHERE=where)
tiernof97fd272016-07-11 14:32:37 +02001830 if len(vnf_db)==0:
1831 raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1832 elif len(vnf_db)>1:
1833 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001834 vnf['uuid']=vnf_db[0]['uuid']
1835 vnf['description']=vnf_db[0]['description']
1836 #get external interfaces
tierno42026a02017-02-10 15:13:40 +01001837 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1838 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
tierno3fcfdb72017-10-24 07:48:24 +02001839 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01001840 for ext_iface in ext_ifaces:
1841 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1842
1843#1.4 get list of connections
1844 conections = topo['topology']['connections']
1845 conections_list = []
tiernoefd80c92016-09-16 14:17:46 +02001846 conections_list_name = []
tierno7edb6752016-03-21 17:37:52 +01001847 for k in conections.keys():
1848 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1849 ifaces_list = conections[k]['nodes'].items()
1850 elif type(conections[k]['nodes'])==list: #list with dictionary
1851 ifaces_list=[]
1852 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1853 for k2 in conection_pair_list:
1854 ifaces_list += k2
1855
1856 con_type = conections[k].get("type", "link")
1857 if con_type != "link":
1858 if k in other_nets:
tiernof97fd272016-07-11 14:32:37 +02001859 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001860 other_nets[k] = {'external': False}
1861 if conections[k].get("graph"):
1862 other_nets[k]["graph"] = conections[k]["graph"]
1863 ifaces_list.append( (k, None) )
1864
tierno42026a02017-02-10 15:13:40 +01001865
tierno7edb6752016-03-21 17:37:52 +01001866 if con_type == "external_network":
1867 other_nets[k]['external'] = True
1868 if conections[k].get("model"):
1869 other_nets[k]["model"] = conections[k]["model"]
1870 else:
1871 other_nets[k]["model"] = k
tierno42026a02017-02-10 15:13:40 +01001872 if con_type == "dataplane_net" or con_type == "bridge_net":
tierno7edb6752016-03-21 17:37:52 +01001873 other_nets[k]["model"] = con_type
tierno42026a02017-02-10 15:13:40 +01001874
tiernoefd80c92016-09-16 14:17:46 +02001875 conections_list_name.append(k)
tierno7edb6752016-03-21 17:37:52 +01001876 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)
1877 #print set(ifaces_list)
1878 #check valid VNF and iface names
1879 for iface in ifaces_list:
1880 if iface[0] not in vnfs and iface[0] not in other_nets :
tiernof97fd272016-07-11 14:32:37 +02001881 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1882 str(k), iface[0]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001883 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
tiernof97fd272016-07-11 14:32:37 +02001884 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1885 str(k), iface[0], iface[1]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001886
1887#1.5 unify connections from the pair list to a consolidated list
1888 index=0
1889 while index < len(conections_list):
1890 index2 = index+1
1891 while index2 < len(conections_list):
1892 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
1893 conections_list[index] |= conections_list[index2]
1894 del conections_list[index2]
tiernoefd80c92016-09-16 14:17:46 +02001895 del conections_list_name[index2]
tierno7edb6752016-03-21 17:37:52 +01001896 else:
1897 index2 += 1
1898 conections_list[index] = list(conections_list[index]) # from set to list again
1899 index += 1
1900 #for k in conections_list:
1901 # print k
tierno42026a02017-02-10 15:13:40 +01001902
tierno7edb6752016-03-21 17:37:52 +01001903
1904
1905#1.6 Delete non external nets
1906# for k in other_nets.keys():
1907# if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1908# for con in conections_list:
1909# delete_indexes=[]
1910# for index in range(0,len(con)):
1911# if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1912# for index in delete_indexes:
1913# del con[index]
1914# del other_nets[k]
1915#1.7: Check external_ports are present at database table datacenter_nets
1916 for k,net in other_nets.items():
1917 error_pos = "'topology':'nodes':'" + k + "'"
1918 if net['external']==False:
1919 if 'name' not in net:
1920 net['name']=k
1921 if 'model' not in net:
tiernof97fd272016-07-11 14:32:37 +02001922 raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001923 if net['model']=='bridge_net':
1924 net['type']='bridge';
1925 elif net['model']=='dataplane_net':
1926 net['type']='data';
1927 else:
tiernof97fd272016-07-11 14:32:37 +02001928 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001929 else: #external
1930#IF we do not want to check that external network exist at datacenter
1931 pass
tierno42026a02017-02-10 15:13:40 +01001932#ELSE
tierno7edb6752016-03-21 17:37:52 +01001933# error_text = ""
1934# WHERE_={}
1935# if 'net_id' in net:
1936# error_text += " 'net_id' " + net['net_id']
1937# WHERE_['uuid'] = net['net_id']
1938# if 'model' in net:
1939# error_text += " 'model' " + net['model']
1940# WHERE_['name'] = net['model']
1941# if len(WHERE_) == 0:
1942# return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1943# r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1944# FROM='datacenter_nets', WHERE=WHERE_ )
1945# if r<0:
1946# print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1947# elif r==0:
1948# print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1949# return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1950# elif r>1:
tierno42026a02017-02-10 15:13:40 +01001951# print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1952# 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 +01001953# other_nets[k].update(net_db[0])
tierno42026a02017-02-10 15:13:40 +01001954#ENDIF
tierno7edb6752016-03-21 17:37:52 +01001955 net_list={}
1956 net_nb=0 #Number of nets
1957 for con in conections_list:
1958 #check if this is connected to a external net
1959 other_net_index=-1
1960 #print
1961 #print "con", con
1962 for index in range(0,len(con)):
1963 #check if this is connected to a external net
1964 for net_key in other_nets.keys():
1965 if con[index][0]==net_key:
1966 if other_net_index>=0:
tierno42026a02017-02-10 15:13:40 +01001967 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 +02001968 #print "nfvo.new_scenario " + error_text
1969 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001970 else:
1971 other_net_index = index
1972 net_target = net_key
1973 break
1974 #print "other_net_index", other_net_index
1975 try:
1976 if other_net_index>=0:
1977 del con[other_net_index]
1978#IF we do not want to check that external network exist at datacenter
1979 if other_nets[net_target]['external'] :
1980 if "name" not in other_nets[net_target]:
1981 other_nets[net_target]['name'] = other_nets[net_target]['model']
1982 if other_nets[net_target]["type"] == "external_network":
1983 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
1984 other_nets[net_target]["type"] = "data"
1985 else:
1986 other_nets[net_target]["type"] = "bridge"
tierno42026a02017-02-10 15:13:40 +01001987#ELSE
tierno7edb6752016-03-21 17:37:52 +01001988# if other_nets[net_target]['external'] :
1989# 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
1990# if type_=='data' and other_nets[net_target]['type']=="ptp":
1991# error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
1992# print "nfvo.new_scenario " + error_text
1993# return -HTTP_Bad_Request, error_text
tierno42026a02017-02-10 15:13:40 +01001994#ENDIF
tierno7edb6752016-03-21 17:37:52 +01001995 for iface in con:
1996 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1997 else:
1998 #create a net
1999 net_type_bridge=False
2000 net_type_data=False
2001 net_target = "__-__net"+str(net_nb)
tierno42026a02017-02-10 15:13:40 +01002002 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
tiernoefd80c92016-09-16 14:17:46 +02002003 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
tierno42026a02017-02-10 15:13:40 +01002004 'external':False}
tierno7edb6752016-03-21 17:37:52 +01002005 for iface in con:
2006 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
2007 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
2008 if iface_type=='mgmt' or iface_type=='bridge':
2009 net_type_bridge = True
2010 else:
2011 net_type_data = True
2012 if net_type_bridge and net_type_data:
2013 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 +02002014 #print "nfvo.new_scenario " + error_text
2015 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002016 elif net_type_bridge:
2017 type_='bridge'
2018 else:
2019 type_='data' if len(con)>2 else 'ptp'
2020 net_list[net_target]['type'] = type_
2021 net_nb+=1
2022 except Exception:
2023 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02002024 #print "nfvo.new_scenario " + error_text
tierno7edb6752016-03-21 17:37:52 +01002025 #raise e
tiernof97fd272016-07-11 14:32:37 +02002026 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002027
2028#1.8: Connect to management net all not already connected interfaces of type 'mgmt'
tierno42026a02017-02-10 15:13:40 +01002029 #1.8.1 obtain management net
tiernof97fd272016-07-11 14:32:37 +02002030 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
tierno7edb6752016-03-21 17:37:52 +01002031 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
tierno42026a02017-02-10 15:13:40 +01002032 #1.8.2 check all interfaces from all vnfs
tiernof97fd272016-07-11 14:32:37 +02002033 if len(mgmt_net)>0:
tierno7edb6752016-03-21 17:37:52 +01002034 add_mgmt_net = False
2035 for vnf in vnfs.values():
2036 for iface in vnf['ifaces'].values():
2037 if iface['type']=='mgmt' and 'net_key' not in iface:
2038 #iface not connected
2039 iface['net_key'] = 'mgmt'
2040 add_mgmt_net = True
2041 if add_mgmt_net and 'mgmt' not in net_list:
2042 net_list['mgmt']=mgmt_net[0]
2043 net_list['mgmt']['external']=True
2044 net_list['mgmt']['graph']={'visible':False}
2045
2046 net_list.update(other_nets)
tiernof97fd272016-07-11 14:32:37 +02002047 #print
2048 #print 'net_list', net_list
2049 #print
2050 #print 'vnfs', vnfs
2051 #print
tierno7edb6752016-03-21 17:37:52 +01002052
2053#2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
tiernof97fd272016-07-11 14:32:37 +02002054 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
tierno392f2852016-05-13 12:28:55 +02002055 'tenant_id':tenant_id, 'name':topo['name'],
2056 'description':topo.get('description',topo['name']),
2057 'public': topo.get('public', False)
2058 })
tierno42026a02017-02-10 15:13:40 +01002059
tiernof97fd272016-07-11 14:32:37 +02002060 return c
tierno7edb6752016-03-21 17:37:52 +01002061
tiernob3d36742017-03-03 23:51:05 +01002062
tierno5bb59dc2017-02-13 14:53:54 +01002063def new_scenario_v02(mydb, tenant_id, scenario_dict, version):
2064 """ This creates a new scenario for version 0.2 and 0.3"""
tierno392f2852016-05-13 12:28:55 +02002065 scenario = scenario_dict["scenario"]
tierno7edb6752016-03-21 17:37:52 +01002066 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01002067 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002068 if "tenant_id" in scenario:
2069 if scenario["tenant_id"] != tenant_id:
tierno5bb59dc2017-02-13 14:53:54 +01002070 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
tiernof97fd272016-07-11 14:32:37 +02002071 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
2072 scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01002073 else:
2074 tenant_id=None
2075
tierno5bb59dc2017-02-13 14:53:54 +01002076 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
tierno7edb6752016-03-21 17:37:52 +01002077 for name,vnf in scenario["vnfs"].iteritems():
tierno3fcfdb72017-10-24 07:48:24 +02002078 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
tierno7edb6752016-03-21 17:37:52 +01002079 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02002080 error_pos = "'scenario':'vnfs':'" + name + "'"
tierno7edb6752016-03-21 17:37:52 +01002081 if 'vnf_id' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01002082 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02002083 where['uuid'] = vnf['vnf_id']
tierno392f2852016-05-13 12:28:55 +02002084 if 'vnf_name' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01002085 error_text += " 'vnf_name' " + vnf['vnf_name']
tiernocea279c2016-07-18 12:36:49 +02002086 where['name'] = vnf['vnf_name']
tierno3fcfdb72017-10-24 07:48:24 +02002087 if len(where) == 1:
garciadeblas71781ea2016-09-19 14:41:59 +02002088 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002089 vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'),
tiernocea279c2016-07-18 12:36:49 +02002090 FROM='vnfs',
tierno3fcfdb72017-10-24 07:48:24 +02002091 WHERE=where)
tierno5bb59dc2017-02-13 14:53:54 +01002092 if len(vnf_db) == 0:
tiernof97fd272016-07-11 14:32:37 +02002093 raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
tierno5bb59dc2017-02-13 14:53:54 +01002094 elif len(vnf_db) > 1:
tiernof97fd272016-07-11 14:32:37 +02002095 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno5bb59dc2017-02-13 14:53:54 +01002096 vnf['uuid'] = vnf_db[0]['uuid']
2097 vnf['description'] = vnf_db[0]['description']
tierno7edb6752016-03-21 17:37:52 +01002098 vnf['ifaces'] = {}
tierno5bb59dc2017-02-13 14:53:54 +01002099 # get external interfaces
2100 ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
2101 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
tierno3fcfdb72017-10-24 07:48:24 +02002102 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01002103 for ext_iface in ext_ifaces:
tierno5bb59dc2017-02-13 14:53:54 +01002104 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']}
2105 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
tierno7edb6752016-03-21 17:37:52 +01002106
tierno5bb59dc2017-02-13 14:53:54 +01002107 # 2: Insert net_key and ip_address at every vnf interface
2108 for net_name, net in scenario["networks"].items():
2109 net_type_bridge = False
2110 net_type_data = False
tierno7edb6752016-03-21 17:37:52 +01002111 for iface_dict in net["interfaces"]:
tierno5bb59dc2017-02-13 14:53:54 +01002112 if version == "0.2":
2113 temp_dict = iface_dict
2114 ip_address = None
2115 elif version == "0.3":
2116 temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]}
2117 ip_address = iface_dict.get('ip_address', None)
2118 for vnf, iface in temp_dict.items():
tierno7edb6752016-03-21 17:37:52 +01002119 if vnf not in scenario["vnfs"]:
tierno5bb59dc2017-02-13 14:53:54 +01002120 error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2121 net_name, vnf)
2122 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02002123 raise NfvoException(error_text, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002124 if iface not in scenario["vnfs"][vnf]['ifaces']:
tierno5bb59dc2017-02-13 14:53:54 +01002125 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2126 .format(net_name, iface)
2127 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02002128 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002129 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
tierno5bb59dc2017-02-13 14:53:54 +01002130 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2131 "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
2132 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02002133 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002134 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
tierno5bb59dc2017-02-13 14:53:54 +01002135 scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address
tierno7edb6752016-03-21 17:37:52 +01002136 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
tierno5bb59dc2017-02-13 14:53:54 +01002137 if iface_type == 'mgmt' or iface_type == 'bridge':
tierno7edb6752016-03-21 17:37:52 +01002138 net_type_bridge = True
2139 else:
2140 net_type_data = True
tierno5bb59dc2017-02-13 14:53:54 +01002141
tierno7edb6752016-03-21 17:37:52 +01002142 if net_type_bridge and net_type_data:
tierno5bb59dc2017-02-13 14:53:54 +01002143 error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2144 .format(net_name)
2145 # logger.debug("nfvo.new_scenario " + error_text)
tiernof97fd272016-07-11 14:32:37 +02002146 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002147 elif net_type_bridge:
tierno5bb59dc2017-02-13 14:53:54 +01002148 type_ = 'bridge'
tierno7edb6752016-03-21 17:37:52 +01002149 else:
tierno5bb59dc2017-02-13 14:53:54 +01002150 type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp'
2151
2152 if net.get("implementation"): # for v0.3
2153 if type_ == "bridge" and net["implementation"] == "underlay":
2154 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2155 "'network':'{}'".format(net_name)
2156 # logger.debug(error_text)
2157 raise NfvoException(error_text, HTTP_Bad_Request)
2158 elif type_ != "bridge" and net["implementation"] == "overlay":
2159 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2160 "'network':'{}'".format(net_name)
2161 # logger.debug(error_text)
2162 raise NfvoException(error_text, HTTP_Bad_Request)
2163 net.pop("implementation")
2164 if "type" in net and version == "0.3": # for v0.3
2165 if type_ == "data" and net["type"] == "e-line":
2166 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\
2167 "'e-line' at 'network':'{}'".format(net_name)
2168 # logger.debug(error_text)
2169 raise NfvoException(error_text, HTTP_Bad_Request)
2170 elif type_ == "ptp" and net["type"] == "e-lan":
2171 type_ = "data"
2172
tierno7edb6752016-03-21 17:37:52 +01002173 net['type'] = type_
2174 net['name'] = net_name
2175 net['external'] = net.get('external', False)
2176
tierno5bb59dc2017-02-13 14:53:54 +01002177 # 3: insert at database
tierno7edb6752016-03-21 17:37:52 +01002178 scenario["nets"] = scenario["networks"]
2179 scenario['tenant_id'] = tenant_id
tierno5bb59dc2017-02-13 14:53:54 +01002180 scenario_id = mydb.new_scenario(scenario)
tiernof97fd272016-07-11 14:32:37 +02002181 return scenario_id
tierno7edb6752016-03-21 17:37:52 +01002182
tiernob3d36742017-03-03 23:51:05 +01002183
tiernof1ba57e2017-09-07 12:23:19 +02002184def new_nsd_v3(mydb, tenant_id, nsd_descriptor):
2185 """
2186 Parses an OSM IM nsd_catalog and insert at DB
2187 :param mydb:
2188 :param tenant_id:
2189 :param nsd_descriptor:
Igor D.Ccaadc442017-11-06 12:48:48 +00002190 :return: The list of created NSD ids
tiernof1ba57e2017-09-07 12:23:19 +02002191 """
2192 try:
2193 mynsd = nsd_catalog.nsd()
tiernoa9550202017-09-22 13:31:35 +02002194 try:
2195 pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd)
2196 except Exception as e:
tiernob2880eb2017-10-04 15:04:53 +02002197 raise NfvoException("Error. Invalid NS descriptor format: " + str(e), HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002198 db_scenarios = []
2199 db_sce_nets = []
2200 db_sce_vnfs = []
2201 db_sce_interfaces = []
Igor D.Ccaadc442017-11-06 12:48:48 +00002202 db_sce_vnffgs = []
2203 db_sce_rsps = []
2204 db_sce_rsp_hops = []
2205 db_sce_classifiers = []
2206 db_sce_classifier_matches = []
tiernof1ba57e2017-09-07 12:23:19 +02002207 db_ip_profiles = []
2208 db_ip_profiles_index = 0
2209 uuid_list = []
2210 nsd_uuid_list = []
tiernob2880eb2017-10-04 15:04:53 +02002211 for nsd_yang in mynsd.nsd_catalog.nsd.itervalues():
2212 nsd = nsd_yang.get()
tiernof1ba57e2017-09-07 12:23:19 +02002213
Igor D.Ccaadc442017-11-06 12:48:48 +00002214 # table scenarios
tiernof1ba57e2017-09-07 12:23:19 +02002215 scenario_uuid = str(uuid4())
2216 uuid_list.append(scenario_uuid)
2217 nsd_uuid_list.append(scenario_uuid)
2218 db_scenario = {
2219 "uuid": scenario_uuid,
2220 "osm_id": get_str(nsd, "id", 255),
2221 "name": get_str(nsd, "name", 255),
2222 "description": get_str(nsd, "description", 255),
2223 "tenant_id": tenant_id,
2224 "vendor": get_str(nsd, "vendor", 255),
2225 "short_name": get_str(nsd, "short-name", 255),
2226 "descriptor": str(nsd_descriptor)[:60000],
2227 }
2228 db_scenarios.append(db_scenario)
2229
2230 # table sce_vnfs (constituent-vnfd)
2231 vnf_index2scevnf_uuid = {}
2232 vnf_index2vnf_uuid = {}
2233 for vnf in nsd.get("constituent-vnfd").itervalues():
2234 existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255],
2235 'tenant_id': tenant_id})
2236 if not existing_vnf:
tiernob2880eb2017-10-04 15:04:53 +02002237 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2238 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2239 str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]),
2240 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002241 sce_vnf_uuid = str(uuid4())
2242 uuid_list.append(sce_vnf_uuid)
2243 db_sce_vnf = {
2244 "uuid": sce_vnf_uuid,
2245 "scenario_id": scenario_uuid,
tierno92c36fd2018-05-04 12:21:10 +02002246 # "name": get_str(vnf, "member-vnf-index", 255),
2247 "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 50),
tiernof1ba57e2017-09-07 12:23:19 +02002248 "vnf_id": existing_vnf[0]["uuid"],
tierno16e3dd42018-04-24 12:52:40 +02002249 "member_vnf_index": str(vnf["member-vnf-index"]),
tiernof1ba57e2017-09-07 12:23:19 +02002250 # TODO 'start-by-default': True
2251 }
tierno16e3dd42018-04-24 12:52:40 +02002252 vnf_index2scevnf_uuid[str(vnf['member-vnf-index'])] = sce_vnf_uuid
2253 vnf_index2vnf_uuid[str(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"]
tiernof1ba57e2017-09-07 12:23:19 +02002254 db_sce_vnfs.append(db_sce_vnf)
2255
2256 # table ip_profiles (ip-profiles)
2257 ip_profile_name2db_table_index = {}
2258 for ip_profile in nsd.get("ip-profiles").itervalues():
2259 db_ip_profile = {
2260 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
2261 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
2262 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
2263 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2264 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
2265 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
2266 }
2267 dns_list = []
2268 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
2269 dns_list.append(str(dns.get("address")))
2270 db_ip_profile["dns_address"] = ";".join(dns_list)
2271 if ip_profile["ip-profile-params"].get('security-group'):
2272 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
2273 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
2274 db_ip_profiles_index += 1
2275 db_ip_profiles.append(db_ip_profile)
2276
2277 # table sce_nets (internal-vld)
2278 for vld in nsd.get("vld").itervalues():
2279 sce_net_uuid = str(uuid4())
2280 uuid_list.append(sce_net_uuid)
2281 db_sce_net = {
2282 "uuid": sce_net_uuid,
2283 "name": get_str(vld, "name", 255),
2284 "scenario_id": scenario_uuid,
2285 # "type": #TODO
2286 "multipoint": not vld.get("type") == "ELINE",
2287 # "external": #TODO
2288 "description": get_str(vld, "description", 255),
2289 }
2290 # guess type of network
2291 if vld.get("mgmt-network"):
2292 db_sce_net["type"] = "bridge"
2293 db_sce_net["external"] = True
2294 elif vld.get("provider-network").get("overlay-type") == "VLAN":
2295 db_sce_net["type"] = "data"
2296 else:
tierno66eba6e2017-11-10 17:09:18 +01002297 # later on it will be fixed to bridge or data depending on the type of interfaces attached to it
2298 db_sce_net["type"] = None
tiernof1ba57e2017-09-07 12:23:19 +02002299 db_sce_nets.append(db_sce_net)
2300
2301 # ip-profile, link db_ip_profile with db_sce_net
2302 if vld.get("ip-profile-ref"):
2303 ip_profile_name = vld.get("ip-profile-ref")
2304 if ip_profile_name not in ip_profile_name2db_table_index:
tiernob2880eb2017-10-04 15:04:53 +02002305 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2306 " Reference to a non-existing 'ip_profiles'".format(
2307 str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
2308 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002309 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["sce_net_id"] = sce_net_uuid
tierno8f79ea12018-05-03 17:37:40 +02002310 elif vld.get("vim-network-name"):
2311 db_sce_net["vim_network_name"] = get_str(vld, "vim-network-name", 255)
tiernof1ba57e2017-09-07 12:23:19 +02002312
2313 # table sce_interfaces (vld:vnfd-connection-point-ref)
2314 for iface in vld.get("vnfd-connection-point-ref").itervalues():
tierno16e3dd42018-04-24 12:52:40 +02002315 vnf_index = str(iface['member-vnf-index-ref'])
tiernof1ba57e2017-09-07 12:23:19 +02002316 # check correct parameters
2317 if vnf_index not in vnf_index2vnf_uuid:
tiernob2880eb2017-10-04 15:04:53 +02002318 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2319 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2320 "'nsd':'constituent-vnfd'".format(
2321 str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])),
2322 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002323
tierno66eba6e2017-11-10 17:09:18 +01002324 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid', 'i.type as iface_type'),
tiernof1ba57e2017-09-07 12:23:19 +02002325 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2326 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2327 'external_name': get_str(iface, "vnfd-connection-point-ref",
2328 255)})
2329 if not existing_ifaces:
tiernob2880eb2017-10-04 15:04:53 +02002330 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2331 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2332 "connection-point name at VNFD '{}'".format(
2333 str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]),
2334 str(iface.get("vnfd-id-ref"))[:255]),
2335 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002336 interface_uuid = existing_ifaces[0]["uuid"]
tierno66eba6e2017-11-10 17:09:18 +01002337 if existing_ifaces[0]["iface_type"] == "data" and not db_sce_net["type"]:
2338 db_sce_net["type"] = "data"
tiernof1ba57e2017-09-07 12:23:19 +02002339 sce_interface_uuid = str(uuid4())
2340 uuid_list.append(sce_net_uuid)
tierno41a69812018-02-16 14:34:33 +01002341 iface_ip_address = None
2342 if iface.get("ip-address"):
2343 iface_ip_address = str(iface.get("ip-address"))
tiernof1ba57e2017-09-07 12:23:19 +02002344 db_sce_interface = {
2345 "uuid": sce_interface_uuid,
2346 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2347 "sce_net_id": sce_net_uuid,
2348 "interface_id": interface_uuid,
tierno41a69812018-02-16 14:34:33 +01002349 "ip_address": iface_ip_address,
tiernof1ba57e2017-09-07 12:23:19 +02002350 }
2351 db_sce_interfaces.append(db_sce_interface)
tierno66eba6e2017-11-10 17:09:18 +01002352 if not db_sce_net["type"]:
2353 db_sce_net["type"] = "bridge"
tiernof1ba57e2017-09-07 12:23:19 +02002354
Igor D.Ccaadc442017-11-06 12:48:48 +00002355 # table sce_vnffgs (vnffgd)
2356 for vnffg in nsd.get("vnffgd").itervalues():
2357 sce_vnffg_uuid = str(uuid4())
2358 uuid_list.append(sce_vnffg_uuid)
2359 db_sce_vnffg = {
2360 "uuid": sce_vnffg_uuid,
2361 "name": get_str(vnffg, "name", 255),
2362 "scenario_id": scenario_uuid,
2363 "vendor": get_str(vnffg, "vendor", 255),
2364 "description": get_str(vld, "description", 255),
2365 }
2366 db_sce_vnffgs.append(db_sce_vnffg)
2367
2368 # deal with rsps
2369 db_sce_rsps = []
2370 for rsp in vnffg.get("rsp").itervalues():
2371 sce_rsp_uuid = str(uuid4())
2372 uuid_list.append(sce_rsp_uuid)
2373 db_sce_rsp = {
2374 "uuid": sce_rsp_uuid,
2375 "name": get_str(rsp, "name", 255),
2376 "sce_vnffg_id": sce_vnffg_uuid,
2377 "id": get_str(rsp, "id", 255), # only useful to link with classifiers; will be removed later in the code
2378 }
2379 db_sce_rsps.append(db_sce_rsp)
2380 db_sce_rsp_hops = []
2381 for iface in rsp.get("vnfd-connection-point-ref").itervalues():
tierno16e3dd42018-04-24 12:52:40 +02002382 vnf_index = str(iface['member-vnf-index-ref'])
Igor D.Ccaadc442017-11-06 12:48:48 +00002383 if_order = int(iface['order'])
2384 # check correct parameters
2385 if vnf_index not in vnf_index2vnf_uuid:
2386 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2387 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2388 "'nsd':'constituent-vnfd'".format(
2389 str(nsd["id"]), str(rsp["id"]), str(iface["member-vnf-index-ref"])),
2390 HTTP_Bad_Request)
2391
2392 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2393 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2394 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2395 'external_name': get_str(iface, "vnfd-connection-point-ref",
2396 255)})
2397 if not existing_ifaces:
2398 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2399 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2400 "connection-point name at VNFD '{}'".format(
2401 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]),
2402 str(iface.get("vnfd-id-ref"))[:255]),
2403 HTTP_Bad_Request)
2404 interface_uuid = existing_ifaces[0]["uuid"]
2405 sce_rsp_hop_uuid = str(uuid4())
2406 uuid_list.append(sce_rsp_hop_uuid)
2407 db_sce_rsp_hop = {
2408 "uuid": sce_rsp_hop_uuid,
2409 "if_order": if_order,
2410 "interface_id": interface_uuid,
2411 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2412 "sce_rsp_id": sce_rsp_uuid,
2413 }
2414 db_sce_rsp_hops.append(db_sce_rsp_hop)
2415
2416 # deal with classifiers
2417 db_sce_classifiers = []
2418 for classifier in vnffg.get("classifier").itervalues():
2419 sce_classifier_uuid = str(uuid4())
2420 uuid_list.append(sce_classifier_uuid)
2421
2422 # source VNF
tierno16e3dd42018-04-24 12:52:40 +02002423 vnf_index = str(classifier['member-vnf-index-ref'])
Igor D.Ccaadc442017-11-06 12:48:48 +00002424 if vnf_index not in vnf_index2vnf_uuid:
2425 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point"
2426 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2427 "'nsd':'constituent-vnfd'".format(
2428 str(nsd["id"]), str(classifier["id"]), str(classifier["member-vnf-index-ref"])),
2429 HTTP_Bad_Request)
2430 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2431 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2432 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2433 'external_name': get_str(classifier, "vnfd-connection-point-ref",
2434 255)})
2435 if not existing_ifaces:
2436 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2437 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2438 "connection-point name at VNFD '{}'".format(
2439 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]),
2440 str(iface.get("vnfd-id-ref"))[:255]),
2441 HTTP_Bad_Request)
2442 interface_uuid = existing_ifaces[0]["uuid"]
2443
2444 db_sce_classifier = {
2445 "uuid": sce_classifier_uuid,
2446 "name": get_str(classifier, "name", 255),
2447 "sce_vnffg_id": sce_vnffg_uuid,
2448 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2449 "interface_id": interface_uuid,
2450 }
2451 rsp_id = get_str(classifier, "rsp-id-ref", 255)
2452 rsp = next((item for item in db_sce_rsps if item["id"] == rsp_id), None)
2453 db_sce_classifier["sce_rsp_id"] = rsp["uuid"]
2454 db_sce_classifiers.append(db_sce_classifier)
2455
2456 db_sce_classifier_matches = []
2457 for match in classifier.get("match-attributes").itervalues():
2458 sce_classifier_match_uuid = str(uuid4())
2459 uuid_list.append(sce_classifier_match_uuid)
2460 db_sce_classifier_match = {
2461 "uuid": sce_classifier_match_uuid,
2462 "ip_proto": get_str(match, "ip-proto", 2),
2463 "source_ip": get_str(match, "source-ip-address", 16),
2464 "destination_ip": get_str(match, "destination-ip-address", 16),
2465 "source_port": get_str(match, "source-port", 5),
2466 "destination_port": get_str(match, "destination-port", 5),
2467 "sce_classifier_id": sce_classifier_uuid,
2468 }
2469 db_sce_classifier_matches.append(db_sce_classifier_match)
2470 # TODO: vnf/cp keys
2471
2472 # remove unneeded id's in sce_rsps
2473 for rsp in db_sce_rsps:
2474 rsp.pop('id')
2475
tiernof1ba57e2017-09-07 12:23:19 +02002476 db_tables = [
2477 {"scenarios": db_scenarios},
2478 {"sce_nets": db_sce_nets},
2479 {"ip_profiles": db_ip_profiles},
2480 {"sce_vnfs": db_sce_vnfs},
2481 {"sce_interfaces": db_sce_interfaces},
Igor D.Ccaadc442017-11-06 12:48:48 +00002482 {"sce_vnffgs": db_sce_vnffgs},
2483 {"sce_rsps": db_sce_rsps},
2484 {"sce_rsp_hops": db_sce_rsp_hops},
2485 {"sce_classifiers": db_sce_classifiers},
2486 {"sce_classifier_matches": db_sce_classifier_matches},
tiernof1ba57e2017-09-07 12:23:19 +02002487 ]
2488
Igor D.Ccaadc442017-11-06 12:48:48 +00002489 logger.debug("new_nsd_v3 done: %s",
tiernof1ba57e2017-09-07 12:23:19 +02002490 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
2491 mydb.new_rows(db_tables, uuid_list)
2492 return nsd_uuid_list
tiernob2880eb2017-10-04 15:04:53 +02002493 except NfvoException:
2494 raise
tiernof1ba57e2017-09-07 12:23:19 +02002495 except Exception as e:
2496 logger.error("Exception {}".format(e))
2497 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
2498
2499
tierno7edb6752016-03-21 17:37:52 +01002500def edit_scenario(mydb, tenant_id, scenario_id, data):
2501 data["uuid"] = scenario_id
2502 data["tenant_id"] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02002503 c = mydb.edit_scenario( data )
2504 return c
tierno7edb6752016-03-21 17:37:52 +01002505
tiernob3d36742017-03-03 23:51:05 +01002506
tierno7edb6752016-03-21 17:37:52 +01002507def 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 +02002508 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00002509 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
2510 vims = {datacenter_id: myvim}
tierno392f2852016-05-13 12:28:55 +02002511 myvim_tenant = myvim['tenant_id']
tierno7edb6752016-03-21 17:37:52 +01002512 datacenter_name = myvim['name']
tiernoa2793912016-10-04 08:15:08 +00002513
tierno7edb6752016-03-21 17:37:52 +01002514 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02002515 try:
2516 #print "Checking that the scenario_id exists and getting the scenario dictionary"
tierno868220c2017-09-26 00:11:05 +02002517 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id)
tiernoa2793912016-10-04 08:15:08 +00002518 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
tiernoae4a8d12016-07-08 12:30:39 +02002519 scenarioDict['datacenter_id'] = datacenter_id
2520 #print '================scenarioDict======================='
2521 #print json.dumps(scenarioDict, indent=4)
2522 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
tierno42026a02017-02-10 15:13:40 +01002523
tiernoae4a8d12016-07-08 12:30:39 +02002524 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
2525 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002526
tiernoae4a8d12016-07-08 12:30:39 +02002527 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2528 auxNetDict['scenario'] = {}
tierno42026a02017-02-10 15:13:40 +01002529
tiernoae4a8d12016-07-08 12:30:39 +02002530 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2531 for sce_net in scenarioDict['nets']:
2532 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
tierno42026a02017-02-10 15:13:40 +01002533
tiernoae4a8d12016-07-08 12:30:39 +02002534 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
tierno7edb6752016-03-21 17:37:52 +01002535 myNetName = myNetName[0:255] #limit length
tiernoae4a8d12016-07-08 12:30:39 +02002536 myNetType = sce_net['type']
tierno7edb6752016-03-21 17:37:52 +01002537 myNetDict = {}
2538 myNetDict["name"] = myNetName
2539 myNetDict["type"] = myNetType
2540 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02002541 myNetIPProfile = sce_net.get('ip_profile', None)
tierno7edb6752016-03-21 17:37:52 +01002542 #TODO:
tiernoae4a8d12016-07-08 12:30:39 +02002543 #We should use the dictionary as input parameter for new_network
tiernof97fd272016-07-11 14:32:37 +02002544 #print myNetDict
tiernoae4a8d12016-07-08 12:30:39 +02002545 if not sce_net["external"]:
garciadeblas9f8456e2016-09-05 05:02:59 +02002546 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02002547 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2548 sce_net['vim_id'] = network_id
2549 auxNetDict['scenario'][sce_net['uuid']] = network_id
2550 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002551 sce_net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02002552 else:
2553 if sce_net['vim_id'] == None:
2554 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
2555 _, message = rollback(mydb, vims, rollbackList)
2556 logger.error("nfvo.start_scenario: %s", error_text)
tiernof97fd272016-07-11 14:32:37 +02002557 raise NfvoException(error_text, HTTP_Bad_Request)
tiernoae4a8d12016-07-08 12:30:39 +02002558 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
2559 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
tierno42026a02017-02-10 15:13:40 +01002560
tiernoae4a8d12016-07-08 12:30:39 +02002561 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2562 #For each vnf net, we create it and we add it to instanceNetlist.
mirabal29356312017-07-27 12:21:22 +02002563
tiernoae4a8d12016-07-08 12:30:39 +02002564 for sce_vnf in scenarioDict['vnfs']:
2565 for net in sce_vnf['nets']:
2566 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
tierno42026a02017-02-10 15:13:40 +01002567
tiernoae4a8d12016-07-08 12:30:39 +02002568 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
2569 myNetName = myNetName[0:255] #limit length
2570 myNetType = net['type']
2571 myNetDict = {}
2572 myNetDict["name"] = myNetName
2573 myNetDict["type"] = myNetType
2574 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02002575 myNetIPProfile = net.get('ip_profile', None)
tiernoae4a8d12016-07-08 12:30:39 +02002576 #print myNetDict
2577 #TODO:
2578 #We should use the dictionary as input parameter for new_network
garciadeblas9f8456e2016-09-05 05:02:59 +02002579 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02002580 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2581 net['vim_id'] = network_id
2582 if sce_vnf['uuid'] not in auxNetDict:
2583 auxNetDict[sce_vnf['uuid']] = {}
2584 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
2585 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002586 net["created"] = True
tierno42026a02017-02-10 15:13:40 +01002587
tiernoae4a8d12016-07-08 12:30:39 +02002588 #print "auxNetDict:"
2589 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002590
tiernoae4a8d12016-07-08 12:30:39 +02002591 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
2592 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2593 i = 0
2594 for sce_vnf in scenarioDict['vnfs']:
tierno5a3273c2017-08-29 11:43:46 +02002595 vnf_availability_zones = []
mirabal29356312017-07-27 12:21:22 +02002596 for vm in sce_vnf['vms']:
2597 vm_av = vm.get('availability_zone')
tierno5a3273c2017-08-29 11:43:46 +02002598 if vm_av and vm_av not in vnf_availability_zones:
2599 vnf_availability_zones.append(vm_av)
2600
2601 # check if there is enough availability zones available at vim level.
2602 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
2603 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
2604 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request)
2605
tiernoae4a8d12016-07-08 12:30:39 +02002606 for vm in sce_vnf['vms']:
2607 i += 1
2608 myVMDict = {}
2609 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
tiernoae65a482016-11-24 16:20:05 +01002610 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
tiernoae4a8d12016-07-08 12:30:39 +02002611 #myVMDict['description'] = vm['description']
2612 myVMDict['description'] = myVMDict['name'][0:99]
2613 if not startvms:
2614 myVMDict['start'] = "no"
2615 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2616 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
tierno42026a02017-02-10 15:13:40 +01002617
tiernoae4a8d12016-07-08 12:30:39 +02002618 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002619 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno42026a02017-02-10 15:13:40 +01002620 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02002621 vm['vim_image_id'] = image_id
tierno42026a02017-02-10 15:13:40 +01002622
tiernoae4a8d12016-07-08 12:30:39 +02002623 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002624 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tiernoae4a8d12016-07-08 12:30:39 +02002625 if flavor_dict['extended']!=None:
2626 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
tierno42026a02017-02-10 15:13:40 +01002627 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02002628 vm['vim_flavor_id'] = flavor_id
tierno42026a02017-02-10 15:13:40 +01002629
2630
tiernoae4a8d12016-07-08 12:30:39 +02002631 myVMDict['imageRef'] = vm['vim_image_id']
2632 myVMDict['flavorRef'] = vm['vim_flavor_id']
2633 myVMDict['networks'] = []
2634 for iface in vm['interfaces']:
2635 netDict = {}
2636 if iface['type']=="data":
2637 netDict['type'] = iface['model']
2638 elif "model" in iface and iface["model"]!=None:
2639 netDict['model']=iface['model']
2640 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2641 #discover type of interface looking at flavor
2642 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2643 for flavor_iface in numa.get('interfaces',[]):
2644 if flavor_iface.get('name') == iface['internal_name']:
2645 if flavor_iface['dedicated'] == 'yes':
2646 netDict['type']="PF" #passthrough
2647 elif flavor_iface['dedicated'] == 'no':
2648 netDict['type']="VF" #siov
2649 elif flavor_iface['dedicated'] == 'yes:sriov':
2650 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2651 netDict["mac_address"] = flavor_iface.get("mac_address")
2652 break;
2653 netDict["use"]=iface['type']
2654 if netDict["use"]=="data" and not netDict.get("type"):
2655 #print "netDict", netDict
2656 #print "iface", iface
2657 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'])
2658 if flavor_dict.get('extended')==None:
tiernof97fd272016-07-11 14:32:37 +02002659 raise NfvoException(e_text + "After database migration some information is not available. \
2660 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tiernoae4a8d12016-07-08 12:30:39 +02002661 else:
tiernof97fd272016-07-11 14:32:37 +02002662 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tiernoae4a8d12016-07-08 12:30:39 +02002663 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2664 netDict["type"]="virtual"
2665 if "vpci" in iface and iface["vpci"] is not None:
2666 netDict['vpci'] = iface['vpci']
2667 if "mac" in iface and iface["mac"] is not None:
2668 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00002669 if "port-security" in iface and iface["port-security"] is not None:
2670 netDict['port_security'] = iface['port-security']
2671 if "floating-ip" in iface and iface["floating-ip"] is not None:
2672 netDict['floating_ip'] = iface['floating-ip']
tiernoae4a8d12016-07-08 12:30:39 +02002673 netDict['name'] = iface['internal_name']
2674 if iface['net_id'] is None:
2675 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02002676 #print iface
2677 #print vnf_iface
tiernoae4a8d12016-07-08 12:30:39 +02002678 if vnf_iface['interface_id']==iface['uuid']:
2679 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
2680 break
2681 else:
2682 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
2683 #skip bridge ifaces not connected to any net
2684 #if 'net_id' not in netDict or netDict['net_id']==None:
2685 # continue
2686 myVMDict['networks'].append(netDict)
2687 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2688 #print myVMDict['name']
2689 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2690 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2691 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
mirabal29356312017-07-27 12:21:22 +02002692
2693 if 'availability_zone' in myVMDict:
tierno5a3273c2017-08-29 11:43:46 +02002694 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
mirabal29356312017-07-27 12:21:22 +02002695 else:
tierno5a3273c2017-08-29 11:43:46 +02002696 av_index = None
mirabal29356312017-07-27 12:21:22 +02002697
tierno98e909c2017-10-14 13:27:03 +02002698 vm_id, _ = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None),
mirabal29356312017-07-27 12:21:22 +02002699 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'],
tierno5a3273c2017-08-29 11:43:46 +02002700 availability_zone_index=av_index,
2701 availability_zone_list=vnf_availability_zones)
tiernoae4a8d12016-07-08 12:30:39 +02002702 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2703 vm['vim_id'] = vm_id
2704 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2705 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2706 for net in myVMDict['networks']:
2707 if "vim_id" in net:
2708 for iface in vm['interfaces']:
2709 if net["name"]==iface["internal_name"]:
2710 iface["vim_id"]=net["vim_id"]
2711 break
tierno42026a02017-02-10 15:13:40 +01002712
tiernoae4a8d12016-07-08 12:30:39 +02002713 logger.debug("start scenario Deployment done")
2714 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2715 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
tiernof97fd272016-07-11 14:32:37 +02002716 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
2717 return mydb.get_instance_scenario(instance_id)
tierno42026a02017-02-10 15:13:40 +01002718
tiernof97fd272016-07-11 14:32:37 +02002719 except (db_base_Exception, vimconn.vimconnException) as e:
tiernoae4a8d12016-07-08 12:30:39 +02002720 _, message = rollback(mydb, vims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02002721 if isinstance(e, db_base_Exception):
2722 error_text = "Exception at database"
2723 else:
2724 error_text = "Exception at VIM"
2725 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2726 #logger.error("start_scenario %s", error_text)
2727 raise NfvoException(error_text, e.http_code)
tierno7edb6752016-03-21 17:37:52 +01002728
tierno36c0b172017-01-12 18:32:28 +01002729def unify_cloud_config(cloud_config_preserve, cloud_config):
tierno40e1bce2017-08-09 09:12:04 +02002730 """ join the cloud config information into cloud_config_preserve.
tierno36c0b172017-01-12 18:32:28 +01002731 In case of conflict cloud_config_preserve preserves
tierno40e1bce2017-08-09 09:12:04 +02002732 None is allowed
2733 """
tierno36c0b172017-01-12 18:32:28 +01002734 if not cloud_config_preserve and not cloud_config:
2735 return None
2736
2737 new_cloud_config = {"key-pairs":[], "users":[]}
2738 # key-pairs
2739 if cloud_config_preserve:
2740 for key in cloud_config_preserve.get("key-pairs", () ):
2741 if key not in new_cloud_config["key-pairs"]:
2742 new_cloud_config["key-pairs"].append(key)
2743 if cloud_config:
2744 for key in cloud_config.get("key-pairs", () ):
2745 if key not in new_cloud_config["key-pairs"]:
2746 new_cloud_config["key-pairs"].append(key)
2747 if not new_cloud_config["key-pairs"]:
2748 del new_cloud_config["key-pairs"]
2749
2750 # users
2751 if cloud_config:
2752 new_cloud_config["users"] += cloud_config.get("users", () )
2753 if cloud_config_preserve:
2754 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
tiernoa4e1a6e2016-08-31 14:19:40 +02002755 index_to_delete = []
tierno36c0b172017-01-12 18:32:28 +01002756 users = new_cloud_config.get("users", [])
tiernoa4e1a6e2016-08-31 14:19:40 +02002757 for index0 in range(0,len(users)):
2758 if index0 in index_to_delete:
2759 continue
2760 for index1 in range(index0+1,len(users)):
2761 if index1 in index_to_delete:
2762 continue
2763 if users[index0]["name"] == users[index1]["name"]:
2764 index_to_delete.append(index1)
2765 for key in users[index1].get("key-pairs",()):
tierno36c0b172017-01-12 18:32:28 +01002766 if "key-pairs" not in users[index0]:
tiernoa4e1a6e2016-08-31 14:19:40 +02002767 users[index0]["key-pairs"] = [key]
2768 elif key not in users[index0]["key-pairs"]:
2769 users[index0]["key-pairs"].append(key)
2770 index_to_delete.sort(reverse=True)
2771 for index in index_to_delete:
2772 del users[index]
tierno36c0b172017-01-12 18:32:28 +01002773 if not new_cloud_config["users"]:
2774 del new_cloud_config["users"]
2775
2776 #boot-data-drive
2777 if cloud_config and cloud_config.get("boot-data-drive") != None:
2778 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
2779 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
2780 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
2781
2782 # user-data
tierno40e1bce2017-08-09 09:12:04 +02002783 new_cloud_config["user-data"] = []
2784 if cloud_config and cloud_config.get("user-data"):
2785 if isinstance(cloud_config["user-data"], list):
2786 new_cloud_config["user-data"] += cloud_config["user-data"]
2787 else:
2788 new_cloud_config["user-data"].append(cloud_config["user-data"])
2789 if cloud_config_preserve and cloud_config_preserve.get("user-data"):
2790 if isinstance(cloud_config_preserve["user-data"], list):
2791 new_cloud_config["user-data"] += cloud_config_preserve["user-data"]
2792 else:
2793 new_cloud_config["user-data"].append(cloud_config_preserve["user-data"])
2794 if not new_cloud_config["user-data"]:
2795 del new_cloud_config["user-data"]
tierno36c0b172017-01-12 18:32:28 +01002796
2797 # config files
2798 new_cloud_config["config-files"] = []
2799 if cloud_config and cloud_config.get("config-files") != None:
2800 new_cloud_config["config-files"] += cloud_config["config-files"]
2801 if cloud_config_preserve:
2802 for file in cloud_config_preserve.get("config-files", ()):
2803 for index in range(0, len(new_cloud_config["config-files"])):
2804 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
2805 new_cloud_config["config-files"][index] = file
2806 break
2807 else:
2808 new_cloud_config["config-files"].append(file)
2809 if not new_cloud_config["config-files"]:
2810 del new_cloud_config["config-files"]
2811 return new_cloud_config
2812
2813
tierno867ffe92017-03-27 12:50:34 +02002814def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
tiernob3d36742017-03-03 23:51:05 +01002815 datacenter_id = None
2816 datacenter_name = None
2817 thread = None
tierno867ffe92017-03-27 12:50:34 +02002818 try:
2819 if datacenter_tenant_id:
2820 thread_id = datacenter_tenant_id
2821 thread = vim_threads["running"].get(datacenter_tenant_id)
tiernob3d36742017-03-03 23:51:05 +01002822 else:
tierno867ffe92017-03-27 12:50:34 +02002823 where_={"td.nfvo_tenant_id": tenant_id}
2824 if datacenter_id_name:
2825 if utils.check_valid_uuid(datacenter_id_name):
2826 datacenter_id = datacenter_id_name
2827 where_["dt.datacenter_id"] = datacenter_id
2828 else:
2829 datacenter_name = datacenter_id_name
2830 where_["d.name"] = datacenter_name
2831 if datacenter_tenant_id:
2832 where_["dt.uuid"] = datacenter_tenant_id
2833 datacenters = mydb.get_rows(
2834 SELECT=("dt.uuid as datacenter_tenant_id",),
2835 FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2836 "join datacenters as d on d.uuid=dt.datacenter_id",
2837 WHERE=where_)
2838 if len(datacenters) > 1:
2839 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
2840 elif datacenters:
2841 thread_id = datacenters[0]["datacenter_tenant_id"]
2842 thread = vim_threads["running"].get(thread_id)
2843 if not thread:
2844 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
2845 return thread_id, thread
2846 except db_base_Exception as e:
2847 raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code)
tiernoa4e1a6e2016-08-31 14:19:40 +02002848
tiernof5755962017-07-13 15:44:34 +02002849
tiernoa15c4b92017-10-05 12:41:44 +02002850def get_datacenter_uuid(mydb, tenant_id, datacenter_id_name):
2851 WHERE_dict={}
2852 if utils.check_valid_uuid(datacenter_id_name):
2853 WHERE_dict['d.uuid'] = datacenter_id_name
2854 else:
2855 WHERE_dict['d.name'] = datacenter_id_name
2856
2857 if tenant_id:
2858 WHERE_dict['nfvo_tenant_id'] = tenant_id
2859 from_= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
2860 " dt on td.datacenter_tenant_id=dt.uuid"
2861 else:
2862 from_ = 'datacenters as d'
2863 vimaccounts = mydb.get_rows(FROM=from_, SELECT=("d.uuid as uuid",), WHERE=WHERE_dict )
2864 if len(vimaccounts) == 0:
2865 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
2866 elif len(vimaccounts)>1:
2867 #print "nfvo.datacenter_action() error. Several datacenters found"
2868 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
2869 return vimaccounts[0]["uuid"]
2870
2871
tiernoa2793912016-10-04 08:15:08 +00002872def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
tiernobe41e222016-09-02 15:16:13 +02002873 datacenter_id = None
2874 datacenter_name = None
2875 if datacenter_id_name:
tierno42026a02017-02-10 15:13:40 +01002876 if utils.check_valid_uuid(datacenter_id_name):
tiernobe41e222016-09-02 15:16:13 +02002877 datacenter_id = datacenter_id_name
2878 else:
2879 datacenter_name = datacenter_id_name
tiernoa2793912016-10-04 08:15:08 +00002880 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
tiernobe41e222016-09-02 15:16:13 +02002881 if len(vims) == 0:
2882 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
2883 elif len(vims)>1:
2884 #print "nfvo.datacenter_action() error. Several datacenters found"
2885 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
2886 return vims.keys()[0], vims.values()[0]
2887
tiernob3d36742017-03-03 23:51:05 +01002888
garciadeblas9f8456e2016-09-05 05:02:59 +02002889def update(d, u):
2890 '''Takes dict d and updates it with the values in dict u.'''
2891 '''It merges all depth levels'''
2892 for k, v in u.iteritems():
2893 if isinstance(v, collections.Mapping):
2894 r = update(d.get(k, {}), v)
2895 d[k] = r
2896 else:
2897 d[k] = u[k]
2898 return d
2899
tierno16e3dd42018-04-24 12:52:40 +02002900
tierno7edb6752016-03-21 17:37:52 +01002901def create_instance(mydb, tenant_id, instance_dict):
tiernob3d36742017-03-03 23:51:05 +01002902 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2903 # logger.debug("Creating instance...")
tierno7edb6752016-03-21 17:37:52 +01002904 scenario = instance_dict["scenario"]
tierno42026a02017-02-10 15:13:40 +01002905
tierno868220c2017-09-26 00:11:05 +02002906 # find main datacenter
tiernobe41e222016-09-02 15:16:13 +02002907 myvims = {}
tierno867ffe92017-03-27 12:50:34 +02002908 myvim_threads_id = {}
tierno7edb6752016-03-21 17:37:52 +01002909 datacenter = instance_dict.get("datacenter")
tiernobe41e222016-09-02 15:16:13 +02002910 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
2911 myvims[default_datacenter_id] = vim
tierno867ffe92017-03-27 12:50:34 +02002912 myvim_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id)
gcalvinoe580c7d2017-09-22 14:09:51 +02002913 tenant = mydb.get_rows_by_id('nfvo_tenants', tenant_id)
tierno868220c2017-09-26 00:11:05 +02002914 # myvim_tenant = myvim['tenant_id']
tierno16e3dd42018-04-24 12:52:40 +02002915 rollbackList = []
tierno42026a02017-02-10 15:13:40 +01002916
tierno868220c2017-09-26 00:11:05 +02002917 # print "Checking that the scenario exists and getting the scenario dictionary"
2918 scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_vim_id=myvim_threads_id[default_datacenter_id],
2919 datacenter_id=default_datacenter_id)
tierno42026a02017-02-10 15:13:40 +01002920
tierno868220c2017-09-26 00:11:05 +02002921 # logger.debug(">>>>>> Dictionaries before merging")
2922 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
2923 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
tierno42026a02017-02-10 15:13:40 +01002924
tierno868220c2017-09-26 00:11:05 +02002925 db_instance_vnfs = []
2926 db_instance_vms = []
2927 db_instance_interfaces = []
Igor D.Ccaadc442017-11-06 12:48:48 +00002928 db_instance_sfis = []
2929 db_instance_sfs = []
2930 db_instance_classifications = []
2931 db_instance_sfps = []
tierno868220c2017-09-26 00:11:05 +02002932 db_ip_profiles = []
2933 db_vim_actions = []
tierno8e690322017-08-10 15:58:50 +02002934 uuid_list = []
tierno868220c2017-09-26 00:11:05 +02002935 task_index = 0
tierno8e690322017-08-10 15:58:50 +02002936 instance_name = instance_dict["name"]
2937 instance_uuid = str(uuid4())
2938 uuid_list.append(instance_uuid)
2939 db_instance_scenario = {
2940 "uuid": instance_uuid,
2941 "name": instance_name,
2942 "tenant_id": tenant_id,
2943 "scenario_id": scenarioDict['uuid'],
2944 "datacenter_id": default_datacenter_id,
2945 # filled bellow 'datacenter_tenant_id'
2946 "description": instance_dict.get("description"),
2947 }
tierno8e690322017-08-10 15:58:50 +02002948 if scenarioDict.get("cloud-config"):
2949 db_instance_scenario["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"],
2950 default_flow_style=True, width=256)
tierno868220c2017-09-26 00:11:05 +02002951 instance_action_id = get_task_id()
2952 db_instance_action = {
2953 "uuid": instance_action_id, # same uuid for the instance and the action on create
2954 "tenant_id": tenant_id,
2955 "instance_id": instance_uuid,
2956 "description": "CREATE",
2957 }
garciadeblas9f8456e2016-09-05 05:02:59 +02002958
tierno868220c2017-09-26 00:11:05 +02002959 # Auxiliary dictionaries from x to y
tierno8e690322017-08-10 15:58:50 +02002960 sce_net2instance = {}
tierno868220c2017-09-26 00:11:05 +02002961 net2task_id = {'scenario': {}}
tierno42026a02017-02-10 15:13:40 +01002962
tierno868220c2017-09-26 00:11:05 +02002963 # logger.debug("Creating instance from scenario-dict:\n%s",
2964 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01002965 try:
tiernob3d36742017-03-03 23:51:05 +01002966 # 0 check correct parameters
tierno868220c2017-09-26 00:11:05 +02002967 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
tiernob3d36742017-03-03 23:51:05 +01002968 found = False
tierno7edb6752016-03-21 17:37:52 +01002969 for scenario_net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +02002970 if net_name == scenario_net["name"]:
tierno7edb6752016-03-21 17:37:52 +01002971 found = True
2972 break
2973 if not found:
tierno868220c2017-09-26 00:11:05 +02002974 raise NfvoException("Invalid scenario network name '{}' at instance:networks".format(net_name),
2975 HTTP_Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02002976 if "sites" not in net_instance_desc:
2977 net_instance_desc["sites"] = [ {} ]
2978 site_without_datacenter_field = False
2979 for site in net_instance_desc["sites"]:
2980 if site.get("datacenter"):
tiernoa15c4b92017-10-05 12:41:44 +02002981 site["datacenter"] = get_datacenter_uuid(mydb, tenant_id, site["datacenter"])
tiernobe41e222016-09-02 15:16:13 +02002982 if site["datacenter"] not in myvims:
tierno868220c2017-09-26 00:11:05 +02002983 # Add this datacenter to myvims
tiernobe41e222016-09-02 15:16:13 +02002984 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
2985 myvims[d] = v
tierno868220c2017-09-26 00:11:05 +02002986 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, site["datacenter"])
2987 site["datacenter"] = d # change name to id
tiernobe41e222016-09-02 15:16:13 +02002988 else:
2989 if site_without_datacenter_field:
tierno868220c2017-09-26 00:11:05 +02002990 raise NfvoException("Found more than one entries without datacenter field at "
2991 "instance:networks:{}:sites".format(net_name), HTTP_Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02002992 site_without_datacenter_field = True
tierno868220c2017-09-26 00:11:05 +02002993 site["datacenter"] = default_datacenter_id # change name to id
tierno42026a02017-02-10 15:13:40 +01002994
tiernobe41e222016-09-02 15:16:13 +02002995 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
tierno868220c2017-09-26 00:11:05 +02002996 found = False
tierno7edb6752016-03-21 17:37:52 +01002997 for scenario_vnf in scenarioDict['vnfs']:
tierno92c36fd2018-05-04 12:21:10 +02002998 if vnf_name == scenario_vnf['name'] or vnf_name == scenario_vnf['member_vnf_index']:
tierno7edb6752016-03-21 17:37:52 +01002999 found = True
3000 break
3001 if not found:
tierno92c36fd2018-05-04 12:21:10 +02003002 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name), HTTP_Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02003003 if "datacenter" in vnf_instance_desc:
tierno868220c2017-09-26 00:11:05 +02003004 # Add this datacenter to myvims
tiernoa15c4b92017-10-05 12:41:44 +02003005 vnf_instance_desc["datacenter"] = get_datacenter_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
tiernobe41e222016-09-02 15:16:13 +02003006 if vnf_instance_desc["datacenter"] not in myvims:
3007 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
3008 myvims[d] = v
tierno868220c2017-09-26 00:11:05 +02003009 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"])
tiernoa2793912016-10-04 08:15:08 +00003010 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
garciadeblas30833382017-01-09 09:46:31 +01003011
tierno868220c2017-09-26 00:11:05 +02003012 # 0.1 parse cloud-config parameters
tierno36c0b172017-01-12 18:32:28 +01003013 cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config"))
garciadeblas9f8456e2016-09-05 05:02:59 +02003014
tierno868220c2017-09-26 00:11:05 +02003015 # 0.2 merge instance information into scenario
3016 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
3017 # However, this is not possible yet.
tierno41a69812018-02-16 14:34:33 +01003018 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
garciadeblas9f8456e2016-09-05 05:02:59 +02003019 for scenario_net in scenarioDict['nets']:
3020 if net_name == scenario_net["name"]:
3021 if 'ip-profile' in net_instance_desc:
tierno455612d2017-05-30 16:40:10 +02003022 # translate from input format to database format
3023 ipprofile_in = net_instance_desc['ip-profile']
3024 ipprofile_db = {}
3025 ipprofile_db['subnet_address'] = ipprofile_in.get('subnet-address')
3026 ipprofile_db['ip_version'] = ipprofile_in.get('ip-version', 'IPv4')
3027 ipprofile_db['gateway_address'] = ipprofile_in.get('gateway-address')
3028 ipprofile_db['dns_address'] = ipprofile_in.get('dns-address')
3029 if isinstance(ipprofile_db['dns_address'], (list, tuple)):
3030 ipprofile_db['dns_address'] = ";".join(ipprofile_db['dns_address'])
3031 if 'dhcp' in ipprofile_in:
3032 ipprofile_db['dhcp_start_address'] = ipprofile_in['dhcp'].get('start-address')
3033 ipprofile_db['dhcp_enabled'] = ipprofile_in['dhcp'].get('enabled', True)
3034 ipprofile_db['dhcp_count'] = ipprofile_in['dhcp'].get('count' )
garciadeblasedca7b32016-09-29 14:01:52 +00003035 if 'ip_profile' not in scenario_net:
tierno455612d2017-05-30 16:40:10 +02003036 scenario_net['ip_profile'] = ipprofile_db
garciadeblasedca7b32016-09-29 14:01:52 +00003037 else:
tierno455612d2017-05-30 16:40:10 +02003038 update(scenario_net['ip_profile'], ipprofile_db)
tierno41a69812018-02-16 14:34:33 +01003039 for interface in net_instance_desc.get('interfaces', ()):
garciadeblas9f8456e2016-09-05 05:02:59 +02003040 if 'ip_address' in interface:
3041 for vnf in scenarioDict['vnfs']:
3042 if interface['vnf'] == vnf['name']:
3043 for vnf_interface in vnf['interfaces']:
3044 if interface['vnf_interface'] == vnf_interface['external_name']:
tierno41a69812018-02-16 14:34:33 +01003045 vnf_interface['ip_address'] = interface['ip_address']
garciadeblas9f8456e2016-09-05 05:02:59 +02003046
tierno868220c2017-09-26 00:11:05 +02003047 # logger.debug(">>>>>>>> Merged dictionary")
3048 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
3049 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
garciadeblas9f8456e2016-09-05 05:02:59 +02003050
tiernob3d36742017-03-03 23:51:05 +01003051 # 1. Creating new nets (sce_nets) in the VIM"
tierno8f79ea12018-05-03 17:37:40 +02003052 number_mgmt_networks = 0
tierno8e690322017-08-10 15:58:50 +02003053 db_instance_nets = []
tierno7edb6752016-03-21 17:37:52 +01003054 for sce_net in scenarioDict['nets']:
tierno868220c2017-09-26 00:11:05 +02003055 descriptor_net = instance_dict.get("networks", {}).get(sce_net["name"], {})
tiernobe41e222016-09-02 15:16:13 +02003056 net_name = descriptor_net.get("vim-network-name")
tierno8e690322017-08-10 15:58:50 +02003057 sce_net2instance[sce_net['uuid']] = {}
tierno868220c2017-09-26 00:11:05 +02003058 net2task_id['scenario'][sce_net['uuid']] = {}
tiernobe41e222016-09-02 15:16:13 +02003059
3060 sites = descriptor_net.get("sites", [ {} ])
3061 for site in sites:
3062 if site.get("datacenter"):
3063 vim = myvims[ site["datacenter"] ]
3064 datacenter_id = site["datacenter"]
tierno867ffe92017-03-27 12:50:34 +02003065 myvim_thread_id = myvim_threads_id[ site["datacenter"] ]
tierno7edb6752016-03-21 17:37:52 +01003066 else:
tiernobe41e222016-09-02 15:16:13 +02003067 vim = myvims[ default_datacenter_id ]
3068 datacenter_id = default_datacenter_id
tierno867ffe92017-03-27 12:50:34 +02003069 myvim_thread_id = myvim_threads_id[default_datacenter_id]
tiernobe41e222016-09-02 15:16:13 +02003070 net_type = sce_net['type']
tierno868220c2017-09-26 00:11:05 +02003071 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
tierno42026a02017-02-10 15:13:40 +01003072
tiernof1ba57e2017-09-07 12:23:19 +02003073 if not net_name:
3074 if sce_net["external"]:
3075 net_name = sce_net["name"]
3076 else:
3077 net_name = "{}.{}".format(instance_name, sce_net["name"])
3078 net_name = net_name[:255] # limit length
3079
tierno8f79ea12018-05-03 17:37:40 +02003080 if sce_net["external"]:
3081 number_mgmt_networks += 1
tiernof1ba57e2017-09-07 12:23:19 +02003082 if "netmap-use" in site or "netmap-create" in site:
3083 create_network = False
3084 lookfor_network = False
3085 if "netmap-use" in site:
3086 lookfor_network = True
3087 if utils.check_valid_uuid(site["netmap-use"]):
tiernof1ba57e2017-09-07 12:23:19 +02003088 lookfor_filter["id"] = site["netmap-use"]
3089 else:
tiernof1ba57e2017-09-07 12:23:19 +02003090 lookfor_filter["name"] = site["netmap-use"]
3091 if "netmap-create" in site:
3092 create_network = True
3093 net_vim_name = net_name
3094 if site["netmap-create"]:
3095 net_vim_name = site["netmap-create"]
tierno8f79ea12018-05-03 17:37:40 +02003096 elif sce_net.get("vim_network_name"):
3097 create_network = False
3098 lookfor_network = True
3099 lookfor_filter["name"] = sce_net.get("vim_network_name")
tiernof1ba57e2017-09-07 12:23:19 +02003100 elif sce_net["external"]:
3101 if sce_net['vim_id'] != None:
tierno868220c2017-09-26 00:11:05 +02003102 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
tiernobe41e222016-09-02 15:16:13 +02003103 create_network = False
3104 lookfor_network = True
3105 lookfor_filter["id"] = sce_net['vim_id']
tierno8f79ea12018-05-03 17:37:40 +02003106 elif vim["config"].get("management_network_id") or vim["config"].get("management_network_name"):
3107 if number_mgmt_networks > 1:
3108 raise NfvoException("Found several VLD of type mgmt. "
3109 "You must concrete what vim-network must be use for each one",
3110 HTTP_Bad_Request)
3111 create_network = False
3112 lookfor_network = True
3113 if vim["config"].get("management_network_id"):
3114 lookfor_filter["id"] = vim["config"]["management_network_id"]
3115 else:
3116 lookfor_filter["name"] = vim["config"]["management_network_name"]
tiernobe41e222016-09-02 15:16:13 +02003117 else:
tierno868220c2017-09-26 00:11:05 +02003118 # There is not a netmap, look at datacenter for a net with this name and create if not found
tiernobe41e222016-09-02 15:16:13 +02003119 create_network = True
3120 lookfor_network = True
3121 lookfor_filter["name"] = sce_net["name"]
3122 net_vim_name = sce_net["name"]
tierno7edb6752016-03-21 17:37:52 +01003123 else:
tiernobe41e222016-09-02 15:16:13 +02003124 net_vim_name = net_name
3125 create_network = True
3126 lookfor_network = False
tierno42026a02017-02-10 15:13:40 +01003127
tiernof1450872017-10-17 23:15:08 +02003128 task_extra = {}
3129 if create_network:
3130 task_action = "CREATE"
3131 task_extra["params"] = (net_vim_name, net_type, sce_net.get('ip_profile', None))
3132 if lookfor_network:
3133 task_extra["find"] = (lookfor_filter,)
tierno868220c2017-09-26 00:11:05 +02003134 elif lookfor_network:
3135 task_action = "FIND"
tiernof1450872017-10-17 23:15:08 +02003136 task_extra["params"] = (lookfor_filter,)
tierno42026a02017-02-10 15:13:40 +01003137
tierno8e690322017-08-10 15:58:50 +02003138 # fill database content
3139 net_uuid = str(uuid4())
3140 uuid_list.append(net_uuid)
3141 sce_net2instance[sce_net['uuid']][datacenter_id] = net_uuid
3142 db_net = {
3143 "uuid": net_uuid,
tierno868220c2017-09-26 00:11:05 +02003144 'vim_net_id': None,
tierno8e690322017-08-10 15:58:50 +02003145 "instance_scenario_id": instance_uuid,
3146 "sce_net_id": sce_net["uuid"],
3147 "created": create_network,
3148 'datacenter_id': datacenter_id,
3149 'datacenter_tenant_id': myvim_thread_id,
3150 'status': 'BUILD' if create_network else "ACTIVE"
3151 }
3152 db_instance_nets.append(db_net)
tierno868220c2017-09-26 00:11:05 +02003153 db_vim_action = {
3154 "instance_action_id": instance_action_id,
3155 "status": "SCHEDULED",
3156 "task_index": task_index,
3157 "datacenter_vim_id": myvim_thread_id,
3158 "action": task_action,
3159 "item": "instance_nets",
3160 "item_id": net_uuid,
tiernof1450872017-10-17 23:15:08 +02003161 "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256)
tierno868220c2017-09-26 00:11:05 +02003162 }
3163 net2task_id['scenario'][sce_net['uuid']][datacenter_id] = task_index
3164 task_index += 1
3165 db_vim_actions.append(db_vim_action)
3166
tierno8e690322017-08-10 15:58:50 +02003167 if 'ip_profile' in sce_net:
3168 db_ip_profile={
3169 'instance_net_id': net_uuid,
3170 'ip_version': sce_net['ip_profile']['ip_version'],
3171 'subnet_address': sce_net['ip_profile']['subnet_address'],
3172 'gateway_address': sce_net['ip_profile']['gateway_address'],
3173 'dns_address': sce_net['ip_profile']['dns_address'],
3174 'dhcp_enabled': sce_net['ip_profile']['dhcp_enabled'],
3175 'dhcp_start_address': sce_net['ip_profile']['dhcp_start_address'],
3176 'dhcp_count': sce_net['ip_profile']['dhcp_count'],
3177 }
3178 db_ip_profiles.append(db_ip_profile)
3179
tierno16e3dd42018-04-24 12:52:40 +02003180 # Create VNFs
3181 vnf_params = {
3182 "default_datacenter_id": default_datacenter_id,
3183 "myvim_threads_id": myvim_threads_id,
3184 "instance_uuid": instance_uuid,
3185 "instance_name": instance_name,
3186 "instance_action_id": instance_action_id,
3187 "myvims": myvims,
3188 "cloud_config": cloud_config,
3189 "RO_pub_key": tenant[0].get('RO_pub_key'),
3190 }
3191 vnf_params_out = {
3192 "task_index": task_index,
3193 "uuid_list": uuid_list,
3194 "db_instance_nets": db_instance_nets,
3195 "db_vim_actions": db_vim_actions,
3196 "db_ip_profiles": db_ip_profiles,
3197 "db_instance_vnfs": db_instance_vnfs,
3198 "db_instance_vms": db_instance_vms,
3199 "db_instance_interfaces": db_instance_interfaces,
3200 "net2task_id": net2task_id,
3201 "sce_net2instance": sce_net2instance,
3202 }
3203 sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
garciadeblasacd4e782017-07-23 19:44:55 +02003204 for sce_vnf in sce_vnf_list:
tierno16e3dd42018-04-24 12:52:40 +02003205 instantiate_vnf(mydb, sce_vnf, vnf_params, vnf_params_out, rollbackList)
3206 task_index = vnf_params_out["task_index"]
3207 uuid_list = vnf_params_out["uuid_list"]
mirabal29356312017-07-27 12:21:22 +02003208
tierno16e3dd42018-04-24 12:52:40 +02003209 # Create VNFFGs
3210 # task_depends_on = []
Igor D.Ccaadc442017-11-06 12:48:48 +00003211 for vnffg in scenarioDict['vnffgs']:
3212 for rsp in vnffg['rsps']:
3213 sfs_created = []
3214 for cp in rsp['connection_points']:
3215 count = mydb.get_rows(
3216 SELECT=('vms.count'),
3217 FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h on interfaces.uuid=h.interface_id",
3218 WHERE={'h.uuid': cp['uuid']})[0]['count']
3219 instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == cp['sce_vnf_id']), None)
3220 instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']]
3221 dependencies = []
3222 for instance_vm in instance_vms:
3223 action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None)
3224 if action:
3225 dependencies.append(action['task_index'])
3226 # TODO: throw exception if count != len(instance_vms)
3227 # TODO: and action shouldn't ever be None
3228 sfis_created = []
3229 for i in range(count):
3230 # create sfis
3231 sfi_uuid = str(uuid4())
3232 uuid_list.append(sfi_uuid)
3233 db_sfi = {
3234 "uuid": sfi_uuid,
3235 "instance_scenario_id": instance_uuid,
3236 'sce_rsp_hop_id': cp['uuid'],
3237 'datacenter_id': datacenter_id,
3238 'datacenter_tenant_id': myvim_thread_id,
3239 "vim_sfi_id": None, # vim thread will populate
3240 }
3241 db_instance_sfis.append(db_sfi)
3242 db_vim_action = {
3243 "instance_action_id": instance_action_id,
3244 "task_index": task_index,
3245 "datacenter_vim_id": myvim_thread_id,
3246 "action": "CREATE",
3247 "status": "SCHEDULED",
3248 "item": "instance_sfis",
3249 "item_id": sfi_uuid,
3250 "extra": yaml.safe_dump({"params": "", "depends_on": [dependencies[i]]},
3251 default_flow_style=True, width=256)
3252 }
3253 sfis_created.append(task_index)
3254 task_index += 1
3255 db_vim_actions.append(db_vim_action)
3256 # create sfs
3257 sf_uuid = str(uuid4())
3258 uuid_list.append(sf_uuid)
3259 db_sf = {
3260 "uuid": sf_uuid,
3261 "instance_scenario_id": instance_uuid,
3262 'sce_rsp_hop_id': cp['uuid'],
3263 'datacenter_id': datacenter_id,
3264 'datacenter_tenant_id': myvim_thread_id,
3265 "vim_sf_id": None, # vim thread will populate
3266 }
3267 db_instance_sfs.append(db_sf)
3268 db_vim_action = {
3269 "instance_action_id": instance_action_id,
3270 "task_index": task_index,
3271 "datacenter_vim_id": myvim_thread_id,
3272 "action": "CREATE",
3273 "status": "SCHEDULED",
3274 "item": "instance_sfs",
3275 "item_id": sf_uuid,
3276 "extra": yaml.safe_dump({"params": "", "depends_on": sfis_created},
3277 default_flow_style=True, width=256)
3278 }
3279 sfs_created.append(task_index)
3280 task_index += 1
3281 db_vim_actions.append(db_vim_action)
3282 classifier = rsp['classifier']
3283
3284 # TODO the following ~13 lines can be reused for the sfi case
3285 count = mydb.get_rows(
3286 SELECT=('vms.count'),
3287 FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id",
3288 WHERE={'c.uuid': classifier['uuid']})[0]['count']
3289 instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == classifier['sce_vnf_id']), None)
3290 instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']]
3291 dependencies = []
3292 for instance_vm in instance_vms:
3293 action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None)
3294 if action:
3295 dependencies.append(action['task_index'])
3296 # TODO: throw exception if count != len(instance_vms)
3297 # TODO: and action shouldn't ever be None
3298 classifications_created = []
3299 for i in range(count):
3300 for match in classifier['matches']:
3301 # create classifications
3302 classification_uuid = str(uuid4())
3303 uuid_list.append(classification_uuid)
3304 db_classification = {
3305 "uuid": classification_uuid,
3306 "instance_scenario_id": instance_uuid,
3307 'sce_classifier_match_id': match['uuid'],
3308 'datacenter_id': datacenter_id,
3309 'datacenter_tenant_id': myvim_thread_id,
3310 "vim_classification_id": None, # vim thread will populate
3311 }
3312 db_instance_classifications.append(db_classification)
3313 classification_params = {
3314 "ip_proto": match["ip_proto"],
3315 "source_ip": match["source_ip"],
3316 "destination_ip": match["destination_ip"],
3317 "source_port": match["source_port"],
3318 "destination_port": match["destination_port"]
3319 }
3320 db_vim_action = {
3321 "instance_action_id": instance_action_id,
3322 "task_index": task_index,
3323 "datacenter_vim_id": myvim_thread_id,
3324 "action": "CREATE",
3325 "status": "SCHEDULED",
3326 "item": "instance_classifications",
3327 "item_id": classification_uuid,
3328 "extra": yaml.safe_dump({"params": classification_params, "depends_on": [dependencies[i]]},
3329 default_flow_style=True, width=256)
3330 }
3331 classifications_created.append(task_index)
3332 task_index += 1
3333 db_vim_actions.append(db_vim_action)
3334
3335 # create sfps
3336 sfp_uuid = str(uuid4())
3337 uuid_list.append(sfp_uuid)
3338 db_sfp = {
3339 "uuid": sfp_uuid,
3340 "instance_scenario_id": instance_uuid,
3341 'sce_rsp_id': rsp['uuid'],
3342 'datacenter_id': datacenter_id,
3343 'datacenter_tenant_id': myvim_thread_id,
3344 "vim_sfp_id": None, # vim thread will populate
3345 }
3346 db_instance_sfps.append(db_sfp)
3347 db_vim_action = {
3348 "instance_action_id": instance_action_id,
3349 "task_index": task_index,
3350 "datacenter_vim_id": myvim_thread_id,
3351 "action": "CREATE",
3352 "status": "SCHEDULED",
3353 "item": "instance_sfps",
3354 "item_id": sfp_uuid,
3355 "extra": yaml.safe_dump({"params": "", "depends_on": sfs_created + classifications_created},
3356 default_flow_style=True, width=256)
3357 }
3358 task_index += 1
3359 db_vim_actions.append(db_vim_action)
3360
tierno867ffe92017-03-27 12:50:34 +02003361 scenarioDict["datacenter2tenant"] = myvim_threads_id
tierno8e690322017-08-10 15:58:50 +02003362
tierno868220c2017-09-26 00:11:05 +02003363 db_instance_action["number_tasks"] = task_index
tierno8e690322017-08-10 15:58:50 +02003364 db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id]
3365 db_instance_scenario['datacenter_id'] = default_datacenter_id
3366 db_tables=[
3367 {"instance_scenarios": db_instance_scenario},
3368 {"instance_vnfs": db_instance_vnfs},
3369 {"instance_nets": db_instance_nets},
3370 {"ip_profiles": db_ip_profiles},
3371 {"instance_vms": db_instance_vms},
3372 {"instance_interfaces": db_instance_interfaces},
tierno868220c2017-09-26 00:11:05 +02003373 {"instance_actions": db_instance_action},
Igor D.Ccaadc442017-11-06 12:48:48 +00003374 {"instance_sfis": db_instance_sfis},
3375 {"instance_sfs": db_instance_sfs},
3376 {"instance_classifications": db_instance_classifications},
3377 {"instance_sfps": db_instance_sfps},
tierno868220c2017-09-26 00:11:05 +02003378 {"vim_actions": db_vim_actions}
tierno8e690322017-08-10 15:58:50 +02003379 ]
3380
tierno868220c2017-09-26 00:11:05 +02003381 logger.debug("create_instance done DB tables: %s",
tierno8e690322017-08-10 15:58:50 +02003382 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
3383 mydb.new_rows(db_tables, uuid_list)
tierno868220c2017-09-26 00:11:05 +02003384 for myvim_thread_id in myvim_threads_id.values():
3385 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
tierno867ffe92017-03-27 12:50:34 +02003386
tierno868220c2017-09-26 00:11:05 +02003387 returned_instance = mydb.get_instance_scenario(instance_uuid)
3388 returned_instance["action_id"] = instance_action_id
3389 return returned_instance
3390 except (NfvoException, vimconn.vimconnException, db_base_Exception) as e:
tiernobe41e222016-09-02 15:16:13 +02003391 message = rollback(mydb, myvims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02003392 if isinstance(e, db_base_Exception):
3393 error_text = "database Exception"
3394 elif isinstance(e, vimconn.vimconnException):
3395 error_text = "VIM Exception"
3396 else:
3397 error_text = "Exception"
3398 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
tierno868220c2017-09-26 00:11:05 +02003399 # logger.error("create_instance: %s", error_text)
tiernof97fd272016-07-11 14:32:37 +02003400 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01003401
tiernob3d36742017-03-03 23:51:05 +01003402
tierno16e3dd42018-04-24 12:52:40 +02003403def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList):
3404 default_datacenter_id = params["default_datacenter_id"]
3405 myvim_threads_id = params["myvim_threads_id"]
3406 instance_uuid = params["instance_uuid"]
3407 instance_name = params["instance_name"]
3408 instance_action_id = params["instance_action_id"]
3409 myvims = params["myvims"]
3410 cloud_config = params["cloud_config"]
3411 RO_pub_key = params["RO_pub_key"]
3412
3413 task_index = params_out["task_index"]
3414 uuid_list = params_out["uuid_list"]
3415 db_instance_nets = params_out["db_instance_nets"]
3416 db_vim_actions = params_out["db_vim_actions"]
3417 db_ip_profiles = params_out["db_ip_profiles"]
3418 db_instance_vnfs = params_out["db_instance_vnfs"]
3419 db_instance_vms = params_out["db_instance_vms"]
3420 db_instance_interfaces = params_out["db_instance_interfaces"]
3421 net2task_id = params_out["net2task_id"]
3422 sce_net2instance = params_out["sce_net2instance"]
3423
3424 vnf_net2instance = {}
3425
3426 # 2. Creating new nets (vnf internal nets) in the VIM"
3427 # For each vnf net, we create it and we add it to instanceNetlist.
3428 if sce_vnf.get("datacenter"):
3429 datacenter_id = sce_vnf["datacenter"]
3430 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
3431 else:
3432 datacenter_id = default_datacenter_id
3433 myvim_thread_id = myvim_threads_id[default_datacenter_id]
3434 for net in sce_vnf['nets']:
3435 # TODO revis
3436 # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
3437 # net_name = descriptor_net.get("name")
3438 net_name = None
3439 if not net_name:
3440 net_name = "{}.{}".format(instance_name, net["name"])
3441 net_name = net_name[:255] # limit length
3442 net_type = net['type']
3443
3444 if sce_vnf['uuid'] not in vnf_net2instance:
3445 vnf_net2instance[sce_vnf['uuid']] = {}
3446 if sce_vnf['uuid'] not in net2task_id:
3447 net2task_id[sce_vnf['uuid']] = {}
3448 net2task_id[sce_vnf['uuid']][net['uuid']] = task_index
3449
3450 # fill database content
3451 net_uuid = str(uuid4())
3452 uuid_list.append(net_uuid)
3453 vnf_net2instance[sce_vnf['uuid']][net['uuid']] = net_uuid
3454 db_net = {
3455 "uuid": net_uuid,
3456 'vim_net_id': None,
3457 "instance_scenario_id": instance_uuid,
3458 "net_id": net["uuid"],
3459 "created": True,
3460 'datacenter_id': datacenter_id,
3461 'datacenter_tenant_id': myvim_thread_id,
3462 }
3463 db_instance_nets.append(db_net)
3464
3465 db_vim_action = {
3466 "instance_action_id": instance_action_id,
3467 "task_index": task_index,
3468 "datacenter_vim_id": myvim_thread_id,
3469 "status": "SCHEDULED",
3470 "action": "CREATE",
3471 "item": "instance_nets",
3472 "item_id": net_uuid,
3473 "extra": yaml.safe_dump({"params": (net_name, net_type, net.get('ip_profile', None))},
3474 default_flow_style=True, width=256)
3475 }
3476 task_index += 1
3477 db_vim_actions.append(db_vim_action)
3478
3479 if 'ip_profile' in net:
3480 db_ip_profile = {
3481 'instance_net_id': net_uuid,
3482 'ip_version': net['ip_profile']['ip_version'],
3483 'subnet_address': net['ip_profile']['subnet_address'],
3484 'gateway_address': net['ip_profile']['gateway_address'],
3485 'dns_address': net['ip_profile']['dns_address'],
3486 'dhcp_enabled': net['ip_profile']['dhcp_enabled'],
3487 'dhcp_start_address': net['ip_profile']['dhcp_start_address'],
3488 'dhcp_count': net['ip_profile']['dhcp_count'],
3489 }
3490 db_ip_profiles.append(db_ip_profile)
3491
3492 # print "vnf_net2instance:"
3493 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
3494
3495 # 3. Creating new vm instances in the VIM
3496 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
3497 ssh_access = None
3498 if sce_vnf.get('mgmt_access'):
3499 ssh_access = sce_vnf['mgmt_access'].get('config-access', {}).get('ssh-access')
3500 vnf_availability_zones = []
3501 for vm in sce_vnf['vms']:
3502 vm_av = vm.get('availability_zone')
3503 if vm_av and vm_av not in vnf_availability_zones:
3504 vnf_availability_zones.append(vm_av)
3505
3506 # check if there is enough availability zones available at vim level.
3507 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
3508 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
3509 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request)
3510
3511 if sce_vnf.get("datacenter"):
3512 vim = myvims[sce_vnf["datacenter"]]
3513 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
3514 datacenter_id = sce_vnf["datacenter"]
3515 else:
3516 vim = myvims[default_datacenter_id]
3517 myvim_thread_id = myvim_threads_id[default_datacenter_id]
3518 datacenter_id = default_datacenter_id
3519 sce_vnf["datacenter_id"] = datacenter_id
3520 i = 0
3521
3522 vnf_uuid = str(uuid4())
3523 uuid_list.append(vnf_uuid)
3524 db_instance_vnf = {
3525 'uuid': vnf_uuid,
3526 'instance_scenario_id': instance_uuid,
3527 'vnf_id': sce_vnf['vnf_id'],
3528 'sce_vnf_id': sce_vnf['uuid'],
3529 'datacenter_id': datacenter_id,
3530 'datacenter_tenant_id': myvim_thread_id,
3531 }
3532 db_instance_vnfs.append(db_instance_vnf)
3533
3534 for vm in sce_vnf['vms']:
3535 myVMDict = {}
3536 myVMDict['name'] = "{}.{}.{}".format(instance_name[:64], sce_vnf['name'][:64], vm["name"][:64])
3537 myVMDict['description'] = myVMDict['name'][0:99]
3538 # if not startvms:
3539 # myVMDict['start'] = "no"
3540 myVMDict['name'] = myVMDict['name'][0:255] # limit name length
3541 # create image at vim in case it not exist
3542 image_uuid = vm['image_id']
3543 if vm.get("image_list"):
3544 for alternative_image in vm["image_list"]:
tiernob6434212018-04-26 16:27:47 +02003545 if alternative_image["vim_type"] == vim["config"]["_vim_type_internal"]:
tierno16e3dd42018-04-24 12:52:40 +02003546 image_uuid = alternative_image['image_id']
3547 break
3548 image_dict = mydb.get_table_by_uuid_name("images", image_uuid)
3549 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
3550 vm['vim_image_id'] = image_id
3551
3552 # create flavor at vim in case it not exist
3553 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
3554 if flavor_dict['extended'] != None:
3555 flavor_dict['extended'] = yaml.load(flavor_dict['extended'])
3556 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
3557
3558 # Obtain information for additional disks
3559 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',),
3560 WHERE={'vim_id': flavor_id})
3561 if not extended_flavor_dict:
3562 raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found)
tierno16e3dd42018-04-24 12:52:40 +02003563
3564 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
3565 myVMDict['disks'] = None
3566 extended_info = extended_flavor_dict[0]['extended']
3567 if extended_info != None:
3568 extended_flavor_dict_yaml = yaml.load(extended_info)
3569 if 'disks' in extended_flavor_dict_yaml:
3570 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
3571
3572 vm['vim_flavor_id'] = flavor_id
3573 myVMDict['imageRef'] = vm['vim_image_id']
3574 myVMDict['flavorRef'] = vm['vim_flavor_id']
3575 myVMDict['availability_zone'] = vm.get('availability_zone')
3576 myVMDict['networks'] = []
3577 task_depends_on = []
3578 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
3579 db_vm_ifaces = []
3580 for iface in vm['interfaces']:
3581 netDict = {}
3582 if iface['type'] == "data":
3583 netDict['type'] = iface['model']
3584 elif "model" in iface and iface["model"] != None:
3585 netDict['model'] = iface['model']
3586 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
3587 # is obtained from iterface table model
3588 # discover type of interface looking at flavor
3589 for numa in flavor_dict.get('extended', {}).get('numas', []):
3590 for flavor_iface in numa.get('interfaces', []):
3591 if flavor_iface.get('name') == iface['internal_name']:
3592 if flavor_iface['dedicated'] == 'yes':
3593 netDict['type'] = "PF" # passthrough
3594 elif flavor_iface['dedicated'] == 'no':
3595 netDict['type'] = "VF" # siov
3596 elif flavor_iface['dedicated'] == 'yes:sriov':
3597 netDict['type'] = "VFnotShared" # sriov but only one sriov on the PF
3598 netDict["mac_address"] = flavor_iface.get("mac_address")
3599 break
3600 netDict["use"] = iface['type']
3601 if netDict["use"] == "data" and not netDict.get("type"):
3602 # print "netDict", netDict
3603 # print "iface", iface
3604 e_text = "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat(
3605 sce_vnf['name'], vm['name'], iface['internal_name'])
3606 if flavor_dict.get('extended') == None:
3607 raise NfvoException(e_text + "After database migration some information is not available. \
3608 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
3609 else:
3610 raise NfvoException(e_text, HTTP_Internal_Server_Error)
3611 if netDict["use"] == "mgmt" or netDict["use"] == "bridge":
3612 netDict["type"] = "virtual"
3613 if iface.get("vpci"):
3614 netDict['vpci'] = iface['vpci']
3615 if iface.get("mac"):
3616 netDict['mac_address'] = iface['mac']
3617 if iface.get("ip_address"):
3618 netDict['ip_address'] = iface['ip_address']
3619 if iface.get("port-security") is not None:
3620 netDict['port_security'] = iface['port-security']
3621 if iface.get("floating-ip") is not None:
3622 netDict['floating_ip'] = iface['floating-ip']
3623 netDict['name'] = iface['internal_name']
3624 if iface['net_id'] is None:
3625 for vnf_iface in sce_vnf["interfaces"]:
3626 # print iface
3627 # print vnf_iface
3628 if vnf_iface['interface_id'] == iface['uuid']:
3629 netDict['net_id'] = "TASK-{}".format(
3630 net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id])
3631 instance_net_id = sce_net2instance[vnf_iface['sce_net_id']][datacenter_id]
3632 task_depends_on.append(net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id])
3633 break
3634 else:
3635 netDict['net_id'] = "TASK-{}".format(net2task_id[sce_vnf['uuid']][iface['net_id']])
3636 instance_net_id = vnf_net2instance[sce_vnf['uuid']][iface['net_id']]
3637 task_depends_on.append(net2task_id[sce_vnf['uuid']][iface['net_id']])
3638 # skip bridge ifaces not connected to any net
3639 if 'net_id' not in netDict or netDict['net_id'] == None:
3640 continue
3641 myVMDict['networks'].append(netDict)
3642 db_vm_iface = {
3643 # "uuid"
3644 # 'instance_vm_id': instance_vm_uuid,
3645 "instance_net_id": instance_net_id,
3646 'interface_id': iface['uuid'],
3647 # 'vim_interface_id': ,
3648 'type': 'external' if iface['external_name'] is not None else 'internal',
3649 'ip_address': iface.get('ip_address'),
3650 'mac_address': iface.get('mac'),
3651 'floating_ip': int(iface.get('floating-ip', False)),
3652 'port_security': int(iface.get('port-security', True))
3653 }
3654 db_vm_ifaces.append(db_vm_iface)
3655 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3656 # print myVMDict['name']
3657 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
3658 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
3659 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3660
3661 # We add the RO key to cloud_config if vnf will need ssh access
3662 cloud_config_vm = cloud_config
3663 if ssh_access and ssh_access['required'] and ssh_access['default-user'] and tenant[0].get('RO_pub_key'):
3664 RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]}
3665 cloud_config_vm = unify_cloud_config(cloud_config_vm, RO_key)
3666 if vm.get("boot_data"):
3667 cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config_vm)
3668
3669 if myVMDict.get('availability_zone'):
3670 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
3671 else:
3672 av_index = None
3673 for vm_index in range(0, vm.get('count', 1)):
3674 vm_index_name = ""
3675 if vm.get('count', 1) > 1:
3676 vm_index_name += "." + chr(97 + vm_index)
3677 task_params = (myVMDict['name'] + vm_index_name, myVMDict['description'], myVMDict.get('start', None),
3678 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm,
3679 myVMDict['disks'], av_index, vnf_availability_zones)
3680 # put interface uuid back to scenario[vnfs][vms[[interfaces]
3681 for net in myVMDict['networks']:
3682 if "vim_id" in net:
3683 for iface in vm['interfaces']:
3684 if net["name"] == iface["internal_name"]:
3685 iface["vim_id"] = net["vim_id"]
3686 break
3687 vm_uuid = str(uuid4())
3688 uuid_list.append(vm_uuid)
3689 db_vm = {
3690 "uuid": vm_uuid,
3691 'instance_vnf_id': vnf_uuid,
3692 # TODO delete "vim_vm_id": vm_id,
3693 "vm_id": vm["uuid"],
3694 # "status":
3695 }
3696 db_instance_vms.append(db_vm)
3697
3698 iface_index = 0
3699 for db_vm_iface in db_vm_ifaces:
3700 iface_uuid = str(uuid4())
3701 uuid_list.append(iface_uuid)
3702 db_vm_iface_instance = {
3703 "uuid": iface_uuid,
3704 "instance_vm_id": vm_uuid
3705 }
3706 db_vm_iface_instance.update(db_vm_iface)
3707 if db_vm_iface_instance.get("ip_address"): # increment ip_address
3708 ip = db_vm_iface_instance.get("ip_address")
3709 i = ip.rfind(".")
3710 if i > 0:
3711 try:
3712 i += 1
3713 ip = ip[i:] + str(int(ip[:i]) + 1)
3714 db_vm_iface_instance["ip_address"] = ip
3715 except:
3716 db_vm_iface_instance["ip_address"] = None
3717 db_instance_interfaces.append(db_vm_iface_instance)
3718 myVMDict['networks'][iface_index]["uuid"] = iface_uuid
3719 iface_index += 1
3720
3721 db_vim_action = {
3722 "instance_action_id": instance_action_id,
3723 "task_index": task_index,
3724 "datacenter_vim_id": myvim_thread_id,
3725 "action": "CREATE",
3726 "status": "SCHEDULED",
3727 "item": "instance_vms",
3728 "item_id": vm_uuid,
3729 "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on},
3730 default_flow_style=True, width=256)
3731 }
3732 task_index += 1
3733 db_vim_actions.append(db_vim_action)
3734 params_out["task_index"] = task_index
3735 params_out["uuid_list"] = uuid_list
3736
3737
tierno7edb6752016-03-21 17:37:52 +01003738def delete_instance(mydb, tenant_id, instance_id):
tierno868220c2017-09-26 00:11:05 +02003739 # print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02003740 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
tierno868220c2017-09-26 00:11:05 +02003741 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01003742 tenant_id = instanceDict["tenant_id"]
tierno868220c2017-09-26 00:11:05 +02003743 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno868220c2017-09-26 00:11:05 +02003744 # 1. Delete from Database
tiernof97fd272016-07-11 14:32:37 +02003745 message = mydb.delete_instance_scenario(instance_id, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01003746
tierno868220c2017-09-26 00:11:05 +02003747 # 2. delete from VIM
tiernoa2793912016-10-04 08:15:08 +00003748 error_msg = ""
tiernob3d36742017-03-03 23:51:05 +01003749 myvims = {}
3750 myvim_threads = {}
tierno868220c2017-09-26 00:11:05 +02003751 vimthread_affected = {}
tierno3fcfdb72017-10-24 07:48:24 +02003752 net2vm_dependencies = {}
tierno7edb6752016-03-21 17:37:52 +01003753
tierno868220c2017-09-26 00:11:05 +02003754 task_index = 0
3755 instance_action_id = get_task_id()
3756 db_vim_actions = []
3757 db_instance_action = {
3758 "uuid": instance_action_id, # same uuid for the instance and the action on create
3759 "tenant_id": tenant_id,
3760 "instance_id": instance_id,
3761 "description": "DELETE",
3762 # "number_tasks": 0 # filled bellow
3763 }
3764
3765 # 2.1 deleting VMs
3766 # vm_fail_list=[]
tierno7edb6752016-03-21 17:37:52 +01003767 for sce_vnf in instanceDict['vnfs']:
tiernoa2793912016-10-04 08:15:08 +00003768 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
tierno868220c2017-09-26 00:11:05 +02003769 vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None
tiernoa2793912016-10-04 08:15:08 +00003770 if datacenter_key not in myvims:
tiernob3d36742017-03-03 23:51:05 +01003771 try:
tierno867ffe92017-03-27 12:50:34 +02003772 _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
tiernob3d36742017-03-03 23:51:05 +01003773 except NfvoException as e:
3774 logger.error(str(e))
3775 myvim_thread = None
3776 myvim_threads[datacenter_key] = myvim_thread
tiernoa2793912016-10-04 08:15:08 +00003777 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
3778 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
3779 if len(vims) == 0:
3780 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
3781 sce_vnf["datacenter_tenant_id"]))
3782 myvims[datacenter_key] = None
3783 else:
3784 myvims[datacenter_key] = vims.values()[0]
3785 myvim = myvims[datacenter_key]
tiernob3d36742017-03-03 23:51:05 +01003786 myvim_thread = myvim_threads[datacenter_key]
tierno7edb6752016-03-21 17:37:52 +01003787 for vm in sce_vnf['vms']:
tiernoa2793912016-10-04 08:15:08 +00003788 if not myvim:
3789 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
3790 continue
tierno3fcfdb72017-10-24 07:48:24 +02003791 db_vim_action = {
3792 "instance_action_id": instance_action_id,
3793 "task_index": task_index,
3794 "datacenter_vim_id": sce_vnf["datacenter_tenant_id"],
3795 "action": "DELETE",
3796 "status": "SCHEDULED",
3797 "item": "instance_vms",
3798 "item_id": vm["uuid"],
3799 "extra": yaml.safe_dump({"params": vm["interfaces"]},
3800 default_flow_style=True, width=256)
3801 }
3802 db_vim_actions.append(db_vim_action)
3803 for interface in vm["interfaces"]:
3804 if not interface.get("instance_net_id"):
3805 continue
3806 if interface["instance_net_id"] not in net2vm_dependencies:
3807 net2vm_dependencies[interface["instance_net_id"]] = []
3808 net2vm_dependencies[interface["instance_net_id"]].append(task_index)
3809 task_index += 1
tierno42026a02017-02-10 15:13:40 +01003810
tierno868220c2017-09-26 00:11:05 +02003811 # 2.2 deleting NETS
3812 # net_fail_list=[]
tierno7edb6752016-03-21 17:37:52 +01003813 for net in instanceDict['nets']:
tierno868220c2017-09-26 00:11:05 +02003814 vimthread_affected[net["datacenter_tenant_id"]] = None
tiernoa2793912016-10-04 08:15:08 +00003815 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
3816 if datacenter_key not in myvims:
tiernob3d36742017-03-03 23:51:05 +01003817 try:
tierno867ffe92017-03-27 12:50:34 +02003818 _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
tiernob3d36742017-03-03 23:51:05 +01003819 except NfvoException as e:
3820 logger.error(str(e))
3821 myvim_thread = None
3822 myvim_threads[datacenter_key] = myvim_thread
tiernoa2793912016-10-04 08:15:08 +00003823 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
3824 datacenter_tenant_id=net["datacenter_tenant_id"])
3825 if len(vims) == 0:
3826 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
3827 myvims[datacenter_key] = None
3828 else:
3829 myvims[datacenter_key] = vims.values()[0]
3830 myvim = myvims[datacenter_key]
tiernob3d36742017-03-03 23:51:05 +01003831 myvim_thread = myvim_threads[datacenter_key]
tiernoa2793912016-10-04 08:15:08 +00003832
tierno7edb6752016-03-21 17:37:52 +01003833 if not myvim:
tiernoa2793912016-10-04 08:15:08 +00003834 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 +01003835 continue
tierno3fcfdb72017-10-24 07:48:24 +02003836 extra = {"params": (net['vim_net_id'], net['sdn_net_id'])}
3837 if net2vm_dependencies.get(net["uuid"]):
3838 extra["depends_on"] = net2vm_dependencies[net["uuid"]]
3839 db_vim_action = {
3840 "instance_action_id": instance_action_id,
3841 "task_index": task_index,
3842 "datacenter_vim_id": net["datacenter_tenant_id"],
3843 "action": "DELETE",
3844 "status": "SCHEDULED",
3845 "item": "instance_nets",
3846 "item_id": net["uuid"],
3847 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
3848 }
3849 task_index += 1
3850 db_vim_actions.append(db_vim_action)
tierno868220c2017-09-26 00:11:05 +02003851
Igor D.Ccaadc442017-11-06 12:48:48 +00003852 # 2.3 deleting VNFFGs
3853
tierno69b590e2018-03-13 18:52:23 +01003854 for sfp in instanceDict.get('sfps', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00003855 vimthread_affected[sfp["datacenter_tenant_id"]] = None
3856 datacenter_key = (sfp["datacenter_id"], sfp["datacenter_tenant_id"])
3857 if datacenter_key not in myvims:
3858 try:
3859 _,myvim_thread = get_vim_thread(mydb, tenant_id, sfp["datacenter_id"], sfp["datacenter_tenant_id"])
3860 except NfvoException as e:
3861 logger.error(str(e))
3862 myvim_thread = None
3863 myvim_threads[datacenter_key] = myvim_thread
3864 vims = get_vim(mydb, tenant_id, datacenter_id=sfp["datacenter_id"],
3865 datacenter_tenant_id=sfp["datacenter_tenant_id"])
3866 if len(vims) == 0:
3867 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp["datacenter_id"], sfp["datacenter_tenant_id"]))
3868 myvims[datacenter_key] = None
3869 else:
3870 myvims[datacenter_key] = vims.values()[0]
3871 myvim = myvims[datacenter_key]
3872 myvim_thread = myvim_threads[datacenter_key]
3873
3874 if not myvim:
3875 error_msg += "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp['vim_sfp_id'], sfp["datacenter_id"])
3876 continue
3877 extra = {"params": (sfp['vim_sfp_id'])}
3878 db_vim_action = {
3879 "instance_action_id": instance_action_id,
3880 "task_index": task_index,
3881 "datacenter_vim_id": sfp["datacenter_tenant_id"],
3882 "action": "DELETE",
3883 "status": "SCHEDULED",
3884 "item": "instance_sfps",
3885 "item_id": sfp["uuid"],
3886 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
3887 }
3888 task_index += 1
3889 db_vim_actions.append(db_vim_action)
3890
tierno69b590e2018-03-13 18:52:23 +01003891 for sf in instanceDict.get('sfs', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00003892 vimthread_affected[sf["datacenter_tenant_id"]] = None
3893 datacenter_key = (sf["datacenter_id"], sf["datacenter_tenant_id"])
3894 if datacenter_key not in myvims:
3895 try:
3896 _,myvim_thread = get_vim_thread(mydb, tenant_id, sf["datacenter_id"], sf["datacenter_tenant_id"])
3897 except NfvoException as e:
3898 logger.error(str(e))
3899 myvim_thread = None
3900 myvim_threads[datacenter_key] = myvim_thread
3901 vims = get_vim(mydb, tenant_id, datacenter_id=sf["datacenter_id"],
3902 datacenter_tenant_id=sf["datacenter_tenant_id"])
3903 if len(vims) == 0:
3904 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf["datacenter_id"], sf["datacenter_tenant_id"]))
3905 myvims[datacenter_key] = None
3906 else:
3907 myvims[datacenter_key] = vims.values()[0]
3908 myvim = myvims[datacenter_key]
3909 myvim_thread = myvim_threads[datacenter_key]
3910
3911 if not myvim:
3912 error_msg += "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf['vim_sf_id'], sf["datacenter_id"])
3913 continue
3914 extra = {"params": (sf['vim_sf_id'])}
3915 db_vim_action = {
3916 "instance_action_id": instance_action_id,
3917 "task_index": task_index,
3918 "datacenter_vim_id": sf["datacenter_tenant_id"],
3919 "action": "DELETE",
3920 "status": "SCHEDULED",
3921 "item": "instance_sfs",
3922 "item_id": sf["uuid"],
3923 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
3924 }
3925 task_index += 1
3926 db_vim_actions.append(db_vim_action)
3927
tierno69b590e2018-03-13 18:52:23 +01003928 for sfi in instanceDict.get('sfis', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00003929 vimthread_affected[sfi["datacenter_tenant_id"]] = None
3930 datacenter_key = (sfi["datacenter_id"], sfi["datacenter_tenant_id"])
3931 if datacenter_key not in myvims:
3932 try:
3933 _,myvim_thread = get_vim_thread(mydb, tenant_id, sfi["datacenter_id"], sfi["datacenter_tenant_id"])
3934 except NfvoException as e:
3935 logger.error(str(e))
3936 myvim_thread = None
3937 myvim_threads[datacenter_key] = myvim_thread
3938 vims = get_vim(mydb, tenant_id, datacenter_id=sfi["datacenter_id"],
3939 datacenter_tenant_id=sfi["datacenter_tenant_id"])
3940 if len(vims) == 0:
3941 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi["datacenter_id"], sfi["datacenter_tenant_id"]))
3942 myvims[datacenter_key] = None
3943 else:
3944 myvims[datacenter_key] = vims.values()[0]
3945 myvim = myvims[datacenter_key]
3946 myvim_thread = myvim_threads[datacenter_key]
3947
3948 if not myvim:
3949 error_msg += "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi['vim_sfi_id'], sfi["datacenter_id"])
3950 continue
3951 extra = {"params": (sfi['vim_sfi_id'])}
3952 db_vim_action = {
3953 "instance_action_id": instance_action_id,
3954 "task_index": task_index,
3955 "datacenter_vim_id": sfi["datacenter_tenant_id"],
3956 "action": "DELETE",
3957 "status": "SCHEDULED",
3958 "item": "instance_sfis",
3959 "item_id": sfi["uuid"],
3960 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
3961 }
3962 task_index += 1
3963 db_vim_actions.append(db_vim_action)
3964
3965 for classification in instanceDict['classifications']:
3966 vimthread_affected[classification["datacenter_tenant_id"]] = None
3967 datacenter_key = (classification["datacenter_id"], classification["datacenter_tenant_id"])
3968 if datacenter_key not in myvims:
3969 try:
3970 _,myvim_thread = get_vim_thread(mydb, tenant_id, classification["datacenter_id"], classification["datacenter_tenant_id"])
3971 except NfvoException as e:
3972 logger.error(str(e))
3973 myvim_thread = None
3974 myvim_threads[datacenter_key] = myvim_thread
3975 vims = get_vim(mydb, tenant_id, datacenter_id=classification["datacenter_id"],
3976 datacenter_tenant_id=classification["datacenter_tenant_id"])
3977 if len(vims) == 0:
3978 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification["datacenter_id"], classification["datacenter_tenant_id"]))
3979 myvims[datacenter_key] = None
3980 else:
3981 myvims[datacenter_key] = vims.values()[0]
3982 myvim = myvims[datacenter_key]
3983 myvim_thread = myvim_threads[datacenter_key]
3984
3985 if not myvim:
3986 error_msg += "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification['vim_classification_id'], classification["datacenter_id"])
3987 continue
3988 extra = {"params": (classification['vim_classification_id'])}
3989 db_vim_action = {
3990 "instance_action_id": instance_action_id,
3991 "task_index": task_index,
3992 "datacenter_vim_id": classification["datacenter_tenant_id"],
3993 "action": "DELETE",
3994 "status": "SCHEDULED",
3995 "item": "instance_classifications",
3996 "item_id": classification["uuid"],
3997 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
3998 }
3999 task_index += 1
4000 db_vim_actions.append(db_vim_action)
4001
tierno868220c2017-09-26 00:11:05 +02004002 db_instance_action["number_tasks"] = task_index
4003 db_tables = [
4004 {"instance_actions": db_instance_action},
4005 {"vim_actions": db_vim_actions}
4006 ]
4007
4008 logger.debug("delete_instance done DB tables: %s",
4009 yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
4010 mydb.new_rows(db_tables, ())
4011 for myvim_thread_id in vimthread_affected.keys():
4012 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
4013
tiernob3d36742017-03-03 23:51:05 +01004014 if len(error_msg) > 0:
tierno868220c2017-09-26 00:11:05 +02004015 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
4016 '(error: 404) from VIM: {}'.format(instance_action_id, message, error_msg)
tierno7edb6752016-03-21 17:37:52 +01004017 else:
tierno868220c2017-09-26 00:11:05 +02004018 return "action_id={} instance {} deleted".format(instance_action_id, message)
tierno7edb6752016-03-21 17:37:52 +01004019
tiernob3d36742017-03-03 23:51:05 +01004020
tierno7edb6752016-03-21 17:37:52 +01004021def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
4022 '''Refreshes a scenario instance. It modifies instanceDict'''
4023 '''Returns:
4024 - 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
4025 - error_msg
4026 '''
tierno867ffe92017-03-27 12:50:34 +02004027 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
4028 # #print "nfvo.refresh_instance begins"
4029 # #print json.dumps(instanceDict, indent=4)
4030 #
4031 # #print "Getting the VIM URL and the VIM tenant_id"
4032 # myvims={}
4033 #
4034 # # 1. Getting VIM vm and net list
4035 # vms_updated = [] #List of VM instance uuids in openmano that were updated
4036 # vms_notupdated=[]
4037 # vm_list = {}
4038 # for sce_vnf in instanceDict['vnfs']:
4039 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4040 # if datacenter_key not in vm_list:
4041 # vm_list[datacenter_key] = []
4042 # if datacenter_key not in myvims:
4043 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
4044 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
4045 # if len(vims) == 0:
4046 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
4047 # myvims[datacenter_key] = None
4048 # else:
4049 # myvims[datacenter_key] = vims.values()[0]
4050 # for vm in sce_vnf['vms']:
4051 # vm_list[datacenter_key].append(vm['vim_vm_id'])
4052 # vms_notupdated.append(vm["uuid"])
4053 #
4054 # nets_updated = [] #List of VM instance uuids in openmano that were updated
4055 # nets_notupdated=[]
4056 # net_list = {}
4057 # for net in instanceDict['nets']:
4058 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4059 # if datacenter_key not in net_list:
4060 # net_list[datacenter_key] = []
4061 # if datacenter_key not in myvims:
4062 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
4063 # datacenter_tenant_id=net["datacenter_tenant_id"])
4064 # if len(vims) == 0:
4065 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
4066 # myvims[datacenter_key] = None
4067 # else:
4068 # myvims[datacenter_key] = vims.values()[0]
4069 #
4070 # net_list[datacenter_key].append(net['vim_net_id'])
4071 # nets_notupdated.append(net["uuid"])
4072 #
4073 # # 1. Getting the status of all VMs
4074 # vm_dict={}
4075 # for datacenter_key in myvims:
4076 # if not vm_list.get(datacenter_key):
4077 # continue
4078 # failed = True
4079 # failed_message=""
4080 # if not myvims[datacenter_key]:
4081 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4082 # else:
4083 # try:
4084 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
4085 # failed = False
4086 # except vimconn.vimconnException as e:
4087 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4088 # failed_message = str(e)
4089 # if failed:
4090 # for vm in vm_list[datacenter_key]:
4091 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4092 #
4093 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
4094 # for sce_vnf in instanceDict['vnfs']:
4095 # for vm in sce_vnf['vms']:
4096 # vm_id = vm['vim_vm_id']
4097 # interfaces = vm_dict[vm_id].pop('interfaces', [])
4098 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
4099 # has_mgmt_iface = False
4100 # for iface in vm["interfaces"]:
4101 # if iface["type"]=="mgmt":
4102 # has_mgmt_iface = True
4103 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
4104 # vm_dict[vm_id]['status'] = "ACTIVE"
4105 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
4106 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
4107 # 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'):
4108 # vm['status'] = vm_dict[vm_id]['status']
4109 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
4110 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
4111 # # 2.1. Update in openmano DB the VMs whose status changed
4112 # try:
4113 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
4114 # vms_notupdated.remove(vm["uuid"])
4115 # if updates>0:
4116 # vms_updated.append(vm["uuid"])
4117 # except db_base_Exception as e:
4118 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4119 # # 2.2. Update in openmano DB the interface VMs
4120 # for interface in interfaces:
4121 # #translate from vim_net_id to instance_net_id
4122 # network_id_list=[]
4123 # for net in instanceDict['nets']:
4124 # if net["vim_net_id"] == interface["vim_net_id"]:
4125 # network_id_list.append(net["uuid"])
4126 # if not network_id_list:
4127 # continue
4128 # del interface["vim_net_id"]
4129 # try:
4130 # for network_id in network_id_list:
4131 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
4132 # except db_base_Exception as e:
4133 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
4134 #
4135 # # 3. Getting the status of all nets
4136 # net_dict = {}
4137 # for datacenter_key in myvims:
4138 # if not net_list.get(datacenter_key):
4139 # continue
4140 # failed = True
4141 # failed_message = ""
4142 # if not myvims[datacenter_key]:
4143 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4144 # else:
4145 # try:
4146 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
4147 # failed = False
4148 # except vimconn.vimconnException as e:
4149 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4150 # failed_message = str(e)
4151 # if failed:
4152 # for net in net_list[datacenter_key]:
4153 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4154 #
4155 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
4156 # # TODO: update nets inside a vnf
4157 # for net in instanceDict['nets']:
4158 # net_id = net['vim_net_id']
4159 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
4160 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
4161 # 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'):
4162 # net['status'] = net_dict[net_id]['status']
4163 # net['error_msg'] = net_dict[net_id].get('error_msg')
4164 # net['vim_info'] = net_dict[net_id].get('vim_info')
4165 # # 5.1. Update in openmano DB the nets whose status changed
4166 # try:
4167 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
4168 # nets_notupdated.remove(net["uuid"])
4169 # if updated>0:
4170 # nets_updated.append(net["uuid"])
4171 # except db_base_Exception as e:
4172 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4173 #
4174 # # Returns appropriate output
4175 # #print "nfvo.refresh_instance finishes"
4176 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
4177 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
tierno7edb6752016-03-21 17:37:52 +01004178 instance_id = instanceDict['uuid']
tierno867ffe92017-03-27 12:50:34 +02004179 # if len(vms_notupdated)+len(nets_notupdated)>0:
4180 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
4181 # 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 +01004182
tiernoae4a8d12016-07-08 12:30:39 +02004183 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
tierno7edb6752016-03-21 17:37:52 +01004184
4185def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
tiernoae4a8d12016-07-08 12:30:39 +02004186 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02004187 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01004188 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4189
tiernoae4a8d12016-07-08 12:30:39 +02004190 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernof97fd272016-07-11 14:32:37 +02004191 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
4192 if len(vims) == 0:
4193 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01004194 myvim = vims.values()[0]
tierno42026a02017-02-10 15:13:40 +01004195
tierno868220c2017-09-26 00:11:05 +02004196 if action_dict.get("create-vdu"):
4197 for vdu in action_dict["create-vdu"]:
4198 vdu_id = vdu.get("vdu-id")
4199 vdu_count = vdu.get("count", 1)
4200 # get from database TODO
4201 # insert tasks TODO
4202 pass
tierno7edb6752016-03-21 17:37:52 +01004203
4204 input_vnfs = action_dict.pop("vnfs", [])
4205 input_vms = action_dict.pop("vms", [])
tierno92c36fd2018-05-04 12:21:10 +02004206 action_over_all = True if not input_vnfs and not input_vms else False
tierno7edb6752016-03-21 17:37:52 +01004207 vm_result = {}
4208 vm_error = 0
4209 vm_ok = 0
4210 for sce_vnf in instanceDict['vnfs']:
4211 for vm in sce_vnf['vms']:
tierno92c36fd2018-05-04 12:21:10 +02004212 if not action_over_all and sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
4213 sce_vnf['member_vnf_index'] not in input_vnfs and \
4214 vm['uuid'] not in input_vms and vm['name'] not in input_vms:
4215 continue
tiernoae4a8d12016-07-08 12:30:39 +02004216 try:
gcalvinoe580c7d2017-09-22 14:09:51 +02004217 if "add_public_key" in action_dict:
4218 mgmt_access = {}
4219 if sce_vnf.get('mgmt_access'):
4220 mgmt_access = yaml.load(sce_vnf['mgmt_access'])
4221 ssh_access = mgmt_access['config-access']['ssh-access']
4222 tenant = mydb.get_rows_by_id('nfvo_tenants', nfvo_tenant)
tierno42026a02017-02-10 15:13:40 +01004223 try:
gcalvinoe580c7d2017-09-22 14:09:51 +02004224 if ssh_access['required'] and ssh_access['default-user']:
4225 if 'ip_address' in vm:
4226 mgmt_ip = vm['ip_address'].split(';')
4227 password = mgmt_access['config-access'].get('password')
4228 priv_RO_key = decrypt_key(tenant[0]['encrypted_RO_priv_key'], tenant[0]['uuid'])
4229 myvim.inject_user_key(mgmt_ip[0], ssh_access['default-user'],
4230 action_dict['add_public_key'],
4231 password=password, ro_key=priv_RO_key)
4232 else:
4233 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
4234 HTTP_Internal_Server_Error)
4235 except KeyError:
4236 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
4237 HTTP_Internal_Server_Error)
4238 else:
4239 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
4240 HTTP_Internal_Server_Error)
4241 else:
4242 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
4243 if "console" in action_dict:
4244 if not global_config["http_console_proxy"]:
tierno20fc2a22016-08-19 17:02:35 +02004245 vm_result[ vm['uuid'] ] = {"vim_result": 200,
4246 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4247 protocol=data["protocol"],
gcalvinoe580c7d2017-09-22 14:09:51 +02004248 ip = data["server"],
4249 port = data["port"],
tierno20fc2a22016-08-19 17:02:35 +02004250 suffix = data["suffix"]),
4251 "name":vm['name']
4252 }
4253 vm_ok +=1
gcalvinoe580c7d2017-09-22 14:09:51 +02004254 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
4255 vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized,
4256 "description": "this console is only reachable by local interface",
4257 "name":vm['name']
4258 }
tierno20fc2a22016-08-19 17:02:35 +02004259 vm_error+=1
gcalvinoe580c7d2017-09-22 14:09:51 +02004260 else:
4261 #print "console data", data
4262 try:
4263 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
4264 vm_result[ vm['uuid'] ] = {"vim_result": 200,
4265 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4266 protocol=data["protocol"],
4267 ip = global_config["http_console_host"],
4268 port = console_thread.port,
4269 suffix = data["suffix"]),
4270 "name":vm['name']
4271 }
4272 vm_ok +=1
4273 except NfvoException as e:
4274 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
4275 vm_error+=1
tierno20fc2a22016-08-19 17:02:35 +02004276
gcalvinoe580c7d2017-09-22 14:09:51 +02004277 else:
4278 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
4279 vm_ok +=1
tiernoae4a8d12016-07-08 12:30:39 +02004280 except vimconn.vimconnException as e:
4281 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
4282 vm_error+=1
tierno7edb6752016-03-21 17:37:52 +01004283
4284 if vm_ok==0: #all goes wrong
tierno351863c2016-07-23 01:46:03 +02004285 return vm_result
tierno7edb6752016-03-21 17:37:52 +01004286 else:
tierno351863c2016-07-23 01:46:03 +02004287 return vm_result
tierno42026a02017-02-10 15:13:40 +01004288
tierno868220c2017-09-26 00:11:05 +02004289def instance_action_get(mydb, nfvo_tenant, instance_id, action_id):
tierno16e3dd42018-04-24 12:52:40 +02004290 filter = {}
tierno868220c2017-09-26 00:11:05 +02004291 if nfvo_tenant and nfvo_tenant != "any":
4292 filter["tenant_id"] = nfvo_tenant
4293 if instance_id and instance_id != "any":
4294 filter["instance_id"] = instance_id
4295 if action_id:
4296 filter["uuid"] = action_id
4297 rows = mydb.get_rows(FROM="instance_actions", WHERE=filter)
tierno16e3dd42018-04-24 12:52:40 +02004298 if action_id:
4299 if not rows:
4300 raise NfvoException("Not found any action with this criteria", HTTP_Not_Found)
4301 vim_actions = mydb.get_rows(FROM="vim_actions", WHERE={"instance_action_id": action_id})
4302 rows[0]["vim_actions"] = vim_actions
tierno868220c2017-09-26 00:11:05 +02004303 return {"ations": rows}
4304
tiernob3d36742017-03-03 23:51:05 +01004305
tierno7edb6752016-03-21 17:37:52 +01004306def create_or_use_console_proxy_thread(console_server, console_port):
4307 #look for a non-used port
4308 console_thread_key = console_server + ":" + str(console_port)
4309 if console_thread_key in global_config["console_thread"]:
4310 #global_config["console_thread"][console_thread_key].start_timeout()
tiernof97fd272016-07-11 14:32:37 +02004311 return global_config["console_thread"][console_thread_key]
tierno42026a02017-02-10 15:13:40 +01004312
tierno7edb6752016-03-21 17:37:52 +01004313 for port in global_config["console_port_iterator"]():
tierno20fc2a22016-08-19 17:02:35 +02004314 #print "create_or_use_console_proxy_thread() port:", port
tierno7edb6752016-03-21 17:37:52 +01004315 if port in global_config["console_ports"]:
4316 continue
4317 try:
4318 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
4319 clithread.start()
4320 global_config["console_thread"][console_thread_key] = clithread
4321 global_config["console_ports"][port] = console_thread_key
tiernof97fd272016-07-11 14:32:37 +02004322 return clithread
tierno7edb6752016-03-21 17:37:52 +01004323 except cli.ConsoleProxyExceptionPortUsed as e:
4324 #port used, try with onoher
4325 continue
4326 except cli.ConsoleProxyException as e:
tiernof97fd272016-07-11 14:32:37 +02004327 raise NfvoException(str(e), HTTP_Bad_Request)
4328 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01004329
tiernob3d36742017-03-03 23:51:05 +01004330
tierno7edb6752016-03-21 17:37:52 +01004331def check_tenant(mydb, tenant_id):
4332 '''check that tenant exists at database'''
tiernof97fd272016-07-11 14:32:37 +02004333 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
4334 if not tenant:
4335 raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found)
4336 return
tierno7edb6752016-03-21 17:37:52 +01004337
4338def new_tenant(mydb, tenant_dict):
tierno7edb6752016-03-21 17:37:52 +01004339
gcalvinoe580c7d2017-09-22 14:09:51 +02004340 tenant_uuid = str(uuid4())
4341 tenant_dict['uuid'] = tenant_uuid
4342 try:
4343 pub_key, priv_key = create_RO_keypair(tenant_uuid)
4344 tenant_dict['RO_pub_key'] = pub_key
4345 tenant_dict['encrypted_RO_priv_key'] = priv_key
gcalvinoc62cfa52017-10-05 18:21:25 +02004346 mydb.new_row("nfvo_tenants", tenant_dict, confidential_data=True)
gcalvinoe580c7d2017-09-22 14:09:51 +02004347 except db_base_Exception as e:
tierno9c5c8322018-03-23 15:44:03 +01004348 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict['name']) + str(e), e.http_code)
gcalvinoe580c7d2017-09-22 14:09:51 +02004349 return tenant_uuid
tiernob3d36742017-03-03 23:51:05 +01004350
tierno7edb6752016-03-21 17:37:52 +01004351def delete_tenant(mydb, tenant):
4352 #get nfvo_tenant info
tierno42026a02017-02-10 15:13:40 +01004353
tiernof97fd272016-07-11 14:32:37 +02004354 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
4355 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
4356 return tenant_dict['uuid'] + " " + tenant_dict["name"]
tierno7edb6752016-03-21 17:37:52 +01004357
tiernob3d36742017-03-03 23:51:05 +01004358
tierno7edb6752016-03-21 17:37:52 +01004359def new_datacenter(mydb, datacenter_descriptor):
4360 if "config" in datacenter_descriptor:
tiernoedf3f4f2018-05-17 23:02:47 +02004361 sdn_port_mapping = datacenter_descriptor["config"].pop("sdn-port-mapping", None)
4362 datacenter_descriptor["config"] = yaml.safe_dump(datacenter_descriptor["config"], default_flow_style=True,
4363 width=256)
4364 # Check that datacenter-type is correct
tierno3ae39742016-09-07 12:17:51 +02004365 datacenter_type = datacenter_descriptor.get("type", "openvim");
tiernoedf3f4f2018-05-17 23:02:47 +02004366 # module_info = None
tierno3ae39742016-09-07 12:17:51 +02004367 try:
4368 module = "vimconn_" + datacenter_type
tierno361275f2017-04-25 16:24:34 +02004369 pkg = __import__("osm_ro." + module)
tiernoedf3f4f2018-05-17 23:02:47 +02004370 # vim_conn = getattr(pkg, module)
tierno361275f2017-04-25 16:24:34 +02004371 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
tierno3ae39742016-09-07 12:17:51 +02004372 except (IOError, ImportError):
tierno361275f2017-04-25 16:24:34 +02004373 # if module_info and module_info[0]:
4374 # file.close(module_info[0])
tiernoedf3f4f2018-05-17 23:02:47 +02004375 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type,
4376 module),
4377 HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +01004378
gcalvinoc62cfa52017-10-05 18:21:25 +02004379 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True, confidential_data=True)
tiernoedf3f4f2018-05-17 23:02:47 +02004380 if sdn_port_mapping:
4381 try:
4382 datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, sdn_port_mapping)
4383 except Exception as e:
4384 mydb.delete_row_by_id("datacenters", datacenter_id) # Rollback
4385 raise e
tiernof97fd272016-07-11 14:32:37 +02004386 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01004387
tiernob3d36742017-03-03 23:51:05 +01004388
tierno7edb6752016-03-21 17:37:52 +01004389def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
tierno8fe7a492017-07-11 13:50:04 +02004390 # obtain data, check that only one exist
tiernof97fd272016-07-11 14:32:37 +02004391 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
tierno8fe7a492017-07-11 13:50:04 +02004392
4393 # edit data
tiernof97fd272016-07-11 14:32:37 +02004394 datacenter_id = datacenter['uuid']
4395 where={'uuid': datacenter['uuid']}
tierno8fe7a492017-07-11 13:50:04 +02004396 remove_port_mapping = False
tiernoedf3f4f2018-05-17 23:02:47 +02004397 new_sdn_port_mapping = None
tierno7edb6752016-03-21 17:37:52 +01004398 if "config" in datacenter_descriptor:
tierno8fe7a492017-07-11 13:50:04 +02004399 if datacenter_descriptor['config'] != None:
tierno7edb6752016-03-21 17:37:52 +01004400 try:
4401 new_config_dict = datacenter_descriptor["config"]
tiernoedf3f4f2018-05-17 23:02:47 +02004402 if "sdn-port-mapping" in new_config_dict:
4403 remove_port_mapping = True
4404 new_sdn_port_mapping = new_config_dict.pop("sdn-port-mapping")
tierno7edb6752016-03-21 17:37:52 +01004405 #delete null fields
4406 to_delete=[]
4407 for k in new_config_dict:
tierno8fe7a492017-07-11 13:50:04 +02004408 if new_config_dict[k] == None:
tierno7edb6752016-03-21 17:37:52 +01004409 to_delete.append(k)
tierno8fe7a492017-07-11 13:50:04 +02004410 if k == 'sdn-controller':
4411 remove_port_mapping = True
tierno42026a02017-02-10 15:13:40 +01004412
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004413 config_text = datacenter.get("config")
4414 if not config_text:
4415 config_text = '{}'
4416 config_dict = yaml.load(config_text)
tierno7edb6752016-03-21 17:37:52 +01004417 config_dict.update(new_config_dict)
4418 #delete null fields
4419 for k in to_delete:
4420 del config_dict[k]
tiernof97fd272016-07-11 14:32:37 +02004421 except Exception as e:
4422 raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request)
tierno8fe7a492017-07-11 13:50:04 +02004423 if config_dict:
4424 datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256)
4425 else:
4426 datacenter_descriptor["config"] = None
4427 if remove_port_mapping:
4428 try:
4429 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id)
4430 except ovimException as e:
4431 logger.error("Error deleting datacenter-port-mapping " + str(e))
4432
tiernof97fd272016-07-11 14:32:37 +02004433 mydb.update_rows('datacenters', datacenter_descriptor, where)
tiernoedf3f4f2018-05-17 23:02:47 +02004434 if new_sdn_port_mapping:
4435 try:
4436 datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, new_sdn_port_mapping)
4437 except ovimException as e:
4438 logger.error("Error adding datacenter-port-mapping " + str(e))
tiernof97fd272016-07-11 14:32:37 +02004439 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01004440
tiernob3d36742017-03-03 23:51:05 +01004441
tierno7edb6752016-03-21 17:37:52 +01004442def delete_datacenter(mydb, datacenter):
4443 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02004444 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
4445 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
tierno8fe7a492017-07-11 13:50:04 +02004446 try:
4447 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid'])
4448 except ovimException as e:
4449 logger.error("Error deleting datacenter-port-mapping " + str(e))
tiernof97fd272016-07-11 14:32:37 +02004450 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
tierno7edb6752016-03-21 17:37:52 +01004451
tiernob3d36742017-03-03 23:51:05 +01004452
tierno8008c3a2016-10-13 15:34:28 +00004453def associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter, vim_tenant_id=None, vim_tenant_name=None, vim_username=None, vim_password=None, config=None):
tierno9c22f2d2017-10-09 16:23:55 +02004454 # get datacenter info
tierno0ea2a7e2017-10-18 00:06:26 +02004455 try:
4456 datacenter_id = get_datacenter_uuid(mydb, None, datacenter)
tierno7edb6752016-03-21 17:37:52 +01004457
tierno0ea2a7e2017-10-18 00:06:26 +02004458 create_vim_tenant = True if not vim_tenant_id and not vim_tenant_name else False
tierno42026a02017-02-10 15:13:40 +01004459
tierno0ea2a7e2017-10-18 00:06:26 +02004460 # get nfvo_tenant info
4461 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
4462 if vim_tenant_name==None:
4463 vim_tenant_name=tenant_dict['name']
tierno42026a02017-02-10 15:13:40 +01004464
tierno0ea2a7e2017-10-18 00:06:26 +02004465 #check that this association does not exist before
4466 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
4467 tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
4468 if len(tenants_datacenters)>0:
4469 raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01004470
tierno0ea2a7e2017-10-18 00:06:26 +02004471 vim_tenant_id_exist_atdb=False
4472 if not create_vim_tenant:
4473 where_={"datacenter_id": datacenter_id}
4474 if vim_tenant_id!=None:
4475 where_["vim_tenant_id"] = vim_tenant_id
4476 if vim_tenant_name!=None:
4477 where_["vim_tenant_name"] = vim_tenant_name
4478 #check if vim_tenant_id is already at database
4479 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
4480 if len(datacenter_tenants_dict)>=1:
4481 datacenter_tenants_dict = datacenter_tenants_dict[0]
4482 vim_tenant_id_exist_atdb=True
4483 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
4484 else: #result=0
4485 datacenter_tenants_dict = {}
4486 #insert at table datacenter_tenants
4487 else: #if vim_tenant_id==None:
4488 #create tenant at VIM if not provided
4489 try:
4490 _, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username,
4491 vim_passwd=vim_password)
4492 datacenter_name = myvim["name"]
4493 vim_tenant_id = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
4494 except vimconn.vimconnException as e:
4495 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 +01004496 datacenter_tenants_dict = {}
tierno0ea2a7e2017-10-18 00:06:26 +02004497 datacenter_tenants_dict["created"]="true"
tierno42026a02017-02-10 15:13:40 +01004498
tierno0ea2a7e2017-10-18 00:06:26 +02004499 #fill datacenter_tenants table
4500 if not vim_tenant_id_exist_atdb:
4501 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant_id
4502 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
4503 datacenter_tenants_dict["user"] = vim_username
4504 datacenter_tenants_dict["passwd"] = vim_password
4505 datacenter_tenants_dict["datacenter_id"] = datacenter_id
4506 if config:
4507 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
4508 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True, confidential_data=True)
4509 datacenter_tenants_dict["uuid"] = id_
tierno42026a02017-02-10 15:13:40 +01004510
tierno0ea2a7e2017-10-18 00:06:26 +02004511 #fill tenants_datacenters table
4512 datacenter_tenant_id = datacenter_tenants_dict["uuid"]
4513 tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id
4514 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
4515 # create thread
4516 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_dict['uuid'], datacenter_id) # reload data
4517 datacenter_name = myvim["name"]
4518 thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid'])
4519 new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, datacenter_name, datacenter_tenant_id,
4520 db=db, db_lock=db_lock, ovim=ovim)
4521 new_thread.start()
4522 thread_id = datacenter_tenants_dict["uuid"]
4523 vim_threads["running"][thread_id] = new_thread
4524 return datacenter_id
4525 except vimconn.vimconnException as e:
4526 raise NfvoException(str(e), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01004527
tierno99314902017-04-26 13:23:09 +02004528
4529def edit_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=None, vim_tenant_name=None,
4530 vim_username=None, vim_password=None, config=None):
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004531 #Obtain the data of this datacenter_tenant_id
4532 vim_data = mydb.get_rows(
4533 SELECT=("datacenter_tenants.vim_tenant_name", "datacenter_tenants.vim_tenant_id", "datacenter_tenants.user",
4534 "datacenter_tenants.passwd", "datacenter_tenants.config"),
4535 FROM="datacenter_tenants JOIN tenants_datacenters ON datacenter_tenants.uuid=tenants_datacenters.datacenter_tenant_id",
4536 WHERE={"tenants_datacenters.nfvo_tenant_id": nfvo_tenant,
4537 "tenants_datacenters.datacenter_id": datacenter_id})
4538
4539 logger.debug(str(vim_data))
4540 if len(vim_data) < 1:
4541 raise NfvoException("Datacenter {} is not attached for tenant {}".format(datacenter_id, nfvo_tenant), HTTP_Conflict)
4542
4543 v = vim_data[0]
4544 if v['config']:
4545 v['config'] = yaml.load(v['config'])
4546
4547 if vim_tenant_id:
4548 v['vim_tenant_id'] = vim_tenant_id
4549 if vim_tenant_name:
4550 v['vim_tenant_name'] = vim_tenant_name
4551 if vim_username:
4552 v['user'] = vim_username
4553 if vim_password:
4554 v['passwd'] = vim_password
4555 if config:
4556 if not v['config']:
4557 v['config'] = {}
4558 v['config'].update(config)
4559
4560 logger.debug(str(v))
4561 deassociate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'])
4562 associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'], vim_tenant_name=v['vim_tenant_name'],
4563 vim_username=v['user'], vim_password=v['passwd'], config=v['config'])
4564
4565 return datacenter_id
tiernob3d36742017-03-03 23:51:05 +01004566
tierno7edb6752016-03-21 17:37:52 +01004567def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=None):
tierno7edb6752016-03-21 17:37:52 +01004568 #get nfvo_tenant info
4569 if not tenant_id or tenant_id=="any":
4570 tenant_uuid = None
4571 else:
tiernof97fd272016-07-11 14:32:37 +02004572 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
tierno7edb6752016-03-21 17:37:52 +01004573 tenant_uuid = tenant_dict['uuid']
4574
tierno0ea2a7e2017-10-18 00:06:26 +02004575 datacenter_id = get_datacenter_uuid(mydb, tenant_uuid, datacenter)
tierno7edb6752016-03-21 17:37:52 +01004576 #check that this association exist before
tierno0ea2a7e2017-10-18 00:06:26 +02004577 tenants_datacenter_dict={"datacenter_id": datacenter_id }
tierno7edb6752016-03-21 17:37:52 +01004578 if tenant_uuid:
4579 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
tiernof97fd272016-07-11 14:32:37 +02004580 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
4581 if len(tenant_datacenter_list)==0 and tenant_uuid:
4582 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01004583
4584 #delete this association
tiernof97fd272016-07-11 14:32:37 +02004585 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
tierno7edb6752016-03-21 17:37:52 +01004586
4587 #get vim_tenant info and deletes
4588 warning=''
4589 for tenant_datacenter_item in tenant_datacenter_list:
tiernof97fd272016-07-11 14:32:37 +02004590 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
4591 #try to delete vim:tenant
4592 try:
4593 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
4594 if vim_tenant_dict['created']=='true':
tierno7edb6752016-03-21 17:37:52 +01004595 #delete tenant at VIM if created by NFVO
tierno42026a02017-02-10 15:13:40 +01004596 try:
tierno0ea2a7e2017-10-18 00:06:26 +02004597 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02004598 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
4599 except vimconn.vimconnException as e:
4600 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
4601 logger.warn(warning)
tiernof97fd272016-07-11 14:32:37 +02004602 except db_base_Exception as e:
4603 logger.error("Cannot delete datacenter_tenants " + str(e))
tierno42026a02017-02-10 15:13:40 +01004604 pass # the error will be caused because dependencies, vim_tenant can not be deleted
tierno867ffe92017-03-27 12:50:34 +02004605 thread_id = tenant_datacenter_item["datacenter_tenant_id"]
tiernoa3572692018-05-14 13:09:33 +02004606 thread = vim_threads["running"].get(thread_id)
4607 if thread:
4608 thread.insert_task("exit")
4609 vim_threads["deleting"][thread_id] = thread
tiernof97fd272016-07-11 14:32:37 +02004610 return "datacenter {} detached. {}".format(datacenter_id, warning)
tierno7edb6752016-03-21 17:37:52 +01004611
tiernob3d36742017-03-03 23:51:05 +01004612
tierno7edb6752016-03-21 17:37:52 +01004613def datacenter_action(mydb, tenant_id, datacenter, action_dict):
4614 #DEPRECATED
tierno42026a02017-02-10 15:13:40 +01004615 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00004616 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01004617
4618 if 'net-update' in action_dict:
tiernoae4a8d12016-07-08 12:30:39 +02004619 try:
tiernof97fd272016-07-11 14:32:37 +02004620 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
tiernoae4a8d12016-07-08 12:30:39 +02004621 #print content
4622 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02004623 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
4624 raise NfvoException(str(e), HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01004625 #update nets Change from VIM format to NFVO format
4626 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02004627 for net in nets:
tierno7edb6752016-03-21 17:37:52 +01004628 net_nfvo={'datacenter_id': datacenter_id}
4629 net_nfvo['name'] = net['name']
4630 #net_nfvo['description']= net['name']
4631 net_nfvo['vim_net_id'] = net['id']
4632 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
4633 net_nfvo['shared'] = net['shared']
4634 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
4635 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02004636 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
4637 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
4638 return inserted
tierno7edb6752016-03-21 17:37:52 +01004639 elif 'net-edit' in action_dict:
4640 net = action_dict['net-edit'].pop('net')
tierno42fcc3b2016-07-06 17:20:40 +02004641 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01004642 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
tierno7edb6752016-03-21 17:37:52 +01004643 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02004644 return result
tierno7edb6752016-03-21 17:37:52 +01004645 elif 'net-delete' in action_dict:
4646 net = action_dict['net-deelte'].get('net')
tierno42fcc3b2016-07-06 17:20:40 +02004647 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01004648 result = mydb.delete_row(FROM='datacenter_nets',
tierno7edb6752016-03-21 17:37:52 +01004649 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02004650 return result
tierno7edb6752016-03-21 17:37:52 +01004651
4652 else:
tiernof97fd272016-07-11 14:32:37 +02004653 raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01004654
tiernob3d36742017-03-03 23:51:05 +01004655
tierno7edb6752016-03-21 17:37:52 +01004656def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
4657 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00004658 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01004659
tierno42fcc3b2016-07-06 17:20:40 +02004660 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
tierno42026a02017-02-10 15:13:40 +01004661 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
tierno7edb6752016-03-21 17:37:52 +01004662 WHERE={'datacenter_id':datacenter_id, what: netmap})
tiernof97fd272016-07-11 14:32:37 +02004663 return result
tierno7edb6752016-03-21 17:37:52 +01004664
tiernob3d36742017-03-03 23:51:05 +01004665
tierno7edb6752016-03-21 17:37:52 +01004666def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
4667 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00004668 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01004669 filter_dict={}
4670 if action_dict:
4671 action_dict = action_dict["netmap"]
4672 if 'vim_id' in action_dict:
4673 filter_dict["id"] = action_dict['vim_id']
4674 if 'vim_name' in action_dict:
4675 filter_dict["name"] = action_dict['vim_name']
4676 else:
4677 filter_dict["shared"] = True
tierno42026a02017-02-10 15:13:40 +01004678
tiernoae4a8d12016-07-08 12:30:39 +02004679 try:
tiernof97fd272016-07-11 14:32:37 +02004680 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02004681 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02004682 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
4683 raise NfvoException(str(e), HTTP_Internal_Server_Error)
4684 if len(vim_nets)>1 and action_dict:
4685 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict)
4686 elif len(vim_nets)==0: # and action_dict:
4687 raise NfvoException("Not found a network at VIM with " + str(filter_dict), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01004688 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02004689 for net in vim_nets:
tierno7edb6752016-03-21 17:37:52 +01004690 net_nfvo={'datacenter_id': datacenter_id}
4691 if action_dict and "name" in action_dict:
4692 net_nfvo['name'] = action_dict['name']
4693 else:
4694 net_nfvo['name'] = net['name']
4695 #net_nfvo['description']= net['name']
4696 net_nfvo['vim_net_id'] = net['id']
4697 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
4698 net_nfvo['shared'] = net['shared']
4699 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
tiernof97fd272016-07-11 14:32:37 +02004700 try:
4701 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01004702 net_nfvo["status"] = "OK"
tiernof97fd272016-07-11 14:32:37 +02004703 net_nfvo["uuid"] = net_id
4704 except db_base_Exception as e:
4705 if action_dict:
4706 raise
4707 else:
4708 net_nfvo["status"] = "FAIL: " + str(e)
tierno42026a02017-02-10 15:13:40 +01004709 net_list.append(net_nfvo)
4710 return net_list
tierno7edb6752016-03-21 17:37:52 +01004711
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004712def get_sdn_net_id(mydb, tenant_id, datacenter, network_id):
4713 # obtain all network data
4714 try:
4715 if utils.check_valid_uuid(network_id):
4716 filter_dict = {"id": network_id}
4717 else:
4718 filter_dict = {"name": network_id}
4719
4720 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
4721 network = myvim.get_network_list(filter_dict=filter_dict)
4722 except vimconn.vimconnException as e:
tiernof1ba57e2017-09-07 12:23:19 +02004723 raise NfvoException("Not possible to get_sdn_net_id from VIM: {}".format(str(e)), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004724
4725 # ensure the network is defined
4726 if len(network) == 0:
4727 raise NfvoException("Network {} is not present in the system".format(network_id),
4728 HTTP_Bad_Request)
4729
4730 # ensure there is only one network with the provided name
4731 if len(network) > 1:
4732 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), HTTP_Bad_Request)
4733
4734 # ensure it is a dataplane network
4735 if network[0]['type'] != 'data':
4736 return None
4737
4738 # ensure we use the id
4739 network_id = network[0]['id']
4740
4741 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
4742 # and with instance_scenario_id==NULL
4743 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
4744 search_dict = {'vim_net_id': network_id}
4745
4746 try:
4747 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
4748 result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)
4749 except db_base_Exception as e:
4750 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
tierno9c5c8322018-03-23 15:44:03 +01004751 network_id) + str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004752
4753 sdn_net_counter = 0
4754 for net in result:
4755 if net['sdn_net_id'] != None:
4756 sdn_net_counter+=1
4757 sdn_net_id = net['sdn_net_id']
4758
4759 if sdn_net_counter == 0:
4760 return None
4761 elif sdn_net_counter == 1:
4762 return sdn_net_id
4763 else:
4764 raise NfvoException("More than one SDN network is associated to vim network {}".format(
4765 network_id), HTTP_Internal_Server_Error)
4766
4767def get_sdn_controller_id(mydb, datacenter):
4768 # Obtain sdn controller id
4769 config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}')
4770 if not config:
4771 return None
4772
4773 return yaml.load(config).get('sdn-controller')
4774
4775def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor):
4776 try:
4777 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
4778 if not sdn_network_id:
4779 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), HTTP_Internal_Server_Error)
4780
4781 #Obtain sdn controller id
4782 controller_id = get_sdn_controller_id(mydb, datacenter)
4783 if not controller_id:
4784 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), HTTP_Internal_Server_Error)
4785
4786 #Obtain sdn controller info
4787 sdn_controller = ovim.show_of_controller(controller_id)
4788
4789 port_data = {
4790 'name': 'external_port',
4791 'net_id': sdn_network_id,
4792 'ofc_id': controller_id,
4793 'switch_dpid': sdn_controller['dpid'],
4794 'switch_port': descriptor['port']
4795 }
4796
4797 if 'vlan' in descriptor:
4798 port_data['vlan'] = descriptor['vlan']
4799 if 'mac' in descriptor:
4800 port_data['mac'] = descriptor['mac']
4801
4802 result = ovim.new_port(port_data)
4803 except ovimException as e:
4804 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
4805 sdn_network_id, network_id) + str(e), HTTP_Internal_Server_Error)
4806 except db_base_Exception as e:
4807 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
tierno9c5c8322018-03-23 15:44:03 +01004808 network_id) + str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004809
4810 return 'Port uuid: '+ result
4811
4812def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None):
4813 if port_id:
4814 filter = {'uuid': port_id}
4815 else:
4816 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
4817 if not sdn_network_id:
4818 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id),
4819 HTTP_Internal_Server_Error)
4820 #in case no port_id is specified only ports marked as 'external_port' will be detached
4821 filter = {'name': 'external_port', 'net_id': sdn_network_id}
4822
4823 try:
4824 port_list = ovim.get_ports(columns={'uuid'}, filter=filter)
4825 except ovimException as e:
4826 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
4827 HTTP_Internal_Server_Error)
4828
4829 if len(port_list) == 0:
4830 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id),
4831 HTTP_Bad_Request)
4832
4833 port_uuid_list = []
4834 for port in port_list:
4835 try:
4836 port_uuid_list.append(port['uuid'])
4837 ovim.delete_port(port['uuid'])
4838 except ovimException as e:
4839 raise NfvoException("ovimException deleting port {} for net {}. ".format(port['uuid'], network_id) + str(e), HTTP_Internal_Server_Error)
4840
4841 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list))
tiernob3d36742017-03-03 23:51:05 +01004842
tierno7edb6752016-03-21 17:37:52 +01004843def vim_action_get(mydb, tenant_id, datacenter, item, name):
4844 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00004845 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01004846 filter_dict={}
4847 if name:
tierno42fcc3b2016-07-06 17:20:40 +02004848 if utils.check_valid_uuid(name):
tierno7edb6752016-03-21 17:37:52 +01004849 filter_dict["id"] = name
4850 else:
4851 filter_dict["name"] = name
tiernoae4a8d12016-07-08 12:30:39 +02004852 try:
4853 if item=="networks":
4854 #filter_dict['tenant_id'] = myvim['tenant_id']
4855 content = myvim.get_network_list(filter_dict=filter_dict)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004856
4857 if len(content) == 0:
4858 raise NfvoException("Network {} is not present in the system. ".format(name),
4859 HTTP_Bad_Request)
4860
4861 #Update the networks with the attached ports
4862 for net in content:
4863 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id'])
4864 if sdn_network_id != None:
4865 try:
4866 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
4867 port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id})
4868 except ovimException as e:
4869 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), HTTP_Internal_Server_Error)
4870 #Remove field name and if port name is external_port save it as 'type'
4871 for port in port_list:
4872 if port['name'] == 'external_port':
4873 port['type'] = "External"
4874 del port['name']
4875 net['sdn_network_id'] = sdn_network_id
4876 net['sdn_attached_ports'] = port_list
4877
tiernoae4a8d12016-07-08 12:30:39 +02004878 elif item=="tenants":
4879 content = myvim.get_tenant_list(filter_dict=filter_dict)
tierno4540ea52017-01-18 17:44:32 +01004880 elif item == "images":
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004881
tierno4540ea52017-01-18 17:44:32 +01004882 content = myvim.get_image_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02004883 else:
tiernof97fd272016-07-11 14:32:37 +02004884 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernobe41e222016-09-02 15:16:13 +02004885 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
tiernoae4a8d12016-07-08 12:30:39 +02004886 if name and len(content)==1:
tiernof97fd272016-07-11 14:32:37 +02004887 return {item[:-1]: content[0]}
tiernoae4a8d12016-07-08 12:30:39 +02004888 elif name and len(content)==0:
tiernof97fd272016-07-11 14:32:37 +02004889 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 +02004890 datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02004891 else:
tiernof97fd272016-07-11 14:32:37 +02004892 return {item: content}
tiernoae4a8d12016-07-08 12:30:39 +02004893 except vimconn.vimconnException as e:
4894 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
tiernof97fd272016-07-11 14:32:37 +02004895 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
tierno42026a02017-02-10 15:13:40 +01004896
tiernob3d36742017-03-03 23:51:05 +01004897
tierno7edb6752016-03-21 17:37:52 +01004898def vim_action_delete(mydb, tenant_id, datacenter, item, name):
4899 #get datacenter info
tierno392f2852016-05-13 12:28:55 +02004900 if tenant_id == "any":
4901 tenant_id=None
4902
tiernoa2793912016-10-04 08:15:08 +00004903 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno392f2852016-05-13 12:28:55 +02004904 #get uuid name
tiernof97fd272016-07-11 14:32:37 +02004905 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
4906 logger.debug("vim_action_delete vim response: " + str(content))
tierno392f2852016-05-13 12:28:55 +02004907 items = content.values()[0]
4908 if type(items)==list and len(items)==0:
tiernof97fd272016-07-11 14:32:37 +02004909 raise NfvoException("Not found " + item, HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02004910 elif type(items)==list and len(items)>1:
tiernof97fd272016-07-11 14:32:37 +02004911 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02004912 else: # it is a dict
4913 item_id = items["id"]
4914 item_name = str(items.get("name"))
tierno42026a02017-02-10 15:13:40 +01004915
tiernoae4a8d12016-07-08 12:30:39 +02004916 try:
4917 if item=="networks":
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004918 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
4919 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id)
4920 if sdn_network_id != None:
4921 #Delete any port attachment to this network
4922 try:
4923 port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id})
4924 except ovimException as e:
4925 raise NfvoException(
4926 "ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
4927 HTTP_Internal_Server_Error)
4928
4929 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
4930 for port in port_list:
4931 vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid'])
4932
4933 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
4934 try:
4935 mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id})
4936 except db_base_Exception as e:
4937 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) +
tierno9c5c8322018-03-23 15:44:03 +01004938 str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004939
4940 #Delete the SDN network
4941 try:
4942 ovim.delete_network(sdn_network_id)
4943 except ovimException as e:
4944 logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True)
4945 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e),
4946 HTTP_Internal_Server_Error)
4947
tiernoae4a8d12016-07-08 12:30:39 +02004948 content = myvim.delete_network(item_id)
4949 elif item=="tenants":
4950 content = myvim.delete_tenant(item_id)
tierno4540ea52017-01-18 17:44:32 +01004951 elif item == "images":
4952 content = myvim.delete_image(item_id)
tiernoae4a8d12016-07-08 12:30:39 +02004953 else:
tierno42026a02017-02-10 15:13:40 +01004954 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02004955 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02004956 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
4957 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02004958
tiernof97fd272016-07-11 14:32:37 +02004959 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
tierno42026a02017-02-10 15:13:40 +01004960
tiernob3d36742017-03-03 23:51:05 +01004961
tierno7edb6752016-03-21 17:37:52 +01004962def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
4963 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00004964 logger.debug("vim_action_create descriptor %s", str(descriptor))
tierno392f2852016-05-13 12:28:55 +02004965 if tenant_id == "any":
4966 tenant_id=None
tiernoa2793912016-10-04 08:15:08 +00004967 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02004968 try:
4969 if item=="networks":
4970 net = descriptor["network"]
4971 net_name = net.pop("name")
4972 net_type = net.pop("type", "bridge")
garciadeblas9f8456e2016-09-05 05:02:59 +02004973 net_public = net.pop("shared", False)
4974 net_ipprofile = net.pop("ip_profile", None)
tiernoa7d34d02017-02-23 14:42:07 +01004975 net_vlan = net.pop("vlan", None)
4976 content = myvim.new_network(net_name, net_type, net_ipprofile, shared=net_public, vlan=net_vlan) #, **net)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004977
4978 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
4979 if get_sdn_controller_id(mydb, datacenter) != None and (net_type == 'data' or net_type == 'ptp'):
tierno00e3df72017-11-29 17:20:13 +01004980 #obtain datacenter_tenant_id
4981 datacenter_tenant_id = mydb.get_rows(SELECT=('uuid',),
4982 FROM='datacenter_tenants',
4983 WHERE={'datacenter_id': datacenter})[0]['uuid']
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004984 try:
4985 sdn_network = {}
4986 sdn_network['vlan'] = net_vlan
4987 sdn_network['type'] = net_type
4988 sdn_network['name'] = net_name
tierno00e3df72017-11-29 17:20:13 +01004989 sdn_network['region'] = datacenter_tenant_id
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004990 ovim_content = ovim.new_network(sdn_network)
4991 except ovimException as e:
tierno00e3df72017-11-29 17:20:13 +01004992 logger.error("ovimException creating SDN network={} ".format(
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004993 sdn_network) + str(e), exc_info=True)
4994 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e),
4995 HTTP_Internal_Server_Error)
4996
4997 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
4998 # use instance_scenario_id=None to distinguish from real instaces of nets
tierno00e3df72017-11-29 17:20:13 +01004999 correspondence = {'instance_scenario_id': None,
5000 'sdn_net_id': ovim_content,
5001 'vim_net_id': content,
5002 'datacenter_tenant_id': datacenter_tenant_id
5003 }
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005004 try:
5005 mydb.new_row('instance_nets', correspondence, add_uuid=True)
5006 except db_base_Exception as e:
tierno00e3df72017-11-29 17:20:13 +01005007 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format(
tierno9c5c8322018-03-23 15:44:03 +01005008 correspondence, e), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005009 elif item=="tenants":
5010 tenant = descriptor["tenant"]
5011 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
5012 else:
tierno42026a02017-02-10 15:13:40 +01005013 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02005014 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005015 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005016
tierno7edb6752016-03-21 17:37:52 +01005017 return vim_action_get(mydb, tenant_id, datacenter, item, content)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005018
5019def sdn_controller_create(mydb, tenant_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005020 data = ovim.new_of_controller(sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005021 logger.debug('New SDN controller created with uuid {}'.format(data))
5022 return data
5023
5024def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005025 data = ovim.edit_of_controller(controller_id, sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005026 msg = 'SDN controller {} updated'.format(data)
5027 logger.debug(msg)
5028 return msg
5029
5030def sdn_controller_list(mydb, tenant_id, controller_id=None):
5031 if controller_id == None:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005032 data = ovim.get_of_controllers()
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005033 else:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005034 data = ovim.show_of_controller(controller_id)
5035
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005036 msg = 'SDN controller list:\n {}'.format(data)
5037 logger.debug(msg)
5038 return data
5039
5040def sdn_controller_delete(mydb, tenant_id, controller_id):
5041 select_ = ('uuid', 'config')
5042 datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_)
5043 for datacenter in datacenters:
5044 if datacenter['config']:
5045 config = yaml.load(datacenter['config'])
5046 if 'sdn-controller' in config and config['sdn-controller'] == controller_id:
5047 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), HTTP_Conflict)
5048
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005049 data = ovim.delete_of_controller(controller_id)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005050 msg = 'SDN controller {} deleted'.format(data)
5051 logger.debug(msg)
5052 return msg
5053
5054def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping):
5055 controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id})
5056 if len(controller) < 1:
5057 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), HTTP_Not_Found)
5058
5059 try:
5060 sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"]
5061 except:
5062 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), HTTP_Bad_Request)
5063
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005064 sdn_controller = ovim.show_of_controller(sdn_controller_id)
5065 switch_dpid = sdn_controller["dpid"]
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005066
5067 maps = list()
5068 for compute_node in sdn_port_mapping:
5069 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
5070 element = dict()
5071 element["compute_node"] = compute_node["compute_node"]
5072 for port in compute_node["ports"]:
5073 element["pci"] = port.get("pci")
5074 element["switch_port"] = port.get("switch_port")
5075 element["switch_mac"] = port.get("switch_mac")
5076 if not element["pci"] or not (element["switch_port"] or element["switch_mac"]):
5077 raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'"
5078 " or 'switch_mac'", HTTP_Bad_Request)
5079 maps.append(dict(element))
5080
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005081 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 +01005082
5083def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005084 maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id})
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005085
5086 result = {
5087 "sdn-controller": None,
5088 "datacenter-id": datacenter_id,
5089 "dpid": None,
5090 "ports_mapping": list()
5091 }
5092
5093 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id)
5094 if datacenter['config']:
5095 config = yaml.load(datacenter['config'])
5096 if 'sdn-controller' in config:
5097 controller_id = config['sdn-controller']
5098 sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id)
5099 result["sdn-controller"] = controller_id
5100 result["dpid"] = sdn_controller["dpid"]
5101
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005102 if result["sdn-controller"] == None:
5103 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), HTTP_Bad_Request)
5104 if result["dpid"] == None:
5105 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]),
5106 HTTP_Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005107
5108 if len(maps) == 0:
5109 return result
5110
5111 ports_correspondence_dict = dict()
5112 for link in maps:
5113 if result["sdn-controller"] != link["ofc_id"]:
5114 raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error)
5115 if result["dpid"] != link["switch_dpid"]:
5116 raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error)
5117 element = dict()
5118 element["pci"] = link["pci"]
5119 if link["switch_port"]:
5120 element["switch_port"] = link["switch_port"]
5121 if link["switch_mac"]:
5122 element["switch_mac"] = link["switch_mac"]
5123
5124 if not link["compute_node"] in ports_correspondence_dict:
5125 content = dict()
5126 content["compute_node"] = link["compute_node"]
5127 content["ports"] = list()
5128 ports_correspondence_dict[link["compute_node"]] = content
5129
5130 ports_correspondence_dict[link["compute_node"]]["ports"].append(element)
5131
5132 for key in sorted(ports_correspondence_dict):
5133 result["ports_mapping"].append(ports_correspondence_dict[key])
5134
5135 return result
5136
5137def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id):
tierno639520f2017-04-05 19:55:36 +02005138 return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id})
gcalvinoe580c7d2017-09-22 14:09:51 +02005139
5140def create_RO_keypair(tenant_id):
5141 """
5142 Creates a public / private keys for a RO tenant and returns their values
5143 Params:
5144 tenant_id: ID of the tenant
5145 Return:
5146 public_key: Public key for the RO tenant
5147 private_key: Encrypted private key for RO tenant
5148 """
5149
5150 bits = 2048
5151 key = RSA.generate(bits)
5152 try:
5153 public_key = key.publickey().exportKey('OpenSSH')
5154 if isinstance(public_key, ValueError):
5155 raise NfvoException("Unable to create public key: {}".format(public_key), HTTP_Internal_Server_Error)
5156 private_key = key.exportKey(passphrase=tenant_id, pkcs=8)
5157 except (ValueError, NameError) as e:
5158 raise NfvoException("Unable to create private key: {}".format(e), HTTP_Internal_Server_Error)
5159 return public_key, private_key
5160
5161def decrypt_key (key, tenant_id):
5162 """
5163 Decrypts an encrypted RSA key
5164 Params:
5165 key: Private key to be decrypted
5166 tenant_id: ID of the tenant
5167 Return:
5168 unencrypted_key: Unencrypted private key for RO tenant
5169 """
5170 try:
5171 key = RSA.importKey(key,tenant_id)
5172 unencrypted_key = key.exportKey('PEM')
5173 if isinstance(unencrypted_key, ValueError):
5174 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key), HTTP_Internal_Server_Error)
5175 except ValueError as e:
5176 raise NfvoException("Unable to decrypt the private key: {}".format(e), HTTP_Internal_Server_Error)
5177 return unencrypted_key