blob: 3ab77cfdf12d13102341013d3baacbe4cdb74f28 [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##
23
24'''
25NFVO engine, implementing all the methods for the creation, deletion and management of vnfs, scenarios and instances
26'''
27__author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
28__date__ ="$16-sep-2014 22:05:01$"
29
tierno361275f2017-04-25 16:24:34 +020030# import imp
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)
811 flavor_create="false"
812 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
884def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
885 """
886 Parses an OSM IM vnfd_catalog and insert at DB
887 :param mydb:
888 :param tenant_id:
889 :param vnf_descriptor:
890 :return: The list of cretated vnf ids
891 """
892 try:
893 myvnfd = vnfd_catalog.vnfd()
tiernoa9550202017-09-22 13:31:35 +0200894 try:
tiernof6bbe222019-04-09 14:19:40 +0000895 pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd, path_helper=True,
896 skip_unknown=True)
tiernoa9550202017-09-22 13:31:35 +0200897 except Exception as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100898 raise NfvoException("Error. Invalid VNF descriptor format " + str(e), httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +0200899 db_vnfs = []
900 db_nets = []
901 db_vms = []
902 db_vms_index = 0
903 db_interfaces = []
904 db_images = []
905 db_flavors = []
tierno41a69812018-02-16 14:34:33 +0100906 db_ip_profiles_index = 0
907 db_ip_profiles = []
tiernof1ba57e2017-09-07 12:23:19 +0200908 uuid_list = []
909 vnfd_uuid_list = []
tiernoe18ba432017-10-12 10:22:45 +0200910 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd:vnfd-catalog")
911 if not vnfd_catalog_descriptor:
912 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd-catalog")
913 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd")
914 if not vnfd_descriptor_list:
915 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd:vnfd")
tiernob2880eb2017-10-04 15:04:53 +0200916 for vnfd_yang in myvnfd.vnfd_catalog.vnfd.itervalues():
917 vnfd = vnfd_yang.get()
tiernof1ba57e2017-09-07 12:23:19 +0200918
919 # table vnf
920 vnf_uuid = str(uuid4())
921 uuid_list.append(vnf_uuid)
922 vnfd_uuid_list.append(vnf_uuid)
tierno66eba6e2017-11-10 17:09:18 +0100923 vnfd_id = get_str(vnfd, "id", 255)
tiernof1ba57e2017-09-07 12:23:19 +0200924 db_vnf = {
925 "uuid": vnf_uuid,
tierno66eba6e2017-11-10 17:09:18 +0100926 "osm_id": vnfd_id,
tiernof1ba57e2017-09-07 12:23:19 +0200927 "name": get_str(vnfd, "name", 255),
928 "description": get_str(vnfd, "description", 255),
929 "tenant_id": tenant_id,
930 "vendor": get_str(vnfd, "vendor", 255),
931 "short_name": get_str(vnfd, "short-name", 255),
932 "descriptor": str(vnf_descriptor)[:60000]
933 }
934
tiernoe18ba432017-10-12 10:22:45 +0200935 for vnfd_descriptor in vnfd_descriptor_list:
936 if vnfd_descriptor["id"] == str(vnfd["id"]):
937 break
938
tierno41a69812018-02-16 14:34:33 +0100939 # table ip_profiles (ip-profiles)
940 ip_profile_name2db_table_index = {}
941 for ip_profile in vnfd.get("ip-profiles").itervalues():
942 db_ip_profile = {
943 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
944 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
945 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
946 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
947 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
948 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
949 }
950 dns_list = []
951 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
952 dns_list.append(str(dns.get("address")))
953 db_ip_profile["dns_address"] = ";".join(dns_list)
954 if ip_profile["ip-profile-params"].get('security-group'):
955 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
956 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
957 db_ip_profiles_index += 1
958 db_ip_profiles.append(db_ip_profile)
959
tiernof1ba57e2017-09-07 12:23:19 +0200960 # table nets (internal-vld)
961 net_id2uuid = {} # for mapping interface with network
962 for vld in vnfd.get("internal-vld").itervalues():
963 net_uuid = str(uuid4())
964 uuid_list.append(net_uuid)
965 db_net = {
966 "name": get_str(vld, "name", 255),
967 "vnf_id": vnf_uuid,
968 "uuid": net_uuid,
969 "description": get_str(vld, "description", 255),
tierno1df468d2018-07-06 14:25:16 +0200970 "osm_id": get_str(vld, "id", 255),
tiernof1ba57e2017-09-07 12:23:19 +0200971 "type": "bridge", # TODO adjust depending on connection point type
972 }
973 net_id2uuid[vld.get("id")] = net_uuid
974 db_nets.append(db_net)
tierno41a69812018-02-16 14:34:33 +0100975 # ip-profile, link db_ip_profile with db_sce_net
976 if vld.get("ip-profile-ref"):
977 ip_profile_name = vld.get("ip-profile-ref")
978 if ip_profile_name not in ip_profile_name2db_table_index:
979 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':"
980 "'{}'. Reference to a non-existing 'ip_profiles'".format(
981 str(vnfd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100982 httperrors.Bad_Request)
tierno41a69812018-02-16 14:34:33 +0100983 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["net_id"] = net_uuid
984 else: #check no ip-address has been defined
tierno45140f52018-03-26 12:11:46 +0200985 for icp in vld.get("internal-connection-point").itervalues():
tierno41a69812018-02-16 14:34:33 +0100986 if icp.get("ip-address"):
987 raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' "
988 "contains an ip-address but no ip-profile has been defined at VLD".format(
989 str(vnfd["id"]), str(vld["id"]), str(icp["id"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100990 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +0200991
tiernocf596692017-11-20 15:47:51 +0100992 # connection points vaiable declaration
993 cp_name2iface_uuid = {}
994 cp_name2vm_uuid = {}
995 cp_name2db_interface = {}
tiernob6990792018-11-13 10:37:42 +0100996 vdu_id2cp_name = {} # stored only when one external connection point is presented at this VDU
tiernocf596692017-11-20 15:47:51 +0100997
tiernof1ba57e2017-09-07 12:23:19 +0200998 # table vms (vdus)
999 vdu_id2uuid = {}
1000 vdu_id2db_table_index = {}
1001 for vdu in vnfd.get("vdu").itervalues():
tierno41a69812018-02-16 14:34:33 +01001002
1003 for vdu_descriptor in vnfd_descriptor["vdu"]:
1004 if vdu_descriptor["id"] == str(vdu["id"]):
1005 break
tiernof1ba57e2017-09-07 12:23:19 +02001006 vm_uuid = str(uuid4())
1007 uuid_list.append(vm_uuid)
tierno66eba6e2017-11-10 17:09:18 +01001008 vdu_id = get_str(vdu, "id", 255)
tiernof1ba57e2017-09-07 12:23:19 +02001009 db_vm = {
1010 "uuid": vm_uuid,
tierno66eba6e2017-11-10 17:09:18 +01001011 "osm_id": vdu_id,
tiernof1ba57e2017-09-07 12:23:19 +02001012 "name": get_str(vdu, "name", 255),
1013 "description": get_str(vdu, "description", 255),
tiernob6990792018-11-13 10:37:42 +01001014 "pdu_type": get_str(vdu, "pdu-type", 255),
tiernof1ba57e2017-09-07 12:23:19 +02001015 "vnf_id": vnf_uuid,
1016 }
1017 vdu_id2uuid[db_vm["osm_id"]] = vm_uuid
1018 vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index
1019 if vdu.get("count"):
1020 db_vm["count"] = int(vdu["count"])
1021
1022 # table image
1023 image_present = False
1024 if vdu.get("image"):
1025 image_present = True
1026 db_image = {}
1027 image_uuid = _lookfor_or_create_image(db_image, mydb, vdu)
1028 if not image_uuid:
1029 image_uuid = db_image["uuid"]
1030 db_images.append(db_image)
1031 db_vm["image_id"] = image_uuid
tierno16e3dd42018-04-24 12:52:40 +02001032 if vdu.get("alternative-images"):
1033 vm_alternative_images = []
1034 for alt_image in vdu.get("alternative-images").itervalues():
1035 db_image = {}
1036 image_uuid = _lookfor_or_create_image(db_image, mydb, alt_image)
1037 if not image_uuid:
1038 image_uuid = db_image["uuid"]
1039 db_images.append(db_image)
1040 vm_alternative_images.append({
1041 "image_id": image_uuid,
1042 "vim_type": str(alt_image["vim-type"]),
1043 # "universal_name": str(alt_image["image"]),
1044 # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None
1045 })
1046
1047 db_vm["image_list"] = yaml.safe_dump(vm_alternative_images, default_flow_style=True, width=256)
tiernof1ba57e2017-09-07 12:23:19 +02001048
1049 # volumes
1050 devices = []
1051 if vdu.get("volumes"):
tierno1df468d2018-07-06 14:25:16 +02001052 for volume_key in vdu["volumes"]:
tiernof1ba57e2017-09-07 12:23:19 +02001053 volume = vdu["volumes"][volume_key]
1054 if not image_present:
1055 # Convert the first volume to vnfc.image
1056 image_present = True
1057 db_image = {}
1058 image_uuid = _lookfor_or_create_image(db_image, mydb, volume)
1059 if not image_uuid:
1060 image_uuid = db_image["uuid"]
1061 db_images.append(db_image)
1062 db_vm["image_id"] = image_uuid
1063 else:
1064 # Add Openmano devices
tierno1df468d2018-07-06 14:25:16 +02001065 device = {"name": str(volume.get("name"))}
tiernof1ba57e2017-09-07 12:23:19 +02001066 device["type"] = str(volume.get("device-type"))
1067 if volume.get("size"):
1068 device["size"] = int(volume["size"])
1069 if volume.get("image"):
1070 device["image name"] = str(volume["image"])
1071 if volume.get("image-checksum"):
1072 device["image checksum"] = str(volume["image-checksum"])
tierno1df468d2018-07-06 14:25:16 +02001073
tiernof1ba57e2017-09-07 12:23:19 +02001074 devices.append(device)
1075
tierno89aada42018-12-19 16:00:25 +00001076 if not db_vm.get("image_id"):
1077 if not db_vm["pdu_type"]:
1078 raise NfvoException("Not defined image for VDU")
1079 # create a fake image
1080
tierno66eba6e2017-11-10 17:09:18 +01001081 # cloud-init
1082 boot_data = {}
1083 if vdu.get("cloud-init"):
1084 boot_data["user-data"] = str(vdu["cloud-init"])
1085 elif vdu.get("cloud-init-file"):
1086 # TODO Where this file content is present???
1087 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
1088 boot_data["user-data"] = str(vdu["cloud-init-file"])
1089
1090 if vdu.get("supplemental-boot-data"):
1091 if vdu["supplemental-boot-data"].get('boot-data-drive'):
1092 boot_data['boot-data-drive'] = True
1093 if vdu["supplemental-boot-data"].get('config-file'):
1094 om_cfgfile_list = list()
1095 for custom_config_file in vdu["supplemental-boot-data"]['config-file'].itervalues():
1096 # TODO Where this file content is present???
1097 cfg_source = str(custom_config_file["source"])
1098 om_cfgfile_list.append({"dest": custom_config_file["dest"],
1099 "content": cfg_source})
1100 boot_data['config-files'] = om_cfgfile_list
1101 if boot_data:
1102 db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256)
1103
1104 db_vms.append(db_vm)
1105 db_vms_index += 1
1106
1107 # table interfaces (internal/external interfaces)
1108 flavor_epa_interfaces = []
tierno66eba6e2017-11-10 17:09:18 +01001109 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
1110 for iface in vdu.get("interface").itervalues():
1111 flavor_epa_interface = {}
1112 iface_uuid = str(uuid4())
1113 uuid_list.append(iface_uuid)
1114 db_interface = {
1115 "uuid": iface_uuid,
1116 "internal_name": get_str(iface, "name", 255),
1117 "vm_id": vm_uuid,
1118 }
1119 flavor_epa_interface["name"] = db_interface["internal_name"]
1120 if iface.get("virtual-interface").get("vpci"):
1121 db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12)
1122 flavor_epa_interface["vpci"] = db_interface["vpci"]
1123
1124 if iface.get("virtual-interface").get("bandwidth"):
1125 bps = int(iface.get("virtual-interface").get("bandwidth"))
1126 db_interface["bw"] = int(math.ceil(bps/1000000.0))
1127 flavor_epa_interface["bandwidth"] = "{} Mbps".format(db_interface["bw"])
1128
1129 if iface.get("virtual-interface").get("type") == "OM-MGMT":
1130 db_interface["type"] = "mgmt"
garciadeblas31e141b2018-10-25 18:33:19 +02001131 elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
tierno66eba6e2017-11-10 17:09:18 +01001132 db_interface["type"] = "bridge"
1133 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1134 elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1135 db_interface["type"] = "data"
1136 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1137 flavor_epa_interface["dedicated"] = "no" if iface["virtual-interface"]["type"] == "SR-IOV" \
1138 else "yes"
1139 flavor_epa_interfaces.append(flavor_epa_interface)
1140 else:
1141 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1142 "-interface':'type':'{}'. Interface type is not supported".format(
1143 vnfd_id, vdu_id, iface.get("virtual-interface").get("type")),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001144 httperrors.Bad_Request)
tierno66eba6e2017-11-10 17:09:18 +01001145
tiernoe72710b2018-07-23 16:16:00 +02001146 if iface.get("mgmt-interface"):
1147 db_interface["type"] = "mgmt"
1148
tierno66eba6e2017-11-10 17:09:18 +01001149 if iface.get("external-connection-point-ref"):
1150 try:
1151 cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")]
1152 db_interface["external_name"] = get_str(cp, "name", 255)
1153 cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid
1154 cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid
1155 cp_name2db_interface[db_interface["external_name"]] = db_interface
1156 for cp_descriptor in vnfd_descriptor["connection-point"]:
1157 if cp_descriptor["name"] == db_interface["external_name"]:
1158 break
1159 else:
1160 raise KeyError()
1161
1162 if vdu_id in vdu_id2cp_name:
1163 vdu_id2cp_name[vdu_id] = None # more than two connecdtion point for this VDU
1164 else:
1165 vdu_id2cp_name[vdu_id] = db_interface["external_name"]
1166
1167 # port security
1168 if str(cp_descriptor.get("port-security-enabled")).lower() == "false":
1169 db_interface["port_security"] = 0
1170 elif str(cp_descriptor.get("port-security-enabled")).lower() == "true":
1171 db_interface["port_security"] = 1
1172 except KeyError:
1173 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1174 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1175 " at connection-point".format(
1176 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
1177 cp=iface.get("vnfd-connection-point-ref")),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001178 httperrors.Bad_Request)
tierno66eba6e2017-11-10 17:09:18 +01001179 elif iface.get("internal-connection-point-ref"):
1180 try:
tierno41a69812018-02-16 14:34:33 +01001181 for icp_descriptor in vdu_descriptor["internal-connection-point"]:
1182 if icp_descriptor["id"] == str(iface.get("internal-connection-point-ref")):
1183 break
1184 else:
1185 raise KeyError("does not exist at vdu:internal-connection-point")
1186 icp = None
1187 icp_vld = None
tierno66eba6e2017-11-10 17:09:18 +01001188 for vld in vnfd.get("internal-vld").itervalues():
1189 for cp in vld.get("internal-connection-point").itervalues():
1190 if cp.get("id-ref") == iface.get("internal-connection-point-ref"):
tierno41a69812018-02-16 14:34:33 +01001191 if icp:
1192 raise KeyError("is referenced by more than one 'internal-vld'")
1193 icp = cp
1194 icp_vld = vld
1195 if not icp:
1196 raise KeyError("is not referenced by any 'internal-vld'")
1197
1198 db_interface["net_id"] = net_id2uuid[icp_vld.get("id")]
1199 if str(icp_descriptor.get("port-security-enabled")).lower() == "false":
1200 db_interface["port_security"] = 0
1201 elif str(icp_descriptor.get("port-security-enabled")).lower() == "true":
1202 db_interface["port_security"] = 1
1203 if icp.get("ip-address"):
1204 if not icp_vld.get("ip-profile-ref"):
1205 raise NfvoException
1206 db_interface["ip_address"] = str(icp.get("ip-address"))
1207 except KeyError as e:
tierno66eba6e2017-11-10 17:09:18 +01001208 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
tierno41a69812018-02-16 14:34:33 +01001209 "'interface[{iface}]':'internal-connection-point-ref':'{cp}'"
1210 " {msg}".format(
tierno66eba6e2017-11-10 17:09:18 +01001211 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
tierno41a69812018-02-16 14:34:33 +01001212 cp=iface.get("internal-connection-point-ref"), msg=str(e)),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001213 httperrors.Bad_Request)
tierno55d234c2018-07-04 18:29:21 +02001214 if iface.get("position"):
1215 db_interface["created_at"] = int(iface.get("position")) * 50
tierno41a69812018-02-16 14:34:33 +01001216 if iface.get("mac-address"):
1217 db_interface["mac"] = str(iface.get("mac-address"))
tierno66eba6e2017-11-10 17:09:18 +01001218 db_interfaces.append(db_interface)
1219
tiernof1ba57e2017-09-07 12:23:19 +02001220 # table flavors
1221 db_flavor = {
1222 "name": get_str(vdu, "name", 250) + "-flv",
1223 "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)),
1224 "ram": int(vdu["vm-flavor"].get("memory-mb", 1)),
garciadeblas79d1a1a2017-12-11 16:07:07 +01001225 "disk": int(vdu["vm-flavor"].get("storage-gb", 0)),
tiernof1ba57e2017-09-07 12:23:19 +02001226 }
tiernocf596692017-11-20 15:47:51 +01001227 # TODO revise the case of several numa-node-policy node
tiernof1ba57e2017-09-07 12:23:19 +02001228 extended = {}
1229 numa = {}
1230 if devices:
1231 extended["devices"] = devices
tierno66eba6e2017-11-10 17:09:18 +01001232 if flavor_epa_interfaces:
1233 numa["interfaces"] = flavor_epa_interfaces
tiernof1ba57e2017-09-07 12:23:19 +02001234 if vdu.get("guest-epa"): # TODO or dedicated_int:
1235 epa_vcpu_set = False
1236 if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
1237 numa_node_policy = vdu["guest-epa"].get("numa-node-policy")
1238 if numa_node_policy.get("node"):
tiernocf596692017-11-20 15:47:51 +01001239 numa_node = numa_node_policy["node"].values()[0]
tiernof1ba57e2017-09-07 12:23:19 +02001240 if numa_node.get("num-cores"):
1241 numa["cores"] = numa_node["num-cores"]
1242 epa_vcpu_set = True
1243 if numa_node.get("paired-threads"):
1244 if numa_node["paired-threads"].get("num-paired-threads"):
tierno39dddcc2017-10-05 18:48:06 +02001245 numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"])
tiernof1ba57e2017-09-07 12:23:19 +02001246 epa_vcpu_set = True
tierno39dddcc2017-10-05 18:48:06 +02001247 if len(numa_node["paired-threads"].get("paired-thread-ids")):
tiernof1ba57e2017-09-07 12:23:19 +02001248 numa["paired-threads-id"] = []
tierno39dddcc2017-10-05 18:48:06 +02001249 for pair in numa_node["paired-threads"]["paired-thread-ids"].itervalues():
tiernof1ba57e2017-09-07 12:23:19 +02001250 numa["paired-threads-id"].append(
1251 (str(pair["thread-a"]), str(pair["thread-b"]))
1252 )
1253 if numa_node.get("num-threads"):
tierno39dddcc2017-10-05 18:48:06 +02001254 numa["threads"] = int(numa_node["num-threads"])
tiernof1ba57e2017-09-07 12:23:19 +02001255 epa_vcpu_set = True
1256 if numa_node.get("memory-mb"):
1257 numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1)
1258 if vdu["guest-epa"].get("mempage-size"):
1259 if vdu["guest-epa"]["mempage-size"] != "SMALL":
1260 numa["memory"] = max(int(db_flavor["ram"] / 1024), 1)
1261 if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set:
1262 if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
1263 if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \
1264 vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
1265 numa["cores"] = max(db_flavor["vcpus"], 1)
1266 else:
1267 numa["threads"] = max(db_flavor["vcpus"], 1)
1268 if numa:
1269 extended["numas"] = [numa]
1270 if extended:
1271 extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256)
1272 db_flavor["extended"] = extended_text
1273 # look if flavor exist
garciadeblas79d1a1a2017-12-11 16:07:07 +01001274 temp_flavor_dict = {'disk': db_flavor.get('disk', 0),
tiernof1ba57e2017-09-07 12:23:19 +02001275 'ram': db_flavor.get('ram'),
1276 'vcpus': db_flavor.get('vcpus'),
1277 'extended': db_flavor.get('extended')
1278 }
1279 existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
1280 if existing_flavors:
1281 flavor_uuid = existing_flavors[0]["uuid"]
1282 else:
1283 flavor_uuid = str(uuid4())
1284 uuid_list.append(flavor_uuid)
1285 db_flavor["uuid"] = flavor_uuid
1286 db_flavors.append(db_flavor)
1287 db_vm["flavor_id"] = flavor_uuid
1288
tiernof1ba57e2017-09-07 12:23:19 +02001289 # VNF affinity and antiaffinity
1290 for pg in vnfd.get("placement-groups").itervalues():
1291 pg_name = get_str(pg, "name", 255)
1292 for vdu in pg.get("member-vdus").itervalues():
1293 vdu_id = get_str(vdu, "member-vdu-ref", 255)
1294 if vdu_id not in vdu_id2db_table_index:
tiernob2880eb2017-10-04 15:04:53 +02001295 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1296 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
tierno66eba6e2017-11-10 17:09:18 +01001297 vnf=vnfd_id, pg=pg_name, vdu=vdu_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001298 httperrors.Bad_Request)
tierno55fe3972019-03-29 08:50:12 +00001299 db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name
tiernof1ba57e2017-09-07 12:23:19 +02001300 # TODO consider the case of isolation and not colocation
1301 # if pg.get("strategy") == "ISOLATION":
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001302
tiernof1ba57e2017-09-07 12:23:19 +02001303 # VNF mgmt configuration
1304 mgmt_access = {}
1305 if vnfd["mgmt-interface"].get("vdu-id"):
tierno66eba6e2017-11-10 17:09:18 +01001306 mgmt_vdu_id = get_str(vnfd["mgmt-interface"], "vdu-id", 255)
1307 if mgmt_vdu_id not in vdu_id2uuid:
tiernob2880eb2017-10-04 15:04:53 +02001308 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1309 "'{vdu}'. Reference to a non-existing vdu".format(
tierno66eba6e2017-11-10 17:09:18 +01001310 vnf=vnfd_id, vdu=mgmt_vdu_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001311 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001312 mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]]
tierno66eba6e2017-11-10 17:09:18 +01001313 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
1314 if vdu_id2cp_name.get(mgmt_vdu_id):
tiernob6990792018-11-13 10:37:42 +01001315 if cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]:
1316 cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt"
tierno66eba6e2017-11-10 17:09:18 +01001317
tiernof1ba57e2017-09-07 12:23:19 +02001318 if vnfd["mgmt-interface"].get("ip-address"):
1319 mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address"))
1320 if vnfd["mgmt-interface"].get("cp"):
1321 if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid:
tiernob6990792018-11-13 10:37:42 +01001322 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp'['{cp}']. "
tiernob2880eb2017-10-04 15:04:53 +02001323 "Reference to a non-existing connection-point".format(
tierno66eba6e2017-11-10 17:09:18 +01001324 vnf=vnfd_id, cp=vnfd["mgmt-interface"]["cp"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001325 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001326 mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]]
1327 mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]]
tiernoe2ff1ce2017-11-02 17:01:10 +01001328 # mark this interface as of type mgmt
tiernob6990792018-11-13 10:37:42 +01001329 if cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]:
1330 cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt"
tiernoe2ff1ce2017-11-02 17:01:10 +01001331
tiernoa9550202017-09-22 13:31:35 +02001332 default_user = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
tiernof1ba57e2017-09-07 12:23:19 +02001333 "default-user", 64)
gcalvinoe580c7d2017-09-22 14:09:51 +02001334
tiernof1ba57e2017-09-07 12:23:19 +02001335 if default_user:
1336 mgmt_access["default_user"] = default_user
gcalvinoe580c7d2017-09-22 14:09:51 +02001337 required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1338 "required", 6)
1339 if required:
1340 mgmt_access["required"] = required
1341
tiernof1ba57e2017-09-07 12:23:19 +02001342 if mgmt_access:
1343 db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256)
1344
1345 db_vnfs.append(db_vnf)
1346 db_tables=[
1347 {"vnfs": db_vnfs},
1348 {"nets": db_nets},
1349 {"images": db_images},
1350 {"flavors": db_flavors},
tierno41a69812018-02-16 14:34:33 +01001351 {"ip_profiles": db_ip_profiles},
tiernof1ba57e2017-09-07 12:23:19 +02001352 {"vms": db_vms},
1353 {"interfaces": db_interfaces},
1354 ]
1355
1356 logger.debug("create_vnf Deployment done vnfDict: %s",
1357 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
1358 mydb.new_rows(db_tables, uuid_list)
1359 return vnfd_uuid_list
tiernob2880eb2017-10-04 15:04:53 +02001360 except NfvoException:
1361 raise
tiernof1ba57e2017-09-07 12:23:19 +02001362 except Exception as e:
1363 logger.error("Exception {}".format(e))
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001364 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001365
1366
tiernob8569aa2018-08-24 11:34:54 +02001367@deprecated("Use new_vnfd_v3")
tierno7edb6752016-03-21 17:37:52 +01001368def new_vnf(mydb, tenant_id, vnf_descriptor):
1369 global global_config
tierno42026a02017-02-10 15:13:40 +01001370
tierno7edb6752016-03-21 17:37:52 +01001371 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +01001372 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
tierno7edb6752016-03-21 17:37:52 +01001373 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +01001374 vims = {}
tierno7edb6752016-03-21 17:37:52 +01001375 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001376 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001377 if "tenant_id" in vnf_descriptor["vnf"]:
1378 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001379 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 +01001380 httperrors.Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001381 else:
1382 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1383 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +01001384 if global_config["auto_push_VNF_to_VIMs"]:
tiernocbb52052018-05-31 18:57:30 +02001385 vims = get_vim(mydb, tenant_id, ignore_errors=True)
tierno7edb6752016-03-21 17:37:52 +01001386
1387 # Step 4. Review the descriptor and add missing fields
1388 #print vnf_descriptor
tiernof97fd272016-07-11 14:32:37 +02001389 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
tierno7edb6752016-03-21 17:37:52 +01001390 vnf_name = vnf_descriptor['vnf']['name']
1391 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1392 if "physical" in vnf_descriptor['vnf']:
1393 del vnf_descriptor['vnf']['physical']
1394 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +01001395
tierno42026a02017-02-10 15:13:40 +01001396 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
tiernof97fd272016-07-11 14:32:37 +02001397 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1398 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +01001399
tierno7edb6752016-03-21 17:37:52 +01001400 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1401 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1402 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
tierno7edb6752016-03-21 17:37:52 +01001403 try:
tiernof97fd272016-07-11 14:32:37 +02001404 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +01001405 for vnfc in vnf_descriptor['vnf']['VNFC']:
1406 VNFCitem={}
1407 VNFCitem["name"] = vnfc['name']
mirabal29356312017-07-27 12:21:22 +02001408 VNFCitem["availability_zone"] = vnfc.get('availability_zone')
tierno7edb6752016-03-21 17:37:52 +01001409 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +01001410
tiernof97fd272016-07-11 14:32:37 +02001411 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +01001412
tierno7edb6752016-03-21 17:37:52 +01001413 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +02001414 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 +01001415 myflavorDict["description"] = VNFCitem["description"]
1416 myflavorDict["ram"] = vnfc.get("ram", 0)
1417 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
garciadeblas79d1a1a2017-12-11 16:07:07 +01001418 myflavorDict["disk"] = vnfc.get("disk", 0)
tierno7edb6752016-03-21 17:37:52 +01001419 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +01001420
tierno7edb6752016-03-21 17:37:52 +01001421 devices = vnfc.get("devices")
1422 if devices != None:
1423 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +01001424
tierno7edb6752016-03-21 17:37:52 +01001425 # TODO:
1426 # 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 +01001427 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1428
tierno7edb6752016-03-21 17:37:52 +01001429 # Previous code has been commented
1430 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1431 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1432 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1433 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1434 #else:
1435 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1436 # if result2:
1437 # print "Error creating flavor: unknown processor model. Rollback successful."
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001438 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
tierno7edb6752016-03-21 17:37:52 +01001439 # else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001440 # 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 +01001441 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +01001442
tierno7edb6752016-03-21 17:37:52 +01001443 if 'numas' in vnfc and len(vnfc['numas'])>0:
1444 myflavorDict['extended']['numas'] = vnfc['numas']
1445
1446 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +01001447
tierno7edb6752016-03-21 17:37:52 +01001448 # Step 6.2 New flavors are created in the VIM
tiernof97fd272016-07-11 14:32:37 +02001449 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
tierno7edb6752016-03-21 17:37:52 +01001450
tiernof97fd272016-07-11 14:32:37 +02001451 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
tierno7edb6752016-03-21 17:37:52 +01001452 VNFCitem["flavor_id"] = flavor_id
1453 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +01001454
tiernof97fd272016-07-11 14:32:37 +02001455 logger.debug("Creating new images in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +01001456 # Step 6.3 New images are created in the VIM
1457 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +01001458 #This "for" loop might be integrated with the previous one
tierno7edb6752016-03-21 17:37:52 +01001459 #In case this integration is made, the VNFCDict might become a VNFClist.
1460 for vnfc in vnf_descriptor['vnf']['VNFC']:
tiernof97fd272016-07-11 14:32:37 +02001461 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +02001462 image_dict={}
1463 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1464 image_dict['universal_name']=vnfc.get('image name')
1465 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1466 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +01001467 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +02001468 image_dict['checksum']=vnfc.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +01001469 image_metadata_dict = vnfc.get('image metadata', None)
1470 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +01001471 if image_metadata_dict is not None:
tierno7edb6752016-03-21 17:37:52 +01001472 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1473 image_dict['metadata']=image_metadata_str
1474 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
tiernof97fd272016-07-11 14:32:37 +02001475 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1476 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
tierno7edb6752016-03-21 17:37:52 +01001477 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +02001478 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno8e690322017-08-10 15:58:50 +02001479 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
tierno36c0b172017-01-12 18:32:28 +01001480 if vnfc.get("boot-data"):
1481 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
tierno7edb6752016-03-21 17:37:52 +01001482
tierno42026a02017-02-10 15:13:40 +01001483
tiernof97fd272016-07-11 14:32:37 +02001484 # Step 7. Storing the VNF descriptor in the repository
1485 if "descriptor" not in vnf_descriptor["vnf"]:
1486 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001487
tiernof97fd272016-07-11 14:32:37 +02001488 # Step 8. Adding the VNF to the NFVO DB
1489 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1490 return vnf_id
1491 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
tierno7edb6752016-03-21 17:37:52 +01001492 _, message = rollback(mydb, vims, rollback_list)
tiernof97fd272016-07-11 14:32:37 +02001493 if isinstance(e, db_base_Exception):
1494 error_text = "Exception at database"
1495 elif isinstance(e, KeyError):
1496 error_text = "KeyError exception "
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001497 e.http_code = httperrors.Internal_Server_Error
tiernof97fd272016-07-11 14:32:37 +02001498 else:
1499 error_text = "Exception at VIM"
1500 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1501 #logger.error("start_scenario %s", error_text)
1502 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01001503
tiernob3d36742017-03-03 23:51:05 +01001504
tiernob8569aa2018-08-24 11:34:54 +02001505@deprecated("Use new_vnfd_v3")
garciadeblas9f8456e2016-09-05 05:02:59 +02001506def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
1507 global global_config
tierno42026a02017-02-10 15:13:40 +01001508
garciadeblas9f8456e2016-09-05 05:02:59 +02001509 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +01001510 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
garciadeblas9f8456e2016-09-05 05:02:59 +02001511 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +01001512 vims = {}
garciadeblas9f8456e2016-09-05 05:02:59 +02001513 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001514 check_tenant(mydb, tenant_id)
garciadeblas9f8456e2016-09-05 05:02:59 +02001515 if "tenant_id" in vnf_descriptor["vnf"]:
1516 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
1517 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 +01001518 httperrors.Unauthorized)
garciadeblas9f8456e2016-09-05 05:02:59 +02001519 else:
1520 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1521 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +01001522 if global_config["auto_push_VNF_to_VIMs"]:
tiernocbb52052018-05-31 18:57:30 +02001523 vims = get_vim(mydb, tenant_id, ignore_errors=True)
garciadeblas9f8456e2016-09-05 05:02:59 +02001524
1525 # Step 4. Review the descriptor and add missing fields
1526 #print vnf_descriptor
1527 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1528 vnf_name = vnf_descriptor['vnf']['name']
1529 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1530 if "physical" in vnf_descriptor['vnf']:
1531 del vnf_descriptor['vnf']['physical']
1532 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +01001533
tierno42026a02017-02-10 15:13:40 +01001534 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
garciadeblas9f8456e2016-09-05 05:02:59 +02001535 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1536 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +01001537
garciadeblas9f8456e2016-09-05 05:02:59 +02001538 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1539 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1540 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
1541 try:
1542 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1543 for vnfc in vnf_descriptor['vnf']['VNFC']:
1544 VNFCitem={}
1545 VNFCitem["name"] = vnfc['name']
1546 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +01001547
garciadeblas9f8456e2016-09-05 05:02:59 +02001548 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +01001549
garciadeblas9f8456e2016-09-05 05:02:59 +02001550 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +02001551 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 +02001552 myflavorDict["description"] = VNFCitem["description"]
1553 myflavorDict["ram"] = vnfc.get("ram", 0)
1554 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
garciadeblas79d1a1a2017-12-11 16:07:07 +01001555 myflavorDict["disk"] = vnfc.get("disk", 0)
garciadeblas9f8456e2016-09-05 05:02:59 +02001556 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +01001557
garciadeblas9f8456e2016-09-05 05:02:59 +02001558 devices = vnfc.get("devices")
1559 if devices != None:
1560 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +01001561
garciadeblas9f8456e2016-09-05 05:02:59 +02001562 # TODO:
1563 # 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 +01001564 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1565
garciadeblas9f8456e2016-09-05 05:02:59 +02001566 # Previous code has been commented
1567 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1568 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1569 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1570 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1571 #else:
1572 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1573 # if result2:
1574 # print "Error creating flavor: unknown processor model. Rollback successful."
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001575 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
garciadeblas9f8456e2016-09-05 05:02:59 +02001576 # else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001577 # 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 +02001578 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +01001579
garciadeblas9f8456e2016-09-05 05:02:59 +02001580 if 'numas' in vnfc and len(vnfc['numas'])>0:
1581 myflavorDict['extended']['numas'] = vnfc['numas']
1582
1583 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +01001584
garciadeblas9f8456e2016-09-05 05:02:59 +02001585 # Step 6.2 New flavors are created in the VIM
1586 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
1587
1588 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1589 VNFCitem["flavor_id"] = flavor_id
1590 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +01001591
garciadeblas9f8456e2016-09-05 05:02:59 +02001592 logger.debug("Creating new images in the VIM for each VNFC")
1593 # Step 6.3 New images are created in the VIM
1594 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +01001595 #This "for" loop might be integrated with the previous one
garciadeblas9f8456e2016-09-05 05:02:59 +02001596 #In case this integration is made, the VNFCDict might become a VNFClist.
1597 for vnfc in vnf_descriptor['vnf']['VNFC']:
1598 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +02001599 image_dict={}
1600 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1601 image_dict['universal_name']=vnfc.get('image name')
1602 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1603 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +01001604 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +02001605 image_dict['checksum']=vnfc.get('image checksum')
garciadeblas9f8456e2016-09-05 05:02:59 +02001606 image_metadata_dict = vnfc.get('image metadata', None)
1607 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +01001608 if image_metadata_dict is not None:
garciadeblas9f8456e2016-09-05 05:02:59 +02001609 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1610 image_dict['metadata']=image_metadata_str
1611 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1612 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1613 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1614 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +02001615 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno8e690322017-08-10 15:58:50 +02001616 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
tierno36c0b172017-01-12 18:32:28 +01001617 if vnfc.get("boot-data"):
1618 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
garciadeblas9f8456e2016-09-05 05:02:59 +02001619
garciadeblas9f8456e2016-09-05 05:02:59 +02001620 # Step 7. Storing the VNF descriptor in the repository
1621 if "descriptor" not in vnf_descriptor["vnf"]:
1622 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001623
garciadeblas9f8456e2016-09-05 05:02:59 +02001624 # Step 8. Adding the VNF to the NFVO DB
1625 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1626 return vnf_id
1627 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
1628 _, message = rollback(mydb, vims, rollback_list)
1629 if isinstance(e, db_base_Exception):
1630 error_text = "Exception at database"
1631 elif isinstance(e, KeyError):
1632 error_text = "KeyError exception "
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001633 e.http_code = httperrors.Internal_Server_Error
garciadeblas9f8456e2016-09-05 05:02:59 +02001634 else:
1635 error_text = "Exception at VIM"
1636 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1637 #logger.error("start_scenario %s", error_text)
1638 raise NfvoException(error_text, e.http_code)
1639
tiernob3d36742017-03-03 23:51:05 +01001640
tierno7edb6752016-03-21 17:37:52 +01001641def get_vnf_id(mydb, tenant_id, vnf_id):
1642 #check valid tenant_id
tierno42026a02017-02-10 15:13:40 +01001643 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001644 #obtain data
1645 where_or = {}
1646 if tenant_id != "any":
1647 where_or["tenant_id"] = tenant_id
1648 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001649 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
1650
tiernof1ba57e2017-09-07 12:23:19 +02001651 vnf_id = vnf["uuid"]
1652 filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
tiernof97fd272016-07-11 14:32:37 +02001653 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
tierno7edb6752016-03-21 17:37:52 +01001654 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1655 data={'vnf' : filtered_content}
1656 #GET VM
tiernof97fd272016-07-11 14:32:37 +02001657 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
tiernof1ba57e2017-09-07 12:23:19 +02001658 SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1659 'boot_data'),
tierno7edb6752016-03-21 17:37:52 +01001660 WHERE={'vnfs.uuid': vnf_id} )
gcalvinobfa2fd92018-11-13 18:47:28 +01001661 if len(content) != 0:
Anderson Bravalheric5293de2018-11-28 17:21:26 +00001662 #raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
tierno36c0b172017-01-12 18:32:28 +01001663 # change boot_data into boot-data
gcalvino319b8a52018-11-05 15:33:23 +01001664 for vm in content:
1665 if vm.get("boot_data"):
1666 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
1667 del vm["boot_data"]
tierno7edb6752016-03-21 17:37:52 +01001668
gcalvinobfa2fd92018-11-13 18:47:28 +01001669 data['vnf']['VNFC'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001670 #TODO: GET all the information from a VNFC and include it in the output.
tierno42026a02017-02-10 15:13:40 +01001671
tierno7edb6752016-03-21 17:37:52 +01001672 #GET NET
tierno42026a02017-02-10 15:13:40 +01001673 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
tierno7edb6752016-03-21 17:37:52 +01001674 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1675 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +02001676 data['vnf']['nets'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001677
1678 #GET ip-profile for each net
1679 for net in data['vnf']['nets']:
1680 ipprofiles = mydb.get_rows(FROM='ip_profiles',
1681 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1682 WHERE={'net_id': net["uuid"]} )
1683 if len(ipprofiles)==1:
1684 net["ip_profile"] = ipprofiles[0]
1685 elif len(ipprofiles)>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001686 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 +01001687
1688
garciadeblas9f8456e2016-09-05 05:02:59 +02001689 #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 +01001690
garciadeblas9f8456e2016-09-05 05:02:59 +02001691 #GET External Interfaces
tiernof97fd272016-07-11 14:32:37 +02001692 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 +01001693 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1694 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
tierno3fcfdb72017-10-24 07:48:24 +02001695 WHERE={'vnfs.uuid': vnf_id, 'interfaces.external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01001696 #print content
tiernof97fd272016-07-11 14:32:37 +02001697 data['vnf']['external-connections'] = content
tierno42026a02017-02-10 15:13:40 +01001698
tiernof97fd272016-07-11 14:32:37 +02001699 return data
tierno7edb6752016-03-21 17:37:52 +01001700
1701
1702def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
1703 # Check tenant exist
1704 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +02001705 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001706 # Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernocbb52052018-05-31 18:57:30 +02001707 vims = get_vim(mydb, tenant_id, ignore_errors=True)
tierno7edb6752016-03-21 17:37:52 +01001708 else:
1709 vims={}
1710
1711 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1712 where_or = {}
1713 if tenant_id != "any":
1714 where_or["tenant_id"] = tenant_id
1715 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001716 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 +02001717 vnf_id = vnf["uuid"]
tierno42026a02017-02-10 15:13:40 +01001718
tierno7edb6752016-03-21 17:37:52 +01001719 # "Getting the list of flavors and tenants of the VNF"
tierno42026a02017-02-10 15:13:40 +01001720 flavorList = get_flavorlist(mydb, vnf_id)
tiernof97fd272016-07-11 14:32:37 +02001721 if len(flavorList)==0:
1722 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001723
tiernof97fd272016-07-11 14:32:37 +02001724 imageList = get_imagelist(mydb, vnf_id)
1725 if len(imageList)==0:
1726 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001727
tiernof97fd272016-07-11 14:32:37 +02001728 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
1729 if deleted == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001730 raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
tierno42026a02017-02-10 15:13:40 +01001731
tierno7edb6752016-03-21 17:37:52 +01001732 undeletedItems = []
1733 for flavor in flavorList:
1734 #check if flavor is used by other vnf
tiernof97fd272016-07-11 14:32:37 +02001735 try:
1736 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
1737 if len(c) > 0:
1738 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
1739 continue
1740 #flavor not used, must be deleted
1741 #delelte at VIM
tierno96ebf002017-12-13 10:55:38 +01001742 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id': flavor})
tierno7edb6752016-03-21 17:37:52 +01001743 for flavor_vim in c:
tierno96ebf002017-12-13 10:55:38 +01001744 if not flavor_vim['created']: # skip this flavor because not created by openmano
tierno7edb6752016-03-21 17:37:52 +01001745 continue
tierno96ebf002017-12-13 10:55:38 +01001746 # look for vim
1747 myvim = None
1748 for vim in vims.values():
1749 if vim["config"]["datacenter_tenant_id"] == flavor_vim["datacenter_vim_id"]:
1750 myvim = vim
1751 break
1752 if not myvim:
tierno7edb6752016-03-21 17:37:52 +01001753 continue
tiernoae4a8d12016-07-08 12:30:39 +02001754 try:
1755 myvim.delete_flavor(flavor_vim["vim_id"])
tierno96ebf002017-12-13 10:55:38 +01001756 except vimconn.vimconnNotFoundException:
1757 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"],
1758 flavor_vim["datacenter_vim_id"] )
tiernoae4a8d12016-07-08 12:30:39 +02001759 except vimconn.vimconnException as e:
1760 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
tierno96ebf002017-12-13 10:55:38 +01001761 flavor_vim["vim_id"], flavor_vim["datacenter_vim_id"], type(e).__name__, str(e))
1762 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"],
1763 flavor_vim["datacenter_vim_id"]))
1764 # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
tiernof97fd272016-07-11 14:32:37 +02001765 mydb.delete_row_by_id('flavors', flavor)
1766 except db_base_Exception as e:
1767 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
tierno96ebf002017-12-13 10:55:38 +01001768 undeletedItems.append("flavor {}".format(flavor))
tiernof97fd272016-07-11 14:32:37 +02001769
tierno42026a02017-02-10 15:13:40 +01001770
tierno7edb6752016-03-21 17:37:52 +01001771 for image in imageList:
tiernof97fd272016-07-11 14:32:37 +02001772 try:
1773 #check if image is used by other vnf
tierno16e3dd42018-04-24 12:52:40 +02001774 c = mydb.get_rows(FROM='vms', WHERE=[{'image_id': image}, {'image_list LIKE ': '%' + image + '%'}])
tiernof97fd272016-07-11 14:32:37 +02001775 if len(c) > 0:
1776 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
1777 continue
1778 #image not used, must be deleted
1779 #delelte at VIM
1780 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
tierno7edb6752016-03-21 17:37:52 +01001781 for image_vim in c:
tierno868220c2017-09-26 00:11:05 +02001782 if image_vim["datacenter_vim_id"] not in vims: # TODO change to datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +01001783 continue
1784 if image_vim['created']=='false': #skip this image because not created by openmano
1785 continue
1786 myvim=vims[ image_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +02001787 try:
1788 myvim.delete_image(image_vim["vim_id"])
1789 except vimconn.vimconnNotFoundException as e:
1790 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
1791 except vimconn.vimconnException as e:
1792 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1793 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
1794 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +02001795 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1796 mydb.delete_row_by_id('images', image)
1797 except db_base_Exception as e:
1798 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
tierno7edb6752016-03-21 17:37:52 +01001799 undeletedItems.append("image %s" % image)
1800
tiernof97fd272016-07-11 14:32:37 +02001801 return vnf_id + " " + vnf["name"]
tierno42026a02017-02-10 15:13:40 +01001802 #if undeletedItems:
tiernof97fd272016-07-11 14:32:37 +02001803 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
tierno7edb6752016-03-21 17:37:52 +01001804
tiernob3d36742017-03-03 23:51:05 +01001805
tiernob8569aa2018-08-24 11:34:54 +02001806@deprecated("Not used")
tierno7edb6752016-03-21 17:37:52 +01001807def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
1808 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
1809 if result < 0:
1810 return result, vims
1811 elif result == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001812 return -httperrors.Not_Found, "datacenter '%s' not found" % datacenter_name
tierno7edb6752016-03-21 17:37:52 +01001813 myvim = vims.values()[0]
1814 result,servers = myvim.get_hosts_info()
1815 if result < 0:
1816 return result, servers
1817 topology = {'name':myvim['name'] , 'servers': servers}
1818 return result, topology
1819
tiernob3d36742017-03-03 23:51:05 +01001820
tierno7edb6752016-03-21 17:37:52 +01001821def get_hosts(mydb, nfvo_tenant_id):
tiernof97fd272016-07-11 14:32:37 +02001822 vims = get_vim(mydb, nfvo_tenant_id)
1823 if len(vims) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001824 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), httperrors.Not_Found)
tiernof97fd272016-07-11 14:32:37 +02001825 elif len(vims)>1:
1826 #print "nfvo.datacenter_action() error. Several datacenters found"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001827 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
tierno7edb6752016-03-21 17:37:52 +01001828 myvim = vims.values()[0]
tiernof97fd272016-07-11 14:32:37 +02001829 try:
1830 hosts = myvim.get_hosts()
1831 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01001832
tiernof97fd272016-07-11 14:32:37 +02001833 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
1834 for host in hosts:
1835 server={'name':host['name'], 'vms':[]}
1836 for vm in host['instances']:
1837 #get internal name and model
tierno42026a02017-02-10 15:13:40 +01001838 try:
tiernof97fd272016-07-11 14:32:37 +02001839 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1840 WHERE={'vim_vm_id':vm['id']} )
1841 if len(c) == 0:
1842 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1843 continue
1844 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
tierno42026a02017-02-10 15:13:40 +01001845
tiernof97fd272016-07-11 14:32:37 +02001846 except db_base_Exception as e:
1847 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1848 datacenter['Datacenters'][0]['servers'].append(server)
1849 #return -400, "en construccion"
tierno42026a02017-02-10 15:13:40 +01001850
tiernof97fd272016-07-11 14:32:37 +02001851 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1852 return datacenter
1853 except vimconn.vimconnException as e:
1854 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001855
tiernob3d36742017-03-03 23:51:05 +01001856
tiernob8569aa2018-08-24 11:34:54 +02001857@deprecated("Use new_nsd_v3")
tierno7edb6752016-03-21 17:37:52 +01001858def new_scenario(mydb, tenant_id, topo):
1859
1860# result, vims = get_vim(mydb, tenant_id)
1861# if result < 0:
1862# return result, vims
1863#1: parse input
1864 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001865 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001866 if "tenant_id" in topo:
1867 if topo["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001868 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 +01001869 httperrors.Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001870 else:
1871 tenant_id=None
1872
tierno42026a02017-02-10 15:13:40 +01001873#1.1: get VNFs and external_networks (other_nets).
tierno7edb6752016-03-21 17:37:52 +01001874 vnfs={}
1875 other_nets={} #external_networks, bridge_networks and data_networkds
1876 nodes = topo['topology']['nodes']
1877 for k in nodes.keys():
1878 if nodes[k]['type'] == 'VNF':
1879 vnfs[k] = nodes[k]
1880 vnfs[k]['ifaces'] = {}
tierno42026a02017-02-10 15:13:40 +01001881 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
tierno7edb6752016-03-21 17:37:52 +01001882 other_nets[k] = nodes[k]
1883 other_nets[k]['external']=True
tierno42026a02017-02-10 15:13:40 +01001884 elif nodes[k]['type'] == 'network':
tierno7edb6752016-03-21 17:37:52 +01001885 other_nets[k] = nodes[k]
1886 other_nets[k]['external']=False
tierno42026a02017-02-10 15:13:40 +01001887
tierno7edb6752016-03-21 17:37:52 +01001888
1889#1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1890 for name,vnf in vnfs.items():
tierno3fcfdb72017-10-24 07:48:24 +02001891 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
tierno7edb6752016-03-21 17:37:52 +01001892 error_text = ""
1893 error_pos = "'topology':'nodes':'" + name + "'"
1894 if 'vnf_id' in vnf:
1895 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001896 where['uuid'] = vnf['vnf_id']
tierno7edb6752016-03-21 17:37:52 +01001897 if 'VNF model' in vnf:
1898 error_text += " 'VNF model' " + vnf['VNF model']
tiernocea279c2016-07-18 12:36:49 +02001899 where['name'] = vnf['VNF model']
tierno3fcfdb72017-10-24 07:48:24 +02001900 if len(where) == 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001901 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +01001902
tiernocea279c2016-07-18 12:36:49 +02001903 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1904 FROM='vnfs',
tierno3fcfdb72017-10-24 07:48:24 +02001905 WHERE=where)
tiernof97fd272016-07-11 14:32:37 +02001906 if len(vnf_db)==0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001907 raise NfvoException("unknown" + error_text + " at " + error_pos, httperrors.Not_Found)
tiernof97fd272016-07-11 14:32:37 +02001908 elif len(vnf_db)>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001909 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", httperrors.Conflict)
tierno7edb6752016-03-21 17:37:52 +01001910 vnf['uuid']=vnf_db[0]['uuid']
1911 vnf['description']=vnf_db[0]['description']
1912 #get external interfaces
tierno42026a02017-02-10 15:13:40 +01001913 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1914 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 +02001915 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01001916 for ext_iface in ext_ifaces:
1917 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1918
1919#1.4 get list of connections
1920 conections = topo['topology']['connections']
1921 conections_list = []
tiernoefd80c92016-09-16 14:17:46 +02001922 conections_list_name = []
tierno7edb6752016-03-21 17:37:52 +01001923 for k in conections.keys():
1924 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1925 ifaces_list = conections[k]['nodes'].items()
1926 elif type(conections[k]['nodes'])==list: #list with dictionary
1927 ifaces_list=[]
1928 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1929 for k2 in conection_pair_list:
1930 ifaces_list += k2
1931
1932 con_type = conections[k].get("type", "link")
1933 if con_type != "link":
1934 if k in other_nets:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001935 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001936 other_nets[k] = {'external': False}
1937 if conections[k].get("graph"):
1938 other_nets[k]["graph"] = conections[k]["graph"]
1939 ifaces_list.append( (k, None) )
1940
tierno42026a02017-02-10 15:13:40 +01001941
tierno7edb6752016-03-21 17:37:52 +01001942 if con_type == "external_network":
1943 other_nets[k]['external'] = True
1944 if conections[k].get("model"):
1945 other_nets[k]["model"] = conections[k]["model"]
1946 else:
1947 other_nets[k]["model"] = k
tierno42026a02017-02-10 15:13:40 +01001948 if con_type == "dataplane_net" or con_type == "bridge_net":
tierno7edb6752016-03-21 17:37:52 +01001949 other_nets[k]["model"] = con_type
tierno42026a02017-02-10 15:13:40 +01001950
tiernoefd80c92016-09-16 14:17:46 +02001951 conections_list_name.append(k)
tierno7edb6752016-03-21 17:37:52 +01001952 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)
1953 #print set(ifaces_list)
1954 #check valid VNF and iface names
1955 for iface in ifaces_list:
1956 if iface[0] not in vnfs and iface[0] not in other_nets :
tiernof97fd272016-07-11 14:32:37 +02001957 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001958 str(k), iface[0]), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001959 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
tiernof97fd272016-07-11 14:32:37 +02001960 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001961 str(k), iface[0], iface[1]), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001962
1963#1.5 unify connections from the pair list to a consolidated list
1964 index=0
1965 while index < len(conections_list):
1966 index2 = index+1
1967 while index2 < len(conections_list):
1968 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
1969 conections_list[index] |= conections_list[index2]
1970 del conections_list[index2]
tiernoefd80c92016-09-16 14:17:46 +02001971 del conections_list_name[index2]
tierno7edb6752016-03-21 17:37:52 +01001972 else:
1973 index2 += 1
1974 conections_list[index] = list(conections_list[index]) # from set to list again
1975 index += 1
1976 #for k in conections_list:
1977 # print k
tierno42026a02017-02-10 15:13:40 +01001978
tierno7edb6752016-03-21 17:37:52 +01001979
1980
1981#1.6 Delete non external nets
1982# for k in other_nets.keys():
1983# if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1984# for con in conections_list:
1985# delete_indexes=[]
1986# for index in range(0,len(con)):
1987# if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1988# for index in delete_indexes:
1989# del con[index]
1990# del other_nets[k]
1991#1.7: Check external_ports are present at database table datacenter_nets
1992 for k,net in other_nets.items():
1993 error_pos = "'topology':'nodes':'" + k + "'"
1994 if net['external']==False:
1995 if 'name' not in net:
1996 net['name']=k
1997 if 'model' not in net:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001998 raise NfvoException("needed a 'model' at " + error_pos, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001999 if net['model']=='bridge_net':
2000 net['type']='bridge';
2001 elif net['model']=='dataplane_net':
2002 net['type']='data';
2003 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002004 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002005 else: #external
2006#IF we do not want to check that external network exist at datacenter
2007 pass
tierno42026a02017-02-10 15:13:40 +01002008#ELSE
tierno7edb6752016-03-21 17:37:52 +01002009# error_text = ""
2010# WHERE_={}
2011# if 'net_id' in net:
2012# error_text += " 'net_id' " + net['net_id']
2013# WHERE_['uuid'] = net['net_id']
2014# if 'model' in net:
2015# error_text += " 'model' " + net['model']
2016# WHERE_['name'] = net['model']
2017# if len(WHERE_) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002018# return -httperrors.Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
tierno7edb6752016-03-21 17:37:52 +01002019# r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
2020# FROM='datacenter_nets', WHERE=WHERE_ )
2021# if r<0:
2022# print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
2023# elif r==0:
2024# print "nfvo.new_scenario Error" +error_text+ " is not present at database"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002025# return -httperrors.Bad_Request, "unknown " +error_text+ " at " + error_pos
tierno7edb6752016-03-21 17:37:52 +01002026# elif r>1:
tierno42026a02017-02-10 15:13:40 +01002027# print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002028# 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 +01002029# other_nets[k].update(net_db[0])
tierno42026a02017-02-10 15:13:40 +01002030#ENDIF
tierno7edb6752016-03-21 17:37:52 +01002031 net_list={}
2032 net_nb=0 #Number of nets
2033 for con in conections_list:
2034 #check if this is connected to a external net
2035 other_net_index=-1
2036 #print
2037 #print "con", con
2038 for index in range(0,len(con)):
2039 #check if this is connected to a external net
2040 for net_key in other_nets.keys():
2041 if con[index][0]==net_key:
2042 if other_net_index>=0:
tierno42026a02017-02-10 15:13:40 +01002043 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 +02002044 #print "nfvo.new_scenario " + error_text
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002045 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002046 else:
2047 other_net_index = index
2048 net_target = net_key
2049 break
2050 #print "other_net_index", other_net_index
2051 try:
2052 if other_net_index>=0:
2053 del con[other_net_index]
2054#IF we do not want to check that external network exist at datacenter
2055 if other_nets[net_target]['external'] :
2056 if "name" not in other_nets[net_target]:
2057 other_nets[net_target]['name'] = other_nets[net_target]['model']
2058 if other_nets[net_target]["type"] == "external_network":
2059 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
2060 other_nets[net_target]["type"] = "data"
2061 else:
2062 other_nets[net_target]["type"] = "bridge"
tierno42026a02017-02-10 15:13:40 +01002063#ELSE
tierno7edb6752016-03-21 17:37:52 +01002064# if other_nets[net_target]['external'] :
2065# 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
2066# if type_=='data' and other_nets[net_target]['type']=="ptp":
2067# error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
2068# print "nfvo.new_scenario " + error_text
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002069# return -httperrors.Bad_Request, error_text
tierno42026a02017-02-10 15:13:40 +01002070#ENDIF
tierno7edb6752016-03-21 17:37:52 +01002071 for iface in con:
2072 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
2073 else:
2074 #create a net
2075 net_type_bridge=False
2076 net_type_data=False
2077 net_target = "__-__net"+str(net_nb)
tierno42026a02017-02-10 15:13:40 +01002078 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
tiernoefd80c92016-09-16 14:17:46 +02002079 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
tierno42026a02017-02-10 15:13:40 +01002080 'external':False}
tierno7edb6752016-03-21 17:37:52 +01002081 for iface in con:
2082 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
2083 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
2084 if iface_type=='mgmt' or iface_type=='bridge':
2085 net_type_bridge = True
2086 else:
2087 net_type_data = True
2088 if net_type_bridge and net_type_data:
2089 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 +02002090 #print "nfvo.new_scenario " + error_text
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002091 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002092 elif net_type_bridge:
2093 type_='bridge'
2094 else:
2095 type_='data' if len(con)>2 else 'ptp'
2096 net_list[net_target]['type'] = type_
2097 net_nb+=1
2098 except Exception:
2099 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02002100 #print "nfvo.new_scenario " + error_text
tierno7edb6752016-03-21 17:37:52 +01002101 #raise e
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002102 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002103
2104#1.8: Connect to management net all not already connected interfaces of type 'mgmt'
tierno42026a02017-02-10 15:13:40 +01002105 #1.8.1 obtain management net
tiernof97fd272016-07-11 14:32:37 +02002106 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
tierno7edb6752016-03-21 17:37:52 +01002107 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
tierno42026a02017-02-10 15:13:40 +01002108 #1.8.2 check all interfaces from all vnfs
tiernof97fd272016-07-11 14:32:37 +02002109 if len(mgmt_net)>0:
tierno7edb6752016-03-21 17:37:52 +01002110 add_mgmt_net = False
2111 for vnf in vnfs.values():
2112 for iface in vnf['ifaces'].values():
2113 if iface['type']=='mgmt' and 'net_key' not in iface:
2114 #iface not connected
2115 iface['net_key'] = 'mgmt'
2116 add_mgmt_net = True
2117 if add_mgmt_net and 'mgmt' not in net_list:
2118 net_list['mgmt']=mgmt_net[0]
2119 net_list['mgmt']['external']=True
2120 net_list['mgmt']['graph']={'visible':False}
2121
2122 net_list.update(other_nets)
tiernof97fd272016-07-11 14:32:37 +02002123 #print
2124 #print 'net_list', net_list
2125 #print
2126 #print 'vnfs', vnfs
2127 #print
tierno7edb6752016-03-21 17:37:52 +01002128
2129#2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
tiernof97fd272016-07-11 14:32:37 +02002130 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
tierno392f2852016-05-13 12:28:55 +02002131 'tenant_id':tenant_id, 'name':topo['name'],
2132 'description':topo.get('description',topo['name']),
2133 'public': topo.get('public', False)
2134 })
tierno42026a02017-02-10 15:13:40 +01002135
tiernof97fd272016-07-11 14:32:37 +02002136 return c
tierno7edb6752016-03-21 17:37:52 +01002137
tiernob3d36742017-03-03 23:51:05 +01002138
tiernob8569aa2018-08-24 11:34:54 +02002139@deprecated("Use new_nsd_v3")
tierno5bb59dc2017-02-13 14:53:54 +01002140def new_scenario_v02(mydb, tenant_id, scenario_dict, version):
2141 """ This creates a new scenario for version 0.2 and 0.3"""
tierno392f2852016-05-13 12:28:55 +02002142 scenario = scenario_dict["scenario"]
tierno7edb6752016-03-21 17:37:52 +01002143 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01002144 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002145 if "tenant_id" in scenario:
2146 if scenario["tenant_id"] != tenant_id:
tierno5bb59dc2017-02-13 14:53:54 +01002147 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
tiernof97fd272016-07-11 14:32:37 +02002148 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002149 scenario["tenant_id"], tenant_id), httperrors.Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01002150 else:
2151 tenant_id=None
2152
tierno5bb59dc2017-02-13 14:53:54 +01002153 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
tierno7edb6752016-03-21 17:37:52 +01002154 for name,vnf in scenario["vnfs"].iteritems():
tierno3fcfdb72017-10-24 07:48:24 +02002155 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
tierno7edb6752016-03-21 17:37:52 +01002156 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02002157 error_pos = "'scenario':'vnfs':'" + name + "'"
tierno7edb6752016-03-21 17:37:52 +01002158 if 'vnf_id' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01002159 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02002160 where['uuid'] = vnf['vnf_id']
tierno392f2852016-05-13 12:28:55 +02002161 if 'vnf_name' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01002162 error_text += " 'vnf_name' " + vnf['vnf_name']
tiernocea279c2016-07-18 12:36:49 +02002163 where['name'] = vnf['vnf_name']
tierno3fcfdb72017-10-24 07:48:24 +02002164 if len(where) == 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002165 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, httperrors.Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002166 vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'),
tiernocea279c2016-07-18 12:36:49 +02002167 FROM='vnfs',
tierno3fcfdb72017-10-24 07:48:24 +02002168 WHERE=where)
tierno5bb59dc2017-02-13 14:53:54 +01002169 if len(vnf_db) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002170 raise NfvoException("Unknown" + error_text + " at " + error_pos, httperrors.Not_Found)
tierno5bb59dc2017-02-13 14:53:54 +01002171 elif len(vnf_db) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002172 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", httperrors.Conflict)
tierno5bb59dc2017-02-13 14:53:54 +01002173 vnf['uuid'] = vnf_db[0]['uuid']
2174 vnf['description'] = vnf_db[0]['description']
tierno7edb6752016-03-21 17:37:52 +01002175 vnf['ifaces'] = {}
tierno5bb59dc2017-02-13 14:53:54 +01002176 # get external interfaces
2177 ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
2178 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 +02002179 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01002180 for ext_iface in ext_ifaces:
tierno5bb59dc2017-02-13 14:53:54 +01002181 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']}
2182 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
tierno7edb6752016-03-21 17:37:52 +01002183
tierno5bb59dc2017-02-13 14:53:54 +01002184 # 2: Insert net_key and ip_address at every vnf interface
2185 for net_name, net in scenario["networks"].items():
2186 net_type_bridge = False
2187 net_type_data = False
tierno7edb6752016-03-21 17:37:52 +01002188 for iface_dict in net["interfaces"]:
tierno5bb59dc2017-02-13 14:53:54 +01002189 if version == "0.2":
2190 temp_dict = iface_dict
2191 ip_address = None
2192 elif version == "0.3":
2193 temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]}
2194 ip_address = iface_dict.get('ip_address', None)
2195 for vnf, iface in temp_dict.items():
tierno7edb6752016-03-21 17:37:52 +01002196 if vnf not in scenario["vnfs"]:
tierno5bb59dc2017-02-13 14:53:54 +01002197 error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2198 net_name, vnf)
2199 # logger.debug("nfvo.new_scenario_v02 " + error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002200 raise NfvoException(error_text, httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002201 if iface not in scenario["vnfs"][vnf]['ifaces']:
tierno5bb59dc2017-02-13 14:53:54 +01002202 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2203 .format(net_name, iface)
2204 # logger.debug("nfvo.new_scenario_v02 " + error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002205 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002206 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
tierno5bb59dc2017-02-13 14:53:54 +01002207 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2208 "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
2209 # logger.debug("nfvo.new_scenario_v02 " + error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002210 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002211 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
tierno5bb59dc2017-02-13 14:53:54 +01002212 scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address
tierno7edb6752016-03-21 17:37:52 +01002213 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
tierno5bb59dc2017-02-13 14:53:54 +01002214 if iface_type == 'mgmt' or iface_type == 'bridge':
tierno7edb6752016-03-21 17:37:52 +01002215 net_type_bridge = True
2216 else:
2217 net_type_data = True
tierno5bb59dc2017-02-13 14:53:54 +01002218
tierno7edb6752016-03-21 17:37:52 +01002219 if net_type_bridge and net_type_data:
tierno5bb59dc2017-02-13 14:53:54 +01002220 error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2221 .format(net_name)
2222 # logger.debug("nfvo.new_scenario " + error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002223 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002224 elif net_type_bridge:
tierno5bb59dc2017-02-13 14:53:54 +01002225 type_ = 'bridge'
tierno7edb6752016-03-21 17:37:52 +01002226 else:
tierno5bb59dc2017-02-13 14:53:54 +01002227 type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp'
2228
2229 if net.get("implementation"): # for v0.3
2230 if type_ == "bridge" and net["implementation"] == "underlay":
2231 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2232 "'network':'{}'".format(net_name)
2233 # logger.debug(error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002234 raise NfvoException(error_text, httperrors.Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002235 elif type_ != "bridge" and net["implementation"] == "overlay":
2236 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2237 "'network':'{}'".format(net_name)
2238 # logger.debug(error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002239 raise NfvoException(error_text, httperrors.Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002240 net.pop("implementation")
2241 if "type" in net and version == "0.3": # for v0.3
2242 if type_ == "data" and net["type"] == "e-line":
2243 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\
2244 "'e-line' at 'network':'{}'".format(net_name)
2245 # logger.debug(error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002246 raise NfvoException(error_text, httperrors.Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002247 elif type_ == "ptp" and net["type"] == "e-lan":
2248 type_ = "data"
2249
tierno7edb6752016-03-21 17:37:52 +01002250 net['type'] = type_
2251 net['name'] = net_name
2252 net['external'] = net.get('external', False)
2253
tierno5bb59dc2017-02-13 14:53:54 +01002254 # 3: insert at database
tierno7edb6752016-03-21 17:37:52 +01002255 scenario["nets"] = scenario["networks"]
2256 scenario['tenant_id'] = tenant_id
tierno5bb59dc2017-02-13 14:53:54 +01002257 scenario_id = mydb.new_scenario(scenario)
tiernof97fd272016-07-11 14:32:37 +02002258 return scenario_id
tierno7edb6752016-03-21 17:37:52 +01002259
tiernob3d36742017-03-03 23:51:05 +01002260
tiernof1ba57e2017-09-07 12:23:19 +02002261def new_nsd_v3(mydb, tenant_id, nsd_descriptor):
2262 """
2263 Parses an OSM IM nsd_catalog and insert at DB
2264 :param mydb:
2265 :param tenant_id:
2266 :param nsd_descriptor:
Igor D.Ccaadc442017-11-06 12:48:48 +00002267 :return: The list of created NSD ids
tiernof1ba57e2017-09-07 12:23:19 +02002268 """
2269 try:
2270 mynsd = nsd_catalog.nsd()
tiernoa9550202017-09-22 13:31:35 +02002271 try:
tiernof6bbe222019-04-09 14:19:40 +00002272 pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd, skip_unknown=True)
tiernoa9550202017-09-22 13:31:35 +02002273 except Exception as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002274 raise NfvoException("Error. Invalid NS descriptor format: " + str(e), httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002275 db_scenarios = []
2276 db_sce_nets = []
2277 db_sce_vnfs = []
2278 db_sce_interfaces = []
Igor D.Ccaadc442017-11-06 12:48:48 +00002279 db_sce_vnffgs = []
2280 db_sce_rsps = []
2281 db_sce_rsp_hops = []
2282 db_sce_classifiers = []
2283 db_sce_classifier_matches = []
tiernof1ba57e2017-09-07 12:23:19 +02002284 db_ip_profiles = []
2285 db_ip_profiles_index = 0
2286 uuid_list = []
2287 nsd_uuid_list = []
tiernob2880eb2017-10-04 15:04:53 +02002288 for nsd_yang in mynsd.nsd_catalog.nsd.itervalues():
2289 nsd = nsd_yang.get()
tiernof1ba57e2017-09-07 12:23:19 +02002290
Igor D.Ccaadc442017-11-06 12:48:48 +00002291 # table scenarios
tiernof1ba57e2017-09-07 12:23:19 +02002292 scenario_uuid = str(uuid4())
2293 uuid_list.append(scenario_uuid)
2294 nsd_uuid_list.append(scenario_uuid)
2295 db_scenario = {
2296 "uuid": scenario_uuid,
2297 "osm_id": get_str(nsd, "id", 255),
2298 "name": get_str(nsd, "name", 255),
2299 "description": get_str(nsd, "description", 255),
2300 "tenant_id": tenant_id,
2301 "vendor": get_str(nsd, "vendor", 255),
2302 "short_name": get_str(nsd, "short-name", 255),
2303 "descriptor": str(nsd_descriptor)[:60000],
2304 }
2305 db_scenarios.append(db_scenario)
2306
2307 # table sce_vnfs (constituent-vnfd)
2308 vnf_index2scevnf_uuid = {}
2309 vnf_index2vnf_uuid = {}
2310 for vnf in nsd.get("constituent-vnfd").itervalues():
2311 existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255],
2312 'tenant_id': tenant_id})
2313 if not existing_vnf:
tiernob2880eb2017-10-04 15:04:53 +02002314 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2315 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2316 str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002317 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002318 sce_vnf_uuid = str(uuid4())
2319 uuid_list.append(sce_vnf_uuid)
2320 db_sce_vnf = {
2321 "uuid": sce_vnf_uuid,
2322 "scenario_id": scenario_uuid,
tierno92c36fd2018-05-04 12:21:10 +02002323 # "name": get_str(vnf, "member-vnf-index", 255),
2324 "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 50),
tiernof1ba57e2017-09-07 12:23:19 +02002325 "vnf_id": existing_vnf[0]["uuid"],
tierno16e3dd42018-04-24 12:52:40 +02002326 "member_vnf_index": str(vnf["member-vnf-index"]),
tiernof1ba57e2017-09-07 12:23:19 +02002327 # TODO 'start-by-default': True
2328 }
tierno16e3dd42018-04-24 12:52:40 +02002329 vnf_index2scevnf_uuid[str(vnf['member-vnf-index'])] = sce_vnf_uuid
2330 vnf_index2vnf_uuid[str(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"]
tiernof1ba57e2017-09-07 12:23:19 +02002331 db_sce_vnfs.append(db_sce_vnf)
2332
2333 # table ip_profiles (ip-profiles)
2334 ip_profile_name2db_table_index = {}
2335 for ip_profile in nsd.get("ip-profiles").itervalues():
2336 db_ip_profile = {
2337 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
2338 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
2339 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
2340 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2341 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
2342 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
2343 }
2344 dns_list = []
2345 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
2346 dns_list.append(str(dns.get("address")))
2347 db_ip_profile["dns_address"] = ";".join(dns_list)
2348 if ip_profile["ip-profile-params"].get('security-group'):
2349 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
2350 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
2351 db_ip_profiles_index += 1
2352 db_ip_profiles.append(db_ip_profile)
2353
2354 # table sce_nets (internal-vld)
2355 for vld in nsd.get("vld").itervalues():
2356 sce_net_uuid = str(uuid4())
2357 uuid_list.append(sce_net_uuid)
2358 db_sce_net = {
2359 "uuid": sce_net_uuid,
2360 "name": get_str(vld, "name", 255),
2361 "scenario_id": scenario_uuid,
2362 # "type": #TODO
2363 "multipoint": not vld.get("type") == "ELINE",
tierno1df468d2018-07-06 14:25:16 +02002364 "osm_id": get_str(vld, "id", 255),
tiernof1ba57e2017-09-07 12:23:19 +02002365 # "external": #TODO
2366 "description": get_str(vld, "description", 255),
2367 }
2368 # guess type of network
2369 if vld.get("mgmt-network"):
2370 db_sce_net["type"] = "bridge"
2371 db_sce_net["external"] = True
2372 elif vld.get("provider-network").get("overlay-type") == "VLAN":
2373 db_sce_net["type"] = "data"
2374 else:
tierno66eba6e2017-11-10 17:09:18 +01002375 # later on it will be fixed to bridge or data depending on the type of interfaces attached to it
2376 db_sce_net["type"] = None
tiernof1ba57e2017-09-07 12:23:19 +02002377 db_sce_nets.append(db_sce_net)
2378
2379 # ip-profile, link db_ip_profile with db_sce_net
2380 if vld.get("ip-profile-ref"):
2381 ip_profile_name = vld.get("ip-profile-ref")
2382 if ip_profile_name not in ip_profile_name2db_table_index:
tiernob2880eb2017-10-04 15:04:53 +02002383 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2384 " Reference to a non-existing 'ip_profiles'".format(
2385 str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002386 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002387 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["sce_net_id"] = sce_net_uuid
tierno8f79ea12018-05-03 17:37:40 +02002388 elif vld.get("vim-network-name"):
2389 db_sce_net["vim_network_name"] = get_str(vld, "vim-network-name", 255)
tiernof1ba57e2017-09-07 12:23:19 +02002390
2391 # table sce_interfaces (vld:vnfd-connection-point-ref)
2392 for iface in vld.get("vnfd-connection-point-ref").itervalues():
tierno16e3dd42018-04-24 12:52:40 +02002393 vnf_index = str(iface['member-vnf-index-ref'])
tiernof1ba57e2017-09-07 12:23:19 +02002394 # check correct parameters
2395 if vnf_index not in vnf_index2vnf_uuid:
tiernob2880eb2017-10-04 15:04:53 +02002396 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2397 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2398 "'nsd':'constituent-vnfd'".format(
2399 str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002400 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002401
tierno66eba6e2017-11-10 17:09:18 +01002402 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid', 'i.type as iface_type'),
tiernof1ba57e2017-09-07 12:23:19 +02002403 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2404 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2405 'external_name': get_str(iface, "vnfd-connection-point-ref",
2406 255)})
2407 if not existing_ifaces:
tiernob2880eb2017-10-04 15:04:53 +02002408 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2409 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2410 "connection-point name at VNFD '{}'".format(
2411 str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]),
2412 str(iface.get("vnfd-id-ref"))[:255]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002413 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002414 interface_uuid = existing_ifaces[0]["uuid"]
garciadeblasebd66722019-01-31 16:01:31 +00002415 if existing_ifaces[0]["iface_type"] == "data":
tierno66eba6e2017-11-10 17:09:18 +01002416 db_sce_net["type"] = "data"
tiernof1ba57e2017-09-07 12:23:19 +02002417 sce_interface_uuid = str(uuid4())
2418 uuid_list.append(sce_net_uuid)
tierno41a69812018-02-16 14:34:33 +01002419 iface_ip_address = None
2420 if iface.get("ip-address"):
2421 iface_ip_address = str(iface.get("ip-address"))
tiernof1ba57e2017-09-07 12:23:19 +02002422 db_sce_interface = {
2423 "uuid": sce_interface_uuid,
2424 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2425 "sce_net_id": sce_net_uuid,
2426 "interface_id": interface_uuid,
tierno41a69812018-02-16 14:34:33 +01002427 "ip_address": iface_ip_address,
tiernof1ba57e2017-09-07 12:23:19 +02002428 }
2429 db_sce_interfaces.append(db_sce_interface)
tierno66eba6e2017-11-10 17:09:18 +01002430 if not db_sce_net["type"]:
2431 db_sce_net["type"] = "bridge"
tiernof1ba57e2017-09-07 12:23:19 +02002432
Igor D.Ccaadc442017-11-06 12:48:48 +00002433 # table sce_vnffgs (vnffgd)
2434 for vnffg in nsd.get("vnffgd").itervalues():
2435 sce_vnffg_uuid = str(uuid4())
2436 uuid_list.append(sce_vnffg_uuid)
2437 db_sce_vnffg = {
2438 "uuid": sce_vnffg_uuid,
2439 "name": get_str(vnffg, "name", 255),
2440 "scenario_id": scenario_uuid,
2441 "vendor": get_str(vnffg, "vendor", 255),
2442 "description": get_str(vld, "description", 255),
2443 }
2444 db_sce_vnffgs.append(db_sce_vnffg)
2445
2446 # deal with rsps
Igor D.Ccaadc442017-11-06 12:48:48 +00002447 for rsp in vnffg.get("rsp").itervalues():
2448 sce_rsp_uuid = str(uuid4())
2449 uuid_list.append(sce_rsp_uuid)
2450 db_sce_rsp = {
2451 "uuid": sce_rsp_uuid,
2452 "name": get_str(rsp, "name", 255),
2453 "sce_vnffg_id": sce_vnffg_uuid,
2454 "id": get_str(rsp, "id", 255), # only useful to link with classifiers; will be removed later in the code
2455 }
2456 db_sce_rsps.append(db_sce_rsp)
Igor D.Ccaadc442017-11-06 12:48:48 +00002457 for iface in rsp.get("vnfd-connection-point-ref").itervalues():
tierno16e3dd42018-04-24 12:52:40 +02002458 vnf_index = str(iface['member-vnf-index-ref'])
Igor D.Ccaadc442017-11-06 12:48:48 +00002459 if_order = int(iface['order'])
2460 # check correct parameters
2461 if vnf_index not in vnf_index2vnf_uuid:
2462 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2463 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2464 "'nsd':'constituent-vnfd'".format(
2465 str(nsd["id"]), str(rsp["id"]), str(iface["member-vnf-index-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002466 httperrors.Bad_Request)
Igor D.Ccaadc442017-11-06 12:48:48 +00002467
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002468 ingress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2469 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2470 WHERE={
2471 'vnf_id': vnf_index2vnf_uuid[vnf_index],
2472 'external_name': get_str(iface, "vnfd-ingress-connection-point-ref",
2473 255)})
2474 if not ingress_existing_ifaces:
Igor D.Ccaadc442017-11-06 12:48:48 +00002475 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002476 "-ref':'vnfd-ingress-connection-point-ref':'{}'. Reference to a non-existing "
Igor D.Ccaadc442017-11-06 12:48:48 +00002477 "connection-point name at VNFD '{}'".format(
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002478 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-ingress-connection-point-ref"]),
2479 str(iface.get("vnfd-id-ref"))[:255]), httperrors.Bad_Request)
2480
2481 egress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2482 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2483 WHERE={
2484 'vnf_id': vnf_index2vnf_uuid[vnf_index],
2485 'external_name': get_str(iface, "vnfd-egress-connection-point-ref",
2486 255)})
2487 if not egress_existing_ifaces:
2488 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2489 "-ref':'vnfd-egress-connection-point-ref':'{}'. Reference to a non-existing "
2490 "connection-point name at VNFD '{}'".format(
2491 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-egress-connection-point-ref"]),
2492 str(iface.get("vnfd-id-ref"))[:255]), HTTP_Bad_Request)
2493
2494 ingress_interface_uuid = ingress_existing_ifaces[0]["uuid"]
2495 egress_interface_uuid = egress_existing_ifaces[0]["uuid"]
Igor D.Ccaadc442017-11-06 12:48:48 +00002496 sce_rsp_hop_uuid = str(uuid4())
2497 uuid_list.append(sce_rsp_hop_uuid)
2498 db_sce_rsp_hop = {
2499 "uuid": sce_rsp_hop_uuid,
2500 "if_order": if_order,
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002501 "ingress_interface_id": ingress_interface_uuid,
2502 "egress_interface_id": egress_interface_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00002503 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2504 "sce_rsp_id": sce_rsp_uuid,
2505 }
2506 db_sce_rsp_hops.append(db_sce_rsp_hop)
2507
2508 # deal with classifiers
Igor D.Ccaadc442017-11-06 12:48:48 +00002509 for classifier in vnffg.get("classifier").itervalues():
2510 sce_classifier_uuid = str(uuid4())
2511 uuid_list.append(sce_classifier_uuid)
2512
2513 # source VNF
tierno16e3dd42018-04-24 12:52:40 +02002514 vnf_index = str(classifier['member-vnf-index-ref'])
Igor D.Ccaadc442017-11-06 12:48:48 +00002515 if vnf_index not in vnf_index2vnf_uuid:
2516 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point"
2517 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2518 "'nsd':'constituent-vnfd'".format(
2519 str(nsd["id"]), str(classifier["id"]), str(classifier["member-vnf-index-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002520 httperrors.Bad_Request)
Igor D.Ccaadc442017-11-06 12:48:48 +00002521 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2522 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2523 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2524 'external_name': get_str(classifier, "vnfd-connection-point-ref",
2525 255)})
2526 if not existing_ifaces:
2527 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2528 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2529 "connection-point name at VNFD '{}'".format(
2530 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]),
2531 str(iface.get("vnfd-id-ref"))[:255]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002532 httperrors.Bad_Request)
Igor D.Ccaadc442017-11-06 12:48:48 +00002533 interface_uuid = existing_ifaces[0]["uuid"]
2534
2535 db_sce_classifier = {
2536 "uuid": sce_classifier_uuid,
2537 "name": get_str(classifier, "name", 255),
2538 "sce_vnffg_id": sce_vnffg_uuid,
2539 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2540 "interface_id": interface_uuid,
2541 }
2542 rsp_id = get_str(classifier, "rsp-id-ref", 255)
2543 rsp = next((item for item in db_sce_rsps if item["id"] == rsp_id), None)
2544 db_sce_classifier["sce_rsp_id"] = rsp["uuid"]
2545 db_sce_classifiers.append(db_sce_classifier)
2546
Igor D.Ccaadc442017-11-06 12:48:48 +00002547 for match in classifier.get("match-attributes").itervalues():
2548 sce_classifier_match_uuid = str(uuid4())
2549 uuid_list.append(sce_classifier_match_uuid)
2550 db_sce_classifier_match = {
2551 "uuid": sce_classifier_match_uuid,
2552 "ip_proto": get_str(match, "ip-proto", 2),
2553 "source_ip": get_str(match, "source-ip-address", 16),
2554 "destination_ip": get_str(match, "destination-ip-address", 16),
2555 "source_port": get_str(match, "source-port", 5),
2556 "destination_port": get_str(match, "destination-port", 5),
2557 "sce_classifier_id": sce_classifier_uuid,
2558 }
2559 db_sce_classifier_matches.append(db_sce_classifier_match)
2560 # TODO: vnf/cp keys
2561
2562 # remove unneeded id's in sce_rsps
2563 for rsp in db_sce_rsps:
2564 rsp.pop('id')
2565
tiernof1ba57e2017-09-07 12:23:19 +02002566 db_tables = [
2567 {"scenarios": db_scenarios},
2568 {"sce_nets": db_sce_nets},
2569 {"ip_profiles": db_ip_profiles},
2570 {"sce_vnfs": db_sce_vnfs},
2571 {"sce_interfaces": db_sce_interfaces},
Igor D.Ccaadc442017-11-06 12:48:48 +00002572 {"sce_vnffgs": db_sce_vnffgs},
2573 {"sce_rsps": db_sce_rsps},
2574 {"sce_rsp_hops": db_sce_rsp_hops},
2575 {"sce_classifiers": db_sce_classifiers},
2576 {"sce_classifier_matches": db_sce_classifier_matches},
tiernof1ba57e2017-09-07 12:23:19 +02002577 ]
2578
Igor D.Ccaadc442017-11-06 12:48:48 +00002579 logger.debug("new_nsd_v3 done: %s",
tiernof1ba57e2017-09-07 12:23:19 +02002580 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
2581 mydb.new_rows(db_tables, uuid_list)
2582 return nsd_uuid_list
tiernob2880eb2017-10-04 15:04:53 +02002583 except NfvoException:
2584 raise
tiernof1ba57e2017-09-07 12:23:19 +02002585 except Exception as e:
2586 logger.error("Exception {}".format(e))
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002587 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002588
2589
tierno7edb6752016-03-21 17:37:52 +01002590def edit_scenario(mydb, tenant_id, scenario_id, data):
2591 data["uuid"] = scenario_id
2592 data["tenant_id"] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02002593 c = mydb.edit_scenario( data )
2594 return c
tierno7edb6752016-03-21 17:37:52 +01002595
tiernob3d36742017-03-03 23:51:05 +01002596
tiernob8569aa2018-08-24 11:34:54 +02002597@deprecated("Use create_instance")
tierno7edb6752016-03-21 17:37:52 +01002598def 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 +02002599 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00002600 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
2601 vims = {datacenter_id: myvim}
tierno392f2852016-05-13 12:28:55 +02002602 myvim_tenant = myvim['tenant_id']
tierno7edb6752016-03-21 17:37:52 +01002603 datacenter_name = myvim['name']
tiernoa2793912016-10-04 08:15:08 +00002604
tierno7edb6752016-03-21 17:37:52 +01002605 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02002606 try:
2607 #print "Checking that the scenario_id exists and getting the scenario dictionary"
tierno868220c2017-09-26 00:11:05 +02002608 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id)
tiernoa2793912016-10-04 08:15:08 +00002609 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
tiernoae4a8d12016-07-08 12:30:39 +02002610 scenarioDict['datacenter_id'] = datacenter_id
2611 #print '================scenarioDict======================='
2612 #print json.dumps(scenarioDict, indent=4)
2613 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
tierno42026a02017-02-10 15:13:40 +01002614
tiernoae4a8d12016-07-08 12:30:39 +02002615 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
2616 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002617
tiernoae4a8d12016-07-08 12:30:39 +02002618 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2619 auxNetDict['scenario'] = {}
tierno42026a02017-02-10 15:13:40 +01002620
tiernoae4a8d12016-07-08 12:30:39 +02002621 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2622 for sce_net in scenarioDict['nets']:
2623 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
tierno42026a02017-02-10 15:13:40 +01002624
tiernoae4a8d12016-07-08 12:30:39 +02002625 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
tierno7edb6752016-03-21 17:37:52 +01002626 myNetName = myNetName[0:255] #limit length
tiernoae4a8d12016-07-08 12:30:39 +02002627 myNetType = sce_net['type']
tierno7edb6752016-03-21 17:37:52 +01002628 myNetDict = {}
2629 myNetDict["name"] = myNetName
2630 myNetDict["type"] = myNetType
2631 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02002632 myNetIPProfile = sce_net.get('ip_profile', None)
tierno7edb6752016-03-21 17:37:52 +01002633 #TODO:
tiernoae4a8d12016-07-08 12:30:39 +02002634 #We should use the dictionary as input parameter for new_network
tiernof97fd272016-07-11 14:32:37 +02002635 #print myNetDict
tiernoae4a8d12016-07-08 12:30:39 +02002636 if not sce_net["external"]:
garciadeblasebd66722019-01-31 16:01:31 +00002637 network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02002638 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2639 sce_net['vim_id'] = network_id
2640 auxNetDict['scenario'][sce_net['uuid']] = network_id
2641 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002642 sce_net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02002643 else:
2644 if sce_net['vim_id'] == None:
2645 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
2646 _, message = rollback(mydb, vims, rollbackList)
2647 logger.error("nfvo.start_scenario: %s", error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002648 raise NfvoException(error_text, httperrors.Bad_Request)
tiernoae4a8d12016-07-08 12:30:39 +02002649 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
2650 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
tierno42026a02017-02-10 15:13:40 +01002651
tiernoae4a8d12016-07-08 12:30:39 +02002652 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2653 #For each vnf net, we create it and we add it to instanceNetlist.
mirabal29356312017-07-27 12:21:22 +02002654
tiernoae4a8d12016-07-08 12:30:39 +02002655 for sce_vnf in scenarioDict['vnfs']:
2656 for net in sce_vnf['nets']:
2657 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
tierno42026a02017-02-10 15:13:40 +01002658
tiernoae4a8d12016-07-08 12:30:39 +02002659 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
2660 myNetName = myNetName[0:255] #limit length
2661 myNetType = net['type']
2662 myNetDict = {}
2663 myNetDict["name"] = myNetName
2664 myNetDict["type"] = myNetType
2665 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02002666 myNetIPProfile = net.get('ip_profile', None)
tiernoae4a8d12016-07-08 12:30:39 +02002667 #print myNetDict
2668 #TODO:
2669 #We should use the dictionary as input parameter for new_network
garciadeblasebd66722019-01-31 16:01:31 +00002670 network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02002671 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2672 net['vim_id'] = network_id
2673 if sce_vnf['uuid'] not in auxNetDict:
2674 auxNetDict[sce_vnf['uuid']] = {}
2675 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
2676 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002677 net["created"] = True
tierno42026a02017-02-10 15:13:40 +01002678
tiernoae4a8d12016-07-08 12:30:39 +02002679 #print "auxNetDict:"
2680 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002681
tiernoae4a8d12016-07-08 12:30:39 +02002682 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
2683 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2684 i = 0
2685 for sce_vnf in scenarioDict['vnfs']:
tierno5a3273c2017-08-29 11:43:46 +02002686 vnf_availability_zones = []
mirabal29356312017-07-27 12:21:22 +02002687 for vm in sce_vnf['vms']:
2688 vm_av = vm.get('availability_zone')
tierno5a3273c2017-08-29 11:43:46 +02002689 if vm_av and vm_av not in vnf_availability_zones:
2690 vnf_availability_zones.append(vm_av)
2691
2692 # check if there is enough availability zones available at vim level.
2693 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
2694 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002695 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors.Bad_Request)
tierno5a3273c2017-08-29 11:43:46 +02002696
tiernoae4a8d12016-07-08 12:30:39 +02002697 for vm in sce_vnf['vms']:
2698 i += 1
2699 myVMDict = {}
2700 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
tiernoae65a482016-11-24 16:20:05 +01002701 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
tiernoae4a8d12016-07-08 12:30:39 +02002702 #myVMDict['description'] = vm['description']
2703 myVMDict['description'] = myVMDict['name'][0:99]
2704 if not startvms:
2705 myVMDict['start'] = "no"
2706 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2707 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
tierno42026a02017-02-10 15:13:40 +01002708
tiernoae4a8d12016-07-08 12:30:39 +02002709 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002710 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno42026a02017-02-10 15:13:40 +01002711 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02002712 vm['vim_image_id'] = image_id
tierno42026a02017-02-10 15:13:40 +01002713
tiernoae4a8d12016-07-08 12:30:39 +02002714 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002715 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tiernoae4a8d12016-07-08 12:30:39 +02002716 if flavor_dict['extended']!=None:
2717 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
tierno42026a02017-02-10 15:13:40 +01002718 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02002719 vm['vim_flavor_id'] = flavor_id
tierno42026a02017-02-10 15:13:40 +01002720
2721
tiernoae4a8d12016-07-08 12:30:39 +02002722 myVMDict['imageRef'] = vm['vim_image_id']
2723 myVMDict['flavorRef'] = vm['vim_flavor_id']
2724 myVMDict['networks'] = []
2725 for iface in vm['interfaces']:
2726 netDict = {}
2727 if iface['type']=="data":
2728 netDict['type'] = iface['model']
2729 elif "model" in iface and iface["model"]!=None:
2730 netDict['model']=iface['model']
2731 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2732 #discover type of interface looking at flavor
2733 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2734 for flavor_iface in numa.get('interfaces',[]):
2735 if flavor_iface.get('name') == iface['internal_name']:
2736 if flavor_iface['dedicated'] == 'yes':
2737 netDict['type']="PF" #passthrough
2738 elif flavor_iface['dedicated'] == 'no':
2739 netDict['type']="VF" #siov
2740 elif flavor_iface['dedicated'] == 'yes:sriov':
2741 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2742 netDict["mac_address"] = flavor_iface.get("mac_address")
2743 break;
2744 netDict["use"]=iface['type']
2745 if netDict["use"]=="data" and not netDict.get("type"):
2746 #print "netDict", netDict
2747 #print "iface", iface
2748 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'])
2749 if flavor_dict.get('extended')==None:
tiernof97fd272016-07-11 14:32:37 +02002750 raise NfvoException(e_text + "After database migration some information is not available. \
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002751 Try to delete and create the scenarios and VNFs again", httperrors.Conflict)
tiernoae4a8d12016-07-08 12:30:39 +02002752 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002753 raise NfvoException(e_text, httperrors.Internal_Server_Error)
tiernoae4a8d12016-07-08 12:30:39 +02002754 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2755 netDict["type"]="virtual"
2756 if "vpci" in iface and iface["vpci"] is not None:
2757 netDict['vpci'] = iface['vpci']
2758 if "mac" in iface and iface["mac"] is not None:
2759 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00002760 if "port-security" in iface and iface["port-security"] is not None:
2761 netDict['port_security'] = iface['port-security']
2762 if "floating-ip" in iface and iface["floating-ip"] is not None:
2763 netDict['floating_ip'] = iface['floating-ip']
tiernoae4a8d12016-07-08 12:30:39 +02002764 netDict['name'] = iface['internal_name']
2765 if iface['net_id'] is None:
2766 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02002767 #print iface
2768 #print vnf_iface
tiernoae4a8d12016-07-08 12:30:39 +02002769 if vnf_iface['interface_id']==iface['uuid']:
2770 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
2771 break
2772 else:
2773 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
2774 #skip bridge ifaces not connected to any net
2775 #if 'net_id' not in netDict or netDict['net_id']==None:
2776 # continue
2777 myVMDict['networks'].append(netDict)
2778 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2779 #print myVMDict['name']
2780 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2781 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2782 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
mirabal29356312017-07-27 12:21:22 +02002783
2784 if 'availability_zone' in myVMDict:
tierno5a3273c2017-08-29 11:43:46 +02002785 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
mirabal29356312017-07-27 12:21:22 +02002786 else:
tierno5a3273c2017-08-29 11:43:46 +02002787 av_index = None
mirabal29356312017-07-27 12:21:22 +02002788
tierno98e909c2017-10-14 13:27:03 +02002789 vm_id, _ = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None),
mirabal29356312017-07-27 12:21:22 +02002790 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'],
tierno5a3273c2017-08-29 11:43:46 +02002791 availability_zone_index=av_index,
2792 availability_zone_list=vnf_availability_zones)
tiernoae4a8d12016-07-08 12:30:39 +02002793 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2794 vm['vim_id'] = vm_id
2795 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2796 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2797 for net in myVMDict['networks']:
2798 if "vim_id" in net:
2799 for iface in vm['interfaces']:
2800 if net["name"]==iface["internal_name"]:
2801 iface["vim_id"]=net["vim_id"]
2802 break
tierno42026a02017-02-10 15:13:40 +01002803
tiernoae4a8d12016-07-08 12:30:39 +02002804 logger.debug("start scenario Deployment done")
2805 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2806 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
tiernof97fd272016-07-11 14:32:37 +02002807 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
2808 return mydb.get_instance_scenario(instance_id)
tierno42026a02017-02-10 15:13:40 +01002809
tiernof97fd272016-07-11 14:32:37 +02002810 except (db_base_Exception, vimconn.vimconnException) as e:
tiernoae4a8d12016-07-08 12:30:39 +02002811 _, message = rollback(mydb, vims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02002812 if isinstance(e, db_base_Exception):
2813 error_text = "Exception at database"
2814 else:
2815 error_text = "Exception at VIM"
2816 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2817 #logger.error("start_scenario %s", error_text)
2818 raise NfvoException(error_text, e.http_code)
tierno7edb6752016-03-21 17:37:52 +01002819
tierno36c0b172017-01-12 18:32:28 +01002820def unify_cloud_config(cloud_config_preserve, cloud_config):
tierno40e1bce2017-08-09 09:12:04 +02002821 """ join the cloud config information into cloud_config_preserve.
tierno36c0b172017-01-12 18:32:28 +01002822 In case of conflict cloud_config_preserve preserves
tierno40e1bce2017-08-09 09:12:04 +02002823 None is allowed
2824 """
tierno36c0b172017-01-12 18:32:28 +01002825 if not cloud_config_preserve and not cloud_config:
2826 return None
2827
2828 new_cloud_config = {"key-pairs":[], "users":[]}
2829 # key-pairs
2830 if cloud_config_preserve:
2831 for key in cloud_config_preserve.get("key-pairs", () ):
2832 if key not in new_cloud_config["key-pairs"]:
2833 new_cloud_config["key-pairs"].append(key)
2834 if cloud_config:
2835 for key in cloud_config.get("key-pairs", () ):
2836 if key not in new_cloud_config["key-pairs"]:
2837 new_cloud_config["key-pairs"].append(key)
2838 if not new_cloud_config["key-pairs"]:
2839 del new_cloud_config["key-pairs"]
2840
2841 # users
2842 if cloud_config:
2843 new_cloud_config["users"] += cloud_config.get("users", () )
2844 if cloud_config_preserve:
2845 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
tiernoa4e1a6e2016-08-31 14:19:40 +02002846 index_to_delete = []
tierno36c0b172017-01-12 18:32:28 +01002847 users = new_cloud_config.get("users", [])
tiernoa4e1a6e2016-08-31 14:19:40 +02002848 for index0 in range(0,len(users)):
2849 if index0 in index_to_delete:
2850 continue
2851 for index1 in range(index0+1,len(users)):
2852 if index1 in index_to_delete:
2853 continue
2854 if users[index0]["name"] == users[index1]["name"]:
2855 index_to_delete.append(index1)
2856 for key in users[index1].get("key-pairs",()):
tierno36c0b172017-01-12 18:32:28 +01002857 if "key-pairs" not in users[index0]:
tiernoa4e1a6e2016-08-31 14:19:40 +02002858 users[index0]["key-pairs"] = [key]
2859 elif key not in users[index0]["key-pairs"]:
2860 users[index0]["key-pairs"].append(key)
2861 index_to_delete.sort(reverse=True)
2862 for index in index_to_delete:
2863 del users[index]
tierno36c0b172017-01-12 18:32:28 +01002864 if not new_cloud_config["users"]:
2865 del new_cloud_config["users"]
2866
2867 #boot-data-drive
2868 if cloud_config and cloud_config.get("boot-data-drive") != None:
2869 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
2870 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
2871 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
2872
2873 # user-data
tierno40e1bce2017-08-09 09:12:04 +02002874 new_cloud_config["user-data"] = []
2875 if cloud_config and cloud_config.get("user-data"):
2876 if isinstance(cloud_config["user-data"], list):
2877 new_cloud_config["user-data"] += cloud_config["user-data"]
2878 else:
2879 new_cloud_config["user-data"].append(cloud_config["user-data"])
2880 if cloud_config_preserve and cloud_config_preserve.get("user-data"):
2881 if isinstance(cloud_config_preserve["user-data"], list):
2882 new_cloud_config["user-data"] += cloud_config_preserve["user-data"]
2883 else:
2884 new_cloud_config["user-data"].append(cloud_config_preserve["user-data"])
2885 if not new_cloud_config["user-data"]:
2886 del new_cloud_config["user-data"]
tierno36c0b172017-01-12 18:32:28 +01002887
2888 # config files
2889 new_cloud_config["config-files"] = []
2890 if cloud_config and cloud_config.get("config-files") != None:
2891 new_cloud_config["config-files"] += cloud_config["config-files"]
2892 if cloud_config_preserve:
2893 for file in cloud_config_preserve.get("config-files", ()):
2894 for index in range(0, len(new_cloud_config["config-files"])):
2895 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
2896 new_cloud_config["config-files"][index] = file
2897 break
2898 else:
2899 new_cloud_config["config-files"].append(file)
2900 if not new_cloud_config["config-files"]:
2901 del new_cloud_config["config-files"]
2902 return new_cloud_config
2903
2904
tierno867ffe92017-03-27 12:50:34 +02002905def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
tiernob3d36742017-03-03 23:51:05 +01002906 datacenter_id = None
2907 datacenter_name = None
2908 thread = None
tierno867ffe92017-03-27 12:50:34 +02002909 try:
2910 if datacenter_tenant_id:
2911 thread_id = datacenter_tenant_id
2912 thread = vim_threads["running"].get(datacenter_tenant_id)
tiernob3d36742017-03-03 23:51:05 +01002913 else:
tierno867ffe92017-03-27 12:50:34 +02002914 where_={"td.nfvo_tenant_id": tenant_id}
2915 if datacenter_id_name:
2916 if utils.check_valid_uuid(datacenter_id_name):
2917 datacenter_id = datacenter_id_name
2918 where_["dt.datacenter_id"] = datacenter_id
2919 else:
2920 datacenter_name = datacenter_id_name
2921 where_["d.name"] = datacenter_name
2922 if datacenter_tenant_id:
2923 where_["dt.uuid"] = datacenter_tenant_id
2924 datacenters = mydb.get_rows(
2925 SELECT=("dt.uuid as datacenter_tenant_id",),
2926 FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2927 "join datacenters as d on d.uuid=dt.datacenter_id",
2928 WHERE=where_)
2929 if len(datacenters) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002930 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
tierno867ffe92017-03-27 12:50:34 +02002931 elif datacenters:
2932 thread_id = datacenters[0]["datacenter_tenant_id"]
2933 thread = vim_threads["running"].get(thread_id)
2934 if not thread:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002935 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found)
tierno867ffe92017-03-27 12:50:34 +02002936 return thread_id, thread
2937 except db_base_Exception as e:
2938 raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code)
tiernoa4e1a6e2016-08-31 14:19:40 +02002939
tiernof5755962017-07-13 15:44:34 +02002940
tiernoa15c4b92017-10-05 12:41:44 +02002941def get_datacenter_uuid(mydb, tenant_id, datacenter_id_name):
2942 WHERE_dict={}
2943 if utils.check_valid_uuid(datacenter_id_name):
2944 WHERE_dict['d.uuid'] = datacenter_id_name
2945 else:
2946 WHERE_dict['d.name'] = datacenter_id_name
2947
2948 if tenant_id:
2949 WHERE_dict['nfvo_tenant_id'] = tenant_id
2950 from_= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
2951 " dt on td.datacenter_tenant_id=dt.uuid"
2952 else:
2953 from_ = 'datacenters as d'
tiernod3750b32018-07-20 15:33:08 +02002954 vimaccounts = mydb.get_rows(FROM=from_, SELECT=("d.uuid as uuid, d.name as name",), WHERE=WHERE_dict )
tiernoa15c4b92017-10-05 12:41:44 +02002955 if len(vimaccounts) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002956 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found)
tiernoa15c4b92017-10-05 12:41:44 +02002957 elif len(vimaccounts)>1:
2958 #print "nfvo.datacenter_action() error. Several datacenters found"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002959 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
tiernod3750b32018-07-20 15:33:08 +02002960 return vimaccounts[0]["uuid"], vimaccounts[0]["name"]
tiernoa15c4b92017-10-05 12:41:44 +02002961
2962
tiernoa2793912016-10-04 08:15:08 +00002963def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
tiernobe41e222016-09-02 15:16:13 +02002964 datacenter_id = None
2965 datacenter_name = None
2966 if datacenter_id_name:
tierno42026a02017-02-10 15:13:40 +01002967 if utils.check_valid_uuid(datacenter_id_name):
tiernobe41e222016-09-02 15:16:13 +02002968 datacenter_id = datacenter_id_name
2969 else:
2970 datacenter_name = datacenter_id_name
tiernoa2793912016-10-04 08:15:08 +00002971 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
tiernobe41e222016-09-02 15:16:13 +02002972 if len(vims) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002973 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found)
tiernobe41e222016-09-02 15:16:13 +02002974 elif len(vims)>1:
2975 #print "nfvo.datacenter_action() error. Several datacenters found"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002976 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
tiernobe41e222016-09-02 15:16:13 +02002977 return vims.keys()[0], vims.values()[0]
2978
tiernob3d36742017-03-03 23:51:05 +01002979
garciadeblas9f8456e2016-09-05 05:02:59 +02002980def update(d, u):
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002981 """Takes dict d and updates it with the values in dict u.
2982 It merges all depth levels"""
garciadeblas9f8456e2016-09-05 05:02:59 +02002983 for k, v in u.iteritems():
2984 if isinstance(v, collections.Mapping):
2985 r = update(d.get(k, {}), v)
2986 d[k] = r
2987 else:
2988 d[k] = u[k]
2989 return d
2990
tierno16e3dd42018-04-24 12:52:40 +02002991
tierno7edb6752016-03-21 17:37:52 +01002992def create_instance(mydb, tenant_id, instance_dict):
tiernob3d36742017-03-03 23:51:05 +01002993 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2994 # logger.debug("Creating instance...")
tierno7edb6752016-03-21 17:37:52 +01002995 scenario = instance_dict["scenario"]
tierno42026a02017-02-10 15:13:40 +01002996
tierno868220c2017-09-26 00:11:05 +02002997 # find main datacenter
tiernobe41e222016-09-02 15:16:13 +02002998 myvims = {}
tierno867ffe92017-03-27 12:50:34 +02002999 myvim_threads_id = {}
tierno7edb6752016-03-21 17:37:52 +01003000 datacenter = instance_dict.get("datacenter")
tiernofc7cfbf2019-03-20 17:23:45 +00003001 default_wim_account = instance_dict.get("wim_account")
tiernobe41e222016-09-02 15:16:13 +02003002 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3003 myvims[default_datacenter_id] = vim
tierno867ffe92017-03-27 12:50:34 +02003004 myvim_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id)
gcalvinoe580c7d2017-09-22 14:09:51 +02003005 tenant = mydb.get_rows_by_id('nfvo_tenants', tenant_id)
tierno868220c2017-09-26 00:11:05 +02003006 # myvim_tenant = myvim['tenant_id']
tierno16e3dd42018-04-24 12:52:40 +02003007 rollbackList = []
tierno42026a02017-02-10 15:13:40 +01003008
tierno868220c2017-09-26 00:11:05 +02003009 # print "Checking that the scenario exists and getting the scenario dictionary"
tierno7fe82642018-11-26 14:14:51 +00003010 if isinstance(scenario, str):
3011 scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_vim_id=myvim_threads_id[default_datacenter_id],
3012 datacenter_id=default_datacenter_id)
3013 else:
3014 scenarioDict = scenario
3015 scenarioDict["uuid"] = None
tierno42026a02017-02-10 15:13:40 +01003016
tierno868220c2017-09-26 00:11:05 +02003017 # logger.debug(">>>>>> Dictionaries before merging")
3018 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
3019 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
tierno42026a02017-02-10 15:13:40 +01003020
tierno868220c2017-09-26 00:11:05 +02003021 db_instance_vnfs = []
3022 db_instance_vms = []
3023 db_instance_interfaces = []
Igor D.Ccaadc442017-11-06 12:48:48 +00003024 db_instance_sfis = []
3025 db_instance_sfs = []
3026 db_instance_classifications = []
3027 db_instance_sfps = []
tierno868220c2017-09-26 00:11:05 +02003028 db_ip_profiles = []
3029 db_vim_actions = []
tierno8e690322017-08-10 15:58:50 +02003030 uuid_list = []
tierno868220c2017-09-26 00:11:05 +02003031 task_index = 0
tierno8e690322017-08-10 15:58:50 +02003032 instance_name = instance_dict["name"]
3033 instance_uuid = str(uuid4())
3034 uuid_list.append(instance_uuid)
3035 db_instance_scenario = {
3036 "uuid": instance_uuid,
3037 "name": instance_name,
3038 "tenant_id": tenant_id,
3039 "scenario_id": scenarioDict['uuid'],
3040 "datacenter_id": default_datacenter_id,
3041 # filled bellow 'datacenter_tenant_id'
3042 "description": instance_dict.get("description"),
3043 }
tierno8e690322017-08-10 15:58:50 +02003044 if scenarioDict.get("cloud-config"):
3045 db_instance_scenario["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"],
3046 default_flow_style=True, width=256)
tierno868220c2017-09-26 00:11:05 +02003047 instance_action_id = get_task_id()
3048 db_instance_action = {
3049 "uuid": instance_action_id, # same uuid for the instance and the action on create
3050 "tenant_id": tenant_id,
3051 "instance_id": instance_uuid,
3052 "description": "CREATE",
3053 }
garciadeblas9f8456e2016-09-05 05:02:59 +02003054
tierno868220c2017-09-26 00:11:05 +02003055 # Auxiliary dictionaries from x to y
tierno8e690322017-08-10 15:58:50 +02003056 sce_net2instance = {}
tierno868220c2017-09-26 00:11:05 +02003057 net2task_id = {'scenario': {}}
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003058 # Mapping between local networks and WIMs
3059 wim_usage = {}
tierno42026a02017-02-10 15:13:40 +01003060
tierno1df468d2018-07-06 14:25:16 +02003061 def ip_profile_IM2RO(ip_profile_im):
3062 # translate from input format to database format
3063 ip_profile_ro = {}
3064 if 'subnet-address' in ip_profile_im:
3065 ip_profile_ro['subnet_address'] = ip_profile_im['subnet-address']
3066 if 'ip-version' in ip_profile_im:
3067 ip_profile_ro['ip_version'] = ip_profile_im['ip-version']
3068 if 'gateway-address' in ip_profile_im:
3069 ip_profile_ro['gateway_address'] = ip_profile_im['gateway-address']
3070 if 'dns-address' in ip_profile_im:
3071 ip_profile_ro['dns_address'] = ip_profile_im['dns-address']
3072 if isinstance(ip_profile_ro['dns_address'], (list, tuple)):
3073 ip_profile_ro['dns_address'] = ";".join(ip_profile_ro['dns_address'])
3074 if 'dhcp' in ip_profile_im:
3075 ip_profile_ro['dhcp_start_address'] = ip_profile_im['dhcp'].get('start-address')
3076 ip_profile_ro['dhcp_enabled'] = ip_profile_im['dhcp'].get('enabled', True)
3077 ip_profile_ro['dhcp_count'] = ip_profile_im['dhcp'].get('count')
3078 return ip_profile_ro
3079
tierno868220c2017-09-26 00:11:05 +02003080 # logger.debug("Creating instance from scenario-dict:\n%s",
3081 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01003082 try:
tiernob3d36742017-03-03 23:51:05 +01003083 # 0 check correct parameters
tierno868220c2017-09-26 00:11:05 +02003084 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01003085 for scenario_net in scenarioDict['nets']:
tierno1df468d2018-07-06 14:25:16 +02003086 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 +01003087 break
tierno1df468d2018-07-06 14:25:16 +02003088 else:
3089 raise NfvoException("Invalid scenario network name or id '{}' at instance:networks".format(net_name),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003090 httperrors.Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02003091 if "sites" not in net_instance_desc:
3092 net_instance_desc["sites"] = [ {} ]
3093 site_without_datacenter_field = False
3094 for site in net_instance_desc["sites"]:
3095 if site.get("datacenter"):
tiernod3750b32018-07-20 15:33:08 +02003096 site["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, site["datacenter"])
tiernobe41e222016-09-02 15:16:13 +02003097 if site["datacenter"] not in myvims:
tierno868220c2017-09-26 00:11:05 +02003098 # Add this datacenter to myvims
tiernobe41e222016-09-02 15:16:13 +02003099 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
3100 myvims[d] = v
tierno868220c2017-09-26 00:11:05 +02003101 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, site["datacenter"])
3102 site["datacenter"] = d # change name to id
tiernobe41e222016-09-02 15:16:13 +02003103 else:
3104 if site_without_datacenter_field:
tierno868220c2017-09-26 00:11:05 +02003105 raise NfvoException("Found more than one entries without datacenter field at "
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003106 "instance:networks:{}:sites".format(net_name), httperrors.Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02003107 site_without_datacenter_field = True
tierno868220c2017-09-26 00:11:05 +02003108 site["datacenter"] = default_datacenter_id # change name to id
tierno42026a02017-02-10 15:13:40 +01003109
tiernobe41e222016-09-02 15:16:13 +02003110 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01003111 for scenario_vnf in scenarioDict['vnfs']:
tierno1df468d2018-07-06 14:25:16 +02003112 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 +01003113 break
tierno1df468d2018-07-06 14:25:16 +02003114 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003115 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name), httperrors.Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02003116 if "datacenter" in vnf_instance_desc:
tierno868220c2017-09-26 00:11:05 +02003117 # Add this datacenter to myvims
tiernod3750b32018-07-20 15:33:08 +02003118 vnf_instance_desc["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
tiernobe41e222016-09-02 15:16:13 +02003119 if vnf_instance_desc["datacenter"] not in myvims:
3120 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
3121 myvims[d] = v
tierno868220c2017-09-26 00:11:05 +02003122 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"])
tiernoa2793912016-10-04 08:15:08 +00003123 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
garciadeblas30833382017-01-09 09:46:31 +01003124
tierno1df468d2018-07-06 14:25:16 +02003125 for net_id, net_instance_desc in vnf_instance_desc.get("networks", {}).iteritems():
3126 for scenario_net in scenario_vnf['nets']:
3127 if net_id == scenario_net['osm_id'] or net_id == scenario_net['uuid'] or net_id == scenario_net["name"]:
3128 break
3129 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003130 raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id), httperrors.Bad_Request)
tierno1df468d2018-07-06 14:25:16 +02003131 if net_instance_desc.get("vim-network-name"):
3132 scenario_net["vim-network-name"] = net_instance_desc["vim-network-name"]
gcalvino0a480542018-12-17 16:19:33 +01003133 if net_instance_desc.get("vim-network-id"):
3134 scenario_net["vim-network-id"] = net_instance_desc["vim-network-id"]
tierno1df468d2018-07-06 14:25:16 +02003135 if net_instance_desc.get("name"):
3136 scenario_net["name"] = net_instance_desc["name"]
3137 if 'ip-profile' in net_instance_desc:
3138 ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile'])
3139 if 'ip_profile' not in scenario_net:
3140 scenario_net['ip_profile'] = ipprofile_db
3141 else:
3142 update(scenario_net['ip_profile'], ipprofile_db)
3143
3144 for vdu_id, vdu_instance_desc in vnf_instance_desc.get("vdus", {}).iteritems():
3145 for scenario_vm in scenario_vnf['vms']:
3146 if vdu_id == scenario_vm['osm_id'] or vdu_id == scenario_vm["name"]:
3147 break
3148 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003149 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), httperrors.Bad_Request)
tierno1df468d2018-07-06 14:25:16 +02003150 scenario_vm["instance_parameters"] = vdu_instance_desc
3151 for iface_id, iface_instance_desc in vdu_instance_desc.get("interfaces", {}).iteritems():
3152 for scenario_interface in scenario_vm['interfaces']:
3153 if iface_id == scenario_interface['internal_name'] or iface_id == scenario_interface["external_name"]:
3154 scenario_interface.update(iface_instance_desc)
3155 break
3156 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003157 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), httperrors.Bad_Request)
tierno1df468d2018-07-06 14:25:16 +02003158
tierno868220c2017-09-26 00:11:05 +02003159 # 0.1 parse cloud-config parameters
tierno36c0b172017-01-12 18:32:28 +01003160 cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config"))
garciadeblas9f8456e2016-09-05 05:02:59 +02003161
tierno868220c2017-09-26 00:11:05 +02003162 # 0.2 merge instance information into scenario
3163 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
3164 # However, this is not possible yet.
tierno41a69812018-02-16 14:34:33 +01003165 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
garciadeblas9f8456e2016-09-05 05:02:59 +02003166 for scenario_net in scenarioDict['nets']:
tiernofc7cfbf2019-03-20 17:23:45 +00003167 if net_name == scenario_net.get("name") or net_name == scenario_net.get("osm_id") or net_name == scenario_net.get("uuid"):
3168 if "wim_account" in net_instance_desc and net_instance_desc["wim_account"] is not None:
3169 scenario_net["wim_account"] = net_instance_desc["wim_account"]
garciadeblas9f8456e2016-09-05 05:02:59 +02003170 if 'ip-profile' in net_instance_desc:
tierno1df468d2018-07-06 14:25:16 +02003171 ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile'])
garciadeblasedca7b32016-09-29 14:01:52 +00003172 if 'ip_profile' not in scenario_net:
tierno455612d2017-05-30 16:40:10 +02003173 scenario_net['ip_profile'] = ipprofile_db
garciadeblasedca7b32016-09-29 14:01:52 +00003174 else:
tierno455612d2017-05-30 16:40:10 +02003175 update(scenario_net['ip_profile'], ipprofile_db)
tierno41a69812018-02-16 14:34:33 +01003176 for interface in net_instance_desc.get('interfaces', ()):
garciadeblas9f8456e2016-09-05 05:02:59 +02003177 if 'ip_address' in interface:
3178 for vnf in scenarioDict['vnfs']:
3179 if interface['vnf'] == vnf['name']:
3180 for vnf_interface in vnf['interfaces']:
3181 if interface['vnf_interface'] == vnf_interface['external_name']:
tierno41a69812018-02-16 14:34:33 +01003182 vnf_interface['ip_address'] = interface['ip_address']
garciadeblas9f8456e2016-09-05 05:02:59 +02003183
tierno868220c2017-09-26 00:11:05 +02003184 # logger.debug(">>>>>>>> Merged dictionary")
3185 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
3186 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
garciadeblas9f8456e2016-09-05 05:02:59 +02003187
tiernob3d36742017-03-03 23:51:05 +01003188 # 1. Creating new nets (sce_nets) in the VIM"
tierno8f79ea12018-05-03 17:37:40 +02003189 number_mgmt_networks = 0
tierno8e690322017-08-10 15:58:50 +02003190 db_instance_nets = []
tierno7edb6752016-03-21 17:37:52 +01003191 for sce_net in scenarioDict['nets']:
tierno7fe82642018-11-26 14:14:51 +00003192 sce_net_uuid = sce_net.get('uuid', sce_net["name"])
tierno1df468d2018-07-06 14:25:16 +02003193 # get involved datacenters where this network need to be created
3194 involved_datacenters = []
tierno7fe82642018-11-26 14:14:51 +00003195 for sce_vnf in scenarioDict.get("vnfs", ()):
tierno1df468d2018-07-06 14:25:16 +02003196 vnf_datacenter = sce_vnf.get("datacenter", default_datacenter_id)
3197 if vnf_datacenter in involved_datacenters:
3198 continue
3199 if sce_vnf.get("interfaces"):
3200 for sce_vnf_ifaces in sce_vnf["interfaces"]:
3201 if sce_vnf_ifaces.get("sce_net_id") == sce_net["uuid"]:
3202 involved_datacenters.append(vnf_datacenter)
3203 break
gcalvinod6fac4d2018-11-05 10:42:06 +01003204 if not involved_datacenters:
3205 involved_datacenters.append(default_datacenter_id)
tierno80391822019-03-21 22:12:14 +00003206 target_wim_account = sce_net.get("wim_account", default_wim_account)
tierno1df468d2018-07-06 14:25:16 +02003207
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003208 # --> WIM
3209 # TODO: use this information during network creation
tierno4070e442019-01-23 10:19:23 +00003210 wim_account_id = wim_account_name = None
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003211 if len(involved_datacenters) > 1 and 'uuid' in sce_net:
tiernofc7cfbf2019-03-20 17:23:45 +00003212 if target_wim_account is None or target_wim_account is True: # automatic selection of WIM
3213 # OBS: sce_net without uuid are used internally to VNFs
3214 # and the assumption is that VNFs will not be split among
3215 # different datacenters
3216 wim_account = wim_engine.find_suitable_wim_account(
3217 involved_datacenters, tenant_id)
3218 wim_account_id = wim_account['uuid']
3219 wim_account_name = wim_account['name']
3220 wim_usage[sce_net['uuid']] = wim_account_id
3221 elif isinstance(target_wim_account, str): # manual selection of WIM
3222 wim_account.persist.get_wim_account_by(target_wim_account, tenant_id)
3223 wim_account_id = wim_account['uuid']
3224 wim_account_name = wim_account['name']
3225 wim_usage[sce_net['uuid']] = wim_account_id
3226 else: # not WIM usage
3227 wim_usage[sce_net['uuid']] = False
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003228 # <-- WIM
3229
tierno1df468d2018-07-06 14:25:16 +02003230 descriptor_net = {}
tierno3c44e7b2019-03-04 17:32:01 +00003231 if instance_dict.get("networks"):
3232 if sce_net.get("uuid") in instance_dict["networks"]:
3233 descriptor_net = instance_dict["networks"][sce_net["uuid"]]
3234 descriptor_net_name = sce_net["uuid"]
3235 elif sce_net.get("osm_id") in instance_dict["networks"]:
3236 descriptor_net = instance_dict["networks"][sce_net["osm_id"]]
3237 descriptor_net_name = sce_net["osm_id"]
3238 elif sce_net["name"] in instance_dict["networks"]:
3239 descriptor_net = instance_dict["networks"][sce_net["name"]]
3240 descriptor_net_name = sce_net["name"]
tiernobe41e222016-09-02 15:16:13 +02003241 net_name = descriptor_net.get("vim-network-name")
tierno7fe82642018-11-26 14:14:51 +00003242 # add datacenters from instantiation parameters
3243 if descriptor_net.get("sites"):
3244 for site in descriptor_net["sites"]:
3245 if site.get("datacenter") and site["datacenter"] not in involved_datacenters:
3246 involved_datacenters.append(site["datacenter"])
3247 sce_net2instance[sce_net_uuid] = {}
3248 net2task_id['scenario'][sce_net_uuid] = {}
tiernobe41e222016-09-02 15:16:13 +02003249
tierno3c44e7b2019-03-04 17:32:01 +00003250 use_network = None
3251 related_network = None
3252 if descriptor_net.get("use-network"):
3253 target_instance_nets = mydb.get_rows(
3254 SELECT="related",
3255 FROM="instance_nets",
3256 WHERE={"instance_scenario_id": descriptor_net["use-network"]["instance_scenario_id"],
3257 "osm_id": descriptor_net["use-network"]["osm_id"]},
3258 )
3259 if not target_instance_nets:
3260 raise NfvoException(
3261 "Cannot find the target network at instance:networks[{}]:use-network".format(descriptor_net_name),
3262 httperrors.Bad_Request)
3263 else:
3264 use_network = target_instance_nets[0]["related"]
3265
tierno1df468d2018-07-06 14:25:16 +02003266 if sce_net["external"]:
3267 number_mgmt_networks += 1
3268
3269 for datacenter_id in involved_datacenters:
3270 netmap_use = None
3271 netmap_create = None
3272 if descriptor_net.get("sites"):
3273 for site in descriptor_net["sites"]:
3274 if site.get("datacenter") == datacenter_id:
3275 netmap_use = site.get("netmap-use")
3276 netmap_create = site.get("netmap-create")
3277 break
3278
3279 vim = myvims[datacenter_id]
3280 myvim_thread_id = myvim_threads_id[datacenter_id]
3281
tiernobe41e222016-09-02 15:16:13 +02003282 net_type = sce_net['type']
tiernob6990792018-11-13 10:37:42 +01003283 net_vim_name = None
tierno868220c2017-09-26 00:11:05 +02003284 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
tierno42026a02017-02-10 15:13:40 +01003285
tiernof1ba57e2017-09-07 12:23:19 +02003286 if not net_name:
3287 if sce_net["external"]:
3288 net_name = sce_net["name"]
3289 else:
tierno1df468d2018-07-06 14:25:16 +02003290 net_name = "{}-{}".format(instance_name, sce_net["name"])
tiernof1ba57e2017-09-07 12:23:19 +02003291 net_name = net_name[:255] # limit length
3292
tierno1df468d2018-07-06 14:25:16 +02003293 if netmap_use or netmap_create:
tiernof1ba57e2017-09-07 12:23:19 +02003294 create_network = False
3295 lookfor_network = False
tierno1df468d2018-07-06 14:25:16 +02003296 if netmap_use:
tiernof1ba57e2017-09-07 12:23:19 +02003297 lookfor_network = True
tierno1df468d2018-07-06 14:25:16 +02003298 if utils.check_valid_uuid(netmap_use):
3299 lookfor_filter["id"] = netmap_use
tiernof1ba57e2017-09-07 12:23:19 +02003300 else:
tierno1df468d2018-07-06 14:25:16 +02003301 lookfor_filter["name"] = netmap_use
3302 if netmap_create:
tiernof1ba57e2017-09-07 12:23:19 +02003303 create_network = True
3304 net_vim_name = net_name
tierno1df468d2018-07-06 14:25:16 +02003305 if isinstance(netmap_create, str):
3306 net_vim_name = netmap_create
tierno8f79ea12018-05-03 17:37:40 +02003307 elif sce_net.get("vim_network_name"):
3308 create_network = False
3309 lookfor_network = True
3310 lookfor_filter["name"] = sce_net.get("vim_network_name")
tiernof1ba57e2017-09-07 12:23:19 +02003311 elif sce_net["external"]:
tiernod108c412018-12-18 15:19:27 +00003312 if sce_net.get('vim_id'):
tierno868220c2017-09-26 00:11:05 +02003313 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
tiernobe41e222016-09-02 15:16:13 +02003314 create_network = False
3315 lookfor_network = True
3316 lookfor_filter["id"] = sce_net['vim_id']
tierno8f79ea12018-05-03 17:37:40 +02003317 elif vim["config"].get("management_network_id") or vim["config"].get("management_network_name"):
3318 if number_mgmt_networks > 1:
3319 raise NfvoException("Found several VLD of type mgmt. "
3320 "You must concrete what vim-network must be use for each one",
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003321 httperrors.Bad_Request)
tierno8f79ea12018-05-03 17:37:40 +02003322 create_network = False
3323 lookfor_network = True
3324 if vim["config"].get("management_network_id"):
3325 lookfor_filter["id"] = vim["config"]["management_network_id"]
3326 else:
3327 lookfor_filter["name"] = vim["config"]["management_network_name"]
tiernobe41e222016-09-02 15:16:13 +02003328 else:
tierno868220c2017-09-26 00:11:05 +02003329 # 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 +02003330 create_network = True
3331 lookfor_network = True
3332 lookfor_filter["name"] = sce_net["name"]
3333 net_vim_name = sce_net["name"]
tierno7edb6752016-03-21 17:37:52 +01003334 else:
tiernobe41e222016-09-02 15:16:13 +02003335 net_vim_name = net_name
3336 create_network = True
3337 lookfor_network = False
tierno42026a02017-02-10 15:13:40 +01003338
tiernof1450872017-10-17 23:15:08 +02003339 task_extra = {}
3340 if create_network:
3341 task_action = "CREATE"
tierno4070e442019-01-23 10:19:23 +00003342 task_extra["params"] = (net_vim_name, net_type, sce_net.get('ip_profile', None), wim_account_name)
tiernof1450872017-10-17 23:15:08 +02003343 if lookfor_network:
3344 task_extra["find"] = (lookfor_filter,)
tierno868220c2017-09-26 00:11:05 +02003345 elif lookfor_network:
3346 task_action = "FIND"
tiernof1450872017-10-17 23:15:08 +02003347 task_extra["params"] = (lookfor_filter,)
tierno42026a02017-02-10 15:13:40 +01003348
tierno8e690322017-08-10 15:58:50 +02003349 # fill database content
3350 net_uuid = str(uuid4())
3351 uuid_list.append(net_uuid)
tierno7fe82642018-11-26 14:14:51 +00003352 sce_net2instance[sce_net_uuid][datacenter_id] = net_uuid
tierno3c44e7b2019-03-04 17:32:01 +00003353 if not related_network: # all db_instance_nets will have same related
3354 related_network = use_network or net_uuid
tierno8e690322017-08-10 15:58:50 +02003355 db_net = {
3356 "uuid": net_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003357 "osm_id": sce_net.get("osm_id") or sce_net["name"],
3358 "related": related_network,
tierno868220c2017-09-26 00:11:05 +02003359 'vim_net_id': None,
tiernob6990792018-11-13 10:37:42 +01003360 "vim_name": net_vim_name,
tierno8e690322017-08-10 15:58:50 +02003361 "instance_scenario_id": instance_uuid,
tierno7fe82642018-11-26 14:14:51 +00003362 "sce_net_id": sce_net.get("uuid"),
tierno8e690322017-08-10 15:58:50 +02003363 "created": create_network,
3364 'datacenter_id': datacenter_id,
3365 'datacenter_tenant_id': myvim_thread_id,
tiernod2836fc2018-05-30 15:03:27 +02003366 'status': 'BUILD' # if create_network else "ACTIVE"
tierno8e690322017-08-10 15:58:50 +02003367 }
3368 db_instance_nets.append(db_net)
tierno868220c2017-09-26 00:11:05 +02003369 db_vim_action = {
3370 "instance_action_id": instance_action_id,
3371 "status": "SCHEDULED",
3372 "task_index": task_index,
3373 "datacenter_vim_id": myvim_thread_id,
3374 "action": task_action,
3375 "item": "instance_nets",
3376 "item_id": net_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003377 "related": related_network,
tiernof1450872017-10-17 23:15:08 +02003378 "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256)
tierno868220c2017-09-26 00:11:05 +02003379 }
tierno7fe82642018-11-26 14:14:51 +00003380 net2task_id['scenario'][sce_net_uuid][datacenter_id] = task_index
tierno868220c2017-09-26 00:11:05 +02003381 task_index += 1
3382 db_vim_actions.append(db_vim_action)
3383
tierno8e690322017-08-10 15:58:50 +02003384 if 'ip_profile' in sce_net:
3385 db_ip_profile={
3386 'instance_net_id': net_uuid,
3387 'ip_version': sce_net['ip_profile']['ip_version'],
3388 'subnet_address': sce_net['ip_profile']['subnet_address'],
3389 'gateway_address': sce_net['ip_profile']['gateway_address'],
3390 'dns_address': sce_net['ip_profile']['dns_address'],
3391 'dhcp_enabled': sce_net['ip_profile']['dhcp_enabled'],
3392 'dhcp_start_address': sce_net['ip_profile']['dhcp_start_address'],
3393 'dhcp_count': sce_net['ip_profile']['dhcp_count'],
3394 }
3395 db_ip_profiles.append(db_ip_profile)
3396
tierno16e3dd42018-04-24 12:52:40 +02003397 # Create VNFs
3398 vnf_params = {
3399 "default_datacenter_id": default_datacenter_id,
3400 "myvim_threads_id": myvim_threads_id,
3401 "instance_uuid": instance_uuid,
3402 "instance_name": instance_name,
3403 "instance_action_id": instance_action_id,
3404 "myvims": myvims,
3405 "cloud_config": cloud_config,
3406 "RO_pub_key": tenant[0].get('RO_pub_key'),
tierno67881db2018-10-24 18:46:03 +02003407 "instance_parameters": instance_dict,
tierno16e3dd42018-04-24 12:52:40 +02003408 }
3409 vnf_params_out = {
3410 "task_index": task_index,
3411 "uuid_list": uuid_list,
3412 "db_instance_nets": db_instance_nets,
3413 "db_vim_actions": db_vim_actions,
3414 "db_ip_profiles": db_ip_profiles,
3415 "db_instance_vnfs": db_instance_vnfs,
3416 "db_instance_vms": db_instance_vms,
3417 "db_instance_interfaces": db_instance_interfaces,
3418 "net2task_id": net2task_id,
3419 "sce_net2instance": sce_net2instance,
3420 }
tierno55d234c2018-07-04 18:29:21 +02003421 # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
tierno7fe82642018-11-26 14:14:51 +00003422 for sce_vnf in scenarioDict.get('vnfs', ()): # sce_vnf_list:
tierno16e3dd42018-04-24 12:52:40 +02003423 instantiate_vnf(mydb, sce_vnf, vnf_params, vnf_params_out, rollbackList)
3424 task_index = vnf_params_out["task_index"]
3425 uuid_list = vnf_params_out["uuid_list"]
mirabal29356312017-07-27 12:21:22 +02003426
tierno16e3dd42018-04-24 12:52:40 +02003427 # Create VNFFGs
3428 # task_depends_on = []
tierno7fe82642018-11-26 14:14:51 +00003429 for vnffg in scenarioDict.get('vnffgs', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00003430 for rsp in vnffg['rsps']:
3431 sfs_created = []
3432 for cp in rsp['connection_points']:
3433 count = mydb.get_rows(
Eduardo Sousa16cfd562018-11-30 15:33:35 +00003434 SELECT='vms.count',
3435 FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h "
3436 "on interfaces.uuid=h.ingress_interface_id",
Igor D.Ccaadc442017-11-06 12:48:48 +00003437 WHERE={'h.uuid': cp['uuid']})[0]['count']
3438 instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == cp['sce_vnf_id']), None)
3439 instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']]
3440 dependencies = []
3441 for instance_vm in instance_vms:
3442 action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None)
3443 if action:
3444 dependencies.append(action['task_index'])
3445 # TODO: throw exception if count != len(instance_vms)
3446 # TODO: and action shouldn't ever be None
3447 sfis_created = []
3448 for i in range(count):
3449 # create sfis
3450 sfi_uuid = str(uuid4())
Eduardo Sousa16cfd562018-11-30 15:33:35 +00003451 extra_params = {
3452 "ingress_interface_id": cp["ingress_interface_id"],
3453 "egress_interface_id": cp["egress_interface_id"]
3454 }
Igor D.Ccaadc442017-11-06 12:48:48 +00003455 uuid_list.append(sfi_uuid)
3456 db_sfi = {
3457 "uuid": sfi_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003458 "related": sfi_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003459 "instance_scenario_id": instance_uuid,
3460 'sce_rsp_hop_id': cp['uuid'],
3461 'datacenter_id': datacenter_id,
3462 'datacenter_tenant_id': myvim_thread_id,
3463 "vim_sfi_id": None, # vim thread will populate
3464 }
3465 db_instance_sfis.append(db_sfi)
3466 db_vim_action = {
3467 "instance_action_id": instance_action_id,
3468 "task_index": task_index,
3469 "datacenter_vim_id": myvim_thread_id,
3470 "action": "CREATE",
3471 "status": "SCHEDULED",
3472 "item": "instance_sfis",
3473 "item_id": sfi_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003474 "related": sfi_uuid,
Eduardo Sousa16cfd562018-11-30 15:33:35 +00003475 "extra": yaml.safe_dump({"params": extra_params, "depends_on": [dependencies[i]]},
Igor D.Ccaadc442017-11-06 12:48:48 +00003476 default_flow_style=True, width=256)
3477 }
3478 sfis_created.append(task_index)
3479 task_index += 1
3480 db_vim_actions.append(db_vim_action)
3481 # create sfs
3482 sf_uuid = str(uuid4())
3483 uuid_list.append(sf_uuid)
3484 db_sf = {
3485 "uuid": sf_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003486 "related": sf_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003487 "instance_scenario_id": instance_uuid,
3488 'sce_rsp_hop_id': cp['uuid'],
3489 'datacenter_id': datacenter_id,
3490 'datacenter_tenant_id': myvim_thread_id,
3491 "vim_sf_id": None, # vim thread will populate
3492 }
3493 db_instance_sfs.append(db_sf)
3494 db_vim_action = {
3495 "instance_action_id": instance_action_id,
3496 "task_index": task_index,
3497 "datacenter_vim_id": myvim_thread_id,
3498 "action": "CREATE",
3499 "status": "SCHEDULED",
3500 "item": "instance_sfs",
3501 "item_id": sf_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003502 "related": sf_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003503 "extra": yaml.safe_dump({"params": "", "depends_on": sfis_created},
3504 default_flow_style=True, width=256)
3505 }
3506 sfs_created.append(task_index)
3507 task_index += 1
3508 db_vim_actions.append(db_vim_action)
3509 classifier = rsp['classifier']
3510
3511 # TODO the following ~13 lines can be reused for the sfi case
3512 count = mydb.get_rows(
3513 SELECT=('vms.count'),
3514 FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id",
3515 WHERE={'c.uuid': classifier['uuid']})[0]['count']
3516 instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == classifier['sce_vnf_id']), None)
3517 instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']]
3518 dependencies = []
3519 for instance_vm in instance_vms:
3520 action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None)
3521 if action:
3522 dependencies.append(action['task_index'])
3523 # TODO: throw exception if count != len(instance_vms)
3524 # TODO: and action shouldn't ever be None
3525 classifications_created = []
3526 for i in range(count):
3527 for match in classifier['matches']:
3528 # create classifications
3529 classification_uuid = str(uuid4())
3530 uuid_list.append(classification_uuid)
3531 db_classification = {
3532 "uuid": classification_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003533 "related": classification_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003534 "instance_scenario_id": instance_uuid,
3535 'sce_classifier_match_id': match['uuid'],
3536 'datacenter_id': datacenter_id,
3537 'datacenter_tenant_id': myvim_thread_id,
3538 "vim_classification_id": None, # vim thread will populate
3539 }
3540 db_instance_classifications.append(db_classification)
3541 classification_params = {
3542 "ip_proto": match["ip_proto"],
3543 "source_ip": match["source_ip"],
3544 "destination_ip": match["destination_ip"],
3545 "source_port": match["source_port"],
3546 "destination_port": match["destination_port"]
3547 }
3548 db_vim_action = {
3549 "instance_action_id": instance_action_id,
3550 "task_index": task_index,
3551 "datacenter_vim_id": myvim_thread_id,
3552 "action": "CREATE",
3553 "status": "SCHEDULED",
3554 "item": "instance_classifications",
3555 "item_id": classification_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003556 "related": classification_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003557 "extra": yaml.safe_dump({"params": classification_params, "depends_on": [dependencies[i]]},
3558 default_flow_style=True, width=256)
3559 }
3560 classifications_created.append(task_index)
3561 task_index += 1
3562 db_vim_actions.append(db_vim_action)
3563
3564 # create sfps
3565 sfp_uuid = str(uuid4())
3566 uuid_list.append(sfp_uuid)
3567 db_sfp = {
3568 "uuid": sfp_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003569 "related": sfp_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003570 "instance_scenario_id": instance_uuid,
3571 'sce_rsp_id': rsp['uuid'],
3572 'datacenter_id': datacenter_id,
3573 'datacenter_tenant_id': myvim_thread_id,
3574 "vim_sfp_id": None, # vim thread will populate
3575 }
3576 db_instance_sfps.append(db_sfp)
3577 db_vim_action = {
3578 "instance_action_id": instance_action_id,
3579 "task_index": task_index,
3580 "datacenter_vim_id": myvim_thread_id,
3581 "action": "CREATE",
3582 "status": "SCHEDULED",
3583 "item": "instance_sfps",
3584 "item_id": sfp_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003585 "related": sfp_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003586 "extra": yaml.safe_dump({"params": "", "depends_on": sfs_created + classifications_created},
3587 default_flow_style=True, width=256)
3588 }
3589 task_index += 1
3590 db_vim_actions.append(db_vim_action)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003591 db_instance_action["number_tasks"] = task_index
3592
3593 # --> WIM
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003594 logger.debug('wim_usage:\n%s\n\n', pformat(wim_usage))
3595 wan_links = wim_engine.derive_wan_links(wim_usage, db_instance_nets, tenant_id)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003596 wim_actions = wim_engine.create_actions(wan_links)
3597 wim_actions, db_instance_action = (
3598 wim_engine.incorporate_actions(wim_actions, db_instance_action))
3599 # <-- WIM
Igor D.Ccaadc442017-11-06 12:48:48 +00003600
tierno867ffe92017-03-27 12:50:34 +02003601 scenarioDict["datacenter2tenant"] = myvim_threads_id
tierno8e690322017-08-10 15:58:50 +02003602
3603 db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id]
3604 db_instance_scenario['datacenter_id'] = default_datacenter_id
3605 db_tables=[
3606 {"instance_scenarios": db_instance_scenario},
3607 {"instance_vnfs": db_instance_vnfs},
3608 {"instance_nets": db_instance_nets},
3609 {"ip_profiles": db_ip_profiles},
3610 {"instance_vms": db_instance_vms},
3611 {"instance_interfaces": db_instance_interfaces},
tierno868220c2017-09-26 00:11:05 +02003612 {"instance_actions": db_instance_action},
Igor D.Ccaadc442017-11-06 12:48:48 +00003613 {"instance_sfis": db_instance_sfis},
3614 {"instance_sfs": db_instance_sfs},
3615 {"instance_classifications": db_instance_classifications},
3616 {"instance_sfps": db_instance_sfps},
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003617 {"instance_wim_nets": wan_links},
3618 {"vim_wim_actions": db_vim_actions + wim_actions}
tierno8e690322017-08-10 15:58:50 +02003619 ]
3620
tierno868220c2017-09-26 00:11:05 +02003621 logger.debug("create_instance done DB tables: %s",
tierno8e690322017-08-10 15:58:50 +02003622 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
3623 mydb.new_rows(db_tables, uuid_list)
tierno868220c2017-09-26 00:11:05 +02003624 for myvim_thread_id in myvim_threads_id.values():
3625 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
tierno867ffe92017-03-27 12:50:34 +02003626
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003627 wim_engine.dispatch(wim_actions)
3628
tierno868220c2017-09-26 00:11:05 +02003629 returned_instance = mydb.get_instance_scenario(instance_uuid)
3630 returned_instance["action_id"] = instance_action_id
3631 return returned_instance
tierno4491ba92019-03-25 15:00:02 +00003632 except (NfvoException, vimconn.vimconnException, wimconn.WimConnectorError, db_base_Exception) as e:
tiernobe41e222016-09-02 15:16:13 +02003633 message = rollback(mydb, myvims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02003634 if isinstance(e, db_base_Exception):
3635 error_text = "database Exception"
3636 elif isinstance(e, vimconn.vimconnException):
3637 error_text = "VIM Exception"
tierno4491ba92019-03-25 15:00:02 +00003638 elif isinstance(e, wimconn.WimConnectorError):
3639 error_text = "WIM Exception"
tiernof97fd272016-07-11 14:32:37 +02003640 else:
3641 error_text = "Exception"
3642 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
tierno868220c2017-09-26 00:11:05 +02003643 # logger.error("create_instance: %s", error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003644 logger.exception(e)
tiernof97fd272016-07-11 14:32:37 +02003645 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01003646
tiernob3d36742017-03-03 23:51:05 +01003647
tierno16e3dd42018-04-24 12:52:40 +02003648def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList):
3649 default_datacenter_id = params["default_datacenter_id"]
3650 myvim_threads_id = params["myvim_threads_id"]
3651 instance_uuid = params["instance_uuid"]
3652 instance_name = params["instance_name"]
3653 instance_action_id = params["instance_action_id"]
3654 myvims = params["myvims"]
3655 cloud_config = params["cloud_config"]
3656 RO_pub_key = params["RO_pub_key"]
3657
3658 task_index = params_out["task_index"]
3659 uuid_list = params_out["uuid_list"]
3660 db_instance_nets = params_out["db_instance_nets"]
3661 db_vim_actions = params_out["db_vim_actions"]
3662 db_ip_profiles = params_out["db_ip_profiles"]
3663 db_instance_vnfs = params_out["db_instance_vnfs"]
3664 db_instance_vms = params_out["db_instance_vms"]
3665 db_instance_interfaces = params_out["db_instance_interfaces"]
3666 net2task_id = params_out["net2task_id"]
3667 sce_net2instance = params_out["sce_net2instance"]
3668
3669 vnf_net2instance = {}
3670
3671 # 2. Creating new nets (vnf internal nets) in the VIM"
3672 # For each vnf net, we create it and we add it to instanceNetlist.
3673 if sce_vnf.get("datacenter"):
3674 datacenter_id = sce_vnf["datacenter"]
3675 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
3676 else:
3677 datacenter_id = default_datacenter_id
3678 myvim_thread_id = myvim_threads_id[default_datacenter_id]
3679 for net in sce_vnf['nets']:
3680 # TODO revis
3681 # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
3682 # net_name = descriptor_net.get("name")
3683 net_name = None
3684 if not net_name:
tierno1df468d2018-07-06 14:25:16 +02003685 net_name = "{}-{}".format(instance_name, net["name"])
tierno16e3dd42018-04-24 12:52:40 +02003686 net_name = net_name[:255] # limit length
3687 net_type = net['type']
3688
3689 if sce_vnf['uuid'] not in vnf_net2instance:
3690 vnf_net2instance[sce_vnf['uuid']] = {}
3691 if sce_vnf['uuid'] not in net2task_id:
3692 net2task_id[sce_vnf['uuid']] = {}
3693 net2task_id[sce_vnf['uuid']][net['uuid']] = task_index
3694
3695 # fill database content
3696 net_uuid = str(uuid4())
3697 uuid_list.append(net_uuid)
3698 vnf_net2instance[sce_vnf['uuid']][net['uuid']] = net_uuid
3699 db_net = {
3700 "uuid": net_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003701 "related": net_uuid,
tierno16e3dd42018-04-24 12:52:40 +02003702 'vim_net_id': None,
tiernob6990792018-11-13 10:37:42 +01003703 "vim_name": net_name,
tierno16e3dd42018-04-24 12:52:40 +02003704 "instance_scenario_id": instance_uuid,
3705 "net_id": net["uuid"],
3706 "created": True,
3707 'datacenter_id': datacenter_id,
3708 'datacenter_tenant_id': myvim_thread_id,
3709 }
3710 db_instance_nets.append(db_net)
3711
gcalvino0a480542018-12-17 16:19:33 +01003712 lookfor_filter = {}
tierno1df468d2018-07-06 14:25:16 +02003713 if net.get("vim-network-name"):
gcalvino0a480542018-12-17 16:19:33 +01003714 lookfor_filter["name"] = net["vim-network-name"]
3715 if net.get("vim-network-id"):
3716 lookfor_filter["id"] = net["vim-network-id"]
3717 if lookfor_filter:
tierno1df468d2018-07-06 14:25:16 +02003718 task_action = "FIND"
3719 task_extra = {"params": (lookfor_filter,)}
3720 else:
3721 task_action = "CREATE"
3722 task_extra = {"params": (net_name, net_type, net.get('ip_profile', None))}
3723
tierno16e3dd42018-04-24 12:52:40 +02003724 db_vim_action = {
3725 "instance_action_id": instance_action_id,
3726 "task_index": task_index,
3727 "datacenter_vim_id": myvim_thread_id,
3728 "status": "SCHEDULED",
tierno1df468d2018-07-06 14:25:16 +02003729 "action": task_action,
tierno16e3dd42018-04-24 12:52:40 +02003730 "item": "instance_nets",
3731 "item_id": net_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003732 "related": net_uuid,
tierno1df468d2018-07-06 14:25:16 +02003733 "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256)
tierno16e3dd42018-04-24 12:52:40 +02003734 }
3735 task_index += 1
3736 db_vim_actions.append(db_vim_action)
3737
3738 if 'ip_profile' in net:
3739 db_ip_profile = {
3740 'instance_net_id': net_uuid,
3741 'ip_version': net['ip_profile']['ip_version'],
3742 'subnet_address': net['ip_profile']['subnet_address'],
3743 'gateway_address': net['ip_profile']['gateway_address'],
3744 'dns_address': net['ip_profile']['dns_address'],
3745 'dhcp_enabled': net['ip_profile']['dhcp_enabled'],
3746 'dhcp_start_address': net['ip_profile']['dhcp_start_address'],
3747 'dhcp_count': net['ip_profile']['dhcp_count'],
3748 }
3749 db_ip_profiles.append(db_ip_profile)
3750
3751 # print "vnf_net2instance:"
3752 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
3753
3754 # 3. Creating new vm instances in the VIM
3755 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
3756 ssh_access = None
3757 if sce_vnf.get('mgmt_access'):
3758 ssh_access = sce_vnf['mgmt_access'].get('config-access', {}).get('ssh-access')
3759 vnf_availability_zones = []
gcalvinod6fac4d2018-11-05 10:42:06 +01003760 for vm in sce_vnf.get('vms'):
tierno16e3dd42018-04-24 12:52:40 +02003761 vm_av = vm.get('availability_zone')
3762 if vm_av and vm_av not in vnf_availability_zones:
3763 vnf_availability_zones.append(vm_av)
3764
3765 # check if there is enough availability zones available at vim level.
3766 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
3767 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003768 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors.Bad_Request)
tierno16e3dd42018-04-24 12:52:40 +02003769
3770 if sce_vnf.get("datacenter"):
3771 vim = myvims[sce_vnf["datacenter"]]
3772 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
3773 datacenter_id = sce_vnf["datacenter"]
3774 else:
3775 vim = myvims[default_datacenter_id]
3776 myvim_thread_id = myvim_threads_id[default_datacenter_id]
3777 datacenter_id = default_datacenter_id
3778 sce_vnf["datacenter_id"] = datacenter_id
3779 i = 0
3780
3781 vnf_uuid = str(uuid4())
3782 uuid_list.append(vnf_uuid)
3783 db_instance_vnf = {
3784 'uuid': vnf_uuid,
3785 'instance_scenario_id': instance_uuid,
3786 'vnf_id': sce_vnf['vnf_id'],
3787 'sce_vnf_id': sce_vnf['uuid'],
3788 'datacenter_id': datacenter_id,
3789 'datacenter_tenant_id': myvim_thread_id,
3790 }
3791 db_instance_vnfs.append(db_instance_vnf)
3792
3793 for vm in sce_vnf['vms']:
tiernob6990792018-11-13 10:37:42 +01003794 # skip PDUs
3795 if vm.get("pdu_type"):
3796 continue
3797
tierno16e3dd42018-04-24 12:52:40 +02003798 myVMDict = {}
tierno7f426e92018-06-28 15:21:32 +02003799 sce_vnf_name = sce_vnf['member_vnf_index'] if sce_vnf['member_vnf_index'] else sce_vnf['name']
3800 myVMDict['name'] = "{}-{}-{}".format(instance_name[:64], sce_vnf_name[:64], vm["name"][:64])
tierno16e3dd42018-04-24 12:52:40 +02003801 myVMDict['description'] = myVMDict['name'][0:99]
3802 # if not startvms:
3803 # myVMDict['start'] = "no"
tierno1df468d2018-07-06 14:25:16 +02003804 if vm.get("instance_parameters") and vm["instance_parameters"].get("name"):
3805 myVMDict['name'] = vm["instance_parameters"].get("name")
tierno16e3dd42018-04-24 12:52:40 +02003806 myVMDict['name'] = myVMDict['name'][0:255] # limit name length
3807 # create image at vim in case it not exist
3808 image_uuid = vm['image_id']
3809 if vm.get("image_list"):
3810 for alternative_image in vm["image_list"]:
tiernob6434212018-04-26 16:27:47 +02003811 if alternative_image["vim_type"] == vim["config"]["_vim_type_internal"]:
tierno16e3dd42018-04-24 12:52:40 +02003812 image_uuid = alternative_image['image_id']
3813 break
3814 image_dict = mydb.get_table_by_uuid_name("images", image_uuid)
3815 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
3816 vm['vim_image_id'] = image_id
3817
3818 # create flavor at vim in case it not exist
3819 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
3820 if flavor_dict['extended'] != None:
3821 flavor_dict['extended'] = yaml.load(flavor_dict['extended'])
3822 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
3823
3824 # Obtain information for additional disks
3825 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',),
3826 WHERE={'vim_id': flavor_id})
3827 if not extended_flavor_dict:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003828 raise NfvoException("flavor '{}' not found".format(flavor_id), httperrors.Not_Found)
tierno16e3dd42018-04-24 12:52:40 +02003829
3830 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
3831 myVMDict['disks'] = None
3832 extended_info = extended_flavor_dict[0]['extended']
3833 if extended_info != None:
3834 extended_flavor_dict_yaml = yaml.load(extended_info)
3835 if 'disks' in extended_flavor_dict_yaml:
3836 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
tierno1df468d2018-07-06 14:25:16 +02003837 if vm.get("instance_parameters") and vm["instance_parameters"].get("devices"):
3838 for disk in myVMDict['disks']:
3839 if disk.get("name") in vm["instance_parameters"]["devices"]:
3840 disk.update(vm["instance_parameters"]["devices"][disk.get("name")])
tierno16e3dd42018-04-24 12:52:40 +02003841
3842 vm['vim_flavor_id'] = flavor_id
3843 myVMDict['imageRef'] = vm['vim_image_id']
3844 myVMDict['flavorRef'] = vm['vim_flavor_id']
3845 myVMDict['availability_zone'] = vm.get('availability_zone')
3846 myVMDict['networks'] = []
3847 task_depends_on = []
3848 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
tierno67881db2018-10-24 18:46:03 +02003849 is_management_vm = False
tierno16e3dd42018-04-24 12:52:40 +02003850 db_vm_ifaces = []
3851 for iface in vm['interfaces']:
3852 netDict = {}
3853 if iface['type'] == "data":
3854 netDict['type'] = iface['model']
3855 elif "model" in iface and iface["model"] != None:
3856 netDict['model'] = iface['model']
3857 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
3858 # is obtained from iterface table model
3859 # discover type of interface looking at flavor
3860 for numa in flavor_dict.get('extended', {}).get('numas', []):
3861 for flavor_iface in numa.get('interfaces', []):
3862 if flavor_iface.get('name') == iface['internal_name']:
3863 if flavor_iface['dedicated'] == 'yes':
3864 netDict['type'] = "PF" # passthrough
3865 elif flavor_iface['dedicated'] == 'no':
3866 netDict['type'] = "VF" # siov
3867 elif flavor_iface['dedicated'] == 'yes:sriov':
3868 netDict['type'] = "VFnotShared" # sriov but only one sriov on the PF
3869 netDict["mac_address"] = flavor_iface.get("mac_address")
3870 break
3871 netDict["use"] = iface['type']
3872 if netDict["use"] == "data" and not netDict.get("type"):
3873 # print "netDict", netDict
3874 # print "iface", iface
3875 e_text = "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat(
3876 sce_vnf['name'], vm['name'], iface['internal_name'])
3877 if flavor_dict.get('extended') == None:
3878 raise NfvoException(e_text + "After database migration some information is not available. \
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003879 Try to delete and create the scenarios and VNFs again", httperrors.Conflict)
tierno16e3dd42018-04-24 12:52:40 +02003880 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003881 raise NfvoException(e_text, httperrors.Internal_Server_Error)
tierno67881db2018-10-24 18:46:03 +02003882 if netDict["use"] == "mgmt":
3883 is_management_vm = True
3884 netDict["type"] = "virtual"
3885 if netDict["use"] == "bridge":
tierno16e3dd42018-04-24 12:52:40 +02003886 netDict["type"] = "virtual"
3887 if iface.get("vpci"):
3888 netDict['vpci'] = iface['vpci']
3889 if iface.get("mac"):
3890 netDict['mac_address'] = iface['mac']
tierno6082b7d2018-08-31 11:24:08 +00003891 if iface.get("mac_address"):
3892 netDict['mac_address'] = iface['mac_address']
tierno16e3dd42018-04-24 12:52:40 +02003893 if iface.get("ip_address"):
3894 netDict['ip_address'] = iface['ip_address']
3895 if iface.get("port-security") is not None:
3896 netDict['port_security'] = iface['port-security']
3897 if iface.get("floating-ip") is not None:
3898 netDict['floating_ip'] = iface['floating-ip']
3899 netDict['name'] = iface['internal_name']
3900 if iface['net_id'] is None:
3901 for vnf_iface in sce_vnf["interfaces"]:
3902 # print iface
3903 # print vnf_iface
3904 if vnf_iface['interface_id'] == iface['uuid']:
3905 netDict['net_id'] = "TASK-{}".format(
3906 net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id])
3907 instance_net_id = sce_net2instance[vnf_iface['sce_net_id']][datacenter_id]
3908 task_depends_on.append(net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id])
3909 break
3910 else:
3911 netDict['net_id'] = "TASK-{}".format(net2task_id[sce_vnf['uuid']][iface['net_id']])
3912 instance_net_id = vnf_net2instance[sce_vnf['uuid']][iface['net_id']]
3913 task_depends_on.append(net2task_id[sce_vnf['uuid']][iface['net_id']])
3914 # skip bridge ifaces not connected to any net
3915 if 'net_id' not in netDict or netDict['net_id'] == None:
3916 continue
3917 myVMDict['networks'].append(netDict)
3918 db_vm_iface = {
3919 # "uuid"
3920 # 'instance_vm_id': instance_vm_uuid,
3921 "instance_net_id": instance_net_id,
3922 'interface_id': iface['uuid'],
3923 # 'vim_interface_id': ,
3924 'type': 'external' if iface['external_name'] is not None else 'internal',
3925 'ip_address': iface.get('ip_address'),
3926 'mac_address': iface.get('mac'),
3927 'floating_ip': int(iface.get('floating-ip', False)),
3928 'port_security': int(iface.get('port-security', True))
3929 }
3930 db_vm_ifaces.append(db_vm_iface)
3931 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3932 # print myVMDict['name']
3933 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
3934 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
3935 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3936
3937 # We add the RO key to cloud_config if vnf will need ssh access
3938 cloud_config_vm = cloud_config
tierno67881db2018-10-24 18:46:03 +02003939 if is_management_vm and params["instance_parameters"].get("mgmt_keys"):
3940 cloud_config_vm = unify_cloud_config({"key-pairs": params["instance_parameters"]["mgmt_keys"]},
3941 cloud_config_vm)
3942
3943 if vm.get("instance_parameters") and vm["instance_parameters"].get("mgmt_keys"):
3944 cloud_config_vm = unify_cloud_config({"key-pairs": vm["instance_parameters"]["mgmt_keys"]},
3945 cloud_config_vm)
3946 # if ssh_access and ssh_access['required'] and ssh_access['default-user'] and tenant[0].get('RO_pub_key'):
3947 # RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]}
3948 # cloud_config_vm = unify_cloud_config(cloud_config_vm, RO_key)
tierno16e3dd42018-04-24 12:52:40 +02003949 if vm.get("boot_data"):
3950 cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config_vm)
3951
3952 if myVMDict.get('availability_zone'):
3953 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
3954 else:
3955 av_index = None
3956 for vm_index in range(0, vm.get('count', 1)):
tiernofc5f80b2018-05-29 16:00:43 +02003957 vm_name = myVMDict['name'] + "-" + str(vm_index+1)
3958 task_params = (vm_name, myVMDict['description'], myVMDict.get('start', None),
tierno16e3dd42018-04-24 12:52:40 +02003959 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm,
3960 myVMDict['disks'], av_index, vnf_availability_zones)
3961 # put interface uuid back to scenario[vnfs][vms[[interfaces]
3962 for net in myVMDict['networks']:
3963 if "vim_id" in net:
3964 for iface in vm['interfaces']:
3965 if net["name"] == iface["internal_name"]:
3966 iface["vim_id"] = net["vim_id"]
3967 break
3968 vm_uuid = str(uuid4())
3969 uuid_list.append(vm_uuid)
3970 db_vm = {
3971 "uuid": vm_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003972 "related": vm_uuid,
tierno16e3dd42018-04-24 12:52:40 +02003973 'instance_vnf_id': vnf_uuid,
3974 # TODO delete "vim_vm_id": vm_id,
3975 "vm_id": vm["uuid"],
tiernofc5f80b2018-05-29 16:00:43 +02003976 "vim_name": vm_name,
tierno16e3dd42018-04-24 12:52:40 +02003977 # "status":
3978 }
3979 db_instance_vms.append(db_vm)
3980
3981 iface_index = 0
3982 for db_vm_iface in db_vm_ifaces:
3983 iface_uuid = str(uuid4())
3984 uuid_list.append(iface_uuid)
3985 db_vm_iface_instance = {
3986 "uuid": iface_uuid,
3987 "instance_vm_id": vm_uuid
3988 }
3989 db_vm_iface_instance.update(db_vm_iface)
3990 if db_vm_iface_instance.get("ip_address"): # increment ip_address
3991 ip = db_vm_iface_instance.get("ip_address")
3992 i = ip.rfind(".")
3993 if i > 0:
3994 try:
3995 i += 1
3996 ip = ip[i:] + str(int(ip[:i]) + 1)
3997 db_vm_iface_instance["ip_address"] = ip
3998 except:
3999 db_vm_iface_instance["ip_address"] = None
4000 db_instance_interfaces.append(db_vm_iface_instance)
4001 myVMDict['networks'][iface_index]["uuid"] = iface_uuid
4002 iface_index += 1
4003
4004 db_vim_action = {
4005 "instance_action_id": instance_action_id,
4006 "task_index": task_index,
4007 "datacenter_vim_id": myvim_thread_id,
4008 "action": "CREATE",
4009 "status": "SCHEDULED",
4010 "item": "instance_vms",
4011 "item_id": vm_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00004012 "related": vm_uuid,
tierno16e3dd42018-04-24 12:52:40 +02004013 "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on},
4014 default_flow_style=True, width=256)
4015 }
4016 task_index += 1
4017 db_vim_actions.append(db_vim_action)
4018 params_out["task_index"] = task_index
4019 params_out["uuid_list"] = uuid_list
4020
4021
tierno7edb6752016-03-21 17:37:52 +01004022def delete_instance(mydb, tenant_id, instance_id):
tierno868220c2017-09-26 00:11:05 +02004023 # print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02004024 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
tierno868220c2017-09-26 00:11:05 +02004025 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01004026 tenant_id = instanceDict["tenant_id"]
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004027
4028 # --> WIM
4029 # We need to retrieve the WIM Actions now, before the instance_scenario is
4030 # deleted. The reason for that is that: ON CASCADE rules will delete the
4031 # instance_wim_nets record in the database
4032 wim_actions = wim_engine.delete_actions(instance_scenario_id=instance_id)
4033 # <-- WIM
4034
tierno868220c2017-09-26 00:11:05 +02004035 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno868220c2017-09-26 00:11:05 +02004036 # 1. Delete from Database
tiernof97fd272016-07-11 14:32:37 +02004037 message = mydb.delete_instance_scenario(instance_id, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01004038
tierno868220c2017-09-26 00:11:05 +02004039 # 2. delete from VIM
tiernoa2793912016-10-04 08:15:08 +00004040 error_msg = ""
tiernob3d36742017-03-03 23:51:05 +01004041 myvims = {}
4042 myvim_threads = {}
tierno868220c2017-09-26 00:11:05 +02004043 vimthread_affected = {}
tierno3fcfdb72017-10-24 07:48:24 +02004044 net2vm_dependencies = {}
tierno7edb6752016-03-21 17:37:52 +01004045
tierno868220c2017-09-26 00:11:05 +02004046 task_index = 0
4047 instance_action_id = get_task_id()
4048 db_vim_actions = []
4049 db_instance_action = {
4050 "uuid": instance_action_id, # same uuid for the instance and the action on create
4051 "tenant_id": tenant_id,
4052 "instance_id": instance_id,
4053 "description": "DELETE",
4054 # "number_tasks": 0 # filled bellow
4055 }
4056
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004057 # 2.1 deleting VNFFGs
tierno69b590e2018-03-13 18:52:23 +01004058 for sfp in instanceDict.get('sfps', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00004059 vimthread_affected[sfp["datacenter_tenant_id"]] = None
4060 datacenter_key = (sfp["datacenter_id"], sfp["datacenter_tenant_id"])
4061 if datacenter_key not in myvims:
4062 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004063 _, myvim_thread = get_vim_thread(mydb, tenant_id, sfp["datacenter_id"], sfp["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004064 except NfvoException as e:
4065 logger.error(str(e))
4066 myvim_thread = None
4067 myvim_threads[datacenter_key] = myvim_thread
4068 vims = get_vim(mydb, tenant_id, datacenter_id=sfp["datacenter_id"],
4069 datacenter_tenant_id=sfp["datacenter_tenant_id"])
4070 if len(vims) == 0:
4071 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp["datacenter_id"], sfp["datacenter_tenant_id"]))
4072 myvims[datacenter_key] = None
4073 else:
4074 myvims[datacenter_key] = vims.values()[0]
4075 myvim = myvims[datacenter_key]
4076 myvim_thread = myvim_threads[datacenter_key]
4077
4078 if not myvim:
4079 error_msg += "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp['vim_sfp_id'], sfp["datacenter_id"])
4080 continue
4081 extra = {"params": (sfp['vim_sfp_id'])}
4082 db_vim_action = {
4083 "instance_action_id": instance_action_id,
4084 "task_index": task_index,
4085 "datacenter_vim_id": sfp["datacenter_tenant_id"],
4086 "action": "DELETE",
4087 "status": "SCHEDULED",
4088 "item": "instance_sfps",
4089 "item_id": sfp["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004090 "related": sfp["related"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004091 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4092 }
4093 task_index += 1
4094 db_vim_actions.append(db_vim_action)
4095
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004096 for classification in instanceDict['classifications']:
4097 vimthread_affected[classification["datacenter_tenant_id"]] = None
4098 datacenter_key = (classification["datacenter_id"], classification["datacenter_tenant_id"])
4099 if datacenter_key not in myvims:
4100 try:
4101 _, myvim_thread = get_vim_thread(mydb, tenant_id, classification["datacenter_id"], classification["datacenter_tenant_id"])
4102 except NfvoException as e:
4103 logger.error(str(e))
4104 myvim_thread = None
4105 myvim_threads[datacenter_key] = myvim_thread
4106 vims = get_vim(mydb, tenant_id, datacenter_id=classification["datacenter_id"],
4107 datacenter_tenant_id=classification["datacenter_tenant_id"])
4108 if len(vims) == 0:
4109 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification["datacenter_id"],
4110 classification["datacenter_tenant_id"]))
4111 myvims[datacenter_key] = None
4112 else:
4113 myvims[datacenter_key] = vims.values()[0]
4114 myvim = myvims[datacenter_key]
4115 myvim_thread = myvim_threads[datacenter_key]
4116
4117 if not myvim:
4118 error_msg += "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification['vim_classification_id'],
4119 classification["datacenter_id"])
4120 continue
4121 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"]
4122 extra = {"params": (classification['vim_classification_id']), "depends_on": depends_on}
4123 db_vim_action = {
4124 "instance_action_id": instance_action_id,
4125 "task_index": task_index,
4126 "datacenter_vim_id": classification["datacenter_tenant_id"],
4127 "action": "DELETE",
4128 "status": "SCHEDULED",
4129 "item": "instance_classifications",
4130 "item_id": classification["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004131 "related": classification["related"],
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004132 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4133 }
4134 task_index += 1
4135 db_vim_actions.append(db_vim_action)
4136
tierno69b590e2018-03-13 18:52:23 +01004137 for sf in instanceDict.get('sfs', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00004138 vimthread_affected[sf["datacenter_tenant_id"]] = None
4139 datacenter_key = (sf["datacenter_id"], sf["datacenter_tenant_id"])
4140 if datacenter_key not in myvims:
4141 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004142 _, myvim_thread = get_vim_thread(mydb, tenant_id, sf["datacenter_id"], sf["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004143 except NfvoException as e:
4144 logger.error(str(e))
4145 myvim_thread = None
4146 myvim_threads[datacenter_key] = myvim_thread
4147 vims = get_vim(mydb, tenant_id, datacenter_id=sf["datacenter_id"],
4148 datacenter_tenant_id=sf["datacenter_tenant_id"])
4149 if len(vims) == 0:
4150 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf["datacenter_id"], sf["datacenter_tenant_id"]))
4151 myvims[datacenter_key] = None
4152 else:
4153 myvims[datacenter_key] = vims.values()[0]
4154 myvim = myvims[datacenter_key]
4155 myvim_thread = myvim_threads[datacenter_key]
4156
4157 if not myvim:
4158 error_msg += "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf['vim_sf_id'], sf["datacenter_id"])
4159 continue
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004160 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"]
4161 extra = {"params": (sf['vim_sf_id']), "depends_on": depends_on}
Igor D.Ccaadc442017-11-06 12:48:48 +00004162 db_vim_action = {
4163 "instance_action_id": instance_action_id,
4164 "task_index": task_index,
4165 "datacenter_vim_id": sf["datacenter_tenant_id"],
4166 "action": "DELETE",
4167 "status": "SCHEDULED",
4168 "item": "instance_sfs",
4169 "item_id": sf["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004170 "related": sf["related"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004171 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4172 }
4173 task_index += 1
4174 db_vim_actions.append(db_vim_action)
4175
tierno69b590e2018-03-13 18:52:23 +01004176 for sfi in instanceDict.get('sfis', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00004177 vimthread_affected[sfi["datacenter_tenant_id"]] = None
4178 datacenter_key = (sfi["datacenter_id"], sfi["datacenter_tenant_id"])
4179 if datacenter_key not in myvims:
4180 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004181 _, myvim_thread = get_vim_thread(mydb, tenant_id, sfi["datacenter_id"], sfi["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004182 except NfvoException as e:
4183 logger.error(str(e))
4184 myvim_thread = None
4185 myvim_threads[datacenter_key] = myvim_thread
4186 vims = get_vim(mydb, tenant_id, datacenter_id=sfi["datacenter_id"],
4187 datacenter_tenant_id=sfi["datacenter_tenant_id"])
4188 if len(vims) == 0:
4189 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi["datacenter_id"], sfi["datacenter_tenant_id"]))
4190 myvims[datacenter_key] = None
4191 else:
4192 myvims[datacenter_key] = vims.values()[0]
4193 myvim = myvims[datacenter_key]
4194 myvim_thread = myvim_threads[datacenter_key]
4195
4196 if not myvim:
4197 error_msg += "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi['vim_sfi_id'], sfi["datacenter_id"])
4198 continue
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004199 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfs"]
4200 extra = {"params": (sfi['vim_sfi_id']), "depends_on": depends_on}
Igor D.Ccaadc442017-11-06 12:48:48 +00004201 db_vim_action = {
4202 "instance_action_id": instance_action_id,
4203 "task_index": task_index,
4204 "datacenter_vim_id": sfi["datacenter_tenant_id"],
4205 "action": "DELETE",
4206 "status": "SCHEDULED",
4207 "item": "instance_sfis",
4208 "item_id": sfi["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004209 "related": sfi["related"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004210 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4211 }
4212 task_index += 1
4213 db_vim_actions.append(db_vim_action)
4214
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004215 # 2.2 deleting VMs
4216 # vm_fail_list=[]
gcalvinod6fac4d2018-11-05 10:42:06 +01004217 for sce_vnf in instanceDict.get('vnfs', ()):
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004218 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4219 vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None
Igor D.Ccaadc442017-11-06 12:48:48 +00004220 if datacenter_key not in myvims:
4221 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004222 _, 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 +00004223 except NfvoException as e:
4224 logger.error(str(e))
4225 myvim_thread = None
4226 myvim_threads[datacenter_key] = myvim_thread
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004227 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
4228 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004229 if len(vims) == 0:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004230 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
4231 sce_vnf["datacenter_tenant_id"]))
4232 myvims[datacenter_key] = None
4233 else:
4234 myvims[datacenter_key] = vims.values()[0]
4235 myvim = myvims[datacenter_key]
4236 myvim_thread = myvim_threads[datacenter_key]
4237
4238 for vm in sce_vnf['vms']:
4239 if not myvim:
4240 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
4241 continue
4242 sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"]
4243 db_vim_action = {
4244 "instance_action_id": instance_action_id,
4245 "task_index": task_index,
4246 "datacenter_vim_id": sce_vnf["datacenter_tenant_id"],
4247 "action": "DELETE",
4248 "status": "SCHEDULED",
4249 "item": "instance_vms",
4250 "item_id": vm["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004251 "related": vm["related"],
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004252 "extra": yaml.safe_dump({"params": vm["interfaces"], "depends_on": sfi_dependencies},
4253 default_flow_style=True, width=256)
4254 }
4255 db_vim_actions.append(db_vim_action)
4256 for interface in vm["interfaces"]:
4257 if not interface.get("instance_net_id"):
4258 continue
4259 if interface["instance_net_id"] not in net2vm_dependencies:
4260 net2vm_dependencies[interface["instance_net_id"]] = []
4261 net2vm_dependencies[interface["instance_net_id"]].append(task_index)
4262 task_index += 1
4263
4264 # 2.3 deleting NETS
4265 # net_fail_list=[]
4266 for net in instanceDict['nets']:
4267 vimthread_affected[net["datacenter_tenant_id"]] = None
4268 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4269 if datacenter_key not in myvims:
4270 try:
gcalvinod6fac4d2018-11-05 10:42:06 +01004271 _,myvim_thread = get_vim_thread(mydb, tenant_id, net["datacenter_id"], net["datacenter_tenant_id"])
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004272 except NfvoException as e:
4273 logger.error(str(e))
4274 myvim_thread = None
4275 myvim_threads[datacenter_key] = myvim_thread
4276 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
4277 datacenter_tenant_id=net["datacenter_tenant_id"])
4278 if len(vims) == 0:
4279 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 +00004280 myvims[datacenter_key] = None
4281 else:
4282 myvims[datacenter_key] = vims.values()[0]
4283 myvim = myvims[datacenter_key]
4284 myvim_thread = myvim_threads[datacenter_key]
4285
4286 if not myvim:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004287 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 +00004288 continue
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004289 extra = {"params": (net['vim_net_id'], net['sdn_net_id'])}
4290 if net2vm_dependencies.get(net["uuid"]):
4291 extra["depends_on"] = net2vm_dependencies[net["uuid"]]
4292 sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"]
4293 if len(sfi_dependencies) > 0:
4294 if "depends_on" in extra:
4295 extra["depends_on"] += sfi_dependencies
4296 else:
4297 extra["depends_on"] = sfi_dependencies
Igor D.Ccaadc442017-11-06 12:48:48 +00004298 db_vim_action = {
4299 "instance_action_id": instance_action_id,
4300 "task_index": task_index,
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004301 "datacenter_vim_id": net["datacenter_tenant_id"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004302 "action": "DELETE",
4303 "status": "SCHEDULED",
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004304 "item": "instance_nets",
4305 "item_id": net["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004306 "related": net["related"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004307 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4308 }
4309 task_index += 1
4310 db_vim_actions.append(db_vim_action)
4311
tierno868220c2017-09-26 00:11:05 +02004312 db_instance_action["number_tasks"] = task_index
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004313
4314 # --> WIM
4315 wim_actions, db_instance_action = (
4316 wim_engine.incorporate_actions(wim_actions, db_instance_action))
4317 # <-- WIM
4318
tierno868220c2017-09-26 00:11:05 +02004319 db_tables = [
4320 {"instance_actions": db_instance_action},
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004321 {"vim_wim_actions": db_vim_actions + wim_actions}
tierno868220c2017-09-26 00:11:05 +02004322 ]
4323
4324 logger.debug("delete_instance done DB tables: %s",
4325 yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
4326 mydb.new_rows(db_tables, ())
4327 for myvim_thread_id in vimthread_affected.keys():
4328 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
4329
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004330 wim_engine.dispatch(wim_actions)
4331
tiernob3d36742017-03-03 23:51:05 +01004332 if len(error_msg) > 0:
tierno868220c2017-09-26 00:11:05 +02004333 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
4334 '(error: 404) from VIM: {}'.format(instance_action_id, message, error_msg)
tierno7edb6752016-03-21 17:37:52 +01004335 else:
tierno868220c2017-09-26 00:11:05 +02004336 return "action_id={} instance {} deleted".format(instance_action_id, message)
tierno7edb6752016-03-21 17:37:52 +01004337
tierno7f426e92018-06-28 15:21:32 +02004338def get_instance_id(mydb, tenant_id, instance_id):
4339 global ovim
4340 #check valid tenant_id
4341 check_tenant(mydb, tenant_id)
4342 #obtain data
4343
4344 instance_dict = mydb.get_instance_scenario(instance_id, tenant_id, verbose=True)
4345 for net in instance_dict["nets"]:
4346 if net.get("sdn_net_id"):
4347 net_sdn = ovim.show_network(net["sdn_net_id"])
4348 net["sdn_info"] = {
4349 "admin_state_up": net_sdn.get("admin_state_up"),
4350 "flows": net_sdn.get("flows"),
4351 "last_error": net_sdn.get("last_error"),
4352 "ports": net_sdn.get("ports"),
4353 "type": net_sdn.get("type"),
4354 "status": net_sdn.get("status"),
4355 "vlan": net_sdn.get("vlan"),
4356 }
4357 return instance_dict
tiernob3d36742017-03-03 23:51:05 +01004358
tiernob8569aa2018-08-24 11:34:54 +02004359@deprecated("Instance is automatically refreshed by vim_threads")
tierno7edb6752016-03-21 17:37:52 +01004360def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
4361 '''Refreshes a scenario instance. It modifies instanceDict'''
4362 '''Returns:
4363 - 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
4364 - error_msg
4365 '''
tierno867ffe92017-03-27 12:50:34 +02004366 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
4367 # #print "nfvo.refresh_instance begins"
4368 # #print json.dumps(instanceDict, indent=4)
4369 #
4370 # #print "Getting the VIM URL and the VIM tenant_id"
4371 # myvims={}
4372 #
4373 # # 1. Getting VIM vm and net list
4374 # vms_updated = [] #List of VM instance uuids in openmano that were updated
4375 # vms_notupdated=[]
4376 # vm_list = {}
4377 # for sce_vnf in instanceDict['vnfs']:
4378 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4379 # if datacenter_key not in vm_list:
4380 # vm_list[datacenter_key] = []
4381 # if datacenter_key not in myvims:
4382 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
4383 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
4384 # if len(vims) == 0:
4385 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
4386 # myvims[datacenter_key] = None
4387 # else:
4388 # myvims[datacenter_key] = vims.values()[0]
4389 # for vm in sce_vnf['vms']:
4390 # vm_list[datacenter_key].append(vm['vim_vm_id'])
4391 # vms_notupdated.append(vm["uuid"])
4392 #
4393 # nets_updated = [] #List of VM instance uuids in openmano that were updated
4394 # nets_notupdated=[]
4395 # net_list = {}
4396 # for net in instanceDict['nets']:
4397 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4398 # if datacenter_key not in net_list:
4399 # net_list[datacenter_key] = []
4400 # if datacenter_key not in myvims:
4401 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
4402 # datacenter_tenant_id=net["datacenter_tenant_id"])
4403 # if len(vims) == 0:
4404 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
4405 # myvims[datacenter_key] = None
4406 # else:
4407 # myvims[datacenter_key] = vims.values()[0]
4408 #
4409 # net_list[datacenter_key].append(net['vim_net_id'])
4410 # nets_notupdated.append(net["uuid"])
4411 #
4412 # # 1. Getting the status of all VMs
4413 # vm_dict={}
4414 # for datacenter_key in myvims:
4415 # if not vm_list.get(datacenter_key):
4416 # continue
4417 # failed = True
4418 # failed_message=""
4419 # if not myvims[datacenter_key]:
4420 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4421 # else:
4422 # try:
4423 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
4424 # failed = False
4425 # except vimconn.vimconnException as e:
4426 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4427 # failed_message = str(e)
4428 # if failed:
4429 # for vm in vm_list[datacenter_key]:
4430 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4431 #
4432 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
4433 # for sce_vnf in instanceDict['vnfs']:
4434 # for vm in sce_vnf['vms']:
4435 # vm_id = vm['vim_vm_id']
4436 # interfaces = vm_dict[vm_id].pop('interfaces', [])
4437 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
4438 # has_mgmt_iface = False
4439 # for iface in vm["interfaces"]:
4440 # if iface["type"]=="mgmt":
4441 # has_mgmt_iface = True
4442 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
4443 # vm_dict[vm_id]['status'] = "ACTIVE"
4444 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
4445 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
4446 # 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'):
4447 # vm['status'] = vm_dict[vm_id]['status']
4448 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
4449 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
4450 # # 2.1. Update in openmano DB the VMs whose status changed
4451 # try:
4452 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
4453 # vms_notupdated.remove(vm["uuid"])
4454 # if updates>0:
4455 # vms_updated.append(vm["uuid"])
4456 # except db_base_Exception as e:
4457 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4458 # # 2.2. Update in openmano DB the interface VMs
4459 # for interface in interfaces:
4460 # #translate from vim_net_id to instance_net_id
4461 # network_id_list=[]
4462 # for net in instanceDict['nets']:
4463 # if net["vim_net_id"] == interface["vim_net_id"]:
4464 # network_id_list.append(net["uuid"])
4465 # if not network_id_list:
4466 # continue
4467 # del interface["vim_net_id"]
4468 # try:
4469 # for network_id in network_id_list:
4470 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
4471 # except db_base_Exception as e:
4472 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
4473 #
4474 # # 3. Getting the status of all nets
4475 # net_dict = {}
4476 # for datacenter_key in myvims:
4477 # if not net_list.get(datacenter_key):
4478 # continue
4479 # failed = True
4480 # failed_message = ""
4481 # if not myvims[datacenter_key]:
4482 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4483 # else:
4484 # try:
4485 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
4486 # failed = False
4487 # except vimconn.vimconnException as e:
4488 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4489 # failed_message = str(e)
4490 # if failed:
4491 # for net in net_list[datacenter_key]:
4492 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4493 #
4494 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
4495 # # TODO: update nets inside a vnf
4496 # for net in instanceDict['nets']:
4497 # net_id = net['vim_net_id']
4498 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
4499 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
4500 # 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'):
4501 # net['status'] = net_dict[net_id]['status']
4502 # net['error_msg'] = net_dict[net_id].get('error_msg')
4503 # net['vim_info'] = net_dict[net_id].get('vim_info')
4504 # # 5.1. Update in openmano DB the nets whose status changed
4505 # try:
4506 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
4507 # nets_notupdated.remove(net["uuid"])
4508 # if updated>0:
4509 # nets_updated.append(net["uuid"])
4510 # except db_base_Exception as e:
4511 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4512 #
4513 # # Returns appropriate output
4514 # #print "nfvo.refresh_instance finishes"
4515 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
4516 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
tierno7edb6752016-03-21 17:37:52 +01004517 instance_id = instanceDict['uuid']
tierno867ffe92017-03-27 12:50:34 +02004518 # if len(vms_notupdated)+len(nets_notupdated)>0:
4519 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
4520 # 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 +01004521
tiernoae4a8d12016-07-08 12:30:39 +02004522 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
tierno7edb6752016-03-21 17:37:52 +01004523
4524def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
tiernoae4a8d12016-07-08 12:30:39 +02004525 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02004526 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01004527 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4528
tiernoae4a8d12016-07-08 12:30:39 +02004529 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernof97fd272016-07-11 14:32:37 +02004530 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
4531 if len(vims) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004532 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01004533 myvim = vims.values()[0]
tiernofc5f80b2018-05-29 16:00:43 +02004534 vm_result = {}
4535 vm_error = 0
4536 vm_ok = 0
tierno42026a02017-02-10 15:13:40 +01004537
tiernofc5f80b2018-05-29 16:00:43 +02004538 myvim_threads_id = {}
4539 if action_dict.get("vdu-scaling"):
4540 db_instance_vms = []
4541 db_vim_actions = []
4542 db_instance_interfaces = []
4543 instance_action_id = get_task_id()
4544 db_instance_action = {
4545 "uuid": instance_action_id, # same uuid for the instance and the action on create
4546 "tenant_id": nfvo_tenant,
4547 "instance_id": instance_id,
4548 "description": "SCALE",
4549 }
4550 vm_result["instance_action_id"] = instance_action_id
tierno67881db2018-10-24 18:46:03 +02004551 vm_result["created"] = []
4552 vm_result["deleted"] = []
tiernofc5f80b2018-05-29 16:00:43 +02004553 task_index = 0
4554 for vdu in action_dict["vdu-scaling"]:
tierno868220c2017-09-26 00:11:05 +02004555 vdu_id = vdu.get("vdu-id")
tiernofc5f80b2018-05-29 16:00:43 +02004556 osm_vdu_id = vdu.get("osm_vdu_id")
4557 member_vnf_index = vdu.get("member-vnf-index")
tierno868220c2017-09-26 00:11:05 +02004558 vdu_count = vdu.get("count", 1)
tiernofc5f80b2018-05-29 16:00:43 +02004559 if vdu_id:
tierno67881db2018-10-24 18:46:03 +02004560 target_vms = mydb.get_rows(
tiernofc5f80b2018-05-29 16:00:43 +02004561 FROM="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid",
4562 WHERE={"vms.uuid": vdu_id},
4563 ORDER_BY="vms.created_at"
4564 )
tierno67881db2018-10-24 18:46:03 +02004565 if not target_vms:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004566 raise NfvoException("Cannot find the vdu with id {}".format(vdu_id), httperrors.Not_Found)
tiernofc5f80b2018-05-29 16:00:43 +02004567 else:
4568 if not osm_vdu_id and not member_vnf_index:
tiernoa43bd9e2018-11-26 09:28:58 +00004569 raise NfvoException("Invalid input vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'")
tierno67881db2018-10-24 18:46:03 +02004570 target_vms = mydb.get_rows(
tiernofc5f80b2018-05-29 16:00:43 +02004571 # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"),
4572 FROM="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\
4573 " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\
4574 " join vms on ivms.vm_id=vms.uuid",
tiernoa43bd9e2018-11-26 09:28:58 +00004575 WHERE={"vms.osm_id": osm_vdu_id, "svnfs.member_vnf_index": member_vnf_index,
4576 "ivnfs.instance_scenario_id": instance_id},
tiernofc5f80b2018-05-29 16:00:43 +02004577 ORDER_BY="ivms.created_at"
4578 )
tierno67881db2018-10-24 18:46:03 +02004579 if not target_vms:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004580 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 +02004581 vdu_id = target_vms[-1]["uuid"]
4582 target_vm = target_vms[-1]
tiernofc5f80b2018-05-29 16:00:43 +02004583 datacenter = target_vm["datacenter_id"]
4584 myvim_threads_id[datacenter], _ = get_vim_thread(mydb, nfvo_tenant, datacenter)
tiernofc5f80b2018-05-29 16:00:43 +02004585
tierno67881db2018-10-24 18:46:03 +02004586 if vdu["type"] == "delete":
4587 for index in range(0, vdu_count):
4588 target_vm = target_vms[-1-index]
4589 vdu_id = target_vm["uuid"]
4590 # look for nm
4591 vm_interfaces = None
4592 for sce_vnf in instanceDict['vnfs']:
4593 for vm in sce_vnf['vms']:
4594 if vm["uuid"] == vdu_id:
4595 vm_interfaces = vm["interfaces"]
4596 break
4597
4598 db_vim_action = {
4599 "instance_action_id": instance_action_id,
4600 "task_index": task_index,
4601 "datacenter_vim_id": target_vm["datacenter_tenant_id"],
4602 "action": "DELETE",
4603 "status": "SCHEDULED",
4604 "item": "instance_vms",
4605 "item_id": vdu_id,
tierno3c44e7b2019-03-04 17:32:01 +00004606 "related": vm["related"],
tierno67881db2018-10-24 18:46:03 +02004607 "extra": yaml.safe_dump({"params": vm_interfaces},
4608 default_flow_style=True, width=256)
4609 }
4610 task_index += 1
4611 db_vim_actions.append(db_vim_action)
4612 vm_result["deleted"].append(vdu_id)
4613 # delete from database
4614 db_instance_vms.append({"TO-DELETE": vdu_id})
tiernofc5f80b2018-05-29 16:00:43 +02004615
4616 else: # vdu["type"] == "create":
4617 iface2iface = {}
4618 where = {"item": "instance_vms", "item_id": target_vm["uuid"], "action": "CREATE"}
4619
garciadeblas72cd59f2018-12-05 10:59:40 +01004620 vim_action_to_clone = mydb.get_rows(FROM="vim_wim_actions", WHERE=where)
tiernofc5f80b2018-05-29 16:00:43 +02004621 if not vim_action_to_clone:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004622 raise NfvoException("Cannot find the vim_action at database with {}".format(where), httperrors.Internal_Server_Error)
tiernofc5f80b2018-05-29 16:00:43 +02004623 vim_action_to_clone = vim_action_to_clone[0]
4624 extra = yaml.safe_load(vim_action_to_clone["extra"])
4625
4626 # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y
4627 # TODO do the same for flavor and image when available
4628 task_depends_on = []
4629 task_params = extra["params"]
4630 task_params_networks = deepcopy(task_params[5])
4631 for iface in task_params[5]:
4632 if iface["net_id"].startswith("TASK-"):
4633 if "." not in iface["net_id"]:
4634 task_depends_on.append("{}.{}".format(vim_action_to_clone["instance_action_id"],
4635 iface["net_id"][5:]))
4636 iface["net_id"] = "TASK-{}.{}".format(vim_action_to_clone["instance_action_id"],
4637 iface["net_id"][5:])
4638 else:
4639 task_depends_on.append(iface["net_id"][5:])
4640 if "mac_address" in iface:
4641 del iface["mac_address"]
4642
4643 vm_ifaces_to_clone = mydb.get_rows(FROM="instance_interfaces", WHERE={"instance_vm_id": target_vm["uuid"]})
4644 for index in range(0, vdu_count):
4645 vm_uuid = str(uuid4())
4646 vm_name = target_vm.get('vim_name')
4647 try:
4648 suffix = vm_name.rfind("-")
tierno67881db2018-10-24 18:46:03 +02004649 vm_name = vm_name[:suffix+1] + str(index + 1 + int(vm_name[suffix+1:]))
tiernofc5f80b2018-05-29 16:00:43 +02004650 except Exception:
4651 pass
4652 db_instance_vm = {
4653 "uuid": vm_uuid,
4654 'instance_vnf_id': target_vm['instance_vnf_id'],
4655 'vm_id': target_vm['vm_id'],
4656 'vim_name': vm_name
4657 }
4658 db_instance_vms.append(db_instance_vm)
4659
4660 for vm_iface in vm_ifaces_to_clone:
4661 iface_uuid = str(uuid4())
4662 iface2iface[vm_iface["uuid"]] = iface_uuid
4663 db_vm_iface = {
4664 "uuid": iface_uuid,
4665 'instance_vm_id': vm_uuid,
4666 "instance_net_id": vm_iface["instance_net_id"],
4667 'interface_id': vm_iface['interface_id'],
4668 'type': vm_iface['type'],
4669 'floating_ip': vm_iface['floating_ip'],
4670 'port_security': vm_iface['port_security']
4671 }
4672 db_instance_interfaces.append(db_vm_iface)
4673 task_params_copy = deepcopy(task_params)
4674 for iface in task_params_copy[5]:
4675 iface["uuid"] = iface2iface[iface["uuid"]]
4676 # increment ip_address
4677 if "ip_address" in iface:
4678 ip = iface.get("ip_address")
4679 i = ip.rfind(".")
4680 if i > 0:
4681 try:
4682 i += 1
4683 ip = ip[i:] + str(int(ip[:i]) + 1)
4684 iface["ip_address"] = ip
4685 except:
4686 iface["ip_address"] = None
4687 if vm_name:
4688 task_params_copy[0] = vm_name
4689 db_vim_action = {
4690 "instance_action_id": instance_action_id,
4691 "task_index": task_index,
4692 "datacenter_vim_id": vim_action_to_clone["datacenter_vim_id"],
4693 "action": "CREATE",
4694 "status": "SCHEDULED",
4695 "item": "instance_vms",
4696 "item_id": vm_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00004697 "related": target_vm["related"],
tiernofc5f80b2018-05-29 16:00:43 +02004698 # ALF
4699 # ALF
4700 # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd.
4701 # ALF
4702 # ALF
4703 "extra": yaml.safe_dump({"params": task_params_copy, "depends_on": task_depends_on}, default_flow_style=True, width=256)
4704 }
4705 task_index += 1
4706 db_vim_actions.append(db_vim_action)
tierno67881db2018-10-24 18:46:03 +02004707 vm_result["created"].append(vm_uuid)
tiernofc5f80b2018-05-29 16:00:43 +02004708
4709 db_instance_action["number_tasks"] = task_index
4710 db_tables = [
4711 {"instance_vms": db_instance_vms},
4712 {"instance_interfaces": db_instance_interfaces},
4713 {"instance_actions": db_instance_action},
4714 # TODO revise sfps
4715 # {"instance_sfis": db_instance_sfis},
4716 # {"instance_sfs": db_instance_sfs},
4717 # {"instance_classifications": db_instance_classifications},
4718 # {"instance_sfps": db_instance_sfps},
garciadeblasaba7a0d2018-12-05 12:42:35 +01004719 {"vim_wim_actions": db_vim_actions}
tiernofc5f80b2018-05-29 16:00:43 +02004720 ]
4721 logger.debug("create_vdu done DB tables: %s",
4722 yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
4723 mydb.new_rows(db_tables, [])
4724 for myvim_thread in myvim_threads_id.values():
4725 vim_threads["running"][myvim_thread].insert_task(db_vim_actions)
4726
4727 return vm_result
tierno7edb6752016-03-21 17:37:52 +01004728
4729 input_vnfs = action_dict.pop("vnfs", [])
4730 input_vms = action_dict.pop("vms", [])
tierno92c36fd2018-05-04 12:21:10 +02004731 action_over_all = True if not input_vnfs and not input_vms else False
tierno7edb6752016-03-21 17:37:52 +01004732 for sce_vnf in instanceDict['vnfs']:
4733 for vm in sce_vnf['vms']:
tierno92c36fd2018-05-04 12:21:10 +02004734 if not action_over_all and sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
4735 sce_vnf['member_vnf_index'] not in input_vnfs and \
4736 vm['uuid'] not in input_vms and vm['name'] not in input_vms:
4737 continue
tiernoae4a8d12016-07-08 12:30:39 +02004738 try:
gcalvinoe580c7d2017-09-22 14:09:51 +02004739 if "add_public_key" in action_dict:
4740 mgmt_access = {}
4741 if sce_vnf.get('mgmt_access'):
4742 mgmt_access = yaml.load(sce_vnf['mgmt_access'])
4743 ssh_access = mgmt_access['config-access']['ssh-access']
4744 tenant = mydb.get_rows_by_id('nfvo_tenants', nfvo_tenant)
tierno42026a02017-02-10 15:13:40 +01004745 try:
gcalvinoe580c7d2017-09-22 14:09:51 +02004746 if ssh_access['required'] and ssh_access['default-user']:
4747 if 'ip_address' in vm:
4748 mgmt_ip = vm['ip_address'].split(';')
4749 password = mgmt_access['config-access'].get('password')
4750 priv_RO_key = decrypt_key(tenant[0]['encrypted_RO_priv_key'], tenant[0]['uuid'])
4751 myvim.inject_user_key(mgmt_ip[0], ssh_access['default-user'],
4752 action_dict['add_public_key'],
4753 password=password, ro_key=priv_RO_key)
4754 else:
4755 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004756 httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02004757 except KeyError:
4758 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004759 httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02004760 else:
4761 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004762 httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02004763 else:
4764 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
4765 if "console" in action_dict:
4766 if not global_config["http_console_proxy"]:
tierno20fc2a22016-08-19 17:02:35 +02004767 vm_result[ vm['uuid'] ] = {"vim_result": 200,
4768 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4769 protocol=data["protocol"],
gcalvinoe580c7d2017-09-22 14:09:51 +02004770 ip = data["server"],
4771 port = data["port"],
tierno20fc2a22016-08-19 17:02:35 +02004772 suffix = data["suffix"]),
4773 "name":vm['name']
4774 }
4775 vm_ok +=1
gcalvinoe580c7d2017-09-22 14:09:51 +02004776 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004777 vm_result[ vm['uuid'] ] = {"vim_result": -httperrors.Unauthorized,
gcalvinoe580c7d2017-09-22 14:09:51 +02004778 "description": "this console is only reachable by local interface",
4779 "name":vm['name']
4780 }
tierno20fc2a22016-08-19 17:02:35 +02004781 vm_error+=1
gcalvinoe580c7d2017-09-22 14:09:51 +02004782 else:
4783 #print "console data", data
4784 try:
4785 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
4786 vm_result[ vm['uuid'] ] = {"vim_result": 200,
4787 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4788 protocol=data["protocol"],
4789 ip = global_config["http_console_host"],
4790 port = console_thread.port,
4791 suffix = data["suffix"]),
4792 "name":vm['name']
4793 }
4794 vm_ok +=1
4795 except NfvoException as e:
4796 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
4797 vm_error+=1
tierno20fc2a22016-08-19 17:02:35 +02004798
gcalvinoe580c7d2017-09-22 14:09:51 +02004799 else:
4800 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
4801 vm_ok +=1
tiernoae4a8d12016-07-08 12:30:39 +02004802 except vimconn.vimconnException as e:
4803 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
4804 vm_error+=1
tierno7edb6752016-03-21 17:37:52 +01004805
4806 if vm_ok==0: #all goes wrong
tierno351863c2016-07-23 01:46:03 +02004807 return vm_result
tierno7edb6752016-03-21 17:37:52 +01004808 else:
tierno351863c2016-07-23 01:46:03 +02004809 return vm_result
tierno42026a02017-02-10 15:13:40 +01004810
tierno868220c2017-09-26 00:11:05 +02004811def instance_action_get(mydb, nfvo_tenant, instance_id, action_id):
tierno16e3dd42018-04-24 12:52:40 +02004812 filter = {}
tierno868220c2017-09-26 00:11:05 +02004813 if nfvo_tenant and nfvo_tenant != "any":
4814 filter["tenant_id"] = nfvo_tenant
4815 if instance_id and instance_id != "any":
4816 filter["instance_id"] = instance_id
4817 if action_id:
4818 filter["uuid"] = action_id
4819 rows = mydb.get_rows(FROM="instance_actions", WHERE=filter)
tierno16e3dd42018-04-24 12:52:40 +02004820 if action_id:
4821 if not rows:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004822 raise NfvoException("Not found any action with this criteria", httperrors.Not_Found)
4823 vim_wim_actions = mydb.get_rows(FROM="vim_wim_actions", WHERE={"instance_action_id": action_id})
4824 rows[0]["vim_wim_actions"] = vim_wim_actions
tierno31e121f2018-12-03 12:04:48 +00004825 # for backward compatibility set vim_actions = vim_wim_actions
4826 rows[0]["vim_actions"] = vim_wim_actions
tiernofc5f80b2018-05-29 16:00:43 +02004827 return {"actions": rows}
tierno868220c2017-09-26 00:11:05 +02004828
tiernob3d36742017-03-03 23:51:05 +01004829
tierno7edb6752016-03-21 17:37:52 +01004830def create_or_use_console_proxy_thread(console_server, console_port):
4831 #look for a non-used port
4832 console_thread_key = console_server + ":" + str(console_port)
4833 if console_thread_key in global_config["console_thread"]:
4834 #global_config["console_thread"][console_thread_key].start_timeout()
tiernof97fd272016-07-11 14:32:37 +02004835 return global_config["console_thread"][console_thread_key]
tierno42026a02017-02-10 15:13:40 +01004836
tierno7edb6752016-03-21 17:37:52 +01004837 for port in global_config["console_port_iterator"]():
tierno20fc2a22016-08-19 17:02:35 +02004838 #print "create_or_use_console_proxy_thread() port:", port
tierno7edb6752016-03-21 17:37:52 +01004839 if port in global_config["console_ports"]:
4840 continue
4841 try:
4842 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
4843 clithread.start()
4844 global_config["console_thread"][console_thread_key] = clithread
4845 global_config["console_ports"][port] = console_thread_key
tiernof97fd272016-07-11 14:32:37 +02004846 return clithread
tierno7edb6752016-03-21 17:37:52 +01004847 except cli.ConsoleProxyExceptionPortUsed as e:
4848 #port used, try with onoher
4849 continue
4850 except cli.ConsoleProxyException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004851 raise NfvoException(str(e), httperrors.Bad_Request)
4852 raise NfvoException("Not found any free 'http_console_ports'", httperrors.Conflict)
tierno7edb6752016-03-21 17:37:52 +01004853
tiernob3d36742017-03-03 23:51:05 +01004854
tierno7edb6752016-03-21 17:37:52 +01004855def check_tenant(mydb, tenant_id):
4856 '''check that tenant exists at database'''
tiernof97fd272016-07-11 14:32:37 +02004857 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
4858 if not tenant:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004859 raise NfvoException("tenant '{}' not found".format(tenant_id), httperrors.Not_Found)
tiernof97fd272016-07-11 14:32:37 +02004860 return
tierno7edb6752016-03-21 17:37:52 +01004861
4862def new_tenant(mydb, tenant_dict):
tierno7edb6752016-03-21 17:37:52 +01004863
gcalvinoe580c7d2017-09-22 14:09:51 +02004864 tenant_uuid = str(uuid4())
4865 tenant_dict['uuid'] = tenant_uuid
4866 try:
4867 pub_key, priv_key = create_RO_keypair(tenant_uuid)
4868 tenant_dict['RO_pub_key'] = pub_key
4869 tenant_dict['encrypted_RO_priv_key'] = priv_key
gcalvinoc62cfa52017-10-05 18:21:25 +02004870 mydb.new_row("nfvo_tenants", tenant_dict, confidential_data=True)
gcalvinoe580c7d2017-09-22 14:09:51 +02004871 except db_base_Exception as e:
tierno9c5c8322018-03-23 15:44:03 +01004872 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict['name']) + str(e), e.http_code)
gcalvinoe580c7d2017-09-22 14:09:51 +02004873 return tenant_uuid
tiernob3d36742017-03-03 23:51:05 +01004874
tierno7edb6752016-03-21 17:37:52 +01004875def delete_tenant(mydb, tenant):
4876 #get nfvo_tenant info
tierno42026a02017-02-10 15:13:40 +01004877
tiernof97fd272016-07-11 14:32:37 +02004878 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
4879 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
4880 return tenant_dict['uuid'] + " " + tenant_dict["name"]
tierno7edb6752016-03-21 17:37:52 +01004881
tiernob3d36742017-03-03 23:51:05 +01004882
tierno7edb6752016-03-21 17:37:52 +01004883def new_datacenter(mydb, datacenter_descriptor):
tierno1c848c02018-05-21 16:40:33 +02004884 sdn_port_mapping = None
tierno7edb6752016-03-21 17:37:52 +01004885 if "config" in datacenter_descriptor:
tiernoedf3f4f2018-05-17 23:02:47 +02004886 sdn_port_mapping = datacenter_descriptor["config"].pop("sdn-port-mapping", None)
4887 datacenter_descriptor["config"] = yaml.safe_dump(datacenter_descriptor["config"], default_flow_style=True,
4888 width=256)
4889 # Check that datacenter-type is correct
tierno3ae39742016-09-07 12:17:51 +02004890 datacenter_type = datacenter_descriptor.get("type", "openvim");
tiernoedf3f4f2018-05-17 23:02:47 +02004891 # module_info = None
tierno3ae39742016-09-07 12:17:51 +02004892 try:
4893 module = "vimconn_" + datacenter_type
tierno361275f2017-04-25 16:24:34 +02004894 pkg = __import__("osm_ro." + module)
tiernoedf3f4f2018-05-17 23:02:47 +02004895 # vim_conn = getattr(pkg, module)
tierno361275f2017-04-25 16:24:34 +02004896 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
tierno3ae39742016-09-07 12:17:51 +02004897 except (IOError, ImportError):
tierno361275f2017-04-25 16:24:34 +02004898 # if module_info and module_info[0]:
4899 # file.close(module_info[0])
tiernoedf3f4f2018-05-17 23:02:47 +02004900 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type,
4901 module),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004902 httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +01004903
gcalvinoc62cfa52017-10-05 18:21:25 +02004904 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True, confidential_data=True)
tiernoedf3f4f2018-05-17 23:02:47 +02004905 if sdn_port_mapping:
4906 try:
4907 datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, sdn_port_mapping)
4908 except Exception as e:
4909 mydb.delete_row_by_id("datacenters", datacenter_id) # Rollback
4910 raise e
tiernof97fd272016-07-11 14:32:37 +02004911 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01004912
tiernob3d36742017-03-03 23:51:05 +01004913
tierno7edb6752016-03-21 17:37:52 +01004914def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
tierno8fe7a492017-07-11 13:50:04 +02004915 # obtain data, check that only one exist
tiernof97fd272016-07-11 14:32:37 +02004916 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
tierno8fe7a492017-07-11 13:50:04 +02004917
4918 # edit data
tiernof97fd272016-07-11 14:32:37 +02004919 datacenter_id = datacenter['uuid']
tiernod72182f2018-08-29 10:56:13 +02004920 where = {'uuid': datacenter['uuid']}
tierno8fe7a492017-07-11 13:50:04 +02004921 remove_port_mapping = False
tiernoedf3f4f2018-05-17 23:02:47 +02004922 new_sdn_port_mapping = None
tierno7edb6752016-03-21 17:37:52 +01004923 if "config" in datacenter_descriptor:
tierno8fe7a492017-07-11 13:50:04 +02004924 if datacenter_descriptor['config'] != None:
tierno7edb6752016-03-21 17:37:52 +01004925 try:
4926 new_config_dict = datacenter_descriptor["config"]
tiernoedf3f4f2018-05-17 23:02:47 +02004927 if "sdn-port-mapping" in new_config_dict:
4928 remove_port_mapping = True
4929 new_sdn_port_mapping = new_config_dict.pop("sdn-port-mapping")
tiernod72182f2018-08-29 10:56:13 +02004930 # delete null fields
4931 to_delete = []
tierno7edb6752016-03-21 17:37:52 +01004932 for k in new_config_dict:
tiernod72182f2018-08-29 10:56:13 +02004933 if new_config_dict[k] is None:
tierno7edb6752016-03-21 17:37:52 +01004934 to_delete.append(k)
tierno8fe7a492017-07-11 13:50:04 +02004935 if k == 'sdn-controller':
4936 remove_port_mapping = True
tierno42026a02017-02-10 15:13:40 +01004937
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004938 config_text = datacenter.get("config")
4939 if not config_text:
4940 config_text = '{}'
4941 config_dict = yaml.load(config_text)
tierno7edb6752016-03-21 17:37:52 +01004942 config_dict.update(new_config_dict)
tiernod72182f2018-08-29 10:56:13 +02004943 # delete null fields
tierno7edb6752016-03-21 17:37:52 +01004944 for k in to_delete:
4945 del config_dict[k]
tiernof97fd272016-07-11 14:32:37 +02004946 except Exception as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004947 raise NfvoException("Bad format at datacenter:config " + str(e), httperrors.Bad_Request)
tierno8fe7a492017-07-11 13:50:04 +02004948 if config_dict:
4949 datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256)
4950 else:
4951 datacenter_descriptor["config"] = None
4952 if remove_port_mapping:
4953 try:
4954 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id)
4955 except ovimException as e:
Anderson Bravalheric5293de2018-11-28 17:21:26 +00004956 raise NfvoException("Error deleting datacenter-port-mapping " + str(e), httperrors.Conflict)
tierno8fe7a492017-07-11 13:50:04 +02004957
tiernof97fd272016-07-11 14:32:37 +02004958 mydb.update_rows('datacenters', datacenter_descriptor, where)
tiernoedf3f4f2018-05-17 23:02:47 +02004959 if new_sdn_port_mapping:
4960 try:
4961 datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, new_sdn_port_mapping)
4962 except ovimException as e:
tiernod72182f2018-08-29 10:56:13 +02004963 # Rollback
4964 mydb.update_rows('datacenters', datacenter, where)
Anderson Bravalheric5293de2018-11-28 17:21:26 +00004965 raise NfvoException("Error adding datacenter-port-mapping " + str(e), httperrors.Conflict)
tiernof97fd272016-07-11 14:32:37 +02004966 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01004967
tiernob3d36742017-03-03 23:51:05 +01004968
tierno7edb6752016-03-21 17:37:52 +01004969def delete_datacenter(mydb, datacenter):
4970 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02004971 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
4972 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
tierno8fe7a492017-07-11 13:50:04 +02004973 try:
4974 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid'])
4975 except ovimException as e:
tiernod72182f2018-08-29 10:56:13 +02004976 raise NfvoException("Error deleting datacenter-port-mapping " + str(e))
tiernof97fd272016-07-11 14:32:37 +02004977 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
tierno7edb6752016-03-21 17:37:52 +01004978
tiernob3d36742017-03-03 23:51:05 +01004979
tiernod3750b32018-07-20 15:33:08 +02004980def create_vim_account(mydb, nfvo_tenant, datacenter_id, name=None, vim_id=None, vim_tenant=None, vim_tenant_name=None,
4981 vim_username=None, vim_password=None, config=None):
tierno9c22f2d2017-10-09 16:23:55 +02004982 # get datacenter info
tierno0ea2a7e2017-10-18 00:06:26 +02004983 try:
tiernod3750b32018-07-20 15:33:08 +02004984 if not datacenter_id:
4985 if not vim_id:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004986 raise NfvoException("You must provide 'vim_id", http_code=httperrors.Bad_Request)
tiernod3750b32018-07-20 15:33:08 +02004987 datacenter_id = vim_id
4988 datacenter_id, datacenter_name = get_datacenter_uuid(mydb, None, datacenter_id)
tierno7edb6752016-03-21 17:37:52 +01004989
tiernod3750b32018-07-20 15:33:08 +02004990 create_vim_tenant = True if not vim_tenant and not vim_tenant_name else False
tierno42026a02017-02-10 15:13:40 +01004991
tierno0ea2a7e2017-10-18 00:06:26 +02004992 # get nfvo_tenant info
4993 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
4994 if vim_tenant_name==None:
4995 vim_tenant_name=tenant_dict['name']
tierno42026a02017-02-10 15:13:40 +01004996
tierno0ea2a7e2017-10-18 00:06:26 +02004997 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
tiernod3750b32018-07-20 15:33:08 +02004998 # #check that this association does not exist before
4999 # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
5000 # if len(tenants_datacenters)>0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005001 # raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Conflict)
tierno7edb6752016-03-21 17:37:52 +01005002
tierno0ea2a7e2017-10-18 00:06:26 +02005003 vim_tenant_id_exist_atdb=False
5004 if not create_vim_tenant:
5005 where_={"datacenter_id": datacenter_id}
tiernod3750b32018-07-20 15:33:08 +02005006 if vim_tenant!=None:
5007 where_["vim_tenant_id"] = vim_tenant
tierno0ea2a7e2017-10-18 00:06:26 +02005008 if vim_tenant_name!=None:
5009 where_["vim_tenant_name"] = vim_tenant_name
5010 #check if vim_tenant_id is already at database
5011 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
5012 if len(datacenter_tenants_dict)>=1:
5013 datacenter_tenants_dict = datacenter_tenants_dict[0]
5014 vim_tenant_id_exist_atdb=True
5015 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
5016 else: #result=0
5017 datacenter_tenants_dict = {}
5018 #insert at table datacenter_tenants
tiernod3750b32018-07-20 15:33:08 +02005019 else: #if vim_tenant==None:
tierno0ea2a7e2017-10-18 00:06:26 +02005020 #create tenant at VIM if not provided
5021 try:
5022 _, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username,
5023 vim_passwd=vim_password)
5024 datacenter_name = myvim["name"]
tiernod3750b32018-07-20 15:33:08 +02005025 vim_tenant = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
tierno0ea2a7e2017-10-18 00:06:26 +02005026 except vimconn.vimconnException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005027 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 +01005028 datacenter_tenants_dict = {}
tierno0ea2a7e2017-10-18 00:06:26 +02005029 datacenter_tenants_dict["created"]="true"
tierno42026a02017-02-10 15:13:40 +01005030
tierno0ea2a7e2017-10-18 00:06:26 +02005031 #fill datacenter_tenants table
5032 if not vim_tenant_id_exist_atdb:
tiernod3750b32018-07-20 15:33:08 +02005033 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant
tierno0ea2a7e2017-10-18 00:06:26 +02005034 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
5035 datacenter_tenants_dict["user"] = vim_username
5036 datacenter_tenants_dict["passwd"] = vim_password
5037 datacenter_tenants_dict["datacenter_id"] = datacenter_id
tiernod3750b32018-07-20 15:33:08 +02005038 if name:
5039 datacenter_tenants_dict["name"] = name
5040 else:
5041 datacenter_tenants_dict["name"] = datacenter_name
tierno0ea2a7e2017-10-18 00:06:26 +02005042 if config:
5043 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
5044 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True, confidential_data=True)
5045 datacenter_tenants_dict["uuid"] = id_
tierno42026a02017-02-10 15:13:40 +01005046
tierno0ea2a7e2017-10-18 00:06:26 +02005047 #fill tenants_datacenters table
5048 datacenter_tenant_id = datacenter_tenants_dict["uuid"]
5049 tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id
5050 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
tiernod3750b32018-07-20 15:33:08 +02005051
tierno0ea2a7e2017-10-18 00:06:26 +02005052 # create thread
tierno0ea2a7e2017-10-18 00:06:26 +02005053 thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid'])
tiernod3750b32018-07-20 15:33:08 +02005054 new_thread = vim_thread.vim_thread(task_lock, thread_name, datacenter_name, datacenter_tenant_id,
tierno0ea2a7e2017-10-18 00:06:26 +02005055 db=db, db_lock=db_lock, ovim=ovim)
5056 new_thread.start()
5057 thread_id = datacenter_tenants_dict["uuid"]
5058 vim_threads["running"][thread_id] = new_thread
tiernod3750b32018-07-20 15:33:08 +02005059 return thread_id
tierno0ea2a7e2017-10-18 00:06:26 +02005060 except vimconn.vimconnException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005061 raise NfvoException(str(e), httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01005062
tierno99314902017-04-26 13:23:09 +02005063
tiernod3750b32018-07-20 15:33:08 +02005064def edit_vim_account(mydb, nfvo_tenant, datacenter_tenant_id, datacenter_id=None, name=None, vim_tenant=None,
5065 vim_tenant_name=None, vim_username=None, vim_password=None, config=None):
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005066
tiernod3750b32018-07-20 15:33:08 +02005067 # get vim_account; check is valid for this tenant
5068 from_ = "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id"
5069 where_ = {"td.nfvo_tenant_id": nfvo_tenant}
5070 if datacenter_tenant_id:
5071 where_["dt.uuid"] = datacenter_tenant_id
5072 if datacenter_id:
5073 where_["dt.datacenter_id"] = datacenter_id
5074 vim_accounts = mydb.get_rows(SELECT="dt.uuid as uuid, config", FROM=from_, WHERE=where_)
5075 if not vim_accounts:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005076 raise NfvoException("vim_account not found for this tenant", http_code=httperrors.Not_Found)
tiernod3750b32018-07-20 15:33:08 +02005077 elif len(vim_accounts) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005078 raise NfvoException("found more than one vim_account for this tenant", http_code=httperrors.Conflict)
tiernod3750b32018-07-20 15:33:08 +02005079 datacenter_tenant_id = vim_accounts[0]["uuid"]
5080 original_config = vim_accounts[0]["config"]
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005081
tiernod3750b32018-07-20 15:33:08 +02005082 update_ = {}
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005083 if config:
tiernod3750b32018-07-20 15:33:08 +02005084 original_config_dict = yaml.load(original_config)
5085 original_config_dict.update(config)
5086 update["config"] = yaml.safe_dump(original_config_dict, default_flow_style=True, width=256)
5087 if name:
5088 update_['name'] = name
5089 if vim_tenant:
5090 update_['vim_tenant_id'] = vim_tenant
5091 if vim_tenant_name:
5092 update_['vim_tenant_name'] = vim_tenant_name
5093 if vim_username:
5094 update_['user'] = vim_username
5095 if vim_password:
5096 update_['passwd'] = vim_password
5097 if update_:
5098 mydb.update_rows("datacenter_tenants", UPDATE=update_, WHERE={"uuid": datacenter_tenant_id})
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005099
tiernod3750b32018-07-20 15:33:08 +02005100 vim_threads["running"][datacenter_tenant_id].insert_task("reload")
5101 return datacenter_tenant_id
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005102
tiernod3750b32018-07-20 15:33:08 +02005103def delete_vim_account(mydb, tenant_id, vim_account_id, datacenter=None):
tierno7edb6752016-03-21 17:37:52 +01005104 #get nfvo_tenant info
5105 if not tenant_id or tenant_id=="any":
5106 tenant_uuid = None
5107 else:
tiernof97fd272016-07-11 14:32:37 +02005108 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
tierno7edb6752016-03-21 17:37:52 +01005109 tenant_uuid = tenant_dict['uuid']
5110
5111 #check that this association exist before
tiernod3750b32018-07-20 15:33:08 +02005112 tenants_datacenter_dict = {}
5113 if datacenter:
5114 datacenter_id, _ = get_datacenter_uuid(mydb, tenant_uuid, datacenter)
5115 tenants_datacenter_dict["datacenter_id"] = datacenter_id
5116 elif vim_account_id:
5117 tenants_datacenter_dict["datacenter_tenant_id"] = vim_account_id
tierno7edb6752016-03-21 17:37:52 +01005118 if tenant_uuid:
5119 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
tiernof97fd272016-07-11 14:32:37 +02005120 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
5121 if len(tenant_datacenter_list)==0 and tenant_uuid:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005122 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01005123
5124 #delete this association
tiernof97fd272016-07-11 14:32:37 +02005125 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
tierno7edb6752016-03-21 17:37:52 +01005126
5127 #get vim_tenant info and deletes
5128 warning=''
5129 for tenant_datacenter_item in tenant_datacenter_list:
tiernof97fd272016-07-11 14:32:37 +02005130 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
5131 #try to delete vim:tenant
5132 try:
5133 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
5134 if vim_tenant_dict['created']=='true':
tierno7edb6752016-03-21 17:37:52 +01005135 #delete tenant at VIM if created by NFVO
tierno42026a02017-02-10 15:13:40 +01005136 try:
tierno0ea2a7e2017-10-18 00:06:26 +02005137 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02005138 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
5139 except vimconn.vimconnException as e:
5140 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
5141 logger.warn(warning)
tiernof97fd272016-07-11 14:32:37 +02005142 except db_base_Exception as e:
5143 logger.error("Cannot delete datacenter_tenants " + str(e))
tierno42026a02017-02-10 15:13:40 +01005144 pass # the error will be caused because dependencies, vim_tenant can not be deleted
tierno867ffe92017-03-27 12:50:34 +02005145 thread_id = tenant_datacenter_item["datacenter_tenant_id"]
tiernoa3572692018-05-14 13:09:33 +02005146 thread = vim_threads["running"].get(thread_id)
5147 if thread:
5148 thread.insert_task("exit")
5149 vim_threads["deleting"][thread_id] = thread
tiernof97fd272016-07-11 14:32:37 +02005150 return "datacenter {} detached. {}".format(datacenter_id, warning)
tierno7edb6752016-03-21 17:37:52 +01005151
tiernob3d36742017-03-03 23:51:05 +01005152
tierno7edb6752016-03-21 17:37:52 +01005153def datacenter_action(mydb, tenant_id, datacenter, action_dict):
5154 #DEPRECATED
tierno42026a02017-02-10 15:13:40 +01005155 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005156 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005157
5158 if 'net-update' in action_dict:
tiernoae4a8d12016-07-08 12:30:39 +02005159 try:
tiernof97fd272016-07-11 14:32:37 +02005160 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
tiernoae4a8d12016-07-08 12:30:39 +02005161 #print content
5162 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005163 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005164 raise NfvoException(str(e), httperrors.Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01005165 #update nets Change from VIM format to NFVO format
5166 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02005167 for net in nets:
tierno7edb6752016-03-21 17:37:52 +01005168 net_nfvo={'datacenter_id': datacenter_id}
5169 net_nfvo['name'] = net['name']
5170 #net_nfvo['description']= net['name']
5171 net_nfvo['vim_net_id'] = net['id']
5172 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5173 net_nfvo['shared'] = net['shared']
5174 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
5175 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02005176 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
5177 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
5178 return inserted
tierno7edb6752016-03-21 17:37:52 +01005179 elif 'net-edit' in action_dict:
5180 net = action_dict['net-edit'].pop('net')
tierno42fcc3b2016-07-06 17:20:40 +02005181 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01005182 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
tierno7edb6752016-03-21 17:37:52 +01005183 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02005184 return result
tierno7edb6752016-03-21 17:37:52 +01005185 elif 'net-delete' in action_dict:
5186 net = action_dict['net-deelte'].get('net')
tierno42fcc3b2016-07-06 17:20:40 +02005187 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01005188 result = mydb.delete_row(FROM='datacenter_nets',
tierno7edb6752016-03-21 17:37:52 +01005189 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02005190 return result
tierno7edb6752016-03-21 17:37:52 +01005191
5192 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005193 raise NfvoException("Unknown action " + str(action_dict), httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01005194
tiernob3d36742017-03-03 23:51:05 +01005195
tierno7edb6752016-03-21 17:37:52 +01005196def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
5197 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005198 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005199
tierno42fcc3b2016-07-06 17:20:40 +02005200 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
tierno42026a02017-02-10 15:13:40 +01005201 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
tierno7edb6752016-03-21 17:37:52 +01005202 WHERE={'datacenter_id':datacenter_id, what: netmap})
tiernof97fd272016-07-11 14:32:37 +02005203 return result
tierno7edb6752016-03-21 17:37:52 +01005204
tiernob3d36742017-03-03 23:51:05 +01005205
tierno7edb6752016-03-21 17:37:52 +01005206def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
5207 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005208 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005209 filter_dict={}
5210 if action_dict:
5211 action_dict = action_dict["netmap"]
5212 if 'vim_id' in action_dict:
5213 filter_dict["id"] = action_dict['vim_id']
5214 if 'vim_name' in action_dict:
5215 filter_dict["name"] = action_dict['vim_name']
5216 else:
5217 filter_dict["shared"] = True
tierno42026a02017-02-10 15:13:40 +01005218
tiernoae4a8d12016-07-08 12:30:39 +02005219 try:
tiernof97fd272016-07-11 14:32:37 +02005220 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02005221 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005222 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005223 raise NfvoException(str(e), httperrors.Internal_Server_Error)
tiernof97fd272016-07-11 14:32:37 +02005224 if len(vim_nets)>1 and action_dict:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005225 raise NfvoException("more than two networks found, specify with vim_id", httperrors.Conflict)
tiernof97fd272016-07-11 14:32:37 +02005226 elif len(vim_nets)==0: # and action_dict:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005227 raise NfvoException("Not found a network at VIM with " + str(filter_dict), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01005228 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02005229 for net in vim_nets:
tierno7edb6752016-03-21 17:37:52 +01005230 net_nfvo={'datacenter_id': datacenter_id}
5231 if action_dict and "name" in action_dict:
5232 net_nfvo['name'] = action_dict['name']
5233 else:
5234 net_nfvo['name'] = net['name']
5235 #net_nfvo['description']= net['name']
5236 net_nfvo['vim_net_id'] = net['id']
5237 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5238 net_nfvo['shared'] = net['shared']
5239 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
tiernof97fd272016-07-11 14:32:37 +02005240 try:
5241 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01005242 net_nfvo["status"] = "OK"
tiernof97fd272016-07-11 14:32:37 +02005243 net_nfvo["uuid"] = net_id
5244 except db_base_Exception as e:
5245 if action_dict:
5246 raise
5247 else:
5248 net_nfvo["status"] = "FAIL: " + str(e)
tierno42026a02017-02-10 15:13:40 +01005249 net_list.append(net_nfvo)
5250 return net_list
tierno7edb6752016-03-21 17:37:52 +01005251
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005252def get_sdn_net_id(mydb, tenant_id, datacenter, network_id):
5253 # obtain all network data
5254 try:
5255 if utils.check_valid_uuid(network_id):
5256 filter_dict = {"id": network_id}
5257 else:
5258 filter_dict = {"name": network_id}
5259
5260 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
5261 network = myvim.get_network_list(filter_dict=filter_dict)
5262 except vimconn.vimconnException as e:
tiernof1ba57e2017-09-07 12:23:19 +02005263 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 +02005264
5265 # ensure the network is defined
5266 if len(network) == 0:
5267 raise NfvoException("Network {} is not present in the system".format(network_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005268 httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005269
5270 # ensure there is only one network with the provided name
5271 if len(network) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005272 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005273
5274 # ensure it is a dataplane network
5275 if network[0]['type'] != 'data':
5276 return None
5277
5278 # ensure we use the id
5279 network_id = network[0]['id']
5280
5281 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
5282 # and with instance_scenario_id==NULL
5283 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
5284 search_dict = {'vim_net_id': network_id}
5285
5286 try:
5287 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
5288 result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)
5289 except db_base_Exception as e:
5290 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
tierno9c5c8322018-03-23 15:44:03 +01005291 network_id) + str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005292
5293 sdn_net_counter = 0
5294 for net in result:
5295 if net['sdn_net_id'] != None:
5296 sdn_net_counter+=1
5297 sdn_net_id = net['sdn_net_id']
5298
5299 if sdn_net_counter == 0:
5300 return None
5301 elif sdn_net_counter == 1:
5302 return sdn_net_id
5303 else:
5304 raise NfvoException("More than one SDN network is associated to vim network {}".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005305 network_id), httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005306
5307def get_sdn_controller_id(mydb, datacenter):
5308 # Obtain sdn controller id
5309 config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}')
5310 if not config:
5311 return None
5312
5313 return yaml.load(config).get('sdn-controller')
5314
5315def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor):
5316 try:
5317 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
5318 if not sdn_network_id:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005319 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 +02005320
5321 #Obtain sdn controller id
5322 controller_id = get_sdn_controller_id(mydb, datacenter)
5323 if not controller_id:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005324 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005325
5326 #Obtain sdn controller info
5327 sdn_controller = ovim.show_of_controller(controller_id)
5328
5329 port_data = {
5330 'name': 'external_port',
5331 'net_id': sdn_network_id,
5332 'ofc_id': controller_id,
5333 'switch_dpid': sdn_controller['dpid'],
5334 'switch_port': descriptor['port']
5335 }
5336
5337 if 'vlan' in descriptor:
5338 port_data['vlan'] = descriptor['vlan']
5339 if 'mac' in descriptor:
5340 port_data['mac'] = descriptor['mac']
5341
5342 result = ovim.new_port(port_data)
5343 except ovimException as e:
5344 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005345 sdn_network_id, network_id) + str(e), httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005346 except db_base_Exception as e:
5347 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
tierno9c5c8322018-03-23 15:44:03 +01005348 network_id) + str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005349
5350 return 'Port uuid: '+ result
5351
5352def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None):
5353 if port_id:
5354 filter = {'uuid': port_id}
5355 else:
5356 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
5357 if not sdn_network_id:
5358 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005359 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005360 #in case no port_id is specified only ports marked as 'external_port' will be detached
5361 filter = {'name': 'external_port', 'net_id': sdn_network_id}
5362
5363 try:
5364 port_list = ovim.get_ports(columns={'uuid'}, filter=filter)
5365 except ovimException as e:
5366 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005367 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005368
5369 if len(port_list) == 0:
5370 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005371 httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005372
5373 port_uuid_list = []
5374 for port in port_list:
5375 try:
5376 port_uuid_list.append(port['uuid'])
5377 ovim.delete_port(port['uuid'])
5378 except ovimException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005379 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 +02005380
5381 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list))
tiernob3d36742017-03-03 23:51:05 +01005382
tierno7edb6752016-03-21 17:37:52 +01005383def vim_action_get(mydb, tenant_id, datacenter, item, name):
5384 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005385 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005386 filter_dict={}
5387 if name:
tierno42fcc3b2016-07-06 17:20:40 +02005388 if utils.check_valid_uuid(name):
tierno7edb6752016-03-21 17:37:52 +01005389 filter_dict["id"] = name
5390 else:
5391 filter_dict["name"] = name
tiernoae4a8d12016-07-08 12:30:39 +02005392 try:
5393 if item=="networks":
5394 #filter_dict['tenant_id'] = myvim['tenant_id']
5395 content = myvim.get_network_list(filter_dict=filter_dict)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005396
5397 if len(content) == 0:
5398 raise NfvoException("Network {} is not present in the system. ".format(name),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005399 httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005400
5401 #Update the networks with the attached ports
5402 for net in content:
5403 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id'])
5404 if sdn_network_id != None:
5405 try:
5406 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
5407 port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id})
5408 except ovimException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005409 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 +02005410 #Remove field name and if port name is external_port save it as 'type'
5411 for port in port_list:
5412 if port['name'] == 'external_port':
5413 port['type'] = "External"
5414 del port['name']
5415 net['sdn_network_id'] = sdn_network_id
5416 net['sdn_attached_ports'] = port_list
5417
tiernoae4a8d12016-07-08 12:30:39 +02005418 elif item=="tenants":
5419 content = myvim.get_tenant_list(filter_dict=filter_dict)
tierno4540ea52017-01-18 17:44:32 +01005420 elif item == "images":
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005421
tierno4540ea52017-01-18 17:44:32 +01005422 content = myvim.get_image_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02005423 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005424 raise NfvoException(item + "?", httperrors.Method_Not_Allowed)
tiernobe41e222016-09-02 15:16:13 +02005425 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
tiernoae4a8d12016-07-08 12:30:39 +02005426 if name and len(content)==1:
tiernof97fd272016-07-11 14:32:37 +02005427 return {item[:-1]: content[0]}
tiernoae4a8d12016-07-08 12:30:39 +02005428 elif name and len(content)==0:
tiernof97fd272016-07-11 14:32:37 +02005429 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 +02005430 datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02005431 else:
tiernof97fd272016-07-11 14:32:37 +02005432 return {item: content}
tiernoae4a8d12016-07-08 12:30:39 +02005433 except vimconn.vimconnException as e:
5434 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
tiernof97fd272016-07-11 14:32:37 +02005435 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
tierno42026a02017-02-10 15:13:40 +01005436
tiernob3d36742017-03-03 23:51:05 +01005437
tierno7edb6752016-03-21 17:37:52 +01005438def vim_action_delete(mydb, tenant_id, datacenter, item, name):
5439 #get datacenter info
tierno392f2852016-05-13 12:28:55 +02005440 if tenant_id == "any":
5441 tenant_id=None
5442
tiernoa2793912016-10-04 08:15:08 +00005443 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno392f2852016-05-13 12:28:55 +02005444 #get uuid name
tiernof97fd272016-07-11 14:32:37 +02005445 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
5446 logger.debug("vim_action_delete vim response: " + str(content))
tierno392f2852016-05-13 12:28:55 +02005447 items = content.values()[0]
5448 if type(items)==list and len(items)==0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005449 raise NfvoException("Not found " + item, httperrors.Not_Found)
tierno392f2852016-05-13 12:28:55 +02005450 elif type(items)==list and len(items)>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005451 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), httperrors.Not_Found)
tierno392f2852016-05-13 12:28:55 +02005452 else: # it is a dict
5453 item_id = items["id"]
5454 item_name = str(items.get("name"))
tierno42026a02017-02-10 15:13:40 +01005455
tiernoae4a8d12016-07-08 12:30:39 +02005456 try:
5457 if item=="networks":
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005458 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
5459 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id)
5460 if sdn_network_id != None:
5461 #Delete any port attachment to this network
5462 try:
5463 port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id})
5464 except ovimException as e:
5465 raise NfvoException(
5466 "ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005467 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005468
5469 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
5470 for port in port_list:
5471 vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid'])
5472
5473 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
5474 try:
5475 mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id})
5476 except db_base_Exception as e:
5477 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) +
tierno9c5c8322018-03-23 15:44:03 +01005478 str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005479
5480 #Delete the SDN network
5481 try:
5482 ovim.delete_network(sdn_network_id)
5483 except ovimException as e:
5484 logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True)
5485 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005486 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005487
tiernoae4a8d12016-07-08 12:30:39 +02005488 content = myvim.delete_network(item_id)
5489 elif item=="tenants":
5490 content = myvim.delete_tenant(item_id)
tierno4540ea52017-01-18 17:44:32 +01005491 elif item == "images":
5492 content = myvim.delete_image(item_id)
tiernoae4a8d12016-07-08 12:30:39 +02005493 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005494 raise NfvoException(item + "?", httperrors.Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02005495 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005496 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
5497 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005498
tiernof97fd272016-07-11 14:32:37 +02005499 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
tierno42026a02017-02-10 15:13:40 +01005500
tiernob3d36742017-03-03 23:51:05 +01005501
tierno7edb6752016-03-21 17:37:52 +01005502def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
5503 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005504 logger.debug("vim_action_create descriptor %s", str(descriptor))
tierno392f2852016-05-13 12:28:55 +02005505 if tenant_id == "any":
5506 tenant_id=None
tiernoa2793912016-10-04 08:15:08 +00005507 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02005508 try:
5509 if item=="networks":
5510 net = descriptor["network"]
5511 net_name = net.pop("name")
5512 net_type = net.pop("type", "bridge")
garciadeblas9f8456e2016-09-05 05:02:59 +02005513 net_public = net.pop("shared", False)
5514 net_ipprofile = net.pop("ip_profile", None)
tiernoa7d34d02017-02-23 14:42:07 +01005515 net_vlan = net.pop("vlan", None)
garciadeblasebd66722019-01-31 16:01:31 +00005516 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 +02005517
5518 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
5519 if get_sdn_controller_id(mydb, datacenter) != None and (net_type == 'data' or net_type == 'ptp'):
tierno00e3df72017-11-29 17:20:13 +01005520 #obtain datacenter_tenant_id
5521 datacenter_tenant_id = mydb.get_rows(SELECT=('uuid',),
5522 FROM='datacenter_tenants',
5523 WHERE={'datacenter_id': datacenter})[0]['uuid']
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005524 try:
5525 sdn_network = {}
5526 sdn_network['vlan'] = net_vlan
5527 sdn_network['type'] = net_type
5528 sdn_network['name'] = net_name
tierno00e3df72017-11-29 17:20:13 +01005529 sdn_network['region'] = datacenter_tenant_id
garciadeblasebd66722019-01-31 16:01:31 +00005530 ovim_content = ovim.new_network(sdn_network)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005531 except ovimException as e:
tierno00e3df72017-11-29 17:20:13 +01005532 logger.error("ovimException creating SDN network={} ".format(
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005533 sdn_network) + str(e), exc_info=True)
5534 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005535 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005536
5537 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
5538 # use instance_scenario_id=None to distinguish from real instaces of nets
tierno00e3df72017-11-29 17:20:13 +01005539 correspondence = {'instance_scenario_id': None,
5540 'sdn_net_id': ovim_content,
5541 'vim_net_id': content,
5542 'datacenter_tenant_id': datacenter_tenant_id
5543 }
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005544 try:
5545 mydb.new_row('instance_nets', correspondence, add_uuid=True)
5546 except db_base_Exception as e:
tierno00e3df72017-11-29 17:20:13 +01005547 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format(
tierno9c5c8322018-03-23 15:44:03 +01005548 correspondence, e), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005549 elif item=="tenants":
5550 tenant = descriptor["tenant"]
5551 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
5552 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005553 raise NfvoException(item + "?", httperrors.Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02005554 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005555 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005556
tierno7edb6752016-03-21 17:37:52 +01005557 return vim_action_get(mydb, tenant_id, datacenter, item, content)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005558
5559def sdn_controller_create(mydb, tenant_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005560 data = ovim.new_of_controller(sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005561 logger.debug('New SDN controller created with uuid {}'.format(data))
5562 return data
5563
5564def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005565 data = ovim.edit_of_controller(controller_id, sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005566 msg = 'SDN controller {} updated'.format(data)
5567 logger.debug(msg)
5568 return msg
5569
5570def sdn_controller_list(mydb, tenant_id, controller_id=None):
5571 if controller_id == None:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005572 data = ovim.get_of_controllers()
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005573 else:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005574 data = ovim.show_of_controller(controller_id)
5575
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005576 msg = 'SDN controller list:\n {}'.format(data)
5577 logger.debug(msg)
5578 return data
5579
5580def sdn_controller_delete(mydb, tenant_id, controller_id):
5581 select_ = ('uuid', 'config')
5582 datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_)
5583 for datacenter in datacenters:
5584 if datacenter['config']:
5585 config = yaml.load(datacenter['config'])
5586 if 'sdn-controller' in config and config['sdn-controller'] == controller_id:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005587 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), httperrors.Conflict)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005588
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005589 data = ovim.delete_of_controller(controller_id)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005590 msg = 'SDN controller {} deleted'.format(data)
5591 logger.debug(msg)
5592 return msg
5593
5594def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping):
5595 controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id})
5596 if len(controller) < 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005597 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), httperrors.Not_Found)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005598
5599 try:
5600 sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"]
5601 except:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005602 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), httperrors.Bad_Request)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005603
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005604 sdn_controller = ovim.show_of_controller(sdn_controller_id)
5605 switch_dpid = sdn_controller["dpid"]
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005606
5607 maps = list()
5608 for compute_node in sdn_port_mapping:
5609 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
5610 element = dict()
5611 element["compute_node"] = compute_node["compute_node"]
5612 for port in compute_node["ports"]:
tierno7f426e92018-06-28 15:21:32 +02005613 pci = port.get("pci")
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005614 element["switch_port"] = port.get("switch_port")
5615 element["switch_mac"] = port.get("switch_mac")
tierno4070e442019-01-23 10:19:23 +00005616 if not element["switch_port"] and not element["switch_mac"]:
5617 raise NfvoException ("The mapping must contain 'switch_port' or 'switch_mac'", httperrors.Bad_Request)
tierno7f426e92018-06-28 15:21:32 +02005618 for pci_expanded in utils.expand_brackets(pci):
5619 element["pci"] = pci_expanded
5620 maps.append(dict(element))
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005621
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005622 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 +01005623
5624def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005625 maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id})
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005626
5627 result = {
5628 "sdn-controller": None,
5629 "datacenter-id": datacenter_id,
5630 "dpid": None,
5631 "ports_mapping": list()
5632 }
5633
5634 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id)
5635 if datacenter['config']:
5636 config = yaml.load(datacenter['config'])
5637 if 'sdn-controller' in config:
5638 controller_id = config['sdn-controller']
5639 sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id)
5640 result["sdn-controller"] = controller_id
5641 result["dpid"] = sdn_controller["dpid"]
5642
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005643 if result["sdn-controller"] == None:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005644 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005645 if result["dpid"] == None:
5646 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005647 httperrors.Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005648
5649 if len(maps) == 0:
5650 return result
5651
5652 ports_correspondence_dict = dict()
5653 for link in maps:
5654 if result["sdn-controller"] != link["ofc_id"]:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005655 raise NfvoException("The sdn-controller specified for different port mappings differ", httperrors.Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005656 if result["dpid"] != link["switch_dpid"]:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005657 raise NfvoException("The dpid specified for different port mappings differ", httperrors.Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005658 element = dict()
5659 element["pci"] = link["pci"]
5660 if link["switch_port"]:
5661 element["switch_port"] = link["switch_port"]
5662 if link["switch_mac"]:
5663 element["switch_mac"] = link["switch_mac"]
5664
5665 if not link["compute_node"] in ports_correspondence_dict:
5666 content = dict()
5667 content["compute_node"] = link["compute_node"]
5668 content["ports"] = list()
5669 ports_correspondence_dict[link["compute_node"]] = content
5670
5671 ports_correspondence_dict[link["compute_node"]]["ports"].append(element)
5672
5673 for key in sorted(ports_correspondence_dict):
5674 result["ports_mapping"].append(ports_correspondence_dict[key])
5675
5676 return result
5677
5678def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id):
tierno639520f2017-04-05 19:55:36 +02005679 return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id})
gcalvinoe580c7d2017-09-22 14:09:51 +02005680
5681def create_RO_keypair(tenant_id):
5682 """
5683 Creates a public / private keys for a RO tenant and returns their values
5684 Params:
5685 tenant_id: ID of the tenant
5686 Return:
5687 public_key: Public key for the RO tenant
5688 private_key: Encrypted private key for RO tenant
5689 """
5690
5691 bits = 2048
5692 key = RSA.generate(bits)
5693 try:
5694 public_key = key.publickey().exportKey('OpenSSH')
5695 if isinstance(public_key, ValueError):
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005696 raise NfvoException("Unable to create public key: {}".format(public_key), httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02005697 private_key = key.exportKey(passphrase=tenant_id, pkcs=8)
5698 except (ValueError, NameError) as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005699 raise NfvoException("Unable to create private key: {}".format(e), httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02005700 return public_key, private_key
5701
5702def decrypt_key (key, tenant_id):
5703 """
5704 Decrypts an encrypted RSA key
5705 Params:
5706 key: Private key to be decrypted
5707 tenant_id: ID of the tenant
5708 Return:
5709 unencrypted_key: Unencrypted private key for RO tenant
5710 """
5711 try:
5712 key = RSA.importKey(key,tenant_id)
5713 unencrypted_key = key.exportKey('PEM')
5714 if isinstance(unencrypted_key, ValueError):
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005715 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key), httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02005716 except ValueError as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005717 raise NfvoException("Unable to decrypt the private key: {}".format(e), httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02005718 return unencrypted_key