blob: 9cf115e089b15935f78e7f12269b42f4bad0cfc3 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- coding: utf-8 -*-
2
3##
tierno92021022018-09-12 16:29:23 +02004# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
tierno7edb6752016-03-21 17:37:52 +01005# 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##
calvinosanch0aa0e2f2019-11-07 11:46:38 +010023
tierno7edb6752016-03-21 17:37:52 +010024'''
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
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010031import json
tierno7edb6752016-03-21 17:37:52 +010032import yaml
tierno42fcc3b2016-07-06 17:20:40 +020033import utils
tiernob8569aa2018-08-24 11:34:54 +020034from utils import deprecated
tierno42026a02017-02-10 15:13:40 +010035import vim_thread
tierno7edb6752016-03-21 17:37:52 +010036import console_proxy_thread as cli
tiernoae4a8d12016-07-08 12:30:39 +020037import vimconn
38import logging
garciadeblas9f8456e2016-09-05 05:02:59 +020039import collections
tierno66eba6e2017-11-10 17:09:18 +010040import math
tierno8e690322017-08-10 15:58:50 +020041from uuid import uuid4
tiernof97fd272016-07-11 14:32:37 +020042from db_base import db_base_Exception
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +010043
tiernob3d36742017-03-03 23:51:05 +010044import nfvo_db
45from threading import Lock
tierno868220c2017-09-26 00:11:05 +020046import time as t
tierno01b3e172017-04-21 10:52:34 +020047from lib_osm_openvim import ovim as ovim_module
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +020048from lib_osm_openvim.ovim import ovimException
gcalvinoe580c7d2017-09-22 14:09:51 +020049from Crypto.PublicKey import RSA
tierno7edb6752016-03-21 17:37:52 +010050
tiernof1ba57e2017-09-07 12:23:19 +020051import osm_im.vnfd as vnfd_catalog
52import osm_im.nsd as nsd_catalog
tiernof1ba57e2017-09-07 12:23:19 +020053from pyangbind.lib.serialise import pybindJSONDecoder
tiernofc5f80b2018-05-29 16:00:43 +020054from copy import deepcopy
55
tiernof1ba57e2017-09-07 12:23:19 +020056
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010057# WIM
58import wim.wimconn as wimconn
59import wim.wim_thread as wim_thread
60from .http_tools import errors as httperrors
61from .wim.engine import WimEngine
62from .wim.persistence import WimPersistence
63from copy import deepcopy
Anderson Bravalherie2c09f32018-11-30 09:55:29 +000064from pprint import pformat
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010065#
66
tierno7edb6752016-03-21 17:37:52 +010067global global_config
68global vimconn_imported
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010069# WIM
70global wim_engine
71wim_engine = None
72global wimconn_imported
73#
tierno73ad9e42016-09-12 18:11:11 +020074global logger
montesmoreno0c8def02016-12-22 12:16:23 +000075global default_volume_size
76default_volume_size = '5' #size in GB
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +010077global ovim
78ovim = None
tiernoc5651792017-03-27 10:50:43 +020079global_config = None
tiernoae4a8d12016-07-08 12:30:39 +020080
tierno42026a02017-02-10 15:13:40 +010081vimconn_imported = {} # dictionary with VIM type as key, loaded module as value
82vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
tiernob3d36742017-03-03 23:51:05 +010083vim_persistent_info = {}
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010084# WIM
85wimconn_imported = {} # dictionary with WIM type as key, loaded module as value
86wim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-WIMs
87wim_persistent_info = {}
88#
89
tierno73ad9e42016-09-12 18:11:11 +020090logger = logging.getLogger('openmano.nfvo')
tiernob3d36742017-03-03 23:51:05 +010091task_lock = Lock()
tiernob3d36742017-03-03 23:51:05 +010092last_task_id = 0.0
tierno868220c2017-09-26 00:11:05 +020093db = None
94db_lock = Lock()
tierno7edb6752016-03-21 17:37:52 +010095
Anderson Bravalheri0446cd52018-08-17 15:26:19 +010096
97class NfvoException(httperrors.HttpMappedError):
98 """Common Class for NFVO errors"""
tierno7edb6752016-03-21 17:37:52 +010099
100
tiernob3d36742017-03-03 23:51:05 +0100101def get_task_id():
102 global last_task_id
tierno868220c2017-09-26 00:11:05 +0200103 task_id = t.time()
tiernob3d36742017-03-03 23:51:05 +0100104 if task_id <= last_task_id:
105 task_id = last_task_id + 0.000001
106 last_task_id = task_id
tierno868220c2017-09-26 00:11:05 +0200107 return "ACTION-{:.6f}".format(task_id)
108 # 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 +0100109
110
tierno867ffe92017-03-27 12:50:34 +0200111def new_task(name, params, depends=None):
tierno868220c2017-09-26 00:11:05 +0200112 """Deprected!!!"""
tiernob3d36742017-03-03 23:51:05 +0100113 task_id = get_task_id()
114 task = {"status": "enqueued", "id": task_id, "name": name, "params": params}
115 if depends:
116 task["depends"] = depends
tiernob3d36742017-03-03 23:51:05 +0100117 return task
118
119
120def is_task_id(id):
tierno868220c2017-09-26 00:11:05 +0200121 return True if id[:5] == "TASK-" else False
tiernob3d36742017-03-03 23:51:05 +0100122
123
tierno42026a02017-02-10 15:13:40 +0100124def get_non_used_vim_name(datacenter_name, datacenter_id, tenant_name, tenant_id):
125 name = datacenter_name[:16]
126 if name not in vim_threads["names"]:
127 vim_threads["names"].append(name)
128 return name
tierno98c11d82019-05-06 13:24:12 +0000129 if tenant_name:
130 name = datacenter_name[:16] + "." + tenant_name[:16]
131 if name not in vim_threads["names"]:
132 vim_threads["names"].append(name)
133 return name
134 name = datacenter_id
tierno42026a02017-02-10 15:13:40 +0100135 vim_threads["names"].append(name)
136 return name
137
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100138# -- Move
139def get_non_used_wim_name(wim_name, wim_id, tenant_name, tenant_id):
140 name = wim_name[:16]
141 if name not in wim_threads["names"]:
142 wim_threads["names"].append(name)
143 return name
144 name = wim_name[:16] + "." + tenant_name[:16]
145 if name not in wim_threads["names"]:
146 wim_threads["names"].append(name)
147 return name
148 name = wim_id + "-" + tenant_id
149 wim_threads["names"].append(name)
150 return name
tierno42026a02017-02-10 15:13:40 +0100151
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100152
153def start_service(mydb, persistence=None, wim=None):
tiernob3d36742017-03-03 23:51:05 +0100154 global db, global_config
Anderson Bravalheridfed5112019-02-08 01:44:14 +0000155 db = nfvo_db.nfvo_db(lock=db_lock)
156 mydb.lock = db_lock
tiernob3d36742017-03-03 23:51:05 +0100157 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 +0100158 global ovim
159
Anderson Bravalheridfed5112019-02-08 01:44:14 +0000160 persistence = persistence or WimPersistence(db)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100161
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100162 # Initialize openvim for SDN control
163 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
164 # TODO: review ovim.py to delete not needed configuration
165 ovim_configuration = {
tierno639520f2017-04-05 19:55:36 +0200166 'logger_name': 'openmano.ovim',
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100167 'network_vlan_range_start': 1000,
168 'network_vlan_range_end': 4096,
tierno639520f2017-04-05 19:55:36 +0200169 'db_name': global_config["db_ovim_name"],
170 'db_host': global_config["db_ovim_host"],
171 'db_user': global_config["db_ovim_user"],
172 'db_passwd': global_config["db_ovim_passwd"],
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100173 'bridge_ifaces': {},
174 'mode': 'normal',
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100175 'network_type': 'bridge',
176 #TODO: log_level_of should not be needed. To be modified in ovim
177 'log_level_of': 'DEBUG'
178 }
tierno42026a02017-02-10 15:13:40 +0100179 try:
tierno3fcfdb72017-10-24 07:48:24 +0200180 # starts ovim library
tierno46df9672017-05-26 13:12:21 +0200181 ovim = ovim_module.ovim(ovim_configuration)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100182
183 global wim_engine
184 wim_engine = wim or WimEngine(persistence)
185 wim_engine.ovim = ovim
186
tierno46df9672017-05-26 13:12:21 +0200187 ovim.start_service()
188
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100189 #delete old unneeded vim_wim_actions
tierno3fcfdb72017-10-24 07:48:24 +0200190 clean_db(mydb)
191
192 # starts vim_threads
tierno46df9672017-05-26 13:12:21 +0200193 from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
194 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
195 select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
196 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
197 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
198 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
tierno42026a02017-02-10 15:13:40 +0100199 vims = mydb.get_rows(FROM=from_, SELECT=select_)
200 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200201 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
202 'datacenter_id': vim.get('datacenter_id')}
tierno42026a02017-02-10 15:13:40 +0100203 if vim["config"]:
204 extra.update(yaml.load(vim["config"]))
205 if vim.get('dt_config'):
206 extra.update(yaml.load(vim["dt_config"]))
207 if vim["type"] not in vimconn_imported:
208 module_info=None
209 try:
210 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200211 pkg = __import__("osm_ro." + module)
212 vim_conn = getattr(pkg, module)
213 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
214 # vim_conn = imp.load_module(vim["type"], *module_info)
tierno42026a02017-02-10 15:13:40 +0100215 vimconn_imported[vim["type"]] = vim_conn
216 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200217 # if module_info and module_info[0]:
218 # file.close(module_info[0])
tiernocdee8cc2017-04-25 13:42:06 +0200219 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100220 vim["type"], module, type(e).__name__, str(e)), httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100221
tierno867ffe92017-03-27 12:50:34 +0200222 thread_id = vim['datacenter_tenant_id']
tiernob3d36742017-03-03 23:51:05 +0100223 vim_persistent_info[thread_id] = {}
tierno42026a02017-02-10 15:13:40 +0100224 try:
225 #if not tenant:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100226 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
tierno42026a02017-02-10 15:13:40 +0100227 myvim = vimconn_imported[ vim["type"] ].vimconnector(
tiernob3d36742017-03-03 23:51:05 +0100228 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
229 tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'],
230 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
231 user=vim['user'], passwd=vim['passwd'],
232 config=extra, persistent_info=vim_persistent_info[thread_id]
233 )
tierno9c22f2d2017-10-09 16:23:55 +0200234 except vimconn.vimconnException as e:
235 myvim = e
236 logger.error("Cannot launch thread for VIM {} '{}': {}".format(vim['datacenter_name'],
237 vim['datacenter_id'], e))
tierno42026a02017-02-10 15:13:40 +0100238 except Exception as e:
tierno46df9672017-05-26 13:12:21 +0200239 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100240 httperrors.Internal_Server_Error)
tierno98c11d82019-05-06 13:24:12 +0000241 thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['datacenter_id'], vim['vim_tenant_name'],
tierno46df9672017-05-26 13:12:21 +0200242 vim['vim_tenant_id'])
tiernod3750b32018-07-20 15:33:08 +0200243 new_thread = vim_thread.vim_thread(task_lock, thread_name, vim['datacenter_name'],
tierno867ffe92017-03-27 12:50:34 +0200244 vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim)
tierno42026a02017-02-10 15:13:40 +0100245 new_thread.start()
tierno42026a02017-02-10 15:13:40 +0100246 vim_threads["running"][thread_id] = new_thread
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100247
248 wim_engine.start_threads()
tierno42026a02017-02-10 15:13:40 +0100249 except db_base_Exception as e:
250 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
tierno46df9672017-05-26 13:12:21 +0200251 except ovim_module.ovimException as e:
252 message = str(e)
253 if message[:22] == "DATABASE wrong version":
254 message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
255 "at host {dbhost}".format(
256 msg=message[22:-3], dbname=global_config["db_ovim_name"],
257 dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"],
258 ver=message[-3:-1], dbhost=global_config["db_ovim_host"])
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100259 raise NfvoException(message, httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100260
tierno867ffe92017-03-27 12:50:34 +0200261
tierno42026a02017-02-10 15:13:40 +0100262def stop_service():
tiernoc5651792017-03-27 10:50:43 +0200263 global ovim, global_config
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100264 if ovim:
265 ovim.stop_service()
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100266 for thread_id, thread in vim_threads["running"].items():
tierno868220c2017-09-26 00:11:05 +0200267 thread.insert_task("exit")
tierno42026a02017-02-10 15:13:40 +0100268 vim_threads["deleting"][thread_id] = thread
tiernob3d36742017-03-03 23:51:05 +0100269 vim_threads["running"] = {}
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100270
271 if wim_engine:
272 wim_engine.stop_threads()
273
tiernoc5651792017-03-27 10:50:43 +0200274 if global_config and global_config.get("console_thread"):
275 for thread in global_config["console_thread"]:
276 thread.terminate = True
tiernob3d36742017-03-03 23:51:05 +0100277
tierno6ddeded2017-05-16 15:40:26 +0200278def get_version():
279 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"],
280 global_config["version_date"] ))
281
tierno3fcfdb72017-10-24 07:48:24 +0200282def clean_db(mydb):
283 """
284 Clean unused or old entries at database to avoid unlimited growing
285 :param mydb: database connector
286 :return: None
287 """
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100288 # get and delete unused vim_wim_actions: all elements deleted, one week before, instance not present
tierno3fcfdb72017-10-24 07:48:24 +0200289 now = t.time()-3600*24*7
290 instance_action_id = None
291 nb_deleted = 0
292 while True:
293 actions_to_delete = mydb.get_rows(
294 SELECT=("item", "item_id", "instance_action_id"),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100295 FROM="vim_wim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid "
tierno3fcfdb72017-10-24 07:48:24 +0200296 "left join instance_scenarios as i on ia.instance_id=i.uuid",
297 WHERE={"va.action": "DELETE", "va.modified_at<": now, "i.uuid": None,
298 "va.status": ("DONE", "SUPERSEDED")},
299 LIMIT=100
300 )
301 for to_delete in actions_to_delete:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100302 mydb.delete_row(FROM="vim_wim_actions", WHERE=to_delete)
tierno3fcfdb72017-10-24 07:48:24 +0200303 if instance_action_id != to_delete["instance_action_id"]:
304 instance_action_id = to_delete["instance_action_id"]
305 mydb.delete_row(FROM="instance_actions", WHERE={"uuid": instance_action_id})
306 nb_deleted += len(actions_to_delete)
307 if len(actions_to_delete) < 100:
308 break
tierno3c44e7b2019-03-04 17:32:01 +0000309 # clean locks
310 mydb.update_rows("vim_wim_actions", UPDATE={"worker": None}, WHERE={"worker<>": None})
311
tierno3fcfdb72017-10-24 07:48:24 +0200312 if nb_deleted:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100313 logger.debug("Removed {} unused vim_wim_actions".format(nb_deleted))
tierno3fcfdb72017-10-24 07:48:24 +0200314
tierno42026a02017-02-10 15:13:40 +0100315
tierno7edb6752016-03-21 17:37:52 +0100316def get_flavorlist(mydb, vnf_id, nfvo_tenant=None):
317 '''Obtain flavorList
318 return result, content:
319 <0, error_text upon error
320 nb_records, flavor_list on success
321 '''
322 WHERE_dict={}
323 WHERE_dict['vnf_id'] = vnf_id
324 if nfvo_tenant is not None:
325 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
tierno42026a02017-02-10 15:13:40 +0100326
tierno7edb6752016-03-21 17:37:52 +0100327 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
328 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
tiernof97fd272016-07-11 14:32:37 +0200329 flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict )
330 #print "get_flavor_list result:", result
331 #print "get_flavor_list content:", content
tierno7edb6752016-03-21 17:37:52 +0100332 flavorList=[]
tiernof97fd272016-07-11 14:32:37 +0200333 for flavor in flavors:
tierno7edb6752016-03-21 17:37:52 +0100334 flavorList.append(flavor['flavor_id'])
tiernof97fd272016-07-11 14:32:37 +0200335 return flavorList
tierno7edb6752016-03-21 17:37:52 +0100336
tiernob3d36742017-03-03 23:51:05 +0100337
tierno7edb6752016-03-21 17:37:52 +0100338def get_imagelist(mydb, vnf_id, nfvo_tenant=None):
tierno16e3dd42018-04-24 12:52:40 +0200339 """
340 Get used images of all vms belonging to this VNFD
341 :param mydb: database conector
342 :param vnf_id: vnfd uuid
343 :param nfvo_tenant: tenant, not used
344 :return: The list of image uuid used
345 """
346 image_list = []
347 vms = mydb.get_rows(SELECT=('image_id','image_list'), FROM='vms', WHERE={'vnf_id': vnf_id})
348 for vm in vms:
tierno89aada42018-12-19 16:00:25 +0000349 if vm["image_id"] and vm["image_id"] not in image_list:
tierno16e3dd42018-04-24 12:52:40 +0200350 image_list.append(vm["image_id"])
351 if vm["image_list"]:
352 vm_image_list = yaml.load(vm["image_list"])
353 for image_dict in vm_image_list:
354 if image_dict["image_id"] not in image_list:
355 image_list.append(image_dict["image_id"])
356 return image_list
tierno7edb6752016-03-21 17:37:52 +0100357
tiernob3d36742017-03-03 23:51:05 +0100358
tiernoa2793912016-10-04 08:15:08 +0000359def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None,
tiernocbb52052018-05-31 18:57:30 +0200360 vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None, ignore_errors=False):
tierno7edb6752016-03-21 17:37:52 +0100361 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
tierno42026a02017-02-10 15:13:40 +0100362 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
tierno7edb6752016-03-21 17:37:52 +0100363 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
tiernobe41e222016-09-02 15:16:13 +0200364 raise exception upon error
tierno7edb6752016-03-21 17:37:52 +0100365 '''
366 WHERE_dict={}
367 if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
368 if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id
tiernoa2793912016-10-04 08:15:08 +0000369 if datacenter_tenant_id is not None: WHERE_dict['datacenter_tenant_id'] = datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +0100370 if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name
371 if vim_tenant is not None: WHERE_dict['dt.vim_tenant_id'] = vim_tenant
tiernoa2793912016-10-04 08:15:08 +0000372 if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name
373 if nfvo_tenant or vim_tenant or vim_tenant_name or datacenter_tenant_id:
tierno7edb6752016-03-21 17:37:52 +0100374 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 +0000375 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 +0100376 '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 +0000377 'user','passwd', 'dt.config as dt_config')
tierno7edb6752016-03-21 17:37:52 +0100378 else:
379 from_ = 'datacenters as d'
380 select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
tiernof97fd272016-07-11 14:32:37 +0200381 try:
382 vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
383 vim_dict={}
384 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200385 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
tierno16e3dd42018-04-24 12:52:40 +0200386 'datacenter_id': vim.get('datacenter_id'),
tiernob6434212018-04-26 16:27:47 +0200387 '_vim_type_internal': vim.get('type')}
tierno8008c3a2016-10-13 15:34:28 +0000388 if vim["config"]:
tiernof97fd272016-07-11 14:32:37 +0200389 extra.update(yaml.load(vim["config"]))
tierno8008c3a2016-10-13 15:34:28 +0000390 if vim.get('dt_config'):
391 extra.update(yaml.load(vim["dt_config"]))
tiernof97fd272016-07-11 14:32:37 +0200392 if vim["type"] not in vimconn_imported:
393 module_info=None
394 try:
395 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200396 pkg = __import__("osm_ro." + module)
397 vim_conn = getattr(pkg, module)
398 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
399 # vim_conn = imp.load_module(vim["type"], *module_info)
tiernof97fd272016-07-11 14:32:37 +0200400 vimconn_imported[vim["type"]] = vim_conn
401 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200402 # if module_info and module_info[0]:
403 # file.close(module_info[0])
tiernocbb52052018-05-31 18:57:30 +0200404 if ignore_errors:
405 logger.error("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
406 vim["type"], module, type(e).__name__, str(e)))
407 continue
tiernof97fd272016-07-11 14:32:37 +0200408 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100409 vim["type"], module, type(e).__name__, str(e)), httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100410
tierno7edb6752016-03-21 17:37:52 +0100411 try:
tierno867ffe92017-03-27 12:50:34 +0200412 if 'datacenter_tenant_id' in vim:
413 thread_id = vim["datacenter_tenant_id"]
tiernob3d36742017-03-03 23:51:05 +0100414 if thread_id not in vim_persistent_info:
415 vim_persistent_info[thread_id] = {}
416 persistent_info = vim_persistent_info[thread_id]
417 else:
418 persistent_info = {}
tiernof97fd272016-07-11 14:32:37 +0200419 #if not tenant:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100420 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
tiernof97fd272016-07-11 14:32:37 +0200421 vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector(
422 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
tiernob3d36742017-03-03 23:51:05 +0100423 tenant_id=vim.get('vim_tenant_id',vim_tenant),
424 tenant_name=vim.get('vim_tenant_name',vim_tenant_name),
tierno42026a02017-02-10 15:13:40 +0100425 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
tierno3ae39742016-09-07 12:17:51 +0200426 user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd),
tiernob3d36742017-03-03 23:51:05 +0100427 config=extra, persistent_info=persistent_info
tiernof97fd272016-07-11 14:32:37 +0200428 )
429 except Exception as e:
tiernocbb52052018-05-31 18:57:30 +0200430 if ignore_errors:
431 logger.error("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)))
432 continue
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100433 http_code = httperrors.Internal_Server_Error
tiernoa3572692018-05-14 13:09:33 +0200434 if isinstance(e, vimconn.vimconnException):
435 http_code = e.http_code
436 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), http_code)
tiernof97fd272016-07-11 14:32:37 +0200437 return vim_dict
438 except db_base_Exception as e:
439 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
tierno42026a02017-02-10 15:13:40 +0100440
tiernob3d36742017-03-03 23:51:05 +0100441
tierno7edb6752016-03-21 17:37:52 +0100442def rollback(mydb, vims, rollback_list):
443 undeleted_items=[]
tierno42026a02017-02-10 15:13:40 +0100444 #delete things by reverse order
tierno7edb6752016-03-21 17:37:52 +0100445 for i in range(len(rollback_list)-1, -1, -1):
446 item = rollback_list[i]
447 if item["where"]=="vim":
448 if item["vim_id"] not in vims:
449 continue
tierno56d73d22017-08-02 13:53:02 +0200450 if is_task_id(item["uuid"]):
451 continue
452 vim = vims[item["vim_id"]]
tiernoae4a8d12016-07-08 12:30:39 +0200453 try:
454 if item["what"]=="image":
455 vim.delete_image(item["uuid"])
tierno868220c2017-09-26 00:11:05 +0200456 mydb.delete_row(FROM="datacenters_images", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200457 elif item["what"]=="flavor":
458 vim.delete_flavor(item["uuid"])
tiernoad6bdd42018-01-10 10:43:46 +0100459 mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200460 elif item["what"]=="network":
461 vim.delete_network(item["uuid"])
462 elif item["what"]=="vm":
463 vim.delete_vminstance(item["uuid"])
464 except vimconn.vimconnException as e:
465 logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e))
466 undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"]))
tiernof97fd272016-07-11 14:32:37 +0200467 except db_base_Exception as e:
468 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 +0100469
tierno7edb6752016-03-21 17:37:52 +0100470 else: # where==mano
tiernof97fd272016-07-11 14:32:37 +0200471 try:
472 if item["what"]=="image":
473 mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]})
474 elif item["what"]=="flavor":
475 mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]})
476 except db_base_Exception as e:
477 logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e))
478 undeleted_items.append("{} '{}'".format(item['what'], item["uuid"]))
tierno42026a02017-02-10 15:13:40 +0100479 if len(undeleted_items)==0:
tierno7edb6752016-03-21 17:37:52 +0100480 return True," Rollback successful."
481 else:
482 return False," Rollback fails to delete: " + str(undeleted_items)
tierno42026a02017-02-10 15:13:40 +0100483
tiernob3d36742017-03-03 23:51:05 +0100484
tiernoafed5f12017-01-26 17:57:43 +0100485def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1):
tierno7edb6752016-03-21 17:37:52 +0100486 global global_config
tierno42026a02017-02-10 15:13:40 +0100487 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
tierno7edb6752016-03-21 17:37:52 +0100488 vnfc_interfaces={}
489 for vnfc in vnf_descriptor["vnf"]["VNFC"]:
tiernoafed5f12017-01-26 17:57:43 +0100490 name_dict = {}
tierno7edb6752016-03-21 17:37:52 +0100491 #dataplane interfaces
492 for numa in vnfc.get("numas",() ):
493 for interface in numa.get("interfaces",()):
tiernoafed5f12017-01-26 17:57:43 +0100494 if interface["name"] in name_dict:
495 raise NfvoException(
496 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
497 vnfc["name"], interface["name"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100498 httperrors.Bad_Request)
tiernoafed5f12017-01-26 17:57:43 +0100499 name_dict[ interface["name"] ] = "underlay"
tierno7edb6752016-03-21 17:37:52 +0100500 #bridge interfaces
501 for interface in vnfc.get("bridge-ifaces",() ):
tiernoafed5f12017-01-26 17:57:43 +0100502 if interface["name"] in name_dict:
503 raise NfvoException(
504 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
505 vnfc["name"], interface["name"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100506 httperrors.Bad_Request)
tiernoafed5f12017-01-26 17:57:43 +0100507 name_dict[ interface["name"] ] = "overlay"
508 vnfc_interfaces[ vnfc["name"] ] = name_dict
tierno36c0b172017-01-12 18:32:28 +0100509 # check bood-data info
tierno40e1bce2017-08-09 09:12:04 +0200510 # if "boot-data" in vnfc:
511 # # check that user-data is incompatible with users and config-files
512 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
513 # raise NfvoException(
514 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100515 # httperrors.Bad_Request)
tierno36c0b172017-01-12 18:32:28 +0100516
tierno7edb6752016-03-21 17:37:52 +0100517 #check if the info in external_connections matches with the one in the vnfcs
518 name_list=[]
519 for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
520 if external_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100521 raise NfvoException(
522 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
523 external_connection["name"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100524 httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100525 name_list.append(external_connection["name"])
526 if external_connection["VNFC"] not in vnfc_interfaces:
tiernoafed5f12017-01-26 17:57:43 +0100527 raise NfvoException(
528 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
529 external_connection["name"], external_connection["VNFC"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100530 httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100531
tierno7edb6752016-03-21 17:37:52 +0100532 if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
tiernoafed5f12017-01-26 17:57:43 +0100533 raise NfvoException(
534 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
535 external_connection["name"],
536 external_connection["local_iface_name"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100537 httperrors.Bad_Request )
tierno42026a02017-02-10 15:13:40 +0100538
tierno7edb6752016-03-21 17:37:52 +0100539 #check if the info in internal_connections matches with the one in the vnfcs
540 name_list=[]
541 for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
542 if internal_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100543 raise NfvoException(
544 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
545 internal_connection["name"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100546 httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100547 name_list.append(internal_connection["name"])
548 #We should check that internal-connections of type "ptp" have only 2 elements
tiernoafed5f12017-01-26 17:57:43 +0100549
550 if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"):
551 raise NfvoException(
552 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
553 internal_connection["name"],
554 'ptp' if vnf_descriptor_version==1 else 'e-line',
555 'data' if vnf_descriptor_version==1 else "e-lan"),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100556 httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100557 for port in internal_connection["elements"]:
tiernoafed5f12017-01-26 17:57:43 +0100558 vnf = port["VNFC"]
559 iface = port["local_iface_name"]
560 if vnf not in vnfc_interfaces:
561 raise NfvoException(
562 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
563 internal_connection["name"], vnf),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100564 httperrors.Bad_Request)
tiernoafed5f12017-01-26 17:57:43 +0100565 if iface not in vnfc_interfaces[ vnf ]:
566 raise NfvoException(
567 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
568 internal_connection["name"], iface),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100569 httperrors.Bad_Request)
570 return -httperrors.Bad_Request,
tiernoafed5f12017-01-26 17:57:43 +0100571 if vnf_descriptor_version==1 and "type" not in internal_connection:
572 if vnfc_interfaces[vnf][iface] == "overlay":
573 internal_connection["type"] = "bridge"
574 else:
575 internal_connection["type"] = "data"
576 if vnf_descriptor_version==2 and "implementation" not in internal_connection:
577 if vnfc_interfaces[vnf][iface] == "overlay":
578 internal_connection["implementation"] = "overlay"
579 else:
580 internal_connection["implementation"] = "underlay"
581 if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \
582 internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay":
583 raise NfvoException(
584 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
585 internal_connection["name"],
586 iface, 'bridge' if vnf_descriptor_version==1 else 'overlay',
587 'data' if vnf_descriptor_version==1 else 'underlay'),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100588 httperrors.Bad_Request)
tiernoafed5f12017-01-26 17:57:43 +0100589 if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \
590 vnfc_interfaces[vnf][iface] == "underlay":
591 raise NfvoException(
592 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
593 internal_connection["name"], iface,
594 'data' if vnf_descriptor_version==1 else 'underlay',
595 'bridge' if vnf_descriptor_version==1 else 'overlay'),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100596 httperrors.Bad_Request)
tiernoafed5f12017-01-26 17:57:43 +0100597
tierno7edb6752016-03-21 17:37:52 +0100598
tierno56d73d22017-08-02 13:53:02 +0200599def 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 +0100600 #look if image exist
601 if only_create_at_vim:
602 image_mano_id = image_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000603 if return_on_error == None:
604 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100605 else:
garciadeblas14480452017-01-10 13:08:07 +0100606 if image_dict['location']:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200607 images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
608 else:
609 images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']})
tiernof97fd272016-07-11 14:32:37 +0200610 if len(images)>=1:
611 image_mano_id = images[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100612 else:
garciadeblas14480452017-01-10 13:08:07 +0100613 #create image in MANO DB
tierno7edb6752016-03-21 17:37:52 +0100614 temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None),
garciadeblasb69fa9f2016-09-28 12:04:10 +0200615 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None),
616 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum']
tierno7edb6752016-03-21 17:37:52 +0100617 }
garciadeblas14480452017-01-10 13:08:07 +0100618 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
tiernof97fd272016-07-11 14:32:37 +0200619 image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True)
620 rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100621 #create image at every vim
622 for vim_id,vim in vims.iteritems():
tierno868220c2017-09-26 00:11:05 +0200623 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
tierno7edb6752016-03-21 17:37:52 +0100624 image_created="false"
625 #look at database
tierno868220c2017-09-26 00:11:05 +0200626 image_db = mydb.get_rows(FROM="datacenters_images",
627 WHERE={'datacenter_vim_id': datacenter_vim_id, 'image_id': image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100628 #look at VIM if this image exist
tiernoae4a8d12016-07-08 12:30:39 +0200629 try:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200630 if image_dict['location'] is not None:
631 image_vim_id = vim.get_image_id_from_path(image_dict['location'])
632 else:
garciadeblas30833382017-01-09 09:46:31 +0100633 filter_dict = {}
634 filter_dict['name'] = image_dict['universal_name']
635 if image_dict.get('checksum') != None:
636 filter_dict['checksum'] = image_dict['checksum']
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000637 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200638 vim_images = vim.get_image_list(filter_dict)
garciadeblas14480452017-01-10 13:08:07 +0100639 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200640 if len(vim_images) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100641 raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), httperrors.Conflict)
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000642 elif len(vim_images) == 0:
garciadeblas3fa2c052017-01-05 12:00:08 +0100643 raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict)))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200644 else:
garciadeblas14480452017-01-10 13:08:07 +0100645 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
646 image_vim_id = vim_images[0]['id']
garciadeblasb69fa9f2016-09-28 12:04:10 +0200647
tiernoae4a8d12016-07-08 12:30:39 +0200648 except vimconn.vimconnNotFoundException as e:
garciadeblas14480452017-01-10 13:08:07 +0100649 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
tierno42026a02017-02-10 15:13:40 +0100650 try:
garciadeblas14480452017-01-10 13:08:07 +0100651 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
652 if image_dict['location']:
653 image_vim_id = vim.new_image(image_dict)
654 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
655 image_created="true"
656 else:
garciadeblasb6153a22017-02-06 15:38:33 +0100657 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
658 raise vimconn.vimconnException(str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200659 except vimconn.vimconnException as e:
660 if return_on_error:
garciadeblas14480452017-01-10 13:08:07 +0100661 logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200662 raise
tierno5e91eb82016-10-04 09:39:07 +0000663 image_vim_id = None
garciadeblas14480452017-01-10 13:08:07 +0100664 logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200665 continue
666 except vimconn.vimconnException as e:
tierno5e91eb82016-10-04 09:39:07 +0000667 if return_on_error:
668 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
669 raise
garciadeblasb69fa9f2016-09-28 12:04:10 +0200670 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
tierno5e91eb82016-10-04 09:39:07 +0000671 image_vim_id = None
garciadeblas30833382017-01-09 09:46:31 +0100672 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200673 #if we reach here, the image has been created or existed
tiernof97fd272016-07-11 14:32:37 +0200674 if len(image_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100675 #add new vim_id at datacenters_images
tierno868220c2017-09-26 00:11:05 +0200676 mydb.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id,
677 'image_id':image_mano_id,
678 'vim_id': image_vim_id,
679 'created':image_created})
tierno7edb6752016-03-21 17:37:52 +0100680 elif image_db[0]["vim_id"]!=image_vim_id:
681 #modify existing vim_id at datacenters_images
tierno868220c2017-09-26 00:11:05 +0200682 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 +0100683
tiernof97fd272016-07-11 14:32:37 +0200684 return image_vim_id if only_create_at_vim else image_mano_id
tierno7edb6752016-03-21 17:37:52 +0100685
tiernob3d36742017-03-03 23:51:05 +0100686
tierno5e91eb82016-10-04 09:39:07 +0000687def 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 +0100688 temp_flavor_dict= {'disk':flavor_dict.get('disk',0),
tierno7edb6752016-03-21 17:37:52 +0100689 'ram':flavor_dict.get('ram'),
690 'vcpus':flavor_dict.get('vcpus'),
691 }
692 if 'extended' in flavor_dict and flavor_dict['extended']==None:
693 del flavor_dict['extended']
694 if 'extended' in flavor_dict:
695 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
696
697 #look if flavor exist
698 if only_create_at_vim:
699 flavor_mano_id = flavor_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000700 if return_on_error == None:
701 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100702 else:
tiernof97fd272016-07-11 14:32:37 +0200703 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
704 if len(flavors)>=1:
705 flavor_mano_id = flavors[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100706 else:
707 #create flavor
708 #create one by one the images of aditional disks
709 dev_image_list=[] #list of images
710 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
711 dev_nb=0
712 for device in flavor_dict['extended'].get('devices',[]):
garciadeblas41f18be2016-10-04 09:09:58 +0200713 if "image" not in device and "image name" not in device:
tierno7edb6752016-03-21 17:37:52 +0100714 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200715 image_dict={}
716 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
717 image_dict['universal_name']=device.get('image name')
718 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
719 image_dict['location']=device.get('image')
garciadeblas14480452017-01-10 13:08:07 +0100720 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200721 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100722 image_metadata_dict = device.get('image metadata', None)
723 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100724 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100725 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
726 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200727 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
728 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
tierno7edb6752016-03-21 17:37:52 +0100729 dev_image_list.append(image_id)
tierno42026a02017-02-10 15:13:40 +0100730 dev_nb += 1
tierno7edb6752016-03-21 17:37:52 +0100731 temp_flavor_dict['name'] = flavor_dict['name']
732 temp_flavor_dict['description'] = flavor_dict.get('description',None)
tiernof97fd272016-07-11 14:32:37 +0200733 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
734 flavor_mano_id= content
735 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100736 #create flavor at every vim
737 if 'uuid' in flavor_dict:
738 del flavor_dict['uuid']
739 flavor_vim_id=None
740 for vim_id,vim in vims.items():
tierno868220c2017-09-26 00:11:05 +0200741 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
tierno7edb6752016-03-21 17:37:52 +0100742 flavor_created="false"
743 #look at database
tierno868220c2017-09-26 00:11:05 +0200744 flavor_db = mydb.get_rows(FROM="datacenters_flavors",
745 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100746 #look at VIM if this flavor exist SKIPPED
747 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
748 #if res_vim < 0:
749 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
750 # continue
751 #elif res_vim==0:
tierno42026a02017-02-10 15:13:40 +0100752
tiernof1ba57e2017-09-07 12:23:19 +0200753 # Create the flavor in VIM
754 # Translate images at devices from MANO id to VIM id
montesmoreno0c8def02016-12-22 12:16:23 +0000755 disk_list = []
tierno7edb6752016-03-21 17:37:52 +0100756 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
tiernof1ba57e2017-09-07 12:23:19 +0200757 # make a copy of original devices
tierno7edb6752016-03-21 17:37:52 +0100758 devices_original=[]
montesmoreno0c8def02016-12-22 12:16:23 +0000759
tierno7edb6752016-03-21 17:37:52 +0100760 for device in flavor_dict["extended"].get("devices",[]):
761 dev={}
762 dev.update(device)
763 devices_original.append(dev)
764 if 'image' in device:
765 del device['image']
766 if 'image metadata' in device:
767 del device['image metadata']
tiernof1ba57e2017-09-07 12:23:19 +0200768 if 'image checksum' in device:
769 del device['image checksum']
770 dev_nb = 0
tierno7edb6752016-03-21 17:37:52 +0100771 for index in range(0,len(devices_original)) :
772 device=devices_original[index]
montesmoreno0c8def02016-12-22 12:16:23 +0000773 if "image" not in device and "image name" not in device:
tiernoecc68392018-09-06 13:47:11 +0200774 # if 'size' in device:
775 disk_list.append({'size': device.get('size', default_volume_size), 'name': device.get('name')})
tierno7edb6752016-03-21 17:37:52 +0100776 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200777 image_dict={}
778 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
779 image_dict['universal_name']=device.get('image name')
780 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
781 image_dict['location']=device.get('image')
tiernof1ba57e2017-09-07 12:23:19 +0200782 # image_dict['new_location']=device.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200783 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100784 image_metadata_dict = device.get('image metadata', None)
785 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100786 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100787 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
788 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200789 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 +0100790 image_dict["uuid"]=image_mano_id
tiernof97fd272016-07-11 14:32:37 +0200791 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 +0000792
793 #save disk information (image must be based on and size
794 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
795
tierno7edb6752016-03-21 17:37:52 +0100796 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
797 dev_nb += 1
tiernof97fd272016-07-11 14:32:37 +0200798 if len(flavor_db)>0:
tierno7edb6752016-03-21 17:37:52 +0100799 #check that this vim_id exist in VIM, if not create
800 flavor_vim_id=flavor_db[0]["vim_id"]
tiernoae4a8d12016-07-08 12:30:39 +0200801 try:
802 vim.get_flavor(flavor_vim_id)
803 continue #flavor exist
804 except vimconn.vimconnException:
805 pass
tierno7edb6752016-03-21 17:37:52 +0100806 #create flavor at vim
tiernoae4a8d12016-07-08 12:30:39 +0200807 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
808 try:
tiernocf157a82017-01-30 14:07:06 +0100809 flavor_vim_id = None
810 flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict)
tiernob7aa1bb2019-07-24 15:47:16 +0000811 flavor_created="false"
tiernocf157a82017-01-30 14:07:06 +0100812 except vimconn.vimconnException as e:
813 pass
814 try:
815 if not flavor_vim_id:
816 flavor_vim_id = vim.new_flavor(flavor_dict)
817 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
818 flavor_created="true"
tiernoae4a8d12016-07-08 12:30:39 +0200819 except vimconn.vimconnException as e:
820 if return_on_error:
821 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200822 raise
tiernoae4a8d12016-07-08 12:30:39 +0200823 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tierno5e91eb82016-10-04 09:39:07 +0000824 flavor_vim_id = None
tiernoae4a8d12016-07-08 12:30:39 +0200825 continue
tierno7edb6752016-03-21 17:37:52 +0100826 #if reach here the flavor has been create or exist
tiernof97fd272016-07-11 14:32:37 +0200827 if len(flavor_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100828 #add new vim_id at datacenters_flavors
montesmoreno0c8def02016-12-22 12:16:23 +0000829 extended_devices_yaml = None
830 if len(disk_list) > 0:
831 extended_devices = dict()
832 extended_devices['disks'] = disk_list
833 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
834 mydb.new_row('datacenters_flavors',
tierno868220c2017-09-26 00:11:05 +0200835 {'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id, 'vim_id': flavor_vim_id,
836 'created': flavor_created, 'extended': extended_devices_yaml})
tierno7edb6752016-03-21 17:37:52 +0100837 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
838 #modify existing vim_id at datacenters_flavors
tierno868220c2017-09-26 00:11:05 +0200839 mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id},
840 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
tierno42026a02017-02-10 15:13:40 +0100841
tiernof97fd272016-07-11 14:32:37 +0200842 return flavor_vim_id if only_create_at_vim else flavor_mano_id
tierno7edb6752016-03-21 17:37:52 +0100843
tiernob3d36742017-03-03 23:51:05 +0100844
tiernof1ba57e2017-09-07 12:23:19 +0200845def get_str(obj, field, length):
846 """
847 Obtain the str value,
848 :param obj:
849 :param length:
850 :return:
851 """
852 value = obj.get(field)
853 if value is not None:
854 value = str(value)[:length]
855 return value
856
857def _lookfor_or_create_image(db_image, mydb, descriptor):
858 """
859 fill image content at db_image dictionary. Check if the image with this image and checksum exist
860 :param db_image: dictionary to insert data
861 :param mydb: database connector
862 :param descriptor: yang descriptor
863 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
864 """
865
866 db_image["name"] = get_str(descriptor, "image", 255)
867 db_image["checksum"] = get_str(descriptor, "image-checksum", 32)
868 if not db_image["checksum"]: # Ensure that if empty string, None is stored
869 db_image["checksum"] = None
870 if db_image["name"].startswith("/"):
871 db_image["location"] = db_image["name"]
872 existing_images = mydb.get_rows(FROM="images", WHERE={'location': db_image["location"]})
873 else:
874 db_image["universal_name"] = db_image["name"]
875 existing_images = mydb.get_rows(FROM="images", WHERE={'universal_name': db_image['universal_name'],
876 'checksum': db_image['checksum']})
877 if existing_images:
878 return existing_images[0]["uuid"]
879 else:
880 image_uuid = str(uuid4())
881 db_image["uuid"] = image_uuid
882 return None
883
anwarsae5f52c2019-04-22 10:35:27 +0530884def get_resource_allocation_params(quota_descriptor):
885 """
886 read the quota_descriptor from vnfd and fetch the resource allocation properties from the descriptor object
887 :param quota_descriptor: cpu/mem/vif/disk-io quota descriptor
888 :return: quota params for limit, reserve, shares from the descriptor object
889 """
890 quota = {}
891 if quota_descriptor.get("limit"):
892 quota["limit"] = int(quota_descriptor["limit"])
893 if quota_descriptor.get("reserve"):
894 quota["reserve"] = int(quota_descriptor["reserve"])
895 if quota_descriptor.get("shares"):
896 quota["shares"] = int(quota_descriptor["shares"])
897 return quota
898
tiernof1ba57e2017-09-07 12:23:19 +0200899def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
900 """
901 Parses an OSM IM vnfd_catalog and insert at DB
902 :param mydb:
903 :param tenant_id:
904 :param vnf_descriptor:
905 :return: The list of cretated vnf ids
906 """
907 try:
908 myvnfd = vnfd_catalog.vnfd()
tiernoa9550202017-09-22 13:31:35 +0200909 try:
tiernof6bbe222019-04-09 14:19:40 +0000910 pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd, path_helper=True,
911 skip_unknown=True)
tiernoa9550202017-09-22 13:31:35 +0200912 except Exception as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100913 raise NfvoException("Error. Invalid VNF descriptor format " + str(e), httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +0200914 db_vnfs = []
915 db_nets = []
916 db_vms = []
917 db_vms_index = 0
918 db_interfaces = []
919 db_images = []
920 db_flavors = []
tierno41a69812018-02-16 14:34:33 +0100921 db_ip_profiles_index = 0
922 db_ip_profiles = []
tiernof1ba57e2017-09-07 12:23:19 +0200923 uuid_list = []
924 vnfd_uuid_list = []
tiernoe18ba432017-10-12 10:22:45 +0200925 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd:vnfd-catalog")
926 if not vnfd_catalog_descriptor:
927 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd-catalog")
928 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd")
929 if not vnfd_descriptor_list:
930 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd:vnfd")
tiernob2880eb2017-10-04 15:04:53 +0200931 for vnfd_yang in myvnfd.vnfd_catalog.vnfd.itervalues():
932 vnfd = vnfd_yang.get()
tiernof1ba57e2017-09-07 12:23:19 +0200933
934 # table vnf
935 vnf_uuid = str(uuid4())
936 uuid_list.append(vnf_uuid)
937 vnfd_uuid_list.append(vnf_uuid)
tierno66eba6e2017-11-10 17:09:18 +0100938 vnfd_id = get_str(vnfd, "id", 255)
tiernof1ba57e2017-09-07 12:23:19 +0200939 db_vnf = {
940 "uuid": vnf_uuid,
tierno66eba6e2017-11-10 17:09:18 +0100941 "osm_id": vnfd_id,
tiernof1ba57e2017-09-07 12:23:19 +0200942 "name": get_str(vnfd, "name", 255),
943 "description": get_str(vnfd, "description", 255),
944 "tenant_id": tenant_id,
945 "vendor": get_str(vnfd, "vendor", 255),
946 "short_name": get_str(vnfd, "short-name", 255),
947 "descriptor": str(vnf_descriptor)[:60000]
948 }
949
tiernoe18ba432017-10-12 10:22:45 +0200950 for vnfd_descriptor in vnfd_descriptor_list:
951 if vnfd_descriptor["id"] == str(vnfd["id"]):
952 break
953
tierno41a69812018-02-16 14:34:33 +0100954 # table ip_profiles (ip-profiles)
955 ip_profile_name2db_table_index = {}
956 for ip_profile in vnfd.get("ip-profiles").itervalues():
957 db_ip_profile = {
958 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
959 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
960 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
961 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
962 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
963 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
964 }
965 dns_list = []
966 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
967 dns_list.append(str(dns.get("address")))
968 db_ip_profile["dns_address"] = ";".join(dns_list)
969 if ip_profile["ip-profile-params"].get('security-group'):
970 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
971 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
972 db_ip_profiles_index += 1
973 db_ip_profiles.append(db_ip_profile)
974
tiernof1ba57e2017-09-07 12:23:19 +0200975 # table nets (internal-vld)
976 net_id2uuid = {} # for mapping interface with network
977 for vld in vnfd.get("internal-vld").itervalues():
978 net_uuid = str(uuid4())
979 uuid_list.append(net_uuid)
980 db_net = {
981 "name": get_str(vld, "name", 255),
982 "vnf_id": vnf_uuid,
983 "uuid": net_uuid,
984 "description": get_str(vld, "description", 255),
tierno1df468d2018-07-06 14:25:16 +0200985 "osm_id": get_str(vld, "id", 255),
tiernof1ba57e2017-09-07 12:23:19 +0200986 "type": "bridge", # TODO adjust depending on connection point type
987 }
988 net_id2uuid[vld.get("id")] = net_uuid
989 db_nets.append(db_net)
tierno41a69812018-02-16 14:34:33 +0100990 # ip-profile, link db_ip_profile with db_sce_net
991 if vld.get("ip-profile-ref"):
992 ip_profile_name = vld.get("ip-profile-ref")
993 if ip_profile_name not in ip_profile_name2db_table_index:
994 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':"
995 "'{}'. Reference to a non-existing 'ip_profiles'".format(
996 str(vnfd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100997 httperrors.Bad_Request)
tierno41a69812018-02-16 14:34:33 +0100998 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["net_id"] = net_uuid
999 else: #check no ip-address has been defined
tierno45140f52018-03-26 12:11:46 +02001000 for icp in vld.get("internal-connection-point").itervalues():
tierno41a69812018-02-16 14:34:33 +01001001 if icp.get("ip-address"):
1002 raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' "
1003 "contains an ip-address but no ip-profile has been defined at VLD".format(
1004 str(vnfd["id"]), str(vld["id"]), str(icp["id"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001005 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001006
tiernocf596692017-11-20 15:47:51 +01001007 # connection points vaiable declaration
1008 cp_name2iface_uuid = {}
tierno7e510052019-09-10 16:16:13 +00001009 cp_name2vdu_id = {}
tiernocf596692017-11-20 15:47:51 +01001010 cp_name2vm_uuid = {}
1011 cp_name2db_interface = {}
tiernob6990792018-11-13 10:37:42 +01001012 vdu_id2cp_name = {} # stored only when one external connection point is presented at this VDU
tiernocf596692017-11-20 15:47:51 +01001013
tiernof1ba57e2017-09-07 12:23:19 +02001014 # table vms (vdus)
1015 vdu_id2uuid = {}
1016 vdu_id2db_table_index = {}
tierno7e510052019-09-10 16:16:13 +00001017 mgmt_access = {}
tiernof1ba57e2017-09-07 12:23:19 +02001018 for vdu in vnfd.get("vdu").itervalues():
tierno41a69812018-02-16 14:34:33 +01001019
1020 for vdu_descriptor in vnfd_descriptor["vdu"]:
1021 if vdu_descriptor["id"] == str(vdu["id"]):
1022 break
tiernof1ba57e2017-09-07 12:23:19 +02001023 vm_uuid = str(uuid4())
1024 uuid_list.append(vm_uuid)
tierno66eba6e2017-11-10 17:09:18 +01001025 vdu_id = get_str(vdu, "id", 255)
tiernof1ba57e2017-09-07 12:23:19 +02001026 db_vm = {
1027 "uuid": vm_uuid,
tierno66eba6e2017-11-10 17:09:18 +01001028 "osm_id": vdu_id,
tiernof1ba57e2017-09-07 12:23:19 +02001029 "name": get_str(vdu, "name", 255),
1030 "description": get_str(vdu, "description", 255),
tiernob6990792018-11-13 10:37:42 +01001031 "pdu_type": get_str(vdu, "pdu-type", 255),
tiernof1ba57e2017-09-07 12:23:19 +02001032 "vnf_id": vnf_uuid,
1033 }
1034 vdu_id2uuid[db_vm["osm_id"]] = vm_uuid
1035 vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index
1036 if vdu.get("count"):
1037 db_vm["count"] = int(vdu["count"])
1038
1039 # table image
1040 image_present = False
1041 if vdu.get("image"):
1042 image_present = True
1043 db_image = {}
1044 image_uuid = _lookfor_or_create_image(db_image, mydb, vdu)
1045 if not image_uuid:
1046 image_uuid = db_image["uuid"]
1047 db_images.append(db_image)
1048 db_vm["image_id"] = image_uuid
tierno16e3dd42018-04-24 12:52:40 +02001049 if vdu.get("alternative-images"):
1050 vm_alternative_images = []
1051 for alt_image in vdu.get("alternative-images").itervalues():
1052 db_image = {}
1053 image_uuid = _lookfor_or_create_image(db_image, mydb, alt_image)
1054 if not image_uuid:
1055 image_uuid = db_image["uuid"]
1056 db_images.append(db_image)
1057 vm_alternative_images.append({
1058 "image_id": image_uuid,
1059 "vim_type": str(alt_image["vim-type"]),
1060 # "universal_name": str(alt_image["image"]),
1061 # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None
1062 })
1063
1064 db_vm["image_list"] = yaml.safe_dump(vm_alternative_images, default_flow_style=True, width=256)
tiernof1ba57e2017-09-07 12:23:19 +02001065
1066 # volumes
1067 devices = []
1068 if vdu.get("volumes"):
tierno1df468d2018-07-06 14:25:16 +02001069 for volume_key in vdu["volumes"]:
tiernof1ba57e2017-09-07 12:23:19 +02001070 volume = vdu["volumes"][volume_key]
1071 if not image_present:
1072 # Convert the first volume to vnfc.image
1073 image_present = True
1074 db_image = {}
1075 image_uuid = _lookfor_or_create_image(db_image, mydb, volume)
1076 if not image_uuid:
1077 image_uuid = db_image["uuid"]
1078 db_images.append(db_image)
1079 db_vm["image_id"] = image_uuid
1080 else:
1081 # Add Openmano devices
tierno1df468d2018-07-06 14:25:16 +02001082 device = {"name": str(volume.get("name"))}
tiernof1ba57e2017-09-07 12:23:19 +02001083 device["type"] = str(volume.get("device-type"))
1084 if volume.get("size"):
1085 device["size"] = int(volume["size"])
1086 if volume.get("image"):
1087 device["image name"] = str(volume["image"])
1088 if volume.get("image-checksum"):
1089 device["image checksum"] = str(volume["image-checksum"])
tierno1df468d2018-07-06 14:25:16 +02001090
tiernof1ba57e2017-09-07 12:23:19 +02001091 devices.append(device)
1092
tierno89aada42018-12-19 16:00:25 +00001093 if not db_vm.get("image_id"):
1094 if not db_vm["pdu_type"]:
1095 raise NfvoException("Not defined image for VDU")
1096 # create a fake image
1097
tierno66eba6e2017-11-10 17:09:18 +01001098 # cloud-init
1099 boot_data = {}
1100 if vdu.get("cloud-init"):
1101 boot_data["user-data"] = str(vdu["cloud-init"])
1102 elif vdu.get("cloud-init-file"):
1103 # TODO Where this file content is present???
1104 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
1105 boot_data["user-data"] = str(vdu["cloud-init-file"])
1106
1107 if vdu.get("supplemental-boot-data"):
1108 if vdu["supplemental-boot-data"].get('boot-data-drive'):
1109 boot_data['boot-data-drive'] = True
1110 if vdu["supplemental-boot-data"].get('config-file'):
1111 om_cfgfile_list = list()
1112 for custom_config_file in vdu["supplemental-boot-data"]['config-file'].itervalues():
1113 # TODO Where this file content is present???
1114 cfg_source = str(custom_config_file["source"])
1115 om_cfgfile_list.append({"dest": custom_config_file["dest"],
1116 "content": cfg_source})
1117 boot_data['config-files'] = om_cfgfile_list
1118 if boot_data:
1119 db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256)
1120
1121 db_vms.append(db_vm)
1122 db_vms_index += 1
1123
1124 # table interfaces (internal/external interfaces)
1125 flavor_epa_interfaces = []
tierno66eba6e2017-11-10 17:09:18 +01001126 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
1127 for iface in vdu.get("interface").itervalues():
1128 flavor_epa_interface = {}
1129 iface_uuid = str(uuid4())
1130 uuid_list.append(iface_uuid)
1131 db_interface = {
1132 "uuid": iface_uuid,
1133 "internal_name": get_str(iface, "name", 255),
1134 "vm_id": vm_uuid,
1135 }
1136 flavor_epa_interface["name"] = db_interface["internal_name"]
1137 if iface.get("virtual-interface").get("vpci"):
1138 db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12)
1139 flavor_epa_interface["vpci"] = db_interface["vpci"]
1140
1141 if iface.get("virtual-interface").get("bandwidth"):
1142 bps = int(iface.get("virtual-interface").get("bandwidth"))
1143 db_interface["bw"] = int(math.ceil(bps/1000000.0))
1144 flavor_epa_interface["bandwidth"] = "{} Mbps".format(db_interface["bw"])
1145
1146 if iface.get("virtual-interface").get("type") == "OM-MGMT":
1147 db_interface["type"] = "mgmt"
garciadeblas31e141b2018-10-25 18:33:19 +02001148 elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
tierno66eba6e2017-11-10 17:09:18 +01001149 db_interface["type"] = "bridge"
1150 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1151 elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1152 db_interface["type"] = "data"
1153 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1154 flavor_epa_interface["dedicated"] = "no" if iface["virtual-interface"]["type"] == "SR-IOV" \
1155 else "yes"
1156 flavor_epa_interfaces.append(flavor_epa_interface)
1157 else:
1158 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1159 "-interface':'type':'{}'. Interface type is not supported".format(
1160 vnfd_id, vdu_id, iface.get("virtual-interface").get("type")),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001161 httperrors.Bad_Request)
tierno66eba6e2017-11-10 17:09:18 +01001162
tiernoe72710b2018-07-23 16:16:00 +02001163 if iface.get("mgmt-interface"):
1164 db_interface["type"] = "mgmt"
1165
tierno66eba6e2017-11-10 17:09:18 +01001166 if iface.get("external-connection-point-ref"):
1167 try:
1168 cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")]
1169 db_interface["external_name"] = get_str(cp, "name", 255)
1170 cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid
tierno7e510052019-09-10 16:16:13 +00001171 cp_name2vdu_id[db_interface["external_name"]] = vdu_id
tierno66eba6e2017-11-10 17:09:18 +01001172 cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid
1173 cp_name2db_interface[db_interface["external_name"]] = db_interface
1174 for cp_descriptor in vnfd_descriptor["connection-point"]:
1175 if cp_descriptor["name"] == db_interface["external_name"]:
1176 break
1177 else:
1178 raise KeyError()
1179
1180 if vdu_id in vdu_id2cp_name:
1181 vdu_id2cp_name[vdu_id] = None # more than two connecdtion point for this VDU
1182 else:
1183 vdu_id2cp_name[vdu_id] = db_interface["external_name"]
1184
1185 # port security
1186 if str(cp_descriptor.get("port-security-enabled")).lower() == "false":
1187 db_interface["port_security"] = 0
1188 elif str(cp_descriptor.get("port-security-enabled")).lower() == "true":
1189 db_interface["port_security"] = 1
1190 except KeyError:
1191 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1192 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1193 " at connection-point".format(
1194 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
1195 cp=iface.get("vnfd-connection-point-ref")),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001196 httperrors.Bad_Request)
tierno66eba6e2017-11-10 17:09:18 +01001197 elif iface.get("internal-connection-point-ref"):
1198 try:
tierno41a69812018-02-16 14:34:33 +01001199 for icp_descriptor in vdu_descriptor["internal-connection-point"]:
1200 if icp_descriptor["id"] == str(iface.get("internal-connection-point-ref")):
1201 break
1202 else:
1203 raise KeyError("does not exist at vdu:internal-connection-point")
1204 icp = None
1205 icp_vld = None
tierno66eba6e2017-11-10 17:09:18 +01001206 for vld in vnfd.get("internal-vld").itervalues():
1207 for cp in vld.get("internal-connection-point").itervalues():
1208 if cp.get("id-ref") == iface.get("internal-connection-point-ref"):
tierno41a69812018-02-16 14:34:33 +01001209 if icp:
1210 raise KeyError("is referenced by more than one 'internal-vld'")
1211 icp = cp
1212 icp_vld = vld
1213 if not icp:
1214 raise KeyError("is not referenced by any 'internal-vld'")
1215
1216 db_interface["net_id"] = net_id2uuid[icp_vld.get("id")]
1217 if str(icp_descriptor.get("port-security-enabled")).lower() == "false":
1218 db_interface["port_security"] = 0
1219 elif str(icp_descriptor.get("port-security-enabled")).lower() == "true":
1220 db_interface["port_security"] = 1
1221 if icp.get("ip-address"):
1222 if not icp_vld.get("ip-profile-ref"):
1223 raise NfvoException
1224 db_interface["ip_address"] = str(icp.get("ip-address"))
1225 except KeyError as e:
tierno66eba6e2017-11-10 17:09:18 +01001226 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
tierno41a69812018-02-16 14:34:33 +01001227 "'interface[{iface}]':'internal-connection-point-ref':'{cp}'"
1228 " {msg}".format(
tierno66eba6e2017-11-10 17:09:18 +01001229 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
tierno41a69812018-02-16 14:34:33 +01001230 cp=iface.get("internal-connection-point-ref"), msg=str(e)),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001231 httperrors.Bad_Request)
tierno55d234c2018-07-04 18:29:21 +02001232 if iface.get("position"):
1233 db_interface["created_at"] = int(iface.get("position")) * 50
tierno41a69812018-02-16 14:34:33 +01001234 if iface.get("mac-address"):
1235 db_interface["mac"] = str(iface.get("mac-address"))
tierno66eba6e2017-11-10 17:09:18 +01001236 db_interfaces.append(db_interface)
1237
tiernof1ba57e2017-09-07 12:23:19 +02001238 # table flavors
1239 db_flavor = {
1240 "name": get_str(vdu, "name", 250) + "-flv",
1241 "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)),
1242 "ram": int(vdu["vm-flavor"].get("memory-mb", 1)),
garciadeblas79d1a1a2017-12-11 16:07:07 +01001243 "disk": int(vdu["vm-flavor"].get("storage-gb", 0)),
tiernof1ba57e2017-09-07 12:23:19 +02001244 }
tiernocf596692017-11-20 15:47:51 +01001245 # TODO revise the case of several numa-node-policy node
tiernof1ba57e2017-09-07 12:23:19 +02001246 extended = {}
1247 numa = {}
1248 if devices:
1249 extended["devices"] = devices
tierno66eba6e2017-11-10 17:09:18 +01001250 if flavor_epa_interfaces:
1251 numa["interfaces"] = flavor_epa_interfaces
tiernof1ba57e2017-09-07 12:23:19 +02001252 if vdu.get("guest-epa"): # TODO or dedicated_int:
1253 epa_vcpu_set = False
1254 if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
1255 numa_node_policy = vdu["guest-epa"].get("numa-node-policy")
1256 if numa_node_policy.get("node"):
tiernocf596692017-11-20 15:47:51 +01001257 numa_node = numa_node_policy["node"].values()[0]
tiernof1ba57e2017-09-07 12:23:19 +02001258 if numa_node.get("num-cores"):
1259 numa["cores"] = numa_node["num-cores"]
1260 epa_vcpu_set = True
1261 if numa_node.get("paired-threads"):
1262 if numa_node["paired-threads"].get("num-paired-threads"):
tierno39dddcc2017-10-05 18:48:06 +02001263 numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"])
tiernof1ba57e2017-09-07 12:23:19 +02001264 epa_vcpu_set = True
tierno39dddcc2017-10-05 18:48:06 +02001265 if len(numa_node["paired-threads"].get("paired-thread-ids")):
tiernof1ba57e2017-09-07 12:23:19 +02001266 numa["paired-threads-id"] = []
tierno39dddcc2017-10-05 18:48:06 +02001267 for pair in numa_node["paired-threads"]["paired-thread-ids"].itervalues():
tiernof1ba57e2017-09-07 12:23:19 +02001268 numa["paired-threads-id"].append(
1269 (str(pair["thread-a"]), str(pair["thread-b"]))
1270 )
1271 if numa_node.get("num-threads"):
tierno39dddcc2017-10-05 18:48:06 +02001272 numa["threads"] = int(numa_node["num-threads"])
tiernof1ba57e2017-09-07 12:23:19 +02001273 epa_vcpu_set = True
1274 if numa_node.get("memory-mb"):
1275 numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1)
1276 if vdu["guest-epa"].get("mempage-size"):
1277 if vdu["guest-epa"]["mempage-size"] != "SMALL":
1278 numa["memory"] = max(int(db_flavor["ram"] / 1024), 1)
1279 if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set:
1280 if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
1281 if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \
1282 vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
1283 numa["cores"] = max(db_flavor["vcpus"], 1)
1284 else:
1285 numa["threads"] = max(db_flavor["vcpus"], 1)
anwarsae5f52c2019-04-22 10:35:27 +05301286 epa_vcpu_set = True
1287 if vdu["guest-epa"].get("cpu-quota") and not epa_vcpu_set:
calvinosanch1d55a232019-08-05 11:03:46 +00001288 cpuquota = get_resource_allocation_params(vdu["guest-epa"].get("cpu-quota"))
1289 if cpuquota:
1290 extended["cpu-quota"] = cpuquota
anwarsae5f52c2019-04-22 10:35:27 +05301291 if vdu["guest-epa"].get("mem-quota"):
calvinosanch1d55a232019-08-05 11:03:46 +00001292 vduquota = get_resource_allocation_params(vdu["guest-epa"].get("mem-quota"))
1293 if vduquota:
1294 extended["mem-quota"] = vduquota
anwarsae5f52c2019-04-22 10:35:27 +05301295 if vdu["guest-epa"].get("disk-io-quota"):
calvinosanch1d55a232019-08-05 11:03:46 +00001296 diskioquota = get_resource_allocation_params(vdu["guest-epa"].get("disk-io-quota"))
1297 if diskioquota:
1298 extended["disk-io-quota"] = diskioquota
anwarsae5f52c2019-04-22 10:35:27 +05301299 if vdu["guest-epa"].get("vif-quota"):
calvinosanch1d55a232019-08-05 11:03:46 +00001300 vifquota = get_resource_allocation_params(vdu["guest-epa"].get("vif-quota"))
1301 if vifquota:
1302 extended["vif-quota"] = vifquota
tiernof1ba57e2017-09-07 12:23:19 +02001303 if numa:
1304 extended["numas"] = [numa]
1305 if extended:
1306 extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256)
1307 db_flavor["extended"] = extended_text
1308 # look if flavor exist
garciadeblas79d1a1a2017-12-11 16:07:07 +01001309 temp_flavor_dict = {'disk': db_flavor.get('disk', 0),
tiernof1ba57e2017-09-07 12:23:19 +02001310 'ram': db_flavor.get('ram'),
1311 'vcpus': db_flavor.get('vcpus'),
1312 'extended': db_flavor.get('extended')
1313 }
1314 existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
1315 if existing_flavors:
1316 flavor_uuid = existing_flavors[0]["uuid"]
1317 else:
1318 flavor_uuid = str(uuid4())
1319 uuid_list.append(flavor_uuid)
1320 db_flavor["uuid"] = flavor_uuid
1321 db_flavors.append(db_flavor)
1322 db_vm["flavor_id"] = flavor_uuid
1323
tiernof1ba57e2017-09-07 12:23:19 +02001324 # VNF affinity and antiaffinity
1325 for pg in vnfd.get("placement-groups").itervalues():
1326 pg_name = get_str(pg, "name", 255)
1327 for vdu in pg.get("member-vdus").itervalues():
1328 vdu_id = get_str(vdu, "member-vdu-ref", 255)
1329 if vdu_id not in vdu_id2db_table_index:
tiernob2880eb2017-10-04 15:04:53 +02001330 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1331 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
tierno66eba6e2017-11-10 17:09:18 +01001332 vnf=vnfd_id, pg=pg_name, vdu=vdu_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001333 httperrors.Bad_Request)
tierno55fe3972019-03-29 08:50:12 +00001334 db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name
tiernof1ba57e2017-09-07 12:23:19 +02001335 # TODO consider the case of isolation and not colocation
1336 # if pg.get("strategy") == "ISOLATION":
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001337
tiernof1ba57e2017-09-07 12:23:19 +02001338 # VNF mgmt configuration
tiernof1ba57e2017-09-07 12:23:19 +02001339 if vnfd["mgmt-interface"].get("vdu-id"):
tierno66eba6e2017-11-10 17:09:18 +01001340 mgmt_vdu_id = get_str(vnfd["mgmt-interface"], "vdu-id", 255)
1341 if mgmt_vdu_id not in vdu_id2uuid:
tiernob2880eb2017-10-04 15:04:53 +02001342 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1343 "'{vdu}'. Reference to a non-existing vdu".format(
tierno66eba6e2017-11-10 17:09:18 +01001344 vnf=vnfd_id, vdu=mgmt_vdu_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001345 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001346 mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]]
tierno7e510052019-09-10 16:16:13 +00001347 mgmt_access["vdu-id"] = vnfd["mgmt-interface"]["vdu-id"]
tierno66eba6e2017-11-10 17:09:18 +01001348 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
1349 if vdu_id2cp_name.get(mgmt_vdu_id):
tiernob6990792018-11-13 10:37:42 +01001350 if cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]:
1351 cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt"
tierno66eba6e2017-11-10 17:09:18 +01001352
tiernof1ba57e2017-09-07 12:23:19 +02001353 if vnfd["mgmt-interface"].get("ip-address"):
1354 mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address"))
calvinosanch0aa0e2f2019-11-07 11:46:38 +01001355 if vnfd["mgmt-interface"].get("cp") and vnfd.get("vdu"):
tiernof1ba57e2017-09-07 12:23:19 +02001356 if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid:
tiernob6990792018-11-13 10:37:42 +01001357 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp'['{cp}']. "
tiernob2880eb2017-10-04 15:04:53 +02001358 "Reference to a non-existing connection-point".format(
tierno66eba6e2017-11-10 17:09:18 +01001359 vnf=vnfd_id, cp=vnfd["mgmt-interface"]["cp"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001360 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001361 mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]]
1362 mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]]
tierno7e510052019-09-10 16:16:13 +00001363 mgmt_access["vdu-id"] = cp_name2vdu_id[vnfd["mgmt-interface"]["cp"]]
tiernoe2ff1ce2017-11-02 17:01:10 +01001364 # mark this interface as of type mgmt
tiernob6990792018-11-13 10:37:42 +01001365 if cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]:
1366 cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt"
tiernoe2ff1ce2017-11-02 17:01:10 +01001367
tiernoa9550202017-09-22 13:31:35 +02001368 default_user = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
tiernof1ba57e2017-09-07 12:23:19 +02001369 "default-user", 64)
1370 if default_user:
1371 mgmt_access["default_user"] = default_user
tierno7e510052019-09-10 16:16:13 +00001372
gcalvinoe580c7d2017-09-22 14:09:51 +02001373 required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1374 "required", 6)
1375 if required:
1376 mgmt_access["required"] = required
1377
tierno7e510052019-09-10 16:16:13 +00001378 password_ = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}),
1379 "password", 64)
1380 if password_:
1381 mgmt_access["password"] = password_
1382
tiernof1ba57e2017-09-07 12:23:19 +02001383 if mgmt_access:
1384 db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256)
1385
1386 db_vnfs.append(db_vnf)
1387 db_tables=[
1388 {"vnfs": db_vnfs},
1389 {"nets": db_nets},
1390 {"images": db_images},
1391 {"flavors": db_flavors},
tierno41a69812018-02-16 14:34:33 +01001392 {"ip_profiles": db_ip_profiles},
tiernof1ba57e2017-09-07 12:23:19 +02001393 {"vms": db_vms},
1394 {"interfaces": db_interfaces},
1395 ]
1396
1397 logger.debug("create_vnf Deployment done vnfDict: %s",
1398 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
1399 mydb.new_rows(db_tables, uuid_list)
1400 return vnfd_uuid_list
tiernob2880eb2017-10-04 15:04:53 +02001401 except NfvoException:
1402 raise
tiernof1ba57e2017-09-07 12:23:19 +02001403 except Exception as e:
1404 logger.error("Exception {}".format(e))
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001405 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001406
1407
tiernob8569aa2018-08-24 11:34:54 +02001408@deprecated("Use new_vnfd_v3")
tierno7edb6752016-03-21 17:37:52 +01001409def new_vnf(mydb, tenant_id, vnf_descriptor):
1410 global global_config
tierno42026a02017-02-10 15:13:40 +01001411
tierno7edb6752016-03-21 17:37:52 +01001412 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +01001413 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
tierno7edb6752016-03-21 17:37:52 +01001414 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +01001415 vims = {}
tierno7edb6752016-03-21 17:37:52 +01001416 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001417 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001418 if "tenant_id" in vnf_descriptor["vnf"]:
1419 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001420 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001421 httperrors.Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001422 else:
1423 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1424 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +01001425 if global_config["auto_push_VNF_to_VIMs"]:
tiernocbb52052018-05-31 18:57:30 +02001426 vims = get_vim(mydb, tenant_id, ignore_errors=True)
tierno7edb6752016-03-21 17:37:52 +01001427
1428 # Step 4. Review the descriptor and add missing fields
1429 #print vnf_descriptor
tiernof97fd272016-07-11 14:32:37 +02001430 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
tierno7edb6752016-03-21 17:37:52 +01001431 vnf_name = vnf_descriptor['vnf']['name']
1432 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1433 if "physical" in vnf_descriptor['vnf']:
1434 del vnf_descriptor['vnf']['physical']
1435 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +01001436
tierno42026a02017-02-10 15:13:40 +01001437 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
tiernof97fd272016-07-11 14:32:37 +02001438 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1439 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +01001440
tierno7edb6752016-03-21 17:37:52 +01001441 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1442 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1443 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
tierno7edb6752016-03-21 17:37:52 +01001444 try:
tiernof97fd272016-07-11 14:32:37 +02001445 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +01001446 for vnfc in vnf_descriptor['vnf']['VNFC']:
1447 VNFCitem={}
1448 VNFCitem["name"] = vnfc['name']
mirabal29356312017-07-27 12:21:22 +02001449 VNFCitem["availability_zone"] = vnfc.get('availability_zone')
tierno7edb6752016-03-21 17:37:52 +01001450 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +01001451
tiernof97fd272016-07-11 14:32:37 +02001452 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +01001453
tierno7edb6752016-03-21 17:37:52 +01001454 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +02001455 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 +01001456 myflavorDict["description"] = VNFCitem["description"]
1457 myflavorDict["ram"] = vnfc.get("ram", 0)
1458 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
garciadeblas79d1a1a2017-12-11 16:07:07 +01001459 myflavorDict["disk"] = vnfc.get("disk", 0)
tierno7edb6752016-03-21 17:37:52 +01001460 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +01001461
tierno7edb6752016-03-21 17:37:52 +01001462 devices = vnfc.get("devices")
1463 if devices != None:
1464 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +01001465
tierno7edb6752016-03-21 17:37:52 +01001466 # TODO:
1467 # 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 +01001468 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1469
tierno7edb6752016-03-21 17:37:52 +01001470 # Previous code has been commented
1471 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1472 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1473 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1474 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1475 #else:
1476 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1477 # if result2:
1478 # print "Error creating flavor: unknown processor model. Rollback successful."
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001479 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
tierno7edb6752016-03-21 17:37:52 +01001480 # else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001481 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
tierno7edb6752016-03-21 17:37:52 +01001482 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +01001483
tierno7edb6752016-03-21 17:37:52 +01001484 if 'numas' in vnfc and len(vnfc['numas'])>0:
1485 myflavorDict['extended']['numas'] = vnfc['numas']
1486
1487 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +01001488
tierno7edb6752016-03-21 17:37:52 +01001489 # Step 6.2 New flavors are created in the VIM
tiernof97fd272016-07-11 14:32:37 +02001490 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
tierno7edb6752016-03-21 17:37:52 +01001491
tiernof97fd272016-07-11 14:32:37 +02001492 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
tierno7edb6752016-03-21 17:37:52 +01001493 VNFCitem["flavor_id"] = flavor_id
1494 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +01001495
tiernof97fd272016-07-11 14:32:37 +02001496 logger.debug("Creating new images in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +01001497 # Step 6.3 New images are created in the VIM
1498 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +01001499 #This "for" loop might be integrated with the previous one
tierno7edb6752016-03-21 17:37:52 +01001500 #In case this integration is made, the VNFCDict might become a VNFClist.
1501 for vnfc in vnf_descriptor['vnf']['VNFC']:
tiernof97fd272016-07-11 14:32:37 +02001502 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +02001503 image_dict={}
1504 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1505 image_dict['universal_name']=vnfc.get('image name')
1506 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1507 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +01001508 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +02001509 image_dict['checksum']=vnfc.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +01001510 image_metadata_dict = vnfc.get('image metadata', None)
1511 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +01001512 if image_metadata_dict is not None:
tierno7edb6752016-03-21 17:37:52 +01001513 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1514 image_dict['metadata']=image_metadata_str
1515 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
tiernof97fd272016-07-11 14:32:37 +02001516 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1517 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
tierno7edb6752016-03-21 17:37:52 +01001518 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +02001519 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno8e690322017-08-10 15:58:50 +02001520 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
tierno36c0b172017-01-12 18:32:28 +01001521 if vnfc.get("boot-data"):
1522 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
tierno7edb6752016-03-21 17:37:52 +01001523
tierno42026a02017-02-10 15:13:40 +01001524
tiernof97fd272016-07-11 14:32:37 +02001525 # Step 7. Storing the VNF descriptor in the repository
1526 if "descriptor" not in vnf_descriptor["vnf"]:
1527 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001528
tiernof97fd272016-07-11 14:32:37 +02001529 # Step 8. Adding the VNF to the NFVO DB
1530 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1531 return vnf_id
1532 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
tierno7edb6752016-03-21 17:37:52 +01001533 _, message = rollback(mydb, vims, rollback_list)
tiernof97fd272016-07-11 14:32:37 +02001534 if isinstance(e, db_base_Exception):
1535 error_text = "Exception at database"
1536 elif isinstance(e, KeyError):
1537 error_text = "KeyError exception "
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001538 e.http_code = httperrors.Internal_Server_Error
tiernof97fd272016-07-11 14:32:37 +02001539 else:
1540 error_text = "Exception at VIM"
1541 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1542 #logger.error("start_scenario %s", error_text)
1543 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01001544
tiernob3d36742017-03-03 23:51:05 +01001545
tiernob8569aa2018-08-24 11:34:54 +02001546@deprecated("Use new_vnfd_v3")
garciadeblas9f8456e2016-09-05 05:02:59 +02001547def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
1548 global global_config
tierno42026a02017-02-10 15:13:40 +01001549
garciadeblas9f8456e2016-09-05 05:02:59 +02001550 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +01001551 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
garciadeblas9f8456e2016-09-05 05:02:59 +02001552 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +01001553 vims = {}
garciadeblas9f8456e2016-09-05 05:02:59 +02001554 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001555 check_tenant(mydb, tenant_id)
garciadeblas9f8456e2016-09-05 05:02:59 +02001556 if "tenant_id" in vnf_descriptor["vnf"]:
1557 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
1558 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001559 httperrors.Unauthorized)
garciadeblas9f8456e2016-09-05 05:02:59 +02001560 else:
1561 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1562 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +01001563 if global_config["auto_push_VNF_to_VIMs"]:
tiernocbb52052018-05-31 18:57:30 +02001564 vims = get_vim(mydb, tenant_id, ignore_errors=True)
garciadeblas9f8456e2016-09-05 05:02:59 +02001565
1566 # Step 4. Review the descriptor and add missing fields
1567 #print vnf_descriptor
1568 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1569 vnf_name = vnf_descriptor['vnf']['name']
1570 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1571 if "physical" in vnf_descriptor['vnf']:
1572 del vnf_descriptor['vnf']['physical']
1573 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +01001574
tierno42026a02017-02-10 15:13:40 +01001575 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
garciadeblas9f8456e2016-09-05 05:02:59 +02001576 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1577 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +01001578
garciadeblas9f8456e2016-09-05 05:02:59 +02001579 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1580 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1581 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
1582 try:
1583 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1584 for vnfc in vnf_descriptor['vnf']['VNFC']:
1585 VNFCitem={}
1586 VNFCitem["name"] = vnfc['name']
1587 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +01001588
garciadeblas9f8456e2016-09-05 05:02:59 +02001589 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +01001590
garciadeblas9f8456e2016-09-05 05:02:59 +02001591 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +02001592 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 +02001593 myflavorDict["description"] = VNFCitem["description"]
1594 myflavorDict["ram"] = vnfc.get("ram", 0)
1595 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
garciadeblas79d1a1a2017-12-11 16:07:07 +01001596 myflavorDict["disk"] = vnfc.get("disk", 0)
garciadeblas9f8456e2016-09-05 05:02:59 +02001597 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +01001598
garciadeblas9f8456e2016-09-05 05:02:59 +02001599 devices = vnfc.get("devices")
1600 if devices != None:
1601 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +01001602
garciadeblas9f8456e2016-09-05 05:02:59 +02001603 # TODO:
1604 # 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 +01001605 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1606
garciadeblas9f8456e2016-09-05 05:02:59 +02001607 # Previous code has been commented
1608 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1609 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1610 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1611 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1612 #else:
1613 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1614 # if result2:
1615 # print "Error creating flavor: unknown processor model. Rollback successful."
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001616 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
garciadeblas9f8456e2016-09-05 05:02:59 +02001617 # else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001618 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
garciadeblas9f8456e2016-09-05 05:02:59 +02001619 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +01001620
garciadeblas9f8456e2016-09-05 05:02:59 +02001621 if 'numas' in vnfc and len(vnfc['numas'])>0:
1622 myflavorDict['extended']['numas'] = vnfc['numas']
1623
1624 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +01001625
garciadeblas9f8456e2016-09-05 05:02:59 +02001626 # Step 6.2 New flavors are created in the VIM
1627 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
1628
1629 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1630 VNFCitem["flavor_id"] = flavor_id
1631 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +01001632
garciadeblas9f8456e2016-09-05 05:02:59 +02001633 logger.debug("Creating new images in the VIM for each VNFC")
1634 # Step 6.3 New images are created in the VIM
1635 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +01001636 #This "for" loop might be integrated with the previous one
garciadeblas9f8456e2016-09-05 05:02:59 +02001637 #In case this integration is made, the VNFCDict might become a VNFClist.
1638 for vnfc in vnf_descriptor['vnf']['VNFC']:
1639 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +02001640 image_dict={}
1641 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1642 image_dict['universal_name']=vnfc.get('image name')
1643 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1644 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +01001645 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +02001646 image_dict['checksum']=vnfc.get('image checksum')
garciadeblas9f8456e2016-09-05 05:02:59 +02001647 image_metadata_dict = vnfc.get('image metadata', None)
1648 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +01001649 if image_metadata_dict is not None:
garciadeblas9f8456e2016-09-05 05:02:59 +02001650 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1651 image_dict['metadata']=image_metadata_str
1652 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1653 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1654 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1655 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +02001656 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno8e690322017-08-10 15:58:50 +02001657 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
tierno36c0b172017-01-12 18:32:28 +01001658 if vnfc.get("boot-data"):
1659 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
garciadeblas9f8456e2016-09-05 05:02:59 +02001660
garciadeblas9f8456e2016-09-05 05:02:59 +02001661 # Step 7. Storing the VNF descriptor in the repository
1662 if "descriptor" not in vnf_descriptor["vnf"]:
1663 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001664
garciadeblas9f8456e2016-09-05 05:02:59 +02001665 # Step 8. Adding the VNF to the NFVO DB
1666 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1667 return vnf_id
1668 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
1669 _, message = rollback(mydb, vims, rollback_list)
1670 if isinstance(e, db_base_Exception):
1671 error_text = "Exception at database"
1672 elif isinstance(e, KeyError):
1673 error_text = "KeyError exception "
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001674 e.http_code = httperrors.Internal_Server_Error
garciadeblas9f8456e2016-09-05 05:02:59 +02001675 else:
1676 error_text = "Exception at VIM"
1677 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1678 #logger.error("start_scenario %s", error_text)
1679 raise NfvoException(error_text, e.http_code)
1680
tiernob3d36742017-03-03 23:51:05 +01001681
tierno7edb6752016-03-21 17:37:52 +01001682def get_vnf_id(mydb, tenant_id, vnf_id):
1683 #check valid tenant_id
tierno42026a02017-02-10 15:13:40 +01001684 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001685 #obtain data
1686 where_or = {}
1687 if tenant_id != "any":
1688 where_or["tenant_id"] = tenant_id
1689 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001690 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
1691
tiernof1ba57e2017-09-07 12:23:19 +02001692 vnf_id = vnf["uuid"]
1693 filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
tiernof97fd272016-07-11 14:32:37 +02001694 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
tierno7edb6752016-03-21 17:37:52 +01001695 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1696 data={'vnf' : filtered_content}
1697 #GET VM
tiernof97fd272016-07-11 14:32:37 +02001698 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
tiernof1ba57e2017-09-07 12:23:19 +02001699 SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1700 'boot_data'),
tierno7edb6752016-03-21 17:37:52 +01001701 WHERE={'vnfs.uuid': vnf_id} )
gcalvinobfa2fd92018-11-13 18:47:28 +01001702 if len(content) != 0:
Anderson Bravalheric5293de2018-11-28 17:21:26 +00001703 #raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
tierno36c0b172017-01-12 18:32:28 +01001704 # change boot_data into boot-data
gcalvino319b8a52018-11-05 15:33:23 +01001705 for vm in content:
1706 if vm.get("boot_data"):
1707 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
1708 del vm["boot_data"]
tierno7edb6752016-03-21 17:37:52 +01001709
gcalvinobfa2fd92018-11-13 18:47:28 +01001710 data['vnf']['VNFC'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001711 #TODO: GET all the information from a VNFC and include it in the output.
tierno42026a02017-02-10 15:13:40 +01001712
tierno7edb6752016-03-21 17:37:52 +01001713 #GET NET
tierno42026a02017-02-10 15:13:40 +01001714 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
tierno7edb6752016-03-21 17:37:52 +01001715 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1716 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +02001717 data['vnf']['nets'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001718
1719 #GET ip-profile for each net
1720 for net in data['vnf']['nets']:
1721 ipprofiles = mydb.get_rows(FROM='ip_profiles',
1722 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1723 WHERE={'net_id': net["uuid"]} )
1724 if len(ipprofiles)==1:
1725 net["ip_profile"] = ipprofiles[0]
1726 elif len(ipprofiles)>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001727 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net['uuid']), httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +01001728
1729
garciadeblas9f8456e2016-09-05 05:02:59 +02001730 #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 +01001731
garciadeblas9f8456e2016-09-05 05:02:59 +02001732 #GET External Interfaces
tiernof97fd272016-07-11 14:32:37 +02001733 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 +01001734 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1735 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
tierno3fcfdb72017-10-24 07:48:24 +02001736 WHERE={'vnfs.uuid': vnf_id, 'interfaces.external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01001737 #print content
tiernof97fd272016-07-11 14:32:37 +02001738 data['vnf']['external-connections'] = content
tierno42026a02017-02-10 15:13:40 +01001739
tiernof97fd272016-07-11 14:32:37 +02001740 return data
tierno7edb6752016-03-21 17:37:52 +01001741
1742
1743def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
1744 # Check tenant exist
1745 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +02001746 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001747 # Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernocbb52052018-05-31 18:57:30 +02001748 vims = get_vim(mydb, tenant_id, ignore_errors=True)
tierno7edb6752016-03-21 17:37:52 +01001749 else:
1750 vims={}
1751
1752 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1753 where_or = {}
1754 if tenant_id != "any":
1755 where_or["tenant_id"] = tenant_id
1756 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001757 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 +02001758 vnf_id = vnf["uuid"]
tierno42026a02017-02-10 15:13:40 +01001759
tierno7edb6752016-03-21 17:37:52 +01001760 # "Getting the list of flavors and tenants of the VNF"
tierno42026a02017-02-10 15:13:40 +01001761 flavorList = get_flavorlist(mydb, vnf_id)
tiernof97fd272016-07-11 14:32:37 +02001762 if len(flavorList)==0:
1763 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001764
tiernof97fd272016-07-11 14:32:37 +02001765 imageList = get_imagelist(mydb, vnf_id)
1766 if len(imageList)==0:
1767 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001768
tiernof97fd272016-07-11 14:32:37 +02001769 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
1770 if deleted == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001771 raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
tierno42026a02017-02-10 15:13:40 +01001772
tierno7edb6752016-03-21 17:37:52 +01001773 undeletedItems = []
1774 for flavor in flavorList:
1775 #check if flavor is used by other vnf
tiernof97fd272016-07-11 14:32:37 +02001776 try:
1777 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
1778 if len(c) > 0:
1779 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
1780 continue
1781 #flavor not used, must be deleted
1782 #delelte at VIM
tierno96ebf002017-12-13 10:55:38 +01001783 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id': flavor})
tierno7edb6752016-03-21 17:37:52 +01001784 for flavor_vim in c:
tierno96ebf002017-12-13 10:55:38 +01001785 if not flavor_vim['created']: # skip this flavor because not created by openmano
tierno7edb6752016-03-21 17:37:52 +01001786 continue
tierno96ebf002017-12-13 10:55:38 +01001787 # look for vim
1788 myvim = None
1789 for vim in vims.values():
1790 if vim["config"]["datacenter_tenant_id"] == flavor_vim["datacenter_vim_id"]:
1791 myvim = vim
1792 break
1793 if not myvim:
tierno7edb6752016-03-21 17:37:52 +01001794 continue
tiernoae4a8d12016-07-08 12:30:39 +02001795 try:
1796 myvim.delete_flavor(flavor_vim["vim_id"])
tierno96ebf002017-12-13 10:55:38 +01001797 except vimconn.vimconnNotFoundException:
1798 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"],
1799 flavor_vim["datacenter_vim_id"] )
tiernoae4a8d12016-07-08 12:30:39 +02001800 except vimconn.vimconnException as e:
1801 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
tierno96ebf002017-12-13 10:55:38 +01001802 flavor_vim["vim_id"], flavor_vim["datacenter_vim_id"], type(e).__name__, str(e))
1803 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"],
1804 flavor_vim["datacenter_vim_id"]))
1805 # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
tiernof97fd272016-07-11 14:32:37 +02001806 mydb.delete_row_by_id('flavors', flavor)
1807 except db_base_Exception as e:
1808 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
tierno96ebf002017-12-13 10:55:38 +01001809 undeletedItems.append("flavor {}".format(flavor))
tiernof97fd272016-07-11 14:32:37 +02001810
tierno42026a02017-02-10 15:13:40 +01001811
tierno7edb6752016-03-21 17:37:52 +01001812 for image in imageList:
tiernof97fd272016-07-11 14:32:37 +02001813 try:
1814 #check if image is used by other vnf
tierno16e3dd42018-04-24 12:52:40 +02001815 c = mydb.get_rows(FROM='vms', WHERE=[{'image_id': image}, {'image_list LIKE ': '%' + image + '%'}])
tiernof97fd272016-07-11 14:32:37 +02001816 if len(c) > 0:
1817 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
1818 continue
1819 #image not used, must be deleted
1820 #delelte at VIM
1821 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
tierno7edb6752016-03-21 17:37:52 +01001822 for image_vim in c:
tierno868220c2017-09-26 00:11:05 +02001823 if image_vim["datacenter_vim_id"] not in vims: # TODO change to datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +01001824 continue
1825 if image_vim['created']=='false': #skip this image because not created by openmano
1826 continue
1827 myvim=vims[ image_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +02001828 try:
1829 myvim.delete_image(image_vim["vim_id"])
1830 except vimconn.vimconnNotFoundException as e:
1831 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
1832 except vimconn.vimconnException as e:
1833 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1834 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
1835 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +02001836 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1837 mydb.delete_row_by_id('images', image)
1838 except db_base_Exception as e:
1839 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
tierno7edb6752016-03-21 17:37:52 +01001840 undeletedItems.append("image %s" % image)
1841
tiernof97fd272016-07-11 14:32:37 +02001842 return vnf_id + " " + vnf["name"]
tierno42026a02017-02-10 15:13:40 +01001843 #if undeletedItems:
tiernof97fd272016-07-11 14:32:37 +02001844 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
tierno7edb6752016-03-21 17:37:52 +01001845
tiernob3d36742017-03-03 23:51:05 +01001846
tiernob8569aa2018-08-24 11:34:54 +02001847@deprecated("Not used")
tierno7edb6752016-03-21 17:37:52 +01001848def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
1849 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
1850 if result < 0:
1851 return result, vims
1852 elif result == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001853 return -httperrors.Not_Found, "datacenter '%s' not found" % datacenter_name
tierno7edb6752016-03-21 17:37:52 +01001854 myvim = vims.values()[0]
1855 result,servers = myvim.get_hosts_info()
1856 if result < 0:
1857 return result, servers
1858 topology = {'name':myvim['name'] , 'servers': servers}
1859 return result, topology
1860
tiernob3d36742017-03-03 23:51:05 +01001861
tierno7edb6752016-03-21 17:37:52 +01001862def get_hosts(mydb, nfvo_tenant_id):
tiernof97fd272016-07-11 14:32:37 +02001863 vims = get_vim(mydb, nfvo_tenant_id)
1864 if len(vims) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001865 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), httperrors.Not_Found)
tiernof97fd272016-07-11 14:32:37 +02001866 elif len(vims)>1:
1867 #print "nfvo.datacenter_action() error. Several datacenters found"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001868 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
tierno7edb6752016-03-21 17:37:52 +01001869 myvim = vims.values()[0]
tiernof97fd272016-07-11 14:32:37 +02001870 try:
1871 hosts = myvim.get_hosts()
1872 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01001873
tiernof97fd272016-07-11 14:32:37 +02001874 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
1875 for host in hosts:
1876 server={'name':host['name'], 'vms':[]}
1877 for vm in host['instances']:
1878 #get internal name and model
tierno42026a02017-02-10 15:13:40 +01001879 try:
tiernof97fd272016-07-11 14:32:37 +02001880 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1881 WHERE={'vim_vm_id':vm['id']} )
1882 if len(c) == 0:
1883 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1884 continue
1885 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
tierno42026a02017-02-10 15:13:40 +01001886
tiernof97fd272016-07-11 14:32:37 +02001887 except db_base_Exception as e:
1888 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1889 datacenter['Datacenters'][0]['servers'].append(server)
1890 #return -400, "en construccion"
tierno42026a02017-02-10 15:13:40 +01001891
tiernof97fd272016-07-11 14:32:37 +02001892 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1893 return datacenter
1894 except vimconn.vimconnException as e:
1895 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001896
tiernob3d36742017-03-03 23:51:05 +01001897
tiernob8569aa2018-08-24 11:34:54 +02001898@deprecated("Use new_nsd_v3")
tierno7edb6752016-03-21 17:37:52 +01001899def new_scenario(mydb, tenant_id, topo):
1900
1901# result, vims = get_vim(mydb, tenant_id)
1902# if result < 0:
1903# return result, vims
1904#1: parse input
1905 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001906 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001907 if "tenant_id" in topo:
1908 if topo["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001909 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001910 httperrors.Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001911 else:
1912 tenant_id=None
1913
tierno42026a02017-02-10 15:13:40 +01001914#1.1: get VNFs and external_networks (other_nets).
tierno7edb6752016-03-21 17:37:52 +01001915 vnfs={}
1916 other_nets={} #external_networks, bridge_networks and data_networkds
1917 nodes = topo['topology']['nodes']
1918 for k in nodes.keys():
1919 if nodes[k]['type'] == 'VNF':
1920 vnfs[k] = nodes[k]
1921 vnfs[k]['ifaces'] = {}
tierno42026a02017-02-10 15:13:40 +01001922 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
tierno7edb6752016-03-21 17:37:52 +01001923 other_nets[k] = nodes[k]
1924 other_nets[k]['external']=True
tierno42026a02017-02-10 15:13:40 +01001925 elif nodes[k]['type'] == 'network':
tierno7edb6752016-03-21 17:37:52 +01001926 other_nets[k] = nodes[k]
1927 other_nets[k]['external']=False
tierno42026a02017-02-10 15:13:40 +01001928
tierno7edb6752016-03-21 17:37:52 +01001929
1930#1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1931 for name,vnf in vnfs.items():
tierno3fcfdb72017-10-24 07:48:24 +02001932 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
tierno7edb6752016-03-21 17:37:52 +01001933 error_text = ""
1934 error_pos = "'topology':'nodes':'" + name + "'"
1935 if 'vnf_id' in vnf:
1936 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001937 where['uuid'] = vnf['vnf_id']
tierno7edb6752016-03-21 17:37:52 +01001938 if 'VNF model' in vnf:
1939 error_text += " 'VNF model' " + vnf['VNF model']
tiernocea279c2016-07-18 12:36:49 +02001940 where['name'] = vnf['VNF model']
tierno3fcfdb72017-10-24 07:48:24 +02001941 if len(where) == 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001942 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +01001943
tiernocea279c2016-07-18 12:36:49 +02001944 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1945 FROM='vnfs',
tierno3fcfdb72017-10-24 07:48:24 +02001946 WHERE=where)
tiernof97fd272016-07-11 14:32:37 +02001947 if len(vnf_db)==0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001948 raise NfvoException("unknown" + error_text + " at " + error_pos, httperrors.Not_Found)
tiernof97fd272016-07-11 14:32:37 +02001949 elif len(vnf_db)>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001950 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", httperrors.Conflict)
tierno7edb6752016-03-21 17:37:52 +01001951 vnf['uuid']=vnf_db[0]['uuid']
1952 vnf['description']=vnf_db[0]['description']
1953 #get external interfaces
tierno42026a02017-02-10 15:13:40 +01001954 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1955 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 +02001956 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01001957 for ext_iface in ext_ifaces:
1958 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1959
1960#1.4 get list of connections
1961 conections = topo['topology']['connections']
1962 conections_list = []
tiernoefd80c92016-09-16 14:17:46 +02001963 conections_list_name = []
tierno7edb6752016-03-21 17:37:52 +01001964 for k in conections.keys():
1965 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1966 ifaces_list = conections[k]['nodes'].items()
1967 elif type(conections[k]['nodes'])==list: #list with dictionary
1968 ifaces_list=[]
1969 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1970 for k2 in conection_pair_list:
1971 ifaces_list += k2
1972
1973 con_type = conections[k].get("type", "link")
1974 if con_type != "link":
1975 if k in other_nets:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001976 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001977 other_nets[k] = {'external': False}
1978 if conections[k].get("graph"):
1979 other_nets[k]["graph"] = conections[k]["graph"]
1980 ifaces_list.append( (k, None) )
1981
tierno42026a02017-02-10 15:13:40 +01001982
tierno7edb6752016-03-21 17:37:52 +01001983 if con_type == "external_network":
1984 other_nets[k]['external'] = True
1985 if conections[k].get("model"):
1986 other_nets[k]["model"] = conections[k]["model"]
1987 else:
1988 other_nets[k]["model"] = k
tierno42026a02017-02-10 15:13:40 +01001989 if con_type == "dataplane_net" or con_type == "bridge_net":
tierno7edb6752016-03-21 17:37:52 +01001990 other_nets[k]["model"] = con_type
tierno42026a02017-02-10 15:13:40 +01001991
tiernoefd80c92016-09-16 14:17:46 +02001992 conections_list_name.append(k)
tierno7edb6752016-03-21 17:37:52 +01001993 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)
1994 #print set(ifaces_list)
1995 #check valid VNF and iface names
1996 for iface in ifaces_list:
1997 if iface[0] not in vnfs and iface[0] not in other_nets :
tiernof97fd272016-07-11 14:32:37 +02001998 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001999 str(k), iface[0]), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002000 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
tiernof97fd272016-07-11 14:32:37 +02002001 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002002 str(k), iface[0], iface[1]), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002003
2004#1.5 unify connections from the pair list to a consolidated list
2005 index=0
2006 while index < len(conections_list):
2007 index2 = index+1
2008 while index2 < len(conections_list):
2009 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
2010 conections_list[index] |= conections_list[index2]
2011 del conections_list[index2]
tiernoefd80c92016-09-16 14:17:46 +02002012 del conections_list_name[index2]
tierno7edb6752016-03-21 17:37:52 +01002013 else:
2014 index2 += 1
2015 conections_list[index] = list(conections_list[index]) # from set to list again
2016 index += 1
2017 #for k in conections_list:
2018 # print k
tierno42026a02017-02-10 15:13:40 +01002019
tierno7edb6752016-03-21 17:37:52 +01002020
2021
2022#1.6 Delete non external nets
2023# for k in other_nets.keys():
2024# if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
2025# for con in conections_list:
2026# delete_indexes=[]
2027# for index in range(0,len(con)):
2028# if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
2029# for index in delete_indexes:
2030# del con[index]
2031# del other_nets[k]
2032#1.7: Check external_ports are present at database table datacenter_nets
2033 for k,net in other_nets.items():
2034 error_pos = "'topology':'nodes':'" + k + "'"
2035 if net['external']==False:
2036 if 'name' not in net:
2037 net['name']=k
2038 if 'model' not in net:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002039 raise NfvoException("needed a 'model' at " + error_pos, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002040 if net['model']=='bridge_net':
2041 net['type']='bridge';
2042 elif net['model']=='dataplane_net':
2043 net['type']='data';
2044 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002045 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002046 else: #external
2047#IF we do not want to check that external network exist at datacenter
2048 pass
tierno42026a02017-02-10 15:13:40 +01002049#ELSE
tierno7edb6752016-03-21 17:37:52 +01002050# error_text = ""
2051# WHERE_={}
2052# if 'net_id' in net:
2053# error_text += " 'net_id' " + net['net_id']
2054# WHERE_['uuid'] = net['net_id']
2055# if 'model' in net:
2056# error_text += " 'model' " + net['model']
2057# WHERE_['name'] = net['model']
2058# if len(WHERE_) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002059# return -httperrors.Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
tierno7edb6752016-03-21 17:37:52 +01002060# r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
2061# FROM='datacenter_nets', WHERE=WHERE_ )
2062# if r<0:
2063# print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
2064# elif r==0:
2065# print "nfvo.new_scenario Error" +error_text+ " is not present at database"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002066# return -httperrors.Bad_Request, "unknown " +error_text+ " at " + error_pos
tierno7edb6752016-03-21 17:37:52 +01002067# elif r>1:
tierno42026a02017-02-10 15:13:40 +01002068# print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002069# return -httperrors.Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
tierno7edb6752016-03-21 17:37:52 +01002070# other_nets[k].update(net_db[0])
tierno42026a02017-02-10 15:13:40 +01002071#ENDIF
tierno7edb6752016-03-21 17:37:52 +01002072 net_list={}
2073 net_nb=0 #Number of nets
2074 for con in conections_list:
2075 #check if this is connected to a external net
2076 other_net_index=-1
2077 #print
2078 #print "con", con
2079 for index in range(0,len(con)):
2080 #check if this is connected to a external net
2081 for net_key in other_nets.keys():
2082 if con[index][0]==net_key:
2083 if other_net_index>=0:
tierno42026a02017-02-10 15:13:40 +01002084 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 +02002085 #print "nfvo.new_scenario " + error_text
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002086 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002087 else:
2088 other_net_index = index
2089 net_target = net_key
2090 break
2091 #print "other_net_index", other_net_index
2092 try:
2093 if other_net_index>=0:
2094 del con[other_net_index]
2095#IF we do not want to check that external network exist at datacenter
2096 if other_nets[net_target]['external'] :
2097 if "name" not in other_nets[net_target]:
2098 other_nets[net_target]['name'] = other_nets[net_target]['model']
2099 if other_nets[net_target]["type"] == "external_network":
2100 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
2101 other_nets[net_target]["type"] = "data"
2102 else:
2103 other_nets[net_target]["type"] = "bridge"
tierno42026a02017-02-10 15:13:40 +01002104#ELSE
tierno7edb6752016-03-21 17:37:52 +01002105# if other_nets[net_target]['external'] :
2106# 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
2107# if type_=='data' and other_nets[net_target]['type']=="ptp":
2108# error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
2109# print "nfvo.new_scenario " + error_text
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002110# return -httperrors.Bad_Request, error_text
tierno42026a02017-02-10 15:13:40 +01002111#ENDIF
tierno7edb6752016-03-21 17:37:52 +01002112 for iface in con:
2113 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
2114 else:
2115 #create a net
2116 net_type_bridge=False
2117 net_type_data=False
2118 net_target = "__-__net"+str(net_nb)
tierno42026a02017-02-10 15:13:40 +01002119 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
tiernoefd80c92016-09-16 14:17:46 +02002120 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
tierno42026a02017-02-10 15:13:40 +01002121 'external':False}
tierno7edb6752016-03-21 17:37:52 +01002122 for iface in con:
2123 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
2124 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
2125 if iface_type=='mgmt' or iface_type=='bridge':
2126 net_type_bridge = True
2127 else:
2128 net_type_data = True
2129 if net_type_bridge and net_type_data:
2130 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 +02002131 #print "nfvo.new_scenario " + error_text
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002132 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002133 elif net_type_bridge:
2134 type_='bridge'
2135 else:
2136 type_='data' if len(con)>2 else 'ptp'
2137 net_list[net_target]['type'] = type_
2138 net_nb+=1
2139 except Exception:
2140 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02002141 #print "nfvo.new_scenario " + error_text
tierno7edb6752016-03-21 17:37:52 +01002142 #raise e
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002143 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002144
2145#1.8: Connect to management net all not already connected interfaces of type 'mgmt'
tierno42026a02017-02-10 15:13:40 +01002146 #1.8.1 obtain management net
tiernof97fd272016-07-11 14:32:37 +02002147 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
tierno7edb6752016-03-21 17:37:52 +01002148 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
tierno42026a02017-02-10 15:13:40 +01002149 #1.8.2 check all interfaces from all vnfs
tiernof97fd272016-07-11 14:32:37 +02002150 if len(mgmt_net)>0:
tierno7edb6752016-03-21 17:37:52 +01002151 add_mgmt_net = False
2152 for vnf in vnfs.values():
2153 for iface in vnf['ifaces'].values():
2154 if iface['type']=='mgmt' and 'net_key' not in iface:
2155 #iface not connected
2156 iface['net_key'] = 'mgmt'
2157 add_mgmt_net = True
2158 if add_mgmt_net and 'mgmt' not in net_list:
2159 net_list['mgmt']=mgmt_net[0]
2160 net_list['mgmt']['external']=True
2161 net_list['mgmt']['graph']={'visible':False}
2162
2163 net_list.update(other_nets)
tiernof97fd272016-07-11 14:32:37 +02002164 #print
2165 #print 'net_list', net_list
2166 #print
2167 #print 'vnfs', vnfs
2168 #print
tierno7edb6752016-03-21 17:37:52 +01002169
2170#2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
tiernof97fd272016-07-11 14:32:37 +02002171 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
tierno392f2852016-05-13 12:28:55 +02002172 'tenant_id':tenant_id, 'name':topo['name'],
2173 'description':topo.get('description',topo['name']),
2174 'public': topo.get('public', False)
2175 })
tierno42026a02017-02-10 15:13:40 +01002176
tiernof97fd272016-07-11 14:32:37 +02002177 return c
tierno7edb6752016-03-21 17:37:52 +01002178
tiernob3d36742017-03-03 23:51:05 +01002179
tiernob8569aa2018-08-24 11:34:54 +02002180@deprecated("Use new_nsd_v3")
tierno5bb59dc2017-02-13 14:53:54 +01002181def new_scenario_v02(mydb, tenant_id, scenario_dict, version):
2182 """ This creates a new scenario for version 0.2 and 0.3"""
tierno392f2852016-05-13 12:28:55 +02002183 scenario = scenario_dict["scenario"]
tierno7edb6752016-03-21 17:37:52 +01002184 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01002185 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002186 if "tenant_id" in scenario:
2187 if scenario["tenant_id"] != tenant_id:
tierno5bb59dc2017-02-13 14:53:54 +01002188 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
tiernof97fd272016-07-11 14:32:37 +02002189 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002190 scenario["tenant_id"], tenant_id), httperrors.Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01002191 else:
2192 tenant_id=None
2193
tierno5bb59dc2017-02-13 14:53:54 +01002194 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
tierno7edb6752016-03-21 17:37:52 +01002195 for name,vnf in scenario["vnfs"].iteritems():
tierno3fcfdb72017-10-24 07:48:24 +02002196 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
tierno7edb6752016-03-21 17:37:52 +01002197 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02002198 error_pos = "'scenario':'vnfs':'" + name + "'"
tierno7edb6752016-03-21 17:37:52 +01002199 if 'vnf_id' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01002200 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02002201 where['uuid'] = vnf['vnf_id']
tierno392f2852016-05-13 12:28:55 +02002202 if 'vnf_name' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01002203 error_text += " 'vnf_name' " + vnf['vnf_name']
tiernocea279c2016-07-18 12:36:49 +02002204 where['name'] = vnf['vnf_name']
tierno3fcfdb72017-10-24 07:48:24 +02002205 if len(where) == 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002206 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, httperrors.Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002207 vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'),
tiernocea279c2016-07-18 12:36:49 +02002208 FROM='vnfs',
tierno3fcfdb72017-10-24 07:48:24 +02002209 WHERE=where)
tierno5bb59dc2017-02-13 14:53:54 +01002210 if len(vnf_db) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002211 raise NfvoException("Unknown" + error_text + " at " + error_pos, httperrors.Not_Found)
tierno5bb59dc2017-02-13 14:53:54 +01002212 elif len(vnf_db) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002213 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", httperrors.Conflict)
tierno5bb59dc2017-02-13 14:53:54 +01002214 vnf['uuid'] = vnf_db[0]['uuid']
2215 vnf['description'] = vnf_db[0]['description']
tierno7edb6752016-03-21 17:37:52 +01002216 vnf['ifaces'] = {}
tierno5bb59dc2017-02-13 14:53:54 +01002217 # get external interfaces
2218 ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
2219 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 +02002220 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01002221 for ext_iface in ext_ifaces:
tierno5bb59dc2017-02-13 14:53:54 +01002222 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']}
2223 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
tierno7edb6752016-03-21 17:37:52 +01002224
tierno5bb59dc2017-02-13 14:53:54 +01002225 # 2: Insert net_key and ip_address at every vnf interface
2226 for net_name, net in scenario["networks"].items():
2227 net_type_bridge = False
2228 net_type_data = False
tierno7edb6752016-03-21 17:37:52 +01002229 for iface_dict in net["interfaces"]:
tierno5bb59dc2017-02-13 14:53:54 +01002230 if version == "0.2":
2231 temp_dict = iface_dict
2232 ip_address = None
2233 elif version == "0.3":
2234 temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]}
2235 ip_address = iface_dict.get('ip_address', None)
2236 for vnf, iface in temp_dict.items():
tierno7edb6752016-03-21 17:37:52 +01002237 if vnf not in scenario["vnfs"]:
tierno5bb59dc2017-02-13 14:53:54 +01002238 error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2239 net_name, vnf)
2240 # logger.debug("nfvo.new_scenario_v02 " + error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002241 raise NfvoException(error_text, httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002242 if iface not in scenario["vnfs"][vnf]['ifaces']:
tierno5bb59dc2017-02-13 14:53:54 +01002243 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2244 .format(net_name, iface)
2245 # logger.debug("nfvo.new_scenario_v02 " + error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002246 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002247 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
tierno5bb59dc2017-02-13 14:53:54 +01002248 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2249 "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
2250 # logger.debug("nfvo.new_scenario_v02 " + error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002251 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002252 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
tierno5bb59dc2017-02-13 14:53:54 +01002253 scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address
tierno7edb6752016-03-21 17:37:52 +01002254 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
tierno5bb59dc2017-02-13 14:53:54 +01002255 if iface_type == 'mgmt' or iface_type == 'bridge':
tierno7edb6752016-03-21 17:37:52 +01002256 net_type_bridge = True
2257 else:
2258 net_type_data = True
tierno5bb59dc2017-02-13 14:53:54 +01002259
tierno7edb6752016-03-21 17:37:52 +01002260 if net_type_bridge and net_type_data:
tierno5bb59dc2017-02-13 14:53:54 +01002261 error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2262 .format(net_name)
2263 # logger.debug("nfvo.new_scenario " + error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002264 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002265 elif net_type_bridge:
tierno5bb59dc2017-02-13 14:53:54 +01002266 type_ = 'bridge'
tierno7edb6752016-03-21 17:37:52 +01002267 else:
tierno5bb59dc2017-02-13 14:53:54 +01002268 type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp'
2269
2270 if net.get("implementation"): # for v0.3
2271 if type_ == "bridge" and net["implementation"] == "underlay":
2272 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2273 "'network':'{}'".format(net_name)
2274 # logger.debug(error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002275 raise NfvoException(error_text, httperrors.Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002276 elif type_ != "bridge" and net["implementation"] == "overlay":
2277 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2278 "'network':'{}'".format(net_name)
2279 # logger.debug(error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002280 raise NfvoException(error_text, httperrors.Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002281 net.pop("implementation")
2282 if "type" in net and version == "0.3": # for v0.3
2283 if type_ == "data" and net["type"] == "e-line":
2284 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\
2285 "'e-line' at 'network':'{}'".format(net_name)
2286 # logger.debug(error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002287 raise NfvoException(error_text, httperrors.Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002288 elif type_ == "ptp" and net["type"] == "e-lan":
2289 type_ = "data"
2290
tierno7edb6752016-03-21 17:37:52 +01002291 net['type'] = type_
2292 net['name'] = net_name
2293 net['external'] = net.get('external', False)
2294
tierno5bb59dc2017-02-13 14:53:54 +01002295 # 3: insert at database
tierno7edb6752016-03-21 17:37:52 +01002296 scenario["nets"] = scenario["networks"]
2297 scenario['tenant_id'] = tenant_id
tierno5bb59dc2017-02-13 14:53:54 +01002298 scenario_id = mydb.new_scenario(scenario)
tiernof97fd272016-07-11 14:32:37 +02002299 return scenario_id
tierno7edb6752016-03-21 17:37:52 +01002300
tiernob3d36742017-03-03 23:51:05 +01002301
tiernof1ba57e2017-09-07 12:23:19 +02002302def new_nsd_v3(mydb, tenant_id, nsd_descriptor):
2303 """
2304 Parses an OSM IM nsd_catalog and insert at DB
2305 :param mydb:
2306 :param tenant_id:
2307 :param nsd_descriptor:
Igor D.Ccaadc442017-11-06 12:48:48 +00002308 :return: The list of created NSD ids
tiernof1ba57e2017-09-07 12:23:19 +02002309 """
2310 try:
2311 mynsd = nsd_catalog.nsd()
tiernoa9550202017-09-22 13:31:35 +02002312 try:
tiernof6bbe222019-04-09 14:19:40 +00002313 pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd, skip_unknown=True)
tiernoa9550202017-09-22 13:31:35 +02002314 except Exception as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002315 raise NfvoException("Error. Invalid NS descriptor format: " + str(e), httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002316 db_scenarios = []
2317 db_sce_nets = []
2318 db_sce_vnfs = []
2319 db_sce_interfaces = []
Igor D.Ccaadc442017-11-06 12:48:48 +00002320 db_sce_vnffgs = []
2321 db_sce_rsps = []
2322 db_sce_rsp_hops = []
2323 db_sce_classifiers = []
2324 db_sce_classifier_matches = []
tiernof1ba57e2017-09-07 12:23:19 +02002325 db_ip_profiles = []
2326 db_ip_profiles_index = 0
2327 uuid_list = []
2328 nsd_uuid_list = []
tiernob2880eb2017-10-04 15:04:53 +02002329 for nsd_yang in mynsd.nsd_catalog.nsd.itervalues():
2330 nsd = nsd_yang.get()
tiernof1ba57e2017-09-07 12:23:19 +02002331
Igor D.Ccaadc442017-11-06 12:48:48 +00002332 # table scenarios
tiernof1ba57e2017-09-07 12:23:19 +02002333 scenario_uuid = str(uuid4())
2334 uuid_list.append(scenario_uuid)
2335 nsd_uuid_list.append(scenario_uuid)
2336 db_scenario = {
2337 "uuid": scenario_uuid,
2338 "osm_id": get_str(nsd, "id", 255),
2339 "name": get_str(nsd, "name", 255),
2340 "description": get_str(nsd, "description", 255),
2341 "tenant_id": tenant_id,
2342 "vendor": get_str(nsd, "vendor", 255),
2343 "short_name": get_str(nsd, "short-name", 255),
2344 "descriptor": str(nsd_descriptor)[:60000],
2345 }
2346 db_scenarios.append(db_scenario)
2347
2348 # table sce_vnfs (constituent-vnfd)
2349 vnf_index2scevnf_uuid = {}
2350 vnf_index2vnf_uuid = {}
2351 for vnf in nsd.get("constituent-vnfd").itervalues():
2352 existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255],
2353 'tenant_id': tenant_id})
2354 if not existing_vnf:
tiernob2880eb2017-10-04 15:04:53 +02002355 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2356 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2357 str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002358 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002359 sce_vnf_uuid = str(uuid4())
2360 uuid_list.append(sce_vnf_uuid)
2361 db_sce_vnf = {
2362 "uuid": sce_vnf_uuid,
2363 "scenario_id": scenario_uuid,
tierno92c36fd2018-05-04 12:21:10 +02002364 # "name": get_str(vnf, "member-vnf-index", 255),
2365 "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 50),
tiernof1ba57e2017-09-07 12:23:19 +02002366 "vnf_id": existing_vnf[0]["uuid"],
tierno16e3dd42018-04-24 12:52:40 +02002367 "member_vnf_index": str(vnf["member-vnf-index"]),
tiernof1ba57e2017-09-07 12:23:19 +02002368 # TODO 'start-by-default': True
2369 }
tierno16e3dd42018-04-24 12:52:40 +02002370 vnf_index2scevnf_uuid[str(vnf['member-vnf-index'])] = sce_vnf_uuid
2371 vnf_index2vnf_uuid[str(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"]
tiernof1ba57e2017-09-07 12:23:19 +02002372 db_sce_vnfs.append(db_sce_vnf)
2373
2374 # table ip_profiles (ip-profiles)
2375 ip_profile_name2db_table_index = {}
2376 for ip_profile in nsd.get("ip-profiles").itervalues():
2377 db_ip_profile = {
2378 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
2379 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
2380 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
2381 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2382 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
2383 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
2384 }
2385 dns_list = []
2386 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
2387 dns_list.append(str(dns.get("address")))
2388 db_ip_profile["dns_address"] = ";".join(dns_list)
2389 if ip_profile["ip-profile-params"].get('security-group'):
2390 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
2391 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
2392 db_ip_profiles_index += 1
2393 db_ip_profiles.append(db_ip_profile)
2394
2395 # table sce_nets (internal-vld)
2396 for vld in nsd.get("vld").itervalues():
2397 sce_net_uuid = str(uuid4())
2398 uuid_list.append(sce_net_uuid)
2399 db_sce_net = {
2400 "uuid": sce_net_uuid,
2401 "name": get_str(vld, "name", 255),
2402 "scenario_id": scenario_uuid,
2403 # "type": #TODO
2404 "multipoint": not vld.get("type") == "ELINE",
tierno1df468d2018-07-06 14:25:16 +02002405 "osm_id": get_str(vld, "id", 255),
tiernof1ba57e2017-09-07 12:23:19 +02002406 # "external": #TODO
2407 "description": get_str(vld, "description", 255),
2408 }
2409 # guess type of network
2410 if vld.get("mgmt-network"):
2411 db_sce_net["type"] = "bridge"
2412 db_sce_net["external"] = True
2413 elif vld.get("provider-network").get("overlay-type") == "VLAN":
2414 db_sce_net["type"] = "data"
2415 else:
tierno66eba6e2017-11-10 17:09:18 +01002416 # later on it will be fixed to bridge or data depending on the type of interfaces attached to it
2417 db_sce_net["type"] = None
tiernof1ba57e2017-09-07 12:23:19 +02002418 db_sce_nets.append(db_sce_net)
2419
2420 # ip-profile, link db_ip_profile with db_sce_net
2421 if vld.get("ip-profile-ref"):
2422 ip_profile_name = vld.get("ip-profile-ref")
2423 if ip_profile_name not in ip_profile_name2db_table_index:
tiernob2880eb2017-10-04 15:04:53 +02002424 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2425 " Reference to a non-existing 'ip_profiles'".format(
2426 str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002427 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002428 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["sce_net_id"] = sce_net_uuid
tierno8f79ea12018-05-03 17:37:40 +02002429 elif vld.get("vim-network-name"):
2430 db_sce_net["vim_network_name"] = get_str(vld, "vim-network-name", 255)
tiernof1ba57e2017-09-07 12:23:19 +02002431
calvinosanch0aa0e2f2019-11-07 11:46:38 +01002432
tiernof1ba57e2017-09-07 12:23:19 +02002433 # table sce_interfaces (vld:vnfd-connection-point-ref)
2434 for iface in vld.get("vnfd-connection-point-ref").itervalues():
calvinosanch0aa0e2f2019-11-07 11:46:38 +01002435 # Check if there are VDUs in the descriptor
tierno16e3dd42018-04-24 12:52:40 +02002436 vnf_index = str(iface['member-vnf-index-ref'])
calvinosanch0aa0e2f2019-11-07 11:46:38 +01002437 existing_vdus = mydb.get_rows(SELECT=('vms.uuid'), FROM="vms", WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index]})
2438 if existing_vdus:
2439 # check correct parameters
2440 if vnf_index not in vnf_index2vnf_uuid:
2441 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2442 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2443 "'nsd':'constituent-vnfd'".format(
2444 str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])),
2445 httperrors.Bad_Request)
2446
2447 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid', 'i.type as iface_type'),
2448 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2449 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2450 'external_name': get_str(iface, "vnfd-connection-point-ref",
2451 255)})
2452 if not existing_ifaces:
2453 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2454 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2455 "connection-point name at VNFD '{}'".format(
2456 str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]),
2457 str(iface.get("vnfd-id-ref"))[:255]),
2458 httperrors.Bad_Request)
2459 interface_uuid = existing_ifaces[0]["uuid"]
2460 if existing_ifaces[0]["iface_type"] == "data":
2461 db_sce_net["type"] = "data"
2462 sce_interface_uuid = str(uuid4())
2463 uuid_list.append(sce_net_uuid)
2464 iface_ip_address = None
2465 if iface.get("ip-address"):
2466 iface_ip_address = str(iface.get("ip-address"))
2467 db_sce_interface = {
2468 "uuid": sce_interface_uuid,
2469 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2470 "sce_net_id": sce_net_uuid,
2471 "interface_id": interface_uuid,
2472 "ip_address": iface_ip_address,
2473 }
2474 db_sce_interfaces.append(db_sce_interface)
2475 if not db_sce_net["type"]:
2476 db_sce_net["type"] = "bridge"
tiernof1ba57e2017-09-07 12:23:19 +02002477
Igor D.Ccaadc442017-11-06 12:48:48 +00002478 # table sce_vnffgs (vnffgd)
2479 for vnffg in nsd.get("vnffgd").itervalues():
2480 sce_vnffg_uuid = str(uuid4())
2481 uuid_list.append(sce_vnffg_uuid)
2482 db_sce_vnffg = {
2483 "uuid": sce_vnffg_uuid,
2484 "name": get_str(vnffg, "name", 255),
2485 "scenario_id": scenario_uuid,
2486 "vendor": get_str(vnffg, "vendor", 255),
2487 "description": get_str(vld, "description", 255),
2488 }
2489 db_sce_vnffgs.append(db_sce_vnffg)
2490
2491 # deal with rsps
Igor D.Ccaadc442017-11-06 12:48:48 +00002492 for rsp in vnffg.get("rsp").itervalues():
2493 sce_rsp_uuid = str(uuid4())
2494 uuid_list.append(sce_rsp_uuid)
2495 db_sce_rsp = {
2496 "uuid": sce_rsp_uuid,
2497 "name": get_str(rsp, "name", 255),
2498 "sce_vnffg_id": sce_vnffg_uuid,
2499 "id": get_str(rsp, "id", 255), # only useful to link with classifiers; will be removed later in the code
2500 }
2501 db_sce_rsps.append(db_sce_rsp)
Igor D.Ccaadc442017-11-06 12:48:48 +00002502 for iface in rsp.get("vnfd-connection-point-ref").itervalues():
tierno16e3dd42018-04-24 12:52:40 +02002503 vnf_index = str(iface['member-vnf-index-ref'])
Igor D.Ccaadc442017-11-06 12:48:48 +00002504 if_order = int(iface['order'])
2505 # check correct parameters
2506 if vnf_index not in vnf_index2vnf_uuid:
2507 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2508 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2509 "'nsd':'constituent-vnfd'".format(
2510 str(nsd["id"]), str(rsp["id"]), str(iface["member-vnf-index-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002511 httperrors.Bad_Request)
Igor D.Ccaadc442017-11-06 12:48:48 +00002512
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002513 ingress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2514 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2515 WHERE={
2516 'vnf_id': vnf_index2vnf_uuid[vnf_index],
2517 'external_name': get_str(iface, "vnfd-ingress-connection-point-ref",
2518 255)})
2519 if not ingress_existing_ifaces:
Igor D.Ccaadc442017-11-06 12:48:48 +00002520 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002521 "-ref':'vnfd-ingress-connection-point-ref':'{}'. Reference to a non-existing "
Igor D.Ccaadc442017-11-06 12:48:48 +00002522 "connection-point name at VNFD '{}'".format(
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002523 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-ingress-connection-point-ref"]),
2524 str(iface.get("vnfd-id-ref"))[:255]), httperrors.Bad_Request)
2525
2526 egress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2527 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2528 WHERE={
2529 'vnf_id': vnf_index2vnf_uuid[vnf_index],
2530 'external_name': get_str(iface, "vnfd-egress-connection-point-ref",
2531 255)})
2532 if not egress_existing_ifaces:
2533 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2534 "-ref':'vnfd-egress-connection-point-ref':'{}'. Reference to a non-existing "
2535 "connection-point name at VNFD '{}'".format(
2536 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-egress-connection-point-ref"]),
2537 str(iface.get("vnfd-id-ref"))[:255]), HTTP_Bad_Request)
2538
2539 ingress_interface_uuid = ingress_existing_ifaces[0]["uuid"]
2540 egress_interface_uuid = egress_existing_ifaces[0]["uuid"]
Igor D.Ccaadc442017-11-06 12:48:48 +00002541 sce_rsp_hop_uuid = str(uuid4())
2542 uuid_list.append(sce_rsp_hop_uuid)
2543 db_sce_rsp_hop = {
2544 "uuid": sce_rsp_hop_uuid,
2545 "if_order": if_order,
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002546 "ingress_interface_id": ingress_interface_uuid,
2547 "egress_interface_id": egress_interface_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00002548 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2549 "sce_rsp_id": sce_rsp_uuid,
2550 }
2551 db_sce_rsp_hops.append(db_sce_rsp_hop)
2552
2553 # deal with classifiers
Igor D.Ccaadc442017-11-06 12:48:48 +00002554 for classifier in vnffg.get("classifier").itervalues():
2555 sce_classifier_uuid = str(uuid4())
2556 uuid_list.append(sce_classifier_uuid)
2557
2558 # source VNF
tierno16e3dd42018-04-24 12:52:40 +02002559 vnf_index = str(classifier['member-vnf-index-ref'])
Igor D.Ccaadc442017-11-06 12:48:48 +00002560 if vnf_index not in vnf_index2vnf_uuid:
2561 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point"
2562 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2563 "'nsd':'constituent-vnfd'".format(
2564 str(nsd["id"]), str(classifier["id"]), str(classifier["member-vnf-index-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002565 httperrors.Bad_Request)
Igor D.Ccaadc442017-11-06 12:48:48 +00002566 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2567 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2568 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2569 'external_name': get_str(classifier, "vnfd-connection-point-ref",
2570 255)})
2571 if not existing_ifaces:
2572 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2573 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2574 "connection-point name at VNFD '{}'".format(
2575 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]),
2576 str(iface.get("vnfd-id-ref"))[:255]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002577 httperrors.Bad_Request)
Igor D.Ccaadc442017-11-06 12:48:48 +00002578 interface_uuid = existing_ifaces[0]["uuid"]
2579
2580 db_sce_classifier = {
2581 "uuid": sce_classifier_uuid,
2582 "name": get_str(classifier, "name", 255),
2583 "sce_vnffg_id": sce_vnffg_uuid,
2584 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2585 "interface_id": interface_uuid,
2586 }
2587 rsp_id = get_str(classifier, "rsp-id-ref", 255)
2588 rsp = next((item for item in db_sce_rsps if item["id"] == rsp_id), None)
2589 db_sce_classifier["sce_rsp_id"] = rsp["uuid"]
2590 db_sce_classifiers.append(db_sce_classifier)
2591
Igor D.Ccaadc442017-11-06 12:48:48 +00002592 for match in classifier.get("match-attributes").itervalues():
2593 sce_classifier_match_uuid = str(uuid4())
2594 uuid_list.append(sce_classifier_match_uuid)
2595 db_sce_classifier_match = {
2596 "uuid": sce_classifier_match_uuid,
2597 "ip_proto": get_str(match, "ip-proto", 2),
2598 "source_ip": get_str(match, "source-ip-address", 16),
2599 "destination_ip": get_str(match, "destination-ip-address", 16),
2600 "source_port": get_str(match, "source-port", 5),
2601 "destination_port": get_str(match, "destination-port", 5),
2602 "sce_classifier_id": sce_classifier_uuid,
2603 }
2604 db_sce_classifier_matches.append(db_sce_classifier_match)
2605 # TODO: vnf/cp keys
2606
2607 # remove unneeded id's in sce_rsps
2608 for rsp in db_sce_rsps:
2609 rsp.pop('id')
2610
tiernof1ba57e2017-09-07 12:23:19 +02002611 db_tables = [
2612 {"scenarios": db_scenarios},
2613 {"sce_nets": db_sce_nets},
2614 {"ip_profiles": db_ip_profiles},
2615 {"sce_vnfs": db_sce_vnfs},
2616 {"sce_interfaces": db_sce_interfaces},
Igor D.Ccaadc442017-11-06 12:48:48 +00002617 {"sce_vnffgs": db_sce_vnffgs},
2618 {"sce_rsps": db_sce_rsps},
2619 {"sce_rsp_hops": db_sce_rsp_hops},
2620 {"sce_classifiers": db_sce_classifiers},
2621 {"sce_classifier_matches": db_sce_classifier_matches},
tiernof1ba57e2017-09-07 12:23:19 +02002622 ]
2623
Igor D.Ccaadc442017-11-06 12:48:48 +00002624 logger.debug("new_nsd_v3 done: %s",
tiernof1ba57e2017-09-07 12:23:19 +02002625 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
2626 mydb.new_rows(db_tables, uuid_list)
2627 return nsd_uuid_list
tiernob2880eb2017-10-04 15:04:53 +02002628 except NfvoException:
2629 raise
tiernof1ba57e2017-09-07 12:23:19 +02002630 except Exception as e:
2631 logger.error("Exception {}".format(e))
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002632 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002633
2634
tierno7edb6752016-03-21 17:37:52 +01002635def edit_scenario(mydb, tenant_id, scenario_id, data):
2636 data["uuid"] = scenario_id
2637 data["tenant_id"] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02002638 c = mydb.edit_scenario( data )
2639 return c
tierno7edb6752016-03-21 17:37:52 +01002640
tiernob3d36742017-03-03 23:51:05 +01002641
tiernob8569aa2018-08-24 11:34:54 +02002642@deprecated("Use create_instance")
tierno7edb6752016-03-21 17:37:52 +01002643def 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 +02002644 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00002645 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
2646 vims = {datacenter_id: myvim}
tierno392f2852016-05-13 12:28:55 +02002647 myvim_tenant = myvim['tenant_id']
tierno7edb6752016-03-21 17:37:52 +01002648 datacenter_name = myvim['name']
tiernoa2793912016-10-04 08:15:08 +00002649
tierno7edb6752016-03-21 17:37:52 +01002650 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02002651 try:
2652 #print "Checking that the scenario_id exists and getting the scenario dictionary"
tierno868220c2017-09-26 00:11:05 +02002653 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id)
tiernoa2793912016-10-04 08:15:08 +00002654 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
tiernoae4a8d12016-07-08 12:30:39 +02002655 scenarioDict['datacenter_id'] = datacenter_id
2656 #print '================scenarioDict======================='
2657 #print json.dumps(scenarioDict, indent=4)
2658 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
tierno42026a02017-02-10 15:13:40 +01002659
tiernoae4a8d12016-07-08 12:30:39 +02002660 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
2661 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002662
tiernoae4a8d12016-07-08 12:30:39 +02002663 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2664 auxNetDict['scenario'] = {}
tierno42026a02017-02-10 15:13:40 +01002665
tiernoae4a8d12016-07-08 12:30:39 +02002666 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2667 for sce_net in scenarioDict['nets']:
2668 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
tierno42026a02017-02-10 15:13:40 +01002669
tiernoae4a8d12016-07-08 12:30:39 +02002670 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
tierno7edb6752016-03-21 17:37:52 +01002671 myNetName = myNetName[0:255] #limit length
tiernoae4a8d12016-07-08 12:30:39 +02002672 myNetType = sce_net['type']
tierno7edb6752016-03-21 17:37:52 +01002673 myNetDict = {}
2674 myNetDict["name"] = myNetName
2675 myNetDict["type"] = myNetType
2676 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02002677 myNetIPProfile = sce_net.get('ip_profile', None)
tierno7edb6752016-03-21 17:37:52 +01002678 #TODO:
tiernoae4a8d12016-07-08 12:30:39 +02002679 #We should use the dictionary as input parameter for new_network
tiernof97fd272016-07-11 14:32:37 +02002680 #print myNetDict
tiernoae4a8d12016-07-08 12:30:39 +02002681 if not sce_net["external"]:
garciadeblasebd66722019-01-31 16:01:31 +00002682 network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02002683 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2684 sce_net['vim_id'] = network_id
2685 auxNetDict['scenario'][sce_net['uuid']] = network_id
2686 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002687 sce_net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02002688 else:
2689 if sce_net['vim_id'] == None:
2690 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
2691 _, message = rollback(mydb, vims, rollbackList)
2692 logger.error("nfvo.start_scenario: %s", error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002693 raise NfvoException(error_text, httperrors.Bad_Request)
tiernoae4a8d12016-07-08 12:30:39 +02002694 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
2695 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
tierno42026a02017-02-10 15:13:40 +01002696
tiernoae4a8d12016-07-08 12:30:39 +02002697 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2698 #For each vnf net, we create it and we add it to instanceNetlist.
mirabal29356312017-07-27 12:21:22 +02002699
tiernoae4a8d12016-07-08 12:30:39 +02002700 for sce_vnf in scenarioDict['vnfs']:
2701 for net in sce_vnf['nets']:
2702 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
tierno42026a02017-02-10 15:13:40 +01002703
tiernoae4a8d12016-07-08 12:30:39 +02002704 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
2705 myNetName = myNetName[0:255] #limit length
2706 myNetType = net['type']
2707 myNetDict = {}
2708 myNetDict["name"] = myNetName
2709 myNetDict["type"] = myNetType
2710 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02002711 myNetIPProfile = net.get('ip_profile', None)
tiernoae4a8d12016-07-08 12:30:39 +02002712 #print myNetDict
2713 #TODO:
2714 #We should use the dictionary as input parameter for new_network
garciadeblasebd66722019-01-31 16:01:31 +00002715 network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02002716 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2717 net['vim_id'] = network_id
2718 if sce_vnf['uuid'] not in auxNetDict:
2719 auxNetDict[sce_vnf['uuid']] = {}
2720 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
2721 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002722 net["created"] = True
tierno42026a02017-02-10 15:13:40 +01002723
tiernoae4a8d12016-07-08 12:30:39 +02002724 #print "auxNetDict:"
2725 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002726
tiernoae4a8d12016-07-08 12:30:39 +02002727 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
2728 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2729 i = 0
2730 for sce_vnf in scenarioDict['vnfs']:
tierno5a3273c2017-08-29 11:43:46 +02002731 vnf_availability_zones = []
mirabal29356312017-07-27 12:21:22 +02002732 for vm in sce_vnf['vms']:
2733 vm_av = vm.get('availability_zone')
tierno5a3273c2017-08-29 11:43:46 +02002734 if vm_av and vm_av not in vnf_availability_zones:
2735 vnf_availability_zones.append(vm_av)
2736
2737 # check if there is enough availability zones available at vim level.
2738 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
2739 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002740 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors.Bad_Request)
tierno5a3273c2017-08-29 11:43:46 +02002741
tiernoae4a8d12016-07-08 12:30:39 +02002742 for vm in sce_vnf['vms']:
2743 i += 1
2744 myVMDict = {}
2745 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
tiernoae65a482016-11-24 16:20:05 +01002746 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
tiernoae4a8d12016-07-08 12:30:39 +02002747 #myVMDict['description'] = vm['description']
2748 myVMDict['description'] = myVMDict['name'][0:99]
2749 if not startvms:
2750 myVMDict['start'] = "no"
2751 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2752 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
tierno42026a02017-02-10 15:13:40 +01002753
tiernoae4a8d12016-07-08 12:30:39 +02002754 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002755 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno42026a02017-02-10 15:13:40 +01002756 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02002757 vm['vim_image_id'] = image_id
tierno42026a02017-02-10 15:13:40 +01002758
tiernoae4a8d12016-07-08 12:30:39 +02002759 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002760 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tiernoae4a8d12016-07-08 12:30:39 +02002761 if flavor_dict['extended']!=None:
2762 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
tierno42026a02017-02-10 15:13:40 +01002763 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02002764 vm['vim_flavor_id'] = flavor_id
tierno42026a02017-02-10 15:13:40 +01002765
2766
tiernoae4a8d12016-07-08 12:30:39 +02002767 myVMDict['imageRef'] = vm['vim_image_id']
2768 myVMDict['flavorRef'] = vm['vim_flavor_id']
2769 myVMDict['networks'] = []
2770 for iface in vm['interfaces']:
2771 netDict = {}
2772 if iface['type']=="data":
2773 netDict['type'] = iface['model']
2774 elif "model" in iface and iface["model"]!=None:
2775 netDict['model']=iface['model']
2776 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2777 #discover type of interface looking at flavor
2778 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2779 for flavor_iface in numa.get('interfaces',[]):
2780 if flavor_iface.get('name') == iface['internal_name']:
2781 if flavor_iface['dedicated'] == 'yes':
2782 netDict['type']="PF" #passthrough
2783 elif flavor_iface['dedicated'] == 'no':
2784 netDict['type']="VF" #siov
2785 elif flavor_iface['dedicated'] == 'yes:sriov':
2786 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2787 netDict["mac_address"] = flavor_iface.get("mac_address")
2788 break;
2789 netDict["use"]=iface['type']
2790 if netDict["use"]=="data" and not netDict.get("type"):
2791 #print "netDict", netDict
2792 #print "iface", iface
2793 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'])
2794 if flavor_dict.get('extended')==None:
tiernof97fd272016-07-11 14:32:37 +02002795 raise NfvoException(e_text + "After database migration some information is not available. \
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002796 Try to delete and create the scenarios and VNFs again", httperrors.Conflict)
tiernoae4a8d12016-07-08 12:30:39 +02002797 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002798 raise NfvoException(e_text, httperrors.Internal_Server_Error)
tiernoae4a8d12016-07-08 12:30:39 +02002799 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2800 netDict["type"]="virtual"
2801 if "vpci" in iface and iface["vpci"] is not None:
2802 netDict['vpci'] = iface['vpci']
2803 if "mac" in iface and iface["mac"] is not None:
2804 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00002805 if "port-security" in iface and iface["port-security"] is not None:
2806 netDict['port_security'] = iface['port-security']
2807 if "floating-ip" in iface and iface["floating-ip"] is not None:
2808 netDict['floating_ip'] = iface['floating-ip']
tiernoae4a8d12016-07-08 12:30:39 +02002809 netDict['name'] = iface['internal_name']
2810 if iface['net_id'] is None:
2811 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02002812 #print iface
2813 #print vnf_iface
tiernoae4a8d12016-07-08 12:30:39 +02002814 if vnf_iface['interface_id']==iface['uuid']:
2815 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
2816 break
2817 else:
2818 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
2819 #skip bridge ifaces not connected to any net
2820 #if 'net_id' not in netDict or netDict['net_id']==None:
2821 # continue
2822 myVMDict['networks'].append(netDict)
2823 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2824 #print myVMDict['name']
2825 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2826 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2827 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
mirabal29356312017-07-27 12:21:22 +02002828
2829 if 'availability_zone' in myVMDict:
tierno5a3273c2017-08-29 11:43:46 +02002830 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
mirabal29356312017-07-27 12:21:22 +02002831 else:
tierno5a3273c2017-08-29 11:43:46 +02002832 av_index = None
mirabal29356312017-07-27 12:21:22 +02002833
tierno98e909c2017-10-14 13:27:03 +02002834 vm_id, _ = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None),
mirabal29356312017-07-27 12:21:22 +02002835 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'],
tierno5a3273c2017-08-29 11:43:46 +02002836 availability_zone_index=av_index,
2837 availability_zone_list=vnf_availability_zones)
tiernoae4a8d12016-07-08 12:30:39 +02002838 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2839 vm['vim_id'] = vm_id
2840 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2841 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2842 for net in myVMDict['networks']:
2843 if "vim_id" in net:
2844 for iface in vm['interfaces']:
2845 if net["name"]==iface["internal_name"]:
2846 iface["vim_id"]=net["vim_id"]
2847 break
tierno42026a02017-02-10 15:13:40 +01002848
tiernoae4a8d12016-07-08 12:30:39 +02002849 logger.debug("start scenario Deployment done")
2850 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2851 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
tiernof97fd272016-07-11 14:32:37 +02002852 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
2853 return mydb.get_instance_scenario(instance_id)
tierno42026a02017-02-10 15:13:40 +01002854
tiernof97fd272016-07-11 14:32:37 +02002855 except (db_base_Exception, vimconn.vimconnException) as e:
tiernoae4a8d12016-07-08 12:30:39 +02002856 _, message = rollback(mydb, vims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02002857 if isinstance(e, db_base_Exception):
2858 error_text = "Exception at database"
2859 else:
2860 error_text = "Exception at VIM"
2861 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2862 #logger.error("start_scenario %s", error_text)
2863 raise NfvoException(error_text, e.http_code)
tierno7edb6752016-03-21 17:37:52 +01002864
tierno36c0b172017-01-12 18:32:28 +01002865def unify_cloud_config(cloud_config_preserve, cloud_config):
tierno40e1bce2017-08-09 09:12:04 +02002866 """ join the cloud config information into cloud_config_preserve.
tierno36c0b172017-01-12 18:32:28 +01002867 In case of conflict cloud_config_preserve preserves
tierno40e1bce2017-08-09 09:12:04 +02002868 None is allowed
2869 """
tierno36c0b172017-01-12 18:32:28 +01002870 if not cloud_config_preserve and not cloud_config:
2871 return None
2872
2873 new_cloud_config = {"key-pairs":[], "users":[]}
2874 # key-pairs
2875 if cloud_config_preserve:
2876 for key in cloud_config_preserve.get("key-pairs", () ):
2877 if key not in new_cloud_config["key-pairs"]:
2878 new_cloud_config["key-pairs"].append(key)
2879 if cloud_config:
2880 for key in cloud_config.get("key-pairs", () ):
2881 if key not in new_cloud_config["key-pairs"]:
2882 new_cloud_config["key-pairs"].append(key)
2883 if not new_cloud_config["key-pairs"]:
2884 del new_cloud_config["key-pairs"]
2885
2886 # users
2887 if cloud_config:
2888 new_cloud_config["users"] += cloud_config.get("users", () )
2889 if cloud_config_preserve:
2890 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
tiernoa4e1a6e2016-08-31 14:19:40 +02002891 index_to_delete = []
tierno36c0b172017-01-12 18:32:28 +01002892 users = new_cloud_config.get("users", [])
tiernoa4e1a6e2016-08-31 14:19:40 +02002893 for index0 in range(0,len(users)):
2894 if index0 in index_to_delete:
2895 continue
2896 for index1 in range(index0+1,len(users)):
2897 if index1 in index_to_delete:
2898 continue
2899 if users[index0]["name"] == users[index1]["name"]:
2900 index_to_delete.append(index1)
2901 for key in users[index1].get("key-pairs",()):
tierno36c0b172017-01-12 18:32:28 +01002902 if "key-pairs" not in users[index0]:
tiernoa4e1a6e2016-08-31 14:19:40 +02002903 users[index0]["key-pairs"] = [key]
2904 elif key not in users[index0]["key-pairs"]:
2905 users[index0]["key-pairs"].append(key)
2906 index_to_delete.sort(reverse=True)
2907 for index in index_to_delete:
2908 del users[index]
tierno36c0b172017-01-12 18:32:28 +01002909 if not new_cloud_config["users"]:
2910 del new_cloud_config["users"]
2911
2912 #boot-data-drive
2913 if cloud_config and cloud_config.get("boot-data-drive") != None:
2914 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
2915 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
2916 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
2917
2918 # user-data
tierno40e1bce2017-08-09 09:12:04 +02002919 new_cloud_config["user-data"] = []
2920 if cloud_config and cloud_config.get("user-data"):
2921 if isinstance(cloud_config["user-data"], list):
2922 new_cloud_config["user-data"] += cloud_config["user-data"]
2923 else:
2924 new_cloud_config["user-data"].append(cloud_config["user-data"])
2925 if cloud_config_preserve and cloud_config_preserve.get("user-data"):
2926 if isinstance(cloud_config_preserve["user-data"], list):
2927 new_cloud_config["user-data"] += cloud_config_preserve["user-data"]
2928 else:
2929 new_cloud_config["user-data"].append(cloud_config_preserve["user-data"])
2930 if not new_cloud_config["user-data"]:
2931 del new_cloud_config["user-data"]
tierno36c0b172017-01-12 18:32:28 +01002932
2933 # config files
2934 new_cloud_config["config-files"] = []
2935 if cloud_config and cloud_config.get("config-files") != None:
2936 new_cloud_config["config-files"] += cloud_config["config-files"]
2937 if cloud_config_preserve:
2938 for file in cloud_config_preserve.get("config-files", ()):
2939 for index in range(0, len(new_cloud_config["config-files"])):
2940 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
2941 new_cloud_config["config-files"][index] = file
2942 break
2943 else:
2944 new_cloud_config["config-files"].append(file)
2945 if not new_cloud_config["config-files"]:
2946 del new_cloud_config["config-files"]
2947 return new_cloud_config
2948
2949
tierno867ffe92017-03-27 12:50:34 +02002950def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
tiernob3d36742017-03-03 23:51:05 +01002951 datacenter_id = None
2952 datacenter_name = None
2953 thread = None
tierno867ffe92017-03-27 12:50:34 +02002954 try:
2955 if datacenter_tenant_id:
2956 thread_id = datacenter_tenant_id
2957 thread = vim_threads["running"].get(datacenter_tenant_id)
tiernob3d36742017-03-03 23:51:05 +01002958 else:
tierno867ffe92017-03-27 12:50:34 +02002959 where_={"td.nfvo_tenant_id": tenant_id}
2960 if datacenter_id_name:
2961 if utils.check_valid_uuid(datacenter_id_name):
2962 datacenter_id = datacenter_id_name
2963 where_["dt.datacenter_id"] = datacenter_id
2964 else:
2965 datacenter_name = datacenter_id_name
2966 where_["d.name"] = datacenter_name
2967 if datacenter_tenant_id:
2968 where_["dt.uuid"] = datacenter_tenant_id
2969 datacenters = mydb.get_rows(
2970 SELECT=("dt.uuid as datacenter_tenant_id",),
2971 FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2972 "join datacenters as d on d.uuid=dt.datacenter_id",
2973 WHERE=where_)
2974 if len(datacenters) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002975 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
tierno867ffe92017-03-27 12:50:34 +02002976 elif datacenters:
2977 thread_id = datacenters[0]["datacenter_tenant_id"]
2978 thread = vim_threads["running"].get(thread_id)
2979 if not thread:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002980 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found)
tierno867ffe92017-03-27 12:50:34 +02002981 return thread_id, thread
2982 except db_base_Exception as e:
2983 raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code)
tiernoa4e1a6e2016-08-31 14:19:40 +02002984
tiernof5755962017-07-13 15:44:34 +02002985
tiernoa15c4b92017-10-05 12:41:44 +02002986def get_datacenter_uuid(mydb, tenant_id, datacenter_id_name):
2987 WHERE_dict={}
2988 if utils.check_valid_uuid(datacenter_id_name):
2989 WHERE_dict['d.uuid'] = datacenter_id_name
2990 else:
2991 WHERE_dict['d.name'] = datacenter_id_name
2992
2993 if tenant_id:
2994 WHERE_dict['nfvo_tenant_id'] = tenant_id
2995 from_= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
2996 " dt on td.datacenter_tenant_id=dt.uuid"
2997 else:
2998 from_ = 'datacenters as d'
tiernod3750b32018-07-20 15:33:08 +02002999 vimaccounts = mydb.get_rows(FROM=from_, SELECT=("d.uuid as uuid, d.name as name",), WHERE=WHERE_dict )
tiernoa15c4b92017-10-05 12:41:44 +02003000 if len(vimaccounts) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003001 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found)
tiernoa15c4b92017-10-05 12:41:44 +02003002 elif len(vimaccounts)>1:
3003 #print "nfvo.datacenter_action() error. Several datacenters found"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003004 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
tiernod3750b32018-07-20 15:33:08 +02003005 return vimaccounts[0]["uuid"], vimaccounts[0]["name"]
tiernoa15c4b92017-10-05 12:41:44 +02003006
3007
tiernoa2793912016-10-04 08:15:08 +00003008def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
tiernobe41e222016-09-02 15:16:13 +02003009 datacenter_id = None
3010 datacenter_name = None
3011 if datacenter_id_name:
tierno42026a02017-02-10 15:13:40 +01003012 if utils.check_valid_uuid(datacenter_id_name):
tiernobe41e222016-09-02 15:16:13 +02003013 datacenter_id = datacenter_id_name
3014 else:
3015 datacenter_name = datacenter_id_name
tiernoa2793912016-10-04 08:15:08 +00003016 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
tiernobe41e222016-09-02 15:16:13 +02003017 if len(vims) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003018 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found)
tiernobe41e222016-09-02 15:16:13 +02003019 elif len(vims)>1:
3020 #print "nfvo.datacenter_action() error. Several datacenters found"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003021 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
tiernobe41e222016-09-02 15:16:13 +02003022 return vims.keys()[0], vims.values()[0]
3023
tiernob3d36742017-03-03 23:51:05 +01003024
garciadeblas9f8456e2016-09-05 05:02:59 +02003025def update(d, u):
Eduardo Sousa16cfd562018-11-30 15:33:35 +00003026 """Takes dict d and updates it with the values in dict u.
3027 It merges all depth levels"""
garciadeblas9f8456e2016-09-05 05:02:59 +02003028 for k, v in u.iteritems():
3029 if isinstance(v, collections.Mapping):
3030 r = update(d.get(k, {}), v)
3031 d[k] = r
3032 else:
3033 d[k] = u[k]
3034 return d
3035
tierno16e3dd42018-04-24 12:52:40 +02003036
tierno7edb6752016-03-21 17:37:52 +01003037def create_instance(mydb, tenant_id, instance_dict):
tiernob3d36742017-03-03 23:51:05 +01003038 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
3039 # logger.debug("Creating instance...")
tierno7edb6752016-03-21 17:37:52 +01003040 scenario = instance_dict["scenario"]
tierno42026a02017-02-10 15:13:40 +01003041
tierno868220c2017-09-26 00:11:05 +02003042 # find main datacenter
tiernobe41e222016-09-02 15:16:13 +02003043 myvims = {}
tierno867ffe92017-03-27 12:50:34 +02003044 myvim_threads_id = {}
tierno7edb6752016-03-21 17:37:52 +01003045 datacenter = instance_dict.get("datacenter")
tiernofc7cfbf2019-03-20 17:23:45 +00003046 default_wim_account = instance_dict.get("wim_account")
tiernobe41e222016-09-02 15:16:13 +02003047 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3048 myvims[default_datacenter_id] = vim
tierno867ffe92017-03-27 12:50:34 +02003049 myvim_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id)
gcalvinoe580c7d2017-09-22 14:09:51 +02003050 tenant = mydb.get_rows_by_id('nfvo_tenants', tenant_id)
tierno868220c2017-09-26 00:11:05 +02003051 # myvim_tenant = myvim['tenant_id']
tierno16e3dd42018-04-24 12:52:40 +02003052 rollbackList = []
tierno42026a02017-02-10 15:13:40 +01003053
tierno868220c2017-09-26 00:11:05 +02003054 # print "Checking that the scenario exists and getting the scenario dictionary"
tierno7fe82642018-11-26 14:14:51 +00003055 if isinstance(scenario, str):
3056 scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_vim_id=myvim_threads_id[default_datacenter_id],
3057 datacenter_id=default_datacenter_id)
3058 else:
3059 scenarioDict = scenario
3060 scenarioDict["uuid"] = None
tierno42026a02017-02-10 15:13:40 +01003061
tierno868220c2017-09-26 00:11:05 +02003062 # logger.debug(">>>>>> Dictionaries before merging")
3063 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
3064 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
tierno42026a02017-02-10 15:13:40 +01003065
tierno868220c2017-09-26 00:11:05 +02003066 db_instance_vnfs = []
3067 db_instance_vms = []
3068 db_instance_interfaces = []
Igor D.Ccaadc442017-11-06 12:48:48 +00003069 db_instance_sfis = []
3070 db_instance_sfs = []
3071 db_instance_classifications = []
3072 db_instance_sfps = []
tierno868220c2017-09-26 00:11:05 +02003073 db_ip_profiles = []
3074 db_vim_actions = []
tierno8e690322017-08-10 15:58:50 +02003075 uuid_list = []
tierno868220c2017-09-26 00:11:05 +02003076 task_index = 0
tierno8e690322017-08-10 15:58:50 +02003077 instance_name = instance_dict["name"]
3078 instance_uuid = str(uuid4())
3079 uuid_list.append(instance_uuid)
3080 db_instance_scenario = {
3081 "uuid": instance_uuid,
3082 "name": instance_name,
3083 "tenant_id": tenant_id,
3084 "scenario_id": scenarioDict['uuid'],
3085 "datacenter_id": default_datacenter_id,
3086 # filled bellow 'datacenter_tenant_id'
3087 "description": instance_dict.get("description"),
3088 }
tierno8e690322017-08-10 15:58:50 +02003089 if scenarioDict.get("cloud-config"):
3090 db_instance_scenario["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"],
3091 default_flow_style=True, width=256)
tierno868220c2017-09-26 00:11:05 +02003092 instance_action_id = get_task_id()
3093 db_instance_action = {
3094 "uuid": instance_action_id, # same uuid for the instance and the action on create
3095 "tenant_id": tenant_id,
3096 "instance_id": instance_uuid,
3097 "description": "CREATE",
3098 }
garciadeblas9f8456e2016-09-05 05:02:59 +02003099
tierno868220c2017-09-26 00:11:05 +02003100 # Auxiliary dictionaries from x to y
tierno8e690322017-08-10 15:58:50 +02003101 sce_net2instance = {}
tierno868220c2017-09-26 00:11:05 +02003102 net2task_id = {'scenario': {}}
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003103 # Mapping between local networks and WIMs
3104 wim_usage = {}
tierno42026a02017-02-10 15:13:40 +01003105
tierno1df468d2018-07-06 14:25:16 +02003106 def ip_profile_IM2RO(ip_profile_im):
3107 # translate from input format to database format
3108 ip_profile_ro = {}
3109 if 'subnet-address' in ip_profile_im:
3110 ip_profile_ro['subnet_address'] = ip_profile_im['subnet-address']
3111 if 'ip-version' in ip_profile_im:
3112 ip_profile_ro['ip_version'] = ip_profile_im['ip-version']
3113 if 'gateway-address' in ip_profile_im:
3114 ip_profile_ro['gateway_address'] = ip_profile_im['gateway-address']
3115 if 'dns-address' in ip_profile_im:
3116 ip_profile_ro['dns_address'] = ip_profile_im['dns-address']
3117 if isinstance(ip_profile_ro['dns_address'], (list, tuple)):
3118 ip_profile_ro['dns_address'] = ";".join(ip_profile_ro['dns_address'])
3119 if 'dhcp' in ip_profile_im:
3120 ip_profile_ro['dhcp_start_address'] = ip_profile_im['dhcp'].get('start-address')
3121 ip_profile_ro['dhcp_enabled'] = ip_profile_im['dhcp'].get('enabled', True)
3122 ip_profile_ro['dhcp_count'] = ip_profile_im['dhcp'].get('count')
3123 return ip_profile_ro
3124
tierno868220c2017-09-26 00:11:05 +02003125 # logger.debug("Creating instance from scenario-dict:\n%s",
3126 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01003127 try:
tiernob3d36742017-03-03 23:51:05 +01003128 # 0 check correct parameters
tierno868220c2017-09-26 00:11:05 +02003129 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01003130 for scenario_net in scenarioDict['nets']:
tierno1df468d2018-07-06 14:25:16 +02003131 if net_name == scenario_net.get("name") or net_name == scenario_net.get("osm_id") or net_name == scenario_net.get("uuid"):
tierno7edb6752016-03-21 17:37:52 +01003132 break
tierno1df468d2018-07-06 14:25:16 +02003133 else:
3134 raise NfvoException("Invalid scenario network name or id '{}' at instance:networks".format(net_name),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003135 httperrors.Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02003136 if "sites" not in net_instance_desc:
3137 net_instance_desc["sites"] = [ {} ]
3138 site_without_datacenter_field = False
3139 for site in net_instance_desc["sites"]:
3140 if site.get("datacenter"):
tiernod3750b32018-07-20 15:33:08 +02003141 site["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, site["datacenter"])
tiernobe41e222016-09-02 15:16:13 +02003142 if site["datacenter"] not in myvims:
tierno868220c2017-09-26 00:11:05 +02003143 # Add this datacenter to myvims
tiernobe41e222016-09-02 15:16:13 +02003144 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
3145 myvims[d] = v
tierno868220c2017-09-26 00:11:05 +02003146 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, site["datacenter"])
3147 site["datacenter"] = d # change name to id
tiernobe41e222016-09-02 15:16:13 +02003148 else:
3149 if site_without_datacenter_field:
tierno868220c2017-09-26 00:11:05 +02003150 raise NfvoException("Found more than one entries without datacenter field at "
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003151 "instance:networks:{}:sites".format(net_name), httperrors.Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02003152 site_without_datacenter_field = True
tierno868220c2017-09-26 00:11:05 +02003153 site["datacenter"] = default_datacenter_id # change name to id
tierno42026a02017-02-10 15:13:40 +01003154
tiernobe41e222016-09-02 15:16:13 +02003155 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01003156 for scenario_vnf in scenarioDict['vnfs']:
tierno1df468d2018-07-06 14:25:16 +02003157 if vnf_name == scenario_vnf['member_vnf_index'] or vnf_name == scenario_vnf['uuid'] or vnf_name == scenario_vnf['name']:
tierno7edb6752016-03-21 17:37:52 +01003158 break
tierno1df468d2018-07-06 14:25:16 +02003159 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003160 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name), httperrors.Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02003161 if "datacenter" in vnf_instance_desc:
tierno868220c2017-09-26 00:11:05 +02003162 # Add this datacenter to myvims
tiernod3750b32018-07-20 15:33:08 +02003163 vnf_instance_desc["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
tiernobe41e222016-09-02 15:16:13 +02003164 if vnf_instance_desc["datacenter"] not in myvims:
3165 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
3166 myvims[d] = v
tierno868220c2017-09-26 00:11:05 +02003167 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"])
tiernoa2793912016-10-04 08:15:08 +00003168 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
garciadeblas30833382017-01-09 09:46:31 +01003169
tierno1df468d2018-07-06 14:25:16 +02003170 for net_id, net_instance_desc in vnf_instance_desc.get("networks", {}).iteritems():
3171 for scenario_net in scenario_vnf['nets']:
3172 if net_id == scenario_net['osm_id'] or net_id == scenario_net['uuid'] or net_id == scenario_net["name"]:
3173 break
3174 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003175 raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id), httperrors.Bad_Request)
tierno1df468d2018-07-06 14:25:16 +02003176 if net_instance_desc.get("vim-network-name"):
3177 scenario_net["vim-network-name"] = net_instance_desc["vim-network-name"]
gcalvino0a480542018-12-17 16:19:33 +01003178 if net_instance_desc.get("vim-network-id"):
3179 scenario_net["vim-network-id"] = net_instance_desc["vim-network-id"]
tierno1df468d2018-07-06 14:25:16 +02003180 if net_instance_desc.get("name"):
3181 scenario_net["name"] = net_instance_desc["name"]
3182 if 'ip-profile' in net_instance_desc:
3183 ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile'])
3184 if 'ip_profile' not in scenario_net:
3185 scenario_net['ip_profile'] = ipprofile_db
3186 else:
3187 update(scenario_net['ip_profile'], ipprofile_db)
3188
3189 for vdu_id, vdu_instance_desc in vnf_instance_desc.get("vdus", {}).iteritems():
3190 for scenario_vm in scenario_vnf['vms']:
3191 if vdu_id == scenario_vm['osm_id'] or vdu_id == scenario_vm["name"]:
3192 break
3193 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003194 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), httperrors.Bad_Request)
tierno1df468d2018-07-06 14:25:16 +02003195 scenario_vm["instance_parameters"] = vdu_instance_desc
3196 for iface_id, iface_instance_desc in vdu_instance_desc.get("interfaces", {}).iteritems():
3197 for scenario_interface in scenario_vm['interfaces']:
3198 if iface_id == scenario_interface['internal_name'] or iface_id == scenario_interface["external_name"]:
3199 scenario_interface.update(iface_instance_desc)
3200 break
3201 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003202 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), httperrors.Bad_Request)
tierno1df468d2018-07-06 14:25:16 +02003203
tierno868220c2017-09-26 00:11:05 +02003204 # 0.1 parse cloud-config parameters
tierno36c0b172017-01-12 18:32:28 +01003205 cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config"))
garciadeblas9f8456e2016-09-05 05:02:59 +02003206
tierno868220c2017-09-26 00:11:05 +02003207 # 0.2 merge instance information into scenario
3208 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
3209 # However, this is not possible yet.
tierno41a69812018-02-16 14:34:33 +01003210 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
garciadeblas9f8456e2016-09-05 05:02:59 +02003211 for scenario_net in scenarioDict['nets']:
tiernofc7cfbf2019-03-20 17:23:45 +00003212 if net_name == scenario_net.get("name") or net_name == scenario_net.get("osm_id") or net_name == scenario_net.get("uuid"):
3213 if "wim_account" in net_instance_desc and net_instance_desc["wim_account"] is not None:
3214 scenario_net["wim_account"] = net_instance_desc["wim_account"]
garciadeblas9f8456e2016-09-05 05:02:59 +02003215 if 'ip-profile' in net_instance_desc:
tierno1df468d2018-07-06 14:25:16 +02003216 ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile'])
garciadeblasedca7b32016-09-29 14:01:52 +00003217 if 'ip_profile' not in scenario_net:
tierno455612d2017-05-30 16:40:10 +02003218 scenario_net['ip_profile'] = ipprofile_db
garciadeblasedca7b32016-09-29 14:01:52 +00003219 else:
tierno455612d2017-05-30 16:40:10 +02003220 update(scenario_net['ip_profile'], ipprofile_db)
tierno41a69812018-02-16 14:34:33 +01003221 for interface in net_instance_desc.get('interfaces', ()):
garciadeblas9f8456e2016-09-05 05:02:59 +02003222 if 'ip_address' in interface:
3223 for vnf in scenarioDict['vnfs']:
3224 if interface['vnf'] == vnf['name']:
3225 for vnf_interface in vnf['interfaces']:
3226 if interface['vnf_interface'] == vnf_interface['external_name']:
tierno41a69812018-02-16 14:34:33 +01003227 vnf_interface['ip_address'] = interface['ip_address']
garciadeblas9f8456e2016-09-05 05:02:59 +02003228
tierno868220c2017-09-26 00:11:05 +02003229 # logger.debug(">>>>>>>> Merged dictionary")
3230 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
3231 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
garciadeblas9f8456e2016-09-05 05:02:59 +02003232
tiernob3d36742017-03-03 23:51:05 +01003233 # 1. Creating new nets (sce_nets) in the VIM"
tierno8f79ea12018-05-03 17:37:40 +02003234 number_mgmt_networks = 0
tierno8e690322017-08-10 15:58:50 +02003235 db_instance_nets = []
tierno7edb6752016-03-21 17:37:52 +01003236 for sce_net in scenarioDict['nets']:
tierno7fe82642018-11-26 14:14:51 +00003237 sce_net_uuid = sce_net.get('uuid', sce_net["name"])
tierno1df468d2018-07-06 14:25:16 +02003238 # get involved datacenters where this network need to be created
3239 involved_datacenters = []
tierno7fe82642018-11-26 14:14:51 +00003240 for sce_vnf in scenarioDict.get("vnfs", ()):
tierno1df468d2018-07-06 14:25:16 +02003241 vnf_datacenter = sce_vnf.get("datacenter", default_datacenter_id)
3242 if vnf_datacenter in involved_datacenters:
3243 continue
3244 if sce_vnf.get("interfaces"):
3245 for sce_vnf_ifaces in sce_vnf["interfaces"]:
3246 if sce_vnf_ifaces.get("sce_net_id") == sce_net["uuid"]:
3247 involved_datacenters.append(vnf_datacenter)
3248 break
gcalvinod6fac4d2018-11-05 10:42:06 +01003249 if not involved_datacenters:
3250 involved_datacenters.append(default_datacenter_id)
tierno80391822019-03-21 22:12:14 +00003251 target_wim_account = sce_net.get("wim_account", default_wim_account)
tierno1df468d2018-07-06 14:25:16 +02003252
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003253 # --> WIM
3254 # TODO: use this information during network creation
tierno4070e442019-01-23 10:19:23 +00003255 wim_account_id = wim_account_name = None
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003256 if len(involved_datacenters) > 1 and 'uuid' in sce_net:
tiernofc7cfbf2019-03-20 17:23:45 +00003257 if target_wim_account is None or target_wim_account is True: # automatic selection of WIM
3258 # OBS: sce_net without uuid are used internally to VNFs
3259 # and the assumption is that VNFs will not be split among
3260 # different datacenters
3261 wim_account = wim_engine.find_suitable_wim_account(
3262 involved_datacenters, tenant_id)
3263 wim_account_id = wim_account['uuid']
3264 wim_account_name = wim_account['name']
3265 wim_usage[sce_net['uuid']] = wim_account_id
3266 elif isinstance(target_wim_account, str): # manual selection of WIM
3267 wim_account.persist.get_wim_account_by(target_wim_account, tenant_id)
3268 wim_account_id = wim_account['uuid']
3269 wim_account_name = wim_account['name']
3270 wim_usage[sce_net['uuid']] = wim_account_id
3271 else: # not WIM usage
3272 wim_usage[sce_net['uuid']] = False
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003273 # <-- WIM
3274
tierno1df468d2018-07-06 14:25:16 +02003275 descriptor_net = {}
tierno3c44e7b2019-03-04 17:32:01 +00003276 if instance_dict.get("networks"):
3277 if sce_net.get("uuid") in instance_dict["networks"]:
3278 descriptor_net = instance_dict["networks"][sce_net["uuid"]]
3279 descriptor_net_name = sce_net["uuid"]
3280 elif sce_net.get("osm_id") in instance_dict["networks"]:
3281 descriptor_net = instance_dict["networks"][sce_net["osm_id"]]
3282 descriptor_net_name = sce_net["osm_id"]
3283 elif sce_net["name"] in instance_dict["networks"]:
3284 descriptor_net = instance_dict["networks"][sce_net["name"]]
3285 descriptor_net_name = sce_net["name"]
tiernobe41e222016-09-02 15:16:13 +02003286 net_name = descriptor_net.get("vim-network-name")
tierno7fe82642018-11-26 14:14:51 +00003287 # add datacenters from instantiation parameters
3288 if descriptor_net.get("sites"):
3289 for site in descriptor_net["sites"]:
3290 if site.get("datacenter") and site["datacenter"] not in involved_datacenters:
3291 involved_datacenters.append(site["datacenter"])
3292 sce_net2instance[sce_net_uuid] = {}
3293 net2task_id['scenario'][sce_net_uuid] = {}
tiernobe41e222016-09-02 15:16:13 +02003294
tierno3c44e7b2019-03-04 17:32:01 +00003295 use_network = None
3296 related_network = None
3297 if descriptor_net.get("use-network"):
3298 target_instance_nets = mydb.get_rows(
3299 SELECT="related",
3300 FROM="instance_nets",
3301 WHERE={"instance_scenario_id": descriptor_net["use-network"]["instance_scenario_id"],
3302 "osm_id": descriptor_net["use-network"]["osm_id"]},
3303 )
3304 if not target_instance_nets:
3305 raise NfvoException(
3306 "Cannot find the target network at instance:networks[{}]:use-network".format(descriptor_net_name),
3307 httperrors.Bad_Request)
3308 else:
3309 use_network = target_instance_nets[0]["related"]
3310
tierno1df468d2018-07-06 14:25:16 +02003311 if sce_net["external"]:
3312 number_mgmt_networks += 1
3313
3314 for datacenter_id in involved_datacenters:
3315 netmap_use = None
3316 netmap_create = None
3317 if descriptor_net.get("sites"):
3318 for site in descriptor_net["sites"]:
3319 if site.get("datacenter") == datacenter_id:
3320 netmap_use = site.get("netmap-use")
3321 netmap_create = site.get("netmap-create")
3322 break
3323
3324 vim = myvims[datacenter_id]
3325 myvim_thread_id = myvim_threads_id[datacenter_id]
3326
tiernobe41e222016-09-02 15:16:13 +02003327 net_type = sce_net['type']
tiernob6990792018-11-13 10:37:42 +01003328 net_vim_name = None
tierno868220c2017-09-26 00:11:05 +02003329 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
tierno42026a02017-02-10 15:13:40 +01003330
tiernof1ba57e2017-09-07 12:23:19 +02003331 if not net_name:
3332 if sce_net["external"]:
3333 net_name = sce_net["name"]
3334 else:
tierno1df468d2018-07-06 14:25:16 +02003335 net_name = "{}-{}".format(instance_name, sce_net["name"])
tiernof1ba57e2017-09-07 12:23:19 +02003336 net_name = net_name[:255] # limit length
3337
tierno1df468d2018-07-06 14:25:16 +02003338 if netmap_use or netmap_create:
tiernof1ba57e2017-09-07 12:23:19 +02003339 create_network = False
3340 lookfor_network = False
tierno1df468d2018-07-06 14:25:16 +02003341 if netmap_use:
tiernof1ba57e2017-09-07 12:23:19 +02003342 lookfor_network = True
tierno1df468d2018-07-06 14:25:16 +02003343 if utils.check_valid_uuid(netmap_use):
3344 lookfor_filter["id"] = netmap_use
tiernof1ba57e2017-09-07 12:23:19 +02003345 else:
tierno1df468d2018-07-06 14:25:16 +02003346 lookfor_filter["name"] = netmap_use
3347 if netmap_create:
tiernof1ba57e2017-09-07 12:23:19 +02003348 create_network = True
3349 net_vim_name = net_name
tierno1df468d2018-07-06 14:25:16 +02003350 if isinstance(netmap_create, str):
3351 net_vim_name = netmap_create
tierno8f79ea12018-05-03 17:37:40 +02003352 elif sce_net.get("vim_network_name"):
3353 create_network = False
3354 lookfor_network = True
3355 lookfor_filter["name"] = sce_net.get("vim_network_name")
tiernof1ba57e2017-09-07 12:23:19 +02003356 elif sce_net["external"]:
tiernod108c412018-12-18 15:19:27 +00003357 if sce_net.get('vim_id'):
tierno868220c2017-09-26 00:11:05 +02003358 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
tiernobe41e222016-09-02 15:16:13 +02003359 create_network = False
3360 lookfor_network = True
3361 lookfor_filter["id"] = sce_net['vim_id']
tierno8f79ea12018-05-03 17:37:40 +02003362 elif vim["config"].get("management_network_id") or vim["config"].get("management_network_name"):
3363 if number_mgmt_networks > 1:
3364 raise NfvoException("Found several VLD of type mgmt. "
3365 "You must concrete what vim-network must be use for each one",
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003366 httperrors.Bad_Request)
tierno8f79ea12018-05-03 17:37:40 +02003367 create_network = False
3368 lookfor_network = True
3369 if vim["config"].get("management_network_id"):
3370 lookfor_filter["id"] = vim["config"]["management_network_id"]
3371 else:
3372 lookfor_filter["name"] = vim["config"]["management_network_name"]
tiernobe41e222016-09-02 15:16:13 +02003373 else:
tierno868220c2017-09-26 00:11:05 +02003374 # 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 +02003375 create_network = True
3376 lookfor_network = True
3377 lookfor_filter["name"] = sce_net["name"]
3378 net_vim_name = sce_net["name"]
tierno7edb6752016-03-21 17:37:52 +01003379 else:
tiernobe41e222016-09-02 15:16:13 +02003380 net_vim_name = net_name
3381 create_network = True
3382 lookfor_network = False
tierno42026a02017-02-10 15:13:40 +01003383
tiernof1450872017-10-17 23:15:08 +02003384 task_extra = {}
3385 if create_network:
3386 task_action = "CREATE"
tierno4070e442019-01-23 10:19:23 +00003387 task_extra["params"] = (net_vim_name, net_type, sce_net.get('ip_profile', None), wim_account_name)
tiernof1450872017-10-17 23:15:08 +02003388 if lookfor_network:
3389 task_extra["find"] = (lookfor_filter,)
tierno868220c2017-09-26 00:11:05 +02003390 elif lookfor_network:
3391 task_action = "FIND"
tiernof1450872017-10-17 23:15:08 +02003392 task_extra["params"] = (lookfor_filter,)
tierno42026a02017-02-10 15:13:40 +01003393
tierno8e690322017-08-10 15:58:50 +02003394 # fill database content
3395 net_uuid = str(uuid4())
3396 uuid_list.append(net_uuid)
tierno7fe82642018-11-26 14:14:51 +00003397 sce_net2instance[sce_net_uuid][datacenter_id] = net_uuid
tierno3c44e7b2019-03-04 17:32:01 +00003398 if not related_network: # all db_instance_nets will have same related
3399 related_network = use_network or net_uuid
tierno8e690322017-08-10 15:58:50 +02003400 db_net = {
3401 "uuid": net_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003402 "osm_id": sce_net.get("osm_id") or sce_net["name"],
3403 "related": related_network,
tierno868220c2017-09-26 00:11:05 +02003404 'vim_net_id': None,
tiernob6990792018-11-13 10:37:42 +01003405 "vim_name": net_vim_name,
tierno8e690322017-08-10 15:58:50 +02003406 "instance_scenario_id": instance_uuid,
tierno7fe82642018-11-26 14:14:51 +00003407 "sce_net_id": sce_net.get("uuid"),
tierno8e690322017-08-10 15:58:50 +02003408 "created": create_network,
3409 'datacenter_id': datacenter_id,
3410 'datacenter_tenant_id': myvim_thread_id,
tiernod2836fc2018-05-30 15:03:27 +02003411 'status': 'BUILD' # if create_network else "ACTIVE"
tierno8e690322017-08-10 15:58:50 +02003412 }
3413 db_instance_nets.append(db_net)
tierno868220c2017-09-26 00:11:05 +02003414 db_vim_action = {
3415 "instance_action_id": instance_action_id,
3416 "status": "SCHEDULED",
3417 "task_index": task_index,
3418 "datacenter_vim_id": myvim_thread_id,
3419 "action": task_action,
3420 "item": "instance_nets",
3421 "item_id": net_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003422 "related": related_network,
tiernof1450872017-10-17 23:15:08 +02003423 "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256)
tierno868220c2017-09-26 00:11:05 +02003424 }
tierno7fe82642018-11-26 14:14:51 +00003425 net2task_id['scenario'][sce_net_uuid][datacenter_id] = task_index
tierno868220c2017-09-26 00:11:05 +02003426 task_index += 1
3427 db_vim_actions.append(db_vim_action)
3428
tierno8e690322017-08-10 15:58:50 +02003429 if 'ip_profile' in sce_net:
3430 db_ip_profile={
3431 'instance_net_id': net_uuid,
3432 'ip_version': sce_net['ip_profile']['ip_version'],
3433 'subnet_address': sce_net['ip_profile']['subnet_address'],
3434 'gateway_address': sce_net['ip_profile']['gateway_address'],
3435 'dns_address': sce_net['ip_profile']['dns_address'],
3436 'dhcp_enabled': sce_net['ip_profile']['dhcp_enabled'],
3437 'dhcp_start_address': sce_net['ip_profile']['dhcp_start_address'],
3438 'dhcp_count': sce_net['ip_profile']['dhcp_count'],
3439 }
3440 db_ip_profiles.append(db_ip_profile)
3441
tierno16e3dd42018-04-24 12:52:40 +02003442 # Create VNFs
3443 vnf_params = {
3444 "default_datacenter_id": default_datacenter_id,
3445 "myvim_threads_id": myvim_threads_id,
3446 "instance_uuid": instance_uuid,
3447 "instance_name": instance_name,
3448 "instance_action_id": instance_action_id,
3449 "myvims": myvims,
3450 "cloud_config": cloud_config,
3451 "RO_pub_key": tenant[0].get('RO_pub_key'),
tierno67881db2018-10-24 18:46:03 +02003452 "instance_parameters": instance_dict,
tierno16e3dd42018-04-24 12:52:40 +02003453 }
3454 vnf_params_out = {
3455 "task_index": task_index,
3456 "uuid_list": uuid_list,
3457 "db_instance_nets": db_instance_nets,
3458 "db_vim_actions": db_vim_actions,
3459 "db_ip_profiles": db_ip_profiles,
3460 "db_instance_vnfs": db_instance_vnfs,
3461 "db_instance_vms": db_instance_vms,
3462 "db_instance_interfaces": db_instance_interfaces,
3463 "net2task_id": net2task_id,
3464 "sce_net2instance": sce_net2instance,
3465 }
tierno55d234c2018-07-04 18:29:21 +02003466 # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
tierno7fe82642018-11-26 14:14:51 +00003467 for sce_vnf in scenarioDict.get('vnfs', ()): # sce_vnf_list:
tierno16e3dd42018-04-24 12:52:40 +02003468 instantiate_vnf(mydb, sce_vnf, vnf_params, vnf_params_out, rollbackList)
3469 task_index = vnf_params_out["task_index"]
3470 uuid_list = vnf_params_out["uuid_list"]
mirabal29356312017-07-27 12:21:22 +02003471
tierno16e3dd42018-04-24 12:52:40 +02003472 # Create VNFFGs
3473 # task_depends_on = []
tierno7fe82642018-11-26 14:14:51 +00003474 for vnffg in scenarioDict.get('vnffgs', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00003475 for rsp in vnffg['rsps']:
3476 sfs_created = []
3477 for cp in rsp['connection_points']:
3478 count = mydb.get_rows(
Eduardo Sousa16cfd562018-11-30 15:33:35 +00003479 SELECT='vms.count',
3480 FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h "
3481 "on interfaces.uuid=h.ingress_interface_id",
Igor D.Ccaadc442017-11-06 12:48:48 +00003482 WHERE={'h.uuid': cp['uuid']})[0]['count']
3483 instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == cp['sce_vnf_id']), None)
3484 instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']]
3485 dependencies = []
3486 for instance_vm in instance_vms:
3487 action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None)
3488 if action:
3489 dependencies.append(action['task_index'])
3490 # TODO: throw exception if count != len(instance_vms)
3491 # TODO: and action shouldn't ever be None
3492 sfis_created = []
3493 for i in range(count):
3494 # create sfis
3495 sfi_uuid = str(uuid4())
Eduardo Sousa16cfd562018-11-30 15:33:35 +00003496 extra_params = {
3497 "ingress_interface_id": cp["ingress_interface_id"],
3498 "egress_interface_id": cp["egress_interface_id"]
3499 }
Igor D.Ccaadc442017-11-06 12:48:48 +00003500 uuid_list.append(sfi_uuid)
3501 db_sfi = {
3502 "uuid": sfi_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003503 "related": sfi_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003504 "instance_scenario_id": instance_uuid,
3505 'sce_rsp_hop_id': cp['uuid'],
3506 'datacenter_id': datacenter_id,
3507 'datacenter_tenant_id': myvim_thread_id,
3508 "vim_sfi_id": None, # vim thread will populate
3509 }
3510 db_instance_sfis.append(db_sfi)
3511 db_vim_action = {
3512 "instance_action_id": instance_action_id,
3513 "task_index": task_index,
3514 "datacenter_vim_id": myvim_thread_id,
3515 "action": "CREATE",
3516 "status": "SCHEDULED",
3517 "item": "instance_sfis",
3518 "item_id": sfi_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003519 "related": sfi_uuid,
Eduardo Sousa16cfd562018-11-30 15:33:35 +00003520 "extra": yaml.safe_dump({"params": extra_params, "depends_on": [dependencies[i]]},
Igor D.Ccaadc442017-11-06 12:48:48 +00003521 default_flow_style=True, width=256)
3522 }
3523 sfis_created.append(task_index)
3524 task_index += 1
3525 db_vim_actions.append(db_vim_action)
3526 # create sfs
3527 sf_uuid = str(uuid4())
3528 uuid_list.append(sf_uuid)
3529 db_sf = {
3530 "uuid": sf_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003531 "related": sf_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003532 "instance_scenario_id": instance_uuid,
3533 'sce_rsp_hop_id': cp['uuid'],
3534 'datacenter_id': datacenter_id,
3535 'datacenter_tenant_id': myvim_thread_id,
3536 "vim_sf_id": None, # vim thread will populate
3537 }
3538 db_instance_sfs.append(db_sf)
3539 db_vim_action = {
3540 "instance_action_id": instance_action_id,
3541 "task_index": task_index,
3542 "datacenter_vim_id": myvim_thread_id,
3543 "action": "CREATE",
3544 "status": "SCHEDULED",
3545 "item": "instance_sfs",
3546 "item_id": sf_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003547 "related": sf_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003548 "extra": yaml.safe_dump({"params": "", "depends_on": sfis_created},
3549 default_flow_style=True, width=256)
3550 }
3551 sfs_created.append(task_index)
3552 task_index += 1
3553 db_vim_actions.append(db_vim_action)
3554 classifier = rsp['classifier']
3555
3556 # TODO the following ~13 lines can be reused for the sfi case
3557 count = mydb.get_rows(
3558 SELECT=('vms.count'),
3559 FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id",
3560 WHERE={'c.uuid': classifier['uuid']})[0]['count']
3561 instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == classifier['sce_vnf_id']), None)
3562 instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']]
3563 dependencies = []
3564 for instance_vm in instance_vms:
3565 action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None)
3566 if action:
3567 dependencies.append(action['task_index'])
3568 # TODO: throw exception if count != len(instance_vms)
3569 # TODO: and action shouldn't ever be None
3570 classifications_created = []
3571 for i in range(count):
3572 for match in classifier['matches']:
3573 # create classifications
3574 classification_uuid = str(uuid4())
3575 uuid_list.append(classification_uuid)
3576 db_classification = {
3577 "uuid": classification_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003578 "related": classification_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003579 "instance_scenario_id": instance_uuid,
3580 'sce_classifier_match_id': match['uuid'],
3581 'datacenter_id': datacenter_id,
3582 'datacenter_tenant_id': myvim_thread_id,
3583 "vim_classification_id": None, # vim thread will populate
3584 }
3585 db_instance_classifications.append(db_classification)
3586 classification_params = {
3587 "ip_proto": match["ip_proto"],
3588 "source_ip": match["source_ip"],
3589 "destination_ip": match["destination_ip"],
3590 "source_port": match["source_port"],
3591 "destination_port": match["destination_port"]
3592 }
3593 db_vim_action = {
3594 "instance_action_id": instance_action_id,
3595 "task_index": task_index,
3596 "datacenter_vim_id": myvim_thread_id,
3597 "action": "CREATE",
3598 "status": "SCHEDULED",
3599 "item": "instance_classifications",
3600 "item_id": classification_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003601 "related": classification_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003602 "extra": yaml.safe_dump({"params": classification_params, "depends_on": [dependencies[i]]},
3603 default_flow_style=True, width=256)
3604 }
3605 classifications_created.append(task_index)
3606 task_index += 1
3607 db_vim_actions.append(db_vim_action)
3608
3609 # create sfps
3610 sfp_uuid = str(uuid4())
3611 uuid_list.append(sfp_uuid)
3612 db_sfp = {
3613 "uuid": sfp_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003614 "related": sfp_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003615 "instance_scenario_id": instance_uuid,
3616 'sce_rsp_id': rsp['uuid'],
3617 'datacenter_id': datacenter_id,
3618 'datacenter_tenant_id': myvim_thread_id,
3619 "vim_sfp_id": None, # vim thread will populate
3620 }
3621 db_instance_sfps.append(db_sfp)
3622 db_vim_action = {
3623 "instance_action_id": instance_action_id,
3624 "task_index": task_index,
3625 "datacenter_vim_id": myvim_thread_id,
3626 "action": "CREATE",
3627 "status": "SCHEDULED",
3628 "item": "instance_sfps",
3629 "item_id": sfp_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003630 "related": sfp_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003631 "extra": yaml.safe_dump({"params": "", "depends_on": sfs_created + classifications_created},
3632 default_flow_style=True, width=256)
3633 }
3634 task_index += 1
3635 db_vim_actions.append(db_vim_action)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003636 db_instance_action["number_tasks"] = task_index
3637
3638 # --> WIM
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003639 logger.debug('wim_usage:\n%s\n\n', pformat(wim_usage))
3640 wan_links = wim_engine.derive_wan_links(wim_usage, db_instance_nets, tenant_id)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003641 wim_actions = wim_engine.create_actions(wan_links)
3642 wim_actions, db_instance_action = (
3643 wim_engine.incorporate_actions(wim_actions, db_instance_action))
3644 # <-- WIM
Igor D.Ccaadc442017-11-06 12:48:48 +00003645
tierno867ffe92017-03-27 12:50:34 +02003646 scenarioDict["datacenter2tenant"] = myvim_threads_id
tierno8e690322017-08-10 15:58:50 +02003647
3648 db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id]
3649 db_instance_scenario['datacenter_id'] = default_datacenter_id
3650 db_tables=[
3651 {"instance_scenarios": db_instance_scenario},
3652 {"instance_vnfs": db_instance_vnfs},
3653 {"instance_nets": db_instance_nets},
3654 {"ip_profiles": db_ip_profiles},
3655 {"instance_vms": db_instance_vms},
3656 {"instance_interfaces": db_instance_interfaces},
tierno868220c2017-09-26 00:11:05 +02003657 {"instance_actions": db_instance_action},
Igor D.Ccaadc442017-11-06 12:48:48 +00003658 {"instance_sfis": db_instance_sfis},
3659 {"instance_sfs": db_instance_sfs},
3660 {"instance_classifications": db_instance_classifications},
3661 {"instance_sfps": db_instance_sfps},
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003662 {"instance_wim_nets": wan_links},
3663 {"vim_wim_actions": db_vim_actions + wim_actions}
tierno8e690322017-08-10 15:58:50 +02003664 ]
3665
tierno868220c2017-09-26 00:11:05 +02003666 logger.debug("create_instance done DB tables: %s",
tierno8e690322017-08-10 15:58:50 +02003667 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
3668 mydb.new_rows(db_tables, uuid_list)
tierno868220c2017-09-26 00:11:05 +02003669 for myvim_thread_id in myvim_threads_id.values():
3670 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
tierno867ffe92017-03-27 12:50:34 +02003671
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003672 wim_engine.dispatch(wim_actions)
3673
tierno868220c2017-09-26 00:11:05 +02003674 returned_instance = mydb.get_instance_scenario(instance_uuid)
3675 returned_instance["action_id"] = instance_action_id
3676 return returned_instance
tierno4491ba92019-03-25 15:00:02 +00003677 except (NfvoException, vimconn.vimconnException, wimconn.WimConnectorError, db_base_Exception) as e:
tiernobe41e222016-09-02 15:16:13 +02003678 message = rollback(mydb, myvims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02003679 if isinstance(e, db_base_Exception):
3680 error_text = "database Exception"
3681 elif isinstance(e, vimconn.vimconnException):
3682 error_text = "VIM Exception"
tierno4491ba92019-03-25 15:00:02 +00003683 elif isinstance(e, wimconn.WimConnectorError):
3684 error_text = "WIM Exception"
tiernof97fd272016-07-11 14:32:37 +02003685 else:
3686 error_text = "Exception"
3687 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
tierno868220c2017-09-26 00:11:05 +02003688 # logger.error("create_instance: %s", error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003689 logger.exception(e)
tiernof97fd272016-07-11 14:32:37 +02003690 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01003691
tiernob3d36742017-03-03 23:51:05 +01003692
tierno16e3dd42018-04-24 12:52:40 +02003693def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList):
3694 default_datacenter_id = params["default_datacenter_id"]
3695 myvim_threads_id = params["myvim_threads_id"]
3696 instance_uuid = params["instance_uuid"]
3697 instance_name = params["instance_name"]
3698 instance_action_id = params["instance_action_id"]
3699 myvims = params["myvims"]
3700 cloud_config = params["cloud_config"]
3701 RO_pub_key = params["RO_pub_key"]
3702
3703 task_index = params_out["task_index"]
3704 uuid_list = params_out["uuid_list"]
3705 db_instance_nets = params_out["db_instance_nets"]
3706 db_vim_actions = params_out["db_vim_actions"]
3707 db_ip_profiles = params_out["db_ip_profiles"]
3708 db_instance_vnfs = params_out["db_instance_vnfs"]
3709 db_instance_vms = params_out["db_instance_vms"]
3710 db_instance_interfaces = params_out["db_instance_interfaces"]
3711 net2task_id = params_out["net2task_id"]
3712 sce_net2instance = params_out["sce_net2instance"]
3713
3714 vnf_net2instance = {}
3715
3716 # 2. Creating new nets (vnf internal nets) in the VIM"
3717 # For each vnf net, we create it and we add it to instanceNetlist.
3718 if sce_vnf.get("datacenter"):
3719 datacenter_id = sce_vnf["datacenter"]
3720 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
3721 else:
3722 datacenter_id = default_datacenter_id
3723 myvim_thread_id = myvim_threads_id[default_datacenter_id]
3724 for net in sce_vnf['nets']:
3725 # TODO revis
3726 # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
3727 # net_name = descriptor_net.get("name")
3728 net_name = None
3729 if not net_name:
tierno1df468d2018-07-06 14:25:16 +02003730 net_name = "{}-{}".format(instance_name, net["name"])
tierno16e3dd42018-04-24 12:52:40 +02003731 net_name = net_name[:255] # limit length
3732 net_type = net['type']
3733
3734 if sce_vnf['uuid'] not in vnf_net2instance:
3735 vnf_net2instance[sce_vnf['uuid']] = {}
3736 if sce_vnf['uuid'] not in net2task_id:
3737 net2task_id[sce_vnf['uuid']] = {}
3738 net2task_id[sce_vnf['uuid']][net['uuid']] = task_index
3739
3740 # fill database content
3741 net_uuid = str(uuid4())
3742 uuid_list.append(net_uuid)
3743 vnf_net2instance[sce_vnf['uuid']][net['uuid']] = net_uuid
3744 db_net = {
3745 "uuid": net_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003746 "related": net_uuid,
tierno16e3dd42018-04-24 12:52:40 +02003747 'vim_net_id': None,
tiernob6990792018-11-13 10:37:42 +01003748 "vim_name": net_name,
tierno16e3dd42018-04-24 12:52:40 +02003749 "instance_scenario_id": instance_uuid,
3750 "net_id": net["uuid"],
3751 "created": True,
3752 'datacenter_id': datacenter_id,
3753 'datacenter_tenant_id': myvim_thread_id,
3754 }
3755 db_instance_nets.append(db_net)
3756
gcalvino0a480542018-12-17 16:19:33 +01003757 lookfor_filter = {}
tierno1df468d2018-07-06 14:25:16 +02003758 if net.get("vim-network-name"):
gcalvino0a480542018-12-17 16:19:33 +01003759 lookfor_filter["name"] = net["vim-network-name"]
3760 if net.get("vim-network-id"):
3761 lookfor_filter["id"] = net["vim-network-id"]
3762 if lookfor_filter:
tierno1df468d2018-07-06 14:25:16 +02003763 task_action = "FIND"
3764 task_extra = {"params": (lookfor_filter,)}
3765 else:
3766 task_action = "CREATE"
3767 task_extra = {"params": (net_name, net_type, net.get('ip_profile', None))}
3768
tierno16e3dd42018-04-24 12:52:40 +02003769 db_vim_action = {
3770 "instance_action_id": instance_action_id,
3771 "task_index": task_index,
3772 "datacenter_vim_id": myvim_thread_id,
3773 "status": "SCHEDULED",
tierno1df468d2018-07-06 14:25:16 +02003774 "action": task_action,
tierno16e3dd42018-04-24 12:52:40 +02003775 "item": "instance_nets",
3776 "item_id": net_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003777 "related": net_uuid,
tierno1df468d2018-07-06 14:25:16 +02003778 "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256)
tierno16e3dd42018-04-24 12:52:40 +02003779 }
3780 task_index += 1
3781 db_vim_actions.append(db_vim_action)
3782
3783 if 'ip_profile' in net:
3784 db_ip_profile = {
3785 'instance_net_id': net_uuid,
3786 'ip_version': net['ip_profile']['ip_version'],
3787 'subnet_address': net['ip_profile']['subnet_address'],
3788 'gateway_address': net['ip_profile']['gateway_address'],
3789 'dns_address': net['ip_profile']['dns_address'],
3790 'dhcp_enabled': net['ip_profile']['dhcp_enabled'],
3791 'dhcp_start_address': net['ip_profile']['dhcp_start_address'],
3792 'dhcp_count': net['ip_profile']['dhcp_count'],
3793 }
3794 db_ip_profiles.append(db_ip_profile)
3795
3796 # print "vnf_net2instance:"
3797 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
3798
3799 # 3. Creating new vm instances in the VIM
3800 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
3801 ssh_access = None
3802 if sce_vnf.get('mgmt_access'):
3803 ssh_access = sce_vnf['mgmt_access'].get('config-access', {}).get('ssh-access')
3804 vnf_availability_zones = []
gcalvinod6fac4d2018-11-05 10:42:06 +01003805 for vm in sce_vnf.get('vms'):
tierno16e3dd42018-04-24 12:52:40 +02003806 vm_av = vm.get('availability_zone')
3807 if vm_av and vm_av not in vnf_availability_zones:
3808 vnf_availability_zones.append(vm_av)
3809
3810 # check if there is enough availability zones available at vim level.
3811 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
3812 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003813 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors.Bad_Request)
tierno16e3dd42018-04-24 12:52:40 +02003814
3815 if sce_vnf.get("datacenter"):
3816 vim = myvims[sce_vnf["datacenter"]]
3817 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
3818 datacenter_id = sce_vnf["datacenter"]
3819 else:
3820 vim = myvims[default_datacenter_id]
3821 myvim_thread_id = myvim_threads_id[default_datacenter_id]
3822 datacenter_id = default_datacenter_id
3823 sce_vnf["datacenter_id"] = datacenter_id
3824 i = 0
3825
3826 vnf_uuid = str(uuid4())
3827 uuid_list.append(vnf_uuid)
3828 db_instance_vnf = {
3829 'uuid': vnf_uuid,
3830 'instance_scenario_id': instance_uuid,
3831 'vnf_id': sce_vnf['vnf_id'],
3832 'sce_vnf_id': sce_vnf['uuid'],
3833 'datacenter_id': datacenter_id,
3834 'datacenter_tenant_id': myvim_thread_id,
3835 }
3836 db_instance_vnfs.append(db_instance_vnf)
3837
3838 for vm in sce_vnf['vms']:
tiernob6990792018-11-13 10:37:42 +01003839 # skip PDUs
3840 if vm.get("pdu_type"):
3841 continue
3842
tierno16e3dd42018-04-24 12:52:40 +02003843 myVMDict = {}
tierno7f426e92018-06-28 15:21:32 +02003844 sce_vnf_name = sce_vnf['member_vnf_index'] if sce_vnf['member_vnf_index'] else sce_vnf['name']
3845 myVMDict['name'] = "{}-{}-{}".format(instance_name[:64], sce_vnf_name[:64], vm["name"][:64])
tierno16e3dd42018-04-24 12:52:40 +02003846 myVMDict['description'] = myVMDict['name'][0:99]
3847 # if not startvms:
3848 # myVMDict['start'] = "no"
tierno1df468d2018-07-06 14:25:16 +02003849 if vm.get("instance_parameters") and vm["instance_parameters"].get("name"):
3850 myVMDict['name'] = vm["instance_parameters"].get("name")
tierno16e3dd42018-04-24 12:52:40 +02003851 myVMDict['name'] = myVMDict['name'][0:255] # limit name length
3852 # create image at vim in case it not exist
3853 image_uuid = vm['image_id']
3854 if vm.get("image_list"):
3855 for alternative_image in vm["image_list"]:
tiernob6434212018-04-26 16:27:47 +02003856 if alternative_image["vim_type"] == vim["config"]["_vim_type_internal"]:
tierno16e3dd42018-04-24 12:52:40 +02003857 image_uuid = alternative_image['image_id']
3858 break
3859 image_dict = mydb.get_table_by_uuid_name("images", image_uuid)
3860 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
3861 vm['vim_image_id'] = image_id
3862
3863 # create flavor at vim in case it not exist
3864 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
3865 if flavor_dict['extended'] != None:
3866 flavor_dict['extended'] = yaml.load(flavor_dict['extended'])
3867 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
3868
3869 # Obtain information for additional disks
3870 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',),
3871 WHERE={'vim_id': flavor_id})
3872 if not extended_flavor_dict:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003873 raise NfvoException("flavor '{}' not found".format(flavor_id), httperrors.Not_Found)
tierno16e3dd42018-04-24 12:52:40 +02003874
3875 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
3876 myVMDict['disks'] = None
3877 extended_info = extended_flavor_dict[0]['extended']
3878 if extended_info != None:
3879 extended_flavor_dict_yaml = yaml.load(extended_info)
3880 if 'disks' in extended_flavor_dict_yaml:
3881 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
tierno1df468d2018-07-06 14:25:16 +02003882 if vm.get("instance_parameters") and vm["instance_parameters"].get("devices"):
3883 for disk in myVMDict['disks']:
3884 if disk.get("name") in vm["instance_parameters"]["devices"]:
3885 disk.update(vm["instance_parameters"]["devices"][disk.get("name")])
tierno16e3dd42018-04-24 12:52:40 +02003886
3887 vm['vim_flavor_id'] = flavor_id
3888 myVMDict['imageRef'] = vm['vim_image_id']
3889 myVMDict['flavorRef'] = vm['vim_flavor_id']
3890 myVMDict['availability_zone'] = vm.get('availability_zone')
3891 myVMDict['networks'] = []
3892 task_depends_on = []
3893 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
tierno67881db2018-10-24 18:46:03 +02003894 is_management_vm = False
tierno16e3dd42018-04-24 12:52:40 +02003895 db_vm_ifaces = []
3896 for iface in vm['interfaces']:
3897 netDict = {}
3898 if iface['type'] == "data":
3899 netDict['type'] = iface['model']
3900 elif "model" in iface and iface["model"] != None:
3901 netDict['model'] = iface['model']
3902 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
3903 # is obtained from iterface table model
3904 # discover type of interface looking at flavor
3905 for numa in flavor_dict.get('extended', {}).get('numas', []):
3906 for flavor_iface in numa.get('interfaces', []):
3907 if flavor_iface.get('name') == iface['internal_name']:
3908 if flavor_iface['dedicated'] == 'yes':
3909 netDict['type'] = "PF" # passthrough
3910 elif flavor_iface['dedicated'] == 'no':
3911 netDict['type'] = "VF" # siov
3912 elif flavor_iface['dedicated'] == 'yes:sriov':
3913 netDict['type'] = "VFnotShared" # sriov but only one sriov on the PF
3914 netDict["mac_address"] = flavor_iface.get("mac_address")
3915 break
3916 netDict["use"] = iface['type']
3917 if netDict["use"] == "data" and not netDict.get("type"):
3918 # print "netDict", netDict
3919 # print "iface", iface
3920 e_text = "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat(
3921 sce_vnf['name'], vm['name'], iface['internal_name'])
3922 if flavor_dict.get('extended') == None:
3923 raise NfvoException(e_text + "After database migration some information is not available. \
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003924 Try to delete and create the scenarios and VNFs again", httperrors.Conflict)
tierno16e3dd42018-04-24 12:52:40 +02003925 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003926 raise NfvoException(e_text, httperrors.Internal_Server_Error)
tierno67881db2018-10-24 18:46:03 +02003927 if netDict["use"] == "mgmt":
3928 is_management_vm = True
3929 netDict["type"] = "virtual"
3930 if netDict["use"] == "bridge":
tierno16e3dd42018-04-24 12:52:40 +02003931 netDict["type"] = "virtual"
3932 if iface.get("vpci"):
3933 netDict['vpci'] = iface['vpci']
3934 if iface.get("mac"):
3935 netDict['mac_address'] = iface['mac']
tierno6082b7d2018-08-31 11:24:08 +00003936 if iface.get("mac_address"):
3937 netDict['mac_address'] = iface['mac_address']
tierno16e3dd42018-04-24 12:52:40 +02003938 if iface.get("ip_address"):
3939 netDict['ip_address'] = iface['ip_address']
3940 if iface.get("port-security") is not None:
3941 netDict['port_security'] = iface['port-security']
3942 if iface.get("floating-ip") is not None:
3943 netDict['floating_ip'] = iface['floating-ip']
3944 netDict['name'] = iface['internal_name']
3945 if iface['net_id'] is None:
3946 for vnf_iface in sce_vnf["interfaces"]:
3947 # print iface
3948 # print vnf_iface
3949 if vnf_iface['interface_id'] == iface['uuid']:
3950 netDict['net_id'] = "TASK-{}".format(
3951 net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id])
3952 instance_net_id = sce_net2instance[vnf_iface['sce_net_id']][datacenter_id]
3953 task_depends_on.append(net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id])
3954 break
3955 else:
3956 netDict['net_id'] = "TASK-{}".format(net2task_id[sce_vnf['uuid']][iface['net_id']])
3957 instance_net_id = vnf_net2instance[sce_vnf['uuid']][iface['net_id']]
3958 task_depends_on.append(net2task_id[sce_vnf['uuid']][iface['net_id']])
3959 # skip bridge ifaces not connected to any net
3960 if 'net_id' not in netDict or netDict['net_id'] == None:
3961 continue
3962 myVMDict['networks'].append(netDict)
3963 db_vm_iface = {
3964 # "uuid"
3965 # 'instance_vm_id': instance_vm_uuid,
3966 "instance_net_id": instance_net_id,
3967 'interface_id': iface['uuid'],
3968 # 'vim_interface_id': ,
3969 'type': 'external' if iface['external_name'] is not None else 'internal',
3970 'ip_address': iface.get('ip_address'),
3971 'mac_address': iface.get('mac'),
3972 'floating_ip': int(iface.get('floating-ip', False)),
3973 'port_security': int(iface.get('port-security', True))
3974 }
3975 db_vm_ifaces.append(db_vm_iface)
3976 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3977 # print myVMDict['name']
3978 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
3979 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
3980 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3981
3982 # We add the RO key to cloud_config if vnf will need ssh access
3983 cloud_config_vm = cloud_config
tierno67881db2018-10-24 18:46:03 +02003984 if is_management_vm and params["instance_parameters"].get("mgmt_keys"):
3985 cloud_config_vm = unify_cloud_config({"key-pairs": params["instance_parameters"]["mgmt_keys"]},
3986 cloud_config_vm)
3987
tierno7e510052019-09-10 16:16:13 +00003988 if vm.get("instance_parameters") and "mgmt_keys" in vm["instance_parameters"]:
3989 if vm["instance_parameters"]["mgmt_keys"]:
3990 cloud_config_vm = unify_cloud_config({"key-pairs": vm["instance_parameters"]["mgmt_keys"]},
3991 cloud_config_vm)
3992 if RO_pub_key:
calvinosanch5db670b2019-10-13 15:52:33 +02003993 cloud_config_vm = unify_cloud_config(cloud_config_vm, {"key-pairs": [RO_pub_key]})
tierno16e3dd42018-04-24 12:52:40 +02003994 if vm.get("boot_data"):
3995 cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config_vm)
3996
3997 if myVMDict.get('availability_zone'):
3998 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
3999 else:
4000 av_index = None
4001 for vm_index in range(0, vm.get('count', 1)):
tiernofc5f80b2018-05-29 16:00:43 +02004002 vm_name = myVMDict['name'] + "-" + str(vm_index+1)
4003 task_params = (vm_name, myVMDict['description'], myVMDict.get('start', None),
tierno16e3dd42018-04-24 12:52:40 +02004004 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm,
4005 myVMDict['disks'], av_index, vnf_availability_zones)
4006 # put interface uuid back to scenario[vnfs][vms[[interfaces]
4007 for net in myVMDict['networks']:
4008 if "vim_id" in net:
4009 for iface in vm['interfaces']:
4010 if net["name"] == iface["internal_name"]:
4011 iface["vim_id"] = net["vim_id"]
4012 break
4013 vm_uuid = str(uuid4())
4014 uuid_list.append(vm_uuid)
4015 db_vm = {
4016 "uuid": vm_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00004017 "related": vm_uuid,
tierno16e3dd42018-04-24 12:52:40 +02004018 'instance_vnf_id': vnf_uuid,
4019 # TODO delete "vim_vm_id": vm_id,
4020 "vm_id": vm["uuid"],
tiernofc5f80b2018-05-29 16:00:43 +02004021 "vim_name": vm_name,
tierno16e3dd42018-04-24 12:52:40 +02004022 # "status":
4023 }
4024 db_instance_vms.append(db_vm)
4025
4026 iface_index = 0
4027 for db_vm_iface in db_vm_ifaces:
4028 iface_uuid = str(uuid4())
4029 uuid_list.append(iface_uuid)
4030 db_vm_iface_instance = {
4031 "uuid": iface_uuid,
4032 "instance_vm_id": vm_uuid
4033 }
4034 db_vm_iface_instance.update(db_vm_iface)
4035 if db_vm_iface_instance.get("ip_address"): # increment ip_address
4036 ip = db_vm_iface_instance.get("ip_address")
4037 i = ip.rfind(".")
4038 if i > 0:
4039 try:
4040 i += 1
4041 ip = ip[i:] + str(int(ip[:i]) + 1)
4042 db_vm_iface_instance["ip_address"] = ip
4043 except:
4044 db_vm_iface_instance["ip_address"] = None
4045 db_instance_interfaces.append(db_vm_iface_instance)
4046 myVMDict['networks'][iface_index]["uuid"] = iface_uuid
4047 iface_index += 1
4048
4049 db_vim_action = {
4050 "instance_action_id": instance_action_id,
4051 "task_index": task_index,
4052 "datacenter_vim_id": myvim_thread_id,
4053 "action": "CREATE",
4054 "status": "SCHEDULED",
4055 "item": "instance_vms",
4056 "item_id": vm_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00004057 "related": vm_uuid,
tierno16e3dd42018-04-24 12:52:40 +02004058 "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on},
4059 default_flow_style=True, width=256)
4060 }
4061 task_index += 1
4062 db_vim_actions.append(db_vim_action)
4063 params_out["task_index"] = task_index
4064 params_out["uuid_list"] = uuid_list
4065
4066
tierno7edb6752016-03-21 17:37:52 +01004067def delete_instance(mydb, tenant_id, instance_id):
tierno868220c2017-09-26 00:11:05 +02004068 # print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02004069 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
tierno868220c2017-09-26 00:11:05 +02004070 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01004071 tenant_id = instanceDict["tenant_id"]
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004072
4073 # --> WIM
4074 # We need to retrieve the WIM Actions now, before the instance_scenario is
4075 # deleted. The reason for that is that: ON CASCADE rules will delete the
4076 # instance_wim_nets record in the database
4077 wim_actions = wim_engine.delete_actions(instance_scenario_id=instance_id)
4078 # <-- WIM
4079
tierno868220c2017-09-26 00:11:05 +02004080 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno868220c2017-09-26 00:11:05 +02004081 # 1. Delete from Database
tiernof97fd272016-07-11 14:32:37 +02004082 message = mydb.delete_instance_scenario(instance_id, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01004083
tierno868220c2017-09-26 00:11:05 +02004084 # 2. delete from VIM
tiernoa2793912016-10-04 08:15:08 +00004085 error_msg = ""
tiernob3d36742017-03-03 23:51:05 +01004086 myvims = {}
4087 myvim_threads = {}
tierno868220c2017-09-26 00:11:05 +02004088 vimthread_affected = {}
tierno3fcfdb72017-10-24 07:48:24 +02004089 net2vm_dependencies = {}
tierno7edb6752016-03-21 17:37:52 +01004090
tierno868220c2017-09-26 00:11:05 +02004091 task_index = 0
4092 instance_action_id = get_task_id()
4093 db_vim_actions = []
4094 db_instance_action = {
4095 "uuid": instance_action_id, # same uuid for the instance and the action on create
4096 "tenant_id": tenant_id,
4097 "instance_id": instance_id,
4098 "description": "DELETE",
4099 # "number_tasks": 0 # filled bellow
4100 }
4101
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004102 # 2.1 deleting VNFFGs
tierno69b590e2018-03-13 18:52:23 +01004103 for sfp in instanceDict.get('sfps', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00004104 vimthread_affected[sfp["datacenter_tenant_id"]] = None
4105 datacenter_key = (sfp["datacenter_id"], sfp["datacenter_tenant_id"])
4106 if datacenter_key not in myvims:
4107 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004108 _, myvim_thread = get_vim_thread(mydb, tenant_id, sfp["datacenter_id"], sfp["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004109 except NfvoException as e:
4110 logger.error(str(e))
4111 myvim_thread = None
4112 myvim_threads[datacenter_key] = myvim_thread
4113 vims = get_vim(mydb, tenant_id, datacenter_id=sfp["datacenter_id"],
4114 datacenter_tenant_id=sfp["datacenter_tenant_id"])
4115 if len(vims) == 0:
4116 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp["datacenter_id"], sfp["datacenter_tenant_id"]))
4117 myvims[datacenter_key] = None
4118 else:
4119 myvims[datacenter_key] = vims.values()[0]
4120 myvim = myvims[datacenter_key]
4121 myvim_thread = myvim_threads[datacenter_key]
4122
4123 if not myvim:
4124 error_msg += "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp['vim_sfp_id'], sfp["datacenter_id"])
4125 continue
4126 extra = {"params": (sfp['vim_sfp_id'])}
4127 db_vim_action = {
4128 "instance_action_id": instance_action_id,
4129 "task_index": task_index,
4130 "datacenter_vim_id": sfp["datacenter_tenant_id"],
4131 "action": "DELETE",
4132 "status": "SCHEDULED",
4133 "item": "instance_sfps",
4134 "item_id": sfp["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004135 "related": sfp["related"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004136 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4137 }
4138 task_index += 1
4139 db_vim_actions.append(db_vim_action)
4140
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004141 for classification in instanceDict['classifications']:
4142 vimthread_affected[classification["datacenter_tenant_id"]] = None
4143 datacenter_key = (classification["datacenter_id"], classification["datacenter_tenant_id"])
4144 if datacenter_key not in myvims:
4145 try:
4146 _, myvim_thread = get_vim_thread(mydb, tenant_id, classification["datacenter_id"], classification["datacenter_tenant_id"])
4147 except NfvoException as e:
4148 logger.error(str(e))
4149 myvim_thread = None
4150 myvim_threads[datacenter_key] = myvim_thread
4151 vims = get_vim(mydb, tenant_id, datacenter_id=classification["datacenter_id"],
4152 datacenter_tenant_id=classification["datacenter_tenant_id"])
4153 if len(vims) == 0:
4154 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification["datacenter_id"],
4155 classification["datacenter_tenant_id"]))
4156 myvims[datacenter_key] = None
4157 else:
4158 myvims[datacenter_key] = vims.values()[0]
4159 myvim = myvims[datacenter_key]
4160 myvim_thread = myvim_threads[datacenter_key]
4161
4162 if not myvim:
4163 error_msg += "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification['vim_classification_id'],
4164 classification["datacenter_id"])
4165 continue
4166 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"]
4167 extra = {"params": (classification['vim_classification_id']), "depends_on": depends_on}
4168 db_vim_action = {
4169 "instance_action_id": instance_action_id,
4170 "task_index": task_index,
4171 "datacenter_vim_id": classification["datacenter_tenant_id"],
4172 "action": "DELETE",
4173 "status": "SCHEDULED",
4174 "item": "instance_classifications",
4175 "item_id": classification["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004176 "related": classification["related"],
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004177 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4178 }
4179 task_index += 1
4180 db_vim_actions.append(db_vim_action)
4181
tierno69b590e2018-03-13 18:52:23 +01004182 for sf in instanceDict.get('sfs', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00004183 vimthread_affected[sf["datacenter_tenant_id"]] = None
4184 datacenter_key = (sf["datacenter_id"], sf["datacenter_tenant_id"])
4185 if datacenter_key not in myvims:
4186 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004187 _, myvim_thread = get_vim_thread(mydb, tenant_id, sf["datacenter_id"], sf["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004188 except NfvoException as e:
4189 logger.error(str(e))
4190 myvim_thread = None
4191 myvim_threads[datacenter_key] = myvim_thread
4192 vims = get_vim(mydb, tenant_id, datacenter_id=sf["datacenter_id"],
4193 datacenter_tenant_id=sf["datacenter_tenant_id"])
4194 if len(vims) == 0:
4195 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf["datacenter_id"], sf["datacenter_tenant_id"]))
4196 myvims[datacenter_key] = None
4197 else:
4198 myvims[datacenter_key] = vims.values()[0]
4199 myvim = myvims[datacenter_key]
4200 myvim_thread = myvim_threads[datacenter_key]
4201
4202 if not myvim:
4203 error_msg += "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf['vim_sf_id'], sf["datacenter_id"])
4204 continue
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004205 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"]
4206 extra = {"params": (sf['vim_sf_id']), "depends_on": depends_on}
Igor D.Ccaadc442017-11-06 12:48:48 +00004207 db_vim_action = {
4208 "instance_action_id": instance_action_id,
4209 "task_index": task_index,
4210 "datacenter_vim_id": sf["datacenter_tenant_id"],
4211 "action": "DELETE",
4212 "status": "SCHEDULED",
4213 "item": "instance_sfs",
4214 "item_id": sf["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004215 "related": sf["related"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004216 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4217 }
4218 task_index += 1
4219 db_vim_actions.append(db_vim_action)
4220
tierno69b590e2018-03-13 18:52:23 +01004221 for sfi in instanceDict.get('sfis', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00004222 vimthread_affected[sfi["datacenter_tenant_id"]] = None
4223 datacenter_key = (sfi["datacenter_id"], sfi["datacenter_tenant_id"])
4224 if datacenter_key not in myvims:
4225 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004226 _, myvim_thread = get_vim_thread(mydb, tenant_id, sfi["datacenter_id"], sfi["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004227 except NfvoException as e:
4228 logger.error(str(e))
4229 myvim_thread = None
4230 myvim_threads[datacenter_key] = myvim_thread
4231 vims = get_vim(mydb, tenant_id, datacenter_id=sfi["datacenter_id"],
4232 datacenter_tenant_id=sfi["datacenter_tenant_id"])
4233 if len(vims) == 0:
4234 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi["datacenter_id"], sfi["datacenter_tenant_id"]))
4235 myvims[datacenter_key] = None
4236 else:
4237 myvims[datacenter_key] = vims.values()[0]
4238 myvim = myvims[datacenter_key]
4239 myvim_thread = myvim_threads[datacenter_key]
4240
4241 if not myvim:
4242 error_msg += "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi['vim_sfi_id'], sfi["datacenter_id"])
4243 continue
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004244 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfs"]
4245 extra = {"params": (sfi['vim_sfi_id']), "depends_on": depends_on}
Igor D.Ccaadc442017-11-06 12:48:48 +00004246 db_vim_action = {
4247 "instance_action_id": instance_action_id,
4248 "task_index": task_index,
4249 "datacenter_vim_id": sfi["datacenter_tenant_id"],
4250 "action": "DELETE",
4251 "status": "SCHEDULED",
4252 "item": "instance_sfis",
4253 "item_id": sfi["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004254 "related": sfi["related"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004255 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4256 }
4257 task_index += 1
4258 db_vim_actions.append(db_vim_action)
4259
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004260 # 2.2 deleting VMs
4261 # vm_fail_list=[]
gcalvinod6fac4d2018-11-05 10:42:06 +01004262 for sce_vnf in instanceDict.get('vnfs', ()):
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004263 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4264 vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None
Igor D.Ccaadc442017-11-06 12:48:48 +00004265 if datacenter_key not in myvims:
4266 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004267 _, myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004268 except NfvoException as e:
4269 logger.error(str(e))
4270 myvim_thread = None
4271 myvim_threads[datacenter_key] = myvim_thread
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004272 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
4273 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004274 if len(vims) == 0:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004275 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
4276 sce_vnf["datacenter_tenant_id"]))
4277 myvims[datacenter_key] = None
4278 else:
4279 myvims[datacenter_key] = vims.values()[0]
4280 myvim = myvims[datacenter_key]
4281 myvim_thread = myvim_threads[datacenter_key]
4282
4283 for vm in sce_vnf['vms']:
4284 if not myvim:
4285 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
4286 continue
4287 sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"]
4288 db_vim_action = {
4289 "instance_action_id": instance_action_id,
4290 "task_index": task_index,
4291 "datacenter_vim_id": sce_vnf["datacenter_tenant_id"],
4292 "action": "DELETE",
4293 "status": "SCHEDULED",
4294 "item": "instance_vms",
4295 "item_id": vm["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004296 "related": vm["related"],
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004297 "extra": yaml.safe_dump({"params": vm["interfaces"], "depends_on": sfi_dependencies},
4298 default_flow_style=True, width=256)
4299 }
4300 db_vim_actions.append(db_vim_action)
4301 for interface in vm["interfaces"]:
4302 if not interface.get("instance_net_id"):
4303 continue
4304 if interface["instance_net_id"] not in net2vm_dependencies:
4305 net2vm_dependencies[interface["instance_net_id"]] = []
4306 net2vm_dependencies[interface["instance_net_id"]].append(task_index)
4307 task_index += 1
4308
4309 # 2.3 deleting NETS
4310 # net_fail_list=[]
4311 for net in instanceDict['nets']:
4312 vimthread_affected[net["datacenter_tenant_id"]] = None
4313 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4314 if datacenter_key not in myvims:
4315 try:
gcalvinod6fac4d2018-11-05 10:42:06 +01004316 _,myvim_thread = get_vim_thread(mydb, tenant_id, net["datacenter_id"], net["datacenter_tenant_id"])
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004317 except NfvoException as e:
4318 logger.error(str(e))
4319 myvim_thread = None
4320 myvim_threads[datacenter_key] = myvim_thread
4321 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
4322 datacenter_tenant_id=net["datacenter_tenant_id"])
4323 if len(vims) == 0:
4324 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
Igor D.Ccaadc442017-11-06 12:48:48 +00004325 myvims[datacenter_key] = None
4326 else:
4327 myvims[datacenter_key] = vims.values()[0]
4328 myvim = myvims[datacenter_key]
4329 myvim_thread = myvim_threads[datacenter_key]
4330
4331 if not myvim:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004332 error_msg += "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net['vim_net_id'], net["datacenter_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004333 continue
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004334 extra = {"params": (net['vim_net_id'], net['sdn_net_id'])}
4335 if net2vm_dependencies.get(net["uuid"]):
4336 extra["depends_on"] = net2vm_dependencies[net["uuid"]]
4337 sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"]
4338 if len(sfi_dependencies) > 0:
4339 if "depends_on" in extra:
4340 extra["depends_on"] += sfi_dependencies
4341 else:
4342 extra["depends_on"] = sfi_dependencies
Igor D.Ccaadc442017-11-06 12:48:48 +00004343 db_vim_action = {
4344 "instance_action_id": instance_action_id,
4345 "task_index": task_index,
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004346 "datacenter_vim_id": net["datacenter_tenant_id"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004347 "action": "DELETE",
4348 "status": "SCHEDULED",
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004349 "item": "instance_nets",
4350 "item_id": net["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004351 "related": net["related"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004352 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4353 }
4354 task_index += 1
4355 db_vim_actions.append(db_vim_action)
4356
tierno868220c2017-09-26 00:11:05 +02004357 db_instance_action["number_tasks"] = task_index
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004358
4359 # --> WIM
4360 wim_actions, db_instance_action = (
4361 wim_engine.incorporate_actions(wim_actions, db_instance_action))
4362 # <-- WIM
4363
tierno868220c2017-09-26 00:11:05 +02004364 db_tables = [
4365 {"instance_actions": db_instance_action},
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004366 {"vim_wim_actions": db_vim_actions + wim_actions}
tierno868220c2017-09-26 00:11:05 +02004367 ]
4368
4369 logger.debug("delete_instance done DB tables: %s",
4370 yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
4371 mydb.new_rows(db_tables, ())
4372 for myvim_thread_id in vimthread_affected.keys():
4373 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
4374
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004375 wim_engine.dispatch(wim_actions)
4376
tiernob3d36742017-03-03 23:51:05 +01004377 if len(error_msg) > 0:
tierno868220c2017-09-26 00:11:05 +02004378 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
4379 '(error: 404) from VIM: {}'.format(instance_action_id, message, error_msg)
tierno7edb6752016-03-21 17:37:52 +01004380 else:
tierno868220c2017-09-26 00:11:05 +02004381 return "action_id={} instance {} deleted".format(instance_action_id, message)
tierno7edb6752016-03-21 17:37:52 +01004382
tierno7f426e92018-06-28 15:21:32 +02004383def get_instance_id(mydb, tenant_id, instance_id):
4384 global ovim
4385 #check valid tenant_id
4386 check_tenant(mydb, tenant_id)
4387 #obtain data
4388
4389 instance_dict = mydb.get_instance_scenario(instance_id, tenant_id, verbose=True)
4390 for net in instance_dict["nets"]:
4391 if net.get("sdn_net_id"):
4392 net_sdn = ovim.show_network(net["sdn_net_id"])
4393 net["sdn_info"] = {
4394 "admin_state_up": net_sdn.get("admin_state_up"),
4395 "flows": net_sdn.get("flows"),
4396 "last_error": net_sdn.get("last_error"),
4397 "ports": net_sdn.get("ports"),
4398 "type": net_sdn.get("type"),
4399 "status": net_sdn.get("status"),
4400 "vlan": net_sdn.get("vlan"),
4401 }
4402 return instance_dict
tiernob3d36742017-03-03 23:51:05 +01004403
tiernob8569aa2018-08-24 11:34:54 +02004404@deprecated("Instance is automatically refreshed by vim_threads")
tierno7edb6752016-03-21 17:37:52 +01004405def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
4406 '''Refreshes a scenario instance. It modifies instanceDict'''
4407 '''Returns:
4408 - 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
4409 - error_msg
4410 '''
tierno867ffe92017-03-27 12:50:34 +02004411 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
4412 # #print "nfvo.refresh_instance begins"
4413 # #print json.dumps(instanceDict, indent=4)
4414 #
4415 # #print "Getting the VIM URL and the VIM tenant_id"
4416 # myvims={}
4417 #
4418 # # 1. Getting VIM vm and net list
4419 # vms_updated = [] #List of VM instance uuids in openmano that were updated
4420 # vms_notupdated=[]
4421 # vm_list = {}
4422 # for sce_vnf in instanceDict['vnfs']:
4423 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4424 # if datacenter_key not in vm_list:
4425 # vm_list[datacenter_key] = []
4426 # if datacenter_key not in myvims:
4427 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
4428 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
4429 # if len(vims) == 0:
4430 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
4431 # myvims[datacenter_key] = None
4432 # else:
4433 # myvims[datacenter_key] = vims.values()[0]
4434 # for vm in sce_vnf['vms']:
4435 # vm_list[datacenter_key].append(vm['vim_vm_id'])
4436 # vms_notupdated.append(vm["uuid"])
4437 #
4438 # nets_updated = [] #List of VM instance uuids in openmano that were updated
4439 # nets_notupdated=[]
4440 # net_list = {}
4441 # for net in instanceDict['nets']:
4442 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4443 # if datacenter_key not in net_list:
4444 # net_list[datacenter_key] = []
4445 # if datacenter_key not in myvims:
4446 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
4447 # datacenter_tenant_id=net["datacenter_tenant_id"])
4448 # if len(vims) == 0:
4449 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
4450 # myvims[datacenter_key] = None
4451 # else:
4452 # myvims[datacenter_key] = vims.values()[0]
4453 #
4454 # net_list[datacenter_key].append(net['vim_net_id'])
4455 # nets_notupdated.append(net["uuid"])
4456 #
4457 # # 1. Getting the status of all VMs
4458 # vm_dict={}
4459 # for datacenter_key in myvims:
4460 # if not vm_list.get(datacenter_key):
4461 # continue
4462 # failed = True
4463 # failed_message=""
4464 # if not myvims[datacenter_key]:
4465 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4466 # else:
4467 # try:
4468 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
4469 # failed = False
4470 # except vimconn.vimconnException as e:
4471 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4472 # failed_message = str(e)
4473 # if failed:
4474 # for vm in vm_list[datacenter_key]:
4475 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4476 #
4477 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
4478 # for sce_vnf in instanceDict['vnfs']:
4479 # for vm in sce_vnf['vms']:
4480 # vm_id = vm['vim_vm_id']
4481 # interfaces = vm_dict[vm_id].pop('interfaces', [])
4482 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
4483 # has_mgmt_iface = False
4484 # for iface in vm["interfaces"]:
4485 # if iface["type"]=="mgmt":
4486 # has_mgmt_iface = True
4487 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
4488 # vm_dict[vm_id]['status'] = "ACTIVE"
4489 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
4490 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
4491 # 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'):
4492 # vm['status'] = vm_dict[vm_id]['status']
4493 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
4494 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
4495 # # 2.1. Update in openmano DB the VMs whose status changed
4496 # try:
4497 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
4498 # vms_notupdated.remove(vm["uuid"])
4499 # if updates>0:
4500 # vms_updated.append(vm["uuid"])
4501 # except db_base_Exception as e:
4502 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4503 # # 2.2. Update in openmano DB the interface VMs
4504 # for interface in interfaces:
4505 # #translate from vim_net_id to instance_net_id
4506 # network_id_list=[]
4507 # for net in instanceDict['nets']:
4508 # if net["vim_net_id"] == interface["vim_net_id"]:
4509 # network_id_list.append(net["uuid"])
4510 # if not network_id_list:
4511 # continue
4512 # del interface["vim_net_id"]
4513 # try:
4514 # for network_id in network_id_list:
4515 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
4516 # except db_base_Exception as e:
4517 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
4518 #
4519 # # 3. Getting the status of all nets
4520 # net_dict = {}
4521 # for datacenter_key in myvims:
4522 # if not net_list.get(datacenter_key):
4523 # continue
4524 # failed = True
4525 # failed_message = ""
4526 # if not myvims[datacenter_key]:
4527 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4528 # else:
4529 # try:
4530 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
4531 # failed = False
4532 # except vimconn.vimconnException as e:
4533 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4534 # failed_message = str(e)
4535 # if failed:
4536 # for net in net_list[datacenter_key]:
4537 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4538 #
4539 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
4540 # # TODO: update nets inside a vnf
4541 # for net in instanceDict['nets']:
4542 # net_id = net['vim_net_id']
4543 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
4544 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
4545 # 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'):
4546 # net['status'] = net_dict[net_id]['status']
4547 # net['error_msg'] = net_dict[net_id].get('error_msg')
4548 # net['vim_info'] = net_dict[net_id].get('vim_info')
4549 # # 5.1. Update in openmano DB the nets whose status changed
4550 # try:
4551 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
4552 # nets_notupdated.remove(net["uuid"])
4553 # if updated>0:
4554 # nets_updated.append(net["uuid"])
4555 # except db_base_Exception as e:
4556 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4557 #
4558 # # Returns appropriate output
4559 # #print "nfvo.refresh_instance finishes"
4560 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
4561 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
tierno7edb6752016-03-21 17:37:52 +01004562 instance_id = instanceDict['uuid']
tierno867ffe92017-03-27 12:50:34 +02004563 # if len(vms_notupdated)+len(nets_notupdated)>0:
4564 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
4565 # 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 +01004566
tiernoae4a8d12016-07-08 12:30:39 +02004567 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
tierno7edb6752016-03-21 17:37:52 +01004568
4569def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
tiernoae4a8d12016-07-08 12:30:39 +02004570 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02004571 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01004572 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4573
tiernoae4a8d12016-07-08 12:30:39 +02004574 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernof97fd272016-07-11 14:32:37 +02004575 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
4576 if len(vims) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004577 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01004578 myvim = vims.values()[0]
tiernofc5f80b2018-05-29 16:00:43 +02004579 vm_result = {}
4580 vm_error = 0
4581 vm_ok = 0
tierno42026a02017-02-10 15:13:40 +01004582
tiernofc5f80b2018-05-29 16:00:43 +02004583 myvim_threads_id = {}
4584 if action_dict.get("vdu-scaling"):
4585 db_instance_vms = []
4586 db_vim_actions = []
4587 db_instance_interfaces = []
4588 instance_action_id = get_task_id()
4589 db_instance_action = {
4590 "uuid": instance_action_id, # same uuid for the instance and the action on create
4591 "tenant_id": nfvo_tenant,
4592 "instance_id": instance_id,
4593 "description": "SCALE",
4594 }
4595 vm_result["instance_action_id"] = instance_action_id
tierno67881db2018-10-24 18:46:03 +02004596 vm_result["created"] = []
4597 vm_result["deleted"] = []
tiernofc5f80b2018-05-29 16:00:43 +02004598 task_index = 0
4599 for vdu in action_dict["vdu-scaling"]:
tierno868220c2017-09-26 00:11:05 +02004600 vdu_id = vdu.get("vdu-id")
tiernofc5f80b2018-05-29 16:00:43 +02004601 osm_vdu_id = vdu.get("osm_vdu_id")
4602 member_vnf_index = vdu.get("member-vnf-index")
tierno868220c2017-09-26 00:11:05 +02004603 vdu_count = vdu.get("count", 1)
tiernofc5f80b2018-05-29 16:00:43 +02004604 if vdu_id:
tierno67881db2018-10-24 18:46:03 +02004605 target_vms = mydb.get_rows(
tiernofc5f80b2018-05-29 16:00:43 +02004606 FROM="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid",
4607 WHERE={"vms.uuid": vdu_id},
4608 ORDER_BY="vms.created_at"
4609 )
tierno67881db2018-10-24 18:46:03 +02004610 if not target_vms:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004611 raise NfvoException("Cannot find the vdu with id {}".format(vdu_id), httperrors.Not_Found)
tiernofc5f80b2018-05-29 16:00:43 +02004612 else:
4613 if not osm_vdu_id and not member_vnf_index:
tiernoa43bd9e2018-11-26 09:28:58 +00004614 raise NfvoException("Invalid input vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'")
tierno67881db2018-10-24 18:46:03 +02004615 target_vms = mydb.get_rows(
tiernofc5f80b2018-05-29 16:00:43 +02004616 # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"),
4617 FROM="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\
4618 " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\
4619 " join vms on ivms.vm_id=vms.uuid",
tiernoa43bd9e2018-11-26 09:28:58 +00004620 WHERE={"vms.osm_id": osm_vdu_id, "svnfs.member_vnf_index": member_vnf_index,
4621 "ivnfs.instance_scenario_id": instance_id},
tiernofc5f80b2018-05-29 16:00:43 +02004622 ORDER_BY="ivms.created_at"
4623 )
tierno67881db2018-10-24 18:46:03 +02004624 if not target_vms:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004625 raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id, member_vnf_index), httperrors.Not_Found)
tierno67881db2018-10-24 18:46:03 +02004626 vdu_id = target_vms[-1]["uuid"]
4627 target_vm = target_vms[-1]
tiernofc5f80b2018-05-29 16:00:43 +02004628 datacenter = target_vm["datacenter_id"]
4629 myvim_threads_id[datacenter], _ = get_vim_thread(mydb, nfvo_tenant, datacenter)
tiernofc5f80b2018-05-29 16:00:43 +02004630
tierno67881db2018-10-24 18:46:03 +02004631 if vdu["type"] == "delete":
4632 for index in range(0, vdu_count):
4633 target_vm = target_vms[-1-index]
4634 vdu_id = target_vm["uuid"]
4635 # look for nm
4636 vm_interfaces = None
4637 for sce_vnf in instanceDict['vnfs']:
4638 for vm in sce_vnf['vms']:
4639 if vm["uuid"] == vdu_id:
tiernob5091bd2019-05-22 16:45:09 +00004640 # TODO revise this should not be vm["uuid"] instance_vms["vm_id"]
tierno67881db2018-10-24 18:46:03 +02004641 vm_interfaces = vm["interfaces"]
4642 break
4643
4644 db_vim_action = {
4645 "instance_action_id": instance_action_id,
4646 "task_index": task_index,
4647 "datacenter_vim_id": target_vm["datacenter_tenant_id"],
4648 "action": "DELETE",
4649 "status": "SCHEDULED",
4650 "item": "instance_vms",
4651 "item_id": vdu_id,
tiernob5091bd2019-05-22 16:45:09 +00004652 "related": target_vm["related"],
tierno67881db2018-10-24 18:46:03 +02004653 "extra": yaml.safe_dump({"params": vm_interfaces},
4654 default_flow_style=True, width=256)
4655 }
4656 task_index += 1
4657 db_vim_actions.append(db_vim_action)
4658 vm_result["deleted"].append(vdu_id)
4659 # delete from database
4660 db_instance_vms.append({"TO-DELETE": vdu_id})
tiernofc5f80b2018-05-29 16:00:43 +02004661
4662 else: # vdu["type"] == "create":
4663 iface2iface = {}
4664 where = {"item": "instance_vms", "item_id": target_vm["uuid"], "action": "CREATE"}
4665
garciadeblas72cd59f2018-12-05 10:59:40 +01004666 vim_action_to_clone = mydb.get_rows(FROM="vim_wim_actions", WHERE=where)
tiernofc5f80b2018-05-29 16:00:43 +02004667 if not vim_action_to_clone:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004668 raise NfvoException("Cannot find the vim_action at database with {}".format(where), httperrors.Internal_Server_Error)
tiernofc5f80b2018-05-29 16:00:43 +02004669 vim_action_to_clone = vim_action_to_clone[0]
4670 extra = yaml.safe_load(vim_action_to_clone["extra"])
4671
4672 # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y
4673 # TODO do the same for flavor and image when available
4674 task_depends_on = []
4675 task_params = extra["params"]
4676 task_params_networks = deepcopy(task_params[5])
4677 for iface in task_params[5]:
4678 if iface["net_id"].startswith("TASK-"):
4679 if "." not in iface["net_id"]:
4680 task_depends_on.append("{}.{}".format(vim_action_to_clone["instance_action_id"],
4681 iface["net_id"][5:]))
4682 iface["net_id"] = "TASK-{}.{}".format(vim_action_to_clone["instance_action_id"],
4683 iface["net_id"][5:])
4684 else:
4685 task_depends_on.append(iface["net_id"][5:])
4686 if "mac_address" in iface:
4687 del iface["mac_address"]
4688
4689 vm_ifaces_to_clone = mydb.get_rows(FROM="instance_interfaces", WHERE={"instance_vm_id": target_vm["uuid"]})
4690 for index in range(0, vdu_count):
4691 vm_uuid = str(uuid4())
4692 vm_name = target_vm.get('vim_name')
4693 try:
4694 suffix = vm_name.rfind("-")
tierno67881db2018-10-24 18:46:03 +02004695 vm_name = vm_name[:suffix+1] + str(index + 1 + int(vm_name[suffix+1:]))
tiernofc5f80b2018-05-29 16:00:43 +02004696 except Exception:
4697 pass
4698 db_instance_vm = {
4699 "uuid": vm_uuid,
tiernob5091bd2019-05-22 16:45:09 +00004700 'related': vm_uuid,
tiernofc5f80b2018-05-29 16:00:43 +02004701 'instance_vnf_id': target_vm['instance_vnf_id'],
4702 'vm_id': target_vm['vm_id'],
tiernob5091bd2019-05-22 16:45:09 +00004703 'vim_name': vm_name,
tiernofc5f80b2018-05-29 16:00:43 +02004704 }
4705 db_instance_vms.append(db_instance_vm)
4706
4707 for vm_iface in vm_ifaces_to_clone:
4708 iface_uuid = str(uuid4())
4709 iface2iface[vm_iface["uuid"]] = iface_uuid
4710 db_vm_iface = {
4711 "uuid": iface_uuid,
4712 'instance_vm_id': vm_uuid,
4713 "instance_net_id": vm_iface["instance_net_id"],
4714 'interface_id': vm_iface['interface_id'],
4715 'type': vm_iface['type'],
4716 'floating_ip': vm_iface['floating_ip'],
4717 'port_security': vm_iface['port_security']
4718 }
4719 db_instance_interfaces.append(db_vm_iface)
4720 task_params_copy = deepcopy(task_params)
4721 for iface in task_params_copy[5]:
4722 iface["uuid"] = iface2iface[iface["uuid"]]
4723 # increment ip_address
4724 if "ip_address" in iface:
4725 ip = iface.get("ip_address")
4726 i = ip.rfind(".")
4727 if i > 0:
4728 try:
4729 i += 1
4730 ip = ip[i:] + str(int(ip[:i]) + 1)
4731 iface["ip_address"] = ip
4732 except:
4733 iface["ip_address"] = None
4734 if vm_name:
4735 task_params_copy[0] = vm_name
4736 db_vim_action = {
4737 "instance_action_id": instance_action_id,
4738 "task_index": task_index,
4739 "datacenter_vim_id": vim_action_to_clone["datacenter_vim_id"],
4740 "action": "CREATE",
4741 "status": "SCHEDULED",
4742 "item": "instance_vms",
4743 "item_id": vm_uuid,
tiernob5091bd2019-05-22 16:45:09 +00004744 "related": vm_uuid,
tiernofc5f80b2018-05-29 16:00:43 +02004745 # ALF
4746 # ALF
4747 # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd.
4748 # ALF
4749 # ALF
4750 "extra": yaml.safe_dump({"params": task_params_copy, "depends_on": task_depends_on}, default_flow_style=True, width=256)
4751 }
4752 task_index += 1
4753 db_vim_actions.append(db_vim_action)
tierno67881db2018-10-24 18:46:03 +02004754 vm_result["created"].append(vm_uuid)
tiernofc5f80b2018-05-29 16:00:43 +02004755
4756 db_instance_action["number_tasks"] = task_index
4757 db_tables = [
4758 {"instance_vms": db_instance_vms},
4759 {"instance_interfaces": db_instance_interfaces},
4760 {"instance_actions": db_instance_action},
4761 # TODO revise sfps
4762 # {"instance_sfis": db_instance_sfis},
4763 # {"instance_sfs": db_instance_sfs},
4764 # {"instance_classifications": db_instance_classifications},
4765 # {"instance_sfps": db_instance_sfps},
garciadeblasaba7a0d2018-12-05 12:42:35 +01004766 {"vim_wim_actions": db_vim_actions}
tiernofc5f80b2018-05-29 16:00:43 +02004767 ]
4768 logger.debug("create_vdu done DB tables: %s",
4769 yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
4770 mydb.new_rows(db_tables, [])
4771 for myvim_thread in myvim_threads_id.values():
4772 vim_threads["running"][myvim_thread].insert_task(db_vim_actions)
4773
4774 return vm_result
tierno7edb6752016-03-21 17:37:52 +01004775
4776 input_vnfs = action_dict.pop("vnfs", [])
4777 input_vms = action_dict.pop("vms", [])
tierno92c36fd2018-05-04 12:21:10 +02004778 action_over_all = True if not input_vnfs and not input_vms else False
tierno7edb6752016-03-21 17:37:52 +01004779 for sce_vnf in instanceDict['vnfs']:
4780 for vm in sce_vnf['vms']:
tierno92c36fd2018-05-04 12:21:10 +02004781 if not action_over_all and sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
4782 sce_vnf['member_vnf_index'] not in input_vnfs and \
tierno7e510052019-09-10 16:16:13 +00004783 vm['uuid'] not in input_vms and vm['name'] not in input_vms and \
4784 sce_vnf['member_vnf_index'] + "-" + vm['vdu_osm_id'] not in input_vms: # TODO conside vm_count_index
tierno92c36fd2018-05-04 12:21:10 +02004785 continue
tiernoae4a8d12016-07-08 12:30:39 +02004786 try:
gcalvinoe580c7d2017-09-22 14:09:51 +02004787 if "add_public_key" in action_dict:
gcalvinoe580c7d2017-09-22 14:09:51 +02004788 if sce_vnf.get('mgmt_access'):
4789 mgmt_access = yaml.load(sce_vnf['mgmt_access'])
tierno7e510052019-09-10 16:16:13 +00004790 if not input_vms and mgmt_access.get("vdu-id") != vm['vdu_osm_id']:
4791 continue
4792 default_user = mgmt_access.get("default-user")
4793 password = mgmt_access.get("password")
4794 if mgmt_access.get(vm['vdu_osm_id']):
4795 default_user = mgmt_access[vm['vdu_osm_id']].get("default-user", default_user)
4796 password = mgmt_access[vm['vdu_osm_id']].get("password", password)
4797
gcalvinoe580c7d2017-09-22 14:09:51 +02004798 tenant = mydb.get_rows_by_id('nfvo_tenants', nfvo_tenant)
tierno42026a02017-02-10 15:13:40 +01004799 try:
tierno7e510052019-09-10 16:16:13 +00004800 if 'ip_address' in vm:
gcalvinoe580c7d2017-09-22 14:09:51 +02004801 mgmt_ip = vm['ip_address'].split(';')
gcalvinoe580c7d2017-09-22 14:09:51 +02004802 priv_RO_key = decrypt_key(tenant[0]['encrypted_RO_priv_key'], tenant[0]['uuid'])
calvinosanch5db670b2019-10-13 15:52:33 +02004803 data = myvim.inject_user_key(mgmt_ip[0], action_dict.get('user', default_user),
gcalvinoe580c7d2017-09-22 14:09:51 +02004804 action_dict['add_public_key'],
4805 password=password, ro_key=priv_RO_key)
calvinosanch5db670b2019-10-13 15:52:33 +02004806 vm_result[ vm['uuid'] ] = {"vim_result": 200,
4807 "description": "Public key injected",
4808 "name":vm['name']
4809 }
4810
gcalvinoe580c7d2017-09-22 14:09:51 +02004811 except KeyError:
4812 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004813 httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02004814 else:
4815 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004816 httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02004817 else:
4818 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
4819 if "console" in action_dict:
4820 if not global_config["http_console_proxy"]:
tierno20fc2a22016-08-19 17:02:35 +02004821 vm_result[ vm['uuid'] ] = {"vim_result": 200,
4822 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4823 protocol=data["protocol"],
gcalvinoe580c7d2017-09-22 14:09:51 +02004824 ip = data["server"],
4825 port = data["port"],
tierno20fc2a22016-08-19 17:02:35 +02004826 suffix = data["suffix"]),
4827 "name":vm['name']
4828 }
4829 vm_ok +=1
gcalvinoe580c7d2017-09-22 14:09:51 +02004830 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004831 vm_result[ vm['uuid'] ] = {"vim_result": -httperrors.Unauthorized,
gcalvinoe580c7d2017-09-22 14:09:51 +02004832 "description": "this console is only reachable by local interface",
4833 "name":vm['name']
4834 }
tierno20fc2a22016-08-19 17:02:35 +02004835 vm_error+=1
gcalvinoe580c7d2017-09-22 14:09:51 +02004836 else:
4837 #print "console data", data
4838 try:
4839 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
4840 vm_result[ vm['uuid'] ] = {"vim_result": 200,
4841 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4842 protocol=data["protocol"],
4843 ip = global_config["http_console_host"],
4844 port = console_thread.port,
4845 suffix = data["suffix"]),
4846 "name":vm['name']
4847 }
4848 vm_ok +=1
4849 except NfvoException as e:
4850 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
4851 vm_error+=1
tierno20fc2a22016-08-19 17:02:35 +02004852
gcalvinoe580c7d2017-09-22 14:09:51 +02004853 else:
4854 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
4855 vm_ok +=1
tiernoae4a8d12016-07-08 12:30:39 +02004856 except vimconn.vimconnException as e:
4857 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
4858 vm_error+=1
tierno7edb6752016-03-21 17:37:52 +01004859
4860 if vm_ok==0: #all goes wrong
tierno351863c2016-07-23 01:46:03 +02004861 return vm_result
tierno7edb6752016-03-21 17:37:52 +01004862 else:
tierno351863c2016-07-23 01:46:03 +02004863 return vm_result
tierno42026a02017-02-10 15:13:40 +01004864
tierno868220c2017-09-26 00:11:05 +02004865def instance_action_get(mydb, nfvo_tenant, instance_id, action_id):
tierno16e3dd42018-04-24 12:52:40 +02004866 filter = {}
tierno868220c2017-09-26 00:11:05 +02004867 if nfvo_tenant and nfvo_tenant != "any":
4868 filter["tenant_id"] = nfvo_tenant
4869 if instance_id and instance_id != "any":
4870 filter["instance_id"] = instance_id
4871 if action_id:
4872 filter["uuid"] = action_id
4873 rows = mydb.get_rows(FROM="instance_actions", WHERE=filter)
tierno16e3dd42018-04-24 12:52:40 +02004874 if action_id:
4875 if not rows:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004876 raise NfvoException("Not found any action with this criteria", httperrors.Not_Found)
4877 vim_wim_actions = mydb.get_rows(FROM="vim_wim_actions", WHERE={"instance_action_id": action_id})
4878 rows[0]["vim_wim_actions"] = vim_wim_actions
tierno31e121f2018-12-03 12:04:48 +00004879 # for backward compatibility set vim_actions = vim_wim_actions
4880 rows[0]["vim_actions"] = vim_wim_actions
tiernofc5f80b2018-05-29 16:00:43 +02004881 return {"actions": rows}
tierno868220c2017-09-26 00:11:05 +02004882
tiernob3d36742017-03-03 23:51:05 +01004883
tierno7edb6752016-03-21 17:37:52 +01004884def create_or_use_console_proxy_thread(console_server, console_port):
4885 #look for a non-used port
4886 console_thread_key = console_server + ":" + str(console_port)
4887 if console_thread_key in global_config["console_thread"]:
4888 #global_config["console_thread"][console_thread_key].start_timeout()
tiernof97fd272016-07-11 14:32:37 +02004889 return global_config["console_thread"][console_thread_key]
tierno42026a02017-02-10 15:13:40 +01004890
tierno7edb6752016-03-21 17:37:52 +01004891 for port in global_config["console_port_iterator"]():
tierno20fc2a22016-08-19 17:02:35 +02004892 #print "create_or_use_console_proxy_thread() port:", port
tierno7edb6752016-03-21 17:37:52 +01004893 if port in global_config["console_ports"]:
4894 continue
4895 try:
4896 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
4897 clithread.start()
4898 global_config["console_thread"][console_thread_key] = clithread
4899 global_config["console_ports"][port] = console_thread_key
tiernof97fd272016-07-11 14:32:37 +02004900 return clithread
tierno7edb6752016-03-21 17:37:52 +01004901 except cli.ConsoleProxyExceptionPortUsed as e:
4902 #port used, try with onoher
4903 continue
4904 except cli.ConsoleProxyException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004905 raise NfvoException(str(e), httperrors.Bad_Request)
4906 raise NfvoException("Not found any free 'http_console_ports'", httperrors.Conflict)
tierno7edb6752016-03-21 17:37:52 +01004907
tiernob3d36742017-03-03 23:51:05 +01004908
tierno7edb6752016-03-21 17:37:52 +01004909def check_tenant(mydb, tenant_id):
4910 '''check that tenant exists at database'''
tiernof97fd272016-07-11 14:32:37 +02004911 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
4912 if not tenant:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004913 raise NfvoException("tenant '{}' not found".format(tenant_id), httperrors.Not_Found)
tiernof97fd272016-07-11 14:32:37 +02004914 return
tierno7edb6752016-03-21 17:37:52 +01004915
4916def new_tenant(mydb, tenant_dict):
tierno7edb6752016-03-21 17:37:52 +01004917
gcalvinoe580c7d2017-09-22 14:09:51 +02004918 tenant_uuid = str(uuid4())
4919 tenant_dict['uuid'] = tenant_uuid
4920 try:
4921 pub_key, priv_key = create_RO_keypair(tenant_uuid)
4922 tenant_dict['RO_pub_key'] = pub_key
4923 tenant_dict['encrypted_RO_priv_key'] = priv_key
gcalvinoc62cfa52017-10-05 18:21:25 +02004924 mydb.new_row("nfvo_tenants", tenant_dict, confidential_data=True)
gcalvinoe580c7d2017-09-22 14:09:51 +02004925 except db_base_Exception as e:
tierno9c5c8322018-03-23 15:44:03 +01004926 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict['name']) + str(e), e.http_code)
gcalvinoe580c7d2017-09-22 14:09:51 +02004927 return tenant_uuid
tiernob3d36742017-03-03 23:51:05 +01004928
tierno7edb6752016-03-21 17:37:52 +01004929def delete_tenant(mydb, tenant):
4930 #get nfvo_tenant info
tierno42026a02017-02-10 15:13:40 +01004931
tiernof97fd272016-07-11 14:32:37 +02004932 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
4933 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
4934 return tenant_dict['uuid'] + " " + tenant_dict["name"]
tierno7edb6752016-03-21 17:37:52 +01004935
tiernob3d36742017-03-03 23:51:05 +01004936
tierno7edb6752016-03-21 17:37:52 +01004937def new_datacenter(mydb, datacenter_descriptor):
tierno1c848c02018-05-21 16:40:33 +02004938 sdn_port_mapping = None
tierno7edb6752016-03-21 17:37:52 +01004939 if "config" in datacenter_descriptor:
tiernoedf3f4f2018-05-17 23:02:47 +02004940 sdn_port_mapping = datacenter_descriptor["config"].pop("sdn-port-mapping", None)
4941 datacenter_descriptor["config"] = yaml.safe_dump(datacenter_descriptor["config"], default_flow_style=True,
4942 width=256)
4943 # Check that datacenter-type is correct
tierno3ae39742016-09-07 12:17:51 +02004944 datacenter_type = datacenter_descriptor.get("type", "openvim");
tiernoedf3f4f2018-05-17 23:02:47 +02004945 # module_info = None
tierno3ae39742016-09-07 12:17:51 +02004946 try:
4947 module = "vimconn_" + datacenter_type
tierno361275f2017-04-25 16:24:34 +02004948 pkg = __import__("osm_ro." + module)
tiernoedf3f4f2018-05-17 23:02:47 +02004949 # vim_conn = getattr(pkg, module)
tierno361275f2017-04-25 16:24:34 +02004950 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
tierno3ae39742016-09-07 12:17:51 +02004951 except (IOError, ImportError):
tierno361275f2017-04-25 16:24:34 +02004952 # if module_info and module_info[0]:
4953 # file.close(module_info[0])
tiernoedf3f4f2018-05-17 23:02:47 +02004954 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type,
4955 module),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004956 httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +01004957
gcalvinoc62cfa52017-10-05 18:21:25 +02004958 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True, confidential_data=True)
tiernoedf3f4f2018-05-17 23:02:47 +02004959 if sdn_port_mapping:
4960 try:
4961 datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, sdn_port_mapping)
4962 except Exception as e:
4963 mydb.delete_row_by_id("datacenters", datacenter_id) # Rollback
4964 raise e
tiernof97fd272016-07-11 14:32:37 +02004965 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01004966
tiernob3d36742017-03-03 23:51:05 +01004967
tierno7edb6752016-03-21 17:37:52 +01004968def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
tierno8fe7a492017-07-11 13:50:04 +02004969 # obtain data, check that only one exist
tiernof97fd272016-07-11 14:32:37 +02004970 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
tierno8fe7a492017-07-11 13:50:04 +02004971
4972 # edit data
tiernof97fd272016-07-11 14:32:37 +02004973 datacenter_id = datacenter['uuid']
tiernod72182f2018-08-29 10:56:13 +02004974 where = {'uuid': datacenter['uuid']}
tierno8fe7a492017-07-11 13:50:04 +02004975 remove_port_mapping = False
tiernoedf3f4f2018-05-17 23:02:47 +02004976 new_sdn_port_mapping = None
tierno7edb6752016-03-21 17:37:52 +01004977 if "config" in datacenter_descriptor:
tierno8fe7a492017-07-11 13:50:04 +02004978 if datacenter_descriptor['config'] != None:
tierno7edb6752016-03-21 17:37:52 +01004979 try:
4980 new_config_dict = datacenter_descriptor["config"]
tiernoedf3f4f2018-05-17 23:02:47 +02004981 if "sdn-port-mapping" in new_config_dict:
4982 remove_port_mapping = True
4983 new_sdn_port_mapping = new_config_dict.pop("sdn-port-mapping")
tiernod72182f2018-08-29 10:56:13 +02004984 # delete null fields
4985 to_delete = []
tierno7edb6752016-03-21 17:37:52 +01004986 for k in new_config_dict:
tiernod72182f2018-08-29 10:56:13 +02004987 if new_config_dict[k] is None:
tierno7edb6752016-03-21 17:37:52 +01004988 to_delete.append(k)
tierno8fe7a492017-07-11 13:50:04 +02004989 if k == 'sdn-controller':
4990 remove_port_mapping = True
tierno42026a02017-02-10 15:13:40 +01004991
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004992 config_text = datacenter.get("config")
4993 if not config_text:
4994 config_text = '{}'
4995 config_dict = yaml.load(config_text)
tierno7edb6752016-03-21 17:37:52 +01004996 config_dict.update(new_config_dict)
tiernod72182f2018-08-29 10:56:13 +02004997 # delete null fields
tierno7edb6752016-03-21 17:37:52 +01004998 for k in to_delete:
4999 del config_dict[k]
tiernof97fd272016-07-11 14:32:37 +02005000 except Exception as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005001 raise NfvoException("Bad format at datacenter:config " + str(e), httperrors.Bad_Request)
tierno8fe7a492017-07-11 13:50:04 +02005002 if config_dict:
5003 datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256)
5004 else:
5005 datacenter_descriptor["config"] = None
5006 if remove_port_mapping:
5007 try:
5008 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id)
5009 except ovimException as e:
Anderson Bravalheric5293de2018-11-28 17:21:26 +00005010 raise NfvoException("Error deleting datacenter-port-mapping " + str(e), httperrors.Conflict)
tierno8fe7a492017-07-11 13:50:04 +02005011
tiernof97fd272016-07-11 14:32:37 +02005012 mydb.update_rows('datacenters', datacenter_descriptor, where)
tiernoedf3f4f2018-05-17 23:02:47 +02005013 if new_sdn_port_mapping:
5014 try:
5015 datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, new_sdn_port_mapping)
5016 except ovimException as e:
tiernod72182f2018-08-29 10:56:13 +02005017 # Rollback
5018 mydb.update_rows('datacenters', datacenter, where)
Anderson Bravalheric5293de2018-11-28 17:21:26 +00005019 raise NfvoException("Error adding datacenter-port-mapping " + str(e), httperrors.Conflict)
tiernof97fd272016-07-11 14:32:37 +02005020 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01005021
tiernob3d36742017-03-03 23:51:05 +01005022
tierno7edb6752016-03-21 17:37:52 +01005023def delete_datacenter(mydb, datacenter):
5024 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02005025 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
5026 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
tierno8fe7a492017-07-11 13:50:04 +02005027 try:
5028 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid'])
5029 except ovimException as e:
tiernod72182f2018-08-29 10:56:13 +02005030 raise NfvoException("Error deleting datacenter-port-mapping " + str(e))
tiernof97fd272016-07-11 14:32:37 +02005031 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
tierno7edb6752016-03-21 17:37:52 +01005032
tiernob3d36742017-03-03 23:51:05 +01005033
tiernod3750b32018-07-20 15:33:08 +02005034def create_vim_account(mydb, nfvo_tenant, datacenter_id, name=None, vim_id=None, vim_tenant=None, vim_tenant_name=None,
5035 vim_username=None, vim_password=None, config=None):
tierno9c22f2d2017-10-09 16:23:55 +02005036 # get datacenter info
tierno0ea2a7e2017-10-18 00:06:26 +02005037 try:
tiernod3750b32018-07-20 15:33:08 +02005038 if not datacenter_id:
5039 if not vim_id:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005040 raise NfvoException("You must provide 'vim_id", http_code=httperrors.Bad_Request)
tiernod3750b32018-07-20 15:33:08 +02005041 datacenter_id = vim_id
5042 datacenter_id, datacenter_name = get_datacenter_uuid(mydb, None, datacenter_id)
tierno7edb6752016-03-21 17:37:52 +01005043
tiernod3750b32018-07-20 15:33:08 +02005044 create_vim_tenant = True if not vim_tenant and not vim_tenant_name else False
tierno42026a02017-02-10 15:13:40 +01005045
tierno0ea2a7e2017-10-18 00:06:26 +02005046 # get nfvo_tenant info
5047 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
5048 if vim_tenant_name==None:
5049 vim_tenant_name=tenant_dict['name']
tierno42026a02017-02-10 15:13:40 +01005050
tierno0ea2a7e2017-10-18 00:06:26 +02005051 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
tiernod3750b32018-07-20 15:33:08 +02005052 # #check that this association does not exist before
5053 # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
5054 # if len(tenants_datacenters)>0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005055 # raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Conflict)
tierno7edb6752016-03-21 17:37:52 +01005056
tierno0ea2a7e2017-10-18 00:06:26 +02005057 vim_tenant_id_exist_atdb=False
5058 if not create_vim_tenant:
5059 where_={"datacenter_id": datacenter_id}
tiernod3750b32018-07-20 15:33:08 +02005060 if vim_tenant!=None:
5061 where_["vim_tenant_id"] = vim_tenant
tierno0ea2a7e2017-10-18 00:06:26 +02005062 if vim_tenant_name!=None:
5063 where_["vim_tenant_name"] = vim_tenant_name
5064 #check if vim_tenant_id is already at database
5065 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
5066 if len(datacenter_tenants_dict)>=1:
5067 datacenter_tenants_dict = datacenter_tenants_dict[0]
5068 vim_tenant_id_exist_atdb=True
5069 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
5070 else: #result=0
5071 datacenter_tenants_dict = {}
5072 #insert at table datacenter_tenants
tiernod3750b32018-07-20 15:33:08 +02005073 else: #if vim_tenant==None:
tierno0ea2a7e2017-10-18 00:06:26 +02005074 #create tenant at VIM if not provided
5075 try:
5076 _, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username,
5077 vim_passwd=vim_password)
5078 datacenter_name = myvim["name"]
tiernod3750b32018-07-20 15:33:08 +02005079 vim_tenant = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
tierno0ea2a7e2017-10-18 00:06:26 +02005080 except vimconn.vimconnException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005081 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id, str(e)), httperrors.Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01005082 datacenter_tenants_dict = {}
tierno0ea2a7e2017-10-18 00:06:26 +02005083 datacenter_tenants_dict["created"]="true"
tierno42026a02017-02-10 15:13:40 +01005084
tierno0ea2a7e2017-10-18 00:06:26 +02005085 #fill datacenter_tenants table
5086 if not vim_tenant_id_exist_atdb:
tiernod3750b32018-07-20 15:33:08 +02005087 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant
tierno0ea2a7e2017-10-18 00:06:26 +02005088 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
5089 datacenter_tenants_dict["user"] = vim_username
5090 datacenter_tenants_dict["passwd"] = vim_password
5091 datacenter_tenants_dict["datacenter_id"] = datacenter_id
tiernod3750b32018-07-20 15:33:08 +02005092 if name:
5093 datacenter_tenants_dict["name"] = name
5094 else:
5095 datacenter_tenants_dict["name"] = datacenter_name
tierno0ea2a7e2017-10-18 00:06:26 +02005096 if config:
5097 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
5098 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True, confidential_data=True)
5099 datacenter_tenants_dict["uuid"] = id_
tierno42026a02017-02-10 15:13:40 +01005100
tierno0ea2a7e2017-10-18 00:06:26 +02005101 #fill tenants_datacenters table
5102 datacenter_tenant_id = datacenter_tenants_dict["uuid"]
5103 tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id
5104 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
tiernod3750b32018-07-20 15:33:08 +02005105
tierno0ea2a7e2017-10-18 00:06:26 +02005106 # create thread
tierno0ea2a7e2017-10-18 00:06:26 +02005107 thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid'])
tiernod3750b32018-07-20 15:33:08 +02005108 new_thread = vim_thread.vim_thread(task_lock, thread_name, datacenter_name, datacenter_tenant_id,
tierno0ea2a7e2017-10-18 00:06:26 +02005109 db=db, db_lock=db_lock, ovim=ovim)
5110 new_thread.start()
5111 thread_id = datacenter_tenants_dict["uuid"]
5112 vim_threads["running"][thread_id] = new_thread
tiernod3750b32018-07-20 15:33:08 +02005113 return thread_id
tierno0ea2a7e2017-10-18 00:06:26 +02005114 except vimconn.vimconnException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005115 raise NfvoException(str(e), httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01005116
tierno99314902017-04-26 13:23:09 +02005117
tiernod3750b32018-07-20 15:33:08 +02005118def edit_vim_account(mydb, nfvo_tenant, datacenter_tenant_id, datacenter_id=None, name=None, vim_tenant=None,
5119 vim_tenant_name=None, vim_username=None, vim_password=None, config=None):
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005120
tiernod3750b32018-07-20 15:33:08 +02005121 # get vim_account; check is valid for this tenant
5122 from_ = "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id"
5123 where_ = {"td.nfvo_tenant_id": nfvo_tenant}
5124 if datacenter_tenant_id:
5125 where_["dt.uuid"] = datacenter_tenant_id
5126 if datacenter_id:
5127 where_["dt.datacenter_id"] = datacenter_id
5128 vim_accounts = mydb.get_rows(SELECT="dt.uuid as uuid, config", FROM=from_, WHERE=where_)
5129 if not vim_accounts:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005130 raise NfvoException("vim_account not found for this tenant", http_code=httperrors.Not_Found)
tiernod3750b32018-07-20 15:33:08 +02005131 elif len(vim_accounts) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005132 raise NfvoException("found more than one vim_account for this tenant", http_code=httperrors.Conflict)
tiernod3750b32018-07-20 15:33:08 +02005133 datacenter_tenant_id = vim_accounts[0]["uuid"]
5134 original_config = vim_accounts[0]["config"]
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005135
tiernod3750b32018-07-20 15:33:08 +02005136 update_ = {}
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005137 if config:
tiernod3750b32018-07-20 15:33:08 +02005138 original_config_dict = yaml.load(original_config)
5139 original_config_dict.update(config)
5140 update["config"] = yaml.safe_dump(original_config_dict, default_flow_style=True, width=256)
5141 if name:
5142 update_['name'] = name
5143 if vim_tenant:
5144 update_['vim_tenant_id'] = vim_tenant
5145 if vim_tenant_name:
5146 update_['vim_tenant_name'] = vim_tenant_name
5147 if vim_username:
5148 update_['user'] = vim_username
5149 if vim_password:
5150 update_['passwd'] = vim_password
5151 if update_:
5152 mydb.update_rows("datacenter_tenants", UPDATE=update_, WHERE={"uuid": datacenter_tenant_id})
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005153
tiernod3750b32018-07-20 15:33:08 +02005154 vim_threads["running"][datacenter_tenant_id].insert_task("reload")
5155 return datacenter_tenant_id
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005156
tiernod3750b32018-07-20 15:33:08 +02005157def delete_vim_account(mydb, tenant_id, vim_account_id, datacenter=None):
tierno7edb6752016-03-21 17:37:52 +01005158 #get nfvo_tenant info
5159 if not tenant_id or tenant_id=="any":
5160 tenant_uuid = None
5161 else:
tiernof97fd272016-07-11 14:32:37 +02005162 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
tierno7edb6752016-03-21 17:37:52 +01005163 tenant_uuid = tenant_dict['uuid']
5164
5165 #check that this association exist before
tiernod3750b32018-07-20 15:33:08 +02005166 tenants_datacenter_dict = {}
5167 if datacenter:
5168 datacenter_id, _ = get_datacenter_uuid(mydb, tenant_uuid, datacenter)
5169 tenants_datacenter_dict["datacenter_id"] = datacenter_id
5170 elif vim_account_id:
5171 tenants_datacenter_dict["datacenter_tenant_id"] = vim_account_id
tierno7edb6752016-03-21 17:37:52 +01005172 if tenant_uuid:
5173 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
tiernof97fd272016-07-11 14:32:37 +02005174 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
5175 if len(tenant_datacenter_list)==0 and tenant_uuid:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005176 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01005177
5178 #delete this association
tiernof97fd272016-07-11 14:32:37 +02005179 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
tierno7edb6752016-03-21 17:37:52 +01005180
5181 #get vim_tenant info and deletes
5182 warning=''
5183 for tenant_datacenter_item in tenant_datacenter_list:
tiernof97fd272016-07-11 14:32:37 +02005184 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
5185 #try to delete vim:tenant
5186 try:
5187 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
5188 if vim_tenant_dict['created']=='true':
tierno7edb6752016-03-21 17:37:52 +01005189 #delete tenant at VIM if created by NFVO
tierno42026a02017-02-10 15:13:40 +01005190 try:
tierno0ea2a7e2017-10-18 00:06:26 +02005191 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02005192 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
5193 except vimconn.vimconnException as e:
5194 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
5195 logger.warn(warning)
tiernof97fd272016-07-11 14:32:37 +02005196 except db_base_Exception as e:
5197 logger.error("Cannot delete datacenter_tenants " + str(e))
tierno42026a02017-02-10 15:13:40 +01005198 pass # the error will be caused because dependencies, vim_tenant can not be deleted
tierno867ffe92017-03-27 12:50:34 +02005199 thread_id = tenant_datacenter_item["datacenter_tenant_id"]
tiernoa3572692018-05-14 13:09:33 +02005200 thread = vim_threads["running"].get(thread_id)
5201 if thread:
5202 thread.insert_task("exit")
5203 vim_threads["deleting"][thread_id] = thread
tiernof97fd272016-07-11 14:32:37 +02005204 return "datacenter {} detached. {}".format(datacenter_id, warning)
tierno7edb6752016-03-21 17:37:52 +01005205
tiernob3d36742017-03-03 23:51:05 +01005206
tierno7edb6752016-03-21 17:37:52 +01005207def datacenter_action(mydb, tenant_id, datacenter, action_dict):
5208 #DEPRECATED
tierno42026a02017-02-10 15:13:40 +01005209 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005210 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005211
tierno5509c2e2019-07-04 16:23:20 +00005212 if 'check-connectivity' in action_dict:
5213 try:
5214 myvim.check_vim_connectivity()
5215 except vimconn.vimconnException as e:
5216 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
5217 raise NfvoException(str(e), e.http_code)
5218 elif 'net-update' in action_dict:
tiernoae4a8d12016-07-08 12:30:39 +02005219 try:
tiernof97fd272016-07-11 14:32:37 +02005220 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
tiernoae4a8d12016-07-08 12:30:39 +02005221 #print content
5222 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005223 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005224 raise NfvoException(str(e), httperrors.Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01005225 #update nets Change from VIM format to NFVO format
5226 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02005227 for net in nets:
tierno7edb6752016-03-21 17:37:52 +01005228 net_nfvo={'datacenter_id': datacenter_id}
5229 net_nfvo['name'] = net['name']
5230 #net_nfvo['description']= net['name']
5231 net_nfvo['vim_net_id'] = net['id']
5232 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5233 net_nfvo['shared'] = net['shared']
5234 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
5235 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02005236 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
5237 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
5238 return inserted
tierno7edb6752016-03-21 17:37:52 +01005239 elif 'net-edit' in action_dict:
5240 net = action_dict['net-edit'].pop('net')
tierno42fcc3b2016-07-06 17:20:40 +02005241 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01005242 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
tierno7edb6752016-03-21 17:37:52 +01005243 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02005244 return result
tierno7edb6752016-03-21 17:37:52 +01005245 elif 'net-delete' in action_dict:
5246 net = action_dict['net-deelte'].get('net')
tierno42fcc3b2016-07-06 17:20:40 +02005247 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01005248 result = mydb.delete_row(FROM='datacenter_nets',
tierno7edb6752016-03-21 17:37:52 +01005249 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02005250 return result
tierno7edb6752016-03-21 17:37:52 +01005251
5252 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005253 raise NfvoException("Unknown action " + str(action_dict), httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01005254
tiernob3d36742017-03-03 23:51:05 +01005255
tierno7edb6752016-03-21 17:37:52 +01005256def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
5257 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005258 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005259
tierno42fcc3b2016-07-06 17:20:40 +02005260 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
tierno42026a02017-02-10 15:13:40 +01005261 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
tierno7edb6752016-03-21 17:37:52 +01005262 WHERE={'datacenter_id':datacenter_id, what: netmap})
tiernof97fd272016-07-11 14:32:37 +02005263 return result
tierno7edb6752016-03-21 17:37:52 +01005264
tiernob3d36742017-03-03 23:51:05 +01005265
tierno7edb6752016-03-21 17:37:52 +01005266def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
5267 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005268 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005269 filter_dict={}
5270 if action_dict:
5271 action_dict = action_dict["netmap"]
5272 if 'vim_id' in action_dict:
5273 filter_dict["id"] = action_dict['vim_id']
5274 if 'vim_name' in action_dict:
5275 filter_dict["name"] = action_dict['vim_name']
5276 else:
5277 filter_dict["shared"] = True
tierno42026a02017-02-10 15:13:40 +01005278
tiernoae4a8d12016-07-08 12:30:39 +02005279 try:
tiernof97fd272016-07-11 14:32:37 +02005280 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02005281 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005282 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005283 raise NfvoException(str(e), httperrors.Internal_Server_Error)
tiernof97fd272016-07-11 14:32:37 +02005284 if len(vim_nets)>1 and action_dict:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005285 raise NfvoException("more than two networks found, specify with vim_id", httperrors.Conflict)
tiernof97fd272016-07-11 14:32:37 +02005286 elif len(vim_nets)==0: # and action_dict:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005287 raise NfvoException("Not found a network at VIM with " + str(filter_dict), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01005288 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02005289 for net in vim_nets:
tierno7edb6752016-03-21 17:37:52 +01005290 net_nfvo={'datacenter_id': datacenter_id}
5291 if action_dict and "name" in action_dict:
5292 net_nfvo['name'] = action_dict['name']
5293 else:
5294 net_nfvo['name'] = net['name']
5295 #net_nfvo['description']= net['name']
5296 net_nfvo['vim_net_id'] = net['id']
5297 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5298 net_nfvo['shared'] = net['shared']
5299 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
tiernof97fd272016-07-11 14:32:37 +02005300 try:
5301 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01005302 net_nfvo["status"] = "OK"
tiernof97fd272016-07-11 14:32:37 +02005303 net_nfvo["uuid"] = net_id
5304 except db_base_Exception as e:
5305 if action_dict:
5306 raise
5307 else:
5308 net_nfvo["status"] = "FAIL: " + str(e)
tierno42026a02017-02-10 15:13:40 +01005309 net_list.append(net_nfvo)
5310 return net_list
tierno7edb6752016-03-21 17:37:52 +01005311
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005312def get_sdn_net_id(mydb, tenant_id, datacenter, network_id):
5313 # obtain all network data
5314 try:
5315 if utils.check_valid_uuid(network_id):
5316 filter_dict = {"id": network_id}
5317 else:
5318 filter_dict = {"name": network_id}
5319
5320 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
5321 network = myvim.get_network_list(filter_dict=filter_dict)
5322 except vimconn.vimconnException as e:
tiernof1ba57e2017-09-07 12:23:19 +02005323 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 +02005324
5325 # ensure the network is defined
5326 if len(network) == 0:
5327 raise NfvoException("Network {} is not present in the system".format(network_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005328 httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005329
5330 # ensure there is only one network with the provided name
5331 if len(network) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005332 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005333
5334 # ensure it is a dataplane network
5335 if network[0]['type'] != 'data':
5336 return None
5337
5338 # ensure we use the id
5339 network_id = network[0]['id']
5340
5341 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
5342 # and with instance_scenario_id==NULL
5343 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
5344 search_dict = {'vim_net_id': network_id}
5345
5346 try:
5347 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
5348 result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)
5349 except db_base_Exception as e:
5350 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
tierno9c5c8322018-03-23 15:44:03 +01005351 network_id) + str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005352
5353 sdn_net_counter = 0
5354 for net in result:
5355 if net['sdn_net_id'] != None:
5356 sdn_net_counter+=1
5357 sdn_net_id = net['sdn_net_id']
5358
5359 if sdn_net_counter == 0:
5360 return None
5361 elif sdn_net_counter == 1:
5362 return sdn_net_id
5363 else:
5364 raise NfvoException("More than one SDN network is associated to vim network {}".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005365 network_id), httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005366
5367def get_sdn_controller_id(mydb, datacenter):
5368 # Obtain sdn controller id
5369 config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}')
5370 if not config:
5371 return None
5372
5373 return yaml.load(config).get('sdn-controller')
5374
5375def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor):
5376 try:
5377 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
5378 if not sdn_network_id:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005379 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005380
5381 #Obtain sdn controller id
5382 controller_id = get_sdn_controller_id(mydb, datacenter)
5383 if not controller_id:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005384 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005385
5386 #Obtain sdn controller info
5387 sdn_controller = ovim.show_of_controller(controller_id)
5388
5389 port_data = {
5390 'name': 'external_port',
5391 'net_id': sdn_network_id,
5392 'ofc_id': controller_id,
5393 'switch_dpid': sdn_controller['dpid'],
5394 'switch_port': descriptor['port']
5395 }
5396
5397 if 'vlan' in descriptor:
5398 port_data['vlan'] = descriptor['vlan']
5399 if 'mac' in descriptor:
5400 port_data['mac'] = descriptor['mac']
5401
5402 result = ovim.new_port(port_data)
5403 except ovimException as e:
5404 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005405 sdn_network_id, network_id) + str(e), httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005406 except db_base_Exception as e:
5407 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
tierno9c5c8322018-03-23 15:44:03 +01005408 network_id) + str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005409
5410 return 'Port uuid: '+ result
5411
5412def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None):
5413 if port_id:
5414 filter = {'uuid': port_id}
5415 else:
5416 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
5417 if not sdn_network_id:
5418 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005419 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005420 #in case no port_id is specified only ports marked as 'external_port' will be detached
5421 filter = {'name': 'external_port', 'net_id': sdn_network_id}
5422
5423 try:
5424 port_list = ovim.get_ports(columns={'uuid'}, filter=filter)
5425 except ovimException as e:
5426 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005427 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005428
5429 if len(port_list) == 0:
5430 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005431 httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005432
5433 port_uuid_list = []
5434 for port in port_list:
5435 try:
5436 port_uuid_list.append(port['uuid'])
5437 ovim.delete_port(port['uuid'])
5438 except ovimException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005439 raise NfvoException("ovimException deleting port {} for net {}. ".format(port['uuid'], network_id) + str(e), httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005440
5441 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list))
tiernob3d36742017-03-03 23:51:05 +01005442
tierno7edb6752016-03-21 17:37:52 +01005443def vim_action_get(mydb, tenant_id, datacenter, item, name):
5444 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005445 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005446 filter_dict={}
5447 if name:
tierno42fcc3b2016-07-06 17:20:40 +02005448 if utils.check_valid_uuid(name):
tierno7edb6752016-03-21 17:37:52 +01005449 filter_dict["id"] = name
5450 else:
5451 filter_dict["name"] = name
tiernoae4a8d12016-07-08 12:30:39 +02005452 try:
5453 if item=="networks":
5454 #filter_dict['tenant_id'] = myvim['tenant_id']
5455 content = myvim.get_network_list(filter_dict=filter_dict)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005456
5457 if len(content) == 0:
5458 raise NfvoException("Network {} is not present in the system. ".format(name),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005459 httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005460
5461 #Update the networks with the attached ports
5462 for net in content:
5463 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id'])
5464 if sdn_network_id != None:
5465 try:
5466 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
5467 port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id})
5468 except ovimException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005469 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005470 #Remove field name and if port name is external_port save it as 'type'
5471 for port in port_list:
5472 if port['name'] == 'external_port':
5473 port['type'] = "External"
5474 del port['name']
5475 net['sdn_network_id'] = sdn_network_id
5476 net['sdn_attached_ports'] = port_list
5477
tiernoae4a8d12016-07-08 12:30:39 +02005478 elif item=="tenants":
5479 content = myvim.get_tenant_list(filter_dict=filter_dict)
tierno4540ea52017-01-18 17:44:32 +01005480 elif item == "images":
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005481
tierno4540ea52017-01-18 17:44:32 +01005482 content = myvim.get_image_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02005483 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005484 raise NfvoException(item + "?", httperrors.Method_Not_Allowed)
tiernobe41e222016-09-02 15:16:13 +02005485 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
tiernoae4a8d12016-07-08 12:30:39 +02005486 if name and len(content)==1:
tiernof97fd272016-07-11 14:32:37 +02005487 return {item[:-1]: content[0]}
tiernoae4a8d12016-07-08 12:30:39 +02005488 elif name and len(content)==0:
tiernof97fd272016-07-11 14:32:37 +02005489 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 +02005490 datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02005491 else:
tiernof97fd272016-07-11 14:32:37 +02005492 return {item: content}
tiernoae4a8d12016-07-08 12:30:39 +02005493 except vimconn.vimconnException as e:
5494 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
tiernof97fd272016-07-11 14:32:37 +02005495 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
tierno42026a02017-02-10 15:13:40 +01005496
tiernob3d36742017-03-03 23:51:05 +01005497
tierno7edb6752016-03-21 17:37:52 +01005498def vim_action_delete(mydb, tenant_id, datacenter, item, name):
5499 #get datacenter info
tierno392f2852016-05-13 12:28:55 +02005500 if tenant_id == "any":
5501 tenant_id=None
5502
tiernoa2793912016-10-04 08:15:08 +00005503 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno392f2852016-05-13 12:28:55 +02005504 #get uuid name
tiernof97fd272016-07-11 14:32:37 +02005505 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
5506 logger.debug("vim_action_delete vim response: " + str(content))
tierno392f2852016-05-13 12:28:55 +02005507 items = content.values()[0]
5508 if type(items)==list and len(items)==0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005509 raise NfvoException("Not found " + item, httperrors.Not_Found)
tierno392f2852016-05-13 12:28:55 +02005510 elif type(items)==list and len(items)>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005511 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), httperrors.Not_Found)
tierno392f2852016-05-13 12:28:55 +02005512 else: # it is a dict
5513 item_id = items["id"]
5514 item_name = str(items.get("name"))
tierno42026a02017-02-10 15:13:40 +01005515
tiernoae4a8d12016-07-08 12:30:39 +02005516 try:
5517 if item=="networks":
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005518 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
5519 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id)
5520 if sdn_network_id != None:
5521 #Delete any port attachment to this network
5522 try:
5523 port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id})
5524 except ovimException as e:
5525 raise NfvoException(
5526 "ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005527 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005528
5529 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
5530 for port in port_list:
5531 vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid'])
5532
5533 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
5534 try:
5535 mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id})
5536 except db_base_Exception as e:
5537 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) +
tierno9c5c8322018-03-23 15:44:03 +01005538 str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005539
5540 #Delete the SDN network
5541 try:
5542 ovim.delete_network(sdn_network_id)
5543 except ovimException as e:
5544 logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True)
5545 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005546 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005547
tiernoae4a8d12016-07-08 12:30:39 +02005548 content = myvim.delete_network(item_id)
5549 elif item=="tenants":
5550 content = myvim.delete_tenant(item_id)
tierno4540ea52017-01-18 17:44:32 +01005551 elif item == "images":
5552 content = myvim.delete_image(item_id)
tiernoae4a8d12016-07-08 12:30:39 +02005553 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005554 raise NfvoException(item + "?", httperrors.Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02005555 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005556 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
5557 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005558
tiernof97fd272016-07-11 14:32:37 +02005559 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
tierno42026a02017-02-10 15:13:40 +01005560
tiernob3d36742017-03-03 23:51:05 +01005561
tierno7edb6752016-03-21 17:37:52 +01005562def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
5563 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005564 logger.debug("vim_action_create descriptor %s", str(descriptor))
tierno392f2852016-05-13 12:28:55 +02005565 if tenant_id == "any":
5566 tenant_id=None
tiernoa2793912016-10-04 08:15:08 +00005567 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02005568 try:
5569 if item=="networks":
5570 net = descriptor["network"]
5571 net_name = net.pop("name")
5572 net_type = net.pop("type", "bridge")
garciadeblas9f8456e2016-09-05 05:02:59 +02005573 net_public = net.pop("shared", False)
5574 net_ipprofile = net.pop("ip_profile", None)
tiernoa7d34d02017-02-23 14:42:07 +01005575 net_vlan = net.pop("vlan", None)
garciadeblasebd66722019-01-31 16:01:31 +00005576 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 +02005577
5578 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
5579 if get_sdn_controller_id(mydb, datacenter) != None and (net_type == 'data' or net_type == 'ptp'):
tierno00e3df72017-11-29 17:20:13 +01005580 #obtain datacenter_tenant_id
5581 datacenter_tenant_id = mydb.get_rows(SELECT=('uuid',),
5582 FROM='datacenter_tenants',
5583 WHERE={'datacenter_id': datacenter})[0]['uuid']
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005584 try:
5585 sdn_network = {}
5586 sdn_network['vlan'] = net_vlan
5587 sdn_network['type'] = net_type
5588 sdn_network['name'] = net_name
tierno00e3df72017-11-29 17:20:13 +01005589 sdn_network['region'] = datacenter_tenant_id
garciadeblasebd66722019-01-31 16:01:31 +00005590 ovim_content = ovim.new_network(sdn_network)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005591 except ovimException as e:
tierno00e3df72017-11-29 17:20:13 +01005592 logger.error("ovimException creating SDN network={} ".format(
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005593 sdn_network) + str(e), exc_info=True)
5594 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005595 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005596
5597 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
5598 # use instance_scenario_id=None to distinguish from real instaces of nets
tierno00e3df72017-11-29 17:20:13 +01005599 correspondence = {'instance_scenario_id': None,
5600 'sdn_net_id': ovim_content,
5601 'vim_net_id': content,
5602 'datacenter_tenant_id': datacenter_tenant_id
5603 }
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005604 try:
5605 mydb.new_row('instance_nets', correspondence, add_uuid=True)
5606 except db_base_Exception as e:
tierno00e3df72017-11-29 17:20:13 +01005607 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format(
tierno9c5c8322018-03-23 15:44:03 +01005608 correspondence, e), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005609 elif item=="tenants":
5610 tenant = descriptor["tenant"]
5611 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
5612 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005613 raise NfvoException(item + "?", httperrors.Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02005614 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005615 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005616
tierno7edb6752016-03-21 17:37:52 +01005617 return vim_action_get(mydb, tenant_id, datacenter, item, content)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005618
5619def sdn_controller_create(mydb, tenant_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005620 data = ovim.new_of_controller(sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005621 logger.debug('New SDN controller created with uuid {}'.format(data))
5622 return data
5623
5624def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005625 data = ovim.edit_of_controller(controller_id, sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005626 msg = 'SDN controller {} updated'.format(data)
5627 logger.debug(msg)
5628 return msg
5629
5630def sdn_controller_list(mydb, tenant_id, controller_id=None):
5631 if controller_id == None:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005632 data = ovim.get_of_controllers()
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005633 else:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005634 data = ovim.show_of_controller(controller_id)
5635
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005636 msg = 'SDN controller list:\n {}'.format(data)
5637 logger.debug(msg)
5638 return data
5639
5640def sdn_controller_delete(mydb, tenant_id, controller_id):
5641 select_ = ('uuid', 'config')
5642 datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_)
5643 for datacenter in datacenters:
5644 if datacenter['config']:
5645 config = yaml.load(datacenter['config'])
5646 if 'sdn-controller' in config and config['sdn-controller'] == controller_id:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005647 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), httperrors.Conflict)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005648
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005649 data = ovim.delete_of_controller(controller_id)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005650 msg = 'SDN controller {} deleted'.format(data)
5651 logger.debug(msg)
5652 return msg
5653
5654def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping):
5655 controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id})
5656 if len(controller) < 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005657 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), httperrors.Not_Found)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005658
5659 try:
5660 sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"]
5661 except:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005662 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), httperrors.Bad_Request)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005663
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005664 sdn_controller = ovim.show_of_controller(sdn_controller_id)
5665 switch_dpid = sdn_controller["dpid"]
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005666
5667 maps = list()
5668 for compute_node in sdn_port_mapping:
5669 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
5670 element = dict()
5671 element["compute_node"] = compute_node["compute_node"]
5672 for port in compute_node["ports"]:
tierno7f426e92018-06-28 15:21:32 +02005673 pci = port.get("pci")
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005674 element["switch_port"] = port.get("switch_port")
5675 element["switch_mac"] = port.get("switch_mac")
tierno4070e442019-01-23 10:19:23 +00005676 if not element["switch_port"] and not element["switch_mac"]:
5677 raise NfvoException ("The mapping must contain 'switch_port' or 'switch_mac'", httperrors.Bad_Request)
tierno7f426e92018-06-28 15:21:32 +02005678 for pci_expanded in utils.expand_brackets(pci):
5679 element["pci"] = pci_expanded
5680 maps.append(dict(element))
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005681
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005682 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 +01005683
5684def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005685 maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id})
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005686
5687 result = {
5688 "sdn-controller": None,
5689 "datacenter-id": datacenter_id,
5690 "dpid": None,
5691 "ports_mapping": list()
5692 }
5693
5694 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id)
5695 if datacenter['config']:
5696 config = yaml.load(datacenter['config'])
5697 if 'sdn-controller' in config:
5698 controller_id = config['sdn-controller']
5699 sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id)
5700 result["sdn-controller"] = controller_id
5701 result["dpid"] = sdn_controller["dpid"]
5702
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005703 if result["sdn-controller"] == None:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005704 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005705 if result["dpid"] == None:
5706 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005707 httperrors.Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005708
5709 if len(maps) == 0:
5710 return result
5711
5712 ports_correspondence_dict = dict()
5713 for link in maps:
5714 if result["sdn-controller"] != link["ofc_id"]:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005715 raise NfvoException("The sdn-controller specified for different port mappings differ", httperrors.Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005716 if result["dpid"] != link["switch_dpid"]:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005717 raise NfvoException("The dpid specified for different port mappings differ", httperrors.Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005718 element = dict()
5719 element["pci"] = link["pci"]
5720 if link["switch_port"]:
5721 element["switch_port"] = link["switch_port"]
5722 if link["switch_mac"]:
5723 element["switch_mac"] = link["switch_mac"]
5724
5725 if not link["compute_node"] in ports_correspondence_dict:
5726 content = dict()
5727 content["compute_node"] = link["compute_node"]
5728 content["ports"] = list()
5729 ports_correspondence_dict[link["compute_node"]] = content
5730
5731 ports_correspondence_dict[link["compute_node"]]["ports"].append(element)
5732
5733 for key in sorted(ports_correspondence_dict):
5734 result["ports_mapping"].append(ports_correspondence_dict[key])
5735
5736 return result
5737
5738def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id):
tierno639520f2017-04-05 19:55:36 +02005739 return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id})
gcalvinoe580c7d2017-09-22 14:09:51 +02005740
5741def create_RO_keypair(tenant_id):
5742 """
5743 Creates a public / private keys for a RO tenant and returns their values
5744 Params:
5745 tenant_id: ID of the tenant
5746 Return:
5747 public_key: Public key for the RO tenant
5748 private_key: Encrypted private key for RO tenant
5749 """
5750
5751 bits = 2048
5752 key = RSA.generate(bits)
5753 try:
5754 public_key = key.publickey().exportKey('OpenSSH')
5755 if isinstance(public_key, ValueError):
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005756 raise NfvoException("Unable to create public key: {}".format(public_key), httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02005757 private_key = key.exportKey(passphrase=tenant_id, pkcs=8)
5758 except (ValueError, NameError) as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005759 raise NfvoException("Unable to create private key: {}".format(e), httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02005760 return public_key, private_key
5761
5762def decrypt_key (key, tenant_id):
5763 """
5764 Decrypts an encrypted RSA key
5765 Params:
5766 key: Private key to be decrypted
5767 tenant_id: ID of the tenant
5768 Return:
5769 unencrypted_key: Unencrypted private key for RO tenant
5770 """
5771 try:
5772 key = RSA.importKey(key,tenant_id)
5773 unencrypted_key = key.exportKey('PEM')
5774 if isinstance(unencrypted_key, ValueError):
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005775 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key), httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02005776 except ValueError as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005777 raise NfvoException("Unable to decrypt the private key: {}".format(e), httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02005778 return unencrypted_key