blob: d433ac96c386bfad62c8cc8598c946ac9b559214 [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
tiernob3d36742017-03-03 23:51:05 +0100129 name = datacenter_name[:16] + "." + tenant_name[:16]
tierno42026a02017-02-10 15:13:40 +0100130 if name not in vim_threads["names"]:
131 vim_threads["names"].append(name)
132 return name
133 name = datacenter_id + "-" + tenant_id
134 vim_threads["names"].append(name)
135 return name
136
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100137# -- Move
138def get_non_used_wim_name(wim_name, wim_id, tenant_name, tenant_id):
139 name = wim_name[:16]
140 if name not in wim_threads["names"]:
141 wim_threads["names"].append(name)
142 return name
143 name = wim_name[:16] + "." + tenant_name[:16]
144 if name not in wim_threads["names"]:
145 wim_threads["names"].append(name)
146 return name
147 name = wim_id + "-" + tenant_id
148 wim_threads["names"].append(name)
149 return name
tierno42026a02017-02-10 15:13:40 +0100150
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100151
152def start_service(mydb, persistence=None, wim=None):
tiernob3d36742017-03-03 23:51:05 +0100153 global db, global_config
Anderson Bravalheridfed5112019-02-08 01:44:14 +0000154 db = nfvo_db.nfvo_db(lock=db_lock)
155 mydb.lock = db_lock
tiernob3d36742017-03-03 23:51:05 +0100156 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 +0100157 global ovim
158
Anderson Bravalheridfed5112019-02-08 01:44:14 +0000159 persistence = persistence or WimPersistence(db)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100160
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100161 # Initialize openvim for SDN control
162 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
163 # TODO: review ovim.py to delete not needed configuration
164 ovim_configuration = {
tierno639520f2017-04-05 19:55:36 +0200165 'logger_name': 'openmano.ovim',
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100166 'network_vlan_range_start': 1000,
167 'network_vlan_range_end': 4096,
tierno639520f2017-04-05 19:55:36 +0200168 'db_name': global_config["db_ovim_name"],
169 'db_host': global_config["db_ovim_host"],
170 'db_user': global_config["db_ovim_user"],
171 'db_passwd': global_config["db_ovim_passwd"],
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100172 'bridge_ifaces': {},
173 'mode': 'normal',
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100174 'network_type': 'bridge',
175 #TODO: log_level_of should not be needed. To be modified in ovim
176 'log_level_of': 'DEBUG'
177 }
tierno42026a02017-02-10 15:13:40 +0100178 try:
tierno3fcfdb72017-10-24 07:48:24 +0200179 # starts ovim library
tierno46df9672017-05-26 13:12:21 +0200180 ovim = ovim_module.ovim(ovim_configuration)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100181
182 global wim_engine
183 wim_engine = wim or WimEngine(persistence)
184 wim_engine.ovim = ovim
185
tierno46df9672017-05-26 13:12:21 +0200186 ovim.start_service()
187
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100188 #delete old unneeded vim_wim_actions
tierno3fcfdb72017-10-24 07:48:24 +0200189 clean_db(mydb)
190
191 # starts vim_threads
tierno46df9672017-05-26 13:12:21 +0200192 from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
193 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
194 select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
195 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
196 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
197 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
tierno42026a02017-02-10 15:13:40 +0100198 vims = mydb.get_rows(FROM=from_, SELECT=select_)
199 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200200 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
201 'datacenter_id': vim.get('datacenter_id')}
tierno42026a02017-02-10 15:13:40 +0100202 if vim["config"]:
203 extra.update(yaml.load(vim["config"]))
204 if vim.get('dt_config'):
205 extra.update(yaml.load(vim["dt_config"]))
206 if vim["type"] not in vimconn_imported:
207 module_info=None
208 try:
209 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200210 pkg = __import__("osm_ro." + module)
211 vim_conn = getattr(pkg, module)
212 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
213 # vim_conn = imp.load_module(vim["type"], *module_info)
tierno42026a02017-02-10 15:13:40 +0100214 vimconn_imported[vim["type"]] = vim_conn
215 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200216 # if module_info and module_info[0]:
217 # file.close(module_info[0])
tiernocdee8cc2017-04-25 13:42:06 +0200218 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100219 vim["type"], module, type(e).__name__, str(e)), httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100220
tierno867ffe92017-03-27 12:50:34 +0200221 thread_id = vim['datacenter_tenant_id']
tiernob3d36742017-03-03 23:51:05 +0100222 vim_persistent_info[thread_id] = {}
tierno42026a02017-02-10 15:13:40 +0100223 try:
224 #if not tenant:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100225 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
tierno42026a02017-02-10 15:13:40 +0100226 myvim = vimconn_imported[ vim["type"] ].vimconnector(
tiernob3d36742017-03-03 23:51:05 +0100227 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
228 tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'],
229 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
230 user=vim['user'], passwd=vim['passwd'],
231 config=extra, persistent_info=vim_persistent_info[thread_id]
232 )
tierno9c22f2d2017-10-09 16:23:55 +0200233 except vimconn.vimconnException as e:
234 myvim = e
235 logger.error("Cannot launch thread for VIM {} '{}': {}".format(vim['datacenter_name'],
236 vim['datacenter_id'], e))
tierno42026a02017-02-10 15:13:40 +0100237 except Exception as e:
tierno46df9672017-05-26 13:12:21 +0200238 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100239 httperrors.Internal_Server_Error)
tierno46df9672017-05-26 13:12:21 +0200240 thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'],
241 vim['vim_tenant_id'])
tiernod3750b32018-07-20 15:33:08 +0200242 new_thread = vim_thread.vim_thread(task_lock, thread_name, vim['datacenter_name'],
tierno867ffe92017-03-27 12:50:34 +0200243 vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim)
tierno42026a02017-02-10 15:13:40 +0100244 new_thread.start()
tierno42026a02017-02-10 15:13:40 +0100245 vim_threads["running"][thread_id] = new_thread
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100246
247 wim_engine.start_threads()
tierno42026a02017-02-10 15:13:40 +0100248 except db_base_Exception as e:
249 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
tierno46df9672017-05-26 13:12:21 +0200250 except ovim_module.ovimException as e:
251 message = str(e)
252 if message[:22] == "DATABASE wrong version":
253 message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
254 "at host {dbhost}".format(
255 msg=message[22:-3], dbname=global_config["db_ovim_name"],
256 dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"],
257 ver=message[-3:-1], dbhost=global_config["db_ovim_host"])
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100258 raise NfvoException(message, httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100259
tierno867ffe92017-03-27 12:50:34 +0200260
tierno42026a02017-02-10 15:13:40 +0100261def stop_service():
tiernoc5651792017-03-27 10:50:43 +0200262 global ovim, global_config
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100263 if ovim:
264 ovim.stop_service()
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100265 for thread_id, thread in vim_threads["running"].items():
tierno868220c2017-09-26 00:11:05 +0200266 thread.insert_task("exit")
tierno42026a02017-02-10 15:13:40 +0100267 vim_threads["deleting"][thread_id] = thread
tiernob3d36742017-03-03 23:51:05 +0100268 vim_threads["running"] = {}
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100269
270 if wim_engine:
271 wim_engine.stop_threads()
272
tiernoc5651792017-03-27 10:50:43 +0200273 if global_config and global_config.get("console_thread"):
274 for thread in global_config["console_thread"]:
275 thread.terminate = True
tiernob3d36742017-03-03 23:51:05 +0100276
tierno6ddeded2017-05-16 15:40:26 +0200277def get_version():
278 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"],
279 global_config["version_date"] ))
280
tierno3fcfdb72017-10-24 07:48:24 +0200281def clean_db(mydb):
282 """
283 Clean unused or old entries at database to avoid unlimited growing
284 :param mydb: database connector
285 :return: None
286 """
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100287 # get and delete unused vim_wim_actions: all elements deleted, one week before, instance not present
tierno3fcfdb72017-10-24 07:48:24 +0200288 now = t.time()-3600*24*7
289 instance_action_id = None
290 nb_deleted = 0
291 while True:
292 actions_to_delete = mydb.get_rows(
293 SELECT=("item", "item_id", "instance_action_id"),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100294 FROM="vim_wim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid "
tierno3fcfdb72017-10-24 07:48:24 +0200295 "left join instance_scenarios as i on ia.instance_id=i.uuid",
296 WHERE={"va.action": "DELETE", "va.modified_at<": now, "i.uuid": None,
297 "va.status": ("DONE", "SUPERSEDED")},
298 LIMIT=100
299 )
300 for to_delete in actions_to_delete:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100301 mydb.delete_row(FROM="vim_wim_actions", WHERE=to_delete)
tierno3fcfdb72017-10-24 07:48:24 +0200302 if instance_action_id != to_delete["instance_action_id"]:
303 instance_action_id = to_delete["instance_action_id"]
304 mydb.delete_row(FROM="instance_actions", WHERE={"uuid": instance_action_id})
305 nb_deleted += len(actions_to_delete)
306 if len(actions_to_delete) < 100:
307 break
tierno3c44e7b2019-03-04 17:32:01 +0000308 # clean locks
309 mydb.update_rows("vim_wim_actions", UPDATE={"worker": None}, WHERE={"worker<>": None})
310
tierno3fcfdb72017-10-24 07:48:24 +0200311 if nb_deleted:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100312 logger.debug("Removed {} unused vim_wim_actions".format(nb_deleted))
tierno3fcfdb72017-10-24 07:48:24 +0200313
tierno42026a02017-02-10 15:13:40 +0100314
tierno7edb6752016-03-21 17:37:52 +0100315def get_flavorlist(mydb, vnf_id, nfvo_tenant=None):
316 '''Obtain flavorList
317 return result, content:
318 <0, error_text upon error
319 nb_records, flavor_list on success
320 '''
321 WHERE_dict={}
322 WHERE_dict['vnf_id'] = vnf_id
323 if nfvo_tenant is not None:
324 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
tierno42026a02017-02-10 15:13:40 +0100325
tierno7edb6752016-03-21 17:37:52 +0100326 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
327 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
tiernof97fd272016-07-11 14:32:37 +0200328 flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict )
329 #print "get_flavor_list result:", result
330 #print "get_flavor_list content:", content
tierno7edb6752016-03-21 17:37:52 +0100331 flavorList=[]
tiernof97fd272016-07-11 14:32:37 +0200332 for flavor in flavors:
tierno7edb6752016-03-21 17:37:52 +0100333 flavorList.append(flavor['flavor_id'])
tiernof97fd272016-07-11 14:32:37 +0200334 return flavorList
tierno7edb6752016-03-21 17:37:52 +0100335
tiernob3d36742017-03-03 23:51:05 +0100336
tierno7edb6752016-03-21 17:37:52 +0100337def get_imagelist(mydb, vnf_id, nfvo_tenant=None):
tierno16e3dd42018-04-24 12:52:40 +0200338 """
339 Get used images of all vms belonging to this VNFD
340 :param mydb: database conector
341 :param vnf_id: vnfd uuid
342 :param nfvo_tenant: tenant, not used
343 :return: The list of image uuid used
344 """
345 image_list = []
346 vms = mydb.get_rows(SELECT=('image_id','image_list'), FROM='vms', WHERE={'vnf_id': vnf_id})
347 for vm in vms:
tierno89aada42018-12-19 16:00:25 +0000348 if vm["image_id"] and vm["image_id"] not in image_list:
tierno16e3dd42018-04-24 12:52:40 +0200349 image_list.append(vm["image_id"])
350 if vm["image_list"]:
351 vm_image_list = yaml.load(vm["image_list"])
352 for image_dict in vm_image_list:
353 if image_dict["image_id"] not in image_list:
354 image_list.append(image_dict["image_id"])
355 return image_list
tierno7edb6752016-03-21 17:37:52 +0100356
tiernob3d36742017-03-03 23:51:05 +0100357
tiernoa2793912016-10-04 08:15:08 +0000358def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None,
tiernocbb52052018-05-31 18:57:30 +0200359 vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None, ignore_errors=False):
tierno7edb6752016-03-21 17:37:52 +0100360 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
tierno42026a02017-02-10 15:13:40 +0100361 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
tierno7edb6752016-03-21 17:37:52 +0100362 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
tiernobe41e222016-09-02 15:16:13 +0200363 raise exception upon error
tierno7edb6752016-03-21 17:37:52 +0100364 '''
365 WHERE_dict={}
366 if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
367 if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id
tiernoa2793912016-10-04 08:15:08 +0000368 if datacenter_tenant_id is not None: WHERE_dict['datacenter_tenant_id'] = datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +0100369 if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name
370 if vim_tenant is not None: WHERE_dict['dt.vim_tenant_id'] = vim_tenant
tiernoa2793912016-10-04 08:15:08 +0000371 if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name
372 if nfvo_tenant or vim_tenant or vim_tenant_name or datacenter_tenant_id:
tierno7edb6752016-03-21 17:37:52 +0100373 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 +0000374 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 +0100375 '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 +0000376 'user','passwd', 'dt.config as dt_config')
tierno7edb6752016-03-21 17:37:52 +0100377 else:
378 from_ = 'datacenters as d'
379 select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
tiernof97fd272016-07-11 14:32:37 +0200380 try:
381 vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
382 vim_dict={}
383 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200384 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
tierno16e3dd42018-04-24 12:52:40 +0200385 'datacenter_id': vim.get('datacenter_id'),
tiernob6434212018-04-26 16:27:47 +0200386 '_vim_type_internal': vim.get('type')}
tierno8008c3a2016-10-13 15:34:28 +0000387 if vim["config"]:
tiernof97fd272016-07-11 14:32:37 +0200388 extra.update(yaml.load(vim["config"]))
tierno8008c3a2016-10-13 15:34:28 +0000389 if vim.get('dt_config'):
390 extra.update(yaml.load(vim["dt_config"]))
tiernof97fd272016-07-11 14:32:37 +0200391 if vim["type"] not in vimconn_imported:
392 module_info=None
393 try:
394 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200395 pkg = __import__("osm_ro." + module)
396 vim_conn = getattr(pkg, module)
397 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
398 # vim_conn = imp.load_module(vim["type"], *module_info)
tiernof97fd272016-07-11 14:32:37 +0200399 vimconn_imported[vim["type"]] = vim_conn
400 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200401 # if module_info and module_info[0]:
402 # file.close(module_info[0])
tiernocbb52052018-05-31 18:57:30 +0200403 if ignore_errors:
404 logger.error("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
405 vim["type"], module, type(e).__name__, str(e)))
406 continue
tiernof97fd272016-07-11 14:32:37 +0200407 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100408 vim["type"], module, type(e).__name__, str(e)), httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100409
tierno7edb6752016-03-21 17:37:52 +0100410 try:
tierno867ffe92017-03-27 12:50:34 +0200411 if 'datacenter_tenant_id' in vim:
412 thread_id = vim["datacenter_tenant_id"]
tiernob3d36742017-03-03 23:51:05 +0100413 if thread_id not in vim_persistent_info:
414 vim_persistent_info[thread_id] = {}
415 persistent_info = vim_persistent_info[thread_id]
416 else:
417 persistent_info = {}
tiernof97fd272016-07-11 14:32:37 +0200418 #if not tenant:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100419 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
tiernof97fd272016-07-11 14:32:37 +0200420 vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector(
421 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
tiernob3d36742017-03-03 23:51:05 +0100422 tenant_id=vim.get('vim_tenant_id',vim_tenant),
423 tenant_name=vim.get('vim_tenant_name',vim_tenant_name),
tierno42026a02017-02-10 15:13:40 +0100424 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
tierno3ae39742016-09-07 12:17:51 +0200425 user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd),
tiernob3d36742017-03-03 23:51:05 +0100426 config=extra, persistent_info=persistent_info
tiernof97fd272016-07-11 14:32:37 +0200427 )
428 except Exception as e:
tiernocbb52052018-05-31 18:57:30 +0200429 if ignore_errors:
430 logger.error("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)))
431 continue
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100432 http_code = httperrors.Internal_Server_Error
tiernoa3572692018-05-14 13:09:33 +0200433 if isinstance(e, vimconn.vimconnException):
434 http_code = e.http_code
435 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), http_code)
tiernof97fd272016-07-11 14:32:37 +0200436 return vim_dict
437 except db_base_Exception as e:
438 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
tierno42026a02017-02-10 15:13:40 +0100439
tiernob3d36742017-03-03 23:51:05 +0100440
tierno7edb6752016-03-21 17:37:52 +0100441def rollback(mydb, vims, rollback_list):
442 undeleted_items=[]
tierno42026a02017-02-10 15:13:40 +0100443 #delete things by reverse order
tierno7edb6752016-03-21 17:37:52 +0100444 for i in range(len(rollback_list)-1, -1, -1):
445 item = rollback_list[i]
446 if item["where"]=="vim":
447 if item["vim_id"] not in vims:
448 continue
tierno56d73d22017-08-02 13:53:02 +0200449 if is_task_id(item["uuid"]):
450 continue
451 vim = vims[item["vim_id"]]
tiernoae4a8d12016-07-08 12:30:39 +0200452 try:
453 if item["what"]=="image":
454 vim.delete_image(item["uuid"])
tierno868220c2017-09-26 00:11:05 +0200455 mydb.delete_row(FROM="datacenters_images", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200456 elif item["what"]=="flavor":
457 vim.delete_flavor(item["uuid"])
tiernoad6bdd42018-01-10 10:43:46 +0100458 mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200459 elif item["what"]=="network":
460 vim.delete_network(item["uuid"])
461 elif item["what"]=="vm":
462 vim.delete_vminstance(item["uuid"])
463 except vimconn.vimconnException as e:
464 logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e))
465 undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"]))
tiernof97fd272016-07-11 14:32:37 +0200466 except db_base_Exception as e:
467 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 +0100468
tierno7edb6752016-03-21 17:37:52 +0100469 else: # where==mano
tiernof97fd272016-07-11 14:32:37 +0200470 try:
471 if item["what"]=="image":
472 mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]})
473 elif item["what"]=="flavor":
474 mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]})
475 except db_base_Exception as e:
476 logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e))
477 undeleted_items.append("{} '{}'".format(item['what'], item["uuid"]))
tierno42026a02017-02-10 15:13:40 +0100478 if len(undeleted_items)==0:
tierno7edb6752016-03-21 17:37:52 +0100479 return True," Rollback successful."
480 else:
481 return False," Rollback fails to delete: " + str(undeleted_items)
tierno42026a02017-02-10 15:13:40 +0100482
tiernob3d36742017-03-03 23:51:05 +0100483
tiernoafed5f12017-01-26 17:57:43 +0100484def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1):
tierno7edb6752016-03-21 17:37:52 +0100485 global global_config
tierno42026a02017-02-10 15:13:40 +0100486 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
tierno7edb6752016-03-21 17:37:52 +0100487 vnfc_interfaces={}
488 for vnfc in vnf_descriptor["vnf"]["VNFC"]:
tiernoafed5f12017-01-26 17:57:43 +0100489 name_dict = {}
tierno7edb6752016-03-21 17:37:52 +0100490 #dataplane interfaces
491 for numa in vnfc.get("numas",() ):
492 for interface in numa.get("interfaces",()):
tiernoafed5f12017-01-26 17:57:43 +0100493 if interface["name"] in name_dict:
494 raise NfvoException(
495 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
496 vnfc["name"], interface["name"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100497 httperrors.Bad_Request)
tiernoafed5f12017-01-26 17:57:43 +0100498 name_dict[ interface["name"] ] = "underlay"
tierno7edb6752016-03-21 17:37:52 +0100499 #bridge interfaces
500 for interface in vnfc.get("bridge-ifaces",() ):
tiernoafed5f12017-01-26 17:57:43 +0100501 if interface["name"] in name_dict:
502 raise NfvoException(
503 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
504 vnfc["name"], interface["name"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100505 httperrors.Bad_Request)
tiernoafed5f12017-01-26 17:57:43 +0100506 name_dict[ interface["name"] ] = "overlay"
507 vnfc_interfaces[ vnfc["name"] ] = name_dict
tierno36c0b172017-01-12 18:32:28 +0100508 # check bood-data info
tierno40e1bce2017-08-09 09:12:04 +0200509 # if "boot-data" in vnfc:
510 # # check that user-data is incompatible with users and config-files
511 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
512 # raise NfvoException(
513 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100514 # httperrors.Bad_Request)
tierno36c0b172017-01-12 18:32:28 +0100515
tierno7edb6752016-03-21 17:37:52 +0100516 #check if the info in external_connections matches with the one in the vnfcs
517 name_list=[]
518 for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
519 if external_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100520 raise NfvoException(
521 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
522 external_connection["name"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100523 httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100524 name_list.append(external_connection["name"])
525 if external_connection["VNFC"] not in vnfc_interfaces:
tiernoafed5f12017-01-26 17:57:43 +0100526 raise NfvoException(
527 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
528 external_connection["name"], external_connection["VNFC"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100529 httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100530
tierno7edb6752016-03-21 17:37:52 +0100531 if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
tiernoafed5f12017-01-26 17:57:43 +0100532 raise NfvoException(
533 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
534 external_connection["name"],
535 external_connection["local_iface_name"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100536 httperrors.Bad_Request )
tierno42026a02017-02-10 15:13:40 +0100537
tierno7edb6752016-03-21 17:37:52 +0100538 #check if the info in internal_connections matches with the one in the vnfcs
539 name_list=[]
540 for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
541 if internal_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100542 raise NfvoException(
543 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
544 internal_connection["name"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100545 httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100546 name_list.append(internal_connection["name"])
547 #We should check that internal-connections of type "ptp" have only 2 elements
tiernoafed5f12017-01-26 17:57:43 +0100548
549 if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"):
550 raise NfvoException(
551 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
552 internal_connection["name"],
553 'ptp' if vnf_descriptor_version==1 else 'e-line',
554 'data' if vnf_descriptor_version==1 else "e-lan"),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100555 httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100556 for port in internal_connection["elements"]:
tiernoafed5f12017-01-26 17:57:43 +0100557 vnf = port["VNFC"]
558 iface = port["local_iface_name"]
559 if vnf not in vnfc_interfaces:
560 raise NfvoException(
561 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
562 internal_connection["name"], vnf),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100563 httperrors.Bad_Request)
tiernoafed5f12017-01-26 17:57:43 +0100564 if iface not in vnfc_interfaces[ vnf ]:
565 raise NfvoException(
566 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
567 internal_connection["name"], iface),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100568 httperrors.Bad_Request)
569 return -httperrors.Bad_Request,
tiernoafed5f12017-01-26 17:57:43 +0100570 if vnf_descriptor_version==1 and "type" not in internal_connection:
571 if vnfc_interfaces[vnf][iface] == "overlay":
572 internal_connection["type"] = "bridge"
573 else:
574 internal_connection["type"] = "data"
575 if vnf_descriptor_version==2 and "implementation" not in internal_connection:
576 if vnfc_interfaces[vnf][iface] == "overlay":
577 internal_connection["implementation"] = "overlay"
578 else:
579 internal_connection["implementation"] = "underlay"
580 if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \
581 internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay":
582 raise NfvoException(
583 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
584 internal_connection["name"],
585 iface, 'bridge' if vnf_descriptor_version==1 else 'overlay',
586 'data' if vnf_descriptor_version==1 else 'underlay'),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100587 httperrors.Bad_Request)
tiernoafed5f12017-01-26 17:57:43 +0100588 if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \
589 vnfc_interfaces[vnf][iface] == "underlay":
590 raise NfvoException(
591 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
592 internal_connection["name"], iface,
593 'data' if vnf_descriptor_version==1 else 'underlay',
594 'bridge' if vnf_descriptor_version==1 else 'overlay'),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100595 httperrors.Bad_Request)
tiernoafed5f12017-01-26 17:57:43 +0100596
tierno7edb6752016-03-21 17:37:52 +0100597
tierno56d73d22017-08-02 13:53:02 +0200598def 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 +0100599 #look if image exist
600 if only_create_at_vim:
601 image_mano_id = image_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000602 if return_on_error == None:
603 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100604 else:
garciadeblas14480452017-01-10 13:08:07 +0100605 if image_dict['location']:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200606 images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
607 else:
608 images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']})
tiernof97fd272016-07-11 14:32:37 +0200609 if len(images)>=1:
610 image_mano_id = images[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100611 else:
garciadeblas14480452017-01-10 13:08:07 +0100612 #create image in MANO DB
tierno7edb6752016-03-21 17:37:52 +0100613 temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None),
garciadeblasb69fa9f2016-09-28 12:04:10 +0200614 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None),
615 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum']
tierno7edb6752016-03-21 17:37:52 +0100616 }
garciadeblas14480452017-01-10 13:08:07 +0100617 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
tiernof97fd272016-07-11 14:32:37 +0200618 image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True)
619 rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100620 #create image at every vim
621 for vim_id,vim in vims.iteritems():
tierno868220c2017-09-26 00:11:05 +0200622 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
tierno7edb6752016-03-21 17:37:52 +0100623 image_created="false"
624 #look at database
tierno868220c2017-09-26 00:11:05 +0200625 image_db = mydb.get_rows(FROM="datacenters_images",
626 WHERE={'datacenter_vim_id': datacenter_vim_id, 'image_id': image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100627 #look at VIM if this image exist
tiernoae4a8d12016-07-08 12:30:39 +0200628 try:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200629 if image_dict['location'] is not None:
630 image_vim_id = vim.get_image_id_from_path(image_dict['location'])
631 else:
garciadeblas30833382017-01-09 09:46:31 +0100632 filter_dict = {}
633 filter_dict['name'] = image_dict['universal_name']
634 if image_dict.get('checksum') != None:
635 filter_dict['checksum'] = image_dict['checksum']
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000636 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200637 vim_images = vim.get_image_list(filter_dict)
garciadeblas14480452017-01-10 13:08:07 +0100638 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200639 if len(vim_images) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100640 raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), httperrors.Conflict)
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000641 elif len(vim_images) == 0:
garciadeblas3fa2c052017-01-05 12:00:08 +0100642 raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict)))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200643 else:
garciadeblas14480452017-01-10 13:08:07 +0100644 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
645 image_vim_id = vim_images[0]['id']
garciadeblasb69fa9f2016-09-28 12:04:10 +0200646
tiernoae4a8d12016-07-08 12:30:39 +0200647 except vimconn.vimconnNotFoundException as e:
garciadeblas14480452017-01-10 13:08:07 +0100648 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
tierno42026a02017-02-10 15:13:40 +0100649 try:
garciadeblas14480452017-01-10 13:08:07 +0100650 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
651 if image_dict['location']:
652 image_vim_id = vim.new_image(image_dict)
653 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
654 image_created="true"
655 else:
garciadeblasb6153a22017-02-06 15:38:33 +0100656 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
657 raise vimconn.vimconnException(str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200658 except vimconn.vimconnException as e:
659 if return_on_error:
garciadeblas14480452017-01-10 13:08:07 +0100660 logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200661 raise
tierno5e91eb82016-10-04 09:39:07 +0000662 image_vim_id = None
garciadeblas14480452017-01-10 13:08:07 +0100663 logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200664 continue
665 except vimconn.vimconnException as e:
tierno5e91eb82016-10-04 09:39:07 +0000666 if return_on_error:
667 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
668 raise
garciadeblasb69fa9f2016-09-28 12:04:10 +0200669 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
tierno5e91eb82016-10-04 09:39:07 +0000670 image_vim_id = None
garciadeblas30833382017-01-09 09:46:31 +0100671 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200672 #if we reach here, the image has been created or existed
tiernof97fd272016-07-11 14:32:37 +0200673 if len(image_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100674 #add new vim_id at datacenters_images
tierno868220c2017-09-26 00:11:05 +0200675 mydb.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id,
676 'image_id':image_mano_id,
677 'vim_id': image_vim_id,
678 'created':image_created})
tierno7edb6752016-03-21 17:37:52 +0100679 elif image_db[0]["vim_id"]!=image_vim_id:
680 #modify existing vim_id at datacenters_images
tierno868220c2017-09-26 00:11:05 +0200681 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 +0100682
tiernof97fd272016-07-11 14:32:37 +0200683 return image_vim_id if only_create_at_vim else image_mano_id
tierno7edb6752016-03-21 17:37:52 +0100684
tiernob3d36742017-03-03 23:51:05 +0100685
tierno5e91eb82016-10-04 09:39:07 +0000686def 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 +0100687 temp_flavor_dict= {'disk':flavor_dict.get('disk',0),
tierno7edb6752016-03-21 17:37:52 +0100688 'ram':flavor_dict.get('ram'),
689 'vcpus':flavor_dict.get('vcpus'),
690 }
691 if 'extended' in flavor_dict and flavor_dict['extended']==None:
692 del flavor_dict['extended']
693 if 'extended' in flavor_dict:
694 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
695
696 #look if flavor exist
697 if only_create_at_vim:
698 flavor_mano_id = flavor_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000699 if return_on_error == None:
700 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100701 else:
tiernof97fd272016-07-11 14:32:37 +0200702 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
703 if len(flavors)>=1:
704 flavor_mano_id = flavors[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100705 else:
706 #create flavor
707 #create one by one the images of aditional disks
708 dev_image_list=[] #list of images
709 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
710 dev_nb=0
711 for device in flavor_dict['extended'].get('devices',[]):
garciadeblas41f18be2016-10-04 09:09:58 +0200712 if "image" not in device and "image name" not in device:
tierno7edb6752016-03-21 17:37:52 +0100713 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200714 image_dict={}
715 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
716 image_dict['universal_name']=device.get('image name')
717 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
718 image_dict['location']=device.get('image')
garciadeblas14480452017-01-10 13:08:07 +0100719 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200720 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100721 image_metadata_dict = device.get('image metadata', None)
722 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100723 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100724 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
725 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200726 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
727 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
tierno7edb6752016-03-21 17:37:52 +0100728 dev_image_list.append(image_id)
tierno42026a02017-02-10 15:13:40 +0100729 dev_nb += 1
tierno7edb6752016-03-21 17:37:52 +0100730 temp_flavor_dict['name'] = flavor_dict['name']
731 temp_flavor_dict['description'] = flavor_dict.get('description',None)
tiernof97fd272016-07-11 14:32:37 +0200732 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
733 flavor_mano_id= content
734 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100735 #create flavor at every vim
736 if 'uuid' in flavor_dict:
737 del flavor_dict['uuid']
738 flavor_vim_id=None
739 for vim_id,vim in vims.items():
tierno868220c2017-09-26 00:11:05 +0200740 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
tierno7edb6752016-03-21 17:37:52 +0100741 flavor_created="false"
742 #look at database
tierno868220c2017-09-26 00:11:05 +0200743 flavor_db = mydb.get_rows(FROM="datacenters_flavors",
744 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100745 #look at VIM if this flavor exist SKIPPED
746 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
747 #if res_vim < 0:
748 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
749 # continue
750 #elif res_vim==0:
tierno42026a02017-02-10 15:13:40 +0100751
tiernof1ba57e2017-09-07 12:23:19 +0200752 # Create the flavor in VIM
753 # Translate images at devices from MANO id to VIM id
montesmoreno0c8def02016-12-22 12:16:23 +0000754 disk_list = []
tierno7edb6752016-03-21 17:37:52 +0100755 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
tiernof1ba57e2017-09-07 12:23:19 +0200756 # make a copy of original devices
tierno7edb6752016-03-21 17:37:52 +0100757 devices_original=[]
montesmoreno0c8def02016-12-22 12:16:23 +0000758
tierno7edb6752016-03-21 17:37:52 +0100759 for device in flavor_dict["extended"].get("devices",[]):
760 dev={}
761 dev.update(device)
762 devices_original.append(dev)
763 if 'image' in device:
764 del device['image']
765 if 'image metadata' in device:
766 del device['image metadata']
tiernof1ba57e2017-09-07 12:23:19 +0200767 if 'image checksum' in device:
768 del device['image checksum']
769 dev_nb = 0
tierno7edb6752016-03-21 17:37:52 +0100770 for index in range(0,len(devices_original)) :
771 device=devices_original[index]
montesmoreno0c8def02016-12-22 12:16:23 +0000772 if "image" not in device and "image name" not in device:
tiernoecc68392018-09-06 13:47:11 +0200773 # if 'size' in device:
774 disk_list.append({'size': device.get('size', default_volume_size), 'name': device.get('name')})
tierno7edb6752016-03-21 17:37:52 +0100775 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200776 image_dict={}
777 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
778 image_dict['universal_name']=device.get('image name')
779 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
780 image_dict['location']=device.get('image')
tiernof1ba57e2017-09-07 12:23:19 +0200781 # image_dict['new_location']=device.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200782 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100783 image_metadata_dict = device.get('image metadata', None)
784 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100785 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100786 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
787 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200788 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 +0100789 image_dict["uuid"]=image_mano_id
tiernof97fd272016-07-11 14:32:37 +0200790 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 +0000791
792 #save disk information (image must be based on and size
793 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
794
tierno7edb6752016-03-21 17:37:52 +0100795 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
796 dev_nb += 1
tiernof97fd272016-07-11 14:32:37 +0200797 if len(flavor_db)>0:
tierno7edb6752016-03-21 17:37:52 +0100798 #check that this vim_id exist in VIM, if not create
799 flavor_vim_id=flavor_db[0]["vim_id"]
tiernoae4a8d12016-07-08 12:30:39 +0200800 try:
801 vim.get_flavor(flavor_vim_id)
802 continue #flavor exist
803 except vimconn.vimconnException:
804 pass
tierno7edb6752016-03-21 17:37:52 +0100805 #create flavor at vim
tiernoae4a8d12016-07-08 12:30:39 +0200806 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
807 try:
tiernocf157a82017-01-30 14:07:06 +0100808 flavor_vim_id = None
809 flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict)
810 flavor_create="false"
811 except vimconn.vimconnException as e:
812 pass
813 try:
814 if not flavor_vim_id:
815 flavor_vim_id = vim.new_flavor(flavor_dict)
816 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
817 flavor_created="true"
tiernoae4a8d12016-07-08 12:30:39 +0200818 except vimconn.vimconnException as e:
819 if return_on_error:
820 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200821 raise
tiernoae4a8d12016-07-08 12:30:39 +0200822 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tierno5e91eb82016-10-04 09:39:07 +0000823 flavor_vim_id = None
tiernoae4a8d12016-07-08 12:30:39 +0200824 continue
tierno7edb6752016-03-21 17:37:52 +0100825 #if reach here the flavor has been create or exist
tiernof97fd272016-07-11 14:32:37 +0200826 if len(flavor_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100827 #add new vim_id at datacenters_flavors
montesmoreno0c8def02016-12-22 12:16:23 +0000828 extended_devices_yaml = None
829 if len(disk_list) > 0:
830 extended_devices = dict()
831 extended_devices['disks'] = disk_list
832 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
833 mydb.new_row('datacenters_flavors',
tierno868220c2017-09-26 00:11:05 +0200834 {'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id, 'vim_id': flavor_vim_id,
835 'created': flavor_created, 'extended': extended_devices_yaml})
tierno7edb6752016-03-21 17:37:52 +0100836 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
837 #modify existing vim_id at datacenters_flavors
tierno868220c2017-09-26 00:11:05 +0200838 mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id},
839 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
tierno42026a02017-02-10 15:13:40 +0100840
tiernof97fd272016-07-11 14:32:37 +0200841 return flavor_vim_id if only_create_at_vim else flavor_mano_id
tierno7edb6752016-03-21 17:37:52 +0100842
tiernob3d36742017-03-03 23:51:05 +0100843
tiernof1ba57e2017-09-07 12:23:19 +0200844def get_str(obj, field, length):
845 """
846 Obtain the str value,
847 :param obj:
848 :param length:
849 :return:
850 """
851 value = obj.get(field)
852 if value is not None:
853 value = str(value)[:length]
854 return value
855
856def _lookfor_or_create_image(db_image, mydb, descriptor):
857 """
858 fill image content at db_image dictionary. Check if the image with this image and checksum exist
859 :param db_image: dictionary to insert data
860 :param mydb: database connector
861 :param descriptor: yang descriptor
862 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
863 """
864
865 db_image["name"] = get_str(descriptor, "image", 255)
866 db_image["checksum"] = get_str(descriptor, "image-checksum", 32)
867 if not db_image["checksum"]: # Ensure that if empty string, None is stored
868 db_image["checksum"] = None
869 if db_image["name"].startswith("/"):
870 db_image["location"] = db_image["name"]
871 existing_images = mydb.get_rows(FROM="images", WHERE={'location': db_image["location"]})
872 else:
873 db_image["universal_name"] = db_image["name"]
874 existing_images = mydb.get_rows(FROM="images", WHERE={'universal_name': db_image['universal_name'],
875 'checksum': db_image['checksum']})
876 if existing_images:
877 return existing_images[0]["uuid"]
878 else:
879 image_uuid = str(uuid4())
880 db_image["uuid"] = image_uuid
881 return None
882
883def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
884 """
885 Parses an OSM IM vnfd_catalog and insert at DB
886 :param mydb:
887 :param tenant_id:
888 :param vnf_descriptor:
889 :return: The list of cretated vnf ids
890 """
891 try:
892 myvnfd = vnfd_catalog.vnfd()
tiernoa9550202017-09-22 13:31:35 +0200893 try:
tiernof6bbe222019-04-09 14:19:40 +0000894 pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd, path_helper=True,
895 skip_unknown=True)
tiernoa9550202017-09-22 13:31:35 +0200896 except Exception as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100897 raise NfvoException("Error. Invalid VNF descriptor format " + str(e), httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +0200898 db_vnfs = []
899 db_nets = []
900 db_vms = []
901 db_vms_index = 0
902 db_interfaces = []
903 db_images = []
904 db_flavors = []
tierno41a69812018-02-16 14:34:33 +0100905 db_ip_profiles_index = 0
906 db_ip_profiles = []
tiernof1ba57e2017-09-07 12:23:19 +0200907 uuid_list = []
908 vnfd_uuid_list = []
tiernoe18ba432017-10-12 10:22:45 +0200909 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd:vnfd-catalog")
910 if not vnfd_catalog_descriptor:
911 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd-catalog")
912 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd")
913 if not vnfd_descriptor_list:
914 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd:vnfd")
tiernob2880eb2017-10-04 15:04:53 +0200915 for vnfd_yang in myvnfd.vnfd_catalog.vnfd.itervalues():
916 vnfd = vnfd_yang.get()
tiernof1ba57e2017-09-07 12:23:19 +0200917
918 # table vnf
919 vnf_uuid = str(uuid4())
920 uuid_list.append(vnf_uuid)
921 vnfd_uuid_list.append(vnf_uuid)
tierno66eba6e2017-11-10 17:09:18 +0100922 vnfd_id = get_str(vnfd, "id", 255)
tiernof1ba57e2017-09-07 12:23:19 +0200923 db_vnf = {
924 "uuid": vnf_uuid,
tierno66eba6e2017-11-10 17:09:18 +0100925 "osm_id": vnfd_id,
tiernof1ba57e2017-09-07 12:23:19 +0200926 "name": get_str(vnfd, "name", 255),
927 "description": get_str(vnfd, "description", 255),
928 "tenant_id": tenant_id,
929 "vendor": get_str(vnfd, "vendor", 255),
930 "short_name": get_str(vnfd, "short-name", 255),
931 "descriptor": str(vnf_descriptor)[:60000]
932 }
933
tiernoe18ba432017-10-12 10:22:45 +0200934 for vnfd_descriptor in vnfd_descriptor_list:
935 if vnfd_descriptor["id"] == str(vnfd["id"]):
936 break
937
tierno41a69812018-02-16 14:34:33 +0100938 # table ip_profiles (ip-profiles)
939 ip_profile_name2db_table_index = {}
940 for ip_profile in vnfd.get("ip-profiles").itervalues():
941 db_ip_profile = {
942 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
943 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
944 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
945 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
946 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
947 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
948 }
949 dns_list = []
950 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
951 dns_list.append(str(dns.get("address")))
952 db_ip_profile["dns_address"] = ";".join(dns_list)
953 if ip_profile["ip-profile-params"].get('security-group'):
954 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
955 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
956 db_ip_profiles_index += 1
957 db_ip_profiles.append(db_ip_profile)
958
tiernof1ba57e2017-09-07 12:23:19 +0200959 # table nets (internal-vld)
960 net_id2uuid = {} # for mapping interface with network
961 for vld in vnfd.get("internal-vld").itervalues():
962 net_uuid = str(uuid4())
963 uuid_list.append(net_uuid)
964 db_net = {
965 "name": get_str(vld, "name", 255),
966 "vnf_id": vnf_uuid,
967 "uuid": net_uuid,
968 "description": get_str(vld, "description", 255),
tierno1df468d2018-07-06 14:25:16 +0200969 "osm_id": get_str(vld, "id", 255),
tiernof1ba57e2017-09-07 12:23:19 +0200970 "type": "bridge", # TODO adjust depending on connection point type
971 }
972 net_id2uuid[vld.get("id")] = net_uuid
973 db_nets.append(db_net)
tierno41a69812018-02-16 14:34:33 +0100974 # ip-profile, link db_ip_profile with db_sce_net
975 if vld.get("ip-profile-ref"):
976 ip_profile_name = vld.get("ip-profile-ref")
977 if ip_profile_name not in ip_profile_name2db_table_index:
978 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':"
979 "'{}'. Reference to a non-existing 'ip_profiles'".format(
980 str(vnfd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100981 httperrors.Bad_Request)
tierno41a69812018-02-16 14:34:33 +0100982 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["net_id"] = net_uuid
983 else: #check no ip-address has been defined
tierno45140f52018-03-26 12:11:46 +0200984 for icp in vld.get("internal-connection-point").itervalues():
tierno41a69812018-02-16 14:34:33 +0100985 if icp.get("ip-address"):
986 raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' "
987 "contains an ip-address but no ip-profile has been defined at VLD".format(
988 str(vnfd["id"]), str(vld["id"]), str(icp["id"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +0100989 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +0200990
tiernocf596692017-11-20 15:47:51 +0100991 # connection points vaiable declaration
992 cp_name2iface_uuid = {}
993 cp_name2vm_uuid = {}
994 cp_name2db_interface = {}
tiernob6990792018-11-13 10:37:42 +0100995 vdu_id2cp_name = {} # stored only when one external connection point is presented at this VDU
tiernocf596692017-11-20 15:47:51 +0100996
tiernof1ba57e2017-09-07 12:23:19 +0200997 # table vms (vdus)
998 vdu_id2uuid = {}
999 vdu_id2db_table_index = {}
1000 for vdu in vnfd.get("vdu").itervalues():
tierno41a69812018-02-16 14:34:33 +01001001
1002 for vdu_descriptor in vnfd_descriptor["vdu"]:
1003 if vdu_descriptor["id"] == str(vdu["id"]):
1004 break
tiernof1ba57e2017-09-07 12:23:19 +02001005 vm_uuid = str(uuid4())
1006 uuid_list.append(vm_uuid)
tierno66eba6e2017-11-10 17:09:18 +01001007 vdu_id = get_str(vdu, "id", 255)
tiernof1ba57e2017-09-07 12:23:19 +02001008 db_vm = {
1009 "uuid": vm_uuid,
tierno66eba6e2017-11-10 17:09:18 +01001010 "osm_id": vdu_id,
tiernof1ba57e2017-09-07 12:23:19 +02001011 "name": get_str(vdu, "name", 255),
1012 "description": get_str(vdu, "description", 255),
tiernob6990792018-11-13 10:37:42 +01001013 "pdu_type": get_str(vdu, "pdu-type", 255),
tiernof1ba57e2017-09-07 12:23:19 +02001014 "vnf_id": vnf_uuid,
1015 }
1016 vdu_id2uuid[db_vm["osm_id"]] = vm_uuid
1017 vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index
1018 if vdu.get("count"):
1019 db_vm["count"] = int(vdu["count"])
1020
1021 # table image
1022 image_present = False
1023 if vdu.get("image"):
1024 image_present = True
1025 db_image = {}
1026 image_uuid = _lookfor_or_create_image(db_image, mydb, vdu)
1027 if not image_uuid:
1028 image_uuid = db_image["uuid"]
1029 db_images.append(db_image)
1030 db_vm["image_id"] = image_uuid
tierno16e3dd42018-04-24 12:52:40 +02001031 if vdu.get("alternative-images"):
1032 vm_alternative_images = []
1033 for alt_image in vdu.get("alternative-images").itervalues():
1034 db_image = {}
1035 image_uuid = _lookfor_or_create_image(db_image, mydb, alt_image)
1036 if not image_uuid:
1037 image_uuid = db_image["uuid"]
1038 db_images.append(db_image)
1039 vm_alternative_images.append({
1040 "image_id": image_uuid,
1041 "vim_type": str(alt_image["vim-type"]),
1042 # "universal_name": str(alt_image["image"]),
1043 # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None
1044 })
1045
1046 db_vm["image_list"] = yaml.safe_dump(vm_alternative_images, default_flow_style=True, width=256)
tiernof1ba57e2017-09-07 12:23:19 +02001047
1048 # volumes
1049 devices = []
1050 if vdu.get("volumes"):
tierno1df468d2018-07-06 14:25:16 +02001051 for volume_key in vdu["volumes"]:
tiernof1ba57e2017-09-07 12:23:19 +02001052 volume = vdu["volumes"][volume_key]
1053 if not image_present:
1054 # Convert the first volume to vnfc.image
1055 image_present = True
1056 db_image = {}
1057 image_uuid = _lookfor_or_create_image(db_image, mydb, volume)
1058 if not image_uuid:
1059 image_uuid = db_image["uuid"]
1060 db_images.append(db_image)
1061 db_vm["image_id"] = image_uuid
1062 else:
1063 # Add Openmano devices
tierno1df468d2018-07-06 14:25:16 +02001064 device = {"name": str(volume.get("name"))}
tiernof1ba57e2017-09-07 12:23:19 +02001065 device["type"] = str(volume.get("device-type"))
1066 if volume.get("size"):
1067 device["size"] = int(volume["size"])
1068 if volume.get("image"):
1069 device["image name"] = str(volume["image"])
1070 if volume.get("image-checksum"):
1071 device["image checksum"] = str(volume["image-checksum"])
tierno1df468d2018-07-06 14:25:16 +02001072
tiernof1ba57e2017-09-07 12:23:19 +02001073 devices.append(device)
1074
tierno89aada42018-12-19 16:00:25 +00001075 if not db_vm.get("image_id"):
1076 if not db_vm["pdu_type"]:
1077 raise NfvoException("Not defined image for VDU")
1078 # create a fake image
1079
tierno66eba6e2017-11-10 17:09:18 +01001080 # cloud-init
1081 boot_data = {}
1082 if vdu.get("cloud-init"):
1083 boot_data["user-data"] = str(vdu["cloud-init"])
1084 elif vdu.get("cloud-init-file"):
1085 # TODO Where this file content is present???
1086 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
1087 boot_data["user-data"] = str(vdu["cloud-init-file"])
1088
1089 if vdu.get("supplemental-boot-data"):
1090 if vdu["supplemental-boot-data"].get('boot-data-drive'):
1091 boot_data['boot-data-drive'] = True
1092 if vdu["supplemental-boot-data"].get('config-file'):
1093 om_cfgfile_list = list()
1094 for custom_config_file in vdu["supplemental-boot-data"]['config-file'].itervalues():
1095 # TODO Where this file content is present???
1096 cfg_source = str(custom_config_file["source"])
1097 om_cfgfile_list.append({"dest": custom_config_file["dest"],
1098 "content": cfg_source})
1099 boot_data['config-files'] = om_cfgfile_list
1100 if boot_data:
1101 db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256)
1102
1103 db_vms.append(db_vm)
1104 db_vms_index += 1
1105
1106 # table interfaces (internal/external interfaces)
1107 flavor_epa_interfaces = []
tierno66eba6e2017-11-10 17:09:18 +01001108 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
1109 for iface in vdu.get("interface").itervalues():
1110 flavor_epa_interface = {}
1111 iface_uuid = str(uuid4())
1112 uuid_list.append(iface_uuid)
1113 db_interface = {
1114 "uuid": iface_uuid,
1115 "internal_name": get_str(iface, "name", 255),
1116 "vm_id": vm_uuid,
1117 }
1118 flavor_epa_interface["name"] = db_interface["internal_name"]
1119 if iface.get("virtual-interface").get("vpci"):
1120 db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12)
1121 flavor_epa_interface["vpci"] = db_interface["vpci"]
1122
1123 if iface.get("virtual-interface").get("bandwidth"):
1124 bps = int(iface.get("virtual-interface").get("bandwidth"))
1125 db_interface["bw"] = int(math.ceil(bps/1000000.0))
1126 flavor_epa_interface["bandwidth"] = "{} Mbps".format(db_interface["bw"])
1127
1128 if iface.get("virtual-interface").get("type") == "OM-MGMT":
1129 db_interface["type"] = "mgmt"
garciadeblas31e141b2018-10-25 18:33:19 +02001130 elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
tierno66eba6e2017-11-10 17:09:18 +01001131 db_interface["type"] = "bridge"
1132 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1133 elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1134 db_interface["type"] = "data"
1135 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1136 flavor_epa_interface["dedicated"] = "no" if iface["virtual-interface"]["type"] == "SR-IOV" \
1137 else "yes"
1138 flavor_epa_interfaces.append(flavor_epa_interface)
1139 else:
1140 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1141 "-interface':'type':'{}'. Interface type is not supported".format(
1142 vnfd_id, vdu_id, iface.get("virtual-interface").get("type")),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001143 httperrors.Bad_Request)
tierno66eba6e2017-11-10 17:09:18 +01001144
tiernoe72710b2018-07-23 16:16:00 +02001145 if iface.get("mgmt-interface"):
1146 db_interface["type"] = "mgmt"
1147
tierno66eba6e2017-11-10 17:09:18 +01001148 if iface.get("external-connection-point-ref"):
1149 try:
1150 cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")]
1151 db_interface["external_name"] = get_str(cp, "name", 255)
1152 cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid
1153 cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid
1154 cp_name2db_interface[db_interface["external_name"]] = db_interface
1155 for cp_descriptor in vnfd_descriptor["connection-point"]:
1156 if cp_descriptor["name"] == db_interface["external_name"]:
1157 break
1158 else:
1159 raise KeyError()
1160
1161 if vdu_id in vdu_id2cp_name:
1162 vdu_id2cp_name[vdu_id] = None # more than two connecdtion point for this VDU
1163 else:
1164 vdu_id2cp_name[vdu_id] = db_interface["external_name"]
1165
1166 # port security
1167 if str(cp_descriptor.get("port-security-enabled")).lower() == "false":
1168 db_interface["port_security"] = 0
1169 elif str(cp_descriptor.get("port-security-enabled")).lower() == "true":
1170 db_interface["port_security"] = 1
1171 except KeyError:
1172 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1173 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1174 " at connection-point".format(
1175 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
1176 cp=iface.get("vnfd-connection-point-ref")),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001177 httperrors.Bad_Request)
tierno66eba6e2017-11-10 17:09:18 +01001178 elif iface.get("internal-connection-point-ref"):
1179 try:
tierno41a69812018-02-16 14:34:33 +01001180 for icp_descriptor in vdu_descriptor["internal-connection-point"]:
1181 if icp_descriptor["id"] == str(iface.get("internal-connection-point-ref")):
1182 break
1183 else:
1184 raise KeyError("does not exist at vdu:internal-connection-point")
1185 icp = None
1186 icp_vld = None
tierno66eba6e2017-11-10 17:09:18 +01001187 for vld in vnfd.get("internal-vld").itervalues():
1188 for cp in vld.get("internal-connection-point").itervalues():
1189 if cp.get("id-ref") == iface.get("internal-connection-point-ref"):
tierno41a69812018-02-16 14:34:33 +01001190 if icp:
1191 raise KeyError("is referenced by more than one 'internal-vld'")
1192 icp = cp
1193 icp_vld = vld
1194 if not icp:
1195 raise KeyError("is not referenced by any 'internal-vld'")
1196
1197 db_interface["net_id"] = net_id2uuid[icp_vld.get("id")]
1198 if str(icp_descriptor.get("port-security-enabled")).lower() == "false":
1199 db_interface["port_security"] = 0
1200 elif str(icp_descriptor.get("port-security-enabled")).lower() == "true":
1201 db_interface["port_security"] = 1
1202 if icp.get("ip-address"):
1203 if not icp_vld.get("ip-profile-ref"):
1204 raise NfvoException
1205 db_interface["ip_address"] = str(icp.get("ip-address"))
1206 except KeyError as e:
tierno66eba6e2017-11-10 17:09:18 +01001207 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
tierno41a69812018-02-16 14:34:33 +01001208 "'interface[{iface}]':'internal-connection-point-ref':'{cp}'"
1209 " {msg}".format(
tierno66eba6e2017-11-10 17:09:18 +01001210 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
tierno41a69812018-02-16 14:34:33 +01001211 cp=iface.get("internal-connection-point-ref"), msg=str(e)),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001212 httperrors.Bad_Request)
tierno55d234c2018-07-04 18:29:21 +02001213 if iface.get("position"):
1214 db_interface["created_at"] = int(iface.get("position")) * 50
tierno41a69812018-02-16 14:34:33 +01001215 if iface.get("mac-address"):
1216 db_interface["mac"] = str(iface.get("mac-address"))
tierno66eba6e2017-11-10 17:09:18 +01001217 db_interfaces.append(db_interface)
1218
tiernof1ba57e2017-09-07 12:23:19 +02001219 # table flavors
1220 db_flavor = {
1221 "name": get_str(vdu, "name", 250) + "-flv",
1222 "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)),
1223 "ram": int(vdu["vm-flavor"].get("memory-mb", 1)),
garciadeblas79d1a1a2017-12-11 16:07:07 +01001224 "disk": int(vdu["vm-flavor"].get("storage-gb", 0)),
tiernof1ba57e2017-09-07 12:23:19 +02001225 }
tiernocf596692017-11-20 15:47:51 +01001226 # TODO revise the case of several numa-node-policy node
tiernof1ba57e2017-09-07 12:23:19 +02001227 extended = {}
1228 numa = {}
1229 if devices:
1230 extended["devices"] = devices
tierno66eba6e2017-11-10 17:09:18 +01001231 if flavor_epa_interfaces:
1232 numa["interfaces"] = flavor_epa_interfaces
tiernof1ba57e2017-09-07 12:23:19 +02001233 if vdu.get("guest-epa"): # TODO or dedicated_int:
1234 epa_vcpu_set = False
1235 if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
1236 numa_node_policy = vdu["guest-epa"].get("numa-node-policy")
1237 if numa_node_policy.get("node"):
tiernocf596692017-11-20 15:47:51 +01001238 numa_node = numa_node_policy["node"].values()[0]
tiernof1ba57e2017-09-07 12:23:19 +02001239 if numa_node.get("num-cores"):
1240 numa["cores"] = numa_node["num-cores"]
1241 epa_vcpu_set = True
1242 if numa_node.get("paired-threads"):
1243 if numa_node["paired-threads"].get("num-paired-threads"):
tierno39dddcc2017-10-05 18:48:06 +02001244 numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"])
tiernof1ba57e2017-09-07 12:23:19 +02001245 epa_vcpu_set = True
tierno39dddcc2017-10-05 18:48:06 +02001246 if len(numa_node["paired-threads"].get("paired-thread-ids")):
tiernof1ba57e2017-09-07 12:23:19 +02001247 numa["paired-threads-id"] = []
tierno39dddcc2017-10-05 18:48:06 +02001248 for pair in numa_node["paired-threads"]["paired-thread-ids"].itervalues():
tiernof1ba57e2017-09-07 12:23:19 +02001249 numa["paired-threads-id"].append(
1250 (str(pair["thread-a"]), str(pair["thread-b"]))
1251 )
1252 if numa_node.get("num-threads"):
tierno39dddcc2017-10-05 18:48:06 +02001253 numa["threads"] = int(numa_node["num-threads"])
tiernof1ba57e2017-09-07 12:23:19 +02001254 epa_vcpu_set = True
1255 if numa_node.get("memory-mb"):
1256 numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1)
1257 if vdu["guest-epa"].get("mempage-size"):
1258 if vdu["guest-epa"]["mempage-size"] != "SMALL":
1259 numa["memory"] = max(int(db_flavor["ram"] / 1024), 1)
1260 if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set:
1261 if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
1262 if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \
1263 vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
1264 numa["cores"] = max(db_flavor["vcpus"], 1)
1265 else:
1266 numa["threads"] = max(db_flavor["vcpus"], 1)
1267 if numa:
1268 extended["numas"] = [numa]
1269 if extended:
1270 extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256)
1271 db_flavor["extended"] = extended_text
1272 # look if flavor exist
garciadeblas79d1a1a2017-12-11 16:07:07 +01001273 temp_flavor_dict = {'disk': db_flavor.get('disk', 0),
tiernof1ba57e2017-09-07 12:23:19 +02001274 'ram': db_flavor.get('ram'),
1275 'vcpus': db_flavor.get('vcpus'),
1276 'extended': db_flavor.get('extended')
1277 }
1278 existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
1279 if existing_flavors:
1280 flavor_uuid = existing_flavors[0]["uuid"]
1281 else:
1282 flavor_uuid = str(uuid4())
1283 uuid_list.append(flavor_uuid)
1284 db_flavor["uuid"] = flavor_uuid
1285 db_flavors.append(db_flavor)
1286 db_vm["flavor_id"] = flavor_uuid
1287
tiernof1ba57e2017-09-07 12:23:19 +02001288 # VNF affinity and antiaffinity
1289 for pg in vnfd.get("placement-groups").itervalues():
1290 pg_name = get_str(pg, "name", 255)
1291 for vdu in pg.get("member-vdus").itervalues():
1292 vdu_id = get_str(vdu, "member-vdu-ref", 255)
1293 if vdu_id not in vdu_id2db_table_index:
tiernob2880eb2017-10-04 15:04:53 +02001294 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1295 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
tierno66eba6e2017-11-10 17:09:18 +01001296 vnf=vnfd_id, pg=pg_name, vdu=vdu_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001297 httperrors.Bad_Request)
tierno55fe3972019-03-29 08:50:12 +00001298 db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name
tiernof1ba57e2017-09-07 12:23:19 +02001299 # TODO consider the case of isolation and not colocation
1300 # if pg.get("strategy") == "ISOLATION":
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001301
tiernof1ba57e2017-09-07 12:23:19 +02001302 # VNF mgmt configuration
1303 mgmt_access = {}
1304 if vnfd["mgmt-interface"].get("vdu-id"):
tierno66eba6e2017-11-10 17:09:18 +01001305 mgmt_vdu_id = get_str(vnfd["mgmt-interface"], "vdu-id", 255)
1306 if mgmt_vdu_id not in vdu_id2uuid:
tiernob2880eb2017-10-04 15:04:53 +02001307 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1308 "'{vdu}'. Reference to a non-existing vdu".format(
tierno66eba6e2017-11-10 17:09:18 +01001309 vnf=vnfd_id, vdu=mgmt_vdu_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001310 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001311 mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]]
tierno66eba6e2017-11-10 17:09:18 +01001312 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
1313 if vdu_id2cp_name.get(mgmt_vdu_id):
tiernob6990792018-11-13 10:37:42 +01001314 if cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]:
1315 cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt"
tierno66eba6e2017-11-10 17:09:18 +01001316
tiernof1ba57e2017-09-07 12:23:19 +02001317 if vnfd["mgmt-interface"].get("ip-address"):
1318 mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address"))
1319 if vnfd["mgmt-interface"].get("cp"):
1320 if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid:
tiernob6990792018-11-13 10:37:42 +01001321 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp'['{cp}']. "
tiernob2880eb2017-10-04 15:04:53 +02001322 "Reference to a non-existing connection-point".format(
tierno66eba6e2017-11-10 17:09:18 +01001323 vnf=vnfd_id, cp=vnfd["mgmt-interface"]["cp"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001324 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001325 mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]]
1326 mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]]
tiernoe2ff1ce2017-11-02 17:01:10 +01001327 # mark this interface as of type mgmt
tiernob6990792018-11-13 10:37:42 +01001328 if cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]:
1329 cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt"
tiernoe2ff1ce2017-11-02 17:01:10 +01001330
tiernoa9550202017-09-22 13:31:35 +02001331 default_user = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
tiernof1ba57e2017-09-07 12:23:19 +02001332 "default-user", 64)
gcalvinoe580c7d2017-09-22 14:09:51 +02001333
tiernof1ba57e2017-09-07 12:23:19 +02001334 if default_user:
1335 mgmt_access["default_user"] = default_user
gcalvinoe580c7d2017-09-22 14:09:51 +02001336 required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1337 "required", 6)
1338 if required:
1339 mgmt_access["required"] = required
1340
tiernof1ba57e2017-09-07 12:23:19 +02001341 if mgmt_access:
1342 db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256)
1343
1344 db_vnfs.append(db_vnf)
1345 db_tables=[
1346 {"vnfs": db_vnfs},
1347 {"nets": db_nets},
1348 {"images": db_images},
1349 {"flavors": db_flavors},
tierno41a69812018-02-16 14:34:33 +01001350 {"ip_profiles": db_ip_profiles},
tiernof1ba57e2017-09-07 12:23:19 +02001351 {"vms": db_vms},
1352 {"interfaces": db_interfaces},
1353 ]
1354
1355 logger.debug("create_vnf Deployment done vnfDict: %s",
1356 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
1357 mydb.new_rows(db_tables, uuid_list)
1358 return vnfd_uuid_list
tiernob2880eb2017-10-04 15:04:53 +02001359 except NfvoException:
1360 raise
tiernof1ba57e2017-09-07 12:23:19 +02001361 except Exception as e:
1362 logger.error("Exception {}".format(e))
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001363 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001364
1365
tiernob8569aa2018-08-24 11:34:54 +02001366@deprecated("Use new_vnfd_v3")
tierno7edb6752016-03-21 17:37:52 +01001367def new_vnf(mydb, tenant_id, vnf_descriptor):
1368 global global_config
tierno42026a02017-02-10 15:13:40 +01001369
tierno7edb6752016-03-21 17:37:52 +01001370 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +01001371 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
tierno7edb6752016-03-21 17:37:52 +01001372 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +01001373 vims = {}
tierno7edb6752016-03-21 17:37:52 +01001374 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001375 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001376 if "tenant_id" in vnf_descriptor["vnf"]:
1377 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001378 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 +01001379 httperrors.Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001380 else:
1381 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1382 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +01001383 if global_config["auto_push_VNF_to_VIMs"]:
tiernocbb52052018-05-31 18:57:30 +02001384 vims = get_vim(mydb, tenant_id, ignore_errors=True)
tierno7edb6752016-03-21 17:37:52 +01001385
1386 # Step 4. Review the descriptor and add missing fields
1387 #print vnf_descriptor
tiernof97fd272016-07-11 14:32:37 +02001388 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
tierno7edb6752016-03-21 17:37:52 +01001389 vnf_name = vnf_descriptor['vnf']['name']
1390 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1391 if "physical" in vnf_descriptor['vnf']:
1392 del vnf_descriptor['vnf']['physical']
1393 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +01001394
tierno42026a02017-02-10 15:13:40 +01001395 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
tiernof97fd272016-07-11 14:32:37 +02001396 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1397 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +01001398
tierno7edb6752016-03-21 17:37:52 +01001399 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1400 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1401 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
tierno7edb6752016-03-21 17:37:52 +01001402 try:
tiernof97fd272016-07-11 14:32:37 +02001403 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +01001404 for vnfc in vnf_descriptor['vnf']['VNFC']:
1405 VNFCitem={}
1406 VNFCitem["name"] = vnfc['name']
mirabal29356312017-07-27 12:21:22 +02001407 VNFCitem["availability_zone"] = vnfc.get('availability_zone')
tierno7edb6752016-03-21 17:37:52 +01001408 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +01001409
tiernof97fd272016-07-11 14:32:37 +02001410 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +01001411
tierno7edb6752016-03-21 17:37:52 +01001412 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +02001413 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 +01001414 myflavorDict["description"] = VNFCitem["description"]
1415 myflavorDict["ram"] = vnfc.get("ram", 0)
1416 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
garciadeblas79d1a1a2017-12-11 16:07:07 +01001417 myflavorDict["disk"] = vnfc.get("disk", 0)
tierno7edb6752016-03-21 17:37:52 +01001418 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +01001419
tierno7edb6752016-03-21 17:37:52 +01001420 devices = vnfc.get("devices")
1421 if devices != None:
1422 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +01001423
tierno7edb6752016-03-21 17:37:52 +01001424 # TODO:
1425 # 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 +01001426 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1427
tierno7edb6752016-03-21 17:37:52 +01001428 # Previous code has been commented
1429 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1430 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1431 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1432 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1433 #else:
1434 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1435 # if result2:
1436 # print "Error creating flavor: unknown processor model. Rollback successful."
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001437 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
tierno7edb6752016-03-21 17:37:52 +01001438 # else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001439 # 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 +01001440 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +01001441
tierno7edb6752016-03-21 17:37:52 +01001442 if 'numas' in vnfc and len(vnfc['numas'])>0:
1443 myflavorDict['extended']['numas'] = vnfc['numas']
1444
1445 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +01001446
tierno7edb6752016-03-21 17:37:52 +01001447 # Step 6.2 New flavors are created in the VIM
tiernof97fd272016-07-11 14:32:37 +02001448 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
tierno7edb6752016-03-21 17:37:52 +01001449
tiernof97fd272016-07-11 14:32:37 +02001450 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
tierno7edb6752016-03-21 17:37:52 +01001451 VNFCitem["flavor_id"] = flavor_id
1452 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +01001453
tiernof97fd272016-07-11 14:32:37 +02001454 logger.debug("Creating new images in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +01001455 # Step 6.3 New images are created in the VIM
1456 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +01001457 #This "for" loop might be integrated with the previous one
tierno7edb6752016-03-21 17:37:52 +01001458 #In case this integration is made, the VNFCDict might become a VNFClist.
1459 for vnfc in vnf_descriptor['vnf']['VNFC']:
tiernof97fd272016-07-11 14:32:37 +02001460 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +02001461 image_dict={}
1462 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1463 image_dict['universal_name']=vnfc.get('image name')
1464 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1465 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +01001466 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +02001467 image_dict['checksum']=vnfc.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +01001468 image_metadata_dict = vnfc.get('image metadata', None)
1469 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +01001470 if image_metadata_dict is not None:
tierno7edb6752016-03-21 17:37:52 +01001471 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1472 image_dict['metadata']=image_metadata_str
1473 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
tiernof97fd272016-07-11 14:32:37 +02001474 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1475 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
tierno7edb6752016-03-21 17:37:52 +01001476 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +02001477 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno8e690322017-08-10 15:58:50 +02001478 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
tierno36c0b172017-01-12 18:32:28 +01001479 if vnfc.get("boot-data"):
1480 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
tierno7edb6752016-03-21 17:37:52 +01001481
tierno42026a02017-02-10 15:13:40 +01001482
tiernof97fd272016-07-11 14:32:37 +02001483 # Step 7. Storing the VNF descriptor in the repository
1484 if "descriptor" not in vnf_descriptor["vnf"]:
1485 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001486
tiernof97fd272016-07-11 14:32:37 +02001487 # Step 8. Adding the VNF to the NFVO DB
1488 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1489 return vnf_id
1490 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
tierno7edb6752016-03-21 17:37:52 +01001491 _, message = rollback(mydb, vims, rollback_list)
tiernof97fd272016-07-11 14:32:37 +02001492 if isinstance(e, db_base_Exception):
1493 error_text = "Exception at database"
1494 elif isinstance(e, KeyError):
1495 error_text = "KeyError exception "
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001496 e.http_code = httperrors.Internal_Server_Error
tiernof97fd272016-07-11 14:32:37 +02001497 else:
1498 error_text = "Exception at VIM"
1499 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1500 #logger.error("start_scenario %s", error_text)
1501 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01001502
tiernob3d36742017-03-03 23:51:05 +01001503
tiernob8569aa2018-08-24 11:34:54 +02001504@deprecated("Use new_vnfd_v3")
garciadeblas9f8456e2016-09-05 05:02:59 +02001505def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
1506 global global_config
tierno42026a02017-02-10 15:13:40 +01001507
garciadeblas9f8456e2016-09-05 05:02:59 +02001508 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +01001509 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
garciadeblas9f8456e2016-09-05 05:02:59 +02001510 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +01001511 vims = {}
garciadeblas9f8456e2016-09-05 05:02:59 +02001512 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001513 check_tenant(mydb, tenant_id)
garciadeblas9f8456e2016-09-05 05:02:59 +02001514 if "tenant_id" in vnf_descriptor["vnf"]:
1515 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
1516 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 +01001517 httperrors.Unauthorized)
garciadeblas9f8456e2016-09-05 05:02:59 +02001518 else:
1519 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1520 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +01001521 if global_config["auto_push_VNF_to_VIMs"]:
tiernocbb52052018-05-31 18:57:30 +02001522 vims = get_vim(mydb, tenant_id, ignore_errors=True)
garciadeblas9f8456e2016-09-05 05:02:59 +02001523
1524 # Step 4. Review the descriptor and add missing fields
1525 #print vnf_descriptor
1526 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1527 vnf_name = vnf_descriptor['vnf']['name']
1528 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1529 if "physical" in vnf_descriptor['vnf']:
1530 del vnf_descriptor['vnf']['physical']
1531 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +01001532
tierno42026a02017-02-10 15:13:40 +01001533 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
garciadeblas9f8456e2016-09-05 05:02:59 +02001534 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1535 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +01001536
garciadeblas9f8456e2016-09-05 05:02:59 +02001537 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1538 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1539 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
1540 try:
1541 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1542 for vnfc in vnf_descriptor['vnf']['VNFC']:
1543 VNFCitem={}
1544 VNFCitem["name"] = vnfc['name']
1545 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +01001546
garciadeblas9f8456e2016-09-05 05:02:59 +02001547 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +01001548
garciadeblas9f8456e2016-09-05 05:02:59 +02001549 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +02001550 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 +02001551 myflavorDict["description"] = VNFCitem["description"]
1552 myflavorDict["ram"] = vnfc.get("ram", 0)
1553 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
garciadeblas79d1a1a2017-12-11 16:07:07 +01001554 myflavorDict["disk"] = vnfc.get("disk", 0)
garciadeblas9f8456e2016-09-05 05:02:59 +02001555 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +01001556
garciadeblas9f8456e2016-09-05 05:02:59 +02001557 devices = vnfc.get("devices")
1558 if devices != None:
1559 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +01001560
garciadeblas9f8456e2016-09-05 05:02:59 +02001561 # TODO:
1562 # 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 +01001563 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1564
garciadeblas9f8456e2016-09-05 05:02:59 +02001565 # Previous code has been commented
1566 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1567 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1568 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1569 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1570 #else:
1571 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1572 # if result2:
1573 # print "Error creating flavor: unknown processor model. Rollback successful."
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001574 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
garciadeblas9f8456e2016-09-05 05:02:59 +02001575 # else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001576 # 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 +02001577 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +01001578
garciadeblas9f8456e2016-09-05 05:02:59 +02001579 if 'numas' in vnfc and len(vnfc['numas'])>0:
1580 myflavorDict['extended']['numas'] = vnfc['numas']
1581
1582 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +01001583
garciadeblas9f8456e2016-09-05 05:02:59 +02001584 # Step 6.2 New flavors are created in the VIM
1585 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
1586
1587 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1588 VNFCitem["flavor_id"] = flavor_id
1589 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +01001590
garciadeblas9f8456e2016-09-05 05:02:59 +02001591 logger.debug("Creating new images in the VIM for each VNFC")
1592 # Step 6.3 New images are created in the VIM
1593 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +01001594 #This "for" loop might be integrated with the previous one
garciadeblas9f8456e2016-09-05 05:02:59 +02001595 #In case this integration is made, the VNFCDict might become a VNFClist.
1596 for vnfc in vnf_descriptor['vnf']['VNFC']:
1597 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +02001598 image_dict={}
1599 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1600 image_dict['universal_name']=vnfc.get('image name')
1601 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1602 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +01001603 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +02001604 image_dict['checksum']=vnfc.get('image checksum')
garciadeblas9f8456e2016-09-05 05:02:59 +02001605 image_metadata_dict = vnfc.get('image metadata', None)
1606 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +01001607 if image_metadata_dict is not None:
garciadeblas9f8456e2016-09-05 05:02:59 +02001608 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1609 image_dict['metadata']=image_metadata_str
1610 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1611 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1612 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1613 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +02001614 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno8e690322017-08-10 15:58:50 +02001615 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
tierno36c0b172017-01-12 18:32:28 +01001616 if vnfc.get("boot-data"):
1617 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
garciadeblas9f8456e2016-09-05 05:02:59 +02001618
garciadeblas9f8456e2016-09-05 05:02:59 +02001619 # Step 7. Storing the VNF descriptor in the repository
1620 if "descriptor" not in vnf_descriptor["vnf"]:
1621 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001622
garciadeblas9f8456e2016-09-05 05:02:59 +02001623 # Step 8. Adding the VNF to the NFVO DB
1624 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1625 return vnf_id
1626 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
1627 _, message = rollback(mydb, vims, rollback_list)
1628 if isinstance(e, db_base_Exception):
1629 error_text = "Exception at database"
1630 elif isinstance(e, KeyError):
1631 error_text = "KeyError exception "
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001632 e.http_code = httperrors.Internal_Server_Error
garciadeblas9f8456e2016-09-05 05:02:59 +02001633 else:
1634 error_text = "Exception at VIM"
1635 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1636 #logger.error("start_scenario %s", error_text)
1637 raise NfvoException(error_text, e.http_code)
1638
tiernob3d36742017-03-03 23:51:05 +01001639
tierno7edb6752016-03-21 17:37:52 +01001640def get_vnf_id(mydb, tenant_id, vnf_id):
1641 #check valid tenant_id
tierno42026a02017-02-10 15:13:40 +01001642 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001643 #obtain data
1644 where_or = {}
1645 if tenant_id != "any":
1646 where_or["tenant_id"] = tenant_id
1647 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001648 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
1649
tiernof1ba57e2017-09-07 12:23:19 +02001650 vnf_id = vnf["uuid"]
1651 filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
tiernof97fd272016-07-11 14:32:37 +02001652 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
tierno7edb6752016-03-21 17:37:52 +01001653 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1654 data={'vnf' : filtered_content}
1655 #GET VM
tiernof97fd272016-07-11 14:32:37 +02001656 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
tiernof1ba57e2017-09-07 12:23:19 +02001657 SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1658 'boot_data'),
tierno7edb6752016-03-21 17:37:52 +01001659 WHERE={'vnfs.uuid': vnf_id} )
gcalvinobfa2fd92018-11-13 18:47:28 +01001660 if len(content) != 0:
Anderson Bravalheric5293de2018-11-28 17:21:26 +00001661 #raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
tierno36c0b172017-01-12 18:32:28 +01001662 # change boot_data into boot-data
gcalvino319b8a52018-11-05 15:33:23 +01001663 for vm in content:
1664 if vm.get("boot_data"):
1665 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
1666 del vm["boot_data"]
tierno7edb6752016-03-21 17:37:52 +01001667
gcalvinobfa2fd92018-11-13 18:47:28 +01001668 data['vnf']['VNFC'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001669 #TODO: GET all the information from a VNFC and include it in the output.
tierno42026a02017-02-10 15:13:40 +01001670
tierno7edb6752016-03-21 17:37:52 +01001671 #GET NET
tierno42026a02017-02-10 15:13:40 +01001672 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
tierno7edb6752016-03-21 17:37:52 +01001673 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1674 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +02001675 data['vnf']['nets'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001676
1677 #GET ip-profile for each net
1678 for net in data['vnf']['nets']:
1679 ipprofiles = mydb.get_rows(FROM='ip_profiles',
1680 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1681 WHERE={'net_id': net["uuid"]} )
1682 if len(ipprofiles)==1:
1683 net["ip_profile"] = ipprofiles[0]
1684 elif len(ipprofiles)>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001685 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 +01001686
1687
garciadeblas9f8456e2016-09-05 05:02:59 +02001688 #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 +01001689
garciadeblas9f8456e2016-09-05 05:02:59 +02001690 #GET External Interfaces
tiernof97fd272016-07-11 14:32:37 +02001691 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 +01001692 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1693 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
tierno3fcfdb72017-10-24 07:48:24 +02001694 WHERE={'vnfs.uuid': vnf_id, 'interfaces.external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01001695 #print content
tiernof97fd272016-07-11 14:32:37 +02001696 data['vnf']['external-connections'] = content
tierno42026a02017-02-10 15:13:40 +01001697
tiernof97fd272016-07-11 14:32:37 +02001698 return data
tierno7edb6752016-03-21 17:37:52 +01001699
1700
1701def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
1702 # Check tenant exist
1703 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +02001704 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001705 # Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernocbb52052018-05-31 18:57:30 +02001706 vims = get_vim(mydb, tenant_id, ignore_errors=True)
tierno7edb6752016-03-21 17:37:52 +01001707 else:
1708 vims={}
1709
1710 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1711 where_or = {}
1712 if tenant_id != "any":
1713 where_or["tenant_id"] = tenant_id
1714 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001715 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 +02001716 vnf_id = vnf["uuid"]
tierno42026a02017-02-10 15:13:40 +01001717
tierno7edb6752016-03-21 17:37:52 +01001718 # "Getting the list of flavors and tenants of the VNF"
tierno42026a02017-02-10 15:13:40 +01001719 flavorList = get_flavorlist(mydb, vnf_id)
tiernof97fd272016-07-11 14:32:37 +02001720 if len(flavorList)==0:
1721 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001722
tiernof97fd272016-07-11 14:32:37 +02001723 imageList = get_imagelist(mydb, vnf_id)
1724 if len(imageList)==0:
1725 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001726
tiernof97fd272016-07-11 14:32:37 +02001727 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
1728 if deleted == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001729 raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
tierno42026a02017-02-10 15:13:40 +01001730
tierno7edb6752016-03-21 17:37:52 +01001731 undeletedItems = []
1732 for flavor in flavorList:
1733 #check if flavor is used by other vnf
tiernof97fd272016-07-11 14:32:37 +02001734 try:
1735 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
1736 if len(c) > 0:
1737 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
1738 continue
1739 #flavor not used, must be deleted
1740 #delelte at VIM
tierno96ebf002017-12-13 10:55:38 +01001741 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id': flavor})
tierno7edb6752016-03-21 17:37:52 +01001742 for flavor_vim in c:
tierno96ebf002017-12-13 10:55:38 +01001743 if not flavor_vim['created']: # skip this flavor because not created by openmano
tierno7edb6752016-03-21 17:37:52 +01001744 continue
tierno96ebf002017-12-13 10:55:38 +01001745 # look for vim
1746 myvim = None
1747 for vim in vims.values():
1748 if vim["config"]["datacenter_tenant_id"] == flavor_vim["datacenter_vim_id"]:
1749 myvim = vim
1750 break
1751 if not myvim:
tierno7edb6752016-03-21 17:37:52 +01001752 continue
tiernoae4a8d12016-07-08 12:30:39 +02001753 try:
1754 myvim.delete_flavor(flavor_vim["vim_id"])
tierno96ebf002017-12-13 10:55:38 +01001755 except vimconn.vimconnNotFoundException:
1756 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"],
1757 flavor_vim["datacenter_vim_id"] )
tiernoae4a8d12016-07-08 12:30:39 +02001758 except vimconn.vimconnException as e:
1759 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
tierno96ebf002017-12-13 10:55:38 +01001760 flavor_vim["vim_id"], flavor_vim["datacenter_vim_id"], type(e).__name__, str(e))
1761 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"],
1762 flavor_vim["datacenter_vim_id"]))
1763 # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
tiernof97fd272016-07-11 14:32:37 +02001764 mydb.delete_row_by_id('flavors', flavor)
1765 except db_base_Exception as e:
1766 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
tierno96ebf002017-12-13 10:55:38 +01001767 undeletedItems.append("flavor {}".format(flavor))
tiernof97fd272016-07-11 14:32:37 +02001768
tierno42026a02017-02-10 15:13:40 +01001769
tierno7edb6752016-03-21 17:37:52 +01001770 for image in imageList:
tiernof97fd272016-07-11 14:32:37 +02001771 try:
1772 #check if image is used by other vnf
tierno16e3dd42018-04-24 12:52:40 +02001773 c = mydb.get_rows(FROM='vms', WHERE=[{'image_id': image}, {'image_list LIKE ': '%' + image + '%'}])
tiernof97fd272016-07-11 14:32:37 +02001774 if len(c) > 0:
1775 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
1776 continue
1777 #image not used, must be deleted
1778 #delelte at VIM
1779 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
tierno7edb6752016-03-21 17:37:52 +01001780 for image_vim in c:
tierno868220c2017-09-26 00:11:05 +02001781 if image_vim["datacenter_vim_id"] not in vims: # TODO change to datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +01001782 continue
1783 if image_vim['created']=='false': #skip this image because not created by openmano
1784 continue
1785 myvim=vims[ image_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +02001786 try:
1787 myvim.delete_image(image_vim["vim_id"])
1788 except vimconn.vimconnNotFoundException as e:
1789 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
1790 except vimconn.vimconnException as e:
1791 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1792 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
1793 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +02001794 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1795 mydb.delete_row_by_id('images', image)
1796 except db_base_Exception as e:
1797 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
tierno7edb6752016-03-21 17:37:52 +01001798 undeletedItems.append("image %s" % image)
1799
tiernof97fd272016-07-11 14:32:37 +02001800 return vnf_id + " " + vnf["name"]
tierno42026a02017-02-10 15:13:40 +01001801 #if undeletedItems:
tiernof97fd272016-07-11 14:32:37 +02001802 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
tierno7edb6752016-03-21 17:37:52 +01001803
tiernob3d36742017-03-03 23:51:05 +01001804
tiernob8569aa2018-08-24 11:34:54 +02001805@deprecated("Not used")
tierno7edb6752016-03-21 17:37:52 +01001806def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
1807 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
1808 if result < 0:
1809 return result, vims
1810 elif result == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001811 return -httperrors.Not_Found, "datacenter '%s' not found" % datacenter_name
tierno7edb6752016-03-21 17:37:52 +01001812 myvim = vims.values()[0]
1813 result,servers = myvim.get_hosts_info()
1814 if result < 0:
1815 return result, servers
1816 topology = {'name':myvim['name'] , 'servers': servers}
1817 return result, topology
1818
tiernob3d36742017-03-03 23:51:05 +01001819
tierno7edb6752016-03-21 17:37:52 +01001820def get_hosts(mydb, nfvo_tenant_id):
tiernof97fd272016-07-11 14:32:37 +02001821 vims = get_vim(mydb, nfvo_tenant_id)
1822 if len(vims) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001823 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), httperrors.Not_Found)
tiernof97fd272016-07-11 14:32:37 +02001824 elif len(vims)>1:
1825 #print "nfvo.datacenter_action() error. Several datacenters found"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001826 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
tierno7edb6752016-03-21 17:37:52 +01001827 myvim = vims.values()[0]
tiernof97fd272016-07-11 14:32:37 +02001828 try:
1829 hosts = myvim.get_hosts()
1830 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01001831
tiernof97fd272016-07-11 14:32:37 +02001832 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
1833 for host in hosts:
1834 server={'name':host['name'], 'vms':[]}
1835 for vm in host['instances']:
1836 #get internal name and model
tierno42026a02017-02-10 15:13:40 +01001837 try:
tiernof97fd272016-07-11 14:32:37 +02001838 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1839 WHERE={'vim_vm_id':vm['id']} )
1840 if len(c) == 0:
1841 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1842 continue
1843 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
tierno42026a02017-02-10 15:13:40 +01001844
tiernof97fd272016-07-11 14:32:37 +02001845 except db_base_Exception as e:
1846 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1847 datacenter['Datacenters'][0]['servers'].append(server)
1848 #return -400, "en construccion"
tierno42026a02017-02-10 15:13:40 +01001849
tiernof97fd272016-07-11 14:32:37 +02001850 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1851 return datacenter
1852 except vimconn.vimconnException as e:
1853 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001854
tiernob3d36742017-03-03 23:51:05 +01001855
tiernob8569aa2018-08-24 11:34:54 +02001856@deprecated("Use new_nsd_v3")
tierno7edb6752016-03-21 17:37:52 +01001857def new_scenario(mydb, tenant_id, topo):
1858
1859# result, vims = get_vim(mydb, tenant_id)
1860# if result < 0:
1861# return result, vims
1862#1: parse input
1863 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001864 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001865 if "tenant_id" in topo:
1866 if topo["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001867 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 +01001868 httperrors.Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001869 else:
1870 tenant_id=None
1871
tierno42026a02017-02-10 15:13:40 +01001872#1.1: get VNFs and external_networks (other_nets).
tierno7edb6752016-03-21 17:37:52 +01001873 vnfs={}
1874 other_nets={} #external_networks, bridge_networks and data_networkds
1875 nodes = topo['topology']['nodes']
1876 for k in nodes.keys():
1877 if nodes[k]['type'] == 'VNF':
1878 vnfs[k] = nodes[k]
1879 vnfs[k]['ifaces'] = {}
tierno42026a02017-02-10 15:13:40 +01001880 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
tierno7edb6752016-03-21 17:37:52 +01001881 other_nets[k] = nodes[k]
1882 other_nets[k]['external']=True
tierno42026a02017-02-10 15:13:40 +01001883 elif nodes[k]['type'] == 'network':
tierno7edb6752016-03-21 17:37:52 +01001884 other_nets[k] = nodes[k]
1885 other_nets[k]['external']=False
tierno42026a02017-02-10 15:13:40 +01001886
tierno7edb6752016-03-21 17:37:52 +01001887
1888#1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1889 for name,vnf in vnfs.items():
tierno3fcfdb72017-10-24 07:48:24 +02001890 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
tierno7edb6752016-03-21 17:37:52 +01001891 error_text = ""
1892 error_pos = "'topology':'nodes':'" + name + "'"
1893 if 'vnf_id' in vnf:
1894 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001895 where['uuid'] = vnf['vnf_id']
tierno7edb6752016-03-21 17:37:52 +01001896 if 'VNF model' in vnf:
1897 error_text += " 'VNF model' " + vnf['VNF model']
tiernocea279c2016-07-18 12:36:49 +02001898 where['name'] = vnf['VNF model']
tierno3fcfdb72017-10-24 07:48:24 +02001899 if len(where) == 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001900 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +01001901
tiernocea279c2016-07-18 12:36:49 +02001902 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1903 FROM='vnfs',
tierno3fcfdb72017-10-24 07:48:24 +02001904 WHERE=where)
tiernof97fd272016-07-11 14:32:37 +02001905 if len(vnf_db)==0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001906 raise NfvoException("unknown" + error_text + " at " + error_pos, httperrors.Not_Found)
tiernof97fd272016-07-11 14:32:37 +02001907 elif len(vnf_db)>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001908 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", httperrors.Conflict)
tierno7edb6752016-03-21 17:37:52 +01001909 vnf['uuid']=vnf_db[0]['uuid']
1910 vnf['description']=vnf_db[0]['description']
1911 #get external interfaces
tierno42026a02017-02-10 15:13:40 +01001912 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1913 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 +02001914 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01001915 for ext_iface in ext_ifaces:
1916 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1917
1918#1.4 get list of connections
1919 conections = topo['topology']['connections']
1920 conections_list = []
tiernoefd80c92016-09-16 14:17:46 +02001921 conections_list_name = []
tierno7edb6752016-03-21 17:37:52 +01001922 for k in conections.keys():
1923 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1924 ifaces_list = conections[k]['nodes'].items()
1925 elif type(conections[k]['nodes'])==list: #list with dictionary
1926 ifaces_list=[]
1927 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1928 for k2 in conection_pair_list:
1929 ifaces_list += k2
1930
1931 con_type = conections[k].get("type", "link")
1932 if con_type != "link":
1933 if k in other_nets:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001934 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001935 other_nets[k] = {'external': False}
1936 if conections[k].get("graph"):
1937 other_nets[k]["graph"] = conections[k]["graph"]
1938 ifaces_list.append( (k, None) )
1939
tierno42026a02017-02-10 15:13:40 +01001940
tierno7edb6752016-03-21 17:37:52 +01001941 if con_type == "external_network":
1942 other_nets[k]['external'] = True
1943 if conections[k].get("model"):
1944 other_nets[k]["model"] = conections[k]["model"]
1945 else:
1946 other_nets[k]["model"] = k
tierno42026a02017-02-10 15:13:40 +01001947 if con_type == "dataplane_net" or con_type == "bridge_net":
tierno7edb6752016-03-21 17:37:52 +01001948 other_nets[k]["model"] = con_type
tierno42026a02017-02-10 15:13:40 +01001949
tiernoefd80c92016-09-16 14:17:46 +02001950 conections_list_name.append(k)
tierno7edb6752016-03-21 17:37:52 +01001951 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)
1952 #print set(ifaces_list)
1953 #check valid VNF and iface names
1954 for iface in ifaces_list:
1955 if iface[0] not in vnfs and iface[0] not in other_nets :
tiernof97fd272016-07-11 14:32:37 +02001956 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001957 str(k), iface[0]), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001958 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
tiernof97fd272016-07-11 14:32:37 +02001959 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001960 str(k), iface[0], iface[1]), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001961
1962#1.5 unify connections from the pair list to a consolidated list
1963 index=0
1964 while index < len(conections_list):
1965 index2 = index+1
1966 while index2 < len(conections_list):
1967 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
1968 conections_list[index] |= conections_list[index2]
1969 del conections_list[index2]
tiernoefd80c92016-09-16 14:17:46 +02001970 del conections_list_name[index2]
tierno7edb6752016-03-21 17:37:52 +01001971 else:
1972 index2 += 1
1973 conections_list[index] = list(conections_list[index]) # from set to list again
1974 index += 1
1975 #for k in conections_list:
1976 # print k
tierno42026a02017-02-10 15:13:40 +01001977
tierno7edb6752016-03-21 17:37:52 +01001978
1979
1980#1.6 Delete non external nets
1981# for k in other_nets.keys():
1982# if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1983# for con in conections_list:
1984# delete_indexes=[]
1985# for index in range(0,len(con)):
1986# if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1987# for index in delete_indexes:
1988# del con[index]
1989# del other_nets[k]
1990#1.7: Check external_ports are present at database table datacenter_nets
1991 for k,net in other_nets.items():
1992 error_pos = "'topology':'nodes':'" + k + "'"
1993 if net['external']==False:
1994 if 'name' not in net:
1995 net['name']=k
1996 if 'model' not in net:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01001997 raise NfvoException("needed a 'model' at " + error_pos, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001998 if net['model']=='bridge_net':
1999 net['type']='bridge';
2000 elif net['model']=='dataplane_net':
2001 net['type']='data';
2002 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002003 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002004 else: #external
2005#IF we do not want to check that external network exist at datacenter
2006 pass
tierno42026a02017-02-10 15:13:40 +01002007#ELSE
tierno7edb6752016-03-21 17:37:52 +01002008# error_text = ""
2009# WHERE_={}
2010# if 'net_id' in net:
2011# error_text += " 'net_id' " + net['net_id']
2012# WHERE_['uuid'] = net['net_id']
2013# if 'model' in net:
2014# error_text += " 'model' " + net['model']
2015# WHERE_['name'] = net['model']
2016# if len(WHERE_) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002017# return -httperrors.Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
tierno7edb6752016-03-21 17:37:52 +01002018# r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
2019# FROM='datacenter_nets', WHERE=WHERE_ )
2020# if r<0:
2021# print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
2022# elif r==0:
2023# print "nfvo.new_scenario Error" +error_text+ " is not present at database"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002024# return -httperrors.Bad_Request, "unknown " +error_text+ " at " + error_pos
tierno7edb6752016-03-21 17:37:52 +01002025# elif r>1:
tierno42026a02017-02-10 15:13:40 +01002026# print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002027# 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 +01002028# other_nets[k].update(net_db[0])
tierno42026a02017-02-10 15:13:40 +01002029#ENDIF
tierno7edb6752016-03-21 17:37:52 +01002030 net_list={}
2031 net_nb=0 #Number of nets
2032 for con in conections_list:
2033 #check if this is connected to a external net
2034 other_net_index=-1
2035 #print
2036 #print "con", con
2037 for index in range(0,len(con)):
2038 #check if this is connected to a external net
2039 for net_key in other_nets.keys():
2040 if con[index][0]==net_key:
2041 if other_net_index>=0:
tierno42026a02017-02-10 15:13:40 +01002042 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 +02002043 #print "nfvo.new_scenario " + error_text
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002044 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002045 else:
2046 other_net_index = index
2047 net_target = net_key
2048 break
2049 #print "other_net_index", other_net_index
2050 try:
2051 if other_net_index>=0:
2052 del con[other_net_index]
2053#IF we do not want to check that external network exist at datacenter
2054 if other_nets[net_target]['external'] :
2055 if "name" not in other_nets[net_target]:
2056 other_nets[net_target]['name'] = other_nets[net_target]['model']
2057 if other_nets[net_target]["type"] == "external_network":
2058 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
2059 other_nets[net_target]["type"] = "data"
2060 else:
2061 other_nets[net_target]["type"] = "bridge"
tierno42026a02017-02-10 15:13:40 +01002062#ELSE
tierno7edb6752016-03-21 17:37:52 +01002063# if other_nets[net_target]['external'] :
2064# 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
2065# if type_=='data' and other_nets[net_target]['type']=="ptp":
2066# error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
2067# print "nfvo.new_scenario " + error_text
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002068# return -httperrors.Bad_Request, error_text
tierno42026a02017-02-10 15:13:40 +01002069#ENDIF
tierno7edb6752016-03-21 17:37:52 +01002070 for iface in con:
2071 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
2072 else:
2073 #create a net
2074 net_type_bridge=False
2075 net_type_data=False
2076 net_target = "__-__net"+str(net_nb)
tierno42026a02017-02-10 15:13:40 +01002077 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
tiernoefd80c92016-09-16 14:17:46 +02002078 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
tierno42026a02017-02-10 15:13:40 +01002079 'external':False}
tierno7edb6752016-03-21 17:37:52 +01002080 for iface in con:
2081 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
2082 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
2083 if iface_type=='mgmt' or iface_type=='bridge':
2084 net_type_bridge = True
2085 else:
2086 net_type_data = True
2087 if net_type_bridge and net_type_data:
2088 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 +02002089 #print "nfvo.new_scenario " + error_text
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002090 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002091 elif net_type_bridge:
2092 type_='bridge'
2093 else:
2094 type_='data' if len(con)>2 else 'ptp'
2095 net_list[net_target]['type'] = type_
2096 net_nb+=1
2097 except Exception:
2098 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02002099 #print "nfvo.new_scenario " + error_text
tierno7edb6752016-03-21 17:37:52 +01002100 #raise e
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002101 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002102
2103#1.8: Connect to management net all not already connected interfaces of type 'mgmt'
tierno42026a02017-02-10 15:13:40 +01002104 #1.8.1 obtain management net
tiernof97fd272016-07-11 14:32:37 +02002105 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
tierno7edb6752016-03-21 17:37:52 +01002106 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
tierno42026a02017-02-10 15:13:40 +01002107 #1.8.2 check all interfaces from all vnfs
tiernof97fd272016-07-11 14:32:37 +02002108 if len(mgmt_net)>0:
tierno7edb6752016-03-21 17:37:52 +01002109 add_mgmt_net = False
2110 for vnf in vnfs.values():
2111 for iface in vnf['ifaces'].values():
2112 if iface['type']=='mgmt' and 'net_key' not in iface:
2113 #iface not connected
2114 iface['net_key'] = 'mgmt'
2115 add_mgmt_net = True
2116 if add_mgmt_net and 'mgmt' not in net_list:
2117 net_list['mgmt']=mgmt_net[0]
2118 net_list['mgmt']['external']=True
2119 net_list['mgmt']['graph']={'visible':False}
2120
2121 net_list.update(other_nets)
tiernof97fd272016-07-11 14:32:37 +02002122 #print
2123 #print 'net_list', net_list
2124 #print
2125 #print 'vnfs', vnfs
2126 #print
tierno7edb6752016-03-21 17:37:52 +01002127
2128#2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
tiernof97fd272016-07-11 14:32:37 +02002129 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
tierno392f2852016-05-13 12:28:55 +02002130 'tenant_id':tenant_id, 'name':topo['name'],
2131 'description':topo.get('description',topo['name']),
2132 'public': topo.get('public', False)
2133 })
tierno42026a02017-02-10 15:13:40 +01002134
tiernof97fd272016-07-11 14:32:37 +02002135 return c
tierno7edb6752016-03-21 17:37:52 +01002136
tiernob3d36742017-03-03 23:51:05 +01002137
tiernob8569aa2018-08-24 11:34:54 +02002138@deprecated("Use new_nsd_v3")
tierno5bb59dc2017-02-13 14:53:54 +01002139def new_scenario_v02(mydb, tenant_id, scenario_dict, version):
2140 """ This creates a new scenario for version 0.2 and 0.3"""
tierno392f2852016-05-13 12:28:55 +02002141 scenario = scenario_dict["scenario"]
tierno7edb6752016-03-21 17:37:52 +01002142 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01002143 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002144 if "tenant_id" in scenario:
2145 if scenario["tenant_id"] != tenant_id:
tierno5bb59dc2017-02-13 14:53:54 +01002146 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
tiernof97fd272016-07-11 14:32:37 +02002147 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002148 scenario["tenant_id"], tenant_id), httperrors.Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01002149 else:
2150 tenant_id=None
2151
tierno5bb59dc2017-02-13 14:53:54 +01002152 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
tierno7edb6752016-03-21 17:37:52 +01002153 for name,vnf in scenario["vnfs"].iteritems():
tierno3fcfdb72017-10-24 07:48:24 +02002154 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
tierno7edb6752016-03-21 17:37:52 +01002155 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02002156 error_pos = "'scenario':'vnfs':'" + name + "'"
tierno7edb6752016-03-21 17:37:52 +01002157 if 'vnf_id' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01002158 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02002159 where['uuid'] = vnf['vnf_id']
tierno392f2852016-05-13 12:28:55 +02002160 if 'vnf_name' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01002161 error_text += " 'vnf_name' " + vnf['vnf_name']
tiernocea279c2016-07-18 12:36:49 +02002162 where['name'] = vnf['vnf_name']
tierno3fcfdb72017-10-24 07:48:24 +02002163 if len(where) == 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002164 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, httperrors.Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002165 vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'),
tiernocea279c2016-07-18 12:36:49 +02002166 FROM='vnfs',
tierno3fcfdb72017-10-24 07:48:24 +02002167 WHERE=where)
tierno5bb59dc2017-02-13 14:53:54 +01002168 if len(vnf_db) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002169 raise NfvoException("Unknown" + error_text + " at " + error_pos, httperrors.Not_Found)
tierno5bb59dc2017-02-13 14:53:54 +01002170 elif len(vnf_db) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002171 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", httperrors.Conflict)
tierno5bb59dc2017-02-13 14:53:54 +01002172 vnf['uuid'] = vnf_db[0]['uuid']
2173 vnf['description'] = vnf_db[0]['description']
tierno7edb6752016-03-21 17:37:52 +01002174 vnf['ifaces'] = {}
tierno5bb59dc2017-02-13 14:53:54 +01002175 # get external interfaces
2176 ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
2177 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 +02002178 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01002179 for ext_iface in ext_ifaces:
tierno5bb59dc2017-02-13 14:53:54 +01002180 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']}
2181 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
tierno7edb6752016-03-21 17:37:52 +01002182
tierno5bb59dc2017-02-13 14:53:54 +01002183 # 2: Insert net_key and ip_address at every vnf interface
2184 for net_name, net in scenario["networks"].items():
2185 net_type_bridge = False
2186 net_type_data = False
tierno7edb6752016-03-21 17:37:52 +01002187 for iface_dict in net["interfaces"]:
tierno5bb59dc2017-02-13 14:53:54 +01002188 if version == "0.2":
2189 temp_dict = iface_dict
2190 ip_address = None
2191 elif version == "0.3":
2192 temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]}
2193 ip_address = iface_dict.get('ip_address', None)
2194 for vnf, iface in temp_dict.items():
tierno7edb6752016-03-21 17:37:52 +01002195 if vnf not in scenario["vnfs"]:
tierno5bb59dc2017-02-13 14:53:54 +01002196 error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2197 net_name, vnf)
2198 # logger.debug("nfvo.new_scenario_v02 " + error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002199 raise NfvoException(error_text, httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002200 if iface not in scenario["vnfs"][vnf]['ifaces']:
tierno5bb59dc2017-02-13 14:53:54 +01002201 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2202 .format(net_name, iface)
2203 # logger.debug("nfvo.new_scenario_v02 " + error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002204 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002205 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
tierno5bb59dc2017-02-13 14:53:54 +01002206 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2207 "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
2208 # logger.debug("nfvo.new_scenario_v02 " + error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002209 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002210 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
tierno5bb59dc2017-02-13 14:53:54 +01002211 scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address
tierno7edb6752016-03-21 17:37:52 +01002212 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
tierno5bb59dc2017-02-13 14:53:54 +01002213 if iface_type == 'mgmt' or iface_type == 'bridge':
tierno7edb6752016-03-21 17:37:52 +01002214 net_type_bridge = True
2215 else:
2216 net_type_data = True
tierno5bb59dc2017-02-13 14:53:54 +01002217
tierno7edb6752016-03-21 17:37:52 +01002218 if net_type_bridge and net_type_data:
tierno5bb59dc2017-02-13 14:53:54 +01002219 error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2220 .format(net_name)
2221 # logger.debug("nfvo.new_scenario " + error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002222 raise NfvoException(error_text, httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002223 elif net_type_bridge:
tierno5bb59dc2017-02-13 14:53:54 +01002224 type_ = 'bridge'
tierno7edb6752016-03-21 17:37:52 +01002225 else:
tierno5bb59dc2017-02-13 14:53:54 +01002226 type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp'
2227
2228 if net.get("implementation"): # for v0.3
2229 if type_ == "bridge" and net["implementation"] == "underlay":
2230 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2231 "'network':'{}'".format(net_name)
2232 # logger.debug(error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002233 raise NfvoException(error_text, httperrors.Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002234 elif type_ != "bridge" and net["implementation"] == "overlay":
2235 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2236 "'network':'{}'".format(net_name)
2237 # logger.debug(error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002238 raise NfvoException(error_text, httperrors.Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002239 net.pop("implementation")
2240 if "type" in net and version == "0.3": # for v0.3
2241 if type_ == "data" and net["type"] == "e-line":
2242 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\
2243 "'e-line' at 'network':'{}'".format(net_name)
2244 # logger.debug(error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002245 raise NfvoException(error_text, httperrors.Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002246 elif type_ == "ptp" and net["type"] == "e-lan":
2247 type_ = "data"
2248
tierno7edb6752016-03-21 17:37:52 +01002249 net['type'] = type_
2250 net['name'] = net_name
2251 net['external'] = net.get('external', False)
2252
tierno5bb59dc2017-02-13 14:53:54 +01002253 # 3: insert at database
tierno7edb6752016-03-21 17:37:52 +01002254 scenario["nets"] = scenario["networks"]
2255 scenario['tenant_id'] = tenant_id
tierno5bb59dc2017-02-13 14:53:54 +01002256 scenario_id = mydb.new_scenario(scenario)
tiernof97fd272016-07-11 14:32:37 +02002257 return scenario_id
tierno7edb6752016-03-21 17:37:52 +01002258
tiernob3d36742017-03-03 23:51:05 +01002259
tiernof1ba57e2017-09-07 12:23:19 +02002260def new_nsd_v3(mydb, tenant_id, nsd_descriptor):
2261 """
2262 Parses an OSM IM nsd_catalog and insert at DB
2263 :param mydb:
2264 :param tenant_id:
2265 :param nsd_descriptor:
Igor D.Ccaadc442017-11-06 12:48:48 +00002266 :return: The list of created NSD ids
tiernof1ba57e2017-09-07 12:23:19 +02002267 """
2268 try:
2269 mynsd = nsd_catalog.nsd()
tiernoa9550202017-09-22 13:31:35 +02002270 try:
tiernof6bbe222019-04-09 14:19:40 +00002271 pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd, skip_unknown=True)
tiernoa9550202017-09-22 13:31:35 +02002272 except Exception as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002273 raise NfvoException("Error. Invalid NS descriptor format: " + str(e), httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002274 db_scenarios = []
2275 db_sce_nets = []
2276 db_sce_vnfs = []
2277 db_sce_interfaces = []
Igor D.Ccaadc442017-11-06 12:48:48 +00002278 db_sce_vnffgs = []
2279 db_sce_rsps = []
2280 db_sce_rsp_hops = []
2281 db_sce_classifiers = []
2282 db_sce_classifier_matches = []
tiernof1ba57e2017-09-07 12:23:19 +02002283 db_ip_profiles = []
2284 db_ip_profiles_index = 0
2285 uuid_list = []
2286 nsd_uuid_list = []
tiernob2880eb2017-10-04 15:04:53 +02002287 for nsd_yang in mynsd.nsd_catalog.nsd.itervalues():
2288 nsd = nsd_yang.get()
tiernof1ba57e2017-09-07 12:23:19 +02002289
Igor D.Ccaadc442017-11-06 12:48:48 +00002290 # table scenarios
tiernof1ba57e2017-09-07 12:23:19 +02002291 scenario_uuid = str(uuid4())
2292 uuid_list.append(scenario_uuid)
2293 nsd_uuid_list.append(scenario_uuid)
2294 db_scenario = {
2295 "uuid": scenario_uuid,
2296 "osm_id": get_str(nsd, "id", 255),
2297 "name": get_str(nsd, "name", 255),
2298 "description": get_str(nsd, "description", 255),
2299 "tenant_id": tenant_id,
2300 "vendor": get_str(nsd, "vendor", 255),
2301 "short_name": get_str(nsd, "short-name", 255),
2302 "descriptor": str(nsd_descriptor)[:60000],
2303 }
2304 db_scenarios.append(db_scenario)
2305
2306 # table sce_vnfs (constituent-vnfd)
2307 vnf_index2scevnf_uuid = {}
2308 vnf_index2vnf_uuid = {}
2309 for vnf in nsd.get("constituent-vnfd").itervalues():
2310 existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255],
2311 'tenant_id': tenant_id})
2312 if not existing_vnf:
tiernob2880eb2017-10-04 15:04:53 +02002313 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2314 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2315 str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002316 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002317 sce_vnf_uuid = str(uuid4())
2318 uuid_list.append(sce_vnf_uuid)
2319 db_sce_vnf = {
2320 "uuid": sce_vnf_uuid,
2321 "scenario_id": scenario_uuid,
tierno92c36fd2018-05-04 12:21:10 +02002322 # "name": get_str(vnf, "member-vnf-index", 255),
2323 "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 50),
tiernof1ba57e2017-09-07 12:23:19 +02002324 "vnf_id": existing_vnf[0]["uuid"],
tierno16e3dd42018-04-24 12:52:40 +02002325 "member_vnf_index": str(vnf["member-vnf-index"]),
tiernof1ba57e2017-09-07 12:23:19 +02002326 # TODO 'start-by-default': True
2327 }
tierno16e3dd42018-04-24 12:52:40 +02002328 vnf_index2scevnf_uuid[str(vnf['member-vnf-index'])] = sce_vnf_uuid
2329 vnf_index2vnf_uuid[str(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"]
tiernof1ba57e2017-09-07 12:23:19 +02002330 db_sce_vnfs.append(db_sce_vnf)
2331
2332 # table ip_profiles (ip-profiles)
2333 ip_profile_name2db_table_index = {}
2334 for ip_profile in nsd.get("ip-profiles").itervalues():
2335 db_ip_profile = {
2336 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
2337 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
2338 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
2339 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2340 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
2341 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
2342 }
2343 dns_list = []
2344 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
2345 dns_list.append(str(dns.get("address")))
2346 db_ip_profile["dns_address"] = ";".join(dns_list)
2347 if ip_profile["ip-profile-params"].get('security-group'):
2348 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
2349 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
2350 db_ip_profiles_index += 1
2351 db_ip_profiles.append(db_ip_profile)
2352
2353 # table sce_nets (internal-vld)
2354 for vld in nsd.get("vld").itervalues():
2355 sce_net_uuid = str(uuid4())
2356 uuid_list.append(sce_net_uuid)
2357 db_sce_net = {
2358 "uuid": sce_net_uuid,
2359 "name": get_str(vld, "name", 255),
2360 "scenario_id": scenario_uuid,
2361 # "type": #TODO
2362 "multipoint": not vld.get("type") == "ELINE",
tierno1df468d2018-07-06 14:25:16 +02002363 "osm_id": get_str(vld, "id", 255),
tiernof1ba57e2017-09-07 12:23:19 +02002364 # "external": #TODO
2365 "description": get_str(vld, "description", 255),
2366 }
2367 # guess type of network
2368 if vld.get("mgmt-network"):
2369 db_sce_net["type"] = "bridge"
2370 db_sce_net["external"] = True
2371 elif vld.get("provider-network").get("overlay-type") == "VLAN":
2372 db_sce_net["type"] = "data"
2373 else:
tierno66eba6e2017-11-10 17:09:18 +01002374 # later on it will be fixed to bridge or data depending on the type of interfaces attached to it
2375 db_sce_net["type"] = None
tiernof1ba57e2017-09-07 12:23:19 +02002376 db_sce_nets.append(db_sce_net)
2377
2378 # ip-profile, link db_ip_profile with db_sce_net
2379 if vld.get("ip-profile-ref"):
2380 ip_profile_name = vld.get("ip-profile-ref")
2381 if ip_profile_name not in ip_profile_name2db_table_index:
tiernob2880eb2017-10-04 15:04:53 +02002382 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2383 " Reference to a non-existing 'ip_profiles'".format(
2384 str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002385 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002386 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["sce_net_id"] = sce_net_uuid
tierno8f79ea12018-05-03 17:37:40 +02002387 elif vld.get("vim-network-name"):
2388 db_sce_net["vim_network_name"] = get_str(vld, "vim-network-name", 255)
tiernof1ba57e2017-09-07 12:23:19 +02002389
2390 # table sce_interfaces (vld:vnfd-connection-point-ref)
2391 for iface in vld.get("vnfd-connection-point-ref").itervalues():
tierno16e3dd42018-04-24 12:52:40 +02002392 vnf_index = str(iface['member-vnf-index-ref'])
tiernof1ba57e2017-09-07 12:23:19 +02002393 # check correct parameters
2394 if vnf_index not in vnf_index2vnf_uuid:
tiernob2880eb2017-10-04 15:04:53 +02002395 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2396 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2397 "'nsd':'constituent-vnfd'".format(
2398 str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002399 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002400
tierno66eba6e2017-11-10 17:09:18 +01002401 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid', 'i.type as iface_type'),
tiernof1ba57e2017-09-07 12:23:19 +02002402 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2403 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2404 'external_name': get_str(iface, "vnfd-connection-point-ref",
2405 255)})
2406 if not existing_ifaces:
tiernob2880eb2017-10-04 15:04:53 +02002407 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2408 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2409 "connection-point name at VNFD '{}'".format(
2410 str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]),
2411 str(iface.get("vnfd-id-ref"))[:255]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002412 httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002413 interface_uuid = existing_ifaces[0]["uuid"]
garciadeblasebd66722019-01-31 16:01:31 +00002414 if existing_ifaces[0]["iface_type"] == "data":
tierno66eba6e2017-11-10 17:09:18 +01002415 db_sce_net["type"] = "data"
tiernof1ba57e2017-09-07 12:23:19 +02002416 sce_interface_uuid = str(uuid4())
2417 uuid_list.append(sce_net_uuid)
tierno41a69812018-02-16 14:34:33 +01002418 iface_ip_address = None
2419 if iface.get("ip-address"):
2420 iface_ip_address = str(iface.get("ip-address"))
tiernof1ba57e2017-09-07 12:23:19 +02002421 db_sce_interface = {
2422 "uuid": sce_interface_uuid,
2423 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2424 "sce_net_id": sce_net_uuid,
2425 "interface_id": interface_uuid,
tierno41a69812018-02-16 14:34:33 +01002426 "ip_address": iface_ip_address,
tiernof1ba57e2017-09-07 12:23:19 +02002427 }
2428 db_sce_interfaces.append(db_sce_interface)
tierno66eba6e2017-11-10 17:09:18 +01002429 if not db_sce_net["type"]:
2430 db_sce_net["type"] = "bridge"
tiernof1ba57e2017-09-07 12:23:19 +02002431
Igor D.Ccaadc442017-11-06 12:48:48 +00002432 # table sce_vnffgs (vnffgd)
2433 for vnffg in nsd.get("vnffgd").itervalues():
2434 sce_vnffg_uuid = str(uuid4())
2435 uuid_list.append(sce_vnffg_uuid)
2436 db_sce_vnffg = {
2437 "uuid": sce_vnffg_uuid,
2438 "name": get_str(vnffg, "name", 255),
2439 "scenario_id": scenario_uuid,
2440 "vendor": get_str(vnffg, "vendor", 255),
2441 "description": get_str(vld, "description", 255),
2442 }
2443 db_sce_vnffgs.append(db_sce_vnffg)
2444
2445 # deal with rsps
Igor D.Ccaadc442017-11-06 12:48:48 +00002446 for rsp in vnffg.get("rsp").itervalues():
2447 sce_rsp_uuid = str(uuid4())
2448 uuid_list.append(sce_rsp_uuid)
2449 db_sce_rsp = {
2450 "uuid": sce_rsp_uuid,
2451 "name": get_str(rsp, "name", 255),
2452 "sce_vnffg_id": sce_vnffg_uuid,
2453 "id": get_str(rsp, "id", 255), # only useful to link with classifiers; will be removed later in the code
2454 }
2455 db_sce_rsps.append(db_sce_rsp)
Igor D.Ccaadc442017-11-06 12:48:48 +00002456 for iface in rsp.get("vnfd-connection-point-ref").itervalues():
tierno16e3dd42018-04-24 12:52:40 +02002457 vnf_index = str(iface['member-vnf-index-ref'])
Igor D.Ccaadc442017-11-06 12:48:48 +00002458 if_order = int(iface['order'])
2459 # check correct parameters
2460 if vnf_index not in vnf_index2vnf_uuid:
2461 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2462 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2463 "'nsd':'constituent-vnfd'".format(
2464 str(nsd["id"]), str(rsp["id"]), str(iface["member-vnf-index-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002465 httperrors.Bad_Request)
Igor D.Ccaadc442017-11-06 12:48:48 +00002466
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002467 ingress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2468 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2469 WHERE={
2470 'vnf_id': vnf_index2vnf_uuid[vnf_index],
2471 'external_name': get_str(iface, "vnfd-ingress-connection-point-ref",
2472 255)})
2473 if not ingress_existing_ifaces:
Igor D.Ccaadc442017-11-06 12:48:48 +00002474 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002475 "-ref':'vnfd-ingress-connection-point-ref':'{}'. Reference to a non-existing "
Igor D.Ccaadc442017-11-06 12:48:48 +00002476 "connection-point name at VNFD '{}'".format(
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002477 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-ingress-connection-point-ref"]),
2478 str(iface.get("vnfd-id-ref"))[:255]), httperrors.Bad_Request)
2479
2480 egress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2481 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2482 WHERE={
2483 'vnf_id': vnf_index2vnf_uuid[vnf_index],
2484 'external_name': get_str(iface, "vnfd-egress-connection-point-ref",
2485 255)})
2486 if not egress_existing_ifaces:
2487 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2488 "-ref':'vnfd-egress-connection-point-ref':'{}'. Reference to a non-existing "
2489 "connection-point name at VNFD '{}'".format(
2490 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-egress-connection-point-ref"]),
2491 str(iface.get("vnfd-id-ref"))[:255]), HTTP_Bad_Request)
2492
2493 ingress_interface_uuid = ingress_existing_ifaces[0]["uuid"]
2494 egress_interface_uuid = egress_existing_ifaces[0]["uuid"]
Igor D.Ccaadc442017-11-06 12:48:48 +00002495 sce_rsp_hop_uuid = str(uuid4())
2496 uuid_list.append(sce_rsp_hop_uuid)
2497 db_sce_rsp_hop = {
2498 "uuid": sce_rsp_hop_uuid,
2499 "if_order": if_order,
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002500 "ingress_interface_id": ingress_interface_uuid,
2501 "egress_interface_id": egress_interface_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00002502 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2503 "sce_rsp_id": sce_rsp_uuid,
2504 }
2505 db_sce_rsp_hops.append(db_sce_rsp_hop)
2506
2507 # deal with classifiers
Igor D.Ccaadc442017-11-06 12:48:48 +00002508 for classifier in vnffg.get("classifier").itervalues():
2509 sce_classifier_uuid = str(uuid4())
2510 uuid_list.append(sce_classifier_uuid)
2511
2512 # source VNF
tierno16e3dd42018-04-24 12:52:40 +02002513 vnf_index = str(classifier['member-vnf-index-ref'])
Igor D.Ccaadc442017-11-06 12:48:48 +00002514 if vnf_index not in vnf_index2vnf_uuid:
2515 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point"
2516 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2517 "'nsd':'constituent-vnfd'".format(
2518 str(nsd["id"]), str(classifier["id"]), str(classifier["member-vnf-index-ref"])),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002519 httperrors.Bad_Request)
Igor D.Ccaadc442017-11-06 12:48:48 +00002520 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2521 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2522 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2523 'external_name': get_str(classifier, "vnfd-connection-point-ref",
2524 255)})
2525 if not existing_ifaces:
2526 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2527 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2528 "connection-point name at VNFD '{}'".format(
2529 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]),
2530 str(iface.get("vnfd-id-ref"))[:255]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002531 httperrors.Bad_Request)
Igor D.Ccaadc442017-11-06 12:48:48 +00002532 interface_uuid = existing_ifaces[0]["uuid"]
2533
2534 db_sce_classifier = {
2535 "uuid": sce_classifier_uuid,
2536 "name": get_str(classifier, "name", 255),
2537 "sce_vnffg_id": sce_vnffg_uuid,
2538 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2539 "interface_id": interface_uuid,
2540 }
2541 rsp_id = get_str(classifier, "rsp-id-ref", 255)
2542 rsp = next((item for item in db_sce_rsps if item["id"] == rsp_id), None)
2543 db_sce_classifier["sce_rsp_id"] = rsp["uuid"]
2544 db_sce_classifiers.append(db_sce_classifier)
2545
Igor D.Ccaadc442017-11-06 12:48:48 +00002546 for match in classifier.get("match-attributes").itervalues():
2547 sce_classifier_match_uuid = str(uuid4())
2548 uuid_list.append(sce_classifier_match_uuid)
2549 db_sce_classifier_match = {
2550 "uuid": sce_classifier_match_uuid,
2551 "ip_proto": get_str(match, "ip-proto", 2),
2552 "source_ip": get_str(match, "source-ip-address", 16),
2553 "destination_ip": get_str(match, "destination-ip-address", 16),
2554 "source_port": get_str(match, "source-port", 5),
2555 "destination_port": get_str(match, "destination-port", 5),
2556 "sce_classifier_id": sce_classifier_uuid,
2557 }
2558 db_sce_classifier_matches.append(db_sce_classifier_match)
2559 # TODO: vnf/cp keys
2560
2561 # remove unneeded id's in sce_rsps
2562 for rsp in db_sce_rsps:
2563 rsp.pop('id')
2564
tiernof1ba57e2017-09-07 12:23:19 +02002565 db_tables = [
2566 {"scenarios": db_scenarios},
2567 {"sce_nets": db_sce_nets},
2568 {"ip_profiles": db_ip_profiles},
2569 {"sce_vnfs": db_sce_vnfs},
2570 {"sce_interfaces": db_sce_interfaces},
Igor D.Ccaadc442017-11-06 12:48:48 +00002571 {"sce_vnffgs": db_sce_vnffgs},
2572 {"sce_rsps": db_sce_rsps},
2573 {"sce_rsp_hops": db_sce_rsp_hops},
2574 {"sce_classifiers": db_sce_classifiers},
2575 {"sce_classifier_matches": db_sce_classifier_matches},
tiernof1ba57e2017-09-07 12:23:19 +02002576 ]
2577
Igor D.Ccaadc442017-11-06 12:48:48 +00002578 logger.debug("new_nsd_v3 done: %s",
tiernof1ba57e2017-09-07 12:23:19 +02002579 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
2580 mydb.new_rows(db_tables, uuid_list)
2581 return nsd_uuid_list
tiernob2880eb2017-10-04 15:04:53 +02002582 except NfvoException:
2583 raise
tiernof1ba57e2017-09-07 12:23:19 +02002584 except Exception as e:
2585 logger.error("Exception {}".format(e))
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002586 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002587
2588
tierno7edb6752016-03-21 17:37:52 +01002589def edit_scenario(mydb, tenant_id, scenario_id, data):
2590 data["uuid"] = scenario_id
2591 data["tenant_id"] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02002592 c = mydb.edit_scenario( data )
2593 return c
tierno7edb6752016-03-21 17:37:52 +01002594
tiernob3d36742017-03-03 23:51:05 +01002595
tiernob8569aa2018-08-24 11:34:54 +02002596@deprecated("Use create_instance")
tierno7edb6752016-03-21 17:37:52 +01002597def 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 +02002598 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00002599 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
2600 vims = {datacenter_id: myvim}
tierno392f2852016-05-13 12:28:55 +02002601 myvim_tenant = myvim['tenant_id']
tierno7edb6752016-03-21 17:37:52 +01002602 datacenter_name = myvim['name']
tiernoa2793912016-10-04 08:15:08 +00002603
tierno7edb6752016-03-21 17:37:52 +01002604 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02002605 try:
2606 #print "Checking that the scenario_id exists and getting the scenario dictionary"
tierno868220c2017-09-26 00:11:05 +02002607 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id)
tiernoa2793912016-10-04 08:15:08 +00002608 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
tiernoae4a8d12016-07-08 12:30:39 +02002609 scenarioDict['datacenter_id'] = datacenter_id
2610 #print '================scenarioDict======================='
2611 #print json.dumps(scenarioDict, indent=4)
2612 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
tierno42026a02017-02-10 15:13:40 +01002613
tiernoae4a8d12016-07-08 12:30:39 +02002614 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
2615 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002616
tiernoae4a8d12016-07-08 12:30:39 +02002617 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2618 auxNetDict['scenario'] = {}
tierno42026a02017-02-10 15:13:40 +01002619
tiernoae4a8d12016-07-08 12:30:39 +02002620 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2621 for sce_net in scenarioDict['nets']:
2622 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
tierno42026a02017-02-10 15:13:40 +01002623
tiernoae4a8d12016-07-08 12:30:39 +02002624 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
tierno7edb6752016-03-21 17:37:52 +01002625 myNetName = myNetName[0:255] #limit length
tiernoae4a8d12016-07-08 12:30:39 +02002626 myNetType = sce_net['type']
tierno7edb6752016-03-21 17:37:52 +01002627 myNetDict = {}
2628 myNetDict["name"] = myNetName
2629 myNetDict["type"] = myNetType
2630 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02002631 myNetIPProfile = sce_net.get('ip_profile', None)
tierno7edb6752016-03-21 17:37:52 +01002632 #TODO:
tiernoae4a8d12016-07-08 12:30:39 +02002633 #We should use the dictionary as input parameter for new_network
tiernof97fd272016-07-11 14:32:37 +02002634 #print myNetDict
tiernoae4a8d12016-07-08 12:30:39 +02002635 if not sce_net["external"]:
garciadeblasebd66722019-01-31 16:01:31 +00002636 network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02002637 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2638 sce_net['vim_id'] = network_id
2639 auxNetDict['scenario'][sce_net['uuid']] = network_id
2640 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002641 sce_net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02002642 else:
2643 if sce_net['vim_id'] == None:
2644 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
2645 _, message = rollback(mydb, vims, rollbackList)
2646 logger.error("nfvo.start_scenario: %s", error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002647 raise NfvoException(error_text, httperrors.Bad_Request)
tiernoae4a8d12016-07-08 12:30:39 +02002648 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
2649 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
tierno42026a02017-02-10 15:13:40 +01002650
tiernoae4a8d12016-07-08 12:30:39 +02002651 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2652 #For each vnf net, we create it and we add it to instanceNetlist.
mirabal29356312017-07-27 12:21:22 +02002653
tiernoae4a8d12016-07-08 12:30:39 +02002654 for sce_vnf in scenarioDict['vnfs']:
2655 for net in sce_vnf['nets']:
2656 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
tierno42026a02017-02-10 15:13:40 +01002657
tiernoae4a8d12016-07-08 12:30:39 +02002658 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
2659 myNetName = myNetName[0:255] #limit length
2660 myNetType = net['type']
2661 myNetDict = {}
2662 myNetDict["name"] = myNetName
2663 myNetDict["type"] = myNetType
2664 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02002665 myNetIPProfile = net.get('ip_profile', None)
tiernoae4a8d12016-07-08 12:30:39 +02002666 #print myNetDict
2667 #TODO:
2668 #We should use the dictionary as input parameter for new_network
garciadeblasebd66722019-01-31 16:01:31 +00002669 network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02002670 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2671 net['vim_id'] = network_id
2672 if sce_vnf['uuid'] not in auxNetDict:
2673 auxNetDict[sce_vnf['uuid']] = {}
2674 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
2675 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002676 net["created"] = True
tierno42026a02017-02-10 15:13:40 +01002677
tiernoae4a8d12016-07-08 12:30:39 +02002678 #print "auxNetDict:"
2679 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002680
tiernoae4a8d12016-07-08 12:30:39 +02002681 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
2682 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2683 i = 0
2684 for sce_vnf in scenarioDict['vnfs']:
tierno5a3273c2017-08-29 11:43:46 +02002685 vnf_availability_zones = []
mirabal29356312017-07-27 12:21:22 +02002686 for vm in sce_vnf['vms']:
2687 vm_av = vm.get('availability_zone')
tierno5a3273c2017-08-29 11:43:46 +02002688 if vm_av and vm_av not in vnf_availability_zones:
2689 vnf_availability_zones.append(vm_av)
2690
2691 # check if there is enough availability zones available at vim level.
2692 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
2693 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002694 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors.Bad_Request)
tierno5a3273c2017-08-29 11:43:46 +02002695
tiernoae4a8d12016-07-08 12:30:39 +02002696 for vm in sce_vnf['vms']:
2697 i += 1
2698 myVMDict = {}
2699 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
tiernoae65a482016-11-24 16:20:05 +01002700 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
tiernoae4a8d12016-07-08 12:30:39 +02002701 #myVMDict['description'] = vm['description']
2702 myVMDict['description'] = myVMDict['name'][0:99]
2703 if not startvms:
2704 myVMDict['start'] = "no"
2705 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2706 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
tierno42026a02017-02-10 15:13:40 +01002707
tiernoae4a8d12016-07-08 12:30:39 +02002708 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002709 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno42026a02017-02-10 15:13:40 +01002710 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02002711 vm['vim_image_id'] = image_id
tierno42026a02017-02-10 15:13:40 +01002712
tiernoae4a8d12016-07-08 12:30:39 +02002713 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002714 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tiernoae4a8d12016-07-08 12:30:39 +02002715 if flavor_dict['extended']!=None:
2716 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
tierno42026a02017-02-10 15:13:40 +01002717 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02002718 vm['vim_flavor_id'] = flavor_id
tierno42026a02017-02-10 15:13:40 +01002719
2720
tiernoae4a8d12016-07-08 12:30:39 +02002721 myVMDict['imageRef'] = vm['vim_image_id']
2722 myVMDict['flavorRef'] = vm['vim_flavor_id']
2723 myVMDict['networks'] = []
2724 for iface in vm['interfaces']:
2725 netDict = {}
2726 if iface['type']=="data":
2727 netDict['type'] = iface['model']
2728 elif "model" in iface and iface["model"]!=None:
2729 netDict['model']=iface['model']
2730 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2731 #discover type of interface looking at flavor
2732 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2733 for flavor_iface in numa.get('interfaces',[]):
2734 if flavor_iface.get('name') == iface['internal_name']:
2735 if flavor_iface['dedicated'] == 'yes':
2736 netDict['type']="PF" #passthrough
2737 elif flavor_iface['dedicated'] == 'no':
2738 netDict['type']="VF" #siov
2739 elif flavor_iface['dedicated'] == 'yes:sriov':
2740 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2741 netDict["mac_address"] = flavor_iface.get("mac_address")
2742 break;
2743 netDict["use"]=iface['type']
2744 if netDict["use"]=="data" and not netDict.get("type"):
2745 #print "netDict", netDict
2746 #print "iface", iface
2747 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'])
2748 if flavor_dict.get('extended')==None:
tiernof97fd272016-07-11 14:32:37 +02002749 raise NfvoException(e_text + "After database migration some information is not available. \
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002750 Try to delete and create the scenarios and VNFs again", httperrors.Conflict)
tiernoae4a8d12016-07-08 12:30:39 +02002751 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002752 raise NfvoException(e_text, httperrors.Internal_Server_Error)
tiernoae4a8d12016-07-08 12:30:39 +02002753 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2754 netDict["type"]="virtual"
2755 if "vpci" in iface and iface["vpci"] is not None:
2756 netDict['vpci'] = iface['vpci']
2757 if "mac" in iface and iface["mac"] is not None:
2758 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00002759 if "port-security" in iface and iface["port-security"] is not None:
2760 netDict['port_security'] = iface['port-security']
2761 if "floating-ip" in iface and iface["floating-ip"] is not None:
2762 netDict['floating_ip'] = iface['floating-ip']
tiernoae4a8d12016-07-08 12:30:39 +02002763 netDict['name'] = iface['internal_name']
2764 if iface['net_id'] is None:
2765 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02002766 #print iface
2767 #print vnf_iface
tiernoae4a8d12016-07-08 12:30:39 +02002768 if vnf_iface['interface_id']==iface['uuid']:
2769 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
2770 break
2771 else:
2772 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
2773 #skip bridge ifaces not connected to any net
2774 #if 'net_id' not in netDict or netDict['net_id']==None:
2775 # continue
2776 myVMDict['networks'].append(netDict)
2777 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2778 #print myVMDict['name']
2779 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2780 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2781 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
mirabal29356312017-07-27 12:21:22 +02002782
2783 if 'availability_zone' in myVMDict:
tierno5a3273c2017-08-29 11:43:46 +02002784 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
mirabal29356312017-07-27 12:21:22 +02002785 else:
tierno5a3273c2017-08-29 11:43:46 +02002786 av_index = None
mirabal29356312017-07-27 12:21:22 +02002787
tierno98e909c2017-10-14 13:27:03 +02002788 vm_id, _ = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None),
mirabal29356312017-07-27 12:21:22 +02002789 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'],
tierno5a3273c2017-08-29 11:43:46 +02002790 availability_zone_index=av_index,
2791 availability_zone_list=vnf_availability_zones)
tiernoae4a8d12016-07-08 12:30:39 +02002792 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2793 vm['vim_id'] = vm_id
2794 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2795 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2796 for net in myVMDict['networks']:
2797 if "vim_id" in net:
2798 for iface in vm['interfaces']:
2799 if net["name"]==iface["internal_name"]:
2800 iface["vim_id"]=net["vim_id"]
2801 break
tierno42026a02017-02-10 15:13:40 +01002802
tiernoae4a8d12016-07-08 12:30:39 +02002803 logger.debug("start scenario Deployment done")
2804 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2805 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
tiernof97fd272016-07-11 14:32:37 +02002806 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
2807 return mydb.get_instance_scenario(instance_id)
tierno42026a02017-02-10 15:13:40 +01002808
tiernof97fd272016-07-11 14:32:37 +02002809 except (db_base_Exception, vimconn.vimconnException) as e:
tiernoae4a8d12016-07-08 12:30:39 +02002810 _, message = rollback(mydb, vims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02002811 if isinstance(e, db_base_Exception):
2812 error_text = "Exception at database"
2813 else:
2814 error_text = "Exception at VIM"
2815 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2816 #logger.error("start_scenario %s", error_text)
2817 raise NfvoException(error_text, e.http_code)
tierno7edb6752016-03-21 17:37:52 +01002818
tierno36c0b172017-01-12 18:32:28 +01002819def unify_cloud_config(cloud_config_preserve, cloud_config):
tierno40e1bce2017-08-09 09:12:04 +02002820 """ join the cloud config information into cloud_config_preserve.
tierno36c0b172017-01-12 18:32:28 +01002821 In case of conflict cloud_config_preserve preserves
tierno40e1bce2017-08-09 09:12:04 +02002822 None is allowed
2823 """
tierno36c0b172017-01-12 18:32:28 +01002824 if not cloud_config_preserve and not cloud_config:
2825 return None
2826
2827 new_cloud_config = {"key-pairs":[], "users":[]}
2828 # key-pairs
2829 if cloud_config_preserve:
2830 for key in cloud_config_preserve.get("key-pairs", () ):
2831 if key not in new_cloud_config["key-pairs"]:
2832 new_cloud_config["key-pairs"].append(key)
2833 if cloud_config:
2834 for key in cloud_config.get("key-pairs", () ):
2835 if key not in new_cloud_config["key-pairs"]:
2836 new_cloud_config["key-pairs"].append(key)
2837 if not new_cloud_config["key-pairs"]:
2838 del new_cloud_config["key-pairs"]
2839
2840 # users
2841 if cloud_config:
2842 new_cloud_config["users"] += cloud_config.get("users", () )
2843 if cloud_config_preserve:
2844 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
tiernoa4e1a6e2016-08-31 14:19:40 +02002845 index_to_delete = []
tierno36c0b172017-01-12 18:32:28 +01002846 users = new_cloud_config.get("users", [])
tiernoa4e1a6e2016-08-31 14:19:40 +02002847 for index0 in range(0,len(users)):
2848 if index0 in index_to_delete:
2849 continue
2850 for index1 in range(index0+1,len(users)):
2851 if index1 in index_to_delete:
2852 continue
2853 if users[index0]["name"] == users[index1]["name"]:
2854 index_to_delete.append(index1)
2855 for key in users[index1].get("key-pairs",()):
tierno36c0b172017-01-12 18:32:28 +01002856 if "key-pairs" not in users[index0]:
tiernoa4e1a6e2016-08-31 14:19:40 +02002857 users[index0]["key-pairs"] = [key]
2858 elif key not in users[index0]["key-pairs"]:
2859 users[index0]["key-pairs"].append(key)
2860 index_to_delete.sort(reverse=True)
2861 for index in index_to_delete:
2862 del users[index]
tierno36c0b172017-01-12 18:32:28 +01002863 if not new_cloud_config["users"]:
2864 del new_cloud_config["users"]
2865
2866 #boot-data-drive
2867 if cloud_config and cloud_config.get("boot-data-drive") != None:
2868 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
2869 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
2870 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
2871
2872 # user-data
tierno40e1bce2017-08-09 09:12:04 +02002873 new_cloud_config["user-data"] = []
2874 if cloud_config and cloud_config.get("user-data"):
2875 if isinstance(cloud_config["user-data"], list):
2876 new_cloud_config["user-data"] += cloud_config["user-data"]
2877 else:
2878 new_cloud_config["user-data"].append(cloud_config["user-data"])
2879 if cloud_config_preserve and cloud_config_preserve.get("user-data"):
2880 if isinstance(cloud_config_preserve["user-data"], list):
2881 new_cloud_config["user-data"] += cloud_config_preserve["user-data"]
2882 else:
2883 new_cloud_config["user-data"].append(cloud_config_preserve["user-data"])
2884 if not new_cloud_config["user-data"]:
2885 del new_cloud_config["user-data"]
tierno36c0b172017-01-12 18:32:28 +01002886
2887 # config files
2888 new_cloud_config["config-files"] = []
2889 if cloud_config and cloud_config.get("config-files") != None:
2890 new_cloud_config["config-files"] += cloud_config["config-files"]
2891 if cloud_config_preserve:
2892 for file in cloud_config_preserve.get("config-files", ()):
2893 for index in range(0, len(new_cloud_config["config-files"])):
2894 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
2895 new_cloud_config["config-files"][index] = file
2896 break
2897 else:
2898 new_cloud_config["config-files"].append(file)
2899 if not new_cloud_config["config-files"]:
2900 del new_cloud_config["config-files"]
2901 return new_cloud_config
2902
2903
tierno867ffe92017-03-27 12:50:34 +02002904def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
tiernob3d36742017-03-03 23:51:05 +01002905 datacenter_id = None
2906 datacenter_name = None
2907 thread = None
tierno867ffe92017-03-27 12:50:34 +02002908 try:
2909 if datacenter_tenant_id:
2910 thread_id = datacenter_tenant_id
2911 thread = vim_threads["running"].get(datacenter_tenant_id)
tiernob3d36742017-03-03 23:51:05 +01002912 else:
tierno867ffe92017-03-27 12:50:34 +02002913 where_={"td.nfvo_tenant_id": tenant_id}
2914 if datacenter_id_name:
2915 if utils.check_valid_uuid(datacenter_id_name):
2916 datacenter_id = datacenter_id_name
2917 where_["dt.datacenter_id"] = datacenter_id
2918 else:
2919 datacenter_name = datacenter_id_name
2920 where_["d.name"] = datacenter_name
2921 if datacenter_tenant_id:
2922 where_["dt.uuid"] = datacenter_tenant_id
2923 datacenters = mydb.get_rows(
2924 SELECT=("dt.uuid as datacenter_tenant_id",),
2925 FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2926 "join datacenters as d on d.uuid=dt.datacenter_id",
2927 WHERE=where_)
2928 if len(datacenters) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002929 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
tierno867ffe92017-03-27 12:50:34 +02002930 elif datacenters:
2931 thread_id = datacenters[0]["datacenter_tenant_id"]
2932 thread = vim_threads["running"].get(thread_id)
2933 if not thread:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002934 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found)
tierno867ffe92017-03-27 12:50:34 +02002935 return thread_id, thread
2936 except db_base_Exception as e:
2937 raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code)
tiernoa4e1a6e2016-08-31 14:19:40 +02002938
tiernof5755962017-07-13 15:44:34 +02002939
tiernoa15c4b92017-10-05 12:41:44 +02002940def get_datacenter_uuid(mydb, tenant_id, datacenter_id_name):
2941 WHERE_dict={}
2942 if utils.check_valid_uuid(datacenter_id_name):
2943 WHERE_dict['d.uuid'] = datacenter_id_name
2944 else:
2945 WHERE_dict['d.name'] = datacenter_id_name
2946
2947 if tenant_id:
2948 WHERE_dict['nfvo_tenant_id'] = tenant_id
2949 from_= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
2950 " dt on td.datacenter_tenant_id=dt.uuid"
2951 else:
2952 from_ = 'datacenters as d'
tiernod3750b32018-07-20 15:33:08 +02002953 vimaccounts = mydb.get_rows(FROM=from_, SELECT=("d.uuid as uuid, d.name as name",), WHERE=WHERE_dict )
tiernoa15c4b92017-10-05 12:41:44 +02002954 if len(vimaccounts) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002955 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found)
tiernoa15c4b92017-10-05 12:41:44 +02002956 elif len(vimaccounts)>1:
2957 #print "nfvo.datacenter_action() error. Several datacenters found"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002958 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
tiernod3750b32018-07-20 15:33:08 +02002959 return vimaccounts[0]["uuid"], vimaccounts[0]["name"]
tiernoa15c4b92017-10-05 12:41:44 +02002960
2961
tiernoa2793912016-10-04 08:15:08 +00002962def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
tiernobe41e222016-09-02 15:16:13 +02002963 datacenter_id = None
2964 datacenter_name = None
2965 if datacenter_id_name:
tierno42026a02017-02-10 15:13:40 +01002966 if utils.check_valid_uuid(datacenter_id_name):
tiernobe41e222016-09-02 15:16:13 +02002967 datacenter_id = datacenter_id_name
2968 else:
2969 datacenter_name = datacenter_id_name
tiernoa2793912016-10-04 08:15:08 +00002970 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
tiernobe41e222016-09-02 15:16:13 +02002971 if len(vims) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002972 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found)
tiernobe41e222016-09-02 15:16:13 +02002973 elif len(vims)>1:
2974 #print "nfvo.datacenter_action() error. Several datacenters found"
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01002975 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
tiernobe41e222016-09-02 15:16:13 +02002976 return vims.keys()[0], vims.values()[0]
2977
tiernob3d36742017-03-03 23:51:05 +01002978
garciadeblas9f8456e2016-09-05 05:02:59 +02002979def update(d, u):
Eduardo Sousa16cfd562018-11-30 15:33:35 +00002980 """Takes dict d and updates it with the values in dict u.
2981 It merges all depth levels"""
garciadeblas9f8456e2016-09-05 05:02:59 +02002982 for k, v in u.iteritems():
2983 if isinstance(v, collections.Mapping):
2984 r = update(d.get(k, {}), v)
2985 d[k] = r
2986 else:
2987 d[k] = u[k]
2988 return d
2989
tierno16e3dd42018-04-24 12:52:40 +02002990
tierno7edb6752016-03-21 17:37:52 +01002991def create_instance(mydb, tenant_id, instance_dict):
tiernob3d36742017-03-03 23:51:05 +01002992 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2993 # logger.debug("Creating instance...")
tierno7edb6752016-03-21 17:37:52 +01002994 scenario = instance_dict["scenario"]
tierno42026a02017-02-10 15:13:40 +01002995
tierno868220c2017-09-26 00:11:05 +02002996 # find main datacenter
tiernobe41e222016-09-02 15:16:13 +02002997 myvims = {}
tierno867ffe92017-03-27 12:50:34 +02002998 myvim_threads_id = {}
tierno7edb6752016-03-21 17:37:52 +01002999 datacenter = instance_dict.get("datacenter")
tiernofc7cfbf2019-03-20 17:23:45 +00003000 default_wim_account = instance_dict.get("wim_account")
tiernobe41e222016-09-02 15:16:13 +02003001 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3002 myvims[default_datacenter_id] = vim
tierno867ffe92017-03-27 12:50:34 +02003003 myvim_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id)
gcalvinoe580c7d2017-09-22 14:09:51 +02003004 tenant = mydb.get_rows_by_id('nfvo_tenants', tenant_id)
tierno868220c2017-09-26 00:11:05 +02003005 # myvim_tenant = myvim['tenant_id']
tierno16e3dd42018-04-24 12:52:40 +02003006 rollbackList = []
tierno42026a02017-02-10 15:13:40 +01003007
tierno868220c2017-09-26 00:11:05 +02003008 # print "Checking that the scenario exists and getting the scenario dictionary"
tierno7fe82642018-11-26 14:14:51 +00003009 if isinstance(scenario, str):
3010 scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_vim_id=myvim_threads_id[default_datacenter_id],
3011 datacenter_id=default_datacenter_id)
3012 else:
3013 scenarioDict = scenario
3014 scenarioDict["uuid"] = None
tierno42026a02017-02-10 15:13:40 +01003015
tierno868220c2017-09-26 00:11:05 +02003016 # logger.debug(">>>>>> Dictionaries before merging")
3017 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
3018 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
tierno42026a02017-02-10 15:13:40 +01003019
tierno868220c2017-09-26 00:11:05 +02003020 db_instance_vnfs = []
3021 db_instance_vms = []
3022 db_instance_interfaces = []
Igor D.Ccaadc442017-11-06 12:48:48 +00003023 db_instance_sfis = []
3024 db_instance_sfs = []
3025 db_instance_classifications = []
3026 db_instance_sfps = []
tierno868220c2017-09-26 00:11:05 +02003027 db_ip_profiles = []
3028 db_vim_actions = []
tierno8e690322017-08-10 15:58:50 +02003029 uuid_list = []
tierno868220c2017-09-26 00:11:05 +02003030 task_index = 0
tierno8e690322017-08-10 15:58:50 +02003031 instance_name = instance_dict["name"]
3032 instance_uuid = str(uuid4())
3033 uuid_list.append(instance_uuid)
3034 db_instance_scenario = {
3035 "uuid": instance_uuid,
3036 "name": instance_name,
3037 "tenant_id": tenant_id,
3038 "scenario_id": scenarioDict['uuid'],
3039 "datacenter_id": default_datacenter_id,
3040 # filled bellow 'datacenter_tenant_id'
3041 "description": instance_dict.get("description"),
3042 }
tierno8e690322017-08-10 15:58:50 +02003043 if scenarioDict.get("cloud-config"):
3044 db_instance_scenario["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"],
3045 default_flow_style=True, width=256)
tierno868220c2017-09-26 00:11:05 +02003046 instance_action_id = get_task_id()
3047 db_instance_action = {
3048 "uuid": instance_action_id, # same uuid for the instance and the action on create
3049 "tenant_id": tenant_id,
3050 "instance_id": instance_uuid,
3051 "description": "CREATE",
3052 }
garciadeblas9f8456e2016-09-05 05:02:59 +02003053
tierno868220c2017-09-26 00:11:05 +02003054 # Auxiliary dictionaries from x to y
tierno8e690322017-08-10 15:58:50 +02003055 sce_net2instance = {}
tierno868220c2017-09-26 00:11:05 +02003056 net2task_id = {'scenario': {}}
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003057 # Mapping between local networks and WIMs
3058 wim_usage = {}
tierno42026a02017-02-10 15:13:40 +01003059
tierno1df468d2018-07-06 14:25:16 +02003060 def ip_profile_IM2RO(ip_profile_im):
3061 # translate from input format to database format
3062 ip_profile_ro = {}
3063 if 'subnet-address' in ip_profile_im:
3064 ip_profile_ro['subnet_address'] = ip_profile_im['subnet-address']
3065 if 'ip-version' in ip_profile_im:
3066 ip_profile_ro['ip_version'] = ip_profile_im['ip-version']
3067 if 'gateway-address' in ip_profile_im:
3068 ip_profile_ro['gateway_address'] = ip_profile_im['gateway-address']
3069 if 'dns-address' in ip_profile_im:
3070 ip_profile_ro['dns_address'] = ip_profile_im['dns-address']
3071 if isinstance(ip_profile_ro['dns_address'], (list, tuple)):
3072 ip_profile_ro['dns_address'] = ";".join(ip_profile_ro['dns_address'])
3073 if 'dhcp' in ip_profile_im:
3074 ip_profile_ro['dhcp_start_address'] = ip_profile_im['dhcp'].get('start-address')
3075 ip_profile_ro['dhcp_enabled'] = ip_profile_im['dhcp'].get('enabled', True)
3076 ip_profile_ro['dhcp_count'] = ip_profile_im['dhcp'].get('count')
3077 return ip_profile_ro
3078
tierno868220c2017-09-26 00:11:05 +02003079 # logger.debug("Creating instance from scenario-dict:\n%s",
3080 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01003081 try:
tiernob3d36742017-03-03 23:51:05 +01003082 # 0 check correct parameters
tierno868220c2017-09-26 00:11:05 +02003083 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01003084 for scenario_net in scenarioDict['nets']:
tierno1df468d2018-07-06 14:25:16 +02003085 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 +01003086 break
tierno1df468d2018-07-06 14:25:16 +02003087 else:
3088 raise NfvoException("Invalid scenario network name or id '{}' at instance:networks".format(net_name),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003089 httperrors.Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02003090 if "sites" not in net_instance_desc:
3091 net_instance_desc["sites"] = [ {} ]
3092 site_without_datacenter_field = False
3093 for site in net_instance_desc["sites"]:
3094 if site.get("datacenter"):
tiernod3750b32018-07-20 15:33:08 +02003095 site["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, site["datacenter"])
tiernobe41e222016-09-02 15:16:13 +02003096 if site["datacenter"] not in myvims:
tierno868220c2017-09-26 00:11:05 +02003097 # Add this datacenter to myvims
tiernobe41e222016-09-02 15:16:13 +02003098 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
3099 myvims[d] = v
tierno868220c2017-09-26 00:11:05 +02003100 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, site["datacenter"])
3101 site["datacenter"] = d # change name to id
tiernobe41e222016-09-02 15:16:13 +02003102 else:
3103 if site_without_datacenter_field:
tierno868220c2017-09-26 00:11:05 +02003104 raise NfvoException("Found more than one entries without datacenter field at "
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003105 "instance:networks:{}:sites".format(net_name), httperrors.Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02003106 site_without_datacenter_field = True
tierno868220c2017-09-26 00:11:05 +02003107 site["datacenter"] = default_datacenter_id # change name to id
tierno42026a02017-02-10 15:13:40 +01003108
tiernobe41e222016-09-02 15:16:13 +02003109 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01003110 for scenario_vnf in scenarioDict['vnfs']:
tierno1df468d2018-07-06 14:25:16 +02003111 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 +01003112 break
tierno1df468d2018-07-06 14:25:16 +02003113 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003114 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name), httperrors.Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02003115 if "datacenter" in vnf_instance_desc:
tierno868220c2017-09-26 00:11:05 +02003116 # Add this datacenter to myvims
tiernod3750b32018-07-20 15:33:08 +02003117 vnf_instance_desc["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
tiernobe41e222016-09-02 15:16:13 +02003118 if vnf_instance_desc["datacenter"] not in myvims:
3119 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
3120 myvims[d] = v
tierno868220c2017-09-26 00:11:05 +02003121 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"])
tiernoa2793912016-10-04 08:15:08 +00003122 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
garciadeblas30833382017-01-09 09:46:31 +01003123
tierno1df468d2018-07-06 14:25:16 +02003124 for net_id, net_instance_desc in vnf_instance_desc.get("networks", {}).iteritems():
3125 for scenario_net in scenario_vnf['nets']:
3126 if net_id == scenario_net['osm_id'] or net_id == scenario_net['uuid'] or net_id == scenario_net["name"]:
3127 break
3128 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003129 raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id), httperrors.Bad_Request)
tierno1df468d2018-07-06 14:25:16 +02003130 if net_instance_desc.get("vim-network-name"):
3131 scenario_net["vim-network-name"] = net_instance_desc["vim-network-name"]
gcalvino0a480542018-12-17 16:19:33 +01003132 if net_instance_desc.get("vim-network-id"):
3133 scenario_net["vim-network-id"] = net_instance_desc["vim-network-id"]
tierno1df468d2018-07-06 14:25:16 +02003134 if net_instance_desc.get("name"):
3135 scenario_net["name"] = net_instance_desc["name"]
3136 if 'ip-profile' in net_instance_desc:
3137 ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile'])
3138 if 'ip_profile' not in scenario_net:
3139 scenario_net['ip_profile'] = ipprofile_db
3140 else:
3141 update(scenario_net['ip_profile'], ipprofile_db)
3142
3143 for vdu_id, vdu_instance_desc in vnf_instance_desc.get("vdus", {}).iteritems():
3144 for scenario_vm in scenario_vnf['vms']:
3145 if vdu_id == scenario_vm['osm_id'] or vdu_id == scenario_vm["name"]:
3146 break
3147 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003148 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), httperrors.Bad_Request)
tierno1df468d2018-07-06 14:25:16 +02003149 scenario_vm["instance_parameters"] = vdu_instance_desc
3150 for iface_id, iface_instance_desc in vdu_instance_desc.get("interfaces", {}).iteritems():
3151 for scenario_interface in scenario_vm['interfaces']:
3152 if iface_id == scenario_interface['internal_name'] or iface_id == scenario_interface["external_name"]:
3153 scenario_interface.update(iface_instance_desc)
3154 break
3155 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003156 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), httperrors.Bad_Request)
tierno1df468d2018-07-06 14:25:16 +02003157
tierno868220c2017-09-26 00:11:05 +02003158 # 0.1 parse cloud-config parameters
tierno36c0b172017-01-12 18:32:28 +01003159 cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config"))
garciadeblas9f8456e2016-09-05 05:02:59 +02003160
tierno868220c2017-09-26 00:11:05 +02003161 # 0.2 merge instance information into scenario
3162 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
3163 # However, this is not possible yet.
tierno41a69812018-02-16 14:34:33 +01003164 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
garciadeblas9f8456e2016-09-05 05:02:59 +02003165 for scenario_net in scenarioDict['nets']:
tiernofc7cfbf2019-03-20 17:23:45 +00003166 if net_name == scenario_net.get("name") or net_name == scenario_net.get("osm_id") or net_name == scenario_net.get("uuid"):
3167 if "wim_account" in net_instance_desc and net_instance_desc["wim_account"] is not None:
3168 scenario_net["wim_account"] = net_instance_desc["wim_account"]
garciadeblas9f8456e2016-09-05 05:02:59 +02003169 if 'ip-profile' in net_instance_desc:
tierno1df468d2018-07-06 14:25:16 +02003170 ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile'])
garciadeblasedca7b32016-09-29 14:01:52 +00003171 if 'ip_profile' not in scenario_net:
tierno455612d2017-05-30 16:40:10 +02003172 scenario_net['ip_profile'] = ipprofile_db
garciadeblasedca7b32016-09-29 14:01:52 +00003173 else:
tierno455612d2017-05-30 16:40:10 +02003174 update(scenario_net['ip_profile'], ipprofile_db)
tierno41a69812018-02-16 14:34:33 +01003175 for interface in net_instance_desc.get('interfaces', ()):
garciadeblas9f8456e2016-09-05 05:02:59 +02003176 if 'ip_address' in interface:
3177 for vnf in scenarioDict['vnfs']:
3178 if interface['vnf'] == vnf['name']:
3179 for vnf_interface in vnf['interfaces']:
3180 if interface['vnf_interface'] == vnf_interface['external_name']:
tierno41a69812018-02-16 14:34:33 +01003181 vnf_interface['ip_address'] = interface['ip_address']
garciadeblas9f8456e2016-09-05 05:02:59 +02003182
tierno868220c2017-09-26 00:11:05 +02003183 # logger.debug(">>>>>>>> Merged dictionary")
3184 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
3185 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
garciadeblas9f8456e2016-09-05 05:02:59 +02003186
tiernob3d36742017-03-03 23:51:05 +01003187 # 1. Creating new nets (sce_nets) in the VIM"
tierno8f79ea12018-05-03 17:37:40 +02003188 number_mgmt_networks = 0
tierno8e690322017-08-10 15:58:50 +02003189 db_instance_nets = []
tierno7edb6752016-03-21 17:37:52 +01003190 for sce_net in scenarioDict['nets']:
tierno7fe82642018-11-26 14:14:51 +00003191 sce_net_uuid = sce_net.get('uuid', sce_net["name"])
tierno1df468d2018-07-06 14:25:16 +02003192 # get involved datacenters where this network need to be created
3193 involved_datacenters = []
tierno7fe82642018-11-26 14:14:51 +00003194 for sce_vnf in scenarioDict.get("vnfs", ()):
tierno1df468d2018-07-06 14:25:16 +02003195 vnf_datacenter = sce_vnf.get("datacenter", default_datacenter_id)
3196 if vnf_datacenter in involved_datacenters:
3197 continue
3198 if sce_vnf.get("interfaces"):
3199 for sce_vnf_ifaces in sce_vnf["interfaces"]:
3200 if sce_vnf_ifaces.get("sce_net_id") == sce_net["uuid"]:
3201 involved_datacenters.append(vnf_datacenter)
3202 break
gcalvinod6fac4d2018-11-05 10:42:06 +01003203 if not involved_datacenters:
3204 involved_datacenters.append(default_datacenter_id)
tierno80391822019-03-21 22:12:14 +00003205 target_wim_account = sce_net.get("wim_account", default_wim_account)
tierno1df468d2018-07-06 14:25:16 +02003206
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003207 # --> WIM
3208 # TODO: use this information during network creation
tierno4070e442019-01-23 10:19:23 +00003209 wim_account_id = wim_account_name = None
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003210 if len(involved_datacenters) > 1 and 'uuid' in sce_net:
tiernofc7cfbf2019-03-20 17:23:45 +00003211 if target_wim_account is None or target_wim_account is True: # automatic selection of WIM
3212 # OBS: sce_net without uuid are used internally to VNFs
3213 # and the assumption is that VNFs will not be split among
3214 # different datacenters
3215 wim_account = wim_engine.find_suitable_wim_account(
3216 involved_datacenters, tenant_id)
3217 wim_account_id = wim_account['uuid']
3218 wim_account_name = wim_account['name']
3219 wim_usage[sce_net['uuid']] = wim_account_id
3220 elif isinstance(target_wim_account, str): # manual selection of WIM
3221 wim_account.persist.get_wim_account_by(target_wim_account, tenant_id)
3222 wim_account_id = wim_account['uuid']
3223 wim_account_name = wim_account['name']
3224 wim_usage[sce_net['uuid']] = wim_account_id
3225 else: # not WIM usage
3226 wim_usage[sce_net['uuid']] = False
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003227 # <-- WIM
3228
tierno1df468d2018-07-06 14:25:16 +02003229 descriptor_net = {}
tierno3c44e7b2019-03-04 17:32:01 +00003230 if instance_dict.get("networks"):
3231 if sce_net.get("uuid") in instance_dict["networks"]:
3232 descriptor_net = instance_dict["networks"][sce_net["uuid"]]
3233 descriptor_net_name = sce_net["uuid"]
3234 elif sce_net.get("osm_id") in instance_dict["networks"]:
3235 descriptor_net = instance_dict["networks"][sce_net["osm_id"]]
3236 descriptor_net_name = sce_net["osm_id"]
3237 elif sce_net["name"] in instance_dict["networks"]:
3238 descriptor_net = instance_dict["networks"][sce_net["name"]]
3239 descriptor_net_name = sce_net["name"]
tiernobe41e222016-09-02 15:16:13 +02003240 net_name = descriptor_net.get("vim-network-name")
tierno7fe82642018-11-26 14:14:51 +00003241 # add datacenters from instantiation parameters
3242 if descriptor_net.get("sites"):
3243 for site in descriptor_net["sites"]:
3244 if site.get("datacenter") and site["datacenter"] not in involved_datacenters:
3245 involved_datacenters.append(site["datacenter"])
3246 sce_net2instance[sce_net_uuid] = {}
3247 net2task_id['scenario'][sce_net_uuid] = {}
tiernobe41e222016-09-02 15:16:13 +02003248
tierno3c44e7b2019-03-04 17:32:01 +00003249 use_network = None
3250 related_network = None
3251 if descriptor_net.get("use-network"):
3252 target_instance_nets = mydb.get_rows(
3253 SELECT="related",
3254 FROM="instance_nets",
3255 WHERE={"instance_scenario_id": descriptor_net["use-network"]["instance_scenario_id"],
3256 "osm_id": descriptor_net["use-network"]["osm_id"]},
3257 )
3258 if not target_instance_nets:
3259 raise NfvoException(
3260 "Cannot find the target network at instance:networks[{}]:use-network".format(descriptor_net_name),
3261 httperrors.Bad_Request)
3262 else:
3263 use_network = target_instance_nets[0]["related"]
3264
tierno1df468d2018-07-06 14:25:16 +02003265 if sce_net["external"]:
3266 number_mgmt_networks += 1
3267
3268 for datacenter_id in involved_datacenters:
3269 netmap_use = None
3270 netmap_create = None
3271 if descriptor_net.get("sites"):
3272 for site in descriptor_net["sites"]:
3273 if site.get("datacenter") == datacenter_id:
3274 netmap_use = site.get("netmap-use")
3275 netmap_create = site.get("netmap-create")
3276 break
3277
3278 vim = myvims[datacenter_id]
3279 myvim_thread_id = myvim_threads_id[datacenter_id]
3280
tiernobe41e222016-09-02 15:16:13 +02003281 net_type = sce_net['type']
tiernob6990792018-11-13 10:37:42 +01003282 net_vim_name = None
tierno868220c2017-09-26 00:11:05 +02003283 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
tierno42026a02017-02-10 15:13:40 +01003284
tiernof1ba57e2017-09-07 12:23:19 +02003285 if not net_name:
3286 if sce_net["external"]:
3287 net_name = sce_net["name"]
3288 else:
tierno1df468d2018-07-06 14:25:16 +02003289 net_name = "{}-{}".format(instance_name, sce_net["name"])
tiernof1ba57e2017-09-07 12:23:19 +02003290 net_name = net_name[:255] # limit length
3291
tierno1df468d2018-07-06 14:25:16 +02003292 if netmap_use or netmap_create:
tiernof1ba57e2017-09-07 12:23:19 +02003293 create_network = False
3294 lookfor_network = False
tierno1df468d2018-07-06 14:25:16 +02003295 if netmap_use:
tiernof1ba57e2017-09-07 12:23:19 +02003296 lookfor_network = True
tierno1df468d2018-07-06 14:25:16 +02003297 if utils.check_valid_uuid(netmap_use):
3298 lookfor_filter["id"] = netmap_use
tiernof1ba57e2017-09-07 12:23:19 +02003299 else:
tierno1df468d2018-07-06 14:25:16 +02003300 lookfor_filter["name"] = netmap_use
3301 if netmap_create:
tiernof1ba57e2017-09-07 12:23:19 +02003302 create_network = True
3303 net_vim_name = net_name
tierno1df468d2018-07-06 14:25:16 +02003304 if isinstance(netmap_create, str):
3305 net_vim_name = netmap_create
tierno8f79ea12018-05-03 17:37:40 +02003306 elif sce_net.get("vim_network_name"):
3307 create_network = False
3308 lookfor_network = True
3309 lookfor_filter["name"] = sce_net.get("vim_network_name")
tiernof1ba57e2017-09-07 12:23:19 +02003310 elif sce_net["external"]:
tiernod108c412018-12-18 15:19:27 +00003311 if sce_net.get('vim_id'):
tierno868220c2017-09-26 00:11:05 +02003312 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
tiernobe41e222016-09-02 15:16:13 +02003313 create_network = False
3314 lookfor_network = True
3315 lookfor_filter["id"] = sce_net['vim_id']
tierno8f79ea12018-05-03 17:37:40 +02003316 elif vim["config"].get("management_network_id") or vim["config"].get("management_network_name"):
3317 if number_mgmt_networks > 1:
3318 raise NfvoException("Found several VLD of type mgmt. "
3319 "You must concrete what vim-network must be use for each one",
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003320 httperrors.Bad_Request)
tierno8f79ea12018-05-03 17:37:40 +02003321 create_network = False
3322 lookfor_network = True
3323 if vim["config"].get("management_network_id"):
3324 lookfor_filter["id"] = vim["config"]["management_network_id"]
3325 else:
3326 lookfor_filter["name"] = vim["config"]["management_network_name"]
tiernobe41e222016-09-02 15:16:13 +02003327 else:
tierno868220c2017-09-26 00:11:05 +02003328 # 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 +02003329 create_network = True
3330 lookfor_network = True
3331 lookfor_filter["name"] = sce_net["name"]
3332 net_vim_name = sce_net["name"]
tierno7edb6752016-03-21 17:37:52 +01003333 else:
tiernobe41e222016-09-02 15:16:13 +02003334 net_vim_name = net_name
3335 create_network = True
3336 lookfor_network = False
tierno42026a02017-02-10 15:13:40 +01003337
tiernof1450872017-10-17 23:15:08 +02003338 task_extra = {}
3339 if create_network:
3340 task_action = "CREATE"
tierno4070e442019-01-23 10:19:23 +00003341 task_extra["params"] = (net_vim_name, net_type, sce_net.get('ip_profile', None), wim_account_name)
tiernof1450872017-10-17 23:15:08 +02003342 if lookfor_network:
3343 task_extra["find"] = (lookfor_filter,)
tierno868220c2017-09-26 00:11:05 +02003344 elif lookfor_network:
3345 task_action = "FIND"
tiernof1450872017-10-17 23:15:08 +02003346 task_extra["params"] = (lookfor_filter,)
tierno42026a02017-02-10 15:13:40 +01003347
tierno8e690322017-08-10 15:58:50 +02003348 # fill database content
3349 net_uuid = str(uuid4())
3350 uuid_list.append(net_uuid)
tierno7fe82642018-11-26 14:14:51 +00003351 sce_net2instance[sce_net_uuid][datacenter_id] = net_uuid
tierno3c44e7b2019-03-04 17:32:01 +00003352 if not related_network: # all db_instance_nets will have same related
3353 related_network = use_network or net_uuid
tierno8e690322017-08-10 15:58:50 +02003354 db_net = {
3355 "uuid": net_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003356 "osm_id": sce_net.get("osm_id") or sce_net["name"],
3357 "related": related_network,
tierno868220c2017-09-26 00:11:05 +02003358 'vim_net_id': None,
tiernob6990792018-11-13 10:37:42 +01003359 "vim_name": net_vim_name,
tierno8e690322017-08-10 15:58:50 +02003360 "instance_scenario_id": instance_uuid,
tierno7fe82642018-11-26 14:14:51 +00003361 "sce_net_id": sce_net.get("uuid"),
tierno8e690322017-08-10 15:58:50 +02003362 "created": create_network,
3363 'datacenter_id': datacenter_id,
3364 'datacenter_tenant_id': myvim_thread_id,
tiernod2836fc2018-05-30 15:03:27 +02003365 'status': 'BUILD' # if create_network else "ACTIVE"
tierno8e690322017-08-10 15:58:50 +02003366 }
3367 db_instance_nets.append(db_net)
tierno868220c2017-09-26 00:11:05 +02003368 db_vim_action = {
3369 "instance_action_id": instance_action_id,
3370 "status": "SCHEDULED",
3371 "task_index": task_index,
3372 "datacenter_vim_id": myvim_thread_id,
3373 "action": task_action,
3374 "item": "instance_nets",
3375 "item_id": net_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003376 "related": related_network,
tiernof1450872017-10-17 23:15:08 +02003377 "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256)
tierno868220c2017-09-26 00:11:05 +02003378 }
tierno7fe82642018-11-26 14:14:51 +00003379 net2task_id['scenario'][sce_net_uuid][datacenter_id] = task_index
tierno868220c2017-09-26 00:11:05 +02003380 task_index += 1
3381 db_vim_actions.append(db_vim_action)
3382
tierno8e690322017-08-10 15:58:50 +02003383 if 'ip_profile' in sce_net:
3384 db_ip_profile={
3385 'instance_net_id': net_uuid,
3386 'ip_version': sce_net['ip_profile']['ip_version'],
3387 'subnet_address': sce_net['ip_profile']['subnet_address'],
3388 'gateway_address': sce_net['ip_profile']['gateway_address'],
3389 'dns_address': sce_net['ip_profile']['dns_address'],
3390 'dhcp_enabled': sce_net['ip_profile']['dhcp_enabled'],
3391 'dhcp_start_address': sce_net['ip_profile']['dhcp_start_address'],
3392 'dhcp_count': sce_net['ip_profile']['dhcp_count'],
3393 }
3394 db_ip_profiles.append(db_ip_profile)
3395
tierno16e3dd42018-04-24 12:52:40 +02003396 # Create VNFs
3397 vnf_params = {
3398 "default_datacenter_id": default_datacenter_id,
3399 "myvim_threads_id": myvim_threads_id,
3400 "instance_uuid": instance_uuid,
3401 "instance_name": instance_name,
3402 "instance_action_id": instance_action_id,
3403 "myvims": myvims,
3404 "cloud_config": cloud_config,
3405 "RO_pub_key": tenant[0].get('RO_pub_key'),
tierno67881db2018-10-24 18:46:03 +02003406 "instance_parameters": instance_dict,
tierno16e3dd42018-04-24 12:52:40 +02003407 }
3408 vnf_params_out = {
3409 "task_index": task_index,
3410 "uuid_list": uuid_list,
3411 "db_instance_nets": db_instance_nets,
3412 "db_vim_actions": db_vim_actions,
3413 "db_ip_profiles": db_ip_profiles,
3414 "db_instance_vnfs": db_instance_vnfs,
3415 "db_instance_vms": db_instance_vms,
3416 "db_instance_interfaces": db_instance_interfaces,
3417 "net2task_id": net2task_id,
3418 "sce_net2instance": sce_net2instance,
3419 }
tierno55d234c2018-07-04 18:29:21 +02003420 # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
tierno7fe82642018-11-26 14:14:51 +00003421 for sce_vnf in scenarioDict.get('vnfs', ()): # sce_vnf_list:
tierno16e3dd42018-04-24 12:52:40 +02003422 instantiate_vnf(mydb, sce_vnf, vnf_params, vnf_params_out, rollbackList)
3423 task_index = vnf_params_out["task_index"]
3424 uuid_list = vnf_params_out["uuid_list"]
mirabal29356312017-07-27 12:21:22 +02003425
tierno16e3dd42018-04-24 12:52:40 +02003426 # Create VNFFGs
3427 # task_depends_on = []
tierno7fe82642018-11-26 14:14:51 +00003428 for vnffg in scenarioDict.get('vnffgs', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00003429 for rsp in vnffg['rsps']:
3430 sfs_created = []
3431 for cp in rsp['connection_points']:
3432 count = mydb.get_rows(
Eduardo Sousa16cfd562018-11-30 15:33:35 +00003433 SELECT='vms.count',
3434 FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h "
3435 "on interfaces.uuid=h.ingress_interface_id",
Igor D.Ccaadc442017-11-06 12:48:48 +00003436 WHERE={'h.uuid': cp['uuid']})[0]['count']
3437 instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == cp['sce_vnf_id']), None)
3438 instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']]
3439 dependencies = []
3440 for instance_vm in instance_vms:
3441 action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None)
3442 if action:
3443 dependencies.append(action['task_index'])
3444 # TODO: throw exception if count != len(instance_vms)
3445 # TODO: and action shouldn't ever be None
3446 sfis_created = []
3447 for i in range(count):
3448 # create sfis
3449 sfi_uuid = str(uuid4())
Eduardo Sousa16cfd562018-11-30 15:33:35 +00003450 extra_params = {
3451 "ingress_interface_id": cp["ingress_interface_id"],
3452 "egress_interface_id": cp["egress_interface_id"]
3453 }
Igor D.Ccaadc442017-11-06 12:48:48 +00003454 uuid_list.append(sfi_uuid)
3455 db_sfi = {
3456 "uuid": sfi_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003457 "related": sfi_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003458 "instance_scenario_id": instance_uuid,
3459 'sce_rsp_hop_id': cp['uuid'],
3460 'datacenter_id': datacenter_id,
3461 'datacenter_tenant_id': myvim_thread_id,
3462 "vim_sfi_id": None, # vim thread will populate
3463 }
3464 db_instance_sfis.append(db_sfi)
3465 db_vim_action = {
3466 "instance_action_id": instance_action_id,
3467 "task_index": task_index,
3468 "datacenter_vim_id": myvim_thread_id,
3469 "action": "CREATE",
3470 "status": "SCHEDULED",
3471 "item": "instance_sfis",
3472 "item_id": sfi_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003473 "related": sfi_uuid,
Eduardo Sousa16cfd562018-11-30 15:33:35 +00003474 "extra": yaml.safe_dump({"params": extra_params, "depends_on": [dependencies[i]]},
Igor D.Ccaadc442017-11-06 12:48:48 +00003475 default_flow_style=True, width=256)
3476 }
3477 sfis_created.append(task_index)
3478 task_index += 1
3479 db_vim_actions.append(db_vim_action)
3480 # create sfs
3481 sf_uuid = str(uuid4())
3482 uuid_list.append(sf_uuid)
3483 db_sf = {
3484 "uuid": sf_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003485 "related": sf_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003486 "instance_scenario_id": instance_uuid,
3487 'sce_rsp_hop_id': cp['uuid'],
3488 'datacenter_id': datacenter_id,
3489 'datacenter_tenant_id': myvim_thread_id,
3490 "vim_sf_id": None, # vim thread will populate
3491 }
3492 db_instance_sfs.append(db_sf)
3493 db_vim_action = {
3494 "instance_action_id": instance_action_id,
3495 "task_index": task_index,
3496 "datacenter_vim_id": myvim_thread_id,
3497 "action": "CREATE",
3498 "status": "SCHEDULED",
3499 "item": "instance_sfs",
3500 "item_id": sf_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003501 "related": sf_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003502 "extra": yaml.safe_dump({"params": "", "depends_on": sfis_created},
3503 default_flow_style=True, width=256)
3504 }
3505 sfs_created.append(task_index)
3506 task_index += 1
3507 db_vim_actions.append(db_vim_action)
3508 classifier = rsp['classifier']
3509
3510 # TODO the following ~13 lines can be reused for the sfi case
3511 count = mydb.get_rows(
3512 SELECT=('vms.count'),
3513 FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id",
3514 WHERE={'c.uuid': classifier['uuid']})[0]['count']
3515 instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == classifier['sce_vnf_id']), None)
3516 instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']]
3517 dependencies = []
3518 for instance_vm in instance_vms:
3519 action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None)
3520 if action:
3521 dependencies.append(action['task_index'])
3522 # TODO: throw exception if count != len(instance_vms)
3523 # TODO: and action shouldn't ever be None
3524 classifications_created = []
3525 for i in range(count):
3526 for match in classifier['matches']:
3527 # create classifications
3528 classification_uuid = str(uuid4())
3529 uuid_list.append(classification_uuid)
3530 db_classification = {
3531 "uuid": classification_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003532 "related": classification_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003533 "instance_scenario_id": instance_uuid,
3534 'sce_classifier_match_id': match['uuid'],
3535 'datacenter_id': datacenter_id,
3536 'datacenter_tenant_id': myvim_thread_id,
3537 "vim_classification_id": None, # vim thread will populate
3538 }
3539 db_instance_classifications.append(db_classification)
3540 classification_params = {
3541 "ip_proto": match["ip_proto"],
3542 "source_ip": match["source_ip"],
3543 "destination_ip": match["destination_ip"],
3544 "source_port": match["source_port"],
3545 "destination_port": match["destination_port"]
3546 }
3547 db_vim_action = {
3548 "instance_action_id": instance_action_id,
3549 "task_index": task_index,
3550 "datacenter_vim_id": myvim_thread_id,
3551 "action": "CREATE",
3552 "status": "SCHEDULED",
3553 "item": "instance_classifications",
3554 "item_id": classification_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003555 "related": classification_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003556 "extra": yaml.safe_dump({"params": classification_params, "depends_on": [dependencies[i]]},
3557 default_flow_style=True, width=256)
3558 }
3559 classifications_created.append(task_index)
3560 task_index += 1
3561 db_vim_actions.append(db_vim_action)
3562
3563 # create sfps
3564 sfp_uuid = str(uuid4())
3565 uuid_list.append(sfp_uuid)
3566 db_sfp = {
3567 "uuid": sfp_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003568 "related": sfp_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003569 "instance_scenario_id": instance_uuid,
3570 'sce_rsp_id': rsp['uuid'],
3571 'datacenter_id': datacenter_id,
3572 'datacenter_tenant_id': myvim_thread_id,
3573 "vim_sfp_id": None, # vim thread will populate
3574 }
3575 db_instance_sfps.append(db_sfp)
3576 db_vim_action = {
3577 "instance_action_id": instance_action_id,
3578 "task_index": task_index,
3579 "datacenter_vim_id": myvim_thread_id,
3580 "action": "CREATE",
3581 "status": "SCHEDULED",
3582 "item": "instance_sfps",
3583 "item_id": sfp_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003584 "related": sfp_uuid,
Igor D.Ccaadc442017-11-06 12:48:48 +00003585 "extra": yaml.safe_dump({"params": "", "depends_on": sfs_created + classifications_created},
3586 default_flow_style=True, width=256)
3587 }
3588 task_index += 1
3589 db_vim_actions.append(db_vim_action)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003590 db_instance_action["number_tasks"] = task_index
3591
3592 # --> WIM
Anderson Bravalherie2c09f32018-11-30 09:55:29 +00003593 logger.debug('wim_usage:\n%s\n\n', pformat(wim_usage))
3594 wan_links = wim_engine.derive_wan_links(wim_usage, db_instance_nets, tenant_id)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003595 wim_actions = wim_engine.create_actions(wan_links)
3596 wim_actions, db_instance_action = (
3597 wim_engine.incorporate_actions(wim_actions, db_instance_action))
3598 # <-- WIM
Igor D.Ccaadc442017-11-06 12:48:48 +00003599
tierno867ffe92017-03-27 12:50:34 +02003600 scenarioDict["datacenter2tenant"] = myvim_threads_id
tierno8e690322017-08-10 15:58:50 +02003601
3602 db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id]
3603 db_instance_scenario['datacenter_id'] = default_datacenter_id
3604 db_tables=[
3605 {"instance_scenarios": db_instance_scenario},
3606 {"instance_vnfs": db_instance_vnfs},
3607 {"instance_nets": db_instance_nets},
3608 {"ip_profiles": db_ip_profiles},
3609 {"instance_vms": db_instance_vms},
3610 {"instance_interfaces": db_instance_interfaces},
tierno868220c2017-09-26 00:11:05 +02003611 {"instance_actions": db_instance_action},
Igor D.Ccaadc442017-11-06 12:48:48 +00003612 {"instance_sfis": db_instance_sfis},
3613 {"instance_sfs": db_instance_sfs},
3614 {"instance_classifications": db_instance_classifications},
3615 {"instance_sfps": db_instance_sfps},
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003616 {"instance_wim_nets": wan_links},
3617 {"vim_wim_actions": db_vim_actions + wim_actions}
tierno8e690322017-08-10 15:58:50 +02003618 ]
3619
tierno868220c2017-09-26 00:11:05 +02003620 logger.debug("create_instance done DB tables: %s",
tierno8e690322017-08-10 15:58:50 +02003621 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
3622 mydb.new_rows(db_tables, uuid_list)
tierno868220c2017-09-26 00:11:05 +02003623 for myvim_thread_id in myvim_threads_id.values():
3624 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
tierno867ffe92017-03-27 12:50:34 +02003625
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003626 wim_engine.dispatch(wim_actions)
3627
tierno868220c2017-09-26 00:11:05 +02003628 returned_instance = mydb.get_instance_scenario(instance_uuid)
3629 returned_instance["action_id"] = instance_action_id
3630 return returned_instance
tierno4491ba92019-03-25 15:00:02 +00003631 except (NfvoException, vimconn.vimconnException, wimconn.WimConnectorError, db_base_Exception) as e:
tiernobe41e222016-09-02 15:16:13 +02003632 message = rollback(mydb, myvims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02003633 if isinstance(e, db_base_Exception):
3634 error_text = "database Exception"
3635 elif isinstance(e, vimconn.vimconnException):
3636 error_text = "VIM Exception"
tierno4491ba92019-03-25 15:00:02 +00003637 elif isinstance(e, wimconn.WimConnectorError):
3638 error_text = "WIM Exception"
tiernof97fd272016-07-11 14:32:37 +02003639 else:
3640 error_text = "Exception"
3641 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
tierno868220c2017-09-26 00:11:05 +02003642 # logger.error("create_instance: %s", error_text)
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003643 logger.exception(e)
tiernof97fd272016-07-11 14:32:37 +02003644 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01003645
tiernob3d36742017-03-03 23:51:05 +01003646
tierno16e3dd42018-04-24 12:52:40 +02003647def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList):
3648 default_datacenter_id = params["default_datacenter_id"]
3649 myvim_threads_id = params["myvim_threads_id"]
3650 instance_uuid = params["instance_uuid"]
3651 instance_name = params["instance_name"]
3652 instance_action_id = params["instance_action_id"]
3653 myvims = params["myvims"]
3654 cloud_config = params["cloud_config"]
3655 RO_pub_key = params["RO_pub_key"]
3656
3657 task_index = params_out["task_index"]
3658 uuid_list = params_out["uuid_list"]
3659 db_instance_nets = params_out["db_instance_nets"]
3660 db_vim_actions = params_out["db_vim_actions"]
3661 db_ip_profiles = params_out["db_ip_profiles"]
3662 db_instance_vnfs = params_out["db_instance_vnfs"]
3663 db_instance_vms = params_out["db_instance_vms"]
3664 db_instance_interfaces = params_out["db_instance_interfaces"]
3665 net2task_id = params_out["net2task_id"]
3666 sce_net2instance = params_out["sce_net2instance"]
3667
3668 vnf_net2instance = {}
3669
3670 # 2. Creating new nets (vnf internal nets) in the VIM"
3671 # For each vnf net, we create it and we add it to instanceNetlist.
3672 if sce_vnf.get("datacenter"):
3673 datacenter_id = sce_vnf["datacenter"]
3674 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
3675 else:
3676 datacenter_id = default_datacenter_id
3677 myvim_thread_id = myvim_threads_id[default_datacenter_id]
3678 for net in sce_vnf['nets']:
3679 # TODO revis
3680 # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
3681 # net_name = descriptor_net.get("name")
3682 net_name = None
3683 if not net_name:
tierno1df468d2018-07-06 14:25:16 +02003684 net_name = "{}-{}".format(instance_name, net["name"])
tierno16e3dd42018-04-24 12:52:40 +02003685 net_name = net_name[:255] # limit length
3686 net_type = net['type']
3687
3688 if sce_vnf['uuid'] not in vnf_net2instance:
3689 vnf_net2instance[sce_vnf['uuid']] = {}
3690 if sce_vnf['uuid'] not in net2task_id:
3691 net2task_id[sce_vnf['uuid']] = {}
3692 net2task_id[sce_vnf['uuid']][net['uuid']] = task_index
3693
3694 # fill database content
3695 net_uuid = str(uuid4())
3696 uuid_list.append(net_uuid)
3697 vnf_net2instance[sce_vnf['uuid']][net['uuid']] = net_uuid
3698 db_net = {
3699 "uuid": net_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003700 "related": net_uuid,
tierno16e3dd42018-04-24 12:52:40 +02003701 'vim_net_id': None,
tiernob6990792018-11-13 10:37:42 +01003702 "vim_name": net_name,
tierno16e3dd42018-04-24 12:52:40 +02003703 "instance_scenario_id": instance_uuid,
3704 "net_id": net["uuid"],
3705 "created": True,
3706 'datacenter_id': datacenter_id,
3707 'datacenter_tenant_id': myvim_thread_id,
3708 }
3709 db_instance_nets.append(db_net)
3710
gcalvino0a480542018-12-17 16:19:33 +01003711 lookfor_filter = {}
tierno1df468d2018-07-06 14:25:16 +02003712 if net.get("vim-network-name"):
gcalvino0a480542018-12-17 16:19:33 +01003713 lookfor_filter["name"] = net["vim-network-name"]
3714 if net.get("vim-network-id"):
3715 lookfor_filter["id"] = net["vim-network-id"]
3716 if lookfor_filter:
tierno1df468d2018-07-06 14:25:16 +02003717 task_action = "FIND"
3718 task_extra = {"params": (lookfor_filter,)}
3719 else:
3720 task_action = "CREATE"
3721 task_extra = {"params": (net_name, net_type, net.get('ip_profile', None))}
3722
tierno16e3dd42018-04-24 12:52:40 +02003723 db_vim_action = {
3724 "instance_action_id": instance_action_id,
3725 "task_index": task_index,
3726 "datacenter_vim_id": myvim_thread_id,
3727 "status": "SCHEDULED",
tierno1df468d2018-07-06 14:25:16 +02003728 "action": task_action,
tierno16e3dd42018-04-24 12:52:40 +02003729 "item": "instance_nets",
3730 "item_id": net_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003731 "related": net_uuid,
tierno1df468d2018-07-06 14:25:16 +02003732 "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256)
tierno16e3dd42018-04-24 12:52:40 +02003733 }
3734 task_index += 1
3735 db_vim_actions.append(db_vim_action)
3736
3737 if 'ip_profile' in net:
3738 db_ip_profile = {
3739 'instance_net_id': net_uuid,
3740 'ip_version': net['ip_profile']['ip_version'],
3741 'subnet_address': net['ip_profile']['subnet_address'],
3742 'gateway_address': net['ip_profile']['gateway_address'],
3743 'dns_address': net['ip_profile']['dns_address'],
3744 'dhcp_enabled': net['ip_profile']['dhcp_enabled'],
3745 'dhcp_start_address': net['ip_profile']['dhcp_start_address'],
3746 'dhcp_count': net['ip_profile']['dhcp_count'],
3747 }
3748 db_ip_profiles.append(db_ip_profile)
3749
3750 # print "vnf_net2instance:"
3751 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
3752
3753 # 3. Creating new vm instances in the VIM
3754 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
3755 ssh_access = None
3756 if sce_vnf.get('mgmt_access'):
3757 ssh_access = sce_vnf['mgmt_access'].get('config-access', {}).get('ssh-access')
3758 vnf_availability_zones = []
gcalvinod6fac4d2018-11-05 10:42:06 +01003759 for vm in sce_vnf.get('vms'):
tierno16e3dd42018-04-24 12:52:40 +02003760 vm_av = vm.get('availability_zone')
3761 if vm_av and vm_av not in vnf_availability_zones:
3762 vnf_availability_zones.append(vm_av)
3763
3764 # check if there is enough availability zones available at vim level.
3765 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
3766 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003767 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors.Bad_Request)
tierno16e3dd42018-04-24 12:52:40 +02003768
3769 if sce_vnf.get("datacenter"):
3770 vim = myvims[sce_vnf["datacenter"]]
3771 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
3772 datacenter_id = sce_vnf["datacenter"]
3773 else:
3774 vim = myvims[default_datacenter_id]
3775 myvim_thread_id = myvim_threads_id[default_datacenter_id]
3776 datacenter_id = default_datacenter_id
3777 sce_vnf["datacenter_id"] = datacenter_id
3778 i = 0
3779
3780 vnf_uuid = str(uuid4())
3781 uuid_list.append(vnf_uuid)
3782 db_instance_vnf = {
3783 'uuid': vnf_uuid,
3784 'instance_scenario_id': instance_uuid,
3785 'vnf_id': sce_vnf['vnf_id'],
3786 'sce_vnf_id': sce_vnf['uuid'],
3787 'datacenter_id': datacenter_id,
3788 'datacenter_tenant_id': myvim_thread_id,
3789 }
3790 db_instance_vnfs.append(db_instance_vnf)
3791
3792 for vm in sce_vnf['vms']:
tiernob6990792018-11-13 10:37:42 +01003793 # skip PDUs
3794 if vm.get("pdu_type"):
3795 continue
3796
tierno16e3dd42018-04-24 12:52:40 +02003797 myVMDict = {}
tierno7f426e92018-06-28 15:21:32 +02003798 sce_vnf_name = sce_vnf['member_vnf_index'] if sce_vnf['member_vnf_index'] else sce_vnf['name']
3799 myVMDict['name'] = "{}-{}-{}".format(instance_name[:64], sce_vnf_name[:64], vm["name"][:64])
tierno16e3dd42018-04-24 12:52:40 +02003800 myVMDict['description'] = myVMDict['name'][0:99]
3801 # if not startvms:
3802 # myVMDict['start'] = "no"
tierno1df468d2018-07-06 14:25:16 +02003803 if vm.get("instance_parameters") and vm["instance_parameters"].get("name"):
3804 myVMDict['name'] = vm["instance_parameters"].get("name")
tierno16e3dd42018-04-24 12:52:40 +02003805 myVMDict['name'] = myVMDict['name'][0:255] # limit name length
3806 # create image at vim in case it not exist
3807 image_uuid = vm['image_id']
3808 if vm.get("image_list"):
3809 for alternative_image in vm["image_list"]:
tiernob6434212018-04-26 16:27:47 +02003810 if alternative_image["vim_type"] == vim["config"]["_vim_type_internal"]:
tierno16e3dd42018-04-24 12:52:40 +02003811 image_uuid = alternative_image['image_id']
3812 break
3813 image_dict = mydb.get_table_by_uuid_name("images", image_uuid)
3814 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
3815 vm['vim_image_id'] = image_id
3816
3817 # create flavor at vim in case it not exist
3818 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
3819 if flavor_dict['extended'] != None:
3820 flavor_dict['extended'] = yaml.load(flavor_dict['extended'])
3821 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
3822
3823 # Obtain information for additional disks
3824 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',),
3825 WHERE={'vim_id': flavor_id})
3826 if not extended_flavor_dict:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003827 raise NfvoException("flavor '{}' not found".format(flavor_id), httperrors.Not_Found)
tierno16e3dd42018-04-24 12:52:40 +02003828
3829 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
3830 myVMDict['disks'] = None
3831 extended_info = extended_flavor_dict[0]['extended']
3832 if extended_info != None:
3833 extended_flavor_dict_yaml = yaml.load(extended_info)
3834 if 'disks' in extended_flavor_dict_yaml:
3835 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
tierno1df468d2018-07-06 14:25:16 +02003836 if vm.get("instance_parameters") and vm["instance_parameters"].get("devices"):
3837 for disk in myVMDict['disks']:
3838 if disk.get("name") in vm["instance_parameters"]["devices"]:
3839 disk.update(vm["instance_parameters"]["devices"][disk.get("name")])
tierno16e3dd42018-04-24 12:52:40 +02003840
3841 vm['vim_flavor_id'] = flavor_id
3842 myVMDict['imageRef'] = vm['vim_image_id']
3843 myVMDict['flavorRef'] = vm['vim_flavor_id']
3844 myVMDict['availability_zone'] = vm.get('availability_zone')
3845 myVMDict['networks'] = []
3846 task_depends_on = []
3847 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
tierno67881db2018-10-24 18:46:03 +02003848 is_management_vm = False
tierno16e3dd42018-04-24 12:52:40 +02003849 db_vm_ifaces = []
3850 for iface in vm['interfaces']:
3851 netDict = {}
3852 if iface['type'] == "data":
3853 netDict['type'] = iface['model']
3854 elif "model" in iface and iface["model"] != None:
3855 netDict['model'] = iface['model']
3856 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
3857 # is obtained from iterface table model
3858 # discover type of interface looking at flavor
3859 for numa in flavor_dict.get('extended', {}).get('numas', []):
3860 for flavor_iface in numa.get('interfaces', []):
3861 if flavor_iface.get('name') == iface['internal_name']:
3862 if flavor_iface['dedicated'] == 'yes':
3863 netDict['type'] = "PF" # passthrough
3864 elif flavor_iface['dedicated'] == 'no':
3865 netDict['type'] = "VF" # siov
3866 elif flavor_iface['dedicated'] == 'yes:sriov':
3867 netDict['type'] = "VFnotShared" # sriov but only one sriov on the PF
3868 netDict["mac_address"] = flavor_iface.get("mac_address")
3869 break
3870 netDict["use"] = iface['type']
3871 if netDict["use"] == "data" and not netDict.get("type"):
3872 # print "netDict", netDict
3873 # print "iface", iface
3874 e_text = "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat(
3875 sce_vnf['name'], vm['name'], iface['internal_name'])
3876 if flavor_dict.get('extended') == None:
3877 raise NfvoException(e_text + "After database migration some information is not available. \
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003878 Try to delete and create the scenarios and VNFs again", httperrors.Conflict)
tierno16e3dd42018-04-24 12:52:40 +02003879 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01003880 raise NfvoException(e_text, httperrors.Internal_Server_Error)
tierno67881db2018-10-24 18:46:03 +02003881 if netDict["use"] == "mgmt":
3882 is_management_vm = True
3883 netDict["type"] = "virtual"
3884 if netDict["use"] == "bridge":
tierno16e3dd42018-04-24 12:52:40 +02003885 netDict["type"] = "virtual"
3886 if iface.get("vpci"):
3887 netDict['vpci'] = iface['vpci']
3888 if iface.get("mac"):
3889 netDict['mac_address'] = iface['mac']
tierno6082b7d2018-08-31 11:24:08 +00003890 if iface.get("mac_address"):
3891 netDict['mac_address'] = iface['mac_address']
tierno16e3dd42018-04-24 12:52:40 +02003892 if iface.get("ip_address"):
3893 netDict['ip_address'] = iface['ip_address']
3894 if iface.get("port-security") is not None:
3895 netDict['port_security'] = iface['port-security']
3896 if iface.get("floating-ip") is not None:
3897 netDict['floating_ip'] = iface['floating-ip']
3898 netDict['name'] = iface['internal_name']
3899 if iface['net_id'] is None:
3900 for vnf_iface in sce_vnf["interfaces"]:
3901 # print iface
3902 # print vnf_iface
3903 if vnf_iface['interface_id'] == iface['uuid']:
3904 netDict['net_id'] = "TASK-{}".format(
3905 net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id])
3906 instance_net_id = sce_net2instance[vnf_iface['sce_net_id']][datacenter_id]
3907 task_depends_on.append(net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id])
3908 break
3909 else:
3910 netDict['net_id'] = "TASK-{}".format(net2task_id[sce_vnf['uuid']][iface['net_id']])
3911 instance_net_id = vnf_net2instance[sce_vnf['uuid']][iface['net_id']]
3912 task_depends_on.append(net2task_id[sce_vnf['uuid']][iface['net_id']])
3913 # skip bridge ifaces not connected to any net
3914 if 'net_id' not in netDict or netDict['net_id'] == None:
3915 continue
3916 myVMDict['networks'].append(netDict)
3917 db_vm_iface = {
3918 # "uuid"
3919 # 'instance_vm_id': instance_vm_uuid,
3920 "instance_net_id": instance_net_id,
3921 'interface_id': iface['uuid'],
3922 # 'vim_interface_id': ,
3923 'type': 'external' if iface['external_name'] is not None else 'internal',
3924 'ip_address': iface.get('ip_address'),
3925 'mac_address': iface.get('mac'),
3926 'floating_ip': int(iface.get('floating-ip', False)),
3927 'port_security': int(iface.get('port-security', True))
3928 }
3929 db_vm_ifaces.append(db_vm_iface)
3930 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3931 # print myVMDict['name']
3932 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
3933 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
3934 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3935
3936 # We add the RO key to cloud_config if vnf will need ssh access
3937 cloud_config_vm = cloud_config
tierno67881db2018-10-24 18:46:03 +02003938 if is_management_vm and params["instance_parameters"].get("mgmt_keys"):
3939 cloud_config_vm = unify_cloud_config({"key-pairs": params["instance_parameters"]["mgmt_keys"]},
3940 cloud_config_vm)
3941
3942 if vm.get("instance_parameters") and vm["instance_parameters"].get("mgmt_keys"):
3943 cloud_config_vm = unify_cloud_config({"key-pairs": vm["instance_parameters"]["mgmt_keys"]},
3944 cloud_config_vm)
3945 # if ssh_access and ssh_access['required'] and ssh_access['default-user'] and tenant[0].get('RO_pub_key'):
3946 # RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]}
3947 # cloud_config_vm = unify_cloud_config(cloud_config_vm, RO_key)
tierno16e3dd42018-04-24 12:52:40 +02003948 if vm.get("boot_data"):
3949 cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config_vm)
3950
3951 if myVMDict.get('availability_zone'):
3952 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
3953 else:
3954 av_index = None
3955 for vm_index in range(0, vm.get('count', 1)):
tiernofc5f80b2018-05-29 16:00:43 +02003956 vm_name = myVMDict['name'] + "-" + str(vm_index+1)
3957 task_params = (vm_name, myVMDict['description'], myVMDict.get('start', None),
tierno16e3dd42018-04-24 12:52:40 +02003958 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm,
3959 myVMDict['disks'], av_index, vnf_availability_zones)
3960 # put interface uuid back to scenario[vnfs][vms[[interfaces]
3961 for net in myVMDict['networks']:
3962 if "vim_id" in net:
3963 for iface in vm['interfaces']:
3964 if net["name"] == iface["internal_name"]:
3965 iface["vim_id"] = net["vim_id"]
3966 break
3967 vm_uuid = str(uuid4())
3968 uuid_list.append(vm_uuid)
3969 db_vm = {
3970 "uuid": vm_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00003971 "related": vm_uuid,
tierno16e3dd42018-04-24 12:52:40 +02003972 'instance_vnf_id': vnf_uuid,
3973 # TODO delete "vim_vm_id": vm_id,
3974 "vm_id": vm["uuid"],
tiernofc5f80b2018-05-29 16:00:43 +02003975 "vim_name": vm_name,
tierno16e3dd42018-04-24 12:52:40 +02003976 # "status":
3977 }
3978 db_instance_vms.append(db_vm)
3979
3980 iface_index = 0
3981 for db_vm_iface in db_vm_ifaces:
3982 iface_uuid = str(uuid4())
3983 uuid_list.append(iface_uuid)
3984 db_vm_iface_instance = {
3985 "uuid": iface_uuid,
3986 "instance_vm_id": vm_uuid
3987 }
3988 db_vm_iface_instance.update(db_vm_iface)
3989 if db_vm_iface_instance.get("ip_address"): # increment ip_address
3990 ip = db_vm_iface_instance.get("ip_address")
3991 i = ip.rfind(".")
3992 if i > 0:
3993 try:
3994 i += 1
3995 ip = ip[i:] + str(int(ip[:i]) + 1)
3996 db_vm_iface_instance["ip_address"] = ip
3997 except:
3998 db_vm_iface_instance["ip_address"] = None
3999 db_instance_interfaces.append(db_vm_iface_instance)
4000 myVMDict['networks'][iface_index]["uuid"] = iface_uuid
4001 iface_index += 1
4002
4003 db_vim_action = {
4004 "instance_action_id": instance_action_id,
4005 "task_index": task_index,
4006 "datacenter_vim_id": myvim_thread_id,
4007 "action": "CREATE",
4008 "status": "SCHEDULED",
4009 "item": "instance_vms",
4010 "item_id": vm_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00004011 "related": vm_uuid,
tierno16e3dd42018-04-24 12:52:40 +02004012 "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on},
4013 default_flow_style=True, width=256)
4014 }
4015 task_index += 1
4016 db_vim_actions.append(db_vim_action)
4017 params_out["task_index"] = task_index
4018 params_out["uuid_list"] = uuid_list
4019
4020
tierno7edb6752016-03-21 17:37:52 +01004021def delete_instance(mydb, tenant_id, instance_id):
tierno868220c2017-09-26 00:11:05 +02004022 # print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02004023 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
tierno868220c2017-09-26 00:11:05 +02004024 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01004025 tenant_id = instanceDict["tenant_id"]
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004026
4027 # --> WIM
4028 # We need to retrieve the WIM Actions now, before the instance_scenario is
4029 # deleted. The reason for that is that: ON CASCADE rules will delete the
4030 # instance_wim_nets record in the database
4031 wim_actions = wim_engine.delete_actions(instance_scenario_id=instance_id)
4032 # <-- WIM
4033
tierno868220c2017-09-26 00:11:05 +02004034 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno868220c2017-09-26 00:11:05 +02004035 # 1. Delete from Database
tiernof97fd272016-07-11 14:32:37 +02004036 message = mydb.delete_instance_scenario(instance_id, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01004037
tierno868220c2017-09-26 00:11:05 +02004038 # 2. delete from VIM
tiernoa2793912016-10-04 08:15:08 +00004039 error_msg = ""
tiernob3d36742017-03-03 23:51:05 +01004040 myvims = {}
4041 myvim_threads = {}
tierno868220c2017-09-26 00:11:05 +02004042 vimthread_affected = {}
tierno3fcfdb72017-10-24 07:48:24 +02004043 net2vm_dependencies = {}
tierno7edb6752016-03-21 17:37:52 +01004044
tierno868220c2017-09-26 00:11:05 +02004045 task_index = 0
4046 instance_action_id = get_task_id()
4047 db_vim_actions = []
4048 db_instance_action = {
4049 "uuid": instance_action_id, # same uuid for the instance and the action on create
4050 "tenant_id": tenant_id,
4051 "instance_id": instance_id,
4052 "description": "DELETE",
4053 # "number_tasks": 0 # filled bellow
4054 }
4055
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004056 # 2.1 deleting VNFFGs
tierno69b590e2018-03-13 18:52:23 +01004057 for sfp in instanceDict.get('sfps', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00004058 vimthread_affected[sfp["datacenter_tenant_id"]] = None
4059 datacenter_key = (sfp["datacenter_id"], sfp["datacenter_tenant_id"])
4060 if datacenter_key not in myvims:
4061 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004062 _, myvim_thread = get_vim_thread(mydb, tenant_id, sfp["datacenter_id"], sfp["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004063 except NfvoException as e:
4064 logger.error(str(e))
4065 myvim_thread = None
4066 myvim_threads[datacenter_key] = myvim_thread
4067 vims = get_vim(mydb, tenant_id, datacenter_id=sfp["datacenter_id"],
4068 datacenter_tenant_id=sfp["datacenter_tenant_id"])
4069 if len(vims) == 0:
4070 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp["datacenter_id"], sfp["datacenter_tenant_id"]))
4071 myvims[datacenter_key] = None
4072 else:
4073 myvims[datacenter_key] = vims.values()[0]
4074 myvim = myvims[datacenter_key]
4075 myvim_thread = myvim_threads[datacenter_key]
4076
4077 if not myvim:
4078 error_msg += "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp['vim_sfp_id'], sfp["datacenter_id"])
4079 continue
4080 extra = {"params": (sfp['vim_sfp_id'])}
4081 db_vim_action = {
4082 "instance_action_id": instance_action_id,
4083 "task_index": task_index,
4084 "datacenter_vim_id": sfp["datacenter_tenant_id"],
4085 "action": "DELETE",
4086 "status": "SCHEDULED",
4087 "item": "instance_sfps",
4088 "item_id": sfp["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004089 "related": sfp["related"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004090 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4091 }
4092 task_index += 1
4093 db_vim_actions.append(db_vim_action)
4094
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004095 for classification in instanceDict['classifications']:
4096 vimthread_affected[classification["datacenter_tenant_id"]] = None
4097 datacenter_key = (classification["datacenter_id"], classification["datacenter_tenant_id"])
4098 if datacenter_key not in myvims:
4099 try:
4100 _, myvim_thread = get_vim_thread(mydb, tenant_id, classification["datacenter_id"], classification["datacenter_tenant_id"])
4101 except NfvoException as e:
4102 logger.error(str(e))
4103 myvim_thread = None
4104 myvim_threads[datacenter_key] = myvim_thread
4105 vims = get_vim(mydb, tenant_id, datacenter_id=classification["datacenter_id"],
4106 datacenter_tenant_id=classification["datacenter_tenant_id"])
4107 if len(vims) == 0:
4108 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification["datacenter_id"],
4109 classification["datacenter_tenant_id"]))
4110 myvims[datacenter_key] = None
4111 else:
4112 myvims[datacenter_key] = vims.values()[0]
4113 myvim = myvims[datacenter_key]
4114 myvim_thread = myvim_threads[datacenter_key]
4115
4116 if not myvim:
4117 error_msg += "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification['vim_classification_id'],
4118 classification["datacenter_id"])
4119 continue
4120 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"]
4121 extra = {"params": (classification['vim_classification_id']), "depends_on": depends_on}
4122 db_vim_action = {
4123 "instance_action_id": instance_action_id,
4124 "task_index": task_index,
4125 "datacenter_vim_id": classification["datacenter_tenant_id"],
4126 "action": "DELETE",
4127 "status": "SCHEDULED",
4128 "item": "instance_classifications",
4129 "item_id": classification["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004130 "related": classification["related"],
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004131 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4132 }
4133 task_index += 1
4134 db_vim_actions.append(db_vim_action)
4135
tierno69b590e2018-03-13 18:52:23 +01004136 for sf in instanceDict.get('sfs', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00004137 vimthread_affected[sf["datacenter_tenant_id"]] = None
4138 datacenter_key = (sf["datacenter_id"], sf["datacenter_tenant_id"])
4139 if datacenter_key not in myvims:
4140 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004141 _, myvim_thread = get_vim_thread(mydb, tenant_id, sf["datacenter_id"], sf["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004142 except NfvoException as e:
4143 logger.error(str(e))
4144 myvim_thread = None
4145 myvim_threads[datacenter_key] = myvim_thread
4146 vims = get_vim(mydb, tenant_id, datacenter_id=sf["datacenter_id"],
4147 datacenter_tenant_id=sf["datacenter_tenant_id"])
4148 if len(vims) == 0:
4149 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf["datacenter_id"], sf["datacenter_tenant_id"]))
4150 myvims[datacenter_key] = None
4151 else:
4152 myvims[datacenter_key] = vims.values()[0]
4153 myvim = myvims[datacenter_key]
4154 myvim_thread = myvim_threads[datacenter_key]
4155
4156 if not myvim:
4157 error_msg += "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf['vim_sf_id'], sf["datacenter_id"])
4158 continue
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004159 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"]
4160 extra = {"params": (sf['vim_sf_id']), "depends_on": depends_on}
Igor D.Ccaadc442017-11-06 12:48:48 +00004161 db_vim_action = {
4162 "instance_action_id": instance_action_id,
4163 "task_index": task_index,
4164 "datacenter_vim_id": sf["datacenter_tenant_id"],
4165 "action": "DELETE",
4166 "status": "SCHEDULED",
4167 "item": "instance_sfs",
4168 "item_id": sf["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004169 "related": sf["related"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004170 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4171 }
4172 task_index += 1
4173 db_vim_actions.append(db_vim_action)
4174
tierno69b590e2018-03-13 18:52:23 +01004175 for sfi in instanceDict.get('sfis', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00004176 vimthread_affected[sfi["datacenter_tenant_id"]] = None
4177 datacenter_key = (sfi["datacenter_id"], sfi["datacenter_tenant_id"])
4178 if datacenter_key not in myvims:
4179 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004180 _, myvim_thread = get_vim_thread(mydb, tenant_id, sfi["datacenter_id"], sfi["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004181 except NfvoException as e:
4182 logger.error(str(e))
4183 myvim_thread = None
4184 myvim_threads[datacenter_key] = myvim_thread
4185 vims = get_vim(mydb, tenant_id, datacenter_id=sfi["datacenter_id"],
4186 datacenter_tenant_id=sfi["datacenter_tenant_id"])
4187 if len(vims) == 0:
4188 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi["datacenter_id"], sfi["datacenter_tenant_id"]))
4189 myvims[datacenter_key] = None
4190 else:
4191 myvims[datacenter_key] = vims.values()[0]
4192 myvim = myvims[datacenter_key]
4193 myvim_thread = myvim_threads[datacenter_key]
4194
4195 if not myvim:
4196 error_msg += "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi['vim_sfi_id'], sfi["datacenter_id"])
4197 continue
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004198 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfs"]
4199 extra = {"params": (sfi['vim_sfi_id']), "depends_on": depends_on}
Igor D.Ccaadc442017-11-06 12:48:48 +00004200 db_vim_action = {
4201 "instance_action_id": instance_action_id,
4202 "task_index": task_index,
4203 "datacenter_vim_id": sfi["datacenter_tenant_id"],
4204 "action": "DELETE",
4205 "status": "SCHEDULED",
4206 "item": "instance_sfis",
4207 "item_id": sfi["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004208 "related": sfi["related"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004209 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4210 }
4211 task_index += 1
4212 db_vim_actions.append(db_vim_action)
4213
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004214 # 2.2 deleting VMs
4215 # vm_fail_list=[]
gcalvinod6fac4d2018-11-05 10:42:06 +01004216 for sce_vnf in instanceDict.get('vnfs', ()):
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004217 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4218 vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None
Igor D.Ccaadc442017-11-06 12:48:48 +00004219 if datacenter_key not in myvims:
4220 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004221 _, 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 +00004222 except NfvoException as e:
4223 logger.error(str(e))
4224 myvim_thread = None
4225 myvim_threads[datacenter_key] = myvim_thread
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004226 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
4227 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004228 if len(vims) == 0:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004229 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
4230 sce_vnf["datacenter_tenant_id"]))
4231 myvims[datacenter_key] = None
4232 else:
4233 myvims[datacenter_key] = vims.values()[0]
4234 myvim = myvims[datacenter_key]
4235 myvim_thread = myvim_threads[datacenter_key]
4236
4237 for vm in sce_vnf['vms']:
4238 if not myvim:
4239 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
4240 continue
4241 sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"]
4242 db_vim_action = {
4243 "instance_action_id": instance_action_id,
4244 "task_index": task_index,
4245 "datacenter_vim_id": sce_vnf["datacenter_tenant_id"],
4246 "action": "DELETE",
4247 "status": "SCHEDULED",
4248 "item": "instance_vms",
4249 "item_id": vm["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004250 "related": vm["related"],
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004251 "extra": yaml.safe_dump({"params": vm["interfaces"], "depends_on": sfi_dependencies},
4252 default_flow_style=True, width=256)
4253 }
4254 db_vim_actions.append(db_vim_action)
4255 for interface in vm["interfaces"]:
4256 if not interface.get("instance_net_id"):
4257 continue
4258 if interface["instance_net_id"] not in net2vm_dependencies:
4259 net2vm_dependencies[interface["instance_net_id"]] = []
4260 net2vm_dependencies[interface["instance_net_id"]].append(task_index)
4261 task_index += 1
4262
4263 # 2.3 deleting NETS
4264 # net_fail_list=[]
4265 for net in instanceDict['nets']:
4266 vimthread_affected[net["datacenter_tenant_id"]] = None
4267 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4268 if datacenter_key not in myvims:
4269 try:
gcalvinod6fac4d2018-11-05 10:42:06 +01004270 _,myvim_thread = get_vim_thread(mydb, tenant_id, net["datacenter_id"], net["datacenter_tenant_id"])
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004271 except NfvoException as e:
4272 logger.error(str(e))
4273 myvim_thread = None
4274 myvim_threads[datacenter_key] = myvim_thread
4275 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
4276 datacenter_tenant_id=net["datacenter_tenant_id"])
4277 if len(vims) == 0:
4278 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 +00004279 myvims[datacenter_key] = None
4280 else:
4281 myvims[datacenter_key] = vims.values()[0]
4282 myvim = myvims[datacenter_key]
4283 myvim_thread = myvim_threads[datacenter_key]
4284
4285 if not myvim:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004286 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 +00004287 continue
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004288 extra = {"params": (net['vim_net_id'], net['sdn_net_id'])}
4289 if net2vm_dependencies.get(net["uuid"]):
4290 extra["depends_on"] = net2vm_dependencies[net["uuid"]]
4291 sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"]
4292 if len(sfi_dependencies) > 0:
4293 if "depends_on" in extra:
4294 extra["depends_on"] += sfi_dependencies
4295 else:
4296 extra["depends_on"] = sfi_dependencies
Igor D.Ccaadc442017-11-06 12:48:48 +00004297 db_vim_action = {
4298 "instance_action_id": instance_action_id,
4299 "task_index": task_index,
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004300 "datacenter_vim_id": net["datacenter_tenant_id"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004301 "action": "DELETE",
4302 "status": "SCHEDULED",
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004303 "item": "instance_nets",
4304 "item_id": net["uuid"],
tierno3c44e7b2019-03-04 17:32:01 +00004305 "related": net["related"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004306 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4307 }
4308 task_index += 1
4309 db_vim_actions.append(db_vim_action)
4310
tierno868220c2017-09-26 00:11:05 +02004311 db_instance_action["number_tasks"] = task_index
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004312
4313 # --> WIM
4314 wim_actions, db_instance_action = (
4315 wim_engine.incorporate_actions(wim_actions, db_instance_action))
4316 # <-- WIM
4317
tierno868220c2017-09-26 00:11:05 +02004318 db_tables = [
4319 {"instance_actions": db_instance_action},
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004320 {"vim_wim_actions": db_vim_actions + wim_actions}
tierno868220c2017-09-26 00:11:05 +02004321 ]
4322
4323 logger.debug("delete_instance done DB tables: %s",
4324 yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
4325 mydb.new_rows(db_tables, ())
4326 for myvim_thread_id in vimthread_affected.keys():
4327 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
4328
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004329 wim_engine.dispatch(wim_actions)
4330
tiernob3d36742017-03-03 23:51:05 +01004331 if len(error_msg) > 0:
tierno868220c2017-09-26 00:11:05 +02004332 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
4333 '(error: 404) from VIM: {}'.format(instance_action_id, message, error_msg)
tierno7edb6752016-03-21 17:37:52 +01004334 else:
tierno868220c2017-09-26 00:11:05 +02004335 return "action_id={} instance {} deleted".format(instance_action_id, message)
tierno7edb6752016-03-21 17:37:52 +01004336
tierno7f426e92018-06-28 15:21:32 +02004337def get_instance_id(mydb, tenant_id, instance_id):
4338 global ovim
4339 #check valid tenant_id
4340 check_tenant(mydb, tenant_id)
4341 #obtain data
4342
4343 instance_dict = mydb.get_instance_scenario(instance_id, tenant_id, verbose=True)
4344 for net in instance_dict["nets"]:
4345 if net.get("sdn_net_id"):
4346 net_sdn = ovim.show_network(net["sdn_net_id"])
4347 net["sdn_info"] = {
4348 "admin_state_up": net_sdn.get("admin_state_up"),
4349 "flows": net_sdn.get("flows"),
4350 "last_error": net_sdn.get("last_error"),
4351 "ports": net_sdn.get("ports"),
4352 "type": net_sdn.get("type"),
4353 "status": net_sdn.get("status"),
4354 "vlan": net_sdn.get("vlan"),
4355 }
4356 return instance_dict
tiernob3d36742017-03-03 23:51:05 +01004357
tiernob8569aa2018-08-24 11:34:54 +02004358@deprecated("Instance is automatically refreshed by vim_threads")
tierno7edb6752016-03-21 17:37:52 +01004359def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
4360 '''Refreshes a scenario instance. It modifies instanceDict'''
4361 '''Returns:
4362 - 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
4363 - error_msg
4364 '''
tierno867ffe92017-03-27 12:50:34 +02004365 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
4366 # #print "nfvo.refresh_instance begins"
4367 # #print json.dumps(instanceDict, indent=4)
4368 #
4369 # #print "Getting the VIM URL and the VIM tenant_id"
4370 # myvims={}
4371 #
4372 # # 1. Getting VIM vm and net list
4373 # vms_updated = [] #List of VM instance uuids in openmano that were updated
4374 # vms_notupdated=[]
4375 # vm_list = {}
4376 # for sce_vnf in instanceDict['vnfs']:
4377 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4378 # if datacenter_key not in vm_list:
4379 # vm_list[datacenter_key] = []
4380 # if datacenter_key not in myvims:
4381 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
4382 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
4383 # if len(vims) == 0:
4384 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
4385 # myvims[datacenter_key] = None
4386 # else:
4387 # myvims[datacenter_key] = vims.values()[0]
4388 # for vm in sce_vnf['vms']:
4389 # vm_list[datacenter_key].append(vm['vim_vm_id'])
4390 # vms_notupdated.append(vm["uuid"])
4391 #
4392 # nets_updated = [] #List of VM instance uuids in openmano that were updated
4393 # nets_notupdated=[]
4394 # net_list = {}
4395 # for net in instanceDict['nets']:
4396 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4397 # if datacenter_key not in net_list:
4398 # net_list[datacenter_key] = []
4399 # if datacenter_key not in myvims:
4400 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
4401 # datacenter_tenant_id=net["datacenter_tenant_id"])
4402 # if len(vims) == 0:
4403 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
4404 # myvims[datacenter_key] = None
4405 # else:
4406 # myvims[datacenter_key] = vims.values()[0]
4407 #
4408 # net_list[datacenter_key].append(net['vim_net_id'])
4409 # nets_notupdated.append(net["uuid"])
4410 #
4411 # # 1. Getting the status of all VMs
4412 # vm_dict={}
4413 # for datacenter_key in myvims:
4414 # if not vm_list.get(datacenter_key):
4415 # continue
4416 # failed = True
4417 # failed_message=""
4418 # if not myvims[datacenter_key]:
4419 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4420 # else:
4421 # try:
4422 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
4423 # failed = False
4424 # except vimconn.vimconnException as e:
4425 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4426 # failed_message = str(e)
4427 # if failed:
4428 # for vm in vm_list[datacenter_key]:
4429 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4430 #
4431 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
4432 # for sce_vnf in instanceDict['vnfs']:
4433 # for vm in sce_vnf['vms']:
4434 # vm_id = vm['vim_vm_id']
4435 # interfaces = vm_dict[vm_id].pop('interfaces', [])
4436 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
4437 # has_mgmt_iface = False
4438 # for iface in vm["interfaces"]:
4439 # if iface["type"]=="mgmt":
4440 # has_mgmt_iface = True
4441 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
4442 # vm_dict[vm_id]['status'] = "ACTIVE"
4443 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
4444 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
4445 # 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'):
4446 # vm['status'] = vm_dict[vm_id]['status']
4447 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
4448 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
4449 # # 2.1. Update in openmano DB the VMs whose status changed
4450 # try:
4451 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
4452 # vms_notupdated.remove(vm["uuid"])
4453 # if updates>0:
4454 # vms_updated.append(vm["uuid"])
4455 # except db_base_Exception as e:
4456 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4457 # # 2.2. Update in openmano DB the interface VMs
4458 # for interface in interfaces:
4459 # #translate from vim_net_id to instance_net_id
4460 # network_id_list=[]
4461 # for net in instanceDict['nets']:
4462 # if net["vim_net_id"] == interface["vim_net_id"]:
4463 # network_id_list.append(net["uuid"])
4464 # if not network_id_list:
4465 # continue
4466 # del interface["vim_net_id"]
4467 # try:
4468 # for network_id in network_id_list:
4469 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
4470 # except db_base_Exception as e:
4471 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
4472 #
4473 # # 3. Getting the status of all nets
4474 # net_dict = {}
4475 # for datacenter_key in myvims:
4476 # if not net_list.get(datacenter_key):
4477 # continue
4478 # failed = True
4479 # failed_message = ""
4480 # if not myvims[datacenter_key]:
4481 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4482 # else:
4483 # try:
4484 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
4485 # failed = False
4486 # except vimconn.vimconnException as e:
4487 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4488 # failed_message = str(e)
4489 # if failed:
4490 # for net in net_list[datacenter_key]:
4491 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4492 #
4493 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
4494 # # TODO: update nets inside a vnf
4495 # for net in instanceDict['nets']:
4496 # net_id = net['vim_net_id']
4497 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
4498 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
4499 # 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'):
4500 # net['status'] = net_dict[net_id]['status']
4501 # net['error_msg'] = net_dict[net_id].get('error_msg')
4502 # net['vim_info'] = net_dict[net_id].get('vim_info')
4503 # # 5.1. Update in openmano DB the nets whose status changed
4504 # try:
4505 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
4506 # nets_notupdated.remove(net["uuid"])
4507 # if updated>0:
4508 # nets_updated.append(net["uuid"])
4509 # except db_base_Exception as e:
4510 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4511 #
4512 # # Returns appropriate output
4513 # #print "nfvo.refresh_instance finishes"
4514 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
4515 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
tierno7edb6752016-03-21 17:37:52 +01004516 instance_id = instanceDict['uuid']
tierno867ffe92017-03-27 12:50:34 +02004517 # if len(vms_notupdated)+len(nets_notupdated)>0:
4518 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
4519 # 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 +01004520
tiernoae4a8d12016-07-08 12:30:39 +02004521 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
tierno7edb6752016-03-21 17:37:52 +01004522
4523def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
tiernoae4a8d12016-07-08 12:30:39 +02004524 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02004525 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01004526 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4527
tiernoae4a8d12016-07-08 12:30:39 +02004528 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernof97fd272016-07-11 14:32:37 +02004529 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
4530 if len(vims) == 0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004531 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01004532 myvim = vims.values()[0]
tiernofc5f80b2018-05-29 16:00:43 +02004533 vm_result = {}
4534 vm_error = 0
4535 vm_ok = 0
tierno42026a02017-02-10 15:13:40 +01004536
tiernofc5f80b2018-05-29 16:00:43 +02004537 myvim_threads_id = {}
4538 if action_dict.get("vdu-scaling"):
4539 db_instance_vms = []
4540 db_vim_actions = []
4541 db_instance_interfaces = []
4542 instance_action_id = get_task_id()
4543 db_instance_action = {
4544 "uuid": instance_action_id, # same uuid for the instance and the action on create
4545 "tenant_id": nfvo_tenant,
4546 "instance_id": instance_id,
4547 "description": "SCALE",
4548 }
4549 vm_result["instance_action_id"] = instance_action_id
tierno67881db2018-10-24 18:46:03 +02004550 vm_result["created"] = []
4551 vm_result["deleted"] = []
tiernofc5f80b2018-05-29 16:00:43 +02004552 task_index = 0
4553 for vdu in action_dict["vdu-scaling"]:
tierno868220c2017-09-26 00:11:05 +02004554 vdu_id = vdu.get("vdu-id")
tiernofc5f80b2018-05-29 16:00:43 +02004555 osm_vdu_id = vdu.get("osm_vdu_id")
4556 member_vnf_index = vdu.get("member-vnf-index")
tierno868220c2017-09-26 00:11:05 +02004557 vdu_count = vdu.get("count", 1)
tiernofc5f80b2018-05-29 16:00:43 +02004558 if vdu_id:
tierno67881db2018-10-24 18:46:03 +02004559 target_vms = mydb.get_rows(
tiernofc5f80b2018-05-29 16:00:43 +02004560 FROM="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid",
4561 WHERE={"vms.uuid": vdu_id},
4562 ORDER_BY="vms.created_at"
4563 )
tierno67881db2018-10-24 18:46:03 +02004564 if not target_vms:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004565 raise NfvoException("Cannot find the vdu with id {}".format(vdu_id), httperrors.Not_Found)
tiernofc5f80b2018-05-29 16:00:43 +02004566 else:
4567 if not osm_vdu_id and not member_vnf_index:
tiernoa43bd9e2018-11-26 09:28:58 +00004568 raise NfvoException("Invalid input vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'")
tierno67881db2018-10-24 18:46:03 +02004569 target_vms = mydb.get_rows(
tiernofc5f80b2018-05-29 16:00:43 +02004570 # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"),
4571 FROM="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\
4572 " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\
4573 " join vms on ivms.vm_id=vms.uuid",
tiernoa43bd9e2018-11-26 09:28:58 +00004574 WHERE={"vms.osm_id": osm_vdu_id, "svnfs.member_vnf_index": member_vnf_index,
4575 "ivnfs.instance_scenario_id": instance_id},
tiernofc5f80b2018-05-29 16:00:43 +02004576 ORDER_BY="ivms.created_at"
4577 )
tierno67881db2018-10-24 18:46:03 +02004578 if not target_vms:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004579 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 +02004580 vdu_id = target_vms[-1]["uuid"]
4581 target_vm = target_vms[-1]
tiernofc5f80b2018-05-29 16:00:43 +02004582 datacenter = target_vm["datacenter_id"]
4583 myvim_threads_id[datacenter], _ = get_vim_thread(mydb, nfvo_tenant, datacenter)
tiernofc5f80b2018-05-29 16:00:43 +02004584
tierno67881db2018-10-24 18:46:03 +02004585 if vdu["type"] == "delete":
4586 for index in range(0, vdu_count):
4587 target_vm = target_vms[-1-index]
4588 vdu_id = target_vm["uuid"]
4589 # look for nm
4590 vm_interfaces = None
4591 for sce_vnf in instanceDict['vnfs']:
4592 for vm in sce_vnf['vms']:
4593 if vm["uuid"] == vdu_id:
4594 vm_interfaces = vm["interfaces"]
4595 break
4596
4597 db_vim_action = {
4598 "instance_action_id": instance_action_id,
4599 "task_index": task_index,
4600 "datacenter_vim_id": target_vm["datacenter_tenant_id"],
4601 "action": "DELETE",
4602 "status": "SCHEDULED",
4603 "item": "instance_vms",
4604 "item_id": vdu_id,
tierno3c44e7b2019-03-04 17:32:01 +00004605 "related": vm["related"],
tierno67881db2018-10-24 18:46:03 +02004606 "extra": yaml.safe_dump({"params": vm_interfaces},
4607 default_flow_style=True, width=256)
4608 }
4609 task_index += 1
4610 db_vim_actions.append(db_vim_action)
4611 vm_result["deleted"].append(vdu_id)
4612 # delete from database
4613 db_instance_vms.append({"TO-DELETE": vdu_id})
tiernofc5f80b2018-05-29 16:00:43 +02004614
4615 else: # vdu["type"] == "create":
4616 iface2iface = {}
4617 where = {"item": "instance_vms", "item_id": target_vm["uuid"], "action": "CREATE"}
4618
garciadeblas72cd59f2018-12-05 10:59:40 +01004619 vim_action_to_clone = mydb.get_rows(FROM="vim_wim_actions", WHERE=where)
tiernofc5f80b2018-05-29 16:00:43 +02004620 if not vim_action_to_clone:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004621 raise NfvoException("Cannot find the vim_action at database with {}".format(where), httperrors.Internal_Server_Error)
tiernofc5f80b2018-05-29 16:00:43 +02004622 vim_action_to_clone = vim_action_to_clone[0]
4623 extra = yaml.safe_load(vim_action_to_clone["extra"])
4624
4625 # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y
4626 # TODO do the same for flavor and image when available
4627 task_depends_on = []
4628 task_params = extra["params"]
4629 task_params_networks = deepcopy(task_params[5])
4630 for iface in task_params[5]:
4631 if iface["net_id"].startswith("TASK-"):
4632 if "." not in iface["net_id"]:
4633 task_depends_on.append("{}.{}".format(vim_action_to_clone["instance_action_id"],
4634 iface["net_id"][5:]))
4635 iface["net_id"] = "TASK-{}.{}".format(vim_action_to_clone["instance_action_id"],
4636 iface["net_id"][5:])
4637 else:
4638 task_depends_on.append(iface["net_id"][5:])
4639 if "mac_address" in iface:
4640 del iface["mac_address"]
4641
4642 vm_ifaces_to_clone = mydb.get_rows(FROM="instance_interfaces", WHERE={"instance_vm_id": target_vm["uuid"]})
4643 for index in range(0, vdu_count):
4644 vm_uuid = str(uuid4())
4645 vm_name = target_vm.get('vim_name')
4646 try:
4647 suffix = vm_name.rfind("-")
tierno67881db2018-10-24 18:46:03 +02004648 vm_name = vm_name[:suffix+1] + str(index + 1 + int(vm_name[suffix+1:]))
tiernofc5f80b2018-05-29 16:00:43 +02004649 except Exception:
4650 pass
4651 db_instance_vm = {
4652 "uuid": vm_uuid,
4653 'instance_vnf_id': target_vm['instance_vnf_id'],
4654 'vm_id': target_vm['vm_id'],
4655 'vim_name': vm_name
4656 }
4657 db_instance_vms.append(db_instance_vm)
4658
4659 for vm_iface in vm_ifaces_to_clone:
4660 iface_uuid = str(uuid4())
4661 iface2iface[vm_iface["uuid"]] = iface_uuid
4662 db_vm_iface = {
4663 "uuid": iface_uuid,
4664 'instance_vm_id': vm_uuid,
4665 "instance_net_id": vm_iface["instance_net_id"],
4666 'interface_id': vm_iface['interface_id'],
4667 'type': vm_iface['type'],
4668 'floating_ip': vm_iface['floating_ip'],
4669 'port_security': vm_iface['port_security']
4670 }
4671 db_instance_interfaces.append(db_vm_iface)
4672 task_params_copy = deepcopy(task_params)
4673 for iface in task_params_copy[5]:
4674 iface["uuid"] = iface2iface[iface["uuid"]]
4675 # increment ip_address
4676 if "ip_address" in iface:
4677 ip = iface.get("ip_address")
4678 i = ip.rfind(".")
4679 if i > 0:
4680 try:
4681 i += 1
4682 ip = ip[i:] + str(int(ip[:i]) + 1)
4683 iface["ip_address"] = ip
4684 except:
4685 iface["ip_address"] = None
4686 if vm_name:
4687 task_params_copy[0] = vm_name
4688 db_vim_action = {
4689 "instance_action_id": instance_action_id,
4690 "task_index": task_index,
4691 "datacenter_vim_id": vim_action_to_clone["datacenter_vim_id"],
4692 "action": "CREATE",
4693 "status": "SCHEDULED",
4694 "item": "instance_vms",
4695 "item_id": vm_uuid,
tierno3c44e7b2019-03-04 17:32:01 +00004696 "related": target_vm["related"],
tiernofc5f80b2018-05-29 16:00:43 +02004697 # ALF
4698 # ALF
4699 # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd.
4700 # ALF
4701 # ALF
4702 "extra": yaml.safe_dump({"params": task_params_copy, "depends_on": task_depends_on}, default_flow_style=True, width=256)
4703 }
4704 task_index += 1
4705 db_vim_actions.append(db_vim_action)
tierno67881db2018-10-24 18:46:03 +02004706 vm_result["created"].append(vm_uuid)
tiernofc5f80b2018-05-29 16:00:43 +02004707
4708 db_instance_action["number_tasks"] = task_index
4709 db_tables = [
4710 {"instance_vms": db_instance_vms},
4711 {"instance_interfaces": db_instance_interfaces},
4712 {"instance_actions": db_instance_action},
4713 # TODO revise sfps
4714 # {"instance_sfis": db_instance_sfis},
4715 # {"instance_sfs": db_instance_sfs},
4716 # {"instance_classifications": db_instance_classifications},
4717 # {"instance_sfps": db_instance_sfps},
garciadeblasaba7a0d2018-12-05 12:42:35 +01004718 {"vim_wim_actions": db_vim_actions}
tiernofc5f80b2018-05-29 16:00:43 +02004719 ]
4720 logger.debug("create_vdu done DB tables: %s",
4721 yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
4722 mydb.new_rows(db_tables, [])
4723 for myvim_thread in myvim_threads_id.values():
4724 vim_threads["running"][myvim_thread].insert_task(db_vim_actions)
4725
4726 return vm_result
tierno7edb6752016-03-21 17:37:52 +01004727
4728 input_vnfs = action_dict.pop("vnfs", [])
4729 input_vms = action_dict.pop("vms", [])
tierno92c36fd2018-05-04 12:21:10 +02004730 action_over_all = True if not input_vnfs and not input_vms else False
tierno7edb6752016-03-21 17:37:52 +01004731 for sce_vnf in instanceDict['vnfs']:
4732 for vm in sce_vnf['vms']:
tierno92c36fd2018-05-04 12:21:10 +02004733 if not action_over_all and sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
4734 sce_vnf['member_vnf_index'] not in input_vnfs and \
4735 vm['uuid'] not in input_vms and vm['name'] not in input_vms:
4736 continue
tiernoae4a8d12016-07-08 12:30:39 +02004737 try:
gcalvinoe580c7d2017-09-22 14:09:51 +02004738 if "add_public_key" in action_dict:
4739 mgmt_access = {}
4740 if sce_vnf.get('mgmt_access'):
4741 mgmt_access = yaml.load(sce_vnf['mgmt_access'])
4742 ssh_access = mgmt_access['config-access']['ssh-access']
4743 tenant = mydb.get_rows_by_id('nfvo_tenants', nfvo_tenant)
tierno42026a02017-02-10 15:13:40 +01004744 try:
gcalvinoe580c7d2017-09-22 14:09:51 +02004745 if ssh_access['required'] and ssh_access['default-user']:
4746 if 'ip_address' in vm:
4747 mgmt_ip = vm['ip_address'].split(';')
4748 password = mgmt_access['config-access'].get('password')
4749 priv_RO_key = decrypt_key(tenant[0]['encrypted_RO_priv_key'], tenant[0]['uuid'])
4750 myvim.inject_user_key(mgmt_ip[0], ssh_access['default-user'],
4751 action_dict['add_public_key'],
4752 password=password, ro_key=priv_RO_key)
4753 else:
4754 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004755 httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02004756 except KeyError:
4757 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004758 httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02004759 else:
4760 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004761 httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02004762 else:
4763 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
4764 if "console" in action_dict:
4765 if not global_config["http_console_proxy"]:
tierno20fc2a22016-08-19 17:02:35 +02004766 vm_result[ vm['uuid'] ] = {"vim_result": 200,
4767 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4768 protocol=data["protocol"],
gcalvinoe580c7d2017-09-22 14:09:51 +02004769 ip = data["server"],
4770 port = data["port"],
tierno20fc2a22016-08-19 17:02:35 +02004771 suffix = data["suffix"]),
4772 "name":vm['name']
4773 }
4774 vm_ok +=1
gcalvinoe580c7d2017-09-22 14:09:51 +02004775 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004776 vm_result[ vm['uuid'] ] = {"vim_result": -httperrors.Unauthorized,
gcalvinoe580c7d2017-09-22 14:09:51 +02004777 "description": "this console is only reachable by local interface",
4778 "name":vm['name']
4779 }
tierno20fc2a22016-08-19 17:02:35 +02004780 vm_error+=1
gcalvinoe580c7d2017-09-22 14:09:51 +02004781 else:
4782 #print "console data", data
4783 try:
4784 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
4785 vm_result[ vm['uuid'] ] = {"vim_result": 200,
4786 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4787 protocol=data["protocol"],
4788 ip = global_config["http_console_host"],
4789 port = console_thread.port,
4790 suffix = data["suffix"]),
4791 "name":vm['name']
4792 }
4793 vm_ok +=1
4794 except NfvoException as e:
4795 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
4796 vm_error+=1
tierno20fc2a22016-08-19 17:02:35 +02004797
gcalvinoe580c7d2017-09-22 14:09:51 +02004798 else:
4799 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
4800 vm_ok +=1
tiernoae4a8d12016-07-08 12:30:39 +02004801 except vimconn.vimconnException as e:
4802 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
4803 vm_error+=1
tierno7edb6752016-03-21 17:37:52 +01004804
4805 if vm_ok==0: #all goes wrong
tierno351863c2016-07-23 01:46:03 +02004806 return vm_result
tierno7edb6752016-03-21 17:37:52 +01004807 else:
tierno351863c2016-07-23 01:46:03 +02004808 return vm_result
tierno42026a02017-02-10 15:13:40 +01004809
tierno868220c2017-09-26 00:11:05 +02004810def instance_action_get(mydb, nfvo_tenant, instance_id, action_id):
tierno16e3dd42018-04-24 12:52:40 +02004811 filter = {}
tierno868220c2017-09-26 00:11:05 +02004812 if nfvo_tenant and nfvo_tenant != "any":
4813 filter["tenant_id"] = nfvo_tenant
4814 if instance_id and instance_id != "any":
4815 filter["instance_id"] = instance_id
4816 if action_id:
4817 filter["uuid"] = action_id
4818 rows = mydb.get_rows(FROM="instance_actions", WHERE=filter)
tierno16e3dd42018-04-24 12:52:40 +02004819 if action_id:
4820 if not rows:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004821 raise NfvoException("Not found any action with this criteria", httperrors.Not_Found)
4822 vim_wim_actions = mydb.get_rows(FROM="vim_wim_actions", WHERE={"instance_action_id": action_id})
4823 rows[0]["vim_wim_actions"] = vim_wim_actions
tierno31e121f2018-12-03 12:04:48 +00004824 # for backward compatibility set vim_actions = vim_wim_actions
4825 rows[0]["vim_actions"] = vim_wim_actions
tiernofc5f80b2018-05-29 16:00:43 +02004826 return {"actions": rows}
tierno868220c2017-09-26 00:11:05 +02004827
tiernob3d36742017-03-03 23:51:05 +01004828
tierno7edb6752016-03-21 17:37:52 +01004829def create_or_use_console_proxy_thread(console_server, console_port):
4830 #look for a non-used port
4831 console_thread_key = console_server + ":" + str(console_port)
4832 if console_thread_key in global_config["console_thread"]:
4833 #global_config["console_thread"][console_thread_key].start_timeout()
tiernof97fd272016-07-11 14:32:37 +02004834 return global_config["console_thread"][console_thread_key]
tierno42026a02017-02-10 15:13:40 +01004835
tierno7edb6752016-03-21 17:37:52 +01004836 for port in global_config["console_port_iterator"]():
tierno20fc2a22016-08-19 17:02:35 +02004837 #print "create_or_use_console_proxy_thread() port:", port
tierno7edb6752016-03-21 17:37:52 +01004838 if port in global_config["console_ports"]:
4839 continue
4840 try:
4841 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
4842 clithread.start()
4843 global_config["console_thread"][console_thread_key] = clithread
4844 global_config["console_ports"][port] = console_thread_key
tiernof97fd272016-07-11 14:32:37 +02004845 return clithread
tierno7edb6752016-03-21 17:37:52 +01004846 except cli.ConsoleProxyExceptionPortUsed as e:
4847 #port used, try with onoher
4848 continue
4849 except cli.ConsoleProxyException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004850 raise NfvoException(str(e), httperrors.Bad_Request)
4851 raise NfvoException("Not found any free 'http_console_ports'", httperrors.Conflict)
tierno7edb6752016-03-21 17:37:52 +01004852
tiernob3d36742017-03-03 23:51:05 +01004853
tierno7edb6752016-03-21 17:37:52 +01004854def check_tenant(mydb, tenant_id):
4855 '''check that tenant exists at database'''
tiernof97fd272016-07-11 14:32:37 +02004856 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
4857 if not tenant:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004858 raise NfvoException("tenant '{}' not found".format(tenant_id), httperrors.Not_Found)
tiernof97fd272016-07-11 14:32:37 +02004859 return
tierno7edb6752016-03-21 17:37:52 +01004860
4861def new_tenant(mydb, tenant_dict):
tierno7edb6752016-03-21 17:37:52 +01004862
gcalvinoe580c7d2017-09-22 14:09:51 +02004863 tenant_uuid = str(uuid4())
4864 tenant_dict['uuid'] = tenant_uuid
4865 try:
4866 pub_key, priv_key = create_RO_keypair(tenant_uuid)
4867 tenant_dict['RO_pub_key'] = pub_key
4868 tenant_dict['encrypted_RO_priv_key'] = priv_key
gcalvinoc62cfa52017-10-05 18:21:25 +02004869 mydb.new_row("nfvo_tenants", tenant_dict, confidential_data=True)
gcalvinoe580c7d2017-09-22 14:09:51 +02004870 except db_base_Exception as e:
tierno9c5c8322018-03-23 15:44:03 +01004871 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict['name']) + str(e), e.http_code)
gcalvinoe580c7d2017-09-22 14:09:51 +02004872 return tenant_uuid
tiernob3d36742017-03-03 23:51:05 +01004873
tierno7edb6752016-03-21 17:37:52 +01004874def delete_tenant(mydb, tenant):
4875 #get nfvo_tenant info
tierno42026a02017-02-10 15:13:40 +01004876
tiernof97fd272016-07-11 14:32:37 +02004877 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
4878 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
4879 return tenant_dict['uuid'] + " " + tenant_dict["name"]
tierno7edb6752016-03-21 17:37:52 +01004880
tiernob3d36742017-03-03 23:51:05 +01004881
tierno7edb6752016-03-21 17:37:52 +01004882def new_datacenter(mydb, datacenter_descriptor):
tierno1c848c02018-05-21 16:40:33 +02004883 sdn_port_mapping = None
tierno7edb6752016-03-21 17:37:52 +01004884 if "config" in datacenter_descriptor:
tiernoedf3f4f2018-05-17 23:02:47 +02004885 sdn_port_mapping = datacenter_descriptor["config"].pop("sdn-port-mapping", None)
4886 datacenter_descriptor["config"] = yaml.safe_dump(datacenter_descriptor["config"], default_flow_style=True,
4887 width=256)
4888 # Check that datacenter-type is correct
tierno3ae39742016-09-07 12:17:51 +02004889 datacenter_type = datacenter_descriptor.get("type", "openvim");
tiernoedf3f4f2018-05-17 23:02:47 +02004890 # module_info = None
tierno3ae39742016-09-07 12:17:51 +02004891 try:
4892 module = "vimconn_" + datacenter_type
tierno361275f2017-04-25 16:24:34 +02004893 pkg = __import__("osm_ro." + module)
tiernoedf3f4f2018-05-17 23:02:47 +02004894 # vim_conn = getattr(pkg, module)
tierno361275f2017-04-25 16:24:34 +02004895 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
tierno3ae39742016-09-07 12:17:51 +02004896 except (IOError, ImportError):
tierno361275f2017-04-25 16:24:34 +02004897 # if module_info and module_info[0]:
4898 # file.close(module_info[0])
tiernoedf3f4f2018-05-17 23:02:47 +02004899 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type,
4900 module),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004901 httperrors.Bad_Request)
tierno42026a02017-02-10 15:13:40 +01004902
gcalvinoc62cfa52017-10-05 18:21:25 +02004903 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True, confidential_data=True)
tiernoedf3f4f2018-05-17 23:02:47 +02004904 if sdn_port_mapping:
4905 try:
4906 datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, sdn_port_mapping)
4907 except Exception as e:
4908 mydb.delete_row_by_id("datacenters", datacenter_id) # Rollback
4909 raise e
tiernof97fd272016-07-11 14:32:37 +02004910 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01004911
tiernob3d36742017-03-03 23:51:05 +01004912
tierno7edb6752016-03-21 17:37:52 +01004913def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
tierno8fe7a492017-07-11 13:50:04 +02004914 # obtain data, check that only one exist
tiernof97fd272016-07-11 14:32:37 +02004915 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
tierno8fe7a492017-07-11 13:50:04 +02004916
4917 # edit data
tiernof97fd272016-07-11 14:32:37 +02004918 datacenter_id = datacenter['uuid']
tiernod72182f2018-08-29 10:56:13 +02004919 where = {'uuid': datacenter['uuid']}
tierno8fe7a492017-07-11 13:50:04 +02004920 remove_port_mapping = False
tiernoedf3f4f2018-05-17 23:02:47 +02004921 new_sdn_port_mapping = None
tierno7edb6752016-03-21 17:37:52 +01004922 if "config" in datacenter_descriptor:
tierno8fe7a492017-07-11 13:50:04 +02004923 if datacenter_descriptor['config'] != None:
tierno7edb6752016-03-21 17:37:52 +01004924 try:
4925 new_config_dict = datacenter_descriptor["config"]
tiernoedf3f4f2018-05-17 23:02:47 +02004926 if "sdn-port-mapping" in new_config_dict:
4927 remove_port_mapping = True
4928 new_sdn_port_mapping = new_config_dict.pop("sdn-port-mapping")
tiernod72182f2018-08-29 10:56:13 +02004929 # delete null fields
4930 to_delete = []
tierno7edb6752016-03-21 17:37:52 +01004931 for k in new_config_dict:
tiernod72182f2018-08-29 10:56:13 +02004932 if new_config_dict[k] is None:
tierno7edb6752016-03-21 17:37:52 +01004933 to_delete.append(k)
tierno8fe7a492017-07-11 13:50:04 +02004934 if k == 'sdn-controller':
4935 remove_port_mapping = True
tierno42026a02017-02-10 15:13:40 +01004936
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004937 config_text = datacenter.get("config")
4938 if not config_text:
4939 config_text = '{}'
4940 config_dict = yaml.load(config_text)
tierno7edb6752016-03-21 17:37:52 +01004941 config_dict.update(new_config_dict)
tiernod72182f2018-08-29 10:56:13 +02004942 # delete null fields
tierno7edb6752016-03-21 17:37:52 +01004943 for k in to_delete:
4944 del config_dict[k]
tiernof97fd272016-07-11 14:32:37 +02004945 except Exception as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004946 raise NfvoException("Bad format at datacenter:config " + str(e), httperrors.Bad_Request)
tierno8fe7a492017-07-11 13:50:04 +02004947 if config_dict:
4948 datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256)
4949 else:
4950 datacenter_descriptor["config"] = None
4951 if remove_port_mapping:
4952 try:
4953 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id)
4954 except ovimException as e:
Anderson Bravalheric5293de2018-11-28 17:21:26 +00004955 raise NfvoException("Error deleting datacenter-port-mapping " + str(e), httperrors.Conflict)
tierno8fe7a492017-07-11 13:50:04 +02004956
tiernof97fd272016-07-11 14:32:37 +02004957 mydb.update_rows('datacenters', datacenter_descriptor, where)
tiernoedf3f4f2018-05-17 23:02:47 +02004958 if new_sdn_port_mapping:
4959 try:
4960 datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, new_sdn_port_mapping)
4961 except ovimException as e:
tiernod72182f2018-08-29 10:56:13 +02004962 # Rollback
4963 mydb.update_rows('datacenters', datacenter, where)
Anderson Bravalheric5293de2018-11-28 17:21:26 +00004964 raise NfvoException("Error adding datacenter-port-mapping " + str(e), httperrors.Conflict)
tiernof97fd272016-07-11 14:32:37 +02004965 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01004966
tiernob3d36742017-03-03 23:51:05 +01004967
tierno7edb6752016-03-21 17:37:52 +01004968def delete_datacenter(mydb, datacenter):
4969 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02004970 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
4971 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
tierno8fe7a492017-07-11 13:50:04 +02004972 try:
4973 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid'])
4974 except ovimException as e:
tiernod72182f2018-08-29 10:56:13 +02004975 raise NfvoException("Error deleting datacenter-port-mapping " + str(e))
tiernof97fd272016-07-11 14:32:37 +02004976 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
tierno7edb6752016-03-21 17:37:52 +01004977
tiernob3d36742017-03-03 23:51:05 +01004978
tiernod3750b32018-07-20 15:33:08 +02004979def create_vim_account(mydb, nfvo_tenant, datacenter_id, name=None, vim_id=None, vim_tenant=None, vim_tenant_name=None,
4980 vim_username=None, vim_password=None, config=None):
tierno9c22f2d2017-10-09 16:23:55 +02004981 # get datacenter info
tierno0ea2a7e2017-10-18 00:06:26 +02004982 try:
tiernod3750b32018-07-20 15:33:08 +02004983 if not datacenter_id:
4984 if not vim_id:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01004985 raise NfvoException("You must provide 'vim_id", http_code=httperrors.Bad_Request)
tiernod3750b32018-07-20 15:33:08 +02004986 datacenter_id = vim_id
4987 datacenter_id, datacenter_name = get_datacenter_uuid(mydb, None, datacenter_id)
tierno7edb6752016-03-21 17:37:52 +01004988
tiernod3750b32018-07-20 15:33:08 +02004989 create_vim_tenant = True if not vim_tenant and not vim_tenant_name else False
tierno42026a02017-02-10 15:13:40 +01004990
tierno0ea2a7e2017-10-18 00:06:26 +02004991 # get nfvo_tenant info
4992 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
4993 if vim_tenant_name==None:
4994 vim_tenant_name=tenant_dict['name']
tierno42026a02017-02-10 15:13:40 +01004995
tierno0ea2a7e2017-10-18 00:06:26 +02004996 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
tiernod3750b32018-07-20 15:33:08 +02004997 # #check that this association does not exist before
4998 # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
4999 # if len(tenants_datacenters)>0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005000 # raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Conflict)
tierno7edb6752016-03-21 17:37:52 +01005001
tierno0ea2a7e2017-10-18 00:06:26 +02005002 vim_tenant_id_exist_atdb=False
5003 if not create_vim_tenant:
5004 where_={"datacenter_id": datacenter_id}
tiernod3750b32018-07-20 15:33:08 +02005005 if vim_tenant!=None:
5006 where_["vim_tenant_id"] = vim_tenant
tierno0ea2a7e2017-10-18 00:06:26 +02005007 if vim_tenant_name!=None:
5008 where_["vim_tenant_name"] = vim_tenant_name
5009 #check if vim_tenant_id is already at database
5010 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
5011 if len(datacenter_tenants_dict)>=1:
5012 datacenter_tenants_dict = datacenter_tenants_dict[0]
5013 vim_tenant_id_exist_atdb=True
5014 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
5015 else: #result=0
5016 datacenter_tenants_dict = {}
5017 #insert at table datacenter_tenants
tiernod3750b32018-07-20 15:33:08 +02005018 else: #if vim_tenant==None:
tierno0ea2a7e2017-10-18 00:06:26 +02005019 #create tenant at VIM if not provided
5020 try:
5021 _, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username,
5022 vim_passwd=vim_password)
5023 datacenter_name = myvim["name"]
tiernod3750b32018-07-20 15:33:08 +02005024 vim_tenant = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
tierno0ea2a7e2017-10-18 00:06:26 +02005025 except vimconn.vimconnException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005026 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 +01005027 datacenter_tenants_dict = {}
tierno0ea2a7e2017-10-18 00:06:26 +02005028 datacenter_tenants_dict["created"]="true"
tierno42026a02017-02-10 15:13:40 +01005029
tierno0ea2a7e2017-10-18 00:06:26 +02005030 #fill datacenter_tenants table
5031 if not vim_tenant_id_exist_atdb:
tiernod3750b32018-07-20 15:33:08 +02005032 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant
tierno0ea2a7e2017-10-18 00:06:26 +02005033 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
5034 datacenter_tenants_dict["user"] = vim_username
5035 datacenter_tenants_dict["passwd"] = vim_password
5036 datacenter_tenants_dict["datacenter_id"] = datacenter_id
tiernod3750b32018-07-20 15:33:08 +02005037 if name:
5038 datacenter_tenants_dict["name"] = name
5039 else:
5040 datacenter_tenants_dict["name"] = datacenter_name
tierno0ea2a7e2017-10-18 00:06:26 +02005041 if config:
5042 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
5043 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True, confidential_data=True)
5044 datacenter_tenants_dict["uuid"] = id_
tierno42026a02017-02-10 15:13:40 +01005045
tierno0ea2a7e2017-10-18 00:06:26 +02005046 #fill tenants_datacenters table
5047 datacenter_tenant_id = datacenter_tenants_dict["uuid"]
5048 tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id
5049 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
tiernod3750b32018-07-20 15:33:08 +02005050
tierno0ea2a7e2017-10-18 00:06:26 +02005051 # create thread
tierno0ea2a7e2017-10-18 00:06:26 +02005052 thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid'])
tiernod3750b32018-07-20 15:33:08 +02005053 new_thread = vim_thread.vim_thread(task_lock, thread_name, datacenter_name, datacenter_tenant_id,
tierno0ea2a7e2017-10-18 00:06:26 +02005054 db=db, db_lock=db_lock, ovim=ovim)
5055 new_thread.start()
5056 thread_id = datacenter_tenants_dict["uuid"]
5057 vim_threads["running"][thread_id] = new_thread
tiernod3750b32018-07-20 15:33:08 +02005058 return thread_id
tierno0ea2a7e2017-10-18 00:06:26 +02005059 except vimconn.vimconnException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005060 raise NfvoException(str(e), httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01005061
tierno99314902017-04-26 13:23:09 +02005062
tiernod3750b32018-07-20 15:33:08 +02005063def edit_vim_account(mydb, nfvo_tenant, datacenter_tenant_id, datacenter_id=None, name=None, vim_tenant=None,
5064 vim_tenant_name=None, vim_username=None, vim_password=None, config=None):
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005065
tiernod3750b32018-07-20 15:33:08 +02005066 # get vim_account; check is valid for this tenant
5067 from_ = "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id"
5068 where_ = {"td.nfvo_tenant_id": nfvo_tenant}
5069 if datacenter_tenant_id:
5070 where_["dt.uuid"] = datacenter_tenant_id
5071 if datacenter_id:
5072 where_["dt.datacenter_id"] = datacenter_id
5073 vim_accounts = mydb.get_rows(SELECT="dt.uuid as uuid, config", FROM=from_, WHERE=where_)
5074 if not vim_accounts:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005075 raise NfvoException("vim_account not found for this tenant", http_code=httperrors.Not_Found)
tiernod3750b32018-07-20 15:33:08 +02005076 elif len(vim_accounts) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005077 raise NfvoException("found more than one vim_account for this tenant", http_code=httperrors.Conflict)
tiernod3750b32018-07-20 15:33:08 +02005078 datacenter_tenant_id = vim_accounts[0]["uuid"]
5079 original_config = vim_accounts[0]["config"]
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005080
tiernod3750b32018-07-20 15:33:08 +02005081 update_ = {}
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005082 if config:
tiernod3750b32018-07-20 15:33:08 +02005083 original_config_dict = yaml.load(original_config)
5084 original_config_dict.update(config)
5085 update["config"] = yaml.safe_dump(original_config_dict, default_flow_style=True, width=256)
5086 if name:
5087 update_['name'] = name
5088 if vim_tenant:
5089 update_['vim_tenant_id'] = vim_tenant
5090 if vim_tenant_name:
5091 update_['vim_tenant_name'] = vim_tenant_name
5092 if vim_username:
5093 update_['user'] = vim_username
5094 if vim_password:
5095 update_['passwd'] = vim_password
5096 if update_:
5097 mydb.update_rows("datacenter_tenants", UPDATE=update_, WHERE={"uuid": datacenter_tenant_id})
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005098
tiernod3750b32018-07-20 15:33:08 +02005099 vim_threads["running"][datacenter_tenant_id].insert_task("reload")
5100 return datacenter_tenant_id
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005101
tiernod3750b32018-07-20 15:33:08 +02005102def delete_vim_account(mydb, tenant_id, vim_account_id, datacenter=None):
tierno7edb6752016-03-21 17:37:52 +01005103 #get nfvo_tenant info
5104 if not tenant_id or tenant_id=="any":
5105 tenant_uuid = None
5106 else:
tiernof97fd272016-07-11 14:32:37 +02005107 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
tierno7edb6752016-03-21 17:37:52 +01005108 tenant_uuid = tenant_dict['uuid']
5109
5110 #check that this association exist before
tiernod3750b32018-07-20 15:33:08 +02005111 tenants_datacenter_dict = {}
5112 if datacenter:
5113 datacenter_id, _ = get_datacenter_uuid(mydb, tenant_uuid, datacenter)
5114 tenants_datacenter_dict["datacenter_id"] = datacenter_id
5115 elif vim_account_id:
5116 tenants_datacenter_dict["datacenter_tenant_id"] = vim_account_id
tierno7edb6752016-03-21 17:37:52 +01005117 if tenant_uuid:
5118 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
tiernof97fd272016-07-11 14:32:37 +02005119 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
5120 if len(tenant_datacenter_list)==0 and tenant_uuid:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005121 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01005122
5123 #delete this association
tiernof97fd272016-07-11 14:32:37 +02005124 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
tierno7edb6752016-03-21 17:37:52 +01005125
5126 #get vim_tenant info and deletes
5127 warning=''
5128 for tenant_datacenter_item in tenant_datacenter_list:
tiernof97fd272016-07-11 14:32:37 +02005129 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
5130 #try to delete vim:tenant
5131 try:
5132 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
5133 if vim_tenant_dict['created']=='true':
tierno7edb6752016-03-21 17:37:52 +01005134 #delete tenant at VIM if created by NFVO
tierno42026a02017-02-10 15:13:40 +01005135 try:
tierno0ea2a7e2017-10-18 00:06:26 +02005136 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02005137 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
5138 except vimconn.vimconnException as e:
5139 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
5140 logger.warn(warning)
tiernof97fd272016-07-11 14:32:37 +02005141 except db_base_Exception as e:
5142 logger.error("Cannot delete datacenter_tenants " + str(e))
tierno42026a02017-02-10 15:13:40 +01005143 pass # the error will be caused because dependencies, vim_tenant can not be deleted
tierno867ffe92017-03-27 12:50:34 +02005144 thread_id = tenant_datacenter_item["datacenter_tenant_id"]
tiernoa3572692018-05-14 13:09:33 +02005145 thread = vim_threads["running"].get(thread_id)
5146 if thread:
5147 thread.insert_task("exit")
5148 vim_threads["deleting"][thread_id] = thread
tiernof97fd272016-07-11 14:32:37 +02005149 return "datacenter {} detached. {}".format(datacenter_id, warning)
tierno7edb6752016-03-21 17:37:52 +01005150
tiernob3d36742017-03-03 23:51:05 +01005151
tierno7edb6752016-03-21 17:37:52 +01005152def datacenter_action(mydb, tenant_id, datacenter, action_dict):
5153 #DEPRECATED
tierno42026a02017-02-10 15:13:40 +01005154 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005155 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005156
5157 if 'net-update' in action_dict:
tiernoae4a8d12016-07-08 12:30:39 +02005158 try:
tiernof97fd272016-07-11 14:32:37 +02005159 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
tiernoae4a8d12016-07-08 12:30:39 +02005160 #print content
5161 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005162 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005163 raise NfvoException(str(e), httperrors.Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01005164 #update nets Change from VIM format to NFVO format
5165 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02005166 for net in nets:
tierno7edb6752016-03-21 17:37:52 +01005167 net_nfvo={'datacenter_id': datacenter_id}
5168 net_nfvo['name'] = net['name']
5169 #net_nfvo['description']= net['name']
5170 net_nfvo['vim_net_id'] = net['id']
5171 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5172 net_nfvo['shared'] = net['shared']
5173 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
5174 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02005175 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
5176 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
5177 return inserted
tierno7edb6752016-03-21 17:37:52 +01005178 elif 'net-edit' in action_dict:
5179 net = action_dict['net-edit'].pop('net')
tierno42fcc3b2016-07-06 17:20:40 +02005180 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01005181 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
tierno7edb6752016-03-21 17:37:52 +01005182 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02005183 return result
tierno7edb6752016-03-21 17:37:52 +01005184 elif 'net-delete' in action_dict:
5185 net = action_dict['net-deelte'].get('net')
tierno42fcc3b2016-07-06 17:20:40 +02005186 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01005187 result = mydb.delete_row(FROM='datacenter_nets',
tierno7edb6752016-03-21 17:37:52 +01005188 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02005189 return result
tierno7edb6752016-03-21 17:37:52 +01005190
5191 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005192 raise NfvoException("Unknown action " + str(action_dict), httperrors.Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01005193
tiernob3d36742017-03-03 23:51:05 +01005194
tierno7edb6752016-03-21 17:37:52 +01005195def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
5196 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005197 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005198
tierno42fcc3b2016-07-06 17:20:40 +02005199 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
tierno42026a02017-02-10 15:13:40 +01005200 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
tierno7edb6752016-03-21 17:37:52 +01005201 WHERE={'datacenter_id':datacenter_id, what: netmap})
tiernof97fd272016-07-11 14:32:37 +02005202 return result
tierno7edb6752016-03-21 17:37:52 +01005203
tiernob3d36742017-03-03 23:51:05 +01005204
tierno7edb6752016-03-21 17:37:52 +01005205def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
5206 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005207 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005208 filter_dict={}
5209 if action_dict:
5210 action_dict = action_dict["netmap"]
5211 if 'vim_id' in action_dict:
5212 filter_dict["id"] = action_dict['vim_id']
5213 if 'vim_name' in action_dict:
5214 filter_dict["name"] = action_dict['vim_name']
5215 else:
5216 filter_dict["shared"] = True
tierno42026a02017-02-10 15:13:40 +01005217
tiernoae4a8d12016-07-08 12:30:39 +02005218 try:
tiernof97fd272016-07-11 14:32:37 +02005219 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02005220 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005221 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005222 raise NfvoException(str(e), httperrors.Internal_Server_Error)
tiernof97fd272016-07-11 14:32:37 +02005223 if len(vim_nets)>1 and action_dict:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005224 raise NfvoException("more than two networks found, specify with vim_id", httperrors.Conflict)
tiernof97fd272016-07-11 14:32:37 +02005225 elif len(vim_nets)==0: # and action_dict:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005226 raise NfvoException("Not found a network at VIM with " + str(filter_dict), httperrors.Not_Found)
tierno7edb6752016-03-21 17:37:52 +01005227 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02005228 for net in vim_nets:
tierno7edb6752016-03-21 17:37:52 +01005229 net_nfvo={'datacenter_id': datacenter_id}
5230 if action_dict and "name" in action_dict:
5231 net_nfvo['name'] = action_dict['name']
5232 else:
5233 net_nfvo['name'] = net['name']
5234 #net_nfvo['description']= net['name']
5235 net_nfvo['vim_net_id'] = net['id']
5236 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5237 net_nfvo['shared'] = net['shared']
5238 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
tiernof97fd272016-07-11 14:32:37 +02005239 try:
5240 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01005241 net_nfvo["status"] = "OK"
tiernof97fd272016-07-11 14:32:37 +02005242 net_nfvo["uuid"] = net_id
5243 except db_base_Exception as e:
5244 if action_dict:
5245 raise
5246 else:
5247 net_nfvo["status"] = "FAIL: " + str(e)
tierno42026a02017-02-10 15:13:40 +01005248 net_list.append(net_nfvo)
5249 return net_list
tierno7edb6752016-03-21 17:37:52 +01005250
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005251def get_sdn_net_id(mydb, tenant_id, datacenter, network_id):
5252 # obtain all network data
5253 try:
5254 if utils.check_valid_uuid(network_id):
5255 filter_dict = {"id": network_id}
5256 else:
5257 filter_dict = {"name": network_id}
5258
5259 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
5260 network = myvim.get_network_list(filter_dict=filter_dict)
5261 except vimconn.vimconnException as e:
tiernof1ba57e2017-09-07 12:23:19 +02005262 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 +02005263
5264 # ensure the network is defined
5265 if len(network) == 0:
5266 raise NfvoException("Network {} is not present in the system".format(network_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005267 httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005268
5269 # ensure there is only one network with the provided name
5270 if len(network) > 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005271 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005272
5273 # ensure it is a dataplane network
5274 if network[0]['type'] != 'data':
5275 return None
5276
5277 # ensure we use the id
5278 network_id = network[0]['id']
5279
5280 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
5281 # and with instance_scenario_id==NULL
5282 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
5283 search_dict = {'vim_net_id': network_id}
5284
5285 try:
5286 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
5287 result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)
5288 except db_base_Exception as e:
5289 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
tierno9c5c8322018-03-23 15:44:03 +01005290 network_id) + str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005291
5292 sdn_net_counter = 0
5293 for net in result:
5294 if net['sdn_net_id'] != None:
5295 sdn_net_counter+=1
5296 sdn_net_id = net['sdn_net_id']
5297
5298 if sdn_net_counter == 0:
5299 return None
5300 elif sdn_net_counter == 1:
5301 return sdn_net_id
5302 else:
5303 raise NfvoException("More than one SDN network is associated to vim network {}".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005304 network_id), httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005305
5306def get_sdn_controller_id(mydb, datacenter):
5307 # Obtain sdn controller id
5308 config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}')
5309 if not config:
5310 return None
5311
5312 return yaml.load(config).get('sdn-controller')
5313
5314def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor):
5315 try:
5316 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
5317 if not sdn_network_id:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005318 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 +02005319
5320 #Obtain sdn controller id
5321 controller_id = get_sdn_controller_id(mydb, datacenter)
5322 if not controller_id:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005323 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005324
5325 #Obtain sdn controller info
5326 sdn_controller = ovim.show_of_controller(controller_id)
5327
5328 port_data = {
5329 'name': 'external_port',
5330 'net_id': sdn_network_id,
5331 'ofc_id': controller_id,
5332 'switch_dpid': sdn_controller['dpid'],
5333 'switch_port': descriptor['port']
5334 }
5335
5336 if 'vlan' in descriptor:
5337 port_data['vlan'] = descriptor['vlan']
5338 if 'mac' in descriptor:
5339 port_data['mac'] = descriptor['mac']
5340
5341 result = ovim.new_port(port_data)
5342 except ovimException as e:
5343 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005344 sdn_network_id, network_id) + str(e), httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005345 except db_base_Exception as e:
5346 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
tierno9c5c8322018-03-23 15:44:03 +01005347 network_id) + str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005348
5349 return 'Port uuid: '+ result
5350
5351def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None):
5352 if port_id:
5353 filter = {'uuid': port_id}
5354 else:
5355 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
5356 if not sdn_network_id:
5357 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005358 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005359 #in case no port_id is specified only ports marked as 'external_port' will be detached
5360 filter = {'name': 'external_port', 'net_id': sdn_network_id}
5361
5362 try:
5363 port_list = ovim.get_ports(columns={'uuid'}, filter=filter)
5364 except ovimException as e:
5365 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005366 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005367
5368 if len(port_list) == 0:
5369 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005370 httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005371
5372 port_uuid_list = []
5373 for port in port_list:
5374 try:
5375 port_uuid_list.append(port['uuid'])
5376 ovim.delete_port(port['uuid'])
5377 except ovimException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005378 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 +02005379
5380 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list))
tiernob3d36742017-03-03 23:51:05 +01005381
tierno7edb6752016-03-21 17:37:52 +01005382def vim_action_get(mydb, tenant_id, datacenter, item, name):
5383 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005384 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005385 filter_dict={}
5386 if name:
tierno42fcc3b2016-07-06 17:20:40 +02005387 if utils.check_valid_uuid(name):
tierno7edb6752016-03-21 17:37:52 +01005388 filter_dict["id"] = name
5389 else:
5390 filter_dict["name"] = name
tiernoae4a8d12016-07-08 12:30:39 +02005391 try:
5392 if item=="networks":
5393 #filter_dict['tenant_id'] = myvim['tenant_id']
5394 content = myvim.get_network_list(filter_dict=filter_dict)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005395
5396 if len(content) == 0:
5397 raise NfvoException("Network {} is not present in the system. ".format(name),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005398 httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005399
5400 #Update the networks with the attached ports
5401 for net in content:
5402 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id'])
5403 if sdn_network_id != None:
5404 try:
5405 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
5406 port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id})
5407 except ovimException as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005408 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 +02005409 #Remove field name and if port name is external_port save it as 'type'
5410 for port in port_list:
5411 if port['name'] == 'external_port':
5412 port['type'] = "External"
5413 del port['name']
5414 net['sdn_network_id'] = sdn_network_id
5415 net['sdn_attached_ports'] = port_list
5416
tiernoae4a8d12016-07-08 12:30:39 +02005417 elif item=="tenants":
5418 content = myvim.get_tenant_list(filter_dict=filter_dict)
tierno4540ea52017-01-18 17:44:32 +01005419 elif item == "images":
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005420
tierno4540ea52017-01-18 17:44:32 +01005421 content = myvim.get_image_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02005422 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005423 raise NfvoException(item + "?", httperrors.Method_Not_Allowed)
tiernobe41e222016-09-02 15:16:13 +02005424 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
tiernoae4a8d12016-07-08 12:30:39 +02005425 if name and len(content)==1:
tiernof97fd272016-07-11 14:32:37 +02005426 return {item[:-1]: content[0]}
tiernoae4a8d12016-07-08 12:30:39 +02005427 elif name and len(content)==0:
tiernof97fd272016-07-11 14:32:37 +02005428 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 +02005429 datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02005430 else:
tiernof97fd272016-07-11 14:32:37 +02005431 return {item: content}
tiernoae4a8d12016-07-08 12:30:39 +02005432 except vimconn.vimconnException as e:
5433 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
tiernof97fd272016-07-11 14:32:37 +02005434 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
tierno42026a02017-02-10 15:13:40 +01005435
tiernob3d36742017-03-03 23:51:05 +01005436
tierno7edb6752016-03-21 17:37:52 +01005437def vim_action_delete(mydb, tenant_id, datacenter, item, name):
5438 #get datacenter info
tierno392f2852016-05-13 12:28:55 +02005439 if tenant_id == "any":
5440 tenant_id=None
5441
tiernoa2793912016-10-04 08:15:08 +00005442 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno392f2852016-05-13 12:28:55 +02005443 #get uuid name
tiernof97fd272016-07-11 14:32:37 +02005444 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
5445 logger.debug("vim_action_delete vim response: " + str(content))
tierno392f2852016-05-13 12:28:55 +02005446 items = content.values()[0]
5447 if type(items)==list and len(items)==0:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005448 raise NfvoException("Not found " + item, httperrors.Not_Found)
tierno392f2852016-05-13 12:28:55 +02005449 elif type(items)==list and len(items)>1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005450 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), httperrors.Not_Found)
tierno392f2852016-05-13 12:28:55 +02005451 else: # it is a dict
5452 item_id = items["id"]
5453 item_name = str(items.get("name"))
tierno42026a02017-02-10 15:13:40 +01005454
tiernoae4a8d12016-07-08 12:30:39 +02005455 try:
5456 if item=="networks":
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005457 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
5458 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id)
5459 if sdn_network_id != None:
5460 #Delete any port attachment to this network
5461 try:
5462 port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id})
5463 except ovimException as e:
5464 raise NfvoException(
5465 "ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005466 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005467
5468 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
5469 for port in port_list:
5470 vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid'])
5471
5472 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
5473 try:
5474 mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id})
5475 except db_base_Exception as e:
5476 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) +
tierno9c5c8322018-03-23 15:44:03 +01005477 str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005478
5479 #Delete the SDN network
5480 try:
5481 ovim.delete_network(sdn_network_id)
5482 except ovimException as e:
5483 logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True)
5484 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005485 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005486
tiernoae4a8d12016-07-08 12:30:39 +02005487 content = myvim.delete_network(item_id)
5488 elif item=="tenants":
5489 content = myvim.delete_tenant(item_id)
tierno4540ea52017-01-18 17:44:32 +01005490 elif item == "images":
5491 content = myvim.delete_image(item_id)
tiernoae4a8d12016-07-08 12:30:39 +02005492 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005493 raise NfvoException(item + "?", httperrors.Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02005494 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005495 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
5496 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005497
tiernof97fd272016-07-11 14:32:37 +02005498 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
tierno42026a02017-02-10 15:13:40 +01005499
tiernob3d36742017-03-03 23:51:05 +01005500
tierno7edb6752016-03-21 17:37:52 +01005501def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
5502 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005503 logger.debug("vim_action_create descriptor %s", str(descriptor))
tierno392f2852016-05-13 12:28:55 +02005504 if tenant_id == "any":
5505 tenant_id=None
tiernoa2793912016-10-04 08:15:08 +00005506 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02005507 try:
5508 if item=="networks":
5509 net = descriptor["network"]
5510 net_name = net.pop("name")
5511 net_type = net.pop("type", "bridge")
garciadeblas9f8456e2016-09-05 05:02:59 +02005512 net_public = net.pop("shared", False)
5513 net_ipprofile = net.pop("ip_profile", None)
tiernoa7d34d02017-02-23 14:42:07 +01005514 net_vlan = net.pop("vlan", None)
garciadeblasebd66722019-01-31 16:01:31 +00005515 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 +02005516
5517 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
5518 if get_sdn_controller_id(mydb, datacenter) != None and (net_type == 'data' or net_type == 'ptp'):
tierno00e3df72017-11-29 17:20:13 +01005519 #obtain datacenter_tenant_id
5520 datacenter_tenant_id = mydb.get_rows(SELECT=('uuid',),
5521 FROM='datacenter_tenants',
5522 WHERE={'datacenter_id': datacenter})[0]['uuid']
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005523 try:
5524 sdn_network = {}
5525 sdn_network['vlan'] = net_vlan
5526 sdn_network['type'] = net_type
5527 sdn_network['name'] = net_name
tierno00e3df72017-11-29 17:20:13 +01005528 sdn_network['region'] = datacenter_tenant_id
garciadeblasebd66722019-01-31 16:01:31 +00005529 ovim_content = ovim.new_network(sdn_network)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005530 except ovimException as e:
tierno00e3df72017-11-29 17:20:13 +01005531 logger.error("ovimException creating SDN network={} ".format(
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005532 sdn_network) + str(e), exc_info=True)
5533 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005534 httperrors.Internal_Server_Error)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005535
5536 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
5537 # use instance_scenario_id=None to distinguish from real instaces of nets
tierno00e3df72017-11-29 17:20:13 +01005538 correspondence = {'instance_scenario_id': None,
5539 'sdn_net_id': ovim_content,
5540 'vim_net_id': content,
5541 'datacenter_tenant_id': datacenter_tenant_id
5542 }
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005543 try:
5544 mydb.new_row('instance_nets', correspondence, add_uuid=True)
5545 except db_base_Exception as e:
tierno00e3df72017-11-29 17:20:13 +01005546 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format(
tierno9c5c8322018-03-23 15:44:03 +01005547 correspondence, e), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005548 elif item=="tenants":
5549 tenant = descriptor["tenant"]
5550 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
5551 else:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005552 raise NfvoException(item + "?", httperrors.Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02005553 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005554 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005555
tierno7edb6752016-03-21 17:37:52 +01005556 return vim_action_get(mydb, tenant_id, datacenter, item, content)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005557
5558def sdn_controller_create(mydb, tenant_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005559 data = ovim.new_of_controller(sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005560 logger.debug('New SDN controller created with uuid {}'.format(data))
5561 return data
5562
5563def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005564 data = ovim.edit_of_controller(controller_id, sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005565 msg = 'SDN controller {} updated'.format(data)
5566 logger.debug(msg)
5567 return msg
5568
5569def sdn_controller_list(mydb, tenant_id, controller_id=None):
5570 if controller_id == None:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005571 data = ovim.get_of_controllers()
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005572 else:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005573 data = ovim.show_of_controller(controller_id)
5574
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005575 msg = 'SDN controller list:\n {}'.format(data)
5576 logger.debug(msg)
5577 return data
5578
5579def sdn_controller_delete(mydb, tenant_id, controller_id):
5580 select_ = ('uuid', 'config')
5581 datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_)
5582 for datacenter in datacenters:
5583 if datacenter['config']:
5584 config = yaml.load(datacenter['config'])
5585 if 'sdn-controller' in config and config['sdn-controller'] == controller_id:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005586 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), httperrors.Conflict)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005587
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005588 data = ovim.delete_of_controller(controller_id)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005589 msg = 'SDN controller {} deleted'.format(data)
5590 logger.debug(msg)
5591 return msg
5592
5593def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping):
5594 controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id})
5595 if len(controller) < 1:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005596 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), httperrors.Not_Found)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005597
5598 try:
5599 sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"]
5600 except:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005601 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), httperrors.Bad_Request)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005602
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005603 sdn_controller = ovim.show_of_controller(sdn_controller_id)
5604 switch_dpid = sdn_controller["dpid"]
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005605
5606 maps = list()
5607 for compute_node in sdn_port_mapping:
5608 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
5609 element = dict()
5610 element["compute_node"] = compute_node["compute_node"]
5611 for port in compute_node["ports"]:
tierno7f426e92018-06-28 15:21:32 +02005612 pci = port.get("pci")
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005613 element["switch_port"] = port.get("switch_port")
5614 element["switch_mac"] = port.get("switch_mac")
tierno4070e442019-01-23 10:19:23 +00005615 if not element["switch_port"] and not element["switch_mac"]:
5616 raise NfvoException ("The mapping must contain 'switch_port' or 'switch_mac'", httperrors.Bad_Request)
tierno7f426e92018-06-28 15:21:32 +02005617 for pci_expanded in utils.expand_brackets(pci):
5618 element["pci"] = pci_expanded
5619 maps.append(dict(element))
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005620
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005621 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 +01005622
5623def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005624 maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id})
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005625
5626 result = {
5627 "sdn-controller": None,
5628 "datacenter-id": datacenter_id,
5629 "dpid": None,
5630 "ports_mapping": list()
5631 }
5632
5633 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id)
5634 if datacenter['config']:
5635 config = yaml.load(datacenter['config'])
5636 if 'sdn-controller' in config:
5637 controller_id = config['sdn-controller']
5638 sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id)
5639 result["sdn-controller"] = controller_id
5640 result["dpid"] = sdn_controller["dpid"]
5641
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005642 if result["sdn-controller"] == None:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005643 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), httperrors.Bad_Request)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005644 if result["dpid"] == None:
5645 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]),
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005646 httperrors.Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005647
5648 if len(maps) == 0:
5649 return result
5650
5651 ports_correspondence_dict = dict()
5652 for link in maps:
5653 if result["sdn-controller"] != link["ofc_id"]:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005654 raise NfvoException("The sdn-controller specified for different port mappings differ", httperrors.Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005655 if result["dpid"] != link["switch_dpid"]:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005656 raise NfvoException("The dpid specified for different port mappings differ", httperrors.Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005657 element = dict()
5658 element["pci"] = link["pci"]
5659 if link["switch_port"]:
5660 element["switch_port"] = link["switch_port"]
5661 if link["switch_mac"]:
5662 element["switch_mac"] = link["switch_mac"]
5663
5664 if not link["compute_node"] in ports_correspondence_dict:
5665 content = dict()
5666 content["compute_node"] = link["compute_node"]
5667 content["ports"] = list()
5668 ports_correspondence_dict[link["compute_node"]] = content
5669
5670 ports_correspondence_dict[link["compute_node"]]["ports"].append(element)
5671
5672 for key in sorted(ports_correspondence_dict):
5673 result["ports_mapping"].append(ports_correspondence_dict[key])
5674
5675 return result
5676
5677def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id):
tierno639520f2017-04-05 19:55:36 +02005678 return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id})
gcalvinoe580c7d2017-09-22 14:09:51 +02005679
5680def create_RO_keypair(tenant_id):
5681 """
5682 Creates a public / private keys for a RO tenant and returns their values
5683 Params:
5684 tenant_id: ID of the tenant
5685 Return:
5686 public_key: Public key for the RO tenant
5687 private_key: Encrypted private key for RO tenant
5688 """
5689
5690 bits = 2048
5691 key = RSA.generate(bits)
5692 try:
5693 public_key = key.publickey().exportKey('OpenSSH')
5694 if isinstance(public_key, ValueError):
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005695 raise NfvoException("Unable to create public key: {}".format(public_key), httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02005696 private_key = key.exportKey(passphrase=tenant_id, pkcs=8)
5697 except (ValueError, NameError) as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005698 raise NfvoException("Unable to create private key: {}".format(e), httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02005699 return public_key, private_key
5700
5701def decrypt_key (key, tenant_id):
5702 """
5703 Decrypts an encrypted RSA key
5704 Params:
5705 key: Private key to be decrypted
5706 tenant_id: ID of the tenant
5707 Return:
5708 unencrypted_key: Unencrypted private key for RO tenant
5709 """
5710 try:
5711 key = RSA.importKey(key,tenant_id)
5712 unencrypted_key = key.exportKey('PEM')
5713 if isinstance(unencrypted_key, ValueError):
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005714 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key), httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02005715 except ValueError as e:
Anderson Bravalheri0446cd52018-08-17 15:26:19 +01005716 raise NfvoException("Unable to decrypt the private key: {}".format(e), httperrors.Internal_Server_Error)
gcalvinoe580c7d2017-09-22 14:09:51 +02005717 return unencrypted_key